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_warn("%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
)) {
253 "%s: buffer_flush_available failed on zclient fd %d, closing",
254 __func__
, zclient
->sock
);
255 return zclient_failed(zclient
);
258 zclient
->t_write
= NULL
;
259 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
260 zclient
->sock
, &zclient
->t_write
);
268 int zclient_send_message(struct zclient
*zclient
)
270 if (zclient
->sock
< 0)
272 switch (buffer_write(zclient
->wb
, zclient
->sock
,
273 STREAM_DATA(zclient
->obuf
),
274 stream_get_endp(zclient
->obuf
))) {
276 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
277 __func__
, zclient
->sock
);
278 return zclient_failed(zclient
);
281 THREAD_OFF(zclient
->t_write
);
284 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
285 zclient
->sock
, &zclient
->t_write
);
291 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
293 /* length placeholder, caller can update */
294 stream_putw(s
, ZEBRA_HEADER_SIZE
);
295 stream_putc(s
, ZEBRA_HEADER_MARKER
);
296 stream_putc(s
, ZSERV_VERSION
);
297 stream_putl(s
, vrf_id
);
298 stream_putw(s
, command
);
301 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
302 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
305 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
308 STREAM_GETW(s
, *size
);
309 *size
-= ZEBRA_HEADER_SIZE
;
310 STREAM_GETC(s
, *marker
);
311 STREAM_GETC(s
, *version
);
312 STREAM_GETL(s
, *vrf_id
);
313 STREAM_GETW(s
, *cmd
);
315 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
316 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
317 "%s: socket %d version mismatch, marker %d, version %d",
318 __func__
, sock
, *marker
, *version
);
322 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
329 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
331 STREAM_GETW(zmsg
, hdr
->length
);
332 STREAM_GETC(zmsg
, hdr
->marker
);
333 STREAM_GETC(zmsg
, hdr
->version
);
334 STREAM_GETL(zmsg
, hdr
->vrf_id
);
335 STREAM_GETW(zmsg
, hdr
->command
);
341 /* Send simple Zebra message. */
342 static int zebra_message_send(struct zclient
*zclient
, int command
,
347 /* Get zclient output buffer. */
351 /* Send very simple command only Zebra message. */
352 zclient_create_header(s
, command
, vrf_id
);
354 return zclient_send_message(zclient
);
357 static int zebra_hello_send(struct zclient
*zclient
)
361 if (zclient
->redist_default
) {
365 /* The VRF ID in the HELLO message is always 0. */
366 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
367 stream_putc(s
, zclient
->redist_default
);
368 stream_putw(s
, zclient
->instance
);
369 if (zclient
->receive_notify
)
374 stream_putw_at(s
, 0, stream_get_endp(s
));
375 return zclient_send_message(zclient
);
381 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
382 mpls_label_t label
, enum lsp_types_t ltype
)
389 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
390 stream_putl(s
, label
);
392 stream_putc(s
, ltype
);
393 stream_putw_at(s
, 0, stream_get_endp(s
));
394 zclient_send_message(zclient
);
397 /* Send register requests to zebra daemon for the information in a VRF. */
398 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
403 /* If not connected to the zebra yet. */
404 if (zclient
->sock
< 0)
408 zlog_debug("%s: send register messages for VRF %u", __func__
,
411 /* We need router-id information. */
412 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
414 /* We need interface information. */
415 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
417 /* Set unwanted redistribute route. */
418 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
419 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
422 /* Flush all redistribute request. */
423 if (vrf_id
== VRF_DEFAULT
) {
424 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
425 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
426 if (!zclient
->mi_redist
[afi
][i
].enabled
)
429 struct listnode
*node
;
432 for (ALL_LIST_ELEMENTS_RO(
433 zclient
->mi_redist
[afi
][i
]
436 if (!(i
== zclient
->redist_default
437 && *id
== zclient
->instance
))
438 zebra_redistribute_send(
439 ZEBRA_REDISTRIBUTE_ADD
,
440 zclient
, afi
, i
, *id
,
446 /* Resend all redistribute request. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
448 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
449 if (i
!= zclient
->redist_default
450 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
452 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
456 /* If default information is needed. */
457 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
458 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
462 /* Send unregister requests to zebra daemon for the information in a VRF. */
463 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
468 /* If not connected to the zebra yet. */
469 if (zclient
->sock
< 0)
473 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
476 /* We need router-id information. */
477 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
479 /* We need interface information. */
480 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
482 /* Set unwanted redistribute route. */
483 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
484 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
487 /* Flush all redistribute request. */
488 if (vrf_id
== VRF_DEFAULT
) {
489 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
490 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
491 if (!zclient
->mi_redist
[afi
][i
].enabled
)
494 struct listnode
*node
;
497 for (ALL_LIST_ELEMENTS_RO(
498 zclient
->mi_redist
[afi
][i
]
501 if (!(i
== zclient
->redist_default
502 && *id
== zclient
->instance
))
503 zebra_redistribute_send(
504 ZEBRA_REDISTRIBUTE_DELETE
,
505 zclient
, afi
, i
, *id
,
511 /* Flush all redistribute request. */
512 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
513 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
514 if (i
!= zclient
->redist_default
515 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
517 zebra_redistribute_send(
518 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
521 /* If default information is needed. */
522 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
523 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
527 /* Send request to zebra daemon to start or stop RA. */
528 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
529 struct interface
*ifp
, int enable
,
534 /* If not connected to the zebra yet. */
535 if (zclient
->sock
< 0)
538 /* Form and send message. */
543 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
545 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
547 stream_putl(s
, ifp
->ifindex
);
548 stream_putl(s
, ra_interval
);
550 stream_putw_at(s
, 0, stream_get_endp(s
));
552 zclient_send_message(zclient
);
555 /* Make connection to zebra daemon. */
556 int zclient_start(struct zclient
*zclient
)
559 zlog_info("zclient_start is called");
561 /* If already connected to the zebra. */
562 if (zclient
->sock
>= 0)
565 /* Check connect thread. */
566 if (zclient
->t_connect
)
569 if (zclient_socket_connect(zclient
) < 0) {
571 zlog_debug("zclient connection fail");
573 zclient_event(ZCLIENT_CONNECT
, zclient
);
577 if (set_nonblocking(zclient
->sock
) < 0)
578 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
581 /* Clear fail count. */
584 zlog_debug("zclient connect success with socket [%d]",
587 /* Create read thread. */
588 zclient_event(ZCLIENT_READ
, zclient
);
590 zebra_hello_send(zclient
);
592 /* Inform the successful connection. */
593 if (zclient
->zebra_connected
)
594 (*zclient
->zebra_connected
)(zclient
);
599 /* Initialize zebra client. Argument redist_default is unwanted
600 redistribute route type. */
601 void zclient_init(struct zclient
*zclient
, int redist_default
,
602 unsigned short instance
, struct zebra_privs_t
*privs
)
606 /* Set -1 to the default socket value. */
608 zclient
->privs
= privs
;
610 /* Clear redistribution flags. */
611 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
612 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
613 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
615 /* Set unwanted redistribute route. bgpd does not need BGP route
617 zclient
->redist_default
= redist_default
;
618 zclient
->instance
= instance
;
619 /* Pending: make afi(s) an arg. */
620 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
621 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
624 /* Set default-information redistribute to zero. */
625 zclient
->default_information
= vrf_bitmap_init();
628 zlog_debug("zclient_start is called");
630 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
633 /* This function is a wrapper function for calling zclient_start from
634 timer or event thread. */
635 static int zclient_connect(struct thread
*t
)
637 struct zclient
*zclient
;
639 zclient
= THREAD_ARG(t
);
640 zclient
->t_connect
= NULL
;
643 zlog_debug("zclient_connect is called");
645 return zclient_start(zclient
);
648 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
649 bool exact_match
, vrf_id_t vrf_id
)
655 zclient_create_header(s
, command
, vrf_id
);
656 stream_putc(s
, (exact_match
) ? 1 : 0);
658 stream_putw(s
, PREFIX_FAMILY(p
));
659 stream_putc(s
, p
->prefixlen
);
660 switch (PREFIX_FAMILY(p
)) {
662 stream_put_in_addr(s
, &p
->u
.prefix4
);
665 stream_put(s
, &(p
->u
.prefix6
), 16);
670 stream_putw_at(s
, 0, stream_get_endp(s
));
672 return zclient_send_message(zclient
);
676 * "xdr_encode"-like interface that allows daemon (client) to send
677 * a message to zebra server for a route that needs to be
678 * added/deleted to the kernel. Info about the route is specified
679 * by the caller in a struct zapi_route. zapi_route_encode() then writes
680 * the info down the zclient socket using the stream_* functions.
682 * The corresponding read ("xdr_decode") function on the server
683 * side is zapi_route_decode().
685 * 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
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | Length (2) | Command | Route Type |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | ZEBRA Flags | Message Flags | Prefix length |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | Destination IPv4 Prefix for route |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
698 * described, as per the Nexthop count. Each nexthop described as:
701 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * | IPv4 Nexthop address or Interface Index number |
704 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 * Alternatively, if the route is a blackhole route, then Nexthop count
707 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
710 * The original struct zapi_route_*() infrastructure was built around
711 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
712 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
713 * nexthops into zapi_route_encode() using the same zapi_route structure.
714 * This is done by setting zapi_route fields as follows:
715 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
716 * - .nexthop_num == .ifindex_num
717 * - .nexthop and .ifindex are filled with gate and ifindex parts of
718 * each compound nexthop, both in the same order
720 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
723 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
726 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
728 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
730 * XXX: No attention paid to alignment.
732 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
733 struct zapi_route
*api
)
735 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
737 return zclient_send_message(zclient
);
740 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
742 struct zapi_nexthop
*api_nh
;
747 zclient_create_header(s
, cmd
, api
->vrf_id
);
749 stream_putc(s
, api
->type
);
750 stream_putw(s
, api
->instance
);
751 stream_putl(s
, api
->flags
);
752 stream_putc(s
, api
->message
);
753 stream_putc(s
, api
->safi
);
755 /* Put prefix information. */
756 stream_putc(s
, api
->prefix
.family
);
757 psize
= PSIZE(api
->prefix
.prefixlen
);
758 stream_putc(s
, api
->prefix
.prefixlen
);
759 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
761 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
762 psize
= PSIZE(api
->src_prefix
.prefixlen
);
763 stream_putc(s
, api
->src_prefix
.prefixlen
);
764 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
768 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
769 /* limit the number of nexthops if necessary */
770 if (api
->nexthop_num
> MULTIPATH_NUM
) {
771 char buf
[PREFIX2STR_BUFFER
];
773 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
775 "%s: prefix %s: can't encode %u nexthops "
777 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
781 stream_putw(s
, api
->nexthop_num
);
783 for (i
= 0; i
< api
->nexthop_num
; i
++) {
784 api_nh
= &api
->nexthops
[i
];
786 stream_putl(s
, api_nh
->vrf_id
);
787 stream_putc(s
, api_nh
->type
);
788 switch (api_nh
->type
) {
789 case NEXTHOP_TYPE_BLACKHOLE
:
790 stream_putc(s
, api_nh
->bh_type
);
792 case NEXTHOP_TYPE_IPV4
:
793 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
795 case NEXTHOP_TYPE_IPV4_IFINDEX
:
796 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
797 stream_putl(s
, api_nh
->ifindex
);
799 case NEXTHOP_TYPE_IFINDEX
:
800 stream_putl(s
, api_nh
->ifindex
);
802 case NEXTHOP_TYPE_IPV6
:
803 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
806 case NEXTHOP_TYPE_IPV6_IFINDEX
:
807 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
809 stream_putl(s
, api_nh
->ifindex
);
813 "%s: Specified Nexthop type %d does not exist",
814 __PRETTY_FUNCTION__
, api_nh
->type
);
818 /* MPLS labels for BGP-LU or Segment Routing */
819 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
820 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
821 char buf
[PREFIX2STR_BUFFER
];
822 prefix2str(&api
->prefix
, buf
,
824 flog_err(LIB_ERR_ZAPI_ENCODE
,
825 "%s: prefix %s: can't encode "
826 "%u labels (maximum is %u)",
833 stream_putc(s
, api_nh
->label_num
);
834 stream_put(s
, &api_nh
->labels
[0],
836 * sizeof(mpls_label_t
));
839 /* Router MAC for EVPN routes. */
840 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
841 stream_put(s
, &(api_nh
->rmac
),
842 sizeof(struct ethaddr
));
847 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
848 stream_putc(s
, api
->distance
);
849 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
850 stream_putl(s
, api
->metric
);
851 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
852 stream_putl(s
, api
->tag
);
853 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
854 stream_putl(s
, api
->mtu
);
855 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
856 stream_putl(s
, api
->tableid
);
858 /* Put length at the first point of the stream. */
859 stream_putw_at(s
, 0, stream_get_endp(s
));
864 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
866 struct zapi_nexthop
*api_nh
;
869 memset(api
, 0, sizeof(*api
));
871 /* Type, flags, message. */
872 STREAM_GETC(s
, api
->type
);
873 if (api
->type
> ZEBRA_ROUTE_MAX
) {
874 zlog_warn("%s: Specified route type: %d is not a legal value\n",
875 __PRETTY_FUNCTION__
, api
->type
);
879 STREAM_GETW(s
, api
->instance
);
880 STREAM_GETL(s
, api
->flags
);
881 STREAM_GETC(s
, api
->message
);
882 STREAM_GETC(s
, api
->safi
);
885 STREAM_GETC(s
, api
->prefix
.family
);
886 STREAM_GETC(s
, api
->prefix
.prefixlen
);
887 switch (api
->prefix
.family
) {
889 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
) {
899 "%s: v6 prefixlen is %d which should not be more than 128",
900 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
905 zlog_warn("%s: Specified family %d is not v4 or v6",
906 __PRETTY_FUNCTION__
, api
->prefix
.family
);
909 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
911 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
912 api
->src_prefix
.family
= AF_INET6
;
913 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
914 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
916 "%s: SRC Prefix prefixlen received: %d is too large",
917 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
920 STREAM_GET(&api
->src_prefix
.prefix
, s
,
921 PSIZE(api
->src_prefix
.prefixlen
));
923 if (api
->prefix
.family
!= AF_INET6
924 || api
->src_prefix
.prefixlen
== 0) {
926 "%s: SRC prefix specified in some manner that makes no sense",
927 __PRETTY_FUNCTION__
);
933 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
934 STREAM_GETW(s
, api
->nexthop_num
);
935 if (api
->nexthop_num
> MULTIPATH_NUM
) {
936 zlog_warn("%s: invalid number of nexthops (%u)",
937 __func__
, api
->nexthop_num
);
941 for (i
= 0; i
< api
->nexthop_num
; i
++) {
942 api_nh
= &api
->nexthops
[i
];
944 STREAM_GETL(s
, api_nh
->vrf_id
);
945 STREAM_GETC(s
, api_nh
->type
);
946 switch (api_nh
->type
) {
947 case NEXTHOP_TYPE_BLACKHOLE
:
948 STREAM_GETC(s
, api_nh
->bh_type
);
950 case NEXTHOP_TYPE_IPV4
:
951 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
954 case NEXTHOP_TYPE_IPV4_IFINDEX
:
955 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
957 STREAM_GETL(s
, api_nh
->ifindex
);
959 case NEXTHOP_TYPE_IFINDEX
:
960 STREAM_GETL(s
, api_nh
->ifindex
);
962 case NEXTHOP_TYPE_IPV6
:
963 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
965 case NEXTHOP_TYPE_IPV6_IFINDEX
:
966 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
967 STREAM_GETL(s
, api_nh
->ifindex
);
971 "%s: Specified nexthop type %d does not exist",
972 __PRETTY_FUNCTION__
, api_nh
->type
);
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
) {
982 "%s: invalid number of MPLS "
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_GETC(s
, nhr
->nexthops
[i
].type
);
1221 switch (nhr
->nexthops
[i
].type
) {
1222 case NEXTHOP_TYPE_IPV4
:
1223 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1224 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1226 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1228 case NEXTHOP_TYPE_IFINDEX
:
1229 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1231 case NEXTHOP_TYPE_IPV6
:
1232 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1233 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1235 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1237 case NEXTHOP_TYPE_BLACKHOLE
:
1240 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1241 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1242 zlog_warn("%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
);
1372 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1376 /* Lookup vrf by vrf_id. */
1377 vrf
= vrf_lookup_by_id(vrf_id
);
1380 * If a routing protocol doesn't know about a
1381 * vrf that is about to be deleted. There is
1382 * no point in attempting to delete it.
1390 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1392 struct interface
*ifp
;
1393 char ifname_tmp
[INTERFACE_NAMSIZ
];
1395 /* Read interface name. */
1396 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1398 /* Lookup/create interface by name. */
1399 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1401 zebra_interface_if_set_value(s
, ifp
);
1407 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1408 * from zebra server. The format of this message is the same as
1409 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1410 * comments for zebra_interface_add_read), except that no sockaddr_dl
1411 * is sent at the tail of the message.
1413 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1415 struct interface
*ifp
;
1416 char ifname_tmp
[INTERFACE_NAMSIZ
];
1418 /* Read interface name. */
1419 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1421 /* Lookup this by interface index. */
1422 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1424 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1425 ifname_tmp
, vrf_id
);
1429 zebra_interface_if_set_value(s
, ifp
);
1434 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1440 iflp
->lp_status
= stream_getl(s
);
1441 iflp
->te_metric
= stream_getl(s
);
1442 iflp
->max_bw
= stream_getf(s
);
1443 iflp
->max_rsv_bw
= stream_getf(s
);
1444 uint32_t bwclassnum
= stream_getl(s
);
1447 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1448 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1450 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
1451 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1452 " - outdated library?",
1453 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1455 iflp
->admin_grp
= stream_getl(s
);
1456 iflp
->rmt_as
= stream_getl(s
);
1457 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1459 iflp
->av_delay
= stream_getl(s
);
1460 iflp
->min_delay
= stream_getl(s
);
1461 iflp
->max_delay
= stream_getl(s
);
1462 iflp
->delay_var
= stream_getl(s
);
1464 iflp
->pkt_loss
= stream_getf(s
);
1465 iflp
->res_bw
= stream_getf(s
);
1466 iflp
->ava_bw
= stream_getf(s
);
1467 iflp
->use_bw
= stream_getf(s
);
1470 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1472 struct if_link_params
*iflp
;
1477 ifindex
= stream_getl(s
);
1479 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1482 flog_err(LIB_ERR_ZAPI_ENCODE
,
1483 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1488 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1491 link_params_set_value(s
, iflp
);
1496 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1498 uint8_t link_params_status
= 0;
1499 ifindex_t old_ifindex
;
1501 old_ifindex
= ifp
->ifindex
;
1502 /* Read interface's index. */
1503 if_set_index(ifp
, stream_getl(s
));
1504 ifp
->status
= stream_getc(s
);
1506 /* Read interface's value. */
1507 ifp
->flags
= stream_getq(s
);
1508 ifp
->ptm_enable
= stream_getc(s
);
1509 ifp
->ptm_status
= stream_getc(s
);
1510 ifp
->metric
= stream_getl(s
);
1511 ifp
->speed
= stream_getl(s
);
1512 ifp
->mtu
= stream_getl(s
);
1513 ifp
->mtu6
= stream_getl(s
);
1514 ifp
->bandwidth
= stream_getl(s
);
1515 ifp
->ll_type
= stream_getl(s
);
1516 ifp
->hw_addr_len
= stream_getl(s
);
1517 if (ifp
->hw_addr_len
)
1518 stream_get(ifp
->hw_addr
, s
,
1519 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1521 /* Read Traffic Engineering status */
1522 link_params_status
= stream_getc(s
);
1523 /* Then, Traffic Engineering parameters if any */
1524 if (link_params_status
) {
1525 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1526 link_params_set_value(s
, iflp
);
1529 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1532 size_t zebra_interface_link_params_write(struct stream
*s
,
1533 struct interface
*ifp
)
1536 struct if_link_params
*iflp
;
1539 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1542 iflp
= ifp
->link_params
;
1545 w
+= stream_putl(s
, iflp
->lp_status
);
1547 w
+= stream_putl(s
, iflp
->te_metric
);
1548 w
+= stream_putf(s
, iflp
->max_bw
);
1549 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1551 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1552 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1553 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1555 w
+= stream_putl(s
, iflp
->admin_grp
);
1556 w
+= stream_putl(s
, iflp
->rmt_as
);
1557 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1559 w
+= stream_putl(s
, iflp
->av_delay
);
1560 w
+= stream_putl(s
, iflp
->min_delay
);
1561 w
+= stream_putl(s
, iflp
->max_delay
);
1562 w
+= stream_putl(s
, iflp
->delay_var
);
1564 w
+= stream_putf(s
, iflp
->pkt_loss
);
1565 w
+= stream_putf(s
, iflp
->res_bw
);
1566 w
+= stream_putf(s
, iflp
->ava_bw
);
1567 w
+= stream_putf(s
, iflp
->use_bw
);
1573 * format of message for address additon is:
1577 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1578 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1587 * | ifc_flags | flags for connected address
1595 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1603 static int memconstant(const void *s
, int c
, size_t n
)
1605 const uint8_t *p
= s
;
1614 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1618 struct interface
*ifp
;
1619 struct connected
*ifc
;
1620 struct prefix p
, d
, *dp
;
1624 memset(&p
, 0, sizeof(p
));
1625 memset(&d
, 0, sizeof(d
));
1627 /* Get interface index. */
1628 ifindex
= stream_getl(s
);
1631 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1633 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1634 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1640 ifc_flags
= stream_getc(s
);
1642 /* Fetch interface address. */
1643 d
.family
= p
.family
= stream_getc(s
);
1644 plen
= prefix_blen(&d
);
1646 zclient_stream_get_prefix(s
, &p
);
1648 /* Fetch destination address. */
1649 stream_get(&d
.u
.prefix
, s
, plen
);
1651 /* N.B. NULL destination pointers are encoded as all zeroes */
1652 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1654 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1655 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1657 /* N.B. NULL destination pointers are encoded as all
1659 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1662 ifc
->flags
= ifc_flags
;
1663 if (ifc
->destination
)
1664 ifc
->destination
->prefixlen
=
1665 ifc
->address
->prefixlen
;
1666 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1667 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1669 char buf
[PREFIX_STRLEN
];
1671 "warning: interface %s address %s "
1672 "with peer flag set, but no peer address!",
1674 prefix2str(ifc
->address
, buf
,
1676 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1680 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1681 ifc
= connected_delete_by_prefix(ifp
, &p
);
1688 * format of message for neighbor connected address is:
1692 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1693 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1708 * | addr_len | len of addr.
1711 struct nbr_connected
*
1712 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1714 unsigned int ifindex
;
1715 struct interface
*ifp
;
1717 struct nbr_connected
*ifc
;
1719 /* Get interface index. */
1720 ifindex
= stream_getl(s
);
1723 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1725 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1726 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1732 p
.family
= stream_getc(s
);
1733 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1734 p
.prefixlen
= stream_getc(s
);
1736 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1737 /* Currently only supporting P2P links, so any new RA source
1739 considered as the replacement of the previously learnt
1740 Link-Local address. */
1741 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1742 ifc
= nbr_connected_new();
1743 ifc
->address
= prefix_new();
1745 listnode_add(ifp
->nbr_connected
, ifc
);
1748 prefix_copy(ifc
->address
, &p
);
1750 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1752 ifc
= nbr_connected_check(ifp
, &p
);
1754 listnode_delete(ifp
->nbr_connected
, ifc
);
1760 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1762 vrf_id_t
*new_vrf_id
)
1764 unsigned int ifindex
;
1765 struct interface
*ifp
;
1768 /* Get interface index. */
1769 ifindex
= stream_getl(s
);
1771 /* Lookup interface. */
1772 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1774 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1779 /* Fetch new VRF Id. */
1780 new_id
= stream_getw(s
);
1782 *new_vrf_id
= new_id
;
1786 /* filter unwanted messages until the expected one arrives */
1787 static int zclient_read_sync_response(struct zclient
*zclient
,
1788 uint16_t expected_cmd
)
1800 cmd
= expected_cmd
+ 1;
1801 while (ret
== 0 && cmd
!= expected_cmd
) {
1805 /* wait until response arrives */
1807 FD_SET(zclient
->sock
, &readfds
);
1808 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1809 if (!FD_ISSET(zclient
->sock
, &readfds
))
1812 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1813 &version
, &vrf_id
, &cmd
);
1815 zlog_debug("%s: Response (%d bytes) received", __func__
,
1819 flog_err(LIB_ERR_ZAPI_ENCODE
,
1820 "%s: Invalid Sync Message Reply", __func__
);
1827 * Connect to label manager in a syncronous way
1829 * It first writes the request to zcient output buffer and then
1830 * immediately reads the answer from the input buffer.
1832 * @param zclient Zclient used to connect to label manager (zebra)
1833 * @param async Synchronous (0) or asynchronous (1) operation
1834 * @result Result of response
1836 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
1841 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
1842 ZEBRA_LABEL_MANAGER_CONNECT
;
1845 zlog_debug("Connecting to Label Manager (LM)");
1847 if (zclient
->sock
< 0) {
1848 zlog_debug("%s: invalid zclient socket", __func__
);
1855 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1858 stream_putc(s
, zclient
->redist_default
);
1860 stream_putw(s
, zclient
->instance
);
1862 /* Put length at the first point of the stream. */
1863 stream_putw_at(s
, 0, stream_get_endp(s
));
1865 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1867 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
1868 close(zclient
->sock
);
1873 flog_err(LIB_ERR_ZAPI_SOCKET
, "Zclient sock closed");
1874 close(zclient
->sock
);
1879 zlog_debug("LM connect request sent (%d bytes)", ret
);
1885 if (zclient_read_sync_response(zclient
, cmd
)
1891 /* read instance and proto */
1892 uint8_t proto
= stream_getc(s
);
1893 uint16_t instance
= stream_getw(s
);
1896 if (proto
!= zclient
->redist_default
)
1897 flog_err(LIB_ERR_ZAPI_ENCODE
,
1898 "Wrong proto (%u) in LM connect response. Should be %u",
1899 proto
, zclient
->redist_default
);
1900 if (instance
!= zclient
->instance
)
1901 flog_err(LIB_ERR_ZAPI_ENCODE
,
1902 "Wrong instId (%u) in LM connect response. Should be %u",
1903 instance
, zclient
->instance
);
1906 result
= stream_getc(s
);
1908 zlog_debug("LM connect-response received, result %u", result
);
1914 * Asynchronous label chunk request
1916 * @param zclient Zclient used to connect to label manager (zebra)
1917 * @param keep Avoid garbage collection
1918 * @param chunk_size Amount of labels requested
1919 * @result 0 on success, -1 otherwise
1921 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1922 uint32_t chunk_size
)
1927 zlog_debug("Getting Label Chunk");
1929 if (zclient
->sock
< 0)
1935 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1937 stream_putc(s
, zclient
->redist_default
);
1939 stream_putw(s
, zclient
->instance
);
1940 stream_putc(s
, keep
);
1941 stream_putl(s
, chunk_size
);
1943 /* Put length at the first point of the stream. */
1944 stream_putw_at(s
, 0, stream_get_endp(s
));
1946 return zclient_send_message(zclient
);
1950 * Function to request a label chunk in a syncronous way
1952 * It first writes the request to zlcient output buffer and then
1953 * immediately reads the answer from the input buffer.
1955 * @param zclient Zclient used to connect to label manager (zebra)
1956 * @param keep Avoid garbage collection
1957 * @param chunk_size Amount of labels requested
1958 * @param start To write first assigned chunk label to
1959 * @param end To write last assigned chunk label to
1960 * @result 0 on success, -1 otherwise
1962 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1963 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1967 uint8_t response_keep
;
1970 zlog_debug("Getting Label Chunk");
1972 if (zclient
->sock
< 0)
1978 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1980 stream_putc(s
, zclient
->redist_default
);
1982 stream_putw(s
, zclient
->instance
);
1984 stream_putc(s
, keep
);
1986 stream_putl(s
, chunk_size
);
1987 /* Put length at the first point of the stream. */
1988 stream_putw_at(s
, 0, stream_get_endp(s
));
1990 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1992 flog_err(LIB_ERR_ZAPI_SOCKET
,
1993 "Can't write to zclient sock");
1994 close(zclient
->sock
);
1999 flog_err(LIB_ERR_ZAPI_SOCKET
,
2000 "Zclient sock closed");
2001 close(zclient
->sock
);
2006 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2009 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2012 /* parse response */
2015 /* read proto and instance */
2016 uint8_t proto
= stream_getc(s
);
2017 uint16_t instance
= stream_getw(s
);
2020 if (proto
!= zclient
->redist_default
)
2021 flog_err(LIB_ERR_ZAPI_ENCODE
,
2022 "Wrong proto (%u) in get chunk response. Should be %u",
2023 proto
, zclient
->redist_default
);
2024 if (instance
!= zclient
->instance
)
2025 flog_err(LIB_ERR_ZAPI_ENCODE
,
2026 "Wrong instId (%u) in get chunk response Should be %u",
2027 instance
, zclient
->instance
);
2030 response_keep
= stream_getc(s
);
2031 /* start and end labels */
2032 *start
= stream_getl(s
);
2033 *end
= stream_getl(s
);
2035 /* not owning this response */
2036 if (keep
!= response_keep
) {
2037 flog_err(LIB_ERR_ZAPI_ENCODE
,
2038 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2039 *start
, *end
, keep
, response_keep
);
2042 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2043 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2044 flog_err(LIB_ERR_ZAPI_ENCODE
,
2045 "Invalid Label chunk: %u - %u", *start
, *end
);
2050 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2057 * Function to release a label chunk
2059 * @param zclient Zclient used to connect to label manager (zebra)
2060 * @param start First label of chunk
2061 * @param end Last label of chunk
2062 * @result 0 on success, -1 otherwise
2064 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2071 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2073 if (zclient
->sock
< 0)
2079 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2082 stream_putc(s
, zclient
->redist_default
);
2084 stream_putw(s
, zclient
->instance
);
2086 stream_putl(s
, start
);
2088 stream_putl(s
, end
);
2090 /* Put length at the first point of the stream. */
2091 stream_putw_at(s
, 0, stream_get_endp(s
));
2093 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2095 flog_err(LIB_ERR_ZAPI_SOCKET
, "Can't write to zclient sock");
2096 close(zclient
->sock
);
2101 flog_err(LIB_ERR_ZAPI_SOCKET
,
2102 "Zclient sock connection closed");
2103 close(zclient
->sock
);
2112 * Connect to table manager in a syncronous way
2114 * It first writes the request to zcient output buffer and then
2115 * immediately reads the answer from the input buffer.
2117 * @param zclient Zclient used to connect to table manager (zebra)
2118 * @result Result of response
2120 int tm_table_manager_connect(struct zclient
*zclient
)
2127 zlog_debug("Connecting to Table Manager");
2129 if (zclient
->sock
< 0)
2135 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2138 stream_putc(s
, zclient
->redist_default
);
2140 stream_putw(s
, zclient
->instance
);
2142 /* Put length at the first point of the stream. */
2143 stream_putw_at(s
, 0, stream_get_endp(s
));
2145 ret
= zclient_send_message(zclient
);
2150 zlog_debug("%s: Table manager connect request sent", __func__
);
2153 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2159 STREAM_GETC(s
, result
);
2162 "%s: Table Manager connect response received, result %u",
2171 * Function to request a table chunk in a syncronous way
2173 * It first writes the request to zclient output buffer and then
2174 * immediately reads the answer from the input buffer.
2176 * @param zclient Zclient used to connect to table manager (zebra)
2177 * @param chunk_size Amount of table requested
2178 * @param start to write first assigned chunk table RT ID to
2179 * @param end To write last assigned chunk table RT ID to
2180 * @result 0 on success, -1 otherwise
2182 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2183 uint32_t *start
, uint32_t *end
)
2189 zlog_debug("Getting Table Chunk");
2191 if (zclient
->sock
< 0)
2197 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2199 stream_putl(s
, chunk_size
);
2200 /* Put length at the first point of the stream. */
2201 stream_putw_at(s
, 0, stream_get_endp(s
));
2203 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2205 flog_err(LIB_ERR_ZAPI_SOCKET
,
2206 "%s: can't write to zclient->sock", __func__
);
2207 close(zclient
->sock
);
2212 flog_err(LIB_ERR_ZAPI_SOCKET
,
2213 "%s: zclient->sock connection closed", __func__
);
2214 close(zclient
->sock
);
2219 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2223 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2227 /* start and end table IDs */
2228 STREAM_GETL(s
, *start
);
2229 STREAM_GETL(s
, *end
);
2232 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2239 * Function to release a table chunk
2241 * @param zclient Zclient used to connect to table manager (zebra)
2242 * @param start First label of table
2243 * @param end Last label of chunk
2244 * @result 0 on success, -1 otherwise
2246 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2252 zlog_debug("Releasing Table Chunk");
2254 if (zclient
->sock
< 0)
2260 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2263 stream_putl(s
, start
);
2265 stream_putl(s
, end
);
2267 /* Put length at the first point of the stream. */
2268 stream_putw_at(s
, 0, stream_get_endp(s
));
2270 return zclient_send_message(zclient
);
2274 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2282 zclient_create_header(s
, command
, VRF_DEFAULT
);
2283 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2284 stream_putl(s
, pw
->ifindex
);
2287 stream_putl(s
, pw
->type
);
2290 stream_putl(s
, pw
->af
);
2293 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2296 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2299 flog_err(LIB_ERR_ZAPI_ENCODE
,
2300 "%s: unknown af", __func__
);
2305 stream_putl(s
, pw
->local_label
);
2306 stream_putl(s
, pw
->remote_label
);
2309 stream_putc(s
, pw
->flags
);
2311 /* Protocol specific fields */
2312 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2314 /* Put length at the first point of the stream. */
2315 stream_putw_at(s
, 0, stream_get_endp(s
));
2317 return zclient_send_message(zclient
);
2321 * Receive PW status update from Zebra and send it to LDE process.
2323 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2324 zebra_size_t length
, vrf_id_t vrf_id
,
2325 struct zapi_pw_status
*pw
)
2329 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2333 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2334 pw
->ifindex
= stream_getl(s
);
2335 pw
->status
= stream_getl(s
);
2338 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2339 zebra_size_t length
, vrf_id_t vrf_id
)
2341 struct zclient_capabilities cap
;
2342 struct stream
*s
= zclient
->ibuf
;
2343 uint8_t mpls_enabled
;
2345 memset(&cap
, 0, sizeof(cap
));
2346 STREAM_GETC(s
, mpls_enabled
);
2347 cap
.mpls_enabled
= !!mpls_enabled
;
2348 STREAM_GETL(s
, cap
.ecmp
);
2350 if (zclient
->zebra_capabilities
)
2351 (*zclient
->zebra_capabilities
)(&cap
);
2357 /* Zebra client message read function. */
2358 static int zclient_read(struct thread
*thread
)
2361 uint16_t length
, command
;
2362 uint8_t marker
, version
;
2364 struct zclient
*zclient
;
2366 /* Get socket to zebra. */
2367 zclient
= THREAD_ARG(thread
);
2368 zclient
->t_read
= NULL
;
2370 /* Read zebra header (if we don't have it already). */
2371 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2373 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2374 ZEBRA_HEADER_SIZE
- already
))
2379 "zclient connection closed socket [%d].",
2381 return zclient_failed(zclient
);
2383 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2384 /* Try again later. */
2385 zclient_event(ZCLIENT_READ
, zclient
);
2388 already
= ZEBRA_HEADER_SIZE
;
2391 /* Reset to read from the beginning of the incoming packet. */
2392 stream_set_getp(zclient
->ibuf
, 0);
2394 /* Fetch header values. */
2395 length
= stream_getw(zclient
->ibuf
);
2396 marker
= stream_getc(zclient
->ibuf
);
2397 version
= stream_getc(zclient
->ibuf
);
2398 vrf_id
= stream_getl(zclient
->ibuf
);
2399 command
= stream_getw(zclient
->ibuf
);
2401 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2402 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2403 "%s: socket %d version mismatch, marker %d, version %d",
2404 __func__
, zclient
->sock
, marker
, version
);
2405 return zclient_failed(zclient
);
2408 if (length
< ZEBRA_HEADER_SIZE
) {
2409 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
2410 "%s: socket %d message length %u is less than %d ",
2411 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2412 return zclient_failed(zclient
);
2416 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
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
);