1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 /* The following definition is to workaround an issue in the Linux kernel
26 * header files with redefinition of 'struct in6_addr' in both
27 * netinet/in.h and linux/in6.h.
28 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
32 #include <net/if_arp.h>
33 #include <linux/lwtunnel.h>
34 #include <linux/mpls_iptunnel.h>
35 #include <linux/seg6_iptunnel.h>
36 #include <linux/seg6_local.h>
37 #include <linux/neighbour.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/nexthop.h>
41 /* Hack for GNU libc version 2. */
43 #define MSG_TRUNC 0x20
44 #endif /* MSG_TRUNC */
51 #include "plist_int.h"
52 #include "connected.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_ns.h"
67 #include "zebra/zebra_vrf.h"
69 #include "zebra/redistribute.h"
70 #include "zebra/interface.h"
71 #include "zebra/debug.h"
72 #include "zebra/rtadv.h"
73 #include "zebra/zebra_ptm.h"
74 #include "zebra/zebra_mpls.h"
75 #include "zebra/kernel_netlink.h"
76 #include "zebra/rt_netlink.h"
77 #include "zebra/zebra_nhg.h"
78 #include "zebra/zebra_mroute.h"
79 #include "zebra/zebra_vxlan.h"
80 #include "zebra/zebra_errors.h"
81 #include "zebra/zebra_evpn_mh.h"
87 /* Re-defining as I am unable to include <linux/if_bridge.h> which has the
88 * UAPI for MAC sync. */
89 #ifndef _UAPI_LINUX_IF_BRIDGE_H
90 #define BR_SPH_LIST_SIZE 10
93 static vlanid_t filter_vlan
= 0;
95 /* We capture whether the current kernel supports nexthop ids; by
96 * default, we'll use them if possible. There's also a configuration
97 * available to _disable_ use of kernel nexthops.
99 static bool supports_nh
;
107 static const char ipv4_ll_buf
[16] = "169.254.0.1";
108 static struct in_addr ipv4_ll
;
110 /* Is this a ipv4 over ipv6 route? */
111 static bool is_route_v4_over_v6(unsigned char rtm_family
,
112 enum nexthop_types_t nexthop_type
)
114 if (rtm_family
== AF_INET
115 && (nexthop_type
== NEXTHOP_TYPE_IPV6
116 || nexthop_type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
122 /* Helper to control use of kernel-level nexthop ids */
123 static bool kernel_nexthops_supported(void)
125 return (supports_nh
&& !vrf_is_backend_netns()
126 && zebra_nhg_kernel_nexthops_enabled());
130 * Some people may only want to use NHGs created by protos and not
131 * implicitly created by Zebra. This check accounts for that.
133 static bool proto_nexthops_only(void)
135 return zebra_nhg_proto_nexthops_only();
138 /* Is this a proto created NHG? */
139 static bool is_proto_nhg(uint32_t id
, int type
)
141 /* If type is available, use it as the source of truth */
143 if (type
!= ZEBRA_ROUTE_NHG
)
148 if (id
>= ZEBRA_NHG_PROTO_LOWER
)
155 * The ipv4_ll data structure is used for all 5549
156 * additions to the kernel. Let's figure out the
157 * correct value one time instead for every
158 * install/remove of a 5549 type route
160 void rt_netlink_init(void)
162 inet_pton(AF_INET
, ipv4_ll_buf
, &ipv4_ll
);
166 * Mapping from dataplane neighbor flags to netlink flags
168 static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags
)
172 if (dplane_flags
& DPLANE_NTF_EXT_LEARNED
)
173 flags
|= NTF_EXT_LEARNED
;
174 if (dplane_flags
& DPLANE_NTF_ROUTER
)
176 if (dplane_flags
& DPLANE_NTF_USE
)
183 * Mapping from dataplane neighbor state to netlink state
185 static uint16_t neigh_state_to_netlink(uint16_t dplane_state
)
189 if (dplane_state
& DPLANE_NUD_REACHABLE
)
190 state
|= NUD_REACHABLE
;
191 if (dplane_state
& DPLANE_NUD_STALE
)
193 if (dplane_state
& DPLANE_NUD_NOARP
)
195 if (dplane_state
& DPLANE_NUD_PROBE
)
197 if (dplane_state
& DPLANE_NUD_INCOMPLETE
)
198 state
|= NUD_INCOMPLETE
;
199 if (dplane_state
& DPLANE_NUD_PERMANENT
)
200 state
|= NUD_PERMANENT
;
201 if (dplane_state
& DPLANE_NUD_FAILED
)
208 static inline bool is_selfroute(int proto
)
210 if ((proto
== RTPROT_BGP
) || (proto
== RTPROT_OSPF
)
211 || (proto
== RTPROT_ZSTATIC
) || (proto
== RTPROT_ZEBRA
)
212 || (proto
== RTPROT_ISIS
) || (proto
== RTPROT_RIPNG
)
213 || (proto
== RTPROT_NHRP
) || (proto
== RTPROT_EIGRP
)
214 || (proto
== RTPROT_LDP
) || (proto
== RTPROT_BABEL
)
215 || (proto
== RTPROT_RIP
) || (proto
== RTPROT_SHARP
)
216 || (proto
== RTPROT_PBR
) || (proto
== RTPROT_OPENFABRIC
)
217 || (proto
== RTPROT_SRTE
)) {
224 static inline int zebra2proto(int proto
)
227 case ZEBRA_ROUTE_BABEL
:
228 proto
= RTPROT_BABEL
;
230 case ZEBRA_ROUTE_BGP
:
233 case ZEBRA_ROUTE_OSPF
:
234 case ZEBRA_ROUTE_OSPF6
:
237 case ZEBRA_ROUTE_STATIC
:
238 proto
= RTPROT_ZSTATIC
;
240 case ZEBRA_ROUTE_ISIS
:
243 case ZEBRA_ROUTE_RIP
:
246 case ZEBRA_ROUTE_RIPNG
:
247 proto
= RTPROT_RIPNG
;
249 case ZEBRA_ROUTE_NHRP
:
252 case ZEBRA_ROUTE_EIGRP
:
253 proto
= RTPROT_EIGRP
;
255 case ZEBRA_ROUTE_LDP
:
258 case ZEBRA_ROUTE_SHARP
:
259 proto
= RTPROT_SHARP
;
261 case ZEBRA_ROUTE_PBR
:
264 case ZEBRA_ROUTE_OPENFABRIC
:
265 proto
= RTPROT_OPENFABRIC
;
267 case ZEBRA_ROUTE_SRTE
:
270 case ZEBRA_ROUTE_TABLE
:
271 case ZEBRA_ROUTE_NHG
:
272 proto
= RTPROT_ZEBRA
;
274 case ZEBRA_ROUTE_CONNECT
:
275 case ZEBRA_ROUTE_KERNEL
:
276 proto
= RTPROT_KERNEL
;
280 * When a user adds a new protocol this will show up
281 * to let them know to do something about it. This
282 * is intentionally a warn because we should see
283 * this as part of development of a new protocol
286 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
288 proto
= RTPROT_ZEBRA
;
295 static inline int proto2zebra(int proto
, int family
, bool is_nexthop
)
299 proto
= ZEBRA_ROUTE_BABEL
;
302 proto
= ZEBRA_ROUTE_BGP
;
305 proto
= (family
== AF_INET
) ? ZEBRA_ROUTE_OSPF
309 proto
= ZEBRA_ROUTE_ISIS
;
312 proto
= ZEBRA_ROUTE_RIP
;
315 proto
= ZEBRA_ROUTE_RIPNG
;
318 proto
= ZEBRA_ROUTE_NHRP
;
321 proto
= ZEBRA_ROUTE_EIGRP
;
324 proto
= ZEBRA_ROUTE_LDP
;
328 proto
= ZEBRA_ROUTE_STATIC
;
331 proto
= ZEBRA_ROUTE_SHARP
;
334 proto
= ZEBRA_ROUTE_PBR
;
336 case RTPROT_OPENFABRIC
:
337 proto
= ZEBRA_ROUTE_OPENFABRIC
;
340 proto
= ZEBRA_ROUTE_SRTE
;
344 proto
= ZEBRA_ROUTE_NHG
;
347 /* Intentional fall thru */
350 * When a user adds a new protocol this will show up
351 * to let them know to do something about it. This
352 * is intentionally a warn because we should see
353 * this as part of development of a new protocol
356 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
358 proto
= ZEBRA_ROUTE_KERNEL
;
365 Pending: create an efficient table_id (in a tree/hash) based lookup)
367 vrf_id_t
vrf_lookup_by_table(uint32_t table_id
, ns_id_t ns_id
)
370 struct zebra_vrf
*zvrf
;
372 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
376 /* case vrf with netns : match the netnsid */
377 if (vrf_is_backend_netns()) {
378 if (ns_id
== zvrf_id(zvrf
))
379 return zvrf_id(zvrf
);
381 /* VRF is VRF_BACKEND_VRF_LITE */
382 if (zvrf
->table_id
!= table_id
)
384 return zvrf_id(zvrf
);
392 * @parse_encap_mpls() - Parses encapsulated mpls attributes
393 * @tb: Pointer to rtattr to look for nested items in.
394 * @labels: Pointer to store labels in.
396 * Return: Number of mpls labels found.
398 static int parse_encap_mpls(struct rtattr
*tb
, mpls_label_t
*labels
)
400 struct rtattr
*tb_encap
[MPLS_IPTUNNEL_MAX
+ 1] = {0};
401 mpls_lse_t
*lses
= NULL
;
406 mpls_label_t label
= 0;
408 netlink_parse_rtattr_nested(tb_encap
, MPLS_IPTUNNEL_MAX
, tb
);
409 lses
= (mpls_lse_t
*)RTA_DATA(tb_encap
[MPLS_IPTUNNEL_DST
]);
410 while (!bos
&& num_labels
< MPLS_MAX_LABELS
) {
411 mpls_lse_decode(lses
[num_labels
], &label
, &ttl
, &exp
, &bos
);
412 labels
[num_labels
++] = label
;
418 static enum seg6local_action_t
419 parse_encap_seg6local(struct rtattr
*tb
,
420 struct seg6local_context
*ctx
)
422 struct rtattr
*tb_encap
[256] = {};
423 enum seg6local_action_t act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
425 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
427 if (tb_encap
[SEG6_LOCAL_ACTION
])
428 act
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_ACTION
]);
430 if (tb_encap
[SEG6_LOCAL_NH4
])
431 ctx
->nh4
= *(struct in_addr
*)RTA_DATA(
432 tb_encap
[SEG6_LOCAL_NH4
]);
434 if (tb_encap
[SEG6_LOCAL_NH6
])
435 ctx
->nh6
= *(struct in6_addr
*)RTA_DATA(
436 tb_encap
[SEG6_LOCAL_NH6
]);
438 if (tb_encap
[SEG6_LOCAL_TABLE
])
439 ctx
->table
= *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_TABLE
]);
441 if (tb_encap
[SEG6_LOCAL_VRFTABLE
])
443 *(uint32_t *)RTA_DATA(tb_encap
[SEG6_LOCAL_VRFTABLE
]);
448 static int parse_encap_seg6(struct rtattr
*tb
, struct in6_addr
*segs
)
450 struct rtattr
*tb_encap
[256] = {};
451 struct seg6_iptunnel_encap
*ipt
= NULL
;
452 struct in6_addr
*segments
= NULL
;
454 netlink_parse_rtattr_nested(tb_encap
, 256, tb
);
457 * TODO: It's not support multiple SID list.
459 if (tb_encap
[SEG6_IPTUNNEL_SRH
]) {
460 ipt
= (struct seg6_iptunnel_encap
*)
461 RTA_DATA(tb_encap
[SEG6_IPTUNNEL_SRH
]);
462 segments
= ipt
->srh
[0].segments
;
471 static struct nexthop
472 parse_nexthop_unicast(ns_id_t ns_id
, struct rtmsg
*rtm
, struct rtattr
**tb
,
473 enum blackhole_type bh_type
, int index
, void *prefsrc
,
474 void *gate
, afi_t afi
, vrf_id_t vrf_id
)
476 struct interface
*ifp
= NULL
;
477 struct nexthop nh
= {0};
478 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
480 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
481 struct seg6local_context seg6l_ctx
= {};
482 struct in6_addr seg6_segs
= {};
485 vrf_id_t nh_vrf_id
= vrf_id
;
486 size_t sz
= (afi
== AFI_IP
) ? 4 : 16;
488 if (bh_type
== BLACKHOLE_UNSPEC
) {
490 nh
.type
= NEXTHOP_TYPE_IFINDEX
;
491 else if (index
&& gate
)
492 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4_IFINDEX
493 : NEXTHOP_TYPE_IPV6_IFINDEX
;
494 else if (!index
&& gate
)
495 nh
.type
= (afi
== AFI_IP
) ? NEXTHOP_TYPE_IPV4
498 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
499 nh
.bh_type
= bh_type
;
502 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
503 nh
.bh_type
= bh_type
;
507 memcpy(&nh
.src
, prefsrc
, sz
);
509 memcpy(&nh
.gate
, gate
, sz
);
512 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), index
);
514 nh_vrf_id
= ifp
->vrf
->vrf_id
;
516 nh
.vrf_id
= nh_vrf_id
;
518 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
519 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
520 == LWTUNNEL_ENCAP_MPLS
) {
521 num_labels
= parse_encap_mpls(tb
[RTA_ENCAP
], labels
);
523 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
524 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
525 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
526 seg6l_act
= parse_encap_seg6local(tb
[RTA_ENCAP
], &seg6l_ctx
);
528 if (tb
[RTA_ENCAP
] && tb
[RTA_ENCAP_TYPE
]
529 && *(uint16_t *)RTA_DATA(tb
[RTA_ENCAP_TYPE
])
530 == LWTUNNEL_ENCAP_SEG6
) {
531 num_segs
= parse_encap_seg6(tb
[RTA_ENCAP
], &seg6_segs
);
534 if (rtm
->rtm_flags
& RTNH_F_ONLINK
)
535 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
538 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
, labels
);
540 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
541 nexthop_add_srv6_seg6local(&nh
, seg6l_act
, &seg6l_ctx
);
544 nexthop_add_srv6_seg6(&nh
, &seg6_segs
);
549 static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id
,
550 struct nexthop_group
*ng
,
552 struct rtnexthop
*rtnh
,
554 void *prefsrc
, vrf_id_t vrf_id
)
557 struct interface
*ifp
= NULL
;
560 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
562 enum seg6local_action_t seg6l_act
= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
;
563 struct seg6local_context seg6l_ctx
= {};
564 struct in6_addr seg6_segs
= {};
566 struct rtattr
*rtnh_tb
[RTA_MAX
+ 1] = {};
568 int len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
569 vrf_id_t nh_vrf_id
= vrf_id
;
572 struct nexthop
*nh
= NULL
;
574 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
577 index
= rtnh
->rtnh_ifindex
;
580 * Yes we are looking this up
581 * for every nexthop and just
582 * using the last one looked
585 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
588 nh_vrf_id
= ifp
->vrf
->vrf_id
;
591 EC_ZEBRA_UNKNOWN_INTERFACE
,
592 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
594 nh_vrf_id
= VRF_DEFAULT
;
599 if (rtnh
->rtnh_len
> sizeof(*rtnh
)) {
600 netlink_parse_rtattr(rtnh_tb
, RTA_MAX
, RTNH_DATA(rtnh
),
601 rtnh
->rtnh_len
- sizeof(*rtnh
));
602 if (rtnh_tb
[RTA_GATEWAY
])
603 gate
= RTA_DATA(rtnh_tb
[RTA_GATEWAY
]);
604 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
605 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
606 == LWTUNNEL_ENCAP_MPLS
) {
607 num_labels
= parse_encap_mpls(
608 rtnh_tb
[RTA_ENCAP
], labels
);
610 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
611 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
612 == LWTUNNEL_ENCAP_SEG6_LOCAL
) {
613 seg6l_act
= parse_encap_seg6local(
614 rtnh_tb
[RTA_ENCAP
], &seg6l_ctx
);
616 if (rtnh_tb
[RTA_ENCAP
] && rtnh_tb
[RTA_ENCAP_TYPE
]
617 && *(uint16_t *)RTA_DATA(rtnh_tb
[RTA_ENCAP_TYPE
])
618 == LWTUNNEL_ENCAP_SEG6
) {
619 num_segs
= parse_encap_seg6(rtnh_tb
[RTA_ENCAP
],
624 if (gate
&& rtm
->rtm_family
== AF_INET
) {
626 nh
= nexthop_from_ipv4_ifindex(
627 gate
, prefsrc
, index
, nh_vrf_id
);
629 nh
= nexthop_from_ipv4(gate
, prefsrc
,
631 } else if (gate
&& rtm
->rtm_family
== AF_INET6
) {
633 nh
= nexthop_from_ipv6_ifindex(
634 gate
, index
, nh_vrf_id
);
636 nh
= nexthop_from_ipv6(gate
, nh_vrf_id
);
638 nh
= nexthop_from_ifindex(index
, nh_vrf_id
);
641 nh
->weight
= rtnh
->rtnh_hops
+ 1;
644 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
,
647 if (seg6l_act
!= ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
)
648 nexthop_add_srv6_seg6local(nh
, seg6l_act
,
652 nexthop_add_srv6_seg6(nh
, &seg6_segs
);
654 if (rtnh
->rtnh_flags
& RTNH_F_ONLINK
)
655 SET_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
);
657 /* Add to temporary list */
658 nexthop_group_add_sorted(ng
, nh
);
661 if (rtnh
->rtnh_len
== 0)
664 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
665 rtnh
= RTNH_NEXT(rtnh
);
668 uint8_t nhop_num
= nexthop_group_nexthop_num(ng
);
673 /* Looking up routing table by netlink interface. */
674 static int netlink_route_change_read_unicast(struct nlmsghdr
*h
, ns_id_t ns_id
,
679 struct rtattr
*tb
[RTA_MAX
+ 1];
682 struct prefix_ipv6 src_p
= {};
686 char anyaddr
[16] = {0};
688 int proto
= ZEBRA_ROUTE_KERNEL
;
693 uint8_t distance
= 0;
699 void *prefsrc
= NULL
; /* IPv4 preferred source host address */
700 void *src
= NULL
; /* IPv6 srcdest source prefix */
701 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
705 if (startup
&& h
->nlmsg_type
!= RTM_NEWROUTE
)
707 switch (rtm
->rtm_type
) {
711 bh_type
= BLACKHOLE_NULL
;
713 case RTN_UNREACHABLE
:
714 bh_type
= BLACKHOLE_REJECT
;
717 bh_type
= BLACKHOLE_ADMINPROHIB
;
720 if (IS_ZEBRA_DEBUG_KERNEL
)
721 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
722 nl_rttype_to_str(rtm
->rtm_type
),
727 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
730 "%s: Message received from netlink is of a broken size %d %zu",
731 __func__
, h
->nlmsg_len
,
732 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
736 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
738 if (rtm
->rtm_flags
& RTM_F_CLONED
)
740 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
742 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
745 selfroute
= is_selfroute(rtm
->rtm_protocol
);
747 if (!startup
&& selfroute
748 && h
->nlmsg_type
== RTM_NEWROUTE
749 && !zrouter
.asic_offloaded
) {
750 if (IS_ZEBRA_DEBUG_KERNEL
)
751 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
756 /* We don't care about change notifications for the MPLS table. */
757 /* TODO: Revisit this. */
758 if (rtm
->rtm_family
== AF_MPLS
)
761 /* Table corresponding to route. */
763 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
765 table
= rtm
->rtm_table
;
768 vrf_id
= vrf_lookup_by_table(table
, ns_id
);
769 if (vrf_id
== VRF_DEFAULT
) {
770 if (!is_zebra_valid_kernel_table(table
)
771 && !is_zebra_main_routing_table(table
))
775 if (rtm
->rtm_flags
& RTM_F_TRAP
)
776 flags
|= ZEBRA_FLAG_TRAPPED
;
777 if (rtm
->rtm_flags
& RTM_F_OFFLOAD
)
778 flags
|= ZEBRA_FLAG_OFFLOADED
;
779 if (rtm
->rtm_flags
& RTM_F_OFFLOAD_FAILED
)
780 flags
|= ZEBRA_FLAG_OFFLOAD_FAILED
;
782 /* Route which inserted by Zebra. */
784 flags
|= ZEBRA_FLAG_SELFROUTE
;
785 proto
= proto2zebra(rtm
->rtm_protocol
, rtm
->rtm_family
, false);
788 index
= *(int *)RTA_DATA(tb
[RTA_OIF
]);
791 dest
= RTA_DATA(tb
[RTA_DST
]);
796 src
= RTA_DATA(tb
[RTA_SRC
]);
801 prefsrc
= RTA_DATA(tb
[RTA_PREFSRC
]);
804 gate
= RTA_DATA(tb
[RTA_GATEWAY
]);
807 nhe_id
= *(uint32_t *)RTA_DATA(tb
[RTA_NH_ID
]);
809 if (tb
[RTA_PRIORITY
])
810 metric
= *(int *)RTA_DATA(tb
[RTA_PRIORITY
]);
812 #if defined(SUPPORT_REALMS)
814 tag
= *(uint32_t *)RTA_DATA(tb
[RTA_FLOW
]);
817 if (tb
[RTA_METRICS
]) {
818 struct rtattr
*mxrta
[RTAX_MAX
+ 1];
820 netlink_parse_rtattr(mxrta
, RTAX_MAX
, RTA_DATA(tb
[RTA_METRICS
]),
821 RTA_PAYLOAD(tb
[RTA_METRICS
]));
824 mtu
= *(uint32_t *)RTA_DATA(mxrta
[RTAX_MTU
]);
827 if (rtm
->rtm_family
== AF_INET
) {
829 if (rtm
->rtm_dst_len
> IPV4_MAX_BITLEN
) {
831 "Invalid destination prefix length: %u received from kernel route change",
835 memcpy(&p
.u
.prefix4
, dest
, 4);
836 p
.prefixlen
= rtm
->rtm_dst_len
;
838 if (rtm
->rtm_src_len
!= 0) {
840 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST
,
841 "unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
846 /* Force debug below to not display anything for source */
848 } else if (rtm
->rtm_family
== AF_INET6
) {
850 if (rtm
->rtm_dst_len
> IPV6_MAX_BITLEN
) {
852 "Invalid destination prefix length: %u received from kernel route change",
856 memcpy(&p
.u
.prefix6
, dest
, 16);
857 p
.prefixlen
= rtm
->rtm_dst_len
;
859 src_p
.family
= AF_INET6
;
860 if (rtm
->rtm_src_len
> IPV6_MAX_BITLEN
) {
862 "Invalid source prefix length: %u received from kernel route change",
866 memcpy(&src_p
.prefix
, src
, 16);
867 src_p
.prefixlen
= rtm
->rtm_src_len
;
869 /* We only handle the AFs we handle... */
870 if (IS_ZEBRA_DEBUG_KERNEL
)
871 zlog_debug("%s: unknown address-family %u", __func__
,
877 * For ZEBRA_ROUTE_KERNEL types:
879 * The metric/priority of the route received from the kernel
880 * is a 32 bit number. We are going to interpret the high
881 * order byte as the Admin Distance and the low order 3 bytes
884 * This will allow us to do two things:
885 * 1) Allow the creation of kernel routes that can be
886 * overridden by zebra.
887 * 2) Allow the old behavior for 'most' kernel route types
888 * if a user enters 'ip route ...' v4 routes get a metric
889 * of 0 and v6 routes get a metric of 1024. Both of these
890 * values will end up with a admin distance of 0, which
891 * will cause them to win for the purposes of zebra.
893 if (proto
== ZEBRA_ROUTE_KERNEL
) {
894 distance
= (metric
>> 24) & 0xFF;
895 metric
= (metric
& 0x00FFFFFF);
898 if (IS_ZEBRA_DEBUG_KERNEL
) {
899 char buf2
[PREFIX_STRLEN
];
902 "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
903 nl_msg_type_to_str(h
->nlmsg_type
), &p
,
904 src_p
.prefixlen
? " from " : "",
905 src_p
.prefixlen
? prefix2str(&src_p
, buf2
, sizeof(buf2
))
907 vrf_id_to_name(vrf_id
), vrf_id
, table
, metric
,
912 if (rtm
->rtm_family
== AF_INET6
)
915 if (h
->nlmsg_type
== RTM_NEWROUTE
) {
917 if (!tb
[RTA_MULTIPATH
]) {
918 struct nexthop nh
= {0};
921 nh
= parse_nexthop_unicast(
922 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
925 rib_add(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
, &p
,
926 &src_p
, &nh
, nhe_id
, table
, metric
, mtu
,
927 distance
, tag
, startup
);
929 /* This is a multipath route */
930 struct route_entry
*re
;
931 struct nexthop_group
*ng
= NULL
;
932 struct rtnexthop
*rtnh
=
933 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
935 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
937 re
->distance
= distance
;
943 re
->uptime
= monotime(NULL
);
950 /* Use temporary list of nexthops; parse
951 * message payload's nexthops.
953 ng
= nexthop_group_new();
955 parse_multipath_nexthops_unicast(
956 ns_id
, ng
, rtm
, rtnh
, tb
,
959 zserv_nexthop_num_warn(
960 __func__
, (const struct prefix
*)&p
,
964 nexthop_group_delete(&ng
);
970 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
971 &src_p
, re
, ng
, startup
);
977 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0, flags
,
978 &p
, &src_p
, NULL
, nhe_id
, table
, metric
,
981 if (!tb
[RTA_MULTIPATH
]) {
984 nh
= parse_nexthop_unicast(
985 ns_id
, rtm
, tb
, bh_type
, index
, prefsrc
,
987 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
988 flags
, &p
, &src_p
, &nh
, 0, table
,
989 metric
, distance
, true);
991 /* XXX: need to compare the entire list of
992 * nexthops here for NLM_F_APPEND stupidity */
993 rib_delete(afi
, SAFI_UNICAST
, vrf_id
, proto
, 0,
994 flags
, &p
, &src_p
, NULL
, 0, table
,
995 metric
, distance
, true);
1003 static struct mcast_route_data
*mroute
= NULL
;
1005 static int netlink_route_change_read_multicast(struct nlmsghdr
*h
,
1006 ns_id_t ns_id
, int startup
)
1010 struct rtattr
*tb
[RTA_MAX
+ 1];
1011 struct mcast_route_data
*m
;
1012 struct mcast_route_data mr
;
1017 char oif_list
[256] = "\0";
1024 memset(&mr
, 0, sizeof(mr
));
1028 rtm
= NLMSG_DATA(h
);
1030 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1032 netlink_parse_rtattr(tb
, RTA_MAX
, RTM_RTA(rtm
), len
);
1035 table
= *(int *)RTA_DATA(tb
[RTA_TABLE
]);
1037 table
= rtm
->rtm_table
;
1039 vrf
= vrf_lookup_by_table(table
, ns_id
);
1042 iif
= *(int *)RTA_DATA(tb
[RTA_IIF
]);
1045 m
->sg
.src
= *(struct in_addr
*)RTA_DATA(tb
[RTA_SRC
]);
1048 m
->sg
.grp
= *(struct in_addr
*)RTA_DATA(tb
[RTA_DST
]);
1050 if (tb
[RTA_EXPIRES
])
1051 m
->lastused
= *(unsigned long long *)RTA_DATA(tb
[RTA_EXPIRES
]);
1053 if (tb
[RTA_MULTIPATH
]) {
1054 struct rtnexthop
*rtnh
=
1055 (struct rtnexthop
*)RTA_DATA(tb
[RTA_MULTIPATH
]);
1057 len
= RTA_PAYLOAD(tb
[RTA_MULTIPATH
]);
1059 if (len
< (int)sizeof(*rtnh
) || rtnh
->rtnh_len
> len
)
1062 oif
[oif_count
] = rtnh
->rtnh_ifindex
;
1065 if (rtnh
->rtnh_len
== 0)
1068 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1069 rtnh
= RTNH_NEXT(rtnh
);
1073 if (IS_ZEBRA_DEBUG_KERNEL
) {
1074 struct interface
*ifp
= NULL
;
1075 struct zebra_vrf
*zvrf
= NULL
;
1077 for (count
= 0; count
< oif_count
; count
++) {
1078 ifp
= if_lookup_by_index(oif
[count
], vrf
);
1081 snprintf(temp
, sizeof(temp
), "%s(%d) ",
1082 ifp
? ifp
->name
: "Unknown", oif
[count
]);
1083 strlcat(oif_list
, temp
, sizeof(oif_list
));
1085 zvrf
= zebra_vrf_lookup_by_id(vrf
);
1086 ifp
= if_lookup_by_index(iif
, vrf
);
1088 "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld",
1089 zvrf_name(zvrf
), vrf
, nl_msg_type_to_str(h
->nlmsg_type
),
1090 &m
->sg
.src
, &m
->sg
.grp
, ifp
? ifp
->name
: "Unknown",
1097 int netlink_route_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1102 rtm
= NLMSG_DATA(h
);
1104 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
)) {
1105 /* If this is not route add/delete message print warning. */
1106 zlog_debug("Kernel message: %s NS %u",
1107 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
1111 if (!(rtm
->rtm_family
== AF_INET
||
1112 rtm
->rtm_family
== AF_INET6
||
1113 rtm
->rtm_family
== RTNL_FAMILY_IPMR
)) {
1115 EC_ZEBRA_UNKNOWN_FAMILY
,
1116 "Invalid address family: %u received from kernel route change: %s",
1117 rtm
->rtm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1121 /* Connected route. */
1122 if (IS_ZEBRA_DEBUG_KERNEL
)
1123 zlog_debug("%s %s %s proto %s NS %u",
1124 nl_msg_type_to_str(h
->nlmsg_type
),
1125 nl_family_to_str(rtm
->rtm_family
),
1126 nl_rttype_to_str(rtm
->rtm_type
),
1127 nl_rtproto_to_str(rtm
->rtm_protocol
), ns_id
);
1130 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct rtmsg
));
1133 "%s: Message received from netlink is of a broken size: %d %zu",
1134 __func__
, h
->nlmsg_len
,
1135 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg
)));
1139 if (rtm
->rtm_type
== RTN_MULTICAST
)
1140 netlink_route_change_read_multicast(h
, ns_id
, startup
);
1142 netlink_route_change_read_unicast(h
, ns_id
, startup
);
1146 /* Request for specific route information from the kernel */
1147 static int netlink_request_route(struct zebra_ns
*zns
, int family
, int type
)
1154 /* Form the request, specifying filter (rtattr) if needed. */
1155 memset(&req
, 0, sizeof(req
));
1156 req
.n
.nlmsg_type
= type
;
1157 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1158 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1159 req
.rtm
.rtm_family
= family
;
1161 return netlink_request(&zns
->netlink_cmd
, &req
);
1164 /* Routing table read function using netlink interface. Only called
1166 int netlink_route_read(struct zebra_ns
*zns
)
1169 struct zebra_dplane_info dp_info
;
1171 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1173 /* Get IPv4 routing table. */
1174 ret
= netlink_request_route(zns
, AF_INET
, RTM_GETROUTE
);
1177 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1178 &zns
->netlink_cmd
, &dp_info
, 0, true);
1182 /* Get IPv6 routing table. */
1183 ret
= netlink_request_route(zns
, AF_INET6
, RTM_GETROUTE
);
1186 ret
= netlink_parse_info(netlink_route_change_read_unicast
,
1187 &zns
->netlink_cmd
, &dp_info
, 0, true);
1195 * The function returns true if the gateway info could be added
1196 * to the message, otherwise false is returned.
1198 static bool _netlink_route_add_gateway_info(uint8_t route_family
,
1200 struct nlmsghdr
*nlmsg
,
1201 size_t req_size
, int bytelen
,
1202 const struct nexthop
*nexthop
)
1204 if (route_family
== AF_MPLS
) {
1205 struct gw_family_t gw_fam
;
1207 gw_fam
.family
= gw_family
;
1208 if (gw_family
== AF_INET
)
1209 memcpy(&gw_fam
.gate
.ipv4
, &nexthop
->gate
.ipv4
, bytelen
);
1211 memcpy(&gw_fam
.gate
.ipv6
, &nexthop
->gate
.ipv6
, bytelen
);
1212 if (!nl_attr_put(nlmsg
, req_size
, RTA_VIA
, &gw_fam
.family
,
1216 if (!(nexthop
->rparent
1217 && IS_MAPPED_IPV6(&nexthop
->rparent
->gate
.ipv6
))) {
1218 if (gw_family
== AF_INET
) {
1219 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1220 &nexthop
->gate
.ipv4
, bytelen
))
1223 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
,
1224 &nexthop
->gate
.ipv6
, bytelen
))
1233 static int build_label_stack(struct mpls_label_stack
*nh_label
,
1234 mpls_lse_t
*out_lse
, char *label_buf
,
1235 size_t label_buf_size
)
1237 char label_buf1
[20];
1240 for (int i
= 0; nh_label
&& i
< nh_label
->num_labels
; i
++) {
1241 if (nh_label
->label
[i
] == MPLS_LABEL_IMPLICIT_NULL
)
1244 if (IS_ZEBRA_DEBUG_KERNEL
) {
1246 snprintf(label_buf
, label_buf_size
, "label %u",
1247 nh_label
->label
[i
]);
1249 snprintf(label_buf1
, sizeof(label_buf1
), "/%u",
1250 nh_label
->label
[i
]);
1251 strlcat(label_buf
, label_buf1
, label_buf_size
);
1255 out_lse
[num_labels
] =
1256 mpls_lse_encode(nh_label
->label
[i
], 0, 0, 0);
1263 static bool _netlink_route_encode_label_info(struct mpls_label_stack
*nh_label
,
1264 struct nlmsghdr
*nlmsg
,
1265 size_t buflen
, struct rtmsg
*rtmsg
,
1267 size_t label_buf_size
)
1269 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
1273 * label_buf is *only* currently used within debugging.
1274 * As such when we assign it we are guarding it inside
1275 * a debug test. If you want to change this make sure
1276 * you fix this assumption
1278 label_buf
[0] = '\0';
1281 build_label_stack(nh_label
, out_lse
, label_buf
, label_buf_size
);
1284 /* Set the BoS bit */
1285 out_lse
[num_labels
- 1] |= htonl(1 << MPLS_LS_S_SHIFT
);
1287 if (rtmsg
->rtm_family
== AF_MPLS
) {
1288 if (!nl_attr_put(nlmsg
, buflen
, RTA_NEWDST
, &out_lse
,
1289 num_labels
* sizeof(mpls_lse_t
)))
1292 struct rtattr
*nest
;
1294 if (!nl_attr_put16(nlmsg
, buflen
, RTA_ENCAP_TYPE
,
1295 LWTUNNEL_ENCAP_MPLS
))
1298 nest
= nl_attr_nest(nlmsg
, buflen
, RTA_ENCAP
);
1302 if (!nl_attr_put(nlmsg
, buflen
, MPLS_IPTUNNEL_DST
,
1304 num_labels
* sizeof(mpls_lse_t
)))
1306 nl_attr_nest_end(nlmsg
, nest
);
1313 static bool _netlink_route_encode_nexthop_src(const struct nexthop
*nexthop
,
1315 struct nlmsghdr
*nlmsg
,
1316 size_t buflen
, int bytelen
)
1318 if (family
== AF_INET
) {
1319 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1320 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1321 &nexthop
->rmap_src
.ipv4
, bytelen
))
1323 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1324 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1325 &nexthop
->src
.ipv4
, bytelen
))
1328 } else if (family
== AF_INET6
) {
1329 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1330 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1331 &nexthop
->rmap_src
.ipv6
, bytelen
))
1333 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1334 if (!nl_attr_put(nlmsg
, buflen
, RTA_PREFSRC
,
1335 &nexthop
->src
.ipv6
, bytelen
))
1343 static ssize_t
fill_seg6ipt_encap(char *buffer
, size_t buflen
,
1344 const struct in6_addr
*seg
)
1346 struct seg6_iptunnel_encap
*ipt
;
1347 struct ipv6_sr_hdr
*srh
;
1348 const size_t srhlen
= 24;
1351 * Caution: Support only SINGLE-SID, not MULTI-SID
1352 * This function only supports the case where segs represents
1353 * a single SID. If you want to extend the SRv6 functionality,
1354 * you should improve the Boundary Check.
1355 * Ex. In case of set a SID-List include multiple-SIDs as an
1356 * argument of the Transit Behavior, we must support variable
1357 * boundary check for buflen.
1359 if (buflen
< (sizeof(struct seg6_iptunnel_encap
) +
1360 sizeof(struct ipv6_sr_hdr
) + 16))
1363 memset(buffer
, 0, buflen
);
1365 ipt
= (struct seg6_iptunnel_encap
*)buffer
;
1366 ipt
->mode
= SEG6_IPTUN_MODE_ENCAP
;
1368 srh
->hdrlen
= (srhlen
>> 3) - 1;
1370 srh
->segments_left
= 0;
1371 srh
->first_segment
= 0;
1372 memcpy(&srh
->segments
[0], seg
, sizeof(struct in6_addr
));
1377 /* This function takes a nexthop as argument and adds
1378 * the appropriate netlink attributes to an existing
1381 * @param routedesc: Human readable description of route type
1382 * (direct/recursive, single-/multipath)
1383 * @param bytelen: Length of addresses in bytes.
1384 * @param nexthop: Nexthop information
1385 * @param nlmsg: nlmsghdr structure to fill in.
1386 * @param req_size: The size allocated for the message.
1388 * The function returns true if the nexthop could be added
1389 * to the message, otherwise false is returned.
1391 static bool _netlink_route_build_singlepath(const struct prefix
*p
,
1392 const char *routedesc
, int bytelen
,
1393 const struct nexthop
*nexthop
,
1394 struct nlmsghdr
*nlmsg
,
1395 struct rtmsg
*rtmsg
,
1396 size_t req_size
, int cmd
)
1399 char label_buf
[256];
1401 char addrstr
[INET6_ADDRSTRLEN
];
1405 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1407 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1408 req_size
, rtmsg
, label_buf
,
1412 if (nexthop
->nh_srv6
) {
1413 if (nexthop
->nh_srv6
->seg6local_action
!=
1414 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
1415 struct rtattr
*nest
;
1416 const struct seg6local_context
*ctx
;
1418 ctx
= &nexthop
->nh_srv6
->seg6local_ctx
;
1419 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1420 LWTUNNEL_ENCAP_SEG6_LOCAL
))
1423 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1427 switch (nexthop
->nh_srv6
->seg6local_action
) {
1428 case ZEBRA_SEG6_LOCAL_ACTION_END
:
1429 if (!nl_attr_put32(nlmsg
, req_size
,
1431 SEG6_LOCAL_ACTION_END
))
1434 case ZEBRA_SEG6_LOCAL_ACTION_END_X
:
1435 if (!nl_attr_put32(nlmsg
, req_size
,
1437 SEG6_LOCAL_ACTION_END_X
))
1439 if (!nl_attr_put(nlmsg
, req_size
,
1440 SEG6_LOCAL_NH6
, &ctx
->nh6
,
1441 sizeof(struct in6_addr
)))
1444 case ZEBRA_SEG6_LOCAL_ACTION_END_T
:
1445 if (!nl_attr_put32(nlmsg
, req_size
,
1447 SEG6_LOCAL_ACTION_END_T
))
1449 if (!nl_attr_put32(nlmsg
, req_size
,
1454 case ZEBRA_SEG6_LOCAL_ACTION_END_DX4
:
1455 if (!nl_attr_put32(nlmsg
, req_size
,
1457 SEG6_LOCAL_ACTION_END_DX4
))
1459 if (!nl_attr_put(nlmsg
, req_size
,
1460 SEG6_LOCAL_NH4
, &ctx
->nh4
,
1461 sizeof(struct in_addr
)))
1464 case ZEBRA_SEG6_LOCAL_ACTION_END_DT6
:
1465 if (!nl_attr_put32(nlmsg
, req_size
,
1467 SEG6_LOCAL_ACTION_END_DT6
))
1469 if (!nl_attr_put32(nlmsg
, req_size
,
1474 case ZEBRA_SEG6_LOCAL_ACTION_END_DT4
:
1475 if (!nl_attr_put32(nlmsg
, req_size
,
1477 SEG6_LOCAL_ACTION_END_DT4
))
1479 if (!nl_attr_put32(nlmsg
, req_size
,
1480 SEG6_LOCAL_VRFTABLE
,
1485 zlog_err("%s: unsupport seg6local behaviour action=%u",
1487 nexthop
->nh_srv6
->seg6local_action
);
1490 nl_attr_nest_end(nlmsg
, nest
);
1493 if (!sid_zero(&nexthop
->nh_srv6
->seg6_segs
)) {
1496 struct rtattr
*nest
;
1498 if (!nl_attr_put16(nlmsg
, req_size
, RTA_ENCAP_TYPE
,
1499 LWTUNNEL_ENCAP_SEG6
))
1501 nest
= nl_attr_nest(nlmsg
, req_size
, RTA_ENCAP
);
1504 tun_len
= fill_seg6ipt_encap(tun_buf
, sizeof(tun_buf
),
1505 &nexthop
->nh_srv6
->seg6_segs
);
1508 if (!nl_attr_put(nlmsg
, req_size
, SEG6_IPTUNNEL_SRH
,
1511 nl_attr_nest_end(nlmsg
, nest
);
1515 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1516 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1518 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1519 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1520 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1522 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1525 if (cmd
== RTM_NEWROUTE
) {
1526 if (!_netlink_route_encode_nexthop_src(
1527 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1531 if (IS_ZEBRA_DEBUG_KERNEL
)
1532 zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1533 __func__
, routedesc
, p
, ipv4_ll_buf
,
1534 label_buf
, nexthop
->ifindex
,
1535 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1539 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1540 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1541 /* Send deletes to the kernel without specifying the next-hop */
1542 if (cmd
!= RTM_DELROUTE
) {
1543 if (!_netlink_route_add_gateway_info(
1544 rtmsg
->rtm_family
, AF_INET
, nlmsg
, req_size
,
1549 if (cmd
== RTM_NEWROUTE
) {
1550 if (!_netlink_route_encode_nexthop_src(
1551 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1555 if (IS_ZEBRA_DEBUG_KERNEL
) {
1556 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, addrstr
,
1558 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1559 __func__
, routedesc
, p
, addrstr
, label_buf
,
1560 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1565 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1566 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1567 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1568 AF_INET6
, nlmsg
, req_size
,
1572 if (cmd
== RTM_NEWROUTE
) {
1573 if (!_netlink_route_encode_nexthop_src(
1574 nexthop
, AF_INET6
, nlmsg
, req_size
,
1579 if (IS_ZEBRA_DEBUG_KERNEL
) {
1580 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, addrstr
,
1582 zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1583 __func__
, routedesc
, p
, addrstr
, label_buf
,
1584 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1590 * We have the ifindex so we should always send it
1591 * This is especially useful if we are doing route
1594 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1595 if (!nl_attr_put32(nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
))
1599 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1600 if (cmd
== RTM_NEWROUTE
) {
1601 if (!_netlink_route_encode_nexthop_src(
1602 nexthop
, AF_INET
, nlmsg
, req_size
, bytelen
))
1606 if (IS_ZEBRA_DEBUG_KERNEL
)
1607 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1608 __func__
, routedesc
, p
, nexthop
->ifindex
,
1609 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1615 /* This function takes a nexthop as argument and
1616 * appends to the given netlink msg. If the nexthop
1617 * defines a preferred source, the src parameter
1618 * will be modified to point to that src, otherwise
1619 * it will be kept unmodified.
1621 * @param routedesc: Human readable description of route type
1622 * (direct/recursive, single-/multipath)
1623 * @param bytelen: Length of addresses in bytes.
1624 * @param nexthop: Nexthop information
1625 * @param nlmsg: nlmsghdr structure to fill in.
1626 * @param req_size: The size allocated for the message.
1627 * @param src: pointer pointing to a location where
1628 * the prefsrc should be stored.
1630 * The function returns true if the nexthop could be added
1631 * to the message, otherwise false is returned.
1633 static bool _netlink_route_build_multipath(const struct prefix
*p
,
1634 const char *routedesc
, int bytelen
,
1635 const struct nexthop
*nexthop
,
1636 struct nlmsghdr
*nlmsg
,
1637 size_t req_size
, struct rtmsg
*rtmsg
,
1638 const union g_addr
**src
)
1640 char label_buf
[256];
1642 struct rtnexthop
*rtnh
;
1644 rtnh
= nl_attr_rtnh(nlmsg
, req_size
);
1650 vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
1652 if (!_netlink_route_encode_label_info(nexthop
->nh_label
, nlmsg
,
1653 req_size
, rtmsg
, label_buf
,
1657 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1658 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1660 if (is_route_v4_over_v6(rtmsg
->rtm_family
, nexthop
->type
)) {
1661 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1662 if (!nl_attr_put(nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4))
1664 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1665 if (nexthop
->weight
)
1666 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1668 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1669 *src
= &nexthop
->rmap_src
;
1670 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1671 *src
= &nexthop
->src
;
1673 if (IS_ZEBRA_DEBUG_KERNEL
)
1675 "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)",
1676 __func__
, routedesc
, p
, ipv4_ll_buf
, label_buf
,
1677 nexthop
->ifindex
, VRF_LOGNAME(vrf
),
1679 nl_attr_rtnh_end(nlmsg
, rtnh
);
1683 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1684 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1685 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
, AF_INET
,
1686 nlmsg
, req_size
, bytelen
,
1690 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1691 *src
= &nexthop
->rmap_src
;
1692 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1693 *src
= &nexthop
->src
;
1695 if (IS_ZEBRA_DEBUG_KERNEL
)
1696 zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)",
1697 __func__
, routedesc
, p
, &nexthop
->gate
.ipv4
,
1698 label_buf
, nexthop
->ifindex
,
1699 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1701 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1702 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
1703 if (!_netlink_route_add_gateway_info(rtmsg
->rtm_family
,
1704 AF_INET6
, nlmsg
, req_size
,
1708 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1709 *src
= &nexthop
->rmap_src
;
1710 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1711 *src
= &nexthop
->src
;
1713 if (IS_ZEBRA_DEBUG_KERNEL
)
1714 zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)",
1715 __func__
, routedesc
, p
, &nexthop
->gate
.ipv6
,
1716 label_buf
, nexthop
->ifindex
,
1717 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1721 * We have figured out the ifindex so we should always send it
1722 * This is especially useful if we are doing route
1725 if (nexthop
->type
!= NEXTHOP_TYPE_BLACKHOLE
)
1726 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1729 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
1730 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
)
1731 *src
= &nexthop
->rmap_src
;
1732 else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
1733 *src
= &nexthop
->src
;
1735 if (IS_ZEBRA_DEBUG_KERNEL
)
1736 zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)",
1737 __func__
, routedesc
, p
, nexthop
->ifindex
,
1738 VRF_LOGNAME(vrf
), nexthop
->vrf_id
);
1741 if (nexthop
->weight
)
1742 rtnh
->rtnh_hops
= nexthop
->weight
- 1;
1744 nl_attr_rtnh_end(nlmsg
, rtnh
);
1749 _netlink_mpls_build_singlepath(const struct prefix
*p
, const char *routedesc
,
1750 const struct zebra_nhlfe
*nhlfe
,
1751 struct nlmsghdr
*nlmsg
, struct rtmsg
*rtmsg
,
1752 size_t req_size
, int cmd
)
1757 family
= NHLFE_FAMILY(nhlfe
);
1758 bytelen
= (family
== AF_INET
? 4 : 16);
1759 return _netlink_route_build_singlepath(p
, routedesc
, bytelen
,
1760 nhlfe
->nexthop
, nlmsg
, rtmsg
,
1766 _netlink_mpls_build_multipath(const struct prefix
*p
, const char *routedesc
,
1767 const struct zebra_nhlfe
*nhlfe
,
1768 struct nlmsghdr
*nlmsg
, size_t req_size
,
1769 struct rtmsg
*rtmsg
, const union g_addr
**src
)
1774 family
= NHLFE_FAMILY(nhlfe
);
1775 bytelen
= (family
== AF_INET
? 4 : 16);
1776 return _netlink_route_build_multipath(p
, routedesc
, bytelen
,
1777 nhlfe
->nexthop
, nlmsg
, req_size
,
1781 static void _netlink_mpls_debug(int cmd
, uint32_t label
, const char *routedesc
)
1783 if (IS_ZEBRA_DEBUG_KERNEL
)
1784 zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20",
1785 routedesc
, nl_msg_type_to_str(cmd
), label
);
1788 static int netlink_neigh_update(int cmd
, int ifindex
, void *addr
, char *lla
,
1789 int llalen
, ns_id_t ns_id
, uint8_t family
,
1790 bool permanent
, uint8_t protocol
)
1798 struct zebra_ns
*zns
= zebra_ns_lookup(ns_id
);
1800 memset(&req
, 0, sizeof(req
));
1802 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
1803 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1804 req
.n
.nlmsg_type
= cmd
; // RTM_NEWNEIGH or RTM_DELNEIGH
1805 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1807 req
.ndm
.ndm_family
= family
;
1808 req
.ndm
.ndm_ifindex
= ifindex
;
1809 req
.ndm
.ndm_type
= RTN_UNICAST
;
1810 if (cmd
== RTM_NEWNEIGH
) {
1812 req
.ndm
.ndm_state
= NUD_REACHABLE
;
1814 req
.ndm
.ndm_state
= NUD_PERMANENT
;
1816 req
.ndm
.ndm_state
= NUD_FAILED
;
1818 nl_attr_put(&req
.n
, sizeof(req
), NDA_PROTOCOL
, &protocol
,
1820 req
.ndm
.ndm_type
= RTN_UNICAST
;
1821 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, addr
,
1822 family2addrsize(family
));
1824 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
1826 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1830 static bool nexthop_set_src(const struct nexthop
*nexthop
, int family
,
1833 if (family
== AF_INET
) {
1834 if (nexthop
->rmap_src
.ipv4
.s_addr
!= INADDR_ANY
) {
1835 src
->ipv4
= nexthop
->rmap_src
.ipv4
;
1837 } else if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
) {
1838 src
->ipv4
= nexthop
->src
.ipv4
;
1841 } else if (family
== AF_INET6
) {
1842 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
)) {
1843 src
->ipv6
= nexthop
->rmap_src
.ipv6
;
1845 } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
)) {
1846 src
->ipv6
= nexthop
->src
.ipv6
;
1855 * The function returns true if the attribute could be added
1856 * to the message, otherwise false is returned.
1858 static int netlink_route_nexthop_encap(struct nlmsghdr
*n
, size_t nlen
,
1861 struct rtattr
*nest
;
1863 switch (nh
->nh_encap_type
) {
1865 if (!nl_attr_put16(n
, nlen
, RTA_ENCAP_TYPE
, nh
->nh_encap_type
))
1868 nest
= nl_attr_nest(n
, nlen
, RTA_ENCAP
);
1872 if (!nl_attr_put32(n
, nlen
, 0 /* VXLAN_VNI */,
1875 nl_attr_nest_end(n
, nest
);
1883 * Routing table change via netlink interface, using a dataplane context object
1885 * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
1886 * otherwise the number of bytes written to buf.
1888 ssize_t
netlink_route_multipath_msg_encode(int cmd
,
1889 struct zebra_dplane_ctx
*ctx
,
1890 uint8_t *data
, size_t datalen
,
1891 bool fpm
, bool force_nhg
)
1894 struct nexthop
*nexthop
= NULL
;
1895 unsigned int nexthop_num
;
1896 const char *routedesc
;
1897 bool setsrc
= false;
1899 const struct prefix
*p
, *src_p
;
1907 } *req
= (void *)data
;
1909 p
= dplane_ctx_get_dest(ctx
);
1910 src_p
= dplane_ctx_get_src(ctx
);
1912 if (datalen
< sizeof(*req
))
1915 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1917 memset(req
, 0, sizeof(*req
));
1919 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1921 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1922 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1924 if ((cmd
== RTM_NEWROUTE
) &&
1925 ((p
->family
== AF_INET
) || v6_rr_semantics
))
1926 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
1928 req
->n
.nlmsg_type
= cmd
;
1930 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
1932 req
->r
.rtm_family
= p
->family
;
1933 req
->r
.rtm_dst_len
= p
->prefixlen
;
1934 req
->r
.rtm_src_len
= src_p
? src_p
->prefixlen
: 0;
1935 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1937 if (cmd
== RTM_DELROUTE
)
1938 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_old_type(ctx
));
1940 req
->r
.rtm_protocol
= zebra2proto(dplane_ctx_get_type(ctx
));
1943 * blackhole routes are not RTN_UNICAST, they are
1944 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1945 * so setting this value as a RTN_UNICAST would
1946 * cause the route lookup of just the prefix
1947 * to fail. So no need to specify this for
1948 * the RTM_DELROUTE case
1950 if (cmd
!= RTM_DELROUTE
)
1951 req
->r
.rtm_type
= RTN_UNICAST
;
1953 if (!nl_attr_put(&req
->n
, datalen
, RTA_DST
, &p
->u
.prefix
, bytelen
))
1956 if (!nl_attr_put(&req
->n
, datalen
, RTA_SRC
, &src_p
->u
.prefix
,
1962 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1964 * either by the kernel or by zebra. Its purely for calculating best
1966 * by the routing protocol and for communicating with protocol peers.
1968 if (!nl_attr_put32(&req
->n
, datalen
, RTA_PRIORITY
,
1969 NL_DEFAULT_ROUTE_METRIC
))
1972 #if defined(SUPPORT_REALMS)
1976 if (cmd
== RTM_DELROUTE
)
1977 tag
= dplane_ctx_get_old_tag(ctx
);
1979 tag
= dplane_ctx_get_tag(ctx
);
1981 if (tag
> 0 && tag
<= 255) {
1982 if (!nl_attr_put32(&req
->n
, datalen
, RTA_FLOW
, tag
))
1987 /* Table corresponding to this route. */
1988 table_id
= dplane_ctx_get_table(ctx
);
1990 req
->r
.rtm_table
= table_id
;
1992 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
1993 if (!nl_attr_put32(&req
->n
, datalen
, RTA_TABLE
, table_id
))
1997 if (IS_ZEBRA_DEBUG_KERNEL
)
1999 "%s: %s %pFX vrf %u(%u)", __func__
,
2000 nl_msg_type_to_str(cmd
), p
, dplane_ctx_get_vrf(ctx
),
2004 * If we are not updating the route and we have received
2005 * a route delete, then all we need to fill in is the
2006 * prefix information to tell the kernel to schwack
2009 if (cmd
== RTM_DELROUTE
)
2010 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2012 if (dplane_ctx_get_mtu(ctx
) || dplane_ctx_get_nh_mtu(ctx
)) {
2013 struct rtattr
*nest
;
2014 uint32_t mtu
= dplane_ctx_get_mtu(ctx
);
2015 uint32_t nexthop_mtu
= dplane_ctx_get_nh_mtu(ctx
);
2017 if (!mtu
|| (nexthop_mtu
&& nexthop_mtu
< mtu
))
2020 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_METRICS
);
2024 if (!nl_attr_put(&req
->n
, datalen
, RTAX_MTU
, &mtu
, sizeof(mtu
)))
2026 nl_attr_nest_end(&req
->n
, nest
);
2030 * Always install blackhole routes without using nexthops, because of
2031 * the following kernel problems:
2032 * 1. Kernel nexthops don't suport unreachable/prohibit route types.
2033 * 2. Blackhole kernel nexthops are deleted when loopback is down.
2035 nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
2037 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2038 nexthop
= nexthop
->resolved
;
2040 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
2041 switch (nexthop
->bh_type
) {
2042 case BLACKHOLE_ADMINPROHIB
:
2043 req
->r
.rtm_type
= RTN_PROHIBIT
;
2045 case BLACKHOLE_REJECT
:
2046 req
->r
.rtm_type
= RTN_UNREACHABLE
;
2049 req
->r
.rtm_type
= RTN_BLACKHOLE
;
2052 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2056 if ((!fpm
&& kernel_nexthops_supported()
2057 && (!proto_nexthops_only()
2058 || is_proto_nhg(dplane_ctx_get_nhe_id(ctx
), 0)))
2059 || (fpm
&& force_nhg
)) {
2060 /* Kernel supports nexthop objects */
2061 if (IS_ZEBRA_DEBUG_KERNEL
)
2062 zlog_debug("%s: %pFX nhg_id is %u", __func__
, p
,
2063 dplane_ctx_get_nhe_id(ctx
));
2065 if (!nl_attr_put32(&req
->n
, datalen
, RTA_NH_ID
,
2066 dplane_ctx_get_nhe_id(ctx
)))
2069 /* Have to determine src still */
2070 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2074 setsrc
= nexthop_set_src(nexthop
, p
->family
, &src
);
2078 if (p
->family
== AF_INET
) {
2079 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2080 &src
.ipv4
, bytelen
))
2082 } else if (p
->family
== AF_INET6
) {
2083 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2084 &src
.ipv6
, bytelen
))
2089 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2092 /* Count overall nexthops so we can decide whether to use singlepath
2093 * or multipath case.
2096 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2097 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2099 if (!NEXTHOP_IS_ACTIVE(nexthop
->flags
))
2105 /* Singlepath case. */
2106 if (nexthop_num
== 1) {
2108 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2109 if (CHECK_FLAG(nexthop
->flags
,
2110 NEXTHOP_FLAG_RECURSIVE
)) {
2115 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2120 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2121 routedesc
= nexthop
->rparent
2122 ? "recursive, single-path"
2125 if (!_netlink_route_build_singlepath(
2126 p
, routedesc
, bytelen
, nexthop
,
2127 &req
->n
, &req
->r
, datalen
, cmd
))
2134 * Add encapsulation information when installing via
2138 if (!netlink_route_nexthop_encap(
2139 &req
->n
, datalen
, nexthop
))
2145 if (p
->family
== AF_INET
) {
2146 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2147 &src
.ipv4
, bytelen
))
2149 } else if (p
->family
== AF_INET6
) {
2150 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2151 &src
.ipv6
, bytelen
))
2155 } else { /* Multipath case */
2156 struct rtattr
*nest
;
2157 const union g_addr
*src1
= NULL
;
2159 nest
= nl_attr_nest(&req
->n
, datalen
, RTA_MULTIPATH
);
2164 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2165 if (CHECK_FLAG(nexthop
->flags
,
2166 NEXTHOP_FLAG_RECURSIVE
)) {
2167 /* This only works for IPv4 now */
2171 setsrc
= nexthop_set_src(nexthop
, p
->family
,
2176 if (NEXTHOP_IS_ACTIVE(nexthop
->flags
)) {
2177 routedesc
= nexthop
->rparent
2178 ? "recursive, multipath"
2182 if (!_netlink_route_build_multipath(
2183 p
, routedesc
, bytelen
, nexthop
,
2184 &req
->n
, datalen
, &req
->r
, &src1
))
2187 if (!setsrc
&& src1
) {
2188 if (p
->family
== AF_INET
)
2189 src
.ipv4
= src1
->ipv4
;
2190 else if (p
->family
== AF_INET6
)
2191 src
.ipv6
= src1
->ipv6
;
2198 nl_attr_nest_end(&req
->n
, nest
);
2201 * Add encapsulation information when installing via
2205 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2207 if (CHECK_FLAG(nexthop
->flags
,
2208 NEXTHOP_FLAG_RECURSIVE
))
2210 if (!netlink_route_nexthop_encap(
2211 &req
->n
, datalen
, nexthop
))
2218 if (p
->family
== AF_INET
) {
2219 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2220 &src
.ipv4
, bytelen
))
2222 } else if (p
->family
== AF_INET6
) {
2223 if (!nl_attr_put(&req
->n
, datalen
, RTA_PREFSRC
,
2224 &src
.ipv6
, bytelen
))
2227 if (IS_ZEBRA_DEBUG_KERNEL
)
2228 zlog_debug("Setting source");
2232 /* If there is no useful nexthop then return. */
2233 if (nexthop_num
== 0) {
2234 if (IS_ZEBRA_DEBUG_KERNEL
)
2235 zlog_debug("%s: No useful nexthop.", __func__
);
2238 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2241 int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *in
)
2243 uint32_t actual_table
;
2245 struct mcast_route_data
*mr
= (struct mcast_route_data
*)in
;
2253 struct zebra_ns
*zns
;
2256 memset(&req
, 0, sizeof(req
));
2258 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2259 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2260 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2262 req
.ndm
.ndm_family
= RTNL_FAMILY_IPMR
;
2263 req
.n
.nlmsg_type
= RTM_GETROUTE
;
2265 nl_attr_put32(&req
.n
, sizeof(req
), RTA_IIF
, mroute
->ifindex
);
2266 nl_attr_put32(&req
.n
, sizeof(req
), RTA_OIF
, mroute
->ifindex
);
2267 nl_attr_put32(&req
.n
, sizeof(req
), RTA_SRC
, mroute
->sg
.src
.s_addr
);
2268 nl_attr_put32(&req
.n
, sizeof(req
), RTA_DST
, mroute
->sg
.grp
.s_addr
);
2272 * So during the namespace cleanup we started storing
2273 * the zvrf table_id for the default table as RT_TABLE_MAIN
2274 * which is what the normal routing table for ip routing is.
2275 * This change caused this to break our lookups of sg data
2276 * because prior to this change the zvrf->table_id was 0
2277 * and when the pim multicast kernel code saw a 0,
2278 * it was auto-translated to RT_TABLE_DEFAULT. But since
2279 * we are now passing in RT_TABLE_MAIN there is no auto-translation
2280 * and the kernel goes screw you and the delicious cookies you
2281 * are trying to give me. So now we have this little hack.
2283 actual_table
= (zvrf
->table_id
== RT_TABLE_MAIN
) ? RT_TABLE_DEFAULT
:
2285 nl_attr_put32(&req
.n
, sizeof(req
), RTA_TABLE
, actual_table
);
2287 suc
= netlink_talk(netlink_route_change_read_multicast
, &req
.n
,
2288 &zns
->netlink_cmd
, zns
, false);
2294 /* Char length to debug ID with */
2295 #define ID_LENGTH 10
2297 static bool _netlink_nexthop_build_group(struct nlmsghdr
*n
, size_t req_size
,
2299 const struct nh_grp
*z_grp
,
2300 const uint8_t count
)
2302 struct nexthop_grp grp
[count
];
2303 /* Need space for max group size, "/", and null term */
2304 char buf
[(MULTIPATH_NUM
* (ID_LENGTH
+ 1)) + 1];
2305 char buf1
[ID_LENGTH
+ 2];
2309 memset(grp
, 0, sizeof(grp
));
2312 for (int i
= 0; i
< count
; i
++) {
2313 grp
[i
].id
= z_grp
[i
].id
;
2314 grp
[i
].weight
= z_grp
[i
].weight
- 1;
2316 if (IS_ZEBRA_DEBUG_KERNEL
) {
2318 snprintf(buf
, sizeof(buf1
), "group %u",
2321 snprintf(buf1
, sizeof(buf1
), "/%u",
2323 strlcat(buf
, buf1
, sizeof(buf
));
2327 if (!nl_attr_put(n
, req_size
, NHA_GROUP
, grp
,
2328 count
* sizeof(*grp
)))
2332 if (IS_ZEBRA_DEBUG_KERNEL
)
2333 zlog_debug("%s: ID (%u): %s", __func__
, id
, buf
);
2339 * Next hop packet encoding helper function.
2341 * \param[in] cmd netlink command.
2342 * \param[in] ctx dataplane context (information snapshot).
2343 * \param[out] buf buffer to hold the packet.
2344 * \param[in] buflen amount of buffer bytes.
2346 * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer
2347 * otherwise the number of bytes written to buf.
2349 ssize_t
netlink_nexthop_msg_encode(uint16_t cmd
,
2350 const struct zebra_dplane_ctx
*ctx
,
2351 void *buf
, size_t buflen
)
2359 mpls_lse_t out_lse
[MPLS_MAX_LABELS
];
2360 char label_buf
[256];
2362 uint32_t id
= dplane_ctx_get_nhe_id(ctx
);
2363 int type
= dplane_ctx_get_nhe_type(ctx
);
2364 struct rtattr
*nest
;
2367 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2371 EC_ZEBRA_NHG_FIB_UPDATE
,
2372 "Failed trying to update a nexthop group in the kernel that does not have an ID");
2377 * Nothing to do if the kernel doesn't support nexthop objects or
2378 * we dont want to install this type of NHG
2380 if (!kernel_nexthops_supported()) {
2381 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2383 "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring",
2384 __func__
, id
, zebra_route_string(type
));
2388 if (proto_nexthops_only() && !is_proto_nhg(id
, type
)) {
2389 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
2391 "%s: nhg_id %u (%s): proto-based nexthops only, ignoring",
2392 __func__
, id
, zebra_route_string(type
));
2396 label_buf
[0] = '\0';
2398 if (buflen
< sizeof(*req
))
2401 memset(req
, 0, sizeof(*req
));
2403 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
2404 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2406 if (cmd
== RTM_NEWNEXTHOP
)
2407 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
2409 req
->n
.nlmsg_type
= cmd
;
2410 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2412 req
->nhm
.nh_family
= AF_UNSPEC
;
2415 if (!nl_attr_put32(&req
->n
, buflen
, NHA_ID
, id
))
2418 if (cmd
== RTM_NEWNEXTHOP
) {
2420 * We distinguish between a "group", which is a collection
2421 * of ids, and a singleton nexthop with an id. The
2422 * group is installed as an id that just refers to a list of
2425 if (dplane_ctx_get_nhe_nh_grp_count(ctx
)) {
2426 if (!_netlink_nexthop_build_group(
2427 &req
->n
, buflen
, id
,
2428 dplane_ctx_get_nhe_nh_grp(ctx
),
2429 dplane_ctx_get_nhe_nh_grp_count(ctx
)))
2432 const struct nexthop
*nh
=
2433 dplane_ctx_get_nhe_ng(ctx
)->nexthop
;
2434 afi_t afi
= dplane_ctx_get_nhe_afi(ctx
);
2437 req
->nhm
.nh_family
= AF_INET
;
2438 else if (afi
== AFI_IP6
)
2439 req
->nhm
.nh_family
= AF_INET6
;
2442 case NEXTHOP_TYPE_IPV4
:
2443 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2444 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2449 case NEXTHOP_TYPE_IPV6
:
2450 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2451 if (!nl_attr_put(&req
->n
, buflen
, NHA_GATEWAY
,
2456 case NEXTHOP_TYPE_BLACKHOLE
:
2457 if (!nl_attr_put(&req
->n
, buflen
, NHA_BLACKHOLE
,
2460 /* Blackhole shouldn't have anymore attributes
2463 case NEXTHOP_TYPE_IFINDEX
:
2464 /* Don't need anymore info for this */
2470 EC_ZEBRA_NHG_FIB_UPDATE
,
2471 "Context received for kernel nexthop update without an interface");
2475 if (!nl_attr_put32(&req
->n
, buflen
, NHA_OIF
,
2479 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ONLINK
))
2480 req
->nhm
.nh_flags
|= RTNH_F_ONLINK
;
2483 build_label_stack(nh
->nh_label
, out_lse
,
2484 label_buf
, sizeof(label_buf
));
2487 /* Set the BoS bit */
2488 out_lse
[num_labels
- 1] |=
2489 htonl(1 << MPLS_LS_S_SHIFT
);
2492 * TODO: MPLS unsupported for now in kernel.
2494 if (req
->nhm
.nh_family
== AF_MPLS
)
2497 encap
= LWTUNNEL_ENCAP_MPLS
;
2498 if (!nl_attr_put16(&req
->n
, buflen
,
2499 NHA_ENCAP_TYPE
, encap
))
2501 nest
= nl_attr_nest(&req
->n
, buflen
, NHA_ENCAP
);
2505 &req
->n
, buflen
, MPLS_IPTUNNEL_DST
,
2507 num_labels
* sizeof(mpls_lse_t
)))
2510 nl_attr_nest_end(&req
->n
, nest
);
2514 if (nh
->nh_srv6
->seg6local_action
!=
2515 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC
) {
2518 struct rtattr
*nest
;
2519 const struct seg6local_context
*ctx
;
2521 req
->nhm
.nh_family
= AF_INET6
;
2522 action
= nh
->nh_srv6
->seg6local_action
;
2523 ctx
= &nh
->nh_srv6
->seg6local_ctx
;
2524 encap
= LWTUNNEL_ENCAP_SEG6_LOCAL
;
2525 if (!nl_attr_put(&req
->n
, buflen
,
2531 nest
= nl_attr_nest(&req
->n
, buflen
,
2532 NHA_ENCAP
| NLA_F_NESTED
);
2537 case SEG6_LOCAL_ACTION_END
:
2541 SEG6_LOCAL_ACTION_END
))
2544 case SEG6_LOCAL_ACTION_END_X
:
2548 SEG6_LOCAL_ACTION_END_X
))
2552 SEG6_LOCAL_NH6
, &ctx
->nh6
,
2553 sizeof(struct in6_addr
)))
2556 case SEG6_LOCAL_ACTION_END_T
:
2560 SEG6_LOCAL_ACTION_END_T
))
2568 case SEG6_LOCAL_ACTION_END_DX4
:
2572 SEG6_LOCAL_ACTION_END_DX4
))
2576 SEG6_LOCAL_NH4
, &ctx
->nh4
,
2577 sizeof(struct in_addr
)))
2580 case SEG6_LOCAL_ACTION_END_DT6
:
2584 SEG6_LOCAL_ACTION_END_DT6
))
2592 case SEG6_LOCAL_ACTION_END_DT4
:
2596 SEG6_LOCAL_ACTION_END_DT4
))
2600 SEG6_LOCAL_VRFTABLE
,
2605 zlog_err("%s: unsupport seg6local behaviour action=%u",
2609 nl_attr_nest_end(&req
->n
, nest
);
2612 if (!sid_zero(&nh
->nh_srv6
->seg6_segs
)) {
2615 struct rtattr
*nest
;
2617 if (!nl_attr_put16(&req
->n
, buflen
,
2619 LWTUNNEL_ENCAP_SEG6
))
2621 nest
= nl_attr_nest(&req
->n
, buflen
,
2622 NHA_ENCAP
| NLA_F_NESTED
);
2625 tun_len
= fill_seg6ipt_encap(tun_buf
,
2627 &nh
->nh_srv6
->seg6_segs
);
2630 if (!nl_attr_put(&req
->n
, buflen
,
2634 nl_attr_nest_end(&req
->n
, nest
);
2640 if (IS_ZEBRA_DEBUG_KERNEL
)
2641 zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ",
2642 __func__
, id
, nh
, nh
->ifindex
,
2643 vrf_id_to_name(nh
->vrf_id
),
2644 nh
->vrf_id
, label_buf
);
2647 req
->nhm
.nh_protocol
= zebra2proto(type
);
2649 } else if (cmd
!= RTM_DELNEXTHOP
) {
2651 EC_ZEBRA_NHG_FIB_UPDATE
,
2652 "Nexthop group kernel update command (%d) does not exist",
2657 if (IS_ZEBRA_DEBUG_KERNEL
)
2658 zlog_debug("%s: %s, id=%u", __func__
, nl_msg_type_to_str(cmd
),
2661 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2664 static ssize_t
netlink_nexthop_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2665 void *buf
, size_t buflen
)
2667 enum dplane_op_e op
;
2670 op
= dplane_ctx_get_op(ctx
);
2671 if (op
== DPLANE_OP_NH_INSTALL
|| op
== DPLANE_OP_NH_UPDATE
)
2672 cmd
= RTM_NEWNEXTHOP
;
2673 else if (op
== DPLANE_OP_NH_DELETE
)
2674 cmd
= RTM_DELNEXTHOP
;
2676 flog_err(EC_ZEBRA_NHG_FIB_UPDATE
,
2677 "Context received for kernel nexthop update with incorrect OP code (%u)",
2682 return netlink_nexthop_msg_encode(cmd
, ctx
, buf
, buflen
);
2685 enum netlink_msg_status
2686 netlink_put_nexthop_update_msg(struct nl_batch
*bth
,
2687 struct zebra_dplane_ctx
*ctx
)
2689 /* Nothing to do if the kernel doesn't support nexthop objects */
2690 if (!kernel_nexthops_supported())
2691 return FRR_NETLINK_SUCCESS
;
2693 return netlink_batch_add_msg(bth
, ctx
, netlink_nexthop_msg_encoder
,
2697 static ssize_t
netlink_newroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2698 void *buf
, size_t buflen
)
2700 return netlink_route_multipath_msg_encode(RTM_NEWROUTE
, ctx
, buf
,
2701 buflen
, false, false);
2704 static ssize_t
netlink_delroute_msg_encoder(struct zebra_dplane_ctx
*ctx
,
2705 void *buf
, size_t buflen
)
2707 return netlink_route_multipath_msg_encode(RTM_DELROUTE
, ctx
, buf
,
2708 buflen
, false, false);
2711 enum netlink_msg_status
2712 netlink_put_route_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
2715 const struct prefix
*p
= dplane_ctx_get_dest(ctx
);
2717 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
2719 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
2721 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
2723 if (p
->family
== AF_INET
|| v6_rr_semantics
) {
2724 /* Single 'replace' operation */
2727 * With route replace semantics in place
2728 * for v4 routes and the new route is a system
2729 * route we do not install anything.
2730 * The problem here is that the new system
2731 * route should cause us to withdraw from
2732 * the kernel the old non-system route
2734 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
))
2735 && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2736 netlink_batch_add_msg(
2737 bth
, ctx
, netlink_delroute_msg_encoder
,
2741 * So v6 route replace semantics are not in
2742 * the kernel at this point as I understand it.
2743 * so let's do a delete then an add.
2744 * In the future once v6 route replace semantics
2745 * are in we can figure out what to do here to
2746 * allow working with old and new kernels.
2748 * I'm also intentionally ignoring the failure case
2749 * of the route delete. If that happens yeah we're
2752 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx
)))
2753 netlink_batch_add_msg(
2754 bth
, ctx
, netlink_delroute_msg_encoder
,
2760 return FRR_NETLINK_ERROR
;
2762 if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx
)))
2763 return FRR_NETLINK_SUCCESS
;
2765 return netlink_batch_add_msg(bth
, ctx
,
2767 ? netlink_newroute_msg_encoder
2768 : netlink_delroute_msg_encoder
,
2773 * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop
2775 * @tb: Netlink RTA data
2776 * @family: Address family in the nhmsg
2777 * @ifp: Interface connected - this should be NULL, we fill it in
2778 * @ns_id: Namspace id
2780 * Return: New nexthop
2782 static struct nexthop
netlink_nexthop_process_nh(struct rtattr
**tb
,
2783 unsigned char family
,
2784 struct interface
**ifp
,
2787 struct nexthop nh
= {};
2789 enum nexthop_types_t type
= 0;
2792 struct interface
*ifp_lookup
;
2794 if_index
= *(int *)RTA_DATA(tb
[NHA_OIF
]);
2797 if (tb
[NHA_GATEWAY
]) {
2800 type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
2804 type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
2809 EC_ZEBRA_BAD_NHG_MESSAGE
,
2810 "Nexthop gateway with bad address family (%d) received from kernel",
2814 gate
= RTA_DATA(tb
[NHA_GATEWAY
]);
2816 type
= NEXTHOP_TYPE_IFINDEX
;
2822 memcpy(&(nh
.gate
), gate
, sz
);
2825 nh
.ifindex
= if_index
;
2828 if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), nh
.ifindex
);
2833 nh
.vrf_id
= ifp_lookup
->vrf
->vrf_id
;
2836 EC_ZEBRA_UNKNOWN_INTERFACE
,
2837 "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT",
2838 __func__
, nh
.ifindex
);
2840 nh
.vrf_id
= VRF_DEFAULT
;
2843 if (tb
[NHA_ENCAP
] && tb
[NHA_ENCAP_TYPE
]) {
2844 uint16_t encap_type
= *(uint16_t *)RTA_DATA(tb
[NHA_ENCAP_TYPE
]);
2847 mpls_label_t labels
[MPLS_MAX_LABELS
] = {0};
2849 if (encap_type
== LWTUNNEL_ENCAP_MPLS
)
2850 num_labels
= parse_encap_mpls(tb
[NHA_ENCAP
], labels
);
2853 nexthop_add_labels(&nh
, ZEBRA_LSP_STATIC
, num_labels
,
2860 static int netlink_nexthop_process_group(struct rtattr
**tb
,
2861 struct nh_grp
*z_grp
, int z_grp_size
)
2864 /* linux/nexthop.h group struct */
2865 struct nexthop_grp
*n_grp
= NULL
;
2867 n_grp
= (struct nexthop_grp
*)RTA_DATA(tb
[NHA_GROUP
]);
2868 count
= (RTA_PAYLOAD(tb
[NHA_GROUP
]) / sizeof(*n_grp
));
2870 if (!count
|| (count
* sizeof(*n_grp
)) != RTA_PAYLOAD(tb
[NHA_GROUP
])) {
2871 flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE
,
2872 "Invalid nexthop group received from the kernel");
2876 for (int i
= 0; ((i
< count
) && (i
< z_grp_size
)); i
++) {
2877 z_grp
[i
].id
= n_grp
[i
].id
;
2878 z_grp
[i
].weight
= n_grp
[i
].weight
+ 1;
2884 * netlink_nexthop_change() - Read in change about nexthops from the kernel
2886 * @h: Netlink message header
2887 * @ns_id: Namspace id
2888 * @startup: Are we reading under startup conditions?
2890 * Return: Result status
2892 int netlink_nexthop_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2895 /* nexthop group id */
2897 unsigned char family
;
2899 afi_t afi
= AFI_UNSPEC
;
2900 vrf_id_t vrf_id
= VRF_DEFAULT
;
2901 struct interface
*ifp
= NULL
;
2902 struct nhmsg
*nhm
= NULL
;
2903 struct nexthop nh
= {};
2904 struct nh_grp grp
[MULTIPATH_NUM
] = {};
2905 /* Count of nexthops in group array */
2906 uint8_t grp_count
= 0;
2907 struct rtattr
*tb
[NHA_MAX
+ 1] = {};
2909 nhm
= NLMSG_DATA(h
);
2914 if (startup
&& h
->nlmsg_type
!= RTM_NEWNEXTHOP
)
2917 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct nhmsg
));
2920 "%s: Message received from netlink is of a broken size %d %zu",
2921 __func__
, h
->nlmsg_len
,
2922 (size_t)NLMSG_LENGTH(sizeof(struct nhmsg
)));
2926 netlink_parse_rtattr_flags(tb
, NHA_MAX
, RTM_NHA(nhm
), len
,
2932 EC_ZEBRA_BAD_NHG_MESSAGE
,
2933 "Nexthop group without an ID received from the kernel");
2937 /* We use the ID key'd nhg table for kernel updates */
2938 id
= *((uint32_t *)RTA_DATA(tb
[NHA_ID
]));
2940 if (zebra_evpn_mh_is_fdb_nh(id
)) {
2941 /* If this is a L2 NH just ignore it */
2942 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
2943 zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x",
2949 family
= nhm
->nh_family
;
2950 afi
= family2afi(family
);
2952 type
= proto2zebra(nhm
->nh_protocol
, 0, true);
2954 if (IS_ZEBRA_DEBUG_KERNEL
)
2955 zlog_debug("%s ID (%u) %s NS %u",
2956 nl_msg_type_to_str(h
->nlmsg_type
), id
,
2957 nl_family_to_str(family
), ns_id
);
2960 if (h
->nlmsg_type
== RTM_NEWNEXTHOP
) {
2961 if (tb
[NHA_GROUP
]) {
2963 * If this is a group message its only going to have
2964 * an array of nexthop IDs associated with it
2966 grp_count
= netlink_nexthop_process_group(
2967 tb
, grp
, array_size(grp
));
2969 if (tb
[NHA_BLACKHOLE
]) {
2971 * This nexthop is just for blackhole-ing
2972 * traffic, it should not have an OIF, GATEWAY,
2975 nh
.type
= NEXTHOP_TYPE_BLACKHOLE
;
2976 nh
.bh_type
= BLACKHOLE_UNSPEC
;
2977 } else if (tb
[NHA_OIF
])
2979 * This is a true new nexthop, so we need
2980 * to parse the gateway and device info
2982 nh
= netlink_nexthop_process_nh(tb
, family
,
2987 EC_ZEBRA_BAD_NHG_MESSAGE
,
2988 "Invalid Nexthop message received from the kernel with ID (%u)",
2992 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ACTIVE
);
2993 if (nhm
->nh_flags
& RTNH_F_ONLINK
)
2994 SET_FLAG(nh
.flags
, NEXTHOP_FLAG_ONLINK
);
2998 if (zebra_nhg_kernel_find(id
, &nh
, grp
, grp_count
, vrf_id
, afi
,
3002 } else if (h
->nlmsg_type
== RTM_DELNEXTHOP
)
3003 zebra_nhg_kernel_del(id
, vrf_id
);
3009 * netlink_request_nexthop() - Request nextop information from the kernel
3010 * @zns: Zebra namespace
3011 * @family: AF_* netlink family
3012 * @type: RTM_* route type
3014 * Return: Result status
3016 static int netlink_request_nexthop(struct zebra_ns
*zns
, int family
, int type
)
3023 /* Form the request, specifying filter (rtattr) if needed. */
3024 memset(&req
, 0, sizeof(req
));
3025 req
.n
.nlmsg_type
= type
;
3026 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3027 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
3028 req
.nhm
.nh_family
= family
;
3030 return netlink_request(&zns
->netlink_cmd
, &req
);
3035 * netlink_nexthop_read() - Nexthop read function using netlink interface
3037 * @zns: Zebra name space
3039 * Return: Result status
3040 * Only called at bootstrap time.
3042 int netlink_nexthop_read(struct zebra_ns
*zns
)
3045 struct zebra_dplane_info dp_info
;
3047 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3049 /* Get nexthop objects */
3050 ret
= netlink_request_nexthop(zns
, AF_UNSPEC
, RTM_GETNEXTHOP
);
3053 ret
= netlink_parse_info(netlink_nexthop_change
, &zns
->netlink_cmd
,
3057 /* If we succesfully read in nexthop objects,
3058 * this kernel must support them.
3061 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_NHG
)
3062 zlog_debug("Nexthop objects %ssupported on this kernel",
3063 supports_nh
? "" : "not ");
3065 zebra_router_set_supports_nhgs(supports_nh
);
3071 int kernel_neigh_update(int add
, int ifindex
, void *addr
, char *lla
, int llalen
,
3072 ns_id_t ns_id
, uint8_t family
, bool permanent
)
3074 return netlink_neigh_update(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, ifindex
,
3075 addr
, lla
, llalen
, ns_id
, family
, permanent
,
3080 * netlink_neigh_update_msg_encode() - Common helper api for encoding
3081 * evpn neighbor update as netlink messages using dataplane context object.
3082 * Here, a neighbor refers to a bridge forwarding database entry for
3083 * either unicast forwarding or head-end replication or an IP neighbor
3085 * @ctx: Dataplane context
3086 * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH)
3087 * @lla: A pointer to neighbor cache link layer address
3088 * @llalen: Length of the pointer to neighbor cache link layer
3090 * @ip: A neighbor cache n/w layer destination address
3091 * In the case of bridge FDB, this represnts the remote
3093 * @replace_obj: Whether NEW request should replace existing object or
3094 * add to the end of the list
3095 * @family: AF_* netlink family
3096 * @type: RTN_* route type
3097 * @flags: NTF_* flags
3098 * @state: NUD_* states
3099 * @data: data buffer pointer
3100 * @datalen: total amount of data buffer space
3101 * @protocol: protocol information
3103 * Return: 0 when the msg doesn't fit entirely in the buffer
3104 * otherwise the number of bytes written to buf.
3106 static ssize_t
netlink_neigh_update_msg_encode(
3107 const struct zebra_dplane_ctx
*ctx
, int cmd
, const void *lla
,
3108 int llalen
, const struct ipaddr
*ip
, bool replace_obj
, uint8_t family
,
3109 uint8_t type
, uint8_t flags
, uint16_t state
, uint32_t nhg_id
, bool nfy
,
3110 uint8_t nfy_flags
, bool ext
, uint32_t ext_flags
, void *data
,
3111 size_t datalen
, uint8_t protocol
)
3119 enum dplane_op_e op
;
3121 if (datalen
< sizeof(*req
))
3123 memset(req
, 0, sizeof(*req
));
3125 op
= dplane_ctx_get_op(ctx
);
3127 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3128 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
3129 if (cmd
== RTM_NEWNEIGH
)
3130 req
->n
.nlmsg_flags
|=
3132 | (replace_obj
? NLM_F_REPLACE
: NLM_F_APPEND
);
3133 req
->n
.nlmsg_type
= cmd
;
3134 req
->ndm
.ndm_family
= family
;
3135 req
->ndm
.ndm_type
= type
;
3136 req
->ndm
.ndm_state
= state
;
3137 req
->ndm
.ndm_flags
= flags
;
3138 req
->ndm
.ndm_ifindex
= dplane_ctx_get_ifindex(ctx
);
3140 if (!nl_attr_put(&req
->n
, datalen
, NDA_PROTOCOL
, &protocol
,
3145 if (!nl_attr_put(&req
->n
, datalen
, NDA_LLADDR
, lla
, llalen
))
3150 struct rtattr
*nest
;
3152 nest
= nl_attr_nest(&req
->n
, datalen
,
3153 NDA_FDB_EXT_ATTRS
| NLA_F_NESTED
);
3157 if (!nl_attr_put(&req
->n
, datalen
, NFEA_ACTIVITY_NOTIFY
,
3158 &nfy_flags
, sizeof(nfy_flags
)))
3160 if (!nl_attr_put(&req
->n
, datalen
, NFEA_DONT_REFRESH
, NULL
, 0))
3163 nl_attr_nest_end(&req
->n
, nest
);
3168 if (!nl_attr_put(&req
->n
, datalen
, NDA_EXT_FLAGS
, &ext_flags
,
3174 if (!nl_attr_put32(&req
->n
, datalen
, NDA_NH_ID
, nhg_id
))
3178 IS_IPADDR_V4(ip
) ? IPV4_MAX_BYTELEN
: IPV6_MAX_BYTELEN
;
3179 if (!nl_attr_put(&req
->n
, datalen
, NDA_DST
, &ip
->ip
.addr
,
3184 if (op
== DPLANE_OP_MAC_INSTALL
|| op
== DPLANE_OP_MAC_DELETE
) {
3185 vlanid_t vid
= dplane_ctx_mac_get_vlan(ctx
);
3188 if (!nl_attr_put16(&req
->n
, datalen
, NDA_VLAN
, vid
))
3192 if (!nl_attr_put32(&req
->n
, datalen
, NDA_MASTER
,
3193 dplane_ctx_mac_get_br_ifindex(ctx
)))
3197 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
3201 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
3202 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
3205 netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx
*ctx
, int cmd
,
3206 void *buf
, size_t buflen
)
3208 struct ethaddr dst_mac
= {.octet
= {0}};
3209 int proto
= RTPROT_ZEBRA
;
3211 if (dplane_ctx_get_type(ctx
) != 0)
3212 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3214 return netlink_neigh_update_msg_encode(
3215 ctx
, cmd
, (const void *)&dst_mac
, ETH_ALEN
,
3216 dplane_ctx_neigh_get_ipaddr(ctx
), false, PF_BRIDGE
, 0, NTF_SELF
,
3217 (NUD_NOARP
| NUD_PERMANENT
), 0 /*nhg*/, false /*nfy*/,
3218 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf
, buflen
,
3223 #define NDA_RTA(r) \
3224 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
3227 static int netlink_macfdb_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3230 struct interface
*ifp
;
3231 struct zebra_if
*zif
;
3232 struct rtattr
*tb
[NDA_MAX
+ 1];
3233 struct interface
*br_if
;
3236 struct in_addr vtep_ip
;
3237 int vid_present
= 0, dst_present
= 0;
3241 bool local_inactive
= false;
3242 bool dp_static
= false;
3243 uint32_t nhg_id
= 0;
3245 ndm
= NLMSG_DATA(h
);
3247 /* We only process macfdb notifications if EVPN is enabled */
3248 if (!is_evpn_enabled())
3251 /* Parse attributes and extract fields of interest. Do basic
3252 * validation of the fields.
3254 netlink_parse_rtattr_flags(tb
, NDA_MAX
, NDA_RTA(ndm
), len
,
3257 if (!tb
[NDA_LLADDR
]) {
3258 if (IS_ZEBRA_DEBUG_KERNEL
)
3259 zlog_debug("%s AF_BRIDGE IF %u - no LLADDR",
3260 nl_msg_type_to_str(h
->nlmsg_type
),
3265 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3266 if (IS_ZEBRA_DEBUG_KERNEL
)
3268 "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu",
3269 nl_msg_type_to_str(h
->nlmsg_type
), ndm
->ndm_ifindex
,
3270 (unsigned long)RTA_PAYLOAD(tb
[NDA_LLADDR
]));
3274 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3276 if ((NDA_VLAN
<= NDA_MAX
) && tb
[NDA_VLAN
]) {
3278 vid
= *(uint16_t *)RTA_DATA(tb
[NDA_VLAN
]);
3279 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3283 /* TODO: Only IPv4 supported now. */
3285 memcpy(&vtep_ip
.s_addr
, RTA_DATA(tb
[NDA_DST
]),
3287 snprintfrr(dst_buf
, sizeof(dst_buf
), " dst %pI4",
3292 nhg_id
= *(uint32_t *)RTA_DATA(tb
[NDA_NH_ID
]);
3294 if (ndm
->ndm_state
& NUD_STALE
)
3295 local_inactive
= true;
3297 if (tb
[NDA_FDB_EXT_ATTRS
]) {
3298 struct rtattr
*attr
= tb
[NDA_FDB_EXT_ATTRS
];
3299 struct rtattr
*nfea_tb
[NFEA_MAX
+ 1] = {0};
3301 netlink_parse_rtattr_nested(nfea_tb
, NFEA_MAX
, attr
);
3302 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
3305 nfy_flags
= *(uint8_t *)RTA_DATA(
3306 nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
3307 if (nfy_flags
& FDB_NOTIFY_BIT
)
3309 if (nfy_flags
& FDB_NOTIFY_INACTIVE_BIT
)
3310 local_inactive
= true;
3314 if (IS_ZEBRA_DEBUG_KERNEL
)
3315 zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d",
3316 nl_msg_type_to_str(h
->nlmsg_type
),
3317 ndm
->ndm_ifindex
, vid_present
? vid_buf
: "",
3318 ndm
->ndm_state
, ndm
->ndm_flags
, &mac
,
3319 dst_present
? dst_buf
: "", nhg_id
);
3321 /* The interface should exist. */
3322 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3324 if (!ifp
|| !ifp
->info
)
3327 /* The interface should be something we're interested in. */
3328 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
3331 zif
= (struct zebra_if
*)ifp
->info
;
3332 if ((br_if
= zif
->brslave_info
.br_if
) == NULL
) {
3333 if (IS_ZEBRA_DEBUG_KERNEL
)
3335 "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master",
3336 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
3338 zif
->brslave_info
.bridge_ifindex
);
3342 sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
3344 if (filter_vlan
&& vid
!= filter_vlan
) {
3345 if (IS_ZEBRA_DEBUG_KERNEL
)
3346 zlog_debug(" Filtered due to filter vlan: %d",
3351 /* If add or update, do accordingly if learnt on a "local" interface; if
3352 * the notification is over VxLAN, this has to be related to
3354 * so perform an implicit delete of any local entry (if it exists).
3356 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3357 /* Drop "permanent" entries. */
3358 if (ndm
->ndm_state
& NUD_PERMANENT
) {
3359 if (IS_ZEBRA_DEBUG_KERNEL
)
3361 " Dropping entry because of NUD_PERMANENT");
3365 if (IS_ZEBRA_IF_VXLAN(ifp
))
3366 return zebra_vxlan_dp_network_mac_add(
3367 ifp
, br_if
, &mac
, vid
, nhg_id
, sticky
,
3368 !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
));
3370 return zebra_vxlan_local_mac_add_update(ifp
, br_if
, &mac
, vid
,
3371 sticky
, local_inactive
, dp_static
);
3374 /* This is a delete notification.
3375 * Ignore the notification with IP dest as it may just signify that the
3376 * MAC has moved from remote to local. The exception is the special
3377 * all-zeros MAC that represents the BUM flooding entry; we may have
3378 * to readd it. Otherwise,
3379 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
3380 * 2. For a MAC over "local" interface, delete the mac
3381 * Note: We will get notifications from both bridge driver and VxLAN
3388 u_char zero_mac
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
3390 if (!memcmp(zero_mac
, mac
.octet
, ETH_ALEN
))
3391 return zebra_vxlan_check_readd_vtep(ifp
, vtep_ip
);
3395 if (IS_ZEBRA_IF_VXLAN(ifp
))
3396 return zebra_vxlan_dp_network_mac_del(ifp
, br_if
, &mac
, vid
);
3398 return zebra_vxlan_local_mac_del(ifp
, br_if
, &mac
, vid
);
3401 static int netlink_macfdb_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3406 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3409 /* Length validity. */
3410 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3414 /* We are interested only in AF_BRIDGE notifications. */
3415 ndm
= NLMSG_DATA(h
);
3416 if (ndm
->ndm_family
!= AF_BRIDGE
)
3419 return netlink_macfdb_change(h
, len
, ns_id
);
3422 /* Request for MAC FDB information from the kernel */
3423 static int netlink_request_macs(struct nlsock
*netlink_cmd
, int family
,
3424 int type
, ifindex_t master_ifindex
)
3428 struct ifinfomsg ifm
;
3432 /* Form the request, specifying filter (rtattr) if needed. */
3433 memset(&req
, 0, sizeof(req
));
3434 req
.n
.nlmsg_type
= type
;
3435 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3436 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
3437 req
.ifm
.ifi_family
= family
;
3439 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master_ifindex
);
3441 return netlink_request(netlink_cmd
, &req
);
3445 * MAC forwarding database read using netlink interface. This is invoked
3448 int netlink_macfdb_read(struct zebra_ns
*zns
)
3451 struct zebra_dplane_info dp_info
;
3453 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3455 /* Get bridge FDB table. */
3456 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3460 /* We are reading entire table. */
3462 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3469 * MAC forwarding database read using netlink interface. This is for a
3470 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
3472 int netlink_macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
3473 struct interface
*br_if
)
3475 struct zebra_if
*br_zif
;
3476 struct zebra_if
*zif
;
3477 struct zebra_l2info_vxlan
*vxl
;
3478 struct zebra_dplane_info dp_info
;
3481 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3483 /* Save VLAN we're filtering on, if needed. */
3484 br_zif
= (struct zebra_if
*)br_if
->info
;
3485 zif
= (struct zebra_if
*)ifp
->info
;
3486 vxl
= &zif
->l2info
.vxl
;
3487 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
))
3488 filter_vlan
= vxl
->access_vlan
;
3490 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3492 ret
= netlink_request_macs(&zns
->netlink_cmd
, AF_BRIDGE
, RTM_GETNEIGH
,
3496 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3497 &dp_info
, 0, false);
3499 /* Reset VLAN filter. */
3505 /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
3506 static int netlink_request_specific_mac_in_bridge(struct zebra_ns
*zns
,
3507 int family
, int type
,
3508 struct interface
*br_if
,
3509 const struct ethaddr
*mac
,
3517 struct zebra_if
*br_zif
;
3519 memset(&req
, 0, sizeof(req
));
3520 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3521 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
3522 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
3523 req
.ndm
.ndm_family
= family
; /* AF_BRIDGE */
3524 /* req.ndm.ndm_state = NUD_REACHABLE; */
3526 nl_attr_put(&req
.n
, sizeof(req
), NDA_LLADDR
, mac
, 6);
3528 br_zif
= (struct zebra_if
*)br_if
->info
;
3529 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif
) && vid
> 0)
3530 nl_attr_put16(&req
.n
, sizeof(req
), NDA_VLAN
, vid
);
3532 nl_attr_put32(&req
.n
, sizeof(req
), NDA_MASTER
, br_if
->ifindex
);
3534 if (IS_ZEBRA_DEBUG_KERNEL
)
3536 "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u",
3537 __func__
, nl_family_to_str(req
.ndm
.ndm_family
),
3538 br_if
->name
, br_if
->ifindex
, br_if
->vrf
->name
,
3539 br_if
->vrf
->vrf_id
, mac
, vid
);
3541 return netlink_request(&zns
->netlink_cmd
, &req
);
3544 int netlink_macfdb_read_specific_mac(struct zebra_ns
*zns
,
3545 struct interface
*br_if
,
3546 const struct ethaddr
*mac
, vlanid_t vid
)
3549 struct zebra_dplane_info dp_info
;
3551 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3553 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
3555 ret
= netlink_request_specific_mac_in_bridge(zns
, AF_BRIDGE
,
3561 ret
= netlink_parse_info(netlink_macfdb_table
, &zns
->netlink_cmd
,
3562 &dp_info
, 1, false);
3568 * Netlink-specific handler for MAC updates using dataplane context object.
3570 ssize_t
netlink_macfdb_update_ctx(struct zebra_dplane_ctx
*ctx
, void *data
,
3573 struct ipaddr vtep_ip
;
3580 uint32_t update_flags
;
3582 uint8_t nfy_flags
= 0;
3583 int proto
= RTPROT_ZEBRA
;
3585 if (dplane_ctx_get_type(ctx
) != 0)
3586 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
3588 cmd
= dplane_ctx_get_op(ctx
) == DPLANE_OP_MAC_INSTALL
3589 ? RTM_NEWNEIGH
: RTM_DELNEIGH
;
3592 state
= NUD_REACHABLE
;
3594 update_flags
= dplane_ctx_mac_get_update_flags(ctx
);
3595 if (update_flags
& DPLANE_MAC_REMOTE
) {
3597 if (dplane_ctx_mac_is_sticky(ctx
)) {
3598 /* NUD_NOARP prevents the entry from expiring */
3600 /* sticky the entry from moving */
3601 flags
|= NTF_STICKY
;
3603 flags
|= NTF_EXT_LEARNED
;
3605 /* if it was static-local previously we need to clear the
3606 * notify flags on replace with remote
3608 if (update_flags
& DPLANE_MAC_WAS_STATIC
)
3612 if (update_flags
& DPLANE_MAC_SET_STATIC
) {
3613 nfy_flags
|= FDB_NOTIFY_BIT
;
3617 if (update_flags
& DPLANE_MAC_SET_INACTIVE
)
3618 nfy_flags
|= FDB_NOTIFY_INACTIVE_BIT
;
3623 nhg_id
= dplane_ctx_mac_get_nhg_id(ctx
);
3624 vtep_ip
.ipaddr_v4
= *(dplane_ctx_mac_get_vtep_ip(ctx
));
3625 SET_IPADDR_V4(&vtep_ip
);
3627 if (IS_ZEBRA_DEBUG_KERNEL
) {
3629 const struct ethaddr
*mac
= dplane_ctx_mac_get_addr(ctx
);
3631 vid
= dplane_ctx_mac_get_vlan(ctx
);
3633 snprintf(vid_buf
, sizeof(vid_buf
), " VLAN %u", vid
);
3638 "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s",
3639 nl_msg_type_to_str(cmd
), nl_family_to_str(AF_BRIDGE
),
3640 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
3641 vid_buf
, dplane_ctx_mac_is_sticky(ctx
) ? "sticky " : "",
3642 mac
, &vtep_ip
, nhg_id
,
3643 (update_flags
& DPLANE_MAC_REMOTE
) ? " rem" : "",
3644 (update_flags
& DPLANE_MAC_WAS_STATIC
) ? " clr_sync"
3646 (update_flags
& DPLANE_MAC_SET_STATIC
) ? " static" : "",
3647 (update_flags
& DPLANE_MAC_SET_INACTIVE
) ? " inactive"
3652 total
= netlink_neigh_update_msg_encode(
3653 ctx
, cmd
, (const void *)dplane_ctx_mac_get_addr(ctx
), ETH_ALEN
,
3654 &vtep_ip
, true, AF_BRIDGE
, 0, flags
, state
, nhg_id
, nfy
,
3655 nfy_flags
, false /*ext*/, 0 /*ext_flags*/, data
, datalen
,
3662 * In the event the kernel deletes ipv4 link-local neighbor entries created for
3663 * 5549 support, re-install them.
3665 static void netlink_handle_5549(struct ndmsg
*ndm
, struct zebra_if
*zif
,
3666 struct interface
*ifp
, struct ipaddr
*ip
,
3669 if (ndm
->ndm_family
!= AF_INET
)
3672 if (!zif
->v6_2_v4_ll_neigh_entry
)
3675 if (ipv4_ll
.s_addr
!= ip
->ip
._v4_addr
.s_addr
)
3678 if (handle_failed
&& ndm
->ndm_state
& NUD_FAILED
) {
3679 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
3684 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, &zif
->v6_2_v4_ll_addr6
, true);
3688 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
3690 #define NUD_LOCAL_ACTIVE \
3691 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE)
3693 static int netlink_nbr_entry_state_to_zclient(int nbr_state
)
3695 /* an exact match is done between
3696 * - netlink neighbor state values: NDM_XXX (see in linux/neighbour.h)
3697 * - zclient neighbor state values: ZEBRA_NEIGH_STATE_XXX
3698 * (see in lib/zclient.h)
3702 static int netlink_ipneigh_change(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
)
3705 struct interface
*ifp
;
3706 struct zebra_if
*zif
;
3707 struct rtattr
*tb
[NDA_MAX
+ 1];
3708 struct interface
*link_if
;
3711 char buf
[ETHER_ADDR_STRLEN
];
3712 int mac_present
= 0;
3715 bool local_inactive
;
3716 uint32_t ext_flags
= 0;
3717 bool dp_static
= false;
3721 ndm
= NLMSG_DATA(h
);
3723 /* The interface should exist. */
3724 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3726 if (!ifp
|| !ifp
->info
)
3729 zif
= (struct zebra_if
*)ifp
->info
;
3731 /* Parse attributes and extract fields of interest. */
3732 netlink_parse_rtattr(tb
, NDA_MAX
, NDA_RTA(ndm
), len
);
3735 zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST",
3736 nl_msg_type_to_str(h
->nlmsg_type
),
3737 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3738 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
3742 memset(&ip
, 0, sizeof(struct ipaddr
));
3743 ip
.ipa_type
= (ndm
->ndm_family
== AF_INET
) ? IPADDR_V4
: IPADDR_V6
;
3744 memcpy(&ip
.ip
.addr
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
3746 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
3747 if (h
->nlmsg_type
== RTM_DELNEIGH
&& (ndm
->ndm_state
& NUD_PERMANENT
)) {
3748 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, false);
3749 if (IS_ZEBRA_DEBUG_KERNEL
)
3751 " Neighbor Entry Received is a 5549 entry, finished");
3755 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
3756 if (h
->nlmsg_type
== RTM_NEWNEIGH
&& !(ndm
->ndm_state
& NUD_VALID
))
3757 netlink_handle_5549(ndm
, zif
, ifp
, &ip
, true);
3759 /* we send link layer information to client:
3760 * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH
3761 * - struct ipaddr ( for DEL and GET)
3762 * - struct ethaddr mac; (for NEW)
3764 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
3765 cmd
= ZEBRA_NHRP_NEIGH_ADDED
;
3766 else if (h
->nlmsg_type
== RTM_GETNEIGH
)
3767 cmd
= ZEBRA_NHRP_NEIGH_GET
;
3768 else if (h
->nlmsg_type
== RTM_DELNEIGH
)
3769 cmd
= ZEBRA_NHRP_NEIGH_REMOVED
;
3771 zlog_debug("%s(): unknown nlmsg type %u", __func__
,
3775 if (tb
[NDA_LLADDR
]) {
3776 /* copy LLADDR information */
3777 l2_len
= RTA_PAYLOAD(tb
[NDA_LLADDR
]);
3779 if (l2_len
== IPV4_MAX_BYTELEN
|| l2_len
== 0) {
3780 union sockunion link_layer_ipv4
;
3783 sockunion_family(&link_layer_ipv4
) = AF_INET
;
3784 memcpy((void *)sockunion_get_addr(&link_layer_ipv4
),
3785 RTA_DATA(tb
[NDA_LLADDR
]), l2_len
);
3787 sockunion_family(&link_layer_ipv4
) = AF_UNSPEC
;
3788 zsend_nhrp_neighbor_notify(
3790 netlink_nbr_entry_state_to_zclient(ndm
->ndm_state
),
3794 if (h
->nlmsg_type
== RTM_GETNEIGH
)
3797 /* The neighbor is present on an SVI. From this, we locate the
3799 * bridge because we're only interested in neighbors on a VxLAN bridge.
3800 * The bridge is located based on the nature of the SVI:
3801 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
3803 * and is linked to the bridge
3804 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
3808 if (IS_ZEBRA_IF_VLAN(ifp
)) {
3809 link_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
),
3813 } else if (IS_ZEBRA_IF_BRIDGE(ifp
))
3816 if (IS_ZEBRA_DEBUG_KERNEL
)
3818 " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
3822 memset(&mac
, 0, sizeof(struct ethaddr
));
3823 if (h
->nlmsg_type
== RTM_NEWNEIGH
) {
3824 if (tb
[NDA_LLADDR
]) {
3825 if (RTA_PAYLOAD(tb
[NDA_LLADDR
]) != ETH_ALEN
) {
3826 if (IS_ZEBRA_DEBUG_KERNEL
)
3828 "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu",
3833 ifp
->name
, ndm
->ndm_ifindex
,
3836 (unsigned long)RTA_PAYLOAD(
3842 memcpy(&mac
, RTA_DATA(tb
[NDA_LLADDR
]), ETH_ALEN
);
3845 is_ext
= !!(ndm
->ndm_flags
& NTF_EXT_LEARNED
);
3846 is_router
= !!(ndm
->ndm_flags
& NTF_ROUTER
);
3848 if (tb
[NDA_EXT_FLAGS
]) {
3849 ext_flags
= *(uint32_t *)RTA_DATA(tb
[NDA_EXT_FLAGS
]);
3850 if (ext_flags
& NTF_E_MH_PEER_SYNC
)
3854 if (IS_ZEBRA_DEBUG_KERNEL
)
3856 "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x",
3857 nl_msg_type_to_str(h
->nlmsg_type
),
3858 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3859 ndm
->ndm_ifindex
, ifp
->vrf
->name
,
3860 ifp
->vrf
->vrf_id
, &ip
,
3862 ? prefix_mac2str(&mac
, buf
, sizeof(buf
))
3864 ndm
->ndm_state
, ndm
->ndm_flags
, ext_flags
);
3866 /* If the neighbor state is valid for use, process as an add or
3868 * else process as a delete. Note that the delete handling may
3870 * in re-adding the neighbor if it is a valid "remote" neighbor.
3872 if (ndm
->ndm_state
& NUD_VALID
) {
3873 if (zebra_evpn_mh_do_adv_reachable_neigh_only())
3875 !(ndm
->ndm_state
& NUD_LOCAL_ACTIVE
);
3877 /* If EVPN-MH is not enabled we treat STALE
3878 * neighbors as locally-active and advertise
3881 local_inactive
= false;
3883 return zebra_vxlan_handle_kernel_neigh_update(
3884 ifp
, link_if
, &ip
, &mac
, ndm
->ndm_state
, is_ext
,
3885 is_router
, local_inactive
, dp_static
);
3888 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3891 if (IS_ZEBRA_DEBUG_KERNEL
)
3892 zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA",
3893 nl_msg_type_to_str(h
->nlmsg_type
),
3894 nl_family_to_str(ndm
->ndm_family
), ifp
->name
,
3895 ndm
->ndm_ifindex
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
,
3898 /* Process the delete - it may result in re-adding the neighbor if it is
3899 * a valid "remote" neighbor.
3901 return zebra_vxlan_handle_kernel_neigh_del(ifp
, link_if
, &ip
);
3904 static int netlink_neigh_table(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
3909 if (h
->nlmsg_type
!= RTM_NEWNEIGH
)
3912 /* Length validity. */
3913 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
3917 /* We are interested only in AF_INET or AF_INET6 notifications. */
3918 ndm
= NLMSG_DATA(h
);
3919 if (ndm
->ndm_family
!= AF_INET
&& ndm
->ndm_family
!= AF_INET6
)
3922 return netlink_neigh_change(h
, len
);
3925 /* Request for IP neighbor information from the kernel */
3926 static int netlink_request_neigh(struct nlsock
*netlink_cmd
, int family
,
3927 int type
, ifindex_t ifindex
)
3935 /* Form the request, specifying filter (rtattr) if needed. */
3936 memset(&req
, 0, sizeof(req
));
3937 req
.n
.nlmsg_type
= type
;
3938 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
3939 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
3940 req
.ndm
.ndm_family
= family
;
3942 nl_attr_put32(&req
.n
, sizeof(req
), NDA_IFINDEX
, ifindex
);
3944 return netlink_request(netlink_cmd
, &req
);
3948 * IP Neighbor table read using netlink interface. This is invoked
3951 int netlink_neigh_read(struct zebra_ns
*zns
)
3954 struct zebra_dplane_info dp_info
;
3956 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3958 /* Get IP neighbor table. */
3959 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3963 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3970 * IP Neighbor table read using netlink interface. This is for a specific
3973 int netlink_neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
3976 struct zebra_dplane_info dp_info
;
3978 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
3980 ret
= netlink_request_neigh(&zns
->netlink_cmd
, AF_UNSPEC
, RTM_GETNEIGH
,
3984 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
3985 &dp_info
, 0, false);
3991 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
3992 * read using netlink interface.
3994 static int netlink_request_specific_neigh_in_vlan(struct zebra_ns
*zns
,
3996 const struct ipaddr
*ip
,
4006 /* Form the request, specifying filter (rtattr) if needed. */
4007 memset(&req
, 0, sizeof(req
));
4008 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
4009 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4010 req
.n
.nlmsg_type
= type
; /* RTM_GETNEIGH */
4011 req
.ndm
.ndm_ifindex
= ifindex
;
4013 if (IS_IPADDR_V4(ip
)) {
4014 ipa_len
= IPV4_MAX_BYTELEN
;
4015 req
.ndm
.ndm_family
= AF_INET
;
4018 ipa_len
= IPV6_MAX_BYTELEN
;
4019 req
.ndm
.ndm_family
= AF_INET6
;
4022 nl_attr_put(&req
.n
, sizeof(req
), NDA_DST
, &ip
->ip
.addr
, ipa_len
);
4024 if (IS_ZEBRA_DEBUG_KERNEL
)
4025 zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x",
4026 __func__
, nl_msg_type_to_str(type
),
4027 nl_family_to_str(req
.ndm
.ndm_family
), ifindex
, ip
,
4030 return netlink_request(&zns
->netlink_cmd
, &req
);
4033 int netlink_neigh_read_specific_ip(const struct ipaddr
*ip
,
4034 struct interface
*vlan_if
)
4037 struct zebra_ns
*zns
;
4038 struct zebra_vrf
*zvrf
= vlan_if
->vrf
->info
;
4039 struct zebra_dplane_info dp_info
;
4043 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
4045 if (IS_ZEBRA_DEBUG_KERNEL
)
4046 zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)",
4047 __func__
, vlan_if
->name
, vlan_if
->ifindex
, ip
,
4048 vlan_if
->vrf
->name
, vlan_if
->vrf
->vrf_id
);
4050 ret
= netlink_request_specific_neigh_in_vlan(zns
, RTM_GETNEIGH
, ip
,
4055 ret
= netlink_parse_info(netlink_neigh_table
, &zns
->netlink_cmd
,
4056 &dp_info
, 1, false);
4061 int netlink_neigh_change(struct nlmsghdr
*h
, ns_id_t ns_id
)
4066 if (!(h
->nlmsg_type
== RTM_NEWNEIGH
|| h
->nlmsg_type
== RTM_DELNEIGH
4067 || h
->nlmsg_type
== RTM_GETNEIGH
))
4070 /* Length validity. */
4071 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ndmsg
));
4074 "%s: Message received from netlink is of a broken size %d %zu",
4075 __func__
, h
->nlmsg_len
,
4076 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg
)));
4080 /* Is this a notification for the MAC FDB or IP neighbor table? */
4081 ndm
= NLMSG_DATA(h
);
4082 if (ndm
->ndm_family
== AF_BRIDGE
)
4083 return netlink_macfdb_change(h
, len
, ns_id
);
4085 if (ndm
->ndm_type
!= RTN_UNICAST
)
4088 if (ndm
->ndm_family
== AF_INET
|| ndm
->ndm_family
== AF_INET6
)
4089 return netlink_ipneigh_change(h
, len
, ns_id
);
4092 EC_ZEBRA_UNKNOWN_FAMILY
,
4093 "Invalid address family: %u received from kernel neighbor change: %s",
4094 ndm
->ndm_family
, nl_msg_type_to_str(h
->nlmsg_type
));
4102 * Utility neighbor-update function, using info from dplane context.
4104 static ssize_t
netlink_neigh_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4105 int cmd
, void *buf
, size_t buflen
)
4107 const struct ipaddr
*ip
;
4108 const struct ethaddr
*mac
= NULL
;
4109 const struct ipaddr
*link_ip
= NULL
;
4110 const void *link_ptr
= NULL
;
4111 char buf2
[ETHER_ADDR_STRLEN
];
4117 uint32_t update_flags
;
4118 uint32_t ext_flags
= 0;
4120 int proto
= RTPROT_ZEBRA
;
4122 if (dplane_ctx_get_type(ctx
) != 0)
4123 proto
= zebra2proto(dplane_ctx_get_type(ctx
));
4125 ip
= dplane_ctx_neigh_get_ipaddr(ctx
);
4127 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_INSTALL
4128 || dplane_ctx_get_op(ctx
) == DPLANE_OP_NEIGH_IP_DELETE
) {
4129 link_ip
= dplane_ctx_neigh_get_link_ip(ctx
);
4130 llalen
= IPADDRSZ(link_ip
);
4131 link_ptr
= (const void *)&(link_ip
->ip
.addr
);
4132 ipaddr2str(link_ip
, buf2
, sizeof(buf2
));
4134 mac
= dplane_ctx_neigh_get_mac(ctx
);
4136 link_ptr
= (const void *)mac
;
4137 if (is_zero_mac(mac
))
4140 prefix_mac2str(mac
, buf2
, sizeof(buf2
));
4142 snprintf(buf2
, sizeof(buf2
), "null");
4144 update_flags
= dplane_ctx_neigh_get_update_flags(ctx
);
4145 flags
= neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx
));
4146 state
= neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx
));
4148 family
= IS_IPADDR_V4(ip
) ? AF_INET
: AF_INET6
;
4150 if (update_flags
& DPLANE_NEIGH_REMOTE
) {
4151 flags
|= NTF_EXT_LEARNED
;
4152 /* if it was static-local previously we need to clear the
4153 * ext flags on replace with remote
4155 if (update_flags
& DPLANE_NEIGH_WAS_STATIC
)
4157 } else if (!(update_flags
& DPLANE_NEIGH_NO_EXTENSION
)) {
4160 if (update_flags
& DPLANE_NEIGH_SET_STATIC
)
4161 ext_flags
|= NTF_E_MH_PEER_SYNC
;
4163 if (IS_ZEBRA_DEBUG_KERNEL
)
4165 "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x",
4166 nl_msg_type_to_str(cmd
), nl_family_to_str(family
),
4167 dplane_ctx_get_ifname(ctx
), dplane_ctx_get_ifindex(ctx
),
4168 ip
, link_ip
? "Link " : "MAC ", buf2
, flags
, state
,
4169 ext
? "ext " : "", ext_flags
);
4171 return netlink_neigh_update_msg_encode(
4172 ctx
, cmd
, link_ptr
, llalen
, ip
, true, family
, RTN_UNICAST
,
4173 flags
, state
, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext
,
4174 ext_flags
, buf
, buflen
, proto
);
4177 static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx
*ctx
,
4178 void *data
, size_t datalen
)
4185 struct rtattr
*nest
;
4190 if (datalen
< sizeof(*req
))
4192 memset(req
, 0, sizeof(*req
));
4193 family
= dplane_ctx_neightable_get_family(ctx
);
4194 idx
= dplane_ctx_get_ifindex(ctx
);
4196 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndtmsg
));
4197 req
->n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_REPLACE
;
4198 req
->n
.nlmsg_type
= RTM_SETNEIGHTBL
;
4199 req
->ndtm
.ndtm_family
= family
;
4201 nl_attr_put(&req
->n
, datalen
, NDTA_NAME
,
4202 family
== AF_INET
? "arp_cache" : "ndisc_cache", 10);
4203 nest
= nl_attr_nest(&req
->n
, datalen
, NDTA_PARMS
);
4206 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_IFINDEX
, &idx
, sizeof(idx
)))
4208 val
= dplane_ctx_neightable_get_app_probes(ctx
);
4209 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_APP_PROBES
, &val
, sizeof(val
)))
4211 val
= dplane_ctx_neightable_get_mcast_probes(ctx
);
4212 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_MCAST_PROBES
, &val
,
4215 val
= dplane_ctx_neightable_get_ucast_probes(ctx
);
4216 if (!nl_attr_put(&req
->n
, datalen
, NDTPA_UCAST_PROBES
, &val
,
4219 nl_attr_nest_end(&req
->n
, nest
);
4221 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4224 static ssize_t
netlink_neigh_msg_encoder(struct zebra_dplane_ctx
*ctx
,
4225 void *buf
, size_t buflen
)
4229 switch (dplane_ctx_get_op(ctx
)) {
4230 case DPLANE_OP_NEIGH_INSTALL
:
4231 case DPLANE_OP_NEIGH_UPDATE
:
4232 case DPLANE_OP_NEIGH_DISCOVER
:
4233 case DPLANE_OP_NEIGH_IP_INSTALL
:
4234 ret
= netlink_neigh_update_ctx(ctx
, RTM_NEWNEIGH
, buf
, buflen
);
4236 case DPLANE_OP_NEIGH_DELETE
:
4237 case DPLANE_OP_NEIGH_IP_DELETE
:
4238 ret
= netlink_neigh_update_ctx(ctx
, RTM_DELNEIGH
, buf
, buflen
);
4240 case DPLANE_OP_VTEP_ADD
:
4241 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_NEWNEIGH
, buf
,
4244 case DPLANE_OP_VTEP_DELETE
:
4245 ret
= netlink_vxlan_flood_update_ctx(ctx
, RTM_DELNEIGH
, buf
,
4248 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4249 ret
= netlink_neigh_table_update_ctx(ctx
, buf
, buflen
);
4259 * Update MAC, using dataplane context object.
4262 enum netlink_msg_status
netlink_put_mac_update_msg(struct nl_batch
*bth
,
4263 struct zebra_dplane_ctx
*ctx
)
4265 return netlink_batch_add_msg(bth
, ctx
, netlink_macfdb_update_ctx
,
4269 enum netlink_msg_status
4270 netlink_put_neigh_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
4272 return netlink_batch_add_msg(bth
, ctx
, netlink_neigh_msg_encoder
,
4277 * MPLS label forwarding table change via netlink interface, using dataplane
4278 * context information.
4280 ssize_t
netlink_mpls_multipath_msg_encode(int cmd
, struct zebra_dplane_ctx
*ctx
,
4281 void *buf
, size_t buflen
)
4284 const struct nhlfe_list_head
*head
;
4285 const struct zebra_nhlfe
*nhlfe
;
4286 struct nexthop
*nexthop
= NULL
;
4287 unsigned int nexthop_num
;
4288 const char *routedesc
;
4290 struct prefix p
= {0};
4292 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
4300 if (buflen
< sizeof(*req
))
4303 memset(req
, 0, sizeof(*req
));
4306 * Count # nexthops so we can decide whether to use singlepath
4307 * or multipath case.
4310 head
= dplane_ctx_get_nhlfe_list(ctx
);
4311 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4312 nexthop
= nhlfe
->nexthop
;
4315 if (cmd
== RTM_NEWROUTE
) {
4316 /* Count all selected NHLFEs */
4317 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4318 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
4321 /* Count all installed NHLFEs */
4322 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
4323 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
4328 if ((nexthop_num
== 0) ||
4329 (!dplane_ctx_get_best_nhlfe(ctx
) && (cmd
!= RTM_DELROUTE
)))
4332 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
4333 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
4334 req
->n
.nlmsg_type
= cmd
;
4335 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
4337 req
->r
.rtm_family
= AF_MPLS
;
4338 req
->r
.rtm_table
= RT_TABLE_MAIN
;
4339 req
->r
.rtm_dst_len
= MPLS_LABEL_LEN_BITS
;
4340 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
4341 req
->r
.rtm_type
= RTN_UNICAST
;
4343 if (cmd
== RTM_NEWROUTE
) {
4344 /* We do a replace to handle update. */
4345 req
->n
.nlmsg_flags
|= NLM_F_REPLACE
;
4347 /* set the protocol value if installing */
4348 route_type
= re_type_from_lsp_type(
4349 dplane_ctx_get_best_nhlfe(ctx
)->type
);
4350 req
->r
.rtm_protocol
= zebra2proto(route_type
);
4353 /* Fill destination */
4354 lse
= mpls_lse_encode(dplane_ctx_get_in_label(ctx
), 0, 0, 1);
4355 if (!nl_attr_put(&req
->n
, buflen
, RTA_DST
, &lse
, sizeof(mpls_lse_t
)))
4358 /* Fill nexthops (paths) based on single-path or multipath. The paths
4359 * chosen depend on the operation.
4361 if (nexthop_num
== 1) {
4362 routedesc
= "single-path";
4363 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4367 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4368 nexthop
= nhlfe
->nexthop
;
4372 if ((cmd
== RTM_NEWROUTE
4373 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4374 && CHECK_FLAG(nexthop
->flags
,
4375 NEXTHOP_FLAG_ACTIVE
)))
4376 || (cmd
== RTM_DELROUTE
4377 && (CHECK_FLAG(nhlfe
->flags
,
4378 NHLFE_FLAG_INSTALLED
)
4379 && CHECK_FLAG(nexthop
->flags
,
4380 NEXTHOP_FLAG_FIB
)))) {
4381 /* Add the gateway */
4382 if (!_netlink_mpls_build_singlepath(
4383 &p
, routedesc
, nhlfe
, &req
->n
,
4384 &req
->r
, buflen
, cmd
))
4391 } else { /* Multipath case */
4392 struct rtattr
*nest
;
4393 const union g_addr
*src1
= NULL
;
4395 nest
= nl_attr_nest(&req
->n
, buflen
, RTA_MULTIPATH
);
4399 routedesc
= "multipath";
4400 _netlink_mpls_debug(cmd
, dplane_ctx_get_in_label(ctx
),
4404 frr_each(nhlfe_list_const
, head
, nhlfe
) {
4405 nexthop
= nhlfe
->nexthop
;
4409 if ((cmd
== RTM_NEWROUTE
4410 && (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)
4411 && CHECK_FLAG(nexthop
->flags
,
4412 NEXTHOP_FLAG_ACTIVE
)))
4413 || (cmd
== RTM_DELROUTE
4414 && (CHECK_FLAG(nhlfe
->flags
,
4415 NHLFE_FLAG_INSTALLED
)
4416 && CHECK_FLAG(nexthop
->flags
,
4417 NEXTHOP_FLAG_FIB
)))) {
4420 /* Build the multipath */
4421 if (!_netlink_mpls_build_multipath(
4422 &p
, routedesc
, nhlfe
, &req
->n
,
4423 buflen
, &req
->r
, &src1
))
4428 /* Add the multipath */
4429 nl_attr_nest_end(&req
->n
, nest
);
4432 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
4435 /****************************************************************************
4436 * This code was developed in a branch that didn't have dplane APIs for
4437 * MAC updates. Hence the use of the legacy style. It will be moved to
4438 * the new dplane style pre-merge to master. XXX
4440 static int netlink_fdb_nh_update(uint32_t nh_id
, struct in_addr vtep_ip
)
4447 int cmd
= RTM_NEWNEXTHOP
;
4448 struct zebra_vrf
*zvrf
;
4449 struct zebra_ns
*zns
;
4451 zvrf
= zebra_vrf_get_evpn();
4456 memset(&req
, 0, sizeof(req
));
4458 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4459 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4460 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4461 req
.n
.nlmsg_type
= cmd
;
4462 req
.nhm
.nh_family
= AF_INET
;
4464 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4466 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4468 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GATEWAY
,
4469 &vtep_ip
, IPV4_MAX_BYTELEN
))
4472 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4473 zlog_debug("Tx %s fdb-nh 0x%x %pI4",
4474 nl_msg_type_to_str(cmd
), nh_id
, &vtep_ip
);
4477 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4481 static int netlink_fdb_nh_del(uint32_t nh_id
)
4488 int cmd
= RTM_DELNEXTHOP
;
4489 struct zebra_vrf
*zvrf
;
4490 struct zebra_ns
*zns
;
4492 zvrf
= zebra_vrf_get_evpn();
4497 memset(&req
, 0, sizeof(req
));
4499 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4500 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4501 req
.n
.nlmsg_type
= cmd
;
4502 req
.nhm
.nh_family
= AF_UNSPEC
;
4504 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nh_id
))
4507 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4508 zlog_debug("Tx %s fdb-nh 0x%x",
4509 nl_msg_type_to_str(cmd
), nh_id
);
4512 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4516 static int netlink_fdb_nhg_update(uint32_t nhg_id
, uint32_t nh_cnt
,
4517 struct nh_grp
*nh_ids
)
4524 int cmd
= RTM_NEWNEXTHOP
;
4525 struct zebra_vrf
*zvrf
;
4526 struct zebra_ns
*zns
;
4527 struct nexthop_grp grp
[nh_cnt
];
4530 zvrf
= zebra_vrf_get_evpn();
4535 memset(&req
, 0, sizeof(req
));
4537 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct nhmsg
));
4538 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
4539 req
.n
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
4540 req
.n
.nlmsg_type
= cmd
;
4541 req
.nhm
.nh_family
= AF_UNSPEC
;
4543 if (!nl_attr_put32(&req
.n
, sizeof(req
), NHA_ID
, nhg_id
))
4545 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_FDB
, NULL
, 0))
4547 memset(&grp
, 0, sizeof(grp
));
4548 for (i
= 0; i
< nh_cnt
; ++i
) {
4549 grp
[i
].id
= nh_ids
[i
].id
;
4550 grp
[i
].weight
= nh_ids
[i
].weight
;
4552 if (!nl_attr_put(&req
.n
, sizeof(req
), NHA_GROUP
,
4553 grp
, nh_cnt
* sizeof(struct nexthop_grp
)))
4557 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_EVPN_MH_NH
) {
4558 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
4562 for (i
= 0; i
< nh_cnt
; ++i
) {
4563 snprintf(nh_buf
, sizeof(nh_buf
), "%u ",
4565 strlcat(vtep_str
, nh_buf
, sizeof(vtep_str
));
4568 zlog_debug("Tx %s fdb-nhg 0x%x %s",
4569 nl_msg_type_to_str(cmd
), nhg_id
, vtep_str
);
4572 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
4576 static int netlink_fdb_nhg_del(uint32_t nhg_id
)
4578 return netlink_fdb_nh_del(nhg_id
);
4581 int kernel_upd_mac_nh(uint32_t nh_id
, struct in_addr vtep_ip
)
4583 return netlink_fdb_nh_update(nh_id
, vtep_ip
);
4586 int kernel_del_mac_nh(uint32_t nh_id
)
4588 return netlink_fdb_nh_del(nh_id
);
4591 int kernel_upd_mac_nhg(uint32_t nhg_id
, uint32_t nh_cnt
,
4592 struct nh_grp
*nh_ids
)
4594 return netlink_fdb_nhg_update(nhg_id
, nh_cnt
, nh_ids
);
4597 int kernel_del_mac_nhg(uint32_t nhg_id
)
4599 return netlink_fdb_nhg_del(nhg_id
);
4602 #endif /* HAVE_NETLINK */