2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/stream.h"
28 #include "lib/memory.h"
29 #include "lib/table.h"
30 #include "lib/network.h"
32 #include "lib/zclient.h"
33 #include "lib/privs.h"
34 #include "lib/nexthop.h"
36 #include "lib/libfrr.h"
37 #include "lib/lib_errors.h"
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
41 #include "zebra/zebra_ns.h"
42 #include "zebra/zebra_vrf.h"
43 #include "zebra/router-id.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_rnh.h"
47 #include "zebra/interface.h"
48 #include "zebra/zebra_ptm.h"
49 #include "zebra/rtadv.h"
50 #include "zebra/zebra_mpls.h"
51 #include "zebra/zebra_mroute.h"
52 #include "zebra/zebra_vxlan.h"
53 #include "zebra/zebra_evpn_mh.h"
55 #include "zebra/zebra_pbr.h"
56 #include "zebra/table_manager.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_errors.h"
59 #include "zebra/zebra_mlag.h"
60 #include "zebra/connected.h"
61 #include "zebra/zebra_opaque.h"
62 #include "zebra/zebra_srte.h"
63 #include "zebra/zebra_srv6.h"
65 DEFINE_MTYPE_STATIC(ZEBRA
, OPAQUE
, "Opaque Data");
67 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
);
69 /* Encoding helpers -------------------------------------------------------- */
71 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
73 /* Interface information. */
74 struct zebra_if
*zif
= ifp
->info
;
76 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
77 stream_putl(s
, ifp
->ifindex
);
78 stream_putc(s
, ifp
->status
);
79 stream_putq(s
, ifp
->flags
);
80 stream_putc(s
, ifp
->ptm_enable
);
81 stream_putc(s
, ifp
->ptm_status
);
82 stream_putl(s
, ifp
->metric
);
83 stream_putl(s
, ifp
->speed
);
84 stream_putl(s
, ifp
->mtu
);
85 stream_putl(s
, ifp
->mtu6
);
86 stream_putl(s
, ifp
->bandwidth
);
87 stream_putl(s
, zif
->link_ifindex
);
88 stream_putl(s
, ifp
->ll_type
);
89 stream_putl(s
, ifp
->hw_addr_len
);
91 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
93 /* Then, Traffic Engineering parameters if any */
94 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
96 zebra_interface_link_params_write(s
, ifp
);
100 /* Write packet size. */
101 stream_putw_at(s
, 0, stream_get_endp(s
));
104 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
106 struct vrf_data data
;
107 const char *netns_name
= zvrf_ns_name(zvrf
);
109 memset(&data
, 0, sizeof(data
));
110 data
.l
.table_id
= zvrf
->table_id
;
113 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
116 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
117 /* Pass the tableid and the netns NAME */
118 stream_put(s
, &data
, sizeof(struct vrf_data
));
119 /* Interface information. */
120 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
121 /* Write packet size. */
122 stream_putw_at(s
, 0, stream_get_endp(s
));
125 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
127 stream_putl(s
, nexthop
->vrf_id
);
128 stream_putc(s
, nexthop
->type
);
129 switch (nexthop
->type
) {
130 case NEXTHOP_TYPE_IPV4
:
131 case NEXTHOP_TYPE_IPV4_IFINDEX
:
132 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
133 stream_putl(s
, nexthop
->ifindex
);
135 case NEXTHOP_TYPE_IPV6
:
136 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
138 case NEXTHOP_TYPE_IPV6_IFINDEX
:
139 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
140 stream_putl(s
, nexthop
->ifindex
);
142 case NEXTHOP_TYPE_IFINDEX
:
143 stream_putl(s
, nexthop
->ifindex
);
153 * Zebra error addition adds error type.
157 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * | enum zebra_error_types |
160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
165 stream_put(s
, &error
, sizeof(error
));
167 /* Write packet size. */
168 stream_putw_at(s
, 0, stream_get_endp(s
));
171 /* Send handlers ----------------------------------------------------------- */
173 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
175 * This function is called in the following situations:
176 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
178 * - at startup, when zebra figures out the available interfaces
179 * - when an interface is added (where support for
180 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
181 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
184 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
186 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
188 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
189 zserv_encode_interface(s
, ifp
);
192 return zserv_send_message(client
, s
);
195 /* Interface deletion from zebra daemon. */
196 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
198 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
200 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
201 zserv_encode_interface(s
, ifp
);
204 return zserv_send_message(client
, s
);
207 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
209 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
211 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
212 zserv_encode_vrf(s
, zvrf
);
214 client
->vrfadd_cnt
++;
215 return zserv_send_message(client
, s
);
218 /* VRF deletion from zebra daemon. */
219 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
222 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
224 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
225 zserv_encode_vrf(s
, zvrf
);
227 client
->vrfdel_cnt
++;
228 return zserv_send_message(client
, s
);
231 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
233 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
235 if (!ifp
->link_params
) {
240 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
242 /* Add Interface Index */
243 stream_putl(s
, ifp
->ifindex
);
245 /* Then TE Link Parameters */
246 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
251 /* Write packet size. */
252 stream_putw_at(s
, 0, stream_get_endp(s
));
254 return zserv_send_message(client
, s
);
257 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
258 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
260 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
261 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
262 * from the client, after the ZEBRA_INTERFACE_ADD has been
263 * sent from zebra to the client
264 * - redistribute new address info to all clients in the following situations
265 * - at startup, when zebra figures out the available interfaces
266 * - when an interface is added (where support for
267 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
268 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
270 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
271 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
272 * - when an RTM_NEWADDR message is received from the kernel,
274 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
276 * zsend_interface_address(DELETE)
279 * zebra_interface_address_delete_update
281 * | | if_delete_update
283 * ip_address_uninstall connected_delete_ipv4
284 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
287 * | RTM_NEWADDR on routing/netlink socket
290 * "no ip address A.B.C.D/M [label LINE]"
291 * "no ip address A.B.C.D/M"
292 * ["no ipv6 address X:X::X:X/M"]
295 int zsend_interface_address(int cmd
, struct zserv
*client
,
296 struct interface
*ifp
, struct connected
*ifc
)
300 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
302 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
303 stream_putl(s
, ifp
->ifindex
);
305 /* Interface address flag. */
306 stream_putc(s
, ifc
->flags
);
308 /* Prefix information. */
310 stream_putc(s
, p
->family
);
311 blen
= prefix_blen(p
);
312 stream_put(s
, &p
->u
.prefix
, blen
);
315 * XXX gnu version does not send prefixlen for
316 * ZEBRA_INTERFACE_ADDRESS_DELETE
317 * but zebra_interface_address_delete_read() in the gnu version
320 stream_putc(s
, p
->prefixlen
);
323 p
= ifc
->destination
;
325 stream_put(s
, &p
->u
.prefix
, blen
);
327 stream_put(s
, NULL
, blen
);
329 /* Write packet size. */
330 stream_putw_at(s
, 0, stream_get_endp(s
));
332 client
->connected_rt_add_cnt
++;
333 return zserv_send_message(client
, s
);
336 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
337 struct interface
*ifp
,
338 struct nbr_connected
*ifc
)
341 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
344 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
345 stream_putl(s
, ifp
->ifindex
);
347 /* Prefix information. */
349 stream_putc(s
, p
->family
);
350 blen
= prefix_blen(p
);
351 stream_put(s
, &p
->u
.prefix
, blen
);
354 * XXX gnu version does not send prefixlen for
355 * ZEBRA_INTERFACE_ADDRESS_DELETE
356 * but zebra_interface_address_delete_read() in the gnu version
359 stream_putc(s
, p
->prefixlen
);
361 /* Write packet size. */
362 stream_putw_at(s
, 0, stream_get_endp(s
));
364 return zserv_send_message(client
, s
);
367 /* Interface address addition. */
368 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
369 struct nbr_connected
*ifc
)
371 struct listnode
*node
, *nnode
;
372 struct zserv
*client
;
375 if (IS_ZEBRA_DEBUG_EVENT
) {
376 char buf
[INET6_ADDRSTRLEN
];
380 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
381 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
383 p
->prefixlen
, ifc
->ifp
->name
);
386 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
387 /* Do not send unsolicited messages to synchronous clients. */
388 if (client
->synchronous
)
391 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
396 /* Interface address deletion. */
397 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
398 struct nbr_connected
*ifc
)
400 struct listnode
*node
, *nnode
;
401 struct zserv
*client
;
404 if (IS_ZEBRA_DEBUG_EVENT
) {
405 char buf
[INET6_ADDRSTRLEN
];
409 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
410 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
412 p
->prefixlen
, ifc
->ifp
->name
);
415 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
416 /* Do not send unsolicited messages to synchronous clients. */
417 if (client
->synchronous
)
420 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
425 /* Send addresses on interface to client */
426 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
428 struct listnode
*cnode
, *cnnode
;
430 struct nbr_connected
*nc
;
432 /* Send interface addresses. */
433 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
434 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
437 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
443 /* Send interface neighbors. */
444 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
445 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
454 /* Notify client about interface moving from one VRF to another.
455 * Whether client is interested in old and new VRF is checked by caller.
457 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
460 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
462 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
464 /* Fill in the name of the interface and its new VRF (id) */
465 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
466 stream_putl(s
, vrf_id
);
468 /* Write packet size. */
469 stream_putw_at(s
, 0, stream_get_endp(s
));
471 client
->if_vrfchg_cnt
++;
472 return zserv_send_message(client
, s
);
475 /* Add new nbr connected IPv6 address */
476 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
478 struct nbr_connected
*ifc
;
482 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
483 p
.prefixlen
= IPV6_MAX_BITLEN
;
485 ifc
= listnode_head(ifp
->nbr_connected
);
488 ifc
= nbr_connected_new();
489 ifc
->address
= prefix_new();
491 listnode_add(ifp
->nbr_connected
, ifc
);
494 prefix_copy(ifc
->address
, &p
);
496 zebra_interface_nbr_address_add_update(ifp
, ifc
);
498 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
501 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
503 struct nbr_connected
*ifc
;
507 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
508 p
.prefixlen
= IPV6_MAX_BITLEN
;
510 ifc
= nbr_connected_check(ifp
, &p
);
514 listnode_delete(ifp
->nbr_connected
, ifc
);
516 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
518 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
520 nbr_connected_free(ifc
);
524 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
525 * ZEBRA_INTERFACE_DOWN.
527 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
528 * the clients in one of 2 situations:
529 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
530 * - a vty command modifying the bandwidth of an interface is received.
531 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
533 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
535 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
537 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
538 zserv_encode_interface(s
, ifp
);
540 if (cmd
== ZEBRA_INTERFACE_UP
)
543 client
->ifdown_cnt
++;
545 return zserv_send_message(client
, s
);
548 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
549 const struct prefix
*p
,
550 const struct prefix
*src_p
,
551 const struct route_entry
*re
)
553 struct zapi_route api
;
554 struct zapi_nexthop
*api_nh
;
555 struct nexthop
*nexthop
;
559 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
561 memset(&api
, 0, sizeof(api
));
562 api
.vrf_id
= re
->vrf_id
;
564 api
.safi
= SAFI_UNICAST
;
565 api
.instance
= re
->instance
;
566 api
.flags
= re
->flags
;
568 afi
= family2afi(p
->family
);
571 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
572 client
->redist_v4_add_cnt
++;
574 client
->redist_v4_del_cnt
++;
577 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
578 client
->redist_v6_add_cnt
++;
580 client
->redist_v6_del_cnt
++;
589 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
590 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
593 for (nexthop
= re
->nhe
->nhg
.nexthop
;
594 nexthop
; nexthop
= nexthop
->next
) {
595 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
598 api_nh
= &api
.nexthops
[count
];
599 api_nh
->vrf_id
= nexthop
->vrf_id
;
600 api_nh
->type
= nexthop
->type
;
601 api_nh
->weight
= nexthop
->weight
;
602 switch (nexthop
->type
) {
603 case NEXTHOP_TYPE_BLACKHOLE
:
604 api_nh
->bh_type
= nexthop
->bh_type
;
606 case NEXTHOP_TYPE_IPV4
:
607 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
609 case NEXTHOP_TYPE_IPV4_IFINDEX
:
610 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
611 api_nh
->ifindex
= nexthop
->ifindex
;
613 case NEXTHOP_TYPE_IFINDEX
:
614 api_nh
->ifindex
= nexthop
->ifindex
;
616 case NEXTHOP_TYPE_IPV6
:
617 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
619 case NEXTHOP_TYPE_IPV6_IFINDEX
:
620 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
621 api_nh
->ifindex
= nexthop
->ifindex
;
628 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
629 api
.nexthop_num
= count
;
633 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
634 api
.distance
= re
->distance
;
635 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
636 api
.metric
= re
->metric
;
638 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
641 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
644 struct stream
*s
= stream_new(stream_size
);
646 /* Encode route and send. */
647 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
652 if (IS_ZEBRA_DEBUG_SEND
)
653 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
654 __func__
, zserv_command_string(cmd
),
655 zebra_route_string(client
->proto
),
656 zebra_route_string(api
.type
), api
.vrf_id
,
658 return zserv_send_message(client
, s
);
662 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
663 * nexthop is not found on mrib. Returns both route metric and protocol
666 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
668 struct route_entry
*re
,
669 struct zebra_vrf
*zvrf
)
674 struct nexthop
*nexthop
;
676 /* Get output stream. */
677 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
680 /* Fill in result. */
681 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
682 stream_put_in_addr(s
, &addr
);
685 struct nexthop_group
*nhg
;
687 stream_putc(s
, re
->distance
);
688 stream_putl(s
, re
->metric
);
690 /* remember position for nexthop_num */
691 nump
= stream_get_endp(s
);
692 /* reserve room for nexthop_num */
694 nhg
= rib_get_fib_nhg(re
);
695 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
696 if (rnh_nexthop_valid(re
, nexthop
))
697 num
+= zserv_encode_nexthop(s
, nexthop
);
700 /* store nexthop_num */
701 stream_putc_at(s
, nump
, num
);
703 stream_putc(s
, 0); /* distance */
704 stream_putl(s
, 0); /* metric */
705 stream_putc(s
, 0); /* nexthop_num */
708 stream_putw_at(s
, 0, stream_get_endp(s
));
710 return zserv_send_message(client
, s
);
713 int zsend_nhg_notify(uint16_t type
, uint16_t instance
, uint32_t session_id
,
714 uint32_t id
, enum zapi_nhg_notify_owner note
)
716 struct zserv
*client
;
719 client
= zserv_find_client_session(type
, instance
, session_id
);
721 if (IS_ZEBRA_DEBUG_PACKET
) {
722 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
723 type
, instance
, id
, note
);
728 if (IS_ZEBRA_DEBUG_SEND
)
729 zlog_debug("%s: type %d, id %d, note %s",
730 __func__
, type
, id
, zapi_nhg_notify_owner2str(note
));
732 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
735 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
737 stream_put(s
, ¬e
, sizeof(note
));
740 stream_putw_at(s
, 0, stream_get_endp(s
));
742 return zserv_send_message(client
, s
);
746 * Common utility send route notification, called from a path using a
747 * route_entry and from a path using a dataplane context.
749 static int route_notify_internal(const struct prefix
*p
, int type
,
750 uint16_t instance
, vrf_id_t vrf_id
,
752 enum zapi_route_notify_owner note
,
753 afi_t afi
, safi_t safi
)
755 struct zserv
*client
;
759 client
= zserv_find_client(type
, instance
);
760 if (!client
|| !client
->notify_owner
) {
761 if (IS_ZEBRA_DEBUG_PACKET
)
763 "Not Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
764 zebra_route_string(type
), p
, table_id
, note
,
769 if (IS_ZEBRA_DEBUG_PACKET
)
771 "Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
772 zebra_route_string(type
), p
, table_id
, note
, vrf_id
);
774 /* We're just allocating a small-ish buffer here, since we only
775 * encode a small amount of data.
777 s
= stream_new(ZEBRA_SMALL_PACKET_SIZE
);
781 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
783 stream_put(s
, ¬e
, sizeof(note
));
785 stream_putc(s
, p
->family
);
787 blen
= prefix_blen(p
);
788 stream_putc(s
, p
->prefixlen
);
789 stream_put(s
, &p
->u
.prefix
, blen
);
791 stream_putl(s
, table_id
);
793 /* Encode AFI, SAFI in the message */
795 stream_putc(s
, safi
);
797 stream_putw_at(s
, 0, stream_get_endp(s
));
799 return zserv_send_message(client
, s
);
802 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
803 enum zapi_route_notify_owner note
,
804 afi_t afi
, safi_t safi
)
806 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
807 re
->table
, note
, afi
, safi
));
811 * Route-owner notification using info from dataplane update context.
813 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
814 enum zapi_route_notify_owner note
)
816 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
817 dplane_ctx_get_type(ctx
),
818 dplane_ctx_get_instance(ctx
),
819 dplane_ctx_get_vrf(ctx
),
820 dplane_ctx_get_table(ctx
),
822 dplane_ctx_get_afi(ctx
),
823 dplane_ctx_get_safi(ctx
)));
826 static void zread_route_notify_request(ZAPI_HANDLER_ARGS
)
830 STREAM_GETC(msg
, notify
);
831 client
->notify_owner
= notify
;
836 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
837 enum zapi_rule_notify_owner note
)
839 struct listnode
*node
;
840 struct zserv
*client
;
843 if (IS_ZEBRA_DEBUG_PACKET
)
844 zlog_debug("%s: Notifying %u", __func__
,
845 dplane_ctx_rule_get_unique(ctx
));
847 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
848 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
855 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
857 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
858 stream_put(s
, ¬e
, sizeof(note
));
859 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
860 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
861 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
862 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
864 stream_putw_at(s
, 0, stream_get_endp(s
));
866 zserv_send_message(client
, s
);
869 void zsend_iptable_notify_owner(const struct zebra_dplane_ctx
*ctx
,
870 enum zapi_iptable_notify_owner note
)
872 struct listnode
*node
;
873 struct zserv
*client
;
875 struct zebra_pbr_iptable ipt
;
876 uint16_t cmd
= ZEBRA_IPTABLE_NOTIFY_OWNER
;
878 if (!dplane_ctx_get_pbr_iptable(ctx
, &ipt
))
881 if (IS_ZEBRA_DEBUG_PACKET
)
882 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
883 zserv_command_string(cmd
), ipt
.unique
, note
);
885 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
886 if (ipt
.sock
== client
->sock
)
893 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
895 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
896 stream_put(s
, ¬e
, sizeof(note
));
897 stream_putl(s
, ipt
.unique
);
898 stream_put(s
, ipt
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
899 stream_putw_at(s
, 0, stream_get_endp(s
));
901 zserv_send_message(client
, s
);
904 void zsend_ipset_notify_owner(const struct zebra_dplane_ctx
*ctx
,
905 enum zapi_ipset_notify_owner note
)
907 struct listnode
*node
;
908 struct zserv
*client
;
910 struct zebra_pbr_ipset ipset
;
911 uint16_t cmd
= ZEBRA_IPSET_NOTIFY_OWNER
;
913 if (!dplane_ctx_get_pbr_ipset(ctx
, &ipset
))
916 if (IS_ZEBRA_DEBUG_PACKET
)
917 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
918 zserv_command_string(cmd
), ipset
.unique
, note
);
920 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
921 if (ipset
.sock
== client
->sock
)
928 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
930 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
931 stream_put(s
, ¬e
, sizeof(note
));
932 stream_putl(s
, ipset
.unique
);
933 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
934 stream_putw_at(s
, 0, stream_get_endp(s
));
936 zserv_send_message(client
, s
);
939 void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx
*ctx
,
940 enum zapi_ipset_entry_notify_owner note
)
942 struct listnode
*node
;
943 struct zserv
*client
;
945 struct zebra_pbr_ipset_entry ipent
;
946 struct zebra_pbr_ipset ipset
;
947 uint16_t cmd
= ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
;
949 if (!dplane_ctx_get_pbr_ipset_entry(ctx
, &ipent
))
951 if (!dplane_ctx_get_pbr_ipset(ctx
, &ipset
))
954 if (IS_ZEBRA_DEBUG_PACKET
)
955 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
956 zserv_command_string(cmd
), ipent
.unique
, note
);
958 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
959 if (ipent
.sock
== client
->sock
)
966 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
968 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
969 stream_put(s
, ¬e
, sizeof(note
));
970 stream_putl(s
, ipent
.unique
);
971 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
972 stream_putw_at(s
, 0, stream_get_endp(s
));
974 zserv_send_message(client
, s
);
977 void zsend_nhrp_neighbor_notify(int cmd
, struct interface
*ifp
,
978 struct ipaddr
*ipaddr
, int ndm_state
,
979 union sockunion
*link_layer_ipv4
)
982 struct listnode
*node
, *nnode
;
983 struct zserv
*client
;
987 if (IS_ZEBRA_DEBUG_PACKET
)
988 zlog_debug("%s: Notifying Neighbor entry (%u)", __func__
, cmd
);
990 sockunion_family(&ip
) = ipaddr_family(ipaddr
);
991 afi
= family2afi(sockunion_family(&ip
));
992 memcpy((char *)sockunion_get_addr(&ip
), &ipaddr
->ip
.addr
,
993 family2addrsize(sockunion_family(&ip
)));
995 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
996 if (!vrf_bitmap_check(client
->nhrp_neighinfo
[afi
], ifp
->vrf_id
))
999 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1000 zclient_neigh_ip_encode(s
, cmd
, &ip
, link_layer_ipv4
, ifp
,
1002 stream_putw_at(s
, 0, stream_get_endp(s
));
1003 zserv_send_message(client
, s
);
1008 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
1009 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
1015 /* Check this client need interface information. */
1016 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
1019 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1022 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1024 /* Prefix information. */
1025 stream_putc(s
, p
->family
);
1026 blen
= prefix_blen(p
);
1027 stream_put(s
, &p
->u
.prefix
, blen
);
1028 stream_putc(s
, p
->prefixlen
);
1030 /* Write packet size. */
1031 stream_putw_at(s
, 0, stream_get_endp(s
));
1033 return zserv_send_message(client
, s
);
1037 * Function used by Zebra to send a PW status update to LDP daemon
1039 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1041 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1043 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1044 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1045 stream_putl(s
, pw
->ifindex
);
1046 stream_putl(s
, pw
->status
);
1048 /* Put length at the first point of the stream. */
1049 stream_putw_at(s
, 0, stream_get_endp(s
));
1051 return zserv_send_message(client
, s
);
1054 /* Send response to a get label chunk request to client */
1055 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
1056 struct label_manager_chunk
*lmc
)
1058 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1060 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1062 stream_putc(s
, client
->proto
);
1064 stream_putw(s
, client
->instance
);
1068 stream_putc(s
, lmc
->keep
);
1069 /* start and end labels */
1070 stream_putl(s
, lmc
->start
);
1071 stream_putl(s
, lmc
->end
);
1074 /* Write packet size. */
1075 stream_putw_at(s
, 0, stream_get_endp(s
));
1077 return zserv_send_message(client
, s
);
1080 /* Send response to a label manager connect request to client */
1081 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1082 unsigned short result
)
1084 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1086 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1089 stream_putc(s
, client
->proto
);
1092 stream_putw(s
, client
->instance
);
1095 stream_putc(s
, result
);
1097 /* Write packet size. */
1098 stream_putw_at(s
, 0, stream_get_endp(s
));
1100 return zserv_send_message(client
, s
);
1103 /* Send response to a get table chunk request to client */
1104 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1106 struct table_manager_chunk
*tmc
)
1108 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1110 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1113 /* start and end labels */
1114 stream_putl(s
, tmc
->start
);
1115 stream_putl(s
, tmc
->end
);
1118 /* Write packet size. */
1119 stream_putw_at(s
, 0, stream_get_endp(s
));
1121 return zserv_send_message(client
, s
);
1124 static int zsend_table_manager_connect_response(struct zserv
*client
,
1128 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1130 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1133 stream_putc(s
, result
);
1135 stream_putw_at(s
, 0, stream_get_endp(s
));
1137 return zserv_send_message(client
, s
);
1140 /* SRv6 locator add notification from zebra daemon. */
1141 int zsend_zebra_srv6_locator_add(struct zserv
*client
, struct srv6_locator
*loc
)
1143 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1145 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_ADD
, VRF_DEFAULT
);
1146 zapi_srv6_locator_encode(s
, loc
);
1147 stream_putw_at(s
, 0, stream_get_endp(s
));
1149 return zserv_send_message(client
, s
);
1152 /* SRv6 locator delete notification from zebra daemon. */
1153 int zsend_zebra_srv6_locator_delete(struct zserv
*client
,
1154 struct srv6_locator
*loc
)
1156 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1158 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_DELETE
, VRF_DEFAULT
);
1159 zapi_srv6_locator_encode(s
, loc
);
1160 stream_putw_at(s
, 0, stream_get_endp(s
));
1162 return zserv_send_message(client
, s
);
1165 /* Inbound message handling ------------------------------------------------ */
1167 const int cmd2type
[] = {
1168 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1169 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1170 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1171 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1174 /* Nexthop register */
1175 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1180 unsigned short l
= 0;
1182 uint16_t type
= cmd2type
[hdr
->command
];
1184 bool flag_changed
= false;
1187 if (IS_ZEBRA_DEBUG_NHT
)
1189 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1190 zebra_route_string(client
->proto
), hdr
->length
,
1191 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1196 if (!client
->nh_reg_time
)
1197 client
->nh_reg_time
= monotime(NULL
);
1199 while (l
< hdr
->length
) {
1200 STREAM_GETC(s
, flags
);
1201 STREAM_GETW(s
, p
.family
);
1202 STREAM_GETC(s
, p
.prefixlen
);
1204 if (p
.family
== AF_INET
) {
1205 client
->v4_nh_watch_add_cnt
++;
1206 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1208 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1209 __func__
, p
.prefixlen
);
1212 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1213 l
+= IPV4_MAX_BYTELEN
;
1214 } else if (p
.family
== AF_INET6
) {
1215 client
->v6_nh_watch_add_cnt
++;
1216 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1218 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1219 __func__
, p
.prefixlen
);
1222 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1223 l
+= IPV6_MAX_BYTELEN
;
1226 EC_ZEBRA_UNKNOWN_FAMILY
,
1227 "rnh_register: Received unknown family type %d",
1231 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1235 orig_flags
= rnh
->flags
;
1236 if (type
== RNH_NEXTHOP_TYPE
) {
1238 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1239 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1241 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1242 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1243 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1245 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1246 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1248 && CHECK_FLAG(rnh
->flags
,
1249 ZEBRA_NHT_EXACT_MATCH
))
1250 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1253 if (orig_flags
!= rnh
->flags
)
1254 flag_changed
= true;
1256 /* Anything not AF_INET/INET6 has been filtered out above */
1257 if (!exist
|| flag_changed
)
1258 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1261 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1268 /* Nexthop register */
1269 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1274 unsigned short l
= 0;
1275 uint16_t type
= cmd2type
[hdr
->command
];
1277 if (IS_ZEBRA_DEBUG_NHT
)
1279 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1280 zebra_route_string(client
->proto
), hdr
->length
,
1285 while (l
< hdr
->length
) {
1288 STREAM_GETC(s
, flags
);
1290 goto stream_failure
;
1292 STREAM_GETW(s
, p
.family
);
1293 STREAM_GETC(s
, p
.prefixlen
);
1295 if (p
.family
== AF_INET
) {
1296 client
->v4_nh_watch_rem_cnt
++;
1297 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1299 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1300 __func__
, p
.prefixlen
);
1303 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1304 l
+= IPV4_MAX_BYTELEN
;
1305 } else if (p
.family
== AF_INET6
) {
1306 client
->v6_nh_watch_rem_cnt
++;
1307 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1309 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1310 __func__
, p
.prefixlen
);
1313 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1314 l
+= IPV6_MAX_BYTELEN
;
1317 EC_ZEBRA_UNKNOWN_FAMILY
,
1318 "rnh_register: Received unknown family type %d",
1322 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1324 client
->nh_dereg_time
= monotime(NULL
);
1325 zebra_remove_rnh_client(rnh
, client
, type
);
1332 #define ZEBRA_MIN_FEC_LENGTH 5
1335 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1338 unsigned short l
= 0;
1341 uint32_t label
= MPLS_INVALID_LABEL
;
1342 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1345 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1350 * The minimum amount of data that can be sent for one fec
1353 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1355 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1356 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1361 while (l
< hdr
->length
) {
1362 STREAM_GETW(s
, flags
);
1363 memset(&p
, 0, sizeof(p
));
1364 STREAM_GETW(s
, p
.family
);
1365 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1367 EC_ZEBRA_UNKNOWN_FAMILY
,
1368 "fec_register: Received unknown family type %d",
1372 STREAM_GETC(s
, p
.prefixlen
);
1373 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1374 || (p
.family
== AF_INET6
1375 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1377 "%s: Specified prefix hdr->length: %d is to long for %d",
1378 __func__
, p
.prefixlen
, p
.family
);
1382 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1383 l
+= PSIZE(p
.prefixlen
);
1384 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1385 STREAM_GETL(s
, label
);
1387 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1388 STREAM_GETL(s
, label_index
);
1392 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1399 /* FEC unregister */
1400 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1403 unsigned short l
= 0;
1408 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1413 * The minimum amount of data that can be sent for one
1414 * fec unregistration
1416 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1418 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1419 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1424 while (l
< hdr
->length
) {
1425 STREAM_GETW(s
, flags
);
1427 goto stream_failure
;
1429 memset(&p
, 0, sizeof(p
));
1430 STREAM_GETW(s
, p
.family
);
1431 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1433 EC_ZEBRA_UNKNOWN_FAMILY
,
1434 "fec_unregister: Received unknown family type %d",
1438 STREAM_GETC(s
, p
.prefixlen
);
1439 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1440 || (p
.family
== AF_INET6
1441 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1443 "%s: Received prefix hdr->length %d which is greater than %d can support",
1444 __func__
, p
.prefixlen
, p
.family
);
1448 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1449 l
+= PSIZE(p
.prefixlen
);
1450 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1459 * Register zebra server interface information.
1460 * Send current all interface and address information.
1462 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1465 struct interface
*ifp
;
1467 vrf_id_t vrf_id
= zvrf_id(zvrf
);
1468 if (vrf_id
!= VRF_DEFAULT
&& vrf_id
!= VRF_UNKNOWN
) {
1469 FOR_ALL_INTERFACES (zvrf
->vrf
, ifp
) {
1470 /* Skip pseudo interface. */
1471 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1474 zsend_interface_add(client
, ifp
);
1475 zsend_interface_link_params(client
, ifp
);
1476 zsend_interface_addresses(client
, ifp
);
1481 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1482 FOR_ALL_INTERFACES (vrf
, ifp
) {
1483 /* Skip pseudo interface. */
1484 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1487 zsend_interface_add(client
, ifp
);
1488 zsend_interface_link_params(client
, ifp
);
1489 zsend_interface_addresses(client
, ifp
);
1494 /* Unregister zebra server interface information. */
1495 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1500 * Handle message requesting interface be set up or down.
1502 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1505 struct interface
*ifp
;
1508 STREAM_GETL(msg
, ifindex
);
1509 STREAM_GETC(msg
, down
);
1512 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1515 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1516 ifindex
, down
? "on" : "off");
1517 zebra_if_set_protodown(ifp
, down
);
1520 "Cannot set protodown %s for interface %u; does not exist",
1521 down
? "on" : "off", ifindex
);
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(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1593 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1594 vtep_ip
.ipa_type
= IPADDR_V4
;
1595 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1596 sizeof(struct in_addr
));
1597 zebra_vxlan_evpn_vrf_route_add(
1598 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1601 case NEXTHOP_TYPE_IPV6
:
1602 if (IS_ZEBRA_DEBUG_RECV
) {
1603 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1605 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1606 nhbuf
, api_nh
->vrf_id
);
1608 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1610 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1611 if (IS_ZEBRA_DEBUG_RECV
) {
1612 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1614 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1615 __func__
, nhbuf
, api_nh
->vrf_id
,
1618 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1622 /* Special handling for IPv6 routes sourced from EVPN:
1623 * the nexthop and associated MAC need to be installed.
1625 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1626 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1627 vtep_ip
.ipa_type
= IPADDR_V6
;
1628 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1629 sizeof(struct in6_addr
));
1630 zebra_vxlan_evpn_vrf_route_add(
1631 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1634 case NEXTHOP_TYPE_BLACKHOLE
:
1635 if (IS_ZEBRA_DEBUG_RECV
)
1636 zlog_debug("%s: nh blackhole %d",
1637 __func__
, api_nh
->bh_type
);
1640 nexthop_from_blackhole(api_nh
->bh_type
, api_nh
->vrf_id
);
1644 /* Return early if we couldn't process the zapi nexthop */
1645 if (nexthop
== NULL
) {
1649 /* Mark nexthop as onlink either if client has explicitly told us
1650 * to or if the nexthop is on an 'unnumbered' interface.
1652 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1653 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1654 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1655 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1656 if (ifp
&& connected_is_unnumbered(ifp
))
1657 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1660 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1661 nexthop
->weight
= api_nh
->weight
;
1663 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1664 /* Validate count */
1665 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1666 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1667 zlog_debug("%s: invalid backup nh count %d",
1668 __func__
, api_nh
->backup_num
);
1669 nexthop_free(nexthop
);
1674 /* Copy backup info */
1675 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1676 nexthop
->backup_num
= api_nh
->backup_num
;
1678 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1679 /* Validate backup index */
1680 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1681 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1683 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1684 zlog_debug("%s: invalid backup nh idx %d",
1686 api_nh
->backup_idx
[i
]);
1687 nexthop_free(nexthop
);
1698 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1699 struct zapi_nexthop
*nhops
, uint32_t flags
,
1700 uint32_t message
, uint16_t nexthop_num
,
1701 uint16_t backup_nh_num
,
1702 struct nexthop_group
**png
,
1703 struct nhg_backup_info
**pbnhg
)
1705 struct nexthop_group
*ng
= NULL
;
1706 struct nhg_backup_info
*bnhg
= NULL
;
1708 struct nexthop
*last_nh
= NULL
;
1710 assert(!(png
&& pbnhg
));
1713 ng
= nexthop_group_new();
1715 if (pbnhg
&& backup_nh_num
> 0) {
1716 if (IS_ZEBRA_DEBUG_RECV
)
1717 zlog_debug("%s: adding %d backup nexthops", __func__
,
1720 bnhg
= zebra_nhg_backup_alloc();
1724 * TBD should _all_ of the nexthop add operations use
1725 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1726 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1728 for (i
= 0; i
< nexthop_num
; i
++) {
1729 struct nexthop
*nexthop
;
1730 enum lsp_types_t label_type
;
1731 char nhbuf
[NEXTHOP_STRLEN
];
1732 char labelbuf
[MPLS_LABEL_STRLEN
];
1733 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1735 /* Convert zapi nexthop */
1736 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1739 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1740 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1741 __func__
, nexthop_num
, i
);
1743 nexthop_group_delete(&ng
);
1745 zebra_nhg_backup_free(&bnhg
);
1750 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1751 if (IS_ZEBRA_DEBUG_RECV
) {
1752 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1753 zlog_debug("%s: backup nh %s with BACKUP flag!",
1756 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1757 nexthop
->backup_num
= 0;
1760 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1761 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1762 nexthop
->srte_color
= api_nh
->srte_color
;
1765 /* MPLS labels for BGP-LU or Segment Routing */
1766 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1767 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1768 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1769 && api_nh
->label_num
> 0) {
1771 label_type
= lsp_type_from_re_type(client
->proto
);
1772 nexthop_add_labels(nexthop
, label_type
,
1774 &api_nh
->labels
[0]);
1777 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)
1778 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1779 if (IS_ZEBRA_DEBUG_RECV
)
1780 zlog_debug("%s: adding seg6local action %s",
1782 seg6local_action2str(
1783 api_nh
->seg6local_action
));
1785 nexthop_add_srv6_seg6local(nexthop
,
1786 api_nh
->seg6local_action
,
1787 &api_nh
->seg6local_ctx
);
1790 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
)
1791 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1792 if (IS_ZEBRA_DEBUG_RECV
)
1793 zlog_debug("%s: adding seg6", __func__
);
1795 nexthop_add_srv6_seg6(nexthop
, &api_nh
->seg6_segs
);
1798 if (IS_ZEBRA_DEBUG_RECV
) {
1802 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1804 if (nexthop
->nh_label
&&
1805 nexthop
->nh_label
->num_labels
> 0) {
1806 mpls_label2str(nexthop
->nh_label
->num_labels
,
1807 nexthop
->nh_label
->label
,
1808 labelbuf
, sizeof(labelbuf
),
1812 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1813 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1817 /* Add new nexthop to temporary list. This list is
1818 * canonicalized - sorted - so that it can be hashed
1819 * later in route processing. We expect that the sender
1820 * has sent the list sorted, and the zapi client api
1821 * attempts to enforce that, so this should be
1822 * inexpensive - but it is necessary to support shared
1825 nexthop_group_add_sorted(ng
, nexthop
);
1828 /* Note that the order of the backup nexthops is
1829 * significant, so we don't sort this list as we do the
1830 * primary nexthops, we just append.
1833 NEXTHOP_APPEND(last_nh
, nexthop
);
1835 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1842 /* succesfully read, set caller pointers now */
1852 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1855 struct zapi_nexthop
*znh
;
1857 STREAM_GETW(s
, api_nhg
->proto
);
1858 STREAM_GETL(s
, api_nhg
->id
);
1860 if (cmd
== ZEBRA_NHG_DEL
)
1864 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1866 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1869 if (api_nhg
->nexthop_num
<= 0) {
1870 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1871 "%s: No nexthops sent", __func__
);
1875 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1876 znh
= &((api_nhg
->nexthops
)[i
]);
1878 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1879 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1880 "%s: Nexthop creation failed", __func__
);
1885 /* Backup Nexthops */
1886 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1888 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1891 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1892 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1894 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1895 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1896 "%s: Backup Nexthop creation failed",
1907 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1908 "%s: Nexthop Group decode failed with some sort of stream read failure",
1913 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1916 struct zapi_nhg api_nhg
= {};
1917 struct nhg_hash_entry
*nhe
;
1920 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1921 if (IS_ZEBRA_DEBUG_RECV
)
1922 zlog_debug("%s: Unable to decode zapi_nhg sent",
1928 * Delete the received nhg id
1930 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1933 zebra_nhg_decrement_ref(nhe
);
1934 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1935 client
->session_id
, api_nhg
.id
,
1938 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1939 client
->session_id
, api_nhg
.id
,
1940 ZAPI_NHG_REMOVE_FAIL
);
1943 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1946 struct zapi_nhg api_nhg
= {};
1947 struct nexthop_group
*nhg
= NULL
;
1948 struct nhg_backup_info
*bnhg
= NULL
;
1949 struct nhg_hash_entry
*nhe
;
1952 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1953 if (IS_ZEBRA_DEBUG_RECV
)
1954 zlog_debug("%s: Unable to decode zapi_nhg sent",
1959 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1960 api_nhg
.nexthop_num
,
1961 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1962 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1963 api_nhg
.backup_nexthop_num
,
1964 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1966 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1967 "%s: Nexthop Group Creation failed", __func__
);
1969 /* Free any local allocations */
1970 nexthop_group_delete(&nhg
);
1971 zebra_nhg_backup_free(&bnhg
);
1976 /* Create a temporary nhe */
1977 nhe
= zebra_nhg_alloc();
1978 nhe
->id
= api_nhg
.id
;
1979 nhe
->type
= api_nhg
.proto
;
1980 nhe
->zapi_instance
= client
->instance
;
1981 nhe
->zapi_session
= client
->session_id
;
1983 /* Take over the list(s) of nexthops */
1984 nhe
->nhg
.nexthop
= nhg
->nexthop
;
1985 nhg
->nexthop
= NULL
;
1988 nhe
->backup_info
= bnhg
;
1994 * Assume fully resolved for now and install.
1995 * Resolution is going to need some more work.
1998 /* Enqueue to workqueue for processing */
1999 rib_queue_nhe_add(nhe
);
2001 /* Free any local allocations */
2002 nexthop_group_delete(&nhg
);
2003 zebra_nhg_backup_free(&bnhg
);
2007 static void zread_route_add(ZAPI_HANDLER_ARGS
)
2010 struct zapi_route api
;
2012 struct prefix_ipv6
*src_p
= NULL
;
2013 struct route_entry
*re
;
2014 struct nexthop_group
*ng
= NULL
;
2015 struct nhg_backup_info
*bnhg
= NULL
;
2018 struct nhg_hash_entry nhe
;
2021 if (zapi_route_decode(s
, &api
) < 0) {
2022 if (IS_ZEBRA_DEBUG_RECV
)
2023 zlog_debug("%s: Unable to decode zapi_route sent",
2028 vrf_id
= zvrf_id(zvrf
);
2030 if (IS_ZEBRA_DEBUG_RECV
)
2031 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2032 __func__
, vrf_id
, api
.tableid
, &api
.prefix
,
2033 (int)api
.message
, api
.flags
);
2035 /* Allocate new route. */
2036 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2037 re
->type
= api
.type
;
2038 re
->instance
= api
.instance
;
2039 re
->flags
= api
.flags
;
2040 re
->uptime
= monotime(NULL
);
2041 re
->vrf_id
= vrf_id
;
2044 re
->table
= api
.tableid
;
2046 re
->table
= zvrf
->table_id
;
2048 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
2049 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
2050 || api
.nexthop_num
== 0)) {
2052 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
2053 "%s: received a route without nexthops for prefix %pFX from client %s",
2054 __func__
, &api
.prefix
,
2055 zebra_route_string(client
->proto
));
2057 XFREE(MTYPE_RE
, re
);
2061 /* Report misuse of the backup flag */
2062 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
2063 && api
.backup_nexthop_num
== 0) {
2064 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
2066 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
2067 __func__
, zebra_route_string(client
->proto
),
2071 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
))
2072 re
->nhe_id
= api
.nhgid
;
2075 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
2076 api
.flags
, api
.message
, api
.nexthop_num
,
2077 api
.backup_nexthop_num
, &ng
, NULL
)
2078 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
2079 api
.flags
, api
.message
,
2080 api
.backup_nexthop_num
,
2081 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
2083 nexthop_group_delete(&ng
);
2084 zebra_nhg_backup_free(&bnhg
);
2085 XFREE(MTYPE_RE
, re
);
2089 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
2090 re
->distance
= api
.distance
;
2091 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
2092 re
->metric
= api
.metric
;
2093 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
2095 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
2098 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
)) {
2099 re
->opaque
= XMALLOC(MTYPE_OPAQUE
,
2100 sizeof(struct opaque
) + api
.opaque
.length
);
2101 re
->opaque
->length
= api
.opaque
.length
;
2102 memcpy(re
->opaque
->data
, api
.opaque
.data
, re
->opaque
->length
);
2105 afi
= family2afi(api
.prefix
.family
);
2106 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2107 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2108 "%s: Received SRC Prefix but afi is not v6",
2110 nexthop_group_delete(&ng
);
2111 zebra_nhg_backup_free(&bnhg
);
2112 XFREE(MTYPE_RE
, re
);
2115 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2116 src_p
= &api
.src_prefix
;
2118 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
2119 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
2120 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
2121 __func__
, api
.safi
);
2122 nexthop_group_delete(&ng
);
2123 zebra_nhg_backup_free(&bnhg
);
2124 XFREE(MTYPE_RE
, re
);
2129 * If we have an ID, this proto owns the NHG it sent along with the
2130 * route, so we just send the ID into rib code with it.
2132 * Havent figured out how to handle backup NHs with this yet, so lets
2133 * keep that separate.
2134 * Include backup info with the route. We use a temporary nhe here;
2135 * if this is a new/unknown nhe, a new copy will be allocated
2139 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
2140 nhe
.nhg
.nexthop
= ng
->nexthop
;
2141 nhe
.backup_info
= bnhg
;
2143 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
,
2147 * rib_add_multipath_nhe only fails in a couple spots
2148 * and in those spots we have not freed memory
2151 client
->error_cnt
++;
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_opaque_free(struct opaque
*opaque
)
2182 XFREE(MTYPE_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_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2235 struct in_addr addr
;
2236 struct route_entry
*re
;
2238 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
2239 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
2240 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
2246 /* Register zebra server router-id information. Send current router-id */
2247 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2253 STREAM_GETW(msg
, afi
);
2255 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2257 "Invalid AFI %u while registering for router ID notifications",
2259 goto stream_failure
;
2262 /* Router-id information is needed. */
2263 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2265 router_id_get(afi
, &p
, zvrf
);
2268 * If we have not officially setup a router-id let's not
2269 * tell the upper level protocol about it yet.
2271 memset(&zero
, 0, sizeof(zero
));
2272 if ((p
.family
== AF_INET
&& p
.u
.prefix4
.s_addr
== INADDR_ANY
)
2273 || (p
.family
== AF_INET6
2274 && memcmp(&p
.u
.prefix6
, &zero
.u
.prefix6
,
2275 sizeof(struct in6_addr
))
2279 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2285 /* Unregister zebra server router-id information. */
2286 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2290 STREAM_GETW(msg
, afi
);
2292 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2294 "Invalid AFI %u while unregistering from router ID notifications",
2296 goto stream_failure
;
2299 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2305 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2307 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2309 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2310 stream_putl(s
, vrf_get_backend());
2311 stream_putc(s
, mpls_enabled
);
2312 stream_putl(s
, zrouter
.multipath_num
);
2313 stream_putc(s
, zebra_mlag_get_role());
2315 stream_putw_at(s
, 0, stream_get_endp(s
));
2316 zserv_send_message(client
, s
);
2319 void zsend_capabilities_all_clients(void)
2321 struct listnode
*node
, *nnode
;
2322 struct zebra_vrf
*zvrf
;
2323 struct zserv
*client
;
2325 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2326 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2327 /* Do not send unsolicited messages to synchronous clients. */
2328 if (client
->synchronous
)
2331 zsend_capabilities(client
, zvrf
);
2335 /* Tie up route-type and client->sock */
2336 static void zread_hello(ZAPI_HANDLER_ARGS
)
2338 /* type of protocol (lib/zebra.h) */
2340 unsigned short instance
;
2342 uint8_t synchronous
;
2343 uint32_t session_id
;
2345 STREAM_GETC(msg
, proto
);
2346 STREAM_GETW(msg
, instance
);
2347 STREAM_GETL(msg
, session_id
);
2348 STREAM_GETC(msg
, notify
);
2349 STREAM_GETC(msg
, synchronous
);
2351 client
->notify_owner
= true;
2354 client
->synchronous
= true;
2356 /* accept only dynamic routing protocols */
2357 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2359 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2360 client
->sock
, zebra_route_string(proto
),
2363 zlog_notice("client protocol instance %d", instance
);
2365 client
->proto
= proto
;
2366 client
->instance
= instance
;
2367 client
->session_id
= session_id
;
2369 /* Graceful restart processing for client connect */
2370 zebra_gr_client_reconnect(client
);
2373 if (!client
->synchronous
) {
2374 zsend_capabilities(client
, zvrf
);
2375 zebra_vrf_update_all(client
);
2381 /* Unregister all information in a VRF. */
2382 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2387 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2388 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2389 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2390 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2391 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2392 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
2397 * Validate incoming zapi mpls lsp / labels message
2399 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2405 const struct zapi_nexthop
*znh
;
2407 /* Validate backup info: no duplicates for a single primary */
2408 if (zl
->backup_nexthop_num
== 0) {
2413 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2414 znh
= &zl
->nexthops
[j
];
2416 memset(bits
, 0, sizeof(bits
));
2418 for (i
= 0; i
< znh
->backup_num
; i
++) {
2419 idx
= znh
->backup_idx
[i
] / 32;
2421 ival
= 1 << znh
->backup_idx
[i
] % 32;
2423 /* Check whether value is already used */
2424 if (ival
& bits
[idx
]) {
2427 if (IS_ZEBRA_DEBUG_RECV
)
2428 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2430 znh
->backup_idx
[i
]);
2434 /* Mark index value */
2447 * Handle request to create an MPLS LSP.
2449 * A single message can fully specify an LSP with multiple nexthops.
2451 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2452 * updated to use the received label(s).
2454 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2457 struct zapi_labels zl
;
2460 /* Get input stream. */
2462 if (zapi_labels_decode(s
, &zl
) < 0) {
2463 if (IS_ZEBRA_DEBUG_RECV
)
2464 zlog_debug("%s: Unable to decode zapi_labels sent",
2472 /* Validate; will debug on failure */
2473 if (zapi_labels_validate(&zl
) < 0)
2476 ret
= mpls_zapi_labels_process(true, zvrf
, &zl
);
2478 if (IS_ZEBRA_DEBUG_RECV
)
2479 zlog_debug("%s: Error processing zapi request",
2485 * Handle request to delete an MPLS LSP.
2487 * An LSP is identified by its type and local label. When the received message
2488 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2489 * listed LSP nexthops (aka NHLFEs) are deleted.
2491 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2492 * FEC (route) nexthops are deleted.
2494 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2497 struct zapi_labels zl
;
2500 /* Get input stream. */
2502 if (zapi_labels_decode(s
, &zl
) < 0) {
2503 if (IS_ZEBRA_DEBUG_RECV
)
2504 zlog_debug("%s: Unable to decode zapi_labels sent",
2512 if (zl
.nexthop_num
> 0) {
2513 ret
= mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2515 if (IS_ZEBRA_DEBUG_RECV
)
2516 zlog_debug("%s: Error processing zapi request",
2520 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2522 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2523 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2524 zl
.route
.type
, zl
.route
.instance
);
2529 * Handle request to add an MPLS LSP or change an existing one.
2531 * A single message can fully specify an LSP with multiple nexthops.
2533 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2534 * updated to use the received label(s).
2536 * NOTE: zebra will use route replace semantics (make-before-break) to update
2537 * the LSP in the forwarding plane if that's supported by the underlying
2540 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2543 struct zapi_labels zl
;
2545 /* Get input stream. */
2547 if (zapi_labels_decode(s
, &zl
) < 0) {
2548 if (IS_ZEBRA_DEBUG_RECV
)
2549 zlog_debug("%s: Unable to decode zapi_labels sent",
2557 /* Validate; will debug on failure */
2558 if (zapi_labels_validate(&zl
) < 0)
2561 /* This removes everything, then re-adds from the client's
2562 * zapi message. Since the LSP will be processed later, on this
2563 * this same pthread, all of the changes will 'appear' at once.
2565 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2566 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2567 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2568 zl
.route
.type
, zl
.route
.instance
);
2570 mpls_zapi_labels_process(true, zvrf
, &zl
);
2573 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2576 struct zapi_sr_policy zp
;
2577 struct zapi_srte_tunnel
*zt
;
2578 struct zebra_sr_policy
*policy
;
2580 /* Get input stream. */
2582 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2583 if (IS_ZEBRA_DEBUG_RECV
)
2584 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2588 zt
= &zp
.segment_list
;
2589 if (zt
->label_num
< 1) {
2590 if (IS_ZEBRA_DEBUG_RECV
)
2592 "%s: SR-TE tunnel must contain at least one label",
2600 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2602 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2603 /* TODO: per-VRF list of SR-TE policies. */
2604 policy
->zvrf
= zvrf
;
2606 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2609 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2612 struct zapi_sr_policy zp
;
2613 struct zebra_sr_policy
*policy
;
2615 /* Get input stream. */
2617 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2618 if (IS_ZEBRA_DEBUG_RECV
)
2619 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2627 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2629 if (IS_ZEBRA_DEBUG_RECV
)
2630 zlog_debug("%s: Unable to find SR-TE policy", __func__
);
2634 zebra_sr_policy_del(policy
);
2637 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2638 char *name
, int status
)
2640 struct zserv
*client
;
2643 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2645 if (IS_ZEBRA_DEBUG_PACKET
)
2647 "Not notifying pathd about policy %s"
2648 " status change to %d",
2653 if (IS_ZEBRA_DEBUG_PACKET
)
2655 "Notifying pathd about policy %s status change"
2659 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2662 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2663 stream_putl(s
, color
);
2664 stream_put_ipaddr(s
, endpoint
);
2665 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2666 stream_putl(s
, status
);
2668 stream_putw_at(s
, 0, stream_get_endp(s
));
2670 return zserv_send_message(client
, s
);
2673 /* Send client close notify to client */
2674 int zsend_client_close_notify(struct zserv
*client
, struct zserv
*closed_client
)
2676 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2678 zclient_create_header(s
, ZEBRA_CLIENT_CLOSE_NOTIFY
, VRF_DEFAULT
);
2680 stream_putc(s
, closed_client
->proto
);
2681 stream_putw(s
, closed_client
->instance
);
2682 stream_putl(s
, closed_client
->session_id
);
2684 stream_putw_at(s
, 0, stream_get_endp(s
));
2686 return zserv_send_message(client
, s
);
2689 int zsend_srv6_manager_get_locator_chunk_response(struct zserv
*client
,
2691 struct srv6_locator
*loc
)
2693 struct srv6_locator_chunk chunk
= {};
2694 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2696 strlcpy(chunk
.locator_name
, loc
->name
, sizeof(chunk
.locator_name
));
2697 chunk
.prefix
= loc
->prefix
;
2698 chunk
.block_bits_length
= loc
->block_bits_length
;
2699 chunk
.node_bits_length
= loc
->node_bits_length
;
2700 chunk
.function_bits_length
= loc
->function_bits_length
;
2701 chunk
.argument_bits_length
= loc
->argument_bits_length
;
2703 chunk
.proto
= client
->proto
;
2704 chunk
.instance
= client
->instance
;
2706 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
, vrf_id
);
2707 zapi_srv6_locator_chunk_encode(s
, &chunk
);
2708 stream_putw_at(s
, 0, stream_get_endp(s
));
2709 return zserv_send_message(client
, s
);
2712 /* Send response to a table manager connect request to client */
2713 static void zread_table_manager_connect(struct zserv
*client
,
2714 struct stream
*msg
, vrf_id_t vrf_id
)
2719 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2724 STREAM_GETC(s
, proto
);
2725 STREAM_GETW(s
, instance
);
2727 /* accept only dynamic routing protocols */
2728 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2729 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2730 "client %d has wrong protocol %s", client
->sock
,
2731 zebra_route_string(proto
));
2732 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2735 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2736 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2737 zebra_route_string(proto
));
2738 client
->proto
= proto
;
2739 client
->instance
= instance
;
2742 * Release previous labels of same protocol and instance.
2743 * This is done in case it restarted from an unexpected shutdown.
2745 release_daemon_table_chunks(client
);
2747 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2753 static void zread_label_manager_connect(struct zserv
*client
,
2754 struct stream
*msg
, vrf_id_t vrf_id
)
2757 /* type of protocol (lib/zebra.h) */
2759 unsigned short instance
;
2761 /* Get input stream. */
2765 STREAM_GETC(s
, proto
);
2766 STREAM_GETW(s
, instance
);
2768 /* accept only dynamic routing protocols */
2769 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2770 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2771 "client %d has wrong protocol %s", client
->sock
,
2772 zebra_route_string(proto
));
2773 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2777 /* recall proto and instance in this socket */
2778 client
->proto
= proto
;
2779 client
->instance
= instance
;
2781 /* call hook for connection using wrapper */
2782 lm_client_connect_call(client
, vrf_id
);
2788 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2793 uint32_t size
, base
;
2794 struct label_manager_chunk
*lmc
= NULL
;
2796 unsigned short instance
;
2798 /* Get input stream. */
2802 STREAM_GETC(s
, proto
);
2803 STREAM_GETW(s
, instance
);
2804 STREAM_GETC(s
, keep
);
2805 STREAM_GETL(s
, size
);
2806 STREAM_GETL(s
, base
);
2808 assert(proto
== client
->proto
&& instance
== client
->instance
);
2810 /* call hook to get a chunk using wrapper */
2811 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2817 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2820 uint32_t start
, end
;
2822 unsigned short instance
;
2824 /* Get input stream. */
2828 STREAM_GETC(s
, proto
);
2829 STREAM_GETW(s
, instance
);
2830 STREAM_GETL(s
, start
);
2831 STREAM_GETL(s
, end
);
2833 assert(proto
== client
->proto
&& instance
== client
->instance
);
2835 /* call hook to release a chunk using wrapper */
2836 lm_release_chunk_call(client
, start
, end
);
2842 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2844 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2845 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2846 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2848 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2849 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2850 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2851 zread_release_label_chunk(client
, msg
);
2855 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2860 struct table_manager_chunk
*tmc
;
2862 /* Get input stream. */
2866 STREAM_GETL(s
, size
);
2868 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2870 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2871 "%s: Unable to assign Table Chunk of size %u",
2874 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2876 /* send response back */
2877 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2883 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2886 uint32_t start
, end
;
2888 /* Get input stream. */
2892 STREAM_GETL(s
, start
);
2893 STREAM_GETL(s
, end
);
2895 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2901 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2903 /* to avoid sending other messages like ZEBRA_INTERFACE_UP */
2904 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2905 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2907 /* Sanity: don't allow 'unidentified' requests */
2908 if (!client
->proto
) {
2911 "Got SRv6 request from an unidentified client");
2914 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2915 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2916 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2917 zread_release_table_chunk(client
, msg
);
2921 static void zread_srv6_manager_get_locator_chunk(struct zserv
*client
,
2925 struct stream
*s
= msg
;
2927 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2930 STREAM_GETW(s
, len
);
2931 STREAM_GET(locator_name
, s
, len
);
2933 /* call hook to get a chunk using wrapper */
2934 struct srv6_locator
*loc
= NULL
;
2935 srv6_manager_get_locator_chunk_call(&loc
, client
, locator_name
, vrf_id
);
2941 static void zread_srv6_manager_release_locator_chunk(struct zserv
*client
,
2945 struct stream
*s
= msg
;
2947 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2950 STREAM_GETW(s
, len
);
2951 STREAM_GET(locator_name
, s
, len
);
2953 /* call hook to release a chunk using wrapper */
2954 srv6_manager_release_locator_chunk_call(client
, locator_name
, vrf_id
);
2960 static void zread_srv6_manager_request(ZAPI_HANDLER_ARGS
)
2962 switch (hdr
->command
) {
2963 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
:
2964 zread_srv6_manager_get_locator_chunk(client
, msg
,
2967 case ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
:
2968 zread_srv6_manager_release_locator_chunk(client
, msg
,
2972 zlog_err("%s: unknown SRv6 Manager command", __func__
);
2977 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2980 char ifname
[IF_NAMESIZE
];
2984 union g_addr nexthop
;
2985 uint32_t local_label
;
2986 uint32_t remote_label
;
2988 union pw_protocol_fields data
;
2990 struct zebra_pw
*pw
;
2992 /* Get input stream. */
2996 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2997 ifname
[IF_NAMESIZE
- 1] = '\0';
2998 STREAM_GETL(s
, ifindex
);
2999 STREAM_GETL(s
, type
);
3003 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
3006 STREAM_GET(&nexthop
.ipv6
, s
, 16);
3011 STREAM_GETL(s
, local_label
);
3012 STREAM_GETL(s
, remote_label
);
3013 STREAM_GETC(s
, flags
);
3014 STREAM_GET(&data
, s
, sizeof(data
));
3015 protocol
= client
->proto
;
3017 pw
= zebra_pw_find(zvrf
, ifname
);
3018 switch (hdr
->command
) {
3021 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
3022 "%s: pseudowire %s already exists [%s]",
3024 zserv_command_string(hdr
->command
));
3028 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
3030 case ZEBRA_PW_DELETE
:
3032 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3033 "%s: pseudowire %s not found [%s]", __func__
,
3034 ifname
, zserv_command_string(hdr
->command
));
3038 zebra_pw_del(zvrf
, pw
);
3041 case ZEBRA_PW_UNSET
:
3043 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3044 "%s: pseudowire %s not found [%s]", __func__
,
3045 ifname
, zserv_command_string(hdr
->command
));
3049 switch (hdr
->command
) {
3053 case ZEBRA_PW_UNSET
:
3058 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
3059 remote_label
, flags
, &data
);
3067 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
3069 struct interface
*master
;
3070 struct interface
*slave
;
3071 struct stream
*s
= msg
;
3075 STREAM_GETL(s
, vrf_id
);
3076 STREAM_GETL(s
, ifindex
);
3077 master
= if_lookup_by_index(ifindex
, vrf_id
);
3079 STREAM_GETL(s
, vrf_id
);
3080 STREAM_GETL(s
, ifindex
);
3081 slave
= if_lookup_by_index(ifindex
, vrf_id
);
3083 if (!master
|| !slave
)
3086 kernel_interface_set_master(master
, slave
);
3093 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
3095 struct interface
*ifp
;
3096 mpls_label_t nlabel
;
3099 struct zebra_vrf
*def_zvrf
;
3100 enum lsp_types_t ltype
;
3103 STREAM_GETL(s
, nlabel
);
3104 STREAM_GETC(s
, afi
);
3106 if (!(IS_VALID_AFI(afi
))) {
3107 zlog_warn("Invalid AFI for VRF label: %u", afi
);
3111 if (nlabel
== zvrf
->label
[afi
]) {
3113 * Nothing to do here move along
3118 STREAM_GETC(s
, ltype
);
3120 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
3121 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
3123 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
3126 zlog_debug("Unable to find specified Interface for %s",
3131 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3133 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
3137 really_remove
= true;
3138 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
3139 if (scrubber
== afi
)
3142 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
3145 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
3146 really_remove
= false;
3152 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
3153 NEXTHOP_TYPE_IFINDEX
, NULL
,
3154 ifp
->ifindex
, false /*backup*/);
3157 if (nlabel
!= MPLS_LABEL_NONE
) {
3158 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
3159 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
3160 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
3163 zvrf
->label
[afi
] = nlabel
;
3164 zvrf
->label_proto
[afi
] = client
->proto
;
3170 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
3172 struct zebra_pbr_rule zpr
;
3175 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
3178 STREAM_GETL(s
, total
);
3180 for (i
= 0; i
< total
; i
++) {
3181 memset(&zpr
, 0, sizeof(zpr
));
3183 zpr
.sock
= client
->sock
;
3184 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
3185 STREAM_GETL(s
, zpr
.rule
.seq
);
3186 STREAM_GETL(s
, zpr
.rule
.priority
);
3187 STREAM_GETL(s
, zpr
.rule
.unique
);
3188 STREAM_GETC(s
, zpr
.rule
.filter
.ip_proto
);
3189 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
3190 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
3191 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
3192 prefix_blen(&zpr
.rule
.filter
.src_ip
));
3193 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
3194 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
3195 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
3196 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
3197 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
3198 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
3199 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
3200 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
3201 STREAM_GETL(s
, zpr
.rule
.action
.table
);
3202 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3204 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
3205 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
3207 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
3208 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
3210 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
3211 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
3213 if (zpr
.rule
.filter
.src_port
)
3214 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3216 if (zpr
.rule
.filter
.dst_port
)
3217 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
3219 if (zpr
.rule
.filter
.dsfield
)
3220 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
3222 if (zpr
.rule
.filter
.ip_proto
)
3223 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_IP_PROTOCOL
;
3225 if (zpr
.rule
.filter
.fwmark
)
3226 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
3228 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
3229 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
3231 "Unsupported PBR source IP family: %s (%hhu)",
3232 family2str(zpr
.rule
.filter
.src_ip
.family
),
3233 zpr
.rule
.filter
.src_ip
.family
);
3236 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
3237 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
3239 "Unsupported PBR destination IP family: %s (%hhu)",
3240 family2str(zpr
.rule
.filter
.dst_ip
.family
),
3241 zpr
.rule
.filter
.dst_ip
.family
);
3246 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
3247 if (hdr
->command
== ZEBRA_RULE_ADD
)
3248 zebra_pbr_add_rule(&zpr
);
3250 zebra_pbr_del_rule(&zpr
);
3257 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
3259 struct zebra_pbr_ipset zpi
;
3264 STREAM_GETL(s
, total
);
3266 for (i
= 0; i
< total
; i
++) {
3267 memset(&zpi
, 0, sizeof(zpi
));
3269 zpi
.sock
= client
->sock
;
3270 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3271 STREAM_GETL(s
, zpi
.unique
);
3272 STREAM_GETL(s
, zpi
.type
);
3273 STREAM_GETC(s
, zpi
.family
);
3274 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3276 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3277 zebra_pbr_create_ipset(&zpi
);
3279 zebra_pbr_destroy_ipset(&zpi
);
3286 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3288 struct zebra_pbr_ipset_entry zpi
;
3289 struct zebra_pbr_ipset ipset
;
3294 STREAM_GETL(s
, total
);
3296 for (i
= 0; i
< total
; i
++) {
3297 memset(&zpi
, 0, sizeof(zpi
));
3298 memset(&ipset
, 0, sizeof(ipset
));
3300 zpi
.sock
= client
->sock
;
3301 STREAM_GETL(s
, zpi
.unique
);
3302 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3303 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3304 STREAM_GETC(s
, zpi
.src
.family
);
3305 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3306 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3307 STREAM_GETC(s
, zpi
.dst
.family
);
3308 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3309 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3311 STREAM_GETW(s
, zpi
.src_port_min
);
3312 STREAM_GETW(s
, zpi
.src_port_max
);
3313 STREAM_GETW(s
, zpi
.dst_port_min
);
3314 STREAM_GETW(s
, zpi
.dst_port_max
);
3315 STREAM_GETC(s
, zpi
.proto
);
3316 if (!is_default_prefix(&zpi
.src
))
3317 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3319 if (!is_default_prefix(&zpi
.dst
))
3320 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3321 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3322 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3323 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3324 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3325 if (zpi
.dst_port_max
!= 0)
3326 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3327 if (zpi
.src_port_max
!= 0)
3328 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3330 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3332 if (!(zpi
.dst
.family
== AF_INET
3333 || zpi
.dst
.family
== AF_INET6
)) {
3335 "Unsupported PBR destination IP family: %s (%hhu)",
3336 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3337 goto stream_failure
;
3339 if (!(zpi
.src
.family
== AF_INET
3340 || zpi
.src
.family
== AF_INET6
)) {
3342 "Unsupported PBR source IP family: %s (%hhu)",
3343 family2str(zpi
.src
.family
), zpi
.src
.family
);
3344 goto stream_failure
;
3347 /* calculate backpointer */
3349 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3351 if (!zpi
.backpointer
) {
3352 zlog_warn("ipset name specified: %s does not exist",
3354 goto stream_failure
;
3357 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3358 zebra_pbr_add_ipset_entry(&zpi
);
3360 zebra_pbr_del_ipset_entry(&zpi
);
3368 static inline void zebra_neigh_register(ZAPI_HANDLER_ARGS
)
3372 STREAM_GETW(msg
, afi
);
3373 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3375 "Invalid AFI %u while registering for neighbors notifications",
3377 goto stream_failure
;
3379 vrf_bitmap_set(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3384 static inline void zebra_neigh_unregister(ZAPI_HANDLER_ARGS
)
3388 STREAM_GETW(msg
, afi
);
3389 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3391 "Invalid AFI %u while unregistering from neighbor notifications",
3393 goto stream_failure
;
3395 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3400 static inline void zebra_gre_get(ZAPI_HANDLER_ARGS
)
3404 struct interface
*ifp
;
3405 struct zebra_if
*zebra_if
= NULL
;
3406 struct zebra_l2info_gre
*gre_info
;
3407 struct interface
*ifp_link
= NULL
;
3408 vrf_id_t vrf_id_link
= VRF_UNKNOWN
;
3409 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3412 STREAM_GETL(s
, idx
);
3413 ifp
= if_lookup_by_index(idx
, vrf_id
);
3416 zebra_if
= ifp
->info
;
3418 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3420 zclient_create_header(s
, ZEBRA_GRE_UPDATE
, vrf_id
);
3422 if (ifp
&& IS_ZEBRA_IF_GRE(ifp
) && zebra_if
) {
3423 gre_info
= &zebra_if
->l2info
.gre
;
3425 stream_putl(s
, idx
);
3426 stream_putl(s
, gre_info
->ikey
);
3427 stream_putl(s
, gre_info
->ikey
);
3428 stream_putl(s
, gre_info
->ifindex_link
);
3430 ifp_link
= if_lookup_by_index_per_ns(
3431 zebra_ns_lookup(gre_info
->link_nsid
),
3432 gre_info
->ifindex_link
);
3434 vrf_id_link
= ifp_link
->vrf_id
;
3435 stream_putl(s
, vrf_id_link
);
3436 stream_putl(s
, gre_info
->vtep_ip
.s_addr
);
3437 stream_putl(s
, gre_info
->vtep_ip_remote
.s_addr
);
3439 stream_putl(s
, idx
);
3442 stream_putl(s
, IFINDEX_INTERNAL
);
3443 stream_putl(s
, VRF_UNKNOWN
);
3446 /* Write packet size. */
3447 stream_putw_at(s
, 0, stream_get_endp(s
));
3448 zserv_send_message(client
, s
);
3455 static inline void zebra_configure_arp(ZAPI_HANDLER_ARGS
)
3460 struct interface
*ifp
;
3463 STREAM_GETC(s
, fam
);
3464 if (fam
!= AF_INET
&& fam
!= AF_INET6
)
3466 STREAM_GETL(s
, idx
);
3467 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, idx
);
3470 dplane_neigh_table_update(ifp
, fam
, 1, 0, 0);
3475 static inline void zebra_neigh_ip_add(ZAPI_HANDLER_ARGS
)
3478 struct zapi_neigh_ip api
= {};
3480 const struct interface
*ifp
;
3483 ret
= zclient_neigh_ip_decode(s
, &api
);
3486 ifp
= if_lookup_by_index(api
.index
, zvrf_id(zvrf
));
3489 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_INSTALL
, ifp
, &api
.ip_out
,
3490 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3494 static inline void zebra_neigh_ip_del(ZAPI_HANDLER_ARGS
)
3497 struct zapi_neigh_ip api
= {};
3499 struct interface
*ifp
;
3502 ret
= zclient_neigh_ip_decode(s
, &api
);
3505 ifp
= if_lookup_by_index(api
.index
, zvrf_id(zvrf
));
3508 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_DELETE
, ifp
, &api
.ip_out
,
3509 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3513 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3515 struct zebra_pbr_iptable
*zpi
=
3516 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
3521 zpi
->interface_name_list
= list_new();
3522 zpi
->sock
= client
->sock
;
3523 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3524 STREAM_GETL(s
, zpi
->unique
);
3525 STREAM_GETL(s
, zpi
->type
);
3526 STREAM_GETL(s
, zpi
->filter_bm
);
3527 STREAM_GETL(s
, zpi
->action
);
3528 STREAM_GETL(s
, zpi
->fwmark
);
3529 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3530 STREAM_GETC(s
, zpi
->family
);
3531 STREAM_GETW(s
, zpi
->pkt_len_min
);
3532 STREAM_GETW(s
, zpi
->pkt_len_max
);
3533 STREAM_GETW(s
, zpi
->tcp_flags
);
3534 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3535 STREAM_GETC(s
, zpi
->dscp_value
);
3536 STREAM_GETC(s
, zpi
->fragment
);
3537 STREAM_GETC(s
, zpi
->protocol
);
3538 STREAM_GETW(s
, zpi
->flow_label
);
3539 STREAM_GETL(s
, zpi
->nb_interface
);
3540 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3542 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3543 zebra_pbr_add_iptable(zpi
);
3545 zebra_pbr_del_iptable(zpi
);
3548 zebra_pbr_iptable_free(zpi
);
3553 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3557 struct interface
*ifp
;
3563 STREAM_GETL(s
, ifindex
);
3565 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3567 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3571 STREAM_GETC(s
, p
.family
);
3572 STREAM_GETC(s
, p
.prefixlen
);
3573 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3575 if (p
.family
== AF_INET
)
3580 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3582 dplane_neigh_discover(ifp
, &ip
);
3588 static inline void zebra_gre_source_set(ZAPI_HANDLER_ARGS
)
3591 ifindex_t idx
, link_idx
;
3592 vrf_id_t link_vrf_id
;
3593 struct interface
*ifp
;
3594 struct interface
*ifp_link
;
3595 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3596 struct zebra_if
*zif
, *gre_zif
;
3597 struct zebra_l2info_gre
*gre_info
;
3601 STREAM_GETL(s
, idx
);
3602 ifp
= if_lookup_by_index(idx
, vrf_id
);
3603 STREAM_GETL(s
, link_idx
);
3604 STREAM_GETL(s
, link_vrf_id
);
3605 STREAM_GETL(s
, mtu
);
3607 ifp_link
= if_lookup_by_index(link_idx
, link_vrf_id
);
3608 if (!ifp_link
|| !ifp
) {
3609 zlog_warn("GRE (index %u, VRF %u) or GRE link interface (index %u, VRF %u) not found, when setting GRE params",
3610 idx
, vrf_id
, link_idx
, link_vrf_id
);
3614 if (!IS_ZEBRA_IF_GRE(ifp
))
3617 gre_zif
= (struct zebra_if
*)ifp
->info
;
3618 zif
= (struct zebra_if
*)ifp_link
->info
;
3619 if (!zif
|| !gre_zif
)
3622 gre_info
= &zif
->l2info
.gre
;
3629 /* if gre link already set or mtu did not change, do not set it */
3630 if (gre_zif
->link
&& gre_zif
->link
== ifp_link
&& mtu
== ifp
->mtu
)
3633 dplane_gre_set(ifp
, ifp_link
, mtu
, gre_info
);
3639 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3640 struct zmsghdr
*bad_hdr
)
3643 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3645 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3647 zserv_encode_error(s
, error
);
3649 client
->error_cnt
++;
3650 zserv_send_message(client
, s
);
3653 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3655 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3656 zlog_debug("ZAPI message specifies unknown VRF: %d",
3659 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3662 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3664 zlog_info("Zebra received unknown command %d", hdr
->command
);
3666 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3669 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3670 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3671 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3672 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3673 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3674 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3675 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3676 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3677 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3678 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3679 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3680 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3681 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
3682 [ZEBRA_HELLO
] = zread_hello
,
3683 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3684 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3685 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
3686 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
3687 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3688 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3689 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3691 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3692 #endif /* HAVE_BFDD */
3693 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3694 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3695 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3696 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3697 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3698 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3699 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3700 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3701 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3702 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3703 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3704 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3705 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3706 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3707 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3708 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3709 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3710 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3711 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3712 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3713 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3714 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3715 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3716 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add_zapi
,
3717 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del_zapi
,
3718 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3719 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3720 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3721 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3722 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3723 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3724 [ZEBRA_PW_SET
] = zread_pseudowire
,
3725 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3726 [ZEBRA_RULE_ADD
] = zread_rule
,
3727 [ZEBRA_RULE_DELETE
] = zread_rule
,
3728 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3729 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3730 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3731 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3732 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3733 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3734 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3735 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3736 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3737 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3738 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3739 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3740 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3741 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3742 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3743 [ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3744 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3745 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3746 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3747 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3748 [ZEBRA_ROUTE_NOTIFY_REQUEST
] = zread_route_notify_request
,
3749 [ZEBRA_EVPN_REMOTE_NH_ADD
] = zebra_evpn_proc_remote_nh
,
3750 [ZEBRA_EVPN_REMOTE_NH_DEL
] = zebra_evpn_proc_remote_nh
,
3751 [ZEBRA_NEIGH_IP_ADD
] = zebra_neigh_ip_add
,
3752 [ZEBRA_NEIGH_IP_DEL
] = zebra_neigh_ip_del
,
3753 [ZEBRA_NHRP_NEIGH_REGISTER
] = zebra_neigh_register
,
3754 [ZEBRA_NHRP_NEIGH_UNREGISTER
] = zebra_neigh_unregister
,
3755 [ZEBRA_CONFIGURE_ARP
] = zebra_configure_arp
,
3756 [ZEBRA_GRE_GET
] = zebra_gre_get
,
3757 [ZEBRA_GRE_SOURCE_SET
] = zebra_gre_source_set
,
3761 * Process a batch of zapi messages.
3763 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3766 struct zebra_vrf
*zvrf
;
3768 struct stream_fifo temp_fifo
;
3770 stream_fifo_init(&temp_fifo
);
3772 while (stream_fifo_head(fifo
)) {
3773 msg
= stream_fifo_pop(fifo
);
3775 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3776 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3778 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3779 STREAM_READABLE(msg
),
3780 ZEBRA_MAX_PACKET_SIZ
);
3784 zapi_parse_header(msg
, &hdr
);
3786 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3787 && IS_ZEBRA_DEBUG_DETAIL
)
3788 zserv_log_message(NULL
, msg
, &hdr
);
3790 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3792 /* Before checking for a handler function, check for
3793 * special messages that are handled in another module;
3794 * we'll treat these as opaque.
3796 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3797 /* Reset message buffer */
3798 stream_set_getp(msg
, 0);
3800 stream_fifo_push(&temp_fifo
, msg
);
3802 /* Continue without freeing the message */
3808 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3810 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3814 if (hdr
.command
>= array_size(zserv_handlers
)
3815 || zserv_handlers
[hdr
.command
] == NULL
) {
3816 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3820 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3826 /* Dispatch any special messages from the temp fifo */
3827 if (stream_fifo_head(&temp_fifo
) != NULL
)
3828 zebra_opaque_enqueue_batch(&temp_fifo
);
3830 stream_fifo_deinit(&temp_fifo
);