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/stream.h"
28 #include "lib/memory.h"
29 #include "lib/table.h"
30 #include "lib/network.h"
32 #include "lib/zclient.h"
33 #include "lib/privs.h"
34 #include "lib/nexthop.h"
36 #include "lib/libfrr.h"
37 #include "lib/lib_errors.h"
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
41 #include "zebra/zebra_memory.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/router-id.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_ptm.h"
50 #include "zebra/rtadv.h"
51 #include "zebra/zebra_mpls.h"
52 #include "zebra/zebra_mroute.h"
53 #include "zebra/zebra_vxlan.h"
54 #include "zebra/zebra_evpn_mh.h"
56 #include "zebra/zebra_pbr.h"
57 #include "zebra/table_manager.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_errors.h"
60 #include "zebra/zebra_mlag.h"
61 #include "zebra/connected.h"
62 #include "zebra/zebra_opaque.h"
63 #include "zebra/zebra_srte.h"
65 /* Encoding helpers -------------------------------------------------------- */
67 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
69 /* Interface information. */
70 struct zebra_if
*zif
= ifp
->info
;
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
, zif
->link_ifindex
);
84 stream_putl(s
, ifp
->ll_type
);
85 stream_putl(s
, ifp
->hw_addr_len
);
87 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
89 /* Then, Traffic Engineering parameters if any */
90 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
92 zebra_interface_link_params_write(s
, ifp
);
96 /* Write packet size. */
97 stream_putw_at(s
, 0, stream_get_endp(s
));
100 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
102 struct vrf_data data
;
103 const char *netns_name
= zvrf_ns_name(zvrf
);
105 data
.l
.table_id
= zvrf
->table_id
;
108 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
111 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
112 /* Pass the tableid and the netns NAME */
113 stream_put(s
, &data
, sizeof(struct vrf_data
));
114 /* Interface information. */
115 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
116 /* Write packet size. */
117 stream_putw_at(s
, 0, stream_get_endp(s
));
120 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
122 stream_putl(s
, nexthop
->vrf_id
);
123 stream_putc(s
, nexthop
->type
);
124 switch (nexthop
->type
) {
125 case NEXTHOP_TYPE_IPV4
:
126 case NEXTHOP_TYPE_IPV4_IFINDEX
:
127 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
128 stream_putl(s
, nexthop
->ifindex
);
130 case NEXTHOP_TYPE_IPV6
:
131 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
133 case NEXTHOP_TYPE_IPV6_IFINDEX
:
134 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
135 stream_putl(s
, nexthop
->ifindex
);
137 case NEXTHOP_TYPE_IFINDEX
:
138 stream_putl(s
, nexthop
->ifindex
);
148 * Zebra error addition adds error type.
152 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154 * | enum zebra_error_types |
155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
160 stream_put(s
, &error
, sizeof(error
));
162 /* Write packet size. */
163 stream_putw_at(s
, 0, stream_get_endp(s
));
166 /* Send handlers ----------------------------------------------------------- */
168 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
170 * This function is called in the following situations:
171 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
173 * - at startup, when zebra figures out the available interfaces
174 * - when an interface is added (where support for
175 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
176 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
179 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
181 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
183 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
184 zserv_encode_interface(s
, ifp
);
187 return zserv_send_message(client
, s
);
190 /* Interface deletion from zebra daemon. */
191 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
193 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
195 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
196 zserv_encode_interface(s
, ifp
);
199 return zserv_send_message(client
, s
);
202 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
204 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
206 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
207 zserv_encode_vrf(s
, zvrf
);
209 client
->vrfadd_cnt
++;
210 return zserv_send_message(client
, s
);
213 /* VRF deletion from zebra daemon. */
214 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
217 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
219 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
220 zserv_encode_vrf(s
, zvrf
);
222 client
->vrfdel_cnt
++;
223 return zserv_send_message(client
, s
);
226 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
228 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
230 if (!ifp
->link_params
) {
235 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
237 /* Add Interface Index */
238 stream_putl(s
, ifp
->ifindex
);
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
246 /* Write packet size. */
247 stream_putw_at(s
, 0, stream_get_endp(s
));
249 return zserv_send_message(client
, s
);
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 * zsend_interface_address(DELETE)
274 * zebra_interface_address_delete_update
276 * | | if_delete_update
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
282 * | RTM_NEWADDR on routing/netlink socket
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M"
287 * ["no ipv6 address X:X::X:X/M"]
290 int zsend_interface_address(int cmd
, struct zserv
*client
,
291 struct interface
*ifp
, struct connected
*ifc
)
295 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
297 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
298 stream_putl(s
, ifp
->ifindex
);
300 /* Interface address flag. */
301 stream_putc(s
, ifc
->flags
);
303 /* Prefix information. */
305 stream_putc(s
, p
->family
);
306 blen
= prefix_blen(p
);
307 stream_put(s
, &p
->u
.prefix
, blen
);
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
315 stream_putc(s
, p
->prefixlen
);
318 p
= ifc
->destination
;
320 stream_put(s
, &p
->u
.prefix
, blen
);
322 stream_put(s
, NULL
, blen
);
324 /* Write packet size. */
325 stream_putw_at(s
, 0, stream_get_endp(s
));
327 client
->connected_rt_add_cnt
++;
328 return zserv_send_message(client
, s
);
331 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
332 struct interface
*ifp
,
333 struct nbr_connected
*ifc
)
336 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
339 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
340 stream_putl(s
, ifp
->ifindex
);
342 /* Prefix information. */
344 stream_putc(s
, p
->family
);
345 blen
= prefix_blen(p
);
346 stream_put(s
, &p
->u
.prefix
, blen
);
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
354 stream_putc(s
, p
->prefixlen
);
356 /* Write packet size. */
357 stream_putw_at(s
, 0, stream_get_endp(s
));
359 return zserv_send_message(client
, s
);
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
364 struct nbr_connected
*ifc
)
366 struct listnode
*node
, *nnode
;
367 struct zserv
*client
;
370 if (IS_ZEBRA_DEBUG_EVENT
) {
371 char buf
[INET6_ADDRSTRLEN
];
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
378 p
->prefixlen
, ifc
->ifp
->name
);
381 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
382 /* Do not send unsolicited messages to synchronous clients. */
383 if (client
->synchronous
)
386 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
391 /* Interface address deletion. */
392 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
393 struct nbr_connected
*ifc
)
395 struct listnode
*node
, *nnode
;
396 struct zserv
*client
;
399 if (IS_ZEBRA_DEBUG_EVENT
) {
400 char buf
[INET6_ADDRSTRLEN
];
404 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
405 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
407 p
->prefixlen
, ifc
->ifp
->name
);
410 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
411 /* Do not send unsolicited messages to synchronous clients. */
412 if (client
->synchronous
)
415 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
420 /* Send addresses on interface to client */
421 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
423 struct listnode
*cnode
, *cnnode
;
425 struct nbr_connected
*nc
;
427 /* Send interface addresses. */
428 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
429 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
432 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
438 /* Send interface neighbors. */
439 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
440 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
449 /* Notify client about interface moving from one VRF to another.
450 * Whether client is interested in old and new VRF is checked by caller.
452 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
455 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
457 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
459 /* Fill in the name of the interface and its new VRF (id) */
460 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
461 stream_putl(s
, vrf_id
);
463 /* Write packet size. */
464 stream_putw_at(s
, 0, stream_get_endp(s
));
466 client
->if_vrfchg_cnt
++;
467 return zserv_send_message(client
, s
);
470 /* Add new nbr connected IPv6 address */
471 void nbr_connected_add_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
= listnode_head(ifp
->nbr_connected
);
483 ifc
= nbr_connected_new();
484 ifc
->address
= prefix_new();
486 listnode_add(ifp
->nbr_connected
, ifc
);
489 prefix_copy(ifc
->address
, &p
);
491 zebra_interface_nbr_address_add_update(ifp
, ifc
);
493 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
496 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
498 struct nbr_connected
*ifc
;
502 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
503 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
505 ifc
= nbr_connected_check(ifp
, &p
);
509 listnode_delete(ifp
->nbr_connected
, ifc
);
511 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
513 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
515 nbr_connected_free(ifc
);
519 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
520 * ZEBRA_INTERFACE_DOWN.
522 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
523 * the clients in one of 2 situations:
524 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
525 * - a vty command modifying the bandwidth of an interface is received.
526 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
528 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
530 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
532 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
533 zserv_encode_interface(s
, ifp
);
535 if (cmd
== ZEBRA_INTERFACE_UP
)
538 client
->ifdown_cnt
++;
540 return zserv_send_message(client
, s
);
543 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
544 const struct prefix
*p
,
545 const struct prefix
*src_p
,
546 const struct route_entry
*re
)
548 struct zapi_route api
;
549 struct zapi_nexthop
*api_nh
;
550 struct nexthop
*nexthop
;
554 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
556 memset(&api
, 0, sizeof(api
));
557 api
.vrf_id
= re
->vrf_id
;
559 api
.safi
= SAFI_UNICAST
;
560 api
.instance
= re
->instance
;
561 api
.flags
= re
->flags
;
563 afi
= family2afi(p
->family
);
566 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
567 client
->redist_v4_add_cnt
++;
569 client
->redist_v4_del_cnt
++;
572 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
573 client
->redist_v6_add_cnt
++;
575 client
->redist_v6_del_cnt
++;
584 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
585 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
588 for (nexthop
= re
->nhe
->nhg
.nexthop
;
589 nexthop
; nexthop
= nexthop
->next
) {
590 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
593 api_nh
= &api
.nexthops
[count
];
594 api_nh
->vrf_id
= nexthop
->vrf_id
;
595 api_nh
->type
= nexthop
->type
;
596 api_nh
->weight
= nexthop
->weight
;
597 switch (nexthop
->type
) {
598 case NEXTHOP_TYPE_BLACKHOLE
:
599 api_nh
->bh_type
= nexthop
->bh_type
;
601 case NEXTHOP_TYPE_IPV4
:
602 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
604 case NEXTHOP_TYPE_IPV4_IFINDEX
:
605 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
606 api_nh
->ifindex
= nexthop
->ifindex
;
608 case NEXTHOP_TYPE_IFINDEX
:
609 api_nh
->ifindex
= nexthop
->ifindex
;
611 case NEXTHOP_TYPE_IPV6
:
612 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
614 case NEXTHOP_TYPE_IPV6_IFINDEX
:
615 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
616 api_nh
->ifindex
= nexthop
->ifindex
;
623 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
624 api
.nexthop_num
= count
;
628 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
629 api
.distance
= re
->distance
;
630 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
631 api
.metric
= re
->metric
;
633 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
636 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
639 struct stream
*s
= stream_new(stream_size
);
641 /* Encode route and send. */
642 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
647 if (IS_ZEBRA_DEBUG_SEND
) {
648 char buf_prefix
[PREFIX_STRLEN
];
650 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
652 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
653 __func__
, zserv_command_string(cmd
),
654 zebra_route_string(client
->proto
),
655 zebra_route_string(api
.type
), api
.vrf_id
,
658 return zserv_send_message(client
, s
);
662 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
663 * nexthop is not found on mrib. Returns both route metric and protocol
666 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
668 struct route_entry
*re
,
669 struct zebra_vrf
*zvrf
)
674 struct nexthop
*nexthop
;
676 /* Get output stream. */
677 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
680 /* Fill in result. */
681 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
682 stream_put_in_addr(s
, &addr
);
685 stream_putc(s
, re
->distance
);
686 stream_putl(s
, re
->metric
);
688 /* remember position for nexthop_num */
689 nump
= stream_get_endp(s
);
690 /* reserve room for nexthop_num */
693 * Only non-recursive routes are elegible to resolve the
694 * nexthop we are looking up. Therefore, we will just iterate
695 * over the top chain of nexthops.
697 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
698 nexthop
= nexthop
->next
)
699 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
700 num
+= zserv_encode_nexthop(s
, nexthop
);
702 /* store nexthop_num */
703 stream_putc_at(s
, nump
, num
);
705 stream_putc(s
, 0); /* distance */
706 stream_putl(s
, 0); /* metric */
707 stream_putc(s
, 0); /* nexthop_num */
710 stream_putw_at(s
, 0, stream_get_endp(s
));
712 return zserv_send_message(client
, s
);
716 * Common utility send route notification, called from a path using a
717 * route_entry and from a path using a dataplane context.
719 static int route_notify_internal(const struct prefix
*p
, int type
,
720 uint16_t instance
, vrf_id_t vrf_id
,
722 enum zapi_route_notify_owner note
)
724 struct zserv
*client
;
728 client
= zserv_find_client(type
, instance
);
729 if (!client
|| !client
->notify_owner
) {
730 if (IS_ZEBRA_DEBUG_PACKET
) {
731 char buff
[PREFIX_STRLEN
];
734 "Not Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
735 zebra_route_string(type
),
736 prefix2str(p
, buff
, sizeof(buff
)), table_id
,
742 if (IS_ZEBRA_DEBUG_PACKET
) {
743 char buff
[PREFIX_STRLEN
];
745 zlog_debug("Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
746 zebra_route_string(type
),
747 prefix2str(p
, buff
, sizeof(buff
)), table_id
, note
,
751 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
754 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
756 stream_put(s
, ¬e
, sizeof(note
));
758 stream_putc(s
, p
->family
);
760 blen
= prefix_blen(p
);
761 stream_putc(s
, p
->prefixlen
);
762 stream_put(s
, &p
->u
.prefix
, blen
);
764 stream_putl(s
, table_id
);
766 stream_putw_at(s
, 0, stream_get_endp(s
));
768 return zserv_send_message(client
, s
);
771 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
772 enum zapi_route_notify_owner note
)
774 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
779 * Route-owner notification using info from dataplane update context.
781 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
782 enum zapi_route_notify_owner note
)
784 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
785 dplane_ctx_get_type(ctx
),
786 dplane_ctx_get_instance(ctx
),
787 dplane_ctx_get_vrf(ctx
),
788 dplane_ctx_get_table(ctx
),
792 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
793 enum zapi_rule_notify_owner note
)
795 struct listnode
*node
;
796 struct zserv
*client
;
799 if (IS_ZEBRA_DEBUG_PACKET
)
800 zlog_debug("%s: Notifying %u", __func__
,
801 dplane_ctx_rule_get_unique(ctx
));
803 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
804 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
811 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
813 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
814 stream_put(s
, ¬e
, sizeof(note
));
815 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
816 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
817 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
818 stream_putl(s
, dplane_ctx_get_ifindex(ctx
));
820 stream_putw_at(s
, 0, stream_get_endp(s
));
822 zserv_send_message(client
, s
);
825 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
826 enum zapi_ipset_notify_owner note
)
828 struct listnode
*node
;
829 struct zserv
*client
;
832 if (IS_ZEBRA_DEBUG_PACKET
)
833 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
835 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
836 if (ipset
->sock
== client
->sock
)
843 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
845 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
846 stream_put(s
, ¬e
, sizeof(note
));
847 stream_putl(s
, ipset
->unique
);
848 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
849 stream_putw_at(s
, 0, stream_get_endp(s
));
851 zserv_send_message(client
, s
);
854 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
855 enum zapi_ipset_entry_notify_owner note
)
857 struct listnode
*node
;
858 struct zserv
*client
;
861 if (IS_ZEBRA_DEBUG_PACKET
)
862 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
864 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
865 if (ipset
->sock
== client
->sock
)
872 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
874 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
875 stream_put(s
, ¬e
, sizeof(note
));
876 stream_putl(s
, ipset
->unique
);
877 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
878 stream_putw_at(s
, 0, stream_get_endp(s
));
880 zserv_send_message(client
, s
);
883 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
884 enum zapi_iptable_notify_owner note
)
886 struct listnode
*node
;
887 struct zserv
*client
;
890 if (IS_ZEBRA_DEBUG_PACKET
)
891 zlog_debug("%s: Notifying %u", __func__
, iptable
->unique
);
893 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
894 if (iptable
->sock
== client
->sock
)
901 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
903 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
904 stream_put(s
, ¬e
, sizeof(note
));
905 stream_putl(s
, iptable
->unique
);
906 stream_putw_at(s
, 0, stream_get_endp(s
));
908 zserv_send_message(client
, s
);
911 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
912 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
918 /* Check this client need interface information. */
919 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
922 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
925 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
927 /* Prefix information. */
928 stream_putc(s
, p
->family
);
929 blen
= prefix_blen(p
);
930 stream_put(s
, &p
->u
.prefix
, blen
);
931 stream_putc(s
, p
->prefixlen
);
933 /* Write packet size. */
934 stream_putw_at(s
, 0, stream_get_endp(s
));
936 return zserv_send_message(client
, s
);
940 * Function used by Zebra to send a PW status update to LDP daemon
942 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
944 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
946 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
947 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
948 stream_putl(s
, pw
->ifindex
);
949 stream_putl(s
, pw
->status
);
951 /* Put length at the first point of the stream. */
952 stream_putw_at(s
, 0, stream_get_endp(s
));
954 return zserv_send_message(client
, s
);
957 /* Send response to a get label chunk request to client */
958 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
959 struct label_manager_chunk
*lmc
)
962 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
964 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
966 stream_putc(s
, client
->proto
);
968 stream_putw(s
, client
->instance
);
972 stream_putc(s
, lmc
->keep
);
973 /* start and end labels */
974 stream_putl(s
, lmc
->start
);
975 stream_putl(s
, lmc
->end
);
978 /* Write packet size. */
979 stream_putw_at(s
, 0, stream_get_endp(s
));
981 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
986 /* Send response to a label manager connect request to client */
987 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
988 unsigned short result
)
991 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
993 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
996 stream_putc(s
, client
->proto
);
999 stream_putw(s
, client
->instance
);
1002 stream_putc(s
, result
);
1004 /* Write packet size. */
1005 stream_putw_at(s
, 0, stream_get_endp(s
));
1007 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1013 /* Send response to a get table chunk request to client */
1014 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1016 struct table_manager_chunk
*tmc
)
1018 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1020 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1023 /* start and end labels */
1024 stream_putl(s
, tmc
->start
);
1025 stream_putl(s
, tmc
->end
);
1028 /* Write packet size. */
1029 stream_putw_at(s
, 0, stream_get_endp(s
));
1031 return zserv_send_message(client
, s
);
1034 static int zsend_table_manager_connect_response(struct zserv
*client
,
1038 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1040 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1043 stream_putc(s
, result
);
1045 stream_putw_at(s
, 0, stream_get_endp(s
));
1047 return zserv_send_message(client
, s
);
1050 /* Inbound message handling ------------------------------------------------ */
1052 const int cmd2type
[] = {
1053 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1054 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1055 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1056 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1059 /* Nexthop register */
1060 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1065 unsigned short l
= 0;
1067 uint16_t type
= cmd2type
[hdr
->command
];
1069 bool flag_changed
= false;
1072 if (IS_ZEBRA_DEBUG_NHT
)
1074 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1075 zebra_route_string(client
->proto
), hdr
->length
,
1076 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1081 if (!client
->nh_reg_time
)
1082 client
->nh_reg_time
= monotime(NULL
);
1084 while (l
< hdr
->length
) {
1085 STREAM_GETC(s
, flags
);
1086 STREAM_GETW(s
, p
.family
);
1087 STREAM_GETC(s
, p
.prefixlen
);
1089 if (p
.family
== AF_INET
) {
1090 client
->v4_nh_watch_add_cnt
++;
1091 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1093 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1094 __func__
, p
.prefixlen
);
1097 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1098 l
+= IPV4_MAX_BYTELEN
;
1099 } else if (p
.family
== AF_INET6
) {
1100 client
->v6_nh_watch_add_cnt
++;
1101 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1103 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1104 __func__
, p
.prefixlen
);
1107 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1108 l
+= IPV6_MAX_BYTELEN
;
1111 EC_ZEBRA_UNKNOWN_FAMILY
,
1112 "rnh_register: Received unknown family type %d\n",
1116 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1120 orig_flags
= rnh
->flags
;
1121 if (type
== RNH_NEXTHOP_TYPE
) {
1123 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1124 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1126 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1127 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1128 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1130 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1131 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1133 && CHECK_FLAG(rnh
->flags
,
1134 ZEBRA_NHT_EXACT_MATCH
))
1135 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1138 if (orig_flags
!= rnh
->flags
)
1139 flag_changed
= true;
1141 /* Anything not AF_INET/INET6 has been filtered out above */
1142 if (!exist
|| flag_changed
)
1143 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1146 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1153 /* Nexthop register */
1154 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1159 unsigned short l
= 0;
1160 uint16_t type
= cmd2type
[hdr
->command
];
1162 if (IS_ZEBRA_DEBUG_NHT
)
1164 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1165 zebra_route_string(client
->proto
), hdr
->length
,
1170 while (l
< hdr
->length
) {
1173 STREAM_GETC(s
, flags
);
1175 goto stream_failure
;
1177 STREAM_GETW(s
, p
.family
);
1178 STREAM_GETC(s
, p
.prefixlen
);
1180 if (p
.family
== AF_INET
) {
1181 client
->v4_nh_watch_rem_cnt
++;
1182 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1184 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1185 __func__
, p
.prefixlen
);
1188 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1189 l
+= IPV4_MAX_BYTELEN
;
1190 } else if (p
.family
== AF_INET6
) {
1191 client
->v6_nh_watch_rem_cnt
++;
1192 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1194 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1195 __func__
, p
.prefixlen
);
1198 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1199 l
+= IPV6_MAX_BYTELEN
;
1202 EC_ZEBRA_UNKNOWN_FAMILY
,
1203 "rnh_register: Received unknown family type %d\n",
1207 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1209 client
->nh_dereg_time
= monotime(NULL
);
1210 zebra_remove_rnh_client(rnh
, client
, type
);
1217 #define ZEBRA_MIN_FEC_LENGTH 5
1220 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1223 unsigned short l
= 0;
1226 uint32_t label
= MPLS_INVALID_LABEL
;
1227 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1230 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1235 * The minimum amount of data that can be sent for one fec
1238 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1240 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1241 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1246 while (l
< hdr
->length
) {
1247 STREAM_GETW(s
, flags
);
1248 memset(&p
, 0, sizeof(p
));
1249 STREAM_GETW(s
, p
.family
);
1250 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1252 EC_ZEBRA_UNKNOWN_FAMILY
,
1253 "fec_register: Received unknown family type %d\n",
1257 STREAM_GETC(s
, p
.prefixlen
);
1258 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1259 || (p
.family
== AF_INET6
1260 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1262 "%s: Specified prefix hdr->length: %d is to long for %d",
1263 __func__
, p
.prefixlen
, p
.family
);
1267 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1268 l
+= PSIZE(p
.prefixlen
);
1269 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1270 STREAM_GETL(s
, label
);
1272 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1273 STREAM_GETL(s
, label_index
);
1277 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1284 /* FEC unregister */
1285 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1288 unsigned short l
= 0;
1293 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1298 * The minimum amount of data that can be sent for one
1299 * fec unregistration
1301 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1303 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1304 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1309 while (l
< hdr
->length
) {
1310 STREAM_GETW(s
, flags
);
1312 goto stream_failure
;
1314 memset(&p
, 0, sizeof(p
));
1315 STREAM_GETW(s
, p
.family
);
1316 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1318 EC_ZEBRA_UNKNOWN_FAMILY
,
1319 "fec_unregister: Received unknown family type %d\n",
1323 STREAM_GETC(s
, p
.prefixlen
);
1324 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1325 || (p
.family
== AF_INET6
1326 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1328 "%s: Received prefix hdr->length %d which is greater than %d can support",
1329 __func__
, p
.prefixlen
, p
.family
);
1333 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1334 l
+= PSIZE(p
.prefixlen
);
1335 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1344 * Register zebra server interface information.
1345 * Send current all interface and address information.
1347 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1350 struct interface
*ifp
;
1352 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1353 FOR_ALL_INTERFACES (vrf
, ifp
) {
1354 /* Skip pseudo interface. */
1355 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1358 zsend_interface_add(client
, ifp
);
1359 zsend_interface_link_params(client
, ifp
);
1360 zsend_interface_addresses(client
, ifp
);
1365 /* Unregister zebra server interface information. */
1366 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1371 * Handle message requesting interface be set up or down.
1373 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1376 struct interface
*ifp
;
1379 STREAM_GETL(msg
, ifindex
);
1380 STREAM_GETC(msg
, down
);
1383 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1386 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1387 ifindex
, down
? "on" : "off");
1388 zebra_if_set_protodown(ifp
, down
);
1391 "Cannot set protodown %s for interface %u; does not exist",
1392 down
? "on" : "off", ifindex
);
1401 void zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1402 const unsigned int nexthop_num
)
1404 if (nexthop_num
> zrouter
.multipath_num
) {
1405 char buff
[PREFIX2STR_BUFFER
];
1407 prefix2str(p
, buff
, sizeof(buff
));
1409 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1410 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1411 caller
, buff
, nexthop_num
, zrouter
.multipath_num
);
1416 * Create a new nexthop based on a zapi nexthop.
1418 static struct nexthop
*nexthop_from_zapi(struct route_entry
*re
,
1419 const struct zapi_nexthop
*api_nh
,
1420 const struct zapi_route
*api
)
1422 struct nexthop
*nexthop
= NULL
;
1423 struct ipaddr vtep_ip
;
1424 struct interface
*ifp
;
1426 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1428 switch (api_nh
->type
) {
1429 case NEXTHOP_TYPE_IFINDEX
:
1430 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1432 case NEXTHOP_TYPE_IPV4
:
1433 if (IS_ZEBRA_DEBUG_RECV
) {
1434 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1436 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1437 nhbuf
, api_nh
->vrf_id
);
1439 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1442 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1443 if (IS_ZEBRA_DEBUG_RECV
) {
1444 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1446 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1447 __func__
, nhbuf
, api_nh
->vrf_id
,
1451 nexthop
= nexthop_from_ipv4_ifindex(
1452 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1455 /* Special handling for IPv4 routes sourced from EVPN:
1456 * the nexthop and associated MAC need to be installed.
1458 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1459 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1460 vtep_ip
.ipa_type
= IPADDR_V4
;
1461 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1462 sizeof(struct in_addr
));
1463 zebra_vxlan_evpn_vrf_route_add(
1464 api_nh
->vrf_id
, &api_nh
->rmac
,
1465 &vtep_ip
, &api
->prefix
);
1468 case NEXTHOP_TYPE_IPV6
:
1469 if (IS_ZEBRA_DEBUG_RECV
) {
1470 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1472 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1473 nhbuf
, api_nh
->vrf_id
);
1475 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1477 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1478 if (IS_ZEBRA_DEBUG_RECV
) {
1479 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1481 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1482 __func__
, nhbuf
, api_nh
->vrf_id
,
1485 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1489 /* Special handling for IPv6 routes sourced from EVPN:
1490 * the nexthop and associated MAC need to be installed.
1492 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1493 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1494 vtep_ip
.ipa_type
= IPADDR_V6
;
1495 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1496 sizeof(struct in6_addr
));
1497 zebra_vxlan_evpn_vrf_route_add(
1498 api_nh
->vrf_id
, &api_nh
->rmac
,
1499 &vtep_ip
, &api
->prefix
);
1502 case NEXTHOP_TYPE_BLACKHOLE
:
1503 if (IS_ZEBRA_DEBUG_RECV
)
1504 zlog_debug("%s: nh blackhole %d",
1505 __func__
, api_nh
->bh_type
);
1507 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1511 /* Return early if we couldn't process the zapi nexthop */
1512 if (nexthop
== NULL
) {
1516 /* Mark nexthop as onlink either if client has explicitly told us
1517 * to or if the nexthop is on an 'unnumbered' interface.
1519 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1520 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1521 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1522 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1523 if (ifp
&& connected_is_unnumbered(ifp
))
1524 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1527 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1528 nexthop
->weight
= api_nh
->weight
;
1530 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1531 /* Validate count */
1532 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1533 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1534 zlog_debug("%s: invalid backup nh count %d",
1535 __func__
, api_nh
->backup_num
);
1536 nexthop_free(nexthop
);
1541 /* Copy backup info */
1542 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1543 nexthop
->backup_num
= api_nh
->backup_num
;
1545 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1546 /* Validate backup index */
1547 if (api_nh
->backup_idx
[i
] < api
->backup_nexthop_num
) {
1548 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1550 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1551 zlog_debug("%s: invalid backup nh idx %d",
1553 api_nh
->backup_idx
[i
]);
1554 nexthop_free(nexthop
);
1565 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1568 struct zapi_route api
;
1569 struct zapi_nexthop
*api_nh
;
1571 struct prefix_ipv6
*src_p
= NULL
;
1572 struct route_entry
*re
;
1573 struct nexthop
*nexthop
= NULL
, *last_nh
;
1574 struct nexthop_group
*ng
= NULL
;
1575 struct nhg_backup_info
*bnhg
= NULL
;
1578 struct nhg_hash_entry nhe
;
1579 enum lsp_types_t label_type
;
1580 char nhbuf
[NEXTHOP_STRLEN
];
1581 char labelbuf
[MPLS_LABEL_STRLEN
];
1584 if (zapi_route_decode(s
, &api
) < 0) {
1585 if (IS_ZEBRA_DEBUG_RECV
)
1586 zlog_debug("%s: Unable to decode zapi_route sent",
1591 vrf_id
= zvrf_id(zvrf
);
1593 if (IS_ZEBRA_DEBUG_RECV
) {
1594 char buf_prefix
[PREFIX_STRLEN
];
1596 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1597 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
1598 __func__
, vrf_id
, api
.tableid
, buf_prefix
, (int)api
.message
, api
.flags
);
1601 /* Allocate new route. */
1602 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1603 re
->type
= api
.type
;
1604 re
->instance
= api
.instance
;
1605 re
->flags
= api
.flags
;
1606 re
->uptime
= monotime(NULL
);
1607 re
->vrf_id
= vrf_id
;
1610 re
->table
= api
.tableid
;
1612 re
->table
= zvrf
->table_id
;
1614 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1615 || api
.nexthop_num
== 0) {
1616 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1617 "%s: received a route without nexthops for prefix %pFX from client %s",
1618 __func__
, &api
.prefix
,
1619 zebra_route_string(client
->proto
));
1621 XFREE(MTYPE_RE
, re
);
1625 /* Report misuse of the backup flag */
1626 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
) &&
1627 api
.backup_nexthop_num
== 0) {
1628 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1629 zlog_debug("%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1631 zebra_route_string(client
->proto
), &api
.prefix
);
1634 /* Use temporary list of nexthops */
1635 ng
= nexthop_group_new();
1638 * TBD should _all_ of the nexthop add operations use
1639 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1640 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1642 for (i
= 0; i
< api
.nexthop_num
; i
++) {
1643 api_nh
= &api
.nexthops
[i
];
1645 /* Convert zapi nexthop */
1646 nexthop
= nexthop_from_zapi(re
, api_nh
, &api
);
1649 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1650 "%s: Nexthops Specified: %d but we failed to properly create one",
1651 __func__
, api
.nexthop_num
);
1652 nexthop_group_delete(&ng
);
1653 XFREE(MTYPE_RE
, re
);
1657 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
)) {
1658 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1659 nexthop
->srte_color
= api_nh
->srte_color
;
1662 /* MPLS labels for BGP-LU or Segment Routing */
1663 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1664 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1665 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1666 && api_nh
->label_num
> 0) {
1668 label_type
= lsp_type_from_re_type(client
->proto
);
1669 nexthop_add_labels(nexthop
, label_type
,
1671 &api_nh
->labels
[0]);
1674 if (IS_ZEBRA_DEBUG_RECV
) {
1678 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1680 if (nexthop
->nh_label
&&
1681 nexthop
->nh_label
->num_labels
> 0) {
1682 mpls_label2str(nexthop
->nh_label
->num_labels
,
1683 nexthop
->nh_label
->label
,
1684 labelbuf
, sizeof(labelbuf
),
1688 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1689 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1692 /* Add new nexthop to temporary list. This list is
1693 * canonicalized - sorted - so that it can be hashed later
1694 * in route processing. We expect that the sender has sent
1695 * the list sorted, and the zapi client api attempts to enforce
1696 * that, so this should be inexpensive - but it is necessary
1697 * to support shared nexthop-groups.
1699 nexthop_group_add_sorted(ng
, nexthop
);
1702 /* Allocate temporary list of backup nexthops, if necessary */
1703 if (api
.backup_nexthop_num
> 0) {
1704 if (IS_ZEBRA_DEBUG_RECV
)
1705 zlog_debug("%s: adding %d backup nexthops",
1706 __func__
, api
.backup_nexthop_num
);
1708 bnhg
= zebra_nhg_backup_alloc();
1713 /* Copy backup nexthops also, if present */
1714 for (i
= 0; i
< api
.backup_nexthop_num
; i
++) {
1715 api_nh
= &api
.backup_nexthops
[i
];
1717 /* Convert zapi backup nexthop */
1718 nexthop
= nexthop_from_zapi(re
, api_nh
, &api
);
1721 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1722 "%s: Backup Nexthops Specified: %d but we failed to properly create one",
1723 __func__
, api
.backup_nexthop_num
);
1724 nexthop_group_delete(&ng
);
1725 zebra_nhg_backup_free(&bnhg
);
1726 XFREE(MTYPE_RE
, re
);
1730 /* Backup nexthops can't have backups; that's not valid. */
1731 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1732 if (IS_ZEBRA_DEBUG_RECV
) {
1733 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1734 zlog_debug("%s: backup nh %s with BACKUP flag!",
1737 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1738 nexthop
->backup_num
= 0;
1741 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
)) {
1742 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1743 nexthop
->srte_color
= api_nh
->srte_color
;
1746 /* MPLS labels for BGP-LU or Segment Routing */
1747 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1748 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1749 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1750 && api_nh
->label_num
> 0) {
1752 label_type
= lsp_type_from_re_type(client
->proto
);
1753 nexthop_add_labels(nexthop
, label_type
,
1755 &api_nh
->labels
[0]);
1758 if (IS_ZEBRA_DEBUG_RECV
) {
1762 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1764 if (nexthop
->nh_label
&&
1765 nexthop
->nh_label
->num_labels
> 0) {
1766 mpls_label2str(nexthop
->nh_label
->num_labels
,
1767 nexthop
->nh_label
->label
,
1768 labelbuf
, sizeof(labelbuf
),
1772 zlog_debug("%s: backup nh=%s, vrf_id=%d %s",
1773 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1776 /* Note that the order of the backup nexthops is significant,
1777 * so we don't sort this list as we do the primary nexthops,
1781 NEXTHOP_APPEND(last_nh
, nexthop
);
1783 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1788 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1789 re
->distance
= api
.distance
;
1790 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1791 re
->metric
= api
.metric
;
1792 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1794 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1797 afi
= family2afi(api
.prefix
.family
);
1798 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1799 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1800 "%s: Received SRC Prefix but afi is not v6",
1802 nexthop_group_delete(&ng
);
1803 zebra_nhg_backup_free(&bnhg
);
1804 XFREE(MTYPE_RE
, re
);
1807 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1808 src_p
= &api
.src_prefix
;
1810 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
1811 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
1812 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1813 __func__
, api
.safi
);
1814 nexthop_group_delete(&ng
);
1815 zebra_nhg_backup_free(&bnhg
);
1816 XFREE(MTYPE_RE
, re
);
1820 /* Include backup info with the route. We use a temporary nhe here;
1821 * if this is a new/unknown nhe, a new copy will be allocated
1824 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
1825 nhe
.nhg
.nexthop
= ng
->nexthop
;
1826 nhe
.backup_info
= bnhg
;
1827 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
,
1830 /* At this point, these allocations are not needed: 're' has been
1831 * retained or freed, and if 're' still exists, it is using
1832 * a reference to a shared group object.
1834 nexthop_group_delete(&ng
);
1836 zebra_nhg_backup_free(&bnhg
);
1839 switch (api
.prefix
.family
) {
1842 client
->v4_route_add_cnt
++;
1844 client
->v4_route_upd8_cnt
++;
1848 client
->v6_route_add_cnt
++;
1850 client
->v6_route_upd8_cnt
++;
1855 static void zread_route_del(ZAPI_HANDLER_ARGS
)
1858 struct zapi_route api
;
1860 struct prefix_ipv6
*src_p
= NULL
;
1864 if (zapi_route_decode(s
, &api
) < 0)
1867 afi
= family2afi(api
.prefix
.family
);
1868 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1869 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1870 "%s: Received a src prefix while afi is not v6",
1874 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1875 src_p
= &api
.src_prefix
;
1878 table_id
= api
.tableid
;
1880 table_id
= zvrf
->table_id
;
1882 if (IS_ZEBRA_DEBUG_RECV
) {
1883 char buf_prefix
[PREFIX_STRLEN
];
1885 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1886 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
1887 __func__
, zvrf_id(zvrf
), table_id
, buf_prefix
,
1888 (int)api
.message
, api
.flags
);
1891 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
1892 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
1893 api
.distance
, false);
1896 switch (api
.prefix
.family
) {
1898 client
->v4_route_del_cnt
++;
1901 client
->v6_route_del_cnt
++;
1906 /* MRIB Nexthop lookup for IPv4. */
1907 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
1909 struct in_addr addr
;
1910 struct route_entry
*re
;
1912 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
1913 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
1914 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
1920 /* Register zebra server router-id information. Send current router-id */
1921 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
1927 STREAM_GETW(msg
, afi
);
1929 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
1931 "Invalid AFI %u while registering for router ID notifications",
1933 goto stream_failure
;
1936 /* Router-id information is needed. */
1937 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
1939 router_id_get(afi
, &p
, zvrf
);
1941 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
1947 /* Unregister zebra server router-id information. */
1948 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
1952 STREAM_GETW(msg
, afi
);
1954 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
1956 "Invalid AFI %u while unregistering from router ID notifications",
1958 goto stream_failure
;
1961 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
1967 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
1969 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1971 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
1972 stream_putl(s
, vrf_get_backend());
1973 stream_putc(s
, mpls_enabled
);
1974 stream_putl(s
, zrouter
.multipath_num
);
1975 stream_putc(s
, zebra_mlag_get_role());
1977 stream_putw_at(s
, 0, stream_get_endp(s
));
1978 zserv_send_message(client
, s
);
1981 void zsend_capabilities_all_clients(void)
1983 struct listnode
*node
, *nnode
;
1984 struct zebra_vrf
*zvrf
;
1985 struct zserv
*client
;
1987 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1988 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1989 /* Do not send unsolicited messages to synchronous clients. */
1990 if (client
->synchronous
)
1993 zsend_capabilities(client
, zvrf
);
1997 /* Tie up route-type and client->sock */
1998 static void zread_hello(ZAPI_HANDLER_ARGS
)
2000 /* type of protocol (lib/zebra.h) */
2002 unsigned short instance
;
2004 uint8_t synchronous
;
2005 uint32_t session_id
;
2007 STREAM_GETC(msg
, proto
);
2008 STREAM_GETW(msg
, instance
);
2009 STREAM_GETL(msg
, session_id
);
2010 STREAM_GETC(msg
, notify
);
2011 STREAM_GETC(msg
, synchronous
);
2013 client
->notify_owner
= true;
2016 client
->synchronous
= true;
2018 /* accept only dynamic routing protocols */
2019 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2021 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2022 client
->sock
, zebra_route_string(proto
),
2025 zlog_notice("client protocol instance %d", instance
);
2027 client
->proto
= proto
;
2028 client
->instance
= instance
;
2029 client
->session_id
= session_id
;
2031 /* Graceful restart processing for client connect */
2032 zebra_gr_client_reconnect(client
);
2035 if (!client
->synchronous
) {
2036 zsend_capabilities(client
, zvrf
);
2037 zebra_vrf_update_all(client
);
2043 /* Unregister all information in a VRF. */
2044 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2049 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2050 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2051 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2052 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2053 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2058 * Validate incoming zapi mpls lsp / labels message
2060 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2066 const struct zapi_nexthop
*znh
;
2068 /* Validate backup info: no duplicates for a single primary */
2069 if (zl
->backup_nexthop_num
== 0) {
2074 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2075 znh
= &zl
->nexthops
[j
];
2077 memset(bits
, 0, sizeof(bits
));
2079 for (i
= 0; i
< znh
->backup_num
; i
++) {
2080 idx
= znh
->backup_idx
[i
] / 32;
2082 ival
= 1 << znh
->backup_idx
[i
] % 32;
2084 /* Check whether value is already used */
2085 if (ival
& bits
[idx
]) {
2088 if (IS_ZEBRA_DEBUG_RECV
)
2089 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2091 znh
->backup_idx
[i
]);
2095 /* Mark index value */
2108 * Handle request to create an MPLS LSP.
2110 * A single message can fully specify an LSP with multiple nexthops.
2112 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2113 * updated to use the received label(s).
2115 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2118 struct zapi_labels zl
;
2121 /* Get input stream. */
2123 if (zapi_labels_decode(s
, &zl
) < 0) {
2124 if (IS_ZEBRA_DEBUG_RECV
)
2125 zlog_debug("%s: Unable to decode zapi_labels sent",
2133 /* Validate; will debug on failure */
2134 if (zapi_labels_validate(&zl
) < 0)
2137 ret
= mpls_zapi_labels_process(true, zvrf
, &zl
);
2139 if (IS_ZEBRA_DEBUG_RECV
)
2140 zlog_debug("%s: Error processing zapi request",
2146 * Handle request to delete an MPLS LSP.
2148 * An LSP is identified by its type and local label. When the received message
2149 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2150 * listed LSP nexthops (aka NHLFEs) are deleted.
2152 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2153 * FEC (route) nexthops are deleted.
2155 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2158 struct zapi_labels zl
;
2161 /* Get input stream. */
2163 if (zapi_labels_decode(s
, &zl
) < 0) {
2164 if (IS_ZEBRA_DEBUG_RECV
)
2165 zlog_debug("%s: Unable to decode zapi_labels sent",
2173 if (zl
.nexthop_num
> 0) {
2174 ret
= mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2176 if (IS_ZEBRA_DEBUG_RECV
)
2177 zlog_debug("%s: Error processing zapi request",
2181 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2183 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2184 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2185 zl
.route
.type
, zl
.route
.instance
);
2190 * Handle request to add an MPLS LSP or change an existing one.
2192 * A single message can fully specify an LSP with multiple nexthops.
2194 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2195 * updated to use the received label(s).
2197 * NOTE: zebra will use route replace semantics (make-before-break) to update
2198 * the LSP in the forwarding plane if that's supported by the underlying
2201 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2204 struct zapi_labels zl
;
2206 /* Get input stream. */
2208 if (zapi_labels_decode(s
, &zl
) < 0) {
2209 if (IS_ZEBRA_DEBUG_RECV
)
2210 zlog_debug("%s: Unable to decode zapi_labels sent",
2218 /* Validate; will debug on failure */
2219 if (zapi_labels_validate(&zl
) < 0)
2222 /* This removes everything, then re-adds from the client's
2223 * zapi message. Since the LSP will be processed later, on this
2224 * this same pthread, all of the changes will 'appear' at once.
2226 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2227 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2228 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2229 zl
.route
.type
, zl
.route
.instance
);
2231 mpls_zapi_labels_process(true, zvrf
, &zl
);
2234 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2237 struct zapi_sr_policy zp
;
2238 struct zapi_srte_tunnel
*zt
;
2239 struct zebra_sr_policy
*policy
;
2241 /* Get input stream. */
2243 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2244 if (IS_ZEBRA_DEBUG_RECV
)
2245 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2246 __PRETTY_FUNCTION__
);
2249 zt
= &zp
.segment_list
;
2250 if (zt
->label_num
< 1) {
2251 if (IS_ZEBRA_DEBUG_RECV
)
2253 "%s: SR-TE tunnel must contain at least one label",
2254 __PRETTY_FUNCTION__
);
2261 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2263 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2264 /* TODO: per-VRF list of SR-TE policies. */
2265 policy
->zvrf
= zvrf
;
2267 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2270 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2273 struct zapi_sr_policy zp
;
2274 struct zebra_sr_policy
*policy
;
2276 /* Get input stream. */
2278 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2279 if (IS_ZEBRA_DEBUG_RECV
)
2280 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2281 __PRETTY_FUNCTION__
);
2288 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2290 if (IS_ZEBRA_DEBUG_RECV
)
2291 zlog_debug("%s: Unable to find SR-TE policy",
2292 __PRETTY_FUNCTION__
);
2296 zebra_sr_policy_del(policy
);
2299 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2300 char *name
, int status
)
2302 struct zserv
*client
;
2305 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2307 if (IS_ZEBRA_DEBUG_PACKET
)
2309 "Not notifying pathd about policy %s"
2310 " status change to %d",
2315 if (IS_ZEBRA_DEBUG_PACKET
)
2317 "Notifying pathd about policy %s status change"
2321 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2324 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2325 stream_putl(s
, color
);
2326 stream_put_ipaddr(s
, endpoint
);
2327 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2328 stream_putl(s
, status
);
2330 stream_putw_at(s
, 0, stream_get_endp(s
));
2332 return zserv_send_message(client
, s
);
2335 /* Send response to a table manager connect request to client */
2336 static void zread_table_manager_connect(struct zserv
*client
,
2337 struct stream
*msg
, vrf_id_t vrf_id
)
2342 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2347 STREAM_GETC(s
, proto
);
2348 STREAM_GETW(s
, instance
);
2350 /* accept only dynamic routing protocols */
2351 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2352 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2353 "client %d has wrong protocol %s", client
->sock
,
2354 zebra_route_string(proto
));
2355 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2358 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2359 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2360 zebra_route_string(proto
));
2361 client
->proto
= proto
;
2362 client
->instance
= instance
;
2365 * Release previous labels of same protocol and instance.
2366 * This is done in case it restarted from an unexpected shutdown.
2368 release_daemon_table_chunks(client
);
2370 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2376 static void zread_label_manager_connect(struct zserv
*client
,
2377 struct stream
*msg
, vrf_id_t vrf_id
)
2380 /* type of protocol (lib/zebra.h) */
2382 unsigned short instance
;
2384 /* Get input stream. */
2388 STREAM_GETC(s
, proto
);
2389 STREAM_GETW(s
, instance
);
2391 /* accept only dynamic routing protocols */
2392 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2393 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2394 "client %d has wrong protocol %s", client
->sock
,
2395 zebra_route_string(proto
));
2396 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2400 /* recall proto and instance in this socket */
2401 client
->proto
= proto
;
2402 client
->instance
= instance
;
2404 /* call hook for connection using wrapper */
2405 lm_client_connect_call(client
, vrf_id
);
2411 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2416 uint32_t size
, base
;
2417 struct label_manager_chunk
*lmc
= NULL
;
2419 unsigned short instance
;
2421 /* Get input stream. */
2425 STREAM_GETC(s
, proto
);
2426 STREAM_GETW(s
, instance
);
2427 STREAM_GETC(s
, keep
);
2428 STREAM_GETL(s
, size
);
2429 STREAM_GETL(s
, base
);
2431 assert(proto
== client
->proto
&& instance
== client
->instance
);
2433 /* call hook to get a chunk using wrapper */
2434 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2440 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2443 uint32_t start
, end
;
2445 unsigned short instance
;
2447 /* Get input stream. */
2451 STREAM_GETC(s
, proto
);
2452 STREAM_GETW(s
, instance
);
2453 STREAM_GETL(s
, start
);
2454 STREAM_GETL(s
, end
);
2456 assert(proto
== client
->proto
&& instance
== client
->instance
);
2458 /* call hook to release a chunk using wrapper */
2459 lm_release_chunk_call(client
, start
, end
);
2465 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2467 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2468 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2469 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2471 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2472 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2473 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2474 zread_release_label_chunk(client
, msg
);
2478 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2483 struct table_manager_chunk
*tmc
;
2485 /* Get input stream. */
2489 STREAM_GETL(s
, size
);
2491 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2493 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2494 "%s: Unable to assign Table Chunk of size %u",
2497 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2499 /* send response back */
2500 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2506 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2509 uint32_t start
, end
;
2511 /* Get input stream. */
2515 STREAM_GETL(s
, start
);
2516 STREAM_GETL(s
, end
);
2518 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2524 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2526 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2527 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2528 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2530 /* Sanity: don't allow 'unidentified' requests */
2531 if (!client
->proto
) {
2534 "Got table request from an unidentified client");
2537 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2538 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2539 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2540 zread_release_table_chunk(client
, msg
);
2544 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2547 char ifname
[IF_NAMESIZE
];
2551 union g_addr nexthop
;
2552 uint32_t local_label
;
2553 uint32_t remote_label
;
2555 union pw_protocol_fields data
;
2557 struct zebra_pw
*pw
;
2559 /* Get input stream. */
2563 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2564 ifname
[IF_NAMESIZE
- 1] = '\0';
2565 STREAM_GETL(s
, ifindex
);
2566 STREAM_GETL(s
, type
);
2570 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2573 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2578 STREAM_GETL(s
, local_label
);
2579 STREAM_GETL(s
, remote_label
);
2580 STREAM_GETC(s
, flags
);
2581 STREAM_GET(&data
, s
, sizeof(data
));
2582 protocol
= client
->proto
;
2584 pw
= zebra_pw_find(zvrf
, ifname
);
2585 switch (hdr
->command
) {
2588 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2589 "%s: pseudowire %s already exists [%s]",
2591 zserv_command_string(hdr
->command
));
2595 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2597 case ZEBRA_PW_DELETE
:
2599 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2600 "%s: pseudowire %s not found [%s]", __func__
,
2601 ifname
, zserv_command_string(hdr
->command
));
2605 zebra_pw_del(zvrf
, pw
);
2608 case ZEBRA_PW_UNSET
:
2610 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2611 "%s: pseudowire %s not found [%s]", __func__
,
2612 ifname
, zserv_command_string(hdr
->command
));
2616 switch (hdr
->command
) {
2620 case ZEBRA_PW_UNSET
:
2625 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2626 remote_label
, flags
, &data
);
2634 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2636 struct interface
*master
;
2637 struct interface
*slave
;
2638 struct stream
*s
= msg
;
2642 STREAM_GETL(s
, vrf_id
);
2643 STREAM_GETL(s
, ifindex
);
2644 master
= if_lookup_by_index(ifindex
, vrf_id
);
2646 STREAM_GETL(s
, vrf_id
);
2647 STREAM_GETL(s
, ifindex
);
2648 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2650 if (!master
|| !slave
)
2653 kernel_interface_set_master(master
, slave
);
2660 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2662 struct interface
*ifp
;
2663 mpls_label_t nlabel
;
2666 struct zebra_vrf
*def_zvrf
;
2667 enum lsp_types_t ltype
;
2670 STREAM_GETL(s
, nlabel
);
2671 STREAM_GETC(s
, afi
);
2673 if (!(IS_VALID_AFI(afi
))) {
2674 zlog_warn("Invalid AFI for VRF label: %u", afi
);
2678 if (nlabel
== zvrf
->label
[afi
]) {
2680 * Nothing to do here move along
2685 STREAM_GETC(s
, ltype
);
2687 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2688 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2690 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2693 zlog_debug("Unable to find specified Interface for %s",
2698 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2700 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2704 really_remove
= true;
2705 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2706 if (scrubber
== afi
)
2709 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2712 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2713 really_remove
= false;
2719 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2720 NEXTHOP_TYPE_IFINDEX
, NULL
,
2721 ifp
->ifindex
, false /*backup*/);
2724 if (nlabel
!= MPLS_LABEL_NONE
) {
2725 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2726 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2727 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2730 zvrf
->label
[afi
] = nlabel
;
2735 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2737 struct zebra_pbr_rule zpr
;
2742 STREAM_GETL(s
, total
);
2744 for (i
= 0; i
< total
; i
++) {
2745 memset(&zpr
, 0, sizeof(zpr
));
2747 zpr
.sock
= client
->sock
;
2748 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2749 STREAM_GETL(s
, zpr
.rule
.seq
);
2750 STREAM_GETL(s
, zpr
.rule
.priority
);
2751 STREAM_GETL(s
, zpr
.rule
.unique
);
2752 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2753 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2754 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2755 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2756 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2757 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2758 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2759 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2760 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2761 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2762 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
2763 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2764 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2765 STREAM_GETL(s
, zpr
.rule
.ifindex
);
2767 if (zpr
.rule
.ifindex
) {
2768 struct interface
*ifp
;
2770 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
,
2773 zlog_debug("Failed to lookup ifindex: %u",
2778 strlcpy(zpr
.ifname
, ifp
->name
, sizeof(zpr
.ifname
));
2781 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2782 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2784 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2785 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2787 if (zpr
.rule
.filter
.src_port
)
2788 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2790 if (zpr
.rule
.filter
.dst_port
)
2791 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2793 if (zpr
.rule
.filter
.dsfield
)
2794 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
2796 if (zpr
.rule
.filter
.fwmark
)
2797 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2799 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
2800 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
2802 "Unsupported PBR source IP family: %s (%hhu)",
2803 family2str(zpr
.rule
.filter
.src_ip
.family
),
2804 zpr
.rule
.filter
.src_ip
.family
);
2807 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
2808 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
2810 "Unsupported PBR destination IP family: %s (%hhu)",
2811 family2str(zpr
.rule
.filter
.dst_ip
.family
),
2812 zpr
.rule
.filter
.dst_ip
.family
);
2817 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2818 if (hdr
->command
== ZEBRA_RULE_ADD
)
2819 zebra_pbr_add_rule(&zpr
);
2821 zebra_pbr_del_rule(&zpr
);
2828 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2830 struct zebra_pbr_ipset zpi
;
2835 STREAM_GETL(s
, total
);
2837 for (i
= 0; i
< total
; i
++) {
2838 memset(&zpi
, 0, sizeof(zpi
));
2840 zpi
.sock
= client
->sock
;
2841 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
2842 STREAM_GETL(s
, zpi
.unique
);
2843 STREAM_GETL(s
, zpi
.type
);
2844 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2846 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
2847 zebra_pbr_create_ipset(&zpi
);
2849 zebra_pbr_destroy_ipset(&zpi
);
2856 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
2858 struct zebra_pbr_ipset_entry zpi
;
2859 struct zebra_pbr_ipset ipset
;
2864 STREAM_GETL(s
, total
);
2866 for (i
= 0; i
< total
; i
++) {
2867 memset(&zpi
, 0, sizeof(zpi
));
2868 memset(&ipset
, 0, sizeof(ipset
));
2870 zpi
.sock
= client
->sock
;
2871 STREAM_GETL(s
, zpi
.unique
);
2872 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2873 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
2874 STREAM_GETC(s
, zpi
.src
.family
);
2875 STREAM_GETC(s
, zpi
.src
.prefixlen
);
2876 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
2877 STREAM_GETC(s
, zpi
.dst
.family
);
2878 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
2879 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
2881 STREAM_GETW(s
, zpi
.src_port_min
);
2882 STREAM_GETW(s
, zpi
.src_port_max
);
2883 STREAM_GETW(s
, zpi
.dst_port_min
);
2884 STREAM_GETW(s
, zpi
.dst_port_max
);
2885 STREAM_GETC(s
, zpi
.proto
);
2886 if (!is_default_prefix(&zpi
.src
))
2887 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
2889 if (!is_default_prefix(&zpi
.dst
))
2890 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
2891 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2892 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
2893 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
2894 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2895 if (zpi
.dst_port_max
!= 0)
2896 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
2897 if (zpi
.src_port_max
!= 0)
2898 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
2900 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
2902 if (!(zpi
.dst
.family
== AF_INET
2903 || zpi
.dst
.family
== AF_INET6
)) {
2905 "Unsupported PBR destination IP family: %s (%hhu)",
2906 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
2907 goto stream_failure
;
2909 if (!(zpi
.src
.family
== AF_INET
2910 || zpi
.src
.family
== AF_INET6
)) {
2912 "Unsupported PBR source IP family: %s (%hhu)",
2913 family2str(zpi
.src
.family
), zpi
.src
.family
);
2914 goto stream_failure
;
2917 /* calculate backpointer */
2919 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
2921 if (!zpi
.backpointer
) {
2922 zlog_warn("ipset name specified: %s does not exist",
2924 goto stream_failure
;
2927 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
2928 zebra_pbr_add_ipset_entry(&zpi
);
2930 zebra_pbr_del_ipset_entry(&zpi
);
2937 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
2939 struct zebra_pbr_iptable
*zpi
=
2940 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
2945 zpi
->interface_name_list
= list_new();
2946 zpi
->sock
= client
->sock
;
2947 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
2948 STREAM_GETL(s
, zpi
->unique
);
2949 STREAM_GETL(s
, zpi
->type
);
2950 STREAM_GETL(s
, zpi
->filter_bm
);
2951 STREAM_GETL(s
, zpi
->action
);
2952 STREAM_GETL(s
, zpi
->fwmark
);
2953 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
2954 STREAM_GETW(s
, zpi
->pkt_len_min
);
2955 STREAM_GETW(s
, zpi
->pkt_len_max
);
2956 STREAM_GETW(s
, zpi
->tcp_flags
);
2957 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
2958 STREAM_GETC(s
, zpi
->dscp_value
);
2959 STREAM_GETC(s
, zpi
->fragment
);
2960 STREAM_GETC(s
, zpi
->protocol
);
2961 STREAM_GETL(s
, zpi
->nb_interface
);
2962 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
2964 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
2965 zebra_pbr_add_iptable(zpi
);
2967 zebra_pbr_del_iptable(zpi
);
2970 zebra_pbr_iptable_free(zpi
);
2975 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
2979 struct interface
*ifp
;
2985 STREAM_GETL(s
, ifindex
);
2987 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
2989 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
2993 STREAM_GETC(s
, p
.family
);
2994 STREAM_GETC(s
, p
.prefixlen
);
2995 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
2997 if (p
.family
== AF_INET
)
3002 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3004 dplane_neigh_discover(ifp
, &ip
);
3010 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3011 struct zmsghdr
*bad_hdr
)
3014 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3016 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3018 zserv_encode_error(s
, error
);
3020 client
->error_cnt
++;
3021 zserv_send_message(client
, s
);
3024 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3026 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3027 zlog_debug("ZAPI message specifies unknown VRF: %d",
3030 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3033 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3035 zlog_info("Zebra received unknown command %d", hdr
->command
);
3037 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3040 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3041 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3042 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3043 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3044 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3045 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3046 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3047 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3048 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3049 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3050 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3051 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3052 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
3053 [ZEBRA_HELLO
] = zread_hello
,
3054 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3055 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3056 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
3057 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
3058 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3059 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3060 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3062 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3063 #endif /* HAVE_BFDD */
3064 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3065 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3066 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3067 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3068 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3069 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3070 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3071 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3072 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3073 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3074 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3075 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3076 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3077 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3078 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3079 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3080 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3081 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3082 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3083 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3084 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3085 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3086 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3087 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
3088 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
3089 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3090 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3091 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3092 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3093 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3094 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3095 [ZEBRA_PW_SET
] = zread_pseudowire
,
3096 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3097 [ZEBRA_RULE_ADD
] = zread_rule
,
3098 [ZEBRA_RULE_DELETE
] = zread_rule
,
3099 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3100 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3101 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3102 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3103 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3104 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3105 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3106 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3107 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3108 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3109 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3110 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3111 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3112 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3113 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3114 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
};
3116 #if defined(HANDLE_ZAPI_FUZZING)
3117 extern struct zebra_privs_t zserv_privs
;
3119 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
3121 char fname
[MAXPATHLEN
];
3122 struct stream
*copy
;
3125 copy
= stream_dup(orig
);
3126 stream_set_getp(copy
, 0);
3128 snprintf(fname
, MAXPATHLEN
, "%s/%u", frr_vtydir
, command
);
3130 frr_with_privs(&zserv_privs
) {
3131 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
3133 stream_flush(copy
, fd
);
3140 * Process a batch of zapi messages.
3142 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3145 struct zebra_vrf
*zvrf
;
3147 struct stream_fifo temp_fifo
;
3149 stream_fifo_init(&temp_fifo
);
3151 while (stream_fifo_head(fifo
)) {
3152 msg
= stream_fifo_pop(fifo
);
3154 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3155 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3157 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3158 STREAM_READABLE(msg
),
3159 ZEBRA_MAX_PACKET_SIZ
);
3163 zapi_parse_header(msg
, &hdr
);
3165 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3166 zserv_log_message(NULL
, msg
, &hdr
);
3168 #if defined(HANDLE_ZAPI_FUZZING)
3169 zserv_write_incoming(msg
, hdr
.command
);
3172 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3174 /* Before checking for a handler function, check for
3175 * special messages that are handled in another module;
3176 * we'll treat these as opaque.
3178 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3179 /* Reset message buffer */
3180 stream_set_getp(msg
, 0);
3182 stream_fifo_push(&temp_fifo
, msg
);
3184 /* Continue without freeing the message */
3190 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3192 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3196 if (hdr
.command
>= array_size(zserv_handlers
)
3197 || zserv_handlers
[hdr
.command
] == NULL
) {
3198 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3202 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3208 /* Dispatch any special messages from the temp fifo */
3209 if (stream_fifo_head(&temp_fifo
) != NULL
)
3210 zebra_opaque_enqueue_batch(&temp_fifo
);
3212 stream_fifo_deinit(&temp_fifo
);