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
],
178 vrf_bitmap_free(zclient
->default_information
);
179 zclient
->default_information
= 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 /* We need interface information. */
418 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
420 /* Set unwanted redistribute route. */
421 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
422 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
425 /* Flush all redistribute request. */
426 if (vrf_id
== VRF_DEFAULT
) {
427 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
428 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
429 if (!zclient
->mi_redist
[afi
][i
].enabled
)
432 struct listnode
*node
;
435 for (ALL_LIST_ELEMENTS_RO(
436 zclient
->mi_redist
[afi
][i
]
439 if (!(i
== zclient
->redist_default
440 && *id
== zclient
->instance
))
441 zebra_redistribute_send(
442 ZEBRA_REDISTRIBUTE_ADD
,
443 zclient
, afi
, i
, *id
,
449 /* Resend all redistribute request. */
450 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
451 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
452 if (i
!= zclient
->redist_default
453 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
455 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
459 /* If default information is needed. */
460 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
461 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
465 /* Send unregister requests to zebra daemon for the information in a VRF. */
466 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
471 /* If not connected to the zebra yet. */
472 if (zclient
->sock
< 0)
476 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
479 /* We need router-id information. */
480 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
482 /* We need interface information. */
483 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
485 /* Set unwanted redistribute route. */
486 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
487 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
490 /* Flush all redistribute request. */
491 if (vrf_id
== VRF_DEFAULT
) {
492 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
493 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
494 if (!zclient
->mi_redist
[afi
][i
].enabled
)
497 struct listnode
*node
;
500 for (ALL_LIST_ELEMENTS_RO(
501 zclient
->mi_redist
[afi
][i
]
504 if (!(i
== zclient
->redist_default
505 && *id
== zclient
->instance
))
506 zebra_redistribute_send(
507 ZEBRA_REDISTRIBUTE_DELETE
,
508 zclient
, afi
, i
, *id
,
514 /* Flush all redistribute request. */
515 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
516 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
517 if (i
!= zclient
->redist_default
518 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
520 zebra_redistribute_send(
521 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
524 /* If default information is needed. */
525 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
526 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
530 /* Send request to zebra daemon to start or stop RA. */
531 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
532 struct interface
*ifp
, int enable
,
537 /* If not connected to the zebra yet. */
538 if (zclient
->sock
< 0)
541 /* Form and send message. */
546 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
548 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
550 stream_putl(s
, ifp
->ifindex
);
551 stream_putl(s
, ra_interval
);
553 stream_putw_at(s
, 0, stream_get_endp(s
));
555 zclient_send_message(zclient
);
558 /* Make connection to zebra daemon. */
559 int zclient_start(struct zclient
*zclient
)
562 zlog_info("zclient_start is called");
564 /* If already connected to the zebra. */
565 if (zclient
->sock
>= 0)
568 /* Check connect thread. */
569 if (zclient
->t_connect
)
572 if (zclient_socket_connect(zclient
) < 0) {
574 zlog_debug("zclient connection fail");
576 zclient_event(ZCLIENT_CONNECT
, zclient
);
580 if (set_nonblocking(zclient
->sock
) < 0)
581 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
582 __func__
, zclient
->sock
);
584 /* Clear fail count. */
587 zlog_debug("zclient connect success with socket [%d]",
590 /* Create read thread. */
591 zclient_event(ZCLIENT_READ
, zclient
);
593 zebra_hello_send(zclient
);
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
= vrf_bitmap_init();
631 zlog_debug("zclient_start is called");
633 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
636 /* This function is a wrapper function for calling zclient_start from
637 timer or event thread. */
638 static int zclient_connect(struct thread
*t
)
640 struct zclient
*zclient
;
642 zclient
= THREAD_ARG(t
);
643 zclient
->t_connect
= NULL
;
646 zlog_debug("zclient_connect is called");
648 return zclient_start(zclient
);
651 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
652 bool exact_match
, vrf_id_t vrf_id
)
658 zclient_create_header(s
, command
, vrf_id
);
659 stream_putc(s
, (exact_match
) ? 1 : 0);
661 stream_putw(s
, PREFIX_FAMILY(p
));
662 stream_putc(s
, p
->prefixlen
);
663 switch (PREFIX_FAMILY(p
)) {
665 stream_put_in_addr(s
, &p
->u
.prefix4
);
668 stream_put(s
, &(p
->u
.prefix6
), 16);
673 stream_putw_at(s
, 0, stream_get_endp(s
));
675 return zclient_send_message(zclient
);
679 * "xdr_encode"-like interface that allows daemon (client) to send
680 * a message to zebra server for a route that needs to be
681 * added/deleted to the kernel. Info about the route is specified
682 * by the caller in a struct zapi_route. zapi_route_encode() then writes
683 * the info down the zclient socket using the stream_* functions.
685 * The corresponding read ("xdr_decode") function on the server
686 * side is zapi_route_decode().
688 * 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
689 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * | Length (2) | Command | Route Type |
691 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
692 * | ZEBRA Flags | Message Flags | Prefix length |
693 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
694 * | Destination IPv4 Prefix for route |
695 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
701 * described, as per the Nexthop count. Each nexthop described as:
704 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
705 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 * | IPv4 Nexthop address or Interface Index number |
707 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709 * Alternatively, if the route is a blackhole route, then Nexthop count
710 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
713 * The original struct zapi_route_*() infrastructure was built around
714 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
715 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
716 * nexthops into zapi_route_encode() using the same zapi_route structure.
717 * This is done by setting zapi_route fields as follows:
718 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
719 * - .nexthop_num == .ifindex_num
720 * - .nexthop and .ifindex are filled with gate and ifindex parts of
721 * each compound nexthop, both in the same order
723 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
726 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
729 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
731 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
733 * XXX: No attention paid to alignment.
735 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
736 struct zapi_route
*api
)
738 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
740 return zclient_send_message(zclient
);
743 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
745 struct zapi_nexthop
*api_nh
;
750 zclient_create_header(s
, cmd
, api
->vrf_id
);
752 stream_putc(s
, api
->type
);
753 stream_putw(s
, api
->instance
);
754 stream_putl(s
, api
->flags
);
755 stream_putc(s
, api
->message
);
756 stream_putc(s
, api
->safi
);
758 /* Put prefix information. */
759 stream_putc(s
, api
->prefix
.family
);
760 psize
= PSIZE(api
->prefix
.prefixlen
);
761 stream_putc(s
, api
->prefix
.prefixlen
);
762 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
764 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
765 psize
= PSIZE(api
->src_prefix
.prefixlen
);
766 stream_putc(s
, api
->src_prefix
.prefixlen
);
767 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
771 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
772 /* limit the number of nexthops if necessary */
773 if (api
->nexthop_num
> MULTIPATH_NUM
) {
774 char buf
[PREFIX2STR_BUFFER
];
776 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
779 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
780 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
784 stream_putw(s
, api
->nexthop_num
);
786 for (i
= 0; i
< api
->nexthop_num
; i
++) {
787 api_nh
= &api
->nexthops
[i
];
789 stream_putl(s
, api_nh
->vrf_id
);
790 stream_putc(s
, api_nh
->type
);
791 switch (api_nh
->type
) {
792 case NEXTHOP_TYPE_BLACKHOLE
:
793 stream_putc(s
, api_nh
->bh_type
);
795 case NEXTHOP_TYPE_IPV4
:
796 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
798 case NEXTHOP_TYPE_IPV4_IFINDEX
:
799 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
800 stream_putl(s
, api_nh
->ifindex
);
802 case NEXTHOP_TYPE_IFINDEX
:
803 stream_putl(s
, api_nh
->ifindex
);
805 case NEXTHOP_TYPE_IPV6
:
806 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
809 case NEXTHOP_TYPE_IPV6_IFINDEX
:
810 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
812 stream_putl(s
, api_nh
->ifindex
);
816 /* MPLS labels for BGP-LU or Segment Routing */
817 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
818 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
819 char buf
[PREFIX2STR_BUFFER
];
820 prefix2str(&api
->prefix
, buf
,
822 flog_err(EC_LIB_ZAPI_ENCODE
,
823 "%s: prefix %s: can't encode "
824 "%u labels (maximum is %u)",
831 stream_putc(s
, api_nh
->label_num
);
832 stream_put(s
, &api_nh
->labels
[0],
834 * sizeof(mpls_label_t
));
837 /* Router MAC for EVPN routes. */
838 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
839 stream_put(s
, &(api_nh
->rmac
),
840 sizeof(struct ethaddr
));
845 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
846 stream_putc(s
, api
->distance
);
847 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
848 stream_putl(s
, api
->metric
);
849 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
850 stream_putl(s
, api
->tag
);
851 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
852 stream_putl(s
, api
->mtu
);
853 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
854 stream_putl(s
, api
->tableid
);
856 /* Put length at the first point of the stream. */
857 stream_putw_at(s
, 0, stream_get_endp(s
));
862 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
864 struct zapi_nexthop
*api_nh
;
867 memset(api
, 0, sizeof(*api
));
869 /* Type, flags, message. */
870 STREAM_GETC(s
, api
->type
);
871 if (api
->type
> ZEBRA_ROUTE_MAX
) {
872 flog_err(EC_LIB_ZAPI_ENCODE
,
873 "%s: Specified route type: %d is not a legal value\n",
874 __PRETTY_FUNCTION__
, api
->type
);
878 STREAM_GETW(s
, api
->instance
);
879 STREAM_GETL(s
, api
->flags
);
880 STREAM_GETC(s
, api
->message
);
881 STREAM_GETC(s
, api
->safi
);
884 STREAM_GETC(s
, api
->prefix
.family
);
885 STREAM_GETC(s
, api
->prefix
.prefixlen
);
886 switch (api
->prefix
.family
) {
888 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
891 "%s: V4 prefixlen is %d which should not be more than 32",
892 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
897 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
900 "%s: v6 prefixlen is %d which should not be more than 128",
901 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
906 flog_err(EC_LIB_ZAPI_ENCODE
,
907 "%s: Specified family %d is not v4 or v6",
908 __PRETTY_FUNCTION__
, api
->prefix
.family
);
911 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
913 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
914 api
->src_prefix
.family
= AF_INET6
;
915 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
916 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
919 "%s: SRC Prefix prefixlen received: %d is too large",
920 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
923 STREAM_GET(&api
->src_prefix
.prefix
, s
,
924 PSIZE(api
->src_prefix
.prefixlen
));
926 if (api
->prefix
.family
!= AF_INET6
927 || api
->src_prefix
.prefixlen
== 0) {
930 "%s: SRC prefix specified in some manner that makes no sense",
931 __PRETTY_FUNCTION__
);
937 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
938 STREAM_GETW(s
, api
->nexthop_num
);
939 if (api
->nexthop_num
> MULTIPATH_NUM
) {
940 flog_err(EC_LIB_ZAPI_ENCODE
,
941 "%s: invalid number of nexthops (%u)",
942 __func__
, api
->nexthop_num
);
946 for (i
= 0; i
< api
->nexthop_num
; i
++) {
947 api_nh
= &api
->nexthops
[i
];
949 STREAM_GETL(s
, api_nh
->vrf_id
);
950 STREAM_GETC(s
, api_nh
->type
);
951 switch (api_nh
->type
) {
952 case NEXTHOP_TYPE_BLACKHOLE
:
953 STREAM_GETC(s
, api_nh
->bh_type
);
955 case NEXTHOP_TYPE_IPV4
:
956 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
959 case NEXTHOP_TYPE_IPV4_IFINDEX
:
960 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
962 STREAM_GETL(s
, api_nh
->ifindex
);
964 case NEXTHOP_TYPE_IFINDEX
:
965 STREAM_GETL(s
, api_nh
->ifindex
);
967 case NEXTHOP_TYPE_IPV6
:
968 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
970 case NEXTHOP_TYPE_IPV6_IFINDEX
:
971 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
972 STREAM_GETL(s
, api_nh
->ifindex
);
976 /* MPLS labels for BGP-LU or Segment Routing */
977 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
978 STREAM_GETC(s
, api_nh
->label_num
);
980 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
983 "%s: invalid number of MPLS labels (%u)",
984 __func__
, api_nh
->label_num
);
988 STREAM_GET(&api_nh
->labels
[0], s
,
990 * sizeof(mpls_label_t
));
993 /* Router MAC for EVPN routes. */
994 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
995 stream_get(&(api_nh
->rmac
), s
,
996 sizeof(struct ethaddr
));
1001 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1002 STREAM_GETC(s
, api
->distance
);
1003 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1004 STREAM_GETL(s
, api
->metric
);
1005 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1006 STREAM_GETL(s
, api
->tag
);
1007 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1008 STREAM_GETL(s
, api
->mtu
);
1009 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1010 STREAM_GETL(s
, api
->tableid
);
1016 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1022 memset(&any
, 0, sizeof(any
));
1023 any
.family
= family
;
1027 stream_putc(s
, p
->family
);
1028 stream_putc(s
, p
->prefixlen
);
1029 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1032 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1035 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1038 * We are sending one item at a time at the moment
1042 stream_putl(s
, zrule
->seq
);
1043 stream_putl(s
, zrule
->priority
);
1044 stream_putl(s
, zrule
->unique
);
1046 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1047 zrule
->filter
.src_ip
.family
);
1048 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1049 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1050 zrule
->filter
.src_ip
.family
);
1051 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1052 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1054 stream_putl(s
, zrule
->action
.table
);
1055 stream_putl(s
, zrule
->ifindex
);
1057 /* Put length at the first point of the stream. */
1058 stream_putw_at(s
, 0, stream_get_endp(s
));
1063 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1065 enum zapi_route_notify_owner
*note
)
1069 STREAM_GET(note
, s
, sizeof(*note
));
1071 STREAM_GETC(s
, p
->family
);
1072 STREAM_GETC(s
, p
->prefixlen
);
1073 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1084 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1085 uint32_t *priority
, uint32_t *unique
,
1087 enum zapi_rule_notify_owner
*note
)
1089 uint32_t prio
, seq
, uni
;
1092 STREAM_GET(note
, s
, sizeof(*note
));
1094 STREAM_GETL(s
, seq
);
1095 STREAM_GETL(s
, prio
);
1096 STREAM_GETL(s
, uni
);
1097 STREAM_GETL(s
, ifi
);
1100 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1113 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1114 enum zapi_ipset_notify_owner
*note
)
1118 STREAM_GET(note
, s
, sizeof(*note
));
1120 STREAM_GETL(s
, uni
);
1123 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1132 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1134 enum zapi_ipset_entry_notify_owner
*note
)
1138 STREAM_GET(note
, s
, sizeof(*note
));
1140 STREAM_GETL(s
, uni
);
1142 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1145 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1154 bool zapi_iptable_notify_decode(struct stream
*s
,
1156 enum zapi_iptable_notify_owner
*note
)
1160 STREAM_GET(note
, s
, sizeof(*note
));
1162 STREAM_GETL(s
, uni
);
1165 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1174 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1176 struct nexthop
*n
= nexthop_new();
1178 n
->type
= znh
->type
;
1179 n
->vrf_id
= znh
->vrf_id
;
1180 n
->ifindex
= znh
->ifindex
;
1181 n
->gate
= znh
->gate
;
1184 * This function currently handles labels
1186 if (znh
->label_num
) {
1187 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1194 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1198 memset(nhr
, 0, sizeof(*nhr
));
1200 STREAM_GETW(s
, nhr
->prefix
.family
);
1201 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1202 switch (nhr
->prefix
.family
) {
1204 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1207 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1213 STREAM_GETC(s
, nhr
->type
);
1214 STREAM_GETW(s
, nhr
->instance
);
1215 STREAM_GETC(s
, nhr
->distance
);
1216 STREAM_GETL(s
, nhr
->metric
);
1217 STREAM_GETC(s
, nhr
->nexthop_num
);
1219 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1220 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1221 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1222 switch (nhr
->nexthops
[i
].type
) {
1223 case NEXTHOP_TYPE_IPV4
:
1224 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1225 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1227 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1229 case NEXTHOP_TYPE_IFINDEX
:
1230 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1232 case NEXTHOP_TYPE_IPV6
:
1233 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1234 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1236 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1238 case NEXTHOP_TYPE_BLACKHOLE
:
1241 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1242 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1243 flog_err(EC_LIB_ZAPI_ENCODE
,
1244 "%s: invalid number of MPLS labels (%u)",
1245 __func__
, nhr
->nexthops
[i
].label_num
);
1248 if (nhr
->nexthops
[i
].label_num
)
1249 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1250 nhr
->nexthops
[i
].label_num
1251 * sizeof(mpls_label_t
));
1260 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1261 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1262 * then set/unset redist[type] in the client handle (a struct zserv) for the
1265 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1266 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1273 zclient_create_header(s
, command
, vrf_id
);
1274 stream_putc(s
, afi
);
1275 stream_putc(s
, type
);
1276 stream_putw(s
, instance
);
1278 stream_putw_at(s
, 0, stream_get_endp(s
));
1280 return zclient_send_message(zclient
);
1283 /* Get prefix in ZServ format; family should be filled in on prefix */
1284 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1286 size_t plen
= prefix_blen(p
);
1293 stream_get(&p
->u
.prefix
, s
, plen
);
1295 p
->prefixlen
= MIN(plen
* 8, c
);
1301 /* Router-id update from zebra daemon. */
1302 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1304 /* Fetch interface address. */
1305 STREAM_GETC(s
, rid
->family
);
1307 zclient_stream_get_prefix(s
, rid
);
1313 /* Interface addition from zebra daemon. */
1315 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1316 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1318 * 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
1319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1325 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1327 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1329 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1332 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1334 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1336 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1338 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1340 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1342 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1343 * | Link Layer Type |
1344 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1345 * | Harware Address Length |
1346 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1347 * | Hardware Address if HW lenght different from 0 |
1348 * | ... max INTERFACE_HWADDR_MAX |
1349 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1350 * | Link_params? | Whether a link-params follows: 1 or 0.
1351 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1352 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1353 * | .... (struct if_link_params). |
1354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1357 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1360 char vrfname_tmp
[VRF_NAMSIZ
];
1361 struct vrf_data data
;
1363 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1364 /* Read interface name. */
1365 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1367 /* Lookup/create vrf by vrf_id. */
1368 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1369 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1370 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1371 /* overwrite default vrf */
1372 if (vrf_id
== VRF_DEFAULT
)
1373 vrf_set_default_name(vrfname_tmp
);
1377 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1381 /* Lookup vrf by vrf_id. */
1382 vrf
= vrf_lookup_by_id(vrf_id
);
1385 * If a routing protocol doesn't know about a
1386 * vrf that is about to be deleted. There is
1387 * no point in attempting to delete it.
1395 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1397 struct interface
*ifp
;
1398 char ifname_tmp
[INTERFACE_NAMSIZ
];
1400 /* Read interface name. */
1401 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1403 /* Lookup/create interface by name. */
1404 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1406 zebra_interface_if_set_value(s
, ifp
);
1412 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1413 * from zebra server. The format of this message is the same as
1414 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1415 * comments for zebra_interface_add_read), except that no sockaddr_dl
1416 * is sent at the tail of the message.
1418 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1420 struct interface
*ifp
;
1421 char ifname_tmp
[INTERFACE_NAMSIZ
];
1423 /* Read interface name. */
1424 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1426 /* Lookup this by interface index. */
1427 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1429 flog_err(EC_LIB_ZAPI_ENCODE
,
1430 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1431 ifname_tmp
, vrf_id
);
1435 zebra_interface_if_set_value(s
, ifp
);
1440 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1446 iflp
->lp_status
= stream_getl(s
);
1447 iflp
->te_metric
= stream_getl(s
);
1448 iflp
->max_bw
= stream_getf(s
);
1449 iflp
->max_rsv_bw
= stream_getf(s
);
1450 uint32_t bwclassnum
= stream_getl(s
);
1453 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1454 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1457 EC_LIB_ZAPI_MISSMATCH
,
1458 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1459 " - outdated library?",
1460 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1462 iflp
->admin_grp
= stream_getl(s
);
1463 iflp
->rmt_as
= stream_getl(s
);
1464 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1466 iflp
->av_delay
= stream_getl(s
);
1467 iflp
->min_delay
= stream_getl(s
);
1468 iflp
->max_delay
= stream_getl(s
);
1469 iflp
->delay_var
= stream_getl(s
);
1471 iflp
->pkt_loss
= stream_getf(s
);
1472 iflp
->res_bw
= stream_getf(s
);
1473 iflp
->ava_bw
= stream_getf(s
);
1474 iflp
->use_bw
= stream_getf(s
);
1477 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1479 struct if_link_params
*iflp
;
1484 ifindex
= stream_getl(s
);
1486 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1489 flog_err(EC_LIB_ZAPI_ENCODE
,
1490 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1495 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1498 link_params_set_value(s
, iflp
);
1503 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1505 uint8_t link_params_status
= 0;
1506 ifindex_t old_ifindex
;
1508 old_ifindex
= ifp
->ifindex
;
1509 /* Read interface's index. */
1510 if_set_index(ifp
, stream_getl(s
));
1511 ifp
->status
= stream_getc(s
);
1513 /* Read interface's value. */
1514 ifp
->flags
= stream_getq(s
);
1515 ifp
->ptm_enable
= stream_getc(s
);
1516 ifp
->ptm_status
= stream_getc(s
);
1517 ifp
->metric
= stream_getl(s
);
1518 ifp
->speed
= stream_getl(s
);
1519 ifp
->mtu
= stream_getl(s
);
1520 ifp
->mtu6
= stream_getl(s
);
1521 ifp
->bandwidth
= stream_getl(s
);
1522 ifp
->ll_type
= stream_getl(s
);
1523 ifp
->hw_addr_len
= stream_getl(s
);
1524 if (ifp
->hw_addr_len
)
1525 stream_get(ifp
->hw_addr
, s
,
1526 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1528 /* Read Traffic Engineering status */
1529 link_params_status
= stream_getc(s
);
1530 /* Then, Traffic Engineering parameters if any */
1531 if (link_params_status
) {
1532 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1533 link_params_set_value(s
, iflp
);
1536 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1539 size_t zebra_interface_link_params_write(struct stream
*s
,
1540 struct interface
*ifp
)
1543 struct if_link_params
*iflp
;
1546 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1549 iflp
= ifp
->link_params
;
1552 w
+= stream_putl(s
, iflp
->lp_status
);
1554 w
+= stream_putl(s
, iflp
->te_metric
);
1555 w
+= stream_putf(s
, iflp
->max_bw
);
1556 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1558 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1559 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1560 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1562 w
+= stream_putl(s
, iflp
->admin_grp
);
1563 w
+= stream_putl(s
, iflp
->rmt_as
);
1564 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1566 w
+= stream_putl(s
, iflp
->av_delay
);
1567 w
+= stream_putl(s
, iflp
->min_delay
);
1568 w
+= stream_putl(s
, iflp
->max_delay
);
1569 w
+= stream_putl(s
, iflp
->delay_var
);
1571 w
+= stream_putf(s
, iflp
->pkt_loss
);
1572 w
+= stream_putf(s
, iflp
->res_bw
);
1573 w
+= stream_putf(s
, iflp
->ava_bw
);
1574 w
+= stream_putf(s
, iflp
->use_bw
);
1580 * format of message for address additon is:
1584 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1585 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1594 * | ifc_flags | flags for connected address
1602 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1610 static int memconstant(const void *s
, int c
, size_t n
)
1612 const uint8_t *p
= s
;
1621 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1625 struct interface
*ifp
;
1626 struct connected
*ifc
;
1627 struct prefix p
, d
, *dp
;
1631 memset(&p
, 0, sizeof(p
));
1632 memset(&d
, 0, sizeof(d
));
1634 /* Get interface index. */
1635 ifindex
= stream_getl(s
);
1638 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1640 flog_err(EC_LIB_ZAPI_ENCODE
,
1641 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1642 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1648 ifc_flags
= stream_getc(s
);
1650 /* Fetch interface address. */
1651 d
.family
= p
.family
= stream_getc(s
);
1652 plen
= prefix_blen(&d
);
1654 zclient_stream_get_prefix(s
, &p
);
1656 /* Fetch destination address. */
1657 stream_get(&d
.u
.prefix
, s
, plen
);
1659 /* N.B. NULL destination pointers are encoded as all zeroes */
1660 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1662 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1663 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1665 /* N.B. NULL destination pointers are encoded as all
1667 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1670 ifc
->flags
= ifc_flags
;
1671 if (ifc
->destination
)
1672 ifc
->destination
->prefixlen
=
1673 ifc
->address
->prefixlen
;
1674 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1675 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1677 char buf
[PREFIX_STRLEN
];
1680 "warning: interface %s address %s with peer flag set, but no peer address!",
1682 prefix2str(ifc
->address
, buf
,
1684 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1688 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1689 ifc
= connected_delete_by_prefix(ifp
, &p
);
1696 * format of message for neighbor connected address is:
1700 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1701 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1716 * | addr_len | len of addr.
1719 struct nbr_connected
*
1720 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1722 unsigned int ifindex
;
1723 struct interface
*ifp
;
1725 struct nbr_connected
*ifc
;
1727 /* Get interface index. */
1728 ifindex
= stream_getl(s
);
1731 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1733 flog_err(EC_LIB_ZAPI_ENCODE
,
1734 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1735 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1741 p
.family
= stream_getc(s
);
1742 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1743 p
.prefixlen
= stream_getc(s
);
1745 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1746 /* Currently only supporting P2P links, so any new RA source
1748 considered as the replacement of the previously learnt
1749 Link-Local address. */
1750 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1751 ifc
= nbr_connected_new();
1752 ifc
->address
= prefix_new();
1754 listnode_add(ifp
->nbr_connected
, ifc
);
1757 prefix_copy(ifc
->address
, &p
);
1759 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1761 ifc
= nbr_connected_check(ifp
, &p
);
1763 listnode_delete(ifp
->nbr_connected
, ifc
);
1769 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1771 vrf_id_t
*new_vrf_id
)
1773 unsigned int ifindex
;
1774 struct interface
*ifp
;
1777 /* Get interface index. */
1778 ifindex
= stream_getl(s
);
1780 /* Lookup interface. */
1781 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1783 flog_err(EC_LIB_ZAPI_ENCODE
,
1784 "INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1789 /* Fetch new VRF Id. */
1790 new_id
= stream_getw(s
);
1792 *new_vrf_id
= new_id
;
1796 /* filter unwanted messages until the expected one arrives */
1797 static int zclient_read_sync_response(struct zclient
*zclient
,
1798 uint16_t expected_cmd
)
1810 cmd
= expected_cmd
+ 1;
1811 while (ret
== 0 && cmd
!= expected_cmd
) {
1815 /* wait until response arrives */
1817 FD_SET(zclient
->sock
, &readfds
);
1818 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1819 if (!FD_ISSET(zclient
->sock
, &readfds
))
1822 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1823 &version
, &vrf_id
, &cmd
);
1825 zlog_debug("%s: Response (%d bytes) received", __func__
,
1829 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
1837 * Connect to label manager in a syncronous way
1839 * It first writes the request to zcient output buffer and then
1840 * immediately reads the answer from the input buffer.
1842 * @param zclient Zclient used to connect to label manager (zebra)
1843 * @param async Synchronous (0) or asynchronous (1) operation
1844 * @result Result of response
1846 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
1851 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
1852 ZEBRA_LABEL_MANAGER_CONNECT
;
1855 zlog_debug("Connecting to Label Manager (LM)");
1857 if (zclient
->sock
< 0) {
1858 zlog_debug("%s: invalid zclient socket", __func__
);
1865 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1868 stream_putc(s
, zclient
->redist_default
);
1870 stream_putw(s
, zclient
->instance
);
1872 /* Put length at the first point of the stream. */
1873 stream_putw_at(s
, 0, stream_get_endp(s
));
1875 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1877 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
1878 close(zclient
->sock
);
1883 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
1884 close(zclient
->sock
);
1889 zlog_debug("LM connect request sent (%d bytes)", ret
);
1895 if (zclient_read_sync_response(zclient
, cmd
)
1901 /* read instance and proto */
1902 uint8_t proto
= stream_getc(s
);
1903 uint16_t instance
= stream_getw(s
);
1906 if (proto
!= zclient
->redist_default
)
1909 "Wrong proto (%u) in LM connect response. Should be %u",
1910 proto
, zclient
->redist_default
);
1911 if (instance
!= zclient
->instance
)
1914 "Wrong instId (%u) in LM connect response. Should be %u",
1915 instance
, zclient
->instance
);
1918 result
= stream_getc(s
);
1920 zlog_debug("LM connect-response received, result %u", result
);
1926 * Asynchronous label chunk request
1928 * @param zclient Zclient used to connect to label manager (zebra)
1929 * @param keep Avoid garbage collection
1930 * @param chunk_size Amount of labels requested
1931 * @result 0 on success, -1 otherwise
1933 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1934 uint32_t chunk_size
)
1939 zlog_debug("Getting Label Chunk");
1941 if (zclient
->sock
< 0)
1947 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1949 stream_putc(s
, zclient
->redist_default
);
1951 stream_putw(s
, zclient
->instance
);
1952 stream_putc(s
, keep
);
1953 stream_putl(s
, chunk_size
);
1955 /* Put length at the first point of the stream. */
1956 stream_putw_at(s
, 0, stream_get_endp(s
));
1958 return zclient_send_message(zclient
);
1962 * Function to request a label chunk in a syncronous way
1964 * It first writes the request to zlcient output buffer and then
1965 * immediately reads the answer from the input buffer.
1967 * @param zclient Zclient used to connect to label manager (zebra)
1968 * @param keep Avoid garbage collection
1969 * @param chunk_size Amount of labels requested
1970 * @param start To write first assigned chunk label to
1971 * @param end To write last assigned chunk label to
1972 * @result 0 on success, -1 otherwise
1974 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1975 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1979 uint8_t response_keep
;
1982 zlog_debug("Getting Label Chunk");
1984 if (zclient
->sock
< 0)
1990 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1992 stream_putc(s
, zclient
->redist_default
);
1994 stream_putw(s
, zclient
->instance
);
1996 stream_putc(s
, keep
);
1998 stream_putl(s
, chunk_size
);
1999 /* Put length at the first point of the stream. */
2000 stream_putw_at(s
, 0, stream_get_endp(s
));
2002 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2004 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2005 close(zclient
->sock
);
2010 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2011 close(zclient
->sock
);
2016 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2019 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2022 /* parse response */
2025 /* read proto and instance */
2026 uint8_t proto
= stream_getc(s
);
2027 uint16_t instance
= stream_getw(s
);
2030 if (proto
!= zclient
->redist_default
)
2031 flog_err(EC_LIB_ZAPI_ENCODE
,
2032 "Wrong proto (%u) in get chunk response. Should be %u",
2033 proto
, zclient
->redist_default
);
2034 if (instance
!= zclient
->instance
)
2035 flog_err(EC_LIB_ZAPI_ENCODE
,
2036 "Wrong instId (%u) in get chunk response Should be %u",
2037 instance
, zclient
->instance
);
2040 response_keep
= stream_getc(s
);
2041 /* start and end labels */
2042 *start
= stream_getl(s
);
2043 *end
= stream_getl(s
);
2045 /* not owning this response */
2046 if (keep
!= response_keep
) {
2049 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2050 *start
, *end
, keep
, response_keep
);
2053 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2054 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2055 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2061 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2068 * Function to release a label chunk
2070 * @param zclient Zclient used to connect to label manager (zebra)
2071 * @param start First label of chunk
2072 * @param end Last label of chunk
2073 * @result 0 on success, -1 otherwise
2075 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2082 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2084 if (zclient
->sock
< 0)
2090 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2093 stream_putc(s
, zclient
->redist_default
);
2095 stream_putw(s
, zclient
->instance
);
2097 stream_putl(s
, start
);
2099 stream_putl(s
, end
);
2101 /* Put length at the first point of the stream. */
2102 stream_putw_at(s
, 0, stream_get_endp(s
));
2104 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2106 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2107 close(zclient
->sock
);
2112 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2113 close(zclient
->sock
);
2122 * Connect to table manager in a syncronous way
2124 * It first writes the request to zcient output buffer and then
2125 * immediately reads the answer from the input buffer.
2127 * @param zclient Zclient used to connect to table manager (zebra)
2128 * @result Result of response
2130 int tm_table_manager_connect(struct zclient
*zclient
)
2137 zlog_debug("Connecting to Table Manager");
2139 if (zclient
->sock
< 0)
2145 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2148 stream_putc(s
, zclient
->redist_default
);
2150 stream_putw(s
, zclient
->instance
);
2152 /* Put length at the first point of the stream. */
2153 stream_putw_at(s
, 0, stream_get_endp(s
));
2155 ret
= zclient_send_message(zclient
);
2160 zlog_debug("%s: Table manager connect request sent", __func__
);
2163 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2169 STREAM_GETC(s
, result
);
2172 "%s: Table Manager connect response received, result %u",
2181 * Function to request a table chunk in a syncronous way
2183 * It first writes the request to zclient output buffer and then
2184 * immediately reads the answer from the input buffer.
2186 * @param zclient Zclient used to connect to table manager (zebra)
2187 * @param chunk_size Amount of table requested
2188 * @param start to write first assigned chunk table RT ID to
2189 * @param end To write last assigned chunk table RT ID to
2190 * @result 0 on success, -1 otherwise
2192 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2193 uint32_t *start
, uint32_t *end
)
2199 zlog_debug("Getting Table Chunk");
2201 if (zclient
->sock
< 0)
2207 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2209 stream_putl(s
, chunk_size
);
2210 /* Put length at the first point of the stream. */
2211 stream_putw_at(s
, 0, stream_get_endp(s
));
2213 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2215 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2217 close(zclient
->sock
);
2222 flog_err(EC_LIB_ZAPI_SOCKET
,
2223 "%s: zclient->sock connection closed", __func__
);
2224 close(zclient
->sock
);
2229 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2233 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2237 /* start and end table IDs */
2238 STREAM_GETL(s
, *start
);
2239 STREAM_GETL(s
, *end
);
2242 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2249 * Function to release a table chunk
2251 * @param zclient Zclient used to connect to table manager (zebra)
2252 * @param start First label of table
2253 * @param end Last label of chunk
2254 * @result 0 on success, -1 otherwise
2256 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2262 zlog_debug("Releasing Table Chunk");
2264 if (zclient
->sock
< 0)
2270 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2273 stream_putl(s
, start
);
2275 stream_putl(s
, end
);
2277 /* Put length at the first point of the stream. */
2278 stream_putw_at(s
, 0, stream_get_endp(s
));
2280 return zclient_send_message(zclient
);
2284 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2292 zclient_create_header(s
, command
, VRF_DEFAULT
);
2293 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2294 stream_putl(s
, pw
->ifindex
);
2297 stream_putl(s
, pw
->type
);
2300 stream_putl(s
, pw
->af
);
2303 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2306 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2309 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2314 stream_putl(s
, pw
->local_label
);
2315 stream_putl(s
, pw
->remote_label
);
2318 stream_putc(s
, pw
->flags
);
2320 /* Protocol specific fields */
2321 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2323 /* Put length at the first point of the stream. */
2324 stream_putw_at(s
, 0, stream_get_endp(s
));
2326 return zclient_send_message(zclient
);
2330 * Receive PW status update from Zebra and send it to LDE process.
2332 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2333 zebra_size_t length
, vrf_id_t vrf_id
,
2334 struct zapi_pw_status
*pw
)
2338 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2342 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2343 pw
->ifindex
= stream_getl(s
);
2344 pw
->status
= stream_getl(s
);
2347 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2348 zebra_size_t length
, vrf_id_t vrf_id
)
2350 struct zclient_capabilities cap
;
2351 struct stream
*s
= zclient
->ibuf
;
2352 uint8_t mpls_enabled
;
2354 memset(&cap
, 0, sizeof(cap
));
2355 STREAM_GETC(s
, mpls_enabled
);
2356 cap
.mpls_enabled
= !!mpls_enabled
;
2357 STREAM_GETL(s
, cap
.ecmp
);
2359 if (zclient
->zebra_capabilities
)
2360 (*zclient
->zebra_capabilities
)(&cap
);
2366 /* Zebra client message read function. */
2367 static int zclient_read(struct thread
*thread
)
2370 uint16_t length
, command
;
2371 uint8_t marker
, version
;
2373 struct zclient
*zclient
;
2375 /* Get socket to zebra. */
2376 zclient
= THREAD_ARG(thread
);
2377 zclient
->t_read
= NULL
;
2379 /* Read zebra header (if we don't have it already). */
2380 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2382 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2383 ZEBRA_HEADER_SIZE
- already
))
2388 "zclient connection closed socket [%d].",
2390 return zclient_failed(zclient
);
2392 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2393 /* Try again later. */
2394 zclient_event(ZCLIENT_READ
, zclient
);
2397 already
= ZEBRA_HEADER_SIZE
;
2400 /* Reset to read from the beginning of the incoming packet. */
2401 stream_set_getp(zclient
->ibuf
, 0);
2403 /* Fetch header values. */
2404 length
= stream_getw(zclient
->ibuf
);
2405 marker
= stream_getc(zclient
->ibuf
);
2406 version
= stream_getc(zclient
->ibuf
);
2407 vrf_id
= stream_getl(zclient
->ibuf
);
2408 command
= stream_getw(zclient
->ibuf
);
2410 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2412 EC_LIB_ZAPI_MISSMATCH
,
2413 "%s: socket %d version mismatch, marker %d, version %d",
2414 __func__
, zclient
->sock
, marker
, version
);
2415 return zclient_failed(zclient
);
2418 if (length
< ZEBRA_HEADER_SIZE
) {
2419 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2420 "%s: socket %d message length %u is less than %d ",
2421 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2422 return zclient_failed(zclient
);
2426 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2430 "%s: message size %u exceeds buffer size %lu, expanding...",
2432 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2433 ns
= stream_new(length
);
2434 stream_copy(ns
, zclient
->ibuf
);
2435 stream_free(zclient
->ibuf
);
2439 /* Read rest of zebra packet. */
2440 if (already
< length
) {
2442 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2448 "zclient connection closed socket [%d].",
2450 return zclient_failed(zclient
);
2452 if (nbyte
!= (ssize_t
)(length
- already
)) {
2453 /* Try again later. */
2454 zclient_event(ZCLIENT_READ
, zclient
);
2459 length
-= ZEBRA_HEADER_SIZE
;
2462 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2463 (void *)zclient
, command
, vrf_id
);
2466 case ZEBRA_CAPABILITIES
:
2467 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2469 case ZEBRA_ROUTER_ID_UPDATE
:
2470 if (zclient
->router_id_update
)
2471 (*zclient
->router_id_update
)(command
, zclient
, length
,
2475 zclient_vrf_add(zclient
, vrf_id
);
2477 case ZEBRA_VRF_DELETE
:
2478 zclient_vrf_delete(zclient
, vrf_id
);
2480 case ZEBRA_INTERFACE_ADD
:
2481 if (zclient
->interface_add
)
2482 (*zclient
->interface_add
)(command
, zclient
, length
,
2485 case ZEBRA_INTERFACE_DELETE
:
2486 if (zclient
->interface_delete
)
2487 (*zclient
->interface_delete
)(command
, zclient
, length
,
2490 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2491 if (zclient
->interface_address_add
)
2492 (*zclient
->interface_address_add
)(command
, zclient
,
2495 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2496 if (zclient
->interface_address_delete
)
2497 (*zclient
->interface_address_delete
)(command
, zclient
,
2500 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2501 if (zclient
->interface_bfd_dest_update
)
2502 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2505 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2506 if (zclient
->interface_nbr_address_add
)
2507 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2510 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2511 if (zclient
->interface_nbr_address_delete
)
2512 (*zclient
->interface_nbr_address_delete
)(
2513 command
, zclient
, length
, vrf_id
);
2515 case ZEBRA_INTERFACE_UP
:
2516 if (zclient
->interface_up
)
2517 (*zclient
->interface_up
)(command
, zclient
, length
,
2520 case ZEBRA_INTERFACE_DOWN
:
2521 if (zclient
->interface_down
)
2522 (*zclient
->interface_down
)(command
, zclient
, length
,
2525 case ZEBRA_INTERFACE_VRF_UPDATE
:
2526 if (zclient
->interface_vrf_update
)
2527 (*zclient
->interface_vrf_update
)(command
, zclient
,
2530 case ZEBRA_NEXTHOP_UPDATE
:
2532 zlog_debug("zclient rcvd nexthop update\n");
2533 if (zclient
->nexthop_update
)
2534 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2537 case ZEBRA_IMPORT_CHECK_UPDATE
:
2539 zlog_debug("zclient rcvd import check update\n");
2540 if (zclient
->import_check_update
)
2541 (*zclient
->import_check_update
)(command
, zclient
,
2544 case ZEBRA_BFD_DEST_REPLAY
:
2545 if (zclient
->bfd_dest_replay
)
2546 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2549 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2550 if (zclient
->redistribute_route_add
)
2551 (*zclient
->redistribute_route_add
)(command
, zclient
,
2554 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2555 if (zclient
->redistribute_route_del
)
2556 (*zclient
->redistribute_route_del
)(command
, zclient
,
2559 case ZEBRA_INTERFACE_LINK_PARAMS
:
2560 if (zclient
->interface_link_params
)
2561 (*zclient
->interface_link_params
)(command
, zclient
,
2564 case ZEBRA_FEC_UPDATE
:
2566 zlog_debug("zclient rcvd fec update\n");
2567 if (zclient
->fec_update
)
2568 (*zclient
->fec_update
)(command
, zclient
, length
);
2570 case ZEBRA_LOCAL_ES_ADD
:
2571 if (zclient
->local_es_add
)
2572 (*zclient
->local_es_add
)(command
, zclient
, length
,
2575 case ZEBRA_LOCAL_ES_DEL
:
2576 if (zclient
->local_es_del
)
2577 (*zclient
->local_es_del
)(command
, zclient
, length
,
2581 if (zclient
->local_vni_add
)
2582 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2586 if (zclient
->local_vni_del
)
2587 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2590 case ZEBRA_L3VNI_ADD
:
2591 if (zclient
->local_l3vni_add
)
2592 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2595 case ZEBRA_L3VNI_DEL
:
2596 if (zclient
->local_l3vni_del
)
2597 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2600 case ZEBRA_MACIP_ADD
:
2601 if (zclient
->local_macip_add
)
2602 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2605 case ZEBRA_MACIP_DEL
:
2606 if (zclient
->local_macip_del
)
2607 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2610 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2611 if (zclient
->local_ip_prefix_add
)
2612 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2615 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2616 if (zclient
->local_ip_prefix_del
)
2617 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2620 case ZEBRA_PW_STATUS_UPDATE
:
2621 if (zclient
->pw_status_update
)
2622 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2625 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2626 if (zclient
->route_notify_owner
)
2627 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2630 case ZEBRA_RULE_NOTIFY_OWNER
:
2631 if (zclient
->rule_notify_owner
)
2632 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2635 case ZEBRA_GET_LABEL_CHUNK
:
2636 if (zclient
->label_chunk
)
2637 (*zclient
->label_chunk
)(command
, zclient
, length
,
2640 case ZEBRA_IPSET_NOTIFY_OWNER
:
2641 if (zclient
->ipset_notify_owner
)
2642 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2645 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2646 if (zclient
->ipset_entry_notify_owner
)
2647 (*zclient
->ipset_entry_notify_owner
)(command
,
2651 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2652 if (zclient
->iptable_notify_owner
)
2653 (*zclient
->iptable_notify_owner
)(command
,
2660 if (zclient
->sock
< 0)
2661 /* Connection was closed during packet processing. */
2664 /* Register read thread. */
2665 stream_reset(zclient
->ibuf
);
2666 zclient_event(ZCLIENT_READ
, zclient
);
2671 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2672 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2676 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2677 if (redist_check_instance(
2678 &zclient
->mi_redist
[afi
][type
], instance
))
2680 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2683 if (!redist_check_instance(
2684 &zclient
->mi_redist
[afi
][type
], instance
))
2686 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2691 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2692 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2695 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2697 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2700 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2704 if (zclient
->sock
> 0)
2705 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2710 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2714 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2715 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2717 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2719 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2721 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2724 if (zclient
->sock
> 0)
2725 zebra_message_send(zclient
, command
, vrf_id
);
2728 static void zclient_event(enum event event
, struct zclient
*zclient
)
2731 case ZCLIENT_SCHEDULE
:
2732 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2733 &zclient
->t_connect
);
2735 case ZCLIENT_CONNECT
:
2738 "zclient connect failures: %d schedule interval is now %d",
2739 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2740 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2741 zclient
->fail
< 3 ? 10 : 60,
2742 &zclient
->t_connect
);
2745 zclient
->t_read
= NULL
;
2746 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2747 zclient
->sock
, &zclient
->t_read
);
2752 void zclient_interface_set_master(struct zclient
*client
,
2753 struct interface
*master
,
2754 struct interface
*slave
)
2761 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2763 stream_putl(s
, master
->vrf_id
);
2764 stream_putl(s
, master
->ifindex
);
2765 stream_putl(s
, slave
->vrf_id
);
2766 stream_putl(s
, slave
->ifindex
);
2768 stream_putw_at(s
, 0, stream_get_endp(s
));
2769 zclient_send_message(client
);