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"
69 /* Encoding helpers -------------------------------------------------------- */
71 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
73 /* Interface information. */
74 struct zebra_if
*zif
= ifp
->info
;
76 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
77 stream_putl(s
, ifp
->ifindex
);
78 stream_putc(s
, ifp
->status
);
79 stream_putq(s
, ifp
->flags
);
80 stream_putc(s
, ifp
->ptm_enable
);
81 stream_putc(s
, ifp
->ptm_status
);
82 stream_putl(s
, ifp
->metric
);
83 stream_putl(s
, ifp
->speed
);
84 stream_putl(s
, ifp
->mtu
);
85 stream_putl(s
, ifp
->mtu6
);
86 stream_putl(s
, ifp
->bandwidth
);
87 stream_putl(s
, zif
->link_ifindex
);
88 stream_putl(s
, ifp
->ll_type
);
89 stream_putl(s
, ifp
->hw_addr_len
);
91 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
93 /* Then, Traffic Engineering parameters if any */
94 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
96 zebra_interface_link_params_write(s
, ifp
);
100 /* Write packet size. */
101 stream_putw_at(s
, 0, stream_get_endp(s
));
104 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
106 struct vrf_data data
;
107 const char *netns_name
= zvrf_ns_name(zvrf
);
109 data
.l
.table_id
= zvrf
->table_id
;
112 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
115 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
116 /* Pass the tableid and the netns NAME */
117 stream_put(s
, &data
, sizeof(struct vrf_data
));
118 /* Interface information. */
119 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
120 /* Write packet size. */
121 stream_putw_at(s
, 0, stream_get_endp(s
));
124 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
126 stream_putl(s
, nexthop
->vrf_id
);
127 stream_putc(s
, nexthop
->type
);
128 switch (nexthop
->type
) {
129 case NEXTHOP_TYPE_IPV4
:
130 case NEXTHOP_TYPE_IPV4_IFINDEX
:
131 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
132 stream_putl(s
, nexthop
->ifindex
);
134 case NEXTHOP_TYPE_IPV6
:
135 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
137 case NEXTHOP_TYPE_IPV6_IFINDEX
:
138 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
139 stream_putl(s
, nexthop
->ifindex
);
141 case NEXTHOP_TYPE_IFINDEX
:
142 stream_putl(s
, nexthop
->ifindex
);
152 * Zebra error addition adds error type.
156 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 * | enum zebra_error_types |
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
164 stream_put(s
, &error
, sizeof(error
));
166 /* Write packet size. */
167 stream_putw_at(s
, 0, stream_get_endp(s
));
170 /* Send handlers ----------------------------------------------------------- */
172 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
174 * This function is called in the following situations:
175 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
177 * - at startup, when zebra figures out the available interfaces
178 * - when an interface is added (where support for
179 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
180 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
183 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
185 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
187 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
188 zserv_encode_interface(s
, ifp
);
191 return zserv_send_message(client
, s
);
194 /* Interface deletion from zebra daemon. */
195 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
197 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
199 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
200 zserv_encode_interface(s
, ifp
);
203 return zserv_send_message(client
, s
);
206 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
208 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
210 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
211 zserv_encode_vrf(s
, zvrf
);
213 client
->vrfadd_cnt
++;
214 return zserv_send_message(client
, s
);
217 /* VRF deletion from zebra daemon. */
218 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
221 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
223 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
224 zserv_encode_vrf(s
, zvrf
);
226 client
->vrfdel_cnt
++;
227 return zserv_send_message(client
, s
);
230 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
232 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
234 if (!ifp
->link_params
) {
239 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
241 /* Add Interface Index */
242 stream_putl(s
, ifp
->ifindex
);
244 /* Then TE Link Parameters */
245 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
250 /* Write packet size. */
251 stream_putw_at(s
, 0, stream_get_endp(s
));
253 return zserv_send_message(client
, s
);
256 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
257 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
259 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
260 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
261 * from the client, after the ZEBRA_INTERFACE_ADD has been
262 * sent from zebra to the client
263 * - redistribute new address info to all clients in the following situations
264 * - at startup, when zebra figures out the available interfaces
265 * - when an interface is added (where support for
266 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
267 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
269 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
270 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
271 * - when an RTM_NEWADDR message is received from the kernel,
273 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
275 * zsend_interface_address(DELETE)
278 * zebra_interface_address_delete_update
280 * | | if_delete_update
282 * ip_address_uninstall connected_delete_ipv4
283 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
286 * | RTM_NEWADDR on routing/netlink socket
289 * "no ip address A.B.C.D/M [label LINE]"
290 * "no ip address A.B.C.D/M"
291 * ["no ipv6 address X:X::X:X/M"]
294 int zsend_interface_address(int cmd
, struct zserv
*client
,
295 struct interface
*ifp
, struct connected
*ifc
)
299 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
301 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
302 stream_putl(s
, ifp
->ifindex
);
304 /* Interface address flag. */
305 stream_putc(s
, ifc
->flags
);
307 /* Prefix information. */
309 stream_putc(s
, p
->family
);
310 blen
= prefix_blen(p
);
311 stream_put(s
, &p
->u
.prefix
, blen
);
314 * XXX gnu version does not send prefixlen for
315 * ZEBRA_INTERFACE_ADDRESS_DELETE
316 * but zebra_interface_address_delete_read() in the gnu version
319 stream_putc(s
, p
->prefixlen
);
322 p
= ifc
->destination
;
324 stream_put(s
, &p
->u
.prefix
, blen
);
326 stream_put(s
, NULL
, blen
);
328 /* Write packet size. */
329 stream_putw_at(s
, 0, stream_get_endp(s
));
331 client
->connected_rt_add_cnt
++;
332 return zserv_send_message(client
, s
);
335 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
336 struct interface
*ifp
,
337 struct nbr_connected
*ifc
)
340 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
343 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
344 stream_putl(s
, ifp
->ifindex
);
346 /* Prefix information. */
348 stream_putc(s
, p
->family
);
349 blen
= prefix_blen(p
);
350 stream_put(s
, &p
->u
.prefix
, blen
);
353 * XXX gnu version does not send prefixlen for
354 * ZEBRA_INTERFACE_ADDRESS_DELETE
355 * but zebra_interface_address_delete_read() in the gnu version
358 stream_putc(s
, p
->prefixlen
);
360 /* Write packet size. */
361 stream_putw_at(s
, 0, stream_get_endp(s
));
363 return zserv_send_message(client
, s
);
366 /* Interface address addition. */
367 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
368 struct nbr_connected
*ifc
)
370 struct listnode
*node
, *nnode
;
371 struct zserv
*client
;
374 if (IS_ZEBRA_DEBUG_EVENT
) {
375 char buf
[INET6_ADDRSTRLEN
];
379 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
380 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
382 p
->prefixlen
, ifc
->ifp
->name
);
385 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
386 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
390 /* Interface address deletion. */
391 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
392 struct nbr_connected
*ifc
)
394 struct listnode
*node
, *nnode
;
395 struct zserv
*client
;
398 if (IS_ZEBRA_DEBUG_EVENT
) {
399 char buf
[INET6_ADDRSTRLEN
];
403 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
404 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
406 p
->prefixlen
, ifc
->ifp
->name
);
409 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
))
410 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
414 /* Send addresses on interface to client */
415 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
417 struct listnode
*cnode
, *cnnode
;
419 struct nbr_connected
*nc
;
421 /* Send interface addresses. */
422 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
423 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
426 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
432 /* Send interface neighbors. */
433 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
434 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
443 /* Notify client about interface moving from one VRF to another.
444 * Whether client is interested in old and new VRF is checked by caller.
446 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
449 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
451 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
453 /* Fill in the name of the interface and its new VRF (id) */
454 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
455 stream_putl(s
, vrf_id
);
457 /* Write packet size. */
458 stream_putw_at(s
, 0, stream_get_endp(s
));
460 client
->if_vrfchg_cnt
++;
461 return zserv_send_message(client
, s
);
464 /* Add new nbr connected IPv6 address */
465 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
467 struct nbr_connected
*ifc
;
471 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
472 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
474 ifc
= listnode_head(ifp
->nbr_connected
);
477 ifc
= nbr_connected_new();
478 ifc
->address
= prefix_new();
480 listnode_add(ifp
->nbr_connected
, ifc
);
483 prefix_copy(ifc
->address
, &p
);
485 zebra_interface_nbr_address_add_update(ifp
, ifc
);
487 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
490 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
492 struct nbr_connected
*ifc
;
496 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
497 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
499 ifc
= nbr_connected_check(ifp
, &p
);
503 listnode_delete(ifp
->nbr_connected
, ifc
);
505 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
507 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
509 nbr_connected_free(ifc
);
513 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
514 * ZEBRA_INTERFACE_DOWN.
516 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
517 * the clients in one of 2 situations:
518 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
519 * - a vty command modifying the bandwidth of an interface is received.
520 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
522 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
524 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
526 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
527 zserv_encode_interface(s
, ifp
);
529 if (cmd
== ZEBRA_INTERFACE_UP
)
532 client
->ifdown_cnt
++;
534 return zserv_send_message(client
, s
);
537 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
538 const struct prefix
*p
,
539 const struct prefix
*src_p
,
540 const struct route_entry
*re
)
542 struct zapi_route api
;
543 struct zapi_nexthop
*api_nh
;
544 struct nexthop
*nexthop
;
548 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
550 memset(&api
, 0, sizeof(api
));
551 api
.vrf_id
= re
->vrf_id
;
553 api
.safi
= SAFI_UNICAST
;
554 api
.instance
= re
->instance
;
555 api
.flags
= re
->flags
;
557 afi
= family2afi(p
->family
);
560 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
561 client
->redist_v4_add_cnt
++;
563 client
->redist_v4_del_cnt
++;
566 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
567 client
->redist_v6_add_cnt
++;
569 client
->redist_v6_del_cnt
++;
578 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
579 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
582 for (nexthop
= re
->nhe
->nhg
->nexthop
;
583 nexthop
; nexthop
= nexthop
->next
) {
584 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
587 api_nh
= &api
.nexthops
[count
];
588 api_nh
->vrf_id
= nexthop
->vrf_id
;
589 api_nh
->type
= nexthop
->type
;
590 api_nh
->weight
= nexthop
->weight
;
591 switch (nexthop
->type
) {
592 case NEXTHOP_TYPE_BLACKHOLE
:
593 api_nh
->bh_type
= nexthop
->bh_type
;
595 case NEXTHOP_TYPE_IPV4
:
596 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
598 case NEXTHOP_TYPE_IPV4_IFINDEX
:
599 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
600 api_nh
->ifindex
= nexthop
->ifindex
;
602 case NEXTHOP_TYPE_IFINDEX
:
603 api_nh
->ifindex
= nexthop
->ifindex
;
605 case NEXTHOP_TYPE_IPV6
:
606 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
608 case NEXTHOP_TYPE_IPV6_IFINDEX
:
609 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
610 api_nh
->ifindex
= nexthop
->ifindex
;
617 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
618 api
.nexthop_num
= count
;
622 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
623 api
.distance
= re
->distance
;
624 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
625 api
.metric
= re
->metric
;
627 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
630 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
633 struct stream
*s
= stream_new(stream_size
);
635 /* Encode route and send. */
636 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
641 if (IS_ZEBRA_DEBUG_SEND
) {
642 char buf_prefix
[PREFIX_STRLEN
];
644 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
646 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
647 __func__
, zserv_command_string(cmd
),
648 zebra_route_string(client
->proto
),
649 zebra_route_string(api
.type
), api
.vrf_id
,
652 return zserv_send_message(client
, s
);
656 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
657 * nexthop is not found on mrib. Returns both route metric and protocol
660 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
662 struct route_entry
*re
,
663 struct zebra_vrf
*zvrf
)
668 struct nexthop
*nexthop
;
670 /* Get output stream. */
671 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
674 /* Fill in result. */
675 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
676 stream_put_in_addr(s
, &addr
);
679 stream_putc(s
, re
->distance
);
680 stream_putl(s
, re
->metric
);
682 /* remember position for nexthop_num */
683 nump
= stream_get_endp(s
);
684 /* reserve room for nexthop_num */
687 * Only non-recursive routes are elegible to resolve the
688 * nexthop we are looking up. Therefore, we will just iterate
689 * over the top chain of nexthops.
691 for (nexthop
= re
->nhe
->nhg
->nexthop
; nexthop
;
692 nexthop
= nexthop
->next
)
693 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
694 num
+= zserv_encode_nexthop(s
, nexthop
);
696 /* store nexthop_num */
697 stream_putc_at(s
, nump
, num
);
699 stream_putc(s
, 0); /* distance */
700 stream_putl(s
, 0); /* metric */
701 stream_putc(s
, 0); /* nexthop_num */
704 stream_putw_at(s
, 0, stream_get_endp(s
));
706 return zserv_send_message(client
, s
);
710 * Common utility send route notification, called from a path using a
711 * route_entry and from a path using a dataplane context.
713 static int route_notify_internal(const struct prefix
*p
, int type
,
714 uint16_t instance
, vrf_id_t vrf_id
,
716 enum zapi_route_notify_owner note
)
718 struct zserv
*client
;
722 client
= zserv_find_client(type
, instance
);
723 if (!client
|| !client
->notify_owner
) {
724 if (IS_ZEBRA_DEBUG_PACKET
) {
725 char buff
[PREFIX_STRLEN
];
728 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
729 type
, prefix2str(p
, buff
, sizeof(buff
)),
730 table_id
, note
, vrf_id
);
735 if (IS_ZEBRA_DEBUG_PACKET
) {
736 char buff
[PREFIX_STRLEN
];
738 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
739 type
, prefix2str(p
, buff
, sizeof(buff
)),
740 table_id
, note
, vrf_id
);
743 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
746 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
748 stream_put(s
, ¬e
, sizeof(note
));
750 stream_putc(s
, p
->family
);
752 blen
= prefix_blen(p
);
753 stream_putc(s
, p
->prefixlen
);
754 stream_put(s
, &p
->u
.prefix
, blen
);
756 stream_putl(s
, table_id
);
758 stream_putw_at(s
, 0, stream_get_endp(s
));
760 return zserv_send_message(client
, s
);
763 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
764 enum zapi_route_notify_owner note
)
766 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
771 * Route-owner notification using info from dataplane update context.
773 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
774 enum zapi_route_notify_owner note
)
776 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
777 dplane_ctx_get_type(ctx
),
778 dplane_ctx_get_instance(ctx
),
779 dplane_ctx_get_vrf(ctx
),
780 dplane_ctx_get_table(ctx
),
784 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
785 enum zapi_rule_notify_owner note
)
787 struct listnode
*node
;
788 struct zserv
*client
;
791 if (IS_ZEBRA_DEBUG_PACKET
)
792 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
795 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
796 if (rule
->sock
== client
->sock
)
803 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
805 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
806 stream_put(s
, ¬e
, sizeof(note
));
807 stream_putl(s
, rule
->rule
.seq
);
808 stream_putl(s
, rule
->rule
.priority
);
809 stream_putl(s
, rule
->rule
.unique
);
810 stream_putl(s
, rule
->rule
.ifindex
);
812 stream_putw_at(s
, 0, stream_get_endp(s
));
814 zserv_send_message(client
, s
);
817 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
818 enum zapi_ipset_notify_owner note
)
820 struct listnode
*node
;
821 struct zserv
*client
;
824 if (IS_ZEBRA_DEBUG_PACKET
)
825 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
828 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
829 if (ipset
->sock
== client
->sock
)
836 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
838 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
839 stream_put(s
, ¬e
, sizeof(note
));
840 stream_putl(s
, ipset
->unique
);
841 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
842 stream_putw_at(s
, 0, stream_get_endp(s
));
844 zserv_send_message(client
, s
);
847 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
848 enum zapi_ipset_entry_notify_owner note
)
850 struct listnode
*node
;
851 struct zserv
*client
;
854 if (IS_ZEBRA_DEBUG_PACKET
)
855 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
858 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
859 if (ipset
->sock
== client
->sock
)
866 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
868 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
869 stream_put(s
, ¬e
, sizeof(note
));
870 stream_putl(s
, ipset
->unique
);
871 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
872 stream_putw_at(s
, 0, stream_get_endp(s
));
874 zserv_send_message(client
, s
);
877 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
878 enum zapi_iptable_notify_owner note
)
880 struct listnode
*node
;
881 struct zserv
*client
;
884 if (IS_ZEBRA_DEBUG_PACKET
)
885 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
888 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
889 if (iptable
->sock
== client
->sock
)
896 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
898 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
899 stream_put(s
, ¬e
, sizeof(note
));
900 stream_putl(s
, iptable
->unique
);
901 stream_putw_at(s
, 0, stream_get_endp(s
));
903 zserv_send_message(client
, s
);
906 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
907 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
912 /* Check this client need interface information. */
913 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
916 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
919 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
921 /* Prefix information. */
922 stream_putc(s
, p
->family
);
923 blen
= prefix_blen(p
);
924 stream_put(s
, &p
->u
.prefix
, blen
);
925 stream_putc(s
, p
->prefixlen
);
927 /* Write packet size. */
928 stream_putw_at(s
, 0, stream_get_endp(s
));
930 return zserv_send_message(client
, s
);
934 * Function used by Zebra to send a PW status update to LDP daemon
936 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
938 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
940 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
941 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
942 stream_putl(s
, pw
->ifindex
);
943 stream_putl(s
, pw
->status
);
945 /* Put length at the first point of the stream. */
946 stream_putw_at(s
, 0, stream_get_endp(s
));
948 return zserv_send_message(client
, s
);
951 /* Send response to a get label chunk request to client */
952 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
953 uint8_t proto
, uint16_t instance
,
954 struct label_manager_chunk
*lmc
)
957 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
959 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
961 stream_putc(s
, proto
);
963 stream_putw(s
, instance
);
967 stream_putc(s
, lmc
->keep
);
968 /* start and end labels */
969 stream_putl(s
, lmc
->start
);
970 stream_putl(s
, lmc
->end
);
973 /* Write packet size. */
974 stream_putw_at(s
, 0, stream_get_endp(s
));
976 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
981 /* Send response to a label manager connect request to client */
982 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
983 unsigned short result
)
986 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
988 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
991 stream_putc(s
, client
->proto
);
994 stream_putw(s
, client
->instance
);
997 stream_putc(s
, result
);
999 /* Write packet size. */
1000 stream_putw_at(s
, 0, stream_get_endp(s
));
1002 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1008 /* Send response to a get table chunk request to client */
1009 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1011 struct table_manager_chunk
*tmc
)
1013 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1015 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1018 /* start and end labels */
1019 stream_putl(s
, tmc
->start
);
1020 stream_putl(s
, tmc
->end
);
1023 /* Write packet size. */
1024 stream_putw_at(s
, 0, stream_get_endp(s
));
1026 return zserv_send_message(client
, s
);
1029 static int zsend_table_manager_connect_response(struct zserv
*client
,
1033 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1035 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1038 stream_putc(s
, result
);
1040 stream_putw_at(s
, 0, stream_get_endp(s
));
1042 return zserv_send_message(client
, s
);
1045 /* Inbound message handling ------------------------------------------------ */
1047 const int cmd2type
[] = {
1048 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1049 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1050 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1051 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1054 /* Nexthop register */
1055 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1060 unsigned short l
= 0;
1062 uint16_t type
= cmd2type
[hdr
->command
];
1064 bool flag_changed
= false;
1067 if (IS_ZEBRA_DEBUG_NHT
)
1069 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1070 zebra_route_string(client
->proto
), hdr
->length
,
1071 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1076 client
->nh_reg_time
= monotime(NULL
);
1078 while (l
< hdr
->length
) {
1079 STREAM_GETC(s
, flags
);
1080 STREAM_GETW(s
, p
.family
);
1081 STREAM_GETC(s
, p
.prefixlen
);
1083 if (p
.family
== AF_INET
) {
1084 client
->v4_nh_watch_add_cnt
++;
1085 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1087 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1088 __PRETTY_FUNCTION__
, p
.prefixlen
);
1091 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1092 l
+= IPV4_MAX_BYTELEN
;
1093 } else if (p
.family
== AF_INET6
) {
1094 client
->v6_nh_watch_add_cnt
++;
1095 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1097 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1098 __PRETTY_FUNCTION__
, p
.prefixlen
);
1101 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1102 l
+= IPV6_MAX_BYTELEN
;
1105 EC_ZEBRA_UNKNOWN_FAMILY
,
1106 "rnh_register: Received unknown family type %d\n",
1110 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1114 orig_flags
= rnh
->flags
;
1115 if (type
== RNH_NEXTHOP_TYPE
) {
1117 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1118 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1120 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1121 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1122 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1124 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1125 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1127 && CHECK_FLAG(rnh
->flags
,
1128 ZEBRA_NHT_EXACT_MATCH
))
1129 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1132 if (orig_flags
!= rnh
->flags
)
1133 flag_changed
= true;
1135 /* Anything not AF_INET/INET6 has been filtered out above */
1136 if (!exist
|| flag_changed
)
1137 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1140 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1147 /* Nexthop register */
1148 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1153 unsigned short l
= 0;
1154 uint16_t type
= cmd2type
[hdr
->command
];
1156 if (IS_ZEBRA_DEBUG_NHT
)
1158 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1159 zebra_route_string(client
->proto
), hdr
->length
,
1164 while (l
< hdr
->length
) {
1167 STREAM_GETC(s
, flags
);
1169 goto stream_failure
;
1171 STREAM_GETW(s
, p
.family
);
1172 STREAM_GETC(s
, p
.prefixlen
);
1174 if (p
.family
== AF_INET
) {
1175 client
->v4_nh_watch_rem_cnt
++;
1176 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1178 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1179 __PRETTY_FUNCTION__
, p
.prefixlen
);
1182 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1183 l
+= IPV4_MAX_BYTELEN
;
1184 } else if (p
.family
== AF_INET6
) {
1185 client
->v6_nh_watch_rem_cnt
++;
1186 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1188 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1189 __PRETTY_FUNCTION__
, p
.prefixlen
);
1192 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1193 l
+= IPV6_MAX_BYTELEN
;
1196 EC_ZEBRA_UNKNOWN_FAMILY
,
1197 "rnh_register: Received unknown family type %d\n",
1201 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1203 client
->nh_dereg_time
= monotime(NULL
);
1204 zebra_remove_rnh_client(rnh
, client
, type
);
1211 #define ZEBRA_MIN_FEC_LENGTH 5
1214 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1217 unsigned short l
= 0;
1220 uint32_t label
= MPLS_INVALID_LABEL
;
1221 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1224 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1229 * The minimum amount of data that can be sent for one fec
1232 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1234 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1235 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1240 while (l
< hdr
->length
) {
1241 STREAM_GETW(s
, flags
);
1242 memset(&p
, 0, sizeof(p
));
1243 STREAM_GETW(s
, p
.family
);
1244 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1246 EC_ZEBRA_UNKNOWN_FAMILY
,
1247 "fec_register: Received unknown family type %d\n",
1251 STREAM_GETC(s
, p
.prefixlen
);
1252 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1253 || (p
.family
== AF_INET6
1254 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1256 "%s: Specified prefix hdr->length: %d is to long for %d",
1257 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1261 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1262 l
+= PSIZE(p
.prefixlen
);
1263 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1264 STREAM_GETL(s
, label
);
1266 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1267 STREAM_GETL(s
, label_index
);
1271 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1278 /* FEC unregister */
1279 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1282 unsigned short l
= 0;
1287 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1292 * The minimum amount of data that can be sent for one
1293 * fec unregistration
1295 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1297 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1298 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1303 while (l
< hdr
->length
) {
1304 STREAM_GETW(s
, flags
);
1306 goto stream_failure
;
1308 memset(&p
, 0, sizeof(p
));
1309 STREAM_GETW(s
, p
.family
);
1310 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1312 EC_ZEBRA_UNKNOWN_FAMILY
,
1313 "fec_unregister: Received unknown family type %d\n",
1317 STREAM_GETC(s
, p
.prefixlen
);
1318 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1319 || (p
.family
== AF_INET6
1320 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1322 "%s: Received prefix hdr->length %d which is greater than %d can support",
1323 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1327 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1328 l
+= PSIZE(p
.prefixlen
);
1329 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1338 * Register zebra server interface information.
1339 * Send current all interface and address information.
1341 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1344 struct interface
*ifp
;
1346 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1347 FOR_ALL_INTERFACES (vrf
, ifp
) {
1348 /* Skip pseudo interface. */
1349 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1352 zsend_interface_add(client
, ifp
);
1353 zsend_interface_link_params(client
, ifp
);
1354 zsend_interface_addresses(client
, ifp
);
1359 /* Unregister zebra server interface information. */
1360 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1365 * Handle message requesting interface be set up or down.
1367 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1370 struct interface
*ifp
;
1373 STREAM_GETL(msg
, ifindex
);
1374 STREAM_GETC(msg
, down
);
1377 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1380 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1381 ifindex
, down
? "on" : "off");
1382 zebra_if_set_protodown(ifp
, down
);
1385 "Cannot set protodown %s for interface %u; does not exist",
1386 down
? "on" : "off", ifindex
);
1395 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1396 const unsigned int nexthop_num
)
1398 if (nexthop_num
> zrouter
.multipath_num
) {
1399 char buff
[PREFIX2STR_BUFFER
];
1401 prefix2str(p
, buff
, sizeof(buff
));
1403 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1404 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1405 caller
, buff
, nexthop_num
, zrouter
.multipath_num
);
1409 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1412 struct zapi_route api
;
1413 struct zapi_nexthop
*api_nh
;
1415 struct prefix_ipv6
*src_p
= NULL
;
1416 struct route_entry
*re
;
1417 struct nexthop
*nexthop
= NULL
;
1418 struct nexthop_group
*ng
= NULL
;
1421 struct ipaddr vtep_ip
;
1424 if (zapi_route_decode(s
, &api
) < 0) {
1425 if (IS_ZEBRA_DEBUG_RECV
)
1426 zlog_debug("%s: Unable to decode zapi_route sent",
1427 __PRETTY_FUNCTION__
);
1431 if (IS_ZEBRA_DEBUG_RECV
) {
1432 char buf_prefix
[PREFIX_STRLEN
];
1434 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1435 zlog_debug("%s: p=%s, flags=0x%x",
1436 __func__
, buf_prefix
, api
.flags
);
1439 /* Allocate new route. */
1440 vrf_id
= zvrf_id(zvrf
);
1441 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1442 re
->type
= api
.type
;
1443 re
->instance
= api
.instance
;
1444 re
->flags
= api
.flags
;
1445 re
->uptime
= monotime(NULL
);
1446 re
->vrf_id
= vrf_id
;
1449 re
->table
= api
.tableid
;
1451 re
->table
= zvrf
->table_id
;
1453 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1454 || api
.nexthop_num
== 0) {
1455 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1456 "%s: received a route without nexthops for prefix %pFX from client %s",
1457 __func__
, &api
.prefix
,
1458 zebra_route_string(client
->proto
));
1460 XFREE(MTYPE_RE
, re
);
1464 /* Use temporary list of nexthops */
1465 ng
= nexthop_group_new();
1468 * TBD should _all_ of the nexthop add operations use
1469 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1470 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1472 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1473 api_nh
= &api
.nexthops
[i
];
1474 ifindex_t ifindex
= 0;
1476 if (IS_ZEBRA_DEBUG_RECV
)
1477 zlog_debug("nh type %d", api_nh
->type
);
1479 switch (api_nh
->type
) {
1480 case NEXTHOP_TYPE_IFINDEX
:
1481 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
,
1484 case NEXTHOP_TYPE_IPV4
:
1485 if (IS_ZEBRA_DEBUG_RECV
) {
1486 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1488 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1490 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1491 nhbuf
, api_nh
->vrf_id
);
1493 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
,
1494 NULL
, api_nh
->vrf_id
);
1496 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1498 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1499 ifindex
= api_nh
->ifindex
;
1500 if (IS_ZEBRA_DEBUG_RECV
) {
1501 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1503 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1506 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1507 __func__
, nhbuf
, api_nh
->vrf_id
,
1508 re
->vrf_id
, ifindex
);
1510 nexthop
= nexthop_from_ipv4_ifindex(
1511 &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1514 /* Special handling for IPv4 routes sourced from EVPN:
1515 * the nexthop and associated MAC need to be installed.
1517 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1518 vtep_ip
.ipa_type
= IPADDR_V4
;
1519 memcpy(&(vtep_ip
.ipaddr_v4
),
1520 &(api_nh
->gate
.ipv4
),
1521 sizeof(struct in_addr
));
1522 zebra_vxlan_evpn_vrf_route_add(
1523 api_nh
->vrf_id
, &api_nh
->rmac
,
1524 &vtep_ip
, &api
.prefix
);
1527 case NEXTHOP_TYPE_IPV6
:
1528 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
,
1531 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1532 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1533 ifindex
= api_nh
->ifindex
;
1534 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1538 /* Special handling for IPv6 routes sourced from EVPN:
1539 * the nexthop and associated MAC need to be installed.
1541 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1542 vtep_ip
.ipa_type
= IPADDR_V6
;
1543 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1544 sizeof(struct in6_addr
));
1545 zebra_vxlan_evpn_vrf_route_add(
1546 api_nh
->vrf_id
, &api_nh
->rmac
,
1547 &vtep_ip
, &api
.prefix
);
1550 case NEXTHOP_TYPE_BLACKHOLE
:
1551 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1557 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1558 "%s: Nexthops Specified: %d but we failed to properly create one",
1559 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1560 nexthop_group_delete(&ng
);
1561 XFREE(MTYPE_RE
, re
);
1565 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1566 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1568 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1569 nexthop
->weight
= api_nh
->weight
;
1571 /* MPLS labels for BGP-LU or Segment Routing */
1572 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1573 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1574 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1575 enum lsp_types_t label_type
;
1577 label_type
= lsp_type_from_re_type(client
->proto
);
1579 if (IS_ZEBRA_DEBUG_RECV
) {
1581 "%s: adding %d labels of type %d (1st=%u)",
1582 __func__
, api_nh
->label_num
, label_type
,
1586 nexthop_add_labels(nexthop
, label_type
,
1588 &api_nh
->labels
[0]);
1591 /* Add new nexthop to temporary list */
1592 nexthop_group_add_sorted(ng
, nexthop
);
1595 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1596 re
->distance
= api
.distance
;
1597 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1598 re
->metric
= api
.metric
;
1599 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1601 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1604 afi
= family2afi(api
.prefix
.family
);
1605 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1606 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1607 "%s: Received SRC Prefix but afi is not v6",
1608 __PRETTY_FUNCTION__
);
1609 nexthop_group_delete(&ng
);
1610 XFREE(MTYPE_RE
, re
);
1613 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1614 src_p
= &api
.src_prefix
;
1616 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
1617 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
1618 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1619 __func__
, api
.safi
);
1620 nexthop_group_delete(&ng
);
1621 XFREE(MTYPE_RE
, re
);
1624 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
, ng
);
1627 switch (api
.prefix
.family
) {
1630 client
->v4_route_add_cnt
++;
1632 client
->v4_route_upd8_cnt
++;
1636 client
->v6_route_add_cnt
++;
1638 client
->v6_route_upd8_cnt
++;
1643 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1646 struct zapi_route api
;
1648 struct prefix_ipv6
*src_p
= NULL
;
1652 if (zapi_route_decode(s
, &api
) < 0)
1655 afi
= family2afi(api
.prefix
.family
);
1656 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1657 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1658 "%s: Received a src prefix while afi is not v6",
1659 __PRETTY_FUNCTION__
);
1662 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1663 src_p
= &api
.src_prefix
;
1666 table_id
= api
.tableid
;
1668 table_id
= zvrf
->table_id
;
1670 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1671 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
1672 api
.distance
, false);
1675 switch (api
.prefix
.family
) {
1677 client
->v4_route_del_cnt
++;
1680 client
->v6_route_del_cnt
++;
1685 /* MRIB Nexthop lookup for IPv4. */
1686 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1688 struct in_addr addr
;
1689 struct route_entry
*re
;
1691 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1692 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1693 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1699 /* Register zebra server router-id information. Send current router-id */
1700 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1704 /* Router-id information is needed. */
1705 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1707 router_id_get(&p
, zvrf_id(zvrf
));
1709 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1712 /* Unregister zebra server router-id information. */
1713 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1715 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1718 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1720 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1722 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1723 stream_putl(s
, vrf_get_backend());
1724 stream_putc(s
, mpls_enabled
);
1725 stream_putl(s
, zrouter
.multipath_num
);
1726 stream_putc(s
, zebra_mlag_get_role());
1728 stream_putw_at(s
, 0, stream_get_endp(s
));
1729 zserv_send_message(client
, s
);
1732 void zsend_capabilities_all_clients(void)
1734 struct listnode
*node
, *nnode
;
1735 struct zebra_vrf
*zvrf
;
1736 struct zserv
*client
;
1738 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1739 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1740 zsend_capabilities(client
, zvrf
);
1744 /* Tie up route-type and client->sock */
1745 static void zread_hello(ZAPI_HANDLER_ARGS
)
1747 /* type of protocol (lib/zebra.h) */
1749 unsigned short instance
;
1752 STREAM_GETC(msg
, proto
);
1753 STREAM_GETW(msg
, instance
);
1754 STREAM_GETC(msg
, notify
);
1756 client
->notify_owner
= true;
1758 /* accept only dynamic routing protocols */
1759 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1761 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1762 client
->sock
, zebra_route_string(proto
),
1765 zlog_notice("client protocol instance %d", instance
);
1767 client
->proto
= proto
;
1768 client
->instance
= instance
;
1771 zsend_capabilities(client
, zvrf
);
1772 zebra_vrf_update_all(client
);
1777 /* Unregister all information in a VRF. */
1778 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1783 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1784 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1785 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1786 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
1788 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1792 * Handle request to create an MPLS LSP.
1794 * A single message can fully specify an LSP with multiple nexthops.
1796 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1797 * updated to use the received label(s).
1799 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
1802 struct zapi_labels zl
;
1804 /* Get input stream. */
1806 if (zapi_labels_decode(s
, &zl
) < 0) {
1807 if (IS_ZEBRA_DEBUG_RECV
)
1808 zlog_debug("%s: Unable to decode zapi_labels sent",
1809 __PRETTY_FUNCTION__
);
1816 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1817 struct zapi_nexthop_label
*znh
;
1819 znh
= &zl
.nexthops
[i
];
1820 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, 1, &znh
->label
,
1821 znh
->type
, &znh
->address
, znh
->ifindex
);
1823 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1824 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1825 znh
->type
, &znh
->address
, znh
->ifindex
,
1826 zl
.route
.type
, zl
.route
.instance
,
1832 * Handle request to delete an MPLS LSP.
1834 * An LSP is identified by its type and local label. When the received message
1835 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
1836 * listed LSP nexthops (aka NHLFEs) are deleted.
1838 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
1839 * FEC (route) nexthops are deleted.
1841 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
1844 struct zapi_labels zl
;
1846 /* Get input stream. */
1848 if (zapi_labels_decode(s
, &zl
) < 0) {
1849 if (IS_ZEBRA_DEBUG_RECV
)
1850 zlog_debug("%s: Unable to decode zapi_labels sent",
1851 __PRETTY_FUNCTION__
);
1858 if (zl
.nexthop_num
> 0) {
1859 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1860 struct zapi_nexthop_label
*znh
;
1862 znh
= &zl
.nexthops
[i
];
1863 mpls_lsp_uninstall(zvrf
, zl
.type
, zl
.local_label
,
1864 znh
->type
, &znh
->address
,
1867 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1868 mpls_ftn_update(0, zvrf
, zl
.type
,
1869 &zl
.route
.prefix
, znh
->type
,
1870 &znh
->address
, znh
->ifindex
,
1872 zl
.route
.instance
, znh
->label
);
1875 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1877 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1878 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1879 zl
.route
.type
, zl
.route
.instance
);
1884 * Handle request to add an MPLS LSP or change an existing one.
1886 * A single message can fully specify an LSP with multiple nexthops.
1888 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1889 * updated to use the received label(s).
1891 * NOTE: zebra will use route replace semantics (make-before-break) to update
1892 * the LSP in the forwarding plane if that's supported by the underlying
1895 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
1898 struct zapi_labels zl
;
1900 /* Get input stream. */
1902 if (zapi_labels_decode(s
, &zl
) < 0) {
1903 if (IS_ZEBRA_DEBUG_RECV
)
1904 zlog_debug("%s: Unable to decode zapi_labels sent",
1905 __PRETTY_FUNCTION__
);
1912 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
1913 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
1914 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
1915 zl
.route
.type
, zl
.route
.instance
);
1917 for (int i
= 0; i
< zl
.nexthop_num
; i
++) {
1918 struct zapi_nexthop_label
*znh
;
1920 znh
= &zl
.nexthops
[i
];
1921 mpls_lsp_install(zvrf
, zl
.type
, zl
.local_label
, 1, &znh
->label
,
1922 znh
->type
, &znh
->address
, znh
->ifindex
);
1924 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
)) {
1925 mpls_ftn_update(1, zvrf
, zl
.type
, &zl
.route
.prefix
,
1926 znh
->type
, &znh
->address
, znh
->ifindex
,
1927 zl
.route
.type
, zl
.route
.instance
,
1933 /* Send response to a table manager connect request to client */
1934 static void zread_table_manager_connect(struct zserv
*client
,
1935 struct stream
*msg
, vrf_id_t vrf_id
)
1944 STREAM_GETC(s
, proto
);
1945 STREAM_GETW(s
, instance
);
1947 /* accept only dynamic routing protocols */
1948 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1949 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1950 "client %d has wrong protocol %s", client
->sock
,
1951 zebra_route_string(proto
));
1952 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1955 zlog_notice("client %d with vrf %u instance %u connected as %s",
1956 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1957 client
->proto
= proto
;
1958 client
->instance
= instance
;
1961 * Release previous labels of same protocol and instance.
1962 * This is done in case it restarted from an unexpected shutdown.
1964 release_daemon_table_chunks(client
);
1966 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1972 static void zread_label_manager_connect(struct zserv
*client
,
1973 struct stream
*msg
, vrf_id_t vrf_id
)
1976 /* type of protocol (lib/zebra.h) */
1978 unsigned short instance
;
1980 /* Get input stream. */
1984 STREAM_GETC(s
, proto
);
1985 STREAM_GETW(s
, instance
);
1987 /* accept only dynamic routing protocols */
1988 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1989 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1990 "client %d has wrong protocol %s", client
->sock
,
1991 zebra_route_string(proto
));
1992 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1996 /* recall proto and instance in this socket */
1997 client
->proto
= proto
;
1998 client
->instance
= instance
;
2000 /* call hook for connection using wrapper */
2001 lm_client_connect_call(proto
, instance
, vrf_id
);
2007 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2012 uint32_t size
, base
;
2013 struct label_manager_chunk
*lmc
= NULL
;
2015 unsigned short instance
;
2017 /* Get input stream. */
2021 STREAM_GETC(s
, proto
);
2022 STREAM_GETW(s
, instance
);
2023 STREAM_GETC(s
, keep
);
2024 STREAM_GETL(s
, size
);
2025 STREAM_GETL(s
, base
);
2027 /* call hook to get a chunk using wrapper */
2028 lm_get_chunk_call(&lmc
, proto
, instance
, keep
, size
, base
, vrf_id
);
2032 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
2033 "Unable to assign Label Chunk of size %u to %s instance %u",
2034 size
, zebra_route_string(proto
), instance
);
2036 if (IS_ZEBRA_DEBUG_PACKET
)
2037 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
2038 lmc
->start
, lmc
->end
,
2039 zebra_route_string(proto
), instance
);
2045 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2048 uint32_t start
, end
;
2050 unsigned short instance
;
2052 /* Get input stream. */
2056 STREAM_GETC(s
, proto
);
2057 STREAM_GETW(s
, instance
);
2058 STREAM_GETL(s
, start
);
2059 STREAM_GETL(s
, end
);
2061 /* call hook to release a chunk using wrapper */
2062 lm_release_chunk_call(proto
, instance
, start
, end
);
2068 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2070 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2071 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2072 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2074 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2075 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2076 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2077 zread_release_label_chunk(client
, msg
);
2081 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2086 struct table_manager_chunk
*tmc
;
2088 /* Get input stream. */
2092 STREAM_GETL(s
, size
);
2094 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2096 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2097 "%s: Unable to assign Table Chunk of size %u",
2100 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2102 /* send response back */
2103 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2109 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2112 uint32_t start
, end
;
2114 /* Get input stream. */
2118 STREAM_GETL(s
, start
);
2119 STREAM_GETL(s
, end
);
2121 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2127 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2129 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2130 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2131 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2133 /* Sanity: don't allow 'unidentified' requests */
2134 if (!client
->proto
) {
2137 "Got table request from an unidentified client");
2140 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2141 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2142 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2143 zread_release_table_chunk(client
, msg
);
2147 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2150 char ifname
[IF_NAMESIZE
];
2154 union g_addr nexthop
;
2155 uint32_t local_label
;
2156 uint32_t remote_label
;
2158 union pw_protocol_fields data
;
2160 struct zebra_pw
*pw
;
2162 /* Get input stream. */
2166 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2167 ifname
[IF_NAMESIZE
- 1] = '\0';
2168 STREAM_GETL(s
, ifindex
);
2169 STREAM_GETL(s
, type
);
2173 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2176 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2181 STREAM_GETL(s
, local_label
);
2182 STREAM_GETL(s
, remote_label
);
2183 STREAM_GETC(s
, flags
);
2184 STREAM_GET(&data
, s
, sizeof(data
));
2185 protocol
= client
->proto
;
2187 pw
= zebra_pw_find(zvrf
, ifname
);
2188 switch (hdr
->command
) {
2191 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2192 "%s: pseudowire %s already exists [%s]",
2194 zserv_command_string(hdr
->command
));
2198 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2200 case ZEBRA_PW_DELETE
:
2202 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2203 "%s: pseudowire %s not found [%s]", __func__
,
2204 ifname
, zserv_command_string(hdr
->command
));
2208 zebra_pw_del(zvrf
, pw
);
2211 case ZEBRA_PW_UNSET
:
2213 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2214 "%s: pseudowire %s not found [%s]", __func__
,
2215 ifname
, zserv_command_string(hdr
->command
));
2219 switch (hdr
->command
) {
2223 case ZEBRA_PW_UNSET
:
2228 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2229 remote_label
, flags
, &data
);
2237 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2239 struct interface
*master
;
2240 struct interface
*slave
;
2241 struct stream
*s
= msg
;
2245 STREAM_GETL(s
, vrf_id
);
2246 STREAM_GETL(s
, ifindex
);
2247 master
= if_lookup_by_index(ifindex
, vrf_id
);
2249 STREAM_GETL(s
, vrf_id
);
2250 STREAM_GETL(s
, ifindex
);
2251 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2253 if (!master
|| !slave
)
2256 kernel_interface_set_master(master
, slave
);
2263 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2265 struct interface
*ifp
;
2266 mpls_label_t nlabel
;
2269 struct zebra_vrf
*def_zvrf
;
2270 enum lsp_types_t ltype
;
2273 STREAM_GETL(s
, nlabel
);
2274 STREAM_GETC(s
, afi
);
2275 if (nlabel
== zvrf
->label
[afi
]) {
2277 * Nothing to do here move along
2282 STREAM_GETC(s
, ltype
);
2284 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2285 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2287 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2290 zlog_debug("Unable to find specified Interface for %s",
2295 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2297 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2301 really_remove
= true;
2302 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2303 if (scrubber
== afi
)
2306 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2309 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2310 really_remove
= false;
2316 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2317 NEXTHOP_TYPE_IFINDEX
, NULL
,
2321 if (nlabel
!= MPLS_LABEL_NONE
) {
2322 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2323 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2324 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2327 zvrf
->label
[afi
] = nlabel
;
2332 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2334 struct zebra_pbr_rule zpr
;
2339 STREAM_GETL(s
, total
);
2341 for (i
= 0; i
< total
; i
++) {
2342 memset(&zpr
, 0, sizeof(zpr
));
2344 zpr
.sock
= client
->sock
;
2345 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2346 STREAM_GETL(s
, zpr
.rule
.seq
);
2347 STREAM_GETL(s
, zpr
.rule
.priority
);
2348 STREAM_GETL(s
, zpr
.rule
.unique
);
2349 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2350 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2351 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2352 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2353 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2354 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2355 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2356 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2357 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2358 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2359 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2360 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2361 STREAM_GETL(s
, zpr
.rule
.ifindex
);
2363 if (zpr
.rule
.ifindex
) {
2364 struct interface
*ifp
;
2366 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
,
2369 zlog_debug("Failed to lookup ifindex: %u",
2374 strlcpy(zpr
.ifname
, ifp
->name
, sizeof(zpr
.ifname
));
2377 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2378 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2380 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2381 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2383 if (zpr
.rule
.filter
.src_port
)
2384 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2386 if (zpr
.rule
.filter
.dst_port
)
2387 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2389 if (zpr
.rule
.filter
.fwmark
)
2390 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2392 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
2393 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
2394 zlog_warn("Unsupported PBR source IP family: %s\n",
2395 family2str(zpr
.rule
.filter
.src_ip
.family
));
2398 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
2399 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
2400 zlog_warn("Unsupported PBR dest IP family: %s\n",
2401 family2str(zpr
.rule
.filter
.dst_ip
.family
));
2406 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2407 if (hdr
->command
== ZEBRA_RULE_ADD
)
2408 zebra_pbr_add_rule(&zpr
);
2410 zebra_pbr_del_rule(&zpr
);
2417 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2419 struct zebra_pbr_ipset zpi
;
2424 STREAM_GETL(s
, total
);
2426 for (i
= 0; i
< total
; i
++) {
2427 memset(&zpi
, 0, sizeof(zpi
));
2429 zpi
.sock
= client
->sock
;
2430 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2431 STREAM_GETL(s
, zpi
.unique
);
2432 STREAM_GETL(s
, zpi
.type
);
2433 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2435 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2436 zebra_pbr_create_ipset(&zpi
);
2438 zebra_pbr_destroy_ipset(&zpi
);
2445 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2447 struct zebra_pbr_ipset_entry zpi
;
2448 struct zebra_pbr_ipset ipset
;
2453 STREAM_GETL(s
, total
);
2455 for (i
= 0; i
< total
; i
++) {
2456 memset(&zpi
, 0, sizeof(zpi
));
2457 memset(&ipset
, 0, sizeof(ipset
));
2459 zpi
.sock
= client
->sock
;
2460 STREAM_GETL(s
, zpi
.unique
);
2461 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2462 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
2463 STREAM_GETC(s
, zpi
.src
.family
);
2464 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2465 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2466 STREAM_GETC(s
, zpi
.dst
.family
);
2467 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2468 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2470 STREAM_GETW(s
, zpi
.src_port_min
);
2471 STREAM_GETW(s
, zpi
.src_port_max
);
2472 STREAM_GETW(s
, zpi
.dst_port_min
);
2473 STREAM_GETW(s
, zpi
.dst_port_max
);
2474 STREAM_GETC(s
, zpi
.proto
);
2475 if (!is_default_prefix(&zpi
.src
))
2476 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2478 if (!is_default_prefix(&zpi
.dst
))
2479 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2480 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2481 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2482 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2483 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2484 if (zpi
.dst_port_max
!= 0)
2485 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2486 if (zpi
.src_port_max
!= 0)
2487 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2489 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2491 /* calculate backpointer */
2493 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2495 if (!zpi
.backpointer
) {
2496 zlog_warn("ipset name specified: %s does not exist",
2498 goto stream_failure
;
2501 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2502 zebra_pbr_add_ipset_entry(&zpi
);
2504 zebra_pbr_del_ipset_entry(&zpi
);
2511 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2513 struct zebra_pbr_iptable
*zpi
=
2514 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
2519 zpi
->interface_name_list
= list_new();
2520 zpi
->sock
= client
->sock
;
2521 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
2522 STREAM_GETL(s
, zpi
->unique
);
2523 STREAM_GETL(s
, zpi
->type
);
2524 STREAM_GETL(s
, zpi
->filter_bm
);
2525 STREAM_GETL(s
, zpi
->action
);
2526 STREAM_GETL(s
, zpi
->fwmark
);
2527 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2528 STREAM_GETW(s
, zpi
->pkt_len_min
);
2529 STREAM_GETW(s
, zpi
->pkt_len_max
);
2530 STREAM_GETW(s
, zpi
->tcp_flags
);
2531 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
2532 STREAM_GETC(s
, zpi
->dscp_value
);
2533 STREAM_GETC(s
, zpi
->fragment
);
2534 STREAM_GETC(s
, zpi
->protocol
);
2535 STREAM_GETL(s
, zpi
->nb_interface
);
2536 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
2538 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2539 zebra_pbr_add_iptable(zpi
);
2541 zebra_pbr_del_iptable(zpi
);
2544 zebra_pbr_iptable_free(zpi
);
2549 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
2550 struct zmsghdr
*bad_hdr
)
2553 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2555 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
2557 zserv_encode_error(s
, error
);
2559 client
->error_cnt
++;
2560 zserv_send_message(client
, s
);
2563 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
2565 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2566 zlog_debug("ZAPI message specifies unknown VRF: %d",
2569 return zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
2572 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
2574 zlog_info("Zebra received unknown command %d", hdr
->command
);
2576 return zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
2579 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2580 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2581 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2582 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2583 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2584 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
2585 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2586 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2587 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2588 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2589 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2590 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2591 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2592 [ZEBRA_HELLO
] = zread_hello
,
2593 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2594 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2595 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2596 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2597 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2598 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2599 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2601 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2602 #endif /* HAVE_BFDD */
2603 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2604 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2605 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2606 #if defined(HAVE_RTADV)
2607 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2608 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2610 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2611 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2613 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
2614 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
2615 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
2616 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2617 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2618 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2619 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2620 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2621 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2622 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2623 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2624 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
2625 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2626 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2627 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2628 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2629 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2630 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2631 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2632 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2633 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2634 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2635 [ZEBRA_PW_SET
] = zread_pseudowire
,
2636 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2637 [ZEBRA_RULE_ADD
] = zread_rule
,
2638 [ZEBRA_RULE_DELETE
] = zread_rule
,
2639 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2640 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2641 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2642 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2643 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2644 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2645 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2646 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2647 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2648 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2649 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
2650 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
2651 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
2652 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
2655 #if defined(HANDLE_ZAPI_FUZZING)
2656 extern struct zebra_privs_t zserv_privs
;
2658 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2660 char fname
[MAXPATHLEN
];
2661 struct stream
*copy
;
2664 copy
= stream_dup(orig
);
2665 stream_set_getp(copy
, 0);
2667 snprintf(fname
, MAXPATHLEN
, "%s/%u", frr_vtydir
, command
);
2669 frr_with_privs(&zserv_privs
) {
2670 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2672 stream_flush(copy
, fd
);
2678 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2681 struct zebra_vrf
*zvrf
;
2683 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
2684 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2686 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
2687 STREAM_READABLE(msg
), ZEBRA_MAX_PACKET_SIZ
);
2691 zapi_parse_header(msg
, &hdr
);
2693 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2694 zserv_log_message(NULL
, msg
, &hdr
);
2696 #if defined(HANDLE_ZAPI_FUZZING)
2697 zserv_write_incoming(msg
, hdr
.command
);
2700 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2703 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2705 return zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
2707 if (hdr
.command
>= array_size(zserv_handlers
)
2708 || zserv_handlers
[hdr
.command
] == NULL
)
2709 return zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
2711 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);