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_putc(s
, nexthop
->type
);
122 switch (nexthop
->type
) {
123 case NEXTHOP_TYPE_IPV4
:
124 case NEXTHOP_TYPE_IPV4_IFINDEX
:
125 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
126 stream_putl(s
, nexthop
->ifindex
);
128 case NEXTHOP_TYPE_IPV6
:
129 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
131 case NEXTHOP_TYPE_IPV6_IFINDEX
:
132 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
133 stream_putl(s
, nexthop
->ifindex
);
135 case NEXTHOP_TYPE_IFINDEX
:
136 stream_putl(s
, nexthop
->ifindex
);
145 /* Send handlers ----------------------------------------------------------- */
147 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
149 * This function is called in the following situations:
150 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
152 * - at startup, when zebra figures out the available interfaces
153 * - when an interface is added (where support for
154 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
155 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
158 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
160 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
162 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
163 zserv_encode_interface(s
, ifp
);
166 return zserv_send_message(client
, s
);
169 /* Interface deletion from zebra daemon. */
170 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
172 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
174 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
175 zserv_encode_interface(s
, ifp
);
178 return zserv_send_message(client
, s
);
181 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
183 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
185 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
186 zserv_encode_vrf(s
, zvrf
);
188 client
->vrfadd_cnt
++;
189 return zserv_send_message(client
, s
);
192 /* VRF deletion from zebra daemon. */
193 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
196 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
198 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
199 zserv_encode_vrf(s
, zvrf
);
201 client
->vrfdel_cnt
++;
202 return zserv_send_message(client
, s
);
205 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
207 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
209 /* Check this client need interface information. */
210 if (!client
->ifinfo
) {
215 if (!ifp
->link_params
) {
220 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
222 /* Add Interface Index */
223 stream_putl(s
, ifp
->ifindex
);
225 /* Then TE Link Parameters */
226 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
231 /* Write packet size. */
232 stream_putw_at(s
, 0, stream_get_endp(s
));
234 return zserv_send_message(client
, s
);
237 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
238 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
240 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
241 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
242 * from the client, after the ZEBRA_INTERFACE_ADD has been
243 * sent from zebra to the client
244 * - redistribute new address info to all clients in the following situations
245 * - at startup, when zebra figures out the available interfaces
246 * - when an interface is added (where support for
247 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
248 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
250 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
251 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
252 * - when an RTM_NEWADDR message is received from the kernel,
254 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
256 * zsend_interface_address(DELETE)
259 * zebra_interface_address_delete_update
261 * | | if_delete_update
263 * ip_address_uninstall connected_delete_ipv4
264 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
267 * | RTM_NEWADDR on routing/netlink socket
270 * "no ip address A.B.C.D/M [label LINE]"
271 * "no ip address A.B.C.D/M secondary"
272 * ["no ipv6 address X:X::X:X/M"]
275 int zsend_interface_address(int cmd
, struct zserv
*client
,
276 struct interface
*ifp
, struct connected
*ifc
)
280 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
282 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
283 stream_putl(s
, ifp
->ifindex
);
285 /* Interface address flag. */
286 stream_putc(s
, ifc
->flags
);
288 /* Prefix information. */
290 stream_putc(s
, p
->family
);
291 blen
= prefix_blen(p
);
292 stream_put(s
, &p
->u
.prefix
, blen
);
295 * XXX gnu version does not send prefixlen for
296 * ZEBRA_INTERFACE_ADDRESS_DELETE
297 * but zebra_interface_address_delete_read() in the gnu version
300 stream_putc(s
, p
->prefixlen
);
303 p
= ifc
->destination
;
305 stream_put(s
, &p
->u
.prefix
, blen
);
307 stream_put(s
, NULL
, blen
);
309 /* Write packet size. */
310 stream_putw_at(s
, 0, stream_get_endp(s
));
312 client
->connected_rt_add_cnt
++;
313 return zserv_send_message(client
, s
);
316 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
317 struct interface
*ifp
,
318 struct nbr_connected
*ifc
)
321 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
324 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
325 stream_putl(s
, ifp
->ifindex
);
327 /* Prefix information. */
329 stream_putc(s
, p
->family
);
330 blen
= prefix_blen(p
);
331 stream_put(s
, &p
->u
.prefix
, blen
);
334 * XXX gnu version does not send prefixlen for
335 * ZEBRA_INTERFACE_ADDRESS_DELETE
336 * but zebra_interface_address_delete_read() in the gnu version
339 stream_putc(s
, p
->prefixlen
);
341 /* Write packet size. */
342 stream_putw_at(s
, 0, stream_get_endp(s
));
344 return zserv_send_message(client
, s
);
347 /* Interface address addition. */
348 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
349 struct nbr_connected
*ifc
)
351 struct listnode
*node
, *nnode
;
352 struct zserv
*client
;
355 if (IS_ZEBRA_DEBUG_EVENT
) {
356 char buf
[INET6_ADDRSTRLEN
];
360 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
361 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
363 p
->prefixlen
, ifc
->ifp
->name
);
366 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
367 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
371 /* Interface address deletion. */
372 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
373 struct nbr_connected
*ifc
)
375 struct listnode
*node
, *nnode
;
376 struct zserv
*client
;
379 if (IS_ZEBRA_DEBUG_EVENT
) {
380 char buf
[INET6_ADDRSTRLEN
];
384 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
385 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
387 p
->prefixlen
, ifc
->ifp
->name
);
390 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
391 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
395 /* Send addresses on interface to client */
396 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
398 struct listnode
*cnode
, *cnnode
;
400 struct nbr_connected
*nc
;
402 /* Send interface addresses. */
403 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
404 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
407 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
413 /* Send interface neighbors. */
414 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
415 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
424 /* Notify client about interface moving from one VRF to another.
425 * Whether client is interested in old and new VRF is checked by caller.
427 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
430 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
432 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
434 /* Fill in the ifIndex of the interface and its new VRF (id) */
435 stream_putl(s
, ifp
->ifindex
);
436 stream_putl(s
, vrf_id
);
438 /* Write packet size. */
439 stream_putw_at(s
, 0, stream_get_endp(s
));
441 client
->if_vrfchg_cnt
++;
442 return zserv_send_message(client
, s
);
445 /* Add new nbr connected IPv6 address */
446 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
448 struct nbr_connected
*ifc
;
452 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
453 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
455 ifc
= listnode_head(ifp
->nbr_connected
);
458 ifc
= nbr_connected_new();
459 ifc
->address
= prefix_new();
461 listnode_add(ifp
->nbr_connected
, ifc
);
464 prefix_copy(ifc
->address
, &p
);
466 zebra_interface_nbr_address_add_update(ifp
, ifc
);
468 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
471 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
473 struct nbr_connected
*ifc
;
477 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
478 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
480 ifc
= nbr_connected_check(ifp
, &p
);
484 listnode_delete(ifp
->nbr_connected
, ifc
);
486 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
488 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
490 nbr_connected_free(ifc
);
494 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
495 * ZEBRA_INTERFACE_DOWN.
497 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
498 * the clients in one of 2 situations:
499 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
500 * - a vty command modifying the bandwidth of an interface is received.
501 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
503 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
505 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
507 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
508 zserv_encode_interface(s
, ifp
);
510 if (cmd
== ZEBRA_INTERFACE_UP
)
513 client
->ifdown_cnt
++;
515 return zserv_send_message(client
, s
);
518 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
519 const struct prefix
*p
,
520 const struct prefix
*src_p
, struct route_entry
*re
)
522 struct zapi_route api
;
523 struct zapi_nexthop
*api_nh
;
524 struct nexthop
*nexthop
;
528 memset(&api
, 0, sizeof(api
));
529 api
.vrf_id
= re
->vrf_id
;
531 api
.instance
= re
->instance
;
532 api
.flags
= re
->flags
;
534 afi
= family2afi(p
->family
);
537 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
538 client
->redist_v4_add_cnt
++;
540 client
->redist_v4_del_cnt
++;
543 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
544 client
->redist_v6_add_cnt
++;
546 client
->redist_v6_del_cnt
++;
555 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
556 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
560 if (re
->nexthop_active_num
) {
561 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
562 api
.nexthop_num
= re
->nexthop_active_num
;
564 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
565 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
568 api_nh
= &api
.nexthops
[count
];
569 api_nh
->vrf_id
= nexthop
->vrf_id
;
570 api_nh
->type
= nexthop
->type
;
571 switch (nexthop
->type
) {
572 case NEXTHOP_TYPE_BLACKHOLE
:
573 api_nh
->bh_type
= nexthop
->bh_type
;
575 case NEXTHOP_TYPE_IPV4
:
576 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
578 case NEXTHOP_TYPE_IPV4_IFINDEX
:
579 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
580 api_nh
->ifindex
= nexthop
->ifindex
;
582 case NEXTHOP_TYPE_IFINDEX
:
583 api_nh
->ifindex
= nexthop
->ifindex
;
585 case NEXTHOP_TYPE_IPV6
:
586 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
588 case NEXTHOP_TYPE_IPV6_IFINDEX
:
589 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
590 api_nh
->ifindex
= nexthop
->ifindex
;
596 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
597 api
.distance
= re
->distance
;
598 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
599 api
.metric
= re
->metric
;
601 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
604 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
607 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
609 /* Encode route and send. */
610 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
615 if (IS_ZEBRA_DEBUG_SEND
) {
616 char buf_prefix
[PREFIX_STRLEN
];
618 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
620 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
621 __func__
, zserv_command_string(cmd
),
622 zebra_route_string(client
->proto
),
623 zebra_route_string(api
.type
), api
.vrf_id
,
626 return zserv_send_message(client
, s
);
630 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
631 * nexthop is not found on mrib. Returns both route metric and protocol
634 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
636 struct route_entry
*re
,
637 struct zebra_vrf
*zvrf
)
642 struct nexthop
*nexthop
;
644 /* Get output stream. */
645 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
648 /* Fill in result. */
649 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
650 stream_put_in_addr(s
, &addr
);
653 stream_putc(s
, re
->distance
);
654 stream_putl(s
, re
->metric
);
656 /* remember position for nexthop_num */
657 nump
= stream_get_endp(s
);
658 /* reserve room for nexthop_num */
661 * Only non-recursive routes are elegible to resolve the
662 * nexthop we are looking up. Therefore, we will just iterate
663 * over the top chain of nexthops.
665 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
666 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
667 num
+= zserv_encode_nexthop(s
, nexthop
);
669 /* store nexthop_num */
670 stream_putc_at(s
, nump
, num
);
672 stream_putc(s
, 0); /* distance */
673 stream_putl(s
, 0); /* metric */
674 stream_putc(s
, 0); /* nexthop_num */
677 stream_putw_at(s
, 0, stream_get_endp(s
));
679 return zserv_send_message(client
, s
);
683 * Common utility send route notification, called from a path using a
684 * route_entry and from a path using a dataplane context.
686 static int route_notify_internal(const struct prefix
*p
, int type
,
687 uint16_t instance
, vrf_id_t vrf_id
,
689 enum zapi_route_notify_owner note
)
691 struct zserv
*client
;
695 client
= zserv_find_client(type
, instance
);
696 if (!client
|| !client
->notify_owner
) {
697 if (IS_ZEBRA_DEBUG_PACKET
) {
698 char buff
[PREFIX_STRLEN
];
701 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
702 type
, prefix2str(p
, buff
, sizeof(buff
)),
703 table_id
, note
, vrf_id
);
708 if (IS_ZEBRA_DEBUG_PACKET
) {
709 char buff
[PREFIX_STRLEN
];
711 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
712 type
, prefix2str(p
, buff
, sizeof(buff
)),
713 table_id
, note
, vrf_id
);
716 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
719 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
721 stream_put(s
, ¬e
, sizeof(note
));
723 stream_putc(s
, p
->family
);
725 blen
= prefix_blen(p
);
726 stream_putc(s
, p
->prefixlen
);
727 stream_put(s
, &p
->u
.prefix
, blen
);
729 stream_putl(s
, table_id
);
731 stream_putw_at(s
, 0, stream_get_endp(s
));
733 return zserv_send_message(client
, s
);
736 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
737 enum zapi_route_notify_owner note
)
739 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
744 * Route-owner notification using info from dataplane update context.
746 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
747 enum zapi_route_notify_owner note
)
749 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
750 dplane_ctx_get_type(ctx
),
751 dplane_ctx_get_instance(ctx
),
752 dplane_ctx_get_vrf(ctx
),
753 dplane_ctx_get_table(ctx
),
757 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
758 enum zapi_rule_notify_owner note
)
760 struct listnode
*node
;
761 struct zserv
*client
;
764 if (IS_ZEBRA_DEBUG_PACKET
)
765 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
768 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
769 if (rule
->sock
== client
->sock
)
776 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
778 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
779 stream_put(s
, ¬e
, sizeof(note
));
780 stream_putl(s
, rule
->rule
.seq
);
781 stream_putl(s
, rule
->rule
.priority
);
782 stream_putl(s
, rule
->rule
.unique
);
784 stream_putl(s
, rule
->ifp
->ifindex
);
788 stream_putw_at(s
, 0, stream_get_endp(s
));
790 zserv_send_message(client
, s
);
793 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
794 enum zapi_ipset_notify_owner note
)
796 struct listnode
*node
;
797 struct zserv
*client
;
800 if (IS_ZEBRA_DEBUG_PACKET
)
801 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
804 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
805 if (ipset
->sock
== client
->sock
)
812 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
814 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
815 stream_put(s
, ¬e
, sizeof(note
));
816 stream_putl(s
, ipset
->unique
);
817 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
818 stream_putw_at(s
, 0, stream_get_endp(s
));
820 zserv_send_message(client
, s
);
823 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
824 enum zapi_ipset_entry_notify_owner note
)
826 struct listnode
*node
;
827 struct zserv
*client
;
830 if (IS_ZEBRA_DEBUG_PACKET
)
831 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
834 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
835 if (ipset
->sock
== client
->sock
)
842 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
844 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
845 stream_put(s
, ¬e
, sizeof(note
));
846 stream_putl(s
, ipset
->unique
);
847 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
848 stream_putw_at(s
, 0, stream_get_endp(s
));
850 zserv_send_message(client
, s
);
853 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
854 enum zapi_iptable_notify_owner note
)
856 struct listnode
*node
;
857 struct zserv
*client
;
860 if (IS_ZEBRA_DEBUG_PACKET
)
861 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
864 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
865 if (iptable
->sock
== client
->sock
)
872 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
874 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
875 stream_put(s
, ¬e
, sizeof(note
));
876 stream_putl(s
, iptable
->unique
);
877 stream_putw_at(s
, 0, stream_get_endp(s
));
879 zserv_send_message(client
, s
);
882 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
883 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
888 /* Check this client need interface information. */
889 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
892 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
895 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
897 /* Prefix information. */
898 stream_putc(s
, p
->family
);
899 blen
= prefix_blen(p
);
900 stream_put(s
, &p
->u
.prefix
, blen
);
901 stream_putc(s
, p
->prefixlen
);
903 /* Write packet size. */
904 stream_putw_at(s
, 0, stream_get_endp(s
));
906 return zserv_send_message(client
, s
);
910 * Function used by Zebra to send a PW status update to LDP daemon
912 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
914 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
916 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
917 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
918 stream_putl(s
, pw
->ifindex
);
919 stream_putl(s
, pw
->status
);
921 /* Put length at the first point of the stream. */
922 stream_putw_at(s
, 0, stream_get_endp(s
));
924 return zserv_send_message(client
, s
);
927 /* Send response to a get label chunk request to client */
928 static int zsend_assign_label_chunk_response(struct zserv
*client
,
930 struct label_manager_chunk
*lmc
)
933 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
935 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
939 stream_putc(s
, lmc
->proto
);
941 stream_putw(s
, lmc
->instance
);
943 stream_putc(s
, lmc
->keep
);
944 /* start and end labels */
945 stream_putl(s
, lmc
->start
);
946 stream_putl(s
, lmc
->end
);
949 /* Write packet size. */
950 stream_putw_at(s
, 0, stream_get_endp(s
));
952 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
957 /* Send response to a label manager connect request to client */
958 static int zsend_label_manager_connect_response(struct zserv
*client
,
960 unsigned short result
)
963 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
965 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
968 stream_putc(s
, client
->proto
);
971 stream_putw(s
, client
->instance
);
974 stream_putc(s
, result
);
976 /* Write packet size. */
977 stream_putw_at(s
, 0, stream_get_endp(s
));
979 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
985 /* Send response to a get table chunk request to client */
986 static int zsend_assign_table_chunk_response(struct zserv
*client
,
988 struct table_manager_chunk
*tmc
)
990 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
992 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
995 /* start and end labels */
996 stream_putl(s
, tmc
->start
);
997 stream_putl(s
, tmc
->end
);
1000 /* Write packet size. */
1001 stream_putw_at(s
, 0, stream_get_endp(s
));
1003 return zserv_send_message(client
, s
);
1006 static int zsend_table_manager_connect_response(struct zserv
*client
,
1010 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1012 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1015 stream_putc(s
, result
);
1017 stream_putw_at(s
, 0, stream_get_endp(s
));
1019 return zserv_send_message(client
, s
);
1022 /* Inbound message handling ------------------------------------------------ */
1025 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1026 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1027 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1028 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1031 /* Nexthop register */
1032 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1037 unsigned short l
= 0;
1039 uint16_t type
= cmd2type
[hdr
->command
];
1042 if (IS_ZEBRA_DEBUG_NHT
)
1044 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1045 zebra_route_string(client
->proto
), hdr
->length
,
1046 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1051 client
->nh_reg_time
= monotime(NULL
);
1053 while (l
< hdr
->length
) {
1054 STREAM_GETC(s
, flags
);
1055 STREAM_GETW(s
, p
.family
);
1056 STREAM_GETC(s
, p
.prefixlen
);
1058 if (p
.family
== AF_INET
) {
1059 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1061 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1062 __PRETTY_FUNCTION__
, p
.prefixlen
);
1065 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1066 l
+= IPV4_MAX_BYTELEN
;
1067 } else if (p
.family
== AF_INET6
) {
1068 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1070 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1071 __PRETTY_FUNCTION__
, p
.prefixlen
);
1074 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1075 l
+= IPV6_MAX_BYTELEN
;
1078 EC_ZEBRA_UNKNOWN_FAMILY
,
1079 "rnh_register: Received unknown family type %d\n",
1083 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1087 if (type
== RNH_NEXTHOP_TYPE
) {
1089 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1090 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1092 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1093 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1094 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1096 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1097 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1099 && CHECK_FLAG(rnh
->flags
,
1100 ZEBRA_NHT_EXACT_MATCH
))
1101 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1104 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1105 /* Anything not AF_INET/INET6 has been filtered out above */
1107 zebra_evaluate_rnh(zvrf
, p
.family
, 1, type
, &p
);
1114 /* Nexthop register */
1115 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1120 unsigned short l
= 0;
1121 uint16_t type
= cmd2type
[hdr
->command
];
1123 if (IS_ZEBRA_DEBUG_NHT
)
1125 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1126 zebra_route_string(client
->proto
), hdr
->length
,
1131 while (l
< hdr
->length
) {
1134 STREAM_GETC(s
, flags
);
1136 goto stream_failure
;
1138 STREAM_GETW(s
, p
.family
);
1139 STREAM_GETC(s
, p
.prefixlen
);
1141 if (p
.family
== AF_INET
) {
1142 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1144 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1145 __PRETTY_FUNCTION__
, p
.prefixlen
);
1148 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1149 l
+= IPV4_MAX_BYTELEN
;
1150 } else if (p
.family
== AF_INET6
) {
1151 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1153 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1154 __PRETTY_FUNCTION__
, p
.prefixlen
);
1157 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1158 l
+= IPV6_MAX_BYTELEN
;
1161 EC_ZEBRA_UNKNOWN_FAMILY
,
1162 "rnh_register: Received unknown family type %d\n",
1166 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1168 client
->nh_dereg_time
= monotime(NULL
);
1169 zebra_remove_rnh_client(rnh
, client
, type
);
1176 #define ZEBRA_MIN_FEC_LENGTH 5
1179 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1182 unsigned short l
= 0;
1185 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1188 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1193 * The minimum amount of data that can be sent for one fec
1196 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1198 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1199 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1204 while (l
< hdr
->length
) {
1205 STREAM_GETW(s
, flags
);
1206 memset(&p
, 0, sizeof(p
));
1207 STREAM_GETW(s
, p
.family
);
1208 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1210 EC_ZEBRA_UNKNOWN_FAMILY
,
1211 "fec_register: Received unknown family type %d\n",
1215 STREAM_GETC(s
, p
.prefixlen
);
1216 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1217 || (p
.family
== AF_INET6
1218 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1220 "%s: Specified prefix hdr->length: %d is to long for %d",
1221 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1225 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1226 l
+= PSIZE(p
.prefixlen
);
1227 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1228 STREAM_GETL(s
, label_index
);
1231 label_index
= MPLS_INVALID_LABEL_INDEX
;
1232 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1239 /* FEC unregister */
1240 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1243 unsigned short l
= 0;
1248 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1253 * The minimum amount of data that can be sent for one
1254 * fec unregistration
1256 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1258 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1259 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1264 while (l
< hdr
->length
) {
1265 STREAM_GETW(s
, flags
);
1267 goto stream_failure
;
1269 memset(&p
, 0, sizeof(p
));
1270 STREAM_GETW(s
, p
.family
);
1271 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1273 EC_ZEBRA_UNKNOWN_FAMILY
,
1274 "fec_unregister: Received unknown family type %d\n",
1278 STREAM_GETC(s
, p
.prefixlen
);
1279 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1280 || (p
.family
== AF_INET6
1281 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1283 "%s: Received prefix hdr->length %d which is greater than %d can support",
1284 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1288 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1289 l
+= PSIZE(p
.prefixlen
);
1290 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1299 * Register zebra server interface information.
1300 * Send current all interface and address information.
1302 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1305 struct interface
*ifp
;
1307 /* Interface information is needed. */
1308 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1310 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1311 FOR_ALL_INTERFACES (vrf
, ifp
) {
1312 /* Skip pseudo interface. */
1313 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1316 zsend_interface_add(client
, ifp
);
1317 zsend_interface_addresses(client
, ifp
);
1322 /* Unregister zebra server interface information. */
1323 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1325 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1328 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1329 const unsigned int nexthop_num
)
1331 if (nexthop_num
> multipath_num
) {
1332 char buff
[PREFIX2STR_BUFFER
];
1334 prefix2str(p
, buff
, sizeof(buff
));
1336 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1337 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1338 caller
, buff
, nexthop_num
, multipath_num
);
1342 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1345 struct zapi_route api
;
1346 struct zapi_nexthop
*api_nh
;
1348 struct prefix_ipv6
*src_p
= NULL
;
1349 struct route_entry
*re
;
1350 struct nexthop
*nexthop
= NULL
;
1352 vrf_id_t vrf_id
= 0;
1353 struct ipaddr vtep_ip
;
1356 if (zapi_route_decode(s
, &api
) < 0) {
1357 if (IS_ZEBRA_DEBUG_RECV
)
1358 zlog_debug("%s: Unable to decode zapi_route sent",
1359 __PRETTY_FUNCTION__
);
1363 if (IS_ZEBRA_DEBUG_RECV
) {
1364 char buf_prefix
[PREFIX_STRLEN
];
1366 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1367 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1368 __func__
, buf_prefix
,
1369 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1374 /* Allocate new route. */
1375 vrf_id
= zvrf_id(zvrf
);
1376 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1377 re
->type
= api
.type
;
1378 re
->instance
= api
.instance
;
1379 re
->flags
= api
.flags
;
1380 re
->uptime
= time(NULL
);
1381 re
->vrf_id
= vrf_id
;
1382 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1383 re
->table
= api
.tableid
;
1385 re
->table
= zvrf
->table_id
;
1388 * TBD should _all_ of the nexthop add operations use
1389 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1390 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1392 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1393 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1394 api_nh
= &api
.nexthops
[i
];
1395 ifindex_t ifindex
= 0;
1397 if (IS_ZEBRA_DEBUG_RECV
)
1398 zlog_debug("nh type %d", api_nh
->type
);
1400 switch (api_nh
->type
) {
1401 case NEXTHOP_TYPE_IFINDEX
:
1402 nexthop
= route_entry_nexthop_ifindex_add(
1403 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1405 case NEXTHOP_TYPE_IPV4
:
1406 if (IS_ZEBRA_DEBUG_RECV
) {
1407 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1409 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1410 nhbuf
, INET6_ADDRSTRLEN
);
1411 zlog_debug("%s: nh=%s, vrf_id=%d",
1415 nexthop
= route_entry_nexthop_ipv4_add(
1416 re
, &api_nh
->gate
.ipv4
, NULL
,
1419 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1421 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1422 if (CHECK_FLAG(api
.flags
,
1423 ZEBRA_FLAG_EVPN_ROUTE
)) {
1424 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1426 ifindex
= api_nh
->ifindex
;
1429 if (IS_ZEBRA_DEBUG_RECV
) {
1430 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1432 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1433 nhbuf
, INET6_ADDRSTRLEN
);
1435 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1436 __func__
, nhbuf
, api_nh
->vrf_id
,
1437 re
->vrf_id
, ifindex
);
1439 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1440 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1443 /* if this an EVPN route entry,
1444 * program the nh as neigh
1446 if (CHECK_FLAG(api
.flags
,
1447 ZEBRA_FLAG_EVPN_ROUTE
)) {
1448 SET_FLAG(nexthop
->flags
,
1449 NEXTHOP_FLAG_EVPN_RVTEP
);
1450 vtep_ip
.ipa_type
= IPADDR_V4
;
1451 memcpy(&(vtep_ip
.ipaddr_v4
),
1452 &(api_nh
->gate
.ipv4
),
1453 sizeof(struct in_addr
));
1454 zebra_vxlan_evpn_vrf_route_add(
1455 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1459 case NEXTHOP_TYPE_IPV6
:
1460 nexthop
= route_entry_nexthop_ipv6_add(
1461 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1463 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1464 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1465 if (CHECK_FLAG(api
.flags
,
1466 ZEBRA_FLAG_EVPN_ROUTE
)) {
1467 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1469 ifindex
= api_nh
->ifindex
;
1472 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1473 re
, &api_nh
->gate
.ipv6
, ifindex
,
1476 /* if this an EVPN route entry,
1477 * program the nh as neigh
1479 if (CHECK_FLAG(api
.flags
,
1480 ZEBRA_FLAG_EVPN_ROUTE
)) {
1481 SET_FLAG(nexthop
->flags
,
1482 NEXTHOP_FLAG_EVPN_RVTEP
);
1483 vtep_ip
.ipa_type
= IPADDR_V6
;
1484 memcpy(&vtep_ip
.ipaddr_v6
,
1485 &(api_nh
->gate
.ipv6
),
1486 sizeof(struct in6_addr
));
1487 zebra_vxlan_evpn_vrf_route_add(
1488 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1492 case NEXTHOP_TYPE_BLACKHOLE
:
1493 nexthop
= route_entry_nexthop_blackhole_add(
1494 re
, api_nh
->bh_type
);
1500 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1501 "%s: Nexthops Specified: %d but we failed to properly create one",
1502 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1503 nexthops_free(re
->ng
.nexthop
);
1504 XFREE(MTYPE_RE
, re
);
1507 /* MPLS labels for BGP-LU or Segment Routing */
1508 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1509 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1510 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1511 enum lsp_types_t label_type
;
1514 lsp_type_from_re_type(client
->proto
);
1516 if (IS_ZEBRA_DEBUG_RECV
) {
1518 "%s: adding %d labels of type %d (1st=%u)",
1519 __func__
, api_nh
->label_num
,
1520 label_type
, api_nh
->labels
[0]);
1523 nexthop_add_labels(nexthop
, label_type
,
1525 &api_nh
->labels
[0]);
1529 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1530 re
->distance
= api
.distance
;
1531 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1532 re
->metric
= api
.metric
;
1533 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1535 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1538 afi
= family2afi(api
.prefix
.family
);
1539 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1540 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1541 "%s: Received SRC Prefix but afi is not v6",
1542 __PRETTY_FUNCTION__
);
1543 nexthops_free(re
->ng
.nexthop
);
1544 XFREE(MTYPE_RE
, re
);
1547 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1548 src_p
= &api
.src_prefix
;
1550 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1553 switch (api
.prefix
.family
) {
1556 client
->v4_route_add_cnt
++;
1558 client
->v4_route_upd8_cnt
++;
1562 client
->v6_route_add_cnt
++;
1564 client
->v6_route_upd8_cnt
++;
1569 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1572 struct zapi_route api
;
1574 struct prefix_ipv6
*src_p
= NULL
;
1578 if (zapi_route_decode(s
, &api
) < 0)
1581 afi
= family2afi(api
.prefix
.family
);
1582 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1583 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1584 "%s: Received a src prefix while afi is not v6",
1585 __PRETTY_FUNCTION__
);
1588 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1589 src_p
= &api
.src_prefix
;
1591 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1592 table_id
= api
.tableid
;
1594 table_id
= zvrf
->table_id
;
1596 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1597 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1598 api
.distance
, false);
1601 switch (api
.prefix
.family
) {
1603 client
->v4_route_del_cnt
++;
1606 client
->v6_route_del_cnt
++;
1611 /* MRIB Nexthop lookup for IPv4. */
1612 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1614 struct in_addr addr
;
1615 struct route_entry
*re
;
1617 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1618 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1619 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1625 /* Register zebra server router-id information. Send current router-id */
1626 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1630 /* Router-id information is needed. */
1631 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1633 router_id_get(&p
, zvrf_id(zvrf
));
1635 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1638 /* Unregister zebra server router-id information. */
1639 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1641 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1644 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1646 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1648 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1649 stream_putc(s
, mpls_enabled
);
1650 stream_putl(s
, multipath_num
);
1652 stream_putw_at(s
, 0, stream_get_endp(s
));
1653 zserv_send_message(client
, s
);
1656 /* Tie up route-type and client->sock */
1657 static void zread_hello(ZAPI_HANDLER_ARGS
)
1659 /* type of protocol (lib/zebra.h) */
1661 unsigned short instance
;
1664 STREAM_GETC(msg
, proto
);
1665 STREAM_GETW(msg
, instance
);
1666 STREAM_GETC(msg
, notify
);
1668 client
->notify_owner
= true;
1670 /* accept only dynamic routing protocols */
1671 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1673 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1674 client
->sock
, zebra_route_string(proto
),
1677 zlog_notice("client protocol instance %d", instance
);
1679 client
->proto
= proto
;
1680 client
->instance
= instance
;
1683 zsend_capabilities(client
, zvrf
);
1688 /* Unregister all information in a VRF. */
1689 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1694 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1695 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1696 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1697 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1698 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1699 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1702 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1705 enum lsp_types_t type
;
1706 struct prefix prefix
;
1707 enum nexthop_types_t gtype
;
1710 mpls_label_t in_label
, out_label
;
1713 /* Get input stream. */
1717 STREAM_GETC(s
, type
);
1718 STREAM_GETL(s
, prefix
.family
);
1719 switch (prefix
.family
) {
1721 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1722 STREAM_GETC(s
, prefix
.prefixlen
);
1723 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1725 "%s: Specified prefix length %d is greater than a v4 address can support",
1726 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1729 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1732 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1733 STREAM_GETC(s
, prefix
.prefixlen
);
1734 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1736 "%s: Specified prefix length %d is greater than a v6 address can support",
1737 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1740 STREAM_GET(&gate
.ipv6
, s
, 16);
1743 zlog_debug("%s: Specified AF %d is not supported for this call",
1744 __PRETTY_FUNCTION__
, prefix
.family
);
1747 STREAM_GETL(s
, ifindex
);
1748 STREAM_GETC(s
, distance
);
1749 STREAM_GETL(s
, in_label
);
1750 STREAM_GETL(s
, out_label
);
1752 switch (prefix
.family
) {
1755 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1757 gtype
= NEXTHOP_TYPE_IPV4
;
1761 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1763 gtype
= NEXTHOP_TYPE_IPV6
;
1772 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1773 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1775 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1776 distance
, out_label
);
1777 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1778 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1779 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1780 distance
, out_label
);
1786 /* Send response to a table manager connect request to client */
1787 static void zread_table_manager_connect(struct zserv
*client
,
1788 struct stream
*msg
, vrf_id_t vrf_id
)
1797 STREAM_GETC(s
, proto
);
1798 STREAM_GETW(s
, instance
);
1800 /* accept only dynamic routing protocols */
1801 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1802 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1803 "client %d has wrong protocol %s", client
->sock
,
1804 zebra_route_string(proto
));
1805 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1808 zlog_notice("client %d with vrf %u instance %u connected as %s",
1809 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1810 client
->proto
= proto
;
1811 client
->instance
= instance
;
1814 * Release previous labels of same protocol and instance.
1815 * This is done in case it restarted from an unexpected shutdown.
1817 release_daemon_table_chunks(client
);
1819 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1825 static void zread_label_manager_connect(struct zserv
*client
,
1826 struct stream
*msg
, vrf_id_t vrf_id
)
1829 /* type of protocol (lib/zebra.h) */
1831 unsigned short instance
;
1833 /* Get input stream. */
1837 STREAM_GETC(s
, proto
);
1838 STREAM_GETW(s
, instance
);
1840 /* accept only dynamic routing protocols */
1841 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1842 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1843 "client %d has wrong protocol %s", client
->sock
,
1844 zebra_route_string(proto
));
1845 if (client
->is_synchronous
)
1846 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1849 zlog_notice("client %d with vrf %u instance %u connected as %s",
1850 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1851 client
->proto
= proto
;
1852 client
->instance
= instance
;
1855 * Release previous labels of same protocol and instance.
1856 * This is done in case it restarted from an unexpected shutdown.
1858 release_daemon_label_chunks(client
);
1861 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1862 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1863 /* send response back */
1864 if (client
->is_synchronous
)
1865 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1871 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1877 struct label_manager_chunk
*lmc
;
1879 unsigned short instance
;
1881 /* Get input stream. */
1885 STREAM_GETC(s
, proto
);
1886 STREAM_GETW(s
, instance
);
1887 STREAM_GETC(s
, keep
);
1888 STREAM_GETL(s
, size
);
1890 lmc
= assign_label_chunk(proto
, instance
, keep
, size
);
1893 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1894 "Unable to assign Label Chunk of size %u to %s instance %u",
1895 size
, zebra_route_string(proto
), instance
);
1897 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1898 lmc
->start
, lmc
->end
,
1899 zebra_route_string(proto
), instance
);
1900 /* send response back */
1901 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1907 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1910 uint32_t start
, end
;
1912 unsigned short instance
;
1914 /* Get input stream. */
1918 STREAM_GETC(s
, proto
);
1919 STREAM_GETW(s
, instance
);
1920 STREAM_GETL(s
, start
);
1921 STREAM_GETL(s
, end
);
1923 release_label_chunk(proto
, instance
, start
, end
);
1928 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1930 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1931 client
->is_synchronous
= hdr
->command
==
1932 ZEBRA_LABEL_MANAGER_CONNECT
;
1934 /* external label manager */
1936 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1938 /* this is a label manager */
1940 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
||
1941 hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
1942 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1944 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1945 zread_get_label_chunk(client
, msg
,
1947 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1948 zread_release_label_chunk(client
, msg
);
1953 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1958 struct table_manager_chunk
*tmc
;
1960 /* Get input stream. */
1964 STREAM_GETL(s
, size
);
1966 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1968 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
1969 "%s: Unable to assign Table Chunk of size %u",
1972 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1974 /* send response back */
1975 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
1981 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
1984 uint32_t start
, end
;
1986 /* Get input stream. */
1990 STREAM_GETL(s
, start
);
1991 STREAM_GETL(s
, end
);
1993 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
1999 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2001 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2002 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2003 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2005 /* Sanity: don't allow 'unidentified' requests */
2006 if (!client
->proto
) {
2009 "Got table request from an unidentified client");
2012 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2013 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2014 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2015 zread_release_table_chunk(client
, msg
);
2019 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2022 char ifname
[IF_NAMESIZE
];
2026 union g_addr nexthop
;
2027 uint32_t local_label
;
2028 uint32_t remote_label
;
2030 union pw_protocol_fields data
;
2032 struct zebra_pw
*pw
;
2034 /* Get input stream. */
2038 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2039 STREAM_GETL(s
, ifindex
);
2040 STREAM_GETL(s
, type
);
2044 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2047 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2052 STREAM_GETL(s
, local_label
);
2053 STREAM_GETL(s
, remote_label
);
2054 STREAM_GETC(s
, flags
);
2055 STREAM_GET(&data
, s
, sizeof(data
));
2056 protocol
= client
->proto
;
2058 pw
= zebra_pw_find(zvrf
, ifname
);
2059 switch (hdr
->command
) {
2062 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2063 "%s: pseudowire %s already exists [%s]",
2065 zserv_command_string(hdr
->command
));
2069 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2071 case ZEBRA_PW_DELETE
:
2073 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2074 "%s: pseudowire %s not found [%s]", __func__
,
2075 ifname
, zserv_command_string(hdr
->command
));
2079 zebra_pw_del(zvrf
, pw
);
2082 case ZEBRA_PW_UNSET
:
2084 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2085 "%s: pseudowire %s not found [%s]", __func__
,
2086 ifname
, zserv_command_string(hdr
->command
));
2090 switch (hdr
->command
) {
2094 case ZEBRA_PW_UNSET
:
2099 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2100 remote_label
, flags
, &data
);
2108 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2110 struct interface
*master
;
2111 struct interface
*slave
;
2112 struct stream
*s
= msg
;
2116 STREAM_GETL(s
, vrf_id
);
2117 STREAM_GETL(s
, ifindex
);
2118 master
= if_lookup_by_index(ifindex
, vrf_id
);
2120 STREAM_GETL(s
, vrf_id
);
2121 STREAM_GETL(s
, ifindex
);
2122 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2124 if (!master
|| !slave
)
2127 kernel_interface_set_master(master
, slave
);
2134 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2136 struct interface
*ifp
;
2137 mpls_label_t nlabel
;
2140 struct zebra_vrf
*def_zvrf
;
2141 enum lsp_types_t ltype
;
2144 STREAM_GETL(s
, nlabel
);
2145 STREAM_GETC(s
, afi
);
2146 if (nlabel
== zvrf
->label
[afi
]) {
2148 * Nothing to do here move along
2153 STREAM_GETC(s
, ltype
);
2155 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2156 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2158 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2161 zlog_debug("Unable to find specified Interface for %s",
2166 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2168 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2172 really_remove
= true;
2173 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2174 if (scrubber
== afi
)
2177 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2180 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2181 really_remove
= false;
2187 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2188 NEXTHOP_TYPE_IFINDEX
, NULL
,
2192 if (nlabel
!= MPLS_LABEL_NONE
)
2193 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2194 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2195 NULL
, ifp
->ifindex
);
2197 zvrf
->label
[afi
] = nlabel
;
2202 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2204 struct zebra_pbr_rule zpr
;
2210 STREAM_GETL(s
, total
);
2212 for (i
= 0; i
< total
; i
++) {
2213 memset(&zpr
, 0, sizeof(zpr
));
2215 zpr
.sock
= client
->sock
;
2216 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2217 STREAM_GETL(s
, zpr
.rule
.seq
);
2218 STREAM_GETL(s
, zpr
.rule
.priority
);
2219 STREAM_GETL(s
, zpr
.rule
.unique
);
2220 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2221 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2222 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2223 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2224 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2225 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2226 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2227 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2228 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2229 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2230 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2231 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2232 STREAM_GETL(s
, ifindex
);
2235 zpr
.ifp
= if_lookup_by_index_per_ns(
2239 zlog_debug("Failed to lookup ifindex: %u",
2245 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2246 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2248 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2249 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2251 if (zpr
.rule
.filter
.src_port
)
2252 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2254 if (zpr
.rule
.filter
.dst_port
)
2255 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2257 if (zpr
.rule
.filter
.fwmark
)
2258 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2260 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2261 if (hdr
->command
== ZEBRA_RULE_ADD
)
2262 zebra_pbr_add_rule(&zpr
);
2264 zebra_pbr_del_rule(&zpr
);
2271 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2273 struct zebra_pbr_ipset zpi
;
2278 STREAM_GETL(s
, total
);
2280 for (i
= 0; i
< total
; i
++) {
2281 memset(&zpi
, 0, sizeof(zpi
));
2283 zpi
.sock
= client
->sock
;
2284 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2285 STREAM_GETL(s
, zpi
.unique
);
2286 STREAM_GETL(s
, zpi
.type
);
2287 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2289 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2290 zebra_pbr_create_ipset(&zpi
);
2292 zebra_pbr_destroy_ipset(&zpi
);
2299 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2301 struct zebra_pbr_ipset_entry zpi
;
2302 struct zebra_pbr_ipset ipset
;
2307 STREAM_GETL(s
, total
);
2309 for (i
= 0; i
< total
; i
++) {
2310 memset(&zpi
, 0, sizeof(zpi
));
2311 memset(&ipset
, 0, sizeof(ipset
));
2313 zpi
.sock
= client
->sock
;
2314 STREAM_GETL(s
, zpi
.unique
);
2315 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2316 STREAM_GETC(s
, zpi
.src
.family
);
2317 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2318 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2319 STREAM_GETC(s
, zpi
.dst
.family
);
2320 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2321 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2323 STREAM_GETW(s
, zpi
.src_port_min
);
2324 STREAM_GETW(s
, zpi
.src_port_max
);
2325 STREAM_GETW(s
, zpi
.dst_port_min
);
2326 STREAM_GETW(s
, zpi
.dst_port_max
);
2327 STREAM_GETC(s
, zpi
.proto
);
2328 if (!is_default_prefix(&zpi
.src
))
2329 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2331 if (!is_default_prefix(&zpi
.dst
))
2332 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2333 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2334 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2335 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2336 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2337 if (zpi
.dst_port_max
!= 0)
2338 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2339 if (zpi
.src_port_max
!= 0)
2340 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2342 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2344 /* calculate backpointer */
2346 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2347 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2348 zebra_pbr_add_ipset_entry(&zpi
);
2350 zebra_pbr_del_ipset_entry(&zpi
);
2357 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2359 struct zebra_pbr_iptable zpi
;
2364 memset(&zpi
, 0, sizeof(zpi
));
2366 zpi
.interface_name_list
= list_new();
2367 zpi
.sock
= client
->sock
;
2368 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2369 STREAM_GETL(s
, zpi
.unique
);
2370 STREAM_GETL(s
, zpi
.type
);
2371 STREAM_GETL(s
, zpi
.filter_bm
);
2372 STREAM_GETL(s
, zpi
.action
);
2373 STREAM_GETL(s
, zpi
.fwmark
);
2374 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2375 STREAM_GETW(s
, zpi
.pkt_len_min
);
2376 STREAM_GETW(s
, zpi
.pkt_len_max
);
2377 STREAM_GETW(s
, zpi
.tcp_flags
);
2378 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2379 STREAM_GETC(s
, zpi
.dscp_value
);
2380 STREAM_GETC(s
, zpi
.fragment
);
2381 STREAM_GETL(s
, zpi
.nb_interface
);
2382 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2384 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2385 zebra_pbr_add_iptable(&zpi
);
2387 zebra_pbr_del_iptable(&zpi
);
2392 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2393 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2394 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2395 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2396 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2397 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2398 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2399 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2400 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2401 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2402 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2403 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2404 [ZEBRA_HELLO
] = zread_hello
,
2405 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2406 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2407 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2408 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2409 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2410 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2411 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2413 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2414 #endif /* HAVE_BFDD */
2415 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2416 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2417 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2418 #if defined(HAVE_RTADV)
2419 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2420 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2422 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2423 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2425 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2426 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2427 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2428 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2429 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2430 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2431 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2432 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2433 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2434 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2435 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2436 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2437 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2438 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2439 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2440 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2441 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2442 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2443 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2444 [ZEBRA_PW_SET
] = zread_pseudowire
,
2445 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2446 [ZEBRA_RULE_ADD
] = zread_rule
,
2447 [ZEBRA_RULE_DELETE
] = zread_rule
,
2448 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2449 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2450 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2451 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2452 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2453 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2454 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2455 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2456 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2457 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2460 #if defined(HANDLE_ZAPI_FUZZING)
2461 extern struct zebra_privs_t zserv_privs
;
2463 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2465 char fname
[MAXPATHLEN
];
2466 struct stream
*copy
;
2469 copy
= stream_dup(orig
);
2470 stream_set_getp(copy
, 0);
2472 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2474 frr_elevate_privs(&zserv_privs
) {
2475 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2477 stream_flush(copy
, fd
);
2483 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2486 struct zebra_vrf
*zvrf
;
2488 zapi_parse_header(msg
, &hdr
);
2490 #if defined(HANDLE_ZAPI_FUZZING)
2491 zserv_write_incoming(msg
, hdr
.command
);
2494 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2497 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2499 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2500 zlog_debug("ZAPI message specifies unknown VRF: %d",
2505 if (hdr
.command
>= array_size(zserv_handlers
)
2506 || zserv_handlers
[hdr
.command
] == NULL
)
2507 zlog_info("Zebra received unknown command %d", hdr
.command
);
2509 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);