2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/zebra_router.h"
46 #include "zebra/rib.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_errors.h"
67 #include "zebra/zebra_mlag.h"
69 /* Encoding helpers -------------------------------------------------------- */
71 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
73 /* Interface information. */
74 struct zebra_if
*zif
= ifp
->info
;
76 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
77 stream_putl(s
, ifp
->ifindex
);
78 stream_putc(s
, ifp
->status
);
79 stream_putq(s
, ifp
->flags
);
80 stream_putc(s
, ifp
->ptm_enable
);
81 stream_putc(s
, ifp
->ptm_status
);
82 stream_putl(s
, ifp
->metric
);
83 stream_putl(s
, ifp
->speed
);
84 stream_putl(s
, ifp
->mtu
);
85 stream_putl(s
, ifp
->mtu6
);
86 stream_putl(s
, ifp
->bandwidth
);
87 stream_putl(s
, zif
->link_ifindex
);
88 stream_putl(s
, ifp
->ll_type
);
89 stream_putl(s
, ifp
->hw_addr_len
);
91 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
93 /* Then, Traffic Engineering parameters if any */
94 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
96 zebra_interface_link_params_write(s
, ifp
);
100 /* Write packet size. */
101 stream_putw_at(s
, 0, stream_get_endp(s
));
104 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
106 struct vrf_data data
;
107 const char *netns_name
= zvrf_ns_name(zvrf
);
109 data
.l
.table_id
= zvrf
->table_id
;
112 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
115 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
116 /* Pass the tableid and the netns NAME */
117 stream_put(s
, &data
, sizeof(struct vrf_data
));
118 /* Interface information. */
119 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
120 /* Write packet size. */
121 stream_putw_at(s
, 0, stream_get_endp(s
));
124 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
126 stream_putl(s
, nexthop
->vrf_id
);
127 stream_putc(s
, nexthop
->type
);
128 switch (nexthop
->type
) {
129 case NEXTHOP_TYPE_IPV4
:
130 case NEXTHOP_TYPE_IPV4_IFINDEX
:
131 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
132 stream_putl(s
, nexthop
->ifindex
);
134 case NEXTHOP_TYPE_IPV6
:
135 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
137 case NEXTHOP_TYPE_IPV6_IFINDEX
:
138 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
139 stream_putl(s
, nexthop
->ifindex
);
141 case NEXTHOP_TYPE_IFINDEX
:
142 stream_putl(s
, nexthop
->ifindex
);
151 /* Send handlers ----------------------------------------------------------- */
153 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
155 * This function is called in the following situations:
156 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
158 * - at startup, when zebra figures out the available interfaces
159 * - when an interface is added (where support for
160 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
161 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
164 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
166 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
168 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
169 zserv_encode_interface(s
, ifp
);
172 return zserv_send_message(client
, s
);
175 /* Interface deletion from zebra daemon. */
176 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
178 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
180 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
181 zserv_encode_interface(s
, ifp
);
184 return zserv_send_message(client
, s
);
187 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
189 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
191 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
192 zserv_encode_vrf(s
, zvrf
);
194 client
->vrfadd_cnt
++;
195 return zserv_send_message(client
, s
);
198 /* VRF deletion from zebra daemon. */
199 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
202 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
204 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
205 zserv_encode_vrf(s
, zvrf
);
207 client
->vrfdel_cnt
++;
208 return zserv_send_message(client
, s
);
211 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
213 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
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 [<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"
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(zrouter
.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(zrouter
.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 name of the interface and its new VRF (id) */
435 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
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 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
530 memset(&api
, 0, sizeof(api
));
531 api
.vrf_id
= re
->vrf_id
;
533 api
.safi
= SAFI_UNICAST
;
534 api
.instance
= re
->instance
;
535 api
.flags
= re
->flags
;
537 afi
= family2afi(p
->family
);
540 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
541 client
->redist_v4_add_cnt
++;
543 client
->redist_v4_del_cnt
++;
546 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
547 client
->redist_v6_add_cnt
++;
549 client
->redist_v6_del_cnt
++;
558 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
559 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
563 if (re
->nexthop_active_num
) {
564 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
565 api
.nexthop_num
= re
->nexthop_active_num
;
567 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
568 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
571 api_nh
= &api
.nexthops
[count
];
572 api_nh
->vrf_id
= nexthop
->vrf_id
;
573 api_nh
->type
= nexthop
->type
;
574 switch (nexthop
->type
) {
575 case NEXTHOP_TYPE_BLACKHOLE
:
576 api_nh
->bh_type
= nexthop
->bh_type
;
578 case NEXTHOP_TYPE_IPV4
:
579 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
581 case NEXTHOP_TYPE_IPV4_IFINDEX
:
582 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
583 api_nh
->ifindex
= nexthop
->ifindex
;
585 case NEXTHOP_TYPE_IFINDEX
:
586 api_nh
->ifindex
= nexthop
->ifindex
;
588 case NEXTHOP_TYPE_IPV6
:
589 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
591 case NEXTHOP_TYPE_IPV6_IFINDEX
:
592 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
593 api_nh
->ifindex
= nexthop
->ifindex
;
599 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
600 api
.distance
= re
->distance
;
601 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
602 api
.metric
= re
->metric
;
604 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
607 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
610 struct stream
*s
= stream_new(stream_size
);
612 /* Encode route and send. */
613 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
618 if (IS_ZEBRA_DEBUG_SEND
) {
619 char buf_prefix
[PREFIX_STRLEN
];
621 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
623 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
624 __func__
, zserv_command_string(cmd
),
625 zebra_route_string(client
->proto
),
626 zebra_route_string(api
.type
), api
.vrf_id
,
629 return zserv_send_message(client
, s
);
633 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
634 * nexthop is not found on mrib. Returns both route metric and protocol
637 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
639 struct route_entry
*re
,
640 struct zebra_vrf
*zvrf
)
645 struct nexthop
*nexthop
;
647 /* Get output stream. */
648 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
651 /* Fill in result. */
652 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
653 stream_put_in_addr(s
, &addr
);
656 stream_putc(s
, re
->distance
);
657 stream_putl(s
, re
->metric
);
659 /* remember position for nexthop_num */
660 nump
= stream_get_endp(s
);
661 /* reserve room for nexthop_num */
664 * Only non-recursive routes are elegible to resolve the
665 * nexthop we are looking up. Therefore, we will just iterate
666 * over the top chain of nexthops.
668 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
669 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
670 num
+= zserv_encode_nexthop(s
, nexthop
);
672 /* store nexthop_num */
673 stream_putc_at(s
, nump
, num
);
675 stream_putc(s
, 0); /* distance */
676 stream_putl(s
, 0); /* metric */
677 stream_putc(s
, 0); /* nexthop_num */
680 stream_putw_at(s
, 0, stream_get_endp(s
));
682 return zserv_send_message(client
, s
);
686 * Common utility send route notification, called from a path using a
687 * route_entry and from a path using a dataplane context.
689 static int route_notify_internal(const struct prefix
*p
, int type
,
690 uint16_t instance
, vrf_id_t vrf_id
,
692 enum zapi_route_notify_owner note
)
694 struct zserv
*client
;
698 client
= zserv_find_client(type
, instance
);
699 if (!client
|| !client
->notify_owner
) {
700 if (IS_ZEBRA_DEBUG_PACKET
) {
701 char buff
[PREFIX_STRLEN
];
704 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
705 type
, prefix2str(p
, buff
, sizeof(buff
)),
706 table_id
, note
, vrf_id
);
711 if (IS_ZEBRA_DEBUG_PACKET
) {
712 char buff
[PREFIX_STRLEN
];
714 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
715 type
, prefix2str(p
, buff
, sizeof(buff
)),
716 table_id
, note
, vrf_id
);
719 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
722 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
724 stream_put(s
, ¬e
, sizeof(note
));
726 stream_putc(s
, p
->family
);
728 blen
= prefix_blen(p
);
729 stream_putc(s
, p
->prefixlen
);
730 stream_put(s
, &p
->u
.prefix
, blen
);
732 stream_putl(s
, table_id
);
734 stream_putw_at(s
, 0, stream_get_endp(s
));
736 return zserv_send_message(client
, s
);
739 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
740 enum zapi_route_notify_owner note
)
742 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
747 * Route-owner notification using info from dataplane update context.
749 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
750 enum zapi_route_notify_owner note
)
752 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
753 dplane_ctx_get_type(ctx
),
754 dplane_ctx_get_instance(ctx
),
755 dplane_ctx_get_vrf(ctx
),
756 dplane_ctx_get_table(ctx
),
760 void zsend_rule_notify_owner(struct zebra_pbr_rule
*rule
,
761 enum zapi_rule_notify_owner note
)
763 struct listnode
*node
;
764 struct zserv
*client
;
767 if (IS_ZEBRA_DEBUG_PACKET
)
768 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
771 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
772 if (rule
->sock
== client
->sock
)
779 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
781 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
782 stream_put(s
, ¬e
, sizeof(note
));
783 stream_putl(s
, rule
->rule
.seq
);
784 stream_putl(s
, rule
->rule
.priority
);
785 stream_putl(s
, rule
->rule
.unique
);
787 stream_putl(s
, rule
->ifp
->ifindex
);
791 stream_putw_at(s
, 0, stream_get_endp(s
));
793 zserv_send_message(client
, s
);
796 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
797 enum zapi_ipset_notify_owner note
)
799 struct listnode
*node
;
800 struct zserv
*client
;
803 if (IS_ZEBRA_DEBUG_PACKET
)
804 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
807 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
808 if (ipset
->sock
== client
->sock
)
815 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
817 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
818 stream_put(s
, ¬e
, sizeof(note
));
819 stream_putl(s
, ipset
->unique
);
820 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
821 stream_putw_at(s
, 0, stream_get_endp(s
));
823 zserv_send_message(client
, s
);
826 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
827 enum zapi_ipset_entry_notify_owner note
)
829 struct listnode
*node
;
830 struct zserv
*client
;
833 if (IS_ZEBRA_DEBUG_PACKET
)
834 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
837 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
838 if (ipset
->sock
== client
->sock
)
845 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
847 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
848 stream_put(s
, ¬e
, sizeof(note
));
849 stream_putl(s
, ipset
->unique
);
850 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
851 stream_putw_at(s
, 0, stream_get_endp(s
));
853 zserv_send_message(client
, s
);
856 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
857 enum zapi_iptable_notify_owner note
)
859 struct listnode
*node
;
860 struct zserv
*client
;
863 if (IS_ZEBRA_DEBUG_PACKET
)
864 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__
,
867 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
868 if (iptable
->sock
== client
->sock
)
875 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
877 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
878 stream_put(s
, ¬e
, sizeof(note
));
879 stream_putl(s
, iptable
->unique
);
880 stream_putw_at(s
, 0, stream_get_endp(s
));
882 zserv_send_message(client
, s
);
885 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
886 int zsend_router_id_update(struct zserv
*client
, struct prefix
*p
,
891 /* Check this client need interface information. */
892 if (!vrf_bitmap_check(client
->ridinfo
, vrf_id
))
895 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
898 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
900 /* Prefix information. */
901 stream_putc(s
, p
->family
);
902 blen
= prefix_blen(p
);
903 stream_put(s
, &p
->u
.prefix
, blen
);
904 stream_putc(s
, p
->prefixlen
);
906 /* Write packet size. */
907 stream_putw_at(s
, 0, stream_get_endp(s
));
909 return zserv_send_message(client
, s
);
913 * Function used by Zebra to send a PW status update to LDP daemon
915 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
917 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
919 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
920 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
921 stream_putl(s
, pw
->ifindex
);
922 stream_putl(s
, pw
->status
);
924 /* Put length at the first point of the stream. */
925 stream_putw_at(s
, 0, stream_get_endp(s
));
927 return zserv_send_message(client
, s
);
930 /* Send response to a get label chunk request to client */
931 static int zsend_assign_label_chunk_response(struct zserv
*client
,
932 vrf_id_t vrf_id
, uint8_t proto
,
934 struct label_manager_chunk
*lmc
)
937 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
939 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
941 stream_putc(s
, proto
);
943 stream_putw(s
, instance
);
947 stream_putc(s
, lmc
->keep
);
948 /* start and end labels */
949 stream_putl(s
, lmc
->start
);
950 stream_putl(s
, lmc
->end
);
953 /* Write packet size. */
954 stream_putw_at(s
, 0, stream_get_endp(s
));
956 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
961 /* Send response to a label manager connect request to client */
962 static int zsend_label_manager_connect_response(struct zserv
*client
,
964 unsigned short result
)
967 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
969 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
972 stream_putc(s
, client
->proto
);
975 stream_putw(s
, client
->instance
);
978 stream_putc(s
, result
);
980 /* Write packet size. */
981 stream_putw_at(s
, 0, stream_get_endp(s
));
983 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
989 /* Send response to a get table chunk request to client */
990 static int zsend_assign_table_chunk_response(struct zserv
*client
,
992 struct table_manager_chunk
*tmc
)
994 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
996 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
999 /* start and end labels */
1000 stream_putl(s
, tmc
->start
);
1001 stream_putl(s
, tmc
->end
);
1004 /* Write packet size. */
1005 stream_putw_at(s
, 0, stream_get_endp(s
));
1007 return zserv_send_message(client
, s
);
1010 static int zsend_table_manager_connect_response(struct zserv
*client
,
1014 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1016 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1019 stream_putc(s
, result
);
1021 stream_putw_at(s
, 0, stream_get_endp(s
));
1023 return zserv_send_message(client
, s
);
1026 /* Inbound message handling ------------------------------------------------ */
1029 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1030 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1031 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1032 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1035 /* Nexthop register */
1036 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1041 unsigned short l
= 0;
1043 uint16_t type
= cmd2type
[hdr
->command
];
1045 bool flag_changed
= false;
1048 if (IS_ZEBRA_DEBUG_NHT
)
1050 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1051 zebra_route_string(client
->proto
), hdr
->length
,
1052 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1057 client
->nh_reg_time
= monotime(NULL
);
1059 while (l
< hdr
->length
) {
1060 STREAM_GETC(s
, flags
);
1061 STREAM_GETW(s
, p
.family
);
1062 STREAM_GETC(s
, p
.prefixlen
);
1064 if (p
.family
== AF_INET
) {
1065 client
->v4_nh_watch_add_cnt
++;
1066 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1068 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1069 __PRETTY_FUNCTION__
, p
.prefixlen
);
1072 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1073 l
+= IPV4_MAX_BYTELEN
;
1074 } else if (p
.family
== AF_INET6
) {
1075 client
->v6_nh_watch_add_cnt
++;
1076 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1078 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1079 __PRETTY_FUNCTION__
, p
.prefixlen
);
1082 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1083 l
+= IPV6_MAX_BYTELEN
;
1086 EC_ZEBRA_UNKNOWN_FAMILY
,
1087 "rnh_register: Received unknown family type %d\n",
1091 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1095 orig_flags
= rnh
->flags
;
1096 if (type
== RNH_NEXTHOP_TYPE
) {
1098 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1099 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1101 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1102 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1103 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1105 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1106 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1108 && CHECK_FLAG(rnh
->flags
,
1109 ZEBRA_NHT_EXACT_MATCH
))
1110 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1113 if (orig_flags
!= rnh
->flags
)
1114 flag_changed
= true;
1116 /* Anything not AF_INET/INET6 has been filtered out above */
1117 if (!exist
|| flag_changed
)
1118 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1121 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1128 /* Nexthop register */
1129 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1134 unsigned short l
= 0;
1135 uint16_t type
= cmd2type
[hdr
->command
];
1137 if (IS_ZEBRA_DEBUG_NHT
)
1139 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1140 zebra_route_string(client
->proto
), hdr
->length
,
1145 while (l
< hdr
->length
) {
1148 STREAM_GETC(s
, flags
);
1150 goto stream_failure
;
1152 STREAM_GETW(s
, p
.family
);
1153 STREAM_GETC(s
, p
.prefixlen
);
1155 if (p
.family
== AF_INET
) {
1156 client
->v4_nh_watch_rem_cnt
++;
1157 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1159 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1160 __PRETTY_FUNCTION__
, p
.prefixlen
);
1163 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1164 l
+= IPV4_MAX_BYTELEN
;
1165 } else if (p
.family
== AF_INET6
) {
1166 client
->v6_nh_watch_rem_cnt
++;
1167 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1169 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1170 __PRETTY_FUNCTION__
, p
.prefixlen
);
1173 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1174 l
+= IPV6_MAX_BYTELEN
;
1177 EC_ZEBRA_UNKNOWN_FAMILY
,
1178 "rnh_register: Received unknown family type %d\n",
1182 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1184 client
->nh_dereg_time
= monotime(NULL
);
1185 zebra_remove_rnh_client(rnh
, client
, type
);
1192 #define ZEBRA_MIN_FEC_LENGTH 5
1195 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1198 unsigned short l
= 0;
1201 uint32_t label
= MPLS_INVALID_LABEL
;
1202 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1205 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1210 * The minimum amount of data that can be sent for one fec
1213 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1215 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1216 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1221 while (l
< hdr
->length
) {
1222 STREAM_GETW(s
, flags
);
1223 memset(&p
, 0, sizeof(p
));
1224 STREAM_GETW(s
, p
.family
);
1225 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1227 EC_ZEBRA_UNKNOWN_FAMILY
,
1228 "fec_register: Received unknown family type %d\n",
1232 STREAM_GETC(s
, p
.prefixlen
);
1233 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1234 || (p
.family
== AF_INET6
1235 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1237 "%s: Specified prefix hdr->length: %d is to long for %d",
1238 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1242 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1243 l
+= PSIZE(p
.prefixlen
);
1244 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1245 STREAM_GETL(s
, label
);
1247 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1248 STREAM_GETL(s
, label_index
);
1252 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1259 /* FEC unregister */
1260 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1263 unsigned short l
= 0;
1268 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1273 * The minimum amount of data that can be sent for one
1274 * fec unregistration
1276 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1278 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1279 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1284 while (l
< hdr
->length
) {
1285 STREAM_GETW(s
, flags
);
1287 goto stream_failure
;
1289 memset(&p
, 0, sizeof(p
));
1290 STREAM_GETW(s
, p
.family
);
1291 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1293 EC_ZEBRA_UNKNOWN_FAMILY
,
1294 "fec_unregister: Received unknown family type %d\n",
1298 STREAM_GETC(s
, p
.prefixlen
);
1299 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1300 || (p
.family
== AF_INET6
1301 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1303 "%s: Received prefix hdr->length %d which is greater than %d can support",
1304 __PRETTY_FUNCTION__
, p
.prefixlen
, p
.family
);
1308 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1309 l
+= PSIZE(p
.prefixlen
);
1310 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1319 * Register zebra server interface information.
1320 * Send current all interface and address information.
1322 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1325 struct interface
*ifp
;
1327 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1328 FOR_ALL_INTERFACES (vrf
, ifp
) {
1329 /* Skip pseudo interface. */
1330 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1333 zsend_interface_add(client
, ifp
);
1334 zsend_interface_link_params(client
, ifp
);
1335 zsend_interface_addresses(client
, ifp
);
1340 /* Unregister zebra server interface information. */
1341 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1346 * Handle message requesting interface be set up or down.
1348 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1351 struct interface
*ifp
;
1354 STREAM_GETL(msg
, ifindex
);
1355 STREAM_GETC(msg
, down
);
1358 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1361 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1362 ifindex
, down
? "on" : "off");
1363 zebra_if_set_protodown(ifp
, down
);
1366 "Cannot set protodown %s for interface %u; does not exist",
1367 down
? "on" : "off", ifindex
);
1376 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1377 const unsigned int nexthop_num
)
1379 if (nexthop_num
> zrouter
.multipath_num
) {
1380 char buff
[PREFIX2STR_BUFFER
];
1382 prefix2str(p
, buff
, sizeof(buff
));
1384 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1385 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1386 caller
, buff
, nexthop_num
, zrouter
.multipath_num
);
1390 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1393 struct zapi_route api
;
1394 struct zapi_nexthop
*api_nh
;
1396 struct prefix_ipv6
*src_p
= NULL
;
1397 struct route_entry
*re
;
1398 struct nexthop
*nexthop
= NULL
;
1400 vrf_id_t vrf_id
= 0;
1401 struct ipaddr vtep_ip
;
1404 if (zapi_route_decode(s
, &api
) < 0) {
1405 if (IS_ZEBRA_DEBUG_RECV
)
1406 zlog_debug("%s: Unable to decode zapi_route sent",
1407 __PRETTY_FUNCTION__
);
1411 if (IS_ZEBRA_DEBUG_RECV
) {
1412 char buf_prefix
[PREFIX_STRLEN
];
1414 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1415 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1416 __func__
, buf_prefix
,
1417 (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
) ? ""
1422 /* Allocate new route. */
1423 vrf_id
= zvrf_id(zvrf
);
1424 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1425 re
->type
= api
.type
;
1426 re
->instance
= api
.instance
;
1427 re
->flags
= api
.flags
;
1428 re
->uptime
= monotime(NULL
);
1429 re
->vrf_id
= vrf_id
;
1430 if (api
.tableid
&& vrf_id
== VRF_DEFAULT
)
1431 re
->table
= api
.tableid
;
1433 re
->table
= zvrf
->table_id
;
1435 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1436 || api
.nexthop_num
== 0) {
1437 char buf_prefix
[PREFIX_STRLEN
];
1439 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1440 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1441 "%s: received a route without nexthops for prefix %s",
1442 __func__
, buf_prefix
);
1443 XFREE(MTYPE_RE
, re
);
1448 * TBD should _all_ of the nexthop add operations use
1449 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1450 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1452 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1453 api_nh
= &api
.nexthops
[i
];
1454 ifindex_t ifindex
= 0;
1456 if (IS_ZEBRA_DEBUG_RECV
)
1457 zlog_debug("nh type %d", api_nh
->type
);
1459 switch (api_nh
->type
) {
1460 case NEXTHOP_TYPE_IFINDEX
:
1461 nexthop
= route_entry_nexthop_ifindex_add(
1462 re
, api_nh
->ifindex
, api_nh
->vrf_id
);
1464 case NEXTHOP_TYPE_IPV4
:
1465 if (IS_ZEBRA_DEBUG_RECV
) {
1466 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1468 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1470 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1471 nhbuf
, api_nh
->vrf_id
);
1473 nexthop
= route_entry_nexthop_ipv4_add(
1474 re
, &api_nh
->gate
.ipv4
, NULL
, api_nh
->vrf_id
);
1476 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1478 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1479 ifindex
= api_nh
->ifindex
;
1480 if (IS_ZEBRA_DEBUG_RECV
) {
1481 char nhbuf
[INET6_ADDRSTRLEN
] = {0};
1483 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1486 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1487 __func__
, nhbuf
, api_nh
->vrf_id
,
1488 re
->vrf_id
, ifindex
);
1490 nexthop
= route_entry_nexthop_ipv4_ifindex_add(
1491 re
, &api_nh
->gate
.ipv4
, NULL
, ifindex
,
1494 /* Special handling for IPv4 routes sourced from EVPN:
1495 * the nexthop and associated MAC need to be installed.
1497 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1498 vtep_ip
.ipa_type
= IPADDR_V4
;
1499 memcpy(&(vtep_ip
.ipaddr_v4
),
1500 &(api_nh
->gate
.ipv4
),
1501 sizeof(struct in_addr
));
1502 zebra_vxlan_evpn_vrf_route_add(
1503 api_nh
->vrf_id
, &api_nh
->rmac
,
1504 &vtep_ip
, &api
.prefix
);
1507 case NEXTHOP_TYPE_IPV6
:
1508 nexthop
= route_entry_nexthop_ipv6_add(
1509 re
, &api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1511 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1512 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1513 ifindex
= api_nh
->ifindex
;
1514 nexthop
= route_entry_nexthop_ipv6_ifindex_add(
1515 re
, &api_nh
->gate
.ipv6
, ifindex
,
1518 /* Special handling for IPv6 routes sourced from EVPN:
1519 * the nexthop and associated MAC need to be installed.
1521 if (CHECK_FLAG(api
.flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1522 vtep_ip
.ipa_type
= IPADDR_V6
;
1523 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1524 sizeof(struct in6_addr
));
1525 zebra_vxlan_evpn_vrf_route_add(
1526 api_nh
->vrf_id
, &api_nh
->rmac
,
1527 &vtep_ip
, &api
.prefix
);
1530 case NEXTHOP_TYPE_BLACKHOLE
:
1531 nexthop
= route_entry_nexthop_blackhole_add(
1532 re
, api_nh
->bh_type
);
1538 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1539 "%s: Nexthops Specified: %d but we failed to properly create one",
1540 __PRETTY_FUNCTION__
, api
.nexthop_num
);
1541 nexthops_free(re
->ng
.nexthop
);
1542 XFREE(MTYPE_RE
, re
);
1546 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1548 /* MPLS labels for BGP-LU or Segment Routing */
1549 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
)
1550 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1551 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
) {
1552 enum lsp_types_t label_type
;
1554 label_type
= lsp_type_from_re_type(client
->proto
);
1556 if (IS_ZEBRA_DEBUG_RECV
) {
1558 "%s: adding %d labels of type %d (1st=%u)",
1559 __func__
, api_nh
->label_num
, label_type
,
1563 nexthop_add_labels(nexthop
, label_type
,
1565 &api_nh
->labels
[0]);
1569 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1570 re
->distance
= api
.distance
;
1571 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1572 re
->metric
= api
.metric
;
1573 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1575 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1578 afi
= family2afi(api
.prefix
.family
);
1579 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1580 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1581 "%s: Received SRC Prefix but afi is not v6",
1582 __PRETTY_FUNCTION__
);
1583 nexthops_free(re
->ng
.nexthop
);
1584 XFREE(MTYPE_RE
, re
);
1587 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1588 src_p
= &api
.src_prefix
;
1590 ret
= rib_add_multipath(afi
, api
.safi
, &api
.prefix
, src_p
, re
);
1593 switch (api
.prefix
.family
) {
1596 client
->v4_route_add_cnt
++;
1598 client
->v4_route_upd8_cnt
++;
1602 client
->v6_route_add_cnt
++;
1604 client
->v6_route_upd8_cnt
++;
1609 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1612 struct zapi_route api
;
1614 struct prefix_ipv6
*src_p
= NULL
;
1618 if (zapi_route_decode(s
, &api
) < 0)
1621 afi
= family2afi(api
.prefix
.family
);
1622 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1623 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1624 "%s: Received a src prefix while afi is not v6",
1625 __PRETTY_FUNCTION__
);
1628 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1629 src_p
= &api
.src_prefix
;
1631 if (api
.vrf_id
== VRF_DEFAULT
&& api
.tableid
!= 0)
1632 table_id
= api
.tableid
;
1634 table_id
= zvrf
->table_id
;
1636 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1637 api
.flags
, &api
.prefix
, src_p
, NULL
, table_id
, api
.metric
,
1638 api
.distance
, false);
1641 switch (api
.prefix
.family
) {
1643 client
->v4_route_del_cnt
++;
1646 client
->v6_route_del_cnt
++;
1651 /* MRIB Nexthop lookup for IPv4. */
1652 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1654 struct in_addr addr
;
1655 struct route_entry
*re
;
1657 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1658 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1659 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1665 /* Register zebra server router-id information. Send current router-id */
1666 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1670 /* Router-id information is needed. */
1671 vrf_bitmap_set(client
->ridinfo
, zvrf_id(zvrf
));
1673 router_id_get(&p
, zvrf_id(zvrf
));
1675 zsend_router_id_update(client
, &p
, zvrf_id(zvrf
));
1678 /* Unregister zebra server router-id information. */
1679 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1681 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1684 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1686 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1688 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1689 stream_putl(s
, vrf_get_backend());
1690 stream_putc(s
, mpls_enabled
);
1691 stream_putl(s
, zrouter
.multipath_num
);
1692 stream_putc(s
, zebra_mlag_get_role());
1694 stream_putw_at(s
, 0, stream_get_endp(s
));
1695 zserv_send_message(client
, s
);
1698 void zsend_capabilities_all_clients(void)
1700 struct listnode
*node
, *nnode
;
1701 struct zebra_vrf
*zvrf
;
1702 struct zserv
*client
;
1704 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1705 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1706 zsend_capabilities(client
, zvrf
);
1710 /* Tie up route-type and client->sock */
1711 static void zread_hello(ZAPI_HANDLER_ARGS
)
1713 /* type of protocol (lib/zebra.h) */
1715 unsigned short instance
;
1718 STREAM_GETC(msg
, proto
);
1719 STREAM_GETW(msg
, instance
);
1720 STREAM_GETC(msg
, notify
);
1722 client
->notify_owner
= true;
1724 /* accept only dynamic routing protocols */
1725 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
1727 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1728 client
->sock
, zebra_route_string(proto
),
1731 zlog_notice("client protocol instance %d", instance
);
1733 client
->proto
= proto
;
1734 client
->instance
= instance
;
1737 zsend_capabilities(client
, zvrf
);
1738 zebra_vrf_update_all(client
);
1743 /* Unregister all information in a VRF. */
1744 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
1749 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1750 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
1751 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
1752 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
1754 vrf_bitmap_unset(client
->ridinfo
, zvrf_id(zvrf
));
1757 static void zread_mpls_labels(ZAPI_HANDLER_ARGS
)
1760 enum lsp_types_t type
;
1761 struct prefix prefix
;
1762 enum nexthop_types_t gtype
;
1765 mpls_label_t in_label
, out_label
;
1768 /* Get input stream. */
1772 STREAM_GETC(s
, type
);
1773 STREAM_GETL(s
, prefix
.family
);
1774 switch (prefix
.family
) {
1776 STREAM_GET(&prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1777 STREAM_GETC(s
, prefix
.prefixlen
);
1778 if (prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
1780 "%s: Specified prefix length %d is greater than a v4 address can support",
1781 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1784 STREAM_GET(&gate
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1787 STREAM_GET(&prefix
.u
.prefix6
, s
, 16);
1788 STREAM_GETC(s
, prefix
.prefixlen
);
1789 if (prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
1791 "%s: Specified prefix length %d is greater than a v6 address can support",
1792 __PRETTY_FUNCTION__
, prefix
.prefixlen
);
1795 STREAM_GET(&gate
.ipv6
, s
, 16);
1798 zlog_debug("%s: Specified AF %d is not supported for this call",
1799 __PRETTY_FUNCTION__
, prefix
.family
);
1802 STREAM_GETL(s
, ifindex
);
1803 STREAM_GETC(s
, distance
);
1804 STREAM_GETL(s
, in_label
);
1805 STREAM_GETL(s
, out_label
);
1807 switch (prefix
.family
) {
1810 gtype
= NEXTHOP_TYPE_IPV4_IFINDEX
;
1812 gtype
= NEXTHOP_TYPE_IPV4
;
1816 gtype
= NEXTHOP_TYPE_IPV6_IFINDEX
;
1818 gtype
= NEXTHOP_TYPE_IPV6
;
1827 if (hdr
->command
== ZEBRA_MPLS_LABELS_ADD
) {
1828 mpls_lsp_install(zvrf
, type
, in_label
, out_label
, gtype
, &gate
,
1830 mpls_ftn_update(1, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1831 distance
, out_label
);
1832 } else if (hdr
->command
== ZEBRA_MPLS_LABELS_DELETE
) {
1833 mpls_lsp_uninstall(zvrf
, type
, in_label
, gtype
, &gate
, ifindex
);
1834 mpls_ftn_update(0, zvrf
, type
, &prefix
, gtype
, &gate
, ifindex
,
1835 distance
, out_label
);
1841 /* Send response to a table manager connect request to client */
1842 static void zread_table_manager_connect(struct zserv
*client
,
1843 struct stream
*msg
, vrf_id_t vrf_id
)
1852 STREAM_GETC(s
, proto
);
1853 STREAM_GETW(s
, instance
);
1855 /* accept only dynamic routing protocols */
1856 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1857 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1858 "client %d has wrong protocol %s", client
->sock
,
1859 zebra_route_string(proto
));
1860 zsend_table_manager_connect_response(client
, vrf_id
, 1);
1863 zlog_notice("client %d with vrf %u instance %u connected as %s",
1864 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1865 client
->proto
= proto
;
1866 client
->instance
= instance
;
1869 * Release previous labels of same protocol and instance.
1870 * This is done in case it restarted from an unexpected shutdown.
1872 release_daemon_table_chunks(client
);
1874 zsend_table_manager_connect_response(client
, vrf_id
, 0);
1880 static void zread_label_manager_connect(struct zserv
*client
,
1881 struct stream
*msg
, vrf_id_t vrf_id
)
1884 /* type of protocol (lib/zebra.h) */
1886 unsigned short instance
;
1888 /* Get input stream. */
1892 STREAM_GETC(s
, proto
);
1893 STREAM_GETW(s
, instance
);
1895 /* accept only dynamic routing protocols */
1896 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
1897 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
1898 "client %d has wrong protocol %s", client
->sock
,
1899 zebra_route_string(proto
));
1900 if (client
->is_synchronous
)
1901 zsend_label_manager_connect_response(client
, vrf_id
, 1);
1904 zlog_notice("client %d with vrf %u instance %u connected as %s",
1905 client
->sock
, vrf_id
, instance
, zebra_route_string(proto
));
1906 client
->proto
= proto
;
1907 client
->instance
= instance
;
1910 * Release previous labels of same protocol and instance.
1911 * This is done in case it restarted from an unexpected shutdown.
1913 release_daemon_label_chunks(client
);
1916 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1917 client
->sock
, zebra_route_string(proto
), vrf_id
, instance
);
1918 /* send response back */
1919 if (client
->is_synchronous
)
1920 zsend_label_manager_connect_response(client
, vrf_id
, 0);
1926 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
1931 uint32_t size
, base
;
1932 struct label_manager_chunk
*lmc
;
1934 unsigned short instance
;
1936 /* Get input stream. */
1940 STREAM_GETC(s
, proto
);
1941 STREAM_GETW(s
, instance
);
1942 STREAM_GETC(s
, keep
);
1943 STREAM_GETL(s
, size
);
1944 STREAM_GETL(s
, base
);
1946 lmc
= assign_label_chunk(proto
, instance
, keep
, size
, base
);
1949 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK
,
1950 "Unable to assign Label Chunk of size %u to %s instance %u",
1951 size
, zebra_route_string(proto
), instance
);
1953 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1954 lmc
->start
, lmc
->end
,
1955 zebra_route_string(proto
), instance
);
1956 /* send response back */
1957 zsend_assign_label_chunk_response(client
, vrf_id
, proto
, instance
, lmc
);
1963 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
1966 uint32_t start
, end
;
1968 unsigned short instance
;
1970 /* Get input stream. */
1974 STREAM_GETC(s
, proto
);
1975 STREAM_GETW(s
, instance
);
1976 STREAM_GETL(s
, start
);
1977 STREAM_GETL(s
, end
);
1979 release_label_chunk(proto
, instance
, start
, end
);
1984 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
1986 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1987 client
->is_synchronous
= hdr
->command
==
1988 ZEBRA_LABEL_MANAGER_CONNECT
;
1990 /* external label manager */
1992 zread_relay_label_manager_request(hdr
->command
, client
, msg
,
1994 /* this is a label manager */
1996 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
||
1997 hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
1998 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2000 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2001 zread_get_label_chunk(client
, msg
,
2003 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2004 zread_release_label_chunk(client
, msg
);
2009 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2014 struct table_manager_chunk
*tmc
;
2016 /* Get input stream. */
2020 STREAM_GETL(s
, size
);
2022 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2024 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2025 "%s: Unable to assign Table Chunk of size %u",
2028 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2030 /* send response back */
2031 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2037 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2040 uint32_t start
, end
;
2042 /* Get input stream. */
2046 STREAM_GETL(s
, start
);
2047 STREAM_GETL(s
, end
);
2049 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2055 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2057 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2058 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2059 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2061 /* Sanity: don't allow 'unidentified' requests */
2062 if (!client
->proto
) {
2065 "Got table request from an unidentified client");
2068 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2069 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2070 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2071 zread_release_table_chunk(client
, msg
);
2075 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2078 char ifname
[IF_NAMESIZE
];
2082 union g_addr nexthop
;
2083 uint32_t local_label
;
2084 uint32_t remote_label
;
2086 union pw_protocol_fields data
;
2088 struct zebra_pw
*pw
;
2090 /* Get input stream. */
2094 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2095 STREAM_GETL(s
, ifindex
);
2096 STREAM_GETL(s
, type
);
2100 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2103 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2108 STREAM_GETL(s
, local_label
);
2109 STREAM_GETL(s
, remote_label
);
2110 STREAM_GETC(s
, flags
);
2111 STREAM_GET(&data
, s
, sizeof(data
));
2112 protocol
= client
->proto
;
2114 pw
= zebra_pw_find(zvrf
, ifname
);
2115 switch (hdr
->command
) {
2118 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2119 "%s: pseudowire %s already exists [%s]",
2121 zserv_command_string(hdr
->command
));
2125 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2127 case ZEBRA_PW_DELETE
:
2129 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2130 "%s: pseudowire %s not found [%s]", __func__
,
2131 ifname
, zserv_command_string(hdr
->command
));
2135 zebra_pw_del(zvrf
, pw
);
2138 case ZEBRA_PW_UNSET
:
2140 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2141 "%s: pseudowire %s not found [%s]", __func__
,
2142 ifname
, zserv_command_string(hdr
->command
));
2146 switch (hdr
->command
) {
2150 case ZEBRA_PW_UNSET
:
2155 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2156 remote_label
, flags
, &data
);
2164 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2166 struct interface
*master
;
2167 struct interface
*slave
;
2168 struct stream
*s
= msg
;
2172 STREAM_GETL(s
, vrf_id
);
2173 STREAM_GETL(s
, ifindex
);
2174 master
= if_lookup_by_index(ifindex
, vrf_id
);
2176 STREAM_GETL(s
, vrf_id
);
2177 STREAM_GETL(s
, ifindex
);
2178 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2180 if (!master
|| !slave
)
2183 kernel_interface_set_master(master
, slave
);
2190 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2192 struct interface
*ifp
;
2193 mpls_label_t nlabel
;
2196 struct zebra_vrf
*def_zvrf
;
2197 enum lsp_types_t ltype
;
2200 STREAM_GETL(s
, nlabel
);
2201 STREAM_GETC(s
, afi
);
2202 if (nlabel
== zvrf
->label
[afi
]) {
2204 * Nothing to do here move along
2209 STREAM_GETC(s
, ltype
);
2211 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2212 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2214 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2217 zlog_debug("Unable to find specified Interface for %s",
2222 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2224 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2228 really_remove
= true;
2229 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2230 if (scrubber
== afi
)
2233 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2236 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2237 really_remove
= false;
2243 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2244 NEXTHOP_TYPE_IFINDEX
, NULL
,
2248 if (nlabel
!= MPLS_LABEL_NONE
)
2249 mpls_lsp_install(def_zvrf
, ltype
, nlabel
,
2250 MPLS_LABEL_IMPLICIT_NULL
, NEXTHOP_TYPE_IFINDEX
,
2251 NULL
, ifp
->ifindex
);
2253 zvrf
->label
[afi
] = nlabel
;
2258 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2260 struct zebra_pbr_rule zpr
;
2266 STREAM_GETL(s
, total
);
2268 for (i
= 0; i
< total
; i
++) {
2269 memset(&zpr
, 0, sizeof(zpr
));
2271 zpr
.sock
= client
->sock
;
2272 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2273 STREAM_GETL(s
, zpr
.rule
.seq
);
2274 STREAM_GETL(s
, zpr
.rule
.priority
);
2275 STREAM_GETL(s
, zpr
.rule
.unique
);
2276 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2277 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2278 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2279 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2280 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2281 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2282 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2283 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2284 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2285 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2286 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2287 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2288 STREAM_GETL(s
, ifindex
);
2291 zpr
.ifp
= if_lookup_by_index_per_ns(
2295 zlog_debug("Failed to lookup ifindex: %u",
2301 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2302 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2304 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2305 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2307 if (zpr
.rule
.filter
.src_port
)
2308 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2310 if (zpr
.rule
.filter
.dst_port
)
2311 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2313 if (zpr
.rule
.filter
.fwmark
)
2314 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2316 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2317 if (hdr
->command
== ZEBRA_RULE_ADD
)
2318 zebra_pbr_add_rule(&zpr
);
2320 zebra_pbr_del_rule(&zpr
);
2327 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2329 struct zebra_pbr_ipset zpi
;
2334 STREAM_GETL(s
, total
);
2336 for (i
= 0; i
< total
; i
++) {
2337 memset(&zpi
, 0, sizeof(zpi
));
2339 zpi
.sock
= client
->sock
;
2340 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2341 STREAM_GETL(s
, zpi
.unique
);
2342 STREAM_GETL(s
, zpi
.type
);
2343 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2345 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2346 zebra_pbr_create_ipset(&zpi
);
2348 zebra_pbr_destroy_ipset(&zpi
);
2355 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2357 struct zebra_pbr_ipset_entry zpi
;
2358 struct zebra_pbr_ipset ipset
;
2363 STREAM_GETL(s
, total
);
2365 for (i
= 0; i
< total
; i
++) {
2366 memset(&zpi
, 0, sizeof(zpi
));
2367 memset(&ipset
, 0, sizeof(ipset
));
2369 zpi
.sock
= client
->sock
;
2370 STREAM_GETL(s
, zpi
.unique
);
2371 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2372 STREAM_GETC(s
, zpi
.src
.family
);
2373 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2374 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2375 STREAM_GETC(s
, zpi
.dst
.family
);
2376 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2377 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2379 STREAM_GETW(s
, zpi
.src_port_min
);
2380 STREAM_GETW(s
, zpi
.src_port_max
);
2381 STREAM_GETW(s
, zpi
.dst_port_min
);
2382 STREAM_GETW(s
, zpi
.dst_port_max
);
2383 STREAM_GETC(s
, zpi
.proto
);
2384 if (!is_default_prefix(&zpi
.src
))
2385 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2387 if (!is_default_prefix(&zpi
.dst
))
2388 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2389 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2390 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2391 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2392 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2393 if (zpi
.dst_port_max
!= 0)
2394 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2395 if (zpi
.src_port_max
!= 0)
2396 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2398 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2400 /* calculate backpointer */
2402 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2403 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2404 zebra_pbr_add_ipset_entry(&zpi
);
2406 zebra_pbr_del_ipset_entry(&zpi
);
2413 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2415 struct zebra_pbr_iptable zpi
;
2420 memset(&zpi
, 0, sizeof(zpi
));
2422 zpi
.interface_name_list
= list_new();
2423 zpi
.sock
= client
->sock
;
2424 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2425 STREAM_GETL(s
, zpi
.unique
);
2426 STREAM_GETL(s
, zpi
.type
);
2427 STREAM_GETL(s
, zpi
.filter_bm
);
2428 STREAM_GETL(s
, zpi
.action
);
2429 STREAM_GETL(s
, zpi
.fwmark
);
2430 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2431 STREAM_GETW(s
, zpi
.pkt_len_min
);
2432 STREAM_GETW(s
, zpi
.pkt_len_max
);
2433 STREAM_GETW(s
, zpi
.tcp_flags
);
2434 STREAM_GETW(s
, zpi
.tcp_mask_flags
);
2435 STREAM_GETC(s
, zpi
.dscp_value
);
2436 STREAM_GETC(s
, zpi
.fragment
);
2437 STREAM_GETL(s
, zpi
.nb_interface
);
2438 zebra_pbr_iptable_update_interfacelist(s
, &zpi
);
2440 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2441 zebra_pbr_add_iptable(&zpi
);
2443 zebra_pbr_del_iptable(&zpi
);
2448 void (*zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
2449 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
2450 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
2451 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
2452 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
2453 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
2454 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
2455 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
2456 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
2457 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
2458 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
2459 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
2460 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
2461 [ZEBRA_HELLO
] = zread_hello
,
2462 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
2463 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
2464 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
2465 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
2466 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
2467 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
2468 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
2470 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
2471 #endif /* HAVE_BFDD */
2472 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
2473 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
2474 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
2475 #if defined(HAVE_RTADV)
2476 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
2477 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
2479 [ZEBRA_INTERFACE_ENABLE_RADV
] = NULL
,
2480 [ZEBRA_INTERFACE_DISABLE_RADV
] = NULL
,
2482 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels
,
2483 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels
,
2484 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
2485 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
2486 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
2487 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
2488 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
2489 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
2490 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
2491 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
2492 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
2493 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
2494 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
2495 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
2496 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
2497 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
2498 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
2499 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
2500 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
2501 [ZEBRA_PW_ADD
] = zread_pseudowire
,
2502 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
2503 [ZEBRA_PW_SET
] = zread_pseudowire
,
2504 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
2505 [ZEBRA_RULE_ADD
] = zread_rule
,
2506 [ZEBRA_RULE_DELETE
] = zread_rule
,
2507 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
2508 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
2509 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
2510 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
2511 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
2512 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
2513 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
2514 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
2515 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
2516 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
2519 #if defined(HANDLE_ZAPI_FUZZING)
2520 extern struct zebra_privs_t zserv_privs
;
2522 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
2524 char fname
[MAXPATHLEN
];
2525 struct stream
*copy
;
2528 copy
= stream_dup(orig
);
2529 stream_set_getp(copy
, 0);
2531 snprintf(fname
, MAXPATHLEN
, "%s/%u", frr_vtydir
, command
);
2533 frr_elevate_privs(&zserv_privs
) {
2534 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
2536 stream_flush(copy
, fd
);
2542 void zserv_handle_commands(struct zserv
*client
, struct stream
*msg
)
2545 struct zebra_vrf
*zvrf
;
2547 zapi_parse_header(msg
, &hdr
);
2549 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2550 zserv_log_message(NULL
, msg
, &hdr
);
2552 #if defined(HANDLE_ZAPI_FUZZING)
2553 zserv_write_incoming(msg
, hdr
.command
);
2556 hdr
.length
-= ZEBRA_HEADER_SIZE
;
2559 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
2561 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
2562 zlog_debug("ZAPI message specifies unknown VRF: %d",
2567 if (hdr
.command
>= array_size(zserv_handlers
)
2568 || zserv_handlers
[hdr
.command
] == NULL
)
2569 zlog_info("Zebra received unknown command %d", hdr
.command
);
2571 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);