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"
82 #include "zebra/zebra_trace.h"
88 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
89 * UAPI for MAC sync. */
90 #ifndef _UAPI_LINUX_IF_BRIDGE_H
91 #define BR_SPH_LIST_SIZE 10
94 static vlanid_t filter_vlan
= 0;
96 /* We capture whether the current kernel supports nexthop ids; by
97 * default, we'll use them if possible. There's also a configuration
98 * available to _disable_ use of kernel nexthops.
100 static bool supports_nh
;
108 static const char ipv4_ll_buf
[16] = "169.254.0.1";
109 static struct in_addr ipv4_ll
;
111 /* Is this a ipv4 over ipv6 route? */
112 static bool is_route_v4_over_v6(unsigned char rtm_family
,
113 enum nexthop_types_t nexthop_type
)
115 if (rtm_family
== AF_INET
116 && (nexthop_type
== NEXTHOP_TYPE_IPV6
117 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
123 /* Helper to control use of kernel-level nexthop ids */
124 static bool kernel_nexthops_supported(void)
126 return (supports_nh
&& !vrf_is_backend_netns()
127 && zebra_nhg_kernel_nexthops_enabled());
131 * Some people may only want to use NHGs created by protos and not
132 * implicitly created by Zebra. This check accounts for that.
134 static bool proto_nexthops_only(void)
136 return zebra_nhg_proto_nexthops_only();
139 /* Is this a proto created NHG? */
140 static bool is_proto_nhg(uint32_t id
, int type
)
142 /* If type is available, use it as the source of truth */
144 if (type
!= ZEBRA_ROUTE_NHG
)
149 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
156 * The ipv4_ll data structure is used for all 5549
157 * additions to the kernel. Let's figure out the
158 * correct value one time instead for every
159 * install/remove of a 5549 type route
161 void rt_netlink_init(void)
163 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
167 * Mapping from dataplane neighbor flags to netlink flags
169 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
173 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
174 flags
|= NTF_EXT_LEARNED
;
175 if (dplane_flags
& DPLANE_NTF_ROUTER
)
177 if (dplane_flags
& DPLANE_NTF_USE
)
184 * Mapping from dataplane neighbor state to netlink state
186 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
190 if (dplane_state
& DPLANE_NUD_REACHABLE
)
191 state
|= NUD_REACHABLE
;
192 if (dplane_state
& DPLANE_NUD_STALE
)
194 if (dplane_state
& DPLANE_NUD_NOARP
)
196 if (dplane_state
& DPLANE_NUD_PROBE
)
198 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
199 state
|= NUD_INCOMPLETE
;
200 if (dplane_state
& DPLANE_NUD_PERMANENT
)
201 state
|= NUD_PERMANENT
;
202 if (dplane_state
& DPLANE_NUD_FAILED
)
209 static inline bool is_selfroute(int proto
)
211 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
212 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
213 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
214 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
215 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
216 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
217 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
218 || (proto
== RTPROT_SRTE
)) {
225 static inline int zebra2proto(int proto
)
228 case ZEBRA_ROUTE_BABEL
:
229 proto
= RTPROT_BABEL
;
231 case ZEBRA_ROUTE_BGP
:
234 case ZEBRA_ROUTE_OSPF
:
235 case ZEBRA_ROUTE_OSPF6
:
238 case ZEBRA_ROUTE_STATIC
:
239 proto
= RTPROT_ZSTATIC
;
241 case ZEBRA_ROUTE_ISIS
:
244 case ZEBRA_ROUTE_RIP
:
247 case ZEBRA_ROUTE_RIPNG
:
248 proto
= RTPROT_RIPNG
;
250 case ZEBRA_ROUTE_NHRP
:
253 case ZEBRA_ROUTE_EIGRP
:
254 proto
= RTPROT_EIGRP
;
256 case ZEBRA_ROUTE_LDP
:
259 case ZEBRA_ROUTE_SHARP
:
260 proto
= RTPROT_SHARP
;
262 case ZEBRA_ROUTE_PBR
:
265 case ZEBRA_ROUTE_OPENFABRIC
:
266 proto
= RTPROT_OPENFABRIC
;
268 case ZEBRA_ROUTE_SRTE
:
271 case ZEBRA_ROUTE_TABLE
:
272 case ZEBRA_ROUTE_NHG
:
273 proto
= RTPROT_ZEBRA
;
275 case ZEBRA_ROUTE_CONNECT
:
276 case ZEBRA_ROUTE_KERNEL
:
277 proto
= RTPROT_KERNEL
;
281 * When a user adds a new protocol this will show up
282 * to let them know to do something about it. This
283 * is intentionally a warn because we should see
284 * this as part of development of a new protocol
287 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
289 proto
= RTPROT_ZEBRA
;
296 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
300 proto
= ZEBRA_ROUTE_BABEL
;
303 proto
= ZEBRA_ROUTE_BGP
;
306 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
310 proto
= ZEBRA_ROUTE_ISIS
;
313 proto
= ZEBRA_ROUTE_RIP
;
316 proto
= ZEBRA_ROUTE_RIPNG
;
319 proto
= ZEBRA_ROUTE_NHRP
;
322 proto
= ZEBRA_ROUTE_EIGRP
;
325 proto
= ZEBRA_ROUTE_LDP
;
329 proto
= ZEBRA_ROUTE_STATIC
;
332 proto
= ZEBRA_ROUTE_SHARP
;
335 proto
= ZEBRA_ROUTE_PBR
;
337 case RTPROT_OPENFABRIC
:
338 proto
= ZEBRA_ROUTE_OPENFABRIC
;
341 proto
= ZEBRA_ROUTE_SRTE
;
345 proto
= ZEBRA_ROUTE_NHG
;
348 /* Intentional fall thru */
351 * When a user adds a new protocol this will show up
352 * to let them know to do something about it. This
353 * is intentionally a warn because we should see
354 * this as part of development of a new protocol
357 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
359 proto
= ZEBRA_ROUTE_KERNEL
;
366 Pending: create an efficient table_id (in a tree/hash) based lookup)
368 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
371 struct zebra_vrf
*zvrf
;
373 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
377 /* case vrf with netns : match the netnsid */
378 if (vrf_is_backend_netns()) {
379 if (ns_id
== zvrf_id(zvrf
))
380 return zvrf_id(zvrf
);
382 /* VRF is VRF_BACKEND_VRF_LITE */
383 if (zvrf
->table_id
!= table_id
)
385 return zvrf_id(zvrf
);
393 * @parse_encap_mpls() - Parses encapsulated mpls attributes
394 * @tb: Pointer to rtattr to look for nested items in.
395 * @labels: Pointer to store labels in.
397 * Return: Number of mpls labels found.
399 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
401 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
402 mpls_lse_t
*lses
= NULL
;
407 mpls_label_t label
= 0;
409 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
410 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
411 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
412 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
413 labels
[num_labels
++] = label
;
419 static enum seg6local_action_t
420 parse_encap_seg6local(struct rtattr
*tb
,
421 struct seg6local_context
*ctx
)
423 struct rtattr
*tb_encap
[256] = {};
424 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
426 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
428 if (tb_encap
[SEG6_LOCAL_ACTION
])
429 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
431 if (tb_encap
[SEG6_LOCAL_NH4
])
432 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
433 tb_encap
[SEG6_LOCAL_NH4
]);
435 if (tb_encap
[SEG6_LOCAL_NH6
])
436 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
437 tb_encap
[SEG6_LOCAL_NH6
]);
439 if (tb_encap
[SEG6_LOCAL_TABLE
])
440 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
442 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
444 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
449 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
451 struct rtattr
*tb_encap
[256] = {};
452 struct seg6_iptunnel_encap
*ipt
= NULL
;
453 struct in6_addr
*segments
= NULL
;
455 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
458 * TODO: It's not support multiple SID list.
460 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
461 ipt
= (struct seg6_iptunnel_encap
*)
462 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
463 segments
= ipt
->srh
[0].segments
;
472 static struct nexthop
473 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
474 enum blackhole_type bh_type
, int index
, void *prefsrc
,
475 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
477 struct interface
*ifp
= NULL
;
478 struct nexthop nh
= {0};
479 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
481 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
482 struct seg6local_context seg6l_ctx
= {};
483 struct in6_addr seg6_segs
= {};
486 vrf_id_t nh_vrf_id
= vrf_id
;
487 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
489 if (bh_type
== BLACKHOLE_UNSPEC
) {
491 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
492 else if (index
&& gate
)
493 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
494 : NEXTHOP_TYPE_IPV6_IFINDEX
;
495 else if (!index
&& gate
)
496 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
499 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
500 nh
.bh_type
= bh_type
;
503 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
504 nh
.bh_type
= bh_type
;
508 memcpy(&nh
.src
, prefsrc
, sz
);
510 memcpy(&nh
.gate
, gate
, sz
);
513 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
515 nh_vrf_id
= ifp
->vrf
->vrf_id
;
517 nh
.vrf_id
= nh_vrf_id
;
519 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
520 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
521 == LWTUNNEL_ENCAP_MPLS
) {
522 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
524 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
525 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
526 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
527 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
529 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
530 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
531 == LWTUNNEL_ENCAP_SEG6
) {
532 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
535 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
536 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
539 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
541 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
542 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
545 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
550 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
551 struct nexthop_group
*ng
,
553 struct rtnexthop
*rtnh
,
555 void *prefsrc
, vrf_id_t vrf_id
)
558 struct interface
*ifp
= NULL
;
561 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
563 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
564 struct seg6local_context seg6l_ctx
= {};
565 struct in6_addr seg6_segs
= {};
567 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
569 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
570 vrf_id_t nh_vrf_id
= vrf_id
;
573 struct nexthop
*nh
= NULL
;
575 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
578 index
= rtnh
->rtnh_ifindex
;
581 * Yes we are looking this up
582 * for every nexthop and just
583 * using the last one looked
586 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
589 nh_vrf_id
= ifp
->vrf
->vrf_id
;
592 EC_ZEBRA_UNKNOWN_INTERFACE
,
593 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
595 nh_vrf_id
= VRF_DEFAULT
;
600 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
601 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
602 rtnh
->rtnh_len
- sizeof(*rtnh
));
603 if (rtnh_tb
[RTA_GATEWAY
])
604 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
605 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
606 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
607 == LWTUNNEL_ENCAP_MPLS
) {
608 num_labels
= parse_encap_mpls(
609 rtnh_tb
[RTA_ENCAP
], labels
);
611 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
612 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
613 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
614 seg6l_act
= parse_encap_seg6local(
615 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
617 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
618 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
619 == LWTUNNEL_ENCAP_SEG6
) {
620 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
625 if (gate
&& rtm
->rtm_family
== AF_INET
) {
627 nh
= nexthop_from_ipv4_ifindex(
628 gate
, prefsrc
, index
, nh_vrf_id
);
630 nh
= nexthop_from_ipv4(gate
, prefsrc
,
632 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
634 nh
= nexthop_from_ipv6_ifindex(
635 gate
, index
, nh_vrf_id
);
637 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
639 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
642 nh
->weight
= rtnh
->rtnh_hops
+ 1;
645 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
648 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
649 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
653 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
655 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
656 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
658 /* Add to temporary list */
659 nexthop_group_add_sorted(ng
, nh
);
662 if (rtnh
->rtnh_len
== 0)
665 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
666 rtnh
= RTNH_NEXT(rtnh
);
669 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
674 /* Looking up routing table by netlink interface. */
675 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
680 struct rtattr
*tb
[RTA_MAX
+ 1];
683 struct prefix_ipv6 src_p
= {};
687 char anyaddr
[16] = {0};
689 int proto
= ZEBRA_ROUTE_KERNEL
;
694 uint8_t distance
= 0;
700 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
701 void *src
= NULL
; /* IPv6 srcdest source prefix */
702 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
704 frrtrace(3, frr_zebra
, netlink_route_change_read_unicast
, h
, ns_id
,
709 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
711 switch (rtm
->rtm_type
) {
715 bh_type
= BLACKHOLE_NULL
;
717 case RTN_UNREACHABLE
:
718 bh_type
= BLACKHOLE_REJECT
;
721 bh_type
= BLACKHOLE_ADMINPROHIB
;
724 if (IS_ZEBRA_DEBUG_KERNEL
)
725 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
726 nl_rttype_to_str(rtm
->rtm_type
),
731 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
734 "%s: Message received from netlink is of a broken size %d %zu",
735 __func__
, h
->nlmsg_len
,
736 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
740 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
742 if (rtm
->rtm_flags
& RTM_F_CLONED
)
744 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
746 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
749 selfroute
= is_selfroute(rtm
->rtm_protocol
);
751 if (!startup
&& selfroute
752 && h
->nlmsg_type
== RTM_NEWROUTE
753 && !zrouter
.asic_offloaded
) {
754 if (IS_ZEBRA_DEBUG_KERNEL
)
755 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
760 /* We don't care about change notifications for the MPLS table. */
761 /* TODO: Revisit this. */
762 if (rtm
->rtm_family
== AF_MPLS
)
765 /* Table corresponding to route. */
767 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
769 table
= rtm
->rtm_table
;
772 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
773 if (vrf_id
== VRF_DEFAULT
) {
774 if (!is_zebra_valid_kernel_table(table
)
775 && !is_zebra_main_routing_table(table
))
779 if (rtm
->rtm_flags
& RTM_F_TRAP
)
780 flags
|= ZEBRA_FLAG_TRAPPED
;
781 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
782 flags
|= ZEBRA_FLAG_OFFLOADED
;
783 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
784 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
786 /* Route which inserted by Zebra. */
788 flags
|= ZEBRA_FLAG_SELFROUTE
;
789 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
792 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
795 dest
= RTA_DATA(tb
[RTA_DST
]);
800 src
= RTA_DATA(tb
[RTA_SRC
]);
805 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
808 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
811 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
813 if (tb
[RTA_PRIORITY
])
814 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
816 #if defined(SUPPORT_REALMS)
818 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
821 if (tb
[RTA_METRICS
]) {
822 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
824 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
825 RTA_PAYLOAD(tb
[RTA_METRICS
]));
828 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
831 if (rtm
->rtm_family
== AF_INET
) {
833 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
835 "Invalid destination prefix length: %u received from kernel route change",
839 memcpy(&p
.u
.prefix4
, dest
, 4);
840 p
.prefixlen
= rtm
->rtm_dst_len
;
842 if (rtm
->rtm_src_len
!= 0) {
844 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
845 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
850 /* Force debug below to not display anything for source */
852 } else if (rtm
->rtm_family
== AF_INET6
) {
854 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
856 "Invalid destination prefix length: %u received from kernel route change",
860 memcpy(&p
.u
.prefix6
, dest
, 16);
861 p
.prefixlen
= rtm
->rtm_dst_len
;
863 src_p
.family
= AF_INET6
;
864 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
866 "Invalid source prefix length: %u received from kernel route change",
870 memcpy(&src_p
.prefix
, src
, 16);
871 src_p
.prefixlen
= rtm
->rtm_src_len
;
873 /* We only handle the AFs we handle... */
874 if (IS_ZEBRA_DEBUG_KERNEL
)
875 zlog_debug("%s: unknown address-family %u", __func__
,
881 * For ZEBRA_ROUTE_KERNEL types:
883 * The metric/priority of the route received from the kernel
884 * is a 32 bit number. We are going to interpret the high
885 * order byte as the Admin Distance and the low order 3 bytes
888 * This will allow us to do two things:
889 * 1) Allow the creation of kernel routes that can be
890 * overridden by zebra.
891 * 2) Allow the old behavior for 'most' kernel route types
892 * if a user enters 'ip route ...' v4 routes get a metric
893 * of 0 and v6 routes get a metric of 1024. Both of these
894 * values will end up with a admin distance of 0, which
895 * will cause them to win for the purposes of zebra.
897 if (proto
== ZEBRA_ROUTE_KERNEL
) {
898 distance
= (metric
>> 24) & 0xFF;
899 metric
= (metric
& 0x00FFFFFF);
902 if (IS_ZEBRA_DEBUG_KERNEL
) {
903 char buf2
[PREFIX_STRLEN
];
906 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
907 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
908 src_p
.prefixlen
? " from " : "",
909 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
911 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
916 if (rtm
->rtm_family
== AF_INET6
)
919 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
921 if (!tb
[RTA_MULTIPATH
]) {
922 struct nexthop nh
= {0};
925 nh
= parse_nexthop_unicast(
926 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
929 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
930 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
931 distance
, tag
, startup
);
933 /* This is a multipath route */
934 struct route_entry
*re
;
935 struct nexthop_group
*ng
= NULL
;
936 struct rtnexthop
*rtnh
=
937 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
939 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
941 re
->distance
= distance
;
947 re
->uptime
= monotime(NULL
);
954 /* Use temporary list of nexthops; parse
955 * message payload's nexthops.
957 ng
= nexthop_group_new();
959 parse_multipath_nexthops_unicast(
960 ns_id
, ng
, rtm
, rtnh
, tb
,
963 zserv_nexthop_num_warn(
964 __func__
, (const struct prefix
*)&p
,
968 nexthop_group_delete(&ng
);
974 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
975 &src_p
, re
, ng
, startup
);
981 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
982 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
985 if (!tb
[RTA_MULTIPATH
]) {
988 nh
= parse_nexthop_unicast(
989 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
991 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
992 flags
, &p
, &src_p
, &nh
, 0, table
,
993 metric
, distance
, true);
995 /* XXX: need to compare the entire list of
996 * nexthops here for NLM_F_APPEND stupidity */
997 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
998 flags
, &p
, &src_p
, NULL
, 0, table
,
999 metric
, distance
, true);
1007 static struct mcast_route_data
*mroute
= NULL
;
1009 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1010 ns_id_t ns_id
, int startup
)
1014 struct rtattr
*tb
[RTA_MAX
+ 1];
1015 struct mcast_route_data
*m
;
1016 struct mcast_route_data mr
;
1021 char oif_list
[256] = "\0";
1028 memset(&mr
, 0, sizeof(mr
));
1032 rtm
= NLMSG_DATA(h
);
1034 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1036 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1039 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1041 table
= rtm
->rtm_table
;
1043 vrf
= vrf_lookup_by_table(table
, ns_id
);
1046 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1049 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1051 *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1054 *(struct in6_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1058 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
)
1060 *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1063 *(struct in6_addr
*)RTA_DATA(tb
[RTA_DST
]);
1066 if (tb
[RTA_EXPIRES
])
1067 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1069 if (tb
[RTA_MULTIPATH
]) {
1070 struct rtnexthop
*rtnh
=
1071 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1073 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1075 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1078 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1081 if (rtnh
->rtnh_len
== 0)
1084 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1085 rtnh
= RTNH_NEXT(rtnh
);
1089 if (rtm
->rtm_family
== RTNL_FAMILY_IPMR
) {
1090 SET_IPADDR_V4(&m
->src
);
1091 SET_IPADDR_V4(&m
->grp
);
1092 } else if (rtm
->rtm_family
== RTNL_FAMILY_IP6MR
) {
1093 SET_IPADDR_V6(&m
->src
);
1094 SET_IPADDR_V6(&m
->grp
);
1096 zlog_warn("%s: Invalid rtm_family received", __func__
);
1100 if (IS_ZEBRA_DEBUG_KERNEL
) {
1101 struct interface
*ifp
= NULL
;
1102 struct zebra_vrf
*zvrf
= NULL
;
1104 for (count
= 0; count
< oif_count
; count
++) {
1105 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1108 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1109 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1110 strlcat(oif_list
, temp
, sizeof(oif_list
));
1112 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1113 ifp
= if_lookup_by_index(iif
, vrf
);
1115 "MCAST VRF: %s(%d) %s (%pIA,%pIA) IIF: %s(%d) OIF: %s jiffies: %lld",
1116 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1117 &m
->src
, &m
->grp
, ifp
? ifp
->name
: "Unknown", iif
,
1118 oif_list
, m
->lastused
);
1123 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1128 rtm
= NLMSG_DATA(h
);
1130 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1131 /* If this is not route add/delete message print warning. */
1132 zlog_debug("Kernel message: %s NS %u",
1133 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1137 if (!(rtm
->rtm_family
== AF_INET
||
1138 rtm
->rtm_family
== AF_INET6
||
1139 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1141 EC_ZEBRA_UNKNOWN_FAMILY
,
1142 "Invalid address family: %u received from kernel route change: %s",
1143 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1147 /* Connected route. */
1148 if (IS_ZEBRA_DEBUG_KERNEL
)
1149 zlog_debug("%s %s %s proto %s NS %u",
1150 nl_msg_type_to_str(h
->nlmsg_type
),
1151 nl_family_to_str(rtm
->rtm_family
),
1152 nl_rttype_to_str(rtm
->rtm_type
),
1153 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1156 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1159 "%s: Message received from netlink is of a broken size: %d %zu",
1160 __func__
, h
->nlmsg_len
,
1161 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1165 if (rtm
->rtm_type
== RTN_MULTICAST
)
1166 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1168 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1172 /* Request for specific route information from the kernel */
1173 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1180 /* Form the request, specifying filter (rtattr) if needed. */
1181 memset(&req
, 0, sizeof(req
));
1182 req
.n
.nlmsg_type
= type
;
1183 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1184 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1185 req
.rtm
.rtm_family
= family
;
1187 return netlink_request(&zns
->netlink_cmd
, &req
);
1190 /* Routing table read function using netlink interface. Only called
1192 int netlink_route_read(struct zebra_ns
*zns
)
1195 struct zebra_dplane_info dp_info
;
1197 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1199 /* Get IPv4 routing table. */
1200 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1203 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1204 &zns
->netlink_cmd
, &dp_info
, 0, true);
1208 /* Get IPv6 routing table. */
1209 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1212 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1213 &zns
->netlink_cmd
, &dp_info
, 0, true);
1221 * The function returns true if the gateway info could be added
1222 * to the message, otherwise false is returned.
1224 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1226 struct nlmsghdr
*nlmsg
,
1227 size_t req_size
, int bytelen
,
1228 const struct nexthop
*nexthop
)
1230 if (route_family
== AF_MPLS
) {
1231 struct gw_family_t gw_fam
;
1233 gw_fam
.family
= gw_family
;
1234 if (gw_family
== AF_INET
)
1235 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1237 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1238 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1242 if (!(nexthop
->rparent
1243 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1244 if (gw_family
== AF_INET
) {
1245 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1246 &nexthop
->gate
.ipv4
, bytelen
))
1249 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1250 &nexthop
->gate
.ipv6
, bytelen
))
1259 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1260 mpls_lse_t
*out_lse
, char *label_buf
,
1261 size_t label_buf_size
)
1263 char label_buf1
[20];
1266 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1267 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1270 if (IS_ZEBRA_DEBUG_KERNEL
) {
1272 snprintf(label_buf
, label_buf_size
, "label %u",
1273 nh_label
->label
[i
]);
1275 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1276 nh_label
->label
[i
]);
1277 strlcat(label_buf
, label_buf1
, label_buf_size
);
1281 out_lse
[num_labels
] =
1282 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1289 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1290 struct nlmsghdr
*nlmsg
,
1291 size_t buflen
, struct rtmsg
*rtmsg
,
1293 size_t label_buf_size
)
1295 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1299 * label_buf is *only* currently used within debugging.
1300 * As such when we assign it we are guarding it inside
1301 * a debug test. If you want to change this make sure
1302 * you fix this assumption
1304 label_buf
[0] = '\0';
1307 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1310 /* Set the BoS bit */
1311 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1313 if (rtmsg
->rtm_family
== AF_MPLS
) {
1314 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1315 num_labels
* sizeof(mpls_lse_t
)))
1318 struct rtattr
*nest
;
1320 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1321 LWTUNNEL_ENCAP_MPLS
))
1324 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1328 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1330 num_labels
* sizeof(mpls_lse_t
)))
1332 nl_attr_nest_end(nlmsg
, nest
);
1339 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1341 struct nlmsghdr
*nlmsg
,
1342 size_t buflen
, int bytelen
)
1344 if (family
== AF_INET
) {
1345 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1346 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1347 &nexthop
->rmap_src
.ipv4
, bytelen
))
1349 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1350 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1351 &nexthop
->src
.ipv4
, bytelen
))
1354 } else if (family
== AF_INET6
) {
1355 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1356 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1357 &nexthop
->rmap_src
.ipv6
, bytelen
))
1359 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1360 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1361 &nexthop
->src
.ipv6
, bytelen
))
1369 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1370 const struct in6_addr
*seg
)
1372 struct seg6_iptunnel_encap
*ipt
;
1373 struct ipv6_sr_hdr
*srh
;
1374 const size_t srhlen
= 24;
1377 * Caution: Support only SINGLE-SID, not MULTI-SID
1378 * This function only supports the case where segs represents
1379 * a single SID. If you want to extend the SRv6 functionality,
1380 * you should improve the Boundary Check.
1381 * Ex. In case of set a SID-List include multiple-SIDs as an
1382 * argument of the Transit Behavior, we must support variable
1383 * boundary check for buflen.
1385 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1386 sizeof(struct ipv6_sr_hdr
) + 16))
1389 memset(buffer
, 0, buflen
);
1391 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1392 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1394 srh
->hdrlen
= (srhlen
>> 3) - 1;
1396 srh
->segments_left
= 0;
1397 srh
->first_segment
= 0;
1398 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1403 /* This function takes a nexthop as argument and adds
1404 * the appropriate netlink attributes to an existing
1407 * @param routedesc: Human readable description of route type
1408 * (direct/recursive, single-/multipath)
1409 * @param bytelen: Length of addresses in bytes.
1410 * @param nexthop: Nexthop information
1411 * @param nlmsg: nlmsghdr structure to fill in.
1412 * @param req_size: The size allocated for the message.
1414 * The function returns true if the nexthop could be added
1415 * to the message, otherwise false is returned.
1417 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1418 const char *routedesc
, int bytelen
,
1419 const struct nexthop
*nexthop
,
1420 struct nlmsghdr
*nlmsg
,
1421 struct rtmsg
*rtmsg
,
1422 size_t req_size
, int cmd
)
1425 char label_buf
[256];
1427 char addrstr
[INET6_ADDRSTRLEN
];
1431 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1433 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1434 req_size
, rtmsg
, label_buf
,
1438 if (nexthop
->nh_srv6
) {
1439 if (nexthop
->nh_srv6
->seg6local_action
!=
1440 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1441 struct rtattr
*nest
;
1442 const struct seg6local_context
*ctx
;
1444 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1445 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1446 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1449 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1453 switch (nexthop
->nh_srv6
->seg6local_action
) {
1454 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1455 if (!nl_attr_put32(nlmsg
, req_size
,
1457 SEG6_LOCAL_ACTION_END
))
1460 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1461 if (!nl_attr_put32(nlmsg
, req_size
,
1463 SEG6_LOCAL_ACTION_END_X
))
1465 if (!nl_attr_put(nlmsg
, req_size
,
1466 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1467 sizeof(struct in6_addr
)))
1470 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1471 if (!nl_attr_put32(nlmsg
, req_size
,
1473 SEG6_LOCAL_ACTION_END_T
))
1475 if (!nl_attr_put32(nlmsg
, req_size
,
1480 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1481 if (!nl_attr_put32(nlmsg
, req_size
,
1483 SEG6_LOCAL_ACTION_END_DX4
))
1485 if (!nl_attr_put(nlmsg
, req_size
,
1486 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1487 sizeof(struct in_addr
)))
1490 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1491 if (!nl_attr_put32(nlmsg
, req_size
,
1493 SEG6_LOCAL_ACTION_END_DT6
))
1495 if (!nl_attr_put32(nlmsg
, req_size
,
1500 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1501 if (!nl_attr_put32(nlmsg
, req_size
,
1503 SEG6_LOCAL_ACTION_END_DT4
))
1505 if (!nl_attr_put32(nlmsg
, req_size
,
1506 SEG6_LOCAL_VRFTABLE
,
1511 zlog_err("%s: unsupport seg6local behaviour action=%u",
1513 nexthop
->nh_srv6
->seg6local_action
);
1516 nl_attr_nest_end(nlmsg
, nest
);
1519 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1522 struct rtattr
*nest
;
1524 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1525 LWTUNNEL_ENCAP_SEG6
))
1527 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1530 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1531 &nexthop
->nh_srv6
->seg6_segs
);
1534 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1537 nl_attr_nest_end(nlmsg
, nest
);
1541 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1542 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1544 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1545 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1546 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1548 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1551 if (cmd
== RTM_NEWROUTE
) {
1552 if (!_netlink_route_encode_nexthop_src(
1553 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1557 if (IS_ZEBRA_DEBUG_KERNEL
)
1558 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1559 __func__
, routedesc
, p
, ipv4_ll_buf
,
1560 label_buf
, nexthop
->ifindex
,
1561 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1565 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1566 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1567 /* Send deletes to the kernel without specifying the next-hop */
1568 if (cmd
!= RTM_DELROUTE
) {
1569 if (!_netlink_route_add_gateway_info(
1570 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1575 if (cmd
== RTM_NEWROUTE
) {
1576 if (!_netlink_route_encode_nexthop_src(
1577 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1581 if (IS_ZEBRA_DEBUG_KERNEL
) {
1582 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1584 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1585 __func__
, routedesc
, p
, addrstr
, label_buf
,
1586 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1591 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1592 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1593 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1594 AF_INET6
, nlmsg
, req_size
,
1598 if (cmd
== RTM_NEWROUTE
) {
1599 if (!_netlink_route_encode_nexthop_src(
1600 nexthop
, AF_INET6
, nlmsg
, req_size
,
1605 if (IS_ZEBRA_DEBUG_KERNEL
) {
1606 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1608 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1609 __func__
, routedesc
, p
, addrstr
, label_buf
,
1610 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1616 * We have the ifindex so we should always send it
1617 * This is especially useful if we are doing route
1620 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1621 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1625 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1626 if (cmd
== RTM_NEWROUTE
) {
1627 if (!_netlink_route_encode_nexthop_src(
1628 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1632 if (IS_ZEBRA_DEBUG_KERNEL
)
1633 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1634 __func__
, routedesc
, p
, nexthop
->ifindex
,
1635 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1641 /* This function takes a nexthop as argument and
1642 * appends to the given netlink msg. If the nexthop
1643 * defines a preferred source, the src parameter
1644 * will be modified to point to that src, otherwise
1645 * it will be kept unmodified.
1647 * @param routedesc: Human readable description of route type
1648 * (direct/recursive, single-/multipath)
1649 * @param bytelen: Length of addresses in bytes.
1650 * @param nexthop: Nexthop information
1651 * @param nlmsg: nlmsghdr structure to fill in.
1652 * @param req_size: The size allocated for the message.
1653 * @param src: pointer pointing to a location where
1654 * the prefsrc should be stored.
1656 * The function returns true if the nexthop could be added
1657 * to the message, otherwise false is returned.
1659 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1660 const char *routedesc
, int bytelen
,
1661 const struct nexthop
*nexthop
,
1662 struct nlmsghdr
*nlmsg
,
1663 size_t req_size
, struct rtmsg
*rtmsg
,
1664 const union g_addr
**src
)
1666 char label_buf
[256];
1668 struct rtnexthop
*rtnh
;
1670 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1676 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1678 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1679 req_size
, rtmsg
, label_buf
,
1683 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1684 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1686 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1687 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1688 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1690 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1691 if (nexthop
->weight
)
1692 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1694 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1695 *src
= &nexthop
->rmap_src
;
1696 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1697 *src
= &nexthop
->src
;
1699 if (IS_ZEBRA_DEBUG_KERNEL
)
1701 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1702 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1703 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1705 nl_attr_rtnh_end(nlmsg
, rtnh
);
1709 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1710 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1711 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1712 nlmsg
, req_size
, bytelen
,
1716 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1717 *src
= &nexthop
->rmap_src
;
1718 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1719 *src
= &nexthop
->src
;
1721 if (IS_ZEBRA_DEBUG_KERNEL
)
1722 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1723 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1724 label_buf
, nexthop
->ifindex
,
1725 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1727 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1728 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1729 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1730 AF_INET6
, nlmsg
, req_size
,
1734 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1735 *src
= &nexthop
->rmap_src
;
1736 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1737 *src
= &nexthop
->src
;
1739 if (IS_ZEBRA_DEBUG_KERNEL
)
1740 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1741 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1742 label_buf
, nexthop
->ifindex
,
1743 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1747 * We have figured out the ifindex so we should always send it
1748 * This is especially useful if we are doing route
1751 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1752 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1755 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1756 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1757 *src
= &nexthop
->rmap_src
;
1758 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1759 *src
= &nexthop
->src
;
1761 if (IS_ZEBRA_DEBUG_KERNEL
)
1762 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1763 __func__
, routedesc
, p
, nexthop
->ifindex
,
1764 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1767 if (nexthop
->weight
)
1768 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1770 nl_attr_rtnh_end(nlmsg
, rtnh
);
1775 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1776 const struct zebra_nhlfe
*nhlfe
,
1777 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1778 size_t req_size
, int cmd
)
1783 family
= NHLFE_FAMILY(nhlfe
);
1784 bytelen
= (family
== AF_INET
? 4 : 16);
1785 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1786 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1792 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1793 const struct zebra_nhlfe
*nhlfe
,
1794 struct nlmsghdr
*nlmsg
, size_t req_size
,
1795 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1800 family
= NHLFE_FAMILY(nhlfe
);
1801 bytelen
= (family
== AF_INET
? 4 : 16);
1802 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1803 nhlfe
->nexthop
, nlmsg
, req_size
,
1807 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1809 if (IS_ZEBRA_DEBUG_KERNEL
)
1810 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1811 routedesc
, nl_msg_type_to_str(cmd
), label
);
1814 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1815 int llalen
, ns_id_t ns_id
, uint8_t family
,
1816 bool permanent
, uint8_t protocol
)
1824 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1826 memset(&req
, 0, sizeof(req
));
1828 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1829 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1830 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1831 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1833 req
.ndm
.ndm_family
= family
;
1834 req
.ndm
.ndm_ifindex
= ifindex
;
1835 req
.ndm
.ndm_type
= RTN_UNICAST
;
1836 if (cmd
== RTM_NEWNEIGH
) {
1838 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1840 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1842 req
.ndm
.ndm_state
= NUD_FAILED
;
1844 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1846 req
.ndm
.ndm_type
= RTN_UNICAST
;
1847 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
1848 family2addrsize(family
));
1850 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1852 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1856 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1859 if (family
== AF_INET
) {
1860 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1861 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1863 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1864 src
->ipv4
= nexthop
->src
.ipv4
;
1867 } else if (family
== AF_INET6
) {
1868 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1869 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1871 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1872 src
->ipv6
= nexthop
->src
.ipv6
;
1881 * The function returns true if the attribute could be added
1882 * to the message, otherwise false is returned.
1884 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1887 struct rtattr
*nest
;
1889 switch (nh
->nh_encap_type
) {
1891 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1894 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1898 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1901 nl_attr_nest_end(n
, nest
);
1909 * Routing table change via netlink interface, using a dataplane context object
1911 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1912 * otherwise the number of bytes written to buf.
1914 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1915 struct zebra_dplane_ctx
*ctx
,
1916 uint8_t *data
, size_t datalen
,
1917 bool fpm
, bool force_nhg
)
1920 struct nexthop
*nexthop
= NULL
;
1921 unsigned int nexthop_num
;
1922 const char *routedesc
;
1923 bool setsrc
= false;
1925 const struct prefix
*p
, *src_p
;
1933 } *req
= (void *)data
;
1935 p
= dplane_ctx_get_dest(ctx
);
1936 src_p
= dplane_ctx_get_src(ctx
);
1938 if (datalen
< sizeof(*req
))
1941 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1943 memset(req
, 0, sizeof(*req
));
1945 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1947 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1948 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1950 if ((cmd
== RTM_NEWROUTE
) &&
1951 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1952 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1954 req
->n
.nlmsg_type
= cmd
;
1956 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
1958 req
->r
.rtm_family
= p
->family
;
1959 req
->r
.rtm_dst_len
= p
->prefixlen
;
1960 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1961 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1963 if (cmd
== RTM_DELROUTE
)
1964 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1966 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1969 * blackhole routes are not RTN_UNICAST, they are
1970 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1971 * so setting this value as a RTN_UNICAST would
1972 * cause the route lookup of just the prefix
1973 * to fail. So no need to specify this for
1974 * the RTM_DELROUTE case
1976 if (cmd
!= RTM_DELROUTE
)
1977 req
->r
.rtm_type
= RTN_UNICAST
;
1979 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1982 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1988 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1990 * either by the kernel or by zebra. Its purely for calculating best
1992 * by the routing protocol and for communicating with protocol peers.
1994 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1995 NL_DEFAULT_ROUTE_METRIC
))
1998 #if defined(SUPPORT_REALMS)
2002 if (cmd
== RTM_DELROUTE
)
2003 tag
= dplane_ctx_get_old_tag(ctx
);
2005 tag
= dplane_ctx_get_tag(ctx
);
2007 if (tag
> 0 && tag
<= 255) {
2008 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
2013 /* Table corresponding to this route. */
2014 table_id
= dplane_ctx_get_table(ctx
);
2016 req
->r
.rtm_table
= table_id
;
2018 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
2019 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
2023 if (IS_ZEBRA_DEBUG_KERNEL
)
2025 "%s: %s %pFX vrf %u(%u)", __func__
,
2026 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2030 * If we are not updating the route and we have received
2031 * a route delete, then all we need to fill in is the
2032 * prefix information to tell the kernel to schwack
2035 if (cmd
== RTM_DELROUTE
)
2036 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2038 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2039 struct rtattr
*nest
;
2040 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2041 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2043 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2046 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2050 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2052 nl_attr_nest_end(&req
->n
, nest
);
2056 * Always install blackhole routes without using nexthops, because of
2057 * the following kernel problems:
2058 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2059 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2061 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2063 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2064 nexthop
= nexthop
->resolved
;
2066 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2067 switch (nexthop
->bh_type
) {
2068 case BLACKHOLE_ADMINPROHIB
:
2069 req
->r
.rtm_type
= RTN_PROHIBIT
;
2071 case BLACKHOLE_REJECT
:
2072 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2075 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2078 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2082 if ((!fpm
&& kernel_nexthops_supported()
2083 && (!proto_nexthops_only()
2084 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2085 || (fpm
&& force_nhg
)) {
2086 /* Kernel supports nexthop objects */
2087 if (IS_ZEBRA_DEBUG_KERNEL
)
2088 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2089 dplane_ctx_get_nhe_id(ctx
));
2091 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2092 dplane_ctx_get_nhe_id(ctx
)))
2095 /* Have to determine src still */
2096 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2100 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2104 if (p
->family
== AF_INET
) {
2105 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2106 &src
.ipv4
, bytelen
))
2108 } else if (p
->family
== AF_INET6
) {
2109 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2110 &src
.ipv6
, bytelen
))
2115 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2118 /* Count overall nexthops so we can decide whether to use singlepath
2119 * or multipath case.
2122 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2123 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2125 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2131 /* Singlepath case. */
2132 if (nexthop_num
== 1) {
2134 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2135 if (CHECK_FLAG(nexthop
->flags
,
2136 NEXTHOP_FLAG_RECURSIVE
)) {
2141 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2146 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2147 routedesc
= nexthop
->rparent
2148 ? "recursive, single-path"
2151 if (!_netlink_route_build_singlepath(
2152 p
, routedesc
, bytelen
, nexthop
,
2153 &req
->n
, &req
->r
, datalen
, cmd
))
2160 * Add encapsulation information when installing via
2164 if (!netlink_route_nexthop_encap(
2165 &req
->n
, datalen
, nexthop
))
2171 if (p
->family
== AF_INET
) {
2172 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2173 &src
.ipv4
, bytelen
))
2175 } else if (p
->family
== AF_INET6
) {
2176 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2177 &src
.ipv6
, bytelen
))
2181 } else { /* Multipath case */
2182 struct rtattr
*nest
;
2183 const union g_addr
*src1
= NULL
;
2185 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2190 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2191 if (CHECK_FLAG(nexthop
->flags
,
2192 NEXTHOP_FLAG_RECURSIVE
)) {
2193 /* This only works for IPv4 now */
2197 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2202 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2203 routedesc
= nexthop
->rparent
2204 ? "recursive, multipath"
2208 if (!_netlink_route_build_multipath(
2209 p
, routedesc
, bytelen
, nexthop
,
2210 &req
->n
, datalen
, &req
->r
, &src1
))
2213 if (!setsrc
&& src1
) {
2214 if (p
->family
== AF_INET
)
2215 src
.ipv4
= src1
->ipv4
;
2216 else if (p
->family
== AF_INET6
)
2217 src
.ipv6
= src1
->ipv6
;
2224 nl_attr_nest_end(&req
->n
, nest
);
2227 * Add encapsulation information when installing via
2231 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2233 if (CHECK_FLAG(nexthop
->flags
,
2234 NEXTHOP_FLAG_RECURSIVE
))
2236 if (!netlink_route_nexthop_encap(
2237 &req
->n
, datalen
, nexthop
))
2244 if (p
->family
== AF_INET
) {
2245 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2246 &src
.ipv4
, bytelen
))
2248 } else if (p
->family
== AF_INET6
) {
2249 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2250 &src
.ipv6
, bytelen
))
2253 if (IS_ZEBRA_DEBUG_KERNEL
)
2254 zlog_debug("Setting source");
2258 /* If there is no useful nexthop then return. */
2259 if (nexthop_num
== 0) {
2260 if (IS_ZEBRA_DEBUG_KERNEL
)
2261 zlog_debug("%s: No useful nexthop.", __func__
);
2264 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2267 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2269 uint32_t actual_table
;
2271 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2279 struct zebra_ns
*zns
;
2282 memset(&req
, 0, sizeof(req
));
2284 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2285 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2286 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2288 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2290 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2291 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2293 if (mroute
->family
== AF_INET
) {
2294 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2295 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2296 &mroute
->src
.ipaddr_v4
,
2297 sizeof(mroute
->src
.ipaddr_v4
));
2298 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2299 &mroute
->grp
.ipaddr_v4
,
2300 sizeof(mroute
->grp
.ipaddr_v4
));
2302 req
.ndm
.ndm_family
= RTNL_FAMILY_IP6MR
;
2303 nl_attr_put(&req
.n
, sizeof(req
), RTA_SRC
,
2304 &mroute
->src
.ipaddr_v6
,
2305 sizeof(mroute
->src
.ipaddr_v6
));
2306 nl_attr_put(&req
.n
, sizeof(req
), RTA_DST
,
2307 &mroute
->grp
.ipaddr_v6
,
2308 sizeof(mroute
->grp
.ipaddr_v6
));
2314 * So during the namespace cleanup we started storing
2315 * the zvrf table_id for the default table as RT_TABLE_MAIN
2316 * which is what the normal routing table for ip routing is.
2317 * This change caused this to break our lookups of sg data
2318 * because prior to this change the zvrf->table_id was 0
2319 * and when the pim multicast kernel code saw a 0,
2320 * it was auto-translated to RT_TABLE_DEFAULT. But since
2321 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2322 * and the kernel goes screw you and the delicious cookies you
2323 * are trying to give me. So now we have this little hack.
2325 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2327 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2329 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2330 &zns
->netlink_cmd
, zns
, false);
2336 /* Char length to debug ID with */
2337 #define ID_LENGTH 10
2339 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2341 const struct nh_grp
*z_grp
,
2342 const uint8_t count
)
2344 struct nexthop_grp grp
[count
];
2345 /* Need space for max group size, "/", and null term */
2346 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2347 char buf1
[ID_LENGTH
+ 2];
2351 memset(grp
, 0, sizeof(grp
));
2354 for (int i
= 0; i
< count
; i
++) {
2355 grp
[i
].id
= z_grp
[i
].id
;
2356 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2358 if (IS_ZEBRA_DEBUG_KERNEL
) {
2360 snprintf(buf
, sizeof(buf1
), "group %u",
2363 snprintf(buf1
, sizeof(buf1
), "/%u",
2365 strlcat(buf
, buf1
, sizeof(buf
));
2369 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2370 count
* sizeof(*grp
)))
2374 if (IS_ZEBRA_DEBUG_KERNEL
)
2375 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2381 * Next hop packet encoding helper function.
2383 * \param[in] cmd netlink command.
2384 * \param[in] ctx dataplane context (information snapshot).
2385 * \param[out] buf buffer to hold the packet.
2386 * \param[in] buflen amount of buffer bytes.
2388 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2389 * otherwise the number of bytes written to buf.
2391 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2392 const struct zebra_dplane_ctx
*ctx
,
2393 void *buf
, size_t buflen
)
2401 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2402 char label_buf
[256];
2404 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2405 int type
= dplane_ctx_get_nhe_type(ctx
);
2406 struct rtattr
*nest
;
2409 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2413 EC_ZEBRA_NHG_FIB_UPDATE
,
2414 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2419 * Nothing to do if the kernel doesn't support nexthop objects or
2420 * we dont want to install this type of NHG
2422 if (!kernel_nexthops_supported()) {
2423 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2425 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2426 __func__
, id
, zebra_route_string(type
));
2430 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2431 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2433 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2434 __func__
, id
, zebra_route_string(type
));
2438 label_buf
[0] = '\0';
2440 if (buflen
< sizeof(*req
))
2443 memset(req
, 0, sizeof(*req
));
2445 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2446 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2448 if (cmd
== RTM_NEWNEXTHOP
)
2449 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2451 req
->n
.nlmsg_type
= cmd
;
2452 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2454 req
->nhm
.nh_family
= AF_UNSPEC
;
2457 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2460 if (cmd
== RTM_NEWNEXTHOP
) {
2462 * We distinguish between a "group", which is a collection
2463 * of ids, and a singleton nexthop with an id. The
2464 * group is installed as an id that just refers to a list of
2467 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2468 if (!_netlink_nexthop_build_group(
2469 &req
->n
, buflen
, id
,
2470 dplane_ctx_get_nhe_nh_grp(ctx
),
2471 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2474 const struct nexthop
*nh
=
2475 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2476 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2479 req
->nhm
.nh_family
= AF_INET
;
2480 else if (afi
== AFI_IP6
)
2481 req
->nhm
.nh_family
= AF_INET6
;
2484 case NEXTHOP_TYPE_IPV4
:
2485 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2486 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2491 case NEXTHOP_TYPE_IPV6
:
2492 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2493 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2498 case NEXTHOP_TYPE_BLACKHOLE
:
2499 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2502 /* Blackhole shouldn't have anymore attributes
2505 case NEXTHOP_TYPE_IFINDEX
:
2506 /* Don't need anymore info for this */
2512 EC_ZEBRA_NHG_FIB_UPDATE
,
2513 "Context received for kernel nexthop update without an interface");
2517 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2521 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2522 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2525 build_label_stack(nh
->nh_label
, out_lse
,
2526 label_buf
, sizeof(label_buf
));
2529 /* Set the BoS bit */
2530 out_lse
[num_labels
- 1] |=
2531 htonl(1 << MPLS_LS_S_SHIFT
);
2534 * TODO: MPLS unsupported for now in kernel.
2536 if (req
->nhm
.nh_family
== AF_MPLS
)
2539 encap
= LWTUNNEL_ENCAP_MPLS
;
2540 if (!nl_attr_put16(&req
->n
, buflen
,
2541 NHA_ENCAP_TYPE
, encap
))
2543 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2547 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2549 num_labels
* sizeof(mpls_lse_t
)))
2552 nl_attr_nest_end(&req
->n
, nest
);
2556 if (nh
->nh_srv6
->seg6local_action
!=
2557 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2560 struct rtattr
*nest
;
2561 const struct seg6local_context
*ctx
;
2563 req
->nhm
.nh_family
= AF_INET6
;
2564 action
= nh
->nh_srv6
->seg6local_action
;
2565 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2566 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2567 if (!nl_attr_put(&req
->n
, buflen
,
2573 nest
= nl_attr_nest(&req
->n
, buflen
,
2574 NHA_ENCAP
| NLA_F_NESTED
);
2579 case SEG6_LOCAL_ACTION_END
:
2583 SEG6_LOCAL_ACTION_END
))
2586 case SEG6_LOCAL_ACTION_END_X
:
2590 SEG6_LOCAL_ACTION_END_X
))
2594 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2595 sizeof(struct in6_addr
)))
2598 case SEG6_LOCAL_ACTION_END_T
:
2602 SEG6_LOCAL_ACTION_END_T
))
2610 case SEG6_LOCAL_ACTION_END_DX4
:
2614 SEG6_LOCAL_ACTION_END_DX4
))
2618 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2619 sizeof(struct in_addr
)))
2622 case SEG6_LOCAL_ACTION_END_DT6
:
2626 SEG6_LOCAL_ACTION_END_DT6
))
2634 case SEG6_LOCAL_ACTION_END_DT4
:
2638 SEG6_LOCAL_ACTION_END_DT4
))
2642 SEG6_LOCAL_VRFTABLE
,
2647 zlog_err("%s: unsupport seg6local behaviour action=%u",
2651 nl_attr_nest_end(&req
->n
, nest
);
2654 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2657 struct rtattr
*nest
;
2659 if (!nl_attr_put16(&req
->n
, buflen
,
2661 LWTUNNEL_ENCAP_SEG6
))
2663 nest
= nl_attr_nest(&req
->n
, buflen
,
2664 NHA_ENCAP
| NLA_F_NESTED
);
2667 tun_len
= fill_seg6ipt_encap(tun_buf
,
2669 &nh
->nh_srv6
->seg6_segs
);
2672 if (!nl_attr_put(&req
->n
, buflen
,
2676 nl_attr_nest_end(&req
->n
, nest
);
2682 if (IS_ZEBRA_DEBUG_KERNEL
)
2683 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2684 __func__
, id
, nh
, nh
->ifindex
,
2685 vrf_id_to_name(nh
->vrf_id
),
2686 nh
->vrf_id
, label_buf
);
2689 req
->nhm
.nh_protocol
= zebra2proto(type
);
2691 } else if (cmd
!= RTM_DELNEXTHOP
) {
2693 EC_ZEBRA_NHG_FIB_UPDATE
,
2694 "Nexthop group kernel update command (%d) does not exist",
2699 if (IS_ZEBRA_DEBUG_KERNEL
)
2700 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2703 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2706 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2707 void *buf
, size_t buflen
)
2709 enum dplane_op_e op
;
2712 op
= dplane_ctx_get_op(ctx
);
2713 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2714 cmd
= RTM_NEWNEXTHOP
;
2715 else if (op
== DPLANE_OP_NH_DELETE
)
2716 cmd
= RTM_DELNEXTHOP
;
2718 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2719 "Context received for kernel nexthop update with incorrect OP code (%u)",
2724 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2727 enum netlink_msg_status
2728 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2729 struct zebra_dplane_ctx
*ctx
)
2731 /* Nothing to do if the kernel doesn't support nexthop objects */
2732 if (!kernel_nexthops_supported())
2733 return FRR_NETLINK_SUCCESS
;
2735 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2739 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2740 void *buf
, size_t buflen
)
2742 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2743 buflen
, false, false);
2746 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2747 void *buf
, size_t buflen
)
2749 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2750 buflen
, false, false);
2753 enum netlink_msg_status
2754 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2757 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2759 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2761 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2763 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2765 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2766 /* Single 'replace' operation */
2769 * With route replace semantics in place
2770 * for v4 routes and the new route is a system
2771 * route we do not install anything.
2772 * The problem here is that the new system
2773 * route should cause us to withdraw from
2774 * the kernel the old non-system route
2776 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2777 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2778 return netlink_batch_add_msg(
2779 bth
, ctx
, netlink_delroute_msg_encoder
,
2783 * So v6 route replace semantics are not in
2784 * the kernel at this point as I understand it.
2785 * so let's do a delete then an add.
2786 * In the future once v6 route replace semantics
2787 * are in we can figure out what to do here to
2788 * allow working with old and new kernels.
2790 * I'm also intentionally ignoring the failure case
2791 * of the route delete. If that happens yeah we're
2794 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2795 netlink_batch_add_msg(
2796 bth
, ctx
, netlink_delroute_msg_encoder
,
2802 return FRR_NETLINK_ERROR
;
2804 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2805 return FRR_NETLINK_SUCCESS
;
2807 return netlink_batch_add_msg(bth
, ctx
,
2809 ? netlink_newroute_msg_encoder
2810 : netlink_delroute_msg_encoder
,
2815 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2817 * @tb: Netlink RTA data
2818 * @family: Address family in the nhmsg
2819 * @ifp: Interface connected - this should be NULL, we fill it in
2820 * @ns_id: Namspace id
2822 * Return: New nexthop
2824 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2825 unsigned char family
,
2826 struct interface
**ifp
,
2829 struct nexthop nh
= {};
2831 enum nexthop_types_t type
= 0;
2834 struct interface
*ifp_lookup
;
2836 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2839 if (tb
[NHA_GATEWAY
]) {
2842 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2846 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2851 EC_ZEBRA_BAD_NHG_MESSAGE
,
2852 "Nexthop gateway with bad address family (%d) received from kernel",
2856 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2858 type
= NEXTHOP_TYPE_IFINDEX
;
2864 memcpy(&(nh
.gate
), gate
, sz
);
2867 nh
.ifindex
= if_index
;
2870 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2875 nh
.vrf_id
= ifp_lookup
->vrf
->vrf_id
;
2878 EC_ZEBRA_UNKNOWN_INTERFACE
,
2879 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2880 __func__
, nh
.ifindex
);
2882 nh
.vrf_id
= VRF_DEFAULT
;
2885 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2886 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2889 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2891 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2892 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2895 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2902 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2903 struct nh_grp
*z_grp
, int z_grp_size
)
2906 /* linux/nexthop.h group struct */
2907 struct nexthop_grp
*n_grp
= NULL
;
2909 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2910 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2912 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2913 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2914 "Invalid nexthop group received from the kernel");
2918 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2919 z_grp
[i
].id
= n_grp
[i
].id
;
2920 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2926 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2928 * @h: Netlink message header
2929 * @ns_id: Namspace id
2930 * @startup: Are we reading under startup conditions?
2932 * Return: Result status
2934 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2937 /* nexthop group id */
2939 unsigned char family
;
2941 afi_t afi
= AFI_UNSPEC
;
2942 vrf_id_t vrf_id
= VRF_DEFAULT
;
2943 struct interface
*ifp
= NULL
;
2944 struct nhmsg
*nhm
= NULL
;
2945 struct nexthop nh
= {};
2946 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2947 /* Count of nexthops in group array */
2948 uint8_t grp_count
= 0;
2949 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2951 frrtrace(3, frr_zebra
, netlink_nexthop_change
, h
, ns_id
, startup
);
2953 nhm
= NLMSG_DATA(h
);
2958 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2961 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2964 "%s: Message received from netlink is of a broken size %d %zu",
2965 __func__
, h
->nlmsg_len
,
2966 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2970 netlink_parse_rtattr_flags(tb
, NHA_MAX
, RTM_NHA(nhm
), len
,
2976 EC_ZEBRA_BAD_NHG_MESSAGE
,
2977 "Nexthop group without an ID received from the kernel");
2981 /* We use the ID key'd nhg table for kernel updates */
2982 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2984 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2985 /* If this is a L2 NH just ignore it */
2986 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2987 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2993 family
= nhm
->nh_family
;
2994 afi
= family2afi(family
);
2996 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2998 if (IS_ZEBRA_DEBUG_KERNEL
)
2999 zlog_debug("%s ID (%u) %s NS %u",
3000 nl_msg_type_to_str(h
->nlmsg_type
), id
,
3001 nl_family_to_str(family
), ns_id
);
3004 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
3005 if (tb
[NHA_GROUP
]) {
3007 * If this is a group message its only going to have
3008 * an array of nexthop IDs associated with it
3010 grp_count
= netlink_nexthop_process_group(
3011 tb
, grp
, array_size(grp
));
3013 if (tb
[NHA_BLACKHOLE
]) {
3015 * This nexthop is just for blackhole-ing
3016 * traffic, it should not have an OIF, GATEWAY,
3019 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
3020 nh
.bh_type
= BLACKHOLE_UNSPEC
;
3021 } else if (tb
[NHA_OIF
])
3023 * This is a true new nexthop, so we need
3024 * to parse the gateway and device info
3026 nh
= netlink_nexthop_process_nh(tb
, family
,
3031 EC_ZEBRA_BAD_NHG_MESSAGE
,
3032 "Invalid Nexthop message received from the kernel with ID (%u)",
3036 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
3037 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
3038 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
3042 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
3046 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
3047 zebra_nhg_kernel_del(id
, vrf_id
);
3053 * netlink_request_nexthop() - Request nextop information from the kernel
3054 * @zns: Zebra namespace
3055 * @family: AF_* netlink family
3056 * @type: RTM_* route type
3058 * Return: Result status
3060 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3067 /* Form the request, specifying filter (rtattr) if needed. */
3068 memset(&req
, 0, sizeof(req
));
3069 req
.n
.nlmsg_type
= type
;
3070 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3071 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3072 req
.nhm
.nh_family
= family
;
3074 return netlink_request(&zns
->netlink_cmd
, &req
);
3079 * netlink_nexthop_read() - Nexthop read function using netlink interface
3081 * @zns: Zebra name space
3083 * Return: Result status
3084 * Only called at bootstrap time.
3086 int netlink_nexthop_read(struct zebra_ns
*zns
)
3089 struct zebra_dplane_info dp_info
;
3091 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3093 /* Get nexthop objects */
3094 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3097 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3101 /* If we succesfully read in nexthop objects,
3102 * this kernel must support them.
3105 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3106 zlog_debug("Nexthop objects %ssupported on this kernel",
3107 supports_nh
? "" : "not ");
3109 zebra_router_set_supports_nhgs(supports_nh
);
3115 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3116 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3118 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3119 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3124 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3125 * evpn neighbor update as netlink messages using dataplane context object.
3126 * Here, a neighbor refers to a bridge forwarding database entry for
3127 * either unicast forwarding or head-end replication or an IP neighbor
3129 * @ctx: Dataplane context
3130 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3131 * @lla: A pointer to neighbor cache link layer address
3132 * @llalen: Length of the pointer to neighbor cache link layer
3134 * @ip: A neighbor cache n/w layer destination address
3135 * In the case of bridge FDB, this represnts the remote
3137 * @replace_obj: Whether NEW request should replace existing object or
3138 * add to the end of the list
3139 * @family: AF_* netlink family
3140 * @type: RTN_* route type
3141 * @flags: NTF_* flags
3142 * @state: NUD_* states
3143 * @data: data buffer pointer
3144 * @datalen: total amount of data buffer space
3145 * @protocol: protocol information
3147 * Return: 0 when the msg doesn't fit entirely in the buffer
3148 * otherwise the number of bytes written to buf.
3150 static ssize_t
netlink_neigh_update_msg_encode(
3151 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3152 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3153 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3154 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3155 size_t datalen
, uint8_t protocol
)
3163 enum dplane_op_e op
;
3165 if (datalen
< sizeof(*req
))
3167 memset(req
, 0, sizeof(*req
));
3169 op
= dplane_ctx_get_op(ctx
);
3171 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3172 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3173 if (cmd
== RTM_NEWNEIGH
)
3174 req
->n
.nlmsg_flags
|=
3176 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3177 req
->n
.nlmsg_type
= cmd
;
3178 req
->ndm
.ndm_family
= family
;
3179 req
->ndm
.ndm_type
= type
;
3180 req
->ndm
.ndm_state
= state
;
3181 req
->ndm
.ndm_flags
= flags
;
3182 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3184 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3189 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3194 struct rtattr
*nest
;
3196 nest
= nl_attr_nest(&req
->n
, datalen
,
3197 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3201 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3202 &nfy_flags
, sizeof(nfy_flags
)))
3204 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3207 nl_attr_nest_end(&req
->n
, nest
);
3212 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3218 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3222 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3223 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3228 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3229 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3232 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3236 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3237 dplane_ctx_mac_get_br_ifindex(ctx
)))
3241 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3245 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3246 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3249 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3250 void *buf
, size_t buflen
)
3252 struct ethaddr dst_mac
= {.octet
= {0}};
3253 int proto
= RTPROT_ZEBRA
;
3255 if (dplane_ctx_get_type(ctx
) != 0)
3256 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3258 return netlink_neigh_update_msg_encode(
3259 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3260 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3261 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3262 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3267 #define NDA_RTA(r) \
3268 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3271 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3274 struct interface
*ifp
;
3275 struct zebra_if
*zif
;
3276 struct rtattr
*tb
[NDA_MAX
+ 1];
3277 struct interface
*br_if
;
3280 struct in_addr vtep_ip
;
3281 int vid_present
= 0, dst_present
= 0;
3285 bool local_inactive
= false;
3286 bool dp_static
= false;
3287 uint32_t nhg_id
= 0;
3289 ndm
= NLMSG_DATA(h
);
3291 /* We only process macfdb notifications if EVPN is enabled */
3292 if (!is_evpn_enabled())
3295 /* Parse attributes and extract fields of interest. Do basic
3296 * validation of the fields.
3298 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3301 if (!tb
[NDA_LLADDR
]) {
3302 if (IS_ZEBRA_DEBUG_KERNEL
)
3303 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3304 nl_msg_type_to_str(h
->nlmsg_type
),
3309 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3310 if (IS_ZEBRA_DEBUG_KERNEL
)
3312 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3313 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3314 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3318 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3322 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3323 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3327 /* TODO: Only IPv4 supported now. */
3329 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3331 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3336 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3338 if (ndm
->ndm_state
& NUD_STALE
)
3339 local_inactive
= true;
3341 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3342 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3343 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3345 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3346 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3349 nfy_flags
= *(uint8_t *)RTA_DATA(
3350 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3351 if (nfy_flags
& FDB_NOTIFY_BIT
)
3353 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3354 local_inactive
= true;
3358 if (IS_ZEBRA_DEBUG_KERNEL
)
3359 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
3360 nl_msg_type_to_str(h
->nlmsg_type
),
3361 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
3362 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
3363 dst_present
? dst_buf
: "", nhg_id
);
3365 /* The interface should exist. */
3366 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3368 if (!ifp
|| !ifp
->info
)
3371 /* The interface should be something we're interested in. */
3372 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3375 zif
= (struct zebra_if
*)ifp
->info
;
3376 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3377 if (IS_ZEBRA_DEBUG_KERNEL
)
3379 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3380 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3382 zif
->brslave_info
.bridge_ifindex
);
3386 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3388 if (filter_vlan
&& vid
!= filter_vlan
) {
3389 if (IS_ZEBRA_DEBUG_KERNEL
)
3390 zlog_debug(" Filtered due to filter vlan: %d",
3395 /* If add or update, do accordingly if learnt on a "local" interface; if
3396 * the notification is over VxLAN, this has to be related to
3398 * so perform an implicit delete of any local entry (if it exists).
3400 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3401 /* Drop "permanent" entries. */
3402 if (ndm
->ndm_state
& NUD_PERMANENT
) {
3403 if (IS_ZEBRA_DEBUG_KERNEL
)
3405 " Dropping entry because of NUD_PERMANENT");
3409 if (IS_ZEBRA_IF_VXLAN(ifp
))
3410 return zebra_vxlan_dp_network_mac_add(
3411 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
3412 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3414 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3415 sticky
, local_inactive
, dp_static
);
3418 /* This is a delete notification.
3419 * Ignore the notification with IP dest as it may just signify that the
3420 * MAC has moved from remote to local. The exception is the special
3421 * all-zeros MAC that represents the BUM flooding entry; we may have
3422 * to readd it. Otherwise,
3423 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3424 * 2. For a MAC over "local" interface, delete the mac
3425 * Note: We will get notifications from both bridge driver and VxLAN
3432 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3434 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
3435 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3439 if (IS_ZEBRA_IF_VXLAN(ifp
))
3440 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3442 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3445 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3450 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3453 /* Length validity. */
3454 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3458 /* We are interested only in AF_BRIDGE notifications. */
3459 ndm
= NLMSG_DATA(h
);
3460 if (ndm
->ndm_family
!= AF_BRIDGE
)
3463 return netlink_macfdb_change(h
, len
, ns_id
);
3466 /* Request for MAC FDB information from the kernel */
3467 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3468 int type
, ifindex_t master_ifindex
)
3472 struct ifinfomsg ifm
;
3476 /* Form the request, specifying filter (rtattr) if needed. */
3477 memset(&req
, 0, sizeof(req
));
3478 req
.n
.nlmsg_type
= type
;
3479 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3480 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3481 req
.ifm
.ifi_family
= family
;
3483 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3485 return netlink_request(netlink_cmd
, &req
);
3489 * MAC forwarding database read using netlink interface. This is invoked
3492 int netlink_macfdb_read(struct zebra_ns
*zns
)
3495 struct zebra_dplane_info dp_info
;
3497 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3499 /* Get bridge FDB table. */
3500 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3504 /* We are reading entire table. */
3506 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3513 * MAC forwarding database read using netlink interface. This is for a
3514 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3516 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3517 struct interface
*br_if
)
3519 struct zebra_if
*br_zif
;
3520 struct zebra_if
*zif
;
3521 struct zebra_l2info_vxlan
*vxl
;
3522 struct zebra_dplane_info dp_info
;
3525 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3527 /* Save VLAN we're filtering on, if needed. */
3528 br_zif
= (struct zebra_if
*)br_if
->info
;
3529 zif
= (struct zebra_if
*)ifp
->info
;
3530 vxl
= &zif
->l2info
.vxl
;
3531 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3532 filter_vlan
= vxl
->access_vlan
;
3534 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3536 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3540 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3541 &dp_info
, 0, false);
3543 /* Reset VLAN filter. */
3549 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3550 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3551 int family
, int type
,
3552 struct interface
*br_if
,
3553 const struct ethaddr
*mac
,
3561 struct zebra_if
*br_zif
;
3563 memset(&req
, 0, sizeof(req
));
3564 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3565 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3566 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3567 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3568 /* req.ndm.ndm_state = NUD_REACHABLE; */
3570 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3572 br_zif
= (struct zebra_if
*)br_if
->info
;
3573 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3574 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3576 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3578 if (IS_ZEBRA_DEBUG_KERNEL
)
3580 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3581 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3582 br_if
->name
, br_if
->ifindex
, br_if
->vrf
->name
,
3583 br_if
->vrf
->vrf_id
, mac
, vid
);
3585 return netlink_request(&zns
->netlink_cmd
, &req
);
3588 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3589 struct interface
*br_if
,
3590 const struct ethaddr
*mac
, vlanid_t vid
)
3593 struct zebra_dplane_info dp_info
;
3595 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3597 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3599 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3605 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3606 &dp_info
, 1, false);
3612 * Netlink-specific handler for MAC updates using dataplane context object.
3614 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3617 struct ipaddr vtep_ip
;
3624 uint32_t update_flags
;
3626 uint8_t nfy_flags
= 0;
3627 int proto
= RTPROT_ZEBRA
;
3629 if (dplane_ctx_get_type(ctx
) != 0)
3630 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3632 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3633 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3636 state
= NUD_REACHABLE
;
3638 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3639 if (update_flags
& DPLANE_MAC_REMOTE
) {
3641 if (dplane_ctx_mac_is_sticky(ctx
)) {
3642 /* NUD_NOARP prevents the entry from expiring */
3644 /* sticky the entry from moving */
3645 flags
|= NTF_STICKY
;
3647 flags
|= NTF_EXT_LEARNED
;
3649 /* if it was static-local previously we need to clear the
3650 * notify flags on replace with remote
3652 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3656 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3657 nfy_flags
|= FDB_NOTIFY_BIT
;
3661 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3662 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3667 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3668 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3669 SET_IPADDR_V4(&vtep_ip
);
3671 if (IS_ZEBRA_DEBUG_KERNEL
) {
3673 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3675 vid
= dplane_ctx_mac_get_vlan(ctx
);
3677 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3682 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3683 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3684 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3685 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3686 mac
, &vtep_ip
, nhg_id
,
3687 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3688 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3690 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3691 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3696 total
= netlink_neigh_update_msg_encode(
3697 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
3698 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
3699 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
3706 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3707 * 5549 support, re-install them.
3709 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3710 struct interface
*ifp
, struct ipaddr
*ip
,
3713 if (ndm
->ndm_family
!= AF_INET
)
3716 if (!zif
->v6_2_v4_ll_neigh_entry
)
3719 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3722 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3723 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3728 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3732 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3734 #define NUD_LOCAL_ACTIVE \
3735 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3737 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
3739 /* an exact match is done between
3740 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
3741 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
3742 * (see in lib/zclient.h)
3746 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3749 struct interface
*ifp
;
3750 struct zebra_if
*zif
;
3751 struct rtattr
*tb
[NDA_MAX
+ 1];
3752 struct interface
*link_if
;
3755 char buf
[ETHER_ADDR_STRLEN
];
3756 int mac_present
= 0;
3759 bool local_inactive
;
3760 uint32_t ext_flags
= 0;
3761 bool dp_static
= false;
3765 ndm
= NLMSG_DATA(h
);
3767 /* The interface should exist. */
3768 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3770 if (!ifp
|| !ifp
->info
)
3773 zif
= (struct zebra_if
*)ifp
->info
;
3775 /* Parse attributes and extract fields of interest. */
3776 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3779 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3780 nl_msg_type_to_str(h
->nlmsg_type
),
3781 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3782 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
3786 memset(&ip
, 0, sizeof(ip
));
3787 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3788 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3790 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3791 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3792 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3793 if (IS_ZEBRA_DEBUG_KERNEL
)
3795 " Neighbor Entry Received is a 5549 entry, finished");
3799 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3800 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3801 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3803 /* we send link layer information to client:
3804 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3805 * - struct ipaddr ( for DEL and GET)
3806 * - struct ethaddr mac; (for NEW)
3808 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
3809 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
3810 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
3811 cmd
= ZEBRA_NHRP_NEIGH_GET
;
3812 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
3813 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
3815 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
3819 if (tb
[NDA_LLADDR
]) {
3820 /* copy LLADDR information */
3821 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
3823 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
3824 union sockunion link_layer_ipv4
;
3827 sockunion_family(&link_layer_ipv4
) = AF_INET
;
3828 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
3829 RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
3831 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
3832 zsend_nhrp_neighbor_notify(
3834 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
3838 if (h
->nlmsg_type
== RTM_GETNEIGH
)
3841 /* The neighbor is present on an SVI. From this, we locate the
3843 * bridge because we're only interested in neighbors on a VxLAN bridge.
3844 * The bridge is located based on the nature of the SVI:
3845 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3847 * and is linked to the bridge
3848 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3852 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3853 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3857 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3860 if (IS_ZEBRA_DEBUG_KERNEL
)
3862 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3866 memset(&mac
, 0, sizeof(mac
));
3867 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3868 if (tb
[NDA_LLADDR
]) {
3869 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3870 if (IS_ZEBRA_DEBUG_KERNEL
)
3872 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3877 ifp
->name
, ndm
->ndm_ifindex
,
3880 (unsigned long)RTA_PAYLOAD(
3886 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3889 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3890 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3892 if (tb
[NDA_EXT_FLAGS
]) {
3893 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3894 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3898 if (IS_ZEBRA_DEBUG_KERNEL
)
3900 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3901 nl_msg_type_to_str(h
->nlmsg_type
),
3902 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3903 ndm
->ndm_ifindex
, ifp
->vrf
->name
,
3904 ifp
->vrf
->vrf_id
, &ip
,
3906 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3908 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3910 /* If the neighbor state is valid for use, process as an add or
3912 * else process as a delete. Note that the delete handling may
3914 * in re-adding the neighbor if it is a valid "remote" neighbor.
3916 if (ndm
->ndm_state
& NUD_VALID
) {
3917 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3919 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3921 /* If EVPN-MH is not enabled we treat STALE
3922 * neighbors as locally-active and advertise
3925 local_inactive
= false;
3927 return zebra_vxlan_handle_kernel_neigh_update(
3928 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3929 is_router
, local_inactive
, dp_static
);
3932 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3935 if (IS_ZEBRA_DEBUG_KERNEL
)
3936 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
3937 nl_msg_type_to_str(h
->nlmsg_type
),
3938 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3939 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
3942 /* Process the delete - it may result in re-adding the neighbor if it is
3943 * a valid "remote" neighbor.
3945 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3948 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3953 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3956 /* Length validity. */
3957 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3961 /* We are interested only in AF_INET or AF_INET6 notifications. */
3962 ndm
= NLMSG_DATA(h
);
3963 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3966 return netlink_neigh_change(h
, len
);
3969 /* Request for IP neighbor information from the kernel */
3970 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3971 int type
, ifindex_t ifindex
)
3979 /* Form the request, specifying filter (rtattr) if needed. */
3980 memset(&req
, 0, sizeof(req
));
3981 req
.n
.nlmsg_type
= type
;
3982 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3983 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3984 req
.ndm
.ndm_family
= family
;
3986 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3988 return netlink_request(netlink_cmd
, &req
);
3992 * IP Neighbor table read using netlink interface. This is invoked
3995 int netlink_neigh_read(struct zebra_ns
*zns
)
3998 struct zebra_dplane_info dp_info
;
4000 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4002 /* Get IP neighbor table. */
4003 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4007 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4014 * IP Neighbor table read using netlink interface. This is for a specific
4017 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
4020 struct zebra_dplane_info dp_info
;
4022 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4024 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
4028 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4029 &dp_info
, 0, false);
4035 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
4036 * read using netlink interface.
4038 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
4040 const struct ipaddr
*ip
,
4050 /* Form the request, specifying filter (rtattr) if needed. */
4051 memset(&req
, 0, sizeof(req
));
4052 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4053 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4054 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4055 req
.ndm
.ndm_ifindex
= ifindex
;
4057 if (IS_IPADDR_V4(ip
)) {
4058 ipa_len
= IPV4_MAX_BYTELEN
;
4059 req
.ndm
.ndm_family
= AF_INET
;
4062 ipa_len
= IPV6_MAX_BYTELEN
;
4063 req
.ndm
.ndm_family
= AF_INET6
;
4066 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4068 if (IS_ZEBRA_DEBUG_KERNEL
)
4069 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4070 __func__
, nl_msg_type_to_str(type
),
4071 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4074 return netlink_request(&zns
->netlink_cmd
, &req
);
4077 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4078 struct interface
*vlan_if
)
4081 struct zebra_ns
*zns
;
4082 struct zebra_vrf
*zvrf
= vlan_if
->vrf
->info
;
4083 struct zebra_dplane_info dp_info
;
4087 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4089 if (IS_ZEBRA_DEBUG_KERNEL
)
4090 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4091 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4092 vlan_if
->vrf
->name
, vlan_if
->vrf
->vrf_id
);
4094 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4099 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4100 &dp_info
, 1, false);
4105 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4110 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4111 || h
->nlmsg_type
== RTM_GETNEIGH
))
4114 /* Length validity. */
4115 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4118 "%s: Message received from netlink is of a broken size %d %zu",
4119 __func__
, h
->nlmsg_len
,
4120 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4124 /* Is this a notification for the MAC FDB or IP neighbor table? */
4125 ndm
= NLMSG_DATA(h
);
4126 if (ndm
->ndm_family
== AF_BRIDGE
)
4127 return netlink_macfdb_change(h
, len
, ns_id
);
4129 if (ndm
->ndm_type
!= RTN_UNICAST
)
4132 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4133 return netlink_ipneigh_change(h
, len
, ns_id
);
4136 EC_ZEBRA_UNKNOWN_FAMILY
,
4137 "Invalid address family: %u received from kernel neighbor change: %s",
4138 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4146 * Utility neighbor-update function, using info from dplane context.
4148 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4149 int cmd
, void *buf
, size_t buflen
)
4151 const struct ipaddr
*ip
;
4152 const struct ethaddr
*mac
= NULL
;
4153 const struct ipaddr
*link_ip
= NULL
;
4154 const void *link_ptr
= NULL
;
4155 char buf2
[ETHER_ADDR_STRLEN
];
4161 uint32_t update_flags
;
4162 uint32_t ext_flags
= 0;
4164 int proto
= RTPROT_ZEBRA
;
4166 if (dplane_ctx_get_type(ctx
) != 0)
4167 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4169 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4171 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4172 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4173 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4174 llalen
= IPADDRSZ(link_ip
);
4175 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4176 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4178 mac
= dplane_ctx_neigh_get_mac(ctx
);
4180 link_ptr
= (const void *)mac
;
4181 if (is_zero_mac(mac
))
4184 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4186 snprintf(buf2
, sizeof(buf2
), "null");
4188 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4189 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4190 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4192 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4194 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4195 flags
|= NTF_EXT_LEARNED
;
4196 /* if it was static-local previously we need to clear the
4197 * ext flags on replace with remote
4199 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4201 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4204 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4205 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4207 if (IS_ZEBRA_DEBUG_KERNEL
)
4209 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4210 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4211 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4212 ip
, link_ip
? "Link " : "MAC ", buf2
, flags
, state
,
4213 ext
? "ext " : "", ext_flags
);
4215 return netlink_neigh_update_msg_encode(
4216 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4217 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4218 ext_flags
, buf
, buflen
, proto
);
4221 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4222 void *data
, size_t datalen
)
4229 struct rtattr
*nest
;
4234 if (datalen
< sizeof(*req
))
4236 memset(req
, 0, sizeof(*req
));
4237 family
= dplane_ctx_neightable_get_family(ctx
);
4238 idx
= dplane_ctx_get_ifindex(ctx
);
4240 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4241 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4242 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4243 req
->ndtm
.ndtm_family
= family
;
4245 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4246 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4247 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4250 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4252 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4253 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4255 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4256 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4259 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4260 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4263 nl_attr_nest_end(&req
->n
, nest
);
4265 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4268 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4269 void *buf
, size_t buflen
)
4273 switch (dplane_ctx_get_op(ctx
)) {
4274 case DPLANE_OP_NEIGH_INSTALL
:
4275 case DPLANE_OP_NEIGH_UPDATE
:
4276 case DPLANE_OP_NEIGH_DISCOVER
:
4277 case DPLANE_OP_NEIGH_IP_INSTALL
:
4278 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4280 case DPLANE_OP_NEIGH_DELETE
:
4281 case DPLANE_OP_NEIGH_IP_DELETE
:
4282 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4284 case DPLANE_OP_VTEP_ADD
:
4285 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4288 case DPLANE_OP_VTEP_DELETE
:
4289 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4292 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4293 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4303 * Update MAC, using dataplane context object.
4306 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4307 struct zebra_dplane_ctx
*ctx
)
4309 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4313 enum netlink_msg_status
4314 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4316 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4321 * MPLS label forwarding table change via netlink interface, using dataplane
4322 * context information.
4324 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4325 void *buf
, size_t buflen
)
4328 const struct nhlfe_list_head
*head
;
4329 const struct zebra_nhlfe
*nhlfe
;
4330 struct nexthop
*nexthop
= NULL
;
4331 unsigned int nexthop_num
;
4332 const char *routedesc
;
4334 struct prefix p
= {0};
4336 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
4344 if (buflen
< sizeof(*req
))
4347 memset(req
, 0, sizeof(*req
));
4350 * Count # nexthops so we can decide whether to use singlepath
4351 * or multipath case.
4354 head
= dplane_ctx_get_nhlfe_list(ctx
);
4355 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4356 nexthop
= nhlfe
->nexthop
;
4359 if (cmd
== RTM_NEWROUTE
) {
4360 /* Count all selected NHLFEs */
4361 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4362 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4365 /* Count all installed NHLFEs */
4366 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4367 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4372 if ((nexthop_num
== 0) ||
4373 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4376 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4377 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4378 req
->n
.nlmsg_type
= cmd
;
4379 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
4381 req
->r
.rtm_family
= AF_MPLS
;
4382 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4383 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4384 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4385 req
->r
.rtm_type
= RTN_UNICAST
;
4387 if (cmd
== RTM_NEWROUTE
) {
4388 /* We do a replace to handle update. */
4389 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4391 /* set the protocol value if installing */
4392 route_type
= re_type_from_lsp_type(
4393 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4394 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4397 /* Fill destination */
4398 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4399 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4402 /* Fill nexthops (paths) based on single-path or multipath. The paths
4403 * chosen depend on the operation.
4405 if (nexthop_num
== 1) {
4406 routedesc
= "single-path";
4407 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4411 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4412 nexthop
= nhlfe
->nexthop
;
4416 if ((cmd
== RTM_NEWROUTE
4417 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4418 && CHECK_FLAG(nexthop
->flags
,
4419 NEXTHOP_FLAG_ACTIVE
)))
4420 || (cmd
== RTM_DELROUTE
4421 && (CHECK_FLAG(nhlfe
->flags
,
4422 NHLFE_FLAG_INSTALLED
)
4423 && CHECK_FLAG(nexthop
->flags
,
4424 NEXTHOP_FLAG_FIB
)))) {
4425 /* Add the gateway */
4426 if (!_netlink_mpls_build_singlepath(
4427 &p
, routedesc
, nhlfe
, &req
->n
,
4428 &req
->r
, buflen
, cmd
))
4435 } else { /* Multipath case */
4436 struct rtattr
*nest
;
4437 const union g_addr
*src1
= NULL
;
4439 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4443 routedesc
= "multipath";
4444 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4448 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4449 nexthop
= nhlfe
->nexthop
;
4453 if ((cmd
== RTM_NEWROUTE
4454 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4455 && CHECK_FLAG(nexthop
->flags
,
4456 NEXTHOP_FLAG_ACTIVE
)))
4457 || (cmd
== RTM_DELROUTE
4458 && (CHECK_FLAG(nhlfe
->flags
,
4459 NHLFE_FLAG_INSTALLED
)
4460 && CHECK_FLAG(nexthop
->flags
,
4461 NEXTHOP_FLAG_FIB
)))) {
4464 /* Build the multipath */
4465 if (!_netlink_mpls_build_multipath(
4466 &p
, routedesc
, nhlfe
, &req
->n
,
4467 buflen
, &req
->r
, &src1
))
4472 /* Add the multipath */
4473 nl_attr_nest_end(&req
->n
, nest
);
4476 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4479 /****************************************************************************
4480 * This code was developed in a branch that didn't have dplane APIs for
4481 * MAC updates. Hence the use of the legacy style. It will be moved to
4482 * the new dplane style pre-merge to master. XXX
4484 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4491 int cmd
= RTM_NEWNEXTHOP
;
4492 struct zebra_vrf
*zvrf
;
4493 struct zebra_ns
*zns
;
4495 zvrf
= zebra_vrf_get_evpn();
4500 memset(&req
, 0, sizeof(req
));
4502 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4503 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4504 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4505 req
.n
.nlmsg_type
= cmd
;
4506 req
.nhm
.nh_family
= AF_INET
;
4508 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4510 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4512 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4513 &vtep_ip
, IPV4_MAX_BYTELEN
))
4516 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4517 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4518 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4521 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4525 static int netlink_fdb_nh_del(uint32_t nh_id
)
4532 int cmd
= RTM_DELNEXTHOP
;
4533 struct zebra_vrf
*zvrf
;
4534 struct zebra_ns
*zns
;
4536 zvrf
= zebra_vrf_get_evpn();
4541 memset(&req
, 0, sizeof(req
));
4543 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4544 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4545 req
.n
.nlmsg_type
= cmd
;
4546 req
.nhm
.nh_family
= AF_UNSPEC
;
4548 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4551 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4552 zlog_debug("Tx %s fdb-nh 0x%x",
4553 nl_msg_type_to_str(cmd
), nh_id
);
4556 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4560 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4561 struct nh_grp
*nh_ids
)
4568 int cmd
= RTM_NEWNEXTHOP
;
4569 struct zebra_vrf
*zvrf
;
4570 struct zebra_ns
*zns
;
4571 struct nexthop_grp grp
[nh_cnt
];
4574 zvrf
= zebra_vrf_get_evpn();
4579 memset(&req
, 0, sizeof(req
));
4581 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4582 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4583 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4584 req
.n
.nlmsg_type
= cmd
;
4585 req
.nhm
.nh_family
= AF_UNSPEC
;
4587 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4589 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4591 memset(&grp
, 0, sizeof(grp
));
4592 for (i
= 0; i
< nh_cnt
; ++i
) {
4593 grp
[i
].id
= nh_ids
[i
].id
;
4594 grp
[i
].weight
= nh_ids
[i
].weight
;
4596 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4597 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4601 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4602 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4606 for (i
= 0; i
< nh_cnt
; ++i
) {
4607 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4609 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4612 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4613 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4616 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4620 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4622 return netlink_fdb_nh_del(nhg_id
);
4625 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4627 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4630 int kernel_del_mac_nh(uint32_t nh_id
)
4632 return netlink_fdb_nh_del(nh_id
);
4635 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4636 struct nh_grp
*nh_ids
)
4638 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4641 int kernel_del_mac_nhg(uint32_t nhg_id
)
4643 return netlink_fdb_nhg_del(nhg_id
);
4646 #endif /* HAVE_NETLINK */