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 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
649 __func__
, zserv_command_string(cmd
),
650 zebra_route_string(client
->proto
),
651 zebra_route_string(api
.type
), api
.vrf_id
,
653 return zserv_send_message(client
, s
);
657 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
658 * nexthop is not found on mrib. Returns both route metric and protocol
661 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
663 struct route_entry
*re
,
664 struct zebra_vrf
*zvrf
)
669 struct nexthop
*nexthop
;
671 /* Get output stream. */
672 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
675 /* Fill in result. */
676 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
677 stream_put_in_addr(s
, &addr
);
680 stream_putc(s
, re
->distance
);
681 stream_putl(s
, re
->metric
);
683 /* remember position for nexthop_num */
684 nump
= stream_get_endp(s
);
685 /* reserve room for nexthop_num */
688 * Only non-recursive routes are elegible to resolve the
689 * nexthop we are looking up. Therefore, we will just iterate
690 * over the top chain of nexthops.
692 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
693 nexthop
= nexthop
->next
)
694 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
695 num
+= zserv_encode_nexthop(s
, nexthop
);
697 /* store nexthop_num */
698 stream_putc_at(s
, nump
, num
);
700 stream_putc(s
, 0); /* distance */
701 stream_putl(s
, 0); /* metric */
702 stream_putc(s
, 0); /* nexthop_num */
705 stream_putw_at(s
, 0, stream_get_endp(s
));
707 return zserv_send_message(client
, s
);
710 static int nhg_notify(uint16_t type
, uint16_t instance
, uint32_t id
,
711 enum zapi_nhg_notify_owner note
)
713 struct zserv
*client
;
716 client
= zserv_find_client(type
, instance
);
718 if (IS_ZEBRA_DEBUG_PACKET
) {
719 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
720 type
, instance
, id
, note
);
725 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
728 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
730 stream_put(s
, ¬e
, sizeof(note
));
733 stream_putw_at(s
, 0, stream_get_endp(s
));
735 return zserv_send_message(client
, s
);
739 * Common utility send route notification, called from a path using a
740 * route_entry and from a path using a dataplane context.
742 static int route_notify_internal(const struct prefix
*p
, int type
,
743 uint16_t instance
, vrf_id_t vrf_id
,
745 enum zapi_route_notify_owner note
)
747 struct zserv
*client
;
751 client
= zserv_find_client(type
, instance
);
752 if (!client
|| !client
->notify_owner
) {
753 if (IS_ZEBRA_DEBUG_PACKET
)
755 "Not Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
756 zebra_route_string(type
), p
, table_id
, note
,
761 if (IS_ZEBRA_DEBUG_PACKET
)
763 "Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
764 zebra_route_string(type
), p
, table_id
, note
, vrf_id
);
766 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
769 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
771 stream_put(s
, ¬e
, sizeof(note
));
773 stream_putc(s
, p
->family
);
775 blen
= prefix_blen(p
);
776 stream_putc(s
, p
->prefixlen
);
777 stream_put(s
, &p
->u
.prefix
, blen
);
779 stream_putl(s
, table_id
);
781 stream_putw_at(s
, 0, stream_get_endp(s
));
783 return zserv_send_message(client
, s
);
786 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
787 enum zapi_route_notify_owner note
)
789 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
794 * Route-owner notification using info from dataplane update context.
796 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
797 enum zapi_route_notify_owner note
)
799 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
800 dplane_ctx_get_type(ctx
),
801 dplane_ctx_get_instance(ctx
),
802 dplane_ctx_get_vrf(ctx
),
803 dplane_ctx_get_table(ctx
),
807 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
808 enum zapi_rule_notify_owner note
)
810 struct listnode
*node
;
811 struct zserv
*client
;
814 if (IS_ZEBRA_DEBUG_PACKET
)
815 zlog_debug("%s: Notifying %u", __func__
,
816 dplane_ctx_rule_get_unique(ctx
));
818 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
819 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
826 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
828 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
829 stream_put(s
, ¬e
, sizeof(note
));
830 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
831 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
832 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
833 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
835 stream_putw_at(s
, 0, stream_get_endp(s
));
837 zserv_send_message(client
, s
);
840 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
841 enum zapi_ipset_notify_owner note
)
843 struct listnode
*node
;
844 struct zserv
*client
;
847 if (IS_ZEBRA_DEBUG_PACKET
)
848 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
850 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
851 if (ipset
->sock
== client
->sock
)
858 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
860 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
861 stream_put(s
, ¬e
, sizeof(note
));
862 stream_putl(s
, ipset
->unique
);
863 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
864 stream_putw_at(s
, 0, stream_get_endp(s
));
866 zserv_send_message(client
, s
);
869 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
870 enum zapi_ipset_entry_notify_owner note
)
872 struct listnode
*node
;
873 struct zserv
*client
;
876 if (IS_ZEBRA_DEBUG_PACKET
)
877 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
879 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
880 if (ipset
->sock
== client
->sock
)
887 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
889 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
890 stream_put(s
, ¬e
, sizeof(note
));
891 stream_putl(s
, ipset
->unique
);
892 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
893 stream_putw_at(s
, 0, stream_get_endp(s
));
895 zserv_send_message(client
, s
);
898 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
899 enum zapi_iptable_notify_owner note
)
901 struct listnode
*node
;
902 struct zserv
*client
;
905 if (IS_ZEBRA_DEBUG_PACKET
)
906 zlog_debug("%s: Notifying %u", __func__
, iptable
->unique
);
908 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
909 if (iptable
->sock
== client
->sock
)
916 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
918 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
919 stream_put(s
, ¬e
, sizeof(note
));
920 stream_putl(s
, iptable
->unique
);
921 stream_putw_at(s
, 0, stream_get_endp(s
));
923 zserv_send_message(client
, s
);
926 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
927 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
933 /* Check this client need interface information. */
934 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
937 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
940 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
942 /* Prefix information. */
943 stream_putc(s
, p
->family
);
944 blen
= prefix_blen(p
);
945 stream_put(s
, &p
->u
.prefix
, blen
);
946 stream_putc(s
, p
->prefixlen
);
948 /* Write packet size. */
949 stream_putw_at(s
, 0, stream_get_endp(s
));
951 return zserv_send_message(client
, s
);
955 * Function used by Zebra to send a PW status update to LDP daemon
957 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
959 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
961 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
962 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
963 stream_putl(s
, pw
->ifindex
);
964 stream_putl(s
, pw
->status
);
966 /* Put length at the first point of the stream. */
967 stream_putw_at(s
, 0, stream_get_endp(s
));
969 return zserv_send_message(client
, s
);
972 /* Send response to a get label chunk request to client */
973 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
974 struct label_manager_chunk
*lmc
)
977 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
979 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
981 stream_putc(s
, client
->proto
);
983 stream_putw(s
, client
->instance
);
987 stream_putc(s
, lmc
->keep
);
988 /* start and end labels */
989 stream_putl(s
, lmc
->start
);
990 stream_putl(s
, lmc
->end
);
993 /* Write packet size. */
994 stream_putw_at(s
, 0, stream_get_endp(s
));
996 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1001 /* Send response to a label manager connect request to client */
1002 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1003 unsigned short result
)
1006 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1008 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1011 stream_putc(s
, client
->proto
);
1014 stream_putw(s
, client
->instance
);
1017 stream_putc(s
, result
);
1019 /* Write packet size. */
1020 stream_putw_at(s
, 0, stream_get_endp(s
));
1022 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1028 /* Send response to a get table chunk request to client */
1029 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1031 struct table_manager_chunk
*tmc
)
1033 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1035 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1038 /* start and end labels */
1039 stream_putl(s
, tmc
->start
);
1040 stream_putl(s
, tmc
->end
);
1043 /* Write packet size. */
1044 stream_putw_at(s
, 0, stream_get_endp(s
));
1046 return zserv_send_message(client
, s
);
1049 static int zsend_table_manager_connect_response(struct zserv
*client
,
1053 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1055 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1058 stream_putc(s
, result
);
1060 stream_putw_at(s
, 0, stream_get_endp(s
));
1062 return zserv_send_message(client
, s
);
1065 /* Inbound message handling ------------------------------------------------ */
1067 const int cmd2type
[] = {
1068 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1069 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1070 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1071 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1074 /* Nexthop register */
1075 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1080 unsigned short l
= 0;
1082 uint16_t type
= cmd2type
[hdr
->command
];
1084 bool flag_changed
= false;
1087 if (IS_ZEBRA_DEBUG_NHT
)
1089 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1090 zebra_route_string(client
->proto
), hdr
->length
,
1091 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1096 if (!client
->nh_reg_time
)
1097 client
->nh_reg_time
= monotime(NULL
);
1099 while (l
< hdr
->length
) {
1100 STREAM_GETC(s
, flags
);
1101 STREAM_GETW(s
, p
.family
);
1102 STREAM_GETC(s
, p
.prefixlen
);
1104 if (p
.family
== AF_INET
) {
1105 client
->v4_nh_watch_add_cnt
++;
1106 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1108 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1109 __func__
, p
.prefixlen
);
1112 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1113 l
+= IPV4_MAX_BYTELEN
;
1114 } else if (p
.family
== AF_INET6
) {
1115 client
->v6_nh_watch_add_cnt
++;
1116 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1118 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1119 __func__
, p
.prefixlen
);
1122 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1123 l
+= IPV6_MAX_BYTELEN
;
1126 EC_ZEBRA_UNKNOWN_FAMILY
,
1127 "rnh_register: Received unknown family type %d\n",
1131 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1135 orig_flags
= rnh
->flags
;
1136 if (type
== RNH_NEXTHOP_TYPE
) {
1138 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1139 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1141 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1142 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1143 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1145 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1146 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1148 && CHECK_FLAG(rnh
->flags
,
1149 ZEBRA_NHT_EXACT_MATCH
))
1150 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1153 if (orig_flags
!= rnh
->flags
)
1154 flag_changed
= true;
1156 /* Anything not AF_INET/INET6 has been filtered out above */
1157 if (!exist
|| flag_changed
)
1158 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1161 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1168 /* Nexthop register */
1169 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1174 unsigned short l
= 0;
1175 uint16_t type
= cmd2type
[hdr
->command
];
1177 if (IS_ZEBRA_DEBUG_NHT
)
1179 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1180 zebra_route_string(client
->proto
), hdr
->length
,
1185 while (l
< hdr
->length
) {
1188 STREAM_GETC(s
, flags
);
1190 goto stream_failure
;
1192 STREAM_GETW(s
, p
.family
);
1193 STREAM_GETC(s
, p
.prefixlen
);
1195 if (p
.family
== AF_INET
) {
1196 client
->v4_nh_watch_rem_cnt
++;
1197 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1199 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1200 __func__
, p
.prefixlen
);
1203 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1204 l
+= IPV4_MAX_BYTELEN
;
1205 } else if (p
.family
== AF_INET6
) {
1206 client
->v6_nh_watch_rem_cnt
++;
1207 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1209 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1210 __func__
, p
.prefixlen
);
1213 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1214 l
+= IPV6_MAX_BYTELEN
;
1217 EC_ZEBRA_UNKNOWN_FAMILY
,
1218 "rnh_register: Received unknown family type %d\n",
1222 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1224 client
->nh_dereg_time
= monotime(NULL
);
1225 zebra_remove_rnh_client(rnh
, client
, type
);
1232 #define ZEBRA_MIN_FEC_LENGTH 5
1235 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1238 unsigned short l
= 0;
1241 uint32_t label
= MPLS_INVALID_LABEL
;
1242 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1245 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1250 * The minimum amount of data that can be sent for one fec
1253 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1255 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1256 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1261 while (l
< hdr
->length
) {
1262 STREAM_GETW(s
, flags
);
1263 memset(&p
, 0, sizeof(p
));
1264 STREAM_GETW(s
, p
.family
);
1265 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1267 EC_ZEBRA_UNKNOWN_FAMILY
,
1268 "fec_register: Received unknown family type %d\n",
1272 STREAM_GETC(s
, p
.prefixlen
);
1273 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1274 || (p
.family
== AF_INET6
1275 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1277 "%s: Specified prefix hdr->length: %d is to long for %d",
1278 __func__
, p
.prefixlen
, p
.family
);
1282 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1283 l
+= PSIZE(p
.prefixlen
);
1284 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1285 STREAM_GETL(s
, label
);
1287 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1288 STREAM_GETL(s
, label_index
);
1292 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1299 /* FEC unregister */
1300 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1303 unsigned short l
= 0;
1308 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1313 * The minimum amount of data that can be sent for one
1314 * fec unregistration
1316 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1318 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1319 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1324 while (l
< hdr
->length
) {
1325 STREAM_GETW(s
, flags
);
1327 goto stream_failure
;
1329 memset(&p
, 0, sizeof(p
));
1330 STREAM_GETW(s
, p
.family
);
1331 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1333 EC_ZEBRA_UNKNOWN_FAMILY
,
1334 "fec_unregister: Received unknown family type %d\n",
1338 STREAM_GETC(s
, p
.prefixlen
);
1339 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1340 || (p
.family
== AF_INET6
1341 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1343 "%s: Received prefix hdr->length %d which is greater than %d can support",
1344 __func__
, p
.prefixlen
, p
.family
);
1348 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1349 l
+= PSIZE(p
.prefixlen
);
1350 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1359 * Register zebra server interface information.
1360 * Send current all interface and address information.
1362 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1365 struct interface
*ifp
;
1367 vrf_id_t vrf_id
= zvrf_id(zvrf
);
1368 if (vrf_id
!= VRF_DEFAULT
&& vrf_id
!= VRF_UNKNOWN
) {
1369 FOR_ALL_INTERFACES (zvrf
->vrf
, ifp
) {
1370 /* Skip pseudo interface. */
1371 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1374 zsend_interface_add(client
, ifp
);
1375 zsend_interface_link_params(client
, ifp
);
1376 zsend_interface_addresses(client
, ifp
);
1381 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1382 FOR_ALL_INTERFACES (vrf
, ifp
) {
1383 /* Skip pseudo interface. */
1384 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1387 zsend_interface_add(client
, ifp
);
1388 zsend_interface_link_params(client
, ifp
);
1389 zsend_interface_addresses(client
, ifp
);
1394 /* Unregister zebra server interface information. */
1395 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1400 * Handle message requesting interface be set up or down.
1402 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1405 struct interface
*ifp
;
1408 STREAM_GETL(msg
, ifindex
);
1409 STREAM_GETC(msg
, down
);
1412 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1415 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1416 ifindex
, down
? "on" : "off");
1417 zebra_if_set_protodown(ifp
, down
);
1420 "Cannot set protodown %s for interface %u; does not exist",
1421 down
? "on" : "off", ifindex
);
1429 bool zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1430 const unsigned int nexthop_num
)
1432 if (nexthop_num
> zrouter
.multipath_num
) {
1433 char buff
[PREFIX2STR_BUFFER
];
1436 prefix2str(p
, buff
, sizeof(buff
));
1439 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1440 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1441 caller
, (p
? buff
: "(NULL)"), nexthop_num
,
1442 zrouter
.multipath_num
);
1450 * Create a new nexthop based on a zapi nexthop.
1452 static struct nexthop
*nexthop_from_zapi(const struct zapi_nexthop
*api_nh
,
1453 uint32_t flags
, struct prefix
*p
,
1454 uint16_t backup_nexthop_num
)
1456 struct nexthop
*nexthop
= NULL
;
1457 struct ipaddr vtep_ip
;
1458 struct interface
*ifp
;
1460 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1462 switch (api_nh
->type
) {
1463 case NEXTHOP_TYPE_IFINDEX
:
1464 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1466 case NEXTHOP_TYPE_IPV4
:
1467 if (IS_ZEBRA_DEBUG_RECV
) {
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
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1476 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1477 if (IS_ZEBRA_DEBUG_RECV
) {
1478 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1480 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1481 __func__
, nhbuf
, api_nh
->vrf_id
,
1485 nexthop
= nexthop_from_ipv4_ifindex(
1486 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1489 /* Special handling for IPv4 routes sourced from EVPN:
1490 * the nexthop and associated MAC need to be installed.
1492 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1493 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1494 vtep_ip
.ipa_type
= IPADDR_V4
;
1495 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1496 sizeof(struct in_addr
));
1497 zebra_vxlan_evpn_vrf_route_add(
1498 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1501 case NEXTHOP_TYPE_IPV6
:
1502 if (IS_ZEBRA_DEBUG_RECV
) {
1503 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1505 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1506 nhbuf
, api_nh
->vrf_id
);
1508 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1510 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1511 if (IS_ZEBRA_DEBUG_RECV
) {
1512 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1514 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1515 __func__
, nhbuf
, api_nh
->vrf_id
,
1518 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1522 /* Special handling for IPv6 routes sourced from EVPN:
1523 * the nexthop and associated MAC need to be installed.
1525 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1526 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1527 vtep_ip
.ipa_type
= IPADDR_V6
;
1528 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1529 sizeof(struct in6_addr
));
1530 zebra_vxlan_evpn_vrf_route_add(
1531 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1534 case NEXTHOP_TYPE_BLACKHOLE
:
1535 if (IS_ZEBRA_DEBUG_RECV
)
1536 zlog_debug("%s: nh blackhole %d",
1537 __func__
, api_nh
->bh_type
);
1539 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1543 /* Return early if we couldn't process the zapi nexthop */
1544 if (nexthop
== NULL
) {
1548 /* Mark nexthop as onlink either if client has explicitly told us
1549 * to or if the nexthop is on an 'unnumbered' interface.
1551 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1552 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1553 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1554 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1555 if (ifp
&& connected_is_unnumbered(ifp
))
1556 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1559 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1560 nexthop
->weight
= api_nh
->weight
;
1562 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1563 /* Validate count */
1564 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1565 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1566 zlog_debug("%s: invalid backup nh count %d",
1567 __func__
, api_nh
->backup_num
);
1568 nexthop_free(nexthop
);
1573 /* Copy backup info */
1574 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1575 nexthop
->backup_num
= api_nh
->backup_num
;
1577 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1578 /* Validate backup index */
1579 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1580 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1582 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1583 zlog_debug("%s: invalid backup nh idx %d",
1585 api_nh
->backup_idx
[i
]);
1586 nexthop_free(nexthop
);
1597 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1598 struct zapi_nexthop
*nhops
, uint32_t flags
,
1599 uint32_t message
, uint16_t nexthop_num
,
1600 uint16_t backup_nh_num
,
1601 struct nexthop_group
**png
,
1602 struct nhg_backup_info
**pbnhg
)
1604 struct nexthop_group
*ng
= NULL
;
1605 struct nhg_backup_info
*bnhg
= NULL
;
1607 struct nexthop
*last_nh
= NULL
;
1609 assert(!(png
&& pbnhg
));
1612 ng
= nexthop_group_new();
1614 if (pbnhg
&& backup_nh_num
> 0) {
1615 if (IS_ZEBRA_DEBUG_RECV
)
1616 zlog_debug("%s: adding %d backup nexthops", __func__
,
1619 bnhg
= zebra_nhg_backup_alloc();
1623 * TBD should _all_ of the nexthop add operations use
1624 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1625 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1627 for (i
= 0; i
< nexthop_num
; i
++) {
1628 struct nexthop
*nexthop
;
1629 enum lsp_types_t label_type
;
1630 char nhbuf
[NEXTHOP_STRLEN
];
1631 char labelbuf
[MPLS_LABEL_STRLEN
];
1632 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1634 /* Convert zapi nexthop */
1635 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1638 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1639 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1640 __func__
, nexthop_num
, i
);
1642 nexthop_group_delete(&ng
);
1644 zebra_nhg_backup_free(&bnhg
);
1649 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1650 if (IS_ZEBRA_DEBUG_RECV
) {
1651 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1652 zlog_debug("%s: backup nh %s with BACKUP flag!",
1655 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1656 nexthop
->backup_num
= 0;
1659 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1660 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1661 nexthop
->srte_color
= api_nh
->srte_color
;
1664 /* MPLS labels for BGP-LU or Segment Routing */
1665 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1666 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1667 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1668 && api_nh
->label_num
> 0) {
1670 label_type
= lsp_type_from_re_type(client
->proto
);
1671 nexthop_add_labels(nexthop
, label_type
,
1673 &api_nh
->labels
[0]);
1676 if (IS_ZEBRA_DEBUG_RECV
) {
1680 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1682 if (nexthop
->nh_label
&&
1683 nexthop
->nh_label
->num_labels
> 0) {
1684 mpls_label2str(nexthop
->nh_label
->num_labels
,
1685 nexthop
->nh_label
->label
,
1686 labelbuf
, sizeof(labelbuf
),
1690 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1691 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1695 /* Add new nexthop to temporary list. This list is
1696 * canonicalized - sorted - so that it can be hashed
1697 * later in route processing. We expect that the sender
1698 * has sent the list sorted, and the zapi client api
1699 * attempts to enforce that, so this should be
1700 * inexpensive - but it is necessary to support shared
1703 nexthop_group_add_sorted(ng
, nexthop
);
1706 /* Note that the order of the backup nexthops is
1707 * significant, so we don't sort this list as we do the
1708 * primary nexthops, we just append.
1711 NEXTHOP_APPEND(last_nh
, nexthop
);
1713 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1720 /* succesfully read, set caller pointers now */
1730 int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1733 struct zapi_nexthop
*znh
;
1735 STREAM_GETW(s
, api_nhg
->proto
);
1736 STREAM_GETL(s
, api_nhg
->id
);
1738 if (cmd
== ZEBRA_NHG_DEL
)
1742 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1744 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1747 if (api_nhg
->nexthop_num
<= 0) {
1748 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1749 "%s: No nexthops sent", __func__
);
1753 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1754 znh
= &((api_nhg
->nexthops
)[i
]);
1756 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1757 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1758 "%s: Nexthop creation failed", __func__
);
1763 /* Backup Nexthops */
1764 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1766 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1769 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1770 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1772 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1773 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1774 "%s: Backup Nexthop creation failed",
1785 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1786 "%s: Nexthop Group decode failed with some sort of stream read failure",
1791 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1794 struct zapi_nhg api_nhg
= {};
1795 struct nhg_hash_entry
*nhe
;
1798 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1799 if (IS_ZEBRA_DEBUG_RECV
)
1800 zlog_debug("%s: Unable to decode zapi_nhg sent",
1806 * Delete the received nhg id
1809 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1812 zebra_nhg_decrement_ref(nhe
);
1813 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1816 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1817 ZAPI_NHG_REMOVE_FAIL
);
1820 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1823 struct zapi_nhg api_nhg
= {};
1824 struct nexthop_group
*nhg
= NULL
;
1825 struct nhg_backup_info
*bnhg
= NULL
;
1826 struct nhg_hash_entry
*nhe
;
1829 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1830 if (IS_ZEBRA_DEBUG_RECV
)
1831 zlog_debug("%s: Unable to decode zapi_nhg sent",
1836 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1837 api_nhg
.nexthop_num
,
1838 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1839 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1840 api_nhg
.backup_nexthop_num
,
1841 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1843 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1844 "%s: Nexthop Group Creation failed", __func__
);
1851 nhe
= zebra_nhg_proto_add(api_nhg
.id
, api_nhg
.proto
, nhg
, 0);
1853 nexthop_group_delete(&nhg
);
1854 zebra_nhg_backup_free(&bnhg
);
1858 * Assume fully resolved for now and install.
1860 * Resolution is going to need some more work.
1863 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1864 ZAPI_NHG_INSTALLED
);
1866 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1867 ZAPI_NHG_FAIL_INSTALL
);
1870 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1873 struct zapi_route api
;
1875 struct prefix_ipv6
*src_p
= NULL
;
1876 struct route_entry
*re
;
1877 struct nexthop_group
*ng
= NULL
;
1878 struct nhg_backup_info
*bnhg
= NULL
;
1881 struct nhg_hash_entry nhe
;
1884 if (zapi_route_decode(s
, &api
) < 0) {
1885 if (IS_ZEBRA_DEBUG_RECV
)
1886 zlog_debug("%s: Unable to decode zapi_route sent",
1891 vrf_id
= zvrf_id(zvrf
);
1893 if (IS_ZEBRA_DEBUG_RECV
)
1894 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
1895 __func__
, vrf_id
, api
.tableid
, &api
.prefix
,
1896 (int)api
.message
, api
.flags
);
1898 /* Allocate new route. */
1899 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1900 re
->type
= api
.type
;
1901 re
->instance
= api
.instance
;
1902 re
->flags
= api
.flags
;
1903 re
->uptime
= monotime(NULL
);
1904 re
->vrf_id
= vrf_id
;
1907 re
->table
= api
.tableid
;
1909 re
->table
= zvrf
->table_id
;
1911 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
1912 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1913 || api
.nexthop_num
== 0)) {
1915 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1916 "%s: received a route without nexthops for prefix %pFX from client %s",
1917 __func__
, &api
.prefix
,
1918 zebra_route_string(client
->proto
));
1920 XFREE(MTYPE_RE
, re
);
1924 /* Report misuse of the backup flag */
1925 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
1926 && api
.backup_nexthop_num
== 0) {
1927 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1929 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1930 __func__
, zebra_route_string(client
->proto
),
1934 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
))
1935 re
->nhe_id
= api
.nhgid
;
1938 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
1939 api
.flags
, api
.message
, api
.nexthop_num
,
1940 api
.backup_nexthop_num
, &ng
, NULL
)
1941 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
1942 api
.flags
, api
.message
,
1943 api
.backup_nexthop_num
,
1944 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
1946 nexthop_group_delete(&ng
);
1947 zebra_nhg_backup_free(&bnhg
);
1948 XFREE(MTYPE_RE
, re
);
1952 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1953 re
->distance
= api
.distance
;
1954 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1955 re
->metric
= api
.metric
;
1956 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1958 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1961 afi
= family2afi(api
.prefix
.family
);
1962 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1963 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1964 "%s: Received SRC Prefix but afi is not v6",
1966 nexthop_group_delete(&ng
);
1967 zebra_nhg_backup_free(&bnhg
);
1968 XFREE(MTYPE_RE
, re
);
1971 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1972 src_p
= &api
.src_prefix
;
1974 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
1975 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
1976 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1977 __func__
, api
.safi
);
1978 nexthop_group_delete(&ng
);
1979 zebra_nhg_backup_free(&bnhg
);
1980 XFREE(MTYPE_RE
, re
);
1985 * If we have an ID, this proto owns the NHG it sent along with the
1986 * route, so we just send the ID into rib code with it.
1988 * Havent figured out how to handle backup NHs with this yet, so lets
1989 * keep that separate.
1990 * Include backup info with the route. We use a temporary nhe here;
1991 * if this is a new/unknown nhe, a new copy will be allocated
1995 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
1996 nhe
.nhg
.nexthop
= ng
->nexthop
;
1997 nhe
.backup_info
= bnhg
;
1999 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
,
2002 /* At this point, these allocations are not needed: 're' has been
2003 * retained or freed, and if 're' still exists, it is using
2004 * a reference to a shared group object.
2006 nexthop_group_delete(&ng
);
2008 zebra_nhg_backup_free(&bnhg
);
2011 switch (api
.prefix
.family
) {
2014 client
->v4_route_add_cnt
++;
2016 client
->v4_route_upd8_cnt
++;
2020 client
->v6_route_add_cnt
++;
2022 client
->v6_route_upd8_cnt
++;
2027 static void zread_route_del(ZAPI_HANDLER_ARGS
)
2030 struct zapi_route api
;
2032 struct prefix_ipv6
*src_p
= NULL
;
2036 if (zapi_route_decode(s
, &api
) < 0)
2039 afi
= family2afi(api
.prefix
.family
);
2040 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2041 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2042 "%s: Received a src prefix while afi is not v6",
2046 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2047 src_p
= &api
.src_prefix
;
2050 table_id
= api
.tableid
;
2052 table_id
= zvrf
->table_id
;
2054 if (IS_ZEBRA_DEBUG_RECV
)
2055 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2056 __func__
, zvrf_id(zvrf
), table_id
, &api
.prefix
,
2057 (int)api
.message
, api
.flags
);
2059 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2060 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
2061 api
.distance
, false, false);
2064 switch (api
.prefix
.family
) {
2066 client
->v4_route_del_cnt
++;
2069 client
->v6_route_del_cnt
++;
2074 /* MRIB Nexthop lookup for IPv4. */
2075 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2077 struct in_addr addr
;
2078 struct route_entry
*re
;
2080 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
2081 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
2082 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
2088 /* Register zebra server router-id information. Send current router-id */
2089 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2095 STREAM_GETW(msg
, afi
);
2097 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2099 "Invalid AFI %u while registering for router ID notifications",
2101 goto stream_failure
;
2104 /* Router-id information is needed. */
2105 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2107 router_id_get(afi
, &p
, zvrf
);
2109 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2115 /* Unregister zebra server router-id information. */
2116 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2120 STREAM_GETW(msg
, afi
);
2122 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2124 "Invalid AFI %u while unregistering from router ID notifications",
2126 goto stream_failure
;
2129 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2135 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2137 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2139 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2140 stream_putl(s
, vrf_get_backend());
2141 stream_putc(s
, mpls_enabled
);
2142 stream_putl(s
, zrouter
.multipath_num
);
2143 stream_putc(s
, zebra_mlag_get_role());
2145 stream_putw_at(s
, 0, stream_get_endp(s
));
2146 zserv_send_message(client
, s
);
2149 void zsend_capabilities_all_clients(void)
2151 struct listnode
*node
, *nnode
;
2152 struct zebra_vrf
*zvrf
;
2153 struct zserv
*client
;
2155 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2156 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2157 /* Do not send unsolicited messages to synchronous clients. */
2158 if (client
->synchronous
)
2161 zsend_capabilities(client
, zvrf
);
2165 /* Tie up route-type and client->sock */
2166 static void zread_hello(ZAPI_HANDLER_ARGS
)
2168 /* type of protocol (lib/zebra.h) */
2170 unsigned short instance
;
2172 uint8_t synchronous
;
2173 uint32_t session_id
;
2175 STREAM_GETC(msg
, proto
);
2176 STREAM_GETW(msg
, instance
);
2177 STREAM_GETL(msg
, session_id
);
2178 STREAM_GETC(msg
, notify
);
2179 STREAM_GETC(msg
, synchronous
);
2181 client
->notify_owner
= true;
2184 client
->synchronous
= true;
2186 /* accept only dynamic routing protocols */
2187 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2189 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2190 client
->sock
, zebra_route_string(proto
),
2193 zlog_notice("client protocol instance %d", instance
);
2195 client
->proto
= proto
;
2196 client
->instance
= instance
;
2197 client
->session_id
= session_id
;
2199 /* Graceful restart processing for client connect */
2200 zebra_gr_client_reconnect(client
);
2203 if (!client
->synchronous
) {
2204 zsend_capabilities(client
, zvrf
);
2205 zebra_vrf_update_all(client
);
2211 /* Unregister all information in a VRF. */
2212 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2217 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2218 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2219 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2220 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2221 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2226 * Validate incoming zapi mpls lsp / labels message
2228 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2234 const struct zapi_nexthop
*znh
;
2236 /* Validate backup info: no duplicates for a single primary */
2237 if (zl
->backup_nexthop_num
== 0) {
2242 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2243 znh
= &zl
->nexthops
[j
];
2245 memset(bits
, 0, sizeof(bits
));
2247 for (i
= 0; i
< znh
->backup_num
; i
++) {
2248 idx
= znh
->backup_idx
[i
] / 32;
2250 ival
= 1 << znh
->backup_idx
[i
] % 32;
2252 /* Check whether value is already used */
2253 if (ival
& bits
[idx
]) {
2256 if (IS_ZEBRA_DEBUG_RECV
)
2257 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2259 znh
->backup_idx
[i
]);
2263 /* Mark index value */
2276 * Handle request to create an MPLS LSP.
2278 * A single message can fully specify an LSP with multiple nexthops.
2280 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2281 * updated to use the received label(s).
2283 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2286 struct zapi_labels zl
;
2289 /* Get input stream. */
2291 if (zapi_labels_decode(s
, &zl
) < 0) {
2292 if (IS_ZEBRA_DEBUG_RECV
)
2293 zlog_debug("%s: Unable to decode zapi_labels sent",
2301 /* Validate; will debug on failure */
2302 if (zapi_labels_validate(&zl
) < 0)
2305 ret
= mpls_zapi_labels_process(true, zvrf
, &zl
);
2307 if (IS_ZEBRA_DEBUG_RECV
)
2308 zlog_debug("%s: Error processing zapi request",
2314 * Handle request to delete an MPLS LSP.
2316 * An LSP is identified by its type and local label. When the received message
2317 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2318 * listed LSP nexthops (aka NHLFEs) are deleted.
2320 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2321 * FEC (route) nexthops are deleted.
2323 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2326 struct zapi_labels zl
;
2329 /* Get input stream. */
2331 if (zapi_labels_decode(s
, &zl
) < 0) {
2332 if (IS_ZEBRA_DEBUG_RECV
)
2333 zlog_debug("%s: Unable to decode zapi_labels sent",
2341 if (zl
.nexthop_num
> 0) {
2342 ret
= mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2344 if (IS_ZEBRA_DEBUG_RECV
)
2345 zlog_debug("%s: Error processing zapi request",
2349 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2351 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2352 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2353 zl
.route
.type
, zl
.route
.instance
);
2358 * Handle request to add an MPLS LSP or change an existing one.
2360 * A single message can fully specify an LSP with multiple nexthops.
2362 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2363 * updated to use the received label(s).
2365 * NOTE: zebra will use route replace semantics (make-before-break) to update
2366 * the LSP in the forwarding plane if that's supported by the underlying
2369 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2372 struct zapi_labels zl
;
2374 /* Get input stream. */
2376 if (zapi_labels_decode(s
, &zl
) < 0) {
2377 if (IS_ZEBRA_DEBUG_RECV
)
2378 zlog_debug("%s: Unable to decode zapi_labels sent",
2386 /* Validate; will debug on failure */
2387 if (zapi_labels_validate(&zl
) < 0)
2390 /* This removes everything, then re-adds from the client's
2391 * zapi message. Since the LSP will be processed later, on this
2392 * this same pthread, all of the changes will 'appear' at once.
2394 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2395 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2396 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2397 zl
.route
.type
, zl
.route
.instance
);
2399 mpls_zapi_labels_process(true, zvrf
, &zl
);
2402 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2405 struct zapi_sr_policy zp
;
2406 struct zapi_srte_tunnel
*zt
;
2407 struct zebra_sr_policy
*policy
;
2409 /* Get input stream. */
2411 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2412 if (IS_ZEBRA_DEBUG_RECV
)
2413 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2414 __PRETTY_FUNCTION__
);
2417 zt
= &zp
.segment_list
;
2418 if (zt
->label_num
< 1) {
2419 if (IS_ZEBRA_DEBUG_RECV
)
2421 "%s: SR-TE tunnel must contain at least one label",
2422 __PRETTY_FUNCTION__
);
2429 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2431 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2432 /* TODO: per-VRF list of SR-TE policies. */
2433 policy
->zvrf
= zvrf
;
2435 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2438 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2441 struct zapi_sr_policy zp
;
2442 struct zebra_sr_policy
*policy
;
2444 /* Get input stream. */
2446 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2447 if (IS_ZEBRA_DEBUG_RECV
)
2448 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2449 __PRETTY_FUNCTION__
);
2456 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2458 if (IS_ZEBRA_DEBUG_RECV
)
2459 zlog_debug("%s: Unable to find SR-TE policy",
2460 __PRETTY_FUNCTION__
);
2464 zebra_sr_policy_del(policy
);
2467 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2468 char *name
, int status
)
2470 struct zserv
*client
;
2473 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2475 if (IS_ZEBRA_DEBUG_PACKET
)
2477 "Not notifying pathd about policy %s"
2478 " status change to %d",
2483 if (IS_ZEBRA_DEBUG_PACKET
)
2485 "Notifying pathd about policy %s status change"
2489 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2492 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2493 stream_putl(s
, color
);
2494 stream_put_ipaddr(s
, endpoint
);
2495 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2496 stream_putl(s
, status
);
2498 stream_putw_at(s
, 0, stream_get_endp(s
));
2500 return zserv_send_message(client
, s
);
2503 /* Send response to a table manager connect request to client */
2504 static void zread_table_manager_connect(struct zserv
*client
,
2505 struct stream
*msg
, vrf_id_t vrf_id
)
2510 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2515 STREAM_GETC(s
, proto
);
2516 STREAM_GETW(s
, instance
);
2518 /* accept only dynamic routing protocols */
2519 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2520 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2521 "client %d has wrong protocol %s", client
->sock
,
2522 zebra_route_string(proto
));
2523 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2526 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2527 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2528 zebra_route_string(proto
));
2529 client
->proto
= proto
;
2530 client
->instance
= instance
;
2533 * Release previous labels of same protocol and instance.
2534 * This is done in case it restarted from an unexpected shutdown.
2536 release_daemon_table_chunks(client
);
2538 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2544 static void zread_label_manager_connect(struct zserv
*client
,
2545 struct stream
*msg
, vrf_id_t vrf_id
)
2548 /* type of protocol (lib/zebra.h) */
2550 unsigned short instance
;
2552 /* Get input stream. */
2556 STREAM_GETC(s
, proto
);
2557 STREAM_GETW(s
, instance
);
2559 /* accept only dynamic routing protocols */
2560 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2561 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2562 "client %d has wrong protocol %s", client
->sock
,
2563 zebra_route_string(proto
));
2564 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2568 /* recall proto and instance in this socket */
2569 client
->proto
= proto
;
2570 client
->instance
= instance
;
2572 /* call hook for connection using wrapper */
2573 lm_client_connect_call(client
, vrf_id
);
2579 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2584 uint32_t size
, base
;
2585 struct label_manager_chunk
*lmc
= NULL
;
2587 unsigned short instance
;
2589 /* Get input stream. */
2593 STREAM_GETC(s
, proto
);
2594 STREAM_GETW(s
, instance
);
2595 STREAM_GETC(s
, keep
);
2596 STREAM_GETL(s
, size
);
2597 STREAM_GETL(s
, base
);
2599 assert(proto
== client
->proto
&& instance
== client
->instance
);
2601 /* call hook to get a chunk using wrapper */
2602 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2608 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2611 uint32_t start
, end
;
2613 unsigned short instance
;
2615 /* Get input stream. */
2619 STREAM_GETC(s
, proto
);
2620 STREAM_GETW(s
, instance
);
2621 STREAM_GETL(s
, start
);
2622 STREAM_GETL(s
, end
);
2624 assert(proto
== client
->proto
&& instance
== client
->instance
);
2626 /* call hook to release a chunk using wrapper */
2627 lm_release_chunk_call(client
, start
, end
);
2633 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2635 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2636 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2637 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2639 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2640 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2641 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2642 zread_release_label_chunk(client
, msg
);
2646 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2651 struct table_manager_chunk
*tmc
;
2653 /* Get input stream. */
2657 STREAM_GETL(s
, size
);
2659 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2661 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2662 "%s: Unable to assign Table Chunk of size %u",
2665 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2667 /* send response back */
2668 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2674 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2677 uint32_t start
, end
;
2679 /* Get input stream. */
2683 STREAM_GETL(s
, start
);
2684 STREAM_GETL(s
, end
);
2686 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2692 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2694 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2695 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2696 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2698 /* Sanity: don't allow 'unidentified' requests */
2699 if (!client
->proto
) {
2702 "Got table request from an unidentified client");
2705 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2706 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2707 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2708 zread_release_table_chunk(client
, msg
);
2712 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2715 char ifname
[IF_NAMESIZE
];
2719 union g_addr nexthop
;
2720 uint32_t local_label
;
2721 uint32_t remote_label
;
2723 union pw_protocol_fields data
;
2725 struct zebra_pw
*pw
;
2727 /* Get input stream. */
2731 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2732 ifname
[IF_NAMESIZE
- 1] = '\0';
2733 STREAM_GETL(s
, ifindex
);
2734 STREAM_GETL(s
, type
);
2738 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2741 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2746 STREAM_GETL(s
, local_label
);
2747 STREAM_GETL(s
, remote_label
);
2748 STREAM_GETC(s
, flags
);
2749 STREAM_GET(&data
, s
, sizeof(data
));
2750 protocol
= client
->proto
;
2752 pw
= zebra_pw_find(zvrf
, ifname
);
2753 switch (hdr
->command
) {
2756 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2757 "%s: pseudowire %s already exists [%s]",
2759 zserv_command_string(hdr
->command
));
2763 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2765 case ZEBRA_PW_DELETE
:
2767 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2768 "%s: pseudowire %s not found [%s]", __func__
,
2769 ifname
, zserv_command_string(hdr
->command
));
2773 zebra_pw_del(zvrf
, pw
);
2776 case ZEBRA_PW_UNSET
:
2778 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2779 "%s: pseudowire %s not found [%s]", __func__
,
2780 ifname
, zserv_command_string(hdr
->command
));
2784 switch (hdr
->command
) {
2788 case ZEBRA_PW_UNSET
:
2793 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2794 remote_label
, flags
, &data
);
2802 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2804 struct interface
*master
;
2805 struct interface
*slave
;
2806 struct stream
*s
= msg
;
2810 STREAM_GETL(s
, vrf_id
);
2811 STREAM_GETL(s
, ifindex
);
2812 master
= if_lookup_by_index(ifindex
, vrf_id
);
2814 STREAM_GETL(s
, vrf_id
);
2815 STREAM_GETL(s
, ifindex
);
2816 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2818 if (!master
|| !slave
)
2821 kernel_interface_set_master(master
, slave
);
2828 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2830 struct interface
*ifp
;
2831 mpls_label_t nlabel
;
2834 struct zebra_vrf
*def_zvrf
;
2835 enum lsp_types_t ltype
;
2838 STREAM_GETL(s
, nlabel
);
2839 STREAM_GETC(s
, afi
);
2841 if (!(IS_VALID_AFI(afi
))) {
2842 zlog_warn("Invalid AFI for VRF label: %u", afi
);
2846 if (nlabel
== zvrf
->label
[afi
]) {
2848 * Nothing to do here move along
2853 STREAM_GETC(s
, ltype
);
2855 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2856 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2858 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2861 zlog_debug("Unable to find specified Interface for %s",
2866 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2868 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2872 really_remove
= true;
2873 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2874 if (scrubber
== afi
)
2877 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2880 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2881 really_remove
= false;
2887 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2888 NEXTHOP_TYPE_IFINDEX
, NULL
,
2889 ifp
->ifindex
, false /*backup*/);
2892 if (nlabel
!= MPLS_LABEL_NONE
) {
2893 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2894 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2895 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2898 zvrf
->label
[afi
] = nlabel
;
2903 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2905 struct zebra_pbr_rule zpr
;
2908 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2911 STREAM_GETL(s
, total
);
2913 for (i
= 0; i
< total
; i
++) {
2914 memset(&zpr
, 0, sizeof(zpr
));
2916 zpr
.sock
= client
->sock
;
2917 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2918 STREAM_GETL(s
, zpr
.rule
.seq
);
2919 STREAM_GETL(s
, zpr
.rule
.priority
);
2920 STREAM_GETL(s
, zpr
.rule
.unique
);
2921 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2922 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2923 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2924 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2925 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2926 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2927 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2928 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2929 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2930 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2931 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
2932 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2933 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2934 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
2936 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
2937 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
2939 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2940 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2942 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2943 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2945 if (zpr
.rule
.filter
.src_port
)
2946 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2948 if (zpr
.rule
.filter
.dst_port
)
2949 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2951 if (zpr
.rule
.filter
.dsfield
)
2952 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
2954 if (zpr
.rule
.filter
.fwmark
)
2955 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2957 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
2958 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
2960 "Unsupported PBR source IP family: %s (%hhu)",
2961 family2str(zpr
.rule
.filter
.src_ip
.family
),
2962 zpr
.rule
.filter
.src_ip
.family
);
2965 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
2966 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
2968 "Unsupported PBR destination IP family: %s (%hhu)",
2969 family2str(zpr
.rule
.filter
.dst_ip
.family
),
2970 zpr
.rule
.filter
.dst_ip
.family
);
2975 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2976 if (hdr
->command
== ZEBRA_RULE_ADD
)
2977 zebra_pbr_add_rule(&zpr
);
2979 zebra_pbr_del_rule(&zpr
);
2986 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
2988 struct zebra_pbr_ipset zpi
;
2993 STREAM_GETL(s
, total
);
2995 for (i
= 0; i
< total
; i
++) {
2996 memset(&zpi
, 0, sizeof(zpi
));
2998 zpi
.sock
= client
->sock
;
2999 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3000 STREAM_GETL(s
, zpi
.unique
);
3001 STREAM_GETL(s
, zpi
.type
);
3002 STREAM_GETC(s
, zpi
.family
);
3003 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3005 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3006 zebra_pbr_create_ipset(&zpi
);
3008 zebra_pbr_destroy_ipset(&zpi
);
3015 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3017 struct zebra_pbr_ipset_entry zpi
;
3018 struct zebra_pbr_ipset ipset
;
3023 STREAM_GETL(s
, total
);
3025 for (i
= 0; i
< total
; i
++) {
3026 memset(&zpi
, 0, sizeof(zpi
));
3027 memset(&ipset
, 0, sizeof(ipset
));
3029 zpi
.sock
= client
->sock
;
3030 STREAM_GETL(s
, zpi
.unique
);
3031 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3032 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3033 STREAM_GETC(s
, zpi
.src
.family
);
3034 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3035 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3036 STREAM_GETC(s
, zpi
.dst
.family
);
3037 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3038 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3040 STREAM_GETW(s
, zpi
.src_port_min
);
3041 STREAM_GETW(s
, zpi
.src_port_max
);
3042 STREAM_GETW(s
, zpi
.dst_port_min
);
3043 STREAM_GETW(s
, zpi
.dst_port_max
);
3044 STREAM_GETC(s
, zpi
.proto
);
3045 if (!is_default_prefix(&zpi
.src
))
3046 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3048 if (!is_default_prefix(&zpi
.dst
))
3049 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3050 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3051 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3052 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3053 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3054 if (zpi
.dst_port_max
!= 0)
3055 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3056 if (zpi
.src_port_max
!= 0)
3057 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3059 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3061 if (!(zpi
.dst
.family
== AF_INET
3062 || zpi
.dst
.family
== AF_INET6
)) {
3064 "Unsupported PBR destination IP family: %s (%hhu)",
3065 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3066 goto stream_failure
;
3068 if (!(zpi
.src
.family
== AF_INET
3069 || zpi
.src
.family
== AF_INET6
)) {
3071 "Unsupported PBR source IP family: %s (%hhu)",
3072 family2str(zpi
.src
.family
), zpi
.src
.family
);
3073 goto stream_failure
;
3076 /* calculate backpointer */
3078 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3080 if (!zpi
.backpointer
) {
3081 zlog_warn("ipset name specified: %s does not exist",
3083 goto stream_failure
;
3086 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3087 zebra_pbr_add_ipset_entry(&zpi
);
3089 zebra_pbr_del_ipset_entry(&zpi
);
3096 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3098 struct zebra_pbr_iptable
*zpi
=
3099 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
3104 zpi
->interface_name_list
= list_new();
3105 zpi
->sock
= client
->sock
;
3106 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3107 STREAM_GETL(s
, zpi
->unique
);
3108 STREAM_GETL(s
, zpi
->type
);
3109 STREAM_GETL(s
, zpi
->filter_bm
);
3110 STREAM_GETL(s
, zpi
->action
);
3111 STREAM_GETL(s
, zpi
->fwmark
);
3112 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3113 STREAM_GETC(s
, zpi
->family
);
3114 STREAM_GETW(s
, zpi
->pkt_len_min
);
3115 STREAM_GETW(s
, zpi
->pkt_len_max
);
3116 STREAM_GETW(s
, zpi
->tcp_flags
);
3117 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3118 STREAM_GETC(s
, zpi
->dscp_value
);
3119 STREAM_GETC(s
, zpi
->fragment
);
3120 STREAM_GETC(s
, zpi
->protocol
);
3121 STREAM_GETW(s
, zpi
->flow_label
);
3122 STREAM_GETL(s
, zpi
->nb_interface
);
3123 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3125 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3126 zebra_pbr_add_iptable(zpi
);
3128 zebra_pbr_del_iptable(zpi
);
3131 zebra_pbr_iptable_free(zpi
);
3136 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3140 struct interface
*ifp
;
3146 STREAM_GETL(s
, ifindex
);
3148 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3150 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3154 STREAM_GETC(s
, p
.family
);
3155 STREAM_GETC(s
, p
.prefixlen
);
3156 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3158 if (p
.family
== AF_INET
)
3163 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3165 dplane_neigh_discover(ifp
, &ip
);
3171 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3172 struct zmsghdr
*bad_hdr
)
3175 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3177 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3179 zserv_encode_error(s
, error
);
3181 client
->error_cnt
++;
3182 zserv_send_message(client
, s
);
3185 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3187 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3188 zlog_debug("ZAPI message specifies unknown VRF: %d",
3191 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3194 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3196 zlog_info("Zebra received unknown command %d", hdr
->command
);
3198 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3201 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3202 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3203 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3204 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3205 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3206 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3207 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3208 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3209 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3210 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3211 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3212 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3213 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
3214 [ZEBRA_HELLO
] = zread_hello
,
3215 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3216 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3217 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
3218 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
3219 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3220 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3221 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3223 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3224 #endif /* HAVE_BFDD */
3225 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3226 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3227 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3228 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3229 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3230 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3231 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3232 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3233 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3234 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3235 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3236 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3237 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3238 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3239 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3240 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3241 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3242 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3243 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3244 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3245 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3246 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3247 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3248 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
3249 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
3250 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3251 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3252 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3253 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3254 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3255 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3256 [ZEBRA_PW_SET
] = zread_pseudowire
,
3257 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3258 [ZEBRA_RULE_ADD
] = zread_rule
,
3259 [ZEBRA_RULE_DELETE
] = zread_rule
,
3260 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3261 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3262 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3263 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3264 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3265 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3266 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3267 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3268 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3269 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3270 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3271 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3272 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3273 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3274 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3275 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3276 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3277 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3281 * Process a batch of zapi messages.
3283 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3286 struct zebra_vrf
*zvrf
;
3288 struct stream_fifo temp_fifo
;
3290 stream_fifo_init(&temp_fifo
);
3292 while (stream_fifo_head(fifo
)) {
3293 msg
= stream_fifo_pop(fifo
);
3295 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3296 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3298 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3299 STREAM_READABLE(msg
),
3300 ZEBRA_MAX_PACKET_SIZ
);
3304 zapi_parse_header(msg
, &hdr
);
3306 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3307 && IS_ZEBRA_DEBUG_DETAIL
)
3308 zserv_log_message(NULL
, msg
, &hdr
);
3310 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3312 /* Before checking for a handler function, check for
3313 * special messages that are handled in another module;
3314 * we'll treat these as opaque.
3316 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3317 /* Reset message buffer */
3318 stream_set_getp(msg
, 0);
3320 stream_fifo_push(&temp_fifo
, msg
);
3322 /* Continue without freeing the message */
3328 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3330 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3334 if (hdr
.command
>= array_size(zserv_handlers
)
3335 || zserv_handlers
[hdr
.command
] == NULL
) {
3336 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3340 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3346 /* Dispatch any special messages from the temp fifo */
3347 if (stream_fifo_head(&temp_fifo
) != NULL
)
3348 zebra_opaque_enqueue_batch(&temp_fifo
);
3350 stream_fifo_deinit(&temp_fifo
);