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 /* If not connected to the zebra yet. */
356 if (zclient
->sock
< 0)
360 zlog_debug("%s: send register messages for VRF %u", __func__
,
363 /* We need router-id information. */
364 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
366 /* We need interface information. */
367 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
369 /* Set unwanted redistribute route. */
370 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
371 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
374 /* Flush all redistribute request. */
375 if (vrf_id
== VRF_DEFAULT
)
376 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
377 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
378 if (zclient
->mi_redist
[afi
][i
].enabled
) {
379 struct listnode
*node
;
382 for (ALL_LIST_ELEMENTS_RO(
383 zclient
->mi_redist
[afi
][i
]
386 if (!(i
== zclient
->redist_default
387 && *id
== zclient
->instance
))
388 zebra_redistribute_send(
389 ZEBRA_REDISTRIBUTE_ADD
,
395 /* Flush all redistribute request. */
396 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
397 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
398 if (i
!= zclient
->redist_default
399 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
401 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
405 /* If default information is needed. */
406 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
407 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
411 /* Send unregister requests to zebra daemon for the information in a VRF. */
412 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
417 /* If not connected to the zebra yet. */
418 if (zclient
->sock
< 0)
422 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
425 /* We need router-id information. */
426 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
428 /* We need interface information. */
429 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
431 /* Set unwanted redistribute route. */
432 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
433 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
436 /* Flush all redistribute request. */
437 if (vrf_id
== VRF_DEFAULT
)
438 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
439 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
440 if (zclient
->mi_redist
[afi
][i
].enabled
) {
441 struct listnode
*node
;
444 for (ALL_LIST_ELEMENTS_RO(
445 zclient
->mi_redist
[afi
][i
]
448 if (!(i
== zclient
->redist_default
449 && *id
== zclient
->instance
))
450 zebra_redistribute_send(
451 ZEBRA_REDISTRIBUTE_DELETE
,
457 /* Flush all redistribute request. */
458 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
459 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
460 if (i
!= zclient
->redist_default
461 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
463 zebra_redistribute_send(
464 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
467 /* If default information is needed. */
468 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
469 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
473 /* Send request to zebra daemon to start or stop RA. */
474 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
475 struct interface
*ifp
, int enable
,
480 /* If not connected to the zebra yet. */
481 if (zclient
->sock
< 0)
484 /* Form and send message. */
489 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
491 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
493 stream_putl(s
, ifp
->ifindex
);
494 stream_putl(s
, ra_interval
);
496 stream_putw_at(s
, 0, stream_get_endp(s
));
498 zclient_send_message(zclient
);
501 /* Make connection to zebra daemon. */
502 int zclient_start(struct zclient
*zclient
)
505 zlog_info("zclient_start is called");
507 /* If already connected to the zebra. */
508 if (zclient
->sock
>= 0)
511 /* Check connect thread. */
512 if (zclient
->t_connect
)
515 if (zclient_socket_connect(zclient
) < 0) {
517 zlog_debug("zclient connection fail");
519 zclient_event(ZCLIENT_CONNECT
, zclient
);
523 if (set_nonblocking(zclient
->sock
) < 0)
524 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
527 /* Clear fail count. */
530 zlog_debug("zclient connect success with socket [%d]",
533 /* Create read thread. */
534 zclient_event(ZCLIENT_READ
, zclient
);
536 zebra_hello_send(zclient
);
538 /* Inform the successful connection. */
539 if (zclient
->zebra_connected
)
540 (*zclient
->zebra_connected
)(zclient
);
545 /* Initialize zebra client. Argument redist_default is unwanted
546 redistribute route type. */
547 void zclient_init(struct zclient
*zclient
, int redist_default
, u_short instance
)
551 /* Set -1 to the default socket value. */
554 /* Clear redistribution flags. */
555 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
556 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
557 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
559 /* Set unwanted redistribute route. bgpd does not need BGP route
561 zclient
->redist_default
= redist_default
;
562 zclient
->instance
= instance
;
563 /* Pending: make afi(s) an arg. */
564 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
565 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
568 /* Set default-information redistribute to zero. */
569 zclient
->default_information
= vrf_bitmap_init();
573 zlog_debug("zclient_start is called");
575 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
578 /* This function is a wrapper function for calling zclient_start from
579 timer or event thread. */
580 static int zclient_connect(struct thread
*t
)
582 struct zclient
*zclient
;
584 zclient
= THREAD_ARG(t
);
585 zclient
->t_connect
= NULL
;
588 zlog_debug("zclient_connect is called");
590 return zclient_start(zclient
);
594 * "xdr_encode"-like interface that allows daemon (client) to send
595 * a message to zebra server for a route that needs to be
596 * added/deleted to the kernel. Info about the route is specified
597 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
598 * the info down the zclient socket using the stream_* functions.
600 * The corresponding read ("xdr_decode") function on the server
601 * side is zread_ipv4_add()/zread_ipv4_delete().
603 * 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
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 * | Length (2) | Command | Route Type |
606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 * | ZEBRA Flags | Message Flags | Prefix length |
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * | Destination IPv4 Prefix for route |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
615 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
616 * described, as per the Nexthop count. Each nexthop described as:
619 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
620 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621 * | IPv4 Nexthop address or Interface Index number |
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
625 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
626 * nexthop information is provided, and the message describes a prefix
627 * to blackhole or reject route.
629 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
630 * infrastructure was built around the traditional (32-bit "gate OR
631 * ifindex") nexthop data unit. A special encoding can be used to feed
632 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
633 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
635 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
636 * - .nexthop_num == .ifindex_num
637 * - .nexthop and .ifindex are filled with gate and ifindex parts of
638 * each compound nexthop, both in the same order
640 * zapi_ipv4_route() will produce two nexthop data units for each such
641 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
642 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
644 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
647 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
650 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
652 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
654 * XXX: No attention paid to alignment.
656 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
657 struct zapi_ipv4
*api
)
667 /* Some checks for labeled-unicast. The current expectation is that each
668 * nexthop is accompanied by a label in the case of labeled-unicast.
670 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
671 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
672 /* We expect prefixes installed with labels and the number to
674 * the number of nexthops.
676 assert(api
->label_num
== api
->nexthop_num
);
679 zclient_create_header(s
, cmd
, api
->vrf_id
);
681 /* Put type and nexthop. */
682 stream_putc(s
, api
->type
);
683 stream_putw(s
, api
->instance
);
684 stream_putl(s
, api
->flags
);
685 stream_putc(s
, api
->message
);
686 stream_putw(s
, api
->safi
);
688 /* Put prefix information. */
689 psize
= PSIZE(p
->prefixlen
);
690 stream_putc(s
, p
->prefixlen
);
691 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
693 /* Nexthop, ifindex, distance and metric information. */
694 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
695 /* traditional 32-bit data units */
696 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
698 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
699 /* XXX assert(api->nexthop_num == 0); */
700 /* XXX assert(api->ifindex_num == 0); */
702 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
704 for (i
= 0; i
< api
->nexthop_num
; i
++) {
705 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
706 stream_put_in_addr(s
, api
->nexthop
[i
]);
707 /* For labeled-unicast, each nexthop is followed by
709 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
710 stream_putl(s
, api
->label
[i
]);
712 for (i
= 0; i
< api
->ifindex_num
; i
++) {
713 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
714 stream_putl(s
, api
->ifindex
[i
]);
718 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
719 stream_putc(s
, api
->distance
);
720 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
721 stream_putl(s
, api
->metric
);
722 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
723 stream_putl(s
, api
->tag
);
724 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
725 stream_putl(s
, api
->mtu
);
727 /* Put length at the first point of the stream. */
728 stream_putw_at(s
, 0, stream_get_endp(s
));
730 return zclient_send_message(zclient
);
733 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
734 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
744 /* Some checks for labeled-unicast. The current expectation is that each
745 * nexthop is accompanied by a label in the case of labeled-unicast.
747 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
748 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
749 /* We expect prefixes installed with labels and the number to
751 * the number of nexthops.
753 assert(api
->label_num
== api
->nexthop_num
);
756 zclient_create_header(s
, cmd
, api
->vrf_id
);
758 /* Put type and nexthop. */
759 stream_putc(s
, api
->type
);
760 stream_putw(s
, api
->instance
);
761 stream_putl(s
, api
->flags
);
762 stream_putc(s
, api
->message
);
763 stream_putw(s
, api
->safi
);
765 /* Put prefix information. */
766 psize
= PSIZE(p
->prefixlen
);
767 stream_putc(s
, p
->prefixlen
);
768 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
770 /* Nexthop, ifindex, distance and metric information. */
771 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
772 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
774 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
775 /* XXX assert(api->nexthop_num == 0); */
776 /* XXX assert(api->ifindex_num == 0); */
778 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
780 for (i
= 0; i
< api
->nexthop_num
; i
++) {
781 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
782 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
783 /* For labeled-unicast, each nexthop is followed by
785 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
786 stream_putl(s
, api
->label
[i
]);
788 for (i
= 0; i
< api
->ifindex_num
; i
++) {
789 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
790 stream_putl(s
, api
->ifindex
[i
]);
794 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
795 stream_putc(s
, api
->distance
);
796 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
797 stream_putl(s
, api
->metric
);
798 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
799 stream_putl(s
, api
->tag
);
800 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
801 stream_putl(s
, api
->mtu
);
803 /* Put length at the first point of the stream. */
804 stream_putw_at(s
, 0, stream_get_endp(s
));
806 return zclient_send_message(zclient
);
809 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
810 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
816 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
818 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
824 /* Some checks for labeled-unicast. The current expectation is that each
825 * nexthop is accompanied by a label in the case of labeled-unicast.
827 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
828 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
829 /* We expect prefixes installed with labels and the number to
831 * the number of nexthops.
833 assert(api
->label_num
== api
->nexthop_num
);
836 zclient_create_header(s
, cmd
, api
->vrf_id
);
838 /* Put type and nexthop. */
839 stream_putc(s
, api
->type
);
840 stream_putw(s
, api
->instance
);
841 stream_putl(s
, api
->flags
);
842 stream_putc(s
, api
->message
);
843 stream_putw(s
, api
->safi
);
845 /* Put prefix information. */
846 psize
= PSIZE(p
->prefixlen
);
847 stream_putc(s
, p
->prefixlen
);
848 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
850 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
851 psize
= PSIZE(src_p
->prefixlen
);
852 stream_putc(s
, src_p
->prefixlen
);
853 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
856 /* Nexthop, ifindex, distance and metric information. */
857 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
858 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
860 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
861 /* XXX assert(api->nexthop_num == 0); */
862 /* XXX assert(api->ifindex_num == 0); */
864 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
866 for (i
= 0; i
< api
->nexthop_num
; i
++) {
867 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
868 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
869 /* For labeled-unicast, each nexthop is followed by
871 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
872 stream_putl(s
, api
->label
[i
]);
874 for (i
= 0; i
< api
->ifindex_num
; i
++) {
875 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
876 stream_putl(s
, api
->ifindex
[i
]);
880 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
881 stream_putc(s
, api
->distance
);
882 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
883 stream_putl(s
, api
->metric
);
884 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
885 stream_putl(s
, api
->tag
);
886 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
887 stream_putl(s
, api
->mtu
);
889 /* Put length at the first point of the stream. */
890 stream_putw_at(s
, 0, stream_get_endp(s
));
892 return zclient_send_message(zclient
);
895 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
896 struct zapi_route
*api
)
898 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
900 return zclient_send_message(zclient
);
903 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
905 struct zapi_nexthop
*api_nh
;
910 zclient_create_header(s
, cmd
, api
->vrf_id
);
912 stream_putc(s
, api
->type
);
913 stream_putw(s
, api
->instance
);
914 stream_putl(s
, api
->flags
);
915 stream_putc(s
, api
->message
);
916 stream_putw(s
, api
->safi
);
918 /* Put prefix information. */
919 stream_putc(s
, api
->prefix
.family
);
920 psize
= PSIZE(api
->prefix
.prefixlen
);
921 stream_putc(s
, api
->prefix
.prefixlen
);
922 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
924 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
925 psize
= PSIZE(api
->src_prefix
.prefixlen
);
926 stream_putc(s
, api
->src_prefix
.prefixlen
);
927 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
931 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
932 /* limit the number of nexthops if necessary */
933 if (api
->nexthop_num
> MULTIPATH_NUM
) {
934 char buf
[PREFIX2STR_BUFFER
];
936 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
938 "%s: prefix %s: encoding %u nexthops out of %u",
939 __func__
, buf
, MULTIPATH_NUM
, api
->nexthop_num
);
940 api
->nexthop_num
= MULTIPATH_NUM
;
943 stream_putc(s
, api
->nexthop_num
);
945 for (i
= 0; i
< api
->nexthop_num
; i
++) {
946 api_nh
= &api
->nexthops
[i
];
948 stream_putc(s
, api_nh
->type
);
949 switch (api_nh
->type
) {
950 case NEXTHOP_TYPE_BLACKHOLE
:
952 case NEXTHOP_TYPE_IPV4
:
953 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
955 case NEXTHOP_TYPE_IPV4_IFINDEX
:
956 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
957 stream_putl(s
, api_nh
->ifindex
);
959 case NEXTHOP_TYPE_IFINDEX
:
960 stream_putl(s
, api_nh
->ifindex
);
962 case NEXTHOP_TYPE_IPV6
:
963 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
966 case NEXTHOP_TYPE_IPV6_IFINDEX
:
967 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
969 stream_putl(s
, api_nh
->ifindex
);
973 /* For labeled-unicast, each nexthop is followed
975 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
976 stream_putl(s
, api_nh
->label
);
981 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
982 stream_putc(s
, api
->distance
);
983 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
984 stream_putl(s
, api
->metric
);
985 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
986 stream_putl(s
, api
->tag
);
987 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
988 stream_putl(s
, api
->mtu
);
990 /* Put length at the first point of the stream. */
991 stream_putw_at(s
, 0, stream_get_endp(s
));
996 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
998 struct zapi_nexthop
*api_nh
;
1001 memset(api
, 0, sizeof(*api
));
1003 /* Type, flags, message. */
1004 api
->type
= stream_getc(s
);
1005 api
->instance
= stream_getw(s
);
1006 api
->flags
= stream_getl(s
);
1007 api
->message
= stream_getc(s
);
1008 api
->safi
= stream_getw(s
);
1011 api
->prefix
.family
= stream_getc(s
);
1012 switch (api
->prefix
.family
) {
1014 api
->prefix
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc(s
));
1017 api
->prefix
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc(s
));
1020 stream_get(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1021 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1022 api
->src_prefix
.family
= AF_INET6
;
1023 api
->src_prefix
.prefixlen
= stream_getc(s
);
1024 stream_get(&api
->src_prefix
.prefix
, s
,
1025 PSIZE(api
->src_prefix
.prefixlen
));
1027 if (api
->prefix
.family
!= AF_INET6
1028 || api
->src_prefix
.prefixlen
== 0)
1029 UNSET_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
);
1033 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1034 api
->nexthop_num
= stream_getc(s
);
1035 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1036 zlog_warn("%s: invalid number of nexthops (%u)",
1037 __func__
, api
->nexthop_num
);
1041 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1042 api_nh
= &api
->nexthops
[i
];
1044 api_nh
->type
= stream_getc(s
);
1045 switch (api_nh
->type
) {
1046 case NEXTHOP_TYPE_BLACKHOLE
:
1048 case NEXTHOP_TYPE_IPV4
:
1049 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1051 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1052 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1053 api_nh
->ifindex
= stream_getl(s
);
1055 case NEXTHOP_TYPE_IFINDEX
:
1056 api_nh
->ifindex
= stream_getl(s
);
1058 case NEXTHOP_TYPE_IPV6
:
1059 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1061 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1062 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1063 api_nh
->ifindex
= stream_getl(s
);
1067 /* For labeled-unicast, each nexthop is followed
1069 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1070 stream_get(&api_nh
->label
, s
,
1071 sizeof(api_nh
->label
));
1077 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1078 api
->distance
= stream_getc(s
);
1079 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1080 api
->metric
= stream_getl(s
);
1081 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1082 api
->tag
= stream_getl(s
);
1083 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1084 api
->mtu
= stream_getl(s
);
1090 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1091 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1092 * then set/unset redist[type] in the client handle (a struct zserv) for the
1095 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1096 int type
, u_short instance
, vrf_id_t vrf_id
)
1103 zclient_create_header(s
, command
, vrf_id
);
1104 stream_putc(s
, afi
);
1105 stream_putc(s
, type
);
1106 stream_putw(s
, instance
);
1108 stream_putw_at(s
, 0, stream_get_endp(s
));
1110 return zclient_send_message(zclient
);
1113 /* Get prefix in ZServ format; family should be filled in on prefix */
1114 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1116 size_t plen
= prefix_blen(p
);
1123 stream_get(&p
->u
.prefix
, s
, plen
);
1125 p
->prefixlen
= MIN(plen
* 8, c
);
1128 /* Router-id update from zebra daemon. */
1129 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1131 /* Fetch interface address. */
1132 rid
->family
= stream_getc(s
);
1134 zclient_stream_get_prefix(s
, rid
);
1137 /* Interface addition from zebra daemon. */
1139 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1140 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1142 * 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
1143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1151 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1156 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1164 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1167 * | Link Layer Type |
1168 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1169 * | Harware Address Length |
1170 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1171 * | Hardware Address if HW lenght different from 0 |
1172 * | ... max INTERFACE_HWADDR_MAX |
1173 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1174 * | Link_params? | Whether a link-params follows: 1 or 0.
1175 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1177 * | .... (struct if_link_params). |
1178 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1181 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1184 char vrfname_tmp
[VRF_NAMSIZ
];
1185 struct vrf_data data
;
1187 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1188 /* Read interface name. */
1189 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1191 /* Lookup/create vrf by vrf_id. */
1192 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1198 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1202 /* Lookup vrf by vrf_id. */
1203 vrf
= vrf_lookup_by_id(vrf_id
);
1206 * If a routing protocol doesn't know about a
1207 * vrf that is about to be deleted. There is
1208 * no point in attempting to delete it.
1216 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1218 struct interface
*ifp
;
1219 char ifname_tmp
[INTERFACE_NAMSIZ
];
1221 /* Read interface name. */
1222 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1224 /* Lookup/create interface by name. */
1225 ifp
= if_get_by_name_len(
1226 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
, 0);
1228 zebra_interface_if_set_value(s
, ifp
);
1234 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1235 * from zebra server. The format of this message is the same as
1236 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1237 * comments for zebra_interface_add_read), except that no sockaddr_dl
1238 * is sent at the tail of the message.
1240 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1242 struct interface
*ifp
;
1243 char ifname_tmp
[INTERFACE_NAMSIZ
];
1245 /* Read interface name. */
1246 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1248 /* Lookup this by interface index. */
1249 ifp
= if_lookup_by_name_len(
1250 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
);
1252 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1253 ifname_tmp
, vrf_id
);
1257 zebra_interface_if_set_value(s
, ifp
);
1262 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1268 iflp
->lp_status
= stream_getl(s
);
1269 iflp
->te_metric
= stream_getl(s
);
1270 iflp
->max_bw
= stream_getf(s
);
1271 iflp
->max_rsv_bw
= stream_getf(s
);
1272 uint32_t bwclassnum
= stream_getl(s
);
1275 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1276 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1279 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1280 " - outdated library?",
1281 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1283 iflp
->admin_grp
= stream_getl(s
);
1284 iflp
->rmt_as
= stream_getl(s
);
1285 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1287 iflp
->av_delay
= stream_getl(s
);
1288 iflp
->min_delay
= stream_getl(s
);
1289 iflp
->max_delay
= stream_getl(s
);
1290 iflp
->delay_var
= stream_getl(s
);
1292 iflp
->pkt_loss
= stream_getf(s
);
1293 iflp
->res_bw
= stream_getf(s
);
1294 iflp
->ava_bw
= stream_getf(s
);
1295 iflp
->use_bw
= stream_getf(s
);
1298 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1300 struct if_link_params
*iflp
;
1305 ifindex
= stream_getl(s
);
1307 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1310 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1315 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1318 link_params_set_value(s
, iflp
);
1323 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1325 u_char link_params_status
= 0;
1327 /* Read interface's index. */
1328 ifp
->ifindex
= stream_getl(s
);
1329 ifp
->status
= stream_getc(s
);
1331 /* Read interface's value. */
1332 ifp
->flags
= stream_getq(s
);
1333 ifp
->ptm_enable
= stream_getc(s
);
1334 ifp
->ptm_status
= stream_getc(s
);
1335 ifp
->metric
= stream_getl(s
);
1336 ifp
->speed
= stream_getl(s
);
1337 ifp
->mtu
= stream_getl(s
);
1338 ifp
->mtu6
= stream_getl(s
);
1339 ifp
->bandwidth
= stream_getl(s
);
1340 ifp
->ll_type
= stream_getl(s
);
1341 ifp
->hw_addr_len
= stream_getl(s
);
1342 if (ifp
->hw_addr_len
)
1343 stream_get(ifp
->hw_addr
, s
,
1344 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1346 /* Read Traffic Engineering status */
1347 link_params_status
= stream_getc(s
);
1348 /* Then, Traffic Engineering parameters if any */
1349 if (link_params_status
) {
1350 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1351 link_params_set_value(s
, iflp
);
1355 size_t zebra_interface_link_params_write(struct stream
*s
,
1356 struct interface
*ifp
)
1359 struct if_link_params
*iflp
;
1362 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1365 iflp
= ifp
->link_params
;
1368 w
+= stream_putl(s
, iflp
->lp_status
);
1370 w
+= stream_putl(s
, iflp
->te_metric
);
1371 w
+= stream_putf(s
, iflp
->max_bw
);
1372 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1374 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1375 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1376 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1378 w
+= stream_putl(s
, iflp
->admin_grp
);
1379 w
+= stream_putl(s
, iflp
->rmt_as
);
1380 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1382 w
+= stream_putl(s
, iflp
->av_delay
);
1383 w
+= stream_putl(s
, iflp
->min_delay
);
1384 w
+= stream_putl(s
, iflp
->max_delay
);
1385 w
+= stream_putl(s
, iflp
->delay_var
);
1387 w
+= stream_putf(s
, iflp
->pkt_loss
);
1388 w
+= stream_putf(s
, iflp
->res_bw
);
1389 w
+= stream_putf(s
, iflp
->ava_bw
);
1390 w
+= stream_putf(s
, iflp
->use_bw
);
1396 * format of message for address additon is:
1400 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1401 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1410 * | ifc_flags | flags for connected address
1418 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1426 static int memconstant(const void *s
, int c
, size_t n
)
1428 const u_char
*p
= s
;
1437 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1441 struct interface
*ifp
;
1442 struct connected
*ifc
;
1443 struct prefix p
, d
, *dp
;
1447 memset(&p
, 0, sizeof(p
));
1448 memset(&d
, 0, sizeof(d
));
1450 /* Get interface index. */
1451 ifindex
= stream_getl(s
);
1454 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1456 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1457 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1463 ifc_flags
= stream_getc(s
);
1465 /* Fetch interface address. */
1466 d
.family
= p
.family
= stream_getc(s
);
1467 plen
= prefix_blen(&d
);
1469 zclient_stream_get_prefix(s
, &p
);
1471 /* Fetch destination address. */
1472 stream_get(&d
.u
.prefix
, s
, plen
);
1474 /* N.B. NULL destination pointers are encoded as all zeroes */
1475 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1477 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1478 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1480 /* N.B. NULL destination pointers are encoded as all
1482 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1485 ifc
->flags
= ifc_flags
;
1486 if (ifc
->destination
)
1487 ifc
->destination
->prefixlen
=
1488 ifc
->address
->prefixlen
;
1489 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1490 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1492 char buf
[PREFIX_STRLEN
];
1494 "warning: interface %s address %s "
1495 "with peer flag set, but no peer address!",
1496 ifp
->name
, prefix2str(ifc
->address
, buf
,
1498 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1502 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1503 ifc
= connected_delete_by_prefix(ifp
, &p
);
1510 * format of message for neighbor connected address is:
1514 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1515 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1530 * | addr_len | len of addr.
1533 struct nbr_connected
*
1534 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1536 unsigned int ifindex
;
1537 struct interface
*ifp
;
1539 struct nbr_connected
*ifc
;
1541 /* Get interface index. */
1542 ifindex
= stream_getl(s
);
1545 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1547 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1548 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1554 p
.family
= stream_getc(s
);
1555 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1556 p
.prefixlen
= stream_getc(s
);
1558 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1559 /* Currently only supporting P2P links, so any new RA source
1561 considered as the replacement of the previously learnt
1562 Link-Local address. */
1563 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1564 ifc
= nbr_connected_new();
1565 ifc
->address
= prefix_new();
1567 listnode_add(ifp
->nbr_connected
, ifc
);
1570 prefix_copy(ifc
->address
, &p
);
1572 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1574 ifc
= nbr_connected_check(ifp
, &p
);
1576 listnode_delete(ifp
->nbr_connected
, ifc
);
1582 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1584 vrf_id_t
*new_vrf_id
)
1586 unsigned int ifindex
;
1587 struct interface
*ifp
;
1588 vrf_id_t new_id
= VRF_DEFAULT
;
1590 /* Get interface index. */
1591 ifindex
= stream_getl(s
);
1593 /* Lookup interface. */
1594 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1596 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1601 /* Fetch new VRF Id. */
1602 new_id
= stream_getw(s
);
1604 *new_vrf_id
= new_id
;
1608 /* filter unwanted messages until the expected one arrives */
1609 static int zclient_read_sync_response(struct zclient
*zclient
,
1610 u_int16_t expected_cmd
)
1622 cmd
= expected_cmd
+ 1;
1623 while (ret
== 0 && cmd
!= expected_cmd
) {
1627 /* wait until response arrives */
1629 FD_SET(zclient
->sock
, &readfds
);
1630 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1631 if (!FD_ISSET(zclient
->sock
, &readfds
))
1634 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1635 &version
, &vrf_id
, &cmd
);
1637 zlog_debug("%s: Response (%d bytes) received", __func__
,
1641 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1648 * Connect to label manager in a syncronous way
1650 * It first writes the request to zcient output buffer and then
1651 * immediately reads the answer from the input buffer.
1653 * @param zclient Zclient used to connect to label manager (zebra)
1654 * @result Result of response
1656 int lm_label_manager_connect(struct zclient
*zclient
)
1663 zlog_debug("Connecting to Label Manager");
1665 if (zclient
->sock
< 0)
1671 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1674 stream_putc(s
, zclient
->redist_default
);
1676 stream_putw(s
, zclient
->instance
);
1678 /* Put length at the first point of the stream. */
1679 stream_putw_at(s
, 0, stream_get_endp(s
));
1681 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1683 zlog_err("%s: can't write to zclient->sock", __func__
);
1684 close(zclient
->sock
);
1689 zlog_err("%s: zclient->sock connection closed", __func__
);
1690 close(zclient
->sock
);
1695 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1699 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1705 result
= stream_getc(s
);
1708 "%s: Label Manager connect response received, result %u",
1715 * Function to request a label chunk in a syncronous way
1717 * It first writes the request to zlcient output buffer and then
1718 * immediately reads the answer from the input buffer.
1720 * @param zclient Zclient used to connect to label manager (zebra)
1721 * @param keep Avoid garbage collection
1722 * @param chunk_size Amount of labels requested
1723 * @param start To write first assigned chunk label to
1724 * @param end To write last assigned chunk label to
1725 * @result 0 on success, -1 otherwise
1727 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1728 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1732 u_char response_keep
;
1735 zlog_debug("Getting Label Chunk");
1737 if (zclient
->sock
< 0)
1743 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1745 stream_putc(s
, keep
);
1747 stream_putl(s
, chunk_size
);
1748 /* Put length at the first point of the stream. */
1749 stream_putw_at(s
, 0, stream_get_endp(s
));
1751 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1753 zlog_err("%s: can't write to zclient->sock", __func__
);
1754 close(zclient
->sock
);
1759 zlog_err("%s: zclient->sock connection closed", __func__
);
1760 close(zclient
->sock
);
1765 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1769 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1774 response_keep
= stream_getc(s
);
1775 /* start and end labels */
1776 *start
= stream_getl(s
);
1777 *end
= stream_getl(s
);
1779 /* not owning this response */
1780 if (keep
!= response_keep
) {
1782 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1783 __func__
, *start
, *end
, keep
, response_keep
);
1786 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1787 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1788 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1794 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1801 * Function to release a label chunk
1803 * @param zclient Zclient used to connect to label manager (zebra)
1804 * @param start First label of chunk
1805 * @param end Last label of chunk
1806 * @result 0 on success, -1 otherwise
1808 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1815 zlog_debug("Releasing Label Chunk");
1817 if (zclient
->sock
< 0)
1823 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1826 stream_putl(s
, start
);
1828 stream_putl(s
, end
);
1830 /* Put length at the first point of the stream. */
1831 stream_putw_at(s
, 0, stream_get_endp(s
));
1833 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1835 zlog_err("%s: can't write to zclient->sock", __func__
);
1836 close(zclient
->sock
);
1841 zlog_err("%s: zclient->sock connection closed", __func__
);
1842 close(zclient
->sock
);
1850 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1858 zclient_create_header(s
, command
, VRF_DEFAULT
);
1859 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1860 stream_putl(s
, pw
->ifindex
);
1863 stream_putl(s
, pw
->type
);
1866 stream_putl(s
, pw
->af
);
1869 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1872 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1875 zlog_err("%s: unknown af", __func__
);
1880 stream_putl(s
, pw
->local_label
);
1881 stream_putl(s
, pw
->remote_label
);
1884 stream_putc(s
, pw
->flags
);
1886 /* Protocol specific fields */
1887 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1889 /* Put length at the first point of the stream. */
1890 stream_putw_at(s
, 0, stream_get_endp(s
));
1892 return zclient_send_message(zclient
);
1896 * Receive PW status update from Zebra and send it to LDE process.
1898 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1899 zebra_size_t length
, vrf_id_t vrf_id
,
1900 struct zapi_pw_status
*pw
)
1904 memset(pw
, 0, sizeof(struct zapi_pw_status
));
1908 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
1909 pw
->ifindex
= stream_getl(s
);
1910 pw
->status
= stream_getl(s
);
1913 /* Zebra client message read function. */
1914 static int zclient_read(struct thread
*thread
)
1917 uint16_t length
, command
;
1918 uint8_t marker
, version
;
1920 struct zclient
*zclient
;
1922 /* Get socket to zebra. */
1923 zclient
= THREAD_ARG(thread
);
1924 zclient
->t_read
= NULL
;
1926 /* Read zebra header (if we don't have it already). */
1927 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
1929 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1930 ZEBRA_HEADER_SIZE
- already
))
1935 "zclient connection closed socket [%d].",
1937 return zclient_failed(zclient
);
1939 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
1940 /* Try again later. */
1941 zclient_event(ZCLIENT_READ
, zclient
);
1944 already
= ZEBRA_HEADER_SIZE
;
1947 /* Reset to read from the beginning of the incoming packet. */
1948 stream_set_getp(zclient
->ibuf
, 0);
1950 /* Fetch header values. */
1951 length
= stream_getw(zclient
->ibuf
);
1952 marker
= stream_getc(zclient
->ibuf
);
1953 version
= stream_getc(zclient
->ibuf
);
1954 vrf_id
= stream_getw(zclient
->ibuf
);
1955 command
= stream_getw(zclient
->ibuf
);
1957 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
1959 "%s: socket %d version mismatch, marker %d, version %d",
1960 __func__
, zclient
->sock
, marker
, version
);
1961 return zclient_failed(zclient
);
1964 if (length
< ZEBRA_HEADER_SIZE
) {
1965 zlog_err("%s: socket %d message length %u is less than %d ",
1966 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1967 return zclient_failed(zclient
);
1971 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
1974 "%s: message size %u exceeds buffer size %lu, expanding...",
1975 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1976 ns
= stream_new(length
);
1977 stream_copy(ns
, zclient
->ibuf
);
1978 stream_free(zclient
->ibuf
);
1982 /* Read rest of zebra packet. */
1983 if (already
< length
) {
1985 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1991 "zclient connection closed socket [%d].",
1993 return zclient_failed(zclient
);
1995 if (nbyte
!= (ssize_t
)(length
- already
)) {
1996 /* Try again later. */
1997 zclient_event(ZCLIENT_READ
, zclient
);
2002 length
-= ZEBRA_HEADER_SIZE
;
2005 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2006 (void *)zclient
, command
, vrf_id
);
2009 case ZEBRA_ROUTER_ID_UPDATE
:
2010 if (zclient
->router_id_update
)
2011 (*zclient
->router_id_update
)(command
, zclient
, length
,
2015 zclient_vrf_add(zclient
, vrf_id
);
2017 case ZEBRA_VRF_DELETE
:
2018 zclient_vrf_delete(zclient
, vrf_id
);
2020 case ZEBRA_INTERFACE_ADD
:
2021 if (zclient
->interface_add
)
2022 (*zclient
->interface_add
)(command
, zclient
, length
,
2025 case ZEBRA_INTERFACE_DELETE
:
2026 if (zclient
->interface_delete
)
2027 (*zclient
->interface_delete
)(command
, zclient
, length
,
2030 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2031 if (zclient
->interface_address_add
)
2032 (*zclient
->interface_address_add
)(command
, zclient
,
2035 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2036 if (zclient
->interface_address_delete
)
2037 (*zclient
->interface_address_delete
)(command
, zclient
,
2040 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2041 if (zclient
->interface_bfd_dest_update
)
2042 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2045 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2046 if (zclient
->interface_nbr_address_add
)
2047 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2050 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2051 if (zclient
->interface_nbr_address_delete
)
2052 (*zclient
->interface_nbr_address_delete
)(
2053 command
, zclient
, length
, vrf_id
);
2055 case ZEBRA_INTERFACE_UP
:
2056 if (zclient
->interface_up
)
2057 (*zclient
->interface_up
)(command
, zclient
, length
,
2060 case ZEBRA_INTERFACE_DOWN
:
2061 if (zclient
->interface_down
)
2062 (*zclient
->interface_down
)(command
, zclient
, length
,
2065 case ZEBRA_INTERFACE_VRF_UPDATE
:
2066 if (zclient
->interface_vrf_update
)
2067 (*zclient
->interface_vrf_update
)(command
, zclient
,
2070 case ZEBRA_NEXTHOP_UPDATE
:
2072 zlog_debug("zclient rcvd nexthop update\n");
2073 if (zclient
->nexthop_update
)
2074 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2077 case ZEBRA_IMPORT_CHECK_UPDATE
:
2079 zlog_debug("zclient rcvd import check update\n");
2080 if (zclient
->import_check_update
)
2081 (*zclient
->import_check_update
)(command
, zclient
,
2084 case ZEBRA_BFD_DEST_REPLAY
:
2085 if (zclient
->bfd_dest_replay
)
2086 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2089 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
2090 if (zclient
->redistribute_route_ipv4_add
)
2091 (*zclient
->redistribute_route_ipv4_add
)(
2092 command
, zclient
, length
, vrf_id
);
2094 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
2095 if (zclient
->redistribute_route_ipv4_del
)
2096 (*zclient
->redistribute_route_ipv4_del
)(
2097 command
, zclient
, length
, vrf_id
);
2099 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
2100 if (zclient
->redistribute_route_ipv6_add
)
2101 (*zclient
->redistribute_route_ipv6_add
)(
2102 command
, zclient
, length
, vrf_id
);
2104 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
2105 if (zclient
->redistribute_route_ipv6_del
)
2106 (*zclient
->redistribute_route_ipv6_del
)(
2107 command
, zclient
, length
, vrf_id
);
2109 case ZEBRA_INTERFACE_LINK_PARAMS
:
2110 if (zclient
->interface_link_params
)
2111 (*zclient
->interface_link_params
)(command
, zclient
,
2114 case ZEBRA_FEC_UPDATE
:
2116 zlog_debug("zclient rcvd fec update\n");
2117 if (zclient
->fec_update
)
2118 (*zclient
->fec_update
)(command
, zclient
, length
);
2121 if (zclient
->local_vni_add
)
2122 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2126 if (zclient
->local_vni_del
)
2127 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2130 case ZEBRA_MACIP_ADD
:
2131 if (zclient
->local_macip_add
)
2132 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2135 case ZEBRA_MACIP_DEL
:
2136 if (zclient
->local_macip_del
)
2137 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2140 case ZEBRA_PW_STATUS_UPDATE
:
2141 if (zclient
->pw_status_update
)
2142 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2149 if (zclient
->sock
< 0)
2150 /* Connection was closed during packet processing. */
2153 /* Register read thread. */
2154 stream_reset(zclient
->ibuf
);
2155 zclient_event(ZCLIENT_READ
, zclient
);
2160 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2161 int type
, u_short instance
, vrf_id_t vrf_id
)
2165 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2166 if (redist_check_instance(
2167 &zclient
->mi_redist
[afi
][type
], instance
))
2169 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2172 if (!redist_check_instance(
2173 &zclient
->mi_redist
[afi
][type
], instance
))
2175 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2180 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2181 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2184 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2186 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2189 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2193 if (zclient
->sock
> 0)
2194 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2199 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2203 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2204 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2206 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2208 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2210 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2213 if (zclient
->sock
> 0)
2214 zebra_message_send(zclient
, command
, vrf_id
);
2217 static void zclient_event(enum event event
, struct zclient
*zclient
)
2220 case ZCLIENT_SCHEDULE
:
2221 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2222 &zclient
->t_connect
);
2224 case ZCLIENT_CONNECT
:
2227 "zclient connect failures: %d schedule interval is now %d",
2228 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2229 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2230 zclient
->fail
< 3 ? 10 : 60,
2231 &zclient
->t_connect
);
2234 zclient
->t_read
= NULL
;
2235 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2236 zclient
->sock
, &zclient
->t_read
);
2241 void zclient_interface_set_master(struct zclient
*client
,
2242 struct interface
*master
,
2243 struct interface
*slave
)
2250 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2252 stream_putw(s
, master
->vrf_id
);
2253 stream_putl(s
, master
->ifindex
);
2254 stream_putw(s
, slave
->vrf_id
);
2255 stream_putl(s
, slave
->ifindex
);
2257 stream_putw_at(s
, 0, stream_get_endp(s
));
2258 zclient_send_message(client
);