2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/rib.h"
46 #include "zebra/zebra_memory.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/router-id.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/debug.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/rt_netlink.h"
54 #include "zebra/interface.h"
55 #include "zebra/zebra_ptm.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/zebra_mpls.h"
58 #include "zebra/zebra_mroute.h"
59 #include "zebra/label_manager.h"
60 #include "zebra/zebra_vxlan.h"
62 #include "zebra/zebra_pbr.h"
63 #include "zebra/table_manager.h"
64 #include "zebra/zapi_msg.h"
65 #include "zebra/zebra_errors.h"
67 /* Encoding helpers -------------------------------------------------------- */
69 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
71 /* Interface information. */
72 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
73 stream_putl(s
, ifp
->ifindex
);
74 stream_putc(s
, ifp
->status
);
75 stream_putq(s
, ifp
->flags
);
76 stream_putc(s
, ifp
->ptm_enable
);
77 stream_putc(s
, ifp
->ptm_status
);
78 stream_putl(s
, ifp
->metric
);
79 stream_putl(s
, ifp
->speed
);
80 stream_putl(s
, ifp
->mtu
);
81 stream_putl(s
, ifp
->mtu6
);
82 stream_putl(s
, ifp
->bandwidth
);
83 stream_putl(s
, ifp
->ll_type
);
84 stream_putl(s
, ifp
->hw_addr_len
);
86 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
88 /* Then, Traffic Engineering parameters if any */
89 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
91 zebra_interface_link_params_write(s
, ifp
);
95 /* Write packet size. */
96 stream_putw_at(s
, 0, stream_get_endp(s
));
99 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
101 struct vrf_data data
;
102 const char *netns_name
= zvrf_ns_name(zvrf
);
104 data
.l
.table_id
= zvrf
->table_id
;
107 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
110 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
111 /* Pass the tableid and the netns NAME */
112 stream_put(s
, &data
, sizeof(struct vrf_data
));
113 /* Interface information. */
114 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
115 /* Write packet size. */
116 stream_putw_at(s
, 0, stream_get_endp(s
));
119 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
121 stream_putc(s
, nexthop
->type
);
122 switch (nexthop
->type
) {
123 case NEXTHOP_TYPE_IPV4
:
124 case NEXTHOP_TYPE_IPV4_IFINDEX
:
125 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
126 stream_putl(s
, nexthop
->ifindex
);
128 case NEXTHOP_TYPE_IPV6
:
129 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
131 case NEXTHOP_TYPE_IPV6_IFINDEX
:
132 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
133 stream_putl(s
, nexthop
->ifindex
);
135 case NEXTHOP_TYPE_IFINDEX
:
136 stream_putl(s
, nexthop
->ifindex
);
145 /* Send handlers ----------------------------------------------------------- */
147 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
149 * This function is called in the following situations:
150 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
152 * - at startup, when zebra figures out the available interfaces
153 * - when an interface is added (where support for
154 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
155 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
158 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
160 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
162 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
163 zserv_encode_interface(s
, ifp
);
166 return zserv_send_message(client
, s
);
169 /* Interface deletion from zebra daemon. */
170 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
172 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
174 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
175 zserv_encode_interface(s
, ifp
);
178 return zserv_send_message(client
, s
);
181 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
183 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
185 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
186 zserv_encode_vrf(s
, zvrf
);
188 client
->vrfadd_cnt
++;
189 return zserv_send_message(client
, s
);
192 /* VRF deletion from zebra daemon. */
193 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
196 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
198 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
199 zserv_encode_vrf(s
, zvrf
);
201 client
->vrfdel_cnt
++;
202 return zserv_send_message(client
, s
);
205 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
207 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
209 /* Check this client need interface information. */
210 if (!client
->ifinfo
) {
215 if (!ifp
->link_params
) {
220 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
222 /* Add Interface Index */
223 stream_putl(s
, ifp
->ifindex
);
225 /* Then TE Link Parameters */
226 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
231 /* Write packet size. */
232 stream_putw_at(s
, 0, stream_get_endp(s
));
234 return zserv_send_message(client
, s
);
237 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
238 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
240 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
241 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
242 * from the client, after the ZEBRA_INTERFACE_ADD has been
243 * sent from zebra to the client
244 * - redistribute new address info to all clients in the following situations
245 * - at startup, when zebra figures out the available interfaces
246 * - when an interface is added (where support for
247 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
248 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
250 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
251 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
252 * - when an RTM_NEWADDR message is received from the kernel,
254 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
256 * zsend_interface_address(DELETE)
259 * zebra_interface_address_delete_update
261 * | | if_delete_update
263 * ip_address_uninstall connected_delete_ipv4
264 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
267 * | RTM_NEWADDR on routing/netlink socket
270 * "no ip address A.B.C.D/M [label LINE]"
271 * "no ip address A.B.C.D/M secondary"
272 * ["no ipv6 address X:X::X:X/M"]
275 int zsend_interface_address(int cmd
, struct zserv
*client
,
276 struct interface
*ifp
, struct connected
*ifc
)
280 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
282 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
283 stream_putl(s
, ifp
->ifindex
);
285 /* Interface address flag. */
286 stream_putc(s
, ifc
->flags
);
288 /* Prefix information. */
290 stream_putc(s
, p
->family
);
291 blen
= prefix_blen(p
);
292 stream_put(s
, &p
->u
.prefix
, blen
);
295 * XXX gnu version does not send prefixlen for
296 * ZEBRA_INTERFACE_ADDRESS_DELETE
297 * but zebra_interface_address_delete_read() in the gnu version
300 stream_putc(s
, p
->prefixlen
);
303 p
= ifc
->destination
;
305 stream_put(s
, &p
->u
.prefix
, blen
);
307 stream_put(s
, NULL
, blen
);
309 /* Write packet size. */
310 stream_putw_at(s
, 0, stream_get_endp(s
));
312 client
->connected_rt_add_cnt
++;
313 return zserv_send_message(client
, s
);
316 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
317 struct interface
*ifp
,
318 struct nbr_connected
*ifc
)
321 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
324 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
325 stream_putl(s
, ifp
->ifindex
);
327 /* Prefix information. */
329 stream_putc(s
, p
->family
);
330 blen
= prefix_blen(p
);
331 stream_put(s
, &p
->u
.prefix
, blen
);
334 * XXX gnu version does not send prefixlen for
335 * ZEBRA_INTERFACE_ADDRESS_DELETE
336 * but zebra_interface_address_delete_read() in the gnu version
339 stream_putc(s
, p
->prefixlen
);
341 /* Write packet size. */
342 stream_putw_at(s
, 0, stream_get_endp(s
));
344 return zserv_send_message(client
, s
);
347 /* Interface address addition. */
348 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
349 struct nbr_connected
*ifc
)
351 struct listnode
*node
, *nnode
;
352 struct zserv
*client
;
355 if (IS_ZEBRA_DEBUG_EVENT
) {
356 char buf
[INET6_ADDRSTRLEN
];
360 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
361 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
363 p
->prefixlen
, ifc
->ifp
->name
);
366 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
367 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
371 /* Interface address deletion. */
372 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
373 struct nbr_connected
*ifc
)
375 struct listnode
*node
, *nnode
;
376 struct zserv
*client
;
379 if (IS_ZEBRA_DEBUG_EVENT
) {
380 char buf
[INET6_ADDRSTRLEN
];
384 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
385 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
387 p
->prefixlen
, ifc
->ifp
->name
);
390 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
391 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
395 /* Send addresses on interface to client */
396 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
398 struct listnode
*cnode
, *cnnode
;
400 struct nbr_connected
*nc
;
402 /* Send interface addresses. */
403 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
404 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
407 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
413 /* Send interface neighbors. */
414 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
415 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
424 /* Notify client about interface moving from one VRF to another.
425 * Whether client is interested in old and new VRF is checked by caller.
427 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
430 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
432 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
434 /* Fill in the ifIndex of the interface and its new VRF (id) */
435 stream_putl(s
, ifp
->ifindex
);
436 stream_putl(s
, vrf_id
);
438 /* Write packet size. */
439 stream_putw_at(s
, 0, stream_get_endp(s
));
441 client
->if_vrfchg_cnt
++;
442 return zserv_send_message(client
, s
);
445 /* Add new nbr connected IPv6 address */
446 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
448 struct nbr_connected
*ifc
;
452 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
453 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
455 ifc
= listnode_head(ifp
->nbr_connected
);
458 ifc
= nbr_connected_new();
459 ifc
->address
= prefix_new();
461 listnode_add(ifp
->nbr_connected
, ifc
);
464 prefix_copy(ifc
->address
, &p
);
466 zebra_interface_nbr_address_add_update(ifp
, ifc
);
468 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
471 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
473 struct nbr_connected
*ifc
;
477 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
478 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
480 ifc
= nbr_connected_check(ifp
, &p
);
484 listnode_delete(ifp
->nbr_connected
, ifc
);
486 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
488 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
490 nbr_connected_free(ifc
);
494 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
495 * ZEBRA_INTERFACE_DOWN.
497 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
498 * the clients in one of 2 situations:
499 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
500 * - a vty command modifying the bandwidth of an interface is received.
501 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
503 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
505 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
507 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
508 zserv_encode_interface(s
, ifp
);
510 if (cmd
== ZEBRA_INTERFACE_UP
)
513 client
->ifdown_cnt
++;
515 return zserv_send_message(client
, s
);
518 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
519 const struct prefix
*p
,
520 const struct prefix
*src_p
, struct route_entry
*re
)
522 struct zapi_route api
;
523 struct zapi_nexthop
*api_nh
;
524 struct nexthop
*nexthop
;
528 memset(&api
, 0, sizeof(api
));
529 api
.vrf_id
= re
->vrf_id
;
531 api
.instance
= re
->instance
;
532 api
.flags
= re
->flags
;
534 afi
= family2afi(p
->family
);
537 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
538 client
->redist_v4_add_cnt
++;
540 client
->redist_v4_del_cnt
++;
543 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
544 client
->redist_v6_add_cnt
++;
546 client
->redist_v6_del_cnt
++;
555 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
556 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
560 if (re
->nexthop_active_num
) {
561 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
562 api
.nexthop_num
= re
->nexthop_active_num
;
564 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
565 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
568 api_nh
= &api
.nexthops
[count
];
569 api_nh
->vrf_id
= nexthop
->vrf_id
;
570 api_nh
->type
= nexthop
->type
;
571 switch (nexthop
->type
) {
572 case NEXTHOP_TYPE_BLACKHOLE
:
573 api_nh
->bh_type
= nexthop
->bh_type
;
575 case NEXTHOP_TYPE_IPV4
:
576 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
578 case NEXTHOP_TYPE_IPV4_IFINDEX
:
579 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
580 api_nh
->ifindex
= nexthop
->ifindex
;
582 case NEXTHOP_TYPE_IFINDEX
:
583 api_nh
->ifindex
= nexthop
->ifindex
;
585 case NEXTHOP_TYPE_IPV6
:
586 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
588 case NEXTHOP_TYPE_IPV6_IFINDEX
:
589 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
590 api_nh
->ifindex
= nexthop
->ifindex
;
596 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
597 api
.distance
= re
->distance
;
598 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
599 api
.metric
= re
->metric
;
601 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
604 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
607 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
609 /* Encode route and send. */
610 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
615 if (IS_ZEBRA_DEBUG_SEND
) {
616 char buf_prefix
[PREFIX_STRLEN
];
618 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
620 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
621 __func__
, zserv_command_string(cmd
),
622 zebra_route_string(client
->proto
),
623 zebra_route_string(api
.type
), api
.vrf_id
,
626 return zserv_send_message(client
, s
);
630 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
631 * nexthop is not found on mrib. Returns both route metric and protocol
634 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
636 struct route_entry
*re
,
637 struct zebra_vrf
*zvrf
)
642 struct nexthop
*nexthop
;
644 /* Get output stream. */
645 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
648 /* Fill in result. */
649 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
650 stream_put_in_addr(s
, &addr
);
653 stream_putc(s
, re
->distance
);
654 stream_putl(s
, re
->metric
);
656 /* remember position for nexthop_num */
657 nump
= stream_get_endp(s
);
658 /* reserve room for nexthop_num */
661 * Only non-recursive routes are elegible to resolve the
662 * nexthop we are looking up. Therefore, we will just iterate
663 * over the top chain of nexthops.
665 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
666 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
667 num
+= zserv_encode_nexthop(s
, nexthop
);
669 /* store nexthop_num */
670 stream_putc_at(s
, nump
, num
);
672 stream_putc(s
, 0); /* distance */
673 stream_putl(s
, 0); /* metric */
674 stream_putc(s
, 0); /* nexthop_num */
677 stream_putw_at(s
, 0, stream_get_endp(s
));
679 return zserv_send_message(client
, s
);
683 * Common utility send route notification, called from a path using a
684 * route_entry and from a path using a dataplane context.
686 static int route_notify_internal(const struct prefix
*p
, int type
,
687 uint16_t instance
, vrf_id_t vrf_id
,
689 enum zapi_route_notify_owner note
)
691 struct zserv
*client
;
695 client
= zserv_find_client(type
, instance
);
696 if (!client
|| !client
->notify_owner
) {
697 if (IS_ZEBRA_DEBUG_PACKET
) {
698 char buff
[PREFIX_STRLEN
];
701 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
702 type
, prefix2str(p
, buff
, sizeof(buff
)),
703 table_id
, note
, vrf_id
);
708 if (IS_ZEBRA_DEBUG_PACKET
) {
709 char buff
[PREFIX_STRLEN
];
711 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
712 type
, prefix2str(p
, buff
, sizeof(buff
)),
713 table_id
, note
, vrf_id
);
716 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
719 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
721 stream_put(s
, ¬e
, sizeof(note
));
723 stream_putc(s
, p
->family
);
725 blen
= prefix_blen(p
);
726 stream_putc(s
, p
->prefixlen
);
727 stream_put(s
, &p
->u
.prefix
, blen
);
729 stream_putl(s
, table_id
);
731 stream_putw_at(s
, 0, stream_get_endp(s
));
733 return zserv_send_message(client
, s
);
736 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
737 enum zapi_route_notify_owner note
)
739 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
743 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
744 enum zapi_rule_notify_owner note
)
746 struct listnode
*node
;
747 struct zserv
*client
;
750 if (IS_ZEBRA_DEBUG_PACKET
)
751 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
754 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
755 if (rule
->sock
== client
->sock
)
762 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
764 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
765 stream_put(s
, ¬e
, sizeof(note
));
766 stream_putl(s
, rule
->rule
.seq
);
767 stream_putl(s
, rule
->rule
.priority
);
768 stream_putl(s
, rule
->rule
.unique
);
770 stream_putl(s
, rule
->ifp
->ifindex
);
774 stream_putw_at(s
, 0, stream_get_endp(s
));
776 zserv_send_message(client
, s
);
779 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
780 enum zapi_ipset_notify_owner note
)
782 struct listnode
*node
;
783 struct zserv
*client
;
786 if (IS_ZEBRA_DEBUG_PACKET
)
787 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
790 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
791 if (ipset
->sock
== client
->sock
)
798 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
800 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
801 stream_put(s
, ¬e
, sizeof(note
));
802 stream_putl(s
, ipset
->unique
);
803 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
804 stream_putw_at(s
, 0, stream_get_endp(s
));
806 zserv_send_message(client
, s
);
809 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
810 enum zapi_ipset_entry_notify_owner note
)
812 struct listnode
*node
;
813 struct zserv
*client
;
816 if (IS_ZEBRA_DEBUG_PACKET
)
817 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
820 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
821 if (ipset
->sock
== client
->sock
)
828 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
830 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
831 stream_put(s
, ¬e
, sizeof(note
));
832 stream_putl(s
, ipset
->unique
);
833 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
834 stream_putw_at(s
, 0, stream_get_endp(s
));
836 zserv_send_message(client
, s
);
839 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
840 enum zapi_iptable_notify_owner note
)
842 struct listnode
*node
;
843 struct zserv
*client
;
846 if (IS_ZEBRA_DEBUG_PACKET
)
847 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
850 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
851 if (iptable
->sock
== client
->sock
)
858 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
860 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
861 stream_put(s
, ¬e
, sizeof(note
));
862 stream_putl(s
, iptable
->unique
);
863 stream_putw_at(s
, 0, stream_get_endp(s
));
865 zserv_send_message(client
, s
);
868 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
869 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
874 /* Check this client need interface information. */
875 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
878 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
881 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
883 /* Prefix information. */
884 stream_putc(s
, p
->family
);
885 blen
= prefix_blen(p
);
886 stream_put(s
, &p
->u
.prefix
, blen
);
887 stream_putc(s
, p
->prefixlen
);
889 /* Write packet size. */
890 stream_putw_at(s
, 0, stream_get_endp(s
));
892 return zserv_send_message(client
, s
);
896 * Function used by Zebra to send a PW status update to LDP daemon
898 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
900 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
902 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
903 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
904 stream_putl(s
, pw
->ifindex
);
905 stream_putl(s
, pw
->status
);
907 /* Put length at the first point of the stream. */
908 stream_putw_at(s
, 0, stream_get_endp(s
));
910 return zserv_send_message(client
, s
);
913 /* Send response to a get label chunk request to client */
914 static int zsend_assign_label_chunk_response(struct zserv
*client
,
916 struct label_manager_chunk
*lmc
)
919 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
921 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
925 stream_putc(s
, lmc
->proto
);
927 stream_putw(s
, lmc
->instance
);
929 stream_putc(s
, lmc
->keep
);
930 /* start and end labels */
931 stream_putl(s
, lmc
->start
);
932 stream_putl(s
, lmc
->end
);
935 /* Write packet size. */
936 stream_putw_at(s
, 0, stream_get_endp(s
));
938 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
943 /* Send response to a label manager connect request to client */
944 static int zsend_label_manager_connect_response(struct zserv
*client
,
946 unsigned short result
)
949 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
951 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
954 stream_putc(s
, client
->proto
);
957 stream_putw(s
, client
->instance
);
960 stream_putc(s
, result
);
962 /* Write packet size. */
963 stream_putw_at(s
, 0, stream_get_endp(s
));
965 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
971 /* Send response to a get table chunk request to client */
972 static int zsend_assign_table_chunk_response(struct zserv
*client
,
974 struct table_manager_chunk
*tmc
)
976 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
978 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
981 /* start and end labels */
982 stream_putl(s
, tmc
->start
);
983 stream_putl(s
, tmc
->end
);
986 /* Write packet size. */
987 stream_putw_at(s
, 0, stream_get_endp(s
));
989 return zserv_send_message(client
, s
);
992 static int zsend_table_manager_connect_response(struct zserv
*client
,
996 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
998 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1001 stream_putc(s
, result
);
1003 stream_putw_at(s
, 0, stream_get_endp(s
));
1005 return zserv_send_message(client
, s
);
1008 /* Inbound message handling ------------------------------------------------ */
1011 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1012 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1013 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1014 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1017 /* Nexthop register */
1018 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1023 unsigned short l
= 0;
1025 uint16_t type
= cmd2type
[hdr
->command
];
1028 if (IS_ZEBRA_DEBUG_NHT
)
1030 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1031 zebra_route_string(client
->proto
), hdr
->length
,
1032 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1037 client
->nh_reg_time
= monotime(NULL
);
1039 while (l
< hdr
->length
) {
1040 STREAM_GETC(s
, flags
);
1041 STREAM_GETW(s
, p
.family
);
1042 STREAM_GETC(s
, p
.prefixlen
);
1044 if (p
.family
== AF_INET
) {
1045 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1047 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1048 __PRETTY_FUNCTION__
, p
.prefixlen
);
1051 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1052 l
+= IPV4_MAX_BYTELEN
;
1053 } else if (p
.family
== AF_INET6
) {
1054 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1056 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1057 __PRETTY_FUNCTION__
, p
.prefixlen
);
1060 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1061 l
+= IPV6_MAX_BYTELEN
;
1064 EC_ZEBRA_UNKNOWN_FAMILY
,
1065 "rnh_register: Received unknown family type %d\n",
1069 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1073 if (type
== RNH_NEXTHOP_TYPE
) {
1075 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1076 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1078 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1079 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1080 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1082 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1083 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1085 && CHECK_FLAG(rnh
->flags
,
1086 ZEBRA_NHT_EXACT_MATCH
))
1087 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1090 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1091 /* Anything not AF_INET/INET6 has been filtered out above */
1093 zebra_evaluate_rnh(zvrf_id(zvrf
), p
.family
, 1, type
,
1101 /* Nexthop register */
1102 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1107 unsigned short l
= 0;
1108 uint16_t type
= cmd2type
[hdr
->command
];
1110 if (IS_ZEBRA_DEBUG_NHT
)
1112 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1113 zebra_route_string(client
->proto
), hdr
->length
,
1118 while (l
< hdr
->length
) {
1121 STREAM_GETC(s
, flags
);
1123 goto stream_failure
;
1125 STREAM_GETW(s
, p
.family
);
1126 STREAM_GETC(s
, p
.prefixlen
);
1128 if (p
.family
== AF_INET
) {
1129 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1131 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1132 __PRETTY_FUNCTION__
, p
.prefixlen
);
1135 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1136 l
+= IPV4_MAX_BYTELEN
;
1137 } else if (p
.family
== AF_INET6
) {
1138 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1140 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1141 __PRETTY_FUNCTION__
, p
.prefixlen
);
1144 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1145 l
+= IPV6_MAX_BYTELEN
;
1148 EC_ZEBRA_UNKNOWN_FAMILY
,
1149 "rnh_register: Received unknown family type %d\n",
1153 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1155 client
->nh_dereg_time
= monotime(NULL
);
1156 zebra_remove_rnh_client(rnh
, client
, type
);
1163 #define ZEBRA_MIN_FEC_LENGTH 5
1166 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1169 unsigned short l
= 0;
1172 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1175 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1180 * The minimum amount of data that can be sent for one fec
1183 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1185 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1186 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1191 while (l
< hdr
->length
) {
1192 STREAM_GETW(s
, flags
);
1193 memset(&p
, 0, sizeof(p
));
1194 STREAM_GETW(s
, p
.family
);
1195 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1197 EC_ZEBRA_UNKNOWN_FAMILY
,
1198 "fec_register: Received unknown family type %d\n",
1202 STREAM_GETC(s
, p
.prefixlen
);
1203 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1204 || (p
.family
== AF_INET6
1205 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1207 "%s: Specified prefix hdr->length: %d is to long for %d",
1208 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1212 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1213 l
+= PSIZE(p
.prefixlen
);
1214 if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1215 STREAM_GETL(s
, label_index
);
1218 label_index
= MPLS_INVALID_LABEL_INDEX
;
1219 zebra_mpls_fec_register(zvrf
, &p
, label_index
, client
);
1226 /* FEC unregister */
1227 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1230 unsigned short l
= 0;
1235 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1240 * The minimum amount of data that can be sent for one
1241 * fec unregistration
1243 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1245 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1246 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1251 while (l
< hdr
->length
) {
1252 STREAM_GETW(s
, flags
);
1254 goto stream_failure
;
1256 memset(&p
, 0, sizeof(p
));
1257 STREAM_GETW(s
, p
.family
);
1258 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1260 EC_ZEBRA_UNKNOWN_FAMILY
,
1261 "fec_unregister: Received unknown family type %d\n",
1265 STREAM_GETC(s
, p
.prefixlen
);
1266 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1267 || (p
.family
== AF_INET6
1268 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1270 "%s: Received prefix hdr->length %d which is greater than %d can support",
1271 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1275 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1276 l
+= PSIZE(p
.prefixlen
);
1277 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1286 * Register zebra server interface information.
1287 * Send current all interface and address information.
1289 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1292 struct interface
*ifp
;
1294 /* Interface information is needed. */
1295 vrf_bitmap_set(client
->ifinfo
, zvrf_id(zvrf
));
1297 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1298 FOR_ALL_INTERFACES (vrf
, ifp
) {
1299 /* Skip pseudo interface. */
1300 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1303 zsend_interface_add(client
, ifp
);
1304 zsend_interface_addresses(client
, ifp
);
1309 /* Unregister zebra server interface information. */
1310 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1312 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1315 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1316 const unsigned int nexthop_num
)
1318 if (nexthop_num
> multipath_num
) {
1319 char buff
[PREFIX2STR_BUFFER
];
1321 prefix2str(p
, buff
, sizeof(buff
));
1323 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1324 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1325 caller
, buff
, nexthop_num
, multipath_num
);
1329 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1332 struct zapi_route api
;
1333 struct zapi_nexthop
*api_nh
;
1335 struct prefix_ipv6
*src_p
= NULL
;
1336 struct route_entry
*re
;
1337 struct nexthop
*nexthop
= NULL
;
1339 vrf_id_t vrf_id
= 0;
1340 struct ipaddr vtep_ip
;
1343 if (zapi_route_decode(s
, &api
) < 0) {
1344 if (IS_ZEBRA_DEBUG_RECV
)
1345 zlog_debug("%s: Unable to decode zapi_route sent",
1346 __PRETTY_FUNCTION__
);
1350 if (IS_ZEBRA_DEBUG_RECV
) {
1351 char buf_prefix
[PREFIX_STRLEN
];
1353 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1354 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1355 __func__
, buf_prefix
,
1356 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1361 /* Allocate new route. */
1362 vrf_id
= zvrf_id(zvrf
);
1363 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1364 re
->type
= api
.type
;
1365 re
->instance
= api
.instance
;
1366 re
->flags
= api
.flags
;
1367 re
->uptime
= time(NULL
);
1368 re
->vrf_id
= vrf_id
;
1369 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1370 re
->table
= api
.tableid
;
1372 re
->table
= zvrf
->table_id
;
1375 * TBD should _all_ of the nexthop add operations use
1376 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1377 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1379 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
))
1380 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1381 api_nh
= &api
.nexthops
[i
];
1382 ifindex_t ifindex
= 0;
1384 if (IS_ZEBRA_DEBUG_RECV
)
1385 zlog_debug("nh type %d", api_nh
->type
);
1387 switch (api_nh
->type
) {
1388 case NEXTHOP_TYPE_IFINDEX
:
1389 nexthop
= route_entry_nexthop_ifindex_add(
1390 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1392 case NEXTHOP_TYPE_IPV4
:
1393 if (IS_ZEBRA_DEBUG_RECV
) {
1394 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1396 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1397 nhbuf
, INET6_ADDRSTRLEN
);
1398 zlog_debug("%s: nh=%s, vrf_id=%d",
1402 nexthop
= route_entry_nexthop_ipv4_add(
1403 re
, &api_nh
->gate
.ipv4
, NULL
,
1406 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1408 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1409 if (CHECK_FLAG(api
.flags
,
1410 ZEBRA_FLAG_EVPN_ROUTE
)) {
1411 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1413 ifindex
= api_nh
->ifindex
;
1416 if (IS_ZEBRA_DEBUG_RECV
) {
1417 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1419 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
,
1420 nhbuf
, INET6_ADDRSTRLEN
);
1422 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1423 __func__
, nhbuf
, api_nh
->vrf_id
,
1424 re
->vrf_id
, ifindex
);
1426 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1427 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1430 /* if this an EVPN route entry,
1431 * program the nh as neigh
1433 if (CHECK_FLAG(api
.flags
,
1434 ZEBRA_FLAG_EVPN_ROUTE
)) {
1435 SET_FLAG(nexthop
->flags
,
1436 NEXTHOP_FLAG_EVPN_RVTEP
);
1437 vtep_ip
.ipa_type
= IPADDR_V4
;
1438 memcpy(&(vtep_ip
.ipaddr_v4
),
1439 &(api_nh
->gate
.ipv4
),
1440 sizeof(struct in_addr
));
1441 zebra_vxlan_evpn_vrf_route_add(
1442 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1446 case NEXTHOP_TYPE_IPV6
:
1447 nexthop
= route_entry_nexthop_ipv6_add(
1448 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1450 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1451 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1452 if (CHECK_FLAG(api
.flags
,
1453 ZEBRA_FLAG_EVPN_ROUTE
)) {
1454 ifindex
= get_l3vni_svi_ifindex(vrf_id
);
1456 ifindex
= api_nh
->ifindex
;
1459 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1460 re
, &api_nh
->gate
.ipv6
, ifindex
,
1463 /* if this an EVPN route entry,
1464 * program the nh as neigh
1466 if (CHECK_FLAG(api
.flags
,
1467 ZEBRA_FLAG_EVPN_ROUTE
)) {
1468 SET_FLAG(nexthop
->flags
,
1469 NEXTHOP_FLAG_EVPN_RVTEP
);
1470 vtep_ip
.ipa_type
= IPADDR_V6
;
1471 memcpy(&vtep_ip
.ipaddr_v6
,
1472 &(api_nh
->gate
.ipv6
),
1473 sizeof(struct in6_addr
));
1474 zebra_vxlan_evpn_vrf_route_add(
1475 vrf_id
, &api_nh
->rmac
, &vtep_ip
,
1479 case NEXTHOP_TYPE_BLACKHOLE
:
1480 nexthop
= route_entry_nexthop_blackhole_add(
1481 re
, api_nh
->bh_type
);
1487 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1488 "%s: Nexthops Specified: %d but we failed to properly create one",
1489 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1490 nexthops_free(re
->ng
.nexthop
);
1491 XFREE(MTYPE_RE
, re
);
1494 /* MPLS labels for BGP-LU or Segment Routing */
1495 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1496 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1497 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1498 enum lsp_types_t label_type
;
1501 lsp_type_from_re_type(client
->proto
);
1503 if (IS_ZEBRA_DEBUG_RECV
) {
1505 "%s: adding %d labels of type %d (1st=%u)",
1506 __func__
, api_nh
->label_num
,
1507 label_type
, api_nh
->labels
[0]);
1510 nexthop_add_labels(nexthop
, label_type
,
1512 &api_nh
->labels
[0]);
1516 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1517 re
->distance
= api
.distance
;
1518 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1519 re
->metric
= api
.metric
;
1520 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1522 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1525 afi
= family2afi(api
.prefix
.family
);
1526 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1527 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1528 "%s: Received SRC Prefix but afi is not v6",
1529 __PRETTY_FUNCTION__
);
1530 nexthops_free(re
->ng
.nexthop
);
1531 XFREE(MTYPE_RE
, re
);
1534 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1535 src_p
= &api
.src_prefix
;
1537 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1540 switch (api
.prefix
.family
) {
1543 client
->v4_route_add_cnt
++;
1545 client
->v4_route_upd8_cnt
++;
1549 client
->v6_route_add_cnt
++;
1551 client
->v6_route_upd8_cnt
++;
1556 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1559 struct zapi_route api
;
1561 struct prefix_ipv6
*src_p
= NULL
;
1565 if (zapi_route_decode(s
, &api
) < 0)
1568 afi
= family2afi(api
.prefix
.family
);
1569 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1570 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1571 "%s: Received a src prefix while afi is not v6",
1572 __PRETTY_FUNCTION__
);
1575 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1576 src_p
= &api
.src_prefix
;
1578 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1579 table_id
= api
.tableid
;
1581 table_id
= zvrf
->table_id
;
1583 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1584 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1585 api
.distance
, false);
1588 switch (api
.prefix
.family
) {
1590 client
->v4_route_del_cnt
++;
1593 client
->v6_route_del_cnt
++;
1598 /* MRIB Nexthop lookup for IPv4. */
1599 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1601 struct in_addr addr
;
1602 struct route_entry
*re
;
1604 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1605 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1606 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1612 /* Register zebra server router-id information. Send current router-id */
1613 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1617 /* Router-id information is needed. */
1618 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1620 router_id_get(&p
, zvrf_id(zvrf
));
1622 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1625 /* Unregister zebra server router-id information. */
1626 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1628 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1631 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1633 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1635 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1636 stream_putc(s
, mpls_enabled
);
1637 stream_putl(s
, multipath_num
);
1639 stream_putw_at(s
, 0, stream_get_endp(s
));
1640 zserv_send_message(client
, s
);
1643 /* Tie up route-type and client->sock */
1644 static void zread_hello(ZAPI_HANDLER_ARGS
)
1646 /* type of protocol (lib/zebra.h) */
1648 unsigned short instance
;
1651 STREAM_GETC(msg
, proto
);
1652 STREAM_GETW(msg
, instance
);
1653 STREAM_GETC(msg
, notify
);
1655 client
->notify_owner
= true;
1657 /* accept only dynamic routing protocols */
1658 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1660 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1661 client
->sock
, zebra_route_string(proto
),
1664 zlog_notice("client protocol instance %d", instance
);
1666 client
->proto
= proto
;
1667 client
->instance
= instance
;
1670 zsend_capabilities(client
, zvrf
);
1675 /* Unregister all information in a VRF. */
1676 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1681 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1682 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1683 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1684 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
1685 vrf_bitmap_unset(client
->ifinfo
, zvrf_id(zvrf
));
1686 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1689 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1692 enum lsp_types_t type
;
1693 struct prefix prefix
;
1694 enum nexthop_types_t gtype
;
1697 mpls_label_t in_label
, out_label
;
1700 /* Get input stream. */
1704 STREAM_GETC(s
, type
);
1705 STREAM_GETL(s
, prefix
.family
);
1706 switch (prefix
.family
) {
1708 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1709 STREAM_GETC(s
, prefix
.prefixlen
);
1710 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1712 "%s: Specified prefix length %d is greater than a v4 address can support",
1713 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1716 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1719 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1720 STREAM_GETC(s
, prefix
.prefixlen
);
1721 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1723 "%s: Specified prefix length %d is greater than a v6 address can support",
1724 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1727 STREAM_GET(&gate
.ipv6
, s
, 16);
1730 zlog_debug("%s: Specified AF %d is not supported for this call",
1731 __PRETTY_FUNCTION__
, prefix
.family
);
1734 STREAM_GETL(s
, ifindex
);
1735 STREAM_GETC(s
, distance
);
1736 STREAM_GETL(s
, in_label
);
1737 STREAM_GETL(s
, out_label
);
1739 switch (prefix
.family
) {
1742 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1744 gtype
= NEXTHOP_TYPE_IPV4
;
1748 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1750 gtype
= NEXTHOP_TYPE_IPV6
;
1759 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1760 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1762 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1763 distance
, out_label
);
1764 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1765 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1766 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1767 distance
, out_label
);
1773 /* Send response to a table manager connect request to client */
1774 static void zread_table_manager_connect(struct zserv
*client
,
1775 struct stream
*msg
, vrf_id_t vrf_id
)
1784 STREAM_GETC(s
, proto
);
1785 STREAM_GETW(s
, instance
);
1787 /* accept only dynamic routing protocols */
1788 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1789 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1790 "client %d has wrong protocol %s", client
->sock
,
1791 zebra_route_string(proto
));
1792 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1795 zlog_notice("client %d with vrf %u instance %u connected as %s",
1796 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1797 client
->proto
= proto
;
1798 client
->instance
= instance
;
1801 * Release previous labels of same protocol and instance.
1802 * This is done in case it restarted from an unexpected shutdown.
1804 release_daemon_table_chunks(client
);
1806 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1812 static void zread_label_manager_connect(struct zserv
*client
,
1813 struct stream
*msg
, vrf_id_t vrf_id
)
1816 /* type of protocol (lib/zebra.h) */
1818 unsigned short instance
;
1820 /* Get input stream. */
1824 STREAM_GETC(s
, proto
);
1825 STREAM_GETW(s
, instance
);
1827 /* accept only dynamic routing protocols */
1828 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1829 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1830 "client %d has wrong protocol %s", client
->sock
,
1831 zebra_route_string(proto
));
1832 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1835 zlog_notice("client %d with vrf %u instance %u connected as %s",
1836 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1837 client
->proto
= proto
;
1838 client
->instance
= instance
;
1841 * Release previous labels of same protocol and instance.
1842 * This is done in case it restarted from an unexpected shutdown.
1844 release_daemon_label_chunks(client
);
1847 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1848 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1849 /* send response back */
1850 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1855 static int msg_client_id_mismatch(const char *op
, struct zserv
*client
,
1856 uint8_t proto
, unsigned int instance
)
1858 if (proto
!= client
->proto
) {
1859 flog_err(EC_ZEBRA_PROTO_OR_INSTANCE_MISMATCH
,
1860 "%s: msg vs client proto mismatch, client=%u msg=%u",
1861 op
, client
->proto
, proto
);
1862 /* TODO: fail when BGP sets proto and instance */
1866 if (instance
!= client
->instance
) {
1868 EC_ZEBRA_PROTO_OR_INSTANCE_MISMATCH
,
1869 "%s: msg vs client instance mismatch, client=%u msg=%u",
1870 op
, client
->instance
, instance
);
1871 /* TODO: fail when BGP sets proto and instance */
1878 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1884 struct label_manager_chunk
*lmc
;
1886 unsigned short instance
;
1888 /* Get input stream. */
1892 STREAM_GETC(s
, proto
);
1893 STREAM_GETW(s
, instance
);
1894 STREAM_GETC(s
, keep
);
1895 STREAM_GETL(s
, size
);
1897 /* detect client vs message (proto,instance) mismatch */
1898 if (msg_client_id_mismatch("Get-label-chunk", client
, proto
, instance
))
1901 lmc
= assign_label_chunk(client
->proto
, client
->instance
, keep
, size
);
1904 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1905 "Unable to assign Label Chunk of size %u to %s instance %u",
1906 size
, zebra_route_string(client
->proto
),
1909 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1910 lmc
->start
, lmc
->end
,
1911 zebra_route_string(client
->proto
), client
->instance
);
1912 /* send response back */
1913 zsend_assign_label_chunk_response(client
, vrf_id
, lmc
);
1919 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1922 uint32_t start
, end
;
1924 unsigned short instance
;
1926 /* Get input stream. */
1930 STREAM_GETC(s
, proto
);
1931 STREAM_GETW(s
, instance
);
1932 STREAM_GETL(s
, start
);
1933 STREAM_GETL(s
, end
);
1935 /* detect client vs message (proto,instance) mismatch */
1936 if (msg_client_id_mismatch("Release-label-chunk", client
, proto
,
1940 release_label_chunk(client
->proto
, client
->instance
, start
, end
);
1945 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1947 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1948 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
1949 client
->is_synchronous
= 1;
1951 /* external label manager */
1953 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1955 /* this is a label manager */
1957 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
)
1958 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
1960 /* Sanity: don't allow 'unidentified' requests */
1961 if (!client
->proto
) {
1964 "Got label request from an unidentified client");
1967 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
1968 zread_get_label_chunk(client
, msg
,
1970 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
1971 zread_release_label_chunk(client
, msg
);
1976 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
1981 struct table_manager_chunk
*tmc
;
1983 /* Get input stream. */
1987 STREAM_GETL(s
, size
);
1989 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
1991 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
1992 "%s: Unable to assign Table Chunk of size %u",
1995 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
1997 /* send response back */
1998 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2004 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2007 uint32_t start
, end
;
2009 /* Get input stream. */
2013 STREAM_GETL(s
, start
);
2014 STREAM_GETL(s
, end
);
2016 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2022 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2024 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2025 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2026 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2028 /* Sanity: don't allow 'unidentified' requests */
2029 if (!client
->proto
) {
2032 "Got table request from an unidentified client");
2035 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2036 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2037 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2038 zread_release_table_chunk(client
, msg
);
2042 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2045 char ifname
[IF_NAMESIZE
];
2049 union g_addr nexthop
;
2050 uint32_t local_label
;
2051 uint32_t remote_label
;
2053 union pw_protocol_fields data
;
2055 struct zebra_pw
*pw
;
2057 /* Get input stream. */
2061 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2062 STREAM_GETL(s
, ifindex
);
2063 STREAM_GETL(s
, type
);
2067 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2070 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2075 STREAM_GETL(s
, local_label
);
2076 STREAM_GETL(s
, remote_label
);
2077 STREAM_GETC(s
, flags
);
2078 STREAM_GET(&data
, s
, sizeof(data
));
2079 protocol
= client
->proto
;
2081 pw
= zebra_pw_find(zvrf
, ifname
);
2082 switch (hdr
->command
) {
2085 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2086 "%s: pseudowire %s already exists [%s]",
2088 zserv_command_string(hdr
->command
));
2092 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2094 case ZEBRA_PW_DELETE
:
2096 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2097 "%s: pseudowire %s not found [%s]", __func__
,
2098 ifname
, zserv_command_string(hdr
->command
));
2102 zebra_pw_del(zvrf
, pw
);
2105 case ZEBRA_PW_UNSET
:
2107 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2108 "%s: pseudowire %s not found [%s]", __func__
,
2109 ifname
, zserv_command_string(hdr
->command
));
2113 switch (hdr
->command
) {
2117 case ZEBRA_PW_UNSET
:
2122 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2123 remote_label
, flags
, &data
);
2131 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2133 struct interface
*master
;
2134 struct interface
*slave
;
2135 struct stream
*s
= msg
;
2139 STREAM_GETL(s
, vrf_id
);
2140 STREAM_GETL(s
, ifindex
);
2141 master
= if_lookup_by_index(ifindex
, vrf_id
);
2143 STREAM_GETL(s
, vrf_id
);
2144 STREAM_GETL(s
, ifindex
);
2145 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2147 if (!master
|| !slave
)
2150 kernel_interface_set_master(master
, slave
);
2157 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2159 struct interface
*ifp
;
2160 mpls_label_t nlabel
;
2163 struct zebra_vrf
*def_zvrf
;
2164 enum lsp_types_t ltype
;
2167 STREAM_GETL(s
, nlabel
);
2168 STREAM_GETC(s
, afi
);
2169 if (nlabel
== zvrf
->label
[afi
]) {
2171 * Nothing to do here move along
2176 STREAM_GETC(s
, ltype
);
2178 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2179 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2181 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2184 zlog_debug("Unable to find specified Interface for %s",
2189 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2191 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2195 really_remove
= true;
2196 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2197 if (scrubber
== afi
)
2200 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2203 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2204 really_remove
= false;
2210 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2211 NEXTHOP_TYPE_IFINDEX
, NULL
,
2215 if (nlabel
!= MPLS_LABEL_NONE
)
2216 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2217 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2218 NULL
, ifp
->ifindex
);
2220 zvrf
->label
[afi
] = nlabel
;
2225 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2227 struct zebra_pbr_rule zpr
;
2233 STREAM_GETL(s
, total
);
2235 for (i
= 0; i
< total
; i
++) {
2236 memset(&zpr
, 0, sizeof(zpr
));
2238 zpr
.sock
= client
->sock
;
2239 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2240 STREAM_GETL(s
, zpr
.rule
.seq
);
2241 STREAM_GETL(s
, zpr
.rule
.priority
);
2242 STREAM_GETL(s
, zpr
.rule
.unique
);
2243 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2244 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2245 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2246 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2247 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2248 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2249 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2250 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2251 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2252 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2253 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2254 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2255 STREAM_GETL(s
, ifindex
);
2258 zpr
.ifp
= if_lookup_by_index_per_ns(
2262 zlog_debug("Failed to lookup ifindex: %u",
2268 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2269 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2271 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2272 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2274 if (zpr
.rule
.filter
.src_port
)
2275 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2277 if (zpr
.rule
.filter
.dst_port
)
2278 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2280 if (zpr
.rule
.filter
.fwmark
)
2281 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2283 if (hdr
->command
== ZEBRA_RULE_ADD
)
2284 zebra_pbr_add_rule(zvrf
->zns
, &zpr
);
2286 zebra_pbr_del_rule(zvrf
->zns
, &zpr
);
2293 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2295 struct zebra_pbr_ipset zpi
;
2300 STREAM_GETL(s
, total
);
2302 for (i
= 0; i
< total
; i
++) {
2303 memset(&zpi
, 0, sizeof(zpi
));
2305 zpi
.sock
= client
->sock
;
2306 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2307 STREAM_GETL(s
, zpi
.unique
);
2308 STREAM_GETL(s
, zpi
.type
);
2309 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2311 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2312 zebra_pbr_create_ipset(zvrf
->zns
, &zpi
);
2314 zebra_pbr_destroy_ipset(zvrf
->zns
, &zpi
);
2321 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2323 struct zebra_pbr_ipset_entry zpi
;
2324 struct zebra_pbr_ipset ipset
;
2329 STREAM_GETL(s
, total
);
2331 for (i
= 0; i
< total
; i
++) {
2332 memset(&zpi
, 0, sizeof(zpi
));
2333 memset(&ipset
, 0, sizeof(ipset
));
2335 zpi
.sock
= client
->sock
;
2336 STREAM_GETL(s
, zpi
.unique
);
2337 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2338 STREAM_GETC(s
, zpi
.src
.family
);
2339 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2340 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2341 STREAM_GETC(s
, zpi
.dst
.family
);
2342 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2343 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2345 STREAM_GETW(s
, zpi
.src_port_min
);
2346 STREAM_GETW(s
, zpi
.src_port_max
);
2347 STREAM_GETW(s
, zpi
.dst_port_min
);
2348 STREAM_GETW(s
, zpi
.dst_port_max
);
2349 STREAM_GETC(s
, zpi
.proto
);
2350 if (!is_default_prefix(&zpi
.src
))
2351 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2353 if (!is_default_prefix(&zpi
.dst
))
2354 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2355 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2356 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2357 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2358 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2359 if (zpi
.dst_port_max
!= 0)
2360 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2361 if (zpi
.src_port_max
!= 0)
2362 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2364 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2366 /* calculate backpointer */
2367 zpi
.backpointer
= zebra_pbr_lookup_ipset_pername(
2368 zvrf
->zns
, ipset
.ipset_name
);
2369 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2370 zebra_pbr_add_ipset_entry(zvrf
->zns
, &zpi
);
2372 zebra_pbr_del_ipset_entry(zvrf
->zns
, &zpi
);
2379 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2381 struct zebra_pbr_iptable zpi
;
2386 memset(&zpi
, 0, sizeof(zpi
));
2388 zpi
.interface_name_list
= list_new();
2389 zpi
.sock
= client
->sock
;
2390 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2391 STREAM_GETL(s
, zpi
.unique
);
2392 STREAM_GETL(s
, zpi
.type
);
2393 STREAM_GETL(s
, zpi
.filter_bm
);
2394 STREAM_GETL(s
, zpi
.action
);
2395 STREAM_GETL(s
, zpi
.fwmark
);
2396 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2397 STREAM_GETW(s
, zpi
.pkt_len_min
);
2398 STREAM_GETW(s
, zpi
.pkt_len_max
);
2399 STREAM_GETW(s
, zpi
.tcp_flags
);
2400 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2401 STREAM_GETC(s
, zpi
.dscp_value
);
2402 STREAM_GETC(s
, zpi
.fragment
);
2403 STREAM_GETL(s
, zpi
.nb_interface
);
2404 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2406 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2407 zebra_pbr_add_iptable(zvrf
->zns
, &zpi
);
2409 zebra_pbr_del_iptable(zvrf
->zns
, &zpi
);
2414 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2415 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2416 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2417 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2418 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2419 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2420 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2421 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2422 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2423 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2424 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2425 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2426 [ZEBRA_HELLO
] = zread_hello
,
2427 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2428 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2429 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2430 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2431 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2432 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2433 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2435 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2436 #endif /* HAVE_BFDD */
2437 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2438 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2439 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2440 #if defined(HAVE_RTADV)
2441 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2442 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2444 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2445 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2447 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2448 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2449 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2450 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2451 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2452 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2453 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2454 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2455 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2456 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2457 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2458 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2459 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2460 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2461 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2462 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2463 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2464 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2465 [ZEBRA_PW_SET
] = zread_pseudowire
,
2466 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2467 [ZEBRA_RULE_ADD
] = zread_rule
,
2468 [ZEBRA_RULE_DELETE
] = zread_rule
,
2469 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2470 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2471 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2472 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2473 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2474 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2475 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2476 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2477 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2480 #if defined(HANDLE_ZAPI_FUZZING)
2481 extern struct zebra_privs_t zserv_privs
;
2483 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2485 char fname
[MAXPATHLEN
];
2486 struct stream
*copy
;
2489 copy
= stream_dup(orig
);
2490 stream_set_getp(copy
, 0);
2492 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
2494 frr_elevate_privs(&zserv_privs
) {
2495 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2497 stream_flush(copy
, fd
);
2503 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2506 struct zebra_vrf
*zvrf
;
2508 zapi_parse_header(msg
, &hdr
);
2510 #if defined(HANDLE_ZAPI_FUZZING)
2511 zserv_write_incoming(msg
, hdr
.command
);
2514 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2517 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2519 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2520 zlog_debug("ZAPI message specifies unknown VRF: %d",
2525 if (hdr
.command
>= array_size(zserv_handlers
)
2526 || zserv_handlers
[hdr
.command
] == NULL
)
2527 zlog_info("Zebra received unknown command %d", hdr
.command
);
2529 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);