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
39 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
40 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
42 /* Zebra client events. */
43 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
45 /* Prototype for event manager. */
46 static void zclient_event(enum event
, struct zclient
*);
48 struct sockaddr_storage zclient_addr
;
49 socklen_t zclient_addr_len
;
51 /* This file local debug flag. */
52 int zclient_debug
= 0;
54 /* Allocate zclient structure. */
55 struct zclient
*zclient_new(struct thread_master
*master
)
57 struct zclient
*zclient
;
58 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
60 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
61 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
62 zclient
->wb
= buffer_new(0);
63 zclient
->master
= master
;
68 /* This function is only called when exiting, because
69 many parts of the code do not check for I/O errors, so they could
70 reference an invalid pointer if the structure was ever freed.
72 Free zclient structure. */
73 void zclient_free(struct zclient
*zclient
)
76 stream_free(zclient
->ibuf
);
78 stream_free(zclient
->obuf
);
80 buffer_free(zclient
->wb
);
82 XFREE(MTYPE_ZCLIENT
, zclient
);
85 u_short
*redist_check_instance(struct redist_proto
*red
, u_short instance
)
87 struct listnode
*node
;
93 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
100 void redist_add_instance(struct redist_proto
*red
, u_short instance
)
107 red
->instances
= list_new();
109 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(u_short
));
111 listnode_add(red
->instances
, in
);
114 void redist_del_instance(struct redist_proto
*red
, u_short instance
)
118 id
= redist_check_instance(red
, instance
);
122 listnode_delete(red
->instances
, id
);
123 XFREE(MTYPE_REDIST_INST
, id
);
124 if (!red
->instances
->count
) {
126 list_free(red
->instances
);
127 red
->instances
= NULL
;
131 /* Stop zebra client services. */
132 void zclient_stop(struct zclient
*zclient
)
138 zlog_debug("zclient stopped");
141 THREAD_OFF(zclient
->t_read
);
142 THREAD_OFF(zclient
->t_connect
);
143 THREAD_OFF(zclient
->t_write
);
146 stream_reset(zclient
->ibuf
);
147 stream_reset(zclient
->obuf
);
149 /* Empty the write buffer. */
150 buffer_reset(zclient
->wb
);
153 if (zclient
->sock
>= 0) {
154 close(zclient
->sock
);
159 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
160 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
161 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
162 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
165 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
169 vrf_bitmap_free(zclient
->default_information
);
170 zclient
->default_information
= VRF_BITMAP_NULL
;
173 void zclient_reset(struct zclient
*zclient
)
177 zclient_stop(zclient
);
179 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
181 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
184 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
);
188 * Connect to zebra daemon.
189 * @param zclient a pointer to zclient structure
190 * @return socket fd just to make sure that connection established
194 int zclient_socket_connect(struct zclient
*zclient
)
199 /* We should think about IPv6 connection. */
200 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
206 /* Connect to zebra. */
207 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
,
211 zlog_warn("%s connect failure: %d(%s)",
212 __PRETTY_FUNCTION__
, errno
,
213 safe_strerror(errno
));
218 zclient
->sock
= sock
;
222 static int zclient_failed(struct zclient
*zclient
)
225 zclient_stop(zclient
);
226 zclient_event(ZCLIENT_CONNECT
, zclient
);
230 static int zclient_flush_data(struct thread
*thread
)
232 struct zclient
*zclient
= THREAD_ARG(thread
);
234 zclient
->t_write
= NULL
;
235 if (zclient
->sock
< 0)
237 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
240 "%s: buffer_flush_available failed on zclient fd %d, closing",
241 __func__
, zclient
->sock
);
242 return zclient_failed(zclient
);
245 zclient
->t_write
= NULL
;
246 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
247 zclient
->sock
, &zclient
->t_write
);
255 int zclient_send_message(struct zclient
*zclient
)
257 if (zclient
->sock
< 0)
259 switch (buffer_write(zclient
->wb
, zclient
->sock
,
260 STREAM_DATA(zclient
->obuf
),
261 stream_get_endp(zclient
->obuf
))) {
263 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
264 __func__
, zclient
->sock
);
265 return zclient_failed(zclient
);
268 THREAD_OFF(zclient
->t_write
);
271 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
272 zclient
->sock
, &zclient
->t_write
);
278 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
280 /* length placeholder, caller can update */
281 stream_putw(s
, ZEBRA_HEADER_SIZE
);
282 stream_putc(s
, ZEBRA_HEADER_MARKER
);
283 stream_putc(s
, ZSERV_VERSION
);
284 stream_putw(s
, vrf_id
);
285 stream_putw(s
, command
);
288 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
289 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
292 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
295 *size
= stream_getw(s
) - ZEBRA_HEADER_SIZE
;
296 *marker
= stream_getc(s
);
297 *version
= stream_getc(s
);
298 *vrf_id
= stream_getw(s
);
299 *cmd
= stream_getw(s
);
301 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
303 "%s: socket %d version mismatch, marker %d, version %d",
304 __func__
, sock
, *marker
, *version
);
308 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
314 /* Send simple Zebra message. */
315 static int zebra_message_send(struct zclient
*zclient
, int command
,
320 /* Get zclient output buffer. */
324 /* Send very simple command only Zebra message. */
325 zclient_create_header(s
, command
, vrf_id
);
327 return zclient_send_message(zclient
);
330 static int zebra_hello_send(struct zclient
*zclient
)
334 if (zclient
->redist_default
) {
338 /* The VRF ID in the HELLO message is always 0. */
339 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
340 stream_putc(s
, zclient
->redist_default
);
341 stream_putw(s
, zclient
->instance
);
342 stream_putw_at(s
, 0, stream_get_endp(s
));
343 return zclient_send_message(zclient
);
349 /* Send register requests to zebra daemon for the information in a VRF. */
350 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
355 /* zclient is disabled. */
356 if (!zclient
->enable
)
359 /* If not connected to the zebra yet. */
360 if (zclient
->sock
< 0)
364 zlog_debug("%s: send register messages for VRF %u", __func__
,
367 /* We need router-id information. */
368 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
370 /* We need interface information. */
371 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
373 /* Set unwanted redistribute route. */
374 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
375 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
378 /* Flush all redistribute request. */
379 if (vrf_id
== VRF_DEFAULT
)
380 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
381 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
382 if (zclient
->mi_redist
[afi
][i
].enabled
) {
383 struct listnode
*node
;
386 for (ALL_LIST_ELEMENTS_RO(
387 zclient
->mi_redist
[afi
][i
]
390 if (!(i
== zclient
->redist_default
391 && *id
== zclient
->instance
))
392 zebra_redistribute_send(
393 ZEBRA_REDISTRIBUTE_ADD
,
399 /* Flush all redistribute request. */
400 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
401 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
402 if (i
!= zclient
->redist_default
403 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
405 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
409 /* If default information is needed. */
410 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
411 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
415 /* Send unregister requests to zebra daemon for the information in a VRF. */
416 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
421 /* zclient is disabled. */
422 if (!zclient
->enable
)
425 /* If not connected to the zebra yet. */
426 if (zclient
->sock
< 0)
430 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
433 /* We need router-id information. */
434 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
436 /* We need interface information. */
437 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
439 /* Set unwanted redistribute route. */
440 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
441 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
444 /* Flush all redistribute request. */
445 if (vrf_id
== VRF_DEFAULT
)
446 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
447 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
448 if (zclient
->mi_redist
[afi
][i
].enabled
) {
449 struct listnode
*node
;
452 for (ALL_LIST_ELEMENTS_RO(
453 zclient
->mi_redist
[afi
][i
]
456 if (!(i
== zclient
->redist_default
457 && *id
== zclient
->instance
))
458 zebra_redistribute_send(
459 ZEBRA_REDISTRIBUTE_DELETE
,
465 /* Flush all redistribute request. */
466 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
467 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
468 if (i
!= zclient
->redist_default
469 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
471 zebra_redistribute_send(
472 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
475 /* If default information is needed. */
476 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
477 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
481 /* Send request to zebra daemon to start or stop RA. */
482 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
483 struct interface
*ifp
, int enable
,
488 /* zclient is disabled. */
489 if (!zclient
->enable
)
492 /* If not connected to the zebra yet. */
493 if (zclient
->sock
< 0)
496 /* Form and send message. */
501 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
503 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
505 stream_putl(s
, ifp
->ifindex
);
506 stream_putl(s
, ra_interval
);
508 stream_putw_at(s
, 0, stream_get_endp(s
));
510 zclient_send_message(zclient
);
513 /* Make connection to zebra daemon. */
514 int zclient_start(struct zclient
*zclient
)
517 zlog_info("zclient_start is called");
519 /* zclient is disabled. */
520 if (!zclient
->enable
)
523 /* If already connected to the zebra. */
524 if (zclient
->sock
>= 0)
527 /* Check connect thread. */
528 if (zclient
->t_connect
)
531 if (zclient_socket_connect(zclient
) < 0) {
533 zlog_debug("zclient connection fail");
535 zclient_event(ZCLIENT_CONNECT
, zclient
);
539 if (set_nonblocking(zclient
->sock
) < 0)
540 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
543 /* Clear fail count. */
546 zlog_debug("zclient connect success with socket [%d]",
549 /* Create read thread. */
550 zclient_event(ZCLIENT_READ
, zclient
);
552 zebra_hello_send(zclient
);
554 /* Inform the successful connection. */
555 if (zclient
->zebra_connected
)
556 (*zclient
->zebra_connected
)(zclient
);
561 /* Initialize zebra client. Argument redist_default is unwanted
562 redistribute route type. */
563 void zclient_init(struct zclient
*zclient
, int redist_default
, u_short instance
)
567 /* Enable zebra client connection by default. */
570 /* Set -1 to the default socket value. */
573 /* Clear redistribution flags. */
574 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
575 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
576 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
578 /* Set unwanted redistribute route. bgpd does not need BGP route
580 zclient
->redist_default
= redist_default
;
581 zclient
->instance
= instance
;
582 /* Pending: make afi(s) an arg. */
583 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
584 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
587 /* Set default-information redistribute to zero. */
588 zclient
->default_information
= vrf_bitmap_init();
592 zlog_debug("zclient_start is called");
594 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
597 /* This function is a wrapper function for calling zclient_start from
598 timer or event thread. */
599 static int zclient_connect(struct thread
*t
)
601 struct zclient
*zclient
;
603 zclient
= THREAD_ARG(t
);
604 zclient
->t_connect
= NULL
;
607 zlog_debug("zclient_connect is called");
609 return zclient_start(zclient
);
613 * "xdr_encode"-like interface that allows daemon (client) to send
614 * a message to zebra server for a route that needs to be
615 * added/deleted to the kernel. Info about the route is specified
616 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
617 * the info down the zclient socket using the stream_* functions.
619 * The corresponding read ("xdr_decode") function on the server
620 * side is zread_ipv4_add()/zread_ipv4_delete().
622 * 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
623 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624 * | Length (2) | Command | Route Type |
625 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
626 * | ZEBRA Flags | Message Flags | Prefix length |
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * | Destination IPv4 Prefix for route |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
635 * described, as per the Nexthop count. Each nexthop described as:
638 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | IPv4 Nexthop address or Interface Index number |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
643 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
644 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
645 * nexthop information is provided, and the message describes a prefix
646 * to blackhole or reject route.
648 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
649 * infrastructure was built around the traditional (32-bit "gate OR
650 * ifindex") nexthop data unit. A special encoding can be used to feed
651 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
652 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
654 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
655 * - .nexthop_num == .ifindex_num
656 * - .nexthop and .ifindex are filled with gate and ifindex parts of
657 * each compound nexthop, both in the same order
659 * zapi_ipv4_route() will produce two nexthop data units for each such
660 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
661 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
663 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
666 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
669 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
671 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
673 * XXX: No attention paid to alignment.
675 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
676 struct zapi_ipv4
*api
)
686 /* Some checks for labeled-unicast. The current expectation is that each
687 * nexthop is accompanied by a label in the case of labeled-unicast.
689 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
690 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
691 /* We expect prefixes installed with labels and the number to
693 * the number of nexthops.
695 assert(api
->label_num
== api
->nexthop_num
);
698 zclient_create_header(s
, cmd
, api
->vrf_id
);
700 /* Put type and nexthop. */
701 stream_putc(s
, api
->type
);
702 stream_putw(s
, api
->instance
);
703 stream_putl(s
, api
->flags
);
704 stream_putc(s
, api
->message
);
705 stream_putw(s
, api
->safi
);
707 /* Put prefix information. */
708 psize
= PSIZE(p
->prefixlen
);
709 stream_putc(s
, p
->prefixlen
);
710 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
712 /* Nexthop, ifindex, distance and metric information. */
713 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
714 /* traditional 32-bit data units */
715 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
717 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
718 /* XXX assert(api->nexthop_num == 0); */
719 /* XXX assert(api->ifindex_num == 0); */
721 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
723 for (i
= 0; i
< api
->nexthop_num
; i
++) {
724 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
725 stream_put_in_addr(s
, api
->nexthop
[i
]);
726 /* For labeled-unicast, each nexthop is followed by
728 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
729 stream_putl(s
, api
->label
[i
]);
731 for (i
= 0; i
< api
->ifindex_num
; i
++) {
732 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
733 stream_putl(s
, api
->ifindex
[i
]);
737 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
738 stream_putc(s
, api
->distance
);
739 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
740 stream_putl(s
, api
->metric
);
741 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
742 stream_putl(s
, api
->tag
);
743 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
744 stream_putl(s
, api
->mtu
);
746 /* Put length at the first point of the stream. */
747 stream_putw_at(s
, 0, stream_get_endp(s
));
749 return zclient_send_message(zclient
);
752 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
753 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
763 /* Some checks for labeled-unicast. The current expectation is that each
764 * nexthop is accompanied by a label in the case of labeled-unicast.
766 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
767 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
768 /* We expect prefixes installed with labels and the number to
770 * the number of nexthops.
772 assert(api
->label_num
== api
->nexthop_num
);
775 zclient_create_header(s
, cmd
, api
->vrf_id
);
777 /* Put type and nexthop. */
778 stream_putc(s
, api
->type
);
779 stream_putw(s
, api
->instance
);
780 stream_putl(s
, api
->flags
);
781 stream_putc(s
, api
->message
);
782 stream_putw(s
, api
->safi
);
784 /* Put prefix information. */
785 psize
= PSIZE(p
->prefixlen
);
786 stream_putc(s
, p
->prefixlen
);
787 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
789 /* Nexthop, ifindex, distance and metric information. */
790 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
791 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
793 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
794 /* XXX assert(api->nexthop_num == 0); */
795 /* XXX assert(api->ifindex_num == 0); */
797 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
799 for (i
= 0; i
< api
->nexthop_num
; i
++) {
800 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
801 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
802 /* For labeled-unicast, each nexthop is followed by
804 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
805 stream_putl(s
, api
->label
[i
]);
807 for (i
= 0; i
< api
->ifindex_num
; i
++) {
808 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
809 stream_putl(s
, api
->ifindex
[i
]);
813 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
814 stream_putc(s
, api
->distance
);
815 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
816 stream_putl(s
, api
->metric
);
817 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
818 stream_putl(s
, api
->tag
);
819 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
820 stream_putl(s
, api
->mtu
);
822 /* Put length at the first point of the stream. */
823 stream_putw_at(s
, 0, stream_get_endp(s
));
825 return zclient_send_message(zclient
);
828 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
829 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
835 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
837 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
843 /* Some checks for labeled-unicast. The current expectation is that each
844 * nexthop is accompanied by a label in the case of labeled-unicast.
846 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
847 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
848 /* We expect prefixes installed with labels and the number to
850 * the number of nexthops.
852 assert(api
->label_num
== api
->nexthop_num
);
855 zclient_create_header(s
, cmd
, api
->vrf_id
);
857 /* Put type and nexthop. */
858 stream_putc(s
, api
->type
);
859 stream_putw(s
, api
->instance
);
860 stream_putl(s
, api
->flags
);
861 stream_putc(s
, api
->message
);
862 stream_putw(s
, api
->safi
);
864 /* Put prefix information. */
865 psize
= PSIZE(p
->prefixlen
);
866 stream_putc(s
, p
->prefixlen
);
867 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
870 psize
= PSIZE(src_p
->prefixlen
);
871 stream_putc(s
, src_p
->prefixlen
);
872 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
875 /* Nexthop, ifindex, distance and metric information. */
876 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
877 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
879 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
880 /* XXX assert(api->nexthop_num == 0); */
881 /* XXX assert(api->ifindex_num == 0); */
883 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
885 for (i
= 0; i
< api
->nexthop_num
; i
++) {
886 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
887 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
888 /* For labeled-unicast, each nexthop is followed by
890 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
891 stream_putl(s
, api
->label
[i
]);
893 for (i
= 0; i
< api
->ifindex_num
; i
++) {
894 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
895 stream_putl(s
, api
->ifindex
[i
]);
899 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
900 stream_putc(s
, api
->distance
);
901 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
902 stream_putl(s
, api
->metric
);
903 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
904 stream_putl(s
, api
->tag
);
905 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
906 stream_putl(s
, api
->mtu
);
908 /* Put length at the first point of the stream. */
909 stream_putw_at(s
, 0, stream_get_endp(s
));
911 return zclient_send_message(zclient
);
914 int zapi_route(u_char cmd
, struct zclient
*zclient
, struct prefix
*p
,
915 struct prefix_ipv6
*src_p
, struct zapi_route
*api
)
921 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
923 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
929 zclient_create_header(s
, cmd
, api
->vrf_id
);
931 /* Put type and nexthop. */
932 stream_putc(s
, api
->type
);
933 stream_putw(s
, api
->instance
);
934 stream_putl(s
, api
->flags
);
935 stream_putc(s
, api
->message
);
936 stream_putw(s
, api
->safi
);
938 /* Put prefix information. */
939 psize
= PSIZE(p
->prefixlen
);
940 stream_putc(s
, p
->prefixlen
);
941 stream_write(s
, (u_char
*)&p
->u
.prefix
, psize
);
943 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
944 psize
= PSIZE(src_p
->prefixlen
);
945 stream_putc(s
, src_p
->prefixlen
);
946 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
949 /* Nexthop, ifindex, distance and metric information. */
950 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
951 stream_putc(s
, api
->nexthop_num
);
953 for (i
= 0; i
< api
->nexthop_num
; i
++) {
954 stream_putc(s
, api
->nexthop
[i
]->type
);
955 switch (api
->nexthop
[i
]->type
) {
956 case NEXTHOP_TYPE_BLACKHOLE
:
958 case NEXTHOP_TYPE_IPV4
:
959 stream_put_in_addr(s
,
960 &api
->nexthop
[i
]->gate
.ipv4
);
962 /* For labeled-unicast, each nexthop is followed
964 if (CHECK_FLAG(api
->message
,
969 ->nh_label
->label
[0]);
971 case NEXTHOP_TYPE_IPV4_IFINDEX
:
972 stream_put_in_addr(s
,
973 &api
->nexthop
[i
]->gate
.ipv4
);
974 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
976 case NEXTHOP_TYPE_IFINDEX
:
977 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
979 case NEXTHOP_TYPE_IPV6
:
982 (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
,
985 /* For labeled-unicast, each nexthop is followed
987 if (CHECK_FLAG(api
->message
,
992 ->nh_label
->label
[0]);
994 case NEXTHOP_TYPE_IPV6_IFINDEX
:
997 (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
,
999 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
1005 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1006 stream_putc(s
, api
->distance
);
1007 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1008 stream_putl(s
, api
->metric
);
1009 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1010 stream_putl(s
, api
->tag
);
1011 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1012 stream_putl(s
, api
->mtu
);
1014 /* Put length at the first point of the stream. */
1015 stream_putw_at(s
, 0, stream_get_endp(s
));
1017 return zclient_send_message(zclient
);
1021 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1022 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1023 * then set/unset redist[type] in the client handle (a struct zserv) for the
1026 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1027 int type
, u_short instance
, vrf_id_t vrf_id
)
1034 zclient_create_header(s
, command
, vrf_id
);
1035 stream_putc(s
, afi
);
1036 stream_putc(s
, type
);
1037 stream_putw(s
, instance
);
1039 stream_putw_at(s
, 0, stream_get_endp(s
));
1041 return zclient_send_message(zclient
);
1044 /* Get prefix in ZServ format; family should be filled in on prefix */
1045 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1047 size_t plen
= prefix_blen(p
);
1054 stream_get(&p
->u
.prefix
, s
, plen
);
1056 p
->prefixlen
= MIN(plen
* 8, c
);
1059 /* Router-id update from zebra daemon. */
1060 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1062 /* Fetch interface address. */
1063 rid
->family
= stream_getc(s
);
1065 zclient_stream_get_prefix(s
, rid
);
1068 /* Interface addition from zebra daemon. */
1070 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1071 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1073 * 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
1074 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1080 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1084 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1087 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1089 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1091 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1093 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1095 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1098 * | Link Layer Type |
1099 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1100 * | Harware Address Length |
1101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1102 * | Hardware Address if HW lenght different from 0 |
1103 * | ... max INTERFACE_HWADDR_MAX |
1104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1105 * | Link_params? | Whether a link-params follows: 1 or 0.
1106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1107 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1108 * | .... (struct if_link_params). |
1109 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1112 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1115 char vrfname_tmp
[VRF_NAMSIZ
];
1116 struct vrf_data data
;
1118 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1119 /* Read interface name. */
1120 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1122 /* Lookup/create vrf by vrf_id. */
1123 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1129 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1133 /* Lookup vrf by vrf_id. */
1134 vrf
= vrf_lookup_by_id(vrf_id
);
1137 * If a routing protocol doesn't know about a
1138 * vrf that is about to be deleted. There is
1139 * no point in attempting to delete it.
1147 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1149 struct interface
*ifp
;
1150 char ifname_tmp
[INTERFACE_NAMSIZ
];
1152 /* Read interface name. */
1153 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1155 /* Lookup/create interface by name. */
1156 ifp
= if_get_by_name_len(
1157 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
, 0);
1159 zebra_interface_if_set_value(s
, ifp
);
1165 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1166 * from zebra server. The format of this message is the same as
1167 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1168 * comments for zebra_interface_add_read), except that no sockaddr_dl
1169 * is sent at the tail of the message.
1171 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1173 struct interface
*ifp
;
1174 char ifname_tmp
[INTERFACE_NAMSIZ
];
1176 /* Read interface name. */
1177 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1179 /* Lookup this by interface index. */
1180 ifp
= if_lookup_by_name_len(
1181 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
);
1183 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1184 ifname_tmp
, vrf_id
);
1188 zebra_interface_if_set_value(s
, ifp
);
1193 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1199 iflp
->lp_status
= stream_getl(s
);
1200 iflp
->te_metric
= stream_getl(s
);
1201 iflp
->max_bw
= stream_getf(s
);
1202 iflp
->max_rsv_bw
= stream_getf(s
);
1203 uint32_t bwclassnum
= stream_getl(s
);
1206 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1207 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1210 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1211 " - outdated library?",
1212 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1214 iflp
->admin_grp
= stream_getl(s
);
1215 iflp
->rmt_as
= stream_getl(s
);
1216 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1218 iflp
->av_delay
= stream_getl(s
);
1219 iflp
->min_delay
= stream_getl(s
);
1220 iflp
->max_delay
= stream_getl(s
);
1221 iflp
->delay_var
= stream_getl(s
);
1223 iflp
->pkt_loss
= stream_getf(s
);
1224 iflp
->res_bw
= stream_getf(s
);
1225 iflp
->ava_bw
= stream_getf(s
);
1226 iflp
->use_bw
= stream_getf(s
);
1229 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1231 struct if_link_params
*iflp
;
1236 ifindex
= stream_getl(s
);
1238 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1241 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1246 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1249 link_params_set_value(s
, iflp
);
1254 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1256 u_char link_params_status
= 0;
1258 /* Read interface's index. */
1259 ifp
->ifindex
= stream_getl(s
);
1260 ifp
->status
= stream_getc(s
);
1262 /* Read interface's value. */
1263 ifp
->flags
= stream_getq(s
);
1264 ifp
->ptm_enable
= stream_getc(s
);
1265 ifp
->ptm_status
= stream_getc(s
);
1266 ifp
->metric
= stream_getl(s
);
1267 ifp
->speed
= stream_getl(s
);
1268 ifp
->mtu
= stream_getl(s
);
1269 ifp
->mtu6
= stream_getl(s
);
1270 ifp
->bandwidth
= stream_getl(s
);
1271 ifp
->ll_type
= stream_getl(s
);
1272 ifp
->hw_addr_len
= stream_getl(s
);
1273 if (ifp
->hw_addr_len
)
1274 stream_get(ifp
->hw_addr
, s
,
1275 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1277 /* Read Traffic Engineering status */
1278 link_params_status
= stream_getc(s
);
1279 /* Then, Traffic Engineering parameters if any */
1280 if (link_params_status
) {
1281 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1282 link_params_set_value(s
, iflp
);
1286 size_t zebra_interface_link_params_write(struct stream
*s
,
1287 struct interface
*ifp
)
1290 struct if_link_params
*iflp
;
1293 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1296 iflp
= ifp
->link_params
;
1299 w
+= stream_putl(s
, iflp
->lp_status
);
1301 w
+= stream_putl(s
, iflp
->te_metric
);
1302 w
+= stream_putf(s
, iflp
->max_bw
);
1303 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1305 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1306 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1307 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1309 w
+= stream_putl(s
, iflp
->admin_grp
);
1310 w
+= stream_putl(s
, iflp
->rmt_as
);
1311 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1313 w
+= stream_putl(s
, iflp
->av_delay
);
1314 w
+= stream_putl(s
, iflp
->min_delay
);
1315 w
+= stream_putl(s
, iflp
->max_delay
);
1316 w
+= stream_putl(s
, iflp
->delay_var
);
1318 w
+= stream_putf(s
, iflp
->pkt_loss
);
1319 w
+= stream_putf(s
, iflp
->res_bw
);
1320 w
+= stream_putf(s
, iflp
->ava_bw
);
1321 w
+= stream_putf(s
, iflp
->use_bw
);
1327 * format of message for address additon is:
1331 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1332 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1341 * | ifc_flags | flags for connected address
1349 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1357 static int memconstant(const void *s
, int c
, size_t n
)
1359 const u_char
*p
= s
;
1368 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1372 struct interface
*ifp
;
1373 struct connected
*ifc
;
1374 struct prefix p
, d
, *dp
;
1378 memset(&p
, 0, sizeof(p
));
1379 memset(&d
, 0, sizeof(d
));
1381 /* Get interface index. */
1382 ifindex
= stream_getl(s
);
1385 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1387 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1388 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1394 ifc_flags
= stream_getc(s
);
1396 /* Fetch interface address. */
1397 d
.family
= p
.family
= stream_getc(s
);
1398 plen
= prefix_blen(&d
);
1400 zclient_stream_get_prefix(s
, &p
);
1402 /* Fetch destination address. */
1403 stream_get(&d
.u
.prefix
, s
, plen
);
1405 /* N.B. NULL destination pointers are encoded as all zeroes */
1406 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1408 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1409 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1411 /* N.B. NULL destination pointers are encoded as all
1413 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1416 ifc
->flags
= ifc_flags
;
1417 if (ifc
->destination
)
1418 ifc
->destination
->prefixlen
=
1419 ifc
->address
->prefixlen
;
1420 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1421 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1423 char buf
[PREFIX_STRLEN
];
1425 "warning: interface %s address %s "
1426 "with peer flag set, but no peer address!",
1427 ifp
->name
, prefix2str(ifc
->address
, buf
,
1429 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1433 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1434 ifc
= connected_delete_by_prefix(ifp
, &p
);
1441 * format of message for neighbor connected address is:
1445 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1446 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1461 * | addr_len | len of addr.
1464 struct nbr_connected
*
1465 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1467 unsigned int ifindex
;
1468 struct interface
*ifp
;
1470 struct nbr_connected
*ifc
;
1472 /* Get interface index. */
1473 ifindex
= stream_getl(s
);
1476 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1478 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1479 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1485 p
.family
= stream_getc(s
);
1486 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1487 p
.prefixlen
= stream_getc(s
);
1489 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1490 /* Currently only supporting P2P links, so any new RA source
1492 considered as the replacement of the previously learnt
1493 Link-Local address. */
1494 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1495 ifc
= nbr_connected_new();
1496 ifc
->address
= prefix_new();
1498 listnode_add(ifp
->nbr_connected
, ifc
);
1501 prefix_copy(ifc
->address
, &p
);
1503 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1505 ifc
= nbr_connected_check(ifp
, &p
);
1507 listnode_delete(ifp
->nbr_connected
, ifc
);
1513 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1515 vrf_id_t
*new_vrf_id
)
1517 unsigned int ifindex
;
1518 struct interface
*ifp
;
1519 vrf_id_t new_id
= VRF_DEFAULT
;
1521 /* Get interface index. */
1522 ifindex
= stream_getl(s
);
1524 /* Lookup interface. */
1525 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1527 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1532 /* Fetch new VRF Id. */
1533 new_id
= stream_getw(s
);
1535 *new_vrf_id
= new_id
;
1539 /* filter unwanted messages until the expected one arrives */
1540 static int zclient_read_sync_response(struct zclient
*zclient
,
1541 u_int16_t expected_cmd
)
1553 cmd
= expected_cmd
+ 1;
1554 while (ret
== 0 && cmd
!= expected_cmd
) {
1558 /* wait until response arrives */
1560 FD_SET(zclient
->sock
, &readfds
);
1561 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1562 if (!FD_ISSET(zclient
->sock
, &readfds
))
1565 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1566 &version
, &vrf_id
, &cmd
);
1568 zlog_debug("%s: Response (%d bytes) received", __func__
,
1572 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1579 * Connect to label manager in a syncronous way
1581 * It first writes the request to zcient output buffer and then
1582 * immediately reads the answer from the input buffer.
1584 * @param zclient Zclient used to connect to label manager (zebra)
1585 * @result Result of response
1587 int lm_label_manager_connect(struct zclient
*zclient
)
1594 zlog_debug("Connecting to Label Manager");
1596 if (zclient
->sock
< 0)
1602 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1605 stream_putc(s
, zclient
->redist_default
);
1607 stream_putw(s
, zclient
->instance
);
1609 /* Put length at the first point of the stream. */
1610 stream_putw_at(s
, 0, stream_get_endp(s
));
1612 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1614 zlog_err("%s: can't write to zclient->sock", __func__
);
1615 close(zclient
->sock
);
1620 zlog_err("%s: zclient->sock connection closed", __func__
);
1621 close(zclient
->sock
);
1626 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1630 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1636 result
= stream_getc(s
);
1639 "%s: Label Manager connect response received, result %u",
1646 * Function to request a label chunk in a syncronous way
1648 * It first writes the request to zlcient output buffer and then
1649 * immediately reads the answer from the input buffer.
1651 * @param zclient Zclient used to connect to label manager (zebra)
1652 * @param keep Avoid garbage collection
1653 * @param chunk_size Amount of labels requested
1654 * @param start To write first assigned chunk label to
1655 * @param end To write last assigned chunk label to
1656 * @result 0 on success, -1 otherwise
1658 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1659 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1663 u_char response_keep
;
1666 zlog_debug("Getting Label Chunk");
1668 if (zclient
->sock
< 0)
1674 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1676 stream_putc(s
, keep
);
1678 stream_putl(s
, chunk_size
);
1679 /* Put length at the first point of the stream. */
1680 stream_putw_at(s
, 0, stream_get_endp(s
));
1682 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1684 zlog_err("%s: can't write to zclient->sock", __func__
);
1685 close(zclient
->sock
);
1690 zlog_err("%s: zclient->sock connection closed", __func__
);
1691 close(zclient
->sock
);
1696 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1700 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1705 response_keep
= stream_getc(s
);
1706 /* start and end labels */
1707 *start
= stream_getl(s
);
1708 *end
= stream_getl(s
);
1710 /* not owning this response */
1711 if (keep
!= response_keep
) {
1713 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1714 __func__
, *start
, *end
, keep
, response_keep
);
1717 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1718 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1719 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1725 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1732 * Function to release a label chunk
1734 * @param zclient Zclient used to connect to label manager (zebra)
1735 * @param start First label of chunk
1736 * @param end Last label of chunk
1737 * @result 0 on success, -1 otherwise
1739 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1746 zlog_debug("Releasing Label Chunk");
1748 if (zclient
->sock
< 0)
1754 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1757 stream_putl(s
, start
);
1759 stream_putl(s
, end
);
1761 /* Put length at the first point of the stream. */
1762 stream_putw_at(s
, 0, stream_get_endp(s
));
1764 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1766 zlog_err("%s: can't write to zclient->sock", __func__
);
1767 close(zclient
->sock
);
1772 zlog_err("%s: zclient->sock connection closed", __func__
);
1773 close(zclient
->sock
);
1781 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1789 zclient_create_header(s
, command
, VRF_DEFAULT
);
1790 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1791 stream_putl(s
, pw
->ifindex
);
1794 stream_putl(s
, pw
->type
);
1797 stream_putl(s
, pw
->af
);
1800 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1803 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1806 zlog_err("%s: unknown af", __func__
);
1811 stream_putl(s
, pw
->local_label
);
1812 stream_putl(s
, pw
->remote_label
);
1815 stream_putc(s
, pw
->flags
);
1817 /* Protocol specific fields */
1818 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1820 /* Put length at the first point of the stream. */
1821 stream_putw_at(s
, 0, stream_get_endp(s
));
1823 return zclient_send_message(zclient
);
1827 * Receive PW status update from Zebra and send it to LDE process.
1829 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1830 zebra_size_t length
, vrf_id_t vrf_id
,
1831 struct zapi_pw_status
*pw
)
1835 memset(pw
, 0, sizeof(struct zapi_pw_status
));
1839 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
1840 pw
->ifindex
= stream_getl(s
);
1841 pw
->status
= stream_getl(s
);
1844 /* Zebra client message read function. */
1845 static int zclient_read(struct thread
*thread
)
1848 uint16_t length
, command
;
1849 uint8_t marker
, version
;
1851 struct zclient
*zclient
;
1853 /* Get socket to zebra. */
1854 zclient
= THREAD_ARG(thread
);
1855 zclient
->t_read
= NULL
;
1857 /* Read zebra header (if we don't have it already). */
1858 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
1860 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1861 ZEBRA_HEADER_SIZE
- already
))
1866 "zclient connection closed socket [%d].",
1868 return zclient_failed(zclient
);
1870 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
1871 /* Try again later. */
1872 zclient_event(ZCLIENT_READ
, zclient
);
1875 already
= ZEBRA_HEADER_SIZE
;
1878 /* Reset to read from the beginning of the incoming packet. */
1879 stream_set_getp(zclient
->ibuf
, 0);
1881 /* Fetch header values. */
1882 length
= stream_getw(zclient
->ibuf
);
1883 marker
= stream_getc(zclient
->ibuf
);
1884 version
= stream_getc(zclient
->ibuf
);
1885 vrf_id
= stream_getw(zclient
->ibuf
);
1886 command
= stream_getw(zclient
->ibuf
);
1888 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
1890 "%s: socket %d version mismatch, marker %d, version %d",
1891 __func__
, zclient
->sock
, marker
, version
);
1892 return zclient_failed(zclient
);
1895 if (length
< ZEBRA_HEADER_SIZE
) {
1896 zlog_err("%s: socket %d message length %u is less than %d ",
1897 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1898 return zclient_failed(zclient
);
1902 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
1905 "%s: message size %u exceeds buffer size %lu, expanding...",
1906 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1907 ns
= stream_new(length
);
1908 stream_copy(ns
, zclient
->ibuf
);
1909 stream_free(zclient
->ibuf
);
1913 /* Read rest of zebra packet. */
1914 if (already
< length
) {
1916 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1922 "zclient connection closed socket [%d].",
1924 return zclient_failed(zclient
);
1926 if (nbyte
!= (ssize_t
)(length
- already
)) {
1927 /* Try again later. */
1928 zclient_event(ZCLIENT_READ
, zclient
);
1933 length
-= ZEBRA_HEADER_SIZE
;
1936 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
1937 (void *)zclient
, command
, vrf_id
);
1940 case ZEBRA_ROUTER_ID_UPDATE
:
1941 if (zclient
->router_id_update
)
1942 (*zclient
->router_id_update
)(command
, zclient
, length
,
1946 zclient_vrf_add(zclient
, vrf_id
);
1948 case ZEBRA_VRF_DELETE
:
1949 zclient_vrf_delete(zclient
, vrf_id
);
1951 case ZEBRA_INTERFACE_ADD
:
1952 if (zclient
->interface_add
)
1953 (*zclient
->interface_add
)(command
, zclient
, length
,
1956 case ZEBRA_INTERFACE_DELETE
:
1957 if (zclient
->interface_delete
)
1958 (*zclient
->interface_delete
)(command
, zclient
, length
,
1961 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1962 if (zclient
->interface_address_add
)
1963 (*zclient
->interface_address_add
)(command
, zclient
,
1966 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1967 if (zclient
->interface_address_delete
)
1968 (*zclient
->interface_address_delete
)(command
, zclient
,
1971 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1972 if (zclient
->interface_bfd_dest_update
)
1973 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
1976 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1977 if (zclient
->interface_nbr_address_add
)
1978 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
1981 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1982 if (zclient
->interface_nbr_address_delete
)
1983 (*zclient
->interface_nbr_address_delete
)(
1984 command
, zclient
, length
, vrf_id
);
1986 case ZEBRA_INTERFACE_UP
:
1987 if (zclient
->interface_up
)
1988 (*zclient
->interface_up
)(command
, zclient
, length
,
1991 case ZEBRA_INTERFACE_DOWN
:
1992 if (zclient
->interface_down
)
1993 (*zclient
->interface_down
)(command
, zclient
, length
,
1996 case ZEBRA_INTERFACE_VRF_UPDATE
:
1997 if (zclient
->interface_vrf_update
)
1998 (*zclient
->interface_vrf_update
)(command
, zclient
,
2001 case ZEBRA_NEXTHOP_UPDATE
:
2003 zlog_debug("zclient rcvd nexthop update\n");
2004 if (zclient
->nexthop_update
)
2005 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2008 case ZEBRA_IMPORT_CHECK_UPDATE
:
2010 zlog_debug("zclient rcvd import check update\n");
2011 if (zclient
->import_check_update
)
2012 (*zclient
->import_check_update
)(command
, zclient
,
2015 case ZEBRA_BFD_DEST_REPLAY
:
2016 if (zclient
->bfd_dest_replay
)
2017 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2020 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
2021 if (zclient
->redistribute_route_ipv4_add
)
2022 (*zclient
->redistribute_route_ipv4_add
)(
2023 command
, zclient
, length
, vrf_id
);
2025 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
2026 if (zclient
->redistribute_route_ipv4_del
)
2027 (*zclient
->redistribute_route_ipv4_del
)(
2028 command
, zclient
, length
, vrf_id
);
2030 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
2031 if (zclient
->redistribute_route_ipv6_add
)
2032 (*zclient
->redistribute_route_ipv6_add
)(
2033 command
, zclient
, length
, vrf_id
);
2035 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
2036 if (zclient
->redistribute_route_ipv6_del
)
2037 (*zclient
->redistribute_route_ipv6_del
)(
2038 command
, zclient
, length
, vrf_id
);
2040 case ZEBRA_INTERFACE_LINK_PARAMS
:
2041 if (zclient
->interface_link_params
)
2042 (*zclient
->interface_link_params
)(command
, zclient
,
2045 case ZEBRA_FEC_UPDATE
:
2047 zlog_debug("zclient rcvd fec update\n");
2048 if (zclient
->fec_update
)
2049 (*zclient
->fec_update
)(command
, zclient
, length
);
2052 if (zclient
->local_vni_add
)
2053 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2057 if (zclient
->local_vni_del
)
2058 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2061 case ZEBRA_MACIP_ADD
:
2062 if (zclient
->local_macip_add
)
2063 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2066 case ZEBRA_MACIP_DEL
:
2067 if (zclient
->local_macip_del
)
2068 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2071 case ZEBRA_PW_STATUS_UPDATE
:
2072 if (zclient
->pw_status_update
)
2073 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2080 if (zclient
->sock
< 0)
2081 /* Connection was closed during packet processing. */
2084 /* Register read thread. */
2085 stream_reset(zclient
->ibuf
);
2086 zclient_event(ZCLIENT_READ
, zclient
);
2091 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2092 int type
, u_short instance
, vrf_id_t vrf_id
)
2096 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2097 if (redist_check_instance(
2098 &zclient
->mi_redist
[afi
][type
], instance
))
2100 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2103 if (!redist_check_instance(
2104 &zclient
->mi_redist
[afi
][type
], instance
))
2106 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2111 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2112 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2115 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2117 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2120 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2124 if (zclient
->sock
> 0)
2125 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2130 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2134 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2135 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2137 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2139 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2141 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2144 if (zclient
->sock
> 0)
2145 zebra_message_send(zclient
, command
, vrf_id
);
2148 static void zclient_event(enum event event
, struct zclient
*zclient
)
2151 case ZCLIENT_SCHEDULE
:
2152 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2153 &zclient
->t_connect
);
2155 case ZCLIENT_CONNECT
:
2158 "zclient connect failures: %d schedule interval is now %d",
2159 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2160 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2161 zclient
->fail
< 3 ? 10 : 60,
2162 &zclient
->t_connect
);
2165 zclient
->t_read
= NULL
;
2166 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2167 zclient
->sock
, &zclient
->t_read
);
2172 void zclient_interface_set_master(struct zclient
*client
,
2173 struct interface
*master
,
2174 struct interface
*slave
)
2181 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2183 stream_putw(s
, master
->vrf_id
);
2184 stream_putl(s
, master
->ifindex
);
2185 stream_putw(s
, slave
->vrf_id
);
2186 stream_putl(s
, slave
->ifindex
);
2188 stream_putw_at(s
, 0, stream_get_endp(s
));
2189 zclient_send_message(client
);