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"
44 #include "lib/lib_errors.h"
46 #include "zebra/zebra_router.h"
47 #include "zebra/rib.h"
48 #include "zebra/zebra_memory.h"
49 #include "zebra/zebra_ns.h"
50 #include "zebra/zebra_vrf.h"
51 #include "zebra/router-id.h"
52 #include "zebra/redistribute.h"
53 #include "zebra/debug.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/rt_netlink.h"
56 #include "zebra/interface.h"
57 #include "zebra/zebra_ptm.h"
58 #include "zebra/rtadv.h"
59 #include "zebra/zebra_mpls.h"
60 #include "zebra/zebra_mroute.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"
68 #include "zebra/connected.h"
70 /* Encoding helpers -------------------------------------------------------- */
72 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
74 /* Interface information. */
75 struct zebra_if
*zif
= ifp
->info
;
77 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
78 stream_putl(s
, ifp
->ifindex
);
79 stream_putc(s
, ifp
->status
);
80 stream_putq(s
, ifp
->flags
);
81 stream_putc(s
, ifp
->ptm_enable
);
82 stream_putc(s
, ifp
->ptm_status
);
83 stream_putl(s
, ifp
->metric
);
84 stream_putl(s
, ifp
->speed
);
85 stream_putl(s
, ifp
->mtu
);
86 stream_putl(s
, ifp
->mtu6
);
87 stream_putl(s
, ifp
->bandwidth
);
88 stream_putl(s
, zif
->link_ifindex
);
89 stream_putl(s
, ifp
->ll_type
);
90 stream_putl(s
, ifp
->hw_addr_len
);
92 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
94 /* Then, Traffic Engineering parameters if any */
95 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
97 zebra_interface_link_params_write(s
, ifp
);
101 /* Write packet size. */
102 stream_putw_at(s
, 0, stream_get_endp(s
));
105 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
107 struct vrf_data data
;
108 const char *netns_name
= zvrf_ns_name(zvrf
);
110 data
.l
.table_id
= zvrf
->table_id
;
113 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
116 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
117 /* Pass the tableid and the netns NAME */
118 stream_put(s
, &data
, sizeof(struct vrf_data
));
119 /* Interface information. */
120 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
121 /* Write packet size. */
122 stream_putw_at(s
, 0, stream_get_endp(s
));
125 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
127 stream_putl(s
, nexthop
->vrf_id
);
128 stream_putc(s
, nexthop
->type
);
129 switch (nexthop
->type
) {
130 case NEXTHOP_TYPE_IPV4
:
131 case NEXTHOP_TYPE_IPV4_IFINDEX
:
132 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
133 stream_putl(s
, nexthop
->ifindex
);
135 case NEXTHOP_TYPE_IPV6
:
136 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
138 case NEXTHOP_TYPE_IPV6_IFINDEX
:
139 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
140 stream_putl(s
, nexthop
->ifindex
);
142 case NEXTHOP_TYPE_IFINDEX
:
143 stream_putl(s
, nexthop
->ifindex
);
153 * Zebra error addition adds error type.
157 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * | enum zebra_error_types |
160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
165 stream_put(s
, &error
, sizeof(error
));
167 /* Write packet size. */
168 stream_putw_at(s
, 0, stream_get_endp(s
));
171 /* Send handlers ----------------------------------------------------------- */
173 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
175 * This function is called in the following situations:
176 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
178 * - at startup, when zebra figures out the available interfaces
179 * - when an interface is added (where support for
180 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
181 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
184 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
186 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
188 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
189 zserv_encode_interface(s
, ifp
);
192 return zserv_send_message(client
, s
);
195 /* Interface deletion from zebra daemon. */
196 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
198 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
200 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
201 zserv_encode_interface(s
, ifp
);
204 return zserv_send_message(client
, s
);
207 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
209 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
211 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
212 zserv_encode_vrf(s
, zvrf
);
214 client
->vrfadd_cnt
++;
215 return zserv_send_message(client
, s
);
218 /* VRF deletion from zebra daemon. */
219 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
222 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
224 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
225 zserv_encode_vrf(s
, zvrf
);
227 client
->vrfdel_cnt
++;
228 return zserv_send_message(client
, s
);
231 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
233 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
235 if (!ifp
->link_params
) {
240 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
242 /* Add Interface Index */
243 stream_putl(s
, ifp
->ifindex
);
245 /* Then TE Link Parameters */
246 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
251 /* Write packet size. */
252 stream_putw_at(s
, 0, stream_get_endp(s
));
254 return zserv_send_message(client
, s
);
257 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
258 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
260 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
261 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
262 * from the client, after the ZEBRA_INTERFACE_ADD has been
263 * sent from zebra to the client
264 * - redistribute new address info to all clients in the following situations
265 * - at startup, when zebra figures out the available interfaces
266 * - when an interface is added (where support for
267 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
268 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
270 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
271 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
272 * - when an RTM_NEWADDR message is received from the kernel,
274 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
276 * zsend_interface_address(DELETE)
279 * zebra_interface_address_delete_update
281 * | | if_delete_update
283 * ip_address_uninstall connected_delete_ipv4
284 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
287 * | RTM_NEWADDR on routing/netlink socket
290 * "no ip address A.B.C.D/M [label LINE]"
291 * "no ip address A.B.C.D/M"
292 * ["no ipv6 address X:X::X:X/M"]
295 int zsend_interface_address(int cmd
, struct zserv
*client
,
296 struct interface
*ifp
, struct connected
*ifc
)
300 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
302 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
303 stream_putl(s
, ifp
->ifindex
);
305 /* Interface address flag. */
306 stream_putc(s
, ifc
->flags
);
308 /* Prefix information. */
310 stream_putc(s
, p
->family
);
311 blen
= prefix_blen(p
);
312 stream_put(s
, &p
->u
.prefix
, blen
);
315 * XXX gnu version does not send prefixlen for
316 * ZEBRA_INTERFACE_ADDRESS_DELETE
317 * but zebra_interface_address_delete_read() in the gnu version
320 stream_putc(s
, p
->prefixlen
);
323 p
= ifc
->destination
;
325 stream_put(s
, &p
->u
.prefix
, blen
);
327 stream_put(s
, NULL
, blen
);
329 /* Write packet size. */
330 stream_putw_at(s
, 0, stream_get_endp(s
));
332 client
->connected_rt_add_cnt
++;
333 return zserv_send_message(client
, s
);
336 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
337 struct interface
*ifp
,
338 struct nbr_connected
*ifc
)
341 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
344 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
345 stream_putl(s
, ifp
->ifindex
);
347 /* Prefix information. */
349 stream_putc(s
, p
->family
);
350 blen
= prefix_blen(p
);
351 stream_put(s
, &p
->u
.prefix
, blen
);
354 * XXX gnu version does not send prefixlen for
355 * ZEBRA_INTERFACE_ADDRESS_DELETE
356 * but zebra_interface_address_delete_read() in the gnu version
359 stream_putc(s
, p
->prefixlen
);
361 /* Write packet size. */
362 stream_putw_at(s
, 0, stream_get_endp(s
));
364 return zserv_send_message(client
, s
);
367 /* Interface address addition. */
368 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
369 struct nbr_connected
*ifc
)
371 struct listnode
*node
, *nnode
;
372 struct zserv
*client
;
375 if (IS_ZEBRA_DEBUG_EVENT
) {
376 char buf
[INET6_ADDRSTRLEN
];
380 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
381 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
383 p
->prefixlen
, ifc
->ifp
->name
);
386 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
387 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
391 /* Interface address deletion. */
392 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
393 struct nbr_connected
*ifc
)
395 struct listnode
*node
, *nnode
;
396 struct zserv
*client
;
399 if (IS_ZEBRA_DEBUG_EVENT
) {
400 char buf
[INET6_ADDRSTRLEN
];
404 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
405 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
407 p
->prefixlen
, ifc
->ifp
->name
);
410 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
411 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
415 /* Send addresses on interface to client */
416 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
418 struct listnode
*cnode
, *cnnode
;
420 struct nbr_connected
*nc
;
422 /* Send interface addresses. */
423 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
424 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
427 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
433 /* Send interface neighbors. */
434 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
435 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
444 /* Notify client about interface moving from one VRF to another.
445 * Whether client is interested in old and new VRF is checked by caller.
447 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
450 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
452 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
454 /* Fill in the name of the interface and its new VRF (id) */
455 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
456 stream_putl(s
, vrf_id
);
458 /* Write packet size. */
459 stream_putw_at(s
, 0, stream_get_endp(s
));
461 client
->if_vrfchg_cnt
++;
462 return zserv_send_message(client
, s
);
465 /* Add new nbr connected IPv6 address */
466 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
468 struct nbr_connected
*ifc
;
472 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
473 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
475 ifc
= listnode_head(ifp
->nbr_connected
);
478 ifc
= nbr_connected_new();
479 ifc
->address
= prefix_new();
481 listnode_add(ifp
->nbr_connected
, ifc
);
484 prefix_copy(ifc
->address
, &p
);
486 zebra_interface_nbr_address_add_update(ifp
, ifc
);
488 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
491 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
493 struct nbr_connected
*ifc
;
497 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
498 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
500 ifc
= nbr_connected_check(ifp
, &p
);
504 listnode_delete(ifp
->nbr_connected
, ifc
);
506 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
508 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
510 nbr_connected_free(ifc
);
514 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
515 * ZEBRA_INTERFACE_DOWN.
517 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
518 * the clients in one of 2 situations:
519 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
520 * - a vty command modifying the bandwidth of an interface is received.
521 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
523 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
525 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
527 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
528 zserv_encode_interface(s
, ifp
);
530 if (cmd
== ZEBRA_INTERFACE_UP
)
533 client
->ifdown_cnt
++;
535 return zserv_send_message(client
, s
);
538 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
539 const struct prefix
*p
,
540 const struct prefix
*src_p
,
541 const struct route_entry
*re
)
543 struct zapi_route api
;
544 struct zapi_nexthop
*api_nh
;
545 struct nexthop
*nexthop
;
549 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
551 memset(&api
, 0, sizeof(api
));
552 api
.vrf_id
= re
->vrf_id
;
554 api
.safi
= SAFI_UNICAST
;
555 api
.instance
= re
->instance
;
556 api
.flags
= re
->flags
;
558 afi
= family2afi(p
->family
);
561 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
562 client
->redist_v4_add_cnt
++;
564 client
->redist_v4_del_cnt
++;
567 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
568 client
->redist_v6_add_cnt
++;
570 client
->redist_v6_del_cnt
++;
579 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
580 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
583 for (nexthop
= re
->nhe
->nhg
->nexthop
;
584 nexthop
; nexthop
= nexthop
->next
) {
585 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
588 api_nh
= &api
.nexthops
[count
];
589 api_nh
->vrf_id
= nexthop
->vrf_id
;
590 api_nh
->type
= nexthop
->type
;
591 api_nh
->weight
= nexthop
->weight
;
592 switch (nexthop
->type
) {
593 case NEXTHOP_TYPE_BLACKHOLE
:
594 api_nh
->bh_type
= nexthop
->bh_type
;
596 case NEXTHOP_TYPE_IPV4
:
597 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
599 case NEXTHOP_TYPE_IPV4_IFINDEX
:
600 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
601 api_nh
->ifindex
= nexthop
->ifindex
;
603 case NEXTHOP_TYPE_IFINDEX
:
604 api_nh
->ifindex
= nexthop
->ifindex
;
606 case NEXTHOP_TYPE_IPV6
:
607 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
609 case NEXTHOP_TYPE_IPV6_IFINDEX
:
610 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
611 api_nh
->ifindex
= nexthop
->ifindex
;
618 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
619 api
.nexthop_num
= count
;
623 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
624 api
.distance
= re
->distance
;
625 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
626 api
.metric
= re
->metric
;
628 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
631 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
634 struct stream
*s
= stream_new(stream_size
);
636 /* Encode route and send. */
637 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
642 if (IS_ZEBRA_DEBUG_SEND
) {
643 char buf_prefix
[PREFIX_STRLEN
];
645 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
647 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
648 __func__
, zserv_command_string(cmd
),
649 zebra_route_string(client
->proto
),
650 zebra_route_string(api
.type
), api
.vrf_id
,
653 return zserv_send_message(client
, s
);
657 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
658 * nexthop is not found on mrib. Returns both route metric and protocol
661 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
663 struct route_entry
*re
,
664 struct zebra_vrf
*zvrf
)
669 struct nexthop
*nexthop
;
671 /* Get output stream. */
672 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
675 /* Fill in result. */
676 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
677 stream_put_in_addr(s
, &addr
);
680 stream_putc(s
, re
->distance
);
681 stream_putl(s
, re
->metric
);
683 /* remember position for nexthop_num */
684 nump
= stream_get_endp(s
);
685 /* reserve room for nexthop_num */
688 * Only non-recursive routes are elegible to resolve the
689 * nexthop we are looking up. Therefore, we will just iterate
690 * over the top chain of nexthops.
692 for (nexthop
= re
->nhe
->nhg
->nexthop
; nexthop
;
693 nexthop
= nexthop
->next
)
694 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
695 num
+= zserv_encode_nexthop(s
, nexthop
);
697 /* store nexthop_num */
698 stream_putc_at(s
, nump
, num
);
700 stream_putc(s
, 0); /* distance */
701 stream_putl(s
, 0); /* metric */
702 stream_putc(s
, 0); /* nexthop_num */
705 stream_putw_at(s
, 0, stream_get_endp(s
));
707 return zserv_send_message(client
, s
);
711 * Common utility send route notification, called from a path using a
712 * route_entry and from a path using a dataplane context.
714 static int route_notify_internal(const struct prefix
*p
, int type
,
715 uint16_t instance
, vrf_id_t vrf_id
,
717 enum zapi_route_notify_owner note
)
719 struct zserv
*client
;
723 client
= zserv_find_client(type
, instance
);
724 if (!client
|| !client
->notify_owner
) {
725 if (IS_ZEBRA_DEBUG_PACKET
) {
726 char buff
[PREFIX_STRLEN
];
729 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
730 type
, prefix2str(p
, buff
, sizeof(buff
)),
731 table_id
, note
, vrf_id
);
736 if (IS_ZEBRA_DEBUG_PACKET
) {
737 char buff
[PREFIX_STRLEN
];
739 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
740 type
, prefix2str(p
, buff
, sizeof(buff
)),
741 table_id
, note
, vrf_id
);
744 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
747 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
749 stream_put(s
, ¬e
, sizeof(note
));
751 stream_putc(s
, p
->family
);
753 blen
= prefix_blen(p
);
754 stream_putc(s
, p
->prefixlen
);
755 stream_put(s
, &p
->u
.prefix
, blen
);
757 stream_putl(s
, table_id
);
759 stream_putw_at(s
, 0, stream_get_endp(s
));
761 return zserv_send_message(client
, s
);
764 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
765 enum zapi_route_notify_owner note
)
767 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
772 * Route-owner notification using info from dataplane update context.
774 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
775 enum zapi_route_notify_owner note
)
777 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
778 dplane_ctx_get_type(ctx
),
779 dplane_ctx_get_instance(ctx
),
780 dplane_ctx_get_vrf(ctx
),
781 dplane_ctx_get_table(ctx
),
785 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
786 enum zapi_rule_notify_owner note
)
788 struct listnode
*node
;
789 struct zserv
*client
;
792 if (IS_ZEBRA_DEBUG_PACKET
)
793 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
796 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
797 if (rule
->sock
== client
->sock
)
804 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
806 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
807 stream_put(s
, ¬e
, sizeof(note
));
808 stream_putl(s
, rule
->rule
.seq
);
809 stream_putl(s
, rule
->rule
.priority
);
810 stream_putl(s
, rule
->rule
.unique
);
811 stream_putl(s
, rule
->rule
.ifindex
);
813 stream_putw_at(s
, 0, stream_get_endp(s
));
815 zserv_send_message(client
, s
);
818 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
819 enum zapi_ipset_notify_owner note
)
821 struct listnode
*node
;
822 struct zserv
*client
;
825 if (IS_ZEBRA_DEBUG_PACKET
)
826 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
829 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
830 if (ipset
->sock
== client
->sock
)
837 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
839 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
840 stream_put(s
, ¬e
, sizeof(note
));
841 stream_putl(s
, ipset
->unique
);
842 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
843 stream_putw_at(s
, 0, stream_get_endp(s
));
845 zserv_send_message(client
, s
);
848 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
849 enum zapi_ipset_entry_notify_owner note
)
851 struct listnode
*node
;
852 struct zserv
*client
;
855 if (IS_ZEBRA_DEBUG_PACKET
)
856 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
859 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
860 if (ipset
->sock
== client
->sock
)
867 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
869 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
870 stream_put(s
, ¬e
, sizeof(note
));
871 stream_putl(s
, ipset
->unique
);
872 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
873 stream_putw_at(s
, 0, stream_get_endp(s
));
875 zserv_send_message(client
, s
);
878 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
879 enum zapi_iptable_notify_owner note
)
881 struct listnode
*node
;
882 struct zserv
*client
;
885 if (IS_ZEBRA_DEBUG_PACKET
)
886 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
889 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
890 if (iptable
->sock
== client
->sock
)
897 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
899 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
900 stream_put(s
, ¬e
, sizeof(note
));
901 stream_putl(s
, iptable
->unique
);
902 stream_putw_at(s
, 0, stream_get_endp(s
));
904 zserv_send_message(client
, s
);
907 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
908 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
913 /* Check this client need interface information. */
914 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
917 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
920 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
922 /* Prefix information. */
923 stream_putc(s
, p
->family
);
924 blen
= prefix_blen(p
);
925 stream_put(s
, &p
->u
.prefix
, blen
);
926 stream_putc(s
, p
->prefixlen
);
928 /* Write packet size. */
929 stream_putw_at(s
, 0, stream_get_endp(s
));
931 return zserv_send_message(client
, s
);
935 * Function used by Zebra to send a PW status update to LDP daemon
937 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
939 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
941 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
942 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
943 stream_putl(s
, pw
->ifindex
);
944 stream_putl(s
, pw
->status
);
946 /* Put length at the first point of the stream. */
947 stream_putw_at(s
, 0, stream_get_endp(s
));
949 return zserv_send_message(client
, s
);
952 /* Send response to a get label chunk request to client */
953 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
954 uint8_t proto
, uint16_t instance
,
955 struct label_manager_chunk
*lmc
)
958 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
960 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
962 stream_putc(s
, proto
);
964 stream_putw(s
, instance
);
968 stream_putc(s
, lmc
->keep
);
969 /* start and end labels */
970 stream_putl(s
, lmc
->start
);
971 stream_putl(s
, lmc
->end
);
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
));
982 /* Send response to a label manager connect request to client */
983 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
984 unsigned short result
)
987 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
989 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
992 stream_putc(s
, client
->proto
);
995 stream_putw(s
, client
->instance
);
998 stream_putc(s
, result
);
1000 /* Write packet size. */
1001 stream_putw_at(s
, 0, stream_get_endp(s
));
1003 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1009 /* Send response to a get table chunk request to client */
1010 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1012 struct table_manager_chunk
*tmc
)
1014 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1016 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1019 /* start and end labels */
1020 stream_putl(s
, tmc
->start
);
1021 stream_putl(s
, tmc
->end
);
1024 /* Write packet size. */
1025 stream_putw_at(s
, 0, stream_get_endp(s
));
1027 return zserv_send_message(client
, s
);
1030 static int zsend_table_manager_connect_response(struct zserv
*client
,
1034 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1036 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1039 stream_putc(s
, result
);
1041 stream_putw_at(s
, 0, stream_get_endp(s
));
1043 return zserv_send_message(client
, s
);
1046 /* Inbound message handling ------------------------------------------------ */
1048 const int cmd2type
[] = {
1049 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1050 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1051 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1052 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1055 /* Nexthop register */
1056 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1061 unsigned short l
= 0;
1063 uint16_t type
= cmd2type
[hdr
->command
];
1065 bool flag_changed
= false;
1068 if (IS_ZEBRA_DEBUG_NHT
)
1070 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1071 zebra_route_string(client
->proto
), hdr
->length
,
1072 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1077 client
->nh_reg_time
= monotime(NULL
);
1079 while (l
< hdr
->length
) {
1080 STREAM_GETC(s
, flags
);
1081 STREAM_GETW(s
, p
.family
);
1082 STREAM_GETC(s
, p
.prefixlen
);
1084 if (p
.family
== AF_INET
) {
1085 client
->v4_nh_watch_add_cnt
++;
1086 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1088 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1089 __PRETTY_FUNCTION__
, p
.prefixlen
);
1092 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1093 l
+= IPV4_MAX_BYTELEN
;
1094 } else if (p
.family
== AF_INET6
) {
1095 client
->v6_nh_watch_add_cnt
++;
1096 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1098 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1099 __PRETTY_FUNCTION__
, p
.prefixlen
);
1102 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1103 l
+= IPV6_MAX_BYTELEN
;
1106 EC_ZEBRA_UNKNOWN_FAMILY
,
1107 "rnh_register: Received unknown family type %d\n",
1111 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1115 orig_flags
= rnh
->flags
;
1116 if (type
== RNH_NEXTHOP_TYPE
) {
1118 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1119 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1121 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1122 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1123 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1125 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1126 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1128 && CHECK_FLAG(rnh
->flags
,
1129 ZEBRA_NHT_EXACT_MATCH
))
1130 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1133 if (orig_flags
!= rnh
->flags
)
1134 flag_changed
= true;
1136 /* Anything not AF_INET/INET6 has been filtered out above */
1137 if (!exist
|| flag_changed
)
1138 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1141 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1148 /* Nexthop register */
1149 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1154 unsigned short l
= 0;
1155 uint16_t type
= cmd2type
[hdr
->command
];
1157 if (IS_ZEBRA_DEBUG_NHT
)
1159 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1160 zebra_route_string(client
->proto
), hdr
->length
,
1165 while (l
< hdr
->length
) {
1168 STREAM_GETC(s
, flags
);
1170 goto stream_failure
;
1172 STREAM_GETW(s
, p
.family
);
1173 STREAM_GETC(s
, p
.prefixlen
);
1175 if (p
.family
== AF_INET
) {
1176 client
->v4_nh_watch_rem_cnt
++;
1177 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1179 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1180 __PRETTY_FUNCTION__
, p
.prefixlen
);
1183 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1184 l
+= IPV4_MAX_BYTELEN
;
1185 } else if (p
.family
== AF_INET6
) {
1186 client
->v6_nh_watch_rem_cnt
++;
1187 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1189 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1190 __PRETTY_FUNCTION__
, p
.prefixlen
);
1193 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1194 l
+= IPV6_MAX_BYTELEN
;
1197 EC_ZEBRA_UNKNOWN_FAMILY
,
1198 "rnh_register: Received unknown family type %d\n",
1202 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1204 client
->nh_dereg_time
= monotime(NULL
);
1205 zebra_remove_rnh_client(rnh
, client
, type
);
1212 #define ZEBRA_MIN_FEC_LENGTH 5
1215 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1218 unsigned short l
= 0;
1221 uint32_t label
= MPLS_INVALID_LABEL
;
1222 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1225 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1230 * The minimum amount of data that can be sent for one fec
1233 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1235 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1236 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1241 while (l
< hdr
->length
) {
1242 STREAM_GETW(s
, flags
);
1243 memset(&p
, 0, sizeof(p
));
1244 STREAM_GETW(s
, p
.family
);
1245 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1247 EC_ZEBRA_UNKNOWN_FAMILY
,
1248 "fec_register: Received unknown family type %d\n",
1252 STREAM_GETC(s
, p
.prefixlen
);
1253 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1254 || (p
.family
== AF_INET6
1255 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1257 "%s: Specified prefix hdr->length: %d is to long for %d",
1258 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1262 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1263 l
+= PSIZE(p
.prefixlen
);
1264 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1265 STREAM_GETL(s
, label
);
1267 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1268 STREAM_GETL(s
, label_index
);
1272 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1279 /* FEC unregister */
1280 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1283 unsigned short l
= 0;
1288 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1293 * The minimum amount of data that can be sent for one
1294 * fec unregistration
1296 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1298 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1299 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1304 while (l
< hdr
->length
) {
1305 STREAM_GETW(s
, flags
);
1307 goto stream_failure
;
1309 memset(&p
, 0, sizeof(p
));
1310 STREAM_GETW(s
, p
.family
);
1311 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1313 EC_ZEBRA_UNKNOWN_FAMILY
,
1314 "fec_unregister: Received unknown family type %d\n",
1318 STREAM_GETC(s
, p
.prefixlen
);
1319 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1320 || (p
.family
== AF_INET6
1321 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1323 "%s: Received prefix hdr->length %d which is greater than %d can support",
1324 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1328 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1329 l
+= PSIZE(p
.prefixlen
);
1330 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1339 * Register zebra server interface information.
1340 * Send current all interface and address information.
1342 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1345 struct interface
*ifp
;
1347 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1348 FOR_ALL_INTERFACES (vrf
, ifp
) {
1349 /* Skip pseudo interface. */
1350 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1353 zsend_interface_add(client
, ifp
);
1354 zsend_interface_link_params(client
, ifp
);
1355 zsend_interface_addresses(client
, ifp
);
1360 /* Unregister zebra server interface information. */
1361 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1366 * Handle message requesting interface be set up or down.
1368 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1371 struct interface
*ifp
;
1374 STREAM_GETL(msg
, ifindex
);
1375 STREAM_GETC(msg
, down
);
1378 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1381 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1382 ifindex
, down
? "on" : "off");
1383 zebra_if_set_protodown(ifp
, down
);
1386 "Cannot set protodown %s for interface %u; does not exist",
1387 down
? "on" : "off", ifindex
);
1396 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1397 const unsigned int nexthop_num
)
1399 if (nexthop_num
> zrouter
.multipath_num
) {
1400 char buff
[PREFIX2STR_BUFFER
];
1402 prefix2str(p
, buff
, sizeof(buff
));
1404 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1405 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1406 caller
, buff
, nexthop_num
, zrouter
.multipath_num
);
1410 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1413 struct zapi_route api
;
1414 struct zapi_nexthop
*api_nh
;
1416 struct prefix_ipv6
*src_p
= NULL
;
1417 struct route_entry
*re
;
1418 struct nexthop
*nexthop
= NULL
;
1419 struct nexthop_group
*ng
= NULL
;
1422 struct ipaddr vtep_ip
;
1423 struct interface
*ifp
;
1426 if (zapi_route_decode(s
, &api
) < 0) {
1427 if (IS_ZEBRA_DEBUG_RECV
)
1428 zlog_debug("%s: Unable to decode zapi_route sent",
1429 __PRETTY_FUNCTION__
);
1433 if (IS_ZEBRA_DEBUG_RECV
) {
1434 char buf_prefix
[PREFIX_STRLEN
];
1436 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1437 zlog_debug("%s: p=%s, flags=0x%x",
1438 __func__
, buf_prefix
, api
.flags
);
1441 /* Allocate new route. */
1442 vrf_id
= zvrf_id(zvrf
);
1443 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1444 re
->type
= api
.type
;
1445 re
->instance
= api
.instance
;
1446 re
->flags
= api
.flags
;
1447 re
->uptime
= monotime(NULL
);
1448 re
->vrf_id
= vrf_id
;
1451 re
->table
= api
.tableid
;
1453 re
->table
= zvrf
->table_id
;
1455 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1456 || api
.nexthop_num
== 0) {
1457 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1458 "%s: received a route without nexthops for prefix %pFX from client %s",
1459 __func__
, &api
.prefix
,
1460 zebra_route_string(client
->proto
));
1462 XFREE(MTYPE_RE
, re
);
1466 /* Use temporary list of nexthops */
1467 ng
= nexthop_group_new();
1470 * TBD should _all_ of the nexthop add operations use
1471 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1472 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1474 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1475 api_nh
= &api
.nexthops
[i
];
1476 ifindex_t ifindex
= 0;
1478 if (IS_ZEBRA_DEBUG_RECV
)
1479 zlog_debug("nh type %d", api_nh
->type
);
1481 switch (api_nh
->type
) {
1482 case NEXTHOP_TYPE_IFINDEX
:
1483 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
,
1486 case NEXTHOP_TYPE_IPV4
:
1487 if (IS_ZEBRA_DEBUG_RECV
) {
1488 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1490 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1492 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1493 nhbuf
, api_nh
->vrf_id
);
1495 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
,
1496 NULL
, api_nh
->vrf_id
);
1498 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1500 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1501 ifindex
= api_nh
->ifindex
;
1502 if (IS_ZEBRA_DEBUG_RECV
) {
1503 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1505 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1508 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1509 __func__
, nhbuf
, api_nh
->vrf_id
,
1510 re
->vrf_id
, ifindex
);
1512 nexthop
= nexthop_from_ipv4_ifindex(
1513 &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1516 ifp
= if_lookup_by_index(ifindex
, api_nh
->vrf_id
);
1517 if (ifp
&& connected_is_unnumbered(ifp
))
1518 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1519 /* Special handling for IPv4 routes sourced from EVPN:
1520 * the nexthop and associated MAC need to be installed.
1522 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1523 vtep_ip
.ipa_type
= IPADDR_V4
;
1524 memcpy(&(vtep_ip
.ipaddr_v4
),
1525 &(api_nh
->gate
.ipv4
),
1526 sizeof(struct in_addr
));
1527 zebra_vxlan_evpn_vrf_route_add(
1528 api_nh
->vrf_id
, &api_nh
->rmac
,
1529 &vtep_ip
, &api
.prefix
);
1532 case NEXTHOP_TYPE_IPV6
:
1533 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
,
1536 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1537 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1538 ifindex
= api_nh
->ifindex
;
1539 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1543 /* Special handling for IPv6 routes sourced from EVPN:
1544 * the nexthop and associated MAC need to be installed.
1546 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1547 vtep_ip
.ipa_type
= IPADDR_V6
;
1548 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1549 sizeof(struct in6_addr
));
1550 zebra_vxlan_evpn_vrf_route_add(
1551 api_nh
->vrf_id
, &api_nh
->rmac
,
1552 &vtep_ip
, &api
.prefix
);
1555 case NEXTHOP_TYPE_BLACKHOLE
:
1556 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1562 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1563 "%s: Nexthops Specified: %d but we failed to properly create one",
1564 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1565 nexthop_group_delete(&ng
);
1566 XFREE(MTYPE_RE
, re
);
1570 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1571 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1573 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1574 nexthop
->weight
= api_nh
->weight
;
1576 /* MPLS labels for BGP-LU or Segment Routing */
1577 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1578 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1579 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1580 enum lsp_types_t label_type
;
1582 label_type
= lsp_type_from_re_type(client
->proto
);
1584 if (IS_ZEBRA_DEBUG_RECV
) {
1586 "%s: adding %d labels of type %d (1st=%u)",
1587 __func__
, api_nh
->label_num
, label_type
,
1591 nexthop_add_labels(nexthop
, label_type
,
1593 &api_nh
->labels
[0]);
1596 /* Add new nexthop to temporary list */
1597 nexthop_group_add_sorted(ng
, nexthop
);
1600 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1601 re
->distance
= api
.distance
;
1602 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1603 re
->metric
= api
.metric
;
1604 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1606 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1609 afi
= family2afi(api
.prefix
.family
);
1610 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1611 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1612 "%s: Received SRC Prefix but afi is not v6",
1613 __PRETTY_FUNCTION__
);
1614 nexthop_group_delete(&ng
);
1615 XFREE(MTYPE_RE
, re
);
1618 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1619 src_p
= &api
.src_prefix
;
1621 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
1622 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
1623 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1624 __func__
, api
.safi
);
1625 nexthop_group_delete(&ng
);
1626 XFREE(MTYPE_RE
, re
);
1629 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
, ng
);
1632 switch (api
.prefix
.family
) {
1635 client
->v4_route_add_cnt
++;
1637 client
->v4_route_upd8_cnt
++;
1641 client
->v6_route_add_cnt
++;
1643 client
->v6_route_upd8_cnt
++;
1648 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1651 struct zapi_route api
;
1653 struct prefix_ipv6
*src_p
= NULL
;
1657 if (zapi_route_decode(s
, &api
) < 0)
1660 afi
= family2afi(api
.prefix
.family
);
1661 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1662 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1663 "%s: Received a src prefix while afi is not v6",
1664 __PRETTY_FUNCTION__
);
1667 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1668 src_p
= &api
.src_prefix
;
1671 table_id
= api
.tableid
;
1673 table_id
= zvrf
->table_id
;
1675 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1676 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
1677 api
.distance
, false);
1680 switch (api
.prefix
.family
) {
1682 client
->v4_route_del_cnt
++;
1685 client
->v6_route_del_cnt
++;
1690 /* MRIB Nexthop lookup for IPv4. */
1691 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1693 struct in_addr addr
;
1694 struct route_entry
*re
;
1696 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1697 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1698 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1704 /* Register zebra server router-id information. Send current router-id */
1705 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1709 /* Router-id information is needed. */
1710 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1712 router_id_get(&p
, zvrf_id(zvrf
));
1714 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1717 /* Unregister zebra server router-id information. */
1718 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1720 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1723 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1725 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1727 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1728 stream_putl(s
, vrf_get_backend());
1729 stream_putc(s
, mpls_enabled
);
1730 stream_putl(s
, zrouter
.multipath_num
);
1731 stream_putc(s
, zebra_mlag_get_role());
1733 stream_putw_at(s
, 0, stream_get_endp(s
));
1734 zserv_send_message(client
, s
);
1737 void zsend_capabilities_all_clients(void)
1739 struct listnode
*node
, *nnode
;
1740 struct zebra_vrf
*zvrf
;
1741 struct zserv
*client
;
1743 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1744 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1745 zsend_capabilities(client
, zvrf
);
1749 /* Tie up route-type and client->sock */
1750 static void zread_hello(ZAPI_HANDLER_ARGS
)
1752 /* type of protocol (lib/zebra.h) */
1754 unsigned short instance
;
1757 STREAM_GETC(msg
, proto
);
1758 STREAM_GETW(msg
, instance
);
1759 STREAM_GETC(msg
, notify
);
1761 client
->notify_owner
= true;
1763 /* accept only dynamic routing protocols */
1764 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1766 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1767 client
->sock
, zebra_route_string(proto
),
1770 zlog_notice("client protocol instance %d", instance
);
1772 client
->proto
= proto
;
1773 client
->instance
= instance
;
1776 zsend_capabilities(client
, zvrf
);
1777 zebra_vrf_update_all(client
);
1782 /* Unregister all information in a VRF. */
1783 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1788 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1789 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1790 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1791 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
1793 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1797 * Handle request to create an MPLS LSP.
1799 * A single message can fully specify an LSP with multiple nexthops.
1801 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1802 * updated to use the received label(s).
1804 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
1807 struct zapi_labels zl
;
1809 /* Get input stream. */
1811 if (zapi_labels_decode(s
, &zl
) < 0) {
1812 if (IS_ZEBRA_DEBUG_RECV
)
1813 zlog_debug("%s: Unable to decode zapi_labels sent",
1814 __PRETTY_FUNCTION__
);
1821 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1822 struct zapi_nexthop_label
*znh
;
1824 znh
= &zl
.nexthops
[i
];
1825 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, 1, &znh
->label
,
1826 znh
->type
, &znh
->address
, znh
->ifindex
);
1828 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1829 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1830 znh
->type
, &znh
->address
, znh
->ifindex
,
1831 zl
.route
.type
, zl
.route
.instance
,
1837 * Handle request to delete an MPLS LSP.
1839 * An LSP is identified by its type and local label. When the received message
1840 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
1841 * listed LSP nexthops (aka NHLFEs) are deleted.
1843 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
1844 * FEC (route) nexthops are deleted.
1846 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
1849 struct zapi_labels zl
;
1851 /* Get input stream. */
1853 if (zapi_labels_decode(s
, &zl
) < 0) {
1854 if (IS_ZEBRA_DEBUG_RECV
)
1855 zlog_debug("%s: Unable to decode zapi_labels sent",
1856 __PRETTY_FUNCTION__
);
1863 if (zl
.nexthop_num
> 0) {
1864 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1865 struct zapi_nexthop_label
*znh
;
1867 znh
= &zl
.nexthops
[i
];
1868 mpls_lsp_uninstall(zvrf
, zl
.type
, zl
.local_label
,
1869 znh
->type
, &znh
->address
,
1872 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1873 mpls_ftn_update(0, zvrf
, zl
.type
,
1874 &zl
.route
.prefix
, znh
->type
,
1875 &znh
->address
, znh
->ifindex
,
1877 zl
.route
.instance
, znh
->label
);
1880 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1882 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1883 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1884 zl
.route
.type
, zl
.route
.instance
);
1889 * Handle request to add an MPLS LSP or change an existing one.
1891 * A single message can fully specify an LSP with multiple nexthops.
1893 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1894 * updated to use the received label(s).
1896 * NOTE: zebra will use route replace semantics (make-before-break) to update
1897 * the LSP in the forwarding plane if that's supported by the underlying
1900 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
1903 struct zapi_labels zl
;
1905 /* Get input stream. */
1907 if (zapi_labels_decode(s
, &zl
) < 0) {
1908 if (IS_ZEBRA_DEBUG_RECV
)
1909 zlog_debug("%s: Unable to decode zapi_labels sent",
1910 __PRETTY_FUNCTION__
);
1917 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1918 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1919 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1920 zl
.route
.type
, zl
.route
.instance
);
1922 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1923 struct zapi_nexthop_label
*znh
;
1925 znh
= &zl
.nexthops
[i
];
1926 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, 1, &znh
->label
,
1927 znh
->type
, &znh
->address
, znh
->ifindex
);
1929 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
)) {
1930 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1931 znh
->type
, &znh
->address
, znh
->ifindex
,
1932 zl
.route
.type
, zl
.route
.instance
,
1938 /* Send response to a table manager connect request to client */
1939 static void zread_table_manager_connect(struct zserv
*client
,
1940 struct stream
*msg
, vrf_id_t vrf_id
)
1949 STREAM_GETC(s
, proto
);
1950 STREAM_GETW(s
, instance
);
1952 /* accept only dynamic routing protocols */
1953 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1954 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1955 "client %d has wrong protocol %s", client
->sock
,
1956 zebra_route_string(proto
));
1957 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1960 zlog_notice("client %d with vrf %u instance %u connected as %s",
1961 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1962 client
->proto
= proto
;
1963 client
->instance
= instance
;
1966 * Release previous labels of same protocol and instance.
1967 * This is done in case it restarted from an unexpected shutdown.
1969 release_daemon_table_chunks(client
);
1971 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1977 static void zread_label_manager_connect(struct zserv
*client
,
1978 struct stream
*msg
, vrf_id_t vrf_id
)
1981 /* type of protocol (lib/zebra.h) */
1983 unsigned short instance
;
1985 /* Get input stream. */
1989 STREAM_GETC(s
, proto
);
1990 STREAM_GETW(s
, instance
);
1992 /* accept only dynamic routing protocols */
1993 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1994 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1995 "client %d has wrong protocol %s", client
->sock
,
1996 zebra_route_string(proto
));
1997 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2001 /* recall proto and instance in this socket */
2002 client
->proto
= proto
;
2003 client
->instance
= instance
;
2005 /* call hook for connection using wrapper */
2006 lm_client_connect_call(proto
, instance
, vrf_id
);
2012 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2017 uint32_t size
, base
;
2018 struct label_manager_chunk
*lmc
= NULL
;
2020 unsigned short instance
;
2022 /* Get input stream. */
2026 STREAM_GETC(s
, proto
);
2027 STREAM_GETW(s
, instance
);
2028 STREAM_GETC(s
, keep
);
2029 STREAM_GETL(s
, size
);
2030 STREAM_GETL(s
, base
);
2032 /* call hook to get a chunk using wrapper */
2033 lm_get_chunk_call(&lmc
, proto
, instance
, keep
, size
, base
, vrf_id
);
2037 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
2038 "Unable to assign Label Chunk of size %u to %s instance %u",
2039 size
, zebra_route_string(proto
), instance
);
2041 if (IS_ZEBRA_DEBUG_PACKET
)
2042 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
2043 lmc
->start
, lmc
->end
,
2044 zebra_route_string(proto
), instance
);
2050 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2053 uint32_t start
, end
;
2055 unsigned short instance
;
2057 /* Get input stream. */
2061 STREAM_GETC(s
, proto
);
2062 STREAM_GETW(s
, instance
);
2063 STREAM_GETL(s
, start
);
2064 STREAM_GETL(s
, end
);
2066 /* call hook to release a chunk using wrapper */
2067 lm_release_chunk_call(proto
, instance
, start
, end
);
2073 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2075 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2076 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2077 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2079 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2080 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2081 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2082 zread_release_label_chunk(client
, msg
);
2086 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2091 struct table_manager_chunk
*tmc
;
2093 /* Get input stream. */
2097 STREAM_GETL(s
, size
);
2099 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2101 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2102 "%s: Unable to assign Table Chunk of size %u",
2105 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2107 /* send response back */
2108 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2114 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2117 uint32_t start
, end
;
2119 /* Get input stream. */
2123 STREAM_GETL(s
, start
);
2124 STREAM_GETL(s
, end
);
2126 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2132 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2134 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2135 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2136 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2138 /* Sanity: don't allow 'unidentified' requests */
2139 if (!client
->proto
) {
2142 "Got table request from an unidentified client");
2145 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2146 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2147 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2148 zread_release_table_chunk(client
, msg
);
2152 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2155 char ifname
[IF_NAMESIZE
];
2159 union g_addr nexthop
;
2160 uint32_t local_label
;
2161 uint32_t remote_label
;
2163 union pw_protocol_fields data
;
2165 struct zebra_pw
*pw
;
2167 /* Get input stream. */
2171 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2172 ifname
[IF_NAMESIZE
- 1] = '\0';
2173 STREAM_GETL(s
, ifindex
);
2174 STREAM_GETL(s
, type
);
2178 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2181 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2186 STREAM_GETL(s
, local_label
);
2187 STREAM_GETL(s
, remote_label
);
2188 STREAM_GETC(s
, flags
);
2189 STREAM_GET(&data
, s
, sizeof(data
));
2190 protocol
= client
->proto
;
2192 pw
= zebra_pw_find(zvrf
, ifname
);
2193 switch (hdr
->command
) {
2196 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2197 "%s: pseudowire %s already exists [%s]",
2199 zserv_command_string(hdr
->command
));
2203 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2205 case ZEBRA_PW_DELETE
:
2207 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2208 "%s: pseudowire %s not found [%s]", __func__
,
2209 ifname
, zserv_command_string(hdr
->command
));
2213 zebra_pw_del(zvrf
, pw
);
2216 case ZEBRA_PW_UNSET
:
2218 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2219 "%s: pseudowire %s not found [%s]", __func__
,
2220 ifname
, zserv_command_string(hdr
->command
));
2224 switch (hdr
->command
) {
2228 case ZEBRA_PW_UNSET
:
2233 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2234 remote_label
, flags
, &data
);
2242 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2244 struct interface
*master
;
2245 struct interface
*slave
;
2246 struct stream
*s
= msg
;
2250 STREAM_GETL(s
, vrf_id
);
2251 STREAM_GETL(s
, ifindex
);
2252 master
= if_lookup_by_index(ifindex
, vrf_id
);
2254 STREAM_GETL(s
, vrf_id
);
2255 STREAM_GETL(s
, ifindex
);
2256 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2258 if (!master
|| !slave
)
2261 kernel_interface_set_master(master
, slave
);
2268 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2270 struct interface
*ifp
;
2271 mpls_label_t nlabel
;
2274 struct zebra_vrf
*def_zvrf
;
2275 enum lsp_types_t ltype
;
2278 STREAM_GETL(s
, nlabel
);
2279 STREAM_GETC(s
, afi
);
2281 if (!(IS_VALID_AFI(afi
))) {
2282 zlog_warn("Invalid AFI for VRF label: %u", afi
);
2286 if (nlabel
== zvrf
->label
[afi
]) {
2288 * Nothing to do here move along
2293 STREAM_GETC(s
, ltype
);
2295 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2296 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2298 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2301 zlog_debug("Unable to find specified Interface for %s",
2306 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2308 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2312 really_remove
= true;
2313 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2314 if (scrubber
== afi
)
2317 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2320 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2321 really_remove
= false;
2327 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2328 NEXTHOP_TYPE_IFINDEX
, NULL
,
2332 if (nlabel
!= MPLS_LABEL_NONE
) {
2333 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2334 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2335 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2338 zvrf
->label
[afi
] = nlabel
;
2343 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2345 struct zebra_pbr_rule zpr
;
2350 STREAM_GETL(s
, total
);
2352 for (i
= 0; i
< total
; i
++) {
2353 memset(&zpr
, 0, sizeof(zpr
));
2355 zpr
.sock
= client
->sock
;
2356 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2357 STREAM_GETL(s
, zpr
.rule
.seq
);
2358 STREAM_GETL(s
, zpr
.rule
.priority
);
2359 STREAM_GETL(s
, zpr
.rule
.unique
);
2360 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2361 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2362 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2363 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2364 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2365 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2366 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2367 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2368 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2369 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2370 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2371 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2372 STREAM_GETL(s
, zpr
.rule
.ifindex
);
2374 if (zpr
.rule
.ifindex
) {
2375 struct interface
*ifp
;
2377 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
,
2380 zlog_debug("Failed to lookup ifindex: %u",
2385 strlcpy(zpr
.ifname
, ifp
->name
, sizeof(zpr
.ifname
));
2388 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2389 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2391 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2392 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2394 if (zpr
.rule
.filter
.src_port
)
2395 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2397 if (zpr
.rule
.filter
.dst_port
)
2398 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2400 if (zpr
.rule
.filter
.fwmark
)
2401 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2403 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
2404 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
2406 "Unsupported PBR source IP family: %s (%" PRIu8
2408 family2str(zpr
.rule
.filter
.src_ip
.family
),
2409 zpr
.rule
.filter
.src_ip
.family
);
2412 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
2413 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
2414 zlog_warn("Unsupported PBR IP family: %s (%" PRIu8
2416 family2str(zpr
.rule
.filter
.dst_ip
.family
),
2417 zpr
.rule
.filter
.dst_ip
.family
);
2422 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2423 if (hdr
->command
== ZEBRA_RULE_ADD
)
2424 zebra_pbr_add_rule(&zpr
);
2426 zebra_pbr_del_rule(&zpr
);
2433 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2435 struct zebra_pbr_ipset zpi
;
2440 STREAM_GETL(s
, total
);
2442 for (i
= 0; i
< total
; i
++) {
2443 memset(&zpi
, 0, sizeof(zpi
));
2445 zpi
.sock
= client
->sock
;
2446 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2447 STREAM_GETL(s
, zpi
.unique
);
2448 STREAM_GETL(s
, zpi
.type
);
2449 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2451 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2452 zebra_pbr_create_ipset(&zpi
);
2454 zebra_pbr_destroy_ipset(&zpi
);
2461 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2463 struct zebra_pbr_ipset_entry zpi
;
2464 struct zebra_pbr_ipset ipset
;
2469 STREAM_GETL(s
, total
);
2471 for (i
= 0; i
< total
; i
++) {
2472 memset(&zpi
, 0, sizeof(zpi
));
2473 memset(&ipset
, 0, sizeof(ipset
));
2475 zpi
.sock
= client
->sock
;
2476 STREAM_GETL(s
, zpi
.unique
);
2477 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2478 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
2479 STREAM_GETC(s
, zpi
.src
.family
);
2480 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2481 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2482 STREAM_GETC(s
, zpi
.dst
.family
);
2483 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2484 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2486 STREAM_GETW(s
, zpi
.src_port_min
);
2487 STREAM_GETW(s
, zpi
.src_port_max
);
2488 STREAM_GETW(s
, zpi
.dst_port_min
);
2489 STREAM_GETW(s
, zpi
.dst_port_max
);
2490 STREAM_GETC(s
, zpi
.proto
);
2491 if (!is_default_prefix(&zpi
.src
))
2492 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2494 if (!is_default_prefix(&zpi
.dst
))
2495 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2496 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2497 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2498 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2499 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2500 if (zpi
.dst_port_max
!= 0)
2501 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2502 if (zpi
.src_port_max
!= 0)
2503 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2505 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2507 /* calculate backpointer */
2509 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2511 if (!zpi
.backpointer
) {
2512 zlog_warn("ipset name specified: %s does not exist",
2514 goto stream_failure
;
2517 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2518 zebra_pbr_add_ipset_entry(&zpi
);
2520 zebra_pbr_del_ipset_entry(&zpi
);
2527 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2529 struct zebra_pbr_iptable
*zpi
=
2530 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
2535 zpi
->interface_name_list
= list_new();
2536 zpi
->sock
= client
->sock
;
2537 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
2538 STREAM_GETL(s
, zpi
->unique
);
2539 STREAM_GETL(s
, zpi
->type
);
2540 STREAM_GETL(s
, zpi
->filter_bm
);
2541 STREAM_GETL(s
, zpi
->action
);
2542 STREAM_GETL(s
, zpi
->fwmark
);
2543 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2544 STREAM_GETW(s
, zpi
->pkt_len_min
);
2545 STREAM_GETW(s
, zpi
->pkt_len_max
);
2546 STREAM_GETW(s
, zpi
->tcp_flags
);
2547 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
2548 STREAM_GETC(s
, zpi
->dscp_value
);
2549 STREAM_GETC(s
, zpi
->fragment
);
2550 STREAM_GETC(s
, zpi
->protocol
);
2551 STREAM_GETL(s
, zpi
->nb_interface
);
2552 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
2554 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2555 zebra_pbr_add_iptable(zpi
);
2557 zebra_pbr_del_iptable(zpi
);
2560 zebra_pbr_iptable_free(zpi
);
2565 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
2566 struct zmsghdr
*bad_hdr
)
2569 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2571 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
2573 zserv_encode_error(s
, error
);
2575 client
->error_cnt
++;
2576 zserv_send_message(client
, s
);
2579 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
2581 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2582 zlog_debug("ZAPI message specifies unknown VRF: %d",
2585 return zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
2588 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
2590 zlog_info("Zebra received unknown command %d", hdr
->command
);
2592 return zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
2595 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2596 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2597 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2598 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2599 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2600 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
2601 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2602 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2603 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2604 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2605 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2606 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2607 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2608 [ZEBRA_HELLO
] = zread_hello
,
2609 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2610 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2611 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2612 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2613 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2614 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2615 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2617 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2618 #endif /* HAVE_BFDD */
2619 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2620 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2621 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2622 #if defined(HAVE_RTADV)
2623 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2624 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2626 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2627 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2629 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
2630 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
2631 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
2632 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2633 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2634 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2635 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2636 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2637 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2638 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2639 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2640 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
2641 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2642 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2643 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2644 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2645 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2646 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2647 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2648 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2649 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2650 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2651 [ZEBRA_PW_SET
] = zread_pseudowire
,
2652 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2653 [ZEBRA_RULE_ADD
] = zread_rule
,
2654 [ZEBRA_RULE_DELETE
] = zread_rule
,
2655 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2656 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2657 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2658 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2659 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2660 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2661 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2662 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2663 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2664 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2665 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
2666 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
2667 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
2668 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
2671 #if defined(HANDLE_ZAPI_FUZZING)
2672 extern struct zebra_privs_t zserv_privs
;
2674 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2676 char fname
[MAXPATHLEN
];
2677 struct stream
*copy
;
2680 copy
= stream_dup(orig
);
2681 stream_set_getp(copy
, 0);
2683 snprintf(fname
, MAXPATHLEN
, "%s/%u", frr_vtydir
, command
);
2685 frr_with_privs(&zserv_privs
) {
2686 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2688 stream_flush(copy
, fd
);
2694 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2697 struct zebra_vrf
*zvrf
;
2699 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
2700 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2702 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
2703 STREAM_READABLE(msg
), ZEBRA_MAX_PACKET_SIZ
);
2707 zapi_parse_header(msg
, &hdr
);
2709 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2710 zserv_log_message(NULL
, msg
, &hdr
);
2712 #if defined(HANDLE_ZAPI_FUZZING)
2713 zserv_write_incoming(msg
, hdr
.command
);
2716 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2719 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2721 return zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
2723 if (hdr
.command
>= array_size(zserv_handlers
)
2724 || zserv_handlers
[hdr
.command
] == NULL
)
2725 return zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
2727 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);