1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zebra API message creation & consumption.
5 * Copyright (C) 1997-1999 Kunihiro Ishiguro
6 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
13 #include "lib/prefix.h"
14 #include "lib/stream.h"
15 #include "lib/memory.h"
16 #include "lib/table.h"
17 #include "lib/network.h"
19 #include "lib/zclient.h"
20 #include "lib/privs.h"
21 #include "lib/nexthop.h"
23 #include "lib/libfrr.h"
24 #include "lib/lib_errors.h"
26 #include "zebra/zebra_router.h"
27 #include "zebra/rib.h"
28 #include "zebra/zebra_ns.h"
29 #include "zebra/zebra_vrf.h"
30 #include "zebra/router-id.h"
31 #include "zebra/redistribute.h"
32 #include "zebra/debug.h"
33 #include "zebra/zebra_rnh.h"
34 #include "zebra/interface.h"
35 #include "zebra/zebra_ptm.h"
36 #include "zebra/rtadv.h"
37 #include "zebra/zebra_mpls.h"
38 #include "zebra/zebra_mroute.h"
39 #include "zebra/zebra_vxlan.h"
40 #include "zebra/zebra_evpn_mh.h"
42 #include "zebra/zebra_pbr.h"
43 #include "zebra/zebra_tc.h"
44 #include "zebra/table_manager.h"
45 #include "zebra/zapi_msg.h"
46 #include "zebra/zebra_errors.h"
47 #include "zebra/zebra_mlag.h"
48 #include "zebra/connected.h"
49 #include "zebra/zebra_opaque.h"
50 #include "zebra/zebra_srte.h"
51 #include "zebra/zebra_srv6.h"
53 DEFINE_MTYPE_STATIC(ZEBRA
, RE_OPAQUE
, "Route Opaque Data");
55 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
);
57 /* Encoding helpers -------------------------------------------------------- */
59 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
61 /* Interface information. */
62 struct zebra_if
*zif
= ifp
->info
;
64 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
65 stream_putl(s
, ifp
->ifindex
);
66 stream_putc(s
, ifp
->status
);
67 stream_putq(s
, ifp
->flags
);
68 stream_putc(s
, ifp
->ptm_enable
);
69 stream_putc(s
, ifp
->ptm_status
);
70 stream_putl(s
, ifp
->metric
);
71 stream_putl(s
, ifp
->speed
);
72 stream_putl(s
, ifp
->mtu
);
73 stream_putl(s
, ifp
->mtu6
);
74 stream_putl(s
, ifp
->bandwidth
);
75 stream_putl(s
, zif
->link_ifindex
);
76 stream_putl(s
, ifp
->ll_type
);
77 stream_putl(s
, ifp
->hw_addr_len
);
79 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
81 /* Then, Traffic Engineering parameters if any */
82 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
84 zebra_interface_link_params_write(s
, ifp
);
88 /* Write packet size. */
89 stream_putw_at(s
, 0, stream_get_endp(s
));
92 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
95 const char *netns_name
= zvrf_ns_name(zvrf
);
97 memset(&data
, 0, sizeof(data
));
98 data
.l
.table_id
= zvrf
->table_id
;
101 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
104 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
105 /* Pass the tableid and the netns NAME */
106 stream_put(s
, &data
, sizeof(struct vrf_data
));
107 /* Interface information. */
108 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
109 /* Write packet size. */
110 stream_putw_at(s
, 0, stream_get_endp(s
));
113 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
115 stream_putl(s
, nexthop
->vrf_id
);
116 stream_putc(s
, nexthop
->type
);
117 switch (nexthop
->type
) {
118 case NEXTHOP_TYPE_IPV4
:
119 case NEXTHOP_TYPE_IPV4_IFINDEX
:
120 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
121 stream_putl(s
, nexthop
->ifindex
);
123 case NEXTHOP_TYPE_IPV6
:
124 case NEXTHOP_TYPE_IPV6_IFINDEX
:
125 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
126 stream_putl(s
, nexthop
->ifindex
);
128 case NEXTHOP_TYPE_IFINDEX
:
129 stream_putl(s
, nexthop
->ifindex
);
131 case NEXTHOP_TYPE_BLACKHOLE
:
139 * Zebra error addition adds error type.
143 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * | enum zebra_error_types |
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
151 stream_put(s
, &error
, sizeof(error
));
153 /* Write packet size. */
154 stream_putw_at(s
, 0, stream_get_endp(s
));
157 /* Send handlers ----------------------------------------------------------- */
159 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
161 * This function is called in the following situations:
162 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
164 * - at startup, when zebra figures out the available interfaces
165 * - when an interface is added (where support for
166 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
167 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
170 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
172 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
174 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf
->vrf_id
);
175 zserv_encode_interface(s
, ifp
);
178 return zserv_send_message(client
, s
);
181 /* Interface deletion from zebra daemon. */
182 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
184 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
186 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf
->vrf_id
);
187 zserv_encode_interface(s
, ifp
);
190 return zserv_send_message(client
, s
);
193 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
195 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
197 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
198 zserv_encode_vrf(s
, zvrf
);
200 client
->vrfadd_cnt
++;
201 return zserv_send_message(client
, s
);
204 /* VRF deletion from zebra daemon. */
205 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
208 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
210 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
211 zserv_encode_vrf(s
, zvrf
);
213 client
->vrfdel_cnt
++;
214 return zserv_send_message(client
, s
);
217 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
219 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
221 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf
->vrf_id
);
223 /* Add Interface Index */
224 stream_putl(s
, ifp
->ifindex
);
226 /* Then TE Link Parameters */
227 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
232 /* Write packet size. */
233 stream_putw_at(s
, 0, stream_get_endp(s
));
235 return zserv_send_message(client
, s
);
238 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
239 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
241 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
242 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
243 * from the client, after the ZEBRA_INTERFACE_ADD has been
244 * sent from zebra to the client
245 * - redistribute new address info to all clients in the following situations
246 * - at startup, when zebra figures out the available interfaces
247 * - when an interface is added (where support for
248 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
249 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
251 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
252 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
253 * - when an RTM_NEWADDR message is received from the kernel,
255 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
257 * zsend_interface_address(DELETE)
260 * zebra_interface_address_delete_update
262 * | | if_delete_update
264 * ip_address_uninstall connected_delete_ipv4
265 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
268 * | RTM_NEWADDR on routing/netlink socket
271 * "no ip address A.B.C.D/M [label LINE]"
272 * "no ip address A.B.C.D/M"
273 * ["no ipv6 address X:X::X:X/M"]
276 int zsend_interface_address(int cmd
, struct zserv
*client
,
277 struct interface
*ifp
, struct connected
*ifc
)
281 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
283 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
284 stream_putl(s
, ifp
->ifindex
);
286 /* Interface address flag. */
287 stream_putc(s
, ifc
->flags
);
289 /* Prefix information. */
291 stream_putc(s
, p
->family
);
292 blen
= prefix_blen(p
);
293 stream_put(s
, &p
->u
.prefix
, blen
);
296 * XXX gnu version does not send prefixlen for
297 * ZEBRA_INTERFACE_ADDRESS_DELETE
298 * but zebra_interface_address_delete_read() in the gnu version
301 stream_putc(s
, p
->prefixlen
);
304 p
= ifc
->destination
;
306 stream_put(s
, &p
->u
.prefix
, blen
);
308 stream_put(s
, NULL
, blen
);
310 /* Write packet size. */
311 stream_putw_at(s
, 0, stream_get_endp(s
));
313 client
->connected_rt_add_cnt
++;
314 return zserv_send_message(client
, s
);
317 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
318 struct interface
*ifp
,
319 struct nbr_connected
*ifc
)
322 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
325 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
326 stream_putl(s
, ifp
->ifindex
);
328 /* Prefix information. */
330 stream_putc(s
, p
->family
);
331 blen
= prefix_blen(p
);
332 stream_put(s
, &p
->u
.prefix
, blen
);
335 * XXX gnu version does not send prefixlen for
336 * ZEBRA_INTERFACE_ADDRESS_DELETE
337 * but zebra_interface_address_delete_read() in the gnu version
340 stream_putc(s
, p
->prefixlen
);
342 /* Write packet size. */
343 stream_putw_at(s
, 0, stream_get_endp(s
));
345 return zserv_send_message(client
, s
);
348 /* Interface address addition. */
349 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
350 struct nbr_connected
*ifc
)
352 struct listnode
*node
, *nnode
;
353 struct zserv
*client
;
356 if (IS_ZEBRA_DEBUG_EVENT
) {
357 char buf
[INET6_ADDRSTRLEN
];
361 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
362 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
364 p
->prefixlen
, ifc
->ifp
->name
);
367 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
368 /* Do not send unsolicited messages to synchronous clients. */
369 if (client
->synchronous
)
372 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
377 /* Interface address deletion. */
378 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
379 struct nbr_connected
*ifc
)
381 struct listnode
*node
, *nnode
;
382 struct zserv
*client
;
385 if (IS_ZEBRA_DEBUG_EVENT
) {
386 char buf
[INET6_ADDRSTRLEN
];
390 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
391 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
393 p
->prefixlen
, ifc
->ifp
->name
);
396 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
397 /* Do not send unsolicited messages to synchronous clients. */
398 if (client
->synchronous
)
401 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
406 /* Send addresses on interface to client */
407 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
409 struct listnode
*cnode
, *cnnode
;
411 struct nbr_connected
*nc
;
413 /* Send interface addresses. */
414 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
415 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
418 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
424 /* Send interface neighbors. */
425 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
426 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
435 /* Notify client about interface moving from one VRF to another.
436 * Whether client is interested in old and new VRF is checked by caller.
438 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
441 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
443 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf
->vrf_id
);
445 /* Fill in the name of the interface and its new VRF (id) */
446 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
447 stream_putl(s
, vrf_id
);
449 /* Write packet size. */
450 stream_putw_at(s
, 0, stream_get_endp(s
));
452 client
->if_vrfchg_cnt
++;
453 return zserv_send_message(client
, s
);
456 /* Add new nbr connected IPv6 address */
457 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
459 struct nbr_connected
*ifc
;
463 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
464 p
.prefixlen
= IPV6_MAX_BITLEN
;
466 ifc
= listnode_head(ifp
->nbr_connected
);
469 ifc
= nbr_connected_new();
470 ifc
->address
= prefix_new();
472 listnode_add(ifp
->nbr_connected
, ifc
);
475 prefix_copy(ifc
->address
, &p
);
477 zebra_interface_nbr_address_add_update(ifp
, ifc
);
479 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
482 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
484 struct nbr_connected
*ifc
;
488 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
489 p
.prefixlen
= IPV6_MAX_BITLEN
;
491 ifc
= nbr_connected_check(ifp
, &p
);
495 listnode_delete(ifp
->nbr_connected
, ifc
);
497 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
499 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
501 nbr_connected_free(ifc
);
505 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
506 * ZEBRA_INTERFACE_DOWN.
508 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
509 * the clients in one of 2 situations:
510 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
511 * - a vty command modifying the bandwidth of an interface is received.
512 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
514 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
516 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
518 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
519 zserv_encode_interface(s
, ifp
);
521 if (cmd
== ZEBRA_INTERFACE_UP
)
524 client
->ifdown_cnt
++;
526 return zserv_send_message(client
, s
);
529 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
530 const struct route_node
*rn
,
531 const struct route_entry
*re
)
533 struct zapi_route api
;
534 struct zapi_nexthop
*api_nh
;
535 struct nexthop
*nexthop
;
536 const struct prefix
*p
, *src_p
;
540 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
542 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
543 memset(&api
, 0, sizeof(api
));
544 api
.vrf_id
= re
->vrf_id
;
546 api
.safi
= SAFI_UNICAST
;
547 api
.instance
= re
->instance
;
548 api
.flags
= re
->flags
;
550 afi
= family2afi(p
->family
);
553 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
554 client
->redist_v4_add_cnt
++;
556 client
->redist_v4_del_cnt
++;
559 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
560 client
->redist_v6_add_cnt
++;
562 client
->redist_v6_del_cnt
++;
573 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
574 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
577 for (nexthop
= re
->nhe
->nhg
.nexthop
;
578 nexthop
; nexthop
= nexthop
->next
) {
579 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
582 api_nh
= &api
.nexthops
[count
];
583 api_nh
->vrf_id
= nexthop
->vrf_id
;
584 api_nh
->type
= nexthop
->type
;
585 api_nh
->weight
= nexthop
->weight
;
586 switch (nexthop
->type
) {
587 case NEXTHOP_TYPE_BLACKHOLE
:
588 api_nh
->bh_type
= nexthop
->bh_type
;
590 case NEXTHOP_TYPE_IPV4
:
591 case NEXTHOP_TYPE_IPV4_IFINDEX
:
592 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
593 api_nh
->ifindex
= nexthop
->ifindex
;
595 case NEXTHOP_TYPE_IFINDEX
:
596 api_nh
->ifindex
= nexthop
->ifindex
;
598 case NEXTHOP_TYPE_IPV6
:
599 case NEXTHOP_TYPE_IPV6_IFINDEX
:
600 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
601 api_nh
->ifindex
= nexthop
->ifindex
;
608 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
609 api
.nexthop_num
= count
;
613 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
614 api
.distance
= re
->distance
;
615 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
616 api
.metric
= re
->metric
;
618 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
621 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
624 struct stream
*s
= stream_new(stream_size
);
626 /* Encode route and send. */
627 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
632 if (IS_ZEBRA_DEBUG_SEND
)
633 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
634 __func__
, zserv_command_string(cmd
),
635 zebra_route_string(client
->proto
),
636 zebra_route_string(api
.type
), api
.vrf_id
,
638 return zserv_send_message(client
, s
);
642 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
643 * nexthop is not found on mrib. Returns both route metric and protocol
646 * *XXX* this ZAPI call is slated to be removed at some point in the future
647 * since MRIB support in PIM is hopelessly broken in its interactions with NHT.
648 * The plan is to make pimd use NHT to receive URIB and MRIB in parallel and
649 * make the decision there, which will obsolete this ZAPI op.
650 * (Otherwise we would need to implement sending NHT updates for the result of
651 * this "URIB-MRIB-combined" table, but we only decide that here on the fly,
652 * so it'd be rather complex to do NHT for.)
654 static int zsend_nexthop_lookup_mrib(struct zserv
*client
, struct ipaddr
*addr
,
655 struct route_entry
*re
,
656 struct zebra_vrf
*zvrf
)
661 struct nexthop
*nexthop
;
663 /* Get output stream. */
664 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
667 /* Fill in result. */
668 zclient_create_header(s
, ZEBRA_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
669 stream_put_ipaddr(s
, addr
);
672 struct nexthop_group
*nhg
;
674 stream_putc(s
, re
->distance
);
675 stream_putl(s
, re
->metric
);
677 /* remember position for nexthop_num */
678 nump
= stream_get_endp(s
);
679 /* reserve room for nexthop_num */
681 nhg
= rib_get_fib_nhg(re
);
682 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
683 if (rnh_nexthop_valid(re
, nexthop
))
684 num
+= zserv_encode_nexthop(s
, nexthop
);
687 /* store nexthop_num */
688 stream_putc_at(s
, nump
, num
);
690 stream_putc(s
, 0); /* distance */
691 stream_putl(s
, 0); /* metric */
692 stream_putc(s
, 0); /* nexthop_num */
695 stream_putw_at(s
, 0, stream_get_endp(s
));
697 return zserv_send_message(client
, s
);
700 int zsend_nhg_notify(uint16_t type
, uint16_t instance
, uint32_t session_id
,
701 uint32_t id
, enum zapi_nhg_notify_owner note
)
703 struct zserv
*client
;
706 client
= zserv_find_client_session(type
, instance
, session_id
);
708 if (IS_ZEBRA_DEBUG_PACKET
) {
709 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
710 type
, instance
, id
, note
);
715 if (IS_ZEBRA_DEBUG_SEND
)
716 zlog_debug("%s: type %d, id %d, note %s",
717 __func__
, type
, id
, zapi_nhg_notify_owner2str(note
));
719 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
722 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
724 stream_put(s
, ¬e
, sizeof(note
));
727 stream_putw_at(s
, 0, stream_get_endp(s
));
729 return zserv_send_message(client
, s
);
733 * Common utility send route notification, called from a path using a
734 * route_entry and from a path using a dataplane context.
736 static int route_notify_internal(const struct route_node
*rn
, int type
,
737 uint16_t instance
, vrf_id_t vrf_id
,
739 enum zapi_route_notify_owner note
, afi_t afi
,
742 struct zserv
*client
;
746 client
= zserv_find_client(type
, instance
);
747 if (!client
|| !client
->notify_owner
) {
748 if (IS_ZEBRA_DEBUG_PACKET
)
750 "Not Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
751 zebra_route_string(type
), rn
, table_id
, note
,
756 if (IS_ZEBRA_DEBUG_PACKET
)
758 "Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
759 zebra_route_string(type
), rn
, table_id
, note
, vrf_id
);
761 /* We're just allocating a small-ish buffer here, since we only
762 * encode a small amount of data.
764 s
= stream_new(ZEBRA_SMALL_PACKET_SIZE
);
768 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
770 stream_put(s
, ¬e
, sizeof(note
));
772 stream_putc(s
, rn
->p
.family
);
774 blen
= prefix_blen(&rn
->p
);
775 stream_putc(s
, rn
->p
.prefixlen
);
776 stream_put(s
, &rn
->p
.u
.prefix
, blen
);
778 stream_putl(s
, table_id
);
780 /* Encode AFI, SAFI in the message */
782 stream_putc(s
, safi
);
784 stream_putw_at(s
, 0, stream_get_endp(s
));
786 return zserv_send_message(client
, s
);
789 int zsend_route_notify_owner(const struct route_node
*rn
,
790 struct route_entry
*re
,
791 enum zapi_route_notify_owner note
, afi_t afi
,
794 return (route_notify_internal(rn
, re
->type
, re
->instance
, re
->vrf_id
,
795 re
->table
, note
, afi
, safi
));
799 * Route-owner notification using info from dataplane update context.
801 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
802 enum zapi_route_notify_owner note
)
804 return (route_notify_internal(
805 rib_find_rn_from_ctx(ctx
), dplane_ctx_get_type(ctx
),
806 dplane_ctx_get_instance(ctx
), dplane_ctx_get_vrf(ctx
),
807 dplane_ctx_get_table(ctx
), note
, dplane_ctx_get_afi(ctx
),
808 dplane_ctx_get_safi(ctx
)));
811 static void zread_route_notify_request(ZAPI_HANDLER_ARGS
)
815 STREAM_GETC(msg
, notify
);
816 client
->notify_owner
= notify
;
821 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
822 enum zapi_rule_notify_owner note
)
824 struct listnode
*node
;
825 struct zserv
*client
;
828 if (IS_ZEBRA_DEBUG_PACKET
)
829 zlog_debug("%s: Notifying %u", __func__
,
830 dplane_ctx_rule_get_unique(ctx
));
832 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
833 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
840 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
842 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
843 stream_put(s
, ¬e
, sizeof(note
));
844 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
845 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
846 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
847 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
849 stream_putw_at(s
, 0, stream_get_endp(s
));
851 zserv_send_message(client
, s
);
854 void zsend_iptable_notify_owner(const struct zebra_dplane_ctx
*ctx
,
855 enum zapi_iptable_notify_owner note
)
857 struct listnode
*node
;
858 struct zserv
*client
;
860 struct zebra_pbr_iptable ipt
;
861 uint16_t cmd
= ZEBRA_IPTABLE_NOTIFY_OWNER
;
862 struct zebra_pbr_iptable
*ipt_hash
;
863 enum dplane_op_e op
= dplane_ctx_get_op(ctx
);
865 dplane_ctx_get_pbr_iptable(ctx
, &ipt
);
867 ipt_hash
= hash_lookup(zrouter
.iptable_hash
, &ipt
);
869 if (op
== DPLANE_OP_IPTABLE_ADD
&&
870 CHECK_FLAG(ipt_hash
->internal_flags
,
871 IPTABLE_INSTALL_QUEUED
))
872 UNSET_FLAG(ipt_hash
->internal_flags
,
873 IPTABLE_INSTALL_QUEUED
);
874 else if (op
== DPLANE_OP_IPTABLE_DELETE
&&
875 CHECK_FLAG(ipt_hash
->internal_flags
,
876 IPTABLE_UNINSTALL_QUEUED
))
877 UNSET_FLAG(ipt_hash
->internal_flags
,
878 IPTABLE_UNINSTALL_QUEUED
);
880 if (IS_ZEBRA_DEBUG_PACKET
)
881 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
882 zserv_command_string(cmd
), ipt
.unique
, note
);
884 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
885 if (ipt
.sock
== client
->sock
)
892 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
894 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
895 stream_putw(s
, note
);
896 stream_putl(s
, ipt
.unique
);
897 stream_put(s
, ipt
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
898 stream_putw_at(s
, 0, stream_get_endp(s
));
900 zserv_send_message(client
, s
);
903 void zsend_ipset_notify_owner(const struct zebra_dplane_ctx
*ctx
,
904 enum zapi_ipset_notify_owner note
)
906 struct listnode
*node
;
907 struct zserv
*client
;
909 struct zebra_pbr_ipset ipset
;
910 uint16_t cmd
= ZEBRA_IPSET_NOTIFY_OWNER
;
912 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
914 if (IS_ZEBRA_DEBUG_PACKET
)
915 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
916 zserv_command_string(cmd
), ipset
.unique
, note
);
918 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
919 if (ipset
.sock
== client
->sock
)
926 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
928 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
929 stream_putw(s
, note
);
930 stream_putl(s
, ipset
.unique
);
931 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
932 stream_putw_at(s
, 0, stream_get_endp(s
));
934 zserv_send_message(client
, s
);
937 void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx
*ctx
,
938 enum zapi_ipset_entry_notify_owner note
)
940 struct listnode
*node
;
941 struct zserv
*client
;
943 struct zebra_pbr_ipset_entry ipent
;
944 struct zebra_pbr_ipset ipset
;
945 uint16_t cmd
= ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
;
947 dplane_ctx_get_pbr_ipset_entry(ctx
, &ipent
);
948 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
950 if (IS_ZEBRA_DEBUG_PACKET
)
951 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
952 zserv_command_string(cmd
), ipent
.unique
, note
);
954 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
955 if (ipent
.sock
== client
->sock
)
962 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
964 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
965 stream_putw(s
, note
);
966 stream_putl(s
, ipent
.unique
);
967 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
968 stream_putw_at(s
, 0, stream_get_endp(s
));
970 zserv_send_message(client
, s
);
973 void zsend_nhrp_neighbor_notify(int cmd
, struct interface
*ifp
,
974 struct ipaddr
*ipaddr
, int ndm_state
,
975 union sockunion
*link_layer_ipv4
)
978 struct listnode
*node
, *nnode
;
979 struct zserv
*client
;
983 if (IS_ZEBRA_DEBUG_PACKET
)
984 zlog_debug("%s: Notifying Neighbor entry (%u)", __func__
, cmd
);
986 sockunion_family(&ip
) = ipaddr_family(ipaddr
);
987 afi
= family2afi(sockunion_family(&ip
));
988 memcpy((char *)sockunion_get_addr(&ip
), &ipaddr
->ip
.addr
,
989 family2addrsize(sockunion_family(&ip
)));
991 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
992 if (!vrf_bitmap_check(client
->nhrp_neighinfo
[afi
],
996 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
997 zclient_neigh_ip_encode(s
, cmd
, &ip
, link_layer_ipv4
, ifp
,
999 stream_putw_at(s
, 0, stream_get_endp(s
));
1000 zserv_send_message(client
, s
);
1005 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
1006 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
1012 /* Check this client need interface information. */
1013 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
1016 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1019 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1021 /* Prefix information. */
1022 stream_putc(s
, p
->family
);
1023 blen
= prefix_blen(p
);
1024 stream_put(s
, &p
->u
.prefix
, blen
);
1025 stream_putc(s
, p
->prefixlen
);
1027 /* Write packet size. */
1028 stream_putw_at(s
, 0, stream_get_endp(s
));
1030 return zserv_send_message(client
, s
);
1034 * Function used by Zebra to send a PW status update to LDP daemon
1036 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1038 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1040 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1041 stream_write(s
, pw
->ifname
, INTERFACE_NAMSIZ
);
1042 stream_putl(s
, pw
->ifindex
);
1043 stream_putl(s
, pw
->status
);
1045 /* Put length at the first point of the stream. */
1046 stream_putw_at(s
, 0, stream_get_endp(s
));
1048 return zserv_send_message(client
, s
);
1051 /* Send response to a get label chunk request to client */
1052 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
1053 struct label_manager_chunk
*lmc
)
1055 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1057 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1059 stream_putc(s
, client
->proto
);
1061 stream_putw(s
, client
->instance
);
1065 stream_putc(s
, lmc
->keep
);
1066 /* start and end labels */
1067 stream_putl(s
, lmc
->start
);
1068 stream_putl(s
, lmc
->end
);
1071 /* Write packet size. */
1072 stream_putw_at(s
, 0, stream_get_endp(s
));
1074 return zserv_send_message(client
, s
);
1077 /* Send response to a label manager connect request to client */
1078 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1079 unsigned short result
)
1081 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1083 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1086 stream_putc(s
, client
->proto
);
1089 stream_putw(s
, client
->instance
);
1092 stream_putc(s
, result
);
1094 /* Write packet size. */
1095 stream_putw_at(s
, 0, stream_get_endp(s
));
1097 return zserv_send_message(client
, s
);
1100 /* Send response to a get table chunk request to client */
1101 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1103 struct table_manager_chunk
*tmc
)
1105 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1107 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1110 /* start and end labels */
1111 stream_putl(s
, tmc
->start
);
1112 stream_putl(s
, tmc
->end
);
1115 /* Write packet size. */
1116 stream_putw_at(s
, 0, stream_get_endp(s
));
1118 return zserv_send_message(client
, s
);
1121 static int zsend_table_manager_connect_response(struct zserv
*client
,
1125 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1127 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1130 stream_putc(s
, result
);
1132 stream_putw_at(s
, 0, stream_get_endp(s
));
1134 return zserv_send_message(client
, s
);
1137 /* SRv6 locator add notification from zebra daemon. */
1138 int zsend_zebra_srv6_locator_add(struct zserv
*client
, struct srv6_locator
*loc
)
1140 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1142 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_ADD
, VRF_DEFAULT
);
1143 zapi_srv6_locator_encode(s
, loc
);
1144 stream_putw_at(s
, 0, stream_get_endp(s
));
1146 return zserv_send_message(client
, s
);
1149 /* SRv6 locator delete notification from zebra daemon. */
1150 int zsend_zebra_srv6_locator_delete(struct zserv
*client
,
1151 struct srv6_locator
*loc
)
1153 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1155 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_DELETE
, VRF_DEFAULT
);
1156 zapi_srv6_locator_encode(s
, loc
);
1157 stream_putw_at(s
, 0, stream_get_endp(s
));
1159 return zserv_send_message(client
, s
);
1162 /* Inbound message handling ------------------------------------------------ */
1164 /* Nexthop register */
1165 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1170 unsigned short l
= 0;
1171 uint8_t connected
= 0;
1172 uint8_t resolve_via_default
;
1174 bool flag_changed
= false;
1178 if (IS_ZEBRA_DEBUG_NHT
)
1180 "rnh_register msg from client %s: hdr->length=%d vrf=%u",
1181 zebra_route_string(client
->proto
), hdr
->length
,
1186 if (!client
->nh_reg_time
)
1187 client
->nh_reg_time
= monotime(NULL
);
1189 while (l
< hdr
->length
) {
1190 STREAM_GETC(s
, connected
);
1191 STREAM_GETC(s
, resolve_via_default
);
1192 STREAM_GETW(s
, safi
);
1193 STREAM_GETW(s
, p
.family
);
1194 STREAM_GETC(s
, p
.prefixlen
);
1196 if (p
.family
== AF_INET
) {
1197 client
->v4_nh_watch_add_cnt
++;
1198 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1200 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1201 __func__
, p
.prefixlen
);
1204 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1205 l
+= IPV4_MAX_BYTELEN
;
1206 } else if (p
.family
== AF_INET6
) {
1207 client
->v6_nh_watch_add_cnt
++;
1208 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1210 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1211 __func__
, p
.prefixlen
);
1214 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1215 l
+= IPV6_MAX_BYTELEN
;
1218 EC_ZEBRA_UNKNOWN_FAMILY
,
1219 "rnh_register: Received unknown family type %d",
1223 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), safi
, &exist
);
1227 orig_flags
= rnh
->flags
;
1228 if (connected
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1229 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1231 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1232 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1234 if (resolve_via_default
)
1235 SET_FLAG(rnh
->flags
, ZEBRA_NHT_RESOLVE_VIA_DEFAULT
);
1237 if (orig_flags
!= rnh
->flags
)
1238 flag_changed
= true;
1240 /* Anything not AF_INET/INET6 has been filtered out above */
1241 if (!exist
|| flag_changed
)
1242 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, &p
,
1245 zebra_add_rnh_client(rnh
, client
, zvrf_id(zvrf
));
1252 /* Nexthop register */
1253 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1258 unsigned short l
= 0;
1261 if (IS_ZEBRA_DEBUG_NHT
)
1263 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1264 zebra_route_string(client
->proto
), hdr
->length
,
1269 while (l
< hdr
->length
) {
1272 STREAM_GETC(s
, ignore
);
1274 goto stream_failure
;
1275 STREAM_GETC(s
, ignore
);
1277 goto stream_failure
;
1279 STREAM_GETW(s
, safi
);
1280 STREAM_GETW(s
, p
.family
);
1281 STREAM_GETC(s
, p
.prefixlen
);
1283 if (p
.family
== AF_INET
) {
1284 client
->v4_nh_watch_rem_cnt
++;
1285 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1287 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1288 __func__
, p
.prefixlen
);
1291 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1292 l
+= IPV4_MAX_BYTELEN
;
1293 } else if (p
.family
== AF_INET6
) {
1294 client
->v6_nh_watch_rem_cnt
++;
1295 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1297 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1298 __func__
, p
.prefixlen
);
1301 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1302 l
+= IPV6_MAX_BYTELEN
;
1305 EC_ZEBRA_UNKNOWN_FAMILY
,
1306 "rnh_register: Received unknown family type %d",
1310 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), safi
);
1312 client
->nh_dereg_time
= monotime(NULL
);
1313 zebra_remove_rnh_client(rnh
, client
);
1320 #define ZEBRA_MIN_FEC_LENGTH 5
1323 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1326 unsigned short l
= 0;
1329 uint32_t label
= MPLS_INVALID_LABEL
;
1330 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1333 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
1338 * The minimum amount of data that can be sent for one fec
1341 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1343 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1344 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1349 while (l
< hdr
->length
) {
1350 STREAM_GETW(s
, flags
);
1351 memset(&p
, 0, sizeof(p
));
1352 STREAM_GETW(s
, p
.family
);
1353 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1355 EC_ZEBRA_UNKNOWN_FAMILY
,
1356 "fec_register: Received unknown family type %d",
1360 STREAM_GETC(s
, p
.prefixlen
);
1361 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1362 || (p
.family
== AF_INET6
1363 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1365 "%s: Specified prefix hdr->length: %d is to long for %d",
1366 __func__
, p
.prefixlen
, p
.family
);
1370 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1371 l
+= PSIZE(p
.prefixlen
);
1372 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1373 STREAM_GETL(s
, label
);
1375 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1376 STREAM_GETL(s
, label_index
);
1380 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1387 /* FEC unregister */
1388 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1391 unsigned short l
= 0;
1396 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
1401 * The minimum amount of data that can be sent for one
1402 * fec unregistration
1404 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1406 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1407 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1412 while (l
< hdr
->length
) {
1413 STREAM_GETW(s
, flags
);
1415 goto stream_failure
;
1417 memset(&p
, 0, sizeof(p
));
1418 STREAM_GETW(s
, p
.family
);
1419 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1421 EC_ZEBRA_UNKNOWN_FAMILY
,
1422 "fec_unregister: Received unknown family type %d",
1426 STREAM_GETC(s
, p
.prefixlen
);
1427 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1428 || (p
.family
== AF_INET6
1429 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1431 "%s: Received prefix hdr->length %d which is greater than %d can support",
1432 __func__
, p
.prefixlen
, p
.family
);
1436 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1437 l
+= PSIZE(p
.prefixlen
);
1438 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1447 * Register zebra server interface information.
1448 * Send current all interface and address information.
1450 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1453 struct interface
*ifp
;
1455 vrf_id_t vrf_id
= zvrf_id(zvrf
);
1456 if (vrf_id
!= VRF_DEFAULT
&& vrf_id
!= VRF_UNKNOWN
) {
1457 FOR_ALL_INTERFACES (zvrf
->vrf
, ifp
) {
1458 /* Skip pseudo interface. */
1459 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1462 zsend_interface_add(client
, ifp
);
1463 zsend_interface_link_params(client
, ifp
);
1464 zsend_interface_addresses(client
, ifp
);
1469 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1470 FOR_ALL_INTERFACES (vrf
, ifp
) {
1471 /* Skip pseudo interface. */
1472 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1475 zsend_interface_add(client
, ifp
);
1476 zsend_interface_link_params(client
, ifp
);
1477 zsend_interface_addresses(client
, ifp
);
1482 /* Unregister zebra server interface information. */
1483 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1488 * Handle message requesting interface be set up or down.
1490 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1493 struct interface
*ifp
;
1495 enum protodown_reasons reason
;
1497 STREAM_GETL(msg
, ifindex
);
1498 STREAM_GETC(msg
, down
);
1501 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1505 "Cannot set protodown %s for interface %u; does not exist",
1506 down
? "on" : "off", ifindex
);
1511 switch (client
->proto
) {
1512 case ZEBRA_ROUTE_VRRP
:
1513 reason
= ZEBRA_PROTODOWN_VRRP
;
1515 case ZEBRA_ROUTE_SHARP
:
1516 reason
= ZEBRA_PROTODOWN_SHARP
;
1523 zebra_if_set_protodown(ifp
, down
, reason
);
1529 bool zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1530 const unsigned int nexthop_num
)
1532 if (nexthop_num
> zrouter
.multipath_num
) {
1533 char buff
[PREFIX2STR_BUFFER
];
1536 prefix2str(p
, buff
, sizeof(buff
));
1539 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1540 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1541 caller
, (p
? buff
: "(NULL)"), nexthop_num
,
1542 zrouter
.multipath_num
);
1550 * Create a new nexthop based on a zapi nexthop.
1552 static struct nexthop
*nexthop_from_zapi(const struct zapi_nexthop
*api_nh
,
1553 uint32_t flags
, struct prefix
*p
,
1554 uint16_t backup_nexthop_num
)
1556 struct nexthop
*nexthop
= NULL
;
1557 struct ipaddr vtep_ip
;
1558 struct interface
*ifp
;
1560 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1562 switch (api_nh
->type
) {
1563 case NEXTHOP_TYPE_IFINDEX
:
1564 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1566 case NEXTHOP_TYPE_IPV4
:
1567 if (IS_ZEBRA_DEBUG_RECV
) {
1568 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1570 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1571 nhbuf
, api_nh
->vrf_id
);
1573 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1576 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1577 if (IS_ZEBRA_DEBUG_RECV
) {
1578 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1580 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1581 __func__
, nhbuf
, api_nh
->vrf_id
,
1585 nexthop
= nexthop_from_ipv4_ifindex(
1586 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1589 /* Special handling for IPv4 routes sourced from EVPN:
1590 * the nexthop and associated MAC need to be installed.
1592 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1593 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
1594 vtep_ip
.ipa_type
= IPADDR_V4
;
1595 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1596 sizeof(struct in_addr
));
1597 zebra_rib_queue_evpn_route_add(
1598 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1599 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
);
1602 case NEXTHOP_TYPE_IPV6
:
1603 if (IS_ZEBRA_DEBUG_RECV
) {
1604 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1606 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1607 nhbuf
, api_nh
->vrf_id
);
1609 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1611 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1612 if (IS_ZEBRA_DEBUG_RECV
) {
1613 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1615 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1616 __func__
, nhbuf
, api_nh
->vrf_id
,
1619 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1623 /* Special handling for IPv6 routes sourced from EVPN:
1624 * the nexthop and associated MAC need to be installed.
1626 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1627 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
1628 vtep_ip
.ipa_type
= IPADDR_V6
;
1629 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1630 sizeof(struct in6_addr
));
1631 zebra_rib_queue_evpn_route_add(
1632 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1633 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
);
1636 case NEXTHOP_TYPE_BLACKHOLE
:
1637 if (IS_ZEBRA_DEBUG_RECV
)
1638 zlog_debug("%s: nh blackhole %d",
1639 __func__
, api_nh
->bh_type
);
1642 nexthop_from_blackhole(api_nh
->bh_type
, api_nh
->vrf_id
);
1646 /* Return early if we couldn't process the zapi nexthop */
1647 if (nexthop
== NULL
) {
1651 /* Mark nexthop as onlink either if client has explicitly told us
1652 * to or if the nexthop is on an 'unnumbered' interface.
1654 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1655 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1656 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1657 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1658 if (ifp
&& connected_is_unnumbered(ifp
))
1659 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1662 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1663 nexthop
->weight
= api_nh
->weight
;
1665 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1666 /* Validate count */
1667 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1668 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1669 zlog_debug("%s: invalid backup nh count %d",
1670 __func__
, api_nh
->backup_num
);
1671 nexthop_free(nexthop
);
1676 /* Copy backup info */
1677 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1678 nexthop
->backup_num
= api_nh
->backup_num
;
1680 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1681 /* Validate backup index */
1682 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1683 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1685 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1686 zlog_debug("%s: invalid backup nh idx %d",
1688 api_nh
->backup_idx
[i
]);
1689 nexthop_free(nexthop
);
1700 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1701 struct zapi_nexthop
*nhops
, uint32_t flags
,
1702 uint32_t message
, uint16_t nexthop_num
,
1703 uint16_t backup_nh_num
,
1704 struct nexthop_group
**png
,
1705 struct nhg_backup_info
**pbnhg
)
1707 struct nexthop_group
*ng
= NULL
;
1708 struct nhg_backup_info
*bnhg
= NULL
;
1710 struct nexthop
*last_nh
= NULL
;
1712 assert(!(png
&& pbnhg
));
1715 ng
= nexthop_group_new();
1717 if (pbnhg
&& backup_nh_num
> 0) {
1718 if (IS_ZEBRA_DEBUG_RECV
)
1719 zlog_debug("%s: adding %d backup nexthops", __func__
,
1722 bnhg
= zebra_nhg_backup_alloc();
1726 * TBD should _all_ of the nexthop add operations use
1727 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1728 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1730 for (i
= 0; i
< nexthop_num
; i
++) {
1731 struct nexthop
*nexthop
;
1732 enum lsp_types_t label_type
;
1733 char nhbuf
[NEXTHOP_STRLEN
];
1734 char labelbuf
[MPLS_LABEL_STRLEN
];
1735 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1737 /* Convert zapi nexthop */
1738 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1741 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1742 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1743 __func__
, nexthop_num
, i
);
1745 nexthop_group_delete(&ng
);
1747 zebra_nhg_backup_free(&bnhg
);
1752 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1753 if (IS_ZEBRA_DEBUG_RECV
) {
1754 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1755 zlog_debug("%s: backup nh %s with BACKUP flag!",
1758 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1759 nexthop
->backup_num
= 0;
1762 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1763 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1764 nexthop
->srte_color
= api_nh
->srte_color
;
1767 /* Labels for MPLS BGP-LU or Segment Routing or EVPN */
1768 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1769 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1770 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1771 && api_nh
->label_num
> 0) {
1773 /* If label type was passed, use it */
1774 if (api_nh
->label_type
)
1775 label_type
= api_nh
->label_type
;
1778 lsp_type_from_re_type(client
->proto
);
1780 nexthop_add_labels(nexthop
, label_type
,
1782 &api_nh
->labels
[0]);
1785 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)
1786 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1787 if (IS_ZEBRA_DEBUG_RECV
)
1788 zlog_debug("%s: adding seg6local action %s",
1790 seg6local_action2str(
1791 api_nh
->seg6local_action
));
1793 nexthop_add_srv6_seg6local(nexthop
,
1794 api_nh
->seg6local_action
,
1795 &api_nh
->seg6local_ctx
);
1798 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
)
1799 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1800 if (IS_ZEBRA_DEBUG_RECV
)
1801 zlog_debug("%s: adding seg6", __func__
);
1803 nexthop_add_srv6_seg6(nexthop
, &api_nh
->seg6_segs
);
1806 if (IS_ZEBRA_DEBUG_RECV
) {
1810 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1812 if (nexthop
->nh_label
&&
1813 nexthop
->nh_label
->num_labels
> 0) {
1814 mpls_label2str(nexthop
->nh_label
->num_labels
,
1815 nexthop
->nh_label
->label
,
1816 labelbuf
, sizeof(labelbuf
),
1817 nexthop
->nh_label_type
, false);
1820 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1821 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1825 /* Add new nexthop to temporary list. This list is
1826 * canonicalized - sorted - so that it can be hashed
1827 * later in route processing. We expect that the sender
1828 * has sent the list sorted, and the zapi client api
1829 * attempts to enforce that, so this should be
1830 * inexpensive - but it is necessary to support shared
1833 nexthop_group_add_sorted(ng
, nexthop
);
1836 /* Note that the order of the backup nexthops is
1837 * significant, so we don't sort this list as we do the
1838 * primary nexthops, we just append.
1841 NEXTHOP_APPEND(last_nh
, nexthop
);
1843 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1850 /* succesfully read, set caller pointers now */
1860 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1863 struct zapi_nexthop
*znh
;
1865 STREAM_GETW(s
, api_nhg
->proto
);
1866 STREAM_GETL(s
, api_nhg
->id
);
1868 if (cmd
== ZEBRA_NHG_DEL
)
1871 STREAM_GETW(s
, api_nhg
->resilience
.buckets
);
1872 STREAM_GETL(s
, api_nhg
->resilience
.idle_timer
);
1873 STREAM_GETL(s
, api_nhg
->resilience
.unbalanced_timer
);
1876 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1878 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1881 if (api_nhg
->nexthop_num
<= 0) {
1882 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1883 "%s: No nexthops sent", __func__
);
1887 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1888 znh
= &((api_nhg
->nexthops
)[i
]);
1890 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1891 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1892 "%s: Nexthop creation failed", __func__
);
1897 /* Backup Nexthops */
1898 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1900 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1903 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1904 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1906 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1907 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1908 "%s: Backup Nexthop creation failed",
1919 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1920 "%s: Nexthop Group decode failed with some sort of stream read failure",
1925 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1928 struct zapi_nhg api_nhg
= {};
1929 struct nhg_hash_entry
*nhe
;
1932 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1933 if (IS_ZEBRA_DEBUG_RECV
)
1934 zlog_debug("%s: Unable to decode zapi_nhg sent",
1940 * Delete the received nhg id
1942 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1945 zebra_nhg_decrement_ref(nhe
);
1946 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1947 client
->session_id
, api_nhg
.id
,
1950 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1951 client
->session_id
, api_nhg
.id
,
1952 ZAPI_NHG_REMOVE_FAIL
);
1955 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1958 struct zapi_nhg api_nhg
= {};
1959 struct nexthop_group
*nhg
= NULL
;
1960 struct nhg_backup_info
*bnhg
= NULL
;
1961 struct nhg_hash_entry
*nhe
;
1964 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1965 if (IS_ZEBRA_DEBUG_RECV
)
1966 zlog_debug("%s: Unable to decode zapi_nhg sent",
1971 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1972 api_nhg
.nexthop_num
,
1973 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1974 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1975 api_nhg
.backup_nexthop_num
,
1976 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1978 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1979 "%s: Nexthop Group Creation failed", __func__
);
1981 /* Free any local allocations */
1982 nexthop_group_delete(&nhg
);
1983 zebra_nhg_backup_free(&bnhg
);
1988 /* Create a temporary nhe */
1989 nhe
= zebra_nhg_alloc();
1990 nhe
->id
= api_nhg
.id
;
1991 nhe
->type
= api_nhg
.proto
;
1992 nhe
->zapi_instance
= client
->instance
;
1993 nhe
->zapi_session
= client
->session_id
;
1995 /* Take over the list(s) of nexthops */
1996 nhe
->nhg
.nexthop
= nhg
->nexthop
;
1997 nhg
->nexthop
= NULL
;
1999 nhe
->nhg
.nhgr
= api_nhg
.resilience
;
2002 nhe
->backup_info
= bnhg
;
2008 * Assume fully resolved for now and install.
2009 * Resolution is going to need some more work.
2012 /* Enqueue to workqueue for processing */
2013 rib_queue_nhe_add(nhe
);
2015 /* Free any local allocations */
2016 nexthop_group_delete(&nhg
);
2017 zebra_nhg_backup_free(&bnhg
);
2021 static void zread_route_add(ZAPI_HANDLER_ARGS
)
2024 struct zapi_route api
;
2026 struct prefix_ipv6
*src_p
= NULL
;
2027 struct route_entry
*re
;
2028 struct nexthop_group
*ng
= NULL
;
2029 struct nhg_backup_info
*bnhg
= NULL
;
2032 struct nhg_hash_entry nhe
, *n
= NULL
;
2035 if (zapi_route_decode(s
, &api
) < 0) {
2036 if (IS_ZEBRA_DEBUG_RECV
)
2037 zlog_debug("%s: Unable to decode zapi_route sent",
2042 vrf_id
= zvrf_id(zvrf
);
2044 if (IS_ZEBRA_DEBUG_RECV
)
2045 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2046 __func__
, vrf_id
, api
.tableid
, &api
.prefix
,
2047 (int)api
.message
, api
.flags
);
2049 /* Allocate new route. */
2050 re
= zebra_rib_route_entry_new(
2051 vrf_id
, api
.type
, api
.instance
, api
.flags
, api
.nhgid
,
2052 api
.tableid
? api
.tableid
: zvrf
->table_id
, api
.metric
, api
.mtu
,
2053 api
.distance
, api
.tag
);
2055 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
2056 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
2057 || api
.nexthop_num
== 0)) {
2059 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
2060 "%s: received a route without nexthops for prefix %pFX from client %s",
2061 __func__
, &api
.prefix
,
2062 zebra_route_string(client
->proto
));
2064 XFREE(MTYPE_RE
, re
);
2068 /* Report misuse of the backup flag */
2069 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
2070 && api
.backup_nexthop_num
== 0) {
2071 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
2073 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
2074 __func__
, zebra_route_string(client
->proto
),
2079 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
2080 api
.flags
, api
.message
, api
.nexthop_num
,
2081 api
.backup_nexthop_num
, &ng
, NULL
)
2082 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
2083 api
.flags
, api
.message
,
2084 api
.backup_nexthop_num
,
2085 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
2087 nexthop_group_delete(&ng
);
2088 zebra_nhg_backup_free(&bnhg
);
2089 XFREE(MTYPE_RE
, re
);
2093 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
)) {
2095 XMALLOC(MTYPE_RE_OPAQUE
,
2096 sizeof(struct re_opaque
) + api
.opaque
.length
);
2097 re
->opaque
->length
= api
.opaque
.length
;
2098 memcpy(re
->opaque
->data
, api
.opaque
.data
, re
->opaque
->length
);
2101 afi
= family2afi(api
.prefix
.family
);
2102 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2103 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2104 "%s: Received SRC Prefix but afi is not v6",
2106 nexthop_group_delete(&ng
);
2107 zebra_nhg_backup_free(&bnhg
);
2108 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2109 XFREE(MTYPE_RE
, re
);
2112 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2113 src_p
= &api
.src_prefix
;
2115 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
2116 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
2117 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
2118 __func__
, api
.safi
);
2119 nexthop_group_delete(&ng
);
2120 zebra_nhg_backup_free(&bnhg
);
2121 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2122 XFREE(MTYPE_RE
, re
);
2127 * If we have an ID, this proto owns the NHG it sent along with the
2128 * route, so we just send the ID into rib code with it.
2130 * Havent figured out how to handle backup NHs with this yet, so lets
2131 * keep that separate.
2132 * Include backup info with the route. We use a temporary nhe here;
2133 * if this is a new/unknown nhe, a new copy will be allocated
2137 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
2138 nhe
.nhg
.nexthop
= ng
->nexthop
;
2139 nhe
.backup_info
= bnhg
;
2140 n
= zebra_nhe_copy(&nhe
, 0);
2142 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
, re
, n
,
2146 * rib_add_multipath_nhe only fails in a couple spots
2147 * and in those spots we have not freed memory
2150 client
->error_cnt
++;
2151 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2152 XFREE(MTYPE_RE
, re
);
2155 /* At this point, these allocations are not needed: 're' has been
2156 * retained or freed, and if 're' still exists, it is using
2157 * a reference to a shared group object.
2159 nexthop_group_delete(&ng
);
2161 zebra_nhg_backup_free(&bnhg
);
2164 switch (api
.prefix
.family
) {
2167 client
->v4_route_add_cnt
++;
2169 client
->v4_route_upd8_cnt
++;
2173 client
->v6_route_add_cnt
++;
2175 client
->v6_route_upd8_cnt
++;
2180 void zapi_re_opaque_free(struct re_opaque
*opaque
)
2182 XFREE(MTYPE_RE_OPAQUE
, opaque
);
2185 static void zread_route_del(ZAPI_HANDLER_ARGS
)
2188 struct zapi_route api
;
2190 struct prefix_ipv6
*src_p
= NULL
;
2194 if (zapi_route_decode(s
, &api
) < 0)
2197 afi
= family2afi(api
.prefix
.family
);
2198 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2199 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2200 "%s: Received a src prefix while afi is not v6",
2204 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2205 src_p
= &api
.src_prefix
;
2208 table_id
= api
.tableid
;
2210 table_id
= zvrf
->table_id
;
2212 if (IS_ZEBRA_DEBUG_RECV
)
2213 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2214 __func__
, zvrf_id(zvrf
), table_id
, &api
.prefix
,
2215 (int)api
.message
, api
.flags
);
2217 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2218 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
2219 api
.distance
, false);
2222 switch (api
.prefix
.family
) {
2224 client
->v4_route_del_cnt
++;
2227 client
->v6_route_del_cnt
++;
2232 /* MRIB Nexthop lookup for IPv4. */
2233 static void zread_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2236 struct route_entry
*re
= NULL
;
2239 STREAM_GET_IPADDR(msg
, &addr
);
2241 switch (addr
.ipa_type
) {
2243 gaddr
.ipv4
= addr
.ipaddr_v4
;
2244 re
= rib_match_multicast(AFI_IP
, zvrf_id(zvrf
), &gaddr
, NULL
);
2247 gaddr
.ipv6
= addr
.ipaddr_v6
;
2248 re
= rib_match_multicast(AFI_IP6
, zvrf_id(zvrf
), &gaddr
, NULL
);
2252 goto stream_failure
;
2255 zsend_nexthop_lookup_mrib(client
, &addr
, re
, zvrf
);
2261 /* Register zebra server router-id information. Send current router-id */
2262 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2268 STREAM_GETW(msg
, afi
);
2270 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2272 "Invalid AFI %u while registering for router ID notifications",
2274 goto stream_failure
;
2277 /* Router-id information is needed. */
2278 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2280 router_id_get(afi
, &p
, zvrf
);
2283 * If we have not officially setup a router-id let's not
2284 * tell the upper level protocol about it yet.
2286 memset(&zero
, 0, sizeof(zero
));
2287 if ((p
.family
== AF_INET
&& p
.u
.prefix4
.s_addr
== INADDR_ANY
)
2288 || (p
.family
== AF_INET6
2289 && memcmp(&p
.u
.prefix6
, &zero
.u
.prefix6
,
2290 sizeof(struct in6_addr
))
2294 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2300 /* Unregister zebra server router-id information. */
2301 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2305 STREAM_GETW(msg
, afi
);
2307 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2309 "Invalid AFI %u while unregistering from router ID notifications",
2311 goto stream_failure
;
2314 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2320 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2322 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2324 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2325 stream_putl(s
, vrf_get_backend());
2326 stream_putc(s
, mpls_enabled
);
2327 stream_putl(s
, zrouter
.multipath_num
);
2328 stream_putc(s
, zebra_mlag_get_role());
2330 stream_putw_at(s
, 0, stream_get_endp(s
));
2331 zserv_send_message(client
, s
);
2334 void zsend_capabilities_all_clients(void)
2336 struct listnode
*node
, *nnode
;
2337 struct zebra_vrf
*zvrf
;
2338 struct zserv
*client
;
2340 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2341 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2342 /* Do not send unsolicited messages to synchronous clients. */
2343 if (client
->synchronous
)
2346 zsend_capabilities(client
, zvrf
);
2350 /* Tie up route-type and client->sock */
2351 static void zread_hello(ZAPI_HANDLER_ARGS
)
2353 /* type of protocol (lib/zebra.h) */
2355 unsigned short instance
;
2357 uint8_t synchronous
;
2358 uint32_t session_id
;
2360 STREAM_GETC(msg
, proto
);
2361 STREAM_GETW(msg
, instance
);
2362 STREAM_GETL(msg
, session_id
);
2363 STREAM_GETC(msg
, notify
);
2364 STREAM_GETC(msg
, synchronous
);
2366 client
->notify_owner
= true;
2369 client
->synchronous
= true;
2371 /* accept only dynamic routing protocols */
2372 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2374 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2375 client
->sock
, zebra_route_string(proto
),
2378 zlog_notice("client protocol instance %d", instance
);
2380 client
->proto
= proto
;
2381 client
->instance
= instance
;
2382 client
->session_id
= session_id
;
2384 /* Graceful restart processing for client connect */
2385 zebra_gr_client_reconnect(client
);
2388 if (!client
->synchronous
) {
2389 zsend_capabilities(client
, zvrf
);
2390 zebra_vrf_update_all(client
);
2396 /* Unregister all information in a VRF. */
2397 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2402 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2403 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2404 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2405 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2406 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2407 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
2412 * Validate incoming zapi mpls lsp / labels message
2414 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2420 const struct zapi_nexthop
*znh
;
2422 /* Validate backup info: no duplicates for a single primary */
2423 if (zl
->backup_nexthop_num
== 0) {
2428 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2429 znh
= &zl
->nexthops
[j
];
2431 memset(bits
, 0, sizeof(bits
));
2433 for (i
= 0; i
< znh
->backup_num
; i
++) {
2434 idx
= znh
->backup_idx
[i
] / 32;
2436 ival
= 1 << znh
->backup_idx
[i
] % 32;
2438 /* Check whether value is already used */
2439 if (ival
& bits
[idx
]) {
2442 if (IS_ZEBRA_DEBUG_RECV
)
2443 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2445 znh
->backup_idx
[i
]);
2449 /* Mark index value */
2462 * Handle request to create an MPLS LSP.
2464 * A single message can fully specify an LSP with multiple nexthops.
2466 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2467 * updated to use the received label(s).
2469 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2472 struct zapi_labels zl
;
2474 /* Get input stream. */
2476 if (zapi_labels_decode(s
, &zl
) < 0) {
2477 if (IS_ZEBRA_DEBUG_RECV
)
2478 zlog_debug("%s: Unable to decode zapi_labels sent",
2486 /* Validate; will debug on failure */
2487 if (zapi_labels_validate(&zl
) < 0)
2490 mpls_zapi_labels_process(true, zvrf
, &zl
);
2494 * Handle request to delete an MPLS LSP.
2496 * An LSP is identified by its type and local label. When the received message
2497 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2498 * listed LSP nexthops (aka NHLFEs) are deleted.
2500 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2501 * FEC (route) nexthops are deleted.
2503 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2506 struct zapi_labels zl
;
2508 /* Get input stream. */
2510 if (zapi_labels_decode(s
, &zl
) < 0) {
2511 if (IS_ZEBRA_DEBUG_RECV
)
2512 zlog_debug("%s: Unable to decode zapi_labels sent",
2520 if (zl
.nexthop_num
> 0) {
2521 mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2523 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2525 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2526 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2527 zl
.route
.type
, zl
.route
.instance
);
2532 * Handle request to add an MPLS LSP or change an existing one.
2534 * A single message can fully specify an LSP with multiple nexthops.
2536 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2537 * updated to use the received label(s).
2539 * NOTE: zebra will use route replace semantics (make-before-break) to update
2540 * the LSP in the forwarding plane if that's supported by the underlying
2543 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2546 struct zapi_labels zl
;
2548 /* Get input stream. */
2550 if (zapi_labels_decode(s
, &zl
) < 0) {
2551 if (IS_ZEBRA_DEBUG_RECV
)
2552 zlog_debug("%s: Unable to decode zapi_labels sent",
2560 /* Validate; will debug on failure */
2561 if (zapi_labels_validate(&zl
) < 0)
2564 /* This removes everything, then re-adds from the client's
2565 * zapi message. Since the LSP will be processed later, on this
2566 * this same pthread, all of the changes will 'appear' at once.
2568 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2569 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2570 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2571 zl
.route
.type
, zl
.route
.instance
);
2573 mpls_zapi_labels_process(true, zvrf
, &zl
);
2576 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2579 struct zapi_sr_policy zp
;
2580 struct zapi_srte_tunnel
*zt
;
2581 struct zebra_sr_policy
*policy
;
2583 /* Get input stream. */
2585 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2586 if (IS_ZEBRA_DEBUG_RECV
)
2587 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2591 zt
= &zp
.segment_list
;
2592 if (zt
->label_num
< 1) {
2593 if (IS_ZEBRA_DEBUG_RECV
)
2595 "%s: SR-TE tunnel must contain at least one label",
2603 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2605 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2606 policy
->sock
= client
->sock
;
2608 /* TODO: per-VRF list of SR-TE policies. */
2609 policy
->zvrf
= zvrf
;
2611 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2614 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2617 struct zapi_sr_policy zp
;
2618 struct zebra_sr_policy
*policy
;
2620 /* Get input stream. */
2622 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2623 if (IS_ZEBRA_DEBUG_RECV
)
2624 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2632 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2634 if (IS_ZEBRA_DEBUG_RECV
)
2635 zlog_debug("%s: Unable to find SR-TE policy", __func__
);
2639 zebra_sr_policy_del(policy
);
2642 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2643 char *name
, int status
)
2645 struct zserv
*client
;
2648 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2650 if (IS_ZEBRA_DEBUG_PACKET
)
2652 "Not notifying pathd about policy %s"
2653 " status change to %d",
2658 if (IS_ZEBRA_DEBUG_PACKET
)
2660 "Notifying pathd about policy %s status change"
2664 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2667 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2668 stream_putl(s
, color
);
2669 stream_put_ipaddr(s
, endpoint
);
2670 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2671 stream_putl(s
, status
);
2673 stream_putw_at(s
, 0, stream_get_endp(s
));
2675 return zserv_send_message(client
, s
);
2678 /* Send client close notify to client */
2679 int zsend_client_close_notify(struct zserv
*client
, struct zserv
*closed_client
)
2681 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2683 zclient_create_header(s
, ZEBRA_CLIENT_CLOSE_NOTIFY
, VRF_DEFAULT
);
2685 stream_putc(s
, closed_client
->proto
);
2686 stream_putw(s
, closed_client
->instance
);
2687 stream_putl(s
, closed_client
->session_id
);
2689 stream_putw_at(s
, 0, stream_get_endp(s
));
2691 return zserv_send_message(client
, s
);
2694 int zsend_srv6_manager_get_locator_chunk_response(struct zserv
*client
,
2696 struct srv6_locator
*loc
)
2698 struct srv6_locator_chunk chunk
= {};
2699 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2701 strlcpy(chunk
.locator_name
, loc
->name
, sizeof(chunk
.locator_name
));
2702 chunk
.prefix
= loc
->prefix
;
2703 chunk
.block_bits_length
= loc
->block_bits_length
;
2704 chunk
.node_bits_length
= loc
->node_bits_length
;
2705 chunk
.function_bits_length
= loc
->function_bits_length
;
2706 chunk
.argument_bits_length
= loc
->argument_bits_length
;
2708 chunk
.proto
= client
->proto
;
2709 chunk
.instance
= client
->instance
;
2710 chunk
.flags
= loc
->flags
;
2712 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
, vrf_id
);
2713 zapi_srv6_locator_chunk_encode(s
, &chunk
);
2714 stream_putw_at(s
, 0, stream_get_endp(s
));
2715 return zserv_send_message(client
, s
);
2718 /* Send response to a table manager connect request to client */
2719 static void zread_table_manager_connect(struct zserv
*client
,
2720 struct stream
*msg
, vrf_id_t vrf_id
)
2725 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2730 STREAM_GETC(s
, proto
);
2731 STREAM_GETW(s
, instance
);
2733 /* accept only dynamic routing protocols */
2734 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2735 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2736 "client %d has wrong protocol %s", client
->sock
,
2737 zebra_route_string(proto
));
2738 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2741 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2742 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2743 zebra_route_string(proto
));
2744 client
->proto
= proto
;
2745 client
->instance
= instance
;
2748 * Release previous labels of same protocol and instance.
2749 * This is done in case it restarted from an unexpected shutdown.
2751 release_daemon_table_chunks(client
);
2753 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2759 static void zread_label_manager_connect(struct zserv
*client
,
2760 struct stream
*msg
, vrf_id_t vrf_id
)
2763 /* type of protocol (lib/zebra.h) */
2765 unsigned short instance
;
2767 /* Get input stream. */
2771 STREAM_GETC(s
, proto
);
2772 STREAM_GETW(s
, instance
);
2774 /* accept only dynamic routing protocols */
2775 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2776 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2777 "client %d has wrong protocol %s", client
->sock
,
2778 zebra_route_string(proto
));
2779 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2783 /* recall proto and instance in this socket */
2784 client
->proto
= proto
;
2785 client
->instance
= instance
;
2787 /* call hook for connection using wrapper */
2788 lm_client_connect_call(client
, vrf_id
);
2794 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2799 uint32_t size
, base
;
2800 struct label_manager_chunk
*lmc
= NULL
;
2802 unsigned short instance
;
2804 /* Get input stream. */
2808 STREAM_GETC(s
, proto
);
2809 STREAM_GETW(s
, instance
);
2810 STREAM_GETC(s
, keep
);
2811 STREAM_GETL(s
, size
);
2812 STREAM_GETL(s
, base
);
2814 assert(proto
== client
->proto
&& instance
== client
->instance
);
2816 /* call hook to get a chunk using wrapper */
2817 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2823 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2826 uint32_t start
, end
;
2828 unsigned short instance
;
2830 /* Get input stream. */
2834 STREAM_GETC(s
, proto
);
2835 STREAM_GETW(s
, instance
);
2836 STREAM_GETL(s
, start
);
2837 STREAM_GETL(s
, end
);
2839 assert(proto
== client
->proto
&& instance
== client
->instance
);
2841 /* call hook to release a chunk using wrapper */
2842 lm_release_chunk_call(client
, start
, end
);
2848 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2850 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2851 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2852 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2854 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2855 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2856 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2857 zread_release_label_chunk(client
, msg
);
2861 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2862 struct zebra_vrf
*zvrf
)
2866 struct table_manager_chunk
*tmc
;
2868 /* Get input stream. */
2872 STREAM_GETL(s
, size
);
2874 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
, zvrf
);
2876 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2877 "%s: Unable to assign Table Chunk of size %u",
2880 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2882 /* send response back */
2883 zsend_assign_table_chunk_response(client
, zvrf_id(zvrf
), tmc
);
2889 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
,
2890 struct zebra_vrf
*zvrf
)
2893 uint32_t start
, end
;
2895 /* Get input stream. */
2899 STREAM_GETL(s
, start
);
2900 STREAM_GETL(s
, end
);
2902 release_table_chunk(client
->proto
, client
->instance
, start
, end
, zvrf
);
2908 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2910 /* to avoid sending other messages like ZEBRA_INTERFACE_UP */
2911 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2912 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2914 /* Sanity: don't allow 'unidentified' requests */
2915 if (!client
->proto
) {
2918 "Got SRv6 request from an unidentified client");
2921 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2922 zread_get_table_chunk(client
, msg
, zvrf
);
2923 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2924 zread_release_table_chunk(client
, msg
, zvrf
);
2928 static void zread_srv6_manager_get_locator_chunk(struct zserv
*client
,
2932 struct stream
*s
= msg
;
2934 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2937 STREAM_GETW(s
, len
);
2938 STREAM_GET(locator_name
, s
, len
);
2940 /* call hook to get a chunk using wrapper */
2941 struct srv6_locator
*loc
= NULL
;
2942 srv6_manager_get_locator_chunk_call(&loc
, client
, locator_name
, vrf_id
);
2948 static void zread_srv6_manager_release_locator_chunk(struct zserv
*client
,
2952 struct stream
*s
= msg
;
2954 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2957 STREAM_GETW(s
, len
);
2958 STREAM_GET(locator_name
, s
, len
);
2960 /* call hook to release a chunk using wrapper */
2961 srv6_manager_release_locator_chunk_call(client
, locator_name
, vrf_id
);
2967 static void zread_srv6_manager_request(ZAPI_HANDLER_ARGS
)
2969 switch (hdr
->command
) {
2970 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
:
2971 zread_srv6_manager_get_locator_chunk(client
, msg
,
2974 case ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
:
2975 zread_srv6_manager_release_locator_chunk(client
, msg
,
2979 zlog_err("%s: unknown SRv6 Manager command", __func__
);
2984 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2987 char ifname
[INTERFACE_NAMSIZ
];
2991 union g_addr nexthop
;
2992 uint32_t local_label
;
2993 uint32_t remote_label
;
2995 union pw_protocol_fields data
;
2997 struct zebra_pw
*pw
;
2999 /* Get input stream. */
3003 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3004 ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
3005 STREAM_GETL(s
, ifindex
);
3006 STREAM_GETL(s
, type
);
3010 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
3013 STREAM_GET(&nexthop
.ipv6
, s
, 16);
3018 STREAM_GETL(s
, local_label
);
3019 STREAM_GETL(s
, remote_label
);
3020 STREAM_GETC(s
, flags
);
3021 STREAM_GET(&data
, s
, sizeof(data
));
3022 protocol
= client
->proto
;
3024 pw
= zebra_pw_find(zvrf
, ifname
);
3025 switch (hdr
->command
) {
3028 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
3029 "%s: pseudowire %s already exists [%s]",
3031 zserv_command_string(hdr
->command
));
3035 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
3037 case ZEBRA_PW_DELETE
:
3039 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3040 "%s: pseudowire %s not found [%s]", __func__
,
3041 ifname
, zserv_command_string(hdr
->command
));
3045 zebra_pw_del(zvrf
, pw
);
3048 case ZEBRA_PW_UNSET
:
3050 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3051 "%s: pseudowire %s not found [%s]", __func__
,
3052 ifname
, zserv_command_string(hdr
->command
));
3056 switch (hdr
->command
) {
3060 case ZEBRA_PW_UNSET
:
3065 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
3066 remote_label
, flags
, &data
);
3074 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
3076 struct interface
*master
;
3077 struct interface
*slave
;
3078 struct stream
*s
= msg
;
3082 STREAM_GETL(s
, vrf_id
);
3083 STREAM_GETL(s
, ifindex
);
3084 master
= if_lookup_by_index(ifindex
, vrf_id
);
3086 STREAM_GETL(s
, vrf_id
);
3087 STREAM_GETL(s
, ifindex
);
3088 slave
= if_lookup_by_index(ifindex
, vrf_id
);
3090 if (!master
|| !slave
)
3093 kernel_interface_set_master(master
, slave
);
3100 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
3102 struct interface
*ifp
;
3103 mpls_label_t nlabel
;
3106 struct zebra_vrf
*def_zvrf
;
3107 enum lsp_types_t ltype
;
3110 STREAM_GETL(s
, nlabel
);
3111 STREAM_GETC(s
, afi
);
3113 if (!(IS_VALID_AFI(afi
))) {
3114 zlog_warn("Invalid AFI for VRF label: %u", afi
);
3118 if (nlabel
== zvrf
->label
[afi
]) {
3120 * Nothing to do here move along
3125 STREAM_GETC(s
, ltype
);
3127 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
3128 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
3130 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
3133 zlog_debug("Unable to find specified Interface for %s",
3138 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3140 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
3144 really_remove
= true;
3145 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
3146 if (scrubber
== afi
)
3149 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
3152 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
3153 really_remove
= false;
3159 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
3160 NEXTHOP_TYPE_IFINDEX
, NULL
,
3161 ifp
->ifindex
, false /*backup*/);
3164 if (nlabel
!= MPLS_LABEL_NONE
) {
3165 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
3166 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
3167 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
3170 zvrf
->label
[afi
] = nlabel
;
3171 zvrf
->label_proto
[afi
] = client
->proto
;
3177 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
3179 struct zebra_pbr_rule zpr
;
3182 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
3185 STREAM_GETL(s
, total
);
3187 for (i
= 0; i
< total
; i
++) {
3188 memset(&zpr
, 0, sizeof(zpr
));
3190 zpr
.sock
= client
->sock
;
3191 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
3192 STREAM_GETL(s
, zpr
.rule
.seq
);
3193 STREAM_GETL(s
, zpr
.rule
.priority
);
3194 STREAM_GETL(s
, zpr
.rule
.unique
);
3195 STREAM_GETC(s
, zpr
.rule
.filter
.ip_proto
);
3196 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
3197 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
3198 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
3199 prefix_blen(&zpr
.rule
.filter
.src_ip
));
3200 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
3201 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
3202 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
3203 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
3204 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
3205 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
3206 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
3207 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
3209 STREAM_GETL(s
, zpr
.rule
.action
.queue_id
);
3210 STREAM_GETW(s
, zpr
.rule
.action
.vlan_id
);
3211 STREAM_GETW(s
, zpr
.rule
.action
.vlan_flags
);
3212 STREAM_GETW(s
, zpr
.rule
.action
.pcp
);
3214 STREAM_GETL(s
, zpr
.rule
.action
.table
);
3215 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3217 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
3218 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
3220 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
3221 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
3223 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
3224 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
3226 if (zpr
.rule
.filter
.src_port
)
3227 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3229 if (zpr
.rule
.filter
.dst_port
)
3230 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
3232 if (zpr
.rule
.filter
.dsfield
)
3233 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
3235 if (zpr
.rule
.filter
.ip_proto
)
3236 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_IP_PROTOCOL
;
3238 if (zpr
.rule
.filter
.fwmark
)
3239 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
3241 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
3242 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
3244 "Unsupported PBR source IP family: %s (%hhu)",
3245 family2str(zpr
.rule
.filter
.src_ip
.family
),
3246 zpr
.rule
.filter
.src_ip
.family
);
3249 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
3250 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
3252 "Unsupported PBR destination IP family: %s (%hhu)",
3253 family2str(zpr
.rule
.filter
.dst_ip
.family
),
3254 zpr
.rule
.filter
.dst_ip
.family
);
3259 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
3260 if (hdr
->command
== ZEBRA_RULE_ADD
)
3261 zebra_pbr_add_rule(&zpr
);
3263 zebra_pbr_del_rule(&zpr
);
3270 static inline void zread_tc_qdisc(ZAPI_HANDLER_ARGS
)
3272 struct zebra_tc_qdisc qdisc
;
3277 STREAM_GETL(s
, total
);
3279 for (i
= 0; i
< total
; i
++) {
3280 memset(&qdisc
, 0, sizeof(qdisc
));
3282 qdisc
.sock
= client
->sock
;
3283 STREAM_GETL(s
, qdisc
.qdisc
.ifindex
);
3284 STREAM_GETL(s
, qdisc
.qdisc
.kind
);
3286 if (hdr
->command
== ZEBRA_TC_QDISC_INSTALL
)
3287 zebra_tc_qdisc_install(&qdisc
);
3289 zebra_tc_qdisc_uninstall(&qdisc
);
3296 static inline void zread_tc_class(ZAPI_HANDLER_ARGS
)
3298 struct zebra_tc_class
class;
3303 STREAM_GETL(s
, total
);
3305 for (i
= 0; i
< total
; i
++) {
3306 memset(&class, 0, sizeof(class));
3308 class.sock
= client
->sock
;
3309 STREAM_GETL(s
, class.class.ifindex
);
3310 STREAM_GETL(s
, class.class.handle
);
3311 STREAM_GETL(s
, class.class.kind
);
3312 STREAM_GETQ(s
, class.class.u
.htb
.rate
);
3313 STREAM_GETQ(s
, class.class.u
.htb
.ceil
);
3315 if (hdr
->command
== ZEBRA_TC_CLASS_ADD
)
3316 zebra_tc_class_add(&class);
3318 zebra_tc_class_delete(&class);
3325 static inline void zread_tc_filter(ZAPI_HANDLER_ARGS
)
3327 struct zebra_tc_filter filter
;
3332 STREAM_GETL(s
, total
);
3334 for (i
= 0; i
< total
; i
++) {
3335 memset(&filter
, 0, sizeof(filter
));
3337 filter
.sock
= client
->sock
;
3338 STREAM_GETL(s
, filter
.filter
.ifindex
);
3339 STREAM_GETL(s
, filter
.filter
.handle
);
3340 STREAM_GETL(s
, filter
.filter
.priority
);
3341 STREAM_GETL(s
, filter
.filter
.protocol
);
3342 STREAM_GETL(s
, filter
.filter
.kind
);
3343 switch (filter
.filter
.kind
) {
3344 case TC_FILTER_FLOWER
: {
3345 STREAM_GETL(s
, filter
.filter
.u
.flower
.filter_bm
);
3346 uint32_t filter_bm
= filter
.filter
.u
.flower
.filter_bm
;
3348 if (filter_bm
& TC_FLOWER_IP_PROTOCOL
)
3349 STREAM_GETC(s
, filter
.filter
.u
.flower
.ip_proto
);
3350 if (filter_bm
& TC_FLOWER_SRC_IP
) {
3353 filter
.filter
.u
.flower
.src_ip
.family
);
3354 STREAM_GETC(s
, filter
.filter
.u
.flower
.src_ip
3357 &filter
.filter
.u
.flower
.src_ip
.u
.prefix
,
3359 prefix_blen(&filter
.filter
.u
.flower
3362 if (!(filter
.filter
.u
.flower
.src_ip
.family
==
3364 filter
.filter
.u
.flower
.src_ip
.family
==
3367 "Unsupported TC source IP family: %s (%hhu)",
3369 filter
.filter
.u
.flower
3371 filter
.filter
.u
.flower
.src_ip
3376 if (filter_bm
& TC_FLOWER_SRC_PORT
) {
3378 s
, filter
.filter
.u
.flower
.src_port_min
);
3380 s
, filter
.filter
.u
.flower
.src_port_max
);
3382 if (filter_bm
& TC_FLOWER_DST_IP
) {
3385 filter
.filter
.u
.flower
.dst_ip
.family
);
3386 STREAM_GETC(s
, filter
.filter
.u
.flower
.dst_ip
3389 &filter
.filter
.u
.flower
.dst_ip
.u
.prefix
,
3391 prefix_blen(&filter
.filter
.u
.flower
3393 if (!(filter
.filter
.u
.flower
.dst_ip
.family
==
3395 filter
.filter
.u
.flower
.dst_ip
.family
==
3398 "Unsupported TC destination IP family: %s (%hhu)",
3400 filter
.filter
.u
.flower
3402 filter
.filter
.u
.flower
.dst_ip
3407 if (filter_bm
& TC_FLOWER_DST_PORT
) {
3409 s
, filter
.filter
.u
.flower
.dst_port_min
);
3411 s
, filter
.filter
.u
.flower
.dst_port_max
);
3413 if (filter_bm
& TC_FLOWER_DSFIELD
) {
3414 STREAM_GETC(s
, filter
.filter
.u
.flower
.dsfield
);
3416 s
, filter
.filter
.u
.flower
.dsfield_mask
);
3418 STREAM_GETL(s
, filter
.filter
.u
.flower
.classid
);
3422 case TC_FILTER_FLOW
:
3424 case TC_FILTER_UNSPEC
:
3428 if (hdr
->command
== ZEBRA_TC_FILTER_ADD
)
3429 zebra_tc_filter_add(&filter
);
3431 zebra_tc_filter_delete(&filter
);
3438 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
3440 struct zebra_pbr_ipset zpi
;
3445 STREAM_GETL(s
, total
);
3447 for (i
= 0; i
< total
; i
++) {
3448 memset(&zpi
, 0, sizeof(zpi
));
3450 zpi
.sock
= client
->sock
;
3451 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3452 STREAM_GETL(s
, zpi
.unique
);
3453 STREAM_GETL(s
, zpi
.type
);
3454 STREAM_GETC(s
, zpi
.family
);
3455 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3457 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3458 zebra_pbr_create_ipset(&zpi
);
3460 zebra_pbr_destroy_ipset(&zpi
);
3467 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3469 struct zebra_pbr_ipset_entry zpi
;
3470 struct zebra_pbr_ipset ipset
;
3475 STREAM_GETL(s
, total
);
3477 for (i
= 0; i
< total
; i
++) {
3478 memset(&zpi
, 0, sizeof(zpi
));
3479 memset(&ipset
, 0, sizeof(ipset
));
3481 zpi
.sock
= client
->sock
;
3482 STREAM_GETL(s
, zpi
.unique
);
3483 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3484 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3485 STREAM_GETC(s
, zpi
.src
.family
);
3486 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3487 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3488 STREAM_GETC(s
, zpi
.dst
.family
);
3489 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3490 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3492 STREAM_GETW(s
, zpi
.src_port_min
);
3493 STREAM_GETW(s
, zpi
.src_port_max
);
3494 STREAM_GETW(s
, zpi
.dst_port_min
);
3495 STREAM_GETW(s
, zpi
.dst_port_max
);
3496 STREAM_GETC(s
, zpi
.proto
);
3497 if (!is_default_prefix(&zpi
.src
))
3498 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3500 if (!is_default_prefix(&zpi
.dst
))
3501 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3502 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3503 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3504 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3505 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3506 if (zpi
.dst_port_max
!= 0)
3507 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3508 if (zpi
.src_port_max
!= 0)
3509 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3511 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3513 if (!(zpi
.dst
.family
== AF_INET
3514 || zpi
.dst
.family
== AF_INET6
)) {
3516 "Unsupported PBR destination IP family: %s (%hhu)",
3517 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3518 goto stream_failure
;
3520 if (!(zpi
.src
.family
== AF_INET
3521 || zpi
.src
.family
== AF_INET6
)) {
3523 "Unsupported PBR source IP family: %s (%hhu)",
3524 family2str(zpi
.src
.family
), zpi
.src
.family
);
3525 goto stream_failure
;
3528 /* calculate backpointer */
3530 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3532 if (!zpi
.backpointer
) {
3533 zlog_warn("ipset name specified: %s does not exist",
3535 goto stream_failure
;
3538 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3539 zebra_pbr_add_ipset_entry(&zpi
);
3541 zebra_pbr_del_ipset_entry(&zpi
);
3549 static inline void zebra_neigh_register(ZAPI_HANDLER_ARGS
)
3553 STREAM_GETW(msg
, afi
);
3554 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3556 "Invalid AFI %u while registering for neighbors notifications",
3558 goto stream_failure
;
3560 vrf_bitmap_set(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3565 static inline void zebra_neigh_unregister(ZAPI_HANDLER_ARGS
)
3569 STREAM_GETW(msg
, afi
);
3570 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3572 "Invalid AFI %u while unregistering from neighbor notifications",
3574 goto stream_failure
;
3576 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3581 static inline void zebra_gre_get(ZAPI_HANDLER_ARGS
)
3585 struct interface
*ifp
;
3586 struct zebra_if
*zebra_if
= NULL
;
3587 struct zebra_l2info_gre
*gre_info
;
3588 struct interface
*ifp_link
= NULL
;
3589 vrf_id_t vrf_id_link
= VRF_UNKNOWN
;
3590 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3593 STREAM_GETL(s
, idx
);
3594 ifp
= if_lookup_by_index(idx
, vrf_id
);
3597 zebra_if
= ifp
->info
;
3599 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3601 zclient_create_header(s
, ZEBRA_GRE_UPDATE
, vrf_id
);
3603 if (ifp
&& IS_ZEBRA_IF_GRE(ifp
) && zebra_if
) {
3604 gre_info
= &zebra_if
->l2info
.gre
;
3606 stream_putl(s
, idx
);
3607 stream_putl(s
, gre_info
->ikey
);
3608 stream_putl(s
, gre_info
->ikey
);
3609 stream_putl(s
, gre_info
->ifindex_link
);
3611 ifp_link
= if_lookup_by_index_per_ns(
3612 zebra_ns_lookup(gre_info
->link_nsid
),
3613 gre_info
->ifindex_link
);
3615 vrf_id_link
= ifp_link
->vrf
->vrf_id
;
3616 stream_putl(s
, vrf_id_link
);
3617 stream_putl(s
, gre_info
->vtep_ip
.s_addr
);
3618 stream_putl(s
, gre_info
->vtep_ip_remote
.s_addr
);
3620 stream_putl(s
, idx
);
3623 stream_putl(s
, IFINDEX_INTERNAL
);
3624 stream_putl(s
, VRF_UNKNOWN
);
3628 /* Write packet size. */
3629 stream_putw_at(s
, 0, stream_get_endp(s
));
3630 zserv_send_message(client
, s
);
3637 static inline void zebra_configure_arp(ZAPI_HANDLER_ARGS
)
3642 struct interface
*ifp
;
3645 STREAM_GETC(s
, fam
);
3646 if (fam
!= AF_INET
&& fam
!= AF_INET6
)
3648 STREAM_GETL(s
, idx
);
3649 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, idx
);
3652 dplane_neigh_table_update(ifp
, fam
, 1, 0, 0);
3657 static inline void zebra_neigh_ip_add(ZAPI_HANDLER_ARGS
)
3660 struct zapi_neigh_ip api
= {};
3662 const struct interface
*ifp
;
3665 ret
= zclient_neigh_ip_decode(s
, &api
);
3668 ifp
= if_lookup_by_index(api
.index
, zvrf_id(zvrf
));
3671 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_INSTALL
, ifp
, &api
.ip_out
,
3672 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3676 static inline void zebra_neigh_ip_del(ZAPI_HANDLER_ARGS
)
3679 struct zapi_neigh_ip api
= {};
3681 struct interface
*ifp
;
3684 ret
= zclient_neigh_ip_decode(s
, &api
);
3687 ifp
= if_lookup_by_index(api
.index
, zvrf_id(zvrf
));
3690 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_DELETE
, ifp
, &api
.ip_out
,
3691 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3695 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3697 struct zebra_pbr_iptable
*zpi
=
3698 XCALLOC(MTYPE_PBR_OBJ
, sizeof(struct zebra_pbr_iptable
));
3703 zpi
->interface_name_list
= list_new();
3704 zpi
->sock
= client
->sock
;
3705 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3706 STREAM_GETL(s
, zpi
->unique
);
3707 STREAM_GETL(s
, zpi
->type
);
3708 STREAM_GETL(s
, zpi
->filter_bm
);
3709 STREAM_GETL(s
, zpi
->action
);
3710 STREAM_GETL(s
, zpi
->fwmark
);
3711 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3712 STREAM_GETC(s
, zpi
->family
);
3713 STREAM_GETW(s
, zpi
->pkt_len_min
);
3714 STREAM_GETW(s
, zpi
->pkt_len_max
);
3715 STREAM_GETW(s
, zpi
->tcp_flags
);
3716 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3717 STREAM_GETC(s
, zpi
->dscp_value
);
3718 STREAM_GETC(s
, zpi
->fragment
);
3719 STREAM_GETC(s
, zpi
->protocol
);
3720 STREAM_GETW(s
, zpi
->flow_label
);
3721 STREAM_GETL(s
, zpi
->nb_interface
);
3722 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3724 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3725 zebra_pbr_add_iptable(zpi
);
3727 zebra_pbr_del_iptable(zpi
);
3730 zebra_pbr_iptable_free(zpi
);
3735 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3739 struct interface
*ifp
;
3745 STREAM_GETL(s
, ifindex
);
3747 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3749 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3753 STREAM_GETC(s
, p
.family
);
3754 STREAM_GETC(s
, p
.prefixlen
);
3755 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3757 if (p
.family
== AF_INET
)
3762 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3764 dplane_neigh_discover(ifp
, &ip
);
3770 static inline void zebra_gre_source_set(ZAPI_HANDLER_ARGS
)
3773 ifindex_t idx
, link_idx
;
3774 vrf_id_t link_vrf_id
;
3775 struct interface
*ifp
;
3776 struct interface
*ifp_link
;
3777 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3778 struct zebra_if
*zif
, *gre_zif
;
3779 struct zebra_l2info_gre
*gre_info
;
3783 STREAM_GETL(s
, idx
);
3784 ifp
= if_lookup_by_index(idx
, vrf_id
);
3785 STREAM_GETL(s
, link_idx
);
3786 STREAM_GETL(s
, link_vrf_id
);
3787 STREAM_GETL(s
, mtu
);
3789 ifp_link
= if_lookup_by_index(link_idx
, link_vrf_id
);
3790 if (!ifp_link
|| !ifp
) {
3791 zlog_warn("GRE (index %u, VRF %u) or GRE link interface (index %u, VRF %u) not found, when setting GRE params",
3792 idx
, vrf_id
, link_idx
, link_vrf_id
);
3796 if (!IS_ZEBRA_IF_GRE(ifp
))
3799 gre_zif
= (struct zebra_if
*)ifp
->info
;
3800 zif
= (struct zebra_if
*)ifp_link
->info
;
3801 if (!zif
|| !gre_zif
)
3804 gre_info
= &zif
->l2info
.gre
;
3811 /* if gre link already set or mtu did not change, do not set it */
3812 if (gre_zif
->link
&& gre_zif
->link
== ifp_link
&& mtu
== ifp
->mtu
)
3815 dplane_gre_set(ifp
, ifp_link
, mtu
, gre_info
);
3821 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3822 struct zmsghdr
*bad_hdr
)
3825 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3827 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3829 zserv_encode_error(s
, error
);
3831 client
->error_cnt
++;
3832 zserv_send_message(client
, s
);
3835 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3837 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3838 zlog_debug("ZAPI message specifies unknown VRF: %d",
3841 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3844 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3846 zlog_info("Zebra received unknown command %d", hdr
->command
);
3848 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3851 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3852 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3853 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3854 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3855 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3856 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3857 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3858 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3859 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3860 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3861 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3862 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3863 [ZEBRA_NEXTHOP_LOOKUP_MRIB
] = zread_nexthop_lookup_mrib
,
3864 [ZEBRA_HELLO
] = zread_hello
,
3865 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3866 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3867 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3868 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3869 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3871 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3872 #endif /* HAVE_BFDD */
3873 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3874 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3875 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3876 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3877 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3878 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3879 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3880 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3881 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3882 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3883 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3884 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3885 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3886 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3887 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3888 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3889 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3890 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3891 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3892 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3893 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3894 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3895 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3896 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add_zapi
,
3897 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del_zapi
,
3898 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3899 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3900 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3901 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3902 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3903 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3904 [ZEBRA_PW_SET
] = zread_pseudowire
,
3905 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3906 [ZEBRA_RULE_ADD
] = zread_rule
,
3907 [ZEBRA_RULE_DELETE
] = zread_rule
,
3908 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3909 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3910 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3911 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3912 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3913 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3914 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3915 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3916 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3917 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3918 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3919 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3920 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3921 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3922 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3923 [ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3924 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3925 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3926 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3927 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3928 [ZEBRA_ROUTE_NOTIFY_REQUEST
] = zread_route_notify_request
,
3929 [ZEBRA_EVPN_REMOTE_NH_ADD
] = zebra_evpn_proc_remote_nh
,
3930 [ZEBRA_EVPN_REMOTE_NH_DEL
] = zebra_evpn_proc_remote_nh
,
3931 [ZEBRA_NEIGH_IP_ADD
] = zebra_neigh_ip_add
,
3932 [ZEBRA_NEIGH_IP_DEL
] = zebra_neigh_ip_del
,
3933 [ZEBRA_NHRP_NEIGH_REGISTER
] = zebra_neigh_register
,
3934 [ZEBRA_NHRP_NEIGH_UNREGISTER
] = zebra_neigh_unregister
,
3935 [ZEBRA_CONFIGURE_ARP
] = zebra_configure_arp
,
3936 [ZEBRA_GRE_GET
] = zebra_gre_get
,
3937 [ZEBRA_GRE_SOURCE_SET
] = zebra_gre_source_set
,
3938 [ZEBRA_TC_QDISC_INSTALL
] = zread_tc_qdisc
,
3939 [ZEBRA_TC_QDISC_UNINSTALL
] = zread_tc_qdisc
,
3940 [ZEBRA_TC_CLASS_ADD
] = zread_tc_class
,
3941 [ZEBRA_TC_CLASS_DELETE
] = zread_tc_class
,
3942 [ZEBRA_TC_FILTER_ADD
] = zread_tc_filter
,
3943 [ZEBRA_TC_FILTER_DELETE
] = zread_tc_filter
,
3947 * Process a batch of zapi messages.
3949 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3952 struct zebra_vrf
*zvrf
;
3954 struct stream_fifo temp_fifo
;
3956 stream_fifo_init(&temp_fifo
);
3958 while (stream_fifo_head(fifo
)) {
3959 msg
= stream_fifo_pop(fifo
);
3961 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3962 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3964 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3965 STREAM_READABLE(msg
),
3966 ZEBRA_MAX_PACKET_SIZ
);
3970 zapi_parse_header(msg
, &hdr
);
3972 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3973 && IS_ZEBRA_DEBUG_DETAIL
)
3974 zserv_log_message(NULL
, msg
, &hdr
);
3976 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3978 /* Before checking for a handler function, check for
3979 * special messages that are handled in another module;
3980 * we'll treat these as opaque.
3982 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3983 /* Reset message buffer */
3984 stream_set_getp(msg
, 0);
3986 stream_fifo_push(&temp_fifo
, msg
);
3988 /* Continue without freeing the message */
3994 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3996 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
4000 if (hdr
.command
>= array_size(zserv_handlers
)
4001 || zserv_handlers
[hdr
.command
] == NULL
) {
4002 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
4006 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
4012 /* Dispatch any special messages from the temp fifo */
4013 if (stream_fifo_head(&temp_fifo
) != NULL
)
4014 zebra_opaque_enqueue_batch(&temp_fifo
);
4016 stream_fifo_deinit(&temp_fifo
);