1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
41 #include "lib_errors.h"
43 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
44 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
46 /* Zebra client events. */
47 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
49 /* Prototype for event manager. */
50 static void zclient_event(enum event
, struct zclient
*);
52 struct sockaddr_storage zclient_addr
;
53 socklen_t zclient_addr_len
;
55 /* This file local debug flag. */
56 int zclient_debug
= 0;
58 struct zclient_options zclient_options_default
= {.receive_notify
= false};
60 /* Allocate zclient structure. */
61 struct zclient
*zclient_new(struct thread_master
*master
,
62 struct zclient_options
*opt
)
64 struct zclient
*zclient
;
65 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
67 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
68 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
69 zclient
->wb
= buffer_new(0);
70 zclient
->master
= master
;
72 zclient
->receive_notify
= opt
->receive_notify
;
77 /* This function is only called when exiting, because
78 many parts of the code do not check for I/O errors, so they could
79 reference an invalid pointer if the structure was ever freed.
81 Free zclient structure. */
82 void zclient_free(struct zclient
*zclient
)
85 stream_free(zclient
->ibuf
);
87 stream_free(zclient
->obuf
);
89 buffer_free(zclient
->wb
);
91 XFREE(MTYPE_ZCLIENT
, zclient
);
94 unsigned short *redist_check_instance(struct redist_proto
*red
,
95 unsigned short instance
)
97 struct listnode
*node
;
103 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
110 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
117 red
->instances
= list_new();
119 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
121 listnode_add(red
->instances
, in
);
124 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
128 id
= redist_check_instance(red
, instance
);
132 listnode_delete(red
->instances
, id
);
133 XFREE(MTYPE_REDIST_INST
, id
);
134 if (!red
->instances
->count
) {
136 list_delete(&red
->instances
);
140 /* Stop zebra client services. */
141 void zclient_stop(struct zclient
*zclient
)
147 zlog_debug("zclient stopped");
150 THREAD_OFF(zclient
->t_read
);
151 THREAD_OFF(zclient
->t_connect
);
152 THREAD_OFF(zclient
->t_write
);
155 stream_reset(zclient
->ibuf
);
156 stream_reset(zclient
->obuf
);
158 /* Empty the write buffer. */
159 buffer_reset(zclient
->wb
);
162 if (zclient
->sock
>= 0) {
163 close(zclient
->sock
);
168 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
169 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
170 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
171 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
174 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
177 vrf_bitmap_free(zclient
->default_information
[afi
]);
178 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
182 void zclient_reset(struct zclient
*zclient
)
186 zclient_stop(zclient
);
188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
190 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
193 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
198 * Connect to zebra daemon.
199 * @param zclient a pointer to zclient structure
200 * @return socket fd just to make sure that connection established
204 int zclient_socket_connect(struct zclient
*zclient
)
209 /* We should think about IPv6 connection. */
210 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
216 frr_elevate_privs(zclient
->privs
) {
217 setsockopt_so_sendbuf(sock
, 1048576);
220 /* Connect to zebra. */
221 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
224 zlog_debug("%s connect failure: %d(%s)",
225 __PRETTY_FUNCTION__
, errno
,
226 safe_strerror(errno
));
231 zclient
->sock
= sock
;
235 static int zclient_failed(struct zclient
*zclient
)
238 zclient_stop(zclient
);
239 zclient_event(ZCLIENT_CONNECT
, zclient
);
243 static int zclient_flush_data(struct thread
*thread
)
245 struct zclient
*zclient
= THREAD_ARG(thread
);
247 zclient
->t_write
= NULL
;
248 if (zclient
->sock
< 0)
250 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
254 "%s: buffer_flush_available failed on zclient fd %d, closing",
255 __func__
, zclient
->sock
);
256 return zclient_failed(zclient
);
259 zclient
->t_write
= NULL
;
260 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
261 zclient
->sock
, &zclient
->t_write
);
269 int zclient_send_message(struct zclient
*zclient
)
271 if (zclient
->sock
< 0)
273 switch (buffer_write(zclient
->wb
, zclient
->sock
,
274 STREAM_DATA(zclient
->obuf
),
275 stream_get_endp(zclient
->obuf
))) {
277 flog_err(EC_LIB_ZAPI_SOCKET
,
278 "%s: buffer_write failed to zclient fd %d, closing",
279 __func__
, zclient
->sock
);
280 return zclient_failed(zclient
);
283 THREAD_OFF(zclient
->t_write
);
286 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
287 zclient
->sock
, &zclient
->t_write
);
293 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
295 /* length placeholder, caller can update */
296 stream_putw(s
, ZEBRA_HEADER_SIZE
);
297 stream_putc(s
, ZEBRA_HEADER_MARKER
);
298 stream_putc(s
, ZSERV_VERSION
);
299 stream_putl(s
, vrf_id
);
300 stream_putw(s
, command
);
303 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
304 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
307 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
310 STREAM_GETW(s
, *size
);
311 *size
-= ZEBRA_HEADER_SIZE
;
312 STREAM_GETC(s
, *marker
);
313 STREAM_GETC(s
, *version
);
314 STREAM_GETL(s
, *vrf_id
);
315 STREAM_GETW(s
, *cmd
);
317 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
319 EC_LIB_ZAPI_MISSMATCH
,
320 "%s: socket %d version mismatch, marker %d, version %d",
321 __func__
, sock
, *marker
, *version
);
325 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
332 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
334 STREAM_GETW(zmsg
, hdr
->length
);
335 STREAM_GETC(zmsg
, hdr
->marker
);
336 STREAM_GETC(zmsg
, hdr
->version
);
337 STREAM_GETL(zmsg
, hdr
->vrf_id
);
338 STREAM_GETW(zmsg
, hdr
->command
);
344 /* Send simple Zebra message. */
345 static int zebra_message_send(struct zclient
*zclient
, int command
,
350 /* Get zclient output buffer. */
354 /* Send very simple command only Zebra message. */
355 zclient_create_header(s
, command
, vrf_id
);
357 return zclient_send_message(zclient
);
360 static int zebra_hello_send(struct zclient
*zclient
)
364 if (zclient
->redist_default
) {
368 /* The VRF ID in the HELLO message is always 0. */
369 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
370 stream_putc(s
, zclient
->redist_default
);
371 stream_putw(s
, zclient
->instance
);
372 if (zclient
->receive_notify
)
377 stream_putw_at(s
, 0, stream_get_endp(s
));
378 return zclient_send_message(zclient
);
384 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
385 mpls_label_t label
, enum lsp_types_t ltype
)
392 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
393 stream_putl(s
, label
);
395 stream_putc(s
, ltype
);
396 stream_putw_at(s
, 0, stream_get_endp(s
));
397 zclient_send_message(zclient
);
400 /* Send register requests to zebra daemon for the information in a VRF. */
401 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
406 /* If not connected to the zebra yet. */
407 if (zclient
->sock
< 0)
411 zlog_debug("%s: send register messages for VRF %u", __func__
,
414 /* We need router-id information. */
415 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
417 /* Set unwanted redistribute route. */
418 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
419 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
422 /* Flush all redistribute request. */
423 if (vrf_id
== VRF_DEFAULT
) {
424 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
425 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
426 if (!zclient
->mi_redist
[afi
][i
].enabled
)
429 struct listnode
*node
;
432 for (ALL_LIST_ELEMENTS_RO(
433 zclient
->mi_redist
[afi
][i
]
436 if (!(i
== zclient
->redist_default
437 && *id
== zclient
->instance
))
438 zebra_redistribute_send(
439 ZEBRA_REDISTRIBUTE_ADD
,
440 zclient
, afi
, i
, *id
,
446 /* Resend all redistribute request. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
448 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
449 if (i
!= zclient
->redist_default
450 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
452 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
456 /* If default information is needed. */
457 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
458 zebra_redistribute_default_send(
459 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
464 /* Send unregister requests to zebra daemon for the information in a VRF. */
465 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
470 /* If not connected to the zebra yet. */
471 if (zclient
->sock
< 0)
475 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
478 /* We need router-id information. */
479 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
481 /* Set unwanted redistribute route. */
482 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
483 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
486 /* Flush all redistribute request. */
487 if (vrf_id
== VRF_DEFAULT
) {
488 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
489 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
490 if (!zclient
->mi_redist
[afi
][i
].enabled
)
493 struct listnode
*node
;
496 for (ALL_LIST_ELEMENTS_RO(
497 zclient
->mi_redist
[afi
][i
]
500 if (!(i
== zclient
->redist_default
501 && *id
== zclient
->instance
))
502 zebra_redistribute_send(
503 ZEBRA_REDISTRIBUTE_DELETE
,
504 zclient
, afi
, i
, *id
,
510 /* Flush all redistribute request. */
511 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
512 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
513 if (i
!= zclient
->redist_default
514 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
516 zebra_redistribute_send(
517 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
520 /* If default information is needed. */
521 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
522 zebra_redistribute_default_send(
523 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
528 /* Send request to zebra daemon to start or stop RA. */
529 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
530 struct interface
*ifp
, int enable
,
535 /* If not connected to the zebra yet. */
536 if (zclient
->sock
< 0)
539 /* Form and send message. */
544 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
546 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
548 stream_putl(s
, ifp
->ifindex
);
549 stream_putl(s
, ra_interval
);
551 stream_putw_at(s
, 0, stream_get_endp(s
));
553 zclient_send_message(zclient
);
556 /* Make connection to zebra daemon. */
557 int zclient_start(struct zclient
*zclient
)
560 zlog_info("zclient_start is called");
562 /* If already connected to the zebra. */
563 if (zclient
->sock
>= 0)
566 /* Check connect thread. */
567 if (zclient
->t_connect
)
570 if (zclient_socket_connect(zclient
) < 0) {
572 zlog_debug("zclient connection fail");
574 zclient_event(ZCLIENT_CONNECT
, zclient
);
578 if (set_nonblocking(zclient
->sock
) < 0)
579 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
580 __func__
, zclient
->sock
);
582 /* Clear fail count. */
585 zlog_debug("zclient connect success with socket [%d]",
588 /* Create read thread. */
589 zclient_event(ZCLIENT_READ
, zclient
);
591 zebra_hello_send(zclient
);
593 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
595 /* Inform the successful connection. */
596 if (zclient
->zebra_connected
)
597 (*zclient
->zebra_connected
)(zclient
);
602 /* Initialize zebra client. Argument redist_default is unwanted
603 redistribute route type. */
604 void zclient_init(struct zclient
*zclient
, int redist_default
,
605 unsigned short instance
, struct zebra_privs_t
*privs
)
609 /* Set -1 to the default socket value. */
611 zclient
->privs
= privs
;
613 /* Clear redistribution flags. */
614 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
615 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
616 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
618 /* Set unwanted redistribute route. bgpd does not need BGP route
620 zclient
->redist_default
= redist_default
;
621 zclient
->instance
= instance
;
622 /* Pending: make afi(s) an arg. */
623 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
624 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
627 /* Set default-information redistribute to zero. */
628 zclient
->default_information
[afi
] = vrf_bitmap_init();
632 zlog_debug("scheduling zclient connection");
634 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
637 /* This function is a wrapper function for calling zclient_start from
638 timer or event thread. */
639 static int zclient_connect(struct thread
*t
)
641 struct zclient
*zclient
;
643 zclient
= THREAD_ARG(t
);
644 zclient
->t_connect
= NULL
;
647 zlog_debug("zclient_connect is called");
649 return zclient_start(zclient
);
652 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
653 bool exact_match
, vrf_id_t vrf_id
)
659 zclient_create_header(s
, command
, vrf_id
);
660 stream_putc(s
, (exact_match
) ? 1 : 0);
662 stream_putw(s
, PREFIX_FAMILY(p
));
663 stream_putc(s
, p
->prefixlen
);
664 switch (PREFIX_FAMILY(p
)) {
666 stream_put_in_addr(s
, &p
->u
.prefix4
);
669 stream_put(s
, &(p
->u
.prefix6
), 16);
674 stream_putw_at(s
, 0, stream_get_endp(s
));
676 return zclient_send_message(zclient
);
680 * "xdr_encode"-like interface that allows daemon (client) to send
681 * a message to zebra server for a route that needs to be
682 * added/deleted to the kernel. Info about the route is specified
683 * by the caller in a struct zapi_route. zapi_route_encode() then writes
684 * the info down the zclient socket using the stream_* functions.
686 * The corresponding read ("xdr_decode") function on the server
687 * side is zapi_route_decode().
689 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | Length (2) | Command | Route Type |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | ZEBRA Flags | Message Flags | Prefix length |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | Destination IPv4 Prefix for route |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
702 * described, as per the Nexthop count. Each nexthop described as:
705 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
706 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
707 * | IPv4 Nexthop address or Interface Index number |
708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
710 * Alternatively, if the route is a blackhole route, then Nexthop count
711 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
714 * The original struct zapi_route_*() infrastructure was built around
715 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
716 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
717 * nexthops into zapi_route_encode() using the same zapi_route structure.
718 * This is done by setting zapi_route fields as follows:
719 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
720 * - .nexthop_num == .ifindex_num
721 * - .nexthop and .ifindex are filled with gate and ifindex parts of
722 * each compound nexthop, both in the same order
724 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
727 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
730 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
732 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
734 * XXX: No attention paid to alignment.
736 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
737 struct zapi_route
*api
)
739 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
741 return zclient_send_message(zclient
);
744 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
746 struct zapi_nexthop
*api_nh
;
751 zclient_create_header(s
, cmd
, api
->vrf_id
);
753 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
754 flog_err(EC_LIB_ZAPI_ENCODE
,
755 "%s: Specified route type (%u) is not a legal value\n",
756 __PRETTY_FUNCTION__
, api
->type
);
759 stream_putc(s
, api
->type
);
761 stream_putw(s
, api
->instance
);
762 stream_putl(s
, api
->flags
);
763 stream_putc(s
, api
->message
);
765 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
766 flog_err(EC_LIB_ZAPI_ENCODE
,
767 "%s: Specified route SAFI (%u) is not a legal value\n",
768 __PRETTY_FUNCTION__
, api
->safi
);
771 stream_putc(s
, api
->safi
);
773 /* Put prefix information. */
774 stream_putc(s
, api
->prefix
.family
);
775 psize
= PSIZE(api
->prefix
.prefixlen
);
776 stream_putc(s
, api
->prefix
.prefixlen
);
777 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
779 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
780 psize
= PSIZE(api
->src_prefix
.prefixlen
);
781 stream_putc(s
, api
->src_prefix
.prefixlen
);
782 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
786 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
787 /* limit the number of nexthops if necessary */
788 if (api
->nexthop_num
> MULTIPATH_NUM
) {
789 char buf
[PREFIX2STR_BUFFER
];
791 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
794 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
795 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
799 stream_putw(s
, api
->nexthop_num
);
801 for (i
= 0; i
< api
->nexthop_num
; i
++) {
802 api_nh
= &api
->nexthops
[i
];
804 stream_putl(s
, api_nh
->vrf_id
);
805 stream_putc(s
, api_nh
->type
);
806 stream_putc(s
, api_nh
->onlink
);
807 switch (api_nh
->type
) {
808 case NEXTHOP_TYPE_BLACKHOLE
:
809 stream_putc(s
, api_nh
->bh_type
);
811 case NEXTHOP_TYPE_IPV4
:
812 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
814 case NEXTHOP_TYPE_IPV4_IFINDEX
:
815 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
816 stream_putl(s
, api_nh
->ifindex
);
818 case NEXTHOP_TYPE_IFINDEX
:
819 stream_putl(s
, api_nh
->ifindex
);
821 case NEXTHOP_TYPE_IPV6
:
822 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
825 case NEXTHOP_TYPE_IPV6_IFINDEX
:
826 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
828 stream_putl(s
, api_nh
->ifindex
);
832 /* MPLS labels for BGP-LU or Segment Routing */
833 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
834 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
835 char buf
[PREFIX2STR_BUFFER
];
836 prefix2str(&api
->prefix
, buf
,
838 flog_err(EC_LIB_ZAPI_ENCODE
,
839 "%s: prefix %s: can't encode "
840 "%u labels (maximum is %u)",
847 stream_putc(s
, api_nh
->label_num
);
848 stream_put(s
, &api_nh
->labels
[0],
850 * sizeof(mpls_label_t
));
853 /* Router MAC for EVPN routes. */
854 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
855 stream_put(s
, &(api_nh
->rmac
),
856 sizeof(struct ethaddr
));
861 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
862 stream_putc(s
, api
->distance
);
863 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
864 stream_putl(s
, api
->metric
);
865 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
866 stream_putl(s
, api
->tag
);
867 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
868 stream_putl(s
, api
->mtu
);
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
870 stream_putl(s
, api
->tableid
);
872 /* Put length at the first point of the stream. */
873 stream_putw_at(s
, 0, stream_get_endp(s
));
878 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
880 struct zapi_nexthop
*api_nh
;
883 memset(api
, 0, sizeof(*api
));
885 /* Type, flags, message. */
886 STREAM_GETC(s
, api
->type
);
887 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
888 flog_err(EC_LIB_ZAPI_ENCODE
,
889 "%s: Specified route type: %d is not a legal value\n",
890 __PRETTY_FUNCTION__
, api
->type
);
894 STREAM_GETW(s
, api
->instance
);
895 STREAM_GETL(s
, api
->flags
);
896 STREAM_GETC(s
, api
->message
);
897 STREAM_GETC(s
, api
->safi
);
898 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
899 flog_err(EC_LIB_ZAPI_ENCODE
,
900 "%s: Specified route SAFI (%u) is not a legal value\n",
901 __PRETTY_FUNCTION__
, api
->safi
);
906 STREAM_GETC(s
, api
->prefix
.family
);
907 STREAM_GETC(s
, api
->prefix
.prefixlen
);
908 switch (api
->prefix
.family
) {
910 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
913 "%s: V4 prefixlen is %d which should not be more than 32",
914 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
919 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
922 "%s: v6 prefixlen is %d which should not be more than 128",
923 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
928 flog_err(EC_LIB_ZAPI_ENCODE
,
929 "%s: Specified family %d is not v4 or v6",
930 __PRETTY_FUNCTION__
, api
->prefix
.family
);
933 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
935 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
936 api
->src_prefix
.family
= AF_INET6
;
937 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
938 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
941 "%s: SRC Prefix prefixlen received: %d is too large",
942 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
945 STREAM_GET(&api
->src_prefix
.prefix
, s
,
946 PSIZE(api
->src_prefix
.prefixlen
));
948 if (api
->prefix
.family
!= AF_INET6
949 || api
->src_prefix
.prefixlen
== 0) {
952 "%s: SRC prefix specified in some manner that makes no sense",
953 __PRETTY_FUNCTION__
);
959 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
960 STREAM_GETW(s
, api
->nexthop_num
);
961 if (api
->nexthop_num
> MULTIPATH_NUM
) {
962 flog_err(EC_LIB_ZAPI_ENCODE
,
963 "%s: invalid number of nexthops (%u)",
964 __func__
, api
->nexthop_num
);
968 for (i
= 0; i
< api
->nexthop_num
; i
++) {
969 api_nh
= &api
->nexthops
[i
];
971 STREAM_GETL(s
, api_nh
->vrf_id
);
972 STREAM_GETC(s
, api_nh
->type
);
973 STREAM_GETC(s
, api_nh
->onlink
);
974 switch (api_nh
->type
) {
975 case NEXTHOP_TYPE_BLACKHOLE
:
976 STREAM_GETC(s
, api_nh
->bh_type
);
978 case NEXTHOP_TYPE_IPV4
:
979 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
982 case NEXTHOP_TYPE_IPV4_IFINDEX
:
983 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
985 STREAM_GETL(s
, api_nh
->ifindex
);
987 case NEXTHOP_TYPE_IFINDEX
:
988 STREAM_GETL(s
, api_nh
->ifindex
);
990 case NEXTHOP_TYPE_IPV6
:
991 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
993 case NEXTHOP_TYPE_IPV6_IFINDEX
:
994 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
995 STREAM_GETL(s
, api_nh
->ifindex
);
999 /* MPLS labels for BGP-LU or Segment Routing */
1000 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1001 STREAM_GETC(s
, api_nh
->label_num
);
1003 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1006 "%s: invalid number of MPLS labels (%u)",
1007 __func__
, api_nh
->label_num
);
1011 STREAM_GET(&api_nh
->labels
[0], s
,
1013 * sizeof(mpls_label_t
));
1016 /* Router MAC for EVPN routes. */
1017 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1018 stream_get(&(api_nh
->rmac
), s
,
1019 sizeof(struct ethaddr
));
1024 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1025 STREAM_GETC(s
, api
->distance
);
1026 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1027 STREAM_GETL(s
, api
->metric
);
1028 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1029 STREAM_GETL(s
, api
->tag
);
1030 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1031 STREAM_GETL(s
, api
->mtu
);
1032 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1033 STREAM_GETL(s
, api
->tableid
);
1039 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1045 memset(&any
, 0, sizeof(any
));
1046 any
.family
= family
;
1050 stream_putc(s
, p
->family
);
1051 stream_putc(s
, p
->prefixlen
);
1052 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1055 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1058 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1061 * We are sending one item at a time at the moment
1065 stream_putl(s
, zrule
->seq
);
1066 stream_putl(s
, zrule
->priority
);
1067 stream_putl(s
, zrule
->unique
);
1069 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1070 zrule
->filter
.src_ip
.family
);
1071 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1072 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1073 zrule
->filter
.src_ip
.family
);
1074 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1075 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1077 stream_putl(s
, zrule
->action
.table
);
1078 stream_putl(s
, zrule
->ifindex
);
1080 /* Put length at the first point of the stream. */
1081 stream_putw_at(s
, 0, stream_get_endp(s
));
1086 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1088 enum zapi_route_notify_owner
*note
)
1092 STREAM_GET(note
, s
, sizeof(*note
));
1094 STREAM_GETC(s
, p
->family
);
1095 STREAM_GETC(s
, p
->prefixlen
);
1096 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1107 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1108 uint32_t *priority
, uint32_t *unique
,
1110 enum zapi_rule_notify_owner
*note
)
1112 uint32_t prio
, seq
, uni
;
1115 STREAM_GET(note
, s
, sizeof(*note
));
1117 STREAM_GETL(s
, seq
);
1118 STREAM_GETL(s
, prio
);
1119 STREAM_GETL(s
, uni
);
1120 STREAM_GETL(s
, ifi
);
1123 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1136 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1137 enum zapi_ipset_notify_owner
*note
)
1141 STREAM_GET(note
, s
, sizeof(*note
));
1143 STREAM_GETL(s
, uni
);
1146 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1155 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1157 enum zapi_ipset_entry_notify_owner
*note
)
1161 STREAM_GET(note
, s
, sizeof(*note
));
1163 STREAM_GETL(s
, uni
);
1165 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1168 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1177 bool zapi_iptable_notify_decode(struct stream
*s
,
1179 enum zapi_iptable_notify_owner
*note
)
1183 STREAM_GET(note
, s
, sizeof(*note
));
1185 STREAM_GETL(s
, uni
);
1188 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1197 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1199 struct nexthop
*n
= nexthop_new();
1201 n
->type
= znh
->type
;
1202 n
->vrf_id
= znh
->vrf_id
;
1203 n
->ifindex
= znh
->ifindex
;
1204 n
->gate
= znh
->gate
;
1207 * This function currently handles labels
1209 if (znh
->label_num
) {
1210 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1217 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1221 memset(nhr
, 0, sizeof(*nhr
));
1223 STREAM_GETW(s
, nhr
->prefix
.family
);
1224 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1225 switch (nhr
->prefix
.family
) {
1227 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1230 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1236 STREAM_GETC(s
, nhr
->type
);
1237 STREAM_GETW(s
, nhr
->instance
);
1238 STREAM_GETC(s
, nhr
->distance
);
1239 STREAM_GETL(s
, nhr
->metric
);
1240 STREAM_GETC(s
, nhr
->nexthop_num
);
1242 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1243 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1244 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1245 switch (nhr
->nexthops
[i
].type
) {
1246 case NEXTHOP_TYPE_IPV4
:
1247 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1248 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1250 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1252 case NEXTHOP_TYPE_IFINDEX
:
1253 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1255 case NEXTHOP_TYPE_IPV6
:
1256 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1257 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1259 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1261 case NEXTHOP_TYPE_BLACKHOLE
:
1264 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1265 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1266 flog_err(EC_LIB_ZAPI_ENCODE
,
1267 "%s: invalid number of MPLS labels (%u)",
1268 __func__
, nhr
->nexthops
[i
].label_num
);
1271 if (nhr
->nexthops
[i
].label_num
)
1272 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1273 nhr
->nexthops
[i
].label_num
1274 * sizeof(mpls_label_t
));
1283 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1284 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1285 * then set/unset redist[type] in the client handle (a struct zserv) for the
1288 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1289 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1296 zclient_create_header(s
, command
, vrf_id
);
1297 stream_putc(s
, afi
);
1298 stream_putc(s
, type
);
1299 stream_putw(s
, instance
);
1301 stream_putw_at(s
, 0, stream_get_endp(s
));
1303 return zclient_send_message(zclient
);
1306 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1307 afi_t afi
, vrf_id_t vrf_id
)
1314 zclient_create_header(s
, command
, vrf_id
);
1315 stream_putc(s
, afi
);
1317 stream_putw_at(s
, 0, stream_get_endp(s
));
1319 return zclient_send_message(zclient
);
1322 /* Get prefix in ZServ format; family should be filled in on prefix */
1323 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1325 size_t plen
= prefix_blen(p
);
1332 stream_get(&p
->u
.prefix
, s
, plen
);
1334 p
->prefixlen
= MIN(plen
* 8, c
);
1340 /* Router-id update from zebra daemon. */
1341 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1343 /* Fetch interface address. */
1344 STREAM_GETC(s
, rid
->family
);
1346 zclient_stream_get_prefix(s
, rid
);
1352 /* Interface addition from zebra daemon. */
1354 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1355 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1357 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1358 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1379 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1381 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1382 * | Link Layer Type |
1383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1384 * | Harware Address Length |
1385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386 * | Hardware Address if HW lenght different from 0 |
1387 * | ... max INTERFACE_HWADDR_MAX |
1388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1389 * | Link_params? | Whether a link-params follows: 1 or 0.
1390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1392 * | .... (struct if_link_params). |
1393 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1396 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1399 char vrfname_tmp
[VRF_NAMSIZ
];
1400 struct vrf_data data
;
1402 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1403 /* Read interface name. */
1404 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1406 /* Lookup/create vrf by vrf_id. */
1407 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1408 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1409 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1410 /* overwrite default vrf */
1411 if (vrf_id
== VRF_DEFAULT
)
1412 vrf_set_default_name(vrfname_tmp
, false);
1416 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1420 /* Lookup vrf by vrf_id. */
1421 vrf
= vrf_lookup_by_id(vrf_id
);
1424 * If a routing protocol doesn't know about a
1425 * vrf that is about to be deleted. There is
1426 * no point in attempting to delete it.
1434 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1436 struct interface
*ifp
;
1437 char ifname_tmp
[INTERFACE_NAMSIZ
];
1439 /* Read interface name. */
1440 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1442 /* Lookup/create interface by name. */
1443 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1445 zebra_interface_if_set_value(s
, ifp
);
1451 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1452 * from zebra server. The format of this message is the same as
1453 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1454 * comments for zebra_interface_add_read), except that no sockaddr_dl
1455 * is sent at the tail of the message.
1457 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1459 struct interface
*ifp
;
1460 char ifname_tmp
[INTERFACE_NAMSIZ
];
1462 /* Read interface name. */
1463 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1465 /* Lookup this by interface index. */
1466 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1468 flog_err(EC_LIB_ZAPI_ENCODE
,
1469 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1470 ifname_tmp
, vrf_id
);
1474 zebra_interface_if_set_value(s
, ifp
);
1479 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1485 iflp
->lp_status
= stream_getl(s
);
1486 iflp
->te_metric
= stream_getl(s
);
1487 iflp
->max_bw
= stream_getf(s
);
1488 iflp
->max_rsv_bw
= stream_getf(s
);
1489 uint32_t bwclassnum
= stream_getl(s
);
1492 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1493 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1496 EC_LIB_ZAPI_MISSMATCH
,
1497 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1498 " - outdated library?",
1499 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1501 iflp
->admin_grp
= stream_getl(s
);
1502 iflp
->rmt_as
= stream_getl(s
);
1503 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1505 iflp
->av_delay
= stream_getl(s
);
1506 iflp
->min_delay
= stream_getl(s
);
1507 iflp
->max_delay
= stream_getl(s
);
1508 iflp
->delay_var
= stream_getl(s
);
1510 iflp
->pkt_loss
= stream_getf(s
);
1511 iflp
->res_bw
= stream_getf(s
);
1512 iflp
->ava_bw
= stream_getf(s
);
1513 iflp
->use_bw
= stream_getf(s
);
1516 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1519 struct if_link_params
*iflp
;
1524 ifindex
= stream_getl(s
);
1526 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1529 flog_err(EC_LIB_ZAPI_ENCODE
,
1530 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1535 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1538 link_params_set_value(s
, iflp
);
1543 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1545 uint8_t link_params_status
= 0;
1546 ifindex_t old_ifindex
;
1548 old_ifindex
= ifp
->ifindex
;
1549 /* Read interface's index. */
1550 if_set_index(ifp
, stream_getl(s
));
1551 ifp
->status
= stream_getc(s
);
1553 /* Read interface's value. */
1554 ifp
->flags
= stream_getq(s
);
1555 ifp
->ptm_enable
= stream_getc(s
);
1556 ifp
->ptm_status
= stream_getc(s
);
1557 ifp
->metric
= stream_getl(s
);
1558 ifp
->speed
= stream_getl(s
);
1559 ifp
->mtu
= stream_getl(s
);
1560 ifp
->mtu6
= stream_getl(s
);
1561 ifp
->bandwidth
= stream_getl(s
);
1562 ifp
->ll_type
= stream_getl(s
);
1563 ifp
->hw_addr_len
= stream_getl(s
);
1564 if (ifp
->hw_addr_len
)
1565 stream_get(ifp
->hw_addr
, s
,
1566 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1568 /* Read Traffic Engineering status */
1569 link_params_status
= stream_getc(s
);
1570 /* Then, Traffic Engineering parameters if any */
1571 if (link_params_status
) {
1572 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1573 link_params_set_value(s
, iflp
);
1576 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1579 size_t zebra_interface_link_params_write(struct stream
*s
,
1580 struct interface
*ifp
)
1583 struct if_link_params
*iflp
;
1586 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1589 iflp
= ifp
->link_params
;
1592 w
+= stream_putl(s
, iflp
->lp_status
);
1594 w
+= stream_putl(s
, iflp
->te_metric
);
1595 w
+= stream_putf(s
, iflp
->max_bw
);
1596 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1598 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1599 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1600 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1602 w
+= stream_putl(s
, iflp
->admin_grp
);
1603 w
+= stream_putl(s
, iflp
->rmt_as
);
1604 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1606 w
+= stream_putl(s
, iflp
->av_delay
);
1607 w
+= stream_putl(s
, iflp
->min_delay
);
1608 w
+= stream_putl(s
, iflp
->max_delay
);
1609 w
+= stream_putl(s
, iflp
->delay_var
);
1611 w
+= stream_putf(s
, iflp
->pkt_loss
);
1612 w
+= stream_putf(s
, iflp
->res_bw
);
1613 w
+= stream_putf(s
, iflp
->ava_bw
);
1614 w
+= stream_putf(s
, iflp
->use_bw
);
1620 * format of message for address additon is:
1624 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1625 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1634 * | ifc_flags | flags for connected address
1642 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1650 static int memconstant(const void *s
, int c
, size_t n
)
1652 const uint8_t *p
= s
;
1661 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1665 struct interface
*ifp
;
1666 struct connected
*ifc
;
1667 struct prefix p
, d
, *dp
;
1671 memset(&p
, 0, sizeof(p
));
1672 memset(&d
, 0, sizeof(d
));
1674 /* Get interface index. */
1675 ifindex
= stream_getl(s
);
1678 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1680 flog_err(EC_LIB_ZAPI_ENCODE
,
1681 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1682 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1688 ifc_flags
= stream_getc(s
);
1690 /* Fetch interface address. */
1691 d
.family
= p
.family
= stream_getc(s
);
1692 plen
= prefix_blen(&d
);
1694 zclient_stream_get_prefix(s
, &p
);
1696 /* Fetch destination address. */
1697 stream_get(&d
.u
.prefix
, s
, plen
);
1699 /* N.B. NULL destination pointers are encoded as all zeroes */
1700 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1702 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1703 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1705 /* N.B. NULL destination pointers are encoded as all
1707 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1710 ifc
->flags
= ifc_flags
;
1711 if (ifc
->destination
)
1712 ifc
->destination
->prefixlen
=
1713 ifc
->address
->prefixlen
;
1714 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1715 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1717 char buf
[PREFIX_STRLEN
];
1720 "warning: interface %s address %s with peer flag set, but no peer address!",
1722 prefix2str(ifc
->address
, buf
,
1724 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1728 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1729 ifc
= connected_delete_by_prefix(ifp
, &p
);
1736 * format of message for neighbor connected address is:
1740 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1741 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1756 * | addr_len | len of addr.
1759 struct nbr_connected
*
1760 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1762 unsigned int ifindex
;
1763 struct interface
*ifp
;
1765 struct nbr_connected
*ifc
;
1767 /* Get interface index. */
1768 ifindex
= stream_getl(s
);
1771 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1773 flog_err(EC_LIB_ZAPI_ENCODE
,
1774 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1775 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1781 p
.family
= stream_getc(s
);
1782 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1783 p
.prefixlen
= stream_getc(s
);
1785 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1786 /* Currently only supporting P2P links, so any new RA source
1788 considered as the replacement of the previously learnt
1789 Link-Local address. */
1790 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1791 ifc
= nbr_connected_new();
1792 ifc
->address
= prefix_new();
1794 listnode_add(ifp
->nbr_connected
, ifc
);
1797 prefix_copy(ifc
->address
, &p
);
1799 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1801 ifc
= nbr_connected_check(ifp
, &p
);
1803 listnode_delete(ifp
->nbr_connected
, ifc
);
1809 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1811 vrf_id_t
*new_vrf_id
)
1813 char ifname
[INTERFACE_NAMSIZ
];
1814 struct interface
*ifp
;
1817 /* Read interface name. */
1818 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
1820 /* Lookup interface. */
1821 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1823 flog_err(EC_LIB_ZAPI_ENCODE
,
1824 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1829 /* Fetch new VRF Id. */
1830 new_id
= stream_getw(s
);
1832 *new_vrf_id
= new_id
;
1836 /* filter unwanted messages until the expected one arrives */
1837 static int zclient_read_sync_response(struct zclient
*zclient
,
1838 uint16_t expected_cmd
)
1850 cmd
= expected_cmd
+ 1;
1851 while (ret
== 0 && cmd
!= expected_cmd
) {
1855 /* wait until response arrives */
1857 FD_SET(zclient
->sock
, &readfds
);
1858 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1859 if (!FD_ISSET(zclient
->sock
, &readfds
))
1862 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1863 &version
, &vrf_id
, &cmd
);
1865 zlog_debug("%s: Response (%d bytes) received", __func__
,
1869 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
1877 * Connect to label manager in a syncronous way
1879 * It first writes the request to zcient output buffer and then
1880 * immediately reads the answer from the input buffer.
1882 * @param zclient Zclient used to connect to label manager (zebra)
1883 * @param async Synchronous (0) or asynchronous (1) operation
1884 * @result Result of response
1886 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
1891 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
1892 ZEBRA_LABEL_MANAGER_CONNECT
;
1895 zlog_debug("Connecting to Label Manager (LM)");
1897 if (zclient
->sock
< 0) {
1898 zlog_debug("%s: invalid zclient socket", __func__
);
1905 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1908 stream_putc(s
, zclient
->redist_default
);
1910 stream_putw(s
, zclient
->instance
);
1912 /* Put length at the first point of the stream. */
1913 stream_putw_at(s
, 0, stream_get_endp(s
));
1915 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1917 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
1918 close(zclient
->sock
);
1923 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
1924 close(zclient
->sock
);
1929 zlog_debug("LM connect request sent (%d bytes)", ret
);
1935 if (zclient_read_sync_response(zclient
, cmd
)
1941 /* read instance and proto */
1942 uint8_t proto
= stream_getc(s
);
1943 uint16_t instance
= stream_getw(s
);
1946 if (proto
!= zclient
->redist_default
)
1949 "Wrong proto (%u) in LM connect response. Should be %u",
1950 proto
, zclient
->redist_default
);
1951 if (instance
!= zclient
->instance
)
1954 "Wrong instId (%u) in LM connect response. Should be %u",
1955 instance
, zclient
->instance
);
1958 result
= stream_getc(s
);
1960 zlog_debug("LM connect-response received, result %u", result
);
1966 * Asynchronous label chunk request
1968 * @param zclient Zclient used to connect to label manager (zebra)
1969 * @param keep Avoid garbage collection
1970 * @param chunk_size Amount of labels requested
1971 * @result 0 on success, -1 otherwise
1973 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1974 uint32_t chunk_size
)
1979 zlog_debug("Getting Label Chunk");
1981 if (zclient
->sock
< 0)
1987 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1989 stream_putc(s
, zclient
->redist_default
);
1991 stream_putw(s
, zclient
->instance
);
1992 stream_putc(s
, keep
);
1993 stream_putl(s
, chunk_size
);
1995 /* Put length at the first point of the stream. */
1996 stream_putw_at(s
, 0, stream_get_endp(s
));
1998 return zclient_send_message(zclient
);
2002 * Function to request a label chunk in a syncronous way
2004 * It first writes the request to zlcient output buffer and then
2005 * immediately reads the answer from the input buffer.
2007 * @param zclient Zclient used to connect to label manager (zebra)
2008 * @param keep Avoid garbage collection
2009 * @param chunk_size Amount of labels requested
2010 * @param start To write first assigned chunk label to
2011 * @param end To write last assigned chunk label to
2012 * @result 0 on success, -1 otherwise
2014 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2015 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2019 uint8_t response_keep
;
2022 zlog_debug("Getting Label Chunk");
2024 if (zclient
->sock
< 0)
2030 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2032 stream_putc(s
, zclient
->redist_default
);
2034 stream_putw(s
, zclient
->instance
);
2036 stream_putc(s
, keep
);
2038 stream_putl(s
, chunk_size
);
2039 /* Put length at the first point of the stream. */
2040 stream_putw_at(s
, 0, stream_get_endp(s
));
2042 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2044 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2045 close(zclient
->sock
);
2050 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2051 close(zclient
->sock
);
2056 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2059 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2062 /* parse response */
2065 /* read proto and instance */
2066 uint8_t proto
= stream_getc(s
);
2067 uint16_t instance
= stream_getw(s
);
2070 if (proto
!= zclient
->redist_default
)
2071 flog_err(EC_LIB_ZAPI_ENCODE
,
2072 "Wrong proto (%u) in get chunk response. Should be %u",
2073 proto
, zclient
->redist_default
);
2074 if (instance
!= zclient
->instance
)
2075 flog_err(EC_LIB_ZAPI_ENCODE
,
2076 "Wrong instId (%u) in get chunk response Should be %u",
2077 instance
, zclient
->instance
);
2080 response_keep
= stream_getc(s
);
2081 /* start and end labels */
2082 *start
= stream_getl(s
);
2083 *end
= stream_getl(s
);
2085 /* not owning this response */
2086 if (keep
!= response_keep
) {
2089 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2090 *start
, *end
, keep
, response_keep
);
2093 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2094 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2095 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2101 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2108 * Function to release a label chunk
2110 * @param zclient Zclient used to connect to label manager (zebra)
2111 * @param start First label of chunk
2112 * @param end Last label of chunk
2113 * @result 0 on success, -1 otherwise
2115 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2122 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2124 if (zclient
->sock
< 0)
2130 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2133 stream_putc(s
, zclient
->redist_default
);
2135 stream_putw(s
, zclient
->instance
);
2137 stream_putl(s
, start
);
2139 stream_putl(s
, end
);
2141 /* Put length at the first point of the stream. */
2142 stream_putw_at(s
, 0, stream_get_endp(s
));
2144 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2146 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2147 close(zclient
->sock
);
2152 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2153 close(zclient
->sock
);
2162 * Connect to table manager in a syncronous way
2164 * It first writes the request to zcient output buffer and then
2165 * immediately reads the answer from the input buffer.
2167 * @param zclient Zclient used to connect to table manager (zebra)
2168 * @result Result of response
2170 int tm_table_manager_connect(struct zclient
*zclient
)
2177 zlog_debug("Connecting to Table Manager");
2179 if (zclient
->sock
< 0)
2185 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2188 stream_putc(s
, zclient
->redist_default
);
2190 stream_putw(s
, zclient
->instance
);
2192 /* Put length at the first point of the stream. */
2193 stream_putw_at(s
, 0, stream_get_endp(s
));
2195 ret
= zclient_send_message(zclient
);
2200 zlog_debug("%s: Table manager connect request sent", __func__
);
2203 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2209 STREAM_GETC(s
, result
);
2212 "%s: Table Manager connect response received, result %u",
2221 * Function to request a table chunk in a syncronous way
2223 * It first writes the request to zclient output buffer and then
2224 * immediately reads the answer from the input buffer.
2226 * @param zclient Zclient used to connect to table manager (zebra)
2227 * @param chunk_size Amount of table requested
2228 * @param start to write first assigned chunk table RT ID to
2229 * @param end To write last assigned chunk table RT ID to
2230 * @result 0 on success, -1 otherwise
2232 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2233 uint32_t *start
, uint32_t *end
)
2239 zlog_debug("Getting Table Chunk");
2241 if (zclient
->sock
< 0)
2247 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2249 stream_putl(s
, chunk_size
);
2250 /* Put length at the first point of the stream. */
2251 stream_putw_at(s
, 0, stream_get_endp(s
));
2253 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2255 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2257 close(zclient
->sock
);
2262 flog_err(EC_LIB_ZAPI_SOCKET
,
2263 "%s: zclient->sock connection closed", __func__
);
2264 close(zclient
->sock
);
2269 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2273 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2277 /* start and end table IDs */
2278 STREAM_GETL(s
, *start
);
2279 STREAM_GETL(s
, *end
);
2282 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2289 * Function to release a table chunk
2291 * @param zclient Zclient used to connect to table manager (zebra)
2292 * @param start First label of table
2293 * @param end Last label of chunk
2294 * @result 0 on success, -1 otherwise
2296 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2302 zlog_debug("Releasing Table Chunk");
2304 if (zclient
->sock
< 0)
2310 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2313 stream_putl(s
, start
);
2315 stream_putl(s
, end
);
2317 /* Put length at the first point of the stream. */
2318 stream_putw_at(s
, 0, stream_get_endp(s
));
2320 return zclient_send_message(zclient
);
2324 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2332 zclient_create_header(s
, command
, VRF_DEFAULT
);
2333 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2334 stream_putl(s
, pw
->ifindex
);
2337 stream_putl(s
, pw
->type
);
2340 stream_putl(s
, pw
->af
);
2343 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2346 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2349 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2354 stream_putl(s
, pw
->local_label
);
2355 stream_putl(s
, pw
->remote_label
);
2358 stream_putc(s
, pw
->flags
);
2360 /* Protocol specific fields */
2361 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2363 /* Put length at the first point of the stream. */
2364 stream_putw_at(s
, 0, stream_get_endp(s
));
2366 return zclient_send_message(zclient
);
2370 * Receive PW status update from Zebra and send it to LDE process.
2372 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2373 zebra_size_t length
, vrf_id_t vrf_id
,
2374 struct zapi_pw_status
*pw
)
2378 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2382 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2383 pw
->ifindex
= stream_getl(s
);
2384 pw
->status
= stream_getl(s
);
2387 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2388 zebra_size_t length
, vrf_id_t vrf_id
)
2390 struct zclient_capabilities cap
;
2391 struct stream
*s
= zclient
->ibuf
;
2393 uint8_t mpls_enabled
;
2395 STREAM_GETL(s
, vrf_backend
);
2396 vrf_configure_backend(vrf_backend
);
2398 memset(&cap
, 0, sizeof(cap
));
2399 STREAM_GETC(s
, mpls_enabled
);
2400 cap
.mpls_enabled
= !!mpls_enabled
;
2401 STREAM_GETL(s
, cap
.ecmp
);
2402 STREAM_GETC(s
, cap
.role
);
2404 if (zclient
->zebra_capabilities
)
2405 (*zclient
->zebra_capabilities
)(&cap
);
2411 /* Zebra client message read function. */
2412 static int zclient_read(struct thread
*thread
)
2415 uint16_t length
, command
;
2416 uint8_t marker
, version
;
2418 struct zclient
*zclient
;
2420 /* Get socket to zebra. */
2421 zclient
= THREAD_ARG(thread
);
2422 zclient
->t_read
= NULL
;
2424 /* Read zebra header (if we don't have it already). */
2425 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2427 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2428 ZEBRA_HEADER_SIZE
- already
))
2433 "zclient connection closed socket [%d].",
2435 return zclient_failed(zclient
);
2437 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2438 /* Try again later. */
2439 zclient_event(ZCLIENT_READ
, zclient
);
2442 already
= ZEBRA_HEADER_SIZE
;
2445 /* Reset to read from the beginning of the incoming packet. */
2446 stream_set_getp(zclient
->ibuf
, 0);
2448 /* Fetch header values. */
2449 length
= stream_getw(zclient
->ibuf
);
2450 marker
= stream_getc(zclient
->ibuf
);
2451 version
= stream_getc(zclient
->ibuf
);
2452 vrf_id
= stream_getl(zclient
->ibuf
);
2453 command
= stream_getw(zclient
->ibuf
);
2455 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2457 EC_LIB_ZAPI_MISSMATCH
,
2458 "%s: socket %d version mismatch, marker %d, version %d",
2459 __func__
, zclient
->sock
, marker
, version
);
2460 return zclient_failed(zclient
);
2463 if (length
< ZEBRA_HEADER_SIZE
) {
2464 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2465 "%s: socket %d message length %u is less than %d ",
2466 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2467 return zclient_failed(zclient
);
2471 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2475 "%s: message size %u exceeds buffer size %lu, expanding...",
2477 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2478 ns
= stream_new(length
);
2479 stream_copy(ns
, zclient
->ibuf
);
2480 stream_free(zclient
->ibuf
);
2484 /* Read rest of zebra packet. */
2485 if (already
< length
) {
2487 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2493 "zclient connection closed socket [%d].",
2495 return zclient_failed(zclient
);
2497 if (nbyte
!= (ssize_t
)(length
- already
)) {
2498 /* Try again later. */
2499 zclient_event(ZCLIENT_READ
, zclient
);
2504 length
-= ZEBRA_HEADER_SIZE
;
2507 zlog_debug("zclient 0x%p command %s VRF %u",
2508 (void *)zclient
, zserv_command_string(command
),
2512 case ZEBRA_CAPABILITIES
:
2513 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2515 case ZEBRA_ROUTER_ID_UPDATE
:
2516 if (zclient
->router_id_update
)
2517 (*zclient
->router_id_update
)(command
, zclient
, length
,
2521 zclient_vrf_add(zclient
, vrf_id
);
2523 case ZEBRA_VRF_DELETE
:
2524 zclient_vrf_delete(zclient
, vrf_id
);
2526 case ZEBRA_INTERFACE_ADD
:
2527 if (zclient
->interface_add
)
2528 (*zclient
->interface_add
)(command
, zclient
, length
,
2531 case ZEBRA_INTERFACE_DELETE
:
2532 if (zclient
->interface_delete
)
2533 (*zclient
->interface_delete
)(command
, zclient
, length
,
2536 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2537 if (zclient
->interface_address_add
)
2538 (*zclient
->interface_address_add
)(command
, zclient
,
2541 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2542 if (zclient
->interface_address_delete
)
2543 (*zclient
->interface_address_delete
)(command
, zclient
,
2546 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2547 if (zclient
->interface_bfd_dest_update
)
2548 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2551 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2552 if (zclient
->interface_nbr_address_add
)
2553 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2556 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2557 if (zclient
->interface_nbr_address_delete
)
2558 (*zclient
->interface_nbr_address_delete
)(
2559 command
, zclient
, length
, vrf_id
);
2561 case ZEBRA_INTERFACE_UP
:
2562 if (zclient
->interface_up
)
2563 (*zclient
->interface_up
)(command
, zclient
, length
,
2566 case ZEBRA_INTERFACE_DOWN
:
2567 if (zclient
->interface_down
)
2568 (*zclient
->interface_down
)(command
, zclient
, length
,
2571 case ZEBRA_INTERFACE_VRF_UPDATE
:
2572 if (zclient
->interface_vrf_update
)
2573 (*zclient
->interface_vrf_update
)(command
, zclient
,
2576 case ZEBRA_NEXTHOP_UPDATE
:
2578 zlog_debug("zclient rcvd nexthop update");
2579 if (zclient
->nexthop_update
)
2580 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2583 case ZEBRA_IMPORT_CHECK_UPDATE
:
2585 zlog_debug("zclient rcvd import check update");
2586 if (zclient
->import_check_update
)
2587 (*zclient
->import_check_update
)(command
, zclient
,
2590 case ZEBRA_BFD_DEST_REPLAY
:
2591 if (zclient
->bfd_dest_replay
)
2592 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2595 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2596 if (zclient
->redistribute_route_add
)
2597 (*zclient
->redistribute_route_add
)(command
, zclient
,
2600 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2601 if (zclient
->redistribute_route_del
)
2602 (*zclient
->redistribute_route_del
)(command
, zclient
,
2605 case ZEBRA_INTERFACE_LINK_PARAMS
:
2606 if (zclient
->interface_link_params
)
2607 (*zclient
->interface_link_params
)(command
, zclient
,
2610 case ZEBRA_FEC_UPDATE
:
2612 zlog_debug("zclient rcvd fec update");
2613 if (zclient
->fec_update
)
2614 (*zclient
->fec_update
)(command
, zclient
, length
);
2616 case ZEBRA_LOCAL_ES_ADD
:
2617 if (zclient
->local_es_add
)
2618 (*zclient
->local_es_add
)(command
, zclient
, length
,
2621 case ZEBRA_LOCAL_ES_DEL
:
2622 if (zclient
->local_es_del
)
2623 (*zclient
->local_es_del
)(command
, zclient
, length
,
2627 if (zclient
->local_vni_add
)
2628 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2632 if (zclient
->local_vni_del
)
2633 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2636 case ZEBRA_L3VNI_ADD
:
2637 if (zclient
->local_l3vni_add
)
2638 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2641 case ZEBRA_L3VNI_DEL
:
2642 if (zclient
->local_l3vni_del
)
2643 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2646 case ZEBRA_MACIP_ADD
:
2647 if (zclient
->local_macip_add
)
2648 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2651 case ZEBRA_MACIP_DEL
:
2652 if (zclient
->local_macip_del
)
2653 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2656 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2657 if (zclient
->local_ip_prefix_add
)
2658 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2661 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2662 if (zclient
->local_ip_prefix_del
)
2663 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2666 case ZEBRA_PW_STATUS_UPDATE
:
2667 if (zclient
->pw_status_update
)
2668 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2671 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2672 if (zclient
->route_notify_owner
)
2673 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2676 case ZEBRA_RULE_NOTIFY_OWNER
:
2677 if (zclient
->rule_notify_owner
)
2678 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2681 case ZEBRA_GET_LABEL_CHUNK
:
2682 if (zclient
->label_chunk
)
2683 (*zclient
->label_chunk
)(command
, zclient
, length
,
2686 case ZEBRA_IPSET_NOTIFY_OWNER
:
2687 if (zclient
->ipset_notify_owner
)
2688 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2691 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2692 if (zclient
->ipset_entry_notify_owner
)
2693 (*zclient
->ipset_entry_notify_owner
)(command
,
2697 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2698 if (zclient
->iptable_notify_owner
)
2699 (*zclient
->iptable_notify_owner
)(command
,
2706 if (zclient
->sock
< 0)
2707 /* Connection was closed during packet processing. */
2710 /* Register read thread. */
2711 stream_reset(zclient
->ibuf
);
2712 zclient_event(ZCLIENT_READ
, zclient
);
2717 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2718 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2722 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2723 if (redist_check_instance(
2724 &zclient
->mi_redist
[afi
][type
], instance
))
2726 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2729 if (!redist_check_instance(
2730 &zclient
->mi_redist
[afi
][type
], instance
))
2732 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2737 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2738 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2741 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2743 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2746 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2750 if (zclient
->sock
> 0)
2751 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2756 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2757 afi_t afi
, vrf_id_t vrf_id
)
2760 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2761 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
2763 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
2765 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
2768 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
2771 if (zclient
->sock
> 0)
2772 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
2775 static void zclient_event(enum event event
, struct zclient
*zclient
)
2778 case ZCLIENT_SCHEDULE
:
2779 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2780 &zclient
->t_connect
);
2782 case ZCLIENT_CONNECT
:
2785 "zclient connect failures: %d schedule interval is now %d",
2786 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2787 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2788 zclient
->fail
< 3 ? 10 : 60,
2789 &zclient
->t_connect
);
2792 zclient
->t_read
= NULL
;
2793 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2794 zclient
->sock
, &zclient
->t_read
);
2799 void zclient_interface_set_master(struct zclient
*client
,
2800 struct interface
*master
,
2801 struct interface
*slave
)
2808 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2810 stream_putl(s
, master
->vrf_id
);
2811 stream_putl(s
, master
->ifindex
);
2812 stream_putl(s
, slave
->vrf_id
);
2813 stream_putl(s
, slave
->ifindex
);
2815 stream_putw_at(s
, 0, stream_get_endp(s
));
2816 zclient_send_message(client
);