1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
41 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
43 /* Zebra client events. */
44 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
46 /* Prototype for event manager. */
47 static void zclient_event(enum event
, struct zclient
*);
49 struct sockaddr_storage zclient_addr
;
50 socklen_t zclient_addr_len
;
52 /* This file local debug flag. */
53 int zclient_debug
= 0;
55 /* Allocate zclient structure. */
56 struct zclient
*zclient_new(struct thread_master
*master
)
58 struct zclient
*zclient
;
59 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
61 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
62 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
63 zclient
->wb
= buffer_new(0);
64 zclient
->master
= master
;
69 /* This function is only called when exiting, because
70 many parts of the code do not check for I/O errors, so they could
71 reference an invalid pointer if the structure was ever freed.
73 Free zclient structure. */
74 void zclient_free(struct zclient
*zclient
)
77 stream_free(zclient
->ibuf
);
79 stream_free(zclient
->obuf
);
81 buffer_free(zclient
->wb
);
83 XFREE(MTYPE_ZCLIENT
, zclient
);
86 u_short
*redist_check_instance(struct redist_proto
*red
, u_short instance
)
88 struct listnode
*node
;
94 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
101 void redist_add_instance(struct redist_proto
*red
, u_short instance
)
108 red
->instances
= list_new();
110 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(u_short
));
112 listnode_add(red
->instances
, in
);
115 void redist_del_instance(struct redist_proto
*red
, u_short instance
)
119 id
= redist_check_instance(red
, instance
);
123 listnode_delete(red
->instances
, id
);
124 XFREE(MTYPE_REDIST_INST
, id
);
125 if (!red
->instances
->count
) {
127 list_delete_and_null(&red
->instances
);
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
,
185 zclient
->instance
, zclient
->privs
);
189 * Connect to zebra daemon.
190 * @param zclient a pointer to zclient structure
191 * @return socket fd just to make sure that connection established
195 int zclient_socket_connect(struct zclient
*zclient
)
200 /* We should think about IPv6 connection. */
201 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
207 zclient
->privs
->change(ZPRIVS_RAISE
);
208 setsockopt_so_sendbuf(sock
, 1048576);
209 zclient
->privs
->change(ZPRIVS_LOWER
);
211 /* Connect to zebra. */
212 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
,
216 zlog_warn("%s connect failure: %d(%s)",
217 __PRETTY_FUNCTION__
, errno
,
218 safe_strerror(errno
));
223 zclient
->sock
= sock
;
227 static int zclient_failed(struct zclient
*zclient
)
230 zclient_stop(zclient
);
231 zclient_event(ZCLIENT_CONNECT
, zclient
);
235 static int zclient_flush_data(struct thread
*thread
)
237 struct zclient
*zclient
= THREAD_ARG(thread
);
239 zclient
->t_write
= NULL
;
240 if (zclient
->sock
< 0)
242 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
245 "%s: buffer_flush_available failed on zclient fd %d, closing",
246 __func__
, zclient
->sock
);
247 return zclient_failed(zclient
);
250 zclient
->t_write
= NULL
;
251 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
252 zclient
->sock
, &zclient
->t_write
);
260 int zclient_send_message(struct zclient
*zclient
)
262 if (zclient
->sock
< 0)
264 switch (buffer_write(zclient
->wb
, zclient
->sock
,
265 STREAM_DATA(zclient
->obuf
),
266 stream_get_endp(zclient
->obuf
))) {
268 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
269 __func__
, zclient
->sock
);
270 return zclient_failed(zclient
);
273 THREAD_OFF(zclient
->t_write
);
276 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
277 zclient
->sock
, &zclient
->t_write
);
283 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
285 /* length placeholder, caller can update */
286 stream_putw(s
, ZEBRA_HEADER_SIZE
);
287 stream_putc(s
, ZEBRA_HEADER_MARKER
);
288 stream_putc(s
, ZSERV_VERSION
);
289 stream_putw(s
, vrf_id
);
290 stream_putw(s
, command
);
293 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
294 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
297 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
300 *size
= stream_getw(s
) - ZEBRA_HEADER_SIZE
;
301 *marker
= stream_getc(s
);
302 *version
= stream_getc(s
);
303 *vrf_id
= stream_getw(s
);
304 *cmd
= stream_getw(s
);
306 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
308 "%s: socket %d version mismatch, marker %d, version %d",
309 __func__
, sock
, *marker
, *version
);
313 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
319 /* Send simple Zebra message. */
320 static int zebra_message_send(struct zclient
*zclient
, int command
,
325 /* Get zclient output buffer. */
329 /* Send very simple command only Zebra message. */
330 zclient_create_header(s
, command
, vrf_id
);
332 return zclient_send_message(zclient
);
335 static int zebra_hello_send(struct zclient
*zclient
)
339 if (zclient
->redist_default
) {
343 /* The VRF ID in the HELLO message is always 0. */
344 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
345 stream_putc(s
, zclient
->redist_default
);
346 stream_putw(s
, zclient
->instance
);
347 stream_putw_at(s
, 0, stream_get_endp(s
));
348 return zclient_send_message(zclient
);
354 /* Send register requests to zebra daemon for the information in a VRF. */
355 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
360 /* If not connected to the zebra yet. */
361 if (zclient
->sock
< 0)
365 zlog_debug("%s: send register messages for VRF %u", __func__
,
368 /* We need router-id information. */
369 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
371 /* We need interface information. */
372 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
374 /* Set unwanted redistribute route. */
375 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
376 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
379 /* Flush all redistribute request. */
380 if (vrf_id
== VRF_DEFAULT
)
381 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
382 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
383 if (zclient
->mi_redist
[afi
][i
].enabled
) {
384 struct listnode
*node
;
387 for (ALL_LIST_ELEMENTS_RO(
388 zclient
->mi_redist
[afi
][i
]
391 if (!(i
== zclient
->redist_default
392 && *id
== zclient
->instance
))
393 zebra_redistribute_send(
394 ZEBRA_REDISTRIBUTE_ADD
,
400 /* Flush all redistribute request. */
401 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
402 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
403 if (i
!= zclient
->redist_default
404 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
406 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
410 /* If default information is needed. */
411 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
412 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
416 /* Send unregister requests to zebra daemon for the information in a VRF. */
417 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
422 /* If not connected to the zebra yet. */
423 if (zclient
->sock
< 0)
427 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
430 /* We need router-id information. */
431 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
433 /* We need interface information. */
434 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
436 /* Set unwanted redistribute route. */
437 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
438 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
441 /* Flush all redistribute request. */
442 if (vrf_id
== VRF_DEFAULT
)
443 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
444 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
445 if (zclient
->mi_redist
[afi
][i
].enabled
) {
446 struct listnode
*node
;
449 for (ALL_LIST_ELEMENTS_RO(
450 zclient
->mi_redist
[afi
][i
]
453 if (!(i
== zclient
->redist_default
454 && *id
== zclient
->instance
))
455 zebra_redistribute_send(
456 ZEBRA_REDISTRIBUTE_DELETE
,
462 /* Flush all redistribute request. */
463 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
464 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
465 if (i
!= zclient
->redist_default
466 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
468 zebra_redistribute_send(
469 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
472 /* If default information is needed. */
473 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
474 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
478 /* Send request to zebra daemon to start or stop RA. */
479 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
480 struct interface
*ifp
, int enable
,
485 /* If not connected to the zebra yet. */
486 if (zclient
->sock
< 0)
489 /* Form and send message. */
494 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
496 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
498 stream_putl(s
, ifp
->ifindex
);
499 stream_putl(s
, ra_interval
);
501 stream_putw_at(s
, 0, stream_get_endp(s
));
503 zclient_send_message(zclient
);
506 /* Make connection to zebra daemon. */
507 int zclient_start(struct zclient
*zclient
)
510 zlog_info("zclient_start is called");
512 /* If already connected to the zebra. */
513 if (zclient
->sock
>= 0)
516 /* Check connect thread. */
517 if (zclient
->t_connect
)
520 if (zclient_socket_connect(zclient
) < 0) {
522 zlog_debug("zclient connection fail");
524 zclient_event(ZCLIENT_CONNECT
, zclient
);
528 if (set_nonblocking(zclient
->sock
) < 0)
529 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
532 /* Clear fail count. */
535 zlog_debug("zclient connect success with socket [%d]",
538 /* Create read thread. */
539 zclient_event(ZCLIENT_READ
, zclient
);
541 zebra_hello_send(zclient
);
543 /* Inform the successful connection. */
544 if (zclient
->zebra_connected
)
545 (*zclient
->zebra_connected
)(zclient
);
550 /* Initialize zebra client. Argument redist_default is unwanted
551 redistribute route type. */
552 void zclient_init(struct zclient
*zclient
, int redist_default
,
553 u_short instance
, struct zebra_privs_t
*privs
)
557 /* Set -1 to the default socket value. */
559 zclient
->privs
= privs
;
561 /* Clear redistribution flags. */
562 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
563 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
564 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
566 /* Set unwanted redistribute route. bgpd does not need BGP route
568 zclient
->redist_default
= redist_default
;
569 zclient
->instance
= instance
;
570 /* Pending: make afi(s) an arg. */
571 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
572 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
575 /* Set default-information redistribute to zero. */
576 zclient
->default_information
= vrf_bitmap_init();
580 zlog_debug("zclient_start is called");
582 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
585 /* This function is a wrapper function for calling zclient_start from
586 timer or event thread. */
587 static int zclient_connect(struct thread
*t
)
589 struct zclient
*zclient
;
591 zclient
= THREAD_ARG(t
);
592 zclient
->t_connect
= NULL
;
595 zlog_debug("zclient_connect is called");
597 return zclient_start(zclient
);
601 * "xdr_encode"-like interface that allows daemon (client) to send
602 * a message to zebra server for a route that needs to be
603 * added/deleted to the kernel. Info about the route is specified
604 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
605 * the info down the zclient socket using the stream_* functions.
607 * The corresponding read ("xdr_decode") function on the server
608 * side is zread_ipv4_add()/zread_ipv4_delete().
610 * 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
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * | Length (2) | Command | Route Type |
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * | ZEBRA Flags | Message Flags | Prefix length |
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * | Destination IPv4 Prefix for route |
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
622 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
623 * described, as per the Nexthop count. Each nexthop described as:
626 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * | IPv4 Nexthop address or Interface Index number |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 * Alternatively, if the route is a blackhole route, then Nexthop count
632 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
635 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
636 * infrastructure was built around the traditional (32-bit "gate OR
637 * ifindex") nexthop data unit. A special encoding can be used to feed
638 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
639 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
641 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
642 * - .nexthop_num == .ifindex_num
643 * - .nexthop and .ifindex are filled with gate and ifindex parts of
644 * each compound nexthop, both in the same order
646 * zapi_ipv4_route() will produce two nexthop data units for each such
647 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
648 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
650 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
653 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
656 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
658 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
660 * XXX: No attention paid to alignment.
662 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
663 struct zapi_ipv4
*api
)
673 /* Some checks for labeled-unicast. The current expectation is that each
674 * nexthop is accompanied by a label in the case of labeled-unicast.
676 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
677 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
678 /* We expect prefixes installed with labels and the number to
680 * the number of nexthops.
682 assert(api
->label_num
== api
->nexthop_num
);
685 zclient_create_header(s
, cmd
, api
->vrf_id
);
687 /* Put type and nexthop. */
688 stream_putc(s
, api
->type
);
689 stream_putw(s
, api
->instance
);
690 stream_putl(s
, api
->flags
);
691 stream_putc(s
, api
->message
);
692 stream_putw(s
, api
->safi
);
694 /* Put prefix information. */
695 psize
= PSIZE(p
->prefixlen
);
696 stream_putc(s
, p
->prefixlen
);
697 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
699 /* Nexthop, ifindex, distance and metric information. */
700 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
701 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
703 for (i
= 0; i
< api
->nexthop_num
; i
++) {
704 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
705 stream_put_in_addr(s
, api
->nexthop
[i
]);
706 /* For labeled-unicast, each nexthop is followed by
708 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
709 stream_putl(s
, api
->label
[i
]);
711 for (i
= 0; i
< api
->ifindex_num
; i
++) {
712 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
713 stream_putl(s
, api
->ifindex
[i
]);
717 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
718 stream_putc(s
, api
->distance
);
719 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
720 stream_putl(s
, api
->metric
);
721 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
722 stream_putl(s
, api
->tag
);
723 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
724 stream_putl(s
, api
->mtu
);
726 /* Put length at the first point of the stream. */
727 stream_putw_at(s
, 0, stream_get_endp(s
));
729 return zclient_send_message(zclient
);
732 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
733 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
743 /* Some checks for labeled-unicast. The current expectation is that each
744 * nexthop is accompanied by a label in the case of labeled-unicast.
746 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
747 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
748 /* We expect prefixes installed with labels and the number to
750 * the number of nexthops.
752 assert(api
->label_num
== api
->nexthop_num
);
755 zclient_create_header(s
, cmd
, api
->vrf_id
);
757 /* Put type and nexthop. */
758 stream_putc(s
, api
->type
);
759 stream_putw(s
, api
->instance
);
760 stream_putl(s
, api
->flags
);
761 stream_putc(s
, api
->message
);
762 stream_putw(s
, api
->safi
);
764 /* Put prefix information. */
765 psize
= PSIZE(p
->prefixlen
);
766 stream_putc(s
, p
->prefixlen
);
767 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
769 /* Nexthop, ifindex, distance and metric information. */
770 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
771 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
773 for (i
= 0; i
< api
->nexthop_num
; i
++) {
774 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
775 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
776 /* For labeled-unicast, each nexthop is followed by
778 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
779 stream_putl(s
, api
->label
[i
]);
781 for (i
= 0; i
< api
->ifindex_num
; i
++) {
782 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
783 stream_putl(s
, api
->ifindex
[i
]);
787 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
788 stream_putc(s
, api
->distance
);
789 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
790 stream_putl(s
, api
->metric
);
791 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
792 stream_putl(s
, api
->tag
);
793 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
794 stream_putl(s
, api
->mtu
);
796 /* Put length at the first point of the stream. */
797 stream_putw_at(s
, 0, stream_get_endp(s
));
799 return zclient_send_message(zclient
);
802 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
803 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
809 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
811 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
817 /* Some checks for labeled-unicast. The current expectation is that each
818 * nexthop is accompanied by a label in the case of labeled-unicast.
820 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
821 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
822 /* We expect prefixes installed with labels and the number to
824 * the number of nexthops.
826 assert(api
->label_num
== api
->nexthop_num
);
829 zclient_create_header(s
, cmd
, api
->vrf_id
);
831 /* Put type and nexthop. */
832 stream_putc(s
, api
->type
);
833 stream_putw(s
, api
->instance
);
834 stream_putl(s
, api
->flags
);
835 stream_putc(s
, api
->message
);
836 stream_putw(s
, api
->safi
);
838 /* Put prefix information. */
839 psize
= PSIZE(p
->prefixlen
);
840 stream_putc(s
, p
->prefixlen
);
841 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
843 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
844 psize
= PSIZE(src_p
->prefixlen
);
845 stream_putc(s
, src_p
->prefixlen
);
846 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
849 /* Nexthop, ifindex, distance and metric information. */
850 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
851 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
853 for (i
= 0; i
< api
->nexthop_num
; i
++) {
854 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
855 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
856 /* For labeled-unicast, each nexthop is followed by
858 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
859 stream_putl(s
, api
->label
[i
]);
861 for (i
= 0; i
< api
->ifindex_num
; i
++) {
862 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
863 stream_putl(s
, api
->ifindex
[i
]);
867 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
868 stream_putc(s
, api
->distance
);
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
870 stream_putl(s
, api
->metric
);
871 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
872 stream_putl(s
, api
->tag
);
873 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
874 stream_putl(s
, api
->mtu
);
876 /* Put length at the first point of the stream. */
877 stream_putw_at(s
, 0, stream_get_endp(s
));
879 return zclient_send_message(zclient
);
882 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
883 struct zapi_route
*api
)
885 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
887 return zclient_send_message(zclient
);
890 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
892 struct zapi_nexthop
*api_nh
;
897 zclient_create_header(s
, cmd
, api
->vrf_id
);
899 stream_putc(s
, api
->type
);
900 stream_putw(s
, api
->instance
);
901 stream_putl(s
, api
->flags
);
902 stream_putc(s
, api
->message
);
903 stream_putw(s
, api
->safi
);
905 /* Put prefix information. */
906 stream_putc(s
, api
->prefix
.family
);
907 psize
= PSIZE(api
->prefix
.prefixlen
);
908 stream_putc(s
, api
->prefix
.prefixlen
);
909 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
911 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
912 psize
= PSIZE(api
->src_prefix
.prefixlen
);
913 stream_putc(s
, api
->src_prefix
.prefixlen
);
914 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
918 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
919 /* limit the number of nexthops if necessary */
920 if (api
->nexthop_num
> MULTIPATH_NUM
) {
921 char buf
[PREFIX2STR_BUFFER
];
923 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
925 "%s: prefix %s: can't encode %u nexthops "
927 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
931 stream_putw(s
, api
->nexthop_num
);
933 for (i
= 0; i
< api
->nexthop_num
; i
++) {
934 api_nh
= &api
->nexthops
[i
];
936 stream_putc(s
, api_nh
->type
);
937 switch (api_nh
->type
) {
938 case NEXTHOP_TYPE_BLACKHOLE
:
939 stream_putc(s
, api_nh
->bh_type
);
941 case NEXTHOP_TYPE_IPV4
:
942 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
944 case NEXTHOP_TYPE_IPV4_IFINDEX
:
945 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
946 stream_putl(s
, api_nh
->ifindex
);
948 case NEXTHOP_TYPE_IFINDEX
:
949 stream_putl(s
, api_nh
->ifindex
);
951 case NEXTHOP_TYPE_IPV6
:
952 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
955 case NEXTHOP_TYPE_IPV6_IFINDEX
:
956 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
958 stream_putl(s
, api_nh
->ifindex
);
962 /* MPLS labels for BGP-LU or Segment Routing */
963 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
964 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
965 char buf
[PREFIX2STR_BUFFER
];
966 prefix2str(&api
->prefix
, buf
,
969 "%s: prefix %s: can't encode "
970 "%u labels (maximum is %u)",
977 stream_putc(s
, api_nh
->label_num
);
978 stream_put(s
, &api_nh
->labels
[0],
980 * sizeof(mpls_label_t
));
986 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
987 stream_putc(s
, api
->distance
);
988 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
989 stream_putl(s
, api
->metric
);
990 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
991 stream_putl(s
, api
->tag
);
992 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
993 stream_putl(s
, api
->mtu
);
995 /* Put length at the first point of the stream. */
996 stream_putw_at(s
, 0, stream_get_endp(s
));
1001 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1003 struct zapi_nexthop
*api_nh
;
1006 memset(api
, 0, sizeof(*api
));
1008 /* Type, flags, message. */
1009 api
->type
= stream_getc(s
);
1010 api
->instance
= stream_getw(s
);
1011 api
->flags
= stream_getl(s
);
1012 api
->message
= stream_getc(s
);
1013 api
->safi
= stream_getw(s
);
1016 api
->prefix
.family
= stream_getc(s
);
1017 switch (api
->prefix
.family
) {
1019 api
->prefix
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc(s
));
1022 api
->prefix
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc(s
));
1025 stream_get(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1026 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1027 api
->src_prefix
.family
= AF_INET6
;
1028 api
->src_prefix
.prefixlen
= stream_getc(s
);
1029 stream_get(&api
->src_prefix
.prefix
, s
,
1030 PSIZE(api
->src_prefix
.prefixlen
));
1032 if (api
->prefix
.family
!= AF_INET6
1033 || api
->src_prefix
.prefixlen
== 0)
1034 UNSET_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
);
1038 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1039 api
->nexthop_num
= stream_getw(s
);
1040 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1041 zlog_warn("%s: invalid number of nexthops (%u)",
1042 __func__
, api
->nexthop_num
);
1046 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1047 api_nh
= &api
->nexthops
[i
];
1049 api_nh
->type
= stream_getc(s
);
1050 switch (api_nh
->type
) {
1051 case NEXTHOP_TYPE_BLACKHOLE
:
1052 api_nh
->bh_type
= stream_getc(s
);
1054 case NEXTHOP_TYPE_IPV4
:
1055 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1057 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1058 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1059 api_nh
->ifindex
= stream_getl(s
);
1061 case NEXTHOP_TYPE_IFINDEX
:
1062 api_nh
->ifindex
= stream_getl(s
);
1064 case NEXTHOP_TYPE_IPV6
:
1065 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1067 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1068 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1069 api_nh
->ifindex
= stream_getl(s
);
1073 /* MPLS labels for BGP-LU or Segment Routing */
1074 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1075 api_nh
->label_num
= stream_getc(s
);
1077 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1079 "%s: invalid number of MPLS "
1081 __func__
, api_nh
->label_num
);
1085 stream_get(&api_nh
->labels
[0], s
,
1087 * sizeof(mpls_label_t
));
1093 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1094 api
->distance
= stream_getc(s
);
1095 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1096 api
->metric
= stream_getl(s
);
1097 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1098 api
->tag
= stream_getl(s
);
1099 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1100 api
->mtu
= stream_getl(s
);
1106 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1107 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1108 * then set/unset redist[type] in the client handle (a struct zserv) for the
1111 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1112 int type
, u_short instance
, vrf_id_t vrf_id
)
1119 zclient_create_header(s
, command
, vrf_id
);
1120 stream_putc(s
, afi
);
1121 stream_putc(s
, type
);
1122 stream_putw(s
, instance
);
1124 stream_putw_at(s
, 0, stream_get_endp(s
));
1126 return zclient_send_message(zclient
);
1129 /* Get prefix in ZServ format; family should be filled in on prefix */
1130 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1132 size_t plen
= prefix_blen(p
);
1139 stream_get(&p
->u
.prefix
, s
, plen
);
1141 p
->prefixlen
= MIN(plen
* 8, c
);
1144 /* Router-id update from zebra daemon. */
1145 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1147 /* Fetch interface address. */
1148 rid
->family
= stream_getc(s
);
1150 zclient_stream_get_prefix(s
, rid
);
1153 /* Interface addition from zebra daemon. */
1155 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1156 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1158 * 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
1159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1167 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1169 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1172 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1174 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1178 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1183 * | Link Layer Type |
1184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1185 * | Harware Address Length |
1186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1187 * | Hardware Address if HW lenght different from 0 |
1188 * | ... max INTERFACE_HWADDR_MAX |
1189 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1190 * | Link_params? | Whether a link-params follows: 1 or 0.
1191 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1192 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1193 * | .... (struct if_link_params). |
1194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1197 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1200 char vrfname_tmp
[VRF_NAMSIZ
];
1201 struct vrf_data data
;
1203 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1204 /* Read interface name. */
1205 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1207 /* Lookup/create vrf by vrf_id. */
1208 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1214 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1218 /* Lookup vrf by vrf_id. */
1219 vrf
= vrf_lookup_by_id(vrf_id
);
1222 * If a routing protocol doesn't know about a
1223 * vrf that is about to be deleted. There is
1224 * no point in attempting to delete it.
1232 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1234 struct interface
*ifp
;
1235 char ifname_tmp
[INTERFACE_NAMSIZ
];
1237 /* Read interface name. */
1238 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1240 /* Lookup/create interface by name. */
1241 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1243 zebra_interface_if_set_value(s
, ifp
);
1249 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1250 * from zebra server. The format of this message is the same as
1251 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1252 * comments for zebra_interface_add_read), except that no sockaddr_dl
1253 * is sent at the tail of the message.
1255 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1257 struct interface
*ifp
;
1258 char ifname_tmp
[INTERFACE_NAMSIZ
];
1260 /* Read interface name. */
1261 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1263 /* Lookup this by interface index. */
1264 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1266 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1267 ifname_tmp
, vrf_id
);
1271 zebra_interface_if_set_value(s
, ifp
);
1276 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1282 iflp
->lp_status
= stream_getl(s
);
1283 iflp
->te_metric
= stream_getl(s
);
1284 iflp
->max_bw
= stream_getf(s
);
1285 iflp
->max_rsv_bw
= stream_getf(s
);
1286 uint32_t bwclassnum
= stream_getl(s
);
1289 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1290 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1293 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1294 " - outdated library?",
1295 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1297 iflp
->admin_grp
= stream_getl(s
);
1298 iflp
->rmt_as
= stream_getl(s
);
1299 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1301 iflp
->av_delay
= stream_getl(s
);
1302 iflp
->min_delay
= stream_getl(s
);
1303 iflp
->max_delay
= stream_getl(s
);
1304 iflp
->delay_var
= stream_getl(s
);
1306 iflp
->pkt_loss
= stream_getf(s
);
1307 iflp
->res_bw
= stream_getf(s
);
1308 iflp
->ava_bw
= stream_getf(s
);
1309 iflp
->use_bw
= stream_getf(s
);
1312 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1314 struct if_link_params
*iflp
;
1319 ifindex
= stream_getl(s
);
1321 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1324 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1329 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1332 link_params_set_value(s
, iflp
);
1337 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1339 u_char link_params_status
= 0;
1341 /* Read interface's index. */
1342 if_set_index(ifp
, stream_getl(s
));
1343 ifp
->status
= stream_getc(s
);
1345 /* Read interface's value. */
1346 ifp
->flags
= stream_getq(s
);
1347 ifp
->ptm_enable
= stream_getc(s
);
1348 ifp
->ptm_status
= stream_getc(s
);
1349 ifp
->metric
= stream_getl(s
);
1350 ifp
->speed
= stream_getl(s
);
1351 ifp
->mtu
= stream_getl(s
);
1352 ifp
->mtu6
= stream_getl(s
);
1353 ifp
->bandwidth
= stream_getl(s
);
1354 ifp
->ll_type
= stream_getl(s
);
1355 ifp
->hw_addr_len
= stream_getl(s
);
1356 if (ifp
->hw_addr_len
)
1357 stream_get(ifp
->hw_addr
, s
,
1358 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1360 /* Read Traffic Engineering status */
1361 link_params_status
= stream_getc(s
);
1362 /* Then, Traffic Engineering parameters if any */
1363 if (link_params_status
) {
1364 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1365 link_params_set_value(s
, iflp
);
1369 size_t zebra_interface_link_params_write(struct stream
*s
,
1370 struct interface
*ifp
)
1373 struct if_link_params
*iflp
;
1376 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1379 iflp
= ifp
->link_params
;
1382 w
+= stream_putl(s
, iflp
->lp_status
);
1384 w
+= stream_putl(s
, iflp
->te_metric
);
1385 w
+= stream_putf(s
, iflp
->max_bw
);
1386 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1388 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1389 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1390 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1392 w
+= stream_putl(s
, iflp
->admin_grp
);
1393 w
+= stream_putl(s
, iflp
->rmt_as
);
1394 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1396 w
+= stream_putl(s
, iflp
->av_delay
);
1397 w
+= stream_putl(s
, iflp
->min_delay
);
1398 w
+= stream_putl(s
, iflp
->max_delay
);
1399 w
+= stream_putl(s
, iflp
->delay_var
);
1401 w
+= stream_putf(s
, iflp
->pkt_loss
);
1402 w
+= stream_putf(s
, iflp
->res_bw
);
1403 w
+= stream_putf(s
, iflp
->ava_bw
);
1404 w
+= stream_putf(s
, iflp
->use_bw
);
1410 * format of message for address additon is:
1414 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1415 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1424 * | ifc_flags | flags for connected address
1432 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1440 static int memconstant(const void *s
, int c
, size_t n
)
1442 const u_char
*p
= s
;
1451 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1455 struct interface
*ifp
;
1456 struct connected
*ifc
;
1457 struct prefix p
, d
, *dp
;
1461 memset(&p
, 0, sizeof(p
));
1462 memset(&d
, 0, sizeof(d
));
1464 /* Get interface index. */
1465 ifindex
= stream_getl(s
);
1468 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1470 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1471 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1477 ifc_flags
= stream_getc(s
);
1479 /* Fetch interface address. */
1480 d
.family
= p
.family
= stream_getc(s
);
1481 plen
= prefix_blen(&d
);
1483 zclient_stream_get_prefix(s
, &p
);
1485 /* Fetch destination address. */
1486 stream_get(&d
.u
.prefix
, s
, plen
);
1488 /* N.B. NULL destination pointers are encoded as all zeroes */
1489 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1491 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1492 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1494 /* N.B. NULL destination pointers are encoded as all
1496 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1499 ifc
->flags
= ifc_flags
;
1500 if (ifc
->destination
)
1501 ifc
->destination
->prefixlen
=
1502 ifc
->address
->prefixlen
;
1503 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1504 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1506 char buf
[PREFIX_STRLEN
];
1508 "warning: interface %s address %s "
1509 "with peer flag set, but no peer address!",
1510 ifp
->name
, prefix2str(ifc
->address
, buf
,
1512 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1516 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1517 ifc
= connected_delete_by_prefix(ifp
, &p
);
1524 * format of message for neighbor connected address is:
1528 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1529 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1544 * | addr_len | len of addr.
1547 struct nbr_connected
*
1548 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1550 unsigned int ifindex
;
1551 struct interface
*ifp
;
1553 struct nbr_connected
*ifc
;
1555 /* Get interface index. */
1556 ifindex
= stream_getl(s
);
1559 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1561 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1562 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1568 p
.family
= stream_getc(s
);
1569 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1570 p
.prefixlen
= stream_getc(s
);
1572 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1573 /* Currently only supporting P2P links, so any new RA source
1575 considered as the replacement of the previously learnt
1576 Link-Local address. */
1577 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1578 ifc
= nbr_connected_new();
1579 ifc
->address
= prefix_new();
1581 listnode_add(ifp
->nbr_connected
, ifc
);
1584 prefix_copy(ifc
->address
, &p
);
1586 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1588 ifc
= nbr_connected_check(ifp
, &p
);
1590 listnode_delete(ifp
->nbr_connected
, ifc
);
1596 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1598 vrf_id_t
*new_vrf_id
)
1600 unsigned int ifindex
;
1601 struct interface
*ifp
;
1602 vrf_id_t new_id
= VRF_DEFAULT
;
1604 /* Get interface index. */
1605 ifindex
= stream_getl(s
);
1607 /* Lookup interface. */
1608 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1610 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1615 /* Fetch new VRF Id. */
1616 new_id
= stream_getw(s
);
1618 *new_vrf_id
= new_id
;
1622 /* filter unwanted messages until the expected one arrives */
1623 static int zclient_read_sync_response(struct zclient
*zclient
,
1624 u_int16_t expected_cmd
)
1627 u_int16_t size
= -1;
1636 cmd
= expected_cmd
+ 1;
1637 while (ret
== 0 && cmd
!= expected_cmd
) {
1641 /* wait until response arrives */
1643 FD_SET(zclient
->sock
, &readfds
);
1644 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1645 if (!FD_ISSET(zclient
->sock
, &readfds
))
1648 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1649 &version
, &vrf_id
, &cmd
);
1651 zlog_debug("%s: Response (%d bytes) received", __func__
,
1655 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1662 * Connect to label manager in a syncronous way
1664 * It first writes the request to zcient output buffer and then
1665 * immediately reads the answer from the input buffer.
1667 * @param zclient Zclient used to connect to label manager (zebra)
1668 * @result Result of response
1670 int lm_label_manager_connect(struct zclient
*zclient
)
1677 zlog_debug("Connecting to Label Manager");
1679 if (zclient
->sock
< 0)
1685 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1688 stream_putc(s
, zclient
->redist_default
);
1690 stream_putw(s
, zclient
->instance
);
1692 /* Put length at the first point of the stream. */
1693 stream_putw_at(s
, 0, stream_get_endp(s
));
1695 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1697 zlog_err("%s: can't write to zclient->sock", __func__
);
1698 close(zclient
->sock
);
1703 zlog_err("%s: zclient->sock connection closed", __func__
);
1704 close(zclient
->sock
);
1709 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1713 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1719 result
= stream_getc(s
);
1722 "%s: Label Manager connect response received, result %u",
1729 * Function to request a label chunk in a syncronous way
1731 * It first writes the request to zlcient output buffer and then
1732 * immediately reads the answer from the input buffer.
1734 * @param zclient Zclient used to connect to label manager (zebra)
1735 * @param keep Avoid garbage collection
1736 * @param chunk_size Amount of labels requested
1737 * @param start To write first assigned chunk label to
1738 * @param end To write last assigned chunk label to
1739 * @result 0 on success, -1 otherwise
1741 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1742 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1746 u_char response_keep
;
1749 zlog_debug("Getting Label Chunk");
1751 if (zclient
->sock
< 0)
1757 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1759 stream_putc(s
, keep
);
1761 stream_putl(s
, chunk_size
);
1762 /* Put length at the first point of the stream. */
1763 stream_putw_at(s
, 0, stream_get_endp(s
));
1765 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1767 zlog_err("%s: can't write to zclient->sock", __func__
);
1768 close(zclient
->sock
);
1773 zlog_err("%s: zclient->sock connection closed", __func__
);
1774 close(zclient
->sock
);
1779 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1783 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1788 response_keep
= stream_getc(s
);
1789 /* start and end labels */
1790 *start
= stream_getl(s
);
1791 *end
= stream_getl(s
);
1793 /* not owning this response */
1794 if (keep
!= response_keep
) {
1796 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1797 __func__
, *start
, *end
, keep
, response_keep
);
1800 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1801 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1802 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1808 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1815 * Function to release a label chunk
1817 * @param zclient Zclient used to connect to label manager (zebra)
1818 * @param start First label of chunk
1819 * @param end Last label of chunk
1820 * @result 0 on success, -1 otherwise
1822 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1829 zlog_debug("Releasing Label Chunk");
1831 if (zclient
->sock
< 0)
1837 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1840 stream_putl(s
, start
);
1842 stream_putl(s
, end
);
1844 /* Put length at the first point of the stream. */
1845 stream_putw_at(s
, 0, stream_get_endp(s
));
1847 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1849 zlog_err("%s: can't write to zclient->sock", __func__
);
1850 close(zclient
->sock
);
1855 zlog_err("%s: zclient->sock connection closed", __func__
);
1856 close(zclient
->sock
);
1864 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1872 zclient_create_header(s
, command
, VRF_DEFAULT
);
1873 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1874 stream_putl(s
, pw
->ifindex
);
1877 stream_putl(s
, pw
->type
);
1880 stream_putl(s
, pw
->af
);
1883 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1886 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1889 zlog_err("%s: unknown af", __func__
);
1894 stream_putl(s
, pw
->local_label
);
1895 stream_putl(s
, pw
->remote_label
);
1898 stream_putc(s
, pw
->flags
);
1900 /* Protocol specific fields */
1901 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1903 /* Put length at the first point of the stream. */
1904 stream_putw_at(s
, 0, stream_get_endp(s
));
1906 return zclient_send_message(zclient
);
1910 * Receive PW status update from Zebra and send it to LDE process.
1912 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1913 zebra_size_t length
, vrf_id_t vrf_id
,
1914 struct zapi_pw_status
*pw
)
1918 memset(pw
, 0, sizeof(struct zapi_pw_status
));
1922 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
1923 pw
->ifindex
= stream_getl(s
);
1924 pw
->status
= stream_getl(s
);
1927 /* Zebra client message read function. */
1928 static int zclient_read(struct thread
*thread
)
1931 uint16_t length
, command
;
1932 uint8_t marker
, version
;
1934 struct zclient
*zclient
;
1936 /* Get socket to zebra. */
1937 zclient
= THREAD_ARG(thread
);
1938 zclient
->t_read
= NULL
;
1940 /* Read zebra header (if we don't have it already). */
1941 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
1943 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1944 ZEBRA_HEADER_SIZE
- already
))
1949 "zclient connection closed socket [%d].",
1951 return zclient_failed(zclient
);
1953 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
1954 /* Try again later. */
1955 zclient_event(ZCLIENT_READ
, zclient
);
1958 already
= ZEBRA_HEADER_SIZE
;
1961 /* Reset to read from the beginning of the incoming packet. */
1962 stream_set_getp(zclient
->ibuf
, 0);
1964 /* Fetch header values. */
1965 length
= stream_getw(zclient
->ibuf
);
1966 marker
= stream_getc(zclient
->ibuf
);
1967 version
= stream_getc(zclient
->ibuf
);
1968 vrf_id
= stream_getw(zclient
->ibuf
);
1969 command
= stream_getw(zclient
->ibuf
);
1971 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
1973 "%s: socket %d version mismatch, marker %d, version %d",
1974 __func__
, zclient
->sock
, marker
, version
);
1975 return zclient_failed(zclient
);
1978 if (length
< ZEBRA_HEADER_SIZE
) {
1979 zlog_err("%s: socket %d message length %u is less than %d ",
1980 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1981 return zclient_failed(zclient
);
1985 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
1988 "%s: message size %u exceeds buffer size %lu, expanding...",
1989 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1990 ns
= stream_new(length
);
1991 stream_copy(ns
, zclient
->ibuf
);
1992 stream_free(zclient
->ibuf
);
1996 /* Read rest of zebra packet. */
1997 if (already
< length
) {
1999 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2005 "zclient connection closed socket [%d].",
2007 return zclient_failed(zclient
);
2009 if (nbyte
!= (ssize_t
)(length
- already
)) {
2010 /* Try again later. */
2011 zclient_event(ZCLIENT_READ
, zclient
);
2016 length
-= ZEBRA_HEADER_SIZE
;
2019 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2020 (void *)zclient
, command
, vrf_id
);
2023 case ZEBRA_ROUTER_ID_UPDATE
:
2024 if (zclient
->router_id_update
)
2025 (*zclient
->router_id_update
)(command
, zclient
, length
,
2029 zclient_vrf_add(zclient
, vrf_id
);
2031 case ZEBRA_VRF_DELETE
:
2032 zclient_vrf_delete(zclient
, vrf_id
);
2034 case ZEBRA_INTERFACE_ADD
:
2035 if (zclient
->interface_add
)
2036 (*zclient
->interface_add
)(command
, zclient
, length
,
2039 case ZEBRA_INTERFACE_DELETE
:
2040 if (zclient
->interface_delete
)
2041 (*zclient
->interface_delete
)(command
, zclient
, length
,
2044 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2045 if (zclient
->interface_address_add
)
2046 (*zclient
->interface_address_add
)(command
, zclient
,
2049 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2050 if (zclient
->interface_address_delete
)
2051 (*zclient
->interface_address_delete
)(command
, zclient
,
2054 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2055 if (zclient
->interface_bfd_dest_update
)
2056 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2059 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2060 if (zclient
->interface_nbr_address_add
)
2061 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2064 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2065 if (zclient
->interface_nbr_address_delete
)
2066 (*zclient
->interface_nbr_address_delete
)(
2067 command
, zclient
, length
, vrf_id
);
2069 case ZEBRA_INTERFACE_UP
:
2070 if (zclient
->interface_up
)
2071 (*zclient
->interface_up
)(command
, zclient
, length
,
2074 case ZEBRA_INTERFACE_DOWN
:
2075 if (zclient
->interface_down
)
2076 (*zclient
->interface_down
)(command
, zclient
, length
,
2079 case ZEBRA_INTERFACE_VRF_UPDATE
:
2080 if (zclient
->interface_vrf_update
)
2081 (*zclient
->interface_vrf_update
)(command
, zclient
,
2084 case ZEBRA_NEXTHOP_UPDATE
:
2086 zlog_debug("zclient rcvd nexthop update\n");
2087 if (zclient
->nexthop_update
)
2088 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2091 case ZEBRA_IMPORT_CHECK_UPDATE
:
2093 zlog_debug("zclient rcvd import check update\n");
2094 if (zclient
->import_check_update
)
2095 (*zclient
->import_check_update
)(command
, zclient
,
2098 case ZEBRA_BFD_DEST_REPLAY
:
2099 if (zclient
->bfd_dest_replay
)
2100 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2103 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2104 if (zclient
->redistribute_route_add
)
2105 (*zclient
->redistribute_route_add
)(command
, zclient
,
2108 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2109 if (zclient
->redistribute_route_del
)
2110 (*zclient
->redistribute_route_del
)(command
, zclient
,
2113 case ZEBRA_INTERFACE_LINK_PARAMS
:
2114 if (zclient
->interface_link_params
)
2115 (*zclient
->interface_link_params
)(command
, zclient
,
2118 case ZEBRA_FEC_UPDATE
:
2120 zlog_debug("zclient rcvd fec update\n");
2121 if (zclient
->fec_update
)
2122 (*zclient
->fec_update
)(command
, zclient
, length
);
2125 if (zclient
->local_vni_add
)
2126 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2130 if (zclient
->local_vni_del
)
2131 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2134 case ZEBRA_MACIP_ADD
:
2135 if (zclient
->local_macip_add
)
2136 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2139 case ZEBRA_MACIP_DEL
:
2140 if (zclient
->local_macip_del
)
2141 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2144 case ZEBRA_PW_STATUS_UPDATE
:
2145 if (zclient
->pw_status_update
)
2146 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2153 if (zclient
->sock
< 0)
2154 /* Connection was closed during packet processing. */
2157 /* Register read thread. */
2158 stream_reset(zclient
->ibuf
);
2159 zclient_event(ZCLIENT_READ
, zclient
);
2164 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2165 int type
, u_short instance
, vrf_id_t vrf_id
)
2169 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2170 if (redist_check_instance(
2171 &zclient
->mi_redist
[afi
][type
], instance
))
2173 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2176 if (!redist_check_instance(
2177 &zclient
->mi_redist
[afi
][type
], instance
))
2179 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2184 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2185 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2188 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2190 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2193 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2197 if (zclient
->sock
> 0)
2198 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2203 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2207 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2208 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2210 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2212 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2214 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2217 if (zclient
->sock
> 0)
2218 zebra_message_send(zclient
, command
, vrf_id
);
2221 static void zclient_event(enum event event
, struct zclient
*zclient
)
2224 case ZCLIENT_SCHEDULE
:
2225 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2226 &zclient
->t_connect
);
2228 case ZCLIENT_CONNECT
:
2231 "zclient connect failures: %d schedule interval is now %d",
2232 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2233 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2234 zclient
->fail
< 3 ? 10 : 60,
2235 &zclient
->t_connect
);
2238 zclient
->t_read
= NULL
;
2239 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2240 zclient
->sock
, &zclient
->t_read
);
2245 void zclient_interface_set_master(struct zclient
*client
,
2246 struct interface
*master
,
2247 struct interface
*slave
)
2254 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2256 stream_putw(s
, master
->vrf_id
);
2257 stream_putl(s
, master
->ifindex
);
2258 stream_putw(s
, slave
->vrf_id
);
2259 stream_putl(s
, slave
->ifindex
);
2261 stream_putw_at(s
, 0, stream_get_endp(s
));
2262 zclient_send_message(client
);