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_notify(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_and_null(&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
202 * @see zclient_new_notify
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(LIB_ERR_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
) {
318 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
319 "%s: socket %d version mismatch, marker %d, version %d",
320 __func__
, sock
, *marker
, *version
);
324 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
331 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
333 STREAM_GETW(zmsg
, hdr
->length
);
334 STREAM_GETC(zmsg
, hdr
->marker
);
335 STREAM_GETC(zmsg
, hdr
->version
);
336 STREAM_GETL(zmsg
, hdr
->vrf_id
);
337 STREAM_GETW(zmsg
, hdr
->command
);
343 /* Send simple Zebra message. */
344 static int zebra_message_send(struct zclient
*zclient
, int command
,
349 /* Get zclient output buffer. */
353 /* Send very simple command only Zebra message. */
354 zclient_create_header(s
, command
, vrf_id
);
356 return zclient_send_message(zclient
);
359 static int zebra_hello_send(struct zclient
*zclient
)
363 if (zclient
->redist_default
) {
367 /* The VRF ID in the HELLO message is always 0. */
368 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
369 stream_putc(s
, zclient
->redist_default
);
370 stream_putw(s
, zclient
->instance
);
371 if (zclient
->receive_notify
)
376 stream_putw_at(s
, 0, stream_get_endp(s
));
377 return zclient_send_message(zclient
);
383 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
384 mpls_label_t label
, enum lsp_types_t ltype
)
391 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
392 stream_putl(s
, label
);
394 stream_putc(s
, ltype
);
395 stream_putw_at(s
, 0, stream_get_endp(s
));
396 zclient_send_message(zclient
);
399 /* Send register requests to zebra daemon for the information in a VRF. */
400 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
405 /* If not connected to the zebra yet. */
406 if (zclient
->sock
< 0)
410 zlog_debug("%s: send register messages for VRF %u", __func__
,
413 /* We need router-id information. */
414 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
416 /* We need interface information. */
417 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
419 /* Set unwanted redistribute route. */
420 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
421 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
424 /* Flush all redistribute request. */
425 if (vrf_id
== VRF_DEFAULT
) {
426 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
427 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
428 if (!zclient
->mi_redist
[afi
][i
].enabled
)
431 struct listnode
*node
;
434 for (ALL_LIST_ELEMENTS_RO(
435 zclient
->mi_redist
[afi
][i
]
438 if (!(i
== zclient
->redist_default
439 && *id
== zclient
->instance
))
440 zebra_redistribute_send(
441 ZEBRA_REDISTRIBUTE_ADD
,
442 zclient
, afi
, i
, *id
,
448 /* Resend all redistribute request. */
449 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
450 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
451 if (i
!= zclient
->redist_default
452 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
454 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
458 /* If default information is needed. */
459 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
460 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
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 /* We need interface information. */
482 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
484 /* Set unwanted redistribute route. */
485 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
486 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
489 /* Flush all redistribute request. */
490 if (vrf_id
== VRF_DEFAULT
) {
491 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
492 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
493 if (!zclient
->mi_redist
[afi
][i
].enabled
)
496 struct listnode
*node
;
499 for (ALL_LIST_ELEMENTS_RO(
500 zclient
->mi_redist
[afi
][i
]
503 if (!(i
== zclient
->redist_default
504 && *id
== zclient
->instance
))
505 zebra_redistribute_send(
506 ZEBRA_REDISTRIBUTE_DELETE
,
507 zclient
, afi
, i
, *id
,
513 /* Flush all redistribute request. */
514 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
515 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
516 if (i
!= zclient
->redist_default
517 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
519 zebra_redistribute_send(
520 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
523 /* If default information is needed. */
524 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
525 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
529 /* Send request to zebra daemon to start or stop RA. */
530 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
531 struct interface
*ifp
, int enable
,
536 /* If not connected to the zebra yet. */
537 if (zclient
->sock
< 0)
540 /* Form and send message. */
545 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
547 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
549 stream_putl(s
, ifp
->ifindex
);
550 stream_putl(s
, ra_interval
);
552 stream_putw_at(s
, 0, stream_get_endp(s
));
554 zclient_send_message(zclient
);
557 /* Make connection to zebra daemon. */
558 int zclient_start(struct zclient
*zclient
)
561 zlog_info("zclient_start is called");
563 /* If already connected to the zebra. */
564 if (zclient
->sock
>= 0)
567 /* Check connect thread. */
568 if (zclient
->t_connect
)
571 if (zclient_socket_connect(zclient
) < 0) {
573 zlog_debug("zclient connection fail");
575 zclient_event(ZCLIENT_CONNECT
, zclient
);
579 if (set_nonblocking(zclient
->sock
) < 0)
580 flog_err(LIB_ERR_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
581 __func__
, zclient
->sock
);
583 /* Clear fail count. */
586 zlog_debug("zclient connect success with socket [%d]",
589 /* Create read thread. */
590 zclient_event(ZCLIENT_READ
, zclient
);
592 zebra_hello_send(zclient
);
594 /* Inform the successful connection. */
595 if (zclient
->zebra_connected
)
596 (*zclient
->zebra_connected
)(zclient
);
601 /* Initialize zebra client. Argument redist_default is unwanted
602 redistribute route type. */
603 void zclient_init(struct zclient
*zclient
, int redist_default
,
604 unsigned short instance
, struct zebra_privs_t
*privs
)
608 /* Set -1 to the default socket value. */
610 zclient
->privs
= privs
;
612 /* Clear redistribution flags. */
613 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
614 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
615 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
617 /* Set unwanted redistribute route. bgpd does not need BGP route
619 zclient
->redist_default
= redist_default
;
620 zclient
->instance
= instance
;
621 /* Pending: make afi(s) an arg. */
622 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
623 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
626 /* Set default-information redistribute to zero. */
627 zclient
->default_information
= vrf_bitmap_init();
630 zlog_debug("zclient_start is called");
632 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
635 /* This function is a wrapper function for calling zclient_start from
636 timer or event thread. */
637 static int zclient_connect(struct thread
*t
)
639 struct zclient
*zclient
;
641 zclient
= THREAD_ARG(t
);
642 zclient
->t_connect
= NULL
;
645 zlog_debug("zclient_connect is called");
647 return zclient_start(zclient
);
650 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
651 bool exact_match
, vrf_id_t vrf_id
)
657 zclient_create_header(s
, command
, vrf_id
);
658 stream_putc(s
, (exact_match
) ? 1 : 0);
660 stream_putw(s
, PREFIX_FAMILY(p
));
661 stream_putc(s
, p
->prefixlen
);
662 switch (PREFIX_FAMILY(p
)) {
664 stream_put_in_addr(s
, &p
->u
.prefix4
);
667 stream_put(s
, &(p
->u
.prefix6
), 16);
672 stream_putw_at(s
, 0, stream_get_endp(s
));
674 return zclient_send_message(zclient
);
678 * "xdr_encode"-like interface that allows daemon (client) to send
679 * a message to zebra server for a route that needs to be
680 * added/deleted to the kernel. Info about the route is specified
681 * by the caller in a struct zapi_route. zapi_route_encode() then writes
682 * the info down the zclient socket using the stream_* functions.
684 * The corresponding read ("xdr_decode") function on the server
685 * side is zapi_route_decode().
687 * 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
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | Length (2) | Command | Route Type |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | ZEBRA Flags | Message Flags | Prefix length |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | Destination IPv4 Prefix for route |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
700 * described, as per the Nexthop count. Each nexthop described as:
703 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
704 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
705 * | IPv4 Nexthop address or Interface Index number |
706 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
708 * Alternatively, if the route is a blackhole route, then Nexthop count
709 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
712 * The original struct zapi_route_*() infrastructure was built around
713 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
714 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
715 * nexthops into zapi_route_encode() using the same zapi_route structure.
716 * This is done by setting zapi_route fields as follows:
717 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
718 * - .nexthop_num == .ifindex_num
719 * - .nexthop and .ifindex are filled with gate and ifindex parts of
720 * each compound nexthop, both in the same order
722 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
725 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
728 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
730 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
732 * XXX: No attention paid to alignment.
734 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
735 struct zapi_route
*api
)
737 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
739 return zclient_send_message(zclient
);
742 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
744 struct zapi_nexthop
*api_nh
;
749 zclient_create_header(s
, cmd
, api
->vrf_id
);
751 stream_putc(s
, api
->type
);
752 stream_putw(s
, api
->instance
);
753 stream_putl(s
, api
->flags
);
754 stream_putc(s
, api
->message
);
755 stream_putc(s
, api
->safi
);
757 /* Put prefix information. */
758 stream_putc(s
, api
->prefix
.family
);
759 psize
= PSIZE(api
->prefix
.prefixlen
);
760 stream_putc(s
, api
->prefix
.prefixlen
);
761 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
763 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
764 psize
= PSIZE(api
->src_prefix
.prefixlen
);
765 stream_putc(s
, api
->src_prefix
.prefixlen
);
766 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
770 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
771 /* limit the number of nexthops if necessary */
772 if (api
->nexthop_num
> MULTIPATH_NUM
) {
773 char buf
[PREFIX2STR_BUFFER
];
775 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
778 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
779 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
783 stream_putw(s
, api
->nexthop_num
);
785 for (i
= 0; i
< api
->nexthop_num
; i
++) {
786 api_nh
= &api
->nexthops
[i
];
788 stream_putl(s
, api_nh
->vrf_id
);
789 stream_putc(s
, api_nh
->type
);
790 switch (api_nh
->type
) {
791 case NEXTHOP_TYPE_BLACKHOLE
:
792 stream_putc(s
, api_nh
->bh_type
);
794 case NEXTHOP_TYPE_IPV4
:
795 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
797 case NEXTHOP_TYPE_IPV4_IFINDEX
:
798 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
799 stream_putl(s
, api_nh
->ifindex
);
801 case NEXTHOP_TYPE_IFINDEX
:
802 stream_putl(s
, api_nh
->ifindex
);
804 case NEXTHOP_TYPE_IPV6
:
805 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
808 case NEXTHOP_TYPE_IPV6_IFINDEX
:
809 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
811 stream_putl(s
, api_nh
->ifindex
);
815 /* MPLS labels for BGP-LU or Segment Routing */
816 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
817 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
818 char buf
[PREFIX2STR_BUFFER
];
819 prefix2str(&api
->prefix
, buf
,
821 flog_err(LIB_ERR_ZAPI_ENCODE
,
822 "%s: prefix %s: can't encode "
823 "%u labels (maximum is %u)",
830 stream_putc(s
, api_nh
->label_num
);
831 stream_put(s
, &api_nh
->labels
[0],
833 * sizeof(mpls_label_t
));
836 /* Router MAC for EVPN routes. */
837 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
838 stream_put(s
, &(api_nh
->rmac
),
839 sizeof(struct ethaddr
));
844 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
845 stream_putc(s
, api
->distance
);
846 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
847 stream_putl(s
, api
->metric
);
848 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
849 stream_putl(s
, api
->tag
);
850 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
851 stream_putl(s
, api
->mtu
);
852 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
853 stream_putl(s
, api
->tableid
);
855 /* Put length at the first point of the stream. */
856 stream_putw_at(s
, 0, stream_get_endp(s
));
861 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
863 struct zapi_nexthop
*api_nh
;
866 memset(api
, 0, sizeof(*api
));
868 /* Type, flags, message. */
869 STREAM_GETC(s
, api
->type
);
870 if (api
->type
> ZEBRA_ROUTE_MAX
) {
871 flog_err(LIB_ERR_ZAPI_ENCODE
,
872 "%s: Specified route type: %d is not a legal value\n",
873 __PRETTY_FUNCTION__
, api
->type
);
877 STREAM_GETW(s
, api
->instance
);
878 STREAM_GETL(s
, api
->flags
);
879 STREAM_GETC(s
, api
->message
);
880 STREAM_GETC(s
, api
->safi
);
883 STREAM_GETC(s
, api
->prefix
.family
);
884 STREAM_GETC(s
, api
->prefix
.prefixlen
);
885 switch (api
->prefix
.family
) {
887 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
890 "%s: V4 prefixlen is %d which should not be more than 32",
891 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
896 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
899 "%s: v6 prefixlen is %d which should not be more than 128",
900 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
905 flog_err(LIB_ERR_ZAPI_ENCODE
,
906 "%s: Specified family %d is not v4 or v6",
907 __PRETTY_FUNCTION__
, api
->prefix
.family
);
910 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
912 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
913 api
->src_prefix
.family
= AF_INET6
;
914 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
915 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
918 "%s: SRC Prefix prefixlen received: %d is too large",
919 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
922 STREAM_GET(&api
->src_prefix
.prefix
, s
,
923 PSIZE(api
->src_prefix
.prefixlen
));
925 if (api
->prefix
.family
!= AF_INET6
926 || api
->src_prefix
.prefixlen
== 0) {
929 "%s: SRC prefix specified in some manner that makes no sense",
930 __PRETTY_FUNCTION__
);
936 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
937 STREAM_GETW(s
, api
->nexthop_num
);
938 if (api
->nexthop_num
> MULTIPATH_NUM
) {
939 flog_err(LIB_ERR_ZAPI_ENCODE
,
940 "%s: invalid number of nexthops (%u)",
941 __func__
, api
->nexthop_num
);
945 for (i
= 0; i
< api
->nexthop_num
; i
++) {
946 api_nh
= &api
->nexthops
[i
];
948 STREAM_GETL(s
, api_nh
->vrf_id
);
949 STREAM_GETC(s
, api_nh
->type
);
950 switch (api_nh
->type
) {
951 case NEXTHOP_TYPE_BLACKHOLE
:
952 STREAM_GETC(s
, api_nh
->bh_type
);
954 case NEXTHOP_TYPE_IPV4
:
955 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
958 case NEXTHOP_TYPE_IPV4_IFINDEX
:
959 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
961 STREAM_GETL(s
, api_nh
->ifindex
);
963 case NEXTHOP_TYPE_IFINDEX
:
964 STREAM_GETL(s
, api_nh
->ifindex
);
966 case NEXTHOP_TYPE_IPV6
:
967 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
969 case NEXTHOP_TYPE_IPV6_IFINDEX
:
970 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
971 STREAM_GETL(s
, api_nh
->ifindex
);
975 /* MPLS labels for BGP-LU or Segment Routing */
976 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
977 STREAM_GETC(s
, api_nh
->label_num
);
979 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
982 "%s: invalid number of MPLS labels (%u)",
983 __func__
, api_nh
->label_num
);
987 STREAM_GET(&api_nh
->labels
[0], s
,
989 * sizeof(mpls_label_t
));
992 /* Router MAC for EVPN routes. */
993 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
994 stream_get(&(api_nh
->rmac
), s
,
995 sizeof(struct ethaddr
));
1000 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1001 STREAM_GETC(s
, api
->distance
);
1002 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1003 STREAM_GETL(s
, api
->metric
);
1004 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1005 STREAM_GETL(s
, api
->tag
);
1006 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1007 STREAM_GETL(s
, api
->mtu
);
1008 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1009 STREAM_GETL(s
, api
->tableid
);
1015 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1021 memset(&any
, 0, sizeof(any
));
1022 any
.family
= family
;
1026 stream_putc(s
, p
->family
);
1027 stream_putc(s
, p
->prefixlen
);
1028 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1031 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1034 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1037 * We are sending one item at a time at the moment
1041 stream_putl(s
, zrule
->seq
);
1042 stream_putl(s
, zrule
->priority
);
1043 stream_putl(s
, zrule
->unique
);
1045 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1046 zrule
->filter
.src_ip
.family
);
1047 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1048 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1049 zrule
->filter
.src_ip
.family
);
1050 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1051 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1053 stream_putl(s
, zrule
->action
.table
);
1054 stream_putl(s
, zrule
->ifindex
);
1056 /* Put length at the first point of the stream. */
1057 stream_putw_at(s
, 0, stream_get_endp(s
));
1062 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1064 enum zapi_route_notify_owner
*note
)
1068 STREAM_GET(note
, s
, sizeof(*note
));
1070 STREAM_GETC(s
, p
->family
);
1071 STREAM_GETC(s
, p
->prefixlen
);
1072 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1083 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1084 uint32_t *priority
, uint32_t *unique
,
1086 enum zapi_rule_notify_owner
*note
)
1088 uint32_t prio
, seq
, uni
;
1091 STREAM_GET(note
, s
, sizeof(*note
));
1093 STREAM_GETL(s
, seq
);
1094 STREAM_GETL(s
, prio
);
1095 STREAM_GETL(s
, uni
);
1096 STREAM_GETL(s
, ifi
);
1099 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1112 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1113 enum zapi_ipset_notify_owner
*note
)
1117 STREAM_GET(note
, s
, sizeof(*note
));
1119 STREAM_GETL(s
, uni
);
1122 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1131 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1133 enum zapi_ipset_entry_notify_owner
*note
)
1137 STREAM_GET(note
, s
, sizeof(*note
));
1139 STREAM_GETL(s
, uni
);
1141 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1144 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1153 bool zapi_iptable_notify_decode(struct stream
*s
,
1155 enum zapi_iptable_notify_owner
*note
)
1159 STREAM_GET(note
, s
, sizeof(*note
));
1161 STREAM_GETL(s
, uni
);
1164 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1173 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1175 struct nexthop
*n
= nexthop_new();
1177 n
->type
= znh
->type
;
1178 n
->vrf_id
= znh
->vrf_id
;
1179 n
->ifindex
= znh
->ifindex
;
1180 n
->gate
= znh
->gate
;
1183 * This function currently handles labels
1185 if (znh
->label_num
) {
1186 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1193 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1197 memset(nhr
, 0, sizeof(*nhr
));
1199 STREAM_GETW(s
, nhr
->prefix
.family
);
1200 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1201 switch (nhr
->prefix
.family
) {
1203 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1206 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1212 STREAM_GETC(s
, nhr
->type
);
1213 STREAM_GETW(s
, nhr
->instance
);
1214 STREAM_GETC(s
, nhr
->distance
);
1215 STREAM_GETL(s
, nhr
->metric
);
1216 STREAM_GETC(s
, nhr
->nexthop_num
);
1218 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1219 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1220 switch (nhr
->nexthops
[i
].type
) {
1221 case NEXTHOP_TYPE_IPV4
:
1222 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1223 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1225 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1227 case NEXTHOP_TYPE_IFINDEX
:
1228 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1230 case NEXTHOP_TYPE_IPV6
:
1231 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1232 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1234 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1236 case NEXTHOP_TYPE_BLACKHOLE
:
1239 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1240 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1241 flog_err(LIB_ERR_ZAPI_ENCODE
,
1242 "%s: invalid number of MPLS labels (%u)",
1243 __func__
, nhr
->nexthops
[i
].label_num
);
1246 if (nhr
->nexthops
[i
].label_num
)
1247 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1248 nhr
->nexthops
[i
].label_num
1249 * sizeof(mpls_label_t
));
1258 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1259 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1260 * then set/unset redist[type] in the client handle (a struct zserv) for the
1263 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1264 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1271 zclient_create_header(s
, command
, vrf_id
);
1272 stream_putc(s
, afi
);
1273 stream_putc(s
, type
);
1274 stream_putw(s
, instance
);
1276 stream_putw_at(s
, 0, stream_get_endp(s
));
1278 return zclient_send_message(zclient
);
1281 /* Get prefix in ZServ format; family should be filled in on prefix */
1282 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1284 size_t plen
= prefix_blen(p
);
1291 stream_get(&p
->u
.prefix
, s
, plen
);
1293 p
->prefixlen
= MIN(plen
* 8, c
);
1299 /* Router-id update from zebra daemon. */
1300 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1302 /* Fetch interface address. */
1303 STREAM_GETC(s
, rid
->family
);
1305 zclient_stream_get_prefix(s
, rid
);
1311 /* Interface addition from zebra daemon. */
1313 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1314 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1316 * 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
1317 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1323 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1325 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1327 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1330 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1332 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1334 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1336 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1338 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1340 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1341 * | Link Layer Type |
1342 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1343 * | Harware Address Length |
1344 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1345 * | Hardware Address if HW lenght different from 0 |
1346 * | ... max INTERFACE_HWADDR_MAX |
1347 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1348 * | Link_params? | Whether a link-params follows: 1 or 0.
1349 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1350 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1351 * | .... (struct if_link_params). |
1352 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1355 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1358 char vrfname_tmp
[VRF_NAMSIZ
];
1359 struct vrf_data data
;
1361 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1362 /* Read interface name. */
1363 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1365 /* Lookup/create vrf by vrf_id. */
1366 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1367 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1368 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1369 /* overwrite default vrf */
1370 if (vrf_id
== VRF_DEFAULT
)
1371 vrf_set_default_name(vrfname_tmp
);
1375 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1379 /* Lookup vrf by vrf_id. */
1380 vrf
= vrf_lookup_by_id(vrf_id
);
1383 * If a routing protocol doesn't know about a
1384 * vrf that is about to be deleted. There is
1385 * no point in attempting to delete it.
1393 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1395 struct interface
*ifp
;
1396 char ifname_tmp
[INTERFACE_NAMSIZ
];
1398 /* Read interface name. */
1399 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1401 /* Lookup/create interface by name. */
1402 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1404 zebra_interface_if_set_value(s
, ifp
);
1410 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1411 * from zebra server. The format of this message is the same as
1412 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1413 * comments for zebra_interface_add_read), except that no sockaddr_dl
1414 * is sent at the tail of the message.
1416 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1418 struct interface
*ifp
;
1419 char ifname_tmp
[INTERFACE_NAMSIZ
];
1421 /* Read interface name. */
1422 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1424 /* Lookup this by interface index. */
1425 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1427 flog_err(LIB_ERR_ZAPI_ENCODE
,
1428 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1429 ifname_tmp
, vrf_id
);
1433 zebra_interface_if_set_value(s
, ifp
);
1438 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1444 iflp
->lp_status
= stream_getl(s
);
1445 iflp
->te_metric
= stream_getl(s
);
1446 iflp
->max_bw
= stream_getf(s
);
1447 iflp
->max_rsv_bw
= stream_getf(s
);
1448 uint32_t bwclassnum
= stream_getl(s
);
1451 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1452 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1454 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
1455 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1456 " - outdated library?",
1457 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1459 iflp
->admin_grp
= stream_getl(s
);
1460 iflp
->rmt_as
= stream_getl(s
);
1461 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1463 iflp
->av_delay
= stream_getl(s
);
1464 iflp
->min_delay
= stream_getl(s
);
1465 iflp
->max_delay
= stream_getl(s
);
1466 iflp
->delay_var
= stream_getl(s
);
1468 iflp
->pkt_loss
= stream_getf(s
);
1469 iflp
->res_bw
= stream_getf(s
);
1470 iflp
->ava_bw
= stream_getf(s
);
1471 iflp
->use_bw
= stream_getf(s
);
1474 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1476 struct if_link_params
*iflp
;
1481 ifindex
= stream_getl(s
);
1483 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1486 flog_err(LIB_ERR_ZAPI_ENCODE
,
1487 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1492 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1495 link_params_set_value(s
, iflp
);
1500 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1502 uint8_t link_params_status
= 0;
1503 ifindex_t old_ifindex
;
1505 old_ifindex
= ifp
->ifindex
;
1506 /* Read interface's index. */
1507 if_set_index(ifp
, stream_getl(s
));
1508 ifp
->status
= stream_getc(s
);
1510 /* Read interface's value. */
1511 ifp
->flags
= stream_getq(s
);
1512 ifp
->ptm_enable
= stream_getc(s
);
1513 ifp
->ptm_status
= stream_getc(s
);
1514 ifp
->metric
= stream_getl(s
);
1515 ifp
->speed
= stream_getl(s
);
1516 ifp
->mtu
= stream_getl(s
);
1517 ifp
->mtu6
= stream_getl(s
);
1518 ifp
->bandwidth
= stream_getl(s
);
1519 ifp
->ll_type
= stream_getl(s
);
1520 ifp
->hw_addr_len
= stream_getl(s
);
1521 if (ifp
->hw_addr_len
)
1522 stream_get(ifp
->hw_addr
, s
,
1523 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1525 /* Read Traffic Engineering status */
1526 link_params_status
= stream_getc(s
);
1527 /* Then, Traffic Engineering parameters if any */
1528 if (link_params_status
) {
1529 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1530 link_params_set_value(s
, iflp
);
1533 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1536 size_t zebra_interface_link_params_write(struct stream
*s
,
1537 struct interface
*ifp
)
1540 struct if_link_params
*iflp
;
1543 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1546 iflp
= ifp
->link_params
;
1549 w
+= stream_putl(s
, iflp
->lp_status
);
1551 w
+= stream_putl(s
, iflp
->te_metric
);
1552 w
+= stream_putf(s
, iflp
->max_bw
);
1553 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1555 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1556 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1557 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1559 w
+= stream_putl(s
, iflp
->admin_grp
);
1560 w
+= stream_putl(s
, iflp
->rmt_as
);
1561 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1563 w
+= stream_putl(s
, iflp
->av_delay
);
1564 w
+= stream_putl(s
, iflp
->min_delay
);
1565 w
+= stream_putl(s
, iflp
->max_delay
);
1566 w
+= stream_putl(s
, iflp
->delay_var
);
1568 w
+= stream_putf(s
, iflp
->pkt_loss
);
1569 w
+= stream_putf(s
, iflp
->res_bw
);
1570 w
+= stream_putf(s
, iflp
->ava_bw
);
1571 w
+= stream_putf(s
, iflp
->use_bw
);
1577 * format of message for address additon is:
1581 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1582 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1591 * | ifc_flags | flags for connected address
1599 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1607 static int memconstant(const void *s
, int c
, size_t n
)
1609 const uint8_t *p
= s
;
1618 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1622 struct interface
*ifp
;
1623 struct connected
*ifc
;
1624 struct prefix p
, d
, *dp
;
1628 memset(&p
, 0, sizeof(p
));
1629 memset(&d
, 0, sizeof(d
));
1631 /* Get interface index. */
1632 ifindex
= stream_getl(s
);
1635 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1637 flog_err(LIB_ERR_ZAPI_ENCODE
,
1638 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1639 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1645 ifc_flags
= stream_getc(s
);
1647 /* Fetch interface address. */
1648 d
.family
= p
.family
= stream_getc(s
);
1649 plen
= prefix_blen(&d
);
1651 zclient_stream_get_prefix(s
, &p
);
1653 /* Fetch destination address. */
1654 stream_get(&d
.u
.prefix
, s
, plen
);
1656 /* N.B. NULL destination pointers are encoded as all zeroes */
1657 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1659 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1660 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1662 /* N.B. NULL destination pointers are encoded as all
1664 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1667 ifc
->flags
= ifc_flags
;
1668 if (ifc
->destination
)
1669 ifc
->destination
->prefixlen
=
1670 ifc
->address
->prefixlen
;
1671 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1672 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1674 char buf
[PREFIX_STRLEN
];
1676 LIB_ERR_ZAPI_ENCODE
,
1677 "warning: interface %s address %s with peer flag set, but no peer address!",
1679 prefix2str(ifc
->address
, buf
,
1681 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1685 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1686 ifc
= connected_delete_by_prefix(ifp
, &p
);
1693 * format of message for neighbor connected address is:
1697 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1698 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1713 * | addr_len | len of addr.
1716 struct nbr_connected
*
1717 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1719 unsigned int ifindex
;
1720 struct interface
*ifp
;
1722 struct nbr_connected
*ifc
;
1724 /* Get interface index. */
1725 ifindex
= stream_getl(s
);
1728 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1730 flog_err(LIB_ERR_ZAPI_ENCODE
,
1731 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1732 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1738 p
.family
= stream_getc(s
);
1739 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1740 p
.prefixlen
= stream_getc(s
);
1742 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1743 /* Currently only supporting P2P links, so any new RA source
1745 considered as the replacement of the previously learnt
1746 Link-Local address. */
1747 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1748 ifc
= nbr_connected_new();
1749 ifc
->address
= prefix_new();
1751 listnode_add(ifp
->nbr_connected
, ifc
);
1754 prefix_copy(ifc
->address
, &p
);
1756 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1758 ifc
= nbr_connected_check(ifp
, &p
);
1760 listnode_delete(ifp
->nbr_connected
, ifc
);
1766 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1768 vrf_id_t
*new_vrf_id
)
1770 unsigned int ifindex
;
1771 struct interface
*ifp
;
1774 /* Get interface index. */
1775 ifindex
= stream_getl(s
);
1777 /* Lookup interface. */
1778 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1780 flog_err(LIB_ERR_ZAPI_ENCODE
,
1781 "INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1786 /* Fetch new VRF Id. */
1787 new_id
= stream_getw(s
);
1789 *new_vrf_id
= new_id
;
1793 /* filter unwanted messages until the expected one arrives */
1794 static int zclient_read_sync_response(struct zclient
*zclient
,
1795 uint16_t expected_cmd
)
1807 cmd
= expected_cmd
+ 1;
1808 while (ret
== 0 && cmd
!= expected_cmd
) {
1812 /* wait until response arrives */
1814 FD_SET(zclient
->sock
, &readfds
);
1815 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1816 if (!FD_ISSET(zclient
->sock
, &readfds
))
1819 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1820 &version
, &vrf_id
, &cmd
);
1822 zlog_debug("%s: Response (%d bytes) received", __func__
,
1826 flog_err(LIB_ERR_ZAPI_ENCODE
,
1827 "%s: Invalid Sync Message Reply", __func__
);
1834 * Connect to label manager in a syncronous way
1836 * It first writes the request to zcient output buffer and then
1837 * immediately reads the answer from the input buffer.
1839 * @param zclient Zclient used to connect to label manager (zebra)
1840 * @result Result of response
1842 int lm_label_manager_connect(struct zclient
*zclient
)
1849 zlog_debug("Connecting to Label Manager (LM)");
1851 if (zclient
->sock
< 0)
1857 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1860 stream_putc(s
, zclient
->redist_default
);
1862 stream_putw(s
, zclient
->instance
);
1864 /* Put length at the first point of the stream. */
1865 stream_putw_at(s
, 0, stream_get_endp(s
));
1867 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1869 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
1870 close(zclient
->sock
);
1875 flog_err(LIB_ERR_ZAPI_SOCKET
, "Zclient sock closed");
1876 close(zclient
->sock
);
1881 zlog_debug("LM connect request sent (%d bytes)", ret
);
1884 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1890 /* read instance and proto */
1891 uint8_t proto
= stream_getc(s
);
1892 uint16_t instance
= stream_getw(s
);
1895 if (proto
!= zclient
->redist_default
)
1896 flog_err(LIB_ERR_ZAPI_ENCODE
,
1897 "Wrong proto (%u) in LM connect response. Should be %u",
1898 proto
, zclient
->redist_default
);
1899 if (instance
!= zclient
->instance
)
1900 flog_err(LIB_ERR_ZAPI_ENCODE
,
1901 "Wrong instId (%u) in LM connect response. Should be %u",
1902 instance
, zclient
->instance
);
1905 result
= stream_getc(s
);
1907 zlog_debug("LM connect-response received, result %u", result
);
1913 * Asynchronous label chunk request
1915 * @param zclient Zclient used to connect to label manager (zebra)
1916 * @param keep Avoid garbage collection
1917 * @param chunk_size Amount of labels requested
1918 * @result 0 on success, -1 otherwise
1920 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1921 uint32_t chunk_size
)
1926 zlog_debug("Getting Label Chunk");
1928 if (zclient
->sock
< 0)
1934 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1936 stream_putc(s
, zclient
->redist_default
);
1938 stream_putw(s
, zclient
->instance
);
1939 stream_putc(s
, keep
);
1940 stream_putl(s
, chunk_size
);
1942 /* Put length at the first point of the stream. */
1943 stream_putw_at(s
, 0, stream_get_endp(s
));
1945 return zclient_send_message(zclient
);
1949 * Function to request a label chunk in a syncronous way
1951 * It first writes the request to zlcient output buffer and then
1952 * immediately reads the answer from the input buffer.
1954 * @param zclient Zclient used to connect to label manager (zebra)
1955 * @param keep Avoid garbage collection
1956 * @param chunk_size Amount of labels requested
1957 * @param start To write first assigned chunk label to
1958 * @param end To write last assigned chunk label to
1959 * @result 0 on success, -1 otherwise
1961 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1962 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1966 uint8_t response_keep
;
1969 zlog_debug("Getting Label Chunk");
1971 if (zclient
->sock
< 0)
1977 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1979 stream_putc(s
, zclient
->redist_default
);
1981 stream_putw(s
, zclient
->instance
);
1983 stream_putc(s
, keep
);
1985 stream_putl(s
, chunk_size
);
1986 /* Put length at the first point of the stream. */
1987 stream_putw_at(s
, 0, stream_get_endp(s
));
1989 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1991 flog_err(LIB_ERR_ZAPI_SOCKET
,
1992 "Can't write to zclient sock");
1993 close(zclient
->sock
);
1998 flog_err(LIB_ERR_ZAPI_SOCKET
,
1999 "Zclient sock closed");
2000 close(zclient
->sock
);
2005 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2008 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2011 /* parse response */
2014 /* read proto and instance */
2015 uint8_t proto
= stream_getc(s
);
2016 uint16_t instance
= stream_getw(s
);
2019 if (proto
!= zclient
->redist_default
)
2020 flog_err(LIB_ERR_ZAPI_ENCODE
,
2021 "Wrong proto (%u) in get chunk response. Should be %u",
2022 proto
, zclient
->redist_default
);
2023 if (instance
!= zclient
->instance
)
2024 flog_err(LIB_ERR_ZAPI_ENCODE
,
2025 "Wrong instId (%u) in get chunk response Should be %u",
2026 instance
, zclient
->instance
);
2029 response_keep
= stream_getc(s
);
2030 /* start and end labels */
2031 *start
= stream_getl(s
);
2032 *end
= stream_getl(s
);
2034 /* not owning this response */
2035 if (keep
!= response_keep
) {
2036 flog_err(LIB_ERR_ZAPI_ENCODE
,
2037 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2038 *start
, *end
, keep
, response_keep
);
2041 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2042 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2043 flog_err(LIB_ERR_ZAPI_ENCODE
,
2044 "Invalid Label chunk: %u - %u", *start
, *end
);
2049 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2056 * Function to release a label chunk
2058 * @param zclient Zclient used to connect to label manager (zebra)
2059 * @param start First label of chunk
2060 * @param end Last label of chunk
2061 * @result 0 on success, -1 otherwise
2063 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2070 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2072 if (zclient
->sock
< 0)
2078 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2081 stream_putc(s
, zclient
->redist_default
);
2083 stream_putw(s
, zclient
->instance
);
2085 stream_putl(s
, start
);
2087 stream_putl(s
, end
);
2089 /* Put length at the first point of the stream. */
2090 stream_putw_at(s
, 0, stream_get_endp(s
));
2092 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2094 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
2095 close(zclient
->sock
);
2100 flog_err(LIB_ERR_ZAPI_SOCKET
,
2101 "Zclient sock connection closed");
2102 close(zclient
->sock
);
2111 * Connect to table manager in a syncronous way
2113 * It first writes the request to zcient output buffer and then
2114 * immediately reads the answer from the input buffer.
2116 * @param zclient Zclient used to connect to table manager (zebra)
2117 * @result Result of response
2119 int tm_table_manager_connect(struct zclient
*zclient
)
2126 zlog_debug("Connecting to Table Manager");
2128 if (zclient
->sock
< 0)
2134 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2137 stream_putc(s
, zclient
->redist_default
);
2139 stream_putw(s
, zclient
->instance
);
2141 /* Put length at the first point of the stream. */
2142 stream_putw_at(s
, 0, stream_get_endp(s
));
2144 ret
= zclient_send_message(zclient
);
2149 zlog_debug("%s: Table manager connect request sent", __func__
);
2152 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2158 STREAM_GETC(s
, result
);
2161 "%s: Table Manager connect response received, result %u",
2170 * Function to request a table chunk in a syncronous way
2172 * It first writes the request to zclient output buffer and then
2173 * immediately reads the answer from the input buffer.
2175 * @param zclient Zclient used to connect to table manager (zebra)
2176 * @param chunk_size Amount of table requested
2177 * @param start to write first assigned chunk table RT ID to
2178 * @param end To write last assigned chunk table RT ID to
2179 * @result 0 on success, -1 otherwise
2181 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2182 uint32_t *start
, uint32_t *end
)
2188 zlog_debug("Getting Table Chunk");
2190 if (zclient
->sock
< 0)
2196 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2198 stream_putl(s
, chunk_size
);
2199 /* Put length at the first point of the stream. */
2200 stream_putw_at(s
, 0, stream_get_endp(s
));
2202 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2204 flog_err(LIB_ERR_ZAPI_SOCKET
,
2205 "%s: can't write to zclient->sock", __func__
);
2206 close(zclient
->sock
);
2211 flog_err(LIB_ERR_ZAPI_SOCKET
,
2212 "%s: zclient->sock connection closed", __func__
);
2213 close(zclient
->sock
);
2218 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2222 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2226 /* start and end table IDs */
2227 STREAM_GETL(s
, *start
);
2228 STREAM_GETL(s
, *end
);
2231 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2238 * Function to release a table chunk
2240 * @param zclient Zclient used to connect to table manager (zebra)
2241 * @param start First label of table
2242 * @param end Last label of chunk
2243 * @result 0 on success, -1 otherwise
2245 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2251 zlog_debug("Releasing Table Chunk");
2253 if (zclient
->sock
< 0)
2259 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2262 stream_putl(s
, start
);
2264 stream_putl(s
, end
);
2266 /* Put length at the first point of the stream. */
2267 stream_putw_at(s
, 0, stream_get_endp(s
));
2269 return zclient_send_message(zclient
);
2273 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2281 zclient_create_header(s
, command
, VRF_DEFAULT
);
2282 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2283 stream_putl(s
, pw
->ifindex
);
2286 stream_putl(s
, pw
->type
);
2289 stream_putl(s
, pw
->af
);
2292 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2295 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2298 flog_err(LIB_ERR_ZAPI_ENCODE
,
2299 "%s: unknown af", __func__
);
2304 stream_putl(s
, pw
->local_label
);
2305 stream_putl(s
, pw
->remote_label
);
2308 stream_putc(s
, pw
->flags
);
2310 /* Protocol specific fields */
2311 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2313 /* Put length at the first point of the stream. */
2314 stream_putw_at(s
, 0, stream_get_endp(s
));
2316 return zclient_send_message(zclient
);
2320 * Receive PW status update from Zebra and send it to LDE process.
2322 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2323 zebra_size_t length
, vrf_id_t vrf_id
,
2324 struct zapi_pw_status
*pw
)
2328 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2332 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2333 pw
->ifindex
= stream_getl(s
);
2334 pw
->status
= stream_getl(s
);
2337 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2338 zebra_size_t length
, vrf_id_t vrf_id
)
2340 struct zclient_capabilities cap
;
2341 struct stream
*s
= zclient
->ibuf
;
2342 uint8_t mpls_enabled
;
2344 memset(&cap
, 0, sizeof(cap
));
2345 STREAM_GETC(s
, mpls_enabled
);
2346 cap
.mpls_enabled
= !!mpls_enabled
;
2347 STREAM_GETL(s
, cap
.ecmp
);
2349 if (zclient
->zebra_capabilities
)
2350 (*zclient
->zebra_capabilities
)(&cap
);
2356 /* Zebra client message read function. */
2357 static int zclient_read(struct thread
*thread
)
2360 uint16_t length
, command
;
2361 uint8_t marker
, version
;
2363 struct zclient
*zclient
;
2365 /* Get socket to zebra. */
2366 zclient
= THREAD_ARG(thread
);
2367 zclient
->t_read
= NULL
;
2369 /* Read zebra header (if we don't have it already). */
2370 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2372 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2373 ZEBRA_HEADER_SIZE
- already
))
2378 "zclient connection closed socket [%d].",
2380 return zclient_failed(zclient
);
2382 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2383 /* Try again later. */
2384 zclient_event(ZCLIENT_READ
, zclient
);
2387 already
= ZEBRA_HEADER_SIZE
;
2390 /* Reset to read from the beginning of the incoming packet. */
2391 stream_set_getp(zclient
->ibuf
, 0);
2393 /* Fetch header values. */
2394 length
= stream_getw(zclient
->ibuf
);
2395 marker
= stream_getc(zclient
->ibuf
);
2396 version
= stream_getc(zclient
->ibuf
);
2397 vrf_id
= stream_getl(zclient
->ibuf
);
2398 command
= stream_getw(zclient
->ibuf
);
2400 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2401 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2402 "%s: socket %d version mismatch, marker %d, version %d",
2403 __func__
, zclient
->sock
, marker
, version
);
2404 return zclient_failed(zclient
);
2407 if (length
< ZEBRA_HEADER_SIZE
) {
2408 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2409 "%s: socket %d message length %u is less than %d ",
2410 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2411 return zclient_failed(zclient
);
2415 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2418 LIB_ERR_ZAPI_ENCODE
,
2419 "%s: message size %u exceeds buffer size %lu, expanding...",
2421 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2422 ns
= stream_new(length
);
2423 stream_copy(ns
, zclient
->ibuf
);
2424 stream_free(zclient
->ibuf
);
2428 /* Read rest of zebra packet. */
2429 if (already
< length
) {
2431 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2437 "zclient connection closed socket [%d].",
2439 return zclient_failed(zclient
);
2441 if (nbyte
!= (ssize_t
)(length
- already
)) {
2442 /* Try again later. */
2443 zclient_event(ZCLIENT_READ
, zclient
);
2448 length
-= ZEBRA_HEADER_SIZE
;
2451 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2452 (void *)zclient
, command
, vrf_id
);
2455 case ZEBRA_CAPABILITIES
:
2456 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2458 case ZEBRA_ROUTER_ID_UPDATE
:
2459 if (zclient
->router_id_update
)
2460 (*zclient
->router_id_update
)(command
, zclient
, length
,
2464 zclient_vrf_add(zclient
, vrf_id
);
2466 case ZEBRA_VRF_DELETE
:
2467 zclient_vrf_delete(zclient
, vrf_id
);
2469 case ZEBRA_INTERFACE_ADD
:
2470 if (zclient
->interface_add
)
2471 (*zclient
->interface_add
)(command
, zclient
, length
,
2474 case ZEBRA_INTERFACE_DELETE
:
2475 if (zclient
->interface_delete
)
2476 (*zclient
->interface_delete
)(command
, zclient
, length
,
2479 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2480 if (zclient
->interface_address_add
)
2481 (*zclient
->interface_address_add
)(command
, zclient
,
2484 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2485 if (zclient
->interface_address_delete
)
2486 (*zclient
->interface_address_delete
)(command
, zclient
,
2489 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2490 if (zclient
->interface_bfd_dest_update
)
2491 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2494 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2495 if (zclient
->interface_nbr_address_add
)
2496 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2499 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2500 if (zclient
->interface_nbr_address_delete
)
2501 (*zclient
->interface_nbr_address_delete
)(
2502 command
, zclient
, length
, vrf_id
);
2504 case ZEBRA_INTERFACE_UP
:
2505 if (zclient
->interface_up
)
2506 (*zclient
->interface_up
)(command
, zclient
, length
,
2509 case ZEBRA_INTERFACE_DOWN
:
2510 if (zclient
->interface_down
)
2511 (*zclient
->interface_down
)(command
, zclient
, length
,
2514 case ZEBRA_INTERFACE_VRF_UPDATE
:
2515 if (zclient
->interface_vrf_update
)
2516 (*zclient
->interface_vrf_update
)(command
, zclient
,
2519 case ZEBRA_NEXTHOP_UPDATE
:
2521 zlog_debug("zclient rcvd nexthop update\n");
2522 if (zclient
->nexthop_update
)
2523 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2526 case ZEBRA_IMPORT_CHECK_UPDATE
:
2528 zlog_debug("zclient rcvd import check update\n");
2529 if (zclient
->import_check_update
)
2530 (*zclient
->import_check_update
)(command
, zclient
,
2533 case ZEBRA_BFD_DEST_REPLAY
:
2534 if (zclient
->bfd_dest_replay
)
2535 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2538 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2539 if (zclient
->redistribute_route_add
)
2540 (*zclient
->redistribute_route_add
)(command
, zclient
,
2543 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2544 if (zclient
->redistribute_route_del
)
2545 (*zclient
->redistribute_route_del
)(command
, zclient
,
2548 case ZEBRA_INTERFACE_LINK_PARAMS
:
2549 if (zclient
->interface_link_params
)
2550 (*zclient
->interface_link_params
)(command
, zclient
,
2553 case ZEBRA_FEC_UPDATE
:
2555 zlog_debug("zclient rcvd fec update\n");
2556 if (zclient
->fec_update
)
2557 (*zclient
->fec_update
)(command
, zclient
, length
);
2559 case ZEBRA_LOCAL_ES_ADD
:
2560 if (zclient
->local_es_add
)
2561 (*zclient
->local_es_add
)(command
, zclient
, length
,
2564 case ZEBRA_LOCAL_ES_DEL
:
2565 if (zclient
->local_es_del
)
2566 (*zclient
->local_es_del
)(command
, zclient
, length
,
2570 if (zclient
->local_vni_add
)
2571 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2575 if (zclient
->local_vni_del
)
2576 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2579 case ZEBRA_L3VNI_ADD
:
2580 if (zclient
->local_l3vni_add
)
2581 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2584 case ZEBRA_L3VNI_DEL
:
2585 if (zclient
->local_l3vni_del
)
2586 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2589 case ZEBRA_MACIP_ADD
:
2590 if (zclient
->local_macip_add
)
2591 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2594 case ZEBRA_MACIP_DEL
:
2595 if (zclient
->local_macip_del
)
2596 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2599 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2600 if (zclient
->local_ip_prefix_add
)
2601 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2604 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2605 if (zclient
->local_ip_prefix_del
)
2606 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2609 case ZEBRA_PW_STATUS_UPDATE
:
2610 if (zclient
->pw_status_update
)
2611 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2614 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2615 if (zclient
->route_notify_owner
)
2616 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2619 case ZEBRA_RULE_NOTIFY_OWNER
:
2620 if (zclient
->rule_notify_owner
)
2621 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2624 case ZEBRA_GET_LABEL_CHUNK
:
2625 if (zclient
->label_chunk
)
2626 (*zclient
->label_chunk
)(command
, zclient
, length
,
2629 case ZEBRA_IPSET_NOTIFY_OWNER
:
2630 if (zclient
->ipset_notify_owner
)
2631 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2634 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2635 if (zclient
->ipset_entry_notify_owner
)
2636 (*zclient
->ipset_entry_notify_owner
)(command
,
2640 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2641 if (zclient
->iptable_notify_owner
)
2642 (*zclient
->iptable_notify_owner
)(command
,
2649 if (zclient
->sock
< 0)
2650 /* Connection was closed during packet processing. */
2653 /* Register read thread. */
2654 stream_reset(zclient
->ibuf
);
2655 zclient_event(ZCLIENT_READ
, zclient
);
2660 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2661 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2665 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2666 if (redist_check_instance(
2667 &zclient
->mi_redist
[afi
][type
], instance
))
2669 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2672 if (!redist_check_instance(
2673 &zclient
->mi_redist
[afi
][type
], instance
))
2675 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2680 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2681 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2684 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2686 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2689 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2693 if (zclient
->sock
> 0)
2694 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2699 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2703 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2704 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2706 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2708 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2710 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2713 if (zclient
->sock
> 0)
2714 zebra_message_send(zclient
, command
, vrf_id
);
2717 static void zclient_event(enum event event
, struct zclient
*zclient
)
2720 case ZCLIENT_SCHEDULE
:
2721 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2722 &zclient
->t_connect
);
2724 case ZCLIENT_CONNECT
:
2727 "zclient connect failures: %d schedule interval is now %d",
2728 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2729 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2730 zclient
->fail
< 3 ? 10 : 60,
2731 &zclient
->t_connect
);
2734 zclient
->t_read
= NULL
;
2735 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2736 zclient
->sock
, &zclient
->t_read
);
2741 void zclient_interface_set_master(struct zclient
*client
,
2742 struct interface
*master
,
2743 struct interface
*slave
)
2750 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2752 stream_putl(s
, master
->vrf_id
);
2753 stream_putl(s
, master
->ifindex
);
2754 stream_putl(s
, slave
->vrf_id
);
2755 stream_putl(s
, slave
->ifindex
);
2757 stream_putw_at(s
, 0, stream_get_endp(s
));
2758 zclient_send_message(client
);