1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 /* The following definition is to workaround an issue in the Linux kernel
26 * header files with redefinition of 'struct in6_addr' in both
27 * netinet/in.h and linux/in6.h.
28 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
32 #include <net/if_arp.h>
33 #include <linux/lwtunnel.h>
34 #include <linux/mpls_iptunnel.h>
35 #include <linux/seg6_iptunnel.h>
36 #include <linux/seg6_local.h>
37 #include <linux/neighbour.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/nexthop.h>
41 /* Hack for GNU libc version 2. */
43 #define MSG_TRUNC 0x20
44 #endif /* MSG_TRUNC */
51 #include "plist_int.h"
52 #include "connected.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_ns.h"
67 #include "zebra/zebra_vrf.h"
69 #include "zebra/redistribute.h"
70 #include "zebra/interface.h"
71 #include "zebra/debug.h"
72 #include "zebra/rtadv.h"
73 #include "zebra/zebra_ptm.h"
74 #include "zebra/zebra_mpls.h"
75 #include "zebra/kernel_netlink.h"
76 #include "zebra/rt_netlink.h"
77 #include "zebra/zebra_nhg.h"
78 #include "zebra/zebra_mroute.h"
79 #include "zebra/zebra_vxlan.h"
80 #include "zebra/zebra_errors.h"
81 #include "zebra/zebra_evpn_mh.h"
87 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
88 * UAPI for MAC sync. */
89 #ifndef _UAPI_LINUX_IF_BRIDGE_H
90 #define BR_SPH_LIST_SIZE 10
93 static vlanid_t filter_vlan
= 0;
95 /* We capture whether the current kernel supports nexthop ids; by
96 * default, we'll use them if possible. There's also a configuration
97 * available to _disable_ use of kernel nexthops.
99 static bool supports_nh
;
107 static const char ipv4_ll_buf
[16] = "169.254.0.1";
108 static struct in_addr ipv4_ll
;
110 /* Is this a ipv4 over ipv6 route? */
111 static bool is_route_v4_over_v6(unsigned char rtm_family
,
112 enum nexthop_types_t nexthop_type
)
114 if (rtm_family
== AF_INET
115 && (nexthop_type
== NEXTHOP_TYPE_IPV6
116 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
122 /* Helper to control use of kernel-level nexthop ids */
123 static bool kernel_nexthops_supported(void)
125 return (supports_nh
&& !vrf_is_backend_netns()
126 && zebra_nhg_kernel_nexthops_enabled());
130 * Some people may only want to use NHGs created by protos and not
131 * implicitly created by Zebra. This check accounts for that.
133 static bool proto_nexthops_only(void)
135 return zebra_nhg_proto_nexthops_only();
138 /* Is this a proto created NHG? */
139 static bool is_proto_nhg(uint32_t id
, int type
)
141 /* If type is available, use it as the source of truth */
143 if (type
!= ZEBRA_ROUTE_NHG
)
148 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
155 * The ipv4_ll data structure is used for all 5549
156 * additions to the kernel. Let's figure out the
157 * correct value one time instead for every
158 * install/remove of a 5549 type route
160 void rt_netlink_init(void)
162 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
166 * Mapping from dataplane neighbor flags to netlink flags
168 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
172 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
173 flags
|= NTF_EXT_LEARNED
;
174 if (dplane_flags
& DPLANE_NTF_ROUTER
)
176 if (dplane_flags
& DPLANE_NTF_USE
)
183 * Mapping from dataplane neighbor state to netlink state
185 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
189 if (dplane_state
& DPLANE_NUD_REACHABLE
)
190 state
|= NUD_REACHABLE
;
191 if (dplane_state
& DPLANE_NUD_STALE
)
193 if (dplane_state
& DPLANE_NUD_NOARP
)
195 if (dplane_state
& DPLANE_NUD_PROBE
)
197 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
198 state
|= NUD_INCOMPLETE
;
199 if (dplane_state
& DPLANE_NUD_PERMANENT
)
200 state
|= NUD_PERMANENT
;
201 if (dplane_state
& DPLANE_NUD_FAILED
)
208 static inline bool is_selfroute(int proto
)
210 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
211 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
212 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
213 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
214 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
215 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
216 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
217 || (proto
== RTPROT_SRTE
)) {
224 static inline int zebra2proto(int proto
)
227 case ZEBRA_ROUTE_BABEL
:
228 proto
= RTPROT_BABEL
;
230 case ZEBRA_ROUTE_BGP
:
233 case ZEBRA_ROUTE_OSPF
:
234 case ZEBRA_ROUTE_OSPF6
:
237 case ZEBRA_ROUTE_STATIC
:
238 proto
= RTPROT_ZSTATIC
;
240 case ZEBRA_ROUTE_ISIS
:
243 case ZEBRA_ROUTE_RIP
:
246 case ZEBRA_ROUTE_RIPNG
:
247 proto
= RTPROT_RIPNG
;
249 case ZEBRA_ROUTE_NHRP
:
252 case ZEBRA_ROUTE_EIGRP
:
253 proto
= RTPROT_EIGRP
;
255 case ZEBRA_ROUTE_LDP
:
258 case ZEBRA_ROUTE_SHARP
:
259 proto
= RTPROT_SHARP
;
261 case ZEBRA_ROUTE_PBR
:
264 case ZEBRA_ROUTE_OPENFABRIC
:
265 proto
= RTPROT_OPENFABRIC
;
267 case ZEBRA_ROUTE_SRTE
:
270 case ZEBRA_ROUTE_TABLE
:
271 case ZEBRA_ROUTE_NHG
:
272 proto
= RTPROT_ZEBRA
;
274 case ZEBRA_ROUTE_CONNECT
:
275 case ZEBRA_ROUTE_KERNEL
:
276 proto
= RTPROT_KERNEL
;
280 * When a user adds a new protocol this will show up
281 * to let them know to do something about it. This
282 * is intentionally a warn because we should see
283 * this as part of development of a new protocol
286 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
288 proto
= RTPROT_ZEBRA
;
295 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
299 proto
= ZEBRA_ROUTE_BABEL
;
302 proto
= ZEBRA_ROUTE_BGP
;
305 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
309 proto
= ZEBRA_ROUTE_ISIS
;
312 proto
= ZEBRA_ROUTE_RIP
;
315 proto
= ZEBRA_ROUTE_RIPNG
;
318 proto
= ZEBRA_ROUTE_NHRP
;
321 proto
= ZEBRA_ROUTE_EIGRP
;
324 proto
= ZEBRA_ROUTE_LDP
;
328 proto
= ZEBRA_ROUTE_STATIC
;
331 proto
= ZEBRA_ROUTE_SHARP
;
334 proto
= ZEBRA_ROUTE_PBR
;
336 case RTPROT_OPENFABRIC
:
337 proto
= ZEBRA_ROUTE_OPENFABRIC
;
340 proto
= ZEBRA_ROUTE_SRTE
;
344 proto
= ZEBRA_ROUTE_NHG
;
347 /* Intentional fall thru */
350 * When a user adds a new protocol this will show up
351 * to let them know to do something about it. This
352 * is intentionally a warn because we should see
353 * this as part of development of a new protocol
356 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
358 proto
= ZEBRA_ROUTE_KERNEL
;
365 Pending: create an efficient table_id (in a tree/hash) based lookup)
367 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
370 struct zebra_vrf
*zvrf
;
372 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
376 /* case vrf with netns : match the netnsid */
377 if (vrf_is_backend_netns()) {
378 if (ns_id
== zvrf_id(zvrf
))
379 return zvrf_id(zvrf
);
381 /* VRF is VRF_BACKEND_VRF_LITE */
382 if (zvrf
->table_id
!= table_id
)
384 return zvrf_id(zvrf
);
392 * @parse_encap_mpls() - Parses encapsulated mpls attributes
393 * @tb: Pointer to rtattr to look for nested items in.
394 * @labels: Pointer to store labels in.
396 * Return: Number of mpls labels found.
398 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
400 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
401 mpls_lse_t
*lses
= NULL
;
406 mpls_label_t label
= 0;
408 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
409 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
410 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
411 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
412 labels
[num_labels
++] = label
;
418 static enum seg6local_action_t
419 parse_encap_seg6local(struct rtattr
*tb
,
420 struct seg6local_context
*ctx
)
422 struct rtattr
*tb_encap
[256] = {};
423 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
425 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
427 if (tb_encap
[SEG6_LOCAL_ACTION
])
428 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
430 if (tb_encap
[SEG6_LOCAL_NH4
])
431 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
432 tb_encap
[SEG6_LOCAL_NH4
]);
434 if (tb_encap
[SEG6_LOCAL_NH6
])
435 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
436 tb_encap
[SEG6_LOCAL_NH6
]);
438 if (tb_encap
[SEG6_LOCAL_TABLE
])
439 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
441 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
443 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
448 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
450 struct rtattr
*tb_encap
[256] = {};
451 struct seg6_iptunnel_encap
*ipt
= NULL
;
452 struct in6_addr
*segments
= NULL
;
454 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
457 * TODO: It's not support multiple SID list.
459 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
460 ipt
= (struct seg6_iptunnel_encap
*)
461 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
462 segments
= ipt
->srh
[0].segments
;
471 static struct nexthop
472 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
473 enum blackhole_type bh_type
, int index
, void *prefsrc
,
474 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
476 struct interface
*ifp
= NULL
;
477 struct nexthop nh
= {0};
478 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
480 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
481 struct seg6local_context seg6l_ctx
= {};
482 struct in6_addr seg6_segs
= {};
485 vrf_id_t nh_vrf_id
= vrf_id
;
486 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
488 if (bh_type
== BLACKHOLE_UNSPEC
) {
490 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
491 else if (index
&& gate
)
492 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
493 : NEXTHOP_TYPE_IPV6_IFINDEX
;
494 else if (!index
&& gate
)
495 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
498 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
499 nh
.bh_type
= bh_type
;
502 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
503 nh
.bh_type
= bh_type
;
507 memcpy(&nh
.src
, prefsrc
, sz
);
509 memcpy(&nh
.gate
, gate
, sz
);
512 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
514 nh_vrf_id
= ifp
->vrf_id
;
516 nh
.vrf_id
= nh_vrf_id
;
518 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
519 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
520 == LWTUNNEL_ENCAP_MPLS
) {
521 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
523 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
524 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
525 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
526 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
528 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
529 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
530 == LWTUNNEL_ENCAP_SEG6
) {
531 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
534 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
535 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
538 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
540 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
541 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
544 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
549 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
550 struct nexthop_group
*ng
,
552 struct rtnexthop
*rtnh
,
554 void *prefsrc
, vrf_id_t vrf_id
)
557 struct interface
*ifp
= NULL
;
560 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
562 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
563 struct seg6local_context seg6l_ctx
= {};
564 struct in6_addr seg6_segs
= {};
566 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
568 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
569 vrf_id_t nh_vrf_id
= vrf_id
;
572 struct nexthop
*nh
= NULL
;
574 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
577 index
= rtnh
->rtnh_ifindex
;
580 * Yes we are looking this up
581 * for every nexthop and just
582 * using the last one looked
585 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
588 nh_vrf_id
= ifp
->vrf_id
;
591 EC_ZEBRA_UNKNOWN_INTERFACE
,
592 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
594 nh_vrf_id
= VRF_DEFAULT
;
599 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
600 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
601 rtnh
->rtnh_len
- sizeof(*rtnh
));
602 if (rtnh_tb
[RTA_GATEWAY
])
603 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
604 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
605 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
606 == LWTUNNEL_ENCAP_MPLS
) {
607 num_labels
= parse_encap_mpls(
608 rtnh_tb
[RTA_ENCAP
], labels
);
610 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
611 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
612 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
613 seg6l_act
= parse_encap_seg6local(
614 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
616 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
617 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
618 == LWTUNNEL_ENCAP_SEG6
) {
619 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
624 if (gate
&& rtm
->rtm_family
== AF_INET
) {
626 nh
= nexthop_from_ipv4_ifindex(
627 gate
, prefsrc
, index
, nh_vrf_id
);
629 nh
= nexthop_from_ipv4(gate
, prefsrc
,
631 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
633 nh
= nexthop_from_ipv6_ifindex(
634 gate
, index
, nh_vrf_id
);
636 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
638 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
641 nh
->weight
= rtnh
->rtnh_hops
+ 1;
644 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
647 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
648 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
652 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
654 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
655 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
657 /* Add to temporary list */
658 nexthop_group_add_sorted(ng
, nh
);
661 if (rtnh
->rtnh_len
== 0)
664 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
665 rtnh
= RTNH_NEXT(rtnh
);
668 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
673 /* Looking up routing table by netlink interface. */
674 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
679 struct rtattr
*tb
[RTA_MAX
+ 1];
682 struct prefix_ipv6 src_p
= {};
686 char anyaddr
[16] = {0};
688 int proto
= ZEBRA_ROUTE_KERNEL
;
693 uint8_t distance
= 0;
699 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
700 void *src
= NULL
; /* IPv6 srcdest source prefix */
701 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
705 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
707 switch (rtm
->rtm_type
) {
711 bh_type
= BLACKHOLE_NULL
;
713 case RTN_UNREACHABLE
:
714 bh_type
= BLACKHOLE_REJECT
;
717 bh_type
= BLACKHOLE_ADMINPROHIB
;
720 if (IS_ZEBRA_DEBUG_KERNEL
)
721 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
722 nl_rttype_to_str(rtm
->rtm_type
),
727 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
730 "%s: Message received from netlink is of a broken size %d %zu",
731 __func__
, h
->nlmsg_len
,
732 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
736 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
738 if (rtm
->rtm_flags
& RTM_F_CLONED
)
740 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
742 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
745 selfroute
= is_selfroute(rtm
->rtm_protocol
);
747 if (!startup
&& selfroute
748 && h
->nlmsg_type
== RTM_NEWROUTE
749 && !zrouter
.asic_offloaded
) {
750 if (IS_ZEBRA_DEBUG_KERNEL
)
751 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
756 /* We don't care about change notifications for the MPLS table. */
757 /* TODO: Revisit this. */
758 if (rtm
->rtm_family
== AF_MPLS
)
761 /* Table corresponding to route. */
763 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
765 table
= rtm
->rtm_table
;
768 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
769 if (vrf_id
== VRF_DEFAULT
) {
770 if (!is_zebra_valid_kernel_table(table
)
771 && !is_zebra_main_routing_table(table
))
775 if (rtm
->rtm_flags
& RTM_F_TRAP
)
776 flags
|= ZEBRA_FLAG_TRAPPED
;
777 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
778 flags
|= ZEBRA_FLAG_OFFLOADED
;
779 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
780 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
782 /* Route which inserted by Zebra. */
784 flags
|= ZEBRA_FLAG_SELFROUTE
;
785 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
788 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
791 dest
= RTA_DATA(tb
[RTA_DST
]);
796 src
= RTA_DATA(tb
[RTA_SRC
]);
801 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
804 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
807 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
809 if (tb
[RTA_PRIORITY
])
810 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
812 #if defined(SUPPORT_REALMS)
814 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
817 if (tb
[RTA_METRICS
]) {
818 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
820 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
821 RTA_PAYLOAD(tb
[RTA_METRICS
]));
824 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
827 if (rtm
->rtm_family
== AF_INET
) {
829 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
831 "Invalid destination prefix length: %u received from kernel route change",
835 memcpy(&p
.u
.prefix4
, dest
, 4);
836 p
.prefixlen
= rtm
->rtm_dst_len
;
838 if (rtm
->rtm_src_len
!= 0) {
840 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
841 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
846 /* Force debug below to not display anything for source */
848 } else if (rtm
->rtm_family
== AF_INET6
) {
850 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
852 "Invalid destination prefix length: %u received from kernel route change",
856 memcpy(&p
.u
.prefix6
, dest
, 16);
857 p
.prefixlen
= rtm
->rtm_dst_len
;
859 src_p
.family
= AF_INET6
;
860 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
862 "Invalid source prefix length: %u received from kernel route change",
866 memcpy(&src_p
.prefix
, src
, 16);
867 src_p
.prefixlen
= rtm
->rtm_src_len
;
869 /* We only handle the AFs we handle... */
870 if (IS_ZEBRA_DEBUG_KERNEL
)
871 zlog_debug("%s: unknown address-family %u", __func__
,
877 * For ZEBRA_ROUTE_KERNEL types:
879 * The metric/priority of the route received from the kernel
880 * is a 32 bit number. We are going to interpret the high
881 * order byte as the Admin Distance and the low order 3 bytes
884 * This will allow us to do two things:
885 * 1) Allow the creation of kernel routes that can be
886 * overridden by zebra.
887 * 2) Allow the old behavior for 'most' kernel route types
888 * if a user enters 'ip route ...' v4 routes get a metric
889 * of 0 and v6 routes get a metric of 1024. Both of these
890 * values will end up with a admin distance of 0, which
891 * will cause them to win for the purposes of zebra.
893 if (proto
== ZEBRA_ROUTE_KERNEL
) {
894 distance
= (metric
>> 24) & 0xFF;
895 metric
= (metric
& 0x00FFFFFF);
898 if (IS_ZEBRA_DEBUG_KERNEL
) {
899 char buf2
[PREFIX_STRLEN
];
902 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
903 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
904 src_p
.prefixlen
? " from " : "",
905 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
907 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
912 if (rtm
->rtm_family
== AF_INET6
)
915 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
917 if (!tb
[RTA_MULTIPATH
]) {
918 struct nexthop nh
= {0};
921 nh
= parse_nexthop_unicast(
922 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
925 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
926 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
929 /* This is a multipath route */
930 struct route_entry
*re
;
931 struct nexthop_group
*ng
= NULL
;
932 struct rtnexthop
*rtnh
=
933 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
935 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
937 re
->distance
= distance
;
943 re
->uptime
= monotime(NULL
);
950 /* Use temporary list of nexthops; parse
951 * message payload's nexthops.
953 ng
= nexthop_group_new();
955 parse_multipath_nexthops_unicast(
956 ns_id
, ng
, rtm
, rtnh
, tb
,
959 zserv_nexthop_num_warn(
960 __func__
, (const struct prefix
*)&p
,
964 nexthop_group_delete(&ng
);
970 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
977 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
978 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
981 if (!tb
[RTA_MULTIPATH
]) {
984 nh
= parse_nexthop_unicast(
985 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
987 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
988 flags
, &p
, &src_p
, &nh
, 0, table
,
989 metric
, distance
, true);
991 /* XXX: need to compare the entire list of
992 * nexthops here for NLM_F_APPEND stupidity */
993 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
994 flags
, &p
, &src_p
, NULL
, 0, table
,
995 metric
, distance
, true);
1003 static struct mcast_route_data
*mroute
= NULL
;
1005 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1006 ns_id_t ns_id
, int startup
)
1010 struct rtattr
*tb
[RTA_MAX
+ 1];
1011 struct mcast_route_data
*m
;
1012 struct mcast_route_data mr
;
1017 char oif_list
[256] = "\0";
1024 memset(&mr
, 0, sizeof(mr
));
1028 rtm
= NLMSG_DATA(h
);
1030 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1032 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1035 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1037 table
= rtm
->rtm_table
;
1039 vrf
= vrf_lookup_by_table(table
, ns_id
);
1042 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1045 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1048 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1050 if (tb
[RTA_EXPIRES
])
1051 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1053 if (tb
[RTA_MULTIPATH
]) {
1054 struct rtnexthop
*rtnh
=
1055 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1057 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1059 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1062 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1065 if (rtnh
->rtnh_len
== 0)
1068 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1069 rtnh
= RTNH_NEXT(rtnh
);
1073 if (IS_ZEBRA_DEBUG_KERNEL
) {
1074 struct interface
*ifp
= NULL
;
1075 struct zebra_vrf
*zvrf
= NULL
;
1077 for (count
= 0; count
< oif_count
; count
++) {
1078 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1081 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1082 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1083 strlcat(oif_list
, temp
, sizeof(oif_list
));
1085 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1086 ifp
= if_lookup_by_index(iif
, vrf
);
1088 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
1089 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1090 &m
->sg
.src
, &m
->sg
.grp
, ifp
? ifp
->name
: "Unknown",
1097 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1102 rtm
= NLMSG_DATA(h
);
1104 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1105 /* If this is not route add/delete message print warning. */
1106 zlog_debug("Kernel message: %s NS %u",
1107 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1111 if (!(rtm
->rtm_family
== AF_INET
||
1112 rtm
->rtm_family
== AF_INET6
||
1113 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1115 EC_ZEBRA_UNKNOWN_FAMILY
,
1116 "Invalid address family: %u received from kernel route change: %s",
1117 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1121 /* Connected route. */
1122 if (IS_ZEBRA_DEBUG_KERNEL
)
1123 zlog_debug("%s %s %s proto %s NS %u",
1124 nl_msg_type_to_str(h
->nlmsg_type
),
1125 nl_family_to_str(rtm
->rtm_family
),
1126 nl_rttype_to_str(rtm
->rtm_type
),
1127 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1130 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1133 "%s: Message received from netlink is of a broken size: %d %zu",
1134 __func__
, h
->nlmsg_len
,
1135 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1139 if (rtm
->rtm_type
== RTN_MULTICAST
)
1140 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1142 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1146 /* Request for specific route information from the kernel */
1147 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1154 /* Form the request, specifying filter (rtattr) if needed. */
1155 memset(&req
, 0, sizeof(req
));
1156 req
.n
.nlmsg_type
= type
;
1157 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1158 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1159 req
.rtm
.rtm_family
= family
;
1161 return netlink_request(&zns
->netlink_cmd
, &req
);
1164 /* Routing table read function using netlink interface. Only called
1166 int netlink_route_read(struct zebra_ns
*zns
)
1169 struct zebra_dplane_info dp_info
;
1171 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1173 /* Get IPv4 routing table. */
1174 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1177 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1178 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1182 /* Get IPv6 routing table. */
1183 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1186 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1187 &zns
->netlink_cmd
, &dp_info
, 0, 1);
1195 * The function returns true if the gateway info could be added
1196 * to the message, otherwise false is returned.
1198 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1200 struct nlmsghdr
*nlmsg
,
1201 size_t req_size
, int bytelen
,
1202 const struct nexthop
*nexthop
)
1204 if (route_family
== AF_MPLS
) {
1205 struct gw_family_t gw_fam
;
1207 gw_fam
.family
= gw_family
;
1208 if (gw_family
== AF_INET
)
1209 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1211 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1212 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1216 if (!(nexthop
->rparent
1217 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1218 if (gw_family
== AF_INET
) {
1219 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1220 &nexthop
->gate
.ipv4
, bytelen
))
1223 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1224 &nexthop
->gate
.ipv6
, bytelen
))
1233 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1234 mpls_lse_t
*out_lse
, char *label_buf
,
1235 size_t label_buf_size
)
1237 char label_buf1
[20];
1240 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1241 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1244 if (IS_ZEBRA_DEBUG_KERNEL
) {
1246 snprintf(label_buf
, label_buf_size
, "label %u",
1247 nh_label
->label
[i
]);
1249 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1250 nh_label
->label
[i
]);
1251 strlcat(label_buf
, label_buf1
, label_buf_size
);
1255 out_lse
[num_labels
] =
1256 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1263 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1264 struct nlmsghdr
*nlmsg
,
1265 size_t buflen
, struct rtmsg
*rtmsg
,
1267 size_t label_buf_size
)
1269 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1273 * label_buf is *only* currently used within debugging.
1274 * As such when we assign it we are guarding it inside
1275 * a debug test. If you want to change this make sure
1276 * you fix this assumption
1278 label_buf
[0] = '\0';
1281 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1284 /* Set the BoS bit */
1285 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1287 if (rtmsg
->rtm_family
== AF_MPLS
) {
1288 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1289 num_labels
* sizeof(mpls_lse_t
)))
1292 struct rtattr
*nest
;
1294 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1295 LWTUNNEL_ENCAP_MPLS
))
1298 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1302 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1304 num_labels
* sizeof(mpls_lse_t
)))
1306 nl_attr_nest_end(nlmsg
, nest
);
1313 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1315 struct nlmsghdr
*nlmsg
,
1316 size_t buflen
, int bytelen
)
1318 if (family
== AF_INET
) {
1319 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1320 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1321 &nexthop
->rmap_src
.ipv4
, bytelen
))
1323 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1324 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1325 &nexthop
->src
.ipv4
, bytelen
))
1328 } else if (family
== AF_INET6
) {
1329 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1330 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1331 &nexthop
->rmap_src
.ipv6
, bytelen
))
1333 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1334 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1335 &nexthop
->src
.ipv6
, bytelen
))
1343 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1344 const struct in6_addr
*seg
)
1346 struct seg6_iptunnel_encap
*ipt
;
1347 struct ipv6_sr_hdr
*srh
;
1348 const size_t srhlen
= 24;
1351 * Caution: Support only SINGLE-SID, not MULTI-SID
1352 * This function only supports the case where segs represents
1353 * a single SID. If you want to extend the SRv6 functionality,
1354 * you should improve the Boundary Check.
1355 * Ex. In case of set a SID-List include multiple-SIDs as an
1356 * argument of the Transit Behavior, we must support variable
1357 * boundary check for buflen.
1359 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1360 sizeof(struct ipv6_sr_hdr
) + 16))
1363 memset(buffer
, 0, buflen
);
1365 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1366 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1368 srh
->hdrlen
= (srhlen
>> 3) - 1;
1370 srh
->segments_left
= 0;
1371 srh
->first_segment
= 0;
1372 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1377 /* This function takes a nexthop as argument and adds
1378 * the appropriate netlink attributes to an existing
1381 * @param routedesc: Human readable description of route type
1382 * (direct/recursive, single-/multipath)
1383 * @param bytelen: Length of addresses in bytes.
1384 * @param nexthop: Nexthop information
1385 * @param nlmsg: nlmsghdr structure to fill in.
1386 * @param req_size: The size allocated for the message.
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_singlepath(const struct prefix
*p
,
1392 const char *routedesc
, int bytelen
,
1393 const struct nexthop
*nexthop
,
1394 struct nlmsghdr
*nlmsg
,
1395 struct rtmsg
*rtmsg
,
1396 size_t req_size
, int cmd
)
1399 char label_buf
[256];
1401 char addrstr
[INET6_ADDRSTRLEN
];
1405 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1407 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1408 req_size
, rtmsg
, label_buf
,
1412 if (nexthop
->nh_srv6
) {
1413 if (nexthop
->nh_srv6
->seg6local_action
!=
1414 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1415 struct rtattr
*nest
;
1416 const struct seg6local_context
*ctx
;
1418 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1419 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1420 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1423 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1427 switch (nexthop
->nh_srv6
->seg6local_action
) {
1428 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1429 if (!nl_attr_put32(nlmsg
, req_size
,
1431 SEG6_LOCAL_ACTION_END
))
1434 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1435 if (!nl_attr_put32(nlmsg
, req_size
,
1437 SEG6_LOCAL_ACTION_END_X
))
1439 if (!nl_attr_put(nlmsg
, req_size
,
1440 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1441 sizeof(struct in6_addr
)))
1444 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1445 if (!nl_attr_put32(nlmsg
, req_size
,
1447 SEG6_LOCAL_ACTION_END_T
))
1449 if (!nl_attr_put32(nlmsg
, req_size
,
1454 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1455 if (!nl_attr_put32(nlmsg
, req_size
,
1457 SEG6_LOCAL_ACTION_END_DX4
))
1459 if (!nl_attr_put(nlmsg
, req_size
,
1460 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1461 sizeof(struct in_addr
)))
1464 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1465 if (!nl_attr_put32(nlmsg
, req_size
,
1467 SEG6_LOCAL_ACTION_END_DT6
))
1469 if (!nl_attr_put32(nlmsg
, req_size
,
1474 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1475 if (!nl_attr_put32(nlmsg
, req_size
,
1477 SEG6_LOCAL_ACTION_END_DT4
))
1479 if (!nl_attr_put32(nlmsg
, req_size
,
1480 SEG6_LOCAL_VRFTABLE
,
1485 zlog_err("%s: unsupport seg6local behaviour action=%u",
1487 nexthop
->nh_srv6
->seg6local_action
);
1490 nl_attr_nest_end(nlmsg
, nest
);
1493 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1496 struct rtattr
*nest
;
1498 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1499 LWTUNNEL_ENCAP_SEG6
))
1501 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1504 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1505 &nexthop
->nh_srv6
->seg6_segs
);
1508 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1511 nl_attr_nest_end(nlmsg
, nest
);
1515 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1516 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1518 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1519 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1520 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1522 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1525 if (cmd
== RTM_NEWROUTE
) {
1526 if (!_netlink_route_encode_nexthop_src(
1527 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1531 if (IS_ZEBRA_DEBUG_KERNEL
)
1532 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1533 __func__
, routedesc
, p
, ipv4_ll_buf
,
1534 label_buf
, nexthop
->ifindex
,
1535 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1539 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1540 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1541 /* Send deletes to the kernel without specifying the next-hop */
1542 if (cmd
!= RTM_DELROUTE
) {
1543 if (!_netlink_route_add_gateway_info(
1544 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1549 if (cmd
== RTM_NEWROUTE
) {
1550 if (!_netlink_route_encode_nexthop_src(
1551 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1555 if (IS_ZEBRA_DEBUG_KERNEL
) {
1556 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1558 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1559 __func__
, routedesc
, p
, addrstr
, label_buf
,
1560 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1565 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1566 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1567 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1568 AF_INET6
, nlmsg
, req_size
,
1572 if (cmd
== RTM_NEWROUTE
) {
1573 if (!_netlink_route_encode_nexthop_src(
1574 nexthop
, AF_INET6
, nlmsg
, req_size
,
1579 if (IS_ZEBRA_DEBUG_KERNEL
) {
1580 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1582 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1583 __func__
, routedesc
, p
, addrstr
, label_buf
,
1584 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1590 * We have the ifindex so we should always send it
1591 * This is especially useful if we are doing route
1594 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1595 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1599 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1600 if (cmd
== RTM_NEWROUTE
) {
1601 if (!_netlink_route_encode_nexthop_src(
1602 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1606 if (IS_ZEBRA_DEBUG_KERNEL
)
1607 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1608 __func__
, routedesc
, p
, nexthop
->ifindex
,
1609 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1615 /* This function takes a nexthop as argument and
1616 * appends to the given netlink msg. If the nexthop
1617 * defines a preferred source, the src parameter
1618 * will be modified to point to that src, otherwise
1619 * it will be kept unmodified.
1621 * @param routedesc: Human readable description of route type
1622 * (direct/recursive, single-/multipath)
1623 * @param bytelen: Length of addresses in bytes.
1624 * @param nexthop: Nexthop information
1625 * @param nlmsg: nlmsghdr structure to fill in.
1626 * @param req_size: The size allocated for the message.
1627 * @param src: pointer pointing to a location where
1628 * the prefsrc should be stored.
1630 * The function returns true if the nexthop could be added
1631 * to the message, otherwise false is returned.
1633 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1634 const char *routedesc
, int bytelen
,
1635 const struct nexthop
*nexthop
,
1636 struct nlmsghdr
*nlmsg
,
1637 size_t req_size
, struct rtmsg
*rtmsg
,
1638 const union g_addr
**src
)
1640 char label_buf
[256];
1642 struct rtnexthop
*rtnh
;
1644 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1650 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1652 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1653 req_size
, rtmsg
, label_buf
,
1657 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1658 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1660 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1661 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1662 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1664 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1665 if (nexthop
->weight
)
1666 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1668 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1669 *src
= &nexthop
->rmap_src
;
1670 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1671 *src
= &nexthop
->src
;
1673 if (IS_ZEBRA_DEBUG_KERNEL
)
1675 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1676 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1677 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1679 nl_attr_rtnh_end(nlmsg
, rtnh
);
1683 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1684 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1685 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1686 nlmsg
, req_size
, bytelen
,
1690 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1691 *src
= &nexthop
->rmap_src
;
1692 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1693 *src
= &nexthop
->src
;
1695 if (IS_ZEBRA_DEBUG_KERNEL
)
1696 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1697 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1698 label_buf
, nexthop
->ifindex
,
1699 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1701 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1702 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1703 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1704 AF_INET6
, nlmsg
, req_size
,
1708 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1709 *src
= &nexthop
->rmap_src
;
1710 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1711 *src
= &nexthop
->src
;
1713 if (IS_ZEBRA_DEBUG_KERNEL
)
1714 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1715 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1716 label_buf
, nexthop
->ifindex
,
1717 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1721 * We have figured out the ifindex so we should always send it
1722 * This is especially useful if we are doing route
1725 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1726 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1729 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1730 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1731 *src
= &nexthop
->rmap_src
;
1732 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1733 *src
= &nexthop
->src
;
1735 if (IS_ZEBRA_DEBUG_KERNEL
)
1736 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1737 __func__
, routedesc
, p
, nexthop
->ifindex
,
1738 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1741 if (nexthop
->weight
)
1742 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1744 nl_attr_rtnh_end(nlmsg
, rtnh
);
1749 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1750 const struct zebra_nhlfe
*nhlfe
,
1751 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1752 size_t req_size
, int cmd
)
1757 family
= NHLFE_FAMILY(nhlfe
);
1758 bytelen
= (family
== AF_INET
? 4 : 16);
1759 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1760 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1766 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1767 const struct zebra_nhlfe
*nhlfe
,
1768 struct nlmsghdr
*nlmsg
, size_t req_size
,
1769 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1774 family
= NHLFE_FAMILY(nhlfe
);
1775 bytelen
= (family
== AF_INET
? 4 : 16);
1776 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1777 nhlfe
->nexthop
, nlmsg
, req_size
,
1781 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1783 if (IS_ZEBRA_DEBUG_KERNEL
)
1784 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1785 routedesc
, nl_msg_type_to_str(cmd
), label
);
1788 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1789 int llalen
, ns_id_t ns_id
, uint8_t family
,
1790 bool permanent
, uint8_t protocol
)
1798 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1800 memset(&req
, 0, sizeof(req
));
1802 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1803 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1804 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1805 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1807 req
.ndm
.ndm_family
= family
;
1808 req
.ndm
.ndm_ifindex
= ifindex
;
1809 req
.ndm
.ndm_type
= RTN_UNICAST
;
1810 if (cmd
== RTM_NEWNEIGH
) {
1812 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1814 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1816 req
.ndm
.ndm_state
= NUD_FAILED
;
1818 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1820 req
.ndm
.ndm_type
= RTN_UNICAST
;
1821 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
1822 family2addrsize(family
));
1824 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1826 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1830 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1833 if (family
== AF_INET
) {
1834 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1835 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1837 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1838 src
->ipv4
= nexthop
->src
.ipv4
;
1841 } else if (family
== AF_INET6
) {
1842 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1843 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1845 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1846 src
->ipv6
= nexthop
->src
.ipv6
;
1855 * The function returns true if the attribute could be added
1856 * to the message, otherwise false is returned.
1858 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1861 struct rtattr
*nest
;
1863 switch (nh
->nh_encap_type
) {
1865 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1868 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1872 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1875 nl_attr_nest_end(n
, nest
);
1883 * Routing table change via netlink interface, using a dataplane context object
1885 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1886 * otherwise the number of bytes written to buf.
1888 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1889 struct zebra_dplane_ctx
*ctx
,
1890 uint8_t *data
, size_t datalen
,
1891 bool fpm
, bool force_nhg
)
1894 struct nexthop
*nexthop
= NULL
;
1895 unsigned int nexthop_num
;
1896 const char *routedesc
;
1897 bool setsrc
= false;
1899 const struct prefix
*p
, *src_p
;
1906 } *req
= (void *)data
;
1908 p
= dplane_ctx_get_dest(ctx
);
1909 src_p
= dplane_ctx_get_src(ctx
);
1911 if (datalen
< sizeof(*req
))
1914 memset(req
, 0, sizeof(*req
));
1916 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1918 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1919 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1921 if ((cmd
== RTM_NEWROUTE
) &&
1922 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1923 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1925 req
->n
.nlmsg_type
= cmd
;
1927 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
1929 req
->r
.rtm_family
= p
->family
;
1930 req
->r
.rtm_dst_len
= p
->prefixlen
;
1931 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1932 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1934 if (cmd
== RTM_DELROUTE
)
1935 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1937 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1940 * blackhole routes are not RTN_UNICAST, they are
1941 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1942 * so setting this value as a RTN_UNICAST would
1943 * cause the route lookup of just the prefix
1944 * to fail. So no need to specify this for
1945 * the RTM_DELROUTE case
1947 if (cmd
!= RTM_DELROUTE
)
1948 req
->r
.rtm_type
= RTN_UNICAST
;
1950 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1953 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1959 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1961 * either by the kernel or by zebra. Its purely for calculating best
1963 * by the routing protocol and for communicating with protocol peers.
1965 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1966 NL_DEFAULT_ROUTE_METRIC
))
1969 #if defined(SUPPORT_REALMS)
1973 if (cmd
== RTM_DELROUTE
)
1974 tag
= dplane_ctx_get_old_tag(ctx
);
1976 tag
= dplane_ctx_get_tag(ctx
);
1978 if (tag
> 0 && tag
<= 255) {
1979 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1984 /* Table corresponding to this route. */
1985 table_id
= dplane_ctx_get_table(ctx
);
1987 req
->r
.rtm_table
= table_id
;
1989 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1990 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1994 if (IS_ZEBRA_DEBUG_KERNEL
)
1996 "%s: %s %pFX vrf %u(%u)", __func__
,
1997 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2001 * If we are not updating the route and we have received
2002 * a route delete, then all we need to fill in is the
2003 * prefix information to tell the kernel to schwack
2006 if (cmd
== RTM_DELROUTE
)
2007 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2009 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2010 struct rtattr
*nest
;
2011 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2012 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2014 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2017 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2021 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2023 nl_attr_nest_end(&req
->n
, nest
);
2027 * Always install blackhole routes without using nexthops, because of
2028 * the following kernel problems:
2029 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2030 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2032 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2034 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2035 nexthop
= nexthop
->resolved
;
2037 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2038 switch (nexthop
->bh_type
) {
2039 case BLACKHOLE_ADMINPROHIB
:
2040 req
->r
.rtm_type
= RTN_PROHIBIT
;
2042 case BLACKHOLE_REJECT
:
2043 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2046 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2049 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2053 if ((!fpm
&& kernel_nexthops_supported()
2054 && (!proto_nexthops_only()
2055 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2056 || (fpm
&& force_nhg
)) {
2057 /* Kernel supports nexthop objects */
2058 if (IS_ZEBRA_DEBUG_KERNEL
)
2059 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2060 dplane_ctx_get_nhe_id(ctx
));
2062 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2063 dplane_ctx_get_nhe_id(ctx
)))
2066 /* Have to determine src still */
2067 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2071 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2075 if (p
->family
== AF_INET
) {
2076 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2077 &src
.ipv4
, bytelen
))
2079 } else if (p
->family
== AF_INET6
) {
2080 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2081 &src
.ipv6
, bytelen
))
2086 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2089 /* Count overall nexthops so we can decide whether to use singlepath
2090 * or multipath case.
2093 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2094 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2096 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2102 /* Singlepath case. */
2103 if (nexthop_num
== 1) {
2105 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2106 if (CHECK_FLAG(nexthop
->flags
,
2107 NEXTHOP_FLAG_RECURSIVE
)) {
2112 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2117 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2118 routedesc
= nexthop
->rparent
2119 ? "recursive, single-path"
2122 if (!_netlink_route_build_singlepath(
2123 p
, routedesc
, bytelen
, nexthop
,
2124 &req
->n
, &req
->r
, datalen
, cmd
))
2131 * Add encapsulation information when installing via
2135 if (!netlink_route_nexthop_encap(
2136 &req
->n
, datalen
, nexthop
))
2142 if (p
->family
== AF_INET
) {
2143 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2144 &src
.ipv4
, bytelen
))
2146 } else if (p
->family
== AF_INET6
) {
2147 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2148 &src
.ipv6
, bytelen
))
2152 } else { /* Multipath case */
2153 struct rtattr
*nest
;
2154 const union g_addr
*src1
= NULL
;
2156 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2161 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2162 if (CHECK_FLAG(nexthop
->flags
,
2163 NEXTHOP_FLAG_RECURSIVE
)) {
2164 /* This only works for IPv4 now */
2168 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2173 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2174 routedesc
= nexthop
->rparent
2175 ? "recursive, multipath"
2179 if (!_netlink_route_build_multipath(
2180 p
, routedesc
, bytelen
, nexthop
,
2181 &req
->n
, datalen
, &req
->r
, &src1
))
2184 if (!setsrc
&& src1
) {
2185 if (p
->family
== AF_INET
)
2186 src
.ipv4
= src1
->ipv4
;
2187 else if (p
->family
== AF_INET6
)
2188 src
.ipv6
= src1
->ipv6
;
2195 nl_attr_nest_end(&req
->n
, nest
);
2198 * Add encapsulation information when installing via
2202 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2204 if (CHECK_FLAG(nexthop
->flags
,
2205 NEXTHOP_FLAG_RECURSIVE
))
2207 if (!netlink_route_nexthop_encap(
2208 &req
->n
, datalen
, nexthop
))
2215 if (p
->family
== AF_INET
) {
2216 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2217 &src
.ipv4
, bytelen
))
2219 } else if (p
->family
== AF_INET6
) {
2220 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2221 &src
.ipv6
, bytelen
))
2224 if (IS_ZEBRA_DEBUG_KERNEL
)
2225 zlog_debug("Setting source");
2229 /* If there is no useful nexthop then return. */
2230 if (nexthop_num
== 0) {
2231 if (IS_ZEBRA_DEBUG_KERNEL
)
2232 zlog_debug("%s: No useful nexthop.", __func__
);
2235 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2238 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2240 uint32_t actual_table
;
2242 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2250 struct zebra_ns
*zns
;
2253 memset(&req
, 0, sizeof(req
));
2255 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2256 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2257 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2259 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2260 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2262 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2263 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2264 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2265 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2269 * So during the namespace cleanup we started storing
2270 * the zvrf table_id for the default table as RT_TABLE_MAIN
2271 * which is what the normal routing table for ip routing is.
2272 * This change caused this to break our lookups of sg data
2273 * because prior to this change the zvrf->table_id was 0
2274 * and when the pim multicast kernel code saw a 0,
2275 * it was auto-translated to RT_TABLE_DEFAULT. But since
2276 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2277 * and the kernel goes screw you and the delicious cookies you
2278 * are trying to give me. So now we have this little hack.
2280 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2282 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2284 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2285 &zns
->netlink_cmd
, zns
, 0);
2291 /* Char length to debug ID with */
2292 #define ID_LENGTH 10
2294 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2296 const struct nh_grp
*z_grp
,
2297 const uint8_t count
)
2299 struct nexthop_grp grp
[count
];
2300 /* Need space for max group size, "/", and null term */
2301 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2302 char buf1
[ID_LENGTH
+ 2];
2306 memset(grp
, 0, sizeof(grp
));
2309 for (int i
= 0; i
< count
; i
++) {
2310 grp
[i
].id
= z_grp
[i
].id
;
2311 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2313 if (IS_ZEBRA_DEBUG_KERNEL
) {
2315 snprintf(buf
, sizeof(buf1
), "group %u",
2318 snprintf(buf1
, sizeof(buf1
), "/%u",
2320 strlcat(buf
, buf1
, sizeof(buf
));
2324 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2325 count
* sizeof(*grp
)))
2329 if (IS_ZEBRA_DEBUG_KERNEL
)
2330 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2336 * Next hop packet encoding helper function.
2338 * \param[in] cmd netlink command.
2339 * \param[in] ctx dataplane context (information snapshot).
2340 * \param[out] buf buffer to hold the packet.
2341 * \param[in] buflen amount of buffer bytes.
2343 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2344 * otherwise the number of bytes written to buf.
2346 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2347 const struct zebra_dplane_ctx
*ctx
,
2348 void *buf
, size_t buflen
)
2356 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2357 char label_buf
[256];
2359 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2360 int type
= dplane_ctx_get_nhe_type(ctx
);
2361 struct rtattr
*nest
;
2366 EC_ZEBRA_NHG_FIB_UPDATE
,
2367 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2372 * Nothing to do if the kernel doesn't support nexthop objects or
2373 * we dont want to install this type of NHG
2375 if (!kernel_nexthops_supported()) {
2376 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2378 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2379 __func__
, id
, zebra_route_string(type
));
2383 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2384 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2386 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2387 __func__
, id
, zebra_route_string(type
));
2391 label_buf
[0] = '\0';
2393 if (buflen
< sizeof(*req
))
2396 memset(req
, 0, sizeof(*req
));
2398 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2399 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2401 if (cmd
== RTM_NEWNEXTHOP
)
2402 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2404 req
->n
.nlmsg_type
= cmd
;
2405 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
2407 req
->nhm
.nh_family
= AF_UNSPEC
;
2410 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2413 if (cmd
== RTM_NEWNEXTHOP
) {
2415 * We distinguish between a "group", which is a collection
2416 * of ids, and a singleton nexthop with an id. The
2417 * group is installed as an id that just refers to a list of
2420 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2421 if (!_netlink_nexthop_build_group(
2422 &req
->n
, buflen
, id
,
2423 dplane_ctx_get_nhe_nh_grp(ctx
),
2424 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2427 const struct nexthop
*nh
=
2428 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2429 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2432 req
->nhm
.nh_family
= AF_INET
;
2433 else if (afi
== AFI_IP6
)
2434 req
->nhm
.nh_family
= AF_INET6
;
2437 case NEXTHOP_TYPE_IPV4
:
2438 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2439 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2444 case NEXTHOP_TYPE_IPV6
:
2445 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2446 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2451 case NEXTHOP_TYPE_BLACKHOLE
:
2452 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2455 /* Blackhole shouldn't have anymore attributes
2458 case NEXTHOP_TYPE_IFINDEX
:
2459 /* Don't need anymore info for this */
2465 EC_ZEBRA_NHG_FIB_UPDATE
,
2466 "Context received for kernel nexthop update without an interface");
2470 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2474 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2475 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2478 build_label_stack(nh
->nh_label
, out_lse
,
2479 label_buf
, sizeof(label_buf
));
2482 /* Set the BoS bit */
2483 out_lse
[num_labels
- 1] |=
2484 htonl(1 << MPLS_LS_S_SHIFT
);
2487 * TODO: MPLS unsupported for now in kernel.
2489 if (req
->nhm
.nh_family
== AF_MPLS
)
2492 encap
= LWTUNNEL_ENCAP_MPLS
;
2493 if (!nl_attr_put16(&req
->n
, buflen
,
2494 NHA_ENCAP_TYPE
, encap
))
2496 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2500 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2502 num_labels
* sizeof(mpls_lse_t
)))
2505 nl_attr_nest_end(&req
->n
, nest
);
2509 if (nh
->nh_srv6
->seg6local_action
!=
2510 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2513 struct rtattr
*nest
;
2514 const struct seg6local_context
*ctx
;
2516 req
->nhm
.nh_family
= AF_INET6
;
2517 action
= nh
->nh_srv6
->seg6local_action
;
2518 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2519 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2520 if (!nl_attr_put(&req
->n
, buflen
,
2526 nest
= nl_attr_nest(&req
->n
, buflen
,
2527 NHA_ENCAP
| NLA_F_NESTED
);
2532 case SEG6_LOCAL_ACTION_END
:
2536 SEG6_LOCAL_ACTION_END
))
2539 case SEG6_LOCAL_ACTION_END_X
:
2543 SEG6_LOCAL_ACTION_END_X
))
2547 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2548 sizeof(struct in6_addr
)))
2551 case SEG6_LOCAL_ACTION_END_T
:
2555 SEG6_LOCAL_ACTION_END_T
))
2563 case SEG6_LOCAL_ACTION_END_DX4
:
2567 SEG6_LOCAL_ACTION_END_DX4
))
2571 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2572 sizeof(struct in_addr
)))
2575 case SEG6_LOCAL_ACTION_END_DT6
:
2579 SEG6_LOCAL_ACTION_END_DT6
))
2587 case SEG6_LOCAL_ACTION_END_DT4
:
2591 SEG6_LOCAL_ACTION_END_DT4
))
2595 SEG6_LOCAL_VRFTABLE
,
2600 zlog_err("%s: unsupport seg6local behaviour action=%u",
2604 nl_attr_nest_end(&req
->n
, nest
);
2607 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2610 struct rtattr
*nest
;
2612 if (!nl_attr_put16(&req
->n
, buflen
,
2614 LWTUNNEL_ENCAP_SEG6
))
2616 nest
= nl_attr_nest(&req
->n
, buflen
,
2617 NHA_ENCAP
| NLA_F_NESTED
);
2620 tun_len
= fill_seg6ipt_encap(tun_buf
,
2622 &nh
->nh_srv6
->seg6_segs
);
2625 if (!nl_attr_put(&req
->n
, buflen
,
2629 nl_attr_nest_end(&req
->n
, nest
);
2635 if (IS_ZEBRA_DEBUG_KERNEL
)
2636 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2637 __func__
, id
, nh
, nh
->ifindex
,
2638 vrf_id_to_name(nh
->vrf_id
),
2639 nh
->vrf_id
, label_buf
);
2642 req
->nhm
.nh_protocol
= zebra2proto(type
);
2644 } else if (cmd
!= RTM_DELNEXTHOP
) {
2646 EC_ZEBRA_NHG_FIB_UPDATE
,
2647 "Nexthop group kernel update command (%d) does not exist",
2652 if (IS_ZEBRA_DEBUG_KERNEL
)
2653 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2656 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2659 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2660 void *buf
, size_t buflen
)
2662 enum dplane_op_e op
;
2665 op
= dplane_ctx_get_op(ctx
);
2666 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2667 cmd
= RTM_NEWNEXTHOP
;
2668 else if (op
== DPLANE_OP_NH_DELETE
)
2669 cmd
= RTM_DELNEXTHOP
;
2671 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2672 "Context received for kernel nexthop update with incorrect OP code (%u)",
2677 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2680 enum netlink_msg_status
2681 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2682 struct zebra_dplane_ctx
*ctx
)
2684 /* Nothing to do if the kernel doesn't support nexthop objects */
2685 if (!kernel_nexthops_supported())
2686 return FRR_NETLINK_SUCCESS
;
2688 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2692 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2693 void *buf
, size_t buflen
)
2695 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2696 buflen
, false, false);
2699 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2700 void *buf
, size_t buflen
)
2702 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2703 buflen
, false, false);
2706 enum netlink_msg_status
2707 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2710 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2712 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2714 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2716 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2718 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2719 /* Single 'replace' operation */
2722 * With route replace semantics in place
2723 * for v4 routes and the new route is a system
2724 * route we do not install anything.
2725 * The problem here is that the new system
2726 * route should cause us to withdraw from
2727 * the kernel the old non-system route
2729 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2730 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2731 netlink_batch_add_msg(
2732 bth
, ctx
, netlink_delroute_msg_encoder
,
2736 * So v6 route replace semantics are not in
2737 * the kernel at this point as I understand it.
2738 * so let's do a delete then an add.
2739 * In the future once v6 route replace semantics
2740 * are in we can figure out what to do here to
2741 * allow working with old and new kernels.
2743 * I'm also intentionally ignoring the failure case
2744 * of the route delete. If that happens yeah we're
2747 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2748 netlink_batch_add_msg(
2749 bth
, ctx
, netlink_delroute_msg_encoder
,
2755 return FRR_NETLINK_ERROR
;
2757 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2758 return FRR_NETLINK_SUCCESS
;
2760 return netlink_batch_add_msg(bth
, ctx
,
2762 ? netlink_newroute_msg_encoder
2763 : netlink_delroute_msg_encoder
,
2768 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2770 * @tb: Netlink RTA data
2771 * @family: Address family in the nhmsg
2772 * @ifp: Interface connected - this should be NULL, we fill it in
2773 * @ns_id: Namspace id
2775 * Return: New nexthop
2777 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2778 unsigned char family
,
2779 struct interface
**ifp
,
2782 struct nexthop nh
= {};
2784 enum nexthop_types_t type
= 0;
2787 struct interface
*ifp_lookup
;
2789 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2792 if (tb
[NHA_GATEWAY
]) {
2795 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2799 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2804 EC_ZEBRA_BAD_NHG_MESSAGE
,
2805 "Nexthop gateway with bad address family (%d) received from kernel",
2809 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2811 type
= NEXTHOP_TYPE_IFINDEX
;
2817 memcpy(&(nh
.gate
), gate
, sz
);
2820 nh
.ifindex
= if_index
;
2823 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2828 nh
.vrf_id
= ifp_lookup
->vrf_id
;
2831 EC_ZEBRA_UNKNOWN_INTERFACE
,
2832 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2833 __func__
, nh
.ifindex
);
2835 nh
.vrf_id
= VRF_DEFAULT
;
2838 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2839 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2842 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2844 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2845 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2848 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2855 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2856 struct nh_grp
*z_grp
, int z_grp_size
)
2859 /* linux/nexthop.h group struct */
2860 struct nexthop_grp
*n_grp
= NULL
;
2862 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2863 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2865 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2866 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2867 "Invalid nexthop group received from the kernel");
2871 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2872 z_grp
[i
].id
= n_grp
[i
].id
;
2873 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2879 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2881 * @h: Netlink message header
2882 * @ns_id: Namspace id
2883 * @startup: Are we reading under startup conditions?
2885 * Return: Result status
2887 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2890 /* nexthop group id */
2892 unsigned char family
;
2894 afi_t afi
= AFI_UNSPEC
;
2895 vrf_id_t vrf_id
= VRF_DEFAULT
;
2896 struct interface
*ifp
= NULL
;
2897 struct nhmsg
*nhm
= NULL
;
2898 struct nexthop nh
= {};
2899 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2900 /* Count of nexthops in group array */
2901 uint8_t grp_count
= 0;
2902 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2904 nhm
= NLMSG_DATA(h
);
2909 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2912 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2915 "%s: Message received from netlink is of a broken size %d %zu",
2916 __func__
, h
->nlmsg_len
,
2917 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2921 netlink_parse_rtattr(tb
, NHA_MAX
, RTM_NHA(nhm
), len
);
2926 EC_ZEBRA_BAD_NHG_MESSAGE
,
2927 "Nexthop group without an ID received from the kernel");
2931 /* We use the ID key'd nhg table for kernel updates */
2932 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2934 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2935 /* If this is a L2 NH just ignore it */
2936 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2937 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2943 family
= nhm
->nh_family
;
2944 afi
= family2afi(family
);
2946 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2948 if (IS_ZEBRA_DEBUG_KERNEL
)
2949 zlog_debug("%s ID (%u) %s NS %u",
2950 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2951 nl_family_to_str(family
), ns_id
);
2954 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2955 if (tb
[NHA_GROUP
]) {
2957 * If this is a group message its only going to have
2958 * an array of nexthop IDs associated with it
2960 grp_count
= netlink_nexthop_process_group(
2961 tb
, grp
, array_size(grp
));
2963 if (tb
[NHA_BLACKHOLE
]) {
2965 * This nexthop is just for blackhole-ing
2966 * traffic, it should not have an OIF, GATEWAY,
2969 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2970 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2971 } else if (tb
[NHA_OIF
])
2973 * This is a true new nexthop, so we need
2974 * to parse the gateway and device info
2976 nh
= netlink_nexthop_process_nh(tb
, family
,
2981 EC_ZEBRA_BAD_NHG_MESSAGE
,
2982 "Invalid Nexthop message received from the kernel with ID (%u)",
2986 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2987 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2988 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2992 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
2996 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
2997 zebra_nhg_kernel_del(id
, vrf_id
);
3003 * netlink_request_nexthop() - Request nextop information from the kernel
3004 * @zns: Zebra namespace
3005 * @family: AF_* netlink family
3006 * @type: RTM_* route type
3008 * Return: Result status
3010 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3017 /* Form the request, specifying filter (rtattr) if needed. */
3018 memset(&req
, 0, sizeof(req
));
3019 req
.n
.nlmsg_type
= type
;
3020 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3021 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3022 req
.nhm
.nh_family
= family
;
3024 return netlink_request(&zns
->netlink_cmd
, &req
);
3029 * netlink_nexthop_read() - Nexthop read function using netlink interface
3031 * @zns: Zebra name space
3033 * Return: Result status
3034 * Only called at bootstrap time.
3036 int netlink_nexthop_read(struct zebra_ns
*zns
)
3039 struct zebra_dplane_info dp_info
;
3041 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3043 /* Get nexthop objects */
3044 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3047 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3051 /* If we succesfully read in nexthop objects,
3052 * this kernel must support them.
3056 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3057 zlog_debug("Nexthop objects %ssupported on this kernel",
3058 supports_nh
? "" : "not ");
3064 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3065 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3067 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3068 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3073 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3074 * evpn neighbor update as netlink messages using dataplane context object.
3075 * Here, a neighbor refers to a bridge forwarding database entry for
3076 * either unicast forwarding or head-end replication or an IP neighbor
3078 * @ctx: Dataplane context
3079 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3080 * @lla: A pointer to neighbor cache link layer address
3081 * @llalen: Length of the pointer to neighbor cache link layer
3083 * @ip: A neighbor cache n/w layer destination address
3084 * In the case of bridge FDB, this represnts the remote
3086 * @replace_obj: Whether NEW request should replace existing object or
3087 * add to the end of the list
3088 * @family: AF_* netlink family
3089 * @type: RTN_* route type
3090 * @flags: NTF_* flags
3091 * @state: NUD_* states
3092 * @data: data buffer pointer
3093 * @datalen: total amount of data buffer space
3094 * @protocol: protocol information
3096 * Return: 0 when the msg doesn't fit entirely in the buffer
3097 * otherwise the number of bytes written to buf.
3099 static ssize_t
netlink_neigh_update_msg_encode(
3100 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3101 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3102 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3103 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3104 size_t datalen
, uint8_t protocol
)
3112 enum dplane_op_e op
;
3114 if (datalen
< sizeof(*req
))
3116 memset(req
, 0, sizeof(*req
));
3118 op
= dplane_ctx_get_op(ctx
);
3120 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3121 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3122 if (cmd
== RTM_NEWNEIGH
)
3123 req
->n
.nlmsg_flags
|=
3125 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3126 req
->n
.nlmsg_type
= cmd
;
3127 req
->ndm
.ndm_family
= family
;
3128 req
->ndm
.ndm_type
= type
;
3129 req
->ndm
.ndm_state
= state
;
3130 req
->ndm
.ndm_flags
= flags
;
3131 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3133 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3138 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3143 struct rtattr
*nest
;
3145 nest
= nl_attr_nest(&req
->n
, datalen
,
3146 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3150 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3151 &nfy_flags
, sizeof(nfy_flags
)))
3153 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3156 nl_attr_nest_end(&req
->n
, nest
);
3161 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3167 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3171 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3172 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3177 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3178 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3181 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3185 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3186 dplane_ctx_mac_get_br_ifindex(ctx
)))
3190 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3194 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3195 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3198 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3199 void *buf
, size_t buflen
)
3201 struct ethaddr dst_mac
= {.octet
= {0}};
3202 int proto
= RTPROT_ZEBRA
;
3204 if (dplane_ctx_get_type(ctx
) != 0)
3205 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3207 return netlink_neigh_update_msg_encode(
3208 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3209 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3210 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3211 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3216 #define NDA_RTA(r) \
3217 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3220 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3223 struct interface
*ifp
;
3224 struct zebra_if
*zif
;
3225 struct rtattr
*tb
[NDA_MAX
+ 1];
3226 struct interface
*br_if
;
3229 struct in_addr vtep_ip
;
3230 int vid_present
= 0, dst_present
= 0;
3234 bool local_inactive
= false;
3235 bool dp_static
= false;
3236 uint32_t nhg_id
= 0;
3238 ndm
= NLMSG_DATA(h
);
3240 /* We only process macfdb notifications if EVPN is enabled */
3241 if (!is_evpn_enabled())
3244 /* Parse attributes and extract fields of interest. Do basic
3245 * validation of the fields.
3247 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3250 if (!tb
[NDA_LLADDR
]) {
3251 if (IS_ZEBRA_DEBUG_KERNEL
)
3252 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3253 nl_msg_type_to_str(h
->nlmsg_type
),
3258 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3259 if (IS_ZEBRA_DEBUG_KERNEL
)
3261 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3262 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3263 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3267 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3269 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
3271 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3272 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3276 /* TODO: Only IPv4 supported now. */
3278 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3280 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3285 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3287 if (ndm
->ndm_state
& NUD_STALE
)
3288 local_inactive
= true;
3290 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3291 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3292 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3294 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3295 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3298 nfy_flags
= *(uint8_t *)RTA_DATA(
3299 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3300 if (nfy_flags
& FDB_NOTIFY_BIT
)
3302 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3303 local_inactive
= true;
3307 if (IS_ZEBRA_DEBUG_KERNEL
)
3308 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
3309 nl_msg_type_to_str(h
->nlmsg_type
),
3310 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
3311 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
3312 dst_present
? dst_buf
: "", nhg_id
);
3314 /* The interface should exist. */
3315 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3317 if (!ifp
|| !ifp
->info
)
3320 /* The interface should be something we're interested in. */
3321 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3324 zif
= (struct zebra_if
*)ifp
->info
;
3325 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3326 if (IS_ZEBRA_DEBUG_KERNEL
)
3328 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3329 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3331 zif
->brslave_info
.bridge_ifindex
);
3335 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3337 if (filter_vlan
&& vid
!= filter_vlan
) {
3338 if (IS_ZEBRA_DEBUG_KERNEL
)
3339 zlog_debug(" Filtered due to filter vlan: %d",
3344 /* If add or update, do accordingly if learnt on a "local" interface; if
3345 * the notification is over VxLAN, this has to be related to
3347 * so perform an implicit delete of any local entry (if it exists).
3349 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3350 /* Drop "permanent" entries. */
3351 if (ndm
->ndm_state
& NUD_PERMANENT
) {
3352 if (IS_ZEBRA_DEBUG_KERNEL
)
3354 " Dropping entry because of NUD_PERMANENT");
3358 if (IS_ZEBRA_IF_VXLAN(ifp
))
3359 return zebra_vxlan_dp_network_mac_add(
3360 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
3361 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3363 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3364 sticky
, local_inactive
, dp_static
);
3367 /* This is a delete notification.
3368 * Ignore the notification with IP dest as it may just signify that the
3369 * MAC has moved from remote to local. The exception is the special
3370 * all-zeros MAC that represents the BUM flooding entry; we may have
3371 * to readd it. Otherwise,
3372 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3373 * 2. For a MAC over "local" interface, delete the mac
3374 * Note: We will get notifications from both bridge driver and VxLAN
3381 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3383 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
3384 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3388 if (IS_ZEBRA_IF_VXLAN(ifp
))
3389 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3391 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3394 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3399 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3402 /* Length validity. */
3403 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3407 /* We are interested only in AF_BRIDGE notifications. */
3408 ndm
= NLMSG_DATA(h
);
3409 if (ndm
->ndm_family
!= AF_BRIDGE
)
3412 return netlink_macfdb_change(h
, len
, ns_id
);
3415 /* Request for MAC FDB information from the kernel */
3416 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3417 int type
, ifindex_t master_ifindex
)
3421 struct ifinfomsg ifm
;
3425 /* Form the request, specifying filter (rtattr) if needed. */
3426 memset(&req
, 0, sizeof(req
));
3427 req
.n
.nlmsg_type
= type
;
3428 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3429 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3430 req
.ifm
.ifi_family
= family
;
3432 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3434 return netlink_request(netlink_cmd
, &req
);
3438 * MAC forwarding database read using netlink interface. This is invoked
3441 int netlink_macfdb_read(struct zebra_ns
*zns
)
3444 struct zebra_dplane_info dp_info
;
3446 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3448 /* Get bridge FDB table. */
3449 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3453 /* We are reading entire table. */
3455 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3462 * MAC forwarding database read using netlink interface. This is for a
3463 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3465 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3466 struct interface
*br_if
)
3468 struct zebra_if
*br_zif
;
3469 struct zebra_if
*zif
;
3470 struct zebra_l2info_vxlan
*vxl
;
3471 struct zebra_dplane_info dp_info
;
3474 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3476 /* Save VLAN we're filtering on, if needed. */
3477 br_zif
= (struct zebra_if
*)br_if
->info
;
3478 zif
= (struct zebra_if
*)ifp
->info
;
3479 vxl
= &zif
->l2info
.vxl
;
3480 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3481 filter_vlan
= vxl
->access_vlan
;
3483 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3485 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3489 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3492 /* Reset VLAN filter. */
3498 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3499 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3500 int family
, int type
,
3501 struct interface
*br_if
,
3502 const struct ethaddr
*mac
,
3510 struct zebra_if
*br_zif
;
3512 memset(&req
, 0, sizeof(req
));
3513 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3514 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3515 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3516 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3517 /* req.ndm.ndm_state = NUD_REACHABLE; */
3519 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3521 br_zif
= (struct zebra_if
*)br_if
->info
;
3522 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3523 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3525 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3527 if (IS_ZEBRA_DEBUG_KERNEL
)
3529 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3530 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3531 br_if
->name
, br_if
->ifindex
,
3532 vrf_id_to_name(br_if
->vrf_id
), br_if
->vrf_id
, mac
, vid
);
3534 return netlink_request(&zns
->netlink_cmd
, &req
);
3537 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3538 struct interface
*br_if
,
3539 const struct ethaddr
*mac
, vlanid_t vid
)
3542 struct zebra_dplane_info dp_info
;
3544 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3546 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3548 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3554 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3561 * Netlink-specific handler for MAC updates using dataplane context object.
3563 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3566 struct ipaddr vtep_ip
;
3573 uint32_t update_flags
;
3575 uint8_t nfy_flags
= 0;
3576 int proto
= RTPROT_ZEBRA
;
3578 if (dplane_ctx_get_type(ctx
) != 0)
3579 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3581 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3582 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3585 state
= NUD_REACHABLE
;
3587 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3588 if (update_flags
& DPLANE_MAC_REMOTE
) {
3590 if (dplane_ctx_mac_is_sticky(ctx
)) {
3591 /* NUD_NOARP prevents the entry from expiring */
3593 /* sticky the entry from moving */
3594 flags
|= NTF_STICKY
;
3596 flags
|= NTF_EXT_LEARNED
;
3598 /* if it was static-local previously we need to clear the
3599 * notify flags on replace with remote
3601 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3605 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3606 nfy_flags
|= FDB_NOTIFY_BIT
;
3610 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3611 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3616 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3617 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3618 SET_IPADDR_V4(&vtep_ip
);
3620 if (IS_ZEBRA_DEBUG_KERNEL
) {
3622 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3624 vid
= dplane_ctx_mac_get_vlan(ctx
);
3626 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3631 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3632 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3633 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3634 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3635 mac
, &vtep_ip
, nhg_id
,
3636 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3637 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3639 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3640 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3645 total
= netlink_neigh_update_msg_encode(
3646 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
3647 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
3648 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
3655 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3656 * 5549 support, re-install them.
3658 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3659 struct interface
*ifp
, struct ipaddr
*ip
,
3662 if (ndm
->ndm_family
!= AF_INET
)
3665 if (!zif
->v6_2_v4_ll_neigh_entry
)
3668 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3671 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3672 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3677 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3681 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3683 #define NUD_LOCAL_ACTIVE \
3684 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3686 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
3688 /* an exact match is done between
3689 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
3690 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
3691 * (see in lib/zclient.h)
3695 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3698 struct interface
*ifp
;
3699 struct zebra_if
*zif
;
3700 struct rtattr
*tb
[NDA_MAX
+ 1];
3701 struct interface
*link_if
;
3705 char buf
[ETHER_ADDR_STRLEN
];
3706 int mac_present
= 0;
3709 bool local_inactive
;
3710 uint32_t ext_flags
= 0;
3711 bool dp_static
= false;
3715 ndm
= NLMSG_DATA(h
);
3717 /* The interface should exist. */
3718 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3720 if (!ifp
|| !ifp
->info
)
3723 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
3724 zif
= (struct zebra_if
*)ifp
->info
;
3726 /* Parse attributes and extract fields of interest. */
3727 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3730 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3731 nl_msg_type_to_str(h
->nlmsg_type
),
3732 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3733 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
);
3737 memset(&ip
, 0, sizeof(struct ipaddr
));
3738 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3739 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3741 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3742 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3743 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3744 if (IS_ZEBRA_DEBUG_KERNEL
)
3746 " Neighbor Entry Received is a 5549 entry, finished");
3750 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3751 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3752 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3754 /* we send link layer information to client:
3755 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3756 * - struct ipaddr ( for DEL and GET)
3757 * - struct ethaddr mac; (for NEW)
3759 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
3760 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
3761 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
3762 cmd
= ZEBRA_NHRP_NEIGH_GET
;
3763 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
3764 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
3766 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
3770 if (tb
[NDA_LLADDR
]) {
3771 /* copy LLADDR information */
3772 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
3773 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
3775 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
3776 union sockunion link_layer_ipv4
;
3779 sockunion_family(&link_layer_ipv4
) = AF_INET
;
3780 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
3783 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
3784 zsend_nhrp_neighbor_notify(
3786 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
3790 if (h
->nlmsg_type
== RTM_GETNEIGH
)
3793 /* The neighbor is present on an SVI. From this, we locate the
3795 * bridge because we're only interested in neighbors on a VxLAN bridge.
3796 * The bridge is located based on the nature of the SVI:
3797 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3799 * and is linked to the bridge
3800 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3804 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3805 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3809 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3812 if (IS_ZEBRA_DEBUG_KERNEL
)
3814 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3818 memset(&mac
, 0, sizeof(struct ethaddr
));
3819 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3820 if (tb
[NDA_LLADDR
]) {
3821 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3822 if (IS_ZEBRA_DEBUG_KERNEL
)
3824 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3829 ifp
->name
, ndm
->ndm_ifindex
,
3830 VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3831 (unsigned long)RTA_PAYLOAD(
3837 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3840 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3841 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3843 if (tb
[NDA_EXT_FLAGS
]) {
3844 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3845 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3849 if (IS_ZEBRA_DEBUG_KERNEL
)
3851 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3852 nl_msg_type_to_str(h
->nlmsg_type
),
3853 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3854 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3857 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3859 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3861 /* If the neighbor state is valid for use, process as an add or
3863 * else process as a delete. Note that the delete handling may
3865 * in re-adding the neighbor if it is a valid "remote" neighbor.
3867 if (ndm
->ndm_state
& NUD_VALID
) {
3868 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3870 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3872 /* If EVPN-MH is not enabled we treat STALE
3873 * neighbors as locally-active and advertise
3876 local_inactive
= false;
3878 return zebra_vxlan_handle_kernel_neigh_update(
3879 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3880 is_router
, local_inactive
, dp_static
);
3883 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3886 if (IS_ZEBRA_DEBUG_KERNEL
)
3887 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
3888 nl_msg_type_to_str(h
->nlmsg_type
),
3889 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3890 ndm
->ndm_ifindex
, VRF_LOGNAME(vrf
), ifp
->vrf_id
,
3893 /* Process the delete - it may result in re-adding the neighbor if it is
3894 * a valid "remote" neighbor.
3896 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3899 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3904 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3907 /* Length validity. */
3908 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3912 /* We are interested only in AF_INET or AF_INET6 notifications. */
3913 ndm
= NLMSG_DATA(h
);
3914 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3917 return netlink_neigh_change(h
, len
);
3920 /* Request for IP neighbor information from the kernel */
3921 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3922 int type
, ifindex_t ifindex
)
3930 /* Form the request, specifying filter (rtattr) if needed. */
3931 memset(&req
, 0, sizeof(req
));
3932 req
.n
.nlmsg_type
= type
;
3933 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3934 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3935 req
.ndm
.ndm_family
= family
;
3937 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3939 return netlink_request(netlink_cmd
, &req
);
3943 * IP Neighbor table read using netlink interface. This is invoked
3946 int netlink_neigh_read(struct zebra_ns
*zns
)
3949 struct zebra_dplane_info dp_info
;
3951 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3953 /* Get IP neighbor table. */
3954 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3958 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3965 * IP Neighbor table read using netlink interface. This is for a specific
3968 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3971 struct zebra_dplane_info dp_info
;
3973 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3975 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3979 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3986 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3987 * read using netlink interface.
3989 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3991 const struct ipaddr
*ip
,
4001 /* Form the request, specifying filter (rtattr) if needed. */
4002 memset(&req
, 0, sizeof(req
));
4003 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4004 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4005 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4006 req
.ndm
.ndm_ifindex
= ifindex
;
4008 if (IS_IPADDR_V4(ip
)) {
4009 ipa_len
= IPV4_MAX_BYTELEN
;
4010 req
.ndm
.ndm_family
= AF_INET
;
4013 ipa_len
= IPV6_MAX_BYTELEN
;
4014 req
.ndm
.ndm_family
= AF_INET6
;
4017 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4019 if (IS_ZEBRA_DEBUG_KERNEL
)
4020 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4021 __func__
, nl_msg_type_to_str(type
),
4022 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4025 return netlink_request(&zns
->netlink_cmd
, &req
);
4028 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4029 struct interface
*vlan_if
)
4032 struct zebra_ns
*zns
;
4033 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(vlan_if
->vrf_id
);
4034 struct zebra_dplane_info dp_info
;
4038 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4040 if (IS_ZEBRA_DEBUG_KERNEL
)
4041 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4042 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4043 vrf_id_to_name(vlan_if
->vrf_id
), vlan_if
->vrf_id
);
4045 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4050 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4056 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4061 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4062 || h
->nlmsg_type
== RTM_GETNEIGH
))
4065 /* Length validity. */
4066 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4069 "%s: Message received from netlink is of a broken size %d %zu",
4070 __func__
, h
->nlmsg_len
,
4071 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4075 /* Is this a notification for the MAC FDB or IP neighbor table? */
4076 ndm
= NLMSG_DATA(h
);
4077 if (ndm
->ndm_family
== AF_BRIDGE
)
4078 return netlink_macfdb_change(h
, len
, ns_id
);
4080 if (ndm
->ndm_type
!= RTN_UNICAST
)
4083 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4084 return netlink_ipneigh_change(h
, len
, ns_id
);
4087 EC_ZEBRA_UNKNOWN_FAMILY
,
4088 "Invalid address family: %u received from kernel neighbor change: %s",
4089 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4097 * Utility neighbor-update function, using info from dplane context.
4099 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4100 int cmd
, void *buf
, size_t buflen
)
4102 const struct ipaddr
*ip
;
4103 const struct ethaddr
*mac
= NULL
;
4104 const struct ipaddr
*link_ip
= NULL
;
4105 const void *link_ptr
= NULL
;
4106 char buf2
[ETHER_ADDR_STRLEN
];
4112 uint32_t update_flags
;
4113 uint32_t ext_flags
= 0;
4115 int proto
= RTPROT_ZEBRA
;
4117 if (dplane_ctx_get_type(ctx
) != 0)
4118 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4120 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4122 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4123 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4124 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4125 llalen
= IPADDRSZ(link_ip
);
4126 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4127 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4129 mac
= dplane_ctx_neigh_get_mac(ctx
);
4131 link_ptr
= (const void *)mac
;
4132 if (is_zero_mac(mac
))
4135 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4137 snprintf(buf2
, sizeof(buf2
), "null");
4139 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4140 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4141 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4143 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4145 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4146 flags
|= NTF_EXT_LEARNED
;
4147 /* if it was static-local previously we need to clear the
4148 * ext flags on replace with remote
4150 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4152 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4155 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4156 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4158 if (IS_ZEBRA_DEBUG_KERNEL
)
4160 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4161 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4162 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4163 ip
, link_ip
? "Link " : "MAC ", buf2
, flags
, state
,
4164 ext
? "ext " : "", ext_flags
);
4166 return netlink_neigh_update_msg_encode(
4167 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4168 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4169 ext_flags
, buf
, buflen
, proto
);
4172 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4173 void *data
, size_t datalen
)
4180 struct rtattr
*nest
;
4185 if (datalen
< sizeof(*req
))
4187 memset(req
, 0, sizeof(*req
));
4188 family
= dplane_ctx_neightable_get_family(ctx
);
4189 idx
= dplane_ctx_get_ifindex(ctx
);
4191 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4192 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4193 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4194 req
->ndtm
.ndtm_family
= family
;
4196 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4197 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4198 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4201 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4203 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4204 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4206 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4207 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4210 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4211 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4214 nl_attr_nest_end(&req
->n
, nest
);
4216 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4219 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4220 void *buf
, size_t buflen
)
4224 switch (dplane_ctx_get_op(ctx
)) {
4225 case DPLANE_OP_NEIGH_INSTALL
:
4226 case DPLANE_OP_NEIGH_UPDATE
:
4227 case DPLANE_OP_NEIGH_DISCOVER
:
4228 case DPLANE_OP_NEIGH_IP_INSTALL
:
4229 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4231 case DPLANE_OP_NEIGH_DELETE
:
4232 case DPLANE_OP_NEIGH_IP_DELETE
:
4233 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4235 case DPLANE_OP_VTEP_ADD
:
4236 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4239 case DPLANE_OP_VTEP_DELETE
:
4240 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4243 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4244 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4254 * Update MAC, using dataplane context object.
4257 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4258 struct zebra_dplane_ctx
*ctx
)
4260 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4264 enum netlink_msg_status
4265 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4267 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4272 * MPLS label forwarding table change via netlink interface, using dataplane
4273 * context information.
4275 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4276 void *buf
, size_t buflen
)
4279 const struct nhlfe_list_head
*head
;
4280 const struct zebra_nhlfe
*nhlfe
;
4281 struct nexthop
*nexthop
= NULL
;
4282 unsigned int nexthop_num
;
4283 const char *routedesc
;
4285 struct prefix p
= {0};
4293 if (buflen
< sizeof(*req
))
4296 memset(req
, 0, sizeof(*req
));
4299 * Count # nexthops so we can decide whether to use singlepath
4300 * or multipath case.
4303 head
= dplane_ctx_get_nhlfe_list(ctx
);
4304 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4305 nexthop
= nhlfe
->nexthop
;
4308 if (cmd
== RTM_NEWROUTE
) {
4309 /* Count all selected NHLFEs */
4310 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4311 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4314 /* Count all installed NHLFEs */
4315 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4316 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4321 if ((nexthop_num
== 0) ||
4322 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4325 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4326 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4327 req
->n
.nlmsg_type
= cmd
;
4328 req
->n
.nlmsg_pid
= dplane_ctx_get_ns(ctx
)->nls
.snl
.nl_pid
;
4330 req
->r
.rtm_family
= AF_MPLS
;
4331 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4332 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4333 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4334 req
->r
.rtm_type
= RTN_UNICAST
;
4336 if (cmd
== RTM_NEWROUTE
) {
4337 /* We do a replace to handle update. */
4338 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4340 /* set the protocol value if installing */
4341 route_type
= re_type_from_lsp_type(
4342 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4343 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4346 /* Fill destination */
4347 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4348 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4351 /* Fill nexthops (paths) based on single-path or multipath. The paths
4352 * chosen depend on the operation.
4354 if (nexthop_num
== 1) {
4355 routedesc
= "single-path";
4356 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4360 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4361 nexthop
= nhlfe
->nexthop
;
4365 if ((cmd
== RTM_NEWROUTE
4366 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4367 && CHECK_FLAG(nexthop
->flags
,
4368 NEXTHOP_FLAG_ACTIVE
)))
4369 || (cmd
== RTM_DELROUTE
4370 && (CHECK_FLAG(nhlfe
->flags
,
4371 NHLFE_FLAG_INSTALLED
)
4372 && CHECK_FLAG(nexthop
->flags
,
4373 NEXTHOP_FLAG_FIB
)))) {
4374 /* Add the gateway */
4375 if (!_netlink_mpls_build_singlepath(
4376 &p
, routedesc
, nhlfe
, &req
->n
,
4377 &req
->r
, buflen
, cmd
))
4384 } else { /* Multipath case */
4385 struct rtattr
*nest
;
4386 const union g_addr
*src1
= NULL
;
4388 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4392 routedesc
= "multipath";
4393 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4397 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4398 nexthop
= nhlfe
->nexthop
;
4402 if ((cmd
== RTM_NEWROUTE
4403 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4404 && CHECK_FLAG(nexthop
->flags
,
4405 NEXTHOP_FLAG_ACTIVE
)))
4406 || (cmd
== RTM_DELROUTE
4407 && (CHECK_FLAG(nhlfe
->flags
,
4408 NHLFE_FLAG_INSTALLED
)
4409 && CHECK_FLAG(nexthop
->flags
,
4410 NEXTHOP_FLAG_FIB
)))) {
4413 /* Build the multipath */
4414 if (!_netlink_mpls_build_multipath(
4415 &p
, routedesc
, nhlfe
, &req
->n
,
4416 buflen
, &req
->r
, &src1
))
4421 /* Add the multipath */
4422 nl_attr_nest_end(&req
->n
, nest
);
4425 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4428 /****************************************************************************
4429 * This code was developed in a branch that didn't have dplane APIs for
4430 * MAC updates. Hence the use of the legacy style. It will be moved to
4431 * the new dplane style pre-merge to master. XXX
4433 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4440 int cmd
= RTM_NEWNEXTHOP
;
4441 struct zebra_vrf
*zvrf
;
4442 struct zebra_ns
*zns
;
4444 zvrf
= zebra_vrf_get_evpn();
4449 memset(&req
, 0, sizeof(req
));
4451 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4452 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4453 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4454 req
.n
.nlmsg_type
= cmd
;
4455 req
.nhm
.nh_family
= AF_INET
;
4457 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4459 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4461 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4462 &vtep_ip
, IPV4_MAX_BYTELEN
))
4465 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4466 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4467 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4470 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4474 static int netlink_fdb_nh_del(uint32_t nh_id
)
4481 int cmd
= RTM_DELNEXTHOP
;
4482 struct zebra_vrf
*zvrf
;
4483 struct zebra_ns
*zns
;
4485 zvrf
= zebra_vrf_get_evpn();
4490 memset(&req
, 0, sizeof(req
));
4492 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4493 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4494 req
.n
.nlmsg_type
= cmd
;
4495 req
.nhm
.nh_family
= AF_UNSPEC
;
4497 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4500 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4501 zlog_debug("Tx %s fdb-nh 0x%x",
4502 nl_msg_type_to_str(cmd
), nh_id
);
4505 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4509 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4510 struct nh_grp
*nh_ids
)
4517 int cmd
= RTM_NEWNEXTHOP
;
4518 struct zebra_vrf
*zvrf
;
4519 struct zebra_ns
*zns
;
4520 struct nexthop_grp grp
[nh_cnt
];
4523 zvrf
= zebra_vrf_get_evpn();
4528 memset(&req
, 0, sizeof(req
));
4530 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4531 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4532 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4533 req
.n
.nlmsg_type
= cmd
;
4534 req
.nhm
.nh_family
= AF_UNSPEC
;
4536 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4538 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4540 memset(&grp
, 0, sizeof(grp
));
4541 for (i
= 0; i
< nh_cnt
; ++i
) {
4542 grp
[i
].id
= nh_ids
[i
].id
;
4543 grp
[i
].weight
= nh_ids
[i
].weight
;
4545 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4546 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4550 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4551 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4555 for (i
= 0; i
< nh_cnt
; ++i
) {
4556 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4558 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4561 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4562 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4565 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4569 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4571 return netlink_fdb_nh_del(nhg_id
);
4574 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4576 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4579 int kernel_del_mac_nh(uint32_t nh_id
)
4581 return netlink_fdb_nh_del(nh_id
);
4584 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4585 struct nh_grp
*nh_ids
)
4587 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4590 int kernel_del_mac_nhg(uint32_t nhg_id
)
4592 return netlink_fdb_nhg_del(nhg_id
);
4595 #endif /* HAVE_NETLINK */