2 * Zebra API message creation & consumption.
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/stream.h"
28 #include "lib/memory.h"
29 #include "lib/table.h"
30 #include "lib/network.h"
32 #include "lib/zclient.h"
33 #include "lib/privs.h"
34 #include "lib/nexthop.h"
36 #include "lib/libfrr.h"
37 #include "lib/lib_errors.h"
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
41 #include "zebra/zebra_memory.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/router-id.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_ptm.h"
50 #include "zebra/rtadv.h"
51 #include "zebra/zebra_mpls.h"
52 #include "zebra/zebra_mroute.h"
53 #include "zebra/zebra_vxlan.h"
54 #include "zebra/zebra_evpn_mh.h"
56 #include "zebra/zebra_pbr.h"
57 #include "zebra/table_manager.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_errors.h"
60 #include "zebra/zebra_mlag.h"
61 #include "zebra/connected.h"
62 #include "zebra/zebra_opaque.h"
63 #include "zebra/zebra_srte.h"
65 /* Encoding helpers -------------------------------------------------------- */
67 static void zserv_encode_interface(struct stream
*s
, struct interface
*ifp
)
69 /* Interface information. */
70 struct zebra_if
*zif
= ifp
->info
;
72 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
73 stream_putl(s
, ifp
->ifindex
);
74 stream_putc(s
, ifp
->status
);
75 stream_putq(s
, ifp
->flags
);
76 stream_putc(s
, ifp
->ptm_enable
);
77 stream_putc(s
, ifp
->ptm_status
);
78 stream_putl(s
, ifp
->metric
);
79 stream_putl(s
, ifp
->speed
);
80 stream_putl(s
, ifp
->mtu
);
81 stream_putl(s
, ifp
->mtu6
);
82 stream_putl(s
, ifp
->bandwidth
);
83 stream_putl(s
, zif
->link_ifindex
);
84 stream_putl(s
, ifp
->ll_type
);
85 stream_putl(s
, ifp
->hw_addr_len
);
87 stream_put(s
, ifp
->hw_addr
, ifp
->hw_addr_len
);
89 /* Then, Traffic Engineering parameters if any */
90 if (HAS_LINK_PARAMS(ifp
) && IS_LINK_PARAMS_SET(ifp
->link_params
)) {
92 zebra_interface_link_params_write(s
, ifp
);
96 /* Write packet size. */
97 stream_putw_at(s
, 0, stream_get_endp(s
));
100 static void zserv_encode_vrf(struct stream
*s
, struct zebra_vrf
*zvrf
)
102 struct vrf_data data
;
103 const char *netns_name
= zvrf_ns_name(zvrf
);
105 data
.l
.table_id
= zvrf
->table_id
;
108 strlcpy(data
.l
.netns_name
, basename((char *)netns_name
),
111 memset(data
.l
.netns_name
, 0, NS_NAMSIZ
);
112 /* Pass the tableid and the netns NAME */
113 stream_put(s
, &data
, sizeof(struct vrf_data
));
114 /* Interface information. */
115 stream_put(s
, zvrf_name(zvrf
), VRF_NAMSIZ
);
116 /* Write packet size. */
117 stream_putw_at(s
, 0, stream_get_endp(s
));
120 static int zserv_encode_nexthop(struct stream
*s
, struct nexthop
*nexthop
)
122 stream_putl(s
, nexthop
->vrf_id
);
123 stream_putc(s
, nexthop
->type
);
124 switch (nexthop
->type
) {
125 case NEXTHOP_TYPE_IPV4
:
126 case NEXTHOP_TYPE_IPV4_IFINDEX
:
127 stream_put_in_addr(s
, &nexthop
->gate
.ipv4
);
128 stream_putl(s
, nexthop
->ifindex
);
130 case NEXTHOP_TYPE_IPV6
:
131 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
133 case NEXTHOP_TYPE_IPV6_IFINDEX
:
134 stream_put(s
, &nexthop
->gate
.ipv6
, 16);
135 stream_putl(s
, nexthop
->ifindex
);
137 case NEXTHOP_TYPE_IFINDEX
:
138 stream_putl(s
, nexthop
->ifindex
);
148 * Zebra error addition adds error type.
152 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154 * | enum zebra_error_types |
155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 static void zserv_encode_error(struct stream
*s
, enum zebra_error_types error
)
160 stream_put(s
, &error
, sizeof(error
));
162 /* Write packet size. */
163 stream_putw_at(s
, 0, stream_get_endp(s
));
166 /* Send handlers ----------------------------------------------------------- */
168 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
170 * This function is called in the following situations:
171 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
173 * - at startup, when zebra figures out the available interfaces
174 * - when an interface is added (where support for
175 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
176 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
179 int zsend_interface_add(struct zserv
*client
, struct interface
*ifp
)
181 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
183 zclient_create_header(s
, ZEBRA_INTERFACE_ADD
, ifp
->vrf_id
);
184 zserv_encode_interface(s
, ifp
);
187 return zserv_send_message(client
, s
);
190 /* Interface deletion from zebra daemon. */
191 int zsend_interface_delete(struct zserv
*client
, struct interface
*ifp
)
193 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
195 zclient_create_header(s
, ZEBRA_INTERFACE_DELETE
, ifp
->vrf_id
);
196 zserv_encode_interface(s
, ifp
);
199 return zserv_send_message(client
, s
);
202 int zsend_vrf_add(struct zserv
*client
, struct zebra_vrf
*zvrf
)
204 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
206 zclient_create_header(s
, ZEBRA_VRF_ADD
, zvrf_id(zvrf
));
207 zserv_encode_vrf(s
, zvrf
);
209 client
->vrfadd_cnt
++;
210 return zserv_send_message(client
, s
);
213 /* VRF deletion from zebra daemon. */
214 int zsend_vrf_delete(struct zserv
*client
, struct zebra_vrf
*zvrf
)
217 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
219 zclient_create_header(s
, ZEBRA_VRF_DELETE
, zvrf_id(zvrf
));
220 zserv_encode_vrf(s
, zvrf
);
222 client
->vrfdel_cnt
++;
223 return zserv_send_message(client
, s
);
226 int zsend_interface_link_params(struct zserv
*client
, struct interface
*ifp
)
228 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
230 if (!ifp
->link_params
) {
235 zclient_create_header(s
, ZEBRA_INTERFACE_LINK_PARAMS
, ifp
->vrf_id
);
237 /* Add Interface Index */
238 stream_putl(s
, ifp
->ifindex
);
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s
, ifp
) == 0) {
246 /* Write packet size. */
247 stream_putw_at(s
, 0, stream_get_endp(s
));
249 return zserv_send_message(client
, s
);
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 * zsend_interface_address(DELETE)
274 * zebra_interface_address_delete_update
276 * | | if_delete_update
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
282 * | RTM_NEWADDR on routing/netlink socket
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M"
287 * ["no ipv6 address X:X::X:X/M"]
290 int zsend_interface_address(int cmd
, struct zserv
*client
,
291 struct interface
*ifp
, struct connected
*ifc
)
295 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
297 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
298 stream_putl(s
, ifp
->ifindex
);
300 /* Interface address flag. */
301 stream_putc(s
, ifc
->flags
);
303 /* Prefix information. */
305 stream_putc(s
, p
->family
);
306 blen
= prefix_blen(p
);
307 stream_put(s
, &p
->u
.prefix
, blen
);
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
315 stream_putc(s
, p
->prefixlen
);
318 p
= ifc
->destination
;
320 stream_put(s
, &p
->u
.prefix
, blen
);
322 stream_put(s
, NULL
, blen
);
324 /* Write packet size. */
325 stream_putw_at(s
, 0, stream_get_endp(s
));
327 client
->connected_rt_add_cnt
++;
328 return zserv_send_message(client
, s
);
331 static int zsend_interface_nbr_address(int cmd
, struct zserv
*client
,
332 struct interface
*ifp
,
333 struct nbr_connected
*ifc
)
336 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
339 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
340 stream_putl(s
, ifp
->ifindex
);
342 /* Prefix information. */
344 stream_putc(s
, p
->family
);
345 blen
= prefix_blen(p
);
346 stream_put(s
, &p
->u
.prefix
, blen
);
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
354 stream_putc(s
, p
->prefixlen
);
356 /* Write packet size. */
357 stream_putw_at(s
, 0, stream_get_endp(s
));
359 return zserv_send_message(client
, s
);
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface
*ifp
,
364 struct nbr_connected
*ifc
)
366 struct listnode
*node
, *nnode
;
367 struct zserv
*client
;
370 if (IS_ZEBRA_DEBUG_EVENT
) {
371 char buf
[INET6_ADDRSTRLEN
];
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
378 p
->prefixlen
, ifc
->ifp
->name
);
381 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
382 /* Do not send unsolicited messages to synchronous clients. */
383 if (client
->synchronous
)
386 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
391 /* Interface address deletion. */
392 static void zebra_interface_nbr_address_delete_update(struct interface
*ifp
,
393 struct nbr_connected
*ifc
)
395 struct listnode
*node
, *nnode
;
396 struct zserv
*client
;
399 if (IS_ZEBRA_DEBUG_EVENT
) {
400 char buf
[INET6_ADDRSTRLEN
];
404 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
405 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
407 p
->prefixlen
, ifc
->ifp
->name
);
410 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
411 /* Do not send unsolicited messages to synchronous clients. */
412 if (client
->synchronous
)
415 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
,
420 /* Send addresses on interface to client */
421 int zsend_interface_addresses(struct zserv
*client
, struct interface
*ifp
)
423 struct listnode
*cnode
, *cnnode
;
425 struct nbr_connected
*nc
;
427 /* Send interface addresses. */
428 for (ALL_LIST_ELEMENTS(ifp
->connected
, cnode
, cnnode
, c
)) {
429 if (!CHECK_FLAG(c
->conf
, ZEBRA_IFC_REAL
))
432 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
, client
,
438 /* Send interface neighbors. */
439 for (ALL_LIST_ELEMENTS(ifp
->nbr_connected
, cnode
, cnnode
, nc
)) {
440 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
,
449 /* Notify client about interface moving from one VRF to another.
450 * Whether client is interested in old and new VRF is checked by caller.
452 int zsend_interface_vrf_update(struct zserv
*client
, struct interface
*ifp
,
455 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
457 zclient_create_header(s
, ZEBRA_INTERFACE_VRF_UPDATE
, ifp
->vrf_id
);
459 /* Fill in the name of the interface and its new VRF (id) */
460 stream_put(s
, ifp
->name
, INTERFACE_NAMSIZ
);
461 stream_putl(s
, vrf_id
);
463 /* Write packet size. */
464 stream_putw_at(s
, 0, stream_get_endp(s
));
466 client
->if_vrfchg_cnt
++;
467 return zserv_send_message(client
, s
);
470 /* Add new nbr connected IPv6 address */
471 void nbr_connected_add_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
473 struct nbr_connected
*ifc
;
477 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
478 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
480 ifc
= listnode_head(ifp
->nbr_connected
);
483 ifc
= nbr_connected_new();
484 ifc
->address
= prefix_new();
486 listnode_add(ifp
->nbr_connected
, ifc
);
489 prefix_copy(ifc
->address
, &p
);
491 zebra_interface_nbr_address_add_update(ifp
, ifc
);
493 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 1);
496 void nbr_connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
)
498 struct nbr_connected
*ifc
;
502 IPV6_ADDR_COPY(&p
.u
.prefix6
, address
);
503 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
505 ifc
= nbr_connected_check(ifp
, &p
);
509 listnode_delete(ifp
->nbr_connected
, ifc
);
511 zebra_interface_nbr_address_delete_update(ifp
, ifc
);
513 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp
, address
, 0);
515 nbr_connected_free(ifc
);
519 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
520 * ZEBRA_INTERFACE_DOWN.
522 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
523 * the clients in one of 2 situations:
524 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
525 * - a vty command modifying the bandwidth of an interface is received.
526 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
528 int zsend_interface_update(int cmd
, struct zserv
*client
, struct interface
*ifp
)
530 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
532 zclient_create_header(s
, cmd
, ifp
->vrf_id
);
533 zserv_encode_interface(s
, ifp
);
535 if (cmd
== ZEBRA_INTERFACE_UP
)
538 client
->ifdown_cnt
++;
540 return zserv_send_message(client
, s
);
543 int zsend_redistribute_route(int cmd
, struct zserv
*client
,
544 const struct prefix
*p
,
545 const struct prefix
*src_p
,
546 const struct route_entry
*re
)
548 struct zapi_route api
;
549 struct zapi_nexthop
*api_nh
;
550 struct nexthop
*nexthop
;
554 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
556 memset(&api
, 0, sizeof(api
));
557 api
.vrf_id
= re
->vrf_id
;
559 api
.safi
= SAFI_UNICAST
;
560 api
.instance
= re
->instance
;
561 api
.flags
= re
->flags
;
563 afi
= family2afi(p
->family
);
566 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
567 client
->redist_v4_add_cnt
++;
569 client
->redist_v4_del_cnt
++;
572 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
573 client
->redist_v6_add_cnt
++;
575 client
->redist_v6_del_cnt
++;
584 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
585 memcpy(&api
.src_prefix
, src_p
, sizeof(api
.src_prefix
));
588 for (nexthop
= re
->nhe
->nhg
.nexthop
;
589 nexthop
; nexthop
= nexthop
->next
) {
590 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
593 api_nh
= &api
.nexthops
[count
];
594 api_nh
->vrf_id
= nexthop
->vrf_id
;
595 api_nh
->type
= nexthop
->type
;
596 api_nh
->weight
= nexthop
->weight
;
597 switch (nexthop
->type
) {
598 case NEXTHOP_TYPE_BLACKHOLE
:
599 api_nh
->bh_type
= nexthop
->bh_type
;
601 case NEXTHOP_TYPE_IPV4
:
602 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
604 case NEXTHOP_TYPE_IPV4_IFINDEX
:
605 api_nh
->gate
.ipv4
= nexthop
->gate
.ipv4
;
606 api_nh
->ifindex
= nexthop
->ifindex
;
608 case NEXTHOP_TYPE_IFINDEX
:
609 api_nh
->ifindex
= nexthop
->ifindex
;
611 case NEXTHOP_TYPE_IPV6
:
612 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
614 case NEXTHOP_TYPE_IPV6_IFINDEX
:
615 api_nh
->gate
.ipv6
= nexthop
->gate
.ipv6
;
616 api_nh
->ifindex
= nexthop
->ifindex
;
623 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
624 api
.nexthop_num
= count
;
628 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
629 api
.distance
= re
->distance
;
630 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
631 api
.metric
= re
->metric
;
633 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
636 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
639 struct stream
*s
= stream_new(stream_size
);
641 /* Encode route and send. */
642 if (zapi_route_encode(cmd
, s
, &api
) < 0) {
647 if (IS_ZEBRA_DEBUG_SEND
) {
648 char buf_prefix
[PREFIX_STRLEN
];
650 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
652 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
653 __func__
, zserv_command_string(cmd
),
654 zebra_route_string(client
->proto
),
655 zebra_route_string(api
.type
), api
.vrf_id
,
658 return zserv_send_message(client
, s
);
662 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
663 * nexthop is not found on mrib. Returns both route metric and protocol
666 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv
*client
,
668 struct route_entry
*re
,
669 struct zebra_vrf
*zvrf
)
674 struct nexthop
*nexthop
;
676 /* Get output stream. */
677 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
680 /* Fill in result. */
681 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, zvrf_id(zvrf
));
682 stream_put_in_addr(s
, &addr
);
685 stream_putc(s
, re
->distance
);
686 stream_putl(s
, re
->metric
);
688 /* remember position for nexthop_num */
689 nump
= stream_get_endp(s
);
690 /* reserve room for nexthop_num */
693 * Only non-recursive routes are elegible to resolve the
694 * nexthop we are looking up. Therefore, we will just iterate
695 * over the top chain of nexthops.
697 for (nexthop
= re
->nhe
->nhg
.nexthop
; nexthop
;
698 nexthop
= nexthop
->next
)
699 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
700 num
+= zserv_encode_nexthop(s
, nexthop
);
702 /* store nexthop_num */
703 stream_putc_at(s
, nump
, num
);
705 stream_putc(s
, 0); /* distance */
706 stream_putl(s
, 0); /* metric */
707 stream_putc(s
, 0); /* nexthop_num */
710 stream_putw_at(s
, 0, stream_get_endp(s
));
712 return zserv_send_message(client
, s
);
715 static int nhg_notify(uint16_t type
, uint16_t instance
, uint32_t id
,
716 enum zapi_nhg_notify_owner note
)
718 struct zserv
*client
;
721 client
= zserv_find_client(type
, instance
);
723 if (IS_ZEBRA_DEBUG_PACKET
) {
724 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
725 type
, instance
, id
, note
);
730 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
733 zclient_create_header(s
, ZEBRA_NHG_NOTIFY_OWNER
, VRF_DEFAULT
);
735 stream_put(s
, ¬e
, sizeof(note
));
738 stream_putw_at(s
, 0, stream_get_endp(s
));
740 return zserv_send_message(client
, s
);
744 * Common utility send route notification, called from a path using a
745 * route_entry and from a path using a dataplane context.
747 static int route_notify_internal(const struct prefix
*p
, int type
,
748 uint16_t instance
, vrf_id_t vrf_id
,
750 enum zapi_route_notify_owner note
)
752 struct zserv
*client
;
756 client
= zserv_find_client(type
, instance
);
757 if (!client
|| !client
->notify_owner
) {
758 if (IS_ZEBRA_DEBUG_PACKET
) {
759 char buff
[PREFIX_STRLEN
];
762 "Not Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
763 zebra_route_string(type
),
764 prefix2str(p
, buff
, sizeof(buff
)), table_id
,
770 if (IS_ZEBRA_DEBUG_PACKET
) {
771 char buff
[PREFIX_STRLEN
];
773 zlog_debug("Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
774 zebra_route_string(type
),
775 prefix2str(p
, buff
, sizeof(buff
)), table_id
, note
,
779 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
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 stream_putw_at(s
, 0, stream_get_endp(s
));
796 return zserv_send_message(client
, s
);
799 int zsend_route_notify_owner(struct route_entry
*re
, const struct prefix
*p
,
800 enum zapi_route_notify_owner note
)
802 return (route_notify_internal(p
, re
->type
, re
->instance
, re
->vrf_id
,
807 * Route-owner notification using info from dataplane update context.
809 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx
*ctx
,
810 enum zapi_route_notify_owner note
)
812 return (route_notify_internal(dplane_ctx_get_dest(ctx
),
813 dplane_ctx_get_type(ctx
),
814 dplane_ctx_get_instance(ctx
),
815 dplane_ctx_get_vrf(ctx
),
816 dplane_ctx_get_table(ctx
),
820 void zsend_rule_notify_owner(const struct zebra_dplane_ctx
*ctx
,
821 enum zapi_rule_notify_owner note
)
823 struct listnode
*node
;
824 struct zserv
*client
;
827 if (IS_ZEBRA_DEBUG_PACKET
)
828 zlog_debug("%s: Notifying %u", __func__
,
829 dplane_ctx_rule_get_unique(ctx
));
831 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
832 if (dplane_ctx_rule_get_sock(ctx
) == client
->sock
)
839 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
841 zclient_create_header(s
, ZEBRA_RULE_NOTIFY_OWNER
, VRF_DEFAULT
);
842 stream_put(s
, ¬e
, sizeof(note
));
843 stream_putl(s
, dplane_ctx_rule_get_seq(ctx
));
844 stream_putl(s
, dplane_ctx_rule_get_priority(ctx
));
845 stream_putl(s
, dplane_ctx_rule_get_unique(ctx
));
846 stream_put(s
, dplane_ctx_rule_get_ifname(ctx
), INTERFACE_NAMSIZ
);
848 stream_putw_at(s
, 0, stream_get_endp(s
));
850 zserv_send_message(client
, s
);
853 void zsend_ipset_notify_owner(struct zebra_pbr_ipset
*ipset
,
854 enum zapi_ipset_notify_owner note
)
856 struct listnode
*node
;
857 struct zserv
*client
;
860 if (IS_ZEBRA_DEBUG_PACKET
)
861 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
863 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
864 if (ipset
->sock
== client
->sock
)
871 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
873 zclient_create_header(s
, ZEBRA_IPSET_NOTIFY_OWNER
, VRF_DEFAULT
);
874 stream_put(s
, ¬e
, sizeof(note
));
875 stream_putl(s
, ipset
->unique
);
876 stream_put(s
, ipset
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
877 stream_putw_at(s
, 0, stream_get_endp(s
));
879 zserv_send_message(client
, s
);
882 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry
*ipset
,
883 enum zapi_ipset_entry_notify_owner note
)
885 struct listnode
*node
;
886 struct zserv
*client
;
889 if (IS_ZEBRA_DEBUG_PACKET
)
890 zlog_debug("%s: Notifying %u", __func__
, ipset
->unique
);
892 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
893 if (ipset
->sock
== client
->sock
)
900 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
902 zclient_create_header(s
, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
, VRF_DEFAULT
);
903 stream_put(s
, ¬e
, sizeof(note
));
904 stream_putl(s
, ipset
->unique
);
905 stream_put(s
, ipset
->backpointer
->ipset_name
, ZEBRA_IPSET_NAME_SIZE
);
906 stream_putw_at(s
, 0, stream_get_endp(s
));
908 zserv_send_message(client
, s
);
911 void zsend_iptable_notify_owner(struct zebra_pbr_iptable
*iptable
,
912 enum zapi_iptable_notify_owner note
)
914 struct listnode
*node
;
915 struct zserv
*client
;
918 if (IS_ZEBRA_DEBUG_PACKET
)
919 zlog_debug("%s: Notifying %u", __func__
, iptable
->unique
);
921 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
922 if (iptable
->sock
== client
->sock
)
929 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
931 zclient_create_header(s
, ZEBRA_IPTABLE_NOTIFY_OWNER
, VRF_DEFAULT
);
932 stream_put(s
, ¬e
, sizeof(note
));
933 stream_putl(s
, iptable
->unique
);
934 stream_putw_at(s
, 0, stream_get_endp(s
));
936 zserv_send_message(client
, s
);
939 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
940 int zsend_router_id_update(struct zserv
*client
, afi_t afi
, struct prefix
*p
,
946 /* Check this client need interface information. */
947 if (!vrf_bitmap_check(client
->ridinfo
[afi
], vrf_id
))
950 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
953 zclient_create_header(s
, ZEBRA_ROUTER_ID_UPDATE
, vrf_id
);
955 /* Prefix information. */
956 stream_putc(s
, p
->family
);
957 blen
= prefix_blen(p
);
958 stream_put(s
, &p
->u
.prefix
, blen
);
959 stream_putc(s
, p
->prefixlen
);
961 /* Write packet size. */
962 stream_putw_at(s
, 0, stream_get_endp(s
));
964 return zserv_send_message(client
, s
);
968 * Function used by Zebra to send a PW status update to LDP daemon
970 int zsend_pw_update(struct zserv
*client
, struct zebra_pw
*pw
)
972 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
974 zclient_create_header(s
, ZEBRA_PW_STATUS_UPDATE
, pw
->vrf_id
);
975 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
976 stream_putl(s
, pw
->ifindex
);
977 stream_putl(s
, pw
->status
);
979 /* Put length at the first point of the stream. */
980 stream_putw_at(s
, 0, stream_get_endp(s
));
982 return zserv_send_message(client
, s
);
985 /* Send response to a get label chunk request to client */
986 int zsend_assign_label_chunk_response(struct zserv
*client
, vrf_id_t vrf_id
,
987 struct label_manager_chunk
*lmc
)
990 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
992 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, vrf_id
);
994 stream_putc(s
, client
->proto
);
996 stream_putw(s
, client
->instance
);
1000 stream_putc(s
, lmc
->keep
);
1001 /* start and end labels */
1002 stream_putl(s
, lmc
->start
);
1003 stream_putl(s
, lmc
->end
);
1006 /* Write packet size. */
1007 stream_putw_at(s
, 0, stream_get_endp(s
));
1009 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1014 /* Send response to a label manager connect request to client */
1015 int zsend_label_manager_connect_response(struct zserv
*client
, vrf_id_t vrf_id
,
1016 unsigned short result
)
1019 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1021 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, vrf_id
);
1024 stream_putc(s
, client
->proto
);
1027 stream_putw(s
, client
->instance
);
1030 stream_putc(s
, result
);
1032 /* Write packet size. */
1033 stream_putw_at(s
, 0, stream_get_endp(s
));
1035 ret
= writen(client
->sock
, s
->data
, stream_get_endp(s
));
1041 /* Send response to a get table chunk request to client */
1042 static int zsend_assign_table_chunk_response(struct zserv
*client
,
1044 struct table_manager_chunk
*tmc
)
1046 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1048 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, vrf_id
);
1051 /* start and end labels */
1052 stream_putl(s
, tmc
->start
);
1053 stream_putl(s
, tmc
->end
);
1056 /* Write packet size. */
1057 stream_putw_at(s
, 0, stream_get_endp(s
));
1059 return zserv_send_message(client
, s
);
1062 static int zsend_table_manager_connect_response(struct zserv
*client
,
1066 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1068 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, vrf_id
);
1071 stream_putc(s
, result
);
1073 stream_putw_at(s
, 0, stream_get_endp(s
));
1075 return zserv_send_message(client
, s
);
1078 /* Inbound message handling ------------------------------------------------ */
1080 const int cmd2type
[] = {
1081 [ZEBRA_NEXTHOP_REGISTER
] = RNH_NEXTHOP_TYPE
,
1082 [ZEBRA_NEXTHOP_UNREGISTER
] = RNH_NEXTHOP_TYPE
,
1083 [ZEBRA_IMPORT_ROUTE_REGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1084 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = RNH_IMPORT_CHECK_TYPE
,
1087 /* Nexthop register */
1088 static void zread_rnh_register(ZAPI_HANDLER_ARGS
)
1093 unsigned short l
= 0;
1095 uint16_t type
= cmd2type
[hdr
->command
];
1097 bool flag_changed
= false;
1100 if (IS_ZEBRA_DEBUG_NHT
)
1102 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1103 zebra_route_string(client
->proto
), hdr
->length
,
1104 (type
== RNH_NEXTHOP_TYPE
) ? "nexthop" : "route",
1109 if (!client
->nh_reg_time
)
1110 client
->nh_reg_time
= monotime(NULL
);
1112 while (l
< hdr
->length
) {
1113 STREAM_GETC(s
, flags
);
1114 STREAM_GETW(s
, p
.family
);
1115 STREAM_GETC(s
, p
.prefixlen
);
1117 if (p
.family
== AF_INET
) {
1118 client
->v4_nh_watch_add_cnt
++;
1119 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1121 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1122 __func__
, p
.prefixlen
);
1125 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1126 l
+= IPV4_MAX_BYTELEN
;
1127 } else if (p
.family
== AF_INET6
) {
1128 client
->v6_nh_watch_add_cnt
++;
1129 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1131 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1132 __func__
, p
.prefixlen
);
1135 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1136 l
+= IPV6_MAX_BYTELEN
;
1139 EC_ZEBRA_UNKNOWN_FAMILY
,
1140 "rnh_register: Received unknown family type %d\n",
1144 rnh
= zebra_add_rnh(&p
, zvrf_id(zvrf
), type
, &exist
);
1148 orig_flags
= rnh
->flags
;
1149 if (type
== RNH_NEXTHOP_TYPE
) {
1151 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1152 SET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1154 && CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
))
1155 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_CONNECTED
);
1156 } else if (type
== RNH_IMPORT_CHECK_TYPE
) {
1158 && !CHECK_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
))
1159 SET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1161 && CHECK_FLAG(rnh
->flags
,
1162 ZEBRA_NHT_EXACT_MATCH
))
1163 UNSET_FLAG(rnh
->flags
, ZEBRA_NHT_EXACT_MATCH
);
1166 if (orig_flags
!= rnh
->flags
)
1167 flag_changed
= true;
1169 /* Anything not AF_INET/INET6 has been filtered out above */
1170 if (!exist
|| flag_changed
)
1171 zebra_evaluate_rnh(zvrf
, family2afi(p
.family
), 1, type
,
1174 zebra_add_rnh_client(rnh
, client
, type
, zvrf_id(zvrf
));
1181 /* Nexthop register */
1182 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS
)
1187 unsigned short l
= 0;
1188 uint16_t type
= cmd2type
[hdr
->command
];
1190 if (IS_ZEBRA_DEBUG_NHT
)
1192 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1193 zebra_route_string(client
->proto
), hdr
->length
,
1198 while (l
< hdr
->length
) {
1201 STREAM_GETC(s
, flags
);
1203 goto stream_failure
;
1205 STREAM_GETW(s
, p
.family
);
1206 STREAM_GETC(s
, p
.prefixlen
);
1208 if (p
.family
== AF_INET
) {
1209 client
->v4_nh_watch_rem_cnt
++;
1210 if (p
.prefixlen
> IPV4_MAX_BITLEN
) {
1212 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1213 __func__
, p
.prefixlen
);
1216 STREAM_GET(&p
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1217 l
+= IPV4_MAX_BYTELEN
;
1218 } else if (p
.family
== AF_INET6
) {
1219 client
->v6_nh_watch_rem_cnt
++;
1220 if (p
.prefixlen
> IPV6_MAX_BITLEN
) {
1222 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1223 __func__
, p
.prefixlen
);
1226 STREAM_GET(&p
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1227 l
+= IPV6_MAX_BYTELEN
;
1230 EC_ZEBRA_UNKNOWN_FAMILY
,
1231 "rnh_register: Received unknown family type %d\n",
1235 rnh
= zebra_lookup_rnh(&p
, zvrf_id(zvrf
), type
);
1237 client
->nh_dereg_time
= monotime(NULL
);
1238 zebra_remove_rnh_client(rnh
, client
, type
);
1245 #define ZEBRA_MIN_FEC_LENGTH 5
1248 static void zread_fec_register(ZAPI_HANDLER_ARGS
)
1251 unsigned short l
= 0;
1254 uint32_t label
= MPLS_INVALID_LABEL
;
1255 uint32_t label_index
= MPLS_INVALID_LABEL_INDEX
;
1258 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1263 * The minimum amount of data that can be sent for one fec
1266 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1268 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1269 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1274 while (l
< hdr
->length
) {
1275 STREAM_GETW(s
, flags
);
1276 memset(&p
, 0, sizeof(p
));
1277 STREAM_GETW(s
, p
.family
);
1278 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1280 EC_ZEBRA_UNKNOWN_FAMILY
,
1281 "fec_register: Received unknown family type %d\n",
1285 STREAM_GETC(s
, p
.prefixlen
);
1286 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1287 || (p
.family
== AF_INET6
1288 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1290 "%s: Specified prefix hdr->length: %d is to long for %d",
1291 __func__
, p
.prefixlen
, p
.family
);
1295 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1296 l
+= PSIZE(p
.prefixlen
);
1297 if (flags
& ZEBRA_FEC_REGISTER_LABEL
) {
1298 STREAM_GETL(s
, label
);
1300 } else if (flags
& ZEBRA_FEC_REGISTER_LABEL_INDEX
) {
1301 STREAM_GETL(s
, label_index
);
1305 zebra_mpls_fec_register(zvrf
, &p
, label
, label_index
, client
);
1312 /* FEC unregister */
1313 static void zread_fec_unregister(ZAPI_HANDLER_ARGS
)
1316 unsigned short l
= 0;
1321 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1326 * The minimum amount of data that can be sent for one
1327 * fec unregistration
1329 if (hdr
->length
< ZEBRA_MIN_FEC_LENGTH
) {
1331 EC_ZEBRA_IRDP_LEN_MISMATCH
,
1332 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1337 while (l
< hdr
->length
) {
1338 STREAM_GETW(s
, flags
);
1340 goto stream_failure
;
1342 memset(&p
, 0, sizeof(p
));
1343 STREAM_GETW(s
, p
.family
);
1344 if (p
.family
!= AF_INET
&& p
.family
!= AF_INET6
) {
1346 EC_ZEBRA_UNKNOWN_FAMILY
,
1347 "fec_unregister: Received unknown family type %d\n",
1351 STREAM_GETC(s
, p
.prefixlen
);
1352 if ((p
.family
== AF_INET
&& p
.prefixlen
> IPV4_MAX_BITLEN
)
1353 || (p
.family
== AF_INET6
1354 && p
.prefixlen
> IPV6_MAX_BITLEN
)) {
1356 "%s: Received prefix hdr->length %d which is greater than %d can support",
1357 __func__
, p
.prefixlen
, p
.family
);
1361 STREAM_GET(&p
.u
.prefix
, s
, PSIZE(p
.prefixlen
));
1362 l
+= PSIZE(p
.prefixlen
);
1363 zebra_mpls_fec_unregister(zvrf
, &p
, client
);
1372 * Register zebra server interface information.
1373 * Send current all interface and address information.
1375 static void zread_interface_add(ZAPI_HANDLER_ARGS
)
1378 struct interface
*ifp
;
1380 vrf_id_t vrf_id
= zvrf_id(zvrf
);
1381 if (vrf_id
!= VRF_DEFAULT
&& vrf_id
!= VRF_UNKNOWN
) {
1382 FOR_ALL_INTERFACES (zvrf
->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 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1395 FOR_ALL_INTERFACES (vrf
, ifp
) {
1396 /* Skip pseudo interface. */
1397 if (!CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1400 zsend_interface_add(client
, ifp
);
1401 zsend_interface_link_params(client
, ifp
);
1402 zsend_interface_addresses(client
, ifp
);
1407 /* Unregister zebra server interface information. */
1408 static void zread_interface_delete(ZAPI_HANDLER_ARGS
)
1413 * Handle message requesting interface be set up or down.
1415 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS
)
1418 struct interface
*ifp
;
1421 STREAM_GETL(msg
, ifindex
);
1422 STREAM_GETC(msg
, down
);
1425 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT
), ifindex
);
1428 zlog_info("Setting interface %s (%u): protodown %s", ifp
->name
,
1429 ifindex
, down
? "on" : "off");
1430 zebra_if_set_protodown(ifp
, down
);
1433 "Cannot set protodown %s for interface %u; does not exist",
1434 down
? "on" : "off", ifindex
);
1442 bool zserv_nexthop_num_warn(const char *caller
, const struct prefix
*p
,
1443 const unsigned int nexthop_num
)
1445 if (nexthop_num
> zrouter
.multipath_num
) {
1446 char buff
[PREFIX2STR_BUFFER
];
1449 prefix2str(p
, buff
, sizeof(buff
));
1452 EC_ZEBRA_MORE_NH_THAN_MULTIPATH
,
1453 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1454 caller
, (p
? buff
: "(NULL)"), nexthop_num
,
1455 zrouter
.multipath_num
);
1463 * Create a new nexthop based on a zapi nexthop.
1465 static struct nexthop
*nexthop_from_zapi(const struct zapi_nexthop
*api_nh
,
1466 uint32_t flags
, struct prefix
*p
,
1467 uint16_t backup_nexthop_num
)
1469 struct nexthop
*nexthop
= NULL
;
1470 struct ipaddr vtep_ip
;
1471 struct interface
*ifp
;
1473 char nhbuf
[INET6_ADDRSTRLEN
] = "";
1475 switch (api_nh
->type
) {
1476 case NEXTHOP_TYPE_IFINDEX
:
1477 nexthop
= nexthop_from_ifindex(api_nh
->ifindex
, api_nh
->vrf_id
);
1479 case NEXTHOP_TYPE_IPV4
:
1480 if (IS_ZEBRA_DEBUG_RECV
) {
1481 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1483 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1484 nhbuf
, api_nh
->vrf_id
);
1486 nexthop
= nexthop_from_ipv4(&api_nh
->gate
.ipv4
, NULL
,
1489 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1490 if (IS_ZEBRA_DEBUG_RECV
) {
1491 inet_ntop(AF_INET
, &api_nh
->gate
.ipv4
, nhbuf
,
1493 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1494 __func__
, nhbuf
, api_nh
->vrf_id
,
1498 nexthop
= nexthop_from_ipv4_ifindex(
1499 &api_nh
->gate
.ipv4
, NULL
, api_nh
->ifindex
,
1502 /* Special handling for IPv4 routes sourced from EVPN:
1503 * the nexthop and associated MAC need to be installed.
1505 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1506 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1507 vtep_ip
.ipa_type
= IPADDR_V4
;
1508 memcpy(&(vtep_ip
.ipaddr_v4
), &(api_nh
->gate
.ipv4
),
1509 sizeof(struct in_addr
));
1510 zebra_vxlan_evpn_vrf_route_add(
1511 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1514 case NEXTHOP_TYPE_IPV6
:
1515 if (IS_ZEBRA_DEBUG_RECV
) {
1516 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1518 zlog_debug("%s: nh=%s, vrf_id=%d", __func__
,
1519 nhbuf
, api_nh
->vrf_id
);
1521 nexthop
= nexthop_from_ipv6(&api_nh
->gate
.ipv6
, api_nh
->vrf_id
);
1523 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1524 if (IS_ZEBRA_DEBUG_RECV
) {
1525 inet_ntop(AF_INET6
, &api_nh
->gate
.ipv6
, nhbuf
,
1527 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1528 __func__
, nhbuf
, api_nh
->vrf_id
,
1531 nexthop
= nexthop_from_ipv6_ifindex(&api_nh
->gate
.ipv6
,
1535 /* Special handling for IPv6 routes sourced from EVPN:
1536 * the nexthop and associated MAC need to be installed.
1538 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
1539 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
1540 vtep_ip
.ipa_type
= IPADDR_V6
;
1541 memcpy(&vtep_ip
.ipaddr_v6
, &(api_nh
->gate
.ipv6
),
1542 sizeof(struct in6_addr
));
1543 zebra_vxlan_evpn_vrf_route_add(
1544 api_nh
->vrf_id
, &api_nh
->rmac
, &vtep_ip
, p
);
1547 case NEXTHOP_TYPE_BLACKHOLE
:
1548 if (IS_ZEBRA_DEBUG_RECV
)
1549 zlog_debug("%s: nh blackhole %d",
1550 __func__
, api_nh
->bh_type
);
1552 nexthop
= nexthop_from_blackhole(api_nh
->bh_type
);
1556 /* Return early if we couldn't process the zapi nexthop */
1557 if (nexthop
== NULL
) {
1561 /* Mark nexthop as onlink either if client has explicitly told us
1562 * to or if the nexthop is on an 'unnumbered' interface.
1564 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
))
1565 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1566 else if (api_nh
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
1567 ifp
= if_lookup_by_index(api_nh
->ifindex
, api_nh
->vrf_id
);
1568 if (ifp
&& connected_is_unnumbered(ifp
))
1569 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
1572 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1573 nexthop
->weight
= api_nh
->weight
;
1575 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
1576 /* Validate count */
1577 if (api_nh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
1578 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1579 zlog_debug("%s: invalid backup nh count %d",
1580 __func__
, api_nh
->backup_num
);
1581 nexthop_free(nexthop
);
1586 /* Copy backup info */
1587 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1588 nexthop
->backup_num
= api_nh
->backup_num
;
1590 for (i
= 0; i
< api_nh
->backup_num
; i
++) {
1591 /* Validate backup index */
1592 if (api_nh
->backup_idx
[i
] < backup_nexthop_num
) {
1593 nexthop
->backup_idx
[i
] = api_nh
->backup_idx
[i
];
1595 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1596 zlog_debug("%s: invalid backup nh idx %d",
1598 api_nh
->backup_idx
[i
]);
1599 nexthop_free(nexthop
);
1610 static bool zapi_read_nexthops(struct zserv
*client
, struct prefix
*p
,
1611 struct zapi_nexthop
*nhops
, uint32_t flags
,
1612 uint32_t message
, uint16_t nexthop_num
,
1613 uint16_t backup_nh_num
,
1614 struct nexthop_group
**png
,
1615 struct nhg_backup_info
**pbnhg
)
1617 struct nexthop_group
*ng
= NULL
;
1618 struct nhg_backup_info
*bnhg
= NULL
;
1620 struct nexthop
*last_nh
= NULL
;
1622 assert(!(png
&& pbnhg
));
1625 ng
= nexthop_group_new();
1627 if (pbnhg
&& backup_nh_num
> 0) {
1628 if (IS_ZEBRA_DEBUG_RECV
)
1629 zlog_debug("%s: adding %d backup nexthops", __func__
,
1632 bnhg
= zebra_nhg_backup_alloc();
1636 * TBD should _all_ of the nexthop add operations use
1637 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1638 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1640 for (i
= 0; i
< nexthop_num
; i
++) {
1641 struct nexthop
*nexthop
;
1642 enum lsp_types_t label_type
;
1643 char nhbuf
[NEXTHOP_STRLEN
];
1644 char labelbuf
[MPLS_LABEL_STRLEN
];
1645 struct zapi_nexthop
*api_nh
= &nhops
[i
];
1647 /* Convert zapi nexthop */
1648 nexthop
= nexthop_from_zapi(api_nh
, flags
, p
, backup_nh_num
);
1651 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1652 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1653 __func__
, nexthop_num
, i
);
1655 nexthop_group_delete(&ng
);
1657 zebra_nhg_backup_free(&bnhg
);
1662 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1663 if (IS_ZEBRA_DEBUG_RECV
) {
1664 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1665 zlog_debug("%s: backup nh %s with BACKUP flag!",
1668 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
1669 nexthop
->backup_num
= 0;
1672 if (CHECK_FLAG(message
, ZAPI_MESSAGE_SRTE
)) {
1673 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
);
1674 nexthop
->srte_color
= api_nh
->srte_color
;
1677 /* MPLS labels for BGP-LU or Segment Routing */
1678 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)
1679 && api_nh
->type
!= NEXTHOP_TYPE_IFINDEX
1680 && api_nh
->type
!= NEXTHOP_TYPE_BLACKHOLE
1681 && api_nh
->label_num
> 0) {
1683 label_type
= lsp_type_from_re_type(client
->proto
);
1684 nexthop_add_labels(nexthop
, label_type
,
1686 &api_nh
->labels
[0]);
1689 if (IS_ZEBRA_DEBUG_RECV
) {
1693 nexthop2str(nexthop
, nhbuf
, sizeof(nhbuf
));
1695 if (nexthop
->nh_label
&&
1696 nexthop
->nh_label
->num_labels
> 0) {
1697 mpls_label2str(nexthop
->nh_label
->num_labels
,
1698 nexthop
->nh_label
->label
,
1699 labelbuf
, sizeof(labelbuf
),
1703 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1704 __func__
, nhbuf
, api_nh
->vrf_id
, labelbuf
);
1708 /* Add new nexthop to temporary list. This list is
1709 * canonicalized - sorted - so that it can be hashed
1710 * later in route processing. We expect that the sender
1711 * has sent the list sorted, and the zapi client api
1712 * attempts to enforce that, so this should be
1713 * inexpensive - but it is necessary to support shared
1716 nexthop_group_add_sorted(ng
, nexthop
);
1719 /* Note that the order of the backup nexthops is
1720 * significant, so we don't sort this list as we do the
1721 * primary nexthops, we just append.
1724 NEXTHOP_APPEND(last_nh
, nexthop
);
1726 bnhg
->nhe
->nhg
.nexthop
= nexthop
;
1733 /* succesfully read, set caller pointers now */
1743 int zapi_nhg_decode(struct stream
*s
, int cmd
, struct zapi_nhg
*api_nhg
)
1746 struct zapi_nexthop
*znh
;
1748 STREAM_GETW(s
, api_nhg
->proto
);
1749 STREAM_GETL(s
, api_nhg
->id
);
1751 if (cmd
== ZEBRA_NHG_DEL
)
1755 STREAM_GETW(s
, api_nhg
->nexthop_num
);
1757 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->nexthop_num
))
1760 if (api_nhg
->nexthop_num
<= 0) {
1761 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1762 "%s: No nexthops sent", __func__
);
1766 for (i
= 0; i
< api_nhg
->nexthop_num
; i
++) {
1767 znh
= &((api_nhg
->nexthops
)[i
]);
1769 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1770 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1771 "%s: Nexthop creation failed", __func__
);
1776 /* Backup Nexthops */
1777 STREAM_GETW(s
, api_nhg
->backup_nexthop_num
);
1779 if (zserv_nexthop_num_warn(__func__
, NULL
, api_nhg
->backup_nexthop_num
))
1782 for (i
= 0; i
< api_nhg
->backup_nexthop_num
; i
++) {
1783 znh
= &((api_nhg
->backup_nexthops
)[i
]);
1785 if (zapi_nexthop_decode(s
, znh
, 0, 0) != 0) {
1786 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1787 "%s: Backup Nexthop creation failed",
1798 EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1799 "%s: Nexthop Group decode failed with some sort of stream read failure",
1804 static void zread_nhg_del(ZAPI_HANDLER_ARGS
)
1807 struct zapi_nhg api_nhg
= {};
1808 struct nhg_hash_entry
*nhe
;
1811 if (zapi_nhg_decode(s
, hdr
->command
, &api_nhg
) < 0) {
1812 if (IS_ZEBRA_DEBUG_RECV
)
1813 zlog_debug("%s: Unable to decode zapi_nhg sent",
1819 * Delete the received nhg id
1822 nhe
= zebra_nhg_proto_del(api_nhg
.id
, api_nhg
.proto
);
1825 zebra_nhg_decrement_ref(nhe
);
1826 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1829 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1830 ZAPI_NHG_REMOVE_FAIL
);
1833 static void zread_nhg_add(ZAPI_HANDLER_ARGS
)
1836 struct zapi_nhg api_nhg
= {};
1837 struct nexthop_group
*nhg
= NULL
;
1838 struct nhg_backup_info
*bnhg
= NULL
;
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",
1849 if ((!zapi_read_nexthops(client
, NULL
, api_nhg
.nexthops
, 0, 0,
1850 api_nhg
.nexthop_num
,
1851 api_nhg
.backup_nexthop_num
, &nhg
, NULL
))
1852 || (!zapi_read_nexthops(client
, NULL
, api_nhg
.backup_nexthops
, 0, 0,
1853 api_nhg
.backup_nexthop_num
,
1854 api_nhg
.backup_nexthop_num
, NULL
, &bnhg
))) {
1856 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED
,
1857 "%s: Nexthop Group Creation failed", __func__
);
1864 nhe
= zebra_nhg_proto_add(api_nhg
.id
, api_nhg
.proto
, nhg
, 0);
1866 nexthop_group_delete(&nhg
);
1867 zebra_nhg_backup_free(&bnhg
);
1871 * Assume fully resolved for now and install.
1873 * Resolution is going to need some more work.
1876 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1877 ZAPI_NHG_INSTALLED
);
1879 nhg_notify(api_nhg
.proto
, client
->instance
, api_nhg
.id
,
1880 ZAPI_NHG_FAIL_INSTALL
);
1883 static void zread_route_add(ZAPI_HANDLER_ARGS
)
1886 struct zapi_route api
;
1888 struct prefix_ipv6
*src_p
= NULL
;
1889 struct route_entry
*re
;
1890 struct nexthop_group
*ng
= NULL
;
1891 struct nhg_backup_info
*bnhg
= NULL
;
1894 struct nhg_hash_entry nhe
;
1897 if (zapi_route_decode(s
, &api
) < 0) {
1898 if (IS_ZEBRA_DEBUG_RECV
)
1899 zlog_debug("%s: Unable to decode zapi_route sent",
1904 vrf_id
= zvrf_id(zvrf
);
1906 if (IS_ZEBRA_DEBUG_RECV
) {
1907 char buf_prefix
[PREFIX_STRLEN
];
1909 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
1910 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
1911 __func__
, vrf_id
, api
.tableid
, buf_prefix
,
1912 (int)api
.message
, api
.flags
);
1915 /* Allocate new route. */
1916 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
1917 re
->type
= api
.type
;
1918 re
->instance
= api
.instance
;
1919 re
->flags
= api
.flags
;
1920 re
->uptime
= monotime(NULL
);
1921 re
->vrf_id
= vrf_id
;
1924 re
->table
= api
.tableid
;
1926 re
->table
= zvrf
->table_id
;
1928 if (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
)
1929 && (!CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)
1930 || api
.nexthop_num
== 0)) {
1932 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS
,
1933 "%s: received a route without nexthops for prefix %pFX from client %s",
1934 __func__
, &api
.prefix
,
1935 zebra_route_string(client
->proto
));
1937 XFREE(MTYPE_RE
, re
);
1941 /* Report misuse of the backup flag */
1942 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
)
1943 && api
.backup_nexthop_num
== 0) {
1944 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_EVENT
)
1946 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1947 __func__
, zebra_route_string(client
->proto
),
1951 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NHG
))
1952 re
->nhe_id
= api
.nhgid
;
1955 && (!zapi_read_nexthops(client
, &api
.prefix
, api
.nexthops
,
1956 api
.flags
, api
.message
, api
.nexthop_num
,
1957 api
.backup_nexthop_num
, &ng
, NULL
)
1958 || !zapi_read_nexthops(client
, &api
.prefix
, api
.backup_nexthops
,
1959 api
.flags
, api
.message
,
1960 api
.backup_nexthop_num
,
1961 api
.backup_nexthop_num
, NULL
, &bnhg
))) {
1963 nexthop_group_delete(&ng
);
1964 zebra_nhg_backup_free(&bnhg
);
1965 XFREE(MTYPE_RE
, re
);
1969 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
))
1970 re
->distance
= api
.distance
;
1971 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
))
1972 re
->metric
= api
.metric
;
1973 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_TAG
))
1975 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_MTU
))
1978 afi
= family2afi(api
.prefix
.family
);
1979 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
1980 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
1981 "%s: Received SRC Prefix but afi is not v6",
1983 nexthop_group_delete(&ng
);
1984 zebra_nhg_backup_free(&bnhg
);
1985 XFREE(MTYPE_RE
, re
);
1988 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
1989 src_p
= &api
.src_prefix
;
1991 if (api
.safi
!= SAFI_UNICAST
&& api
.safi
!= SAFI_MULTICAST
) {
1992 flog_warn(EC_LIB_ZAPI_MISSMATCH
,
1993 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1994 __func__
, api
.safi
);
1995 nexthop_group_delete(&ng
);
1996 zebra_nhg_backup_free(&bnhg
);
1997 XFREE(MTYPE_RE
, re
);
2002 * If we have an ID, this proto owns the NHG it sent along with the
2003 * route, so we just send the ID into rib code with it.
2005 * Havent figured out how to handle backup NHs with this yet, so lets
2006 * keep that separate.
2007 * Include backup info with the route. We use a temporary nhe here;
2008 * if this is a new/unknown nhe, a new copy will be allocated
2012 zebra_nhe_init(&nhe
, afi
, ng
->nexthop
);
2013 nhe
.nhg
.nexthop
= ng
->nexthop
;
2014 nhe
.backup_info
= bnhg
;
2016 ret
= rib_add_multipath_nhe(afi
, api
.safi
, &api
.prefix
, src_p
,
2019 /* At this point, these allocations are not needed: 're' has been
2020 * retained or freed, and if 're' still exists, it is using
2021 * a reference to a shared group object.
2023 nexthop_group_delete(&ng
);
2025 zebra_nhg_backup_free(&bnhg
);
2028 switch (api
.prefix
.family
) {
2031 client
->v4_route_add_cnt
++;
2033 client
->v4_route_upd8_cnt
++;
2037 client
->v6_route_add_cnt
++;
2039 client
->v6_route_upd8_cnt
++;
2044 static void zread_route_del(ZAPI_HANDLER_ARGS
)
2047 struct zapi_route api
;
2049 struct prefix_ipv6
*src_p
= NULL
;
2053 if (zapi_route_decode(s
, &api
) < 0)
2056 afi
= family2afi(api
.prefix
.family
);
2057 if (afi
!= AFI_IP6
&& CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
)) {
2058 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI
,
2059 "%s: Received a src prefix while afi is not v6",
2063 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
2064 src_p
= &api
.src_prefix
;
2067 table_id
= api
.tableid
;
2069 table_id
= zvrf
->table_id
;
2071 if (IS_ZEBRA_DEBUG_RECV
) {
2072 char buf_prefix
[PREFIX_STRLEN
];
2074 prefix2str(&api
.prefix
, buf_prefix
, sizeof(buf_prefix
));
2075 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
2076 __func__
, zvrf_id(zvrf
), table_id
, buf_prefix
,
2077 (int)api
.message
, api
.flags
);
2080 rib_delete(afi
, api
.safi
, zvrf_id(zvrf
), api
.type
, api
.instance
,
2081 api
.flags
, &api
.prefix
, src_p
, NULL
, 0, table_id
, api
.metric
,
2082 api
.distance
, false, false);
2085 switch (api
.prefix
.family
) {
2087 client
->v4_route_del_cnt
++;
2090 client
->v6_route_del_cnt
++;
2095 /* MRIB Nexthop lookup for IPv4. */
2096 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS
)
2098 struct in_addr addr
;
2099 struct route_entry
*re
;
2101 STREAM_GET(&addr
.s_addr
, msg
, IPV4_MAX_BYTELEN
);
2102 re
= rib_match_ipv4_multicast(zvrf_id(zvrf
), addr
, NULL
);
2103 zsend_ipv4_nexthop_lookup_mrib(client
, addr
, re
, zvrf
);
2109 /* Register zebra server router-id information. Send current router-id */
2110 static void zread_router_id_add(ZAPI_HANDLER_ARGS
)
2116 STREAM_GETW(msg
, afi
);
2118 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2120 "Invalid AFI %u while registering for router ID notifications",
2122 goto stream_failure
;
2125 /* Router-id information is needed. */
2126 vrf_bitmap_set(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2128 router_id_get(afi
, &p
, zvrf
);
2130 zsend_router_id_update(client
, afi
, &p
, zvrf_id(zvrf
));
2136 /* Unregister zebra server router-id information. */
2137 static void zread_router_id_delete(ZAPI_HANDLER_ARGS
)
2141 STREAM_GETW(msg
, afi
);
2143 if (afi
<= AFI_UNSPEC
|| afi
>= AFI_MAX
) {
2145 "Invalid AFI %u while unregistering from router ID notifications",
2147 goto stream_failure
;
2150 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2156 static void zsend_capabilities(struct zserv
*client
, struct zebra_vrf
*zvrf
)
2158 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2160 zclient_create_header(s
, ZEBRA_CAPABILITIES
, zvrf
->vrf
->vrf_id
);
2161 stream_putl(s
, vrf_get_backend());
2162 stream_putc(s
, mpls_enabled
);
2163 stream_putl(s
, zrouter
.multipath_num
);
2164 stream_putc(s
, zebra_mlag_get_role());
2166 stream_putw_at(s
, 0, stream_get_endp(s
));
2167 zserv_send_message(client
, s
);
2170 void zsend_capabilities_all_clients(void)
2172 struct listnode
*node
, *nnode
;
2173 struct zebra_vrf
*zvrf
;
2174 struct zserv
*client
;
2176 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2177 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
2178 /* Do not send unsolicited messages to synchronous clients. */
2179 if (client
->synchronous
)
2182 zsend_capabilities(client
, zvrf
);
2186 /* Tie up route-type and client->sock */
2187 static void zread_hello(ZAPI_HANDLER_ARGS
)
2189 /* type of protocol (lib/zebra.h) */
2191 unsigned short instance
;
2193 uint8_t synchronous
;
2194 uint32_t session_id
;
2196 STREAM_GETC(msg
, proto
);
2197 STREAM_GETW(msg
, instance
);
2198 STREAM_GETL(msg
, session_id
);
2199 STREAM_GETC(msg
, notify
);
2200 STREAM_GETC(msg
, synchronous
);
2202 client
->notify_owner
= true;
2205 client
->synchronous
= true;
2207 /* accept only dynamic routing protocols */
2208 if ((proto
< ZEBRA_ROUTE_MAX
) && (proto
> ZEBRA_ROUTE_CONNECT
)) {
2210 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2211 client
->sock
, zebra_route_string(proto
),
2214 zlog_notice("client protocol instance %d", instance
);
2216 client
->proto
= proto
;
2217 client
->instance
= instance
;
2218 client
->session_id
= session_id
;
2220 /* Graceful restart processing for client connect */
2221 zebra_gr_client_reconnect(client
);
2224 if (!client
->synchronous
) {
2225 zsend_capabilities(client
, zvrf
);
2226 zebra_vrf_update_all(client
);
2232 /* Unregister all information in a VRF. */
2233 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS
)
2238 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2239 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
2240 vrf_bitmap_unset(client
->redist
[afi
][i
], zvrf_id(zvrf
));
2241 vrf_bitmap_unset(client
->redist_default
[afi
], zvrf_id(zvrf
));
2242 vrf_bitmap_unset(client
->ridinfo
[afi
], zvrf_id(zvrf
));
2247 * Validate incoming zapi mpls lsp / labels message
2249 static int zapi_labels_validate(const struct zapi_labels
*zl
)
2255 const struct zapi_nexthop
*znh
;
2257 /* Validate backup info: no duplicates for a single primary */
2258 if (zl
->backup_nexthop_num
== 0) {
2263 for (j
= 0; j
< zl
->nexthop_num
; j
++) {
2264 znh
= &zl
->nexthops
[j
];
2266 memset(bits
, 0, sizeof(bits
));
2268 for (i
= 0; i
< znh
->backup_num
; i
++) {
2269 idx
= znh
->backup_idx
[i
] / 32;
2271 ival
= 1 << znh
->backup_idx
[i
] % 32;
2273 /* Check whether value is already used */
2274 if (ival
& bits
[idx
]) {
2277 if (IS_ZEBRA_DEBUG_RECV
)
2278 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2280 znh
->backup_idx
[i
]);
2284 /* Mark index value */
2297 * Handle request to create an MPLS LSP.
2299 * A single message can fully specify an LSP with multiple nexthops.
2301 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2302 * updated to use the received label(s).
2304 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS
)
2307 struct zapi_labels zl
;
2310 /* Get input stream. */
2312 if (zapi_labels_decode(s
, &zl
) < 0) {
2313 if (IS_ZEBRA_DEBUG_RECV
)
2314 zlog_debug("%s: Unable to decode zapi_labels sent",
2322 /* Validate; will debug on failure */
2323 if (zapi_labels_validate(&zl
) < 0)
2326 ret
= mpls_zapi_labels_process(true, zvrf
, &zl
);
2328 if (IS_ZEBRA_DEBUG_RECV
)
2329 zlog_debug("%s: Error processing zapi request",
2335 * Handle request to delete an MPLS LSP.
2337 * An LSP is identified by its type and local label. When the received message
2338 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2339 * listed LSP nexthops (aka NHLFEs) are deleted.
2341 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2342 * FEC (route) nexthops are deleted.
2344 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS
)
2347 struct zapi_labels zl
;
2350 /* Get input stream. */
2352 if (zapi_labels_decode(s
, &zl
) < 0) {
2353 if (IS_ZEBRA_DEBUG_RECV
)
2354 zlog_debug("%s: Unable to decode zapi_labels sent",
2362 if (zl
.nexthop_num
> 0) {
2363 ret
= mpls_zapi_labels_process(false /*delete*/, zvrf
, &zl
);
2365 if (IS_ZEBRA_DEBUG_RECV
)
2366 zlog_debug("%s: Error processing zapi request",
2370 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2372 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2373 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2374 zl
.route
.type
, zl
.route
.instance
);
2379 * Handle request to add an MPLS LSP or change an existing one.
2381 * A single message can fully specify an LSP with multiple nexthops.
2383 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2384 * updated to use the received label(s).
2386 * NOTE: zebra will use route replace semantics (make-before-break) to update
2387 * the LSP in the forwarding plane if that's supported by the underlying
2390 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS
)
2393 struct zapi_labels zl
;
2395 /* Get input stream. */
2397 if (zapi_labels_decode(s
, &zl
) < 0) {
2398 if (IS_ZEBRA_DEBUG_RECV
)
2399 zlog_debug("%s: Unable to decode zapi_labels sent",
2407 /* Validate; will debug on failure */
2408 if (zapi_labels_validate(&zl
) < 0)
2411 /* This removes everything, then re-adds from the client's
2412 * zapi message. Since the LSP will be processed later, on this
2413 * this same pthread, all of the changes will 'appear' at once.
2415 mpls_lsp_uninstall_all_vrf(zvrf
, zl
.type
, zl
.local_label
);
2416 if (CHECK_FLAG(zl
.message
, ZAPI_LABELS_FTN
))
2417 mpls_ftn_uninstall(zvrf
, zl
.type
, &zl
.route
.prefix
,
2418 zl
.route
.type
, zl
.route
.instance
);
2420 mpls_zapi_labels_process(true, zvrf
, &zl
);
2423 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS
)
2426 struct zapi_sr_policy zp
;
2427 struct zapi_srte_tunnel
*zt
;
2428 struct zebra_sr_policy
*policy
;
2430 /* Get input stream. */
2432 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2433 if (IS_ZEBRA_DEBUG_RECV
)
2434 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2435 __PRETTY_FUNCTION__
);
2438 zt
= &zp
.segment_list
;
2439 if (zt
->label_num
< 1) {
2440 if (IS_ZEBRA_DEBUG_RECV
)
2442 "%s: SR-TE tunnel must contain at least one label",
2443 __PRETTY_FUNCTION__
);
2450 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2452 policy
= zebra_sr_policy_add(zp
.color
, &zp
.endpoint
, zp
.name
);
2453 /* TODO: per-VRF list of SR-TE policies. */
2454 policy
->zvrf
= zvrf
;
2456 zebra_sr_policy_validate(policy
, &zp
.segment_list
);
2459 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS
)
2462 struct zapi_sr_policy zp
;
2463 struct zebra_sr_policy
*policy
;
2465 /* Get input stream. */
2467 if (zapi_sr_policy_decode(s
, &zp
) < 0) {
2468 if (IS_ZEBRA_DEBUG_RECV
)
2469 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2470 __PRETTY_FUNCTION__
);
2477 policy
= zebra_sr_policy_find(zp
.color
, &zp
.endpoint
);
2479 if (IS_ZEBRA_DEBUG_RECV
)
2480 zlog_debug("%s: Unable to find SR-TE policy",
2481 __PRETTY_FUNCTION__
);
2485 zebra_sr_policy_del(policy
);
2488 int zsend_sr_policy_notify_status(uint32_t color
, struct ipaddr
*endpoint
,
2489 char *name
, int status
)
2491 struct zserv
*client
;
2494 client
= zserv_find_client(ZEBRA_ROUTE_SRTE
, 0);
2496 if (IS_ZEBRA_DEBUG_PACKET
)
2498 "Not notifying pathd about policy %s"
2499 " status change to %d",
2504 if (IS_ZEBRA_DEBUG_PACKET
)
2506 "Notifying pathd about policy %s status change"
2510 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
2513 zclient_create_header(s
, ZEBRA_SR_POLICY_NOTIFY_STATUS
, VRF_DEFAULT
);
2514 stream_putl(s
, color
);
2515 stream_put_ipaddr(s
, endpoint
);
2516 stream_write(s
, name
, SRTE_POLICY_NAME_MAX_LENGTH
);
2517 stream_putl(s
, status
);
2519 stream_putw_at(s
, 0, stream_get_endp(s
));
2521 return zserv_send_message(client
, s
);
2524 /* Send response to a table manager connect request to client */
2525 static void zread_table_manager_connect(struct zserv
*client
,
2526 struct stream
*msg
, vrf_id_t vrf_id
)
2531 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2536 STREAM_GETC(s
, proto
);
2537 STREAM_GETW(s
, instance
);
2539 /* accept only dynamic routing protocols */
2540 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2541 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2542 "client %d has wrong protocol %s", client
->sock
,
2543 zebra_route_string(proto
));
2544 zsend_table_manager_connect_response(client
, vrf_id
, 1);
2547 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2548 client
->sock
, VRF_LOGNAME(vrf
), vrf_id
, instance
,
2549 zebra_route_string(proto
));
2550 client
->proto
= proto
;
2551 client
->instance
= instance
;
2554 * Release previous labels of same protocol and instance.
2555 * This is done in case it restarted from an unexpected shutdown.
2557 release_daemon_table_chunks(client
);
2559 zsend_table_manager_connect_response(client
, vrf_id
, 0);
2565 static void zread_label_manager_connect(struct zserv
*client
,
2566 struct stream
*msg
, vrf_id_t vrf_id
)
2569 /* type of protocol (lib/zebra.h) */
2571 unsigned short instance
;
2573 /* Get input stream. */
2577 STREAM_GETC(s
, proto
);
2578 STREAM_GETW(s
, instance
);
2580 /* accept only dynamic routing protocols */
2581 if ((proto
>= ZEBRA_ROUTE_MAX
) || (proto
<= ZEBRA_ROUTE_STATIC
)) {
2582 flog_err(EC_ZEBRA_TM_WRONG_PROTO
,
2583 "client %d has wrong protocol %s", client
->sock
,
2584 zebra_route_string(proto
));
2585 zsend_label_manager_connect_response(client
, vrf_id
, 1);
2589 /* recall proto and instance in this socket */
2590 client
->proto
= proto
;
2591 client
->instance
= instance
;
2593 /* call hook for connection using wrapper */
2594 lm_client_connect_call(client
, vrf_id
);
2600 static void zread_get_label_chunk(struct zserv
*client
, struct stream
*msg
,
2605 uint32_t size
, base
;
2606 struct label_manager_chunk
*lmc
= NULL
;
2608 unsigned short instance
;
2610 /* Get input stream. */
2614 STREAM_GETC(s
, proto
);
2615 STREAM_GETW(s
, instance
);
2616 STREAM_GETC(s
, keep
);
2617 STREAM_GETL(s
, size
);
2618 STREAM_GETL(s
, base
);
2620 assert(proto
== client
->proto
&& instance
== client
->instance
);
2622 /* call hook to get a chunk using wrapper */
2623 lm_get_chunk_call(&lmc
, client
, keep
, size
, base
, vrf_id
);
2629 static void zread_release_label_chunk(struct zserv
*client
, struct stream
*msg
)
2632 uint32_t start
, end
;
2634 unsigned short instance
;
2636 /* Get input stream. */
2640 STREAM_GETC(s
, proto
);
2641 STREAM_GETW(s
, instance
);
2642 STREAM_GETL(s
, start
);
2643 STREAM_GETL(s
, end
);
2645 assert(proto
== client
->proto
&& instance
== client
->instance
);
2647 /* call hook to release a chunk using wrapper */
2648 lm_release_chunk_call(client
, start
, end
);
2654 static void zread_label_manager_request(ZAPI_HANDLER_ARGS
)
2656 if (hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT
2657 || hdr
->command
== ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
)
2658 zread_label_manager_connect(client
, msg
, zvrf_id(zvrf
));
2660 if (hdr
->command
== ZEBRA_GET_LABEL_CHUNK
)
2661 zread_get_label_chunk(client
, msg
, zvrf_id(zvrf
));
2662 else if (hdr
->command
== ZEBRA_RELEASE_LABEL_CHUNK
)
2663 zread_release_label_chunk(client
, msg
);
2667 static void zread_get_table_chunk(struct zserv
*client
, struct stream
*msg
,
2672 struct table_manager_chunk
*tmc
;
2674 /* Get input stream. */
2678 STREAM_GETL(s
, size
);
2680 tmc
= assign_table_chunk(client
->proto
, client
->instance
, size
);
2682 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK
,
2683 "%s: Unable to assign Table Chunk of size %u",
2686 zlog_debug("Assigned Table Chunk %u - %u", tmc
->start
,
2688 /* send response back */
2689 zsend_assign_table_chunk_response(client
, vrf_id
, tmc
);
2695 static void zread_release_table_chunk(struct zserv
*client
, struct stream
*msg
)
2698 uint32_t start
, end
;
2700 /* Get input stream. */
2704 STREAM_GETL(s
, start
);
2705 STREAM_GETL(s
, end
);
2707 release_table_chunk(client
->proto
, client
->instance
, start
, end
);
2713 static void zread_table_manager_request(ZAPI_HANDLER_ARGS
)
2715 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2716 if (hdr
->command
== ZEBRA_TABLE_MANAGER_CONNECT
)
2717 zread_table_manager_connect(client
, msg
, zvrf_id(zvrf
));
2719 /* Sanity: don't allow 'unidentified' requests */
2720 if (!client
->proto
) {
2723 "Got table request from an unidentified client");
2726 if (hdr
->command
== ZEBRA_GET_TABLE_CHUNK
)
2727 zread_get_table_chunk(client
, msg
, zvrf_id(zvrf
));
2728 else if (hdr
->command
== ZEBRA_RELEASE_TABLE_CHUNK
)
2729 zread_release_table_chunk(client
, msg
);
2733 static void zread_pseudowire(ZAPI_HANDLER_ARGS
)
2736 char ifname
[IF_NAMESIZE
];
2740 union g_addr nexthop
;
2741 uint32_t local_label
;
2742 uint32_t remote_label
;
2744 union pw_protocol_fields data
;
2746 struct zebra_pw
*pw
;
2748 /* Get input stream. */
2752 STREAM_GET(ifname
, s
, IF_NAMESIZE
);
2753 ifname
[IF_NAMESIZE
- 1] = '\0';
2754 STREAM_GETL(s
, ifindex
);
2755 STREAM_GETL(s
, type
);
2759 STREAM_GET(&nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2762 STREAM_GET(&nexthop
.ipv6
, s
, 16);
2767 STREAM_GETL(s
, local_label
);
2768 STREAM_GETL(s
, remote_label
);
2769 STREAM_GETC(s
, flags
);
2770 STREAM_GET(&data
, s
, sizeof(data
));
2771 protocol
= client
->proto
;
2773 pw
= zebra_pw_find(zvrf
, ifname
);
2774 switch (hdr
->command
) {
2777 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS
,
2778 "%s: pseudowire %s already exists [%s]",
2780 zserv_command_string(hdr
->command
));
2784 zebra_pw_add(zvrf
, ifname
, protocol
, client
);
2786 case ZEBRA_PW_DELETE
:
2788 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2789 "%s: pseudowire %s not found [%s]", __func__
,
2790 ifname
, zserv_command_string(hdr
->command
));
2794 zebra_pw_del(zvrf
, pw
);
2797 case ZEBRA_PW_UNSET
:
2799 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT
,
2800 "%s: pseudowire %s not found [%s]", __func__
,
2801 ifname
, zserv_command_string(hdr
->command
));
2805 switch (hdr
->command
) {
2809 case ZEBRA_PW_UNSET
:
2814 zebra_pw_change(pw
, ifindex
, type
, af
, &nexthop
, local_label
,
2815 remote_label
, flags
, &data
);
2823 static void zread_interface_set_master(ZAPI_HANDLER_ARGS
)
2825 struct interface
*master
;
2826 struct interface
*slave
;
2827 struct stream
*s
= msg
;
2831 STREAM_GETL(s
, vrf_id
);
2832 STREAM_GETL(s
, ifindex
);
2833 master
= if_lookup_by_index(ifindex
, vrf_id
);
2835 STREAM_GETL(s
, vrf_id
);
2836 STREAM_GETL(s
, ifindex
);
2837 slave
= if_lookup_by_index(ifindex
, vrf_id
);
2839 if (!master
|| !slave
)
2842 kernel_interface_set_master(master
, slave
);
2849 static void zread_vrf_label(ZAPI_HANDLER_ARGS
)
2851 struct interface
*ifp
;
2852 mpls_label_t nlabel
;
2855 struct zebra_vrf
*def_zvrf
;
2856 enum lsp_types_t ltype
;
2859 STREAM_GETL(s
, nlabel
);
2860 STREAM_GETC(s
, afi
);
2862 if (!(IS_VALID_AFI(afi
))) {
2863 zlog_warn("Invalid AFI for VRF label: %u", afi
);
2867 if (nlabel
== zvrf
->label
[afi
]) {
2869 * Nothing to do here move along
2874 STREAM_GETC(s
, ltype
);
2876 if (zvrf
->vrf
->vrf_id
!= VRF_DEFAULT
)
2877 ifp
= if_lookup_by_name(zvrf
->vrf
->name
, zvrf
->vrf
->vrf_id
);
2879 ifp
= if_lookup_by_name("lo", VRF_DEFAULT
);
2882 zlog_debug("Unable to find specified Interface for %s",
2887 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2889 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
) {
2893 really_remove
= true;
2894 for (scrubber
= AFI_IP
; scrubber
< AFI_MAX
; scrubber
++) {
2895 if (scrubber
== afi
)
2898 if (zvrf
->label
[scrubber
] == MPLS_LABEL_NONE
)
2901 if (zvrf
->label
[afi
] == zvrf
->label
[scrubber
]) {
2902 really_remove
= false;
2908 mpls_lsp_uninstall(def_zvrf
, ltype
, zvrf
->label
[afi
],
2909 NEXTHOP_TYPE_IFINDEX
, NULL
,
2910 ifp
->ifindex
, false /*backup*/);
2913 if (nlabel
!= MPLS_LABEL_NONE
) {
2914 mpls_label_t out_label
= MPLS_LABEL_IMPLICIT_NULL
;
2915 mpls_lsp_install(def_zvrf
, ltype
, nlabel
, 1, &out_label
,
2916 NEXTHOP_TYPE_IFINDEX
, NULL
, ifp
->ifindex
);
2919 zvrf
->label
[afi
] = nlabel
;
2924 static inline void zread_rule(ZAPI_HANDLER_ARGS
)
2926 struct zebra_pbr_rule zpr
;
2929 char ifname
[INTERFACE_NAMSIZ
+ 1] = {};
2932 STREAM_GETL(s
, total
);
2934 for (i
= 0; i
< total
; i
++) {
2935 memset(&zpr
, 0, sizeof(zpr
));
2937 zpr
.sock
= client
->sock
;
2938 zpr
.rule
.vrf_id
= hdr
->vrf_id
;
2939 STREAM_GETL(s
, zpr
.rule
.seq
);
2940 STREAM_GETL(s
, zpr
.rule
.priority
);
2941 STREAM_GETL(s
, zpr
.rule
.unique
);
2942 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.family
);
2943 STREAM_GETC(s
, zpr
.rule
.filter
.src_ip
.prefixlen
);
2944 STREAM_GET(&zpr
.rule
.filter
.src_ip
.u
.prefix
, s
,
2945 prefix_blen(&zpr
.rule
.filter
.src_ip
));
2946 STREAM_GETW(s
, zpr
.rule
.filter
.src_port
);
2947 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.family
);
2948 STREAM_GETC(s
, zpr
.rule
.filter
.dst_ip
.prefixlen
);
2949 STREAM_GET(&zpr
.rule
.filter
.dst_ip
.u
.prefix
, s
,
2950 prefix_blen(&zpr
.rule
.filter
.dst_ip
));
2951 STREAM_GETW(s
, zpr
.rule
.filter
.dst_port
);
2952 STREAM_GETC(s
, zpr
.rule
.filter
.dsfield
);
2953 STREAM_GETL(s
, zpr
.rule
.filter
.fwmark
);
2954 STREAM_GETL(s
, zpr
.rule
.action
.table
);
2955 STREAM_GET(ifname
, s
, INTERFACE_NAMSIZ
);
2957 strlcpy(zpr
.ifname
, ifname
, sizeof(zpr
.ifname
));
2958 strlcpy(zpr
.rule
.ifname
, ifname
, sizeof(zpr
.rule
.ifname
));
2960 if (!is_default_prefix(&zpr
.rule
.filter
.src_ip
))
2961 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_IP
;
2963 if (!is_default_prefix(&zpr
.rule
.filter
.dst_ip
))
2964 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_IP
;
2966 if (zpr
.rule
.filter
.src_port
)
2967 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_SRC_PORT
;
2969 if (zpr
.rule
.filter
.dst_port
)
2970 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DST_PORT
;
2972 if (zpr
.rule
.filter
.dsfield
)
2973 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_DSFIELD
;
2975 if (zpr
.rule
.filter
.fwmark
)
2976 zpr
.rule
.filter
.filter_bm
|= PBR_FILTER_FWMARK
;
2978 if (!(zpr
.rule
.filter
.src_ip
.family
== AF_INET
2979 || zpr
.rule
.filter
.src_ip
.family
== AF_INET6
)) {
2981 "Unsupported PBR source IP family: %s (%hhu)",
2982 family2str(zpr
.rule
.filter
.src_ip
.family
),
2983 zpr
.rule
.filter
.src_ip
.family
);
2986 if (!(zpr
.rule
.filter
.dst_ip
.family
== AF_INET
2987 || zpr
.rule
.filter
.dst_ip
.family
== AF_INET6
)) {
2989 "Unsupported PBR destination IP family: %s (%hhu)",
2990 family2str(zpr
.rule
.filter
.dst_ip
.family
),
2991 zpr
.rule
.filter
.dst_ip
.family
);
2996 zpr
.vrf_id
= zvrf
->vrf
->vrf_id
;
2997 if (hdr
->command
== ZEBRA_RULE_ADD
)
2998 zebra_pbr_add_rule(&zpr
);
3000 zebra_pbr_del_rule(&zpr
);
3007 static inline void zread_ipset(ZAPI_HANDLER_ARGS
)
3009 struct zebra_pbr_ipset zpi
;
3014 STREAM_GETL(s
, total
);
3016 for (i
= 0; i
< total
; i
++) {
3017 memset(&zpi
, 0, sizeof(zpi
));
3019 zpi
.sock
= client
->sock
;
3020 zpi
.vrf_id
= zvrf
->vrf
->vrf_id
;
3021 STREAM_GETL(s
, zpi
.unique
);
3022 STREAM_GETL(s
, zpi
.type
);
3023 STREAM_GETC(s
, zpi
.family
);
3024 STREAM_GET(&zpi
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3026 if (hdr
->command
== ZEBRA_IPSET_CREATE
)
3027 zebra_pbr_create_ipset(&zpi
);
3029 zebra_pbr_destroy_ipset(&zpi
);
3036 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS
)
3038 struct zebra_pbr_ipset_entry zpi
;
3039 struct zebra_pbr_ipset ipset
;
3044 STREAM_GETL(s
, total
);
3046 for (i
= 0; i
< total
; i
++) {
3047 memset(&zpi
, 0, sizeof(zpi
));
3048 memset(&ipset
, 0, sizeof(ipset
));
3050 zpi
.sock
= client
->sock
;
3051 STREAM_GETL(s
, zpi
.unique
);
3052 STREAM_GET(&ipset
.ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3053 ipset
.ipset_name
[ZEBRA_IPSET_NAME_SIZE
- 1] = '\0';
3054 STREAM_GETC(s
, zpi
.src
.family
);
3055 STREAM_GETC(s
, zpi
.src
.prefixlen
);
3056 STREAM_GET(&zpi
.src
.u
.prefix
, s
, prefix_blen(&zpi
.src
));
3057 STREAM_GETC(s
, zpi
.dst
.family
);
3058 STREAM_GETC(s
, zpi
.dst
.prefixlen
);
3059 STREAM_GET(&zpi
.dst
.u
.prefix
, s
, prefix_blen(&zpi
.dst
));
3061 STREAM_GETW(s
, zpi
.src_port_min
);
3062 STREAM_GETW(s
, zpi
.src_port_max
);
3063 STREAM_GETW(s
, zpi
.dst_port_min
);
3064 STREAM_GETW(s
, zpi
.dst_port_max
);
3065 STREAM_GETC(s
, zpi
.proto
);
3066 if (!is_default_prefix(&zpi
.src
))
3067 zpi
.filter_bm
|= PBR_FILTER_SRC_IP
;
3069 if (!is_default_prefix(&zpi
.dst
))
3070 zpi
.filter_bm
|= PBR_FILTER_DST_IP
;
3071 if (zpi
.dst_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3072 zpi
.filter_bm
|= PBR_FILTER_DST_PORT
;
3073 if (zpi
.src_port_min
!= 0 || zpi
.proto
== IPPROTO_ICMP
)
3074 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT
;
3075 if (zpi
.dst_port_max
!= 0)
3076 zpi
.filter_bm
|= PBR_FILTER_DST_PORT_RANGE
;
3077 if (zpi
.src_port_max
!= 0)
3078 zpi
.filter_bm
|= PBR_FILTER_SRC_PORT_RANGE
;
3080 zpi
.filter_bm
|= PBR_FILTER_PROTO
;
3082 if (!(zpi
.dst
.family
== AF_INET
3083 || zpi
.dst
.family
== AF_INET6
)) {
3085 "Unsupported PBR destination IP family: %s (%hhu)",
3086 family2str(zpi
.dst
.family
), zpi
.dst
.family
);
3087 goto stream_failure
;
3089 if (!(zpi
.src
.family
== AF_INET
3090 || zpi
.src
.family
== AF_INET6
)) {
3092 "Unsupported PBR source IP family: %s (%hhu)",
3093 family2str(zpi
.src
.family
), zpi
.src
.family
);
3094 goto stream_failure
;
3097 /* calculate backpointer */
3099 zebra_pbr_lookup_ipset_pername(ipset
.ipset_name
);
3101 if (!zpi
.backpointer
) {
3102 zlog_warn("ipset name specified: %s does not exist",
3104 goto stream_failure
;
3107 if (hdr
->command
== ZEBRA_IPSET_ENTRY_ADD
)
3108 zebra_pbr_add_ipset_entry(&zpi
);
3110 zebra_pbr_del_ipset_entry(&zpi
);
3117 static inline void zread_iptable(ZAPI_HANDLER_ARGS
)
3119 struct zebra_pbr_iptable
*zpi
=
3120 XCALLOC(MTYPE_TMP
, sizeof(struct zebra_pbr_iptable
));
3125 zpi
->interface_name_list
= list_new();
3126 zpi
->sock
= client
->sock
;
3127 zpi
->vrf_id
= zvrf
->vrf
->vrf_id
;
3128 STREAM_GETL(s
, zpi
->unique
);
3129 STREAM_GETL(s
, zpi
->type
);
3130 STREAM_GETL(s
, zpi
->filter_bm
);
3131 STREAM_GETL(s
, zpi
->action
);
3132 STREAM_GETL(s
, zpi
->fwmark
);
3133 STREAM_GET(&zpi
->ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
3134 STREAM_GETC(s
, zpi
->family
);
3135 STREAM_GETW(s
, zpi
->pkt_len_min
);
3136 STREAM_GETW(s
, zpi
->pkt_len_max
);
3137 STREAM_GETW(s
, zpi
->tcp_flags
);
3138 STREAM_GETW(s
, zpi
->tcp_mask_flags
);
3139 STREAM_GETC(s
, zpi
->dscp_value
);
3140 STREAM_GETC(s
, zpi
->fragment
);
3141 STREAM_GETC(s
, zpi
->protocol
);
3142 STREAM_GETW(s
, zpi
->flow_label
);
3143 STREAM_GETL(s
, zpi
->nb_interface
);
3144 zebra_pbr_iptable_update_interfacelist(s
, zpi
);
3146 if (hdr
->command
== ZEBRA_IPTABLE_ADD
)
3147 zebra_pbr_add_iptable(zpi
);
3149 zebra_pbr_del_iptable(zpi
);
3152 zebra_pbr_iptable_free(zpi
);
3157 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS
)
3161 struct interface
*ifp
;
3167 STREAM_GETL(s
, ifindex
);
3169 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
, ifindex
);
3171 zlog_debug("Failed to lookup ifindex: %u", ifindex
);
3175 STREAM_GETC(s
, p
.family
);
3176 STREAM_GETC(s
, p
.prefixlen
);
3177 STREAM_GET(&p
.u
.prefix
, s
, prefix_blen(&p
));
3179 if (p
.family
== AF_INET
)
3184 memcpy(&ip
.ip
.addr
, &p
.u
.prefix
, prefix_blen(&p
));
3186 dplane_neigh_discover(ifp
, &ip
);
3192 static void zsend_error_msg(struct zserv
*client
, enum zebra_error_types error
,
3193 struct zmsghdr
*bad_hdr
)
3196 struct stream
*s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
3198 zclient_create_header(s
, ZEBRA_ERROR
, bad_hdr
->vrf_id
);
3200 zserv_encode_error(s
, error
);
3202 client
->error_cnt
++;
3203 zserv_send_message(client
, s
);
3206 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS
)
3208 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3209 zlog_debug("ZAPI message specifies unknown VRF: %d",
3212 zsend_error_msg(client
, ZEBRA_NO_VRF
, hdr
);
3215 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS
)
3217 zlog_info("Zebra received unknown command %d", hdr
->command
);
3219 zsend_error_msg(client
, ZEBRA_INVALID_MSG_TYPE
, hdr
);
3222 void (*const zserv_handlers
[])(ZAPI_HANDLER_ARGS
) = {
3223 [ZEBRA_ROUTER_ID_ADD
] = zread_router_id_add
,
3224 [ZEBRA_ROUTER_ID_DELETE
] = zread_router_id_delete
,
3225 [ZEBRA_INTERFACE_ADD
] = zread_interface_add
,
3226 [ZEBRA_INTERFACE_DELETE
] = zread_interface_delete
,
3227 [ZEBRA_INTERFACE_SET_PROTODOWN
] = zread_interface_set_protodown
,
3228 [ZEBRA_ROUTE_ADD
] = zread_route_add
,
3229 [ZEBRA_ROUTE_DELETE
] = zread_route_del
,
3230 [ZEBRA_REDISTRIBUTE_ADD
] = zebra_redistribute_add
,
3231 [ZEBRA_REDISTRIBUTE_DELETE
] = zebra_redistribute_delete
,
3232 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD
] = zebra_redistribute_default_add
,
3233 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
] = zebra_redistribute_default_delete
,
3234 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
] = zread_ipv4_nexthop_lookup_mrib
,
3235 [ZEBRA_HELLO
] = zread_hello
,
3236 [ZEBRA_NEXTHOP_REGISTER
] = zread_rnh_register
,
3237 [ZEBRA_NEXTHOP_UNREGISTER
] = zread_rnh_unregister
,
3238 [ZEBRA_IMPORT_ROUTE_REGISTER
] = zread_rnh_register
,
3239 [ZEBRA_IMPORT_ROUTE_UNREGISTER
] = zread_rnh_unregister
,
3240 [ZEBRA_BFD_DEST_UPDATE
] = zebra_ptm_bfd_dst_register
,
3241 [ZEBRA_BFD_DEST_REGISTER
] = zebra_ptm_bfd_dst_register
,
3242 [ZEBRA_BFD_DEST_DEREGISTER
] = zebra_ptm_bfd_dst_deregister
,
3244 [ZEBRA_BFD_DEST_REPLAY
] = zebra_ptm_bfd_dst_replay
,
3245 #endif /* HAVE_BFDD */
3246 [ZEBRA_VRF_UNREGISTER
] = zread_vrf_unregister
,
3247 [ZEBRA_VRF_LABEL
] = zread_vrf_label
,
3248 [ZEBRA_BFD_CLIENT_REGISTER
] = zebra_ptm_bfd_client_register
,
3249 [ZEBRA_INTERFACE_ENABLE_RADV
] = zebra_interface_radv_enable
,
3250 [ZEBRA_INTERFACE_DISABLE_RADV
] = zebra_interface_radv_disable
,
3251 [ZEBRA_SR_POLICY_SET
] = zread_sr_policy_set
,
3252 [ZEBRA_SR_POLICY_DELETE
] = zread_sr_policy_delete
,
3253 [ZEBRA_MPLS_LABELS_ADD
] = zread_mpls_labels_add
,
3254 [ZEBRA_MPLS_LABELS_DELETE
] = zread_mpls_labels_delete
,
3255 [ZEBRA_MPLS_LABELS_REPLACE
] = zread_mpls_labels_replace
,
3256 [ZEBRA_IPMR_ROUTE_STATS
] = zebra_ipmr_route_stats
,
3257 [ZEBRA_LABEL_MANAGER_CONNECT
] = zread_label_manager_request
,
3258 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
] = zread_label_manager_request
,
3259 [ZEBRA_GET_LABEL_CHUNK
] = zread_label_manager_request
,
3260 [ZEBRA_RELEASE_LABEL_CHUNK
] = zread_label_manager_request
,
3261 [ZEBRA_FEC_REGISTER
] = zread_fec_register
,
3262 [ZEBRA_FEC_UNREGISTER
] = zread_fec_unregister
,
3263 [ZEBRA_ADVERTISE_DEFAULT_GW
] = zebra_vxlan_advertise_gw_macip
,
3264 [ZEBRA_ADVERTISE_SVI_MACIP
] = zebra_vxlan_advertise_svi_macip
,
3265 [ZEBRA_ADVERTISE_SUBNET
] = zebra_vxlan_advertise_subnet
,
3266 [ZEBRA_ADVERTISE_ALL_VNI
] = zebra_vxlan_advertise_all_vni
,
3267 [ZEBRA_REMOTE_ES_VTEP_ADD
] = zebra_evpn_proc_remote_es
,
3268 [ZEBRA_REMOTE_ES_VTEP_DEL
] = zebra_evpn_proc_remote_es
,
3269 [ZEBRA_REMOTE_VTEP_ADD
] = zebra_vxlan_remote_vtep_add
,
3270 [ZEBRA_REMOTE_VTEP_DEL
] = zebra_vxlan_remote_vtep_del
,
3271 [ZEBRA_REMOTE_MACIP_ADD
] = zebra_vxlan_remote_macip_add
,
3272 [ZEBRA_REMOTE_MACIP_DEL
] = zebra_vxlan_remote_macip_del
,
3273 [ZEBRA_DUPLICATE_ADDR_DETECTION
] = zebra_vxlan_dup_addr_detection
,
3274 [ZEBRA_INTERFACE_SET_MASTER
] = zread_interface_set_master
,
3275 [ZEBRA_PW_ADD
] = zread_pseudowire
,
3276 [ZEBRA_PW_DELETE
] = zread_pseudowire
,
3277 [ZEBRA_PW_SET
] = zread_pseudowire
,
3278 [ZEBRA_PW_UNSET
] = zread_pseudowire
,
3279 [ZEBRA_RULE_ADD
] = zread_rule
,
3280 [ZEBRA_RULE_DELETE
] = zread_rule
,
3281 [ZEBRA_TABLE_MANAGER_CONNECT
] = zread_table_manager_request
,
3282 [ZEBRA_GET_TABLE_CHUNK
] = zread_table_manager_request
,
3283 [ZEBRA_RELEASE_TABLE_CHUNK
] = zread_table_manager_request
,
3284 [ZEBRA_IPSET_CREATE
] = zread_ipset
,
3285 [ZEBRA_IPSET_DESTROY
] = zread_ipset
,
3286 [ZEBRA_IPSET_ENTRY_ADD
] = zread_ipset_entry
,
3287 [ZEBRA_IPSET_ENTRY_DELETE
] = zread_ipset_entry
,
3288 [ZEBRA_IPTABLE_ADD
] = zread_iptable
,
3289 [ZEBRA_IPTABLE_DELETE
] = zread_iptable
,
3290 [ZEBRA_VXLAN_FLOOD_CONTROL
] = zebra_vxlan_flood_control
,
3291 [ZEBRA_VXLAN_SG_REPLAY
] = zebra_vxlan_sg_replay
,
3292 [ZEBRA_MLAG_CLIENT_REGISTER
] = zebra_mlag_client_register
,
3293 [ZEBRA_MLAG_CLIENT_UNREGISTER
] = zebra_mlag_client_unregister
,
3294 [ZEBRA_MLAG_FORWARD_MSG
] = zebra_mlag_forward_client_msg
,
3295 [ZEBRA_CLIENT_CAPABILITIES
] = zread_client_capabilities
,
3296 [ZEBRA_NEIGH_DISCOVER
] = zread_neigh_discover
,
3297 [ZEBRA_NHG_ADD
] = zread_nhg_add
,
3298 [ZEBRA_NHG_DEL
] = zread_nhg_del
,
3302 * Process a batch of zapi messages.
3304 void zserv_handle_commands(struct zserv
*client
, struct stream_fifo
*fifo
)
3307 struct zebra_vrf
*zvrf
;
3309 struct stream_fifo temp_fifo
;
3311 stream_fifo_init(&temp_fifo
);
3313 while (stream_fifo_head(fifo
)) {
3314 msg
= stream_fifo_pop(fifo
);
3316 if (STREAM_READABLE(msg
) > ZEBRA_MAX_PACKET_SIZ
) {
3317 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
3319 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3320 STREAM_READABLE(msg
),
3321 ZEBRA_MAX_PACKET_SIZ
);
3325 zapi_parse_header(msg
, &hdr
);
3327 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
3328 && IS_ZEBRA_DEBUG_DETAIL
)
3329 zserv_log_message(NULL
, msg
, &hdr
);
3331 hdr
.length
-= ZEBRA_HEADER_SIZE
;
3333 /* Before checking for a handler function, check for
3334 * special messages that are handled in another module;
3335 * we'll treat these as opaque.
3337 if (zebra_opaque_handles_msgid(hdr
.command
)) {
3338 /* Reset message buffer */
3339 stream_set_getp(msg
, 0);
3341 stream_fifo_push(&temp_fifo
, msg
);
3343 /* Continue without freeing the message */
3349 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
3351 zserv_error_no_vrf(client
, &hdr
, msg
, zvrf
);
3355 if (hdr
.command
>= array_size(zserv_handlers
)
3356 || zserv_handlers
[hdr
.command
] == NULL
) {
3357 zserv_error_invalid_msg_type(client
, &hdr
, msg
, zvrf
);
3361 zserv_handlers
[hdr
.command
](client
, &hdr
, msg
, zvrf
);
3367 /* Dispatch any special messages from the temp fifo */
3368 if (stream_fifo_head(&temp_fifo
) != NULL
)
3369 zebra_opaque_enqueue_batch(&temp_fifo
);
3371 stream_fifo_deinit(&temp_fifo
);