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"
67 /* Encoding helpers -------------------------------------------------------- */
69 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
71 /* Interface information. */
72 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
73 stream_putl(s
, ifp
->ifindex
);
74 stream_putc(s
, ifp
->status
);
75 stream_putq(s
, ifp
->flags
);
76 stream_putc(s
, ifp
->ptm_enable
);
77 stream_putc(s
, ifp
->ptm_status
);
78 stream_putl(s
, ifp
->metric
);
79 stream_putl(s
, ifp
->speed
);
80 stream_putl(s
, ifp
->mtu
);
81 stream_putl(s
, ifp
->mtu6
);
82 stream_putl(s
, ifp
->bandwidth
);
83 stream_putl(s
, ifp
->ll_type
);
84 stream_putl(s
, ifp
->hw_addr_len
);
86 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
88 /* Then, Traffic Engineering parameters if any */
89 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
91 zebra_interface_link_params_write(s
, ifp
);
95 /* Write packet size. */
96 stream_putw_at(s
, 0, stream_get_endp(s
));
99 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
101 struct vrf_data data
;
102 const char *netns_name
= zvrf_ns_name(zvrf
);
104 data
.l
.table_id
= zvrf
->table_id
;
107 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
110 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
111 /* Pass the tableid and the netns NAME */
112 stream_put(s
, &data
, sizeof(struct vrf_data
));
113 /* Interface information. */
114 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
115 /* Write packet size. */
116 stream_putw_at(s
, 0, stream_get_endp(s
));
119 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
121 stream_putl(s
, nexthop
->vrf_id
);
122 stream_putc(s
, nexthop
->type
);
123 switch (nexthop
->type
) {
124 case NEXTHOP_TYPE_IPV4
:
125 case NEXTHOP_TYPE_IPV4_IFINDEX
:
126 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
127 stream_putl(s
, nexthop
->ifindex
);
129 case NEXTHOP_TYPE_IPV6
:
130 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
132 case NEXTHOP_TYPE_IPV6_IFINDEX
:
133 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
134 stream_putl(s
, nexthop
->ifindex
);
136 case NEXTHOP_TYPE_IFINDEX
:
137 stream_putl(s
, nexthop
->ifindex
);
146 /* Send handlers ----------------------------------------------------------- */
148 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
150 * This function is called in the following situations:
151 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
153 * - at startup, when zebra figures out the available interfaces
154 * - when an interface is added (where support for
155 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
156 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
159 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
161 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
163 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
164 zserv_encode_interface(s
, ifp
);
167 return zserv_send_message(client
, s
);
170 /* Interface deletion from zebra daemon. */
171 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
173 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
175 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
176 zserv_encode_interface(s
, ifp
);
179 return zserv_send_message(client
, s
);
182 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
184 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
186 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
187 zserv_encode_vrf(s
, zvrf
);
189 client
->vrfadd_cnt
++;
190 return zserv_send_message(client
, s
);
193 /* VRF deletion from zebra daemon. */
194 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
197 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
199 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
200 zserv_encode_vrf(s
, zvrf
);
202 client
->vrfdel_cnt
++;
203 return zserv_send_message(client
, s
);
206 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
208 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
210 /* Check this client need interface information. */
211 if (!client
->ifinfo
) {
216 if (!ifp
->link_params
) {
221 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
223 /* Add Interface Index */
224 stream_putl(s
, ifp
->ifindex
);
226 /* Then TE Link Parameters */
227 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
232 /* Write packet size. */
233 stream_putw_at(s
, 0, stream_get_endp(s
));
235 return zserv_send_message(client
, s
);
238 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
239 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
241 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
242 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
243 * from the client, after the ZEBRA_INTERFACE_ADD has been
244 * sent from zebra to the client
245 * - redistribute new address info to all clients in the following situations
246 * - at startup, when zebra figures out the available interfaces
247 * - when an interface is added (where support for
248 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
249 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
251 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
252 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
253 * - when an RTM_NEWADDR message is received from the kernel,
255 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
257 * zsend_interface_address(DELETE)
260 * zebra_interface_address_delete_update
262 * | | if_delete_update
264 * ip_address_uninstall connected_delete_ipv4
265 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
268 * | RTM_NEWADDR on routing/netlink socket
271 * "no ip address A.B.C.D/M [label LINE]"
272 * "no ip address A.B.C.D/M"
273 * ["no ipv6 address X:X::X:X/M"]
276 int zsend_interface_address(int cmd
, struct zserv
*client
,
277 struct interface
*ifp
, struct connected
*ifc
)
281 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
283 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
284 stream_putl(s
, ifp
->ifindex
);
286 /* Interface address flag. */
287 stream_putc(s
, ifc
->flags
);
289 /* Prefix information. */
291 stream_putc(s
, p
->family
);
292 blen
= prefix_blen(p
);
293 stream_put(s
, &p
->u
.prefix
, blen
);
296 * XXX gnu version does not send prefixlen for
297 * ZEBRA_INTERFACE_ADDRESS_DELETE
298 * but zebra_interface_address_delete_read() in the gnu version
301 stream_putc(s
, p
->prefixlen
);
304 p
= ifc
->destination
;
306 stream_put(s
, &p
->u
.prefix
, blen
);
308 stream_put(s
, NULL
, blen
);
310 /* Write packet size. */
311 stream_putw_at(s
, 0, stream_get_endp(s
));
313 client
->connected_rt_add_cnt
++;
314 return zserv_send_message(client
, s
);
317 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
318 struct interface
*ifp
,
319 struct nbr_connected
*ifc
)
322 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
325 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
326 stream_putl(s
, ifp
->ifindex
);
328 /* Prefix information. */
330 stream_putc(s
, p
->family
);
331 blen
= prefix_blen(p
);
332 stream_put(s
, &p
->u
.prefix
, blen
);
335 * XXX gnu version does not send prefixlen for
336 * ZEBRA_INTERFACE_ADDRESS_DELETE
337 * but zebra_interface_address_delete_read() in the gnu version
340 stream_putc(s
, p
->prefixlen
);
342 /* Write packet size. */
343 stream_putw_at(s
, 0, stream_get_endp(s
));
345 return zserv_send_message(client
, s
);
348 /* Interface address addition. */
349 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
350 struct nbr_connected
*ifc
)
352 struct listnode
*node
, *nnode
;
353 struct zserv
*client
;
356 if (IS_ZEBRA_DEBUG_EVENT
) {
357 char buf
[INET6_ADDRSTRLEN
];
361 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
362 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
364 p
->prefixlen
, ifc
->ifp
->name
);
367 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
368 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
372 /* Interface address deletion. */
373 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
374 struct nbr_connected
*ifc
)
376 struct listnode
*node
, *nnode
;
377 struct zserv
*client
;
380 if (IS_ZEBRA_DEBUG_EVENT
) {
381 char buf
[INET6_ADDRSTRLEN
];
385 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
386 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
388 p
->prefixlen
, ifc
->ifp
->name
);
391 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
392 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
396 /* Send addresses on interface to client */
397 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
399 struct listnode
*cnode
, *cnnode
;
401 struct nbr_connected
*nc
;
403 /* Send interface addresses. */
404 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
405 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
408 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
414 /* Send interface neighbors. */
415 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
416 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
425 /* Notify client about interface moving from one VRF to another.
426 * Whether client is interested in old and new VRF is checked by caller.
428 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
431 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
433 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
435 /* Fill in the ifIndex of the interface and its new VRF (id) */
436 stream_putl(s
, ifp
->ifindex
);
437 stream_putl(s
, vrf_id
);
439 /* Write packet size. */
440 stream_putw_at(s
, 0, stream_get_endp(s
));
442 client
->if_vrfchg_cnt
++;
443 return zserv_send_message(client
, s
);
446 /* Add new nbr connected IPv6 address */
447 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
449 struct nbr_connected
*ifc
;
453 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
454 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
456 ifc
= listnode_head(ifp
->nbr_connected
);
459 ifc
= nbr_connected_new();
460 ifc
->address
= prefix_new();
462 listnode_add(ifp
->nbr_connected
, ifc
);
465 prefix_copy(ifc
->address
, &p
);
467 zebra_interface_nbr_address_add_update(ifp
, ifc
);
469 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
472 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
474 struct nbr_connected
*ifc
;
478 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
479 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
481 ifc
= nbr_connected_check(ifp
, &p
);
485 listnode_delete(ifp
->nbr_connected
, ifc
);
487 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
489 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
491 nbr_connected_free(ifc
);
495 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
496 * ZEBRA_INTERFACE_DOWN.
498 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
499 * the clients in one of 2 situations:
500 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
501 * - a vty command modifying the bandwidth of an interface is received.
502 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
504 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
506 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
508 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
509 zserv_encode_interface(s
, ifp
);
511 if (cmd
== ZEBRA_INTERFACE_UP
)
514 client
->ifdown_cnt
++;
516 return zserv_send_message(client
, s
);
519 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
520 const struct prefix
*p
,
521 const struct prefix
*src_p
, struct route_entry
*re
)
523 struct zapi_route api
;
524 struct zapi_nexthop
*api_nh
;
525 struct nexthop
*nexthop
;
529 memset(&api
, 0, sizeof(api
));
530 api
.vrf_id
= re
->vrf_id
;
532 api
.instance
= re
->instance
;
533 api
.flags
= re
->flags
;
535 afi
= family2afi(p
->family
);
538 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
539 client
->redist_v4_add_cnt
++;
541 client
->redist_v4_del_cnt
++;
544 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
545 client
->redist_v6_add_cnt
++;
547 client
->redist_v6_del_cnt
++;
556 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
557 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
561 if (re
->nexthop_active_num
) {
562 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
563 api
.nexthop_num
= re
->nexthop_active_num
;
565 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
566 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
569 api_nh
= &api
.nexthops
[count
];
570 api_nh
->vrf_id
= nexthop
->vrf_id
;
571 api_nh
->type
= nexthop
->type
;
572 switch (nexthop
->type
) {
573 case NEXTHOP_TYPE_BLACKHOLE
:
574 api_nh
->bh_type
= nexthop
->bh_type
;
576 case NEXTHOP_TYPE_IPV4
:
577 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
579 case NEXTHOP_TYPE_IPV4_IFINDEX
:
580 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
581 api_nh
->ifindex
= nexthop
->ifindex
;
583 case NEXTHOP_TYPE_IFINDEX
:
584 api_nh
->ifindex
= nexthop
->ifindex
;
586 case NEXTHOP_TYPE_IPV6
:
587 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
589 case NEXTHOP_TYPE_IPV6_IFINDEX
:
590 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
591 api_nh
->ifindex
= nexthop
->ifindex
;
597 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
598 api
.distance
= re
->distance
;
599 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
600 api
.metric
= re
->metric
;
602 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
605 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
608 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
610 /* Encode route and send. */
611 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
616 if (IS_ZEBRA_DEBUG_SEND
) {
617 char buf_prefix
[PREFIX_STRLEN
];
619 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
621 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
622 __func__
, zserv_command_string(cmd
),
623 zebra_route_string(client
->proto
),
624 zebra_route_string(api
.type
), api
.vrf_id
,
627 return zserv_send_message(client
, s
);
631 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
632 * nexthop is not found on mrib. Returns both route metric and protocol
635 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
637 struct route_entry
*re
,
638 struct zebra_vrf
*zvrf
)
643 struct nexthop
*nexthop
;
645 /* Get output stream. */
646 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
649 /* Fill in result. */
650 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
651 stream_put_in_addr(s
, &addr
);
654 stream_putc(s
, re
->distance
);
655 stream_putl(s
, re
->metric
);
657 /* remember position for nexthop_num */
658 nump
= stream_get_endp(s
);
659 /* reserve room for nexthop_num */
662 * Only non-recursive routes are elegible to resolve the
663 * nexthop we are looking up. Therefore, we will just iterate
664 * over the top chain of nexthops.
666 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
667 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
668 num
+= zserv_encode_nexthop(s
, nexthop
);
670 /* store nexthop_num */
671 stream_putc_at(s
, nump
, num
);
673 stream_putc(s
, 0); /* distance */
674 stream_putl(s
, 0); /* metric */
675 stream_putc(s
, 0); /* nexthop_num */
678 stream_putw_at(s
, 0, stream_get_endp(s
));
680 return zserv_send_message(client
, s
);
684 * Common utility send route notification, called from a path using a
685 * route_entry and from a path using a dataplane context.
687 static int route_notify_internal(const struct prefix
*p
, int type
,
688 uint16_t instance
, vrf_id_t vrf_id
,
690 enum zapi_route_notify_owner note
)
692 struct zserv
*client
;
696 client
= zserv_find_client(type
, instance
);
697 if (!client
|| !client
->notify_owner
) {
698 if (IS_ZEBRA_DEBUG_PACKET
) {
699 char buff
[PREFIX_STRLEN
];
702 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
703 type
, prefix2str(p
, buff
, sizeof(buff
)),
704 table_id
, note
, vrf_id
);
709 if (IS_ZEBRA_DEBUG_PACKET
) {
710 char buff
[PREFIX_STRLEN
];
712 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
713 type
, prefix2str(p
, buff
, sizeof(buff
)),
714 table_id
, note
, vrf_id
);
717 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
720 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
722 stream_put(s
, ¬e
, sizeof(note
));
724 stream_putc(s
, p
->family
);
726 blen
= prefix_blen(p
);
727 stream_putc(s
, p
->prefixlen
);
728 stream_put(s
, &p
->u
.prefix
, blen
);
730 stream_putl(s
, table_id
);
732 stream_putw_at(s
, 0, stream_get_endp(s
));
734 return zserv_send_message(client
, s
);
737 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
738 enum zapi_route_notify_owner note
)
740 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
745 * Route-owner notification using info from dataplane update context.
747 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
748 enum zapi_route_notify_owner note
)
750 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
751 dplane_ctx_get_type(ctx
),
752 dplane_ctx_get_instance(ctx
),
753 dplane_ctx_get_vrf(ctx
),
754 dplane_ctx_get_table(ctx
),
758 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
759 enum zapi_rule_notify_owner note
)
761 struct listnode
*node
;
762 struct zserv
*client
;
765 if (IS_ZEBRA_DEBUG_PACKET
)
766 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
769 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
770 if (rule
->sock
== client
->sock
)
777 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
779 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
780 stream_put(s
, ¬e
, sizeof(note
));
781 stream_putl(s
, rule
->rule
.seq
);
782 stream_putl(s
, rule
->rule
.priority
);
783 stream_putl(s
, rule
->rule
.unique
);
785 stream_putl(s
, rule
->ifp
->ifindex
);
789 stream_putw_at(s
, 0, stream_get_endp(s
));
791 zserv_send_message(client
, s
);
794 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
795 enum zapi_ipset_notify_owner note
)
797 struct listnode
*node
;
798 struct zserv
*client
;
801 if (IS_ZEBRA_DEBUG_PACKET
)
802 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
805 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
806 if (ipset
->sock
== client
->sock
)
813 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
815 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
816 stream_put(s
, ¬e
, sizeof(note
));
817 stream_putl(s
, ipset
->unique
);
818 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
819 stream_putw_at(s
, 0, stream_get_endp(s
));
821 zserv_send_message(client
, s
);
824 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
825 enum zapi_ipset_entry_notify_owner note
)
827 struct listnode
*node
;
828 struct zserv
*client
;
831 if (IS_ZEBRA_DEBUG_PACKET
)
832 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
835 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
836 if (ipset
->sock
== client
->sock
)
843 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
845 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
846 stream_put(s
, ¬e
, sizeof(note
));
847 stream_putl(s
, ipset
->unique
);
848 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
849 stream_putw_at(s
, 0, stream_get_endp(s
));
851 zserv_send_message(client
, s
);
854 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
855 enum zapi_iptable_notify_owner note
)
857 struct listnode
*node
;
858 struct zserv
*client
;
861 if (IS_ZEBRA_DEBUG_PACKET
)
862 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
865 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
866 if (iptable
->sock
== client
->sock
)
873 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
875 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
876 stream_put(s
, ¬e
, sizeof(note
));
877 stream_putl(s
, iptable
->unique
);
878 stream_putw_at(s
, 0, stream_get_endp(s
));
880 zserv_send_message(client
, s
);
883 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
884 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
889 /* Check this client need interface information. */
890 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
893 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
896 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
898 /* Prefix information. */
899 stream_putc(s
, p
->family
);
900 blen
= prefix_blen(p
);
901 stream_put(s
, &p
->u
.prefix
, blen
);
902 stream_putc(s
, p
->prefixlen
);
904 /* Write packet size. */
905 stream_putw_at(s
, 0, stream_get_endp(s
));
907 return zserv_send_message(client
, s
);
911 * Function used by Zebra to send a PW status update to LDP daemon
913 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
915 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
917 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
918 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
919 stream_putl(s
, pw
->ifindex
);
920 stream_putl(s
, pw
->status
);
922 /* Put length at the first point of the stream. */
923 stream_putw_at(s
, 0, stream_get_endp(s
));
925 return zserv_send_message(client
, s
);
928 /* Send response to a get label chunk request to client */
929 static int zsend_assign_label_chunk_response(struct zserv
*client
,
931 struct label_manager_chunk
*lmc
)
934 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
936 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
940 stream_putc(s
, lmc
->proto
);
942 stream_putw(s
, lmc
->instance
);
944 stream_putc(s
, lmc
->keep
);
945 /* start and end labels */
946 stream_putl(s
, lmc
->start
);
947 stream_putl(s
, lmc
->end
);
950 /* Write packet size. */
951 stream_putw_at(s
, 0, stream_get_endp(s
));
953 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
958 /* Send response to a label manager connect request to client */
959 static int zsend_label_manager_connect_response(struct zserv
*client
,
961 unsigned short result
)
964 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
966 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
969 stream_putc(s
, client
->proto
);
972 stream_putw(s
, client
->instance
);
975 stream_putc(s
, result
);
977 /* Write packet size. */
978 stream_putw_at(s
, 0, stream_get_endp(s
));
980 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
986 /* Send response to a get table chunk request to client */
987 static int zsend_assign_table_chunk_response(struct zserv
*client
,
989 struct table_manager_chunk
*tmc
)
991 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
993 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
996 /* start and end labels */
997 stream_putl(s
, tmc
->start
);
998 stream_putl(s
, tmc
->end
);
1001 /* Write packet size. */
1002 stream_putw_at(s
, 0, stream_get_endp(s
));
1004 return zserv_send_message(client
, s
);
1007 static int zsend_table_manager_connect_response(struct zserv
*client
,
1011 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1013 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1016 stream_putc(s
, result
);
1018 stream_putw_at(s
, 0, stream_get_endp(s
));
1020 return zserv_send_message(client
, s
);
1023 /* Inbound message handling ------------------------------------------------ */
1026 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1027 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1028 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1029 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1032 /* Nexthop register */
1033 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1038 unsigned short l
= 0;
1040 uint16_t type
= cmd2type
[hdr
->command
];
1043 if (IS_ZEBRA_DEBUG_NHT
)
1045 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1046 zebra_route_string(client
->proto
), hdr
->length
,
1047 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1052 client
->nh_reg_time
= monotime(NULL
);
1054 while (l
< hdr
->length
) {
1055 STREAM_GETC(s
, flags
);
1056 STREAM_GETW(s
, p
.family
);
1057 STREAM_GETC(s
, p
.prefixlen
);
1059 if (p
.family
== AF_INET
) {
1060 client
->v4_nh_watch_add_cnt
++;
1061 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1063 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1064 __PRETTY_FUNCTION__
, p
.prefixlen
);
1067 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1068 l
+= IPV4_MAX_BYTELEN
;
1069 } else if (p
.family
== AF_INET6
) {
1070 client
->v6_nh_watch_add_cnt
++;
1071 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1073 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1074 __PRETTY_FUNCTION__
, p
.prefixlen
);
1077 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1078 l
+= IPV6_MAX_BYTELEN
;
1081 EC_ZEBRA_UNKNOWN_FAMILY
,
1082 "rnh_register: Received unknown family type %d\n",
1086 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1090 if (type
== RNH_NEXTHOP_TYPE
) {
1092 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1093 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1095 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1096 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1097 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1099 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1100 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1102 && CHECK_FLAG(rnh
->flags
,
1103 ZEBRA_NHT_EXACT_MATCH
))
1104 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1107 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1108 /* Anything not AF_INET/INET6 has been filtered out above */
1110 zebra_evaluate_rnh(zvrf
, p
.family
, 1, type
, &p
);
1117 /* Nexthop register */
1118 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1123 unsigned short l
= 0;
1124 uint16_t type
= cmd2type
[hdr
->command
];
1126 if (IS_ZEBRA_DEBUG_NHT
)
1128 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1129 zebra_route_string(client
->proto
), hdr
->length
,
1134 while (l
< hdr
->length
) {
1137 STREAM_GETC(s
, flags
);
1139 goto stream_failure
;
1141 STREAM_GETW(s
, p
.family
);
1142 STREAM_GETC(s
, p
.prefixlen
);
1144 if (p
.family
== AF_INET
) {
1145 client
->v4_nh_watch_rem_cnt
++;
1146 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1148 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1149 __PRETTY_FUNCTION__
, p
.prefixlen
);
1152 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1153 l
+= IPV4_MAX_BYTELEN
;
1154 } else if (p
.family
== AF_INET6
) {
1155 client
->v6_nh_watch_rem_cnt
++;
1156 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1158 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1159 __PRETTY_FUNCTION__
, p
.prefixlen
);
1162 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1163 l
+= IPV6_MAX_BYTELEN
;
1166 EC_ZEBRA_UNKNOWN_FAMILY
,
1167 "rnh_register: Received unknown family type %d\n",
1171 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1173 client
->nh_dereg_time
= monotime(NULL
);
1174 zebra_remove_rnh_client(rnh
, client
, type
);
1181 #define ZEBRA_MIN_FEC_LENGTH 5
1184 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1187 unsigned short l
= 0;
1190 uint32_t label
= MPLS_INVALID_LABEL
;
1191 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1194 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1199 * The minimum amount of data that can be sent for one fec
1202 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1204 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1205 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1210 while (l
< hdr
->length
) {
1211 STREAM_GETW(s
, flags
);
1212 memset(&p
, 0, sizeof(p
));
1213 STREAM_GETW(s
, p
.family
);
1214 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1216 EC_ZEBRA_UNKNOWN_FAMILY
,
1217 "fec_register: Received unknown family type %d\n",
1221 STREAM_GETC(s
, p
.prefixlen
);
1222 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1223 || (p
.family
== AF_INET6
1224 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1226 "%s: Specified prefix hdr->length: %d is to long for %d",
1227 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1231 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1232 l
+= PSIZE(p
.prefixlen
);
1233 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1234 STREAM_GETL(s
, label
);
1236 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1237 STREAM_GETL(s
, label_index
);
1241 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1248 /* FEC unregister */
1249 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1252 unsigned short l
= 0;
1257 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1262 * The minimum amount of data that can be sent for one
1263 * fec unregistration
1265 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1267 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1268 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1273 while (l
< hdr
->length
) {
1274 STREAM_GETW(s
, flags
);
1276 goto stream_failure
;
1278 memset(&p
, 0, sizeof(p
));
1279 STREAM_GETW(s
, p
.family
);
1280 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1282 EC_ZEBRA_UNKNOWN_FAMILY
,
1283 "fec_unregister: Received unknown family type %d\n",
1287 STREAM_GETC(s
, p
.prefixlen
);
1288 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1289 || (p
.family
== AF_INET6
1290 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1292 "%s: Received prefix hdr->length %d which is greater than %d can support",
1293 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1297 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1298 l
+= PSIZE(p
.prefixlen
);
1299 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1308 * Register zebra server interface information.
1309 * Send current all interface and address information.
1311 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1314 struct interface
*ifp
;
1316 /* Interface information is needed. */
1317 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1319 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1320 FOR_ALL_INTERFACES (vrf
, ifp
) {
1321 /* Skip pseudo interface. */
1322 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1325 zsend_interface_add(client
, ifp
);
1326 zsend_interface_addresses(client
, ifp
);
1331 /* Unregister zebra server interface information. */
1332 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1334 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1337 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1338 const unsigned int nexthop_num
)
1340 if (nexthop_num
> multipath_num
) {
1341 char buff
[PREFIX2STR_BUFFER
];
1343 prefix2str(p
, buff
, sizeof(buff
));
1345 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1346 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1347 caller
, buff
, nexthop_num
, multipath_num
);
1351 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1354 struct zapi_route api
;
1355 struct zapi_nexthop
*api_nh
;
1357 struct prefix_ipv6
*src_p
= NULL
;
1358 struct route_entry
*re
;
1359 struct nexthop
*nexthop
= NULL
;
1361 vrf_id_t vrf_id
= 0;
1362 struct ipaddr vtep_ip
;
1365 if (zapi_route_decode(s
, &api
) < 0) {
1366 if (IS_ZEBRA_DEBUG_RECV
)
1367 zlog_debug("%s: Unable to decode zapi_route sent",
1368 __PRETTY_FUNCTION__
);
1372 if (IS_ZEBRA_DEBUG_RECV
) {
1373 char buf_prefix
[PREFIX_STRLEN
];
1375 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1376 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1377 __func__
, buf_prefix
,
1378 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1383 /* Allocate new route. */
1384 vrf_id
= zvrf_id(zvrf
);
1385 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1386 re
->type
= api
.type
;
1387 re
->instance
= api
.instance
;
1388 re
->flags
= api
.flags
;
1389 re
->uptime
= time(NULL
);
1390 re
->vrf_id
= vrf_id
;
1391 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1392 re
->table
= api
.tableid
;
1394 re
->table
= zvrf
->table_id
;
1397 * TBD should _all_ of the nexthop add operations use
1398 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1399 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1401 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1402 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1403 api_nh
= &api
.nexthops
[i
];
1404 ifindex_t ifindex
= 0;
1406 if (IS_ZEBRA_DEBUG_RECV
)
1407 zlog_debug("nh type %d", api_nh
->type
);
1409 switch (api_nh
->type
) {
1410 case NEXTHOP_TYPE_IFINDEX
:
1411 nexthop
= route_entry_nexthop_ifindex_add(
1412 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1414 case NEXTHOP_TYPE_IPV4
:
1415 if (IS_ZEBRA_DEBUG_RECV
) {
1416 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1418 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1419 nhbuf
, INET6_ADDRSTRLEN
);
1420 zlog_debug("%s: nh=%s, vrf_id=%d",
1424 nexthop
= route_entry_nexthop_ipv4_add(
1425 re
, &api_nh
->gate
.ipv4
, NULL
,
1428 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1430 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1431 if (CHECK_FLAG(api
.flags
,
1432 ZEBRA_FLAG_EVPN_ROUTE
)) {
1433 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1435 ifindex
= api_nh
->ifindex
;
1438 if (IS_ZEBRA_DEBUG_RECV
) {
1439 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1441 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1442 nhbuf
, INET6_ADDRSTRLEN
);
1444 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1445 __func__
, nhbuf
, api_nh
->vrf_id
,
1446 re
->vrf_id
, ifindex
);
1448 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1449 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1452 /* if this an EVPN route entry,
1453 * program the nh as neigh
1455 if (CHECK_FLAG(api
.flags
,
1456 ZEBRA_FLAG_EVPN_ROUTE
)) {
1457 SET_FLAG(nexthop
->flags
,
1458 NEXTHOP_FLAG_EVPN_RVTEP
);
1459 vtep_ip
.ipa_type
= IPADDR_V4
;
1460 memcpy(&(vtep_ip
.ipaddr_v4
),
1461 &(api_nh
->gate
.ipv4
),
1462 sizeof(struct in_addr
));
1463 zebra_vxlan_evpn_vrf_route_add(
1464 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1468 case NEXTHOP_TYPE_IPV6
:
1469 nexthop
= route_entry_nexthop_ipv6_add(
1470 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1472 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1473 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1474 if (CHECK_FLAG(api
.flags
,
1475 ZEBRA_FLAG_EVPN_ROUTE
)) {
1476 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1478 ifindex
= api_nh
->ifindex
;
1481 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1482 re
, &api_nh
->gate
.ipv6
, ifindex
,
1485 /* if this an EVPN route entry,
1486 * program the nh as neigh
1488 if (CHECK_FLAG(api
.flags
,
1489 ZEBRA_FLAG_EVPN_ROUTE
)) {
1490 SET_FLAG(nexthop
->flags
,
1491 NEXTHOP_FLAG_EVPN_RVTEP
);
1492 vtep_ip
.ipa_type
= IPADDR_V6
;
1493 memcpy(&vtep_ip
.ipaddr_v6
,
1494 &(api_nh
->gate
.ipv6
),
1495 sizeof(struct in6_addr
));
1496 zebra_vxlan_evpn_vrf_route_add(
1497 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1501 case NEXTHOP_TYPE_BLACKHOLE
:
1502 nexthop
= route_entry_nexthop_blackhole_add(
1503 re
, api_nh
->bh_type
);
1509 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1510 "%s: Nexthops Specified: %d but we failed to properly create one",
1511 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1512 nexthops_free(re
->ng
.nexthop
);
1513 XFREE(MTYPE_RE
, re
);
1516 /* MPLS labels for BGP-LU or Segment Routing */
1517 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1518 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1519 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1520 enum lsp_types_t label_type
;
1523 lsp_type_from_re_type(client
->proto
);
1525 if (IS_ZEBRA_DEBUG_RECV
) {
1527 "%s: adding %d labels of type %d (1st=%u)",
1528 __func__
, api_nh
->label_num
,
1529 label_type
, api_nh
->labels
[0]);
1532 nexthop_add_labels(nexthop
, label_type
,
1534 &api_nh
->labels
[0]);
1538 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1539 re
->distance
= api
.distance
;
1540 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1541 re
->metric
= api
.metric
;
1542 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1544 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1547 afi
= family2afi(api
.prefix
.family
);
1548 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1549 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1550 "%s: Received SRC Prefix but afi is not v6",
1551 __PRETTY_FUNCTION__
);
1552 nexthops_free(re
->ng
.nexthop
);
1553 XFREE(MTYPE_RE
, re
);
1556 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1557 src_p
= &api
.src_prefix
;
1559 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1562 switch (api
.prefix
.family
) {
1565 client
->v4_route_add_cnt
++;
1567 client
->v4_route_upd8_cnt
++;
1571 client
->v6_route_add_cnt
++;
1573 client
->v6_route_upd8_cnt
++;
1578 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1581 struct zapi_route api
;
1583 struct prefix_ipv6
*src_p
= NULL
;
1587 if (zapi_route_decode(s
, &api
) < 0)
1590 afi
= family2afi(api
.prefix
.family
);
1591 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1592 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1593 "%s: Received a src prefix while afi is not v6",
1594 __PRETTY_FUNCTION__
);
1597 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1598 src_p
= &api
.src_prefix
;
1600 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1601 table_id
= api
.tableid
;
1603 table_id
= zvrf
->table_id
;
1605 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1606 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1607 api
.distance
, false);
1610 switch (api
.prefix
.family
) {
1612 client
->v4_route_del_cnt
++;
1615 client
->v6_route_del_cnt
++;
1620 /* MRIB Nexthop lookup for IPv4. */
1621 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1623 struct in_addr addr
;
1624 struct route_entry
*re
;
1626 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1627 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1628 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1634 /* Register zebra server router-id information. Send current router-id */
1635 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1639 /* Router-id information is needed. */
1640 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1642 router_id_get(&p
, zvrf_id(zvrf
));
1644 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1647 /* Unregister zebra server router-id information. */
1648 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1650 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1653 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1655 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1657 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1658 stream_putc(s
, mpls_enabled
);
1659 stream_putl(s
, multipath_num
);
1661 stream_putw_at(s
, 0, stream_get_endp(s
));
1662 zserv_send_message(client
, s
);
1665 /* Tie up route-type and client->sock */
1666 static void zread_hello(ZAPI_HANDLER_ARGS
)
1668 /* type of protocol (lib/zebra.h) */
1670 unsigned short instance
;
1673 STREAM_GETC(msg
, proto
);
1674 STREAM_GETW(msg
, instance
);
1675 STREAM_GETC(msg
, notify
);
1677 client
->notify_owner
= true;
1679 /* accept only dynamic routing protocols */
1680 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1682 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1683 client
->sock
, zebra_route_string(proto
),
1686 zlog_notice("client protocol instance %d", instance
);
1688 client
->proto
= proto
;
1689 client
->instance
= instance
;
1692 zsend_capabilities(client
, zvrf
);
1697 /* Unregister all information in a VRF. */
1698 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1703 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1704 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1705 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1706 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1707 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1708 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1711 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1714 enum lsp_types_t type
;
1715 struct prefix prefix
;
1716 enum nexthop_types_t gtype
;
1719 mpls_label_t in_label
, out_label
;
1722 /* Get input stream. */
1726 STREAM_GETC(s
, type
);
1727 STREAM_GETL(s
, prefix
.family
);
1728 switch (prefix
.family
) {
1730 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1731 STREAM_GETC(s
, prefix
.prefixlen
);
1732 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1734 "%s: Specified prefix length %d is greater than a v4 address can support",
1735 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1738 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1741 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1742 STREAM_GETC(s
, prefix
.prefixlen
);
1743 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1745 "%s: Specified prefix length %d is greater than a v6 address can support",
1746 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1749 STREAM_GET(&gate
.ipv6
, s
, 16);
1752 zlog_debug("%s: Specified AF %d is not supported for this call",
1753 __PRETTY_FUNCTION__
, prefix
.family
);
1756 STREAM_GETL(s
, ifindex
);
1757 STREAM_GETC(s
, distance
);
1758 STREAM_GETL(s
, in_label
);
1759 STREAM_GETL(s
, out_label
);
1761 switch (prefix
.family
) {
1764 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1766 gtype
= NEXTHOP_TYPE_IPV4
;
1770 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1772 gtype
= NEXTHOP_TYPE_IPV6
;
1781 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1782 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1784 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1785 distance
, out_label
);
1786 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1787 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1788 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1789 distance
, out_label
);
1795 /* Send response to a table manager connect request to client */
1796 static void zread_table_manager_connect(struct zserv
*client
,
1797 struct stream
*msg
, vrf_id_t vrf_id
)
1806 STREAM_GETC(s
, proto
);
1807 STREAM_GETW(s
, instance
);
1809 /* accept only dynamic routing protocols */
1810 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1811 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1812 "client %d has wrong protocol %s", client
->sock
,
1813 zebra_route_string(proto
));
1814 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1817 zlog_notice("client %d with vrf %u instance %u connected as %s",
1818 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1819 client
->proto
= proto
;
1820 client
->instance
= instance
;
1823 * Release previous labels of same protocol and instance.
1824 * This is done in case it restarted from an unexpected shutdown.
1826 release_daemon_table_chunks(client
);
1828 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1834 static void zread_label_manager_connect(struct zserv
*client
,
1835 struct stream
*msg
, vrf_id_t vrf_id
)
1838 /* type of protocol (lib/zebra.h) */
1840 unsigned short instance
;
1842 /* Get input stream. */
1846 STREAM_GETC(s
, proto
);
1847 STREAM_GETW(s
, instance
);
1849 /* accept only dynamic routing protocols */
1850 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1851 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1852 "client %d has wrong protocol %s", client
->sock
,
1853 zebra_route_string(proto
));
1854 if (client
->is_synchronous
)
1855 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1858 zlog_notice("client %d with vrf %u instance %u connected as %s",
1859 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1860 client
->proto
= proto
;
1861 client
->instance
= instance
;
1864 * Release previous labels of same protocol and instance.
1865 * This is done in case it restarted from an unexpected shutdown.
1867 release_daemon_label_chunks(client
);
1870 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1871 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1872 /* send response back */
1873 if (client
->is_synchronous
)
1874 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1880 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1886 struct label_manager_chunk
*lmc
;
1888 unsigned short instance
;
1890 /* Get input stream. */
1894 STREAM_GETC(s
, proto
);
1895 STREAM_GETW(s
, instance
);
1896 STREAM_GETC(s
, keep
);
1897 STREAM_GETL(s
, size
);
1899 lmc
= assign_label_chunk(proto
, instance
, keep
, size
);
1902 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1903 "Unable to assign Label Chunk of size %u to %s instance %u",
1904 size
, zebra_route_string(proto
), instance
);
1906 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1907 lmc
->start
, lmc
->end
,
1908 zebra_route_string(proto
), instance
);
1909 /* send response back */
1910 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1916 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1919 uint32_t start
, end
;
1921 unsigned short instance
;
1923 /* Get input stream. */
1927 STREAM_GETC(s
, proto
);
1928 STREAM_GETW(s
, instance
);
1929 STREAM_GETL(s
, start
);
1930 STREAM_GETL(s
, end
);
1932 release_label_chunk(proto
, instance
, start
, end
);
1937 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1939 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1940 client
->is_synchronous
= hdr
->command
==
1941 ZEBRA_LABEL_MANAGER_CONNECT
;
1943 /* external label manager */
1945 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1947 /* this is a label manager */
1949 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
||
1950 hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
1951 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1953 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1954 zread_get_label_chunk(client
, msg
,
1956 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1957 zread_release_label_chunk(client
, msg
);
1962 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1967 struct table_manager_chunk
*tmc
;
1969 /* Get input stream. */
1973 STREAM_GETL(s
, size
);
1975 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1977 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
1978 "%s: Unable to assign Table Chunk of size %u",
1981 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1983 /* send response back */
1984 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
1990 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
1993 uint32_t start
, end
;
1995 /* Get input stream. */
1999 STREAM_GETL(s
, start
);
2000 STREAM_GETL(s
, end
);
2002 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2008 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2010 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2011 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2012 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2014 /* Sanity: don't allow 'unidentified' requests */
2015 if (!client
->proto
) {
2018 "Got table request from an unidentified client");
2021 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2022 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2023 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2024 zread_release_table_chunk(client
, msg
);
2028 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2031 char ifname
[IF_NAMESIZE
];
2035 union g_addr nexthop
;
2036 uint32_t local_label
;
2037 uint32_t remote_label
;
2039 union pw_protocol_fields data
;
2041 struct zebra_pw
*pw
;
2043 /* Get input stream. */
2047 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2048 STREAM_GETL(s
, ifindex
);
2049 STREAM_GETL(s
, type
);
2053 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2056 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2061 STREAM_GETL(s
, local_label
);
2062 STREAM_GETL(s
, remote_label
);
2063 STREAM_GETC(s
, flags
);
2064 STREAM_GET(&data
, s
, sizeof(data
));
2065 protocol
= client
->proto
;
2067 pw
= zebra_pw_find(zvrf
, ifname
);
2068 switch (hdr
->command
) {
2071 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2072 "%s: pseudowire %s already exists [%s]",
2074 zserv_command_string(hdr
->command
));
2078 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2080 case ZEBRA_PW_DELETE
:
2082 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2083 "%s: pseudowire %s not found [%s]", __func__
,
2084 ifname
, zserv_command_string(hdr
->command
));
2088 zebra_pw_del(zvrf
, pw
);
2091 case ZEBRA_PW_UNSET
:
2093 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2094 "%s: pseudowire %s not found [%s]", __func__
,
2095 ifname
, zserv_command_string(hdr
->command
));
2099 switch (hdr
->command
) {
2103 case ZEBRA_PW_UNSET
:
2108 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2109 remote_label
, flags
, &data
);
2117 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2119 struct interface
*master
;
2120 struct interface
*slave
;
2121 struct stream
*s
= msg
;
2125 STREAM_GETL(s
, vrf_id
);
2126 STREAM_GETL(s
, ifindex
);
2127 master
= if_lookup_by_index(ifindex
, vrf_id
);
2129 STREAM_GETL(s
, vrf_id
);
2130 STREAM_GETL(s
, ifindex
);
2131 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2133 if (!master
|| !slave
)
2136 kernel_interface_set_master(master
, slave
);
2143 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2145 struct interface
*ifp
;
2146 mpls_label_t nlabel
;
2149 struct zebra_vrf
*def_zvrf
;
2150 enum lsp_types_t ltype
;
2153 STREAM_GETL(s
, nlabel
);
2154 STREAM_GETC(s
, afi
);
2155 if (nlabel
== zvrf
->label
[afi
]) {
2157 * Nothing to do here move along
2162 STREAM_GETC(s
, ltype
);
2164 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2165 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2167 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2170 zlog_debug("Unable to find specified Interface for %s",
2175 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2177 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2181 really_remove
= true;
2182 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2183 if (scrubber
== afi
)
2186 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2189 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2190 really_remove
= false;
2196 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2197 NEXTHOP_TYPE_IFINDEX
, NULL
,
2201 if (nlabel
!= MPLS_LABEL_NONE
)
2202 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2203 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2204 NULL
, ifp
->ifindex
);
2206 zvrf
->label
[afi
] = nlabel
;
2211 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2213 struct zebra_pbr_rule zpr
;
2219 STREAM_GETL(s
, total
);
2221 for (i
= 0; i
< total
; i
++) {
2222 memset(&zpr
, 0, sizeof(zpr
));
2224 zpr
.sock
= client
->sock
;
2225 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2226 STREAM_GETL(s
, zpr
.rule
.seq
);
2227 STREAM_GETL(s
, zpr
.rule
.priority
);
2228 STREAM_GETL(s
, zpr
.rule
.unique
);
2229 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2230 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2231 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2232 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2233 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2234 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2235 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2236 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2237 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2238 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2239 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2240 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2241 STREAM_GETL(s
, ifindex
);
2244 zpr
.ifp
= if_lookup_by_index_per_ns(
2248 zlog_debug("Failed to lookup ifindex: %u",
2254 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2255 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2257 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2258 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2260 if (zpr
.rule
.filter
.src_port
)
2261 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2263 if (zpr
.rule
.filter
.dst_port
)
2264 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2266 if (zpr
.rule
.filter
.fwmark
)
2267 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2269 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2270 if (hdr
->command
== ZEBRA_RULE_ADD
)
2271 zebra_pbr_add_rule(&zpr
);
2273 zebra_pbr_del_rule(&zpr
);
2280 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2282 struct zebra_pbr_ipset zpi
;
2287 STREAM_GETL(s
, total
);
2289 for (i
= 0; i
< total
; i
++) {
2290 memset(&zpi
, 0, sizeof(zpi
));
2292 zpi
.sock
= client
->sock
;
2293 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2294 STREAM_GETL(s
, zpi
.unique
);
2295 STREAM_GETL(s
, zpi
.type
);
2296 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2298 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2299 zebra_pbr_create_ipset(&zpi
);
2301 zebra_pbr_destroy_ipset(&zpi
);
2308 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2310 struct zebra_pbr_ipset_entry zpi
;
2311 struct zebra_pbr_ipset ipset
;
2316 STREAM_GETL(s
, total
);
2318 for (i
= 0; i
< total
; i
++) {
2319 memset(&zpi
, 0, sizeof(zpi
));
2320 memset(&ipset
, 0, sizeof(ipset
));
2322 zpi
.sock
= client
->sock
;
2323 STREAM_GETL(s
, zpi
.unique
);
2324 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2325 STREAM_GETC(s
, zpi
.src
.family
);
2326 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2327 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2328 STREAM_GETC(s
, zpi
.dst
.family
);
2329 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2330 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2332 STREAM_GETW(s
, zpi
.src_port_min
);
2333 STREAM_GETW(s
, zpi
.src_port_max
);
2334 STREAM_GETW(s
, zpi
.dst_port_min
);
2335 STREAM_GETW(s
, zpi
.dst_port_max
);
2336 STREAM_GETC(s
, zpi
.proto
);
2337 if (!is_default_prefix(&zpi
.src
))
2338 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2340 if (!is_default_prefix(&zpi
.dst
))
2341 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2342 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2343 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2344 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2345 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2346 if (zpi
.dst_port_max
!= 0)
2347 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2348 if (zpi
.src_port_max
!= 0)
2349 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2351 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2353 /* calculate backpointer */
2355 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2356 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2357 zebra_pbr_add_ipset_entry(&zpi
);
2359 zebra_pbr_del_ipset_entry(&zpi
);
2366 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2368 struct zebra_pbr_iptable zpi
;
2373 memset(&zpi
, 0, sizeof(zpi
));
2375 zpi
.interface_name_list
= list_new();
2376 zpi
.sock
= client
->sock
;
2377 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2378 STREAM_GETL(s
, zpi
.unique
);
2379 STREAM_GETL(s
, zpi
.type
);
2380 STREAM_GETL(s
, zpi
.filter_bm
);
2381 STREAM_GETL(s
, zpi
.action
);
2382 STREAM_GETL(s
, zpi
.fwmark
);
2383 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2384 STREAM_GETW(s
, zpi
.pkt_len_min
);
2385 STREAM_GETW(s
, zpi
.pkt_len_max
);
2386 STREAM_GETW(s
, zpi
.tcp_flags
);
2387 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2388 STREAM_GETC(s
, zpi
.dscp_value
);
2389 STREAM_GETC(s
, zpi
.fragment
);
2390 STREAM_GETL(s
, zpi
.nb_interface
);
2391 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2393 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2394 zebra_pbr_add_iptable(&zpi
);
2396 zebra_pbr_del_iptable(&zpi
);
2401 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2402 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2403 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2404 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2405 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2406 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2407 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2408 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2409 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2410 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2411 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2412 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2413 [ZEBRA_HELLO
] = zread_hello
,
2414 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2415 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2416 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2417 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2418 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2419 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2420 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2422 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2423 #endif /* HAVE_BFDD */
2424 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2425 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2426 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2427 #if defined(HAVE_RTADV)
2428 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2429 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2431 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2432 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2434 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2435 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2436 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2437 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2438 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2439 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2440 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2441 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2442 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2443 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2444 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2445 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2446 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2447 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2448 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2449 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2450 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2451 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2452 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2453 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2454 [ZEBRA_PW_SET
] = zread_pseudowire
,
2455 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2456 [ZEBRA_RULE_ADD
] = zread_rule
,
2457 [ZEBRA_RULE_DELETE
] = zread_rule
,
2458 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2459 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2460 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2461 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2462 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2463 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2464 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2465 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2466 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2467 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2470 #if defined(HANDLE_ZAPI_FUZZING)
2471 extern struct zebra_privs_t zserv_privs
;
2473 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2475 char fname
[MAXPATHLEN
];
2476 struct stream
*copy
;
2479 copy
= stream_dup(orig
);
2480 stream_set_getp(copy
, 0);
2482 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2484 frr_elevate_privs(&zserv_privs
) {
2485 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2487 stream_flush(copy
, fd
);
2493 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2496 struct zebra_vrf
*zvrf
;
2498 zapi_parse_header(msg
, &hdr
);
2500 #if defined(HANDLE_ZAPI_FUZZING)
2501 zserv_write_incoming(msg
, hdr
.command
);
2504 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2507 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2509 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2510 zlog_debug("ZAPI message specifies unknown VRF: %d",
2515 if (hdr
.command
>= array_size(zserv_handlers
)
2516 || zserv_handlers
[hdr
.command
] == NULL
)
2517 zlog_info("Zebra received unknown command %d", hdr
.command
);
2519 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);