2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/rib.h"
46 #include "zebra/zebra_memory.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/router-id.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/debug.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/rt_netlink.h"
54 #include "zebra/interface.h"
55 #include "zebra/zebra_ptm.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/zebra_mpls.h"
58 #include "zebra/zebra_mroute.h"
59 #include "zebra/label_manager.h"
60 #include "zebra/zebra_vxlan.h"
62 #include "zebra/zebra_pbr.h"
63 #include "zebra/table_manager.h"
64 #include "zebra/zapi_msg.h"
66 /* Encoding helpers -------------------------------------------------------- */
68 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
70 /* Interface information. */
71 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
72 stream_putl(s
, ifp
->ifindex
);
73 stream_putc(s
, ifp
->status
);
74 stream_putq(s
, ifp
->flags
);
75 stream_putc(s
, ifp
->ptm_enable
);
76 stream_putc(s
, ifp
->ptm_status
);
77 stream_putl(s
, ifp
->metric
);
78 stream_putl(s
, ifp
->speed
);
79 stream_putl(s
, ifp
->mtu
);
80 stream_putl(s
, ifp
->mtu6
);
81 stream_putl(s
, ifp
->bandwidth
);
82 stream_putl(s
, ifp
->ll_type
);
83 stream_putl(s
, ifp
->hw_addr_len
);
85 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
87 /* Then, Traffic Engineering parameters if any */
88 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
90 zebra_interface_link_params_write(s
, ifp
);
94 /* Write packet size. */
95 stream_putw_at(s
, 0, stream_get_endp(s
));
98 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
100 struct vrf_data data
;
101 const char *netns_name
= zvrf_ns_name(zvrf
);
103 data
.l
.table_id
= zvrf
->table_id
;
106 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
109 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
110 /* Pass the tableid and the netns NAME */
111 stream_put(s
, &data
, sizeof(struct vrf_data
));
112 /* Interface information. */
113 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
114 /* Write packet size. */
115 stream_putw_at(s
, 0, stream_get_endp(s
));
118 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
120 stream_putc(s
, nexthop
->type
);
121 switch (nexthop
->type
) {
122 case NEXTHOP_TYPE_IPV4
:
123 case NEXTHOP_TYPE_IPV4_IFINDEX
:
124 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
125 stream_putl(s
, nexthop
->ifindex
);
127 case NEXTHOP_TYPE_IPV6
:
128 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
130 case NEXTHOP_TYPE_IPV6_IFINDEX
:
131 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
132 stream_putl(s
, nexthop
->ifindex
);
134 case NEXTHOP_TYPE_IFINDEX
:
135 stream_putl(s
, nexthop
->ifindex
);
144 /* Send handlers ----------------------------------------------------------- */
146 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
148 * This function is called in the following situations:
149 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
151 * - at startup, when zebra figures out the available interfaces
152 * - when an interface is added (where support for
153 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
154 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
157 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
159 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
161 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
162 zserv_encode_interface(s
, ifp
);
165 return zserv_send_message(client
, s
);
168 /* Interface deletion from zebra daemon. */
169 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
171 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
173 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
174 zserv_encode_interface(s
, ifp
);
177 return zserv_send_message(client
, s
);
180 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
182 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
184 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
185 zserv_encode_vrf(s
, zvrf
);
187 client
->vrfadd_cnt
++;
188 return zserv_send_message(client
, s
);
191 /* VRF deletion from zebra daemon. */
192 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
195 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
197 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
198 zserv_encode_vrf(s
, zvrf
);
200 client
->vrfdel_cnt
++;
201 return zserv_send_message(client
, s
);
204 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
206 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
208 /* Check this client need interface information. */
209 if (!client
->ifinfo
) {
214 if (!ifp
->link_params
) {
219 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
221 /* Add Interface Index */
222 stream_putl(s
, ifp
->ifindex
);
224 /* Then TE Link Parameters */
225 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
230 /* Write packet size. */
231 stream_putw_at(s
, 0, stream_get_endp(s
));
233 return zserv_send_message(client
, s
);
236 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
237 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
239 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
240 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
241 * from the client, after the ZEBRA_INTERFACE_ADD has been
242 * sent from zebra to the client
243 * - redistribute new address info to all clients in the following situations
244 * - at startup, when zebra figures out the available interfaces
245 * - when an interface is added (where support for
246 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
247 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
249 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
250 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
251 * - when an RTM_NEWADDR message is received from the kernel,
253 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
255 * zsend_interface_address(DELETE)
258 * zebra_interface_address_delete_update
260 * | | if_delete_update
262 * ip_address_uninstall connected_delete_ipv4
263 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
266 * | RTM_NEWADDR on routing/netlink socket
269 * "no ip address A.B.C.D/M [label LINE]"
270 * "no ip address A.B.C.D/M secondary"
271 * ["no ipv6 address X:X::X:X/M"]
274 int zsend_interface_address(int cmd
, struct zserv
*client
,
275 struct interface
*ifp
, struct connected
*ifc
)
279 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
281 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
282 stream_putl(s
, ifp
->ifindex
);
284 /* Interface address flag. */
285 stream_putc(s
, ifc
->flags
);
287 /* Prefix information. */
289 stream_putc(s
, p
->family
);
290 blen
= prefix_blen(p
);
291 stream_put(s
, &p
->u
.prefix
, blen
);
294 * XXX gnu version does not send prefixlen for
295 * ZEBRA_INTERFACE_ADDRESS_DELETE
296 * but zebra_interface_address_delete_read() in the gnu version
299 stream_putc(s
, p
->prefixlen
);
302 p
= ifc
->destination
;
304 stream_put(s
, &p
->u
.prefix
, blen
);
306 stream_put(s
, NULL
, blen
);
308 /* Write packet size. */
309 stream_putw_at(s
, 0, stream_get_endp(s
));
311 client
->connected_rt_add_cnt
++;
312 return zserv_send_message(client
, s
);
315 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
316 struct interface
*ifp
,
317 struct nbr_connected
*ifc
)
320 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
323 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
324 stream_putl(s
, ifp
->ifindex
);
326 /* Prefix information. */
328 stream_putc(s
, p
->family
);
329 blen
= prefix_blen(p
);
330 stream_put(s
, &p
->u
.prefix
, blen
);
333 * XXX gnu version does not send prefixlen for
334 * ZEBRA_INTERFACE_ADDRESS_DELETE
335 * but zebra_interface_address_delete_read() in the gnu version
338 stream_putc(s
, p
->prefixlen
);
340 /* Write packet size. */
341 stream_putw_at(s
, 0, stream_get_endp(s
));
343 return zserv_send_message(client
, s
);
346 /* Interface address addition. */
347 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
348 struct nbr_connected
*ifc
)
350 struct listnode
*node
, *nnode
;
351 struct zserv
*client
;
354 if (IS_ZEBRA_DEBUG_EVENT
) {
355 char buf
[INET6_ADDRSTRLEN
];
359 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
360 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
362 p
->prefixlen
, ifc
->ifp
->name
);
365 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
366 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
370 /* Interface address deletion. */
371 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
372 struct nbr_connected
*ifc
)
374 struct listnode
*node
, *nnode
;
375 struct zserv
*client
;
378 if (IS_ZEBRA_DEBUG_EVENT
) {
379 char buf
[INET6_ADDRSTRLEN
];
383 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
384 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
386 p
->prefixlen
, ifc
->ifp
->name
);
389 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
390 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
394 /* Send addresses on interface to client */
395 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
397 struct listnode
*cnode
, *cnnode
;
399 struct nbr_connected
*nc
;
401 /* Send interface addresses. */
402 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
403 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
406 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
412 /* Send interface neighbors. */
413 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
414 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
423 /* Notify client about interface moving from one VRF to another.
424 * Whether client is interested in old and new VRF is checked by caller.
426 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
429 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
431 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
433 /* Fill in the ifIndex of the interface and its new VRF (id) */
434 stream_putl(s
, ifp
->ifindex
);
435 stream_putl(s
, vrf_id
);
437 /* Write packet size. */
438 stream_putw_at(s
, 0, stream_get_endp(s
));
440 client
->if_vrfchg_cnt
++;
441 return zserv_send_message(client
, s
);
444 /* Add new nbr connected IPv6 address */
445 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
447 struct nbr_connected
*ifc
;
451 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
452 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
454 ifc
= listnode_head(ifp
->nbr_connected
);
457 ifc
= nbr_connected_new();
458 ifc
->address
= prefix_new();
460 listnode_add(ifp
->nbr_connected
, ifc
);
463 prefix_copy(ifc
->address
, &p
);
465 zebra_interface_nbr_address_add_update(ifp
, ifc
);
467 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
470 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
472 struct nbr_connected
*ifc
;
476 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
477 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
479 ifc
= nbr_connected_check(ifp
, &p
);
483 listnode_delete(ifp
->nbr_connected
, ifc
);
485 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
487 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
489 nbr_connected_free(ifc
);
493 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
494 * ZEBRA_INTERFACE_DOWN.
496 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
497 * the clients in one of 2 situations:
498 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
499 * - a vty command modifying the bandwidth of an interface is received.
500 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
502 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
504 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
506 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
507 zserv_encode_interface(s
, ifp
);
509 if (cmd
== ZEBRA_INTERFACE_UP
)
512 client
->ifdown_cnt
++;
514 return zserv_send_message(client
, s
);
517 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
518 const struct prefix
*p
,
519 const struct prefix
*src_p
, struct route_entry
*re
)
521 struct zapi_route api
;
522 struct zapi_nexthop
*api_nh
;
523 struct nexthop
*nexthop
;
527 memset(&api
, 0, sizeof(api
));
528 api
.vrf_id
= re
->vrf_id
;
530 api
.instance
= re
->instance
;
531 api
.flags
= re
->flags
;
533 afi
= family2afi(p
->family
);
536 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
537 client
->redist_v4_add_cnt
++;
539 client
->redist_v4_del_cnt
++;
542 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
543 client
->redist_v6_add_cnt
++;
545 client
->redist_v6_del_cnt
++;
554 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
555 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
559 if (re
->nexthop_active_num
) {
560 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
561 api
.nexthop_num
= re
->nexthop_active_num
;
563 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
564 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
567 api_nh
= &api
.nexthops
[count
];
568 api_nh
->vrf_id
= nexthop
->vrf_id
;
569 api_nh
->type
= nexthop
->type
;
570 switch (nexthop
->type
) {
571 case NEXTHOP_TYPE_BLACKHOLE
:
572 api_nh
->bh_type
= nexthop
->bh_type
;
574 case NEXTHOP_TYPE_IPV4
:
575 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
577 case NEXTHOP_TYPE_IPV4_IFINDEX
:
578 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
579 api_nh
->ifindex
= nexthop
->ifindex
;
581 case NEXTHOP_TYPE_IFINDEX
:
582 api_nh
->ifindex
= nexthop
->ifindex
;
584 case NEXTHOP_TYPE_IPV6
:
585 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
587 case NEXTHOP_TYPE_IPV6_IFINDEX
:
588 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
589 api_nh
->ifindex
= nexthop
->ifindex
;
595 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
596 api
.distance
= re
->distance
;
597 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
598 api
.metric
= re
->metric
;
600 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
603 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
606 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
608 /* Encode route and send. */
609 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
614 if (IS_ZEBRA_DEBUG_SEND
) {
615 char buf_prefix
[PREFIX_STRLEN
];
617 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
619 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
620 __func__
, zserv_command_string(cmd
),
621 zebra_route_string(client
->proto
),
622 zebra_route_string(api
.type
), api
.vrf_id
,
625 return zserv_send_message(client
, s
);
629 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
630 * nexthop is not found on mrib. Returns both route metric and protocol
633 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
635 struct route_entry
*re
,
636 struct zebra_vrf
*zvrf
)
641 struct nexthop
*nexthop
;
643 /* Get output stream. */
644 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
647 /* Fill in result. */
648 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
649 stream_put_in_addr(s
, &addr
);
652 stream_putc(s
, re
->distance
);
653 stream_putl(s
, re
->metric
);
655 /* remember position for nexthop_num */
656 nump
= stream_get_endp(s
);
657 /* reserve room for nexthop_num */
660 * Only non-recursive routes are elegible to resolve the
661 * nexthop we are looking up. Therefore, we will just iterate
662 * over the top chain of nexthops.
664 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
665 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
666 num
+= zserv_encode_nexthop(s
, nexthop
);
668 /* store nexthop_num */
669 stream_putc_at(s
, nump
, num
);
671 stream_putc(s
, 0); /* distance */
672 stream_putl(s
, 0); /* metric */
673 stream_putc(s
, 0); /* nexthop_num */
676 stream_putw_at(s
, 0, stream_get_endp(s
));
678 return zserv_send_message(client
, s
);
682 * Common utility send route notification, called from a path using a
683 * route_entry and from a path using a dataplane context.
685 static int route_notify_internal(const struct prefix
*p
, int type
,
686 uint16_t instance
, vrf_id_t vrf_id
,
688 enum zapi_route_notify_owner note
)
690 struct zserv
*client
;
694 client
= zserv_find_client(type
, instance
);
695 if (!client
|| !client
->notify_owner
) {
696 if (IS_ZEBRA_DEBUG_PACKET
) {
697 char buff
[PREFIX_STRLEN
];
700 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
701 type
, prefix2str(p
, buff
, sizeof(buff
)),
702 table_id
, note
, vrf_id
);
707 if (IS_ZEBRA_DEBUG_PACKET
) {
708 char buff
[PREFIX_STRLEN
];
710 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
711 type
, prefix2str(p
, buff
, sizeof(buff
)),
712 table_id
, note
, vrf_id
);
715 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
718 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
720 stream_put(s
, ¬e
, sizeof(note
));
722 stream_putc(s
, p
->family
);
724 blen
= prefix_blen(p
);
725 stream_putc(s
, p
->prefixlen
);
726 stream_put(s
, &p
->u
.prefix
, blen
);
728 stream_putl(s
, table_id
);
730 stream_putw_at(s
, 0, stream_get_endp(s
));
732 return zserv_send_message(client
, s
);
735 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
736 enum zapi_route_notify_owner note
)
738 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
742 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
743 enum zapi_rule_notify_owner note
)
745 struct listnode
*node
;
746 struct zserv
*client
;
749 if (IS_ZEBRA_DEBUG_PACKET
)
750 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
753 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
754 if (rule
->sock
== client
->sock
)
761 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
763 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
764 stream_put(s
, ¬e
, sizeof(note
));
765 stream_putl(s
, rule
->rule
.seq
);
766 stream_putl(s
, rule
->rule
.priority
);
767 stream_putl(s
, rule
->rule
.unique
);
769 stream_putl(s
, rule
->ifp
->ifindex
);
773 stream_putw_at(s
, 0, stream_get_endp(s
));
775 zserv_send_message(client
, s
);
778 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
779 enum zapi_ipset_notify_owner note
)
781 struct listnode
*node
;
782 struct zserv
*client
;
785 if (IS_ZEBRA_DEBUG_PACKET
)
786 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
789 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
790 if (ipset
->sock
== client
->sock
)
797 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
799 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
800 stream_put(s
, ¬e
, sizeof(note
));
801 stream_putl(s
, ipset
->unique
);
802 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
803 stream_putw_at(s
, 0, stream_get_endp(s
));
805 zserv_send_message(client
, s
);
808 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
809 enum zapi_ipset_entry_notify_owner note
)
811 struct listnode
*node
;
812 struct zserv
*client
;
815 if (IS_ZEBRA_DEBUG_PACKET
)
816 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
819 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
820 if (ipset
->sock
== client
->sock
)
827 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
829 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
830 stream_put(s
, ¬e
, sizeof(note
));
831 stream_putl(s
, ipset
->unique
);
832 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
833 stream_putw_at(s
, 0, stream_get_endp(s
));
835 zserv_send_message(client
, s
);
838 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
839 enum zapi_iptable_notify_owner note
)
841 struct listnode
*node
;
842 struct zserv
*client
;
845 if (IS_ZEBRA_DEBUG_PACKET
)
846 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
849 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
850 if (iptable
->sock
== client
->sock
)
857 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
859 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
860 stream_put(s
, ¬e
, sizeof(note
));
861 stream_putl(s
, iptable
->unique
);
862 stream_putw_at(s
, 0, stream_get_endp(s
));
864 zserv_send_message(client
, s
);
867 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
868 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
873 /* Check this client need interface information. */
874 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
877 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
880 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
882 /* Prefix information. */
883 stream_putc(s
, p
->family
);
884 blen
= prefix_blen(p
);
885 stream_put(s
, &p
->u
.prefix
, blen
);
886 stream_putc(s
, p
->prefixlen
);
888 /* Write packet size. */
889 stream_putw_at(s
, 0, stream_get_endp(s
));
891 return zserv_send_message(client
, s
);
895 * Function used by Zebra to send a PW status update to LDP daemon
897 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
899 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
901 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
902 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
903 stream_putl(s
, pw
->ifindex
);
904 stream_putl(s
, pw
->status
);
906 /* Put length at the first point of the stream. */
907 stream_putw_at(s
, 0, stream_get_endp(s
));
909 return zserv_send_message(client
, s
);
912 /* Send response to a get label chunk request to client */
913 static int zsend_assign_label_chunk_response(struct zserv
*client
,
915 struct label_manager_chunk
*lmc
)
918 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
920 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
924 stream_putc(s
, lmc
->proto
);
926 stream_putw(s
, lmc
->instance
);
928 stream_putc(s
, lmc
->keep
);
929 /* start and end labels */
930 stream_putl(s
, lmc
->start
);
931 stream_putl(s
, lmc
->end
);
934 /* Write packet size. */
935 stream_putw_at(s
, 0, stream_get_endp(s
));
937 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
942 /* Send response to a label manager connect request to client */
943 static int zsend_label_manager_connect_response(struct zserv
*client
,
945 unsigned short result
)
948 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
950 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
953 stream_putc(s
, client
->proto
);
956 stream_putw(s
, client
->instance
);
959 stream_putc(s
, result
);
961 /* Write packet size. */
962 stream_putw_at(s
, 0, stream_get_endp(s
));
964 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
970 /* Send response to a get table chunk request to client */
971 static int zsend_assign_table_chunk_response(struct zserv
*client
,
973 struct table_manager_chunk
*tmc
)
975 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
977 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
980 /* start and end labels */
981 stream_putl(s
, tmc
->start
);
982 stream_putl(s
, tmc
->end
);
985 /* Write packet size. */
986 stream_putw_at(s
, 0, stream_get_endp(s
));
988 return zserv_send_message(client
, s
);
991 static int zsend_table_manager_connect_response(struct zserv
*client
,
995 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
997 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1000 stream_putc(s
, result
);
1002 stream_putw_at(s
, 0, stream_get_endp(s
));
1004 return zserv_send_message(client
, s
);
1007 /* Inbound message handling ------------------------------------------------ */
1010 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1011 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1012 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1013 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1016 /* Nexthop register */
1017 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1022 unsigned short l
= 0;
1024 uint16_t type
= cmd2type
[hdr
->command
];
1026 if (IS_ZEBRA_DEBUG_NHT
)
1028 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1029 zebra_route_string(client
->proto
), hdr
->length
,
1030 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1035 client
->nh_reg_time
= monotime(NULL
);
1037 while (l
< hdr
->length
) {
1038 STREAM_GETC(s
, flags
);
1039 STREAM_GETW(s
, p
.family
);
1040 STREAM_GETC(s
, p
.prefixlen
);
1042 if (p
.family
== AF_INET
) {
1043 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1045 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1046 __PRETTY_FUNCTION__
, p
.prefixlen
);
1049 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1050 l
+= IPV4_MAX_BYTELEN
;
1051 } else if (p
.family
== AF_INET6
) {
1052 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1054 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1055 __PRETTY_FUNCTION__
, p
.prefixlen
);
1058 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1059 l
+= IPV6_MAX_BYTELEN
;
1062 ZEBRA_ERR_UNKNOWN_FAMILY
,
1063 "rnh_register: Received unknown family type %d\n",
1067 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
);
1068 if (type
== RNH_NEXTHOP_TYPE
) {
1070 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1071 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1073 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1074 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1075 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1077 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1078 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1080 && CHECK_FLAG(rnh
->flags
,
1081 ZEBRA_NHT_EXACT_MATCH
))
1082 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1085 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1086 /* Anything not AF_INET/INET6 has been filtered out above */
1087 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
, &p
);
1094 /* Nexthop register */
1095 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1100 unsigned short l
= 0;
1101 uint16_t type
= cmd2type
[hdr
->command
];
1103 if (IS_ZEBRA_DEBUG_NHT
)
1105 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1106 zebra_route_string(client
->proto
), hdr
->length
,
1111 while (l
< hdr
->length
) {
1114 STREAM_GETC(s
, flags
);
1116 goto stream_failure
;
1118 STREAM_GETW(s
, p
.family
);
1119 STREAM_GETC(s
, p
.prefixlen
);
1121 if (p
.family
== AF_INET
) {
1122 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1124 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1125 __PRETTY_FUNCTION__
, p
.prefixlen
);
1128 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1129 l
+= IPV4_MAX_BYTELEN
;
1130 } else if (p
.family
== AF_INET6
) {
1131 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1133 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1134 __PRETTY_FUNCTION__
, p
.prefixlen
);
1137 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1138 l
+= IPV6_MAX_BYTELEN
;
1141 ZEBRA_ERR_UNKNOWN_FAMILY
,
1142 "rnh_register: Received unknown family type %d\n",
1146 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1148 client
->nh_dereg_time
= monotime(NULL
);
1149 zebra_remove_rnh_client(rnh
, client
, type
);
1156 #define ZEBRA_MIN_FEC_LENGTH 5
1159 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1162 unsigned short l
= 0;
1165 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1168 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1173 * The minimum amount of data that can be sent for one fec
1176 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1178 ZEBRA_ERR_IRDP_LEN_MISMATCH
,
1179 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1184 while (l
< hdr
->length
) {
1185 STREAM_GETW(s
, flags
);
1186 memset(&p
, 0, sizeof(p
));
1187 STREAM_GETW(s
, p
.family
);
1188 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1190 ZEBRA_ERR_UNKNOWN_FAMILY
,
1191 "fec_register: Received unknown family type %d\n",
1195 STREAM_GETC(s
, p
.prefixlen
);
1196 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1197 || (p
.family
== AF_INET6
1198 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1200 "%s: Specified prefix hdr->length: %d is to long for %d",
1201 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1205 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1206 l
+= PSIZE(p
.prefixlen
);
1207 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1208 STREAM_GETL(s
, label_index
);
1211 label_index
= MPLS_INVALID_LABEL_INDEX
;
1212 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1219 /* FEC unregister */
1220 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1223 unsigned short l
= 0;
1228 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1233 * The minimum amount of data that can be sent for one
1234 * fec unregistration
1236 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1238 ZEBRA_ERR_IRDP_LEN_MISMATCH
,
1239 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1244 while (l
< hdr
->length
) {
1245 STREAM_GETW(s
, flags
);
1247 goto stream_failure
;
1249 memset(&p
, 0, sizeof(p
));
1250 STREAM_GETW(s
, p
.family
);
1251 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1253 ZEBRA_ERR_UNKNOWN_FAMILY
,
1254 "fec_unregister: Received unknown family type %d\n",
1258 STREAM_GETC(s
, p
.prefixlen
);
1259 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1260 || (p
.family
== AF_INET6
1261 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1263 "%s: Received prefix hdr->length %d which is greater than %d can support",
1264 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1268 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1269 l
+= PSIZE(p
.prefixlen
);
1270 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1279 * Register zebra server interface information.
1280 * Send current all interface and address information.
1282 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1285 struct interface
*ifp
;
1287 /* Interface information is needed. */
1288 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1290 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1291 FOR_ALL_INTERFACES (vrf
, ifp
) {
1292 /* Skip pseudo interface. */
1293 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1296 zsend_interface_add(client
, ifp
);
1297 zsend_interface_addresses(client
, ifp
);
1302 /* Unregister zebra server interface information. */
1303 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1305 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1308 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1309 const unsigned int nexthop_num
)
1311 if (nexthop_num
> multipath_num
) {
1312 char buff
[PREFIX2STR_BUFFER
];
1314 prefix2str(p
, buff
, sizeof(buff
));
1316 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1317 caller
, buff
, nexthop_num
, multipath_num
);
1321 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1324 struct zapi_route api
;
1325 struct zapi_nexthop
*api_nh
;
1327 struct prefix_ipv6
*src_p
= NULL
;
1328 struct route_entry
*re
;
1329 struct nexthop
*nexthop
= NULL
;
1331 vrf_id_t vrf_id
= 0;
1332 struct ipaddr vtep_ip
;
1335 if (zapi_route_decode(s
, &api
) < 0) {
1336 if (IS_ZEBRA_DEBUG_RECV
)
1337 zlog_debug("%s: Unable to decode zapi_route sent",
1338 __PRETTY_FUNCTION__
);
1342 if (IS_ZEBRA_DEBUG_RECV
) {
1343 char buf_prefix
[PREFIX_STRLEN
];
1345 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1346 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1347 __func__
, buf_prefix
,
1348 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1353 /* Allocate new route. */
1354 vrf_id
= zvrf_id(zvrf
);
1355 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1356 re
->type
= api
.type
;
1357 re
->instance
= api
.instance
;
1358 re
->flags
= api
.flags
;
1359 re
->uptime
= time(NULL
);
1360 re
->vrf_id
= vrf_id
;
1361 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1362 re
->table
= api
.tableid
;
1364 re
->table
= zvrf
->table_id
;
1367 * TBD should _all_ of the nexthop add operations use
1368 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1369 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1371 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1372 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1373 api_nh
= &api
.nexthops
[i
];
1374 ifindex_t ifindex
= 0;
1376 if (IS_ZEBRA_DEBUG_RECV
)
1377 zlog_debug("nh type %d", api_nh
->type
);
1379 switch (api_nh
->type
) {
1380 case NEXTHOP_TYPE_IFINDEX
:
1381 nexthop
= route_entry_nexthop_ifindex_add(
1382 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1384 case NEXTHOP_TYPE_IPV4
:
1385 if (IS_ZEBRA_DEBUG_RECV
) {
1386 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1388 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1389 nhbuf
, INET6_ADDRSTRLEN
);
1390 zlog_debug("%s: nh=%s, vrf_id=%d",
1394 nexthop
= route_entry_nexthop_ipv4_add(
1395 re
, &api_nh
->gate
.ipv4
, NULL
,
1398 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1400 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1401 if (CHECK_FLAG(api
.flags
,
1402 ZEBRA_FLAG_EVPN_ROUTE
)) {
1403 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1405 ifindex
= api_nh
->ifindex
;
1408 if (IS_ZEBRA_DEBUG_RECV
) {
1409 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1411 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1412 nhbuf
, INET6_ADDRSTRLEN
);
1414 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1415 __func__
, nhbuf
, api_nh
->vrf_id
,
1416 re
->vrf_id
, ifindex
);
1418 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1419 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1422 /* if this an EVPN route entry,
1423 * program the nh as neigh
1425 if (CHECK_FLAG(api
.flags
,
1426 ZEBRA_FLAG_EVPN_ROUTE
)) {
1427 SET_FLAG(nexthop
->flags
,
1428 NEXTHOP_FLAG_EVPN_RVTEP
);
1429 vtep_ip
.ipa_type
= IPADDR_V4
;
1430 memcpy(&(vtep_ip
.ipaddr_v4
),
1431 &(api_nh
->gate
.ipv4
),
1432 sizeof(struct in_addr
));
1433 zebra_vxlan_evpn_vrf_route_add(
1434 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1438 case NEXTHOP_TYPE_IPV6
:
1439 nexthop
= route_entry_nexthop_ipv6_add(
1440 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1442 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1443 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1444 if (CHECK_FLAG(api
.flags
,
1445 ZEBRA_FLAG_EVPN_ROUTE
)) {
1446 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1448 ifindex
= api_nh
->ifindex
;
1451 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1452 re
, &api_nh
->gate
.ipv6
, ifindex
,
1455 /* if this an EVPN route entry,
1456 * program the nh as neigh
1458 if (CHECK_FLAG(api
.flags
,
1459 ZEBRA_FLAG_EVPN_ROUTE
)) {
1460 SET_FLAG(nexthop
->flags
,
1461 NEXTHOP_FLAG_EVPN_RVTEP
);
1462 vtep_ip
.ipa_type
= IPADDR_V6
;
1463 memcpy(&vtep_ip
.ipaddr_v6
,
1464 &(api_nh
->gate
.ipv6
),
1465 sizeof(struct in6_addr
));
1466 zebra_vxlan_evpn_vrf_route_add(
1467 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1471 case NEXTHOP_TYPE_BLACKHOLE
:
1472 nexthop
= route_entry_nexthop_blackhole_add(
1473 re
, api_nh
->bh_type
);
1479 "%s: Nexthops Specified: %d but we failed to properly create one",
1480 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1481 nexthops_free(re
->ng
.nexthop
);
1482 XFREE(MTYPE_RE
, re
);
1485 /* MPLS labels for BGP-LU or Segment Routing */
1486 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1487 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1488 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1489 enum lsp_types_t label_type
;
1492 lsp_type_from_re_type(client
->proto
);
1494 if (IS_ZEBRA_DEBUG_RECV
) {
1496 "%s: adding %d labels of type %d (1st=%u)",
1497 __func__
, api_nh
->label_num
,
1498 label_type
, api_nh
->labels
[0]);
1501 nexthop_add_labels(nexthop
, label_type
,
1503 &api_nh
->labels
[0]);
1507 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1508 re
->distance
= api
.distance
;
1509 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1510 re
->metric
= api
.metric
;
1511 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1513 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1516 afi
= family2afi(api
.prefix
.family
);
1517 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1518 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1519 __PRETTY_FUNCTION__
);
1520 nexthops_free(re
->ng
.nexthop
);
1521 XFREE(MTYPE_RE
, re
);
1524 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1525 src_p
= &api
.src_prefix
;
1527 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1530 switch (api
.prefix
.family
) {
1533 client
->v4_route_add_cnt
++;
1535 client
->v4_route_upd8_cnt
++;
1539 client
->v6_route_add_cnt
++;
1541 client
->v6_route_upd8_cnt
++;
1546 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1549 struct zapi_route api
;
1551 struct prefix_ipv6
*src_p
= NULL
;
1555 if (zapi_route_decode(s
, &api
) < 0)
1558 afi
= family2afi(api
.prefix
.family
);
1559 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1560 zlog_warn("%s: Received a src prefix while afi is not v6",
1561 __PRETTY_FUNCTION__
);
1564 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1565 src_p
= &api
.src_prefix
;
1567 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1568 table_id
= api
.tableid
;
1570 table_id
= zvrf
->table_id
;
1572 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1573 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1574 api
.distance
, false);
1577 switch (api
.prefix
.family
) {
1579 client
->v4_route_del_cnt
++;
1582 client
->v6_route_del_cnt
++;
1587 /* MRIB Nexthop lookup for IPv4. */
1588 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1590 struct in_addr addr
;
1591 struct route_entry
*re
;
1593 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1594 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1595 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1601 /* Register zebra server router-id information. Send current router-id */
1602 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1606 /* Router-id information is needed. */
1607 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1609 router_id_get(&p
, zvrf_id(zvrf
));
1611 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1614 /* Unregister zebra server router-id information. */
1615 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1617 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1620 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1622 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1624 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1625 stream_putc(s
, mpls_enabled
);
1626 stream_putl(s
, multipath_num
);
1628 stream_putw_at(s
, 0, stream_get_endp(s
));
1629 zserv_send_message(client
, s
);
1632 /* Tie up route-type and client->sock */
1633 static void zread_hello(ZAPI_HANDLER_ARGS
)
1635 /* type of protocol (lib/zebra.h) */
1637 unsigned short instance
;
1640 STREAM_GETC(msg
, proto
);
1641 STREAM_GETW(msg
, instance
);
1642 STREAM_GETC(msg
, notify
);
1644 client
->notify_owner
= true;
1646 /* accept only dynamic routing protocols */
1647 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1649 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1650 client
->sock
, zebra_route_string(proto
),
1653 zlog_notice("client protocol instance %d", instance
);
1655 client
->proto
= proto
;
1656 client
->instance
= instance
;
1659 zsend_capabilities(client
, zvrf
);
1664 /* Unregister all information in a VRF. */
1665 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1670 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1671 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1672 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1673 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1674 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1675 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1678 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1681 enum lsp_types_t type
;
1682 struct prefix prefix
;
1683 enum nexthop_types_t gtype
;
1686 mpls_label_t in_label
, out_label
;
1689 /* Get input stream. */
1693 STREAM_GETC(s
, type
);
1694 STREAM_GETL(s
, prefix
.family
);
1695 switch (prefix
.family
) {
1697 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1698 STREAM_GETC(s
, prefix
.prefixlen
);
1699 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1701 "%s: Specified prefix length %d is greater than a v4 address can support",
1702 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1705 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1708 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1709 STREAM_GETC(s
, prefix
.prefixlen
);
1710 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1712 "%s: Specified prefix length %d is greater than a v6 address can support",
1713 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1716 STREAM_GET(&gate
.ipv6
, s
, 16);
1719 zlog_warn("%s: Specified AF %d is not supported for this call",
1720 __PRETTY_FUNCTION__
, prefix
.family
);
1723 STREAM_GETL(s
, ifindex
);
1724 STREAM_GETC(s
, distance
);
1725 STREAM_GETL(s
, in_label
);
1726 STREAM_GETL(s
, out_label
);
1728 switch (prefix
.family
) {
1731 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1733 gtype
= NEXTHOP_TYPE_IPV4
;
1737 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1739 gtype
= NEXTHOP_TYPE_IPV6
;
1748 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1749 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1751 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1752 distance
, out_label
);
1753 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1754 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1755 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1756 distance
, out_label
);
1762 /* Send response to a table manager connect request to client */
1763 static void zread_table_manager_connect(struct zserv
*client
,
1764 struct stream
*msg
, vrf_id_t vrf_id
)
1773 STREAM_GETC(s
, proto
);
1774 STREAM_GETW(s
, instance
);
1776 /* accept only dynamic routing protocols */
1777 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1778 flog_err(ZEBRA_ERR_TM_WRONG_PROTO
,
1779 "client %d has wrong protocol %s", client
->sock
,
1780 zebra_route_string(proto
));
1781 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1784 zlog_notice("client %d with vrf %u instance %u connected as %s",
1785 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1786 client
->proto
= proto
;
1787 client
->instance
= instance
;
1790 * Release previous labels of same protocol and instance.
1791 * This is done in case it restarted from an unexpected shutdown.
1793 release_daemon_table_chunks(client
);
1795 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1801 static void zread_label_manager_connect(struct zserv
*client
,
1802 struct stream
*msg
, vrf_id_t vrf_id
)
1805 /* type of protocol (lib/zebra.h) */
1807 unsigned short instance
;
1809 /* Get input stream. */
1813 STREAM_GETC(s
, proto
);
1814 STREAM_GETW(s
, instance
);
1816 /* accept only dynamic routing protocols */
1817 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1818 flog_err(ZEBRA_ERR_TM_WRONG_PROTO
,
1819 "client %d has wrong protocol %s", client
->sock
,
1820 zebra_route_string(proto
));
1821 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1824 zlog_notice("client %d with vrf %u instance %u connected as %s",
1825 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1826 client
->proto
= proto
;
1827 client
->instance
= instance
;
1830 * Release previous labels of same protocol and instance.
1831 * This is done in case it restarted from an unexpected shutdown.
1833 release_daemon_label_chunks(client
);
1836 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1837 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1838 /* send response back */
1839 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1844 static int msg_client_id_mismatch(const char *op
, struct zserv
*client
,
1845 uint8_t proto
, unsigned int instance
)
1847 if (proto
!= client
->proto
) {
1848 flog_err(ZEBRA_ERR_PROTO_OR_INSTANCE_MISMATCH
,
1849 "%s: msg vs client proto mismatch, client=%u msg=%u",
1850 op
, client
->proto
, proto
);
1851 /* TODO: fail when BGP sets proto and instance */
1855 if (instance
!= client
->instance
) {
1857 ZEBRA_ERR_PROTO_OR_INSTANCE_MISMATCH
,
1858 "%s: msg vs client instance mismatch, client=%u msg=%u",
1859 op
, client
->instance
, instance
);
1860 /* TODO: fail when BGP sets proto and instance */
1867 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1873 struct label_manager_chunk
*lmc
;
1875 unsigned short instance
;
1877 /* Get input stream. */
1881 STREAM_GETC(s
, proto
);
1882 STREAM_GETW(s
, instance
);
1883 STREAM_GETC(s
, keep
);
1884 STREAM_GETL(s
, size
);
1886 /* detect client vs message (proto,instance) mismatch */
1887 if (msg_client_id_mismatch("Get-label-chunk", client
, proto
, instance
))
1890 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
1893 ZEBRA_ERR_LM_CANNOT_ASSIGN_CHUNK
,
1894 "Unable to assign Label Chunk of size %u to %s instance %u",
1895 size
, zebra_route_string(client
->proto
),
1898 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1899 lmc
->start
, lmc
->end
,
1900 zebra_route_string(client
->proto
), client
->instance
);
1901 /* send response back */
1902 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1908 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1911 uint32_t start
, end
;
1913 unsigned short instance
;
1915 /* Get input stream. */
1919 STREAM_GETC(s
, proto
);
1920 STREAM_GETW(s
, instance
);
1921 STREAM_GETL(s
, start
);
1922 STREAM_GETL(s
, end
);
1924 /* detect client vs message (proto,instance) mismatch */
1925 if (msg_client_id_mismatch("Release-label-chunk", client
, proto
,
1929 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
1934 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1936 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1937 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
1938 client
->is_synchronous
= 1;
1940 /* external label manager */
1942 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1944 /* this is a label manager */
1946 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
1947 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1949 /* Sanity: don't allow 'unidentified' requests */
1950 if (!client
->proto
) {
1952 ZEBRA_ERR_LM_ALIENS
,
1953 "Got label request from an unidentified client");
1956 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1957 zread_get_label_chunk(client
, msg
,
1959 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1960 zread_release_label_chunk(client
, msg
);
1965 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1970 struct table_manager_chunk
*tmc
;
1972 /* Get input stream. */
1976 STREAM_GETL(s
, size
);
1978 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1980 flog_err(ZEBRA_ERR_TM_CANNOT_ASSIGN_CHUNK
,
1981 "%s: Unable to assign Table Chunk of size %u",
1984 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1986 /* send response back */
1987 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
1993 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
1996 uint32_t start
, end
;
1998 /* Get input stream. */
2002 STREAM_GETL(s
, start
);
2003 STREAM_GETL(s
, end
);
2005 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2011 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2013 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2014 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2015 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2017 /* Sanity: don't allow 'unidentified' requests */
2018 if (!client
->proto
) {
2020 ZEBRA_ERR_TM_ALIENS
,
2021 "Got table request from an unidentified client");
2024 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2025 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2026 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2027 zread_release_table_chunk(client
, msg
);
2031 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2034 char ifname
[IF_NAMESIZE
];
2038 union g_addr nexthop
;
2039 uint32_t local_label
;
2040 uint32_t remote_label
;
2042 union pw_protocol_fields data
;
2044 struct zebra_pw
*pw
;
2046 /* Get input stream. */
2050 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2051 STREAM_GETL(s
, ifindex
);
2052 STREAM_GETL(s
, type
);
2056 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2059 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2064 STREAM_GETL(s
, local_label
);
2065 STREAM_GETL(s
, remote_label
);
2066 STREAM_GETC(s
, flags
);
2067 STREAM_GET(&data
, s
, sizeof(data
));
2068 protocol
= client
->proto
;
2070 pw
= zebra_pw_find(zvrf
, ifname
);
2071 switch (hdr
->command
) {
2074 zlog_warn("%s: pseudowire %s already exists [%s]",
2076 zserv_command_string(hdr
->command
));
2080 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2082 case ZEBRA_PW_DELETE
:
2084 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2085 ifname
, zserv_command_string(hdr
->command
));
2089 zebra_pw_del(zvrf
, pw
);
2092 case ZEBRA_PW_UNSET
:
2094 zlog_warn("%s: pseudowire %s not found [%s]", __func__
,
2095 ifname
, zserv_command_string(hdr
->command
));
2099 switch (hdr
->command
) {
2103 case ZEBRA_PW_UNSET
:
2108 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2109 remote_label
, flags
, &data
);
2117 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2119 struct interface
*master
;
2120 struct interface
*slave
;
2121 struct stream
*s
= msg
;
2125 STREAM_GETL(s
, vrf_id
);
2126 STREAM_GETL(s
, ifindex
);
2127 master
= if_lookup_by_index(ifindex
, vrf_id
);
2129 STREAM_GETL(s
, vrf_id
);
2130 STREAM_GETL(s
, ifindex
);
2131 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2133 if (!master
|| !slave
)
2136 kernel_interface_set_master(master
, slave
);
2143 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2145 struct interface
*ifp
;
2146 mpls_label_t nlabel
;
2149 struct zebra_vrf
*def_zvrf
;
2150 enum lsp_types_t ltype
;
2153 STREAM_GETL(s
, nlabel
);
2154 STREAM_GETC(s
, afi
);
2155 if (nlabel
== zvrf
->label
[afi
]) {
2157 * Nothing to do here move along
2162 STREAM_GETC(s
, ltype
);
2164 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2165 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2167 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2170 zlog_debug("Unable to find specified Interface for %s",
2175 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2177 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2181 really_remove
= true;
2182 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2183 if (scrubber
== afi
)
2186 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2189 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2190 really_remove
= false;
2196 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2197 NEXTHOP_TYPE_IFINDEX
, NULL
,
2201 if (nlabel
!= MPLS_LABEL_NONE
)
2202 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2203 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2204 NULL
, ifp
->ifindex
);
2206 zvrf
->label
[afi
] = nlabel
;
2211 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2213 struct zebra_pbr_rule zpr
;
2219 STREAM_GETL(s
, total
);
2221 for (i
= 0; i
< total
; i
++) {
2222 memset(&zpr
, 0, sizeof(zpr
));
2224 zpr
.sock
= client
->sock
;
2225 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2226 STREAM_GETL(s
, zpr
.rule
.seq
);
2227 STREAM_GETL(s
, zpr
.rule
.priority
);
2228 STREAM_GETL(s
, zpr
.rule
.unique
);
2229 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2230 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2231 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2232 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2233 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2234 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2235 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2236 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2237 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2238 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2239 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2240 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2241 STREAM_GETL(s
, ifindex
);
2244 zpr
.ifp
= if_lookup_by_index_per_ns(
2248 zlog_debug("Failed to lookup ifindex: %u",
2254 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2255 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2257 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2258 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2260 if (zpr
.rule
.filter
.src_port
)
2261 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2263 if (zpr
.rule
.filter
.dst_port
)
2264 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2266 if (zpr
.rule
.filter
.fwmark
)
2267 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2269 if (hdr
->command
== ZEBRA_RULE_ADD
)
2270 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2272 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2279 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2281 struct zebra_pbr_ipset zpi
;
2286 STREAM_GETL(s
, total
);
2288 for (i
= 0; i
< total
; i
++) {
2289 memset(&zpi
, 0, sizeof(zpi
));
2291 zpi
.sock
= client
->sock
;
2292 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2293 STREAM_GETL(s
, zpi
.unique
);
2294 STREAM_GETL(s
, zpi
.type
);
2295 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2297 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2298 zebra_pbr_create_ipset(zvrf
->zns
, &zpi
);
2300 zebra_pbr_destroy_ipset(zvrf
->zns
, &zpi
);
2307 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2309 struct zebra_pbr_ipset_entry zpi
;
2310 struct zebra_pbr_ipset ipset
;
2315 STREAM_GETL(s
, total
);
2317 for (i
= 0; i
< total
; i
++) {
2318 memset(&zpi
, 0, sizeof(zpi
));
2319 memset(&ipset
, 0, sizeof(ipset
));
2321 zpi
.sock
= client
->sock
;
2322 STREAM_GETL(s
, zpi
.unique
);
2323 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2324 STREAM_GETC(s
, zpi
.src
.family
);
2325 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2326 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2327 STREAM_GETC(s
, zpi
.dst
.family
);
2328 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2329 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2331 STREAM_GETW(s
, zpi
.src_port_min
);
2332 STREAM_GETW(s
, zpi
.src_port_max
);
2333 STREAM_GETW(s
, zpi
.dst_port_min
);
2334 STREAM_GETW(s
, zpi
.dst_port_max
);
2335 STREAM_GETC(s
, zpi
.proto
);
2336 if (!is_default_prefix(&zpi
.src
))
2337 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2339 if (!is_default_prefix(&zpi
.dst
))
2340 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2341 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2342 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2343 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2344 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2345 if (zpi
.dst_port_max
!= 0)
2346 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2347 if (zpi
.src_port_max
!= 0)
2348 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2350 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2352 /* calculate backpointer */
2353 zpi
.backpointer
= zebra_pbr_lookup_ipset_pername(
2354 zvrf
->zns
, ipset
.ipset_name
);
2355 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2356 zebra_pbr_add_ipset_entry(zvrf
->zns
, &zpi
);
2358 zebra_pbr_del_ipset_entry(zvrf
->zns
, &zpi
);
2365 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2367 struct zebra_pbr_iptable zpi
;
2372 memset(&zpi
, 0, sizeof(zpi
));
2374 zpi
.interface_name_list
= list_new();
2375 zpi
.sock
= client
->sock
;
2376 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2377 STREAM_GETL(s
, zpi
.unique
);
2378 STREAM_GETL(s
, zpi
.type
);
2379 STREAM_GETL(s
, zpi
.filter_bm
);
2380 STREAM_GETL(s
, zpi
.action
);
2381 STREAM_GETL(s
, zpi
.fwmark
);
2382 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2383 STREAM_GETW(s
, zpi
.pkt_len_min
);
2384 STREAM_GETW(s
, zpi
.pkt_len_max
);
2385 STREAM_GETW(s
, zpi
.tcp_flags
);
2386 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2387 STREAM_GETC(s
, zpi
.dscp_value
);
2388 STREAM_GETC(s
, zpi
.fragment
);
2389 STREAM_GETL(s
, zpi
.nb_interface
);
2390 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2392 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2393 zebra_pbr_add_iptable(zvrf
->zns
, &zpi
);
2395 zebra_pbr_del_iptable(zvrf
->zns
, &zpi
);
2400 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2401 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2402 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2403 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2404 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2405 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2406 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2407 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2408 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2409 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2410 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2411 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2412 [ZEBRA_HELLO
] = zread_hello
,
2413 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2414 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2415 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2416 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2417 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2418 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2419 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2421 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2422 #endif /* HAVE_BFDD */
2423 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2424 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2425 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2426 #if defined(HAVE_RTADV)
2427 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2428 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2430 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2431 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2433 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2434 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2435 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2436 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2437 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2438 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2439 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2440 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2441 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2442 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2443 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2444 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2445 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2446 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2447 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2448 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2449 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2450 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2451 [ZEBRA_PW_SET
] = zread_pseudowire
,
2452 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2453 [ZEBRA_RULE_ADD
] = zread_rule
,
2454 [ZEBRA_RULE_DELETE
] = zread_rule
,
2455 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2456 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2457 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2458 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2459 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2460 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2461 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2462 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2463 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2466 #if defined(HANDLE_ZAPI_FUZZING)
2467 extern struct zebra_privs_t zserv_privs
;
2469 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2471 char fname
[MAXPATHLEN
];
2472 struct stream
*copy
;
2475 copy
= stream_dup(orig
);
2476 stream_set_getp(copy
, 0);
2478 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2480 frr_elevate_privs(&zserv_privs
) {
2481 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2483 stream_flush(copy
, fd
);
2489 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2492 struct zebra_vrf
*zvrf
;
2494 zapi_parse_header(msg
, &hdr
);
2496 #if defined(HANDLE_ZAPI_FUZZING)
2497 zserv_write_incoming(msg
, hdr
.command
);
2500 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2503 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2505 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2506 zlog_warn("ZAPI message specifies unknown VRF: %d",
2511 if (hdr
.command
>= array_size(zserv_handlers
)
2512 || zserv_handlers
[hdr
.command
] == NULL
)
2513 zlog_info("Zebra received unknown command %d", hdr
.command
);
2515 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);