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/zebra_router.h"
46 #include "zebra/rib.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_errors.h"
67 #include "zebra/zebra_mlag.h"
69 /* Encoding helpers -------------------------------------------------------- */
71 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
73 /* Interface information. */
74 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
75 stream_putl(s
, ifp
->ifindex
);
76 stream_putc(s
, ifp
->status
);
77 stream_putq(s
, ifp
->flags
);
78 stream_putc(s
, ifp
->ptm_enable
);
79 stream_putc(s
, ifp
->ptm_status
);
80 stream_putl(s
, ifp
->metric
);
81 stream_putl(s
, ifp
->speed
);
82 stream_putl(s
, ifp
->mtu
);
83 stream_putl(s
, ifp
->mtu6
);
84 stream_putl(s
, ifp
->bandwidth
);
85 stream_putl(s
, ifp
->ll_type
);
86 stream_putl(s
, ifp
->hw_addr_len
);
88 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
90 /* Then, Traffic Engineering parameters if any */
91 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
93 zebra_interface_link_params_write(s
, ifp
);
97 /* Write packet size. */
98 stream_putw_at(s
, 0, stream_get_endp(s
));
101 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
103 struct vrf_data data
;
104 const char *netns_name
= zvrf_ns_name(zvrf
);
106 data
.l
.table_id
= zvrf
->table_id
;
109 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
112 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
113 /* Pass the tableid and the netns NAME */
114 stream_put(s
, &data
, sizeof(struct vrf_data
));
115 /* Interface information. */
116 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
117 /* Write packet size. */
118 stream_putw_at(s
, 0, stream_get_endp(s
));
121 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
123 stream_putl(s
, nexthop
->vrf_id
);
124 stream_putc(s
, nexthop
->type
);
125 switch (nexthop
->type
) {
126 case NEXTHOP_TYPE_IPV4
:
127 case NEXTHOP_TYPE_IPV4_IFINDEX
:
128 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
129 stream_putl(s
, nexthop
->ifindex
);
131 case NEXTHOP_TYPE_IPV6
:
132 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
134 case NEXTHOP_TYPE_IPV6_IFINDEX
:
135 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
136 stream_putl(s
, nexthop
->ifindex
);
138 case NEXTHOP_TYPE_IFINDEX
:
139 stream_putl(s
, nexthop
->ifindex
);
148 /* Send handlers ----------------------------------------------------------- */
150 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
152 * This function is called in the following situations:
153 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
155 * - at startup, when zebra figures out the available interfaces
156 * - when an interface is added (where support for
157 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
158 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
161 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
163 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
165 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
166 zserv_encode_interface(s
, ifp
);
169 return zserv_send_message(client
, s
);
172 /* Interface deletion from zebra daemon. */
173 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
175 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
177 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
178 zserv_encode_interface(s
, ifp
);
181 return zserv_send_message(client
, s
);
184 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
186 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
188 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
189 zserv_encode_vrf(s
, zvrf
);
191 client
->vrfadd_cnt
++;
192 return zserv_send_message(client
, s
);
195 /* VRF deletion from zebra daemon. */
196 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
199 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
201 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
202 zserv_encode_vrf(s
, zvrf
);
204 client
->vrfdel_cnt
++;
205 return zserv_send_message(client
, s
);
208 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
210 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
212 if (!ifp
->link_params
) {
217 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
219 /* Add Interface Index */
220 stream_putl(s
, ifp
->ifindex
);
222 /* Then TE Link Parameters */
223 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
228 /* Write packet size. */
229 stream_putw_at(s
, 0, stream_get_endp(s
));
231 return zserv_send_message(client
, s
);
234 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
235 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
237 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
238 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
239 * from the client, after the ZEBRA_INTERFACE_ADD has been
240 * sent from zebra to the client
241 * - redistribute new address info to all clients in the following situations
242 * - at startup, when zebra figures out the available interfaces
243 * - when an interface is added (where support for
244 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
245 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
247 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
248 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
249 * - when an RTM_NEWADDR message is received from the kernel,
251 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
253 * zsend_interface_address(DELETE)
256 * zebra_interface_address_delete_update
258 * | | if_delete_update
260 * ip_address_uninstall connected_delete_ipv4
261 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
264 * | RTM_NEWADDR on routing/netlink socket
267 * "no ip address A.B.C.D/M [label LINE]"
268 * "no ip address A.B.C.D/M"
269 * ["no ipv6 address X:X::X:X/M"]
272 int zsend_interface_address(int cmd
, struct zserv
*client
,
273 struct interface
*ifp
, struct connected
*ifc
)
277 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
279 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
280 stream_putl(s
, ifp
->ifindex
);
282 /* Interface address flag. */
283 stream_putc(s
, ifc
->flags
);
285 /* Prefix information. */
287 stream_putc(s
, p
->family
);
288 blen
= prefix_blen(p
);
289 stream_put(s
, &p
->u
.prefix
, blen
);
292 * XXX gnu version does not send prefixlen for
293 * ZEBRA_INTERFACE_ADDRESS_DELETE
294 * but zebra_interface_address_delete_read() in the gnu version
297 stream_putc(s
, p
->prefixlen
);
300 p
= ifc
->destination
;
302 stream_put(s
, &p
->u
.prefix
, blen
);
304 stream_put(s
, NULL
, blen
);
306 /* Write packet size. */
307 stream_putw_at(s
, 0, stream_get_endp(s
));
309 client
->connected_rt_add_cnt
++;
310 return zserv_send_message(client
, s
);
313 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
314 struct interface
*ifp
,
315 struct nbr_connected
*ifc
)
318 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
321 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
322 stream_putl(s
, ifp
->ifindex
);
324 /* Prefix information. */
326 stream_putc(s
, p
->family
);
327 blen
= prefix_blen(p
);
328 stream_put(s
, &p
->u
.prefix
, blen
);
331 * XXX gnu version does not send prefixlen for
332 * ZEBRA_INTERFACE_ADDRESS_DELETE
333 * but zebra_interface_address_delete_read() in the gnu version
336 stream_putc(s
, p
->prefixlen
);
338 /* Write packet size. */
339 stream_putw_at(s
, 0, stream_get_endp(s
));
341 return zserv_send_message(client
, s
);
344 /* Interface address addition. */
345 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
346 struct nbr_connected
*ifc
)
348 struct listnode
*node
, *nnode
;
349 struct zserv
*client
;
352 if (IS_ZEBRA_DEBUG_EVENT
) {
353 char buf
[INET6_ADDRSTRLEN
];
357 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
358 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
360 p
->prefixlen
, ifc
->ifp
->name
);
363 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
364 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
368 /* Interface address deletion. */
369 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
370 struct nbr_connected
*ifc
)
372 struct listnode
*node
, *nnode
;
373 struct zserv
*client
;
376 if (IS_ZEBRA_DEBUG_EVENT
) {
377 char buf
[INET6_ADDRSTRLEN
];
381 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
382 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
384 p
->prefixlen
, ifc
->ifp
->name
);
387 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
388 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
392 /* Send addresses on interface to client */
393 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
395 struct listnode
*cnode
, *cnnode
;
397 struct nbr_connected
*nc
;
399 /* Send interface addresses. */
400 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
401 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
404 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
410 /* Send interface neighbors. */
411 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
412 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
421 /* Notify client about interface moving from one VRF to another.
422 * Whether client is interested in old and new VRF is checked by caller.
424 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
427 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
429 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
431 /* Fill in the name of the interface and its new VRF (id) */
432 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
433 stream_putl(s
, vrf_id
);
435 /* Write packet size. */
436 stream_putw_at(s
, 0, stream_get_endp(s
));
438 client
->if_vrfchg_cnt
++;
439 return zserv_send_message(client
, s
);
442 /* Add new nbr connected IPv6 address */
443 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
445 struct nbr_connected
*ifc
;
449 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
450 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
452 ifc
= listnode_head(ifp
->nbr_connected
);
455 ifc
= nbr_connected_new();
456 ifc
->address
= prefix_new();
458 listnode_add(ifp
->nbr_connected
, ifc
);
461 prefix_copy(ifc
->address
, &p
);
463 zebra_interface_nbr_address_add_update(ifp
, ifc
);
465 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
468 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
470 struct nbr_connected
*ifc
;
474 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
475 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
477 ifc
= nbr_connected_check(ifp
, &p
);
481 listnode_delete(ifp
->nbr_connected
, ifc
);
483 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
485 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
487 nbr_connected_free(ifc
);
491 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
492 * ZEBRA_INTERFACE_DOWN.
494 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
495 * the clients in one of 2 situations:
496 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
497 * - a vty command modifying the bandwidth of an interface is received.
498 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
500 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
502 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
504 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
505 zserv_encode_interface(s
, ifp
);
507 if (cmd
== ZEBRA_INTERFACE_UP
)
510 client
->ifdown_cnt
++;
512 return zserv_send_message(client
, s
);
515 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
516 const struct prefix
*p
,
517 const struct prefix
*src_p
, struct route_entry
*re
)
519 struct zapi_route api
;
520 struct zapi_nexthop
*api_nh
;
521 struct nexthop
*nexthop
;
525 memset(&api
, 0, sizeof(api
));
526 api
.vrf_id
= re
->vrf_id
;
528 api
.safi
= SAFI_UNICAST
;
529 api
.instance
= re
->instance
;
530 api
.flags
= re
->flags
;
532 afi
= family2afi(p
->family
);
535 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
536 client
->redist_v4_add_cnt
++;
538 client
->redist_v4_del_cnt
++;
541 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
542 client
->redist_v6_add_cnt
++;
544 client
->redist_v6_del_cnt
++;
553 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
554 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
558 if (re
->nexthop_active_num
) {
559 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
560 api
.nexthop_num
= re
->nexthop_active_num
;
562 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
563 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
566 api_nh
= &api
.nexthops
[count
];
567 api_nh
->vrf_id
= nexthop
->vrf_id
;
568 api_nh
->type
= nexthop
->type
;
569 switch (nexthop
->type
) {
570 case NEXTHOP_TYPE_BLACKHOLE
:
571 api_nh
->bh_type
= nexthop
->bh_type
;
573 case NEXTHOP_TYPE_IPV4
:
574 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
576 case NEXTHOP_TYPE_IPV4_IFINDEX
:
577 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
578 api_nh
->ifindex
= nexthop
->ifindex
;
580 case NEXTHOP_TYPE_IFINDEX
:
581 api_nh
->ifindex
= nexthop
->ifindex
;
583 case NEXTHOP_TYPE_IPV6
:
584 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
586 case NEXTHOP_TYPE_IPV6_IFINDEX
:
587 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
588 api_nh
->ifindex
= nexthop
->ifindex
;
594 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
595 api
.distance
= re
->distance
;
596 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
597 api
.metric
= re
->metric
;
599 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
602 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
605 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
607 /* Encode route and send. */
608 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
613 if (IS_ZEBRA_DEBUG_SEND
) {
614 char buf_prefix
[PREFIX_STRLEN
];
616 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
618 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
619 __func__
, zserv_command_string(cmd
),
620 zebra_route_string(client
->proto
),
621 zebra_route_string(api
.type
), api
.vrf_id
,
624 return zserv_send_message(client
, s
);
628 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
629 * nexthop is not found on mrib. Returns both route metric and protocol
632 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
634 struct route_entry
*re
,
635 struct zebra_vrf
*zvrf
)
640 struct nexthop
*nexthop
;
642 /* Get output stream. */
643 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
646 /* Fill in result. */
647 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
648 stream_put_in_addr(s
, &addr
);
651 stream_putc(s
, re
->distance
);
652 stream_putl(s
, re
->metric
);
654 /* remember position for nexthop_num */
655 nump
= stream_get_endp(s
);
656 /* reserve room for nexthop_num */
659 * Only non-recursive routes are elegible to resolve the
660 * nexthop we are looking up. Therefore, we will just iterate
661 * over the top chain of nexthops.
663 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
664 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
665 num
+= zserv_encode_nexthop(s
, nexthop
);
667 /* store nexthop_num */
668 stream_putc_at(s
, nump
, num
);
670 stream_putc(s
, 0); /* distance */
671 stream_putl(s
, 0); /* metric */
672 stream_putc(s
, 0); /* nexthop_num */
675 stream_putw_at(s
, 0, stream_get_endp(s
));
677 return zserv_send_message(client
, s
);
681 * Common utility send route notification, called from a path using a
682 * route_entry and from a path using a dataplane context.
684 static int route_notify_internal(const struct prefix
*p
, int type
,
685 uint16_t instance
, vrf_id_t vrf_id
,
687 enum zapi_route_notify_owner note
)
689 struct zserv
*client
;
693 client
= zserv_find_client(type
, instance
);
694 if (!client
|| !client
->notify_owner
) {
695 if (IS_ZEBRA_DEBUG_PACKET
) {
696 char buff
[PREFIX_STRLEN
];
699 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
700 type
, prefix2str(p
, buff
, sizeof(buff
)),
701 table_id
, note
, vrf_id
);
706 if (IS_ZEBRA_DEBUG_PACKET
) {
707 char buff
[PREFIX_STRLEN
];
709 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
710 type
, prefix2str(p
, buff
, sizeof(buff
)),
711 table_id
, note
, vrf_id
);
714 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
717 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
719 stream_put(s
, ¬e
, sizeof(note
));
721 stream_putc(s
, p
->family
);
723 blen
= prefix_blen(p
);
724 stream_putc(s
, p
->prefixlen
);
725 stream_put(s
, &p
->u
.prefix
, blen
);
727 stream_putl(s
, table_id
);
729 stream_putw_at(s
, 0, stream_get_endp(s
));
731 return zserv_send_message(client
, s
);
734 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
735 enum zapi_route_notify_owner note
)
737 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
742 * Route-owner notification using info from dataplane update context.
744 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
745 enum zapi_route_notify_owner note
)
747 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
748 dplane_ctx_get_type(ctx
),
749 dplane_ctx_get_instance(ctx
),
750 dplane_ctx_get_vrf(ctx
),
751 dplane_ctx_get_table(ctx
),
755 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
756 enum zapi_rule_notify_owner note
)
758 struct listnode
*node
;
759 struct zserv
*client
;
762 if (IS_ZEBRA_DEBUG_PACKET
)
763 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
766 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
767 if (rule
->sock
== client
->sock
)
774 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
776 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
777 stream_put(s
, ¬e
, sizeof(note
));
778 stream_putl(s
, rule
->rule
.seq
);
779 stream_putl(s
, rule
->rule
.priority
);
780 stream_putl(s
, rule
->rule
.unique
);
782 stream_putl(s
, rule
->ifp
->ifindex
);
786 stream_putw_at(s
, 0, stream_get_endp(s
));
788 zserv_send_message(client
, s
);
791 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
792 enum zapi_ipset_notify_owner note
)
794 struct listnode
*node
;
795 struct zserv
*client
;
798 if (IS_ZEBRA_DEBUG_PACKET
)
799 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
802 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
803 if (ipset
->sock
== client
->sock
)
810 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
812 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
813 stream_put(s
, ¬e
, sizeof(note
));
814 stream_putl(s
, ipset
->unique
);
815 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
816 stream_putw_at(s
, 0, stream_get_endp(s
));
818 zserv_send_message(client
, s
);
821 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
822 enum zapi_ipset_entry_notify_owner note
)
824 struct listnode
*node
;
825 struct zserv
*client
;
828 if (IS_ZEBRA_DEBUG_PACKET
)
829 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
832 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
833 if (ipset
->sock
== client
->sock
)
840 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
842 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
843 stream_put(s
, ¬e
, sizeof(note
));
844 stream_putl(s
, ipset
->unique
);
845 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
846 stream_putw_at(s
, 0, stream_get_endp(s
));
848 zserv_send_message(client
, s
);
851 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
852 enum zapi_iptable_notify_owner note
)
854 struct listnode
*node
;
855 struct zserv
*client
;
858 if (IS_ZEBRA_DEBUG_PACKET
)
859 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
862 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
863 if (iptable
->sock
== client
->sock
)
870 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
872 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
873 stream_put(s
, ¬e
, sizeof(note
));
874 stream_putl(s
, iptable
->unique
);
875 stream_putw_at(s
, 0, stream_get_endp(s
));
877 zserv_send_message(client
, s
);
880 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
881 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
886 /* Check this client need interface information. */
887 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
890 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
893 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
895 /* Prefix information. */
896 stream_putc(s
, p
->family
);
897 blen
= prefix_blen(p
);
898 stream_put(s
, &p
->u
.prefix
, blen
);
899 stream_putc(s
, p
->prefixlen
);
901 /* Write packet size. */
902 stream_putw_at(s
, 0, stream_get_endp(s
));
904 return zserv_send_message(client
, s
);
908 * Function used by Zebra to send a PW status update to LDP daemon
910 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
912 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
914 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
915 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
916 stream_putl(s
, pw
->ifindex
);
917 stream_putl(s
, pw
->status
);
919 /* Put length at the first point of the stream. */
920 stream_putw_at(s
, 0, stream_get_endp(s
));
922 return zserv_send_message(client
, s
);
925 /* Send response to a get label chunk request to client */
926 static int zsend_assign_label_chunk_response(struct zserv
*client
,
928 struct label_manager_chunk
*lmc
)
931 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
933 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
937 stream_putc(s
, lmc
->proto
);
939 stream_putw(s
, lmc
->instance
);
941 stream_putc(s
, lmc
->keep
);
942 /* start and end labels */
943 stream_putl(s
, lmc
->start
);
944 stream_putl(s
, lmc
->end
);
947 /* Write packet size. */
948 stream_putw_at(s
, 0, stream_get_endp(s
));
950 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
955 /* Send response to a label manager connect request to client */
956 static int zsend_label_manager_connect_response(struct zserv
*client
,
958 unsigned short result
)
961 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
963 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
966 stream_putc(s
, client
->proto
);
969 stream_putw(s
, client
->instance
);
972 stream_putc(s
, result
);
974 /* Write packet size. */
975 stream_putw_at(s
, 0, stream_get_endp(s
));
977 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
983 /* Send response to a get table chunk request to client */
984 static int zsend_assign_table_chunk_response(struct zserv
*client
,
986 struct table_manager_chunk
*tmc
)
988 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
990 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
993 /* start and end labels */
994 stream_putl(s
, tmc
->start
);
995 stream_putl(s
, tmc
->end
);
998 /* Write packet size. */
999 stream_putw_at(s
, 0, stream_get_endp(s
));
1001 return zserv_send_message(client
, s
);
1004 static int zsend_table_manager_connect_response(struct zserv
*client
,
1008 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1010 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1013 stream_putc(s
, result
);
1015 stream_putw_at(s
, 0, stream_get_endp(s
));
1017 return zserv_send_message(client
, s
);
1020 /* Inbound message handling ------------------------------------------------ */
1023 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1024 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1025 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1026 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1029 /* Nexthop register */
1030 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1035 unsigned short l
= 0;
1037 uint16_t type
= cmd2type
[hdr
->command
];
1039 bool flag_changed
= false;
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 client
->v4_nh_watch_add_cnt
++;
1060 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1062 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1063 __PRETTY_FUNCTION__
, p
.prefixlen
);
1066 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1067 l
+= IPV4_MAX_BYTELEN
;
1068 } else if (p
.family
== AF_INET6
) {
1069 client
->v6_nh_watch_add_cnt
++;
1070 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1072 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1073 __PRETTY_FUNCTION__
, p
.prefixlen
);
1076 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1077 l
+= IPV6_MAX_BYTELEN
;
1080 EC_ZEBRA_UNKNOWN_FAMILY
,
1081 "rnh_register: Received unknown family type %d\n",
1085 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1089 orig_flags
= rnh
->flags
;
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 if (orig_flags
!= rnh
->flags
)
1108 flag_changed
= true;
1110 /* Anything not AF_INET/INET6 has been filtered out above */
1111 if (!exist
|| flag_changed
)
1112 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1115 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1122 /* Nexthop register */
1123 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1128 unsigned short l
= 0;
1129 uint16_t type
= cmd2type
[hdr
->command
];
1131 if (IS_ZEBRA_DEBUG_NHT
)
1133 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1134 zebra_route_string(client
->proto
), hdr
->length
,
1139 while (l
< hdr
->length
) {
1142 STREAM_GETC(s
, flags
);
1144 goto stream_failure
;
1146 STREAM_GETW(s
, p
.family
);
1147 STREAM_GETC(s
, p
.prefixlen
);
1149 if (p
.family
== AF_INET
) {
1150 client
->v4_nh_watch_rem_cnt
++;
1151 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1153 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1154 __PRETTY_FUNCTION__
, p
.prefixlen
);
1157 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1158 l
+= IPV4_MAX_BYTELEN
;
1159 } else if (p
.family
== AF_INET6
) {
1160 client
->v6_nh_watch_rem_cnt
++;
1161 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1163 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1164 __PRETTY_FUNCTION__
, p
.prefixlen
);
1167 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1168 l
+= IPV6_MAX_BYTELEN
;
1171 EC_ZEBRA_UNKNOWN_FAMILY
,
1172 "rnh_register: Received unknown family type %d\n",
1176 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1178 client
->nh_dereg_time
= monotime(NULL
);
1179 zebra_remove_rnh_client(rnh
, client
, type
);
1186 #define ZEBRA_MIN_FEC_LENGTH 5
1189 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1192 unsigned short l
= 0;
1195 uint32_t label
= MPLS_INVALID_LABEL
;
1196 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1199 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1204 * The minimum amount of data that can be sent for one fec
1207 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1209 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1210 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1215 while (l
< hdr
->length
) {
1216 STREAM_GETW(s
, flags
);
1217 memset(&p
, 0, sizeof(p
));
1218 STREAM_GETW(s
, p
.family
);
1219 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1221 EC_ZEBRA_UNKNOWN_FAMILY
,
1222 "fec_register: Received unknown family type %d\n",
1226 STREAM_GETC(s
, p
.prefixlen
);
1227 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1228 || (p
.family
== AF_INET6
1229 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1231 "%s: Specified prefix hdr->length: %d is to long for %d",
1232 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1236 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1237 l
+= PSIZE(p
.prefixlen
);
1238 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1239 STREAM_GETL(s
, label
);
1241 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1242 STREAM_GETL(s
, label_index
);
1246 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1253 /* FEC unregister */
1254 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1257 unsigned short l
= 0;
1262 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1267 * The minimum amount of data that can be sent for one
1268 * fec unregistration
1270 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1272 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1273 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1278 while (l
< hdr
->length
) {
1279 STREAM_GETW(s
, flags
);
1281 goto stream_failure
;
1283 memset(&p
, 0, sizeof(p
));
1284 STREAM_GETW(s
, p
.family
);
1285 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1287 EC_ZEBRA_UNKNOWN_FAMILY
,
1288 "fec_unregister: Received unknown family type %d\n",
1292 STREAM_GETC(s
, p
.prefixlen
);
1293 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1294 || (p
.family
== AF_INET6
1295 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1297 "%s: Received prefix hdr->length %d which is greater than %d can support",
1298 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1302 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1303 l
+= PSIZE(p
.prefixlen
);
1304 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1313 * Register zebra server interface information.
1314 * Send current all interface and address information.
1316 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1319 struct interface
*ifp
;
1321 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1322 FOR_ALL_INTERFACES (vrf
, ifp
) {
1323 /* Skip pseudo interface. */
1324 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1327 zsend_interface_add(client
, ifp
);
1328 zsend_interface_link_params(client
, ifp
);
1329 zsend_interface_addresses(client
, ifp
);
1334 /* Unregister zebra server interface information. */
1335 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1339 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1340 const unsigned int nexthop_num
)
1342 if (nexthop_num
> multipath_num
) {
1343 char buff
[PREFIX2STR_BUFFER
];
1345 prefix2str(p
, buff
, sizeof(buff
));
1347 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1348 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1349 caller
, buff
, nexthop_num
, multipath_num
);
1353 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1356 struct zapi_route api
;
1357 struct zapi_nexthop
*api_nh
;
1359 struct prefix_ipv6
*src_p
= NULL
;
1360 struct route_entry
*re
;
1361 struct nexthop
*nexthop
= NULL
;
1363 vrf_id_t vrf_id
= 0;
1364 struct ipaddr vtep_ip
;
1367 if (zapi_route_decode(s
, &api
) < 0) {
1368 if (IS_ZEBRA_DEBUG_RECV
)
1369 zlog_debug("%s: Unable to decode zapi_route sent",
1370 __PRETTY_FUNCTION__
);
1374 if (IS_ZEBRA_DEBUG_RECV
) {
1375 char buf_prefix
[PREFIX_STRLEN
];
1377 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1378 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1379 __func__
, buf_prefix
,
1380 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1385 /* Allocate new route. */
1386 vrf_id
= zvrf_id(zvrf
);
1387 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1388 re
->type
= api
.type
;
1389 re
->instance
= api
.instance
;
1390 re
->flags
= api
.flags
;
1391 re
->uptime
= monotime(NULL
);
1392 re
->vrf_id
= vrf_id
;
1393 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1394 re
->table
= api
.tableid
;
1396 re
->table
= zvrf
->table_id
;
1398 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1399 || api
.nexthop_num
== 0) {
1400 char buf_prefix
[PREFIX_STRLEN
];
1402 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1403 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1404 "%s: received a route without nexthops for prefix %s",
1405 __func__
, buf_prefix
);
1406 XFREE(MTYPE_RE
, re
);
1411 * TBD should _all_ of the nexthop add operations use
1412 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1413 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1415 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1416 api_nh
= &api
.nexthops
[i
];
1417 ifindex_t ifindex
= 0;
1419 if (IS_ZEBRA_DEBUG_RECV
)
1420 zlog_debug("nh type %d", api_nh
->type
);
1422 switch (api_nh
->type
) {
1423 case NEXTHOP_TYPE_IFINDEX
:
1424 nexthop
= route_entry_nexthop_ifindex_add(
1425 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1427 case NEXTHOP_TYPE_IPV4
:
1428 if (IS_ZEBRA_DEBUG_RECV
) {
1429 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1431 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1433 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1434 nhbuf
, api_nh
->vrf_id
);
1436 nexthop
= route_entry_nexthop_ipv4_add(
1437 re
, &api_nh
->gate
.ipv4
, NULL
, api_nh
->vrf_id
);
1439 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1441 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1442 ifindex
= api_nh
->ifindex
;
1443 if (IS_ZEBRA_DEBUG_RECV
) {
1444 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1446 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1449 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1450 __func__
, nhbuf
, api_nh
->vrf_id
,
1451 re
->vrf_id
, ifindex
);
1453 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1454 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1457 /* Special handling for IPv4 routes sourced from EVPN:
1458 * the nexthop and associated MAC need to be installed.
1460 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1461 vtep_ip
.ipa_type
= IPADDR_V4
;
1462 memcpy(&(vtep_ip
.ipaddr_v4
),
1463 &(api_nh
->gate
.ipv4
),
1464 sizeof(struct in_addr
));
1465 zebra_vxlan_evpn_vrf_route_add(
1466 api_nh
->vrf_id
, &api_nh
->rmac
,
1467 &vtep_ip
, &api
.prefix
);
1470 case NEXTHOP_TYPE_IPV6
:
1471 nexthop
= route_entry_nexthop_ipv6_add(
1472 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1474 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1475 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1476 ifindex
= api_nh
->ifindex
;
1477 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1478 re
, &api_nh
->gate
.ipv6
, ifindex
,
1481 /* Special handling for IPv6 routes sourced from EVPN:
1482 * the nexthop and associated MAC need to be installed.
1484 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1485 vtep_ip
.ipa_type
= IPADDR_V6
;
1486 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1487 sizeof(struct in6_addr
));
1488 zebra_vxlan_evpn_vrf_route_add(
1489 api_nh
->vrf_id
, &api_nh
->rmac
,
1490 &vtep_ip
, &api
.prefix
);
1493 case NEXTHOP_TYPE_BLACKHOLE
:
1494 nexthop
= route_entry_nexthop_blackhole_add(
1495 re
, api_nh
->bh_type
);
1501 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1502 "%s: Nexthops Specified: %d but we failed to properly create one",
1503 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1504 nexthops_free(re
->ng
.nexthop
);
1505 XFREE(MTYPE_RE
, re
);
1509 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1511 /* MPLS labels for BGP-LU or Segment Routing */
1512 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1513 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1514 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1515 enum lsp_types_t label_type
;
1517 label_type
= lsp_type_from_re_type(client
->proto
);
1519 if (IS_ZEBRA_DEBUG_RECV
) {
1521 "%s: adding %d labels of type %d (1st=%u)",
1522 __func__
, api_nh
->label_num
, label_type
,
1526 nexthop_add_labels(nexthop
, label_type
,
1528 &api_nh
->labels
[0]);
1532 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1533 re
->distance
= api
.distance
;
1534 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1535 re
->metric
= api
.metric
;
1536 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1538 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1541 afi
= family2afi(api
.prefix
.family
);
1542 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1543 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1544 "%s: Received SRC Prefix but afi is not v6",
1545 __PRETTY_FUNCTION__
);
1546 nexthops_free(re
->ng
.nexthop
);
1547 XFREE(MTYPE_RE
, re
);
1550 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1551 src_p
= &api
.src_prefix
;
1553 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1556 switch (api
.prefix
.family
) {
1559 client
->v4_route_add_cnt
++;
1561 client
->v4_route_upd8_cnt
++;
1565 client
->v6_route_add_cnt
++;
1567 client
->v6_route_upd8_cnt
++;
1572 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1575 struct zapi_route api
;
1577 struct prefix_ipv6
*src_p
= NULL
;
1581 if (zapi_route_decode(s
, &api
) < 0)
1584 afi
= family2afi(api
.prefix
.family
);
1585 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1586 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1587 "%s: Received a src prefix while afi is not v6",
1588 __PRETTY_FUNCTION__
);
1591 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1592 src_p
= &api
.src_prefix
;
1594 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1595 table_id
= api
.tableid
;
1597 table_id
= zvrf
->table_id
;
1599 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1600 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1601 api
.distance
, false);
1604 switch (api
.prefix
.family
) {
1606 client
->v4_route_del_cnt
++;
1609 client
->v6_route_del_cnt
++;
1614 /* MRIB Nexthop lookup for IPv4. */
1615 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1617 struct in_addr addr
;
1618 struct route_entry
*re
;
1620 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1621 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1622 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1628 /* Register zebra server router-id information. Send current router-id */
1629 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1633 /* Router-id information is needed. */
1634 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1636 router_id_get(&p
, zvrf_id(zvrf
));
1638 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1641 /* Unregister zebra server router-id information. */
1642 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1644 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1647 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1649 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1651 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1652 stream_putl(s
, vrf_get_backend());
1653 stream_putc(s
, mpls_enabled
);
1654 stream_putl(s
, multipath_num
);
1655 stream_putc(s
, zebra_mlag_get_role());
1657 stream_putw_at(s
, 0, stream_get_endp(s
));
1658 zserv_send_message(client
, s
);
1661 void zsend_capabilities_all_clients(void)
1663 struct listnode
*node
, *nnode
;
1664 struct zebra_vrf
*zvrf
;
1665 struct zserv
*client
;
1667 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1668 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1669 zsend_capabilities(client
, zvrf
);
1673 /* Tie up route-type and client->sock */
1674 static void zread_hello(ZAPI_HANDLER_ARGS
)
1676 /* type of protocol (lib/zebra.h) */
1678 unsigned short instance
;
1681 STREAM_GETC(msg
, proto
);
1682 STREAM_GETW(msg
, instance
);
1683 STREAM_GETC(msg
, notify
);
1685 client
->notify_owner
= true;
1687 /* accept only dynamic routing protocols */
1688 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1690 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1691 client
->sock
, zebra_route_string(proto
),
1694 zlog_notice("client protocol instance %d", instance
);
1696 client
->proto
= proto
;
1697 client
->instance
= instance
;
1700 zsend_capabilities(client
, zvrf
);
1701 zebra_vrf_update_all(client
);
1706 /* Unregister all information in a VRF. */
1707 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1712 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1713 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1714 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1715 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
1717 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1720 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1723 enum lsp_types_t type
;
1724 struct prefix prefix
;
1725 enum nexthop_types_t gtype
;
1728 mpls_label_t in_label
, out_label
;
1731 /* Get input stream. */
1735 STREAM_GETC(s
, type
);
1736 STREAM_GETL(s
, prefix
.family
);
1737 switch (prefix
.family
) {
1739 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1740 STREAM_GETC(s
, prefix
.prefixlen
);
1741 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1743 "%s: Specified prefix length %d is greater than a v4 address can support",
1744 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1747 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1750 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1751 STREAM_GETC(s
, prefix
.prefixlen
);
1752 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1754 "%s: Specified prefix length %d is greater than a v6 address can support",
1755 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1758 STREAM_GET(&gate
.ipv6
, s
, 16);
1761 zlog_debug("%s: Specified AF %d is not supported for this call",
1762 __PRETTY_FUNCTION__
, prefix
.family
);
1765 STREAM_GETL(s
, ifindex
);
1766 STREAM_GETC(s
, distance
);
1767 STREAM_GETL(s
, in_label
);
1768 STREAM_GETL(s
, out_label
);
1770 switch (prefix
.family
) {
1773 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1775 gtype
= NEXTHOP_TYPE_IPV4
;
1779 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1781 gtype
= NEXTHOP_TYPE_IPV6
;
1790 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1791 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1793 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1794 distance
, out_label
);
1795 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1796 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1797 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1798 distance
, out_label
);
1804 /* Send response to a table manager connect request to client */
1805 static void zread_table_manager_connect(struct zserv
*client
,
1806 struct stream
*msg
, vrf_id_t vrf_id
)
1815 STREAM_GETC(s
, proto
);
1816 STREAM_GETW(s
, instance
);
1818 /* accept only dynamic routing protocols */
1819 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1820 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1821 "client %d has wrong protocol %s", client
->sock
,
1822 zebra_route_string(proto
));
1823 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1826 zlog_notice("client %d with vrf %u instance %u connected as %s",
1827 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1828 client
->proto
= proto
;
1829 client
->instance
= instance
;
1832 * Release previous labels of same protocol and instance.
1833 * This is done in case it restarted from an unexpected shutdown.
1835 release_daemon_table_chunks(client
);
1837 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1843 static void zread_label_manager_connect(struct zserv
*client
,
1844 struct stream
*msg
, vrf_id_t vrf_id
)
1847 /* type of protocol (lib/zebra.h) */
1849 unsigned short instance
;
1851 /* Get input stream. */
1855 STREAM_GETC(s
, proto
);
1856 STREAM_GETW(s
, instance
);
1858 /* accept only dynamic routing protocols */
1859 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1860 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1861 "client %d has wrong protocol %s", client
->sock
,
1862 zebra_route_string(proto
));
1863 if (client
->is_synchronous
)
1864 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1867 zlog_notice("client %d with vrf %u instance %u connected as %s",
1868 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1869 client
->proto
= proto
;
1870 client
->instance
= instance
;
1873 * Release previous labels of same protocol and instance.
1874 * This is done in case it restarted from an unexpected shutdown.
1876 release_daemon_label_chunks(client
);
1879 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1880 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1881 /* send response back */
1882 if (client
->is_synchronous
)
1883 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1889 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1895 struct label_manager_chunk
*lmc
;
1897 unsigned short instance
;
1899 /* Get input stream. */
1903 STREAM_GETC(s
, proto
);
1904 STREAM_GETW(s
, instance
);
1905 STREAM_GETC(s
, keep
);
1906 STREAM_GETL(s
, size
);
1908 lmc
= assign_label_chunk(proto
, instance
, keep
, size
);
1911 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1912 "Unable to assign Label Chunk of size %u to %s instance %u",
1913 size
, zebra_route_string(proto
), instance
);
1915 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1916 lmc
->start
, lmc
->end
,
1917 zebra_route_string(proto
), instance
);
1918 /* send response back */
1919 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1925 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1928 uint32_t start
, end
;
1930 unsigned short instance
;
1932 /* Get input stream. */
1936 STREAM_GETC(s
, proto
);
1937 STREAM_GETW(s
, instance
);
1938 STREAM_GETL(s
, start
);
1939 STREAM_GETL(s
, end
);
1941 release_label_chunk(proto
, instance
, start
, end
);
1946 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1948 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1949 client
->is_synchronous
= hdr
->command
==
1950 ZEBRA_LABEL_MANAGER_CONNECT
;
1952 /* external label manager */
1954 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1956 /* this is a label manager */
1958 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
||
1959 hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
1960 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1962 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1963 zread_get_label_chunk(client
, msg
,
1965 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1966 zread_release_label_chunk(client
, msg
);
1971 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1976 struct table_manager_chunk
*tmc
;
1978 /* Get input stream. */
1982 STREAM_GETL(s
, size
);
1984 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1986 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
1987 "%s: Unable to assign Table Chunk of size %u",
1990 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1992 /* send response back */
1993 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
1999 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2002 uint32_t start
, end
;
2004 /* Get input stream. */
2008 STREAM_GETL(s
, start
);
2009 STREAM_GETL(s
, end
);
2011 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2017 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2019 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2020 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2021 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2023 /* Sanity: don't allow 'unidentified' requests */
2024 if (!client
->proto
) {
2027 "Got table request from an unidentified client");
2030 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2031 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2032 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2033 zread_release_table_chunk(client
, msg
);
2037 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2040 char ifname
[IF_NAMESIZE
];
2044 union g_addr nexthop
;
2045 uint32_t local_label
;
2046 uint32_t remote_label
;
2048 union pw_protocol_fields data
;
2050 struct zebra_pw
*pw
;
2052 /* Get input stream. */
2056 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2057 STREAM_GETL(s
, ifindex
);
2058 STREAM_GETL(s
, type
);
2062 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2065 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2070 STREAM_GETL(s
, local_label
);
2071 STREAM_GETL(s
, remote_label
);
2072 STREAM_GETC(s
, flags
);
2073 STREAM_GET(&data
, s
, sizeof(data
));
2074 protocol
= client
->proto
;
2076 pw
= zebra_pw_find(zvrf
, ifname
);
2077 switch (hdr
->command
) {
2080 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2081 "%s: pseudowire %s already exists [%s]",
2083 zserv_command_string(hdr
->command
));
2087 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2089 case ZEBRA_PW_DELETE
:
2091 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2092 "%s: pseudowire %s not found [%s]", __func__
,
2093 ifname
, zserv_command_string(hdr
->command
));
2097 zebra_pw_del(zvrf
, pw
);
2100 case ZEBRA_PW_UNSET
:
2102 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2103 "%s: pseudowire %s not found [%s]", __func__
,
2104 ifname
, zserv_command_string(hdr
->command
));
2108 switch (hdr
->command
) {
2112 case ZEBRA_PW_UNSET
:
2117 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2118 remote_label
, flags
, &data
);
2126 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2128 struct interface
*master
;
2129 struct interface
*slave
;
2130 struct stream
*s
= msg
;
2134 STREAM_GETL(s
, vrf_id
);
2135 STREAM_GETL(s
, ifindex
);
2136 master
= if_lookup_by_index(ifindex
, vrf_id
);
2138 STREAM_GETL(s
, vrf_id
);
2139 STREAM_GETL(s
, ifindex
);
2140 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2142 if (!master
|| !slave
)
2145 kernel_interface_set_master(master
, slave
);
2152 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2154 struct interface
*ifp
;
2155 mpls_label_t nlabel
;
2158 struct zebra_vrf
*def_zvrf
;
2159 enum lsp_types_t ltype
;
2162 STREAM_GETL(s
, nlabel
);
2163 STREAM_GETC(s
, afi
);
2164 if (nlabel
== zvrf
->label
[afi
]) {
2166 * Nothing to do here move along
2171 STREAM_GETC(s
, ltype
);
2173 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2174 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2176 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2179 zlog_debug("Unable to find specified Interface for %s",
2184 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2186 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2190 really_remove
= true;
2191 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2192 if (scrubber
== afi
)
2195 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2198 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2199 really_remove
= false;
2205 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2206 NEXTHOP_TYPE_IFINDEX
, NULL
,
2210 if (nlabel
!= MPLS_LABEL_NONE
)
2211 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2212 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2213 NULL
, ifp
->ifindex
);
2215 zvrf
->label
[afi
] = nlabel
;
2220 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2222 struct zebra_pbr_rule zpr
;
2228 STREAM_GETL(s
, total
);
2230 for (i
= 0; i
< total
; i
++) {
2231 memset(&zpr
, 0, sizeof(zpr
));
2233 zpr
.sock
= client
->sock
;
2234 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2235 STREAM_GETL(s
, zpr
.rule
.seq
);
2236 STREAM_GETL(s
, zpr
.rule
.priority
);
2237 STREAM_GETL(s
, zpr
.rule
.unique
);
2238 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2239 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2240 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2241 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2242 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2243 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2244 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2245 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2246 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2247 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2248 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2249 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2250 STREAM_GETL(s
, ifindex
);
2253 zpr
.ifp
= if_lookup_by_index_per_ns(
2257 zlog_debug("Failed to lookup ifindex: %u",
2263 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2264 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2266 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2267 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2269 if (zpr
.rule
.filter
.src_port
)
2270 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2272 if (zpr
.rule
.filter
.dst_port
)
2273 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2275 if (zpr
.rule
.filter
.fwmark
)
2276 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2278 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2279 if (hdr
->command
== ZEBRA_RULE_ADD
)
2280 zebra_pbr_add_rule(&zpr
);
2282 zebra_pbr_del_rule(&zpr
);
2289 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2291 struct zebra_pbr_ipset zpi
;
2296 STREAM_GETL(s
, total
);
2298 for (i
= 0; i
< total
; i
++) {
2299 memset(&zpi
, 0, sizeof(zpi
));
2301 zpi
.sock
= client
->sock
;
2302 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2303 STREAM_GETL(s
, zpi
.unique
);
2304 STREAM_GETL(s
, zpi
.type
);
2305 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2307 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2308 zebra_pbr_create_ipset(&zpi
);
2310 zebra_pbr_destroy_ipset(&zpi
);
2317 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2319 struct zebra_pbr_ipset_entry zpi
;
2320 struct zebra_pbr_ipset ipset
;
2325 STREAM_GETL(s
, total
);
2327 for (i
= 0; i
< total
; i
++) {
2328 memset(&zpi
, 0, sizeof(zpi
));
2329 memset(&ipset
, 0, sizeof(ipset
));
2331 zpi
.sock
= client
->sock
;
2332 STREAM_GETL(s
, zpi
.unique
);
2333 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2334 STREAM_GETC(s
, zpi
.src
.family
);
2335 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2336 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2337 STREAM_GETC(s
, zpi
.dst
.family
);
2338 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2339 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2341 STREAM_GETW(s
, zpi
.src_port_min
);
2342 STREAM_GETW(s
, zpi
.src_port_max
);
2343 STREAM_GETW(s
, zpi
.dst_port_min
);
2344 STREAM_GETW(s
, zpi
.dst_port_max
);
2345 STREAM_GETC(s
, zpi
.proto
);
2346 if (!is_default_prefix(&zpi
.src
))
2347 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2349 if (!is_default_prefix(&zpi
.dst
))
2350 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2351 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2352 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2353 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2354 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2355 if (zpi
.dst_port_max
!= 0)
2356 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2357 if (zpi
.src_port_max
!= 0)
2358 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2360 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2362 /* calculate backpointer */
2364 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2365 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2366 zebra_pbr_add_ipset_entry(&zpi
);
2368 zebra_pbr_del_ipset_entry(&zpi
);
2375 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2377 struct zebra_pbr_iptable zpi
;
2382 memset(&zpi
, 0, sizeof(zpi
));
2384 zpi
.interface_name_list
= list_new();
2385 zpi
.sock
= client
->sock
;
2386 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2387 STREAM_GETL(s
, zpi
.unique
);
2388 STREAM_GETL(s
, zpi
.type
);
2389 STREAM_GETL(s
, zpi
.filter_bm
);
2390 STREAM_GETL(s
, zpi
.action
);
2391 STREAM_GETL(s
, zpi
.fwmark
);
2392 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2393 STREAM_GETW(s
, zpi
.pkt_len_min
);
2394 STREAM_GETW(s
, zpi
.pkt_len_max
);
2395 STREAM_GETW(s
, zpi
.tcp_flags
);
2396 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2397 STREAM_GETC(s
, zpi
.dscp_value
);
2398 STREAM_GETC(s
, zpi
.fragment
);
2399 STREAM_GETL(s
, zpi
.nb_interface
);
2400 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2402 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2403 zebra_pbr_add_iptable(&zpi
);
2405 zebra_pbr_del_iptable(&zpi
);
2410 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2411 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2412 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2413 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2414 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2415 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2416 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2417 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2418 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2419 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2420 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2421 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2422 [ZEBRA_HELLO
] = zread_hello
,
2423 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2424 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2425 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2426 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2427 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2428 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2429 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2431 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2432 #endif /* HAVE_BFDD */
2433 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2434 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2435 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2436 #if defined(HAVE_RTADV)
2437 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2438 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2440 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2441 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2443 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2444 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2445 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2446 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2447 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2448 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2449 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2450 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2451 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2452 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2453 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
2454 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2455 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2456 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2457 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2458 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2459 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2460 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2461 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2462 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2463 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2464 [ZEBRA_PW_SET
] = zread_pseudowire
,
2465 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2466 [ZEBRA_RULE_ADD
] = zread_rule
,
2467 [ZEBRA_RULE_DELETE
] = zread_rule
,
2468 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2469 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2470 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2471 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2472 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2473 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2474 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2475 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2476 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2477 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2480 #if defined(HANDLE_ZAPI_FUZZING)
2481 extern struct zebra_privs_t zserv_privs
;
2483 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2485 char fname
[MAXPATHLEN
];
2486 struct stream
*copy
;
2489 copy
= stream_dup(orig
);
2490 stream_set_getp(copy
, 0);
2492 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2494 frr_elevate_privs(&zserv_privs
) {
2495 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2497 stream_flush(copy
, fd
);
2503 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2506 struct zebra_vrf
*zvrf
;
2508 zapi_parse_header(msg
, &hdr
);
2510 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2511 zserv_log_message(NULL
, msg
, &hdr
);
2513 #if defined(HANDLE_ZAPI_FUZZING)
2514 zserv_write_incoming(msg
, hdr
.command
);
2517 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2520 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2522 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2523 zlog_debug("ZAPI message specifies unknown VRF: %d",
2528 if (hdr
.command
>= array_size(zserv_handlers
)
2529 || zserv_handlers
[hdr
.command
] == NULL
)
2530 zlog_info("Zebra received unknown command %d", hdr
.command
);
2532 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);