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
, RE_OPAQUE
, "Route 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
->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
->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 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf
->vrf_id
);
237 /* Add Interface Index */
238 stream_putl(s
, ifp
->ifindex
);
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
246 /* Write packet size. */
247 stream_putw_at(s
, 0, stream_get_endp(s
));
249 return zserv_send_message(client
, s
);
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 * zsend_interface_address(DELETE)
274 * zebra_interface_address_delete_update
276 * | | if_delete_update
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
282 * | RTM_NEWADDR on routing/netlink socket
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M"
287 * ["no ipv6 address X:X::X:X/M"]
290 int zsend_interface_address(int cmd
, struct zserv
*client
,
291 struct interface
*ifp
, struct connected
*ifc
)
295 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
297 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
298 stream_putl(s
, ifp
->ifindex
);
300 /* Interface address flag. */
301 stream_putc(s
, ifc
->flags
);
303 /* Prefix information. */
305 stream_putc(s
, p
->family
);
306 blen
= prefix_blen(p
);
307 stream_put(s
, &p
->u
.prefix
, blen
);
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
315 stream_putc(s
, p
->prefixlen
);
318 p
= ifc
->destination
;
320 stream_put(s
, &p
->u
.prefix
, blen
);
322 stream_put(s
, NULL
, blen
);
324 /* Write packet size. */
325 stream_putw_at(s
, 0, stream_get_endp(s
));
327 client
->connected_rt_add_cnt
++;
328 return zserv_send_message(client
, s
);
331 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
332 struct interface
*ifp
,
333 struct nbr_connected
*ifc
)
336 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
339 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
340 stream_putl(s
, ifp
->ifindex
);
342 /* Prefix information. */
344 stream_putc(s
, p
->family
);
345 blen
= prefix_blen(p
);
346 stream_put(s
, &p
->u
.prefix
, blen
);
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
354 stream_putc(s
, p
->prefixlen
);
356 /* Write packet size. */
357 stream_putw_at(s
, 0, stream_get_endp(s
));
359 return zserv_send_message(client
, s
);
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
364 struct nbr_connected
*ifc
)
366 struct listnode
*node
, *nnode
;
367 struct zserv
*client
;
370 if (IS_ZEBRA_DEBUG_EVENT
) {
371 char buf
[INET6_ADDRSTRLEN
];
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
378 p
->prefixlen
, ifc
->ifp
->name
);
381 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
382 /* Do not send unsolicited messages to synchronous clients. */
383 if (client
->synchronous
)
386 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
391 /* Interface address deletion. */
392 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
393 struct nbr_connected
*ifc
)
395 struct listnode
*node
, *nnode
;
396 struct zserv
*client
;
399 if (IS_ZEBRA_DEBUG_EVENT
) {
400 char buf
[INET6_ADDRSTRLEN
];
404 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
405 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
407 p
->prefixlen
, ifc
->ifp
->name
);
410 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
411 /* Do not send unsolicited messages to synchronous clients. */
412 if (client
->synchronous
)
415 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
420 /* Send addresses on interface to client */
421 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
423 struct listnode
*cnode
, *cnnode
;
425 struct nbr_connected
*nc
;
427 /* Send interface addresses. */
428 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
429 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
432 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
438 /* Send interface neighbors. */
439 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
440 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
449 /* Notify client about interface moving from one VRF to another.
450 * Whether client is interested in old and new VRF is checked by caller.
452 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
455 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
457 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf
->vrf_id
);
459 /* Fill in the name of the interface and its new VRF (id) */
460 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
461 stream_putl(s
, vrf_id
);
463 /* Write packet size. */
464 stream_putw_at(s
, 0, stream_get_endp(s
));
466 client
->if_vrfchg_cnt
++;
467 return zserv_send_message(client
, s
);
470 /* Add new nbr connected IPv6 address */
471 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
473 struct nbr_connected
*ifc
;
477 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
478 p
.prefixlen
= IPV6_MAX_BITLEN
;
480 ifc
= listnode_head(ifp
->nbr_connected
);
483 ifc
= nbr_connected_new();
484 ifc
->address
= prefix_new();
486 listnode_add(ifp
->nbr_connected
, ifc
);
489 prefix_copy(ifc
->address
, &p
);
491 zebra_interface_nbr_address_add_update(ifp
, ifc
);
493 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
496 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
498 struct nbr_connected
*ifc
;
502 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
503 p
.prefixlen
= IPV6_MAX_BITLEN
;
505 ifc
= nbr_connected_check(ifp
, &p
);
509 listnode_delete(ifp
->nbr_connected
, ifc
);
511 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
513 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
515 nbr_connected_free(ifc
);
519 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
520 * ZEBRA_INTERFACE_DOWN.
522 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
523 * the clients in one of 2 situations:
524 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
525 * - a vty command modifying the bandwidth of an interface is received.
526 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
528 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
530 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
532 zclient_create_header(s
, cmd
, ifp
->vrf
->vrf_id
);
533 zserv_encode_interface(s
, ifp
);
535 if (cmd
== ZEBRA_INTERFACE_UP
)
538 client
->ifdown_cnt
++;
540 return zserv_send_message(client
, s
);
543 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
544 const struct route_node
*rn
,
545 const struct route_entry
*re
)
547 struct zapi_route api
;
548 struct zapi_nexthop
*api_nh
;
549 struct nexthop
*nexthop
;
550 const struct prefix
*p
, *src_p
;
554 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
556 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
557 memset(&api
, 0, sizeof(api
));
558 api
.vrf_id
= re
->vrf_id
;
560 api
.safi
= SAFI_UNICAST
;
561 api
.instance
= re
->instance
;
562 api
.flags
= re
->flags
;
564 afi
= family2afi(p
->family
);
567 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
568 client
->redist_v4_add_cnt
++;
570 client
->redist_v4_del_cnt
++;
573 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
574 client
->redist_v6_add_cnt
++;
576 client
->redist_v6_del_cnt
++;
585 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
586 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
589 for (nexthop
= re
->nhe
->nhg
.nexthop
;
590 nexthop
; nexthop
= nexthop
->next
) {
591 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
594 api_nh
= &api
.nexthops
[count
];
595 api_nh
->vrf_id
= nexthop
->vrf_id
;
596 api_nh
->type
= nexthop
->type
;
597 api_nh
->weight
= nexthop
->weight
;
598 switch (nexthop
->type
) {
599 case NEXTHOP_TYPE_BLACKHOLE
:
600 api_nh
->bh_type
= nexthop
->bh_type
;
602 case NEXTHOP_TYPE_IPV4
:
603 case NEXTHOP_TYPE_IPV4_IFINDEX
:
604 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
605 api_nh
->ifindex
= nexthop
->ifindex
;
607 case NEXTHOP_TYPE_IFINDEX
:
608 api_nh
->ifindex
= nexthop
->ifindex
;
610 case NEXTHOP_TYPE_IPV6
:
611 case NEXTHOP_TYPE_IPV6_IFINDEX
:
612 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
613 api_nh
->ifindex
= nexthop
->ifindex
;
620 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
621 api
.nexthop_num
= count
;
625 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
626 api
.distance
= re
->distance
;
627 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
628 api
.metric
= re
->metric
;
630 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
633 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
636 struct stream
*s
= stream_new(stream_size
);
638 /* Encode route and send. */
639 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
644 if (IS_ZEBRA_DEBUG_SEND
)
645 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
646 __func__
, zserv_command_string(cmd
),
647 zebra_route_string(client
->proto
),
648 zebra_route_string(api
.type
), api
.vrf_id
,
650 return zserv_send_message(client
, s
);
654 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
655 * nexthop is not found on mrib. Returns both route metric and protocol
658 * *XXX* this ZAPI call is slated to be removed at some point in the future
659 * since MRIB support in PIM is hopelessly broken in its interactions with NHT.
660 * The plan is to make pimd use NHT to receive URIB and MRIB in parallel and
661 * make the decision there, which will obsolete this ZAPI op.
662 * (Otherwise we would need to implement sending NHT updates for the result of
663 * this "URIB-MRIB-combined" table, but we only decide that here on the fly,
664 * so it'd be rather complex to do NHT for.)
666 static int zsend_nexthop_lookup_mrib(struct zserv
*client
, struct ipaddr
*addr
,
667 struct route_entry
*re
,
668 struct zebra_vrf
*zvrf
)
673 struct nexthop
*nexthop
;
675 /* Get output stream. */
676 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
679 /* Fill in result. */
680 zclient_create_header(s
, ZEBRA_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
681 stream_put_ipaddr(s
, addr
);
684 struct nexthop_group
*nhg
;
686 stream_putc(s
, re
->distance
);
687 stream_putl(s
, re
->metric
);
689 /* remember position for nexthop_num */
690 nump
= stream_get_endp(s
);
691 /* reserve room for nexthop_num */
693 nhg
= rib_get_fib_nhg(re
);
694 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
695 if (rnh_nexthop_valid(re
, nexthop
))
696 num
+= zserv_encode_nexthop(s
, nexthop
);
699 /* store nexthop_num */
700 stream_putc_at(s
, nump
, num
);
702 stream_putc(s
, 0); /* distance */
703 stream_putl(s
, 0); /* metric */
704 stream_putc(s
, 0); /* nexthop_num */
707 stream_putw_at(s
, 0, stream_get_endp(s
));
709 return zserv_send_message(client
, s
);
712 int zsend_nhg_notify(uint16_t type
, uint16_t instance
, uint32_t session_id
,
713 uint32_t id
, enum zapi_nhg_notify_owner note
)
715 struct zserv
*client
;
718 client
= zserv_find_client_session(type
, instance
, session_id
);
720 if (IS_ZEBRA_DEBUG_PACKET
) {
721 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
722 type
, instance
, id
, note
);
727 if (IS_ZEBRA_DEBUG_SEND
)
728 zlog_debug("%s: type %d, id %d, note %s",
729 __func__
, type
, id
, zapi_nhg_notify_owner2str(note
));
731 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
734 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
736 stream_put(s
, ¬e
, sizeof(note
));
739 stream_putw_at(s
, 0, stream_get_endp(s
));
741 return zserv_send_message(client
, s
);
745 * Common utility send route notification, called from a path using a
746 * route_entry and from a path using a dataplane context.
748 static int route_notify_internal(const struct route_node
*rn
, int type
,
749 uint16_t instance
, vrf_id_t vrf_id
,
751 enum zapi_route_notify_owner note
, afi_t afi
,
754 struct zserv
*client
;
758 client
= zserv_find_client(type
, instance
);
759 if (!client
|| !client
->notify_owner
) {
760 if (IS_ZEBRA_DEBUG_PACKET
)
762 "Not Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
763 zebra_route_string(type
), rn
, table_id
, note
,
768 if (IS_ZEBRA_DEBUG_PACKET
)
770 "Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
771 zebra_route_string(type
), rn
, table_id
, note
, vrf_id
);
773 /* We're just allocating a small-ish buffer here, since we only
774 * encode a small amount of data.
776 s
= stream_new(ZEBRA_SMALL_PACKET_SIZE
);
780 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
782 stream_put(s
, ¬e
, sizeof(note
));
784 stream_putc(s
, rn
->p
.family
);
786 blen
= prefix_blen(&rn
->p
);
787 stream_putc(s
, rn
->p
.prefixlen
);
788 stream_put(s
, &rn
->p
.u
.prefix
, blen
);
790 stream_putl(s
, table_id
);
792 /* Encode AFI, SAFI in the message */
794 stream_putc(s
, safi
);
796 stream_putw_at(s
, 0, stream_get_endp(s
));
798 return zserv_send_message(client
, s
);
801 int zsend_route_notify_owner(const struct route_node
*rn
,
802 struct route_entry
*re
,
803 enum zapi_route_notify_owner note
, afi_t afi
,
806 return (route_notify_internal(rn
, 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(
817 rib_find_rn_from_ctx(ctx
), dplane_ctx_get_type(ctx
),
818 dplane_ctx_get_instance(ctx
), dplane_ctx_get_vrf(ctx
),
819 dplane_ctx_get_table(ctx
), note
, dplane_ctx_get_afi(ctx
),
820 dplane_ctx_get_safi(ctx
)));
823 static void zread_route_notify_request(ZAPI_HANDLER_ARGS
)
827 STREAM_GETC(msg
, notify
);
828 client
->notify_owner
= notify
;
833 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
834 enum zapi_rule_notify_owner note
)
836 struct listnode
*node
;
837 struct zserv
*client
;
840 if (IS_ZEBRA_DEBUG_PACKET
)
841 zlog_debug("%s: Notifying %u", __func__
,
842 dplane_ctx_rule_get_unique(ctx
));
844 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
845 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
852 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
854 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
855 stream_put(s
, ¬e
, sizeof(note
));
856 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
857 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
858 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
859 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
861 stream_putw_at(s
, 0, stream_get_endp(s
));
863 zserv_send_message(client
, s
);
866 void zsend_iptable_notify_owner(const struct zebra_dplane_ctx
*ctx
,
867 enum zapi_iptable_notify_owner note
)
869 struct listnode
*node
;
870 struct zserv
*client
;
872 struct zebra_pbr_iptable ipt
;
873 uint16_t cmd
= ZEBRA_IPTABLE_NOTIFY_OWNER
;
874 struct zebra_pbr_iptable
*ipt_hash
;
875 enum dplane_op_e op
= dplane_ctx_get_op(ctx
);
877 dplane_ctx_get_pbr_iptable(ctx
, &ipt
);
879 ipt_hash
= hash_lookup(zrouter
.iptable_hash
, &ipt
);
881 if (op
== DPLANE_OP_IPTABLE_ADD
&&
882 CHECK_FLAG(ipt_hash
->internal_flags
,
883 IPTABLE_INSTALL_QUEUED
))
884 UNSET_FLAG(ipt_hash
->internal_flags
,
885 IPTABLE_INSTALL_QUEUED
);
886 else if (op
== DPLANE_OP_IPTABLE_DELETE
&&
887 CHECK_FLAG(ipt_hash
->internal_flags
,
888 IPTABLE_UNINSTALL_QUEUED
))
889 UNSET_FLAG(ipt_hash
->internal_flags
,
890 IPTABLE_UNINSTALL_QUEUED
);
892 if (IS_ZEBRA_DEBUG_PACKET
)
893 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
894 zserv_command_string(cmd
), ipt
.unique
, note
);
896 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
897 if (ipt
.sock
== client
->sock
)
904 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
906 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
907 stream_putw(s
, note
);
908 stream_putl(s
, ipt
.unique
);
909 stream_put(s
, ipt
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
910 stream_putw_at(s
, 0, stream_get_endp(s
));
912 zserv_send_message(client
, s
);
915 void zsend_ipset_notify_owner(const struct zebra_dplane_ctx
*ctx
,
916 enum zapi_ipset_notify_owner note
)
918 struct listnode
*node
;
919 struct zserv
*client
;
921 struct zebra_pbr_ipset ipset
;
922 uint16_t cmd
= ZEBRA_IPSET_NOTIFY_OWNER
;
924 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
926 if (IS_ZEBRA_DEBUG_PACKET
)
927 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
928 zserv_command_string(cmd
), ipset
.unique
, note
);
930 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
931 if (ipset
.sock
== client
->sock
)
938 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
940 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
941 stream_putw(s
, note
);
942 stream_putl(s
, ipset
.unique
);
943 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
944 stream_putw_at(s
, 0, stream_get_endp(s
));
946 zserv_send_message(client
, s
);
949 void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx
*ctx
,
950 enum zapi_ipset_entry_notify_owner note
)
952 struct listnode
*node
;
953 struct zserv
*client
;
955 struct zebra_pbr_ipset_entry ipent
;
956 struct zebra_pbr_ipset ipset
;
957 uint16_t cmd
= ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
;
959 dplane_ctx_get_pbr_ipset_entry(ctx
, &ipent
);
960 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
962 if (IS_ZEBRA_DEBUG_PACKET
)
963 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
964 zserv_command_string(cmd
), ipent
.unique
, note
);
966 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
967 if (ipent
.sock
== client
->sock
)
974 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
976 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
977 stream_putw(s
, note
);
978 stream_putl(s
, ipent
.unique
);
979 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
980 stream_putw_at(s
, 0, stream_get_endp(s
));
982 zserv_send_message(client
, s
);
985 void zsend_nhrp_neighbor_notify(int cmd
, struct interface
*ifp
,
986 struct ipaddr
*ipaddr
, int ndm_state
,
987 union sockunion
*link_layer_ipv4
)
990 struct listnode
*node
, *nnode
;
991 struct zserv
*client
;
995 if (IS_ZEBRA_DEBUG_PACKET
)
996 zlog_debug("%s: Notifying Neighbor entry (%u)", __func__
, cmd
);
998 sockunion_family(&ip
) = ipaddr_family(ipaddr
);
999 afi
= family2afi(sockunion_family(&ip
));
1000 memcpy((char *)sockunion_get_addr(&ip
), &ipaddr
->ip
.addr
,
1001 family2addrsize(sockunion_family(&ip
)));
1003 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1004 if (!vrf_bitmap_check(client
->nhrp_neighinfo
[afi
],
1008 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1009 zclient_neigh_ip_encode(s
, cmd
, &ip
, link_layer_ipv4
, ifp
,
1011 stream_putw_at(s
, 0, stream_get_endp(s
));
1012 zserv_send_message(client
, s
);
1017 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
1018 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
1024 /* Check this client need interface information. */
1025 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
1028 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1031 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
1033 /* Prefix information. */
1034 stream_putc(s
, p
->family
);
1035 blen
= prefix_blen(p
);
1036 stream_put(s
, &p
->u
.prefix
, blen
);
1037 stream_putc(s
, p
->prefixlen
);
1039 /* Write packet size. */
1040 stream_putw_at(s
, 0, stream_get_endp(s
));
1042 return zserv_send_message(client
, s
);
1046 * Function used by Zebra to send a PW status update to LDP daemon
1048 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1050 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1052 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1053 stream_write(s
, pw
->ifname
, INTERFACE_NAMSIZ
);
1054 stream_putl(s
, pw
->ifindex
);
1055 stream_putl(s
, pw
->status
);
1057 /* Put length at the first point of the stream. */
1058 stream_putw_at(s
, 0, stream_get_endp(s
));
1060 return zserv_send_message(client
, s
);
1063 /* Send response to a get label chunk request to client */
1064 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
1065 struct label_manager_chunk
*lmc
)
1067 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1069 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1071 stream_putc(s
, client
->proto
);
1073 stream_putw(s
, client
->instance
);
1077 stream_putc(s
, lmc
->keep
);
1078 /* start and end labels */
1079 stream_putl(s
, lmc
->start
);
1080 stream_putl(s
, lmc
->end
);
1083 /* Write packet size. */
1084 stream_putw_at(s
, 0, stream_get_endp(s
));
1086 return zserv_send_message(client
, s
);
1089 /* Send response to a label manager connect request to client */
1090 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1091 unsigned short result
)
1093 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1095 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1098 stream_putc(s
, client
->proto
);
1101 stream_putw(s
, client
->instance
);
1104 stream_putc(s
, result
);
1106 /* Write packet size. */
1107 stream_putw_at(s
, 0, stream_get_endp(s
));
1109 return zserv_send_message(client
, s
);
1112 /* Send response to a get table chunk request to client */
1113 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1115 struct table_manager_chunk
*tmc
)
1117 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1119 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1122 /* start and end labels */
1123 stream_putl(s
, tmc
->start
);
1124 stream_putl(s
, tmc
->end
);
1127 /* Write packet size. */
1128 stream_putw_at(s
, 0, stream_get_endp(s
));
1130 return zserv_send_message(client
, s
);
1133 static int zsend_table_manager_connect_response(struct zserv
*client
,
1137 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1139 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1142 stream_putc(s
, result
);
1144 stream_putw_at(s
, 0, stream_get_endp(s
));
1146 return zserv_send_message(client
, s
);
1149 /* SRv6 locator add notification from zebra daemon. */
1150 int zsend_zebra_srv6_locator_add(struct zserv
*client
, struct srv6_locator
*loc
)
1152 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1154 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_ADD
, VRF_DEFAULT
);
1155 zapi_srv6_locator_encode(s
, loc
);
1156 stream_putw_at(s
, 0, stream_get_endp(s
));
1158 return zserv_send_message(client
, s
);
1161 /* SRv6 locator delete notification from zebra daemon. */
1162 int zsend_zebra_srv6_locator_delete(struct zserv
*client
,
1163 struct srv6_locator
*loc
)
1165 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1167 zclient_create_header(s
, ZEBRA_SRV6_LOCATOR_DELETE
, VRF_DEFAULT
);
1168 zapi_srv6_locator_encode(s
, loc
);
1169 stream_putw_at(s
, 0, stream_get_endp(s
));
1171 return zserv_send_message(client
, s
);
1174 /* Inbound message handling ------------------------------------------------ */
1176 /* Nexthop register */
1177 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1182 unsigned short l
= 0;
1183 uint8_t connected
= 0;
1184 uint8_t resolve_via_default
;
1186 bool flag_changed
= false;
1190 if (IS_ZEBRA_DEBUG_NHT
)
1192 "rnh_register msg from client %s: hdr->length=%d vrf=%u",
1193 zebra_route_string(client
->proto
), hdr
->length
,
1198 if (!client
->nh_reg_time
)
1199 client
->nh_reg_time
= monotime(NULL
);
1201 while (l
< hdr
->length
) {
1202 STREAM_GETC(s
, connected
);
1203 STREAM_GETC(s
, resolve_via_default
);
1204 STREAM_GETW(s
, safi
);
1205 STREAM_GETW(s
, p
.family
);
1206 STREAM_GETC(s
, p
.prefixlen
);
1208 if (p
.family
== AF_INET
) {
1209 client
->v4_nh_watch_add_cnt
++;
1210 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1212 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1213 __func__
, p
.prefixlen
);
1216 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1217 l
+= IPV4_MAX_BYTELEN
;
1218 } else if (p
.family
== AF_INET6
) {
1219 client
->v6_nh_watch_add_cnt
++;
1220 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1222 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1223 __func__
, p
.prefixlen
);
1226 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1227 l
+= IPV6_MAX_BYTELEN
;
1230 EC_ZEBRA_UNKNOWN_FAMILY
,
1231 "rnh_register: Received unknown family type %d",
1235 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), safi
, &exist
);
1239 orig_flags
= rnh
->flags
;
1240 if (connected
&& !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1241 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1243 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1244 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1246 if (resolve_via_default
)
1247 SET_FLAG(rnh
->flags
, ZEBRA_NHT_RESOLVE_VIA_DEFAULT
);
1249 if (orig_flags
!= rnh
->flags
)
1250 flag_changed
= true;
1252 /* Anything not AF_INET/INET6 has been filtered out above */
1253 if (!exist
|| flag_changed
)
1254 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, &p
,
1257 zebra_add_rnh_client(rnh
, client
, zvrf_id(zvrf
));
1264 /* Nexthop register */
1265 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1270 unsigned short l
= 0;
1273 if (IS_ZEBRA_DEBUG_NHT
)
1275 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1276 zebra_route_string(client
->proto
), hdr
->length
,
1281 while (l
< hdr
->length
) {
1284 STREAM_GETC(s
, ignore
);
1286 goto stream_failure
;
1287 STREAM_GETC(s
, ignore
);
1289 goto stream_failure
;
1291 STREAM_GETW(s
, safi
);
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
), safi
);
1324 client
->nh_dereg_time
= monotime(NULL
);
1325 zebra_remove_rnh_client(rnh
, client
);
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
;
1507 enum protodown_reasons reason
;
1509 STREAM_GETL(msg
, ifindex
);
1510 STREAM_GETC(msg
, down
);
1513 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1517 "Cannot set protodown %s for interface %u; does not exist",
1518 down
? "on" : "off", ifindex
);
1523 switch (client
->proto
) {
1524 case ZEBRA_ROUTE_VRRP
:
1525 reason
= ZEBRA_PROTODOWN_VRRP
;
1527 case ZEBRA_ROUTE_SHARP
:
1528 reason
= ZEBRA_PROTODOWN_SHARP
;
1535 zebra_if_set_protodown(ifp
, down
, reason
);
1541 bool zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1542 const unsigned int nexthop_num
)
1544 if (nexthop_num
> zrouter
.multipath_num
) {
1545 char buff
[PREFIX2STR_BUFFER
];
1548 prefix2str(p
, buff
, sizeof(buff
));
1551 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1552 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1553 caller
, (p
? buff
: "(NULL)"), nexthop_num
,
1554 zrouter
.multipath_num
);
1562 * Create a new nexthop based on a zapi nexthop.
1564 static struct nexthop
*nexthop_from_zapi(const struct zapi_nexthop
*api_nh
,
1565 uint32_t flags
, struct prefix
*p
,
1566 uint16_t backup_nexthop_num
)
1568 struct nexthop
*nexthop
= NULL
;
1569 struct ipaddr vtep_ip
;
1570 struct interface
*ifp
;
1572 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1574 switch (api_nh
->type
) {
1575 case NEXTHOP_TYPE_IFINDEX
:
1576 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1578 case NEXTHOP_TYPE_IPV4
:
1579 if (IS_ZEBRA_DEBUG_RECV
) {
1580 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1582 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1583 nhbuf
, api_nh
->vrf_id
);
1585 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1588 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1589 if (IS_ZEBRA_DEBUG_RECV
) {
1590 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1592 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1593 __func__
, nhbuf
, api_nh
->vrf_id
,
1597 nexthop
= nexthop_from_ipv4_ifindex(
1598 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1601 /* Special handling for IPv4 routes sourced from EVPN:
1602 * the nexthop and associated MAC need to be installed.
1604 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1605 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
1606 vtep_ip
.ipa_type
= IPADDR_V4
;
1607 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1608 sizeof(struct in_addr
));
1609 zebra_rib_queue_evpn_route_add(
1610 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1611 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
);
1614 case NEXTHOP_TYPE_IPV6
:
1615 if (IS_ZEBRA_DEBUG_RECV
) {
1616 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1618 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1619 nhbuf
, api_nh
->vrf_id
);
1621 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1623 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1624 if (IS_ZEBRA_DEBUG_RECV
) {
1625 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1627 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1628 __func__
, nhbuf
, api_nh
->vrf_id
,
1631 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1635 /* Special handling for IPv6 routes sourced from EVPN:
1636 * the nexthop and associated MAC need to be installed.
1638 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_EVPN
)) {
1639 memset(&vtep_ip
, 0, sizeof(vtep_ip
));
1640 vtep_ip
.ipa_type
= IPADDR_V6
;
1641 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1642 sizeof(struct in6_addr
));
1643 zebra_rib_queue_evpn_route_add(
1644 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1645 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
);
1648 case NEXTHOP_TYPE_BLACKHOLE
:
1649 if (IS_ZEBRA_DEBUG_RECV
)
1650 zlog_debug("%s: nh blackhole %d",
1651 __func__
, api_nh
->bh_type
);
1654 nexthop_from_blackhole(api_nh
->bh_type
, api_nh
->vrf_id
);
1658 /* Return early if we couldn't process the zapi nexthop */
1659 if (nexthop
== NULL
) {
1663 /* Mark nexthop as onlink either if client has explicitly told us
1664 * to or if the nexthop is on an 'unnumbered' interface.
1666 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1667 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1668 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1669 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1670 if (ifp
&& connected_is_unnumbered(ifp
))
1671 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1674 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1675 nexthop
->weight
= api_nh
->weight
;
1677 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1678 /* Validate count */
1679 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1680 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1681 zlog_debug("%s: invalid backup nh count %d",
1682 __func__
, api_nh
->backup_num
);
1683 nexthop_free(nexthop
);
1688 /* Copy backup info */
1689 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1690 nexthop
->backup_num
= api_nh
->backup_num
;
1692 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1693 /* Validate backup index */
1694 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1695 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1697 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1698 zlog_debug("%s: invalid backup nh idx %d",
1700 api_nh
->backup_idx
[i
]);
1701 nexthop_free(nexthop
);
1712 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1713 struct zapi_nexthop
*nhops
, uint32_t flags
,
1714 uint32_t message
, uint16_t nexthop_num
,
1715 uint16_t backup_nh_num
,
1716 struct nexthop_group
**png
,
1717 struct nhg_backup_info
**pbnhg
)
1719 struct nexthop_group
*ng
= NULL
;
1720 struct nhg_backup_info
*bnhg
= NULL
;
1722 struct nexthop
*last_nh
= NULL
;
1724 assert(!(png
&& pbnhg
));
1727 ng
= nexthop_group_new();
1729 if (pbnhg
&& backup_nh_num
> 0) {
1730 if (IS_ZEBRA_DEBUG_RECV
)
1731 zlog_debug("%s: adding %d backup nexthops", __func__
,
1734 bnhg
= zebra_nhg_backup_alloc();
1738 * TBD should _all_ of the nexthop add operations use
1739 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1740 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1742 for (i
= 0; i
< nexthop_num
; i
++) {
1743 struct nexthop
*nexthop
;
1744 enum lsp_types_t label_type
;
1745 char nhbuf
[NEXTHOP_STRLEN
];
1746 char labelbuf
[MPLS_LABEL_STRLEN
];
1747 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1749 /* Convert zapi nexthop */
1750 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1753 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1754 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1755 __func__
, nexthop_num
, i
);
1757 nexthop_group_delete(&ng
);
1759 zebra_nhg_backup_free(&bnhg
);
1764 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1765 if (IS_ZEBRA_DEBUG_RECV
) {
1766 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1767 zlog_debug("%s: backup nh %s with BACKUP flag!",
1770 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1771 nexthop
->backup_num
= 0;
1774 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1775 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1776 nexthop
->srte_color
= api_nh
->srte_color
;
1779 /* MPLS labels for BGP-LU or Segment Routing */
1780 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1781 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1782 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1783 && api_nh
->label_num
> 0) {
1785 label_type
= lsp_type_from_re_type(client
->proto
);
1786 nexthop_add_labels(nexthop
, label_type
,
1788 &api_nh
->labels
[0]);
1791 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6LOCAL
)
1792 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1793 if (IS_ZEBRA_DEBUG_RECV
)
1794 zlog_debug("%s: adding seg6local action %s",
1796 seg6local_action2str(
1797 api_nh
->seg6local_action
));
1799 nexthop_add_srv6_seg6local(nexthop
,
1800 api_nh
->seg6local_action
,
1801 &api_nh
->seg6local_ctx
);
1804 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_SEG6
)
1805 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1806 if (IS_ZEBRA_DEBUG_RECV
)
1807 zlog_debug("%s: adding seg6", __func__
);
1809 nexthop_add_srv6_seg6(nexthop
, &api_nh
->seg6_segs
);
1812 if (IS_ZEBRA_DEBUG_RECV
) {
1816 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1818 if (nexthop
->nh_label
&&
1819 nexthop
->nh_label
->num_labels
> 0) {
1820 mpls_label2str(nexthop
->nh_label
->num_labels
,
1821 nexthop
->nh_label
->label
,
1822 labelbuf
, sizeof(labelbuf
),
1826 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1827 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1831 /* Add new nexthop to temporary list. This list is
1832 * canonicalized - sorted - so that it can be hashed
1833 * later in route processing. We expect that the sender
1834 * has sent the list sorted, and the zapi client api
1835 * attempts to enforce that, so this should be
1836 * inexpensive - but it is necessary to support shared
1839 nexthop_group_add_sorted(ng
, nexthop
);
1842 /* Note that the order of the backup nexthops is
1843 * significant, so we don't sort this list as we do the
1844 * primary nexthops, we just append.
1847 NEXTHOP_APPEND(last_nh
, nexthop
);
1849 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1856 /* succesfully read, set caller pointers now */
1866 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1869 struct zapi_nexthop
*znh
;
1871 STREAM_GETW(s
, api_nhg
->proto
);
1872 STREAM_GETL(s
, api_nhg
->id
);
1874 if (cmd
== ZEBRA_NHG_DEL
)
1877 STREAM_GETW(s
, api_nhg
->resilience
.buckets
);
1878 STREAM_GETL(s
, api_nhg
->resilience
.idle_timer
);
1879 STREAM_GETL(s
, api_nhg
->resilience
.unbalanced_timer
);
1882 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1884 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1887 if (api_nhg
->nexthop_num
<= 0) {
1888 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1889 "%s: No nexthops sent", __func__
);
1893 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1894 znh
= &((api_nhg
->nexthops
)[i
]);
1896 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1897 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1898 "%s: Nexthop creation failed", __func__
);
1903 /* Backup Nexthops */
1904 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1906 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1909 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1910 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1912 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1913 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1914 "%s: Backup Nexthop creation failed",
1925 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1926 "%s: Nexthop Group decode failed with some sort of stream read failure",
1931 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1934 struct zapi_nhg api_nhg
= {};
1935 struct nhg_hash_entry
*nhe
;
1938 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1939 if (IS_ZEBRA_DEBUG_RECV
)
1940 zlog_debug("%s: Unable to decode zapi_nhg sent",
1946 * Delete the received nhg id
1948 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1951 zebra_nhg_decrement_ref(nhe
);
1952 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1953 client
->session_id
, api_nhg
.id
,
1956 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1957 client
->session_id
, api_nhg
.id
,
1958 ZAPI_NHG_REMOVE_FAIL
);
1961 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1964 struct zapi_nhg api_nhg
= {};
1965 struct nexthop_group
*nhg
= NULL
;
1966 struct nhg_backup_info
*bnhg
= NULL
;
1967 struct nhg_hash_entry
*nhe
;
1970 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1971 if (IS_ZEBRA_DEBUG_RECV
)
1972 zlog_debug("%s: Unable to decode zapi_nhg sent",
1977 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1978 api_nhg
.nexthop_num
,
1979 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1980 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1981 api_nhg
.backup_nexthop_num
,
1982 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1984 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1985 "%s: Nexthop Group Creation failed", __func__
);
1987 /* Free any local allocations */
1988 nexthop_group_delete(&nhg
);
1989 zebra_nhg_backup_free(&bnhg
);
1994 /* Create a temporary nhe */
1995 nhe
= zebra_nhg_alloc();
1996 nhe
->id
= api_nhg
.id
;
1997 nhe
->type
= api_nhg
.proto
;
1998 nhe
->zapi_instance
= client
->instance
;
1999 nhe
->zapi_session
= client
->session_id
;
2001 /* Take over the list(s) of nexthops */
2002 nhe
->nhg
.nexthop
= nhg
->nexthop
;
2003 nhg
->nexthop
= NULL
;
2005 nhe
->nhg
.nhgr
= api_nhg
.resilience
;
2008 nhe
->backup_info
= bnhg
;
2014 * Assume fully resolved for now and install.
2015 * Resolution is going to need some more work.
2018 /* Enqueue to workqueue for processing */
2019 rib_queue_nhe_add(nhe
);
2021 /* Free any local allocations */
2022 nexthop_group_delete(&nhg
);
2023 zebra_nhg_backup_free(&bnhg
);
2027 static void zread_route_add(ZAPI_HANDLER_ARGS
)
2030 struct zapi_route api
;
2032 struct prefix_ipv6
*src_p
= NULL
;
2033 struct route_entry
*re
;
2034 struct nexthop_group
*ng
= NULL
;
2035 struct nhg_backup_info
*bnhg
= NULL
;
2038 struct nhg_hash_entry nhe
, *n
= NULL
;
2041 if (zapi_route_decode(s
, &api
) < 0) {
2042 if (IS_ZEBRA_DEBUG_RECV
)
2043 zlog_debug("%s: Unable to decode zapi_route sent",
2048 vrf_id
= zvrf_id(zvrf
);
2050 if (IS_ZEBRA_DEBUG_RECV
)
2051 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2052 __func__
, vrf_id
, api
.tableid
, &api
.prefix
,
2053 (int)api
.message
, api
.flags
);
2055 /* Allocate new route. */
2056 re
= zebra_rib_route_entry_new(
2057 vrf_id
, api
.type
, api
.instance
, api
.flags
, api
.nhgid
,
2058 api
.tableid
? api
.tableid
: zvrf
->table_id
, api
.metric
, api
.mtu
,
2059 api
.distance
, api
.tag
);
2061 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
2062 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
2063 || api
.nexthop_num
== 0)) {
2065 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
2066 "%s: received a route without nexthops for prefix %pFX from client %s",
2067 __func__
, &api
.prefix
,
2068 zebra_route_string(client
->proto
));
2070 XFREE(MTYPE_RE
, re
);
2074 /* Report misuse of the backup flag */
2075 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
2076 && api
.backup_nexthop_num
== 0) {
2077 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
2079 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
2080 __func__
, zebra_route_string(client
->proto
),
2085 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
2086 api
.flags
, api
.message
, api
.nexthop_num
,
2087 api
.backup_nexthop_num
, &ng
, NULL
)
2088 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
2089 api
.flags
, api
.message
,
2090 api
.backup_nexthop_num
,
2091 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
2093 nexthop_group_delete(&ng
);
2094 zebra_nhg_backup_free(&bnhg
);
2095 XFREE(MTYPE_RE
, re
);
2099 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
)) {
2101 XMALLOC(MTYPE_RE_OPAQUE
,
2102 sizeof(struct re_opaque
) + api
.opaque
.length
);
2103 re
->opaque
->length
= api
.opaque
.length
;
2104 memcpy(re
->opaque
->data
, api
.opaque
.data
, re
->opaque
->length
);
2107 afi
= family2afi(api
.prefix
.family
);
2108 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2109 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2110 "%s: Received SRC Prefix but afi is not v6",
2112 nexthop_group_delete(&ng
);
2113 zebra_nhg_backup_free(&bnhg
);
2114 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2115 XFREE(MTYPE_RE
, re
);
2118 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2119 src_p
= &api
.src_prefix
;
2121 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
2122 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
2123 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
2124 __func__
, api
.safi
);
2125 nexthop_group_delete(&ng
);
2126 zebra_nhg_backup_free(&bnhg
);
2127 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2128 XFREE(MTYPE_RE
, re
);
2133 * If we have an ID, this proto owns the NHG it sent along with the
2134 * route, so we just send the ID into rib code with it.
2136 * Havent figured out how to handle backup NHs with this yet, so lets
2137 * keep that separate.
2138 * Include backup info with the route. We use a temporary nhe here;
2139 * if this is a new/unknown nhe, a new copy will be allocated
2143 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
2144 nhe
.nhg
.nexthop
= ng
->nexthop
;
2145 nhe
.backup_info
= bnhg
;
2146 n
= zebra_nhe_copy(&nhe
, 0);
2148 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
, re
, n
,
2152 * rib_add_multipath_nhe only fails in a couple spots
2153 * and in those spots we have not freed memory
2156 client
->error_cnt
++;
2157 XFREE(MTYPE_RE_OPAQUE
, re
->opaque
);
2158 XFREE(MTYPE_RE
, re
);
2161 /* At this point, these allocations are not needed: 're' has been
2162 * retained or freed, and if 're' still exists, it is using
2163 * a reference to a shared group object.
2165 nexthop_group_delete(&ng
);
2167 zebra_nhg_backup_free(&bnhg
);
2170 switch (api
.prefix
.family
) {
2173 client
->v4_route_add_cnt
++;
2175 client
->v4_route_upd8_cnt
++;
2179 client
->v6_route_add_cnt
++;
2181 client
->v6_route_upd8_cnt
++;
2186 void zapi_re_opaque_free(struct re_opaque
*opaque
)
2188 XFREE(MTYPE_RE_OPAQUE
, opaque
);
2191 static void zread_route_del(ZAPI_HANDLER_ARGS
)
2194 struct zapi_route api
;
2196 struct prefix_ipv6
*src_p
= NULL
;
2200 if (zapi_route_decode(s
, &api
) < 0)
2203 afi
= family2afi(api
.prefix
.family
);
2204 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2205 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2206 "%s: Received a src prefix while afi is not v6",
2210 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2211 src_p
= &api
.src_prefix
;
2214 table_id
= api
.tableid
;
2216 table_id
= zvrf
->table_id
;
2218 if (IS_ZEBRA_DEBUG_RECV
)
2219 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2220 __func__
, zvrf_id(zvrf
), table_id
, &api
.prefix
,
2221 (int)api
.message
, api
.flags
);
2223 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2224 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
2225 api
.distance
, false);
2228 switch (api
.prefix
.family
) {
2230 client
->v4_route_del_cnt
++;
2233 client
->v6_route_del_cnt
++;
2238 /* MRIB Nexthop lookup for IPv4. */
2239 static void zread_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2242 struct route_entry
*re
= NULL
;
2244 STREAM_GET_IPADDR(msg
, &addr
);
2246 switch (addr
.ipa_type
) {
2248 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
.ipaddr_v4
,
2252 re
= rib_match_ipv6_multicast(zvrf_id(zvrf
), addr
.ipaddr_v6
,
2257 goto stream_failure
;
2260 zsend_nexthop_lookup_mrib(client
, &addr
, re
, zvrf
);
2266 /* Register zebra server router-id information. Send current router-id */
2267 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2273 STREAM_GETW(msg
, afi
);
2275 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2277 "Invalid AFI %u while registering for router ID notifications",
2279 goto stream_failure
;
2282 /* Router-id information is needed. */
2283 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2285 router_id_get(afi
, &p
, zvrf
);
2288 * If we have not officially setup a router-id let's not
2289 * tell the upper level protocol about it yet.
2291 memset(&zero
, 0, sizeof(zero
));
2292 if ((p
.family
== AF_INET
&& p
.u
.prefix4
.s_addr
== INADDR_ANY
)
2293 || (p
.family
== AF_INET6
2294 && memcmp(&p
.u
.prefix6
, &zero
.u
.prefix6
,
2295 sizeof(struct in6_addr
))
2299 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2305 /* Unregister zebra server router-id information. */
2306 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2310 STREAM_GETW(msg
, afi
);
2312 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2314 "Invalid AFI %u while unregistering from router ID notifications",
2316 goto stream_failure
;
2319 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2325 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2327 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2329 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2330 stream_putl(s
, vrf_get_backend());
2331 stream_putc(s
, mpls_enabled
);
2332 stream_putl(s
, zrouter
.multipath_num
);
2333 stream_putc(s
, zebra_mlag_get_role());
2335 stream_putw_at(s
, 0, stream_get_endp(s
));
2336 zserv_send_message(client
, s
);
2339 void zsend_capabilities_all_clients(void)
2341 struct listnode
*node
, *nnode
;
2342 struct zebra_vrf
*zvrf
;
2343 struct zserv
*client
;
2345 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2346 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2347 /* Do not send unsolicited messages to synchronous clients. */
2348 if (client
->synchronous
)
2351 zsend_capabilities(client
, zvrf
);
2355 /* Tie up route-type and client->sock */
2356 static void zread_hello(ZAPI_HANDLER_ARGS
)
2358 /* type of protocol (lib/zebra.h) */
2360 unsigned short instance
;
2362 uint8_t synchronous
;
2363 uint32_t session_id
;
2365 STREAM_GETC(msg
, proto
);
2366 STREAM_GETW(msg
, instance
);
2367 STREAM_GETL(msg
, session_id
);
2368 STREAM_GETC(msg
, notify
);
2369 STREAM_GETC(msg
, synchronous
);
2371 client
->notify_owner
= true;
2374 client
->synchronous
= true;
2376 /* accept only dynamic routing protocols */
2377 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2379 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2380 client
->sock
, zebra_route_string(proto
),
2383 zlog_notice("client protocol instance %d", instance
);
2385 client
->proto
= proto
;
2386 client
->instance
= instance
;
2387 client
->session_id
= session_id
;
2389 /* Graceful restart processing for client connect */
2390 zebra_gr_client_reconnect(client
);
2393 if (!client
->synchronous
) {
2394 zsend_capabilities(client
, zvrf
);
2395 zebra_vrf_update_all(client
);
2401 /* Unregister all information in a VRF. */
2402 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2407 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2408 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2409 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2410 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2411 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2412 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
2417 * Validate incoming zapi mpls lsp / labels message
2419 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2425 const struct zapi_nexthop
*znh
;
2427 /* Validate backup info: no duplicates for a single primary */
2428 if (zl
->backup_nexthop_num
== 0) {
2433 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2434 znh
= &zl
->nexthops
[j
];
2436 memset(bits
, 0, sizeof(bits
));
2438 for (i
= 0; i
< znh
->backup_num
; i
++) {
2439 idx
= znh
->backup_idx
[i
] / 32;
2441 ival
= 1 << znh
->backup_idx
[i
] % 32;
2443 /* Check whether value is already used */
2444 if (ival
& bits
[idx
]) {
2447 if (IS_ZEBRA_DEBUG_RECV
)
2448 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2450 znh
->backup_idx
[i
]);
2454 /* Mark index value */
2467 * Handle request to create an MPLS LSP.
2469 * A single message can fully specify an LSP with multiple nexthops.
2471 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2472 * updated to use the received label(s).
2474 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2477 struct zapi_labels zl
;
2479 /* Get input stream. */
2481 if (zapi_labels_decode(s
, &zl
) < 0) {
2482 if (IS_ZEBRA_DEBUG_RECV
)
2483 zlog_debug("%s: Unable to decode zapi_labels sent",
2491 /* Validate; will debug on failure */
2492 if (zapi_labels_validate(&zl
) < 0)
2495 mpls_zapi_labels_process(true, zvrf
, &zl
);
2499 * Handle request to delete an MPLS LSP.
2501 * An LSP is identified by its type and local label. When the received message
2502 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2503 * listed LSP nexthops (aka NHLFEs) are deleted.
2505 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2506 * FEC (route) nexthops are deleted.
2508 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2511 struct zapi_labels zl
;
2513 /* Get input stream. */
2515 if (zapi_labels_decode(s
, &zl
) < 0) {
2516 if (IS_ZEBRA_DEBUG_RECV
)
2517 zlog_debug("%s: Unable to decode zapi_labels sent",
2525 if (zl
.nexthop_num
> 0) {
2526 mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2528 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2530 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2531 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2532 zl
.route
.type
, zl
.route
.instance
);
2537 * Handle request to add an MPLS LSP or change an existing one.
2539 * A single message can fully specify an LSP with multiple nexthops.
2541 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2542 * updated to use the received label(s).
2544 * NOTE: zebra will use route replace semantics (make-before-break) to update
2545 * the LSP in the forwarding plane if that's supported by the underlying
2548 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2551 struct zapi_labels zl
;
2553 /* Get input stream. */
2555 if (zapi_labels_decode(s
, &zl
) < 0) {
2556 if (IS_ZEBRA_DEBUG_RECV
)
2557 zlog_debug("%s: Unable to decode zapi_labels sent",
2565 /* Validate; will debug on failure */
2566 if (zapi_labels_validate(&zl
) < 0)
2569 /* This removes everything, then re-adds from the client's
2570 * zapi message. Since the LSP will be processed later, on this
2571 * this same pthread, all of the changes will 'appear' at once.
2573 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2574 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2575 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2576 zl
.route
.type
, zl
.route
.instance
);
2578 mpls_zapi_labels_process(true, zvrf
, &zl
);
2581 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2584 struct zapi_sr_policy zp
;
2585 struct zapi_srte_tunnel
*zt
;
2586 struct zebra_sr_policy
*policy
;
2588 /* Get input stream. */
2590 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2591 if (IS_ZEBRA_DEBUG_RECV
)
2592 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2596 zt
= &zp
.segment_list
;
2597 if (zt
->label_num
< 1) {
2598 if (IS_ZEBRA_DEBUG_RECV
)
2600 "%s: SR-TE tunnel must contain at least one label",
2608 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2610 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2611 policy
->sock
= client
->sock
;
2613 /* TODO: per-VRF list of SR-TE policies. */
2614 policy
->zvrf
= zvrf
;
2616 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2619 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2622 struct zapi_sr_policy zp
;
2623 struct zebra_sr_policy
*policy
;
2625 /* Get input stream. */
2627 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2628 if (IS_ZEBRA_DEBUG_RECV
)
2629 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2637 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2639 if (IS_ZEBRA_DEBUG_RECV
)
2640 zlog_debug("%s: Unable to find SR-TE policy", __func__
);
2644 zebra_sr_policy_del(policy
);
2647 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2648 char *name
, int status
)
2650 struct zserv
*client
;
2653 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2655 if (IS_ZEBRA_DEBUG_PACKET
)
2657 "Not notifying pathd about policy %s"
2658 " status change to %d",
2663 if (IS_ZEBRA_DEBUG_PACKET
)
2665 "Notifying pathd about policy %s status change"
2669 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2672 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2673 stream_putl(s
, color
);
2674 stream_put_ipaddr(s
, endpoint
);
2675 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2676 stream_putl(s
, status
);
2678 stream_putw_at(s
, 0, stream_get_endp(s
));
2680 return zserv_send_message(client
, s
);
2683 /* Send client close notify to client */
2684 int zsend_client_close_notify(struct zserv
*client
, struct zserv
*closed_client
)
2686 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2688 zclient_create_header(s
, ZEBRA_CLIENT_CLOSE_NOTIFY
, VRF_DEFAULT
);
2690 stream_putc(s
, closed_client
->proto
);
2691 stream_putw(s
, closed_client
->instance
);
2692 stream_putl(s
, closed_client
->session_id
);
2694 stream_putw_at(s
, 0, stream_get_endp(s
));
2696 return zserv_send_message(client
, s
);
2699 int zsend_srv6_manager_get_locator_chunk_response(struct zserv
*client
,
2701 struct srv6_locator
*loc
)
2703 struct srv6_locator_chunk chunk
= {};
2704 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2706 strlcpy(chunk
.locator_name
, loc
->name
, sizeof(chunk
.locator_name
));
2707 chunk
.prefix
= loc
->prefix
;
2708 chunk
.block_bits_length
= loc
->block_bits_length
;
2709 chunk
.node_bits_length
= loc
->node_bits_length
;
2710 chunk
.function_bits_length
= loc
->function_bits_length
;
2711 chunk
.argument_bits_length
= loc
->argument_bits_length
;
2713 chunk
.proto
= client
->proto
;
2714 chunk
.instance
= client
->instance
;
2715 chunk
.flags
= loc
->flags
;
2717 zclient_create_header(s
, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
, vrf_id
);
2718 zapi_srv6_locator_chunk_encode(s
, &chunk
);
2719 stream_putw_at(s
, 0, stream_get_endp(s
));
2720 return zserv_send_message(client
, s
);
2723 /* Send response to a table manager connect request to client */
2724 static void zread_table_manager_connect(struct zserv
*client
,
2725 struct stream
*msg
, vrf_id_t vrf_id
)
2730 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2735 STREAM_GETC(s
, proto
);
2736 STREAM_GETW(s
, instance
);
2738 /* accept only dynamic routing protocols */
2739 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2740 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2741 "client %d has wrong protocol %s", client
->sock
,
2742 zebra_route_string(proto
));
2743 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2746 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2747 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2748 zebra_route_string(proto
));
2749 client
->proto
= proto
;
2750 client
->instance
= instance
;
2753 * Release previous labels of same protocol and instance.
2754 * This is done in case it restarted from an unexpected shutdown.
2756 release_daemon_table_chunks(client
);
2758 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2764 static void zread_label_manager_connect(struct zserv
*client
,
2765 struct stream
*msg
, vrf_id_t vrf_id
)
2768 /* type of protocol (lib/zebra.h) */
2770 unsigned short instance
;
2772 /* Get input stream. */
2776 STREAM_GETC(s
, proto
);
2777 STREAM_GETW(s
, instance
);
2779 /* accept only dynamic routing protocols */
2780 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2781 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2782 "client %d has wrong protocol %s", client
->sock
,
2783 zebra_route_string(proto
));
2784 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2788 /* recall proto and instance in this socket */
2789 client
->proto
= proto
;
2790 client
->instance
= instance
;
2792 /* call hook for connection using wrapper */
2793 lm_client_connect_call(client
, vrf_id
);
2799 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2804 uint32_t size
, base
;
2805 struct label_manager_chunk
*lmc
= NULL
;
2807 unsigned short instance
;
2809 /* Get input stream. */
2813 STREAM_GETC(s
, proto
);
2814 STREAM_GETW(s
, instance
);
2815 STREAM_GETC(s
, keep
);
2816 STREAM_GETL(s
, size
);
2817 STREAM_GETL(s
, base
);
2819 assert(proto
== client
->proto
&& instance
== client
->instance
);
2821 /* call hook to get a chunk using wrapper */
2822 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2828 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2831 uint32_t start
, end
;
2833 unsigned short instance
;
2835 /* Get input stream. */
2839 STREAM_GETC(s
, proto
);
2840 STREAM_GETW(s
, instance
);
2841 STREAM_GETL(s
, start
);
2842 STREAM_GETL(s
, end
);
2844 assert(proto
== client
->proto
&& instance
== client
->instance
);
2846 /* call hook to release a chunk using wrapper */
2847 lm_release_chunk_call(client
, start
, end
);
2853 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2855 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2856 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2857 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2859 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2860 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2861 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2862 zread_release_label_chunk(client
, msg
);
2866 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2867 struct zebra_vrf
*zvrf
)
2871 struct table_manager_chunk
*tmc
;
2873 /* Get input stream. */
2877 STREAM_GETL(s
, size
);
2879 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
, zvrf
);
2881 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2882 "%s: Unable to assign Table Chunk of size %u",
2885 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2887 /* send response back */
2888 zsend_assign_table_chunk_response(client
, zvrf_id(zvrf
), tmc
);
2894 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
,
2895 struct zebra_vrf
*zvrf
)
2898 uint32_t start
, end
;
2900 /* Get input stream. */
2904 STREAM_GETL(s
, start
);
2905 STREAM_GETL(s
, end
);
2907 release_table_chunk(client
->proto
, client
->instance
, start
, end
, zvrf
);
2913 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2915 /* to avoid sending other messages like ZEBRA_INTERFACE_UP */
2916 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2917 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2919 /* Sanity: don't allow 'unidentified' requests */
2920 if (!client
->proto
) {
2923 "Got SRv6 request from an unidentified client");
2926 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2927 zread_get_table_chunk(client
, msg
, zvrf
);
2928 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2929 zread_release_table_chunk(client
, msg
, zvrf
);
2933 static void zread_srv6_manager_get_locator_chunk(struct zserv
*client
,
2937 struct stream
*s
= msg
;
2939 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2942 STREAM_GETW(s
, len
);
2943 STREAM_GET(locator_name
, s
, len
);
2945 /* call hook to get a chunk using wrapper */
2946 struct srv6_locator
*loc
= NULL
;
2947 srv6_manager_get_locator_chunk_call(&loc
, client
, locator_name
, vrf_id
);
2953 static void zread_srv6_manager_release_locator_chunk(struct zserv
*client
,
2957 struct stream
*s
= msg
;
2959 char locator_name
[SRV6_LOCNAME_SIZE
] = {0};
2962 STREAM_GETW(s
, len
);
2963 STREAM_GET(locator_name
, s
, len
);
2965 /* call hook to release a chunk using wrapper */
2966 srv6_manager_release_locator_chunk_call(client
, locator_name
, vrf_id
);
2972 static void zread_srv6_manager_request(ZAPI_HANDLER_ARGS
)
2974 switch (hdr
->command
) {
2975 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
:
2976 zread_srv6_manager_get_locator_chunk(client
, msg
,
2979 case ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
:
2980 zread_srv6_manager_release_locator_chunk(client
, msg
,
2984 zlog_err("%s: unknown SRv6 Manager command", __func__
);
2989 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2992 char ifname
[INTERFACE_NAMSIZ
];
2996 union g_addr nexthop
;
2997 uint32_t local_label
;
2998 uint32_t remote_label
;
3000 union pw_protocol_fields data
;
3002 struct zebra_pw
*pw
;
3004 /* Get input stream. */
3008 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3009 ifname
[INTERFACE_NAMSIZ
- 1] = '\0';
3010 STREAM_GETL(s
, ifindex
);
3011 STREAM_GETL(s
, type
);
3015 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
3018 STREAM_GET(&nexthop
.ipv6
, s
, 16);
3023 STREAM_GETL(s
, local_label
);
3024 STREAM_GETL(s
, remote_label
);
3025 STREAM_GETC(s
, flags
);
3026 STREAM_GET(&data
, s
, sizeof(data
));
3027 protocol
= client
->proto
;
3029 pw
= zebra_pw_find(zvrf
, ifname
);
3030 switch (hdr
->command
) {
3033 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
3034 "%s: pseudowire %s already exists [%s]",
3036 zserv_command_string(hdr
->command
));
3040 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
3042 case ZEBRA_PW_DELETE
:
3044 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3045 "%s: pseudowire %s not found [%s]", __func__
,
3046 ifname
, zserv_command_string(hdr
->command
));
3050 zebra_pw_del(zvrf
, pw
);
3053 case ZEBRA_PW_UNSET
:
3055 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
3056 "%s: pseudowire %s not found [%s]", __func__
,
3057 ifname
, zserv_command_string(hdr
->command
));
3061 switch (hdr
->command
) {
3065 case ZEBRA_PW_UNSET
:
3070 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
3071 remote_label
, flags
, &data
);
3079 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
3081 struct interface
*master
;
3082 struct interface
*slave
;
3083 struct stream
*s
= msg
;
3087 STREAM_GETL(s
, vrf_id
);
3088 STREAM_GETL(s
, ifindex
);
3089 master
= if_lookup_by_index(ifindex
, vrf_id
);
3091 STREAM_GETL(s
, vrf_id
);
3092 STREAM_GETL(s
, ifindex
);
3093 slave
= if_lookup_by_index(ifindex
, vrf_id
);
3095 if (!master
|| !slave
)
3098 kernel_interface_set_master(master
, slave
);
3105 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
3107 struct interface
*ifp
;
3108 mpls_label_t nlabel
;
3111 struct zebra_vrf
*def_zvrf
;
3112 enum lsp_types_t ltype
;
3115 STREAM_GETL(s
, nlabel
);
3116 STREAM_GETC(s
, afi
);
3118 if (!(IS_VALID_AFI(afi
))) {
3119 zlog_warn("Invalid AFI for VRF label: %u", afi
);
3123 if (nlabel
== zvrf
->label
[afi
]) {
3125 * Nothing to do here move along
3130 STREAM_GETC(s
, ltype
);
3132 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
3133 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
3135 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
3138 zlog_debug("Unable to find specified Interface for %s",
3143 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3145 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
3149 really_remove
= true;
3150 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
3151 if (scrubber
== afi
)
3154 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
3157 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
3158 really_remove
= false;
3164 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
3165 NEXTHOP_TYPE_IFINDEX
, NULL
,
3166 ifp
->ifindex
, false /*backup*/);
3169 if (nlabel
!= MPLS_LABEL_NONE
) {
3170 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
3171 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
3172 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
3175 zvrf
->label
[afi
] = nlabel
;
3176 zvrf
->label_proto
[afi
] = client
->proto
;
3182 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
3184 struct zebra_pbr_rule zpr
;
3187 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
3190 STREAM_GETL(s
, total
);
3192 for (i
= 0; i
< total
; i
++) {
3193 memset(&zpr
, 0, sizeof(zpr
));
3195 zpr
.sock
= client
->sock
;
3196 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
3197 STREAM_GETL(s
, zpr
.rule
.seq
);
3198 STREAM_GETL(s
, zpr
.rule
.priority
);
3199 STREAM_GETL(s
, zpr
.rule
.unique
);
3200 STREAM_GETC(s
, zpr
.rule
.filter
.ip_proto
);
3201 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
3202 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
3203 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
3204 prefix_blen(&zpr
.rule
.filter
.src_ip
));
3205 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
3206 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
3207 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
3208 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
3209 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
3210 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
3211 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
3212 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
3214 STREAM_GETL(s
, zpr
.rule
.action
.queue_id
);
3215 STREAM_GETW(s
, zpr
.rule
.action
.vlan_id
);
3216 STREAM_GETW(s
, zpr
.rule
.action
.vlan_flags
);
3217 STREAM_GETW(s
, zpr
.rule
.action
.pcp
);
3219 STREAM_GETL(s
, zpr
.rule
.action
.table
);
3220 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3222 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
3223 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
3225 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
3226 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
3228 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
3229 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
3231 if (zpr
.rule
.filter
.src_port
)
3232 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3234 if (zpr
.rule
.filter
.dst_port
)
3235 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
3237 if (zpr
.rule
.filter
.dsfield
)
3238 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
3240 if (zpr
.rule
.filter
.ip_proto
)
3241 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_IP_PROTOCOL
;
3243 if (zpr
.rule
.filter
.fwmark
)
3244 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
3246 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
3247 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
3249 "Unsupported PBR source IP family: %s (%hhu)",
3250 family2str(zpr
.rule
.filter
.src_ip
.family
),
3251 zpr
.rule
.filter
.src_ip
.family
);
3254 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
3255 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
3257 "Unsupported PBR destination IP family: %s (%hhu)",
3258 family2str(zpr
.rule
.filter
.dst_ip
.family
),
3259 zpr
.rule
.filter
.dst_ip
.family
);
3264 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
3265 if (hdr
->command
== ZEBRA_RULE_ADD
)
3266 zebra_pbr_add_rule(&zpr
);
3268 zebra_pbr_del_rule(&zpr
);
3275 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
3277 struct zebra_pbr_ipset zpi
;
3282 STREAM_GETL(s
, total
);
3284 for (i
= 0; i
< total
; i
++) {
3285 memset(&zpi
, 0, sizeof(zpi
));
3287 zpi
.sock
= client
->sock
;
3288 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3289 STREAM_GETL(s
, zpi
.unique
);
3290 STREAM_GETL(s
, zpi
.type
);
3291 STREAM_GETC(s
, zpi
.family
);
3292 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3294 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3295 zebra_pbr_create_ipset(&zpi
);
3297 zebra_pbr_destroy_ipset(&zpi
);
3304 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3306 struct zebra_pbr_ipset_entry zpi
;
3307 struct zebra_pbr_ipset ipset
;
3312 STREAM_GETL(s
, total
);
3314 for (i
= 0; i
< total
; i
++) {
3315 memset(&zpi
, 0, sizeof(zpi
));
3316 memset(&ipset
, 0, sizeof(ipset
));
3318 zpi
.sock
= client
->sock
;
3319 STREAM_GETL(s
, zpi
.unique
);
3320 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3321 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3322 STREAM_GETC(s
, zpi
.src
.family
);
3323 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3324 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3325 STREAM_GETC(s
, zpi
.dst
.family
);
3326 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3327 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3329 STREAM_GETW(s
, zpi
.src_port_min
);
3330 STREAM_GETW(s
, zpi
.src_port_max
);
3331 STREAM_GETW(s
, zpi
.dst_port_min
);
3332 STREAM_GETW(s
, zpi
.dst_port_max
);
3333 STREAM_GETC(s
, zpi
.proto
);
3334 if (!is_default_prefix(&zpi
.src
))
3335 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3337 if (!is_default_prefix(&zpi
.dst
))
3338 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3339 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3340 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3341 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3342 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3343 if (zpi
.dst_port_max
!= 0)
3344 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3345 if (zpi
.src_port_max
!= 0)
3346 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3348 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3350 if (!(zpi
.dst
.family
== AF_INET
3351 || zpi
.dst
.family
== AF_INET6
)) {
3353 "Unsupported PBR destination IP family: %s (%hhu)",
3354 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3355 goto stream_failure
;
3357 if (!(zpi
.src
.family
== AF_INET
3358 || zpi
.src
.family
== AF_INET6
)) {
3360 "Unsupported PBR source IP family: %s (%hhu)",
3361 family2str(zpi
.src
.family
), zpi
.src
.family
);
3362 goto stream_failure
;
3365 /* calculate backpointer */
3367 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3369 if (!zpi
.backpointer
) {
3370 zlog_warn("ipset name specified: %s does not exist",
3372 goto stream_failure
;
3375 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3376 zebra_pbr_add_ipset_entry(&zpi
);
3378 zebra_pbr_del_ipset_entry(&zpi
);
3386 static inline void zebra_neigh_register(ZAPI_HANDLER_ARGS
)
3390 STREAM_GETW(msg
, afi
);
3391 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3393 "Invalid AFI %u while registering for neighbors notifications",
3395 goto stream_failure
;
3397 vrf_bitmap_set(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3402 static inline void zebra_neigh_unregister(ZAPI_HANDLER_ARGS
)
3406 STREAM_GETW(msg
, afi
);
3407 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
3409 "Invalid AFI %u while unregistering from neighbor notifications",
3411 goto stream_failure
;
3413 vrf_bitmap_unset(client
->nhrp_neighinfo
[afi
], zvrf_id(zvrf
));
3418 static inline void zebra_gre_get(ZAPI_HANDLER_ARGS
)
3422 struct interface
*ifp
;
3423 struct zebra_if
*zebra_if
= NULL
;
3424 struct zebra_l2info_gre
*gre_info
;
3425 struct interface
*ifp_link
= NULL
;
3426 vrf_id_t vrf_id_link
= VRF_UNKNOWN
;
3427 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3430 STREAM_GETL(s
, idx
);
3431 ifp
= if_lookup_by_index(idx
, vrf_id
);
3434 zebra_if
= ifp
->info
;
3436 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3438 zclient_create_header(s
, ZEBRA_GRE_UPDATE
, vrf_id
);
3440 if (ifp
&& IS_ZEBRA_IF_GRE(ifp
) && zebra_if
) {
3441 gre_info
= &zebra_if
->l2info
.gre
;
3443 stream_putl(s
, idx
);
3444 stream_putl(s
, gre_info
->ikey
);
3445 stream_putl(s
, gre_info
->ikey
);
3446 stream_putl(s
, gre_info
->ifindex_link
);
3448 ifp_link
= if_lookup_by_index_per_ns(
3449 zebra_ns_lookup(gre_info
->link_nsid
),
3450 gre_info
->ifindex_link
);
3452 vrf_id_link
= ifp_link
->vrf
->vrf_id
;
3453 stream_putl(s
, vrf_id_link
);
3454 stream_putl(s
, gre_info
->vtep_ip
.s_addr
);
3455 stream_putl(s
, gre_info
->vtep_ip_remote
.s_addr
);
3457 stream_putl(s
, idx
);
3460 stream_putl(s
, IFINDEX_INTERNAL
);
3461 stream_putl(s
, VRF_UNKNOWN
);
3465 /* Write packet size. */
3466 stream_putw_at(s
, 0, stream_get_endp(s
));
3467 zserv_send_message(client
, s
);
3474 static inline void zebra_configure_arp(ZAPI_HANDLER_ARGS
)
3479 struct interface
*ifp
;
3482 STREAM_GETC(s
, fam
);
3483 if (fam
!= AF_INET
&& fam
!= AF_INET6
)
3485 STREAM_GETL(s
, idx
);
3486 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, idx
);
3489 dplane_neigh_table_update(ifp
, fam
, 1, 0, 0);
3494 static inline void zebra_neigh_ip_add(ZAPI_HANDLER_ARGS
)
3497 struct zapi_neigh_ip api
= {};
3499 const 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_INSTALL
, ifp
, &api
.ip_out
,
3509 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3513 static inline void zebra_neigh_ip_del(ZAPI_HANDLER_ARGS
)
3516 struct zapi_neigh_ip api
= {};
3518 struct interface
*ifp
;
3521 ret
= zclient_neigh_ip_decode(s
, &api
);
3524 ifp
= if_lookup_by_index(api
.index
, zvrf_id(zvrf
));
3527 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_DELETE
, ifp
, &api
.ip_out
,
3528 &api
.ip_in
, api
.ndm_state
, client
->proto
);
3532 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3534 struct zebra_pbr_iptable
*zpi
=
3535 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
3540 zpi
->interface_name_list
= list_new();
3541 zpi
->sock
= client
->sock
;
3542 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3543 STREAM_GETL(s
, zpi
->unique
);
3544 STREAM_GETL(s
, zpi
->type
);
3545 STREAM_GETL(s
, zpi
->filter_bm
);
3546 STREAM_GETL(s
, zpi
->action
);
3547 STREAM_GETL(s
, zpi
->fwmark
);
3548 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3549 STREAM_GETC(s
, zpi
->family
);
3550 STREAM_GETW(s
, zpi
->pkt_len_min
);
3551 STREAM_GETW(s
, zpi
->pkt_len_max
);
3552 STREAM_GETW(s
, zpi
->tcp_flags
);
3553 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3554 STREAM_GETC(s
, zpi
->dscp_value
);
3555 STREAM_GETC(s
, zpi
->fragment
);
3556 STREAM_GETC(s
, zpi
->protocol
);
3557 STREAM_GETW(s
, zpi
->flow_label
);
3558 STREAM_GETL(s
, zpi
->nb_interface
);
3559 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3561 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3562 zebra_pbr_add_iptable(zpi
);
3564 zebra_pbr_del_iptable(zpi
);
3567 zebra_pbr_iptable_free(zpi
);
3572 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3576 struct interface
*ifp
;
3582 STREAM_GETL(s
, ifindex
);
3584 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3586 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3590 STREAM_GETC(s
, p
.family
);
3591 STREAM_GETC(s
, p
.prefixlen
);
3592 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3594 if (p
.family
== AF_INET
)
3599 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3601 dplane_neigh_discover(ifp
, &ip
);
3607 static inline void zebra_gre_source_set(ZAPI_HANDLER_ARGS
)
3610 ifindex_t idx
, link_idx
;
3611 vrf_id_t link_vrf_id
;
3612 struct interface
*ifp
;
3613 struct interface
*ifp_link
;
3614 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3615 struct zebra_if
*zif
, *gre_zif
;
3616 struct zebra_l2info_gre
*gre_info
;
3620 STREAM_GETL(s
, idx
);
3621 ifp
= if_lookup_by_index(idx
, vrf_id
);
3622 STREAM_GETL(s
, link_idx
);
3623 STREAM_GETL(s
, link_vrf_id
);
3624 STREAM_GETL(s
, mtu
);
3626 ifp_link
= if_lookup_by_index(link_idx
, link_vrf_id
);
3627 if (!ifp_link
|| !ifp
) {
3628 zlog_warn("GRE (index %u, VRF %u) or GRE link interface (index %u, VRF %u) not found, when setting GRE params",
3629 idx
, vrf_id
, link_idx
, link_vrf_id
);
3633 if (!IS_ZEBRA_IF_GRE(ifp
))
3636 gre_zif
= (struct zebra_if
*)ifp
->info
;
3637 zif
= (struct zebra_if
*)ifp_link
->info
;
3638 if (!zif
|| !gre_zif
)
3641 gre_info
= &zif
->l2info
.gre
;
3648 /* if gre link already set or mtu did not change, do not set it */
3649 if (gre_zif
->link
&& gre_zif
->link
== ifp_link
&& mtu
== ifp
->mtu
)
3652 dplane_gre_set(ifp
, ifp_link
, mtu
, gre_info
);
3658 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3659 struct zmsghdr
*bad_hdr
)
3662 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3664 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3666 zserv_encode_error(s
, error
);
3668 client
->error_cnt
++;
3669 zserv_send_message(client
, s
);
3672 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3674 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3675 zlog_debug("ZAPI message specifies unknown VRF: %d",
3678 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3681 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3683 zlog_info("Zebra received unknown command %d", hdr
->command
);
3685 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3688 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3689 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3690 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3691 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3692 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3693 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3694 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3695 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3696 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3697 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3698 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3699 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3700 [ZEBRA_NEXTHOP_LOOKUP_MRIB
] = zread_nexthop_lookup_mrib
,
3701 [ZEBRA_HELLO
] = zread_hello
,
3702 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3703 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3704 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3705 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3706 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3708 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3709 #endif /* HAVE_BFDD */
3710 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3711 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3712 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3713 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3714 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3715 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3716 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3717 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3718 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3719 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3720 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3721 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3722 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3723 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3724 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3725 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3726 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3727 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3728 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3729 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3730 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3731 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3732 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3733 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add_zapi
,
3734 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del_zapi
,
3735 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3736 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3737 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3738 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3739 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3740 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3741 [ZEBRA_PW_SET
] = zread_pseudowire
,
3742 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3743 [ZEBRA_RULE_ADD
] = zread_rule
,
3744 [ZEBRA_RULE_DELETE
] = zread_rule
,
3745 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3746 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3747 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3748 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3749 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3750 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3751 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3752 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3753 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3754 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3755 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3756 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3757 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3758 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3759 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3760 [ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK
] = zread_srv6_manager_request
,
3761 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3762 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3763 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3764 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3765 [ZEBRA_ROUTE_NOTIFY_REQUEST
] = zread_route_notify_request
,
3766 [ZEBRA_EVPN_REMOTE_NH_ADD
] = zebra_evpn_proc_remote_nh
,
3767 [ZEBRA_EVPN_REMOTE_NH_DEL
] = zebra_evpn_proc_remote_nh
,
3768 [ZEBRA_NEIGH_IP_ADD
] = zebra_neigh_ip_add
,
3769 [ZEBRA_NEIGH_IP_DEL
] = zebra_neigh_ip_del
,
3770 [ZEBRA_NHRP_NEIGH_REGISTER
] = zebra_neigh_register
,
3771 [ZEBRA_NHRP_NEIGH_UNREGISTER
] = zebra_neigh_unregister
,
3772 [ZEBRA_CONFIGURE_ARP
] = zebra_configure_arp
,
3773 [ZEBRA_GRE_GET
] = zebra_gre_get
,
3774 [ZEBRA_GRE_SOURCE_SET
] = zebra_gre_source_set
,
3778 * Process a batch of zapi messages.
3780 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3783 struct zebra_vrf
*zvrf
;
3785 struct stream_fifo temp_fifo
;
3787 stream_fifo_init(&temp_fifo
);
3789 while (stream_fifo_head(fifo
)) {
3790 msg
= stream_fifo_pop(fifo
);
3792 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3793 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3795 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3796 STREAM_READABLE(msg
),
3797 ZEBRA_MAX_PACKET_SIZ
);
3801 zapi_parse_header(msg
, &hdr
);
3803 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3804 && IS_ZEBRA_DEBUG_DETAIL
)
3805 zserv_log_message(NULL
, msg
, &hdr
);
3807 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3809 /* Before checking for a handler function, check for
3810 * special messages that are handled in another module;
3811 * we'll treat these as opaque.
3813 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3814 /* Reset message buffer */
3815 stream_set_getp(msg
, 0);
3817 stream_fifo_push(&temp_fifo
, msg
);
3819 /* Continue without freeing the message */
3825 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3827 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3831 if (hdr
.command
>= array_size(zserv_handlers
)
3832 || zserv_handlers
[hdr
.command
] == NULL
) {
3833 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3837 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3843 /* Dispatch any special messages from the temp fifo */
3844 if (stream_fifo_head(&temp_fifo
) != NULL
)
3845 zebra_opaque_enqueue_batch(&temp_fifo
);
3847 stream_fifo_deinit(&temp_fifo
);