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_ns.h"
42 #include "zebra/zebra_vrf.h"
43 #include "zebra/router-id.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_rnh.h"
47 #include "zebra/interface.h"
48 #include "zebra/zebra_ptm.h"
49 #include "zebra/rtadv.h"
50 #include "zebra/zebra_mpls.h"
51 #include "zebra/zebra_mroute.h"
52 #include "zebra/zebra_vxlan.h"
53 #include "zebra/zebra_evpn_mh.h"
55 #include "zebra/zebra_pbr.h"
56 #include "zebra/table_manager.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_errors.h"
59 #include "zebra/zebra_mlag.h"
60 #include "zebra/connected.h"
61 #include "zebra/zebra_opaque.h"
62 #include "zebra/zebra_srte.h"
64 DEFINE_MTYPE_STATIC(ZEBRA
, OPAQUE
, "Opaque Data");
66 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
);
68 /* Encoding helpers -------------------------------------------------------- */
70 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
72 /* Interface information. */
73 struct zebra_if
*zif
= ifp
->info
;
75 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
76 stream_putl(s
, ifp
->ifindex
);
77 stream_putc(s
, ifp
->status
);
78 stream_putq(s
, ifp
->flags
);
79 stream_putc(s
, ifp
->ptm_enable
);
80 stream_putc(s
, ifp
->ptm_status
);
81 stream_putl(s
, ifp
->metric
);
82 stream_putl(s
, ifp
->speed
);
83 stream_putl(s
, ifp
->mtu
);
84 stream_putl(s
, ifp
->mtu6
);
85 stream_putl(s
, ifp
->bandwidth
);
86 stream_putl(s
, zif
->link_ifindex
);
87 stream_putl(s
, ifp
->ll_type
);
88 stream_putl(s
, ifp
->hw_addr_len
);
90 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
92 /* Then, Traffic Engineering parameters if any */
93 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
95 zebra_interface_link_params_write(s
, ifp
);
99 /* Write packet size. */
100 stream_putw_at(s
, 0, stream_get_endp(s
));
103 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
105 struct vrf_data data
;
106 const char *netns_name
= zvrf_ns_name(zvrf
);
108 memset(&data
, 0, sizeof(data
));
109 data
.l
.table_id
= zvrf
->table_id
;
112 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
115 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
116 /* Pass the tableid and the netns NAME */
117 stream_put(s
, &data
, sizeof(struct vrf_data
));
118 /* Interface information. */
119 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
120 /* Write packet size. */
121 stream_putw_at(s
, 0, stream_get_endp(s
));
124 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
126 stream_putl(s
, nexthop
->vrf_id
);
127 stream_putc(s
, nexthop
->type
);
128 switch (nexthop
->type
) {
129 case NEXTHOP_TYPE_IPV4
:
130 case NEXTHOP_TYPE_IPV4_IFINDEX
:
131 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
132 stream_putl(s
, nexthop
->ifindex
);
134 case NEXTHOP_TYPE_IPV6
:
135 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
137 case NEXTHOP_TYPE_IPV6_IFINDEX
:
138 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
139 stream_putl(s
, nexthop
->ifindex
);
141 case NEXTHOP_TYPE_IFINDEX
:
142 stream_putl(s
, nexthop
->ifindex
);
152 * Zebra error addition adds error type.
156 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 * | enum zebra_error_types |
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
164 stream_put(s
, &error
, sizeof(error
));
166 /* Write packet size. */
167 stream_putw_at(s
, 0, stream_get_endp(s
));
170 /* Send handlers ----------------------------------------------------------- */
172 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
174 * This function is called in the following situations:
175 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
177 * - at startup, when zebra figures out the available interfaces
178 * - when an interface is added (where support for
179 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
180 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
183 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
185 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
187 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
188 zserv_encode_interface(s
, ifp
);
191 return zserv_send_message(client
, s
);
194 /* Interface deletion from zebra daemon. */
195 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
197 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
199 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
200 zserv_encode_interface(s
, ifp
);
203 return zserv_send_message(client
, s
);
206 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
208 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
210 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
211 zserv_encode_vrf(s
, zvrf
);
213 client
->vrfadd_cnt
++;
214 return zserv_send_message(client
, s
);
217 /* VRF deletion from zebra daemon. */
218 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
221 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
223 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
224 zserv_encode_vrf(s
, zvrf
);
226 client
->vrfdel_cnt
++;
227 return zserv_send_message(client
, s
);
230 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
232 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
234 if (!ifp
->link_params
) {
239 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
241 /* Add Interface Index */
242 stream_putl(s
, ifp
->ifindex
);
244 /* Then TE Link Parameters */
245 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
250 /* Write packet size. */
251 stream_putw_at(s
, 0, stream_get_endp(s
));
253 return zserv_send_message(client
, s
);
256 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
257 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
259 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
260 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
261 * from the client, after the ZEBRA_INTERFACE_ADD has been
262 * sent from zebra to the client
263 * - redistribute new address info to all clients in the following situations
264 * - at startup, when zebra figures out the available interfaces
265 * - when an interface is added (where support for
266 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
267 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
269 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
270 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
271 * - when an RTM_NEWADDR message is received from the kernel,
273 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
275 * zsend_interface_address(DELETE)
278 * zebra_interface_address_delete_update
280 * | | if_delete_update
282 * ip_address_uninstall connected_delete_ipv4
283 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
286 * | RTM_NEWADDR on routing/netlink socket
289 * "no ip address A.B.C.D/M [label LINE]"
290 * "no ip address A.B.C.D/M"
291 * ["no ipv6 address X:X::X:X/M"]
294 int zsend_interface_address(int cmd
, struct zserv
*client
,
295 struct interface
*ifp
, struct connected
*ifc
)
299 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
301 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
302 stream_putl(s
, ifp
->ifindex
);
304 /* Interface address flag. */
305 stream_putc(s
, ifc
->flags
);
307 /* Prefix information. */
309 stream_putc(s
, p
->family
);
310 blen
= prefix_blen(p
);
311 stream_put(s
, &p
->u
.prefix
, blen
);
314 * XXX gnu version does not send prefixlen for
315 * ZEBRA_INTERFACE_ADDRESS_DELETE
316 * but zebra_interface_address_delete_read() in the gnu version
319 stream_putc(s
, p
->prefixlen
);
322 p
= ifc
->destination
;
324 stream_put(s
, &p
->u
.prefix
, blen
);
326 stream_put(s
, NULL
, blen
);
328 /* Write packet size. */
329 stream_putw_at(s
, 0, stream_get_endp(s
));
331 client
->connected_rt_add_cnt
++;
332 return zserv_send_message(client
, s
);
335 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
336 struct interface
*ifp
,
337 struct nbr_connected
*ifc
)
340 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
343 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
344 stream_putl(s
, ifp
->ifindex
);
346 /* Prefix information. */
348 stream_putc(s
, p
->family
);
349 blen
= prefix_blen(p
);
350 stream_put(s
, &p
->u
.prefix
, blen
);
353 * XXX gnu version does not send prefixlen for
354 * ZEBRA_INTERFACE_ADDRESS_DELETE
355 * but zebra_interface_address_delete_read() in the gnu version
358 stream_putc(s
, p
->prefixlen
);
360 /* Write packet size. */
361 stream_putw_at(s
, 0, stream_get_endp(s
));
363 return zserv_send_message(client
, s
);
366 /* Interface address addition. */
367 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
368 struct nbr_connected
*ifc
)
370 struct listnode
*node
, *nnode
;
371 struct zserv
*client
;
374 if (IS_ZEBRA_DEBUG_EVENT
) {
375 char buf
[INET6_ADDRSTRLEN
];
379 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
380 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
382 p
->prefixlen
, ifc
->ifp
->name
);
385 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
386 /* Do not send unsolicited messages to synchronous clients. */
387 if (client
->synchronous
)
390 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
395 /* Interface address deletion. */
396 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
397 struct nbr_connected
*ifc
)
399 struct listnode
*node
, *nnode
;
400 struct zserv
*client
;
403 if (IS_ZEBRA_DEBUG_EVENT
) {
404 char buf
[INET6_ADDRSTRLEN
];
408 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
409 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
411 p
->prefixlen
, ifc
->ifp
->name
);
414 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
415 /* Do not send unsolicited messages to synchronous clients. */
416 if (client
->synchronous
)
419 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
424 /* Send addresses on interface to client */
425 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
427 struct listnode
*cnode
, *cnnode
;
429 struct nbr_connected
*nc
;
431 /* Send interface addresses. */
432 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
433 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
436 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
442 /* Send interface neighbors. */
443 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
444 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
453 /* Notify client about interface moving from one VRF to another.
454 * Whether client is interested in old and new VRF is checked by caller.
456 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
459 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
461 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
463 /* Fill in the name of the interface and its new VRF (id) */
464 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
465 stream_putl(s
, vrf_id
);
467 /* Write packet size. */
468 stream_putw_at(s
, 0, stream_get_endp(s
));
470 client
->if_vrfchg_cnt
++;
471 return zserv_send_message(client
, s
);
474 /* Add new nbr connected IPv6 address */
475 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
477 struct nbr_connected
*ifc
;
481 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
482 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
484 ifc
= listnode_head(ifp
->nbr_connected
);
487 ifc
= nbr_connected_new();
488 ifc
->address
= prefix_new();
490 listnode_add(ifp
->nbr_connected
, ifc
);
493 prefix_copy(ifc
->address
, &p
);
495 zebra_interface_nbr_address_add_update(ifp
, ifc
);
497 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
500 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
502 struct nbr_connected
*ifc
;
506 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
507 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
509 ifc
= nbr_connected_check(ifp
, &p
);
513 listnode_delete(ifp
->nbr_connected
, ifc
);
515 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
517 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
519 nbr_connected_free(ifc
);
523 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
524 * ZEBRA_INTERFACE_DOWN.
526 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
527 * the clients in one of 2 situations:
528 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
529 * - a vty command modifying the bandwidth of an interface is received.
530 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
532 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
534 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
536 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
537 zserv_encode_interface(s
, ifp
);
539 if (cmd
== ZEBRA_INTERFACE_UP
)
542 client
->ifdown_cnt
++;
544 return zserv_send_message(client
, s
);
547 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
548 const struct prefix
*p
,
549 const struct prefix
*src_p
,
550 const struct route_entry
*re
)
552 struct zapi_route api
;
553 struct zapi_nexthop
*api_nh
;
554 struct nexthop
*nexthop
;
558 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
560 memset(&api
, 0, sizeof(api
));
561 api
.vrf_id
= re
->vrf_id
;
563 api
.safi
= SAFI_UNICAST
;
564 api
.instance
= re
->instance
;
565 api
.flags
= re
->flags
;
567 afi
= family2afi(p
->family
);
570 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
571 client
->redist_v4_add_cnt
++;
573 client
->redist_v4_del_cnt
++;
576 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
577 client
->redist_v6_add_cnt
++;
579 client
->redist_v6_del_cnt
++;
588 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
589 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
592 for (nexthop
= re
->nhe
->nhg
.nexthop
;
593 nexthop
; nexthop
= nexthop
->next
) {
594 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
597 api_nh
= &api
.nexthops
[count
];
598 api_nh
->vrf_id
= nexthop
->vrf_id
;
599 api_nh
->type
= nexthop
->type
;
600 api_nh
->weight
= nexthop
->weight
;
601 switch (nexthop
->type
) {
602 case NEXTHOP_TYPE_BLACKHOLE
:
603 api_nh
->bh_type
= nexthop
->bh_type
;
605 case NEXTHOP_TYPE_IPV4
:
606 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
608 case NEXTHOP_TYPE_IPV4_IFINDEX
:
609 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
610 api_nh
->ifindex
= nexthop
->ifindex
;
612 case NEXTHOP_TYPE_IFINDEX
:
613 api_nh
->ifindex
= nexthop
->ifindex
;
615 case NEXTHOP_TYPE_IPV6
:
616 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
618 case NEXTHOP_TYPE_IPV6_IFINDEX
:
619 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
620 api_nh
->ifindex
= nexthop
->ifindex
;
627 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
628 api
.nexthop_num
= count
;
632 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
633 api
.distance
= re
->distance
;
634 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
635 api
.metric
= re
->metric
;
637 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
640 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
643 struct stream
*s
= stream_new(stream_size
);
645 /* Encode route and send. */
646 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
651 if (IS_ZEBRA_DEBUG_SEND
)
652 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
653 __func__
, zserv_command_string(cmd
),
654 zebra_route_string(client
->proto
),
655 zebra_route_string(api
.type
), api
.vrf_id
,
657 return zserv_send_message(client
, s
);
661 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
662 * nexthop is not found on mrib. Returns both route metric and protocol
665 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
667 struct route_entry
*re
,
668 struct zebra_vrf
*zvrf
)
673 struct nexthop
*nexthop
;
675 /* Get output stream. */
676 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
679 /* Fill in result. */
680 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
681 stream_put_in_addr(s
, &addr
);
684 stream_putc(s
, re
->distance
);
685 stream_putl(s
, re
->metric
);
687 /* remember position for nexthop_num */
688 nump
= stream_get_endp(s
);
689 /* reserve room for nexthop_num */
692 * Only non-recursive routes are elegible to resolve the
693 * nexthop we are looking up. Therefore, we will just iterate
694 * over the top chain of nexthops.
696 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
697 nexthop
= nexthop
->next
)
698 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
699 num
+= zserv_encode_nexthop(s
, nexthop
);
701 /* store nexthop_num */
702 stream_putc_at(s
, nump
, num
);
704 stream_putc(s
, 0); /* distance */
705 stream_putl(s
, 0); /* metric */
706 stream_putc(s
, 0); /* nexthop_num */
709 stream_putw_at(s
, 0, stream_get_endp(s
));
711 return zserv_send_message(client
, s
);
714 int zsend_nhg_notify(uint16_t type
, uint16_t instance
, uint32_t session_id
,
715 uint32_t id
, enum zapi_nhg_notify_owner note
)
717 struct zserv
*client
;
720 client
= zserv_find_client_session(type
, instance
, session_id
);
722 if (IS_ZEBRA_DEBUG_PACKET
) {
723 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
724 type
, instance
, id
, note
);
729 if (IS_ZEBRA_DEBUG_SEND
)
730 zlog_debug("%s: type %d, id %d, note %d",
731 __func__
, type
, id
, note
);
733 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
736 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
738 stream_put(s
, ¬e
, sizeof(note
));
741 stream_putw_at(s
, 0, stream_get_endp(s
));
743 return zserv_send_message(client
, s
);
747 * Common utility send route notification, called from a path using a
748 * route_entry and from a path using a dataplane context.
750 static int route_notify_internal(const struct prefix
*p
, int type
,
751 uint16_t instance
, vrf_id_t vrf_id
,
753 enum zapi_route_notify_owner note
,
754 afi_t afi
, safi_t safi
)
756 struct zserv
*client
;
760 client
= zserv_find_client(type
, instance
);
761 if (!client
|| !client
->notify_owner
) {
762 if (IS_ZEBRA_DEBUG_PACKET
)
764 "Not Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
765 zebra_route_string(type
), p
, table_id
, note
,
770 if (IS_ZEBRA_DEBUG_PACKET
)
772 "Notifying Owner: %s about prefix %pFX(%u) %d vrf: %u",
773 zebra_route_string(type
), p
, table_id
, note
, vrf_id
);
775 /* We're just allocating a small-ish buffer here, since we only
776 * encode a small amount of data.
778 s
= stream_new(ZEBRA_SMALL_PACKET_SIZE
);
782 zclient_create_header(s
, ZEBRA_ROUTE_NOTIFY_OWNER
, vrf_id
);
784 stream_put(s
, ¬e
, sizeof(note
));
786 stream_putc(s
, p
->family
);
788 blen
= prefix_blen(p
);
789 stream_putc(s
, p
->prefixlen
);
790 stream_put(s
, &p
->u
.prefix
, blen
);
792 stream_putl(s
, table_id
);
794 /* Encode AFI, SAFI in the message */
796 stream_putc(s
, safi
);
798 stream_putw_at(s
, 0, stream_get_endp(s
));
800 return zserv_send_message(client
, s
);
803 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
804 enum zapi_route_notify_owner note
,
805 afi_t afi
, safi_t safi
)
807 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
808 re
->table
, note
, afi
, safi
));
812 * Route-owner notification using info from dataplane update context.
814 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
815 enum zapi_route_notify_owner note
)
817 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
818 dplane_ctx_get_type(ctx
),
819 dplane_ctx_get_instance(ctx
),
820 dplane_ctx_get_vrf(ctx
),
821 dplane_ctx_get_table(ctx
),
823 dplane_ctx_get_afi(ctx
),
824 dplane_ctx_get_safi(ctx
)));
827 static void zread_route_notify_request(ZAPI_HANDLER_ARGS
)
831 STREAM_GETC(msg
, notify
);
832 client
->notify_owner
= notify
;
837 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
838 enum zapi_rule_notify_owner note
)
840 struct listnode
*node
;
841 struct zserv
*client
;
844 if (IS_ZEBRA_DEBUG_PACKET
)
845 zlog_debug("%s: Notifying %u", __func__
,
846 dplane_ctx_rule_get_unique(ctx
));
848 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
849 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
856 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
858 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
859 stream_put(s
, ¬e
, sizeof(note
));
860 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
861 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
862 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
863 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
865 stream_putw_at(s
, 0, stream_get_endp(s
));
867 zserv_send_message(client
, s
);
870 void zsend_iptable_notify_owner(const struct zebra_dplane_ctx
*ctx
,
873 struct listnode
*node
;
874 struct zserv
*client
;
876 struct zebra_pbr_iptable ipt
;
877 uint16_t cmd
= ZEBRA_IPTABLE_NOTIFY_OWNER
;
879 if (!dplane_ctx_get_pbr_iptable(ctx
, &ipt
))
882 if (IS_ZEBRA_DEBUG_PACKET
)
883 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
884 zserv_command_string(cmd
), ipt
.unique
, note
);
886 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
887 if (ipt
.sock
== client
->sock
)
894 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
896 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
897 stream_put(s
, ¬e
, sizeof(note
));
898 stream_putl(s
, ipt
.unique
);
899 stream_put(s
, ipt
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
900 stream_putw_at(s
, 0, stream_get_endp(s
));
902 zserv_send_message(client
, s
);
905 void zsend_ipset_notify_owner(const struct zebra_dplane_ctx
*ctx
, uint16_t note
)
907 struct listnode
*node
;
908 struct zserv
*client
;
910 struct zebra_pbr_ipset ipset
;
911 uint16_t cmd
= ZEBRA_IPSET_NOTIFY_OWNER
;
913 if (!dplane_ctx_get_pbr_ipset(ctx
, &ipset
))
916 if (IS_ZEBRA_DEBUG_PACKET
)
917 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
918 zserv_command_string(cmd
), ipset
.unique
, note
);
920 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
921 if (ipset
.sock
== client
->sock
)
928 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
930 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
931 stream_put(s
, ¬e
, sizeof(note
));
932 stream_putl(s
, ipset
.unique
);
933 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
934 stream_putw_at(s
, 0, stream_get_endp(s
));
936 zserv_send_message(client
, s
);
939 void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx
*ctx
,
942 struct listnode
*node
;
943 struct zserv
*client
;
945 struct zebra_pbr_ipset_entry ipent
;
946 struct zebra_pbr_ipset ipset
;
947 uint16_t cmd
= ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
;
949 if (!dplane_ctx_get_pbr_ipset_entry(ctx
, &ipent
))
951 if (!dplane_ctx_get_pbr_ipset(ctx
, &ipset
))
954 if (IS_ZEBRA_DEBUG_PACKET
)
955 zlog_debug("%s: Notifying %s id %u note %u", __func__
,
956 zserv_command_string(cmd
), ipent
.unique
, note
);
958 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
959 if (ipent
.sock
== client
->sock
)
966 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
968 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
969 stream_put(s
, ¬e
, sizeof(note
));
970 stream_putl(s
, ipent
.unique
);
971 stream_put(s
, ipset
.ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
972 stream_putw_at(s
, 0, stream_get_endp(s
));
974 zserv_send_message(client
, s
);
977 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
978 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
984 /* Check this client need interface information. */
985 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
988 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
991 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
993 /* Prefix information. */
994 stream_putc(s
, p
->family
);
995 blen
= prefix_blen(p
);
996 stream_put(s
, &p
->u
.prefix
, blen
);
997 stream_putc(s
, p
->prefixlen
);
999 /* Write packet size. */
1000 stream_putw_at(s
, 0, stream_get_endp(s
));
1002 return zserv_send_message(client
, s
);
1006 * Function used by Zebra to send a PW status update to LDP daemon
1008 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
1010 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1012 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
1013 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1014 stream_putl(s
, pw
->ifindex
);
1015 stream_putl(s
, pw
->status
);
1017 /* Put length at the first point of the stream. */
1018 stream_putw_at(s
, 0, stream_get_endp(s
));
1020 return zserv_send_message(client
, s
);
1023 /* Send response to a get label chunk request to client */
1024 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
1025 struct label_manager_chunk
*lmc
)
1027 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1029 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
1031 stream_putc(s
, client
->proto
);
1033 stream_putw(s
, client
->instance
);
1037 stream_putc(s
, lmc
->keep
);
1038 /* start and end labels */
1039 stream_putl(s
, lmc
->start
);
1040 stream_putl(s
, lmc
->end
);
1043 /* Write packet size. */
1044 stream_putw_at(s
, 0, stream_get_endp(s
));
1046 return zserv_send_message(client
, s
);
1049 /* Send response to a label manager connect request to client */
1050 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1051 unsigned short result
)
1053 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1055 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1058 stream_putc(s
, client
->proto
);
1061 stream_putw(s
, client
->instance
);
1064 stream_putc(s
, result
);
1066 /* Write packet size. */
1067 stream_putw_at(s
, 0, stream_get_endp(s
));
1069 return zserv_send_message(client
, s
);
1072 /* Send response to a get table chunk request to client */
1073 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1075 struct table_manager_chunk
*tmc
)
1077 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1079 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1082 /* start and end labels */
1083 stream_putl(s
, tmc
->start
);
1084 stream_putl(s
, tmc
->end
);
1087 /* Write packet size. */
1088 stream_putw_at(s
, 0, stream_get_endp(s
));
1090 return zserv_send_message(client
, s
);
1093 static int zsend_table_manager_connect_response(struct zserv
*client
,
1097 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1099 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1102 stream_putc(s
, result
);
1104 stream_putw_at(s
, 0, stream_get_endp(s
));
1106 return zserv_send_message(client
, s
);
1109 /* Inbound message handling ------------------------------------------------ */
1111 const int cmd2type
[] = {
1112 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1113 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1114 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1115 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1118 /* Nexthop register */
1119 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1124 unsigned short l
= 0;
1126 uint16_t type
= cmd2type
[hdr
->command
];
1128 bool flag_changed
= false;
1131 if (IS_ZEBRA_DEBUG_NHT
)
1133 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1134 zebra_route_string(client
->proto
), hdr
->length
,
1135 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1140 if (!client
->nh_reg_time
)
1141 client
->nh_reg_time
= monotime(NULL
);
1143 while (l
< hdr
->length
) {
1144 STREAM_GETC(s
, flags
);
1145 STREAM_GETW(s
, p
.family
);
1146 STREAM_GETC(s
, p
.prefixlen
);
1148 if (p
.family
== AF_INET
) {
1149 client
->v4_nh_watch_add_cnt
++;
1150 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1152 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1153 __func__
, p
.prefixlen
);
1156 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1157 l
+= IPV4_MAX_BYTELEN
;
1158 } else if (p
.family
== AF_INET6
) {
1159 client
->v6_nh_watch_add_cnt
++;
1160 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1162 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1163 __func__
, p
.prefixlen
);
1166 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1167 l
+= IPV6_MAX_BYTELEN
;
1170 EC_ZEBRA_UNKNOWN_FAMILY
,
1171 "rnh_register: Received unknown family type %d",
1175 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1179 orig_flags
= rnh
->flags
;
1180 if (type
== RNH_NEXTHOP_TYPE
) {
1182 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1183 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1185 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1186 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1187 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1189 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1190 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1192 && CHECK_FLAG(rnh
->flags
,
1193 ZEBRA_NHT_EXACT_MATCH
))
1194 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1197 if (orig_flags
!= rnh
->flags
)
1198 flag_changed
= true;
1200 /* Anything not AF_INET/INET6 has been filtered out above */
1201 if (!exist
|| flag_changed
)
1202 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1205 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1212 /* Nexthop register */
1213 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1218 unsigned short l
= 0;
1219 uint16_t type
= cmd2type
[hdr
->command
];
1221 if (IS_ZEBRA_DEBUG_NHT
)
1223 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1224 zebra_route_string(client
->proto
), hdr
->length
,
1229 while (l
< hdr
->length
) {
1232 STREAM_GETC(s
, flags
);
1234 goto stream_failure
;
1236 STREAM_GETW(s
, p
.family
);
1237 STREAM_GETC(s
, p
.prefixlen
);
1239 if (p
.family
== AF_INET
) {
1240 client
->v4_nh_watch_rem_cnt
++;
1241 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1243 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1244 __func__
, p
.prefixlen
);
1247 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1248 l
+= IPV4_MAX_BYTELEN
;
1249 } else if (p
.family
== AF_INET6
) {
1250 client
->v6_nh_watch_rem_cnt
++;
1251 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1253 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1254 __func__
, p
.prefixlen
);
1257 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1258 l
+= IPV6_MAX_BYTELEN
;
1261 EC_ZEBRA_UNKNOWN_FAMILY
,
1262 "rnh_register: Received unknown family type %d",
1266 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1268 client
->nh_dereg_time
= monotime(NULL
);
1269 zebra_remove_rnh_client(rnh
, client
, type
);
1276 #define ZEBRA_MIN_FEC_LENGTH 5
1279 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1282 unsigned short l
= 0;
1285 uint32_t label
= MPLS_INVALID_LABEL
;
1286 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1289 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1294 * The minimum amount of data that can be sent for one fec
1297 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1299 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1300 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1305 while (l
< hdr
->length
) {
1306 STREAM_GETW(s
, flags
);
1307 memset(&p
, 0, sizeof(p
));
1308 STREAM_GETW(s
, p
.family
);
1309 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1311 EC_ZEBRA_UNKNOWN_FAMILY
,
1312 "fec_register: Received unknown family type %d",
1316 STREAM_GETC(s
, p
.prefixlen
);
1317 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1318 || (p
.family
== AF_INET6
1319 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1321 "%s: Specified prefix hdr->length: %d is to long for %d",
1322 __func__
, p
.prefixlen
, p
.family
);
1326 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1327 l
+= PSIZE(p
.prefixlen
);
1328 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1329 STREAM_GETL(s
, label
);
1331 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1332 STREAM_GETL(s
, label_index
);
1336 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1343 /* FEC unregister */
1344 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1347 unsigned short l
= 0;
1352 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1357 * The minimum amount of data that can be sent for one
1358 * fec unregistration
1360 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1362 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1363 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1368 while (l
< hdr
->length
) {
1369 STREAM_GETW(s
, flags
);
1371 goto stream_failure
;
1373 memset(&p
, 0, sizeof(p
));
1374 STREAM_GETW(s
, p
.family
);
1375 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1377 EC_ZEBRA_UNKNOWN_FAMILY
,
1378 "fec_unregister: Received unknown family type %d",
1382 STREAM_GETC(s
, p
.prefixlen
);
1383 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1384 || (p
.family
== AF_INET6
1385 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1387 "%s: Received prefix hdr->length %d which is greater than %d can support",
1388 __func__
, p
.prefixlen
, p
.family
);
1392 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1393 l
+= PSIZE(p
.prefixlen
);
1394 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1403 * Register zebra server interface information.
1404 * Send current all interface and address information.
1406 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1409 struct interface
*ifp
;
1411 vrf_id_t vrf_id
= zvrf_id(zvrf
);
1412 if (vrf_id
!= VRF_DEFAULT
&& vrf_id
!= VRF_UNKNOWN
) {
1413 FOR_ALL_INTERFACES (zvrf
->vrf
, ifp
) {
1414 /* Skip pseudo interface. */
1415 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1418 zsend_interface_add(client
, ifp
);
1419 zsend_interface_link_params(client
, ifp
);
1420 zsend_interface_addresses(client
, ifp
);
1425 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1426 FOR_ALL_INTERFACES (vrf
, ifp
) {
1427 /* Skip pseudo interface. */
1428 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1431 zsend_interface_add(client
, ifp
);
1432 zsend_interface_link_params(client
, ifp
);
1433 zsend_interface_addresses(client
, ifp
);
1438 /* Unregister zebra server interface information. */
1439 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1444 * Handle message requesting interface be set up or down.
1446 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1449 struct interface
*ifp
;
1452 STREAM_GETL(msg
, ifindex
);
1453 STREAM_GETC(msg
, down
);
1456 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1459 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1460 ifindex
, down
? "on" : "off");
1461 zebra_if_set_protodown(ifp
, down
);
1464 "Cannot set protodown %s for interface %u; does not exist",
1465 down
? "on" : "off", ifindex
);
1473 bool zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1474 const unsigned int nexthop_num
)
1476 if (nexthop_num
> zrouter
.multipath_num
) {
1477 char buff
[PREFIX2STR_BUFFER
];
1480 prefix2str(p
, buff
, sizeof(buff
));
1483 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1484 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1485 caller
, (p
? buff
: "(NULL)"), nexthop_num
,
1486 zrouter
.multipath_num
);
1494 * Create a new nexthop based on a zapi nexthop.
1496 static struct nexthop
*nexthop_from_zapi(const struct zapi_nexthop
*api_nh
,
1497 uint32_t flags
, struct prefix
*p
,
1498 uint16_t backup_nexthop_num
)
1500 struct nexthop
*nexthop
= NULL
;
1501 struct ipaddr vtep_ip
;
1502 struct interface
*ifp
;
1504 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1506 switch (api_nh
->type
) {
1507 case NEXTHOP_TYPE_IFINDEX
:
1508 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1510 case NEXTHOP_TYPE_IPV4
:
1511 if (IS_ZEBRA_DEBUG_RECV
) {
1512 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1514 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1515 nhbuf
, api_nh
->vrf_id
);
1517 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1520 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1521 if (IS_ZEBRA_DEBUG_RECV
) {
1522 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1524 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1525 __func__
, nhbuf
, api_nh
->vrf_id
,
1529 nexthop
= nexthop_from_ipv4_ifindex(
1530 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1533 /* Special handling for IPv4 routes sourced from EVPN:
1534 * the nexthop and associated MAC need to be installed.
1536 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1537 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1538 vtep_ip
.ipa_type
= IPADDR_V4
;
1539 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1540 sizeof(struct in_addr
));
1541 zebra_vxlan_evpn_vrf_route_add(
1542 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1545 case NEXTHOP_TYPE_IPV6
:
1546 if (IS_ZEBRA_DEBUG_RECV
) {
1547 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1549 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1550 nhbuf
, api_nh
->vrf_id
);
1552 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1554 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1555 if (IS_ZEBRA_DEBUG_RECV
) {
1556 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1558 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1559 __func__
, nhbuf
, api_nh
->vrf_id
,
1562 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1566 /* Special handling for IPv6 routes sourced from EVPN:
1567 * the nexthop and associated MAC need to be installed.
1569 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1570 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1571 vtep_ip
.ipa_type
= IPADDR_V6
;
1572 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1573 sizeof(struct in6_addr
));
1574 zebra_vxlan_evpn_vrf_route_add(
1575 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1578 case NEXTHOP_TYPE_BLACKHOLE
:
1579 if (IS_ZEBRA_DEBUG_RECV
)
1580 zlog_debug("%s: nh blackhole %d",
1581 __func__
, api_nh
->bh_type
);
1583 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1587 /* Return early if we couldn't process the zapi nexthop */
1588 if (nexthop
== NULL
) {
1592 /* Mark nexthop as onlink either if client has explicitly told us
1593 * to or if the nexthop is on an 'unnumbered' interface.
1595 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1596 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1597 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1598 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1599 if (ifp
&& connected_is_unnumbered(ifp
))
1600 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1603 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1604 nexthop
->weight
= api_nh
->weight
;
1606 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1607 /* Validate count */
1608 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1609 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1610 zlog_debug("%s: invalid backup nh count %d",
1611 __func__
, api_nh
->backup_num
);
1612 nexthop_free(nexthop
);
1617 /* Copy backup info */
1618 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1619 nexthop
->backup_num
= api_nh
->backup_num
;
1621 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1622 /* Validate backup index */
1623 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1624 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1626 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1627 zlog_debug("%s: invalid backup nh idx %d",
1629 api_nh
->backup_idx
[i
]);
1630 nexthop_free(nexthop
);
1641 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1642 struct zapi_nexthop
*nhops
, uint32_t flags
,
1643 uint32_t message
, uint16_t nexthop_num
,
1644 uint16_t backup_nh_num
,
1645 struct nexthop_group
**png
,
1646 struct nhg_backup_info
**pbnhg
)
1648 struct nexthop_group
*ng
= NULL
;
1649 struct nhg_backup_info
*bnhg
= NULL
;
1651 struct nexthop
*last_nh
= NULL
;
1653 assert(!(png
&& pbnhg
));
1656 ng
= nexthop_group_new();
1658 if (pbnhg
&& backup_nh_num
> 0) {
1659 if (IS_ZEBRA_DEBUG_RECV
)
1660 zlog_debug("%s: adding %d backup nexthops", __func__
,
1663 bnhg
= zebra_nhg_backup_alloc();
1667 * TBD should _all_ of the nexthop add operations use
1668 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1669 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1671 for (i
= 0; i
< nexthop_num
; i
++) {
1672 struct nexthop
*nexthop
;
1673 enum lsp_types_t label_type
;
1674 char nhbuf
[NEXTHOP_STRLEN
];
1675 char labelbuf
[MPLS_LABEL_STRLEN
];
1676 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1678 /* Convert zapi nexthop */
1679 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1682 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1683 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1684 __func__
, nexthop_num
, i
);
1686 nexthop_group_delete(&ng
);
1688 zebra_nhg_backup_free(&bnhg
);
1693 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1694 if (IS_ZEBRA_DEBUG_RECV
) {
1695 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1696 zlog_debug("%s: backup nh %s with BACKUP flag!",
1699 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1700 nexthop
->backup_num
= 0;
1703 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1704 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1705 nexthop
->srte_color
= api_nh
->srte_color
;
1708 /* MPLS labels for BGP-LU or Segment Routing */
1709 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1710 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1711 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1712 && api_nh
->label_num
> 0) {
1714 label_type
= lsp_type_from_re_type(client
->proto
);
1715 nexthop_add_labels(nexthop
, label_type
,
1717 &api_nh
->labels
[0]);
1720 if (IS_ZEBRA_DEBUG_RECV
) {
1724 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1726 if (nexthop
->nh_label
&&
1727 nexthop
->nh_label
->num_labels
> 0) {
1728 mpls_label2str(nexthop
->nh_label
->num_labels
,
1729 nexthop
->nh_label
->label
,
1730 labelbuf
, sizeof(labelbuf
),
1734 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1735 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1739 /* Add new nexthop to temporary list. This list is
1740 * canonicalized - sorted - so that it can be hashed
1741 * later in route processing. We expect that the sender
1742 * has sent the list sorted, and the zapi client api
1743 * attempts to enforce that, so this should be
1744 * inexpensive - but it is necessary to support shared
1747 nexthop_group_add_sorted(ng
, nexthop
);
1750 /* Note that the order of the backup nexthops is
1751 * significant, so we don't sort this list as we do the
1752 * primary nexthops, we just append.
1755 NEXTHOP_APPEND(last_nh
, nexthop
);
1757 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1764 /* succesfully read, set caller pointers now */
1774 static int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1777 struct zapi_nexthop
*znh
;
1779 STREAM_GETW(s
, api_nhg
->proto
);
1780 STREAM_GETL(s
, api_nhg
->id
);
1782 if (cmd
== ZEBRA_NHG_DEL
)
1786 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1788 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1791 if (api_nhg
->nexthop_num
<= 0) {
1792 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1793 "%s: No nexthops sent", __func__
);
1797 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1798 znh
= &((api_nhg
->nexthops
)[i
]);
1800 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1801 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1802 "%s: Nexthop creation failed", __func__
);
1807 /* Backup Nexthops */
1808 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1810 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1813 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1814 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1816 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1817 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1818 "%s: Backup Nexthop creation failed",
1829 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1830 "%s: Nexthop Group decode failed with some sort of stream read failure",
1835 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1838 struct zapi_nhg api_nhg
= {};
1839 struct nhg_hash_entry
*nhe
;
1842 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1843 if (IS_ZEBRA_DEBUG_RECV
)
1844 zlog_debug("%s: Unable to decode zapi_nhg sent",
1850 * Delete the received nhg id
1852 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1855 zebra_nhg_decrement_ref(nhe
);
1856 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1857 client
->session_id
, api_nhg
.id
,
1860 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1861 client
->session_id
, api_nhg
.id
,
1862 ZAPI_NHG_REMOVE_FAIL
);
1865 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1868 struct zapi_nhg api_nhg
= {};
1869 struct nexthop_group
*nhg
= NULL
;
1870 struct nhg_backup_info
*bnhg
= NULL
;
1871 struct nhg_hash_entry
*nhe
;
1874 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1875 if (IS_ZEBRA_DEBUG_RECV
)
1876 zlog_debug("%s: Unable to decode zapi_nhg sent",
1881 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1882 api_nhg
.nexthop_num
,
1883 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1884 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1885 api_nhg
.backup_nexthop_num
,
1886 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1888 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1889 "%s: Nexthop Group Creation failed", __func__
);
1896 nhe
= zebra_nhg_proto_add(api_nhg
.id
, api_nhg
.proto
, client
->instance
,
1897 client
->session_id
, nhg
, 0);
1899 nexthop_group_delete(&nhg
);
1900 zebra_nhg_backup_free(&bnhg
);
1904 * Assume fully resolved for now and install.
1906 * Resolution is going to need some more work.
1909 /* If there's a failure, notify sender immediately */
1911 zsend_nhg_notify(api_nhg
.proto
, client
->instance
,
1912 client
->session_id
, api_nhg
.id
,
1913 ZAPI_NHG_FAIL_INSTALL
);
1916 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1919 struct zapi_route api
;
1921 struct prefix_ipv6
*src_p
= NULL
;
1922 struct route_entry
*re
;
1923 struct nexthop_group
*ng
= NULL
;
1924 struct nhg_backup_info
*bnhg
= NULL
;
1927 struct nhg_hash_entry nhe
;
1930 if (zapi_route_decode(s
, &api
) < 0) {
1931 if (IS_ZEBRA_DEBUG_RECV
)
1932 zlog_debug("%s: Unable to decode zapi_route sent",
1937 vrf_id
= zvrf_id(zvrf
);
1939 if (IS_ZEBRA_DEBUG_RECV
)
1940 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
1941 __func__
, vrf_id
, api
.tableid
, &api
.prefix
,
1942 (int)api
.message
, api
.flags
);
1944 /* Allocate new route. */
1945 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1946 re
->type
= api
.type
;
1947 re
->instance
= api
.instance
;
1948 re
->flags
= api
.flags
;
1949 re
->uptime
= monotime(NULL
);
1950 re
->vrf_id
= vrf_id
;
1953 re
->table
= api
.tableid
;
1955 re
->table
= zvrf
->table_id
;
1957 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
1958 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1959 || api
.nexthop_num
== 0)) {
1961 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1962 "%s: received a route without nexthops for prefix %pFX from client %s",
1963 __func__
, &api
.prefix
,
1964 zebra_route_string(client
->proto
));
1966 XFREE(MTYPE_RE
, re
);
1970 /* Report misuse of the backup flag */
1971 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
1972 && api
.backup_nexthop_num
== 0) {
1973 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1975 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1976 __func__
, zebra_route_string(client
->proto
),
1980 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
))
1981 re
->nhe_id
= api
.nhgid
;
1984 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
1985 api
.flags
, api
.message
, api
.nexthop_num
,
1986 api
.backup_nexthop_num
, &ng
, NULL
)
1987 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
1988 api
.flags
, api
.message
,
1989 api
.backup_nexthop_num
,
1990 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
1992 nexthop_group_delete(&ng
);
1993 zebra_nhg_backup_free(&bnhg
);
1994 XFREE(MTYPE_RE
, re
);
1998 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1999 re
->distance
= api
.distance
;
2000 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
2001 re
->metric
= api
.metric
;
2002 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
2004 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
2007 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
)) {
2008 re
->opaque
= XMALLOC(MTYPE_OPAQUE
,
2009 sizeof(struct opaque
) + api
.opaque
.length
);
2010 re
->opaque
->length
= api
.opaque
.length
;
2011 memcpy(re
->opaque
->data
, api
.opaque
.data
, re
->opaque
->length
);
2014 afi
= family2afi(api
.prefix
.family
);
2015 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2016 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2017 "%s: Received SRC Prefix but afi is not v6",
2019 nexthop_group_delete(&ng
);
2020 zebra_nhg_backup_free(&bnhg
);
2021 XFREE(MTYPE_RE
, re
);
2024 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2025 src_p
= &api
.src_prefix
;
2027 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
2028 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
2029 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
2030 __func__
, api
.safi
);
2031 nexthop_group_delete(&ng
);
2032 zebra_nhg_backup_free(&bnhg
);
2033 XFREE(MTYPE_RE
, re
);
2038 * If we have an ID, this proto owns the NHG it sent along with the
2039 * route, so we just send the ID into rib code with it.
2041 * Havent figured out how to handle backup NHs with this yet, so lets
2042 * keep that separate.
2043 * Include backup info with the route. We use a temporary nhe here;
2044 * if this is a new/unknown nhe, a new copy will be allocated
2048 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
2049 nhe
.nhg
.nexthop
= ng
->nexthop
;
2050 nhe
.backup_info
= bnhg
;
2052 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
,
2055 /* At this point, these allocations are not needed: 're' has been
2056 * retained or freed, and if 're' still exists, it is using
2057 * a reference to a shared group object.
2059 nexthop_group_delete(&ng
);
2061 zebra_nhg_backup_free(&bnhg
);
2064 switch (api
.prefix
.family
) {
2067 client
->v4_route_add_cnt
++;
2069 client
->v4_route_upd8_cnt
++;
2073 client
->v6_route_add_cnt
++;
2075 client
->v6_route_upd8_cnt
++;
2080 void zapi_opaque_free(struct opaque
*opaque
)
2082 XFREE(MTYPE_OPAQUE
, opaque
);
2085 static void zread_route_del(ZAPI_HANDLER_ARGS
)
2088 struct zapi_route api
;
2090 struct prefix_ipv6
*src_p
= NULL
;
2094 if (zapi_route_decode(s
, &api
) < 0)
2097 afi
= family2afi(api
.prefix
.family
);
2098 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2099 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2100 "%s: Received a src prefix while afi is not v6",
2104 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2105 src_p
= &api
.src_prefix
;
2108 table_id
= api
.tableid
;
2110 table_id
= zvrf
->table_id
;
2112 if (IS_ZEBRA_DEBUG_RECV
)
2113 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2114 __func__
, zvrf_id(zvrf
), table_id
, &api
.prefix
,
2115 (int)api
.message
, api
.flags
);
2117 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2118 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
2119 api
.distance
, false);
2122 switch (api
.prefix
.family
) {
2124 client
->v4_route_del_cnt
++;
2127 client
->v6_route_del_cnt
++;
2132 /* MRIB Nexthop lookup for IPv4. */
2133 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2135 struct in_addr addr
;
2136 struct route_entry
*re
;
2138 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
2139 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
2140 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
2146 /* Register zebra server router-id information. Send current router-id */
2147 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2153 STREAM_GETW(msg
, afi
);
2155 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2157 "Invalid AFI %u while registering for router ID notifications",
2159 goto stream_failure
;
2162 /* Router-id information is needed. */
2163 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2165 router_id_get(afi
, &p
, zvrf
);
2167 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2173 /* Unregister zebra server router-id information. */
2174 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2178 STREAM_GETW(msg
, afi
);
2180 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2182 "Invalid AFI %u while unregistering from router ID notifications",
2184 goto stream_failure
;
2187 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2193 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2195 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2197 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2198 stream_putl(s
, vrf_get_backend());
2199 stream_putc(s
, mpls_enabled
);
2200 stream_putl(s
, zrouter
.multipath_num
);
2201 stream_putc(s
, zebra_mlag_get_role());
2203 stream_putw_at(s
, 0, stream_get_endp(s
));
2204 zserv_send_message(client
, s
);
2207 void zsend_capabilities_all_clients(void)
2209 struct listnode
*node
, *nnode
;
2210 struct zebra_vrf
*zvrf
;
2211 struct zserv
*client
;
2213 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2214 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2215 /* Do not send unsolicited messages to synchronous clients. */
2216 if (client
->synchronous
)
2219 zsend_capabilities(client
, zvrf
);
2223 /* Tie up route-type and client->sock */
2224 static void zread_hello(ZAPI_HANDLER_ARGS
)
2226 /* type of protocol (lib/zebra.h) */
2228 unsigned short instance
;
2230 uint8_t synchronous
;
2231 uint32_t session_id
;
2233 STREAM_GETC(msg
, proto
);
2234 STREAM_GETW(msg
, instance
);
2235 STREAM_GETL(msg
, session_id
);
2236 STREAM_GETC(msg
, notify
);
2237 STREAM_GETC(msg
, synchronous
);
2239 client
->notify_owner
= true;
2242 client
->synchronous
= true;
2244 /* accept only dynamic routing protocols */
2245 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2247 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2248 client
->sock
, zebra_route_string(proto
),
2251 zlog_notice("client protocol instance %d", instance
);
2253 client
->proto
= proto
;
2254 client
->instance
= instance
;
2255 client
->session_id
= session_id
;
2257 /* Graceful restart processing for client connect */
2258 zebra_gr_client_reconnect(client
);
2261 if (!client
->synchronous
) {
2262 zsend_capabilities(client
, zvrf
);
2263 zebra_vrf_update_all(client
);
2269 /* Unregister all information in a VRF. */
2270 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2275 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2276 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2277 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2278 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2279 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2284 * Validate incoming zapi mpls lsp / labels message
2286 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2292 const struct zapi_nexthop
*znh
;
2294 /* Validate backup info: no duplicates for a single primary */
2295 if (zl
->backup_nexthop_num
== 0) {
2300 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2301 znh
= &zl
->nexthops
[j
];
2303 memset(bits
, 0, sizeof(bits
));
2305 for (i
= 0; i
< znh
->backup_num
; i
++) {
2306 idx
= znh
->backup_idx
[i
] / 32;
2308 ival
= 1 << znh
->backup_idx
[i
] % 32;
2310 /* Check whether value is already used */
2311 if (ival
& bits
[idx
]) {
2314 if (IS_ZEBRA_DEBUG_RECV
)
2315 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2317 znh
->backup_idx
[i
]);
2321 /* Mark index value */
2334 * Handle request to create an MPLS LSP.
2336 * A single message can fully specify an LSP with multiple nexthops.
2338 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2339 * updated to use the received label(s).
2341 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2344 struct zapi_labels zl
;
2347 /* Get input stream. */
2349 if (zapi_labels_decode(s
, &zl
) < 0) {
2350 if (IS_ZEBRA_DEBUG_RECV
)
2351 zlog_debug("%s: Unable to decode zapi_labels sent",
2359 /* Validate; will debug on failure */
2360 if (zapi_labels_validate(&zl
) < 0)
2363 ret
= mpls_zapi_labels_process(true, zvrf
, &zl
);
2365 if (IS_ZEBRA_DEBUG_RECV
)
2366 zlog_debug("%s: Error processing zapi request",
2372 * Handle request to delete an MPLS LSP.
2374 * An LSP is identified by its type and local label. When the received message
2375 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2376 * listed LSP nexthops (aka NHLFEs) are deleted.
2378 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2379 * FEC (route) nexthops are deleted.
2381 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2384 struct zapi_labels zl
;
2387 /* Get input stream. */
2389 if (zapi_labels_decode(s
, &zl
) < 0) {
2390 if (IS_ZEBRA_DEBUG_RECV
)
2391 zlog_debug("%s: Unable to decode zapi_labels sent",
2399 if (zl
.nexthop_num
> 0) {
2400 ret
= mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2402 if (IS_ZEBRA_DEBUG_RECV
)
2403 zlog_debug("%s: Error processing zapi request",
2407 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2409 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2410 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2411 zl
.route
.type
, zl
.route
.instance
);
2416 * Handle request to add an MPLS LSP or change an existing one.
2418 * A single message can fully specify an LSP with multiple nexthops.
2420 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2421 * updated to use the received label(s).
2423 * NOTE: zebra will use route replace semantics (make-before-break) to update
2424 * the LSP in the forwarding plane if that's supported by the underlying
2427 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2430 struct zapi_labels zl
;
2432 /* Get input stream. */
2434 if (zapi_labels_decode(s
, &zl
) < 0) {
2435 if (IS_ZEBRA_DEBUG_RECV
)
2436 zlog_debug("%s: Unable to decode zapi_labels sent",
2444 /* Validate; will debug on failure */
2445 if (zapi_labels_validate(&zl
) < 0)
2448 /* This removes everything, then re-adds from the client's
2449 * zapi message. Since the LSP will be processed later, on this
2450 * this same pthread, all of the changes will 'appear' at once.
2452 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2453 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2454 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2455 zl
.route
.type
, zl
.route
.instance
);
2457 mpls_zapi_labels_process(true, zvrf
, &zl
);
2460 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2463 struct zapi_sr_policy zp
;
2464 struct zapi_srte_tunnel
*zt
;
2465 struct zebra_sr_policy
*policy
;
2467 /* Get input stream. */
2469 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2470 if (IS_ZEBRA_DEBUG_RECV
)
2471 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2472 __PRETTY_FUNCTION__
);
2475 zt
= &zp
.segment_list
;
2476 if (zt
->label_num
< 1) {
2477 if (IS_ZEBRA_DEBUG_RECV
)
2479 "%s: SR-TE tunnel must contain at least one label",
2480 __PRETTY_FUNCTION__
);
2487 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2489 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2490 /* TODO: per-VRF list of SR-TE policies. */
2491 policy
->zvrf
= zvrf
;
2493 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2496 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2499 struct zapi_sr_policy zp
;
2500 struct zebra_sr_policy
*policy
;
2502 /* Get input stream. */
2504 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2505 if (IS_ZEBRA_DEBUG_RECV
)
2506 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2507 __PRETTY_FUNCTION__
);
2514 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2516 if (IS_ZEBRA_DEBUG_RECV
)
2517 zlog_debug("%s: Unable to find SR-TE policy",
2518 __PRETTY_FUNCTION__
);
2522 zebra_sr_policy_del(policy
);
2525 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2526 char *name
, int status
)
2528 struct zserv
*client
;
2531 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2533 if (IS_ZEBRA_DEBUG_PACKET
)
2535 "Not notifying pathd about policy %s"
2536 " status change to %d",
2541 if (IS_ZEBRA_DEBUG_PACKET
)
2543 "Notifying pathd about policy %s status change"
2547 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2550 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2551 stream_putl(s
, color
);
2552 stream_put_ipaddr(s
, endpoint
);
2553 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2554 stream_putl(s
, status
);
2556 stream_putw_at(s
, 0, stream_get_endp(s
));
2558 return zserv_send_message(client
, s
);
2561 /* Send client close notify to client */
2562 int zsend_client_close_notify(struct zserv
*client
, struct zserv
*closed_client
)
2564 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2566 zclient_create_header(s
, ZEBRA_CLIENT_CLOSE_NOTIFY
, VRF_DEFAULT
);
2568 stream_putc(s
, closed_client
->proto
);
2569 stream_putw(s
, closed_client
->instance
);
2570 stream_putl(s
, closed_client
->session_id
);
2572 stream_putw_at(s
, 0, stream_get_endp(s
));
2574 return zserv_send_message(client
, s
);
2577 /* Send response to a table manager connect request to client */
2578 static void zread_table_manager_connect(struct zserv
*client
,
2579 struct stream
*msg
, vrf_id_t vrf_id
)
2584 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2589 STREAM_GETC(s
, proto
);
2590 STREAM_GETW(s
, instance
);
2592 /* accept only dynamic routing protocols */
2593 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2594 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2595 "client %d has wrong protocol %s", client
->sock
,
2596 zebra_route_string(proto
));
2597 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2600 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2601 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2602 zebra_route_string(proto
));
2603 client
->proto
= proto
;
2604 client
->instance
= instance
;
2607 * Release previous labels of same protocol and instance.
2608 * This is done in case it restarted from an unexpected shutdown.
2610 release_daemon_table_chunks(client
);
2612 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2618 static void zread_label_manager_connect(struct zserv
*client
,
2619 struct stream
*msg
, vrf_id_t vrf_id
)
2622 /* type of protocol (lib/zebra.h) */
2624 unsigned short instance
;
2626 /* Get input stream. */
2630 STREAM_GETC(s
, proto
);
2631 STREAM_GETW(s
, instance
);
2633 /* accept only dynamic routing protocols */
2634 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2635 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2636 "client %d has wrong protocol %s", client
->sock
,
2637 zebra_route_string(proto
));
2638 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2642 /* recall proto and instance in this socket */
2643 client
->proto
= proto
;
2644 client
->instance
= instance
;
2646 /* call hook for connection using wrapper */
2647 lm_client_connect_call(client
, vrf_id
);
2653 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2658 uint32_t size
, base
;
2659 struct label_manager_chunk
*lmc
= NULL
;
2661 unsigned short instance
;
2663 /* Get input stream. */
2667 STREAM_GETC(s
, proto
);
2668 STREAM_GETW(s
, instance
);
2669 STREAM_GETC(s
, keep
);
2670 STREAM_GETL(s
, size
);
2671 STREAM_GETL(s
, base
);
2673 assert(proto
== client
->proto
&& instance
== client
->instance
);
2675 /* call hook to get a chunk using wrapper */
2676 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2682 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2685 uint32_t start
, end
;
2687 unsigned short instance
;
2689 /* Get input stream. */
2693 STREAM_GETC(s
, proto
);
2694 STREAM_GETW(s
, instance
);
2695 STREAM_GETL(s
, start
);
2696 STREAM_GETL(s
, end
);
2698 assert(proto
== client
->proto
&& instance
== client
->instance
);
2700 /* call hook to release a chunk using wrapper */
2701 lm_release_chunk_call(client
, start
, end
);
2707 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2709 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2710 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2711 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2713 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2714 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2715 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2716 zread_release_label_chunk(client
, msg
);
2720 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2725 struct table_manager_chunk
*tmc
;
2727 /* Get input stream. */
2731 STREAM_GETL(s
, size
);
2733 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2735 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2736 "%s: Unable to assign Table Chunk of size %u",
2739 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2741 /* send response back */
2742 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2748 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2751 uint32_t start
, end
;
2753 /* Get input stream. */
2757 STREAM_GETL(s
, start
);
2758 STREAM_GETL(s
, end
);
2760 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2766 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2768 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2769 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2770 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2772 /* Sanity: don't allow 'unidentified' requests */
2773 if (!client
->proto
) {
2776 "Got table request from an unidentified client");
2779 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2780 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2781 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2782 zread_release_table_chunk(client
, msg
);
2786 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2789 char ifname
[IF_NAMESIZE
];
2793 union g_addr nexthop
;
2794 uint32_t local_label
;
2795 uint32_t remote_label
;
2797 union pw_protocol_fields data
;
2799 struct zebra_pw
*pw
;
2801 /* Get input stream. */
2805 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2806 ifname
[IF_NAMESIZE
- 1] = '\0';
2807 STREAM_GETL(s
, ifindex
);
2808 STREAM_GETL(s
, type
);
2812 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2815 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2820 STREAM_GETL(s
, local_label
);
2821 STREAM_GETL(s
, remote_label
);
2822 STREAM_GETC(s
, flags
);
2823 STREAM_GET(&data
, s
, sizeof(data
));
2824 protocol
= client
->proto
;
2826 pw
= zebra_pw_find(zvrf
, ifname
);
2827 switch (hdr
->command
) {
2830 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2831 "%s: pseudowire %s already exists [%s]",
2833 zserv_command_string(hdr
->command
));
2837 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2839 case ZEBRA_PW_DELETE
:
2841 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2842 "%s: pseudowire %s not found [%s]", __func__
,
2843 ifname
, zserv_command_string(hdr
->command
));
2847 zebra_pw_del(zvrf
, pw
);
2850 case ZEBRA_PW_UNSET
:
2852 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2853 "%s: pseudowire %s not found [%s]", __func__
,
2854 ifname
, zserv_command_string(hdr
->command
));
2858 switch (hdr
->command
) {
2862 case ZEBRA_PW_UNSET
:
2867 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2868 remote_label
, flags
, &data
);
2876 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2878 struct interface
*master
;
2879 struct interface
*slave
;
2880 struct stream
*s
= msg
;
2884 STREAM_GETL(s
, vrf_id
);
2885 STREAM_GETL(s
, ifindex
);
2886 master
= if_lookup_by_index(ifindex
, vrf_id
);
2888 STREAM_GETL(s
, vrf_id
);
2889 STREAM_GETL(s
, ifindex
);
2890 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2892 if (!master
|| !slave
)
2895 kernel_interface_set_master(master
, slave
);
2902 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2904 struct interface
*ifp
;
2905 mpls_label_t nlabel
;
2908 struct zebra_vrf
*def_zvrf
;
2909 enum lsp_types_t ltype
;
2912 STREAM_GETL(s
, nlabel
);
2913 STREAM_GETC(s
, afi
);
2915 if (!(IS_VALID_AFI(afi
))) {
2916 zlog_warn("Invalid AFI for VRF label: %u", afi
);
2920 if (nlabel
== zvrf
->label
[afi
]) {
2922 * Nothing to do here move along
2927 STREAM_GETC(s
, ltype
);
2929 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2930 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2932 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2935 zlog_debug("Unable to find specified Interface for %s",
2940 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2942 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2946 really_remove
= true;
2947 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2948 if (scrubber
== afi
)
2951 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2954 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2955 really_remove
= false;
2961 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2962 NEXTHOP_TYPE_IFINDEX
, NULL
,
2963 ifp
->ifindex
, false /*backup*/);
2966 if (nlabel
!= MPLS_LABEL_NONE
) {
2967 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2968 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2969 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2972 zvrf
->label
[afi
] = nlabel
;
2977 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2979 struct zebra_pbr_rule zpr
;
2982 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2985 STREAM_GETL(s
, total
);
2987 for (i
= 0; i
< total
; i
++) {
2988 memset(&zpr
, 0, sizeof(zpr
));
2990 zpr
.sock
= client
->sock
;
2991 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2992 STREAM_GETL(s
, zpr
.rule
.seq
);
2993 STREAM_GETL(s
, zpr
.rule
.priority
);
2994 STREAM_GETL(s
, zpr
.rule
.unique
);
2995 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2996 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2997 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2998 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2999 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
3000 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
3001 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
3002 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
3003 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
3004 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
3005 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
3006 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
3007 STREAM_GETL(s
, zpr
.rule
.action
.table
);
3008 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
3010 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
3011 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
3013 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
3014 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
3016 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
3017 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
3019 if (zpr
.rule
.filter
.src_port
)
3020 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3022 if (zpr
.rule
.filter
.dst_port
)
3023 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
3025 if (zpr
.rule
.filter
.dsfield
)
3026 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
3028 if (zpr
.rule
.filter
.fwmark
)
3029 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
3031 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
3032 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
3034 "Unsupported PBR source IP family: %s (%hhu)",
3035 family2str(zpr
.rule
.filter
.src_ip
.family
),
3036 zpr
.rule
.filter
.src_ip
.family
);
3039 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
3040 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
3042 "Unsupported PBR destination IP family: %s (%hhu)",
3043 family2str(zpr
.rule
.filter
.dst_ip
.family
),
3044 zpr
.rule
.filter
.dst_ip
.family
);
3049 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
3050 if (hdr
->command
== ZEBRA_RULE_ADD
)
3051 zebra_pbr_add_rule(&zpr
);
3053 zebra_pbr_del_rule(&zpr
);
3060 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
3062 struct zebra_pbr_ipset zpi
;
3067 STREAM_GETL(s
, total
);
3069 for (i
= 0; i
< total
; i
++) {
3070 memset(&zpi
, 0, sizeof(zpi
));
3072 zpi
.sock
= client
->sock
;
3073 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3074 STREAM_GETL(s
, zpi
.unique
);
3075 STREAM_GETL(s
, zpi
.type
);
3076 STREAM_GETC(s
, zpi
.family
);
3077 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3079 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3080 zebra_pbr_create_ipset(&zpi
);
3082 zebra_pbr_destroy_ipset(&zpi
);
3089 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3091 struct zebra_pbr_ipset_entry zpi
;
3092 struct zebra_pbr_ipset ipset
;
3097 STREAM_GETL(s
, total
);
3099 for (i
= 0; i
< total
; i
++) {
3100 memset(&zpi
, 0, sizeof(zpi
));
3101 memset(&ipset
, 0, sizeof(ipset
));
3103 zpi
.sock
= client
->sock
;
3104 STREAM_GETL(s
, zpi
.unique
);
3105 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3106 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3107 STREAM_GETC(s
, zpi
.src
.family
);
3108 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3109 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3110 STREAM_GETC(s
, zpi
.dst
.family
);
3111 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3112 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3114 STREAM_GETW(s
, zpi
.src_port_min
);
3115 STREAM_GETW(s
, zpi
.src_port_max
);
3116 STREAM_GETW(s
, zpi
.dst_port_min
);
3117 STREAM_GETW(s
, zpi
.dst_port_max
);
3118 STREAM_GETC(s
, zpi
.proto
);
3119 if (!is_default_prefix(&zpi
.src
))
3120 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3122 if (!is_default_prefix(&zpi
.dst
))
3123 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3124 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3125 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3126 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3127 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3128 if (zpi
.dst_port_max
!= 0)
3129 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3130 if (zpi
.src_port_max
!= 0)
3131 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3133 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3135 if (!(zpi
.dst
.family
== AF_INET
3136 || zpi
.dst
.family
== AF_INET6
)) {
3138 "Unsupported PBR destination IP family: %s (%hhu)",
3139 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3140 goto stream_failure
;
3142 if (!(zpi
.src
.family
== AF_INET
3143 || zpi
.src
.family
== AF_INET6
)) {
3145 "Unsupported PBR source IP family: %s (%hhu)",
3146 family2str(zpi
.src
.family
), zpi
.src
.family
);
3147 goto stream_failure
;
3150 /* calculate backpointer */
3152 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3154 if (!zpi
.backpointer
) {
3155 zlog_warn("ipset name specified: %s does not exist",
3157 goto stream_failure
;
3160 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3161 zebra_pbr_add_ipset_entry(&zpi
);
3163 zebra_pbr_del_ipset_entry(&zpi
);
3170 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3172 struct zebra_pbr_iptable
*zpi
=
3173 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
3178 zpi
->interface_name_list
= list_new();
3179 zpi
->sock
= client
->sock
;
3180 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3181 STREAM_GETL(s
, zpi
->unique
);
3182 STREAM_GETL(s
, zpi
->type
);
3183 STREAM_GETL(s
, zpi
->filter_bm
);
3184 STREAM_GETL(s
, zpi
->action
);
3185 STREAM_GETL(s
, zpi
->fwmark
);
3186 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3187 STREAM_GETC(s
, zpi
->family
);
3188 STREAM_GETW(s
, zpi
->pkt_len_min
);
3189 STREAM_GETW(s
, zpi
->pkt_len_max
);
3190 STREAM_GETW(s
, zpi
->tcp_flags
);
3191 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3192 STREAM_GETC(s
, zpi
->dscp_value
);
3193 STREAM_GETC(s
, zpi
->fragment
);
3194 STREAM_GETC(s
, zpi
->protocol
);
3195 STREAM_GETW(s
, zpi
->flow_label
);
3196 STREAM_GETL(s
, zpi
->nb_interface
);
3197 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3199 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3200 zebra_pbr_add_iptable(zpi
);
3202 zebra_pbr_del_iptable(zpi
);
3205 zebra_pbr_iptable_free(zpi
);
3210 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3214 struct interface
*ifp
;
3220 STREAM_GETL(s
, ifindex
);
3222 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3224 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3228 STREAM_GETC(s
, p
.family
);
3229 STREAM_GETC(s
, p
.prefixlen
);
3230 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3232 if (p
.family
== AF_INET
)
3237 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3239 dplane_neigh_discover(ifp
, &ip
);
3245 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3246 struct zmsghdr
*bad_hdr
)
3249 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3251 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3253 zserv_encode_error(s
, error
);
3255 client
->error_cnt
++;
3256 zserv_send_message(client
, s
);
3259 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3261 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3262 zlog_debug("ZAPI message specifies unknown VRF: %d",
3265 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3268 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3270 zlog_info("Zebra received unknown command %d", hdr
->command
);
3272 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3275 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3276 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3277 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3278 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3279 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3280 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3281 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3282 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3283 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3284 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3285 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3286 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3287 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
3288 [ZEBRA_HELLO
] = zread_hello
,
3289 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3290 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3291 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
3292 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
3293 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3294 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3295 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3297 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3298 #endif /* HAVE_BFDD */
3299 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3300 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3301 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3302 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3303 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3304 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3305 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3306 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3307 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3308 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3309 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3310 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3311 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3312 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3313 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3314 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3315 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3316 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3317 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3318 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3319 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3320 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3321 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3322 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
3323 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
3324 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3325 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3326 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3327 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3328 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3329 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3330 [ZEBRA_PW_SET
] = zread_pseudowire
,
3331 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3332 [ZEBRA_RULE_ADD
] = zread_rule
,
3333 [ZEBRA_RULE_DELETE
] = zread_rule
,
3334 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3335 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3336 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3337 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3338 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3339 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3340 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3341 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3342 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3343 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3344 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3345 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3346 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3347 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3348 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3349 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3350 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3351 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3352 [ZEBRA_ROUTE_NOTIFY_REQUEST
] = zread_route_notify_request
,
3353 [ZEBRA_EVPN_REMOTE_NH_ADD
] = zebra_evpn_proc_remote_nh
,
3354 [ZEBRA_EVPN_REMOTE_NH_DEL
] = zebra_evpn_proc_remote_nh
,
3358 * Process a batch of zapi messages.
3360 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3363 struct zebra_vrf
*zvrf
;
3365 struct stream_fifo temp_fifo
;
3367 stream_fifo_init(&temp_fifo
);
3369 while (stream_fifo_head(fifo
)) {
3370 msg
= stream_fifo_pop(fifo
);
3372 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3373 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3375 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3376 STREAM_READABLE(msg
),
3377 ZEBRA_MAX_PACKET_SIZ
);
3381 zapi_parse_header(msg
, &hdr
);
3383 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3384 && IS_ZEBRA_DEBUG_DETAIL
)
3385 zserv_log_message(NULL
, msg
, &hdr
);
3387 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3389 /* Before checking for a handler function, check for
3390 * special messages that are handled in another module;
3391 * we'll treat these as opaque.
3393 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3394 /* Reset message buffer */
3395 stream_set_getp(msg
, 0);
3397 stream_fifo_push(&temp_fifo
, msg
);
3399 /* Continue without freeing the message */
3405 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3407 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3411 if (hdr
.command
>= array_size(zserv_handlers
)
3412 || zserv_handlers
[hdr
.command
] == NULL
) {
3413 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3417 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3423 /* Dispatch any special messages from the temp fifo */
3424 if (stream_fifo_head(&temp_fifo
) != NULL
)
3425 zebra_opaque_enqueue_batch(&temp_fifo
);
3427 stream_fifo_deinit(&temp_fifo
);