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/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/rib.h"
46 #include "zebra/zebra_memory.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/router-id.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/debug.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/rt_netlink.h"
54 #include "zebra/interface.h"
55 #include "zebra/zebra_ptm.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/zebra_mpls.h"
58 #include "zebra/zebra_mroute.h"
59 #include "zebra/label_manager.h"
60 #include "zebra/zebra_vxlan.h"
62 #include "zebra/zebra_pbr.h"
63 #include "zebra/table_manager.h"
64 #include "zebra/zapi_msg.h"
65 #include "zebra/zebra_errors.h"
66 #include "zebra/zebra_mlag.h"
68 /* Encoding helpers -------------------------------------------------------- */
70 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
72 /* Interface information. */
73 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
74 stream_putl(s
, ifp
->ifindex
);
75 stream_putc(s
, ifp
->status
);
76 stream_putq(s
, ifp
->flags
);
77 stream_putc(s
, ifp
->ptm_enable
);
78 stream_putc(s
, ifp
->ptm_status
);
79 stream_putl(s
, ifp
->metric
);
80 stream_putl(s
, ifp
->speed
);
81 stream_putl(s
, ifp
->mtu
);
82 stream_putl(s
, ifp
->mtu6
);
83 stream_putl(s
, ifp
->bandwidth
);
84 stream_putl(s
, ifp
->ll_type
);
85 stream_putl(s
, ifp
->hw_addr_len
);
87 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
89 /* Then, Traffic Engineering parameters if any */
90 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
92 zebra_interface_link_params_write(s
, ifp
);
96 /* Write packet size. */
97 stream_putw_at(s
, 0, stream_get_endp(s
));
100 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
102 struct vrf_data data
;
103 const char *netns_name
= zvrf_ns_name(zvrf
);
105 data
.l
.table_id
= zvrf
->table_id
;
108 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
111 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
112 /* Pass the tableid and the netns NAME */
113 stream_put(s
, &data
, sizeof(struct vrf_data
));
114 /* Interface information. */
115 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
116 /* Write packet size. */
117 stream_putw_at(s
, 0, stream_get_endp(s
));
120 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
122 stream_putl(s
, nexthop
->vrf_id
);
123 stream_putc(s
, nexthop
->type
);
124 switch (nexthop
->type
) {
125 case NEXTHOP_TYPE_IPV4
:
126 case NEXTHOP_TYPE_IPV4_IFINDEX
:
127 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
128 stream_putl(s
, nexthop
->ifindex
);
130 case NEXTHOP_TYPE_IPV6
:
131 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
133 case NEXTHOP_TYPE_IPV6_IFINDEX
:
134 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
135 stream_putl(s
, nexthop
->ifindex
);
137 case NEXTHOP_TYPE_IFINDEX
:
138 stream_putl(s
, nexthop
->ifindex
);
147 /* Send handlers ----------------------------------------------------------- */
149 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
151 * This function is called in the following situations:
152 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
154 * - at startup, when zebra figures out the available interfaces
155 * - when an interface is added (where support for
156 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
157 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
160 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
162 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
164 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
165 zserv_encode_interface(s
, ifp
);
168 return zserv_send_message(client
, s
);
171 /* Interface deletion from zebra daemon. */
172 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
174 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
176 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
177 zserv_encode_interface(s
, ifp
);
180 return zserv_send_message(client
, s
);
183 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
185 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
187 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
188 zserv_encode_vrf(s
, zvrf
);
190 client
->vrfadd_cnt
++;
191 return zserv_send_message(client
, s
);
194 /* VRF deletion from zebra daemon. */
195 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
198 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
200 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
201 zserv_encode_vrf(s
, zvrf
);
203 client
->vrfdel_cnt
++;
204 return zserv_send_message(client
, s
);
207 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
209 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
211 /* Check this client need interface information. */
212 if (!client
->ifinfo
) {
217 if (!ifp
->link_params
) {
222 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
224 /* Add Interface Index */
225 stream_putl(s
, ifp
->ifindex
);
227 /* Then TE Link Parameters */
228 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
233 /* Write packet size. */
234 stream_putw_at(s
, 0, stream_get_endp(s
));
236 return zserv_send_message(client
, s
);
239 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
240 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
242 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
243 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
244 * from the client, after the ZEBRA_INTERFACE_ADD has been
245 * sent from zebra to the client
246 * - redistribute new address info to all clients in the following situations
247 * - at startup, when zebra figures out the available interfaces
248 * - when an interface is added (where support for
249 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
250 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
252 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
253 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
254 * - when an RTM_NEWADDR message is received from the kernel,
256 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
258 * zsend_interface_address(DELETE)
261 * zebra_interface_address_delete_update
263 * | | if_delete_update
265 * ip_address_uninstall connected_delete_ipv4
266 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
269 * | RTM_NEWADDR on routing/netlink socket
272 * "no ip address A.B.C.D/M [label LINE]"
273 * "no ip address A.B.C.D/M"
274 * ["no ipv6 address X:X::X:X/M"]
277 int zsend_interface_address(int cmd
, struct zserv
*client
,
278 struct interface
*ifp
, struct connected
*ifc
)
282 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
284 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
285 stream_putl(s
, ifp
->ifindex
);
287 /* Interface address flag. */
288 stream_putc(s
, ifc
->flags
);
290 /* Prefix information. */
292 stream_putc(s
, p
->family
);
293 blen
= prefix_blen(p
);
294 stream_put(s
, &p
->u
.prefix
, blen
);
297 * XXX gnu version does not send prefixlen for
298 * ZEBRA_INTERFACE_ADDRESS_DELETE
299 * but zebra_interface_address_delete_read() in the gnu version
302 stream_putc(s
, p
->prefixlen
);
305 p
= ifc
->destination
;
307 stream_put(s
, &p
->u
.prefix
, blen
);
309 stream_put(s
, NULL
, blen
);
311 /* Write packet size. */
312 stream_putw_at(s
, 0, stream_get_endp(s
));
314 client
->connected_rt_add_cnt
++;
315 return zserv_send_message(client
, s
);
318 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
319 struct interface
*ifp
,
320 struct nbr_connected
*ifc
)
323 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
326 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
327 stream_putl(s
, ifp
->ifindex
);
329 /* Prefix information. */
331 stream_putc(s
, p
->family
);
332 blen
= prefix_blen(p
);
333 stream_put(s
, &p
->u
.prefix
, blen
);
336 * XXX gnu version does not send prefixlen for
337 * ZEBRA_INTERFACE_ADDRESS_DELETE
338 * but zebra_interface_address_delete_read() in the gnu version
341 stream_putc(s
, p
->prefixlen
);
343 /* Write packet size. */
344 stream_putw_at(s
, 0, stream_get_endp(s
));
346 return zserv_send_message(client
, s
);
349 /* Interface address addition. */
350 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
351 struct nbr_connected
*ifc
)
353 struct listnode
*node
, *nnode
;
354 struct zserv
*client
;
357 if (IS_ZEBRA_DEBUG_EVENT
) {
358 char buf
[INET6_ADDRSTRLEN
];
362 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
363 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
365 p
->prefixlen
, ifc
->ifp
->name
);
368 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
369 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
373 /* Interface address deletion. */
374 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
375 struct nbr_connected
*ifc
)
377 struct listnode
*node
, *nnode
;
378 struct zserv
*client
;
381 if (IS_ZEBRA_DEBUG_EVENT
) {
382 char buf
[INET6_ADDRSTRLEN
];
386 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
387 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
389 p
->prefixlen
, ifc
->ifp
->name
);
392 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
393 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
397 /* Send addresses on interface to client */
398 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
400 struct listnode
*cnode
, *cnnode
;
402 struct nbr_connected
*nc
;
404 /* Send interface addresses. */
405 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
406 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
409 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
415 /* Send interface neighbors. */
416 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
417 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
426 /* Notify client about interface moving from one VRF to another.
427 * Whether client is interested in old and new VRF is checked by caller.
429 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
432 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
434 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
436 /* Fill in the name of the interface and its new VRF (id) */
437 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
438 stream_putl(s
, vrf_id
);
440 /* Write packet size. */
441 stream_putw_at(s
, 0, stream_get_endp(s
));
443 client
->if_vrfchg_cnt
++;
444 return zserv_send_message(client
, s
);
447 /* Add new nbr connected IPv6 address */
448 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
450 struct nbr_connected
*ifc
;
454 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
455 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
457 ifc
= listnode_head(ifp
->nbr_connected
);
460 ifc
= nbr_connected_new();
461 ifc
->address
= prefix_new();
463 listnode_add(ifp
->nbr_connected
, ifc
);
466 prefix_copy(ifc
->address
, &p
);
468 zebra_interface_nbr_address_add_update(ifp
, ifc
);
470 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
473 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
475 struct nbr_connected
*ifc
;
479 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
480 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
482 ifc
= nbr_connected_check(ifp
, &p
);
486 listnode_delete(ifp
->nbr_connected
, ifc
);
488 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
490 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
492 nbr_connected_free(ifc
);
496 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
497 * ZEBRA_INTERFACE_DOWN.
499 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
500 * the clients in one of 2 situations:
501 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
502 * - a vty command modifying the bandwidth of an interface is received.
503 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
505 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
507 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
509 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
510 zserv_encode_interface(s
, ifp
);
512 if (cmd
== ZEBRA_INTERFACE_UP
)
515 client
->ifdown_cnt
++;
517 return zserv_send_message(client
, s
);
520 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
521 const struct prefix
*p
,
522 const struct prefix
*src_p
, struct route_entry
*re
)
524 struct zapi_route api
;
525 struct zapi_nexthop
*api_nh
;
526 struct nexthop
*nexthop
;
530 memset(&api
, 0, sizeof(api
));
531 api
.vrf_id
= re
->vrf_id
;
533 api
.instance
= re
->instance
;
534 api
.flags
= re
->flags
;
536 afi
= family2afi(p
->family
);
539 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
540 client
->redist_v4_add_cnt
++;
542 client
->redist_v4_del_cnt
++;
545 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
546 client
->redist_v6_add_cnt
++;
548 client
->redist_v6_del_cnt
++;
557 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
558 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
562 if (re
->nexthop_active_num
) {
563 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
564 api
.nexthop_num
= re
->nexthop_active_num
;
566 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
567 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
570 api_nh
= &api
.nexthops
[count
];
571 api_nh
->vrf_id
= nexthop
->vrf_id
;
572 api_nh
->type
= nexthop
->type
;
573 switch (nexthop
->type
) {
574 case NEXTHOP_TYPE_BLACKHOLE
:
575 api_nh
->bh_type
= nexthop
->bh_type
;
577 case NEXTHOP_TYPE_IPV4
:
578 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
580 case NEXTHOP_TYPE_IPV4_IFINDEX
:
581 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
582 api_nh
->ifindex
= nexthop
->ifindex
;
584 case NEXTHOP_TYPE_IFINDEX
:
585 api_nh
->ifindex
= nexthop
->ifindex
;
587 case NEXTHOP_TYPE_IPV6
:
588 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
590 case NEXTHOP_TYPE_IPV6_IFINDEX
:
591 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
592 api_nh
->ifindex
= nexthop
->ifindex
;
598 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
599 api
.distance
= re
->distance
;
600 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
601 api
.metric
= re
->metric
;
603 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
606 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
609 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
611 /* Encode route and send. */
612 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
617 if (IS_ZEBRA_DEBUG_SEND
) {
618 char buf_prefix
[PREFIX_STRLEN
];
620 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
622 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
623 __func__
, zserv_command_string(cmd
),
624 zebra_route_string(client
->proto
),
625 zebra_route_string(api
.type
), api
.vrf_id
,
628 return zserv_send_message(client
, s
);
632 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
633 * nexthop is not found on mrib. Returns both route metric and protocol
636 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
638 struct route_entry
*re
,
639 struct zebra_vrf
*zvrf
)
644 struct nexthop
*nexthop
;
646 /* Get output stream. */
647 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
650 /* Fill in result. */
651 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
652 stream_put_in_addr(s
, &addr
);
655 stream_putc(s
, re
->distance
);
656 stream_putl(s
, re
->metric
);
658 /* remember position for nexthop_num */
659 nump
= stream_get_endp(s
);
660 /* reserve room for nexthop_num */
663 * Only non-recursive routes are elegible to resolve the
664 * nexthop we are looking up. Therefore, we will just iterate
665 * over the top chain of nexthops.
667 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
668 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
669 num
+= zserv_encode_nexthop(s
, nexthop
);
671 /* store nexthop_num */
672 stream_putc_at(s
, nump
, num
);
674 stream_putc(s
, 0); /* distance */
675 stream_putl(s
, 0); /* metric */
676 stream_putc(s
, 0); /* nexthop_num */
679 stream_putw_at(s
, 0, stream_get_endp(s
));
681 return zserv_send_message(client
, s
);
685 * Common utility send route notification, called from a path using a
686 * route_entry and from a path using a dataplane context.
688 static int route_notify_internal(const struct prefix
*p
, int type
,
689 uint16_t instance
, vrf_id_t vrf_id
,
691 enum zapi_route_notify_owner note
)
693 struct zserv
*client
;
697 client
= zserv_find_client(type
, instance
);
698 if (!client
|| !client
->notify_owner
) {
699 if (IS_ZEBRA_DEBUG_PACKET
) {
700 char buff
[PREFIX_STRLEN
];
703 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
704 type
, prefix2str(p
, buff
, sizeof(buff
)),
705 table_id
, note
, vrf_id
);
710 if (IS_ZEBRA_DEBUG_PACKET
) {
711 char buff
[PREFIX_STRLEN
];
713 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
714 type
, prefix2str(p
, buff
, sizeof(buff
)),
715 table_id
, note
, vrf_id
);
718 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
721 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
723 stream_put(s
, ¬e
, sizeof(note
));
725 stream_putc(s
, p
->family
);
727 blen
= prefix_blen(p
);
728 stream_putc(s
, p
->prefixlen
);
729 stream_put(s
, &p
->u
.prefix
, blen
);
731 stream_putl(s
, table_id
);
733 stream_putw_at(s
, 0, stream_get_endp(s
));
735 return zserv_send_message(client
, s
);
738 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
739 enum zapi_route_notify_owner note
)
741 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
746 * Route-owner notification using info from dataplane update context.
748 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
749 enum zapi_route_notify_owner note
)
751 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
752 dplane_ctx_get_type(ctx
),
753 dplane_ctx_get_instance(ctx
),
754 dplane_ctx_get_vrf(ctx
),
755 dplane_ctx_get_table(ctx
),
759 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
760 enum zapi_rule_notify_owner note
)
762 struct listnode
*node
;
763 struct zserv
*client
;
766 if (IS_ZEBRA_DEBUG_PACKET
)
767 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
770 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
771 if (rule
->sock
== client
->sock
)
778 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
780 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
781 stream_put(s
, ¬e
, sizeof(note
));
782 stream_putl(s
, rule
->rule
.seq
);
783 stream_putl(s
, rule
->rule
.priority
);
784 stream_putl(s
, rule
->rule
.unique
);
786 stream_putl(s
, rule
->ifp
->ifindex
);
790 stream_putw_at(s
, 0, stream_get_endp(s
));
792 zserv_send_message(client
, s
);
795 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
796 enum zapi_ipset_notify_owner note
)
798 struct listnode
*node
;
799 struct zserv
*client
;
802 if (IS_ZEBRA_DEBUG_PACKET
)
803 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
806 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
807 if (ipset
->sock
== client
->sock
)
814 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
816 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
817 stream_put(s
, ¬e
, sizeof(note
));
818 stream_putl(s
, ipset
->unique
);
819 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
820 stream_putw_at(s
, 0, stream_get_endp(s
));
822 zserv_send_message(client
, s
);
825 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
826 enum zapi_ipset_entry_notify_owner note
)
828 struct listnode
*node
;
829 struct zserv
*client
;
832 if (IS_ZEBRA_DEBUG_PACKET
)
833 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
836 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
837 if (ipset
->sock
== client
->sock
)
844 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
846 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
847 stream_put(s
, ¬e
, sizeof(note
));
848 stream_putl(s
, ipset
->unique
);
849 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
850 stream_putw_at(s
, 0, stream_get_endp(s
));
852 zserv_send_message(client
, s
);
855 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
856 enum zapi_iptable_notify_owner note
)
858 struct listnode
*node
;
859 struct zserv
*client
;
862 if (IS_ZEBRA_DEBUG_PACKET
)
863 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
866 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
867 if (iptable
->sock
== client
->sock
)
874 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
876 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
877 stream_put(s
, ¬e
, sizeof(note
));
878 stream_putl(s
, iptable
->unique
);
879 stream_putw_at(s
, 0, stream_get_endp(s
));
881 zserv_send_message(client
, s
);
884 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
885 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
890 /* Check this client need interface information. */
891 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
894 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
897 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
899 /* Prefix information. */
900 stream_putc(s
, p
->family
);
901 blen
= prefix_blen(p
);
902 stream_put(s
, &p
->u
.prefix
, blen
);
903 stream_putc(s
, p
->prefixlen
);
905 /* Write packet size. */
906 stream_putw_at(s
, 0, stream_get_endp(s
));
908 return zserv_send_message(client
, s
);
912 * Function used by Zebra to send a PW status update to LDP daemon
914 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
916 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
918 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
919 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
920 stream_putl(s
, pw
->ifindex
);
921 stream_putl(s
, pw
->status
);
923 /* Put length at the first point of the stream. */
924 stream_putw_at(s
, 0, stream_get_endp(s
));
926 return zserv_send_message(client
, s
);
929 /* Send response to a get label chunk request to client */
930 static int zsend_assign_label_chunk_response(struct zserv
*client
,
932 struct label_manager_chunk
*lmc
)
935 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
937 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
941 stream_putc(s
, lmc
->proto
);
943 stream_putw(s
, lmc
->instance
);
945 stream_putc(s
, lmc
->keep
);
946 /* start and end labels */
947 stream_putl(s
, lmc
->start
);
948 stream_putl(s
, lmc
->end
);
951 /* Write packet size. */
952 stream_putw_at(s
, 0, stream_get_endp(s
));
954 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
959 /* Send response to a label manager connect request to client */
960 static int zsend_label_manager_connect_response(struct zserv
*client
,
962 unsigned short result
)
965 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
967 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
970 stream_putc(s
, client
->proto
);
973 stream_putw(s
, client
->instance
);
976 stream_putc(s
, result
);
978 /* Write packet size. */
979 stream_putw_at(s
, 0, stream_get_endp(s
));
981 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
987 /* Send response to a get table chunk request to client */
988 static int zsend_assign_table_chunk_response(struct zserv
*client
,
990 struct table_manager_chunk
*tmc
)
992 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
994 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
997 /* start and end labels */
998 stream_putl(s
, tmc
->start
);
999 stream_putl(s
, tmc
->end
);
1002 /* Write packet size. */
1003 stream_putw_at(s
, 0, stream_get_endp(s
));
1005 return zserv_send_message(client
, s
);
1008 static int zsend_table_manager_connect_response(struct zserv
*client
,
1012 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1014 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1017 stream_putc(s
, result
);
1019 stream_putw_at(s
, 0, stream_get_endp(s
));
1021 return zserv_send_message(client
, s
);
1024 /* Inbound message handling ------------------------------------------------ */
1027 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1028 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1029 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1030 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1033 /* Nexthop register */
1034 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1039 unsigned short l
= 0;
1041 uint16_t type
= cmd2type
[hdr
->command
];
1044 if (IS_ZEBRA_DEBUG_NHT
)
1046 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1047 zebra_route_string(client
->proto
), hdr
->length
,
1048 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1053 client
->nh_reg_time
= monotime(NULL
);
1055 while (l
< hdr
->length
) {
1056 STREAM_GETC(s
, flags
);
1057 STREAM_GETW(s
, p
.family
);
1058 STREAM_GETC(s
, p
.prefixlen
);
1060 if (p
.family
== AF_INET
) {
1061 client
->v4_nh_watch_add_cnt
++;
1062 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1064 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1065 __PRETTY_FUNCTION__
, p
.prefixlen
);
1068 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1069 l
+= IPV4_MAX_BYTELEN
;
1070 } else if (p
.family
== AF_INET6
) {
1071 client
->v6_nh_watch_add_cnt
++;
1072 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1074 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1075 __PRETTY_FUNCTION__
, p
.prefixlen
);
1078 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1079 l
+= IPV6_MAX_BYTELEN
;
1082 EC_ZEBRA_UNKNOWN_FAMILY
,
1083 "rnh_register: Received unknown family type %d\n",
1087 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1091 if (type
== RNH_NEXTHOP_TYPE
) {
1093 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1094 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1096 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1097 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1098 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1100 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1101 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1103 && CHECK_FLAG(rnh
->flags
,
1104 ZEBRA_NHT_EXACT_MATCH
))
1105 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1108 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1109 /* Anything not AF_INET/INET6 has been filtered out above */
1111 zebra_evaluate_rnh(zvrf
, p
.family
, 1, type
, &p
);
1118 /* Nexthop register */
1119 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1124 unsigned short l
= 0;
1125 uint16_t type
= cmd2type
[hdr
->command
];
1127 if (IS_ZEBRA_DEBUG_NHT
)
1129 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1130 zebra_route_string(client
->proto
), hdr
->length
,
1135 while (l
< hdr
->length
) {
1138 STREAM_GETC(s
, flags
);
1140 goto stream_failure
;
1142 STREAM_GETW(s
, p
.family
);
1143 STREAM_GETC(s
, p
.prefixlen
);
1145 if (p
.family
== AF_INET
) {
1146 client
->v4_nh_watch_rem_cnt
++;
1147 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1149 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1150 __PRETTY_FUNCTION__
, p
.prefixlen
);
1153 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1154 l
+= IPV4_MAX_BYTELEN
;
1155 } else if (p
.family
== AF_INET6
) {
1156 client
->v6_nh_watch_rem_cnt
++;
1157 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1159 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1160 __PRETTY_FUNCTION__
, p
.prefixlen
);
1163 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1164 l
+= IPV6_MAX_BYTELEN
;
1167 EC_ZEBRA_UNKNOWN_FAMILY
,
1168 "rnh_register: Received unknown family type %d\n",
1172 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1174 client
->nh_dereg_time
= monotime(NULL
);
1175 zebra_remove_rnh_client(rnh
, client
, type
);
1182 #define ZEBRA_MIN_FEC_LENGTH 5
1185 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1188 unsigned short l
= 0;
1191 uint32_t label
= MPLS_INVALID_LABEL
;
1192 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1195 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1200 * The minimum amount of data that can be sent for one fec
1203 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1205 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1206 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1211 while (l
< hdr
->length
) {
1212 STREAM_GETW(s
, flags
);
1213 memset(&p
, 0, sizeof(p
));
1214 STREAM_GETW(s
, p
.family
);
1215 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1217 EC_ZEBRA_UNKNOWN_FAMILY
,
1218 "fec_register: Received unknown family type %d\n",
1222 STREAM_GETC(s
, p
.prefixlen
);
1223 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1224 || (p
.family
== AF_INET6
1225 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1227 "%s: Specified prefix hdr->length: %d is to long for %d",
1228 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1232 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1233 l
+= PSIZE(p
.prefixlen
);
1234 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1235 STREAM_GETL(s
, label
);
1237 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1238 STREAM_GETL(s
, label_index
);
1242 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1249 /* FEC unregister */
1250 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1253 unsigned short l
= 0;
1258 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1263 * The minimum amount of data that can be sent for one
1264 * fec unregistration
1266 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1268 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1269 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1274 while (l
< hdr
->length
) {
1275 STREAM_GETW(s
, flags
);
1277 goto stream_failure
;
1279 memset(&p
, 0, sizeof(p
));
1280 STREAM_GETW(s
, p
.family
);
1281 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1283 EC_ZEBRA_UNKNOWN_FAMILY
,
1284 "fec_unregister: Received unknown family type %d\n",
1288 STREAM_GETC(s
, p
.prefixlen
);
1289 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1290 || (p
.family
== AF_INET6
1291 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1293 "%s: Received prefix hdr->length %d which is greater than %d can support",
1294 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1298 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1299 l
+= PSIZE(p
.prefixlen
);
1300 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1309 * Register zebra server interface information.
1310 * Send current all interface and address information.
1312 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1315 struct interface
*ifp
;
1317 /* Interface information is needed. */
1318 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1320 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1321 FOR_ALL_INTERFACES (vrf
, ifp
) {
1322 /* Skip pseudo interface. */
1323 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1326 zsend_interface_add(client
, ifp
);
1327 zsend_interface_addresses(client
, ifp
);
1332 /* Unregister zebra server interface information. */
1333 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1335 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1338 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1339 const unsigned int nexthop_num
)
1341 if (nexthop_num
> multipath_num
) {
1342 char buff
[PREFIX2STR_BUFFER
];
1344 prefix2str(p
, buff
, sizeof(buff
));
1346 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1347 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1348 caller
, buff
, nexthop_num
, multipath_num
);
1352 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1355 struct zapi_route api
;
1356 struct zapi_nexthop
*api_nh
;
1358 struct prefix_ipv6
*src_p
= NULL
;
1359 struct route_entry
*re
;
1360 struct nexthop
*nexthop
= NULL
;
1362 vrf_id_t vrf_id
= 0;
1363 struct ipaddr vtep_ip
;
1366 if (zapi_route_decode(s
, &api
) < 0) {
1367 if (IS_ZEBRA_DEBUG_RECV
)
1368 zlog_debug("%s: Unable to decode zapi_route sent",
1369 __PRETTY_FUNCTION__
);
1373 if (IS_ZEBRA_DEBUG_RECV
) {
1374 char buf_prefix
[PREFIX_STRLEN
];
1376 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1377 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1378 __func__
, buf_prefix
,
1379 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1384 /* Allocate new route. */
1385 vrf_id
= zvrf_id(zvrf
);
1386 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1387 re
->type
= api
.type
;
1388 re
->instance
= api
.instance
;
1389 re
->flags
= api
.flags
;
1390 re
->uptime
= time(NULL
);
1391 re
->vrf_id
= vrf_id
;
1392 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1393 re
->table
= api
.tableid
;
1395 re
->table
= zvrf
->table_id
;
1398 * TBD should _all_ of the nexthop add operations use
1399 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1400 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1402 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1403 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1404 api_nh
= &api
.nexthops
[i
];
1405 ifindex_t ifindex
= 0;
1407 if (IS_ZEBRA_DEBUG_RECV
)
1408 zlog_debug("nh type %d", api_nh
->type
);
1410 switch (api_nh
->type
) {
1411 case NEXTHOP_TYPE_IFINDEX
:
1412 nexthop
= route_entry_nexthop_ifindex_add(
1413 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1415 case NEXTHOP_TYPE_IPV4
:
1416 if (IS_ZEBRA_DEBUG_RECV
) {
1417 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1419 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1420 nhbuf
, INET6_ADDRSTRLEN
);
1421 zlog_debug("%s: nh=%s, vrf_id=%d",
1425 nexthop
= route_entry_nexthop_ipv4_add(
1426 re
, &api_nh
->gate
.ipv4
, NULL
,
1429 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1431 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1432 if (CHECK_FLAG(api
.flags
,
1433 ZEBRA_FLAG_EVPN_ROUTE
)) {
1434 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1436 ifindex
= api_nh
->ifindex
;
1439 if (IS_ZEBRA_DEBUG_RECV
) {
1440 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1442 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1443 nhbuf
, INET6_ADDRSTRLEN
);
1445 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1446 __func__
, nhbuf
, api_nh
->vrf_id
,
1447 re
->vrf_id
, ifindex
);
1449 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1450 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1453 /* if this an EVPN route entry,
1454 * program the nh as neigh
1456 if (CHECK_FLAG(api
.flags
,
1457 ZEBRA_FLAG_EVPN_ROUTE
)) {
1458 SET_FLAG(nexthop
->flags
,
1459 NEXTHOP_FLAG_EVPN_RVTEP
);
1460 vtep_ip
.ipa_type
= IPADDR_V4
;
1461 memcpy(&(vtep_ip
.ipaddr_v4
),
1462 &(api_nh
->gate
.ipv4
),
1463 sizeof(struct in_addr
));
1464 zebra_vxlan_evpn_vrf_route_add(
1465 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1469 case NEXTHOP_TYPE_IPV6
:
1470 nexthop
= route_entry_nexthop_ipv6_add(
1471 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1473 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1474 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1475 if (CHECK_FLAG(api
.flags
,
1476 ZEBRA_FLAG_EVPN_ROUTE
)) {
1477 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1479 ifindex
= api_nh
->ifindex
;
1482 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1483 re
, &api_nh
->gate
.ipv6
, ifindex
,
1486 /* if this an EVPN route entry,
1487 * program the nh as neigh
1489 if (CHECK_FLAG(api
.flags
,
1490 ZEBRA_FLAG_EVPN_ROUTE
)) {
1491 SET_FLAG(nexthop
->flags
,
1492 NEXTHOP_FLAG_EVPN_RVTEP
);
1493 vtep_ip
.ipa_type
= IPADDR_V6
;
1494 memcpy(&vtep_ip
.ipaddr_v6
,
1495 &(api_nh
->gate
.ipv6
),
1496 sizeof(struct in6_addr
));
1497 zebra_vxlan_evpn_vrf_route_add(
1498 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1502 case NEXTHOP_TYPE_BLACKHOLE
:
1503 nexthop
= route_entry_nexthop_blackhole_add(
1504 re
, api_nh
->bh_type
);
1510 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1511 "%s: Nexthops Specified: %d but we failed to properly create one",
1512 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1513 nexthops_free(re
->ng
.nexthop
);
1514 XFREE(MTYPE_RE
, re
);
1517 /* MPLS labels for BGP-LU or Segment Routing */
1518 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1519 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1520 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1521 enum lsp_types_t label_type
;
1524 lsp_type_from_re_type(client
->proto
);
1526 if (IS_ZEBRA_DEBUG_RECV
) {
1528 "%s: adding %d labels of type %d (1st=%u)",
1529 __func__
, api_nh
->label_num
,
1530 label_type
, api_nh
->labels
[0]);
1533 nexthop_add_labels(nexthop
, label_type
,
1535 &api_nh
->labels
[0]);
1539 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1540 re
->distance
= api
.distance
;
1541 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1542 re
->metric
= api
.metric
;
1543 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1545 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1548 afi
= family2afi(api
.prefix
.family
);
1549 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1550 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1551 "%s: Received SRC Prefix but afi is not v6",
1552 __PRETTY_FUNCTION__
);
1553 nexthops_free(re
->ng
.nexthop
);
1554 XFREE(MTYPE_RE
, re
);
1557 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1558 src_p
= &api
.src_prefix
;
1560 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1563 switch (api
.prefix
.family
) {
1566 client
->v4_route_add_cnt
++;
1568 client
->v4_route_upd8_cnt
++;
1572 client
->v6_route_add_cnt
++;
1574 client
->v6_route_upd8_cnt
++;
1579 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1582 struct zapi_route api
;
1584 struct prefix_ipv6
*src_p
= NULL
;
1588 if (zapi_route_decode(s
, &api
) < 0)
1591 afi
= family2afi(api
.prefix
.family
);
1592 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1593 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1594 "%s: Received a src prefix while afi is not v6",
1595 __PRETTY_FUNCTION__
);
1598 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1599 src_p
= &api
.src_prefix
;
1601 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1602 table_id
= api
.tableid
;
1604 table_id
= zvrf
->table_id
;
1606 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1607 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1608 api
.distance
, false);
1611 switch (api
.prefix
.family
) {
1613 client
->v4_route_del_cnt
++;
1616 client
->v6_route_del_cnt
++;
1621 /* MRIB Nexthop lookup for IPv4. */
1622 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1624 struct in_addr addr
;
1625 struct route_entry
*re
;
1627 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1628 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1629 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1635 /* Register zebra server router-id information. Send current router-id */
1636 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1640 /* Router-id information is needed. */
1641 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1643 router_id_get(&p
, zvrf_id(zvrf
));
1645 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1648 /* Unregister zebra server router-id information. */
1649 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1651 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1654 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1656 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1658 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1659 stream_putc(s
, mpls_enabled
);
1660 stream_putl(s
, multipath_num
);
1661 stream_putc(s
, zebra_mlag_get_role());
1663 stream_putw_at(s
, 0, stream_get_endp(s
));
1664 zserv_send_message(client
, s
);
1667 /* Tie up route-type and client->sock */
1668 static void zread_hello(ZAPI_HANDLER_ARGS
)
1670 /* type of protocol (lib/zebra.h) */
1672 unsigned short instance
;
1675 STREAM_GETC(msg
, proto
);
1676 STREAM_GETW(msg
, instance
);
1677 STREAM_GETC(msg
, notify
);
1679 client
->notify_owner
= true;
1681 /* accept only dynamic routing protocols */
1682 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1684 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1685 client
->sock
, zebra_route_string(proto
),
1688 zlog_notice("client protocol instance %d", instance
);
1690 client
->proto
= proto
;
1691 client
->instance
= instance
;
1694 zsend_capabilities(client
, zvrf
);
1699 /* Unregister all information in a VRF. */
1700 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1705 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1706 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1707 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1708 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1709 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1710 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1713 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1716 enum lsp_types_t type
;
1717 struct prefix prefix
;
1718 enum nexthop_types_t gtype
;
1721 mpls_label_t in_label
, out_label
;
1724 /* Get input stream. */
1728 STREAM_GETC(s
, type
);
1729 STREAM_GETL(s
, prefix
.family
);
1730 switch (prefix
.family
) {
1732 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1733 STREAM_GETC(s
, prefix
.prefixlen
);
1734 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1736 "%s: Specified prefix length %d is greater than a v4 address can support",
1737 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1740 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1743 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1744 STREAM_GETC(s
, prefix
.prefixlen
);
1745 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1747 "%s: Specified prefix length %d is greater than a v6 address can support",
1748 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1751 STREAM_GET(&gate
.ipv6
, s
, 16);
1754 zlog_debug("%s: Specified AF %d is not supported for this call",
1755 __PRETTY_FUNCTION__
, prefix
.family
);
1758 STREAM_GETL(s
, ifindex
);
1759 STREAM_GETC(s
, distance
);
1760 STREAM_GETL(s
, in_label
);
1761 STREAM_GETL(s
, out_label
);
1763 switch (prefix
.family
) {
1766 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1768 gtype
= NEXTHOP_TYPE_IPV4
;
1772 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1774 gtype
= NEXTHOP_TYPE_IPV6
;
1783 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1784 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1786 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1787 distance
, out_label
);
1788 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1789 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1790 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1791 distance
, out_label
);
1797 /* Send response to a table manager connect request to client */
1798 static void zread_table_manager_connect(struct zserv
*client
,
1799 struct stream
*msg
, vrf_id_t vrf_id
)
1808 STREAM_GETC(s
, proto
);
1809 STREAM_GETW(s
, instance
);
1811 /* accept only dynamic routing protocols */
1812 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1813 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1814 "client %d has wrong protocol %s", client
->sock
,
1815 zebra_route_string(proto
));
1816 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1819 zlog_notice("client %d with vrf %u instance %u connected as %s",
1820 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1821 client
->proto
= proto
;
1822 client
->instance
= instance
;
1825 * Release previous labels of same protocol and instance.
1826 * This is done in case it restarted from an unexpected shutdown.
1828 release_daemon_table_chunks(client
);
1830 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1836 static void zread_label_manager_connect(struct zserv
*client
,
1837 struct stream
*msg
, vrf_id_t vrf_id
)
1840 /* type of protocol (lib/zebra.h) */
1842 unsigned short instance
;
1844 /* Get input stream. */
1848 STREAM_GETC(s
, proto
);
1849 STREAM_GETW(s
, instance
);
1851 /* accept only dynamic routing protocols */
1852 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1853 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1854 "client %d has wrong protocol %s", client
->sock
,
1855 zebra_route_string(proto
));
1856 if (client
->is_synchronous
)
1857 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1860 zlog_notice("client %d with vrf %u instance %u connected as %s",
1861 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1862 client
->proto
= proto
;
1863 client
->instance
= instance
;
1866 * Release previous labels of same protocol and instance.
1867 * This is done in case it restarted from an unexpected shutdown.
1869 release_daemon_label_chunks(client
);
1872 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1873 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1874 /* send response back */
1875 if (client
->is_synchronous
)
1876 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1882 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1888 struct label_manager_chunk
*lmc
;
1890 unsigned short instance
;
1892 /* Get input stream. */
1896 STREAM_GETC(s
, proto
);
1897 STREAM_GETW(s
, instance
);
1898 STREAM_GETC(s
, keep
);
1899 STREAM_GETL(s
, size
);
1901 lmc
= assign_label_chunk(proto
, instance
, keep
, size
);
1904 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1905 "Unable to assign Label Chunk of size %u to %s instance %u",
1906 size
, zebra_route_string(proto
), instance
);
1908 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1909 lmc
->start
, lmc
->end
,
1910 zebra_route_string(proto
), instance
);
1911 /* send response back */
1912 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1918 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1921 uint32_t start
, end
;
1923 unsigned short instance
;
1925 /* Get input stream. */
1929 STREAM_GETC(s
, proto
);
1930 STREAM_GETW(s
, instance
);
1931 STREAM_GETL(s
, start
);
1932 STREAM_GETL(s
, end
);
1934 release_label_chunk(proto
, instance
, start
, end
);
1939 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1941 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1942 client
->is_synchronous
= hdr
->command
==
1943 ZEBRA_LABEL_MANAGER_CONNECT
;
1945 /* external label manager */
1947 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1949 /* this is a label manager */
1951 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
||
1952 hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
1953 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1955 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1956 zread_get_label_chunk(client
, msg
,
1958 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1959 zread_release_label_chunk(client
, msg
);
1964 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1969 struct table_manager_chunk
*tmc
;
1971 /* Get input stream. */
1975 STREAM_GETL(s
, size
);
1977 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1979 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
1980 "%s: Unable to assign Table Chunk of size %u",
1983 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1985 /* send response back */
1986 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
1992 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
1995 uint32_t start
, end
;
1997 /* Get input stream. */
2001 STREAM_GETL(s
, start
);
2002 STREAM_GETL(s
, end
);
2004 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2010 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2012 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2013 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2014 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2016 /* Sanity: don't allow 'unidentified' requests */
2017 if (!client
->proto
) {
2020 "Got table request from an unidentified client");
2023 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2024 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2025 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2026 zread_release_table_chunk(client
, msg
);
2030 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2033 char ifname
[IF_NAMESIZE
];
2037 union g_addr nexthop
;
2038 uint32_t local_label
;
2039 uint32_t remote_label
;
2041 union pw_protocol_fields data
;
2043 struct zebra_pw
*pw
;
2045 /* Get input stream. */
2049 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2050 STREAM_GETL(s
, ifindex
);
2051 STREAM_GETL(s
, type
);
2055 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2058 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2063 STREAM_GETL(s
, local_label
);
2064 STREAM_GETL(s
, remote_label
);
2065 STREAM_GETC(s
, flags
);
2066 STREAM_GET(&data
, s
, sizeof(data
));
2067 protocol
= client
->proto
;
2069 pw
= zebra_pw_find(zvrf
, ifname
);
2070 switch (hdr
->command
) {
2073 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2074 "%s: pseudowire %s already exists [%s]",
2076 zserv_command_string(hdr
->command
));
2080 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2082 case ZEBRA_PW_DELETE
:
2084 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2085 "%s: pseudowire %s not found [%s]", __func__
,
2086 ifname
, zserv_command_string(hdr
->command
));
2090 zebra_pw_del(zvrf
, pw
);
2093 case ZEBRA_PW_UNSET
:
2095 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2096 "%s: pseudowire %s not found [%s]", __func__
,
2097 ifname
, zserv_command_string(hdr
->command
));
2101 switch (hdr
->command
) {
2105 case ZEBRA_PW_UNSET
:
2110 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2111 remote_label
, flags
, &data
);
2119 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2121 struct interface
*master
;
2122 struct interface
*slave
;
2123 struct stream
*s
= msg
;
2127 STREAM_GETL(s
, vrf_id
);
2128 STREAM_GETL(s
, ifindex
);
2129 master
= if_lookup_by_index(ifindex
, vrf_id
);
2131 STREAM_GETL(s
, vrf_id
);
2132 STREAM_GETL(s
, ifindex
);
2133 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2135 if (!master
|| !slave
)
2138 kernel_interface_set_master(master
, slave
);
2145 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2147 struct interface
*ifp
;
2148 mpls_label_t nlabel
;
2151 struct zebra_vrf
*def_zvrf
;
2152 enum lsp_types_t ltype
;
2155 STREAM_GETL(s
, nlabel
);
2156 STREAM_GETC(s
, afi
);
2157 if (nlabel
== zvrf
->label
[afi
]) {
2159 * Nothing to do here move along
2164 STREAM_GETC(s
, ltype
);
2166 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2167 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2169 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2172 zlog_debug("Unable to find specified Interface for %s",
2177 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2179 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2183 really_remove
= true;
2184 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2185 if (scrubber
== afi
)
2188 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2191 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2192 really_remove
= false;
2198 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2199 NEXTHOP_TYPE_IFINDEX
, NULL
,
2203 if (nlabel
!= MPLS_LABEL_NONE
)
2204 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2205 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2206 NULL
, ifp
->ifindex
);
2208 zvrf
->label
[afi
] = nlabel
;
2213 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2215 struct zebra_pbr_rule zpr
;
2221 STREAM_GETL(s
, total
);
2223 for (i
= 0; i
< total
; i
++) {
2224 memset(&zpr
, 0, sizeof(zpr
));
2226 zpr
.sock
= client
->sock
;
2227 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2228 STREAM_GETL(s
, zpr
.rule
.seq
);
2229 STREAM_GETL(s
, zpr
.rule
.priority
);
2230 STREAM_GETL(s
, zpr
.rule
.unique
);
2231 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2232 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2233 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2234 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2235 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2236 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2237 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2238 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2239 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2240 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2241 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2242 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2243 STREAM_GETL(s
, ifindex
);
2246 zpr
.ifp
= if_lookup_by_index_per_ns(
2250 zlog_debug("Failed to lookup ifindex: %u",
2256 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2257 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2259 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2260 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2262 if (zpr
.rule
.filter
.src_port
)
2263 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2265 if (zpr
.rule
.filter
.dst_port
)
2266 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2268 if (zpr
.rule
.filter
.fwmark
)
2269 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2271 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2272 if (hdr
->command
== ZEBRA_RULE_ADD
)
2273 zebra_pbr_add_rule(&zpr
);
2275 zebra_pbr_del_rule(&zpr
);
2282 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2284 struct zebra_pbr_ipset zpi
;
2289 STREAM_GETL(s
, total
);
2291 for (i
= 0; i
< total
; i
++) {
2292 memset(&zpi
, 0, sizeof(zpi
));
2294 zpi
.sock
= client
->sock
;
2295 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2296 STREAM_GETL(s
, zpi
.unique
);
2297 STREAM_GETL(s
, zpi
.type
);
2298 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2300 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2301 zebra_pbr_create_ipset(&zpi
);
2303 zebra_pbr_destroy_ipset(&zpi
);
2310 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2312 struct zebra_pbr_ipset_entry zpi
;
2313 struct zebra_pbr_ipset ipset
;
2318 STREAM_GETL(s
, total
);
2320 for (i
= 0; i
< total
; i
++) {
2321 memset(&zpi
, 0, sizeof(zpi
));
2322 memset(&ipset
, 0, sizeof(ipset
));
2324 zpi
.sock
= client
->sock
;
2325 STREAM_GETL(s
, zpi
.unique
);
2326 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2327 STREAM_GETC(s
, zpi
.src
.family
);
2328 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2329 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2330 STREAM_GETC(s
, zpi
.dst
.family
);
2331 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2332 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2334 STREAM_GETW(s
, zpi
.src_port_min
);
2335 STREAM_GETW(s
, zpi
.src_port_max
);
2336 STREAM_GETW(s
, zpi
.dst_port_min
);
2337 STREAM_GETW(s
, zpi
.dst_port_max
);
2338 STREAM_GETC(s
, zpi
.proto
);
2339 if (!is_default_prefix(&zpi
.src
))
2340 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2342 if (!is_default_prefix(&zpi
.dst
))
2343 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2344 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2345 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2346 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2347 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2348 if (zpi
.dst_port_max
!= 0)
2349 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2350 if (zpi
.src_port_max
!= 0)
2351 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2353 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2355 /* calculate backpointer */
2357 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2358 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2359 zebra_pbr_add_ipset_entry(&zpi
);
2361 zebra_pbr_del_ipset_entry(&zpi
);
2368 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2370 struct zebra_pbr_iptable zpi
;
2375 memset(&zpi
, 0, sizeof(zpi
));
2377 zpi
.interface_name_list
= list_new();
2378 zpi
.sock
= client
->sock
;
2379 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2380 STREAM_GETL(s
, zpi
.unique
);
2381 STREAM_GETL(s
, zpi
.type
);
2382 STREAM_GETL(s
, zpi
.filter_bm
);
2383 STREAM_GETL(s
, zpi
.action
);
2384 STREAM_GETL(s
, zpi
.fwmark
);
2385 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2386 STREAM_GETW(s
, zpi
.pkt_len_min
);
2387 STREAM_GETW(s
, zpi
.pkt_len_max
);
2388 STREAM_GETW(s
, zpi
.tcp_flags
);
2389 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2390 STREAM_GETC(s
, zpi
.dscp_value
);
2391 STREAM_GETC(s
, zpi
.fragment
);
2392 STREAM_GETL(s
, zpi
.nb_interface
);
2393 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2395 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2396 zebra_pbr_add_iptable(&zpi
);
2398 zebra_pbr_del_iptable(&zpi
);
2403 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2404 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2405 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2406 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2407 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2408 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2409 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2410 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2411 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2412 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2413 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2414 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2415 [ZEBRA_HELLO
] = zread_hello
,
2416 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2417 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2418 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2419 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2420 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2421 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2422 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2424 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2425 #endif /* HAVE_BFDD */
2426 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2427 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2428 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2429 #if defined(HAVE_RTADV)
2430 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2431 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2433 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2434 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2436 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2437 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2438 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2439 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2440 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2441 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2442 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2443 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2444 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2445 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2446 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2447 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2448 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2449 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2450 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2451 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2452 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2453 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2454 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2455 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2456 [ZEBRA_PW_SET
] = zread_pseudowire
,
2457 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2458 [ZEBRA_RULE_ADD
] = zread_rule
,
2459 [ZEBRA_RULE_DELETE
] = zread_rule
,
2460 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2461 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2462 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2463 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2464 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2465 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2466 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2467 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2468 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2469 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2472 #if defined(HANDLE_ZAPI_FUZZING)
2473 extern struct zebra_privs_t zserv_privs
;
2475 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2477 char fname
[MAXPATHLEN
];
2478 struct stream
*copy
;
2481 copy
= stream_dup(orig
);
2482 stream_set_getp(copy
, 0);
2484 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2486 frr_elevate_privs(&zserv_privs
) {
2487 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2489 stream_flush(copy
, fd
);
2495 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2498 struct zebra_vrf
*zvrf
;
2500 zapi_parse_header(msg
, &hdr
);
2502 #if defined(HANDLE_ZAPI_FUZZING)
2503 zserv_write_incoming(msg
, hdr
.command
);
2506 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2509 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2511 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2512 zlog_debug("ZAPI message specifies unknown VRF: %d",
2517 if (hdr
.command
>= array_size(zserv_handlers
)
2518 || zserv_handlers
[hdr
.command
] == NULL
)
2519 zlog_info("Zebra received unknown command %d", hdr
.command
);
2521 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);