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 route is a blackhole route, then Nexthop count
625 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
628 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
629 * infrastructure was built around the traditional (32-bit "gate OR
630 * ifindex") nexthop data unit. A special encoding can be used to feed
631 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
632 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
634 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
635 * - .nexthop_num == .ifindex_num
636 * - .nexthop and .ifindex are filled with gate and ifindex parts of
637 * each compound nexthop, both in the same order
639 * zapi_ipv4_route() will produce two nexthop data units for each such
640 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
641 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
643 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
646 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
649 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
651 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
653 * XXX: No attention paid to alignment.
655 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
656 struct zapi_ipv4
*api
)
666 /* Some checks for labeled-unicast. The current expectation is that each
667 * nexthop is accompanied by a label in the case of labeled-unicast.
669 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
670 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
671 /* We expect prefixes installed with labels and the number to
673 * the number of nexthops.
675 assert(api
->label_num
== api
->nexthop_num
);
678 zclient_create_header(s
, cmd
, api
->vrf_id
);
680 /* Put type and nexthop. */
681 stream_putc(s
, api
->type
);
682 stream_putw(s
, api
->instance
);
683 stream_putl(s
, api
->flags
);
684 stream_putc(s
, api
->message
);
685 stream_putw(s
, api
->safi
);
687 /* Put prefix information. */
688 psize
= PSIZE(p
->prefixlen
);
689 stream_putc(s
, p
->prefixlen
);
690 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
692 /* Nexthop, ifindex, distance and metric information. */
693 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
694 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
696 for (i
= 0; i
< api
->nexthop_num
; i
++) {
697 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
698 stream_put_in_addr(s
, api
->nexthop
[i
]);
699 /* For labeled-unicast, each nexthop is followed by
701 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
702 stream_putl(s
, api
->label
[i
]);
704 for (i
= 0; i
< api
->ifindex_num
; i
++) {
705 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
706 stream_putl(s
, api
->ifindex
[i
]);
710 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
711 stream_putc(s
, api
->distance
);
712 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
713 stream_putl(s
, api
->metric
);
714 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
715 stream_putl(s
, api
->tag
);
716 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
717 stream_putl(s
, api
->mtu
);
719 /* Put length at the first point of the stream. */
720 stream_putw_at(s
, 0, stream_get_endp(s
));
722 return zclient_send_message(zclient
);
725 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
726 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
736 /* Some checks for labeled-unicast. The current expectation is that each
737 * nexthop is accompanied by a label in the case of labeled-unicast.
739 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
740 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
741 /* We expect prefixes installed with labels and the number to
743 * the number of nexthops.
745 assert(api
->label_num
== api
->nexthop_num
);
748 zclient_create_header(s
, cmd
, api
->vrf_id
);
750 /* Put type and nexthop. */
751 stream_putc(s
, api
->type
);
752 stream_putw(s
, api
->instance
);
753 stream_putl(s
, api
->flags
);
754 stream_putc(s
, api
->message
);
755 stream_putw(s
, api
->safi
);
757 /* Put prefix information. */
758 psize
= PSIZE(p
->prefixlen
);
759 stream_putc(s
, p
->prefixlen
);
760 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
762 /* Nexthop, ifindex, distance and metric information. */
763 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
764 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
766 for (i
= 0; i
< api
->nexthop_num
; i
++) {
767 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
768 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
769 /* For labeled-unicast, each nexthop is followed by
771 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
772 stream_putl(s
, api
->label
[i
]);
774 for (i
= 0; i
< api
->ifindex_num
; i
++) {
775 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
776 stream_putl(s
, api
->ifindex
[i
]);
780 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
781 stream_putc(s
, api
->distance
);
782 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
783 stream_putl(s
, api
->metric
);
784 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
785 stream_putl(s
, api
->tag
);
786 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
787 stream_putl(s
, api
->mtu
);
789 /* Put length at the first point of the stream. */
790 stream_putw_at(s
, 0, stream_get_endp(s
));
792 return zclient_send_message(zclient
);
795 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
796 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
802 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
804 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
810 /* Some checks for labeled-unicast. The current expectation is that each
811 * nexthop is accompanied by a label in the case of labeled-unicast.
813 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
814 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
815 /* We expect prefixes installed with labels and the number to
817 * the number of nexthops.
819 assert(api
->label_num
== api
->nexthop_num
);
822 zclient_create_header(s
, cmd
, api
->vrf_id
);
824 /* Put type and nexthop. */
825 stream_putc(s
, api
->type
);
826 stream_putw(s
, api
->instance
);
827 stream_putl(s
, api
->flags
);
828 stream_putc(s
, api
->message
);
829 stream_putw(s
, api
->safi
);
831 /* Put prefix information. */
832 psize
= PSIZE(p
->prefixlen
);
833 stream_putc(s
, p
->prefixlen
);
834 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
836 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
837 psize
= PSIZE(src_p
->prefixlen
);
838 stream_putc(s
, src_p
->prefixlen
);
839 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
842 /* Nexthop, ifindex, distance and metric information. */
843 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
844 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
846 for (i
= 0; i
< api
->nexthop_num
; i
++) {
847 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
848 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
849 /* For labeled-unicast, each nexthop is followed by
851 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
852 stream_putl(s
, api
->label
[i
]);
854 for (i
= 0; i
< api
->ifindex_num
; i
++) {
855 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
856 stream_putl(s
, api
->ifindex
[i
]);
860 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
861 stream_putc(s
, api
->distance
);
862 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
863 stream_putl(s
, api
->metric
);
864 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
865 stream_putl(s
, api
->tag
);
866 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
867 stream_putl(s
, api
->mtu
);
869 /* Put length at the first point of the stream. */
870 stream_putw_at(s
, 0, stream_get_endp(s
));
872 return zclient_send_message(zclient
);
875 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
876 struct zapi_route
*api
)
878 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
880 return zclient_send_message(zclient
);
883 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
885 struct zapi_nexthop
*api_nh
;
890 zclient_create_header(s
, cmd
, api
->vrf_id
);
892 stream_putc(s
, api
->type
);
893 stream_putw(s
, api
->instance
);
894 stream_putl(s
, api
->flags
);
895 stream_putc(s
, api
->message
);
896 stream_putw(s
, api
->safi
);
898 /* Put prefix information. */
899 stream_putc(s
, api
->prefix
.family
);
900 psize
= PSIZE(api
->prefix
.prefixlen
);
901 stream_putc(s
, api
->prefix
.prefixlen
);
902 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
904 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
905 psize
= PSIZE(api
->src_prefix
.prefixlen
);
906 stream_putc(s
, api
->src_prefix
.prefixlen
);
907 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
911 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
912 /* limit the number of nexthops if necessary */
913 if (api
->nexthop_num
> MULTIPATH_NUM
) {
914 char buf
[PREFIX2STR_BUFFER
];
916 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
918 "%s: prefix %s: can't encode %u nexthops "
920 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
924 stream_putw(s
, api
->nexthop_num
);
926 for (i
= 0; i
< api
->nexthop_num
; i
++) {
927 api_nh
= &api
->nexthops
[i
];
929 stream_putc(s
, api_nh
->type
);
930 switch (api_nh
->type
) {
931 case NEXTHOP_TYPE_BLACKHOLE
:
932 stream_putc(s
, api_nh
->bh_type
);
934 case NEXTHOP_TYPE_IPV4
:
935 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
937 case NEXTHOP_TYPE_IPV4_IFINDEX
:
938 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
939 stream_putl(s
, api_nh
->ifindex
);
941 case NEXTHOP_TYPE_IFINDEX
:
942 stream_putl(s
, api_nh
->ifindex
);
944 case NEXTHOP_TYPE_IPV6
:
945 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
948 case NEXTHOP_TYPE_IPV6_IFINDEX
:
949 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
951 stream_putl(s
, api_nh
->ifindex
);
955 /* MPLS labels for BGP-LU or Segment Routing */
956 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
957 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
958 char buf
[PREFIX2STR_BUFFER
];
959 prefix2str(&api
->prefix
, buf
,
962 "%s: prefix %s: can't encode "
963 "%u labels (maximum is %u)",
970 stream_putc(s
, api_nh
->label_num
);
971 stream_put(s
, &api_nh
->labels
[0],
973 * sizeof(mpls_label_t
));
979 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
980 stream_putc(s
, api
->distance
);
981 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
982 stream_putl(s
, api
->metric
);
983 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
984 stream_putl(s
, api
->tag
);
985 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
986 stream_putl(s
, api
->mtu
);
988 /* Put length at the first point of the stream. */
989 stream_putw_at(s
, 0, stream_get_endp(s
));
994 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
996 struct zapi_nexthop
*api_nh
;
999 memset(api
, 0, sizeof(*api
));
1001 /* Type, flags, message. */
1002 api
->type
= stream_getc(s
);
1003 api
->instance
= stream_getw(s
);
1004 api
->flags
= stream_getl(s
);
1005 api
->message
= stream_getc(s
);
1006 api
->safi
= stream_getw(s
);
1009 api
->prefix
.family
= stream_getc(s
);
1010 switch (api
->prefix
.family
) {
1012 api
->prefix
.prefixlen
= MIN(IPV4_MAX_PREFIXLEN
, stream_getc(s
));
1015 api
->prefix
.prefixlen
= MIN(IPV6_MAX_PREFIXLEN
, stream_getc(s
));
1018 stream_get(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1019 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1020 api
->src_prefix
.family
= AF_INET6
;
1021 api
->src_prefix
.prefixlen
= stream_getc(s
);
1022 stream_get(&api
->src_prefix
.prefix
, s
,
1023 PSIZE(api
->src_prefix
.prefixlen
));
1025 if (api
->prefix
.family
!= AF_INET6
1026 || api
->src_prefix
.prefixlen
== 0)
1027 UNSET_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
);
1031 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1032 api
->nexthop_num
= stream_getw(s
);
1033 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1034 zlog_warn("%s: invalid number of nexthops (%u)",
1035 __func__
, api
->nexthop_num
);
1039 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1040 api_nh
= &api
->nexthops
[i
];
1042 api_nh
->type
= stream_getc(s
);
1043 switch (api_nh
->type
) {
1044 case NEXTHOP_TYPE_BLACKHOLE
:
1045 api_nh
->bh_type
= stream_getc(s
);
1047 case NEXTHOP_TYPE_IPV4
:
1048 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1050 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1051 api_nh
->gate
.ipv4
.s_addr
= stream_get_ipv4(s
);
1052 api_nh
->ifindex
= stream_getl(s
);
1054 case NEXTHOP_TYPE_IFINDEX
:
1055 api_nh
->ifindex
= stream_getl(s
);
1057 case NEXTHOP_TYPE_IPV6
:
1058 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1060 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1061 stream_get(&api_nh
->gate
.ipv6
, s
, 16);
1062 api_nh
->ifindex
= stream_getl(s
);
1066 /* MPLS labels for BGP-LU or Segment Routing */
1067 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1068 api_nh
->label_num
= stream_getc(s
);
1070 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1072 "%s: invalid number of MPLS "
1074 __func__
, api_nh
->label_num
);
1078 stream_get(&api_nh
->labels
[0], s
,
1080 * sizeof(mpls_label_t
));
1086 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1087 api
->distance
= stream_getc(s
);
1088 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1089 api
->metric
= stream_getl(s
);
1090 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1091 api
->tag
= stream_getl(s
);
1092 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1093 api
->mtu
= stream_getl(s
);
1099 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1100 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1101 * then set/unset redist[type] in the client handle (a struct zserv) for the
1104 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1105 int type
, u_short instance
, vrf_id_t vrf_id
)
1112 zclient_create_header(s
, command
, vrf_id
);
1113 stream_putc(s
, afi
);
1114 stream_putc(s
, type
);
1115 stream_putw(s
, instance
);
1117 stream_putw_at(s
, 0, stream_get_endp(s
));
1119 return zclient_send_message(zclient
);
1122 /* Get prefix in ZServ format; family should be filled in on prefix */
1123 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1125 size_t plen
= prefix_blen(p
);
1132 stream_get(&p
->u
.prefix
, s
, plen
);
1134 p
->prefixlen
= MIN(plen
* 8, c
);
1137 /* Router-id update from zebra daemon. */
1138 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1140 /* Fetch interface address. */
1141 rid
->family
= stream_getc(s
);
1143 zclient_stream_get_prefix(s
, rid
);
1146 /* Interface addition from zebra daemon. */
1148 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1149 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1151 * 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
1152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1167 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1169 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1173 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1175 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 * | Link Layer Type |
1177 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1178 * | Harware Address Length |
1179 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1180 * | Hardware Address if HW lenght different from 0 |
1181 * | ... max INTERFACE_HWADDR_MAX |
1182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1183 * | Link_params? | Whether a link-params follows: 1 or 0.
1184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1185 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1186 * | .... (struct if_link_params). |
1187 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1190 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1193 char vrfname_tmp
[VRF_NAMSIZ
];
1194 struct vrf_data data
;
1196 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1197 /* Read interface name. */
1198 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1200 /* Lookup/create vrf by vrf_id. */
1201 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1207 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1211 /* Lookup vrf by vrf_id. */
1212 vrf
= vrf_lookup_by_id(vrf_id
);
1215 * If a routing protocol doesn't know about a
1216 * vrf that is about to be deleted. There is
1217 * no point in attempting to delete it.
1225 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1227 struct interface
*ifp
;
1228 char ifname_tmp
[INTERFACE_NAMSIZ
];
1230 /* Read interface name. */
1231 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1233 /* Lookup/create interface by name. */
1234 ifp
= if_get_by_name_len(
1235 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
, 0);
1237 zebra_interface_if_set_value(s
, ifp
);
1243 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1244 * from zebra server. The format of this message is the same as
1245 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1246 * comments for zebra_interface_add_read), except that no sockaddr_dl
1247 * is sent at the tail of the message.
1249 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1251 struct interface
*ifp
;
1252 char ifname_tmp
[INTERFACE_NAMSIZ
];
1254 /* Read interface name. */
1255 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1257 /* Lookup this by interface index. */
1258 ifp
= if_lookup_by_name_len(
1259 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
);
1261 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1262 ifname_tmp
, vrf_id
);
1266 zebra_interface_if_set_value(s
, ifp
);
1271 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1277 iflp
->lp_status
= stream_getl(s
);
1278 iflp
->te_metric
= stream_getl(s
);
1279 iflp
->max_bw
= stream_getf(s
);
1280 iflp
->max_rsv_bw
= stream_getf(s
);
1281 uint32_t bwclassnum
= stream_getl(s
);
1284 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1285 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1288 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1289 " - outdated library?",
1290 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1292 iflp
->admin_grp
= stream_getl(s
);
1293 iflp
->rmt_as
= stream_getl(s
);
1294 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1296 iflp
->av_delay
= stream_getl(s
);
1297 iflp
->min_delay
= stream_getl(s
);
1298 iflp
->max_delay
= stream_getl(s
);
1299 iflp
->delay_var
= stream_getl(s
);
1301 iflp
->pkt_loss
= stream_getf(s
);
1302 iflp
->res_bw
= stream_getf(s
);
1303 iflp
->ava_bw
= stream_getf(s
);
1304 iflp
->use_bw
= stream_getf(s
);
1307 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1309 struct if_link_params
*iflp
;
1314 ifindex
= stream_getl(s
);
1316 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1319 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1324 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1327 link_params_set_value(s
, iflp
);
1332 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1334 u_char link_params_status
= 0;
1336 /* Read interface's index. */
1337 ifp
->ifindex
= stream_getl(s
);
1338 ifp
->status
= stream_getc(s
);
1340 /* Read interface's value. */
1341 ifp
->flags
= stream_getq(s
);
1342 ifp
->ptm_enable
= stream_getc(s
);
1343 ifp
->ptm_status
= stream_getc(s
);
1344 ifp
->metric
= stream_getl(s
);
1345 ifp
->speed
= stream_getl(s
);
1346 ifp
->mtu
= stream_getl(s
);
1347 ifp
->mtu6
= stream_getl(s
);
1348 ifp
->bandwidth
= stream_getl(s
);
1349 ifp
->ll_type
= stream_getl(s
);
1350 ifp
->hw_addr_len
= stream_getl(s
);
1351 if (ifp
->hw_addr_len
)
1352 stream_get(ifp
->hw_addr
, s
,
1353 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1355 /* Read Traffic Engineering status */
1356 link_params_status
= stream_getc(s
);
1357 /* Then, Traffic Engineering parameters if any */
1358 if (link_params_status
) {
1359 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1360 link_params_set_value(s
, iflp
);
1364 size_t zebra_interface_link_params_write(struct stream
*s
,
1365 struct interface
*ifp
)
1368 struct if_link_params
*iflp
;
1371 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1374 iflp
= ifp
->link_params
;
1377 w
+= stream_putl(s
, iflp
->lp_status
);
1379 w
+= stream_putl(s
, iflp
->te_metric
);
1380 w
+= stream_putf(s
, iflp
->max_bw
);
1381 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1383 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1384 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1385 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1387 w
+= stream_putl(s
, iflp
->admin_grp
);
1388 w
+= stream_putl(s
, iflp
->rmt_as
);
1389 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1391 w
+= stream_putl(s
, iflp
->av_delay
);
1392 w
+= stream_putl(s
, iflp
->min_delay
);
1393 w
+= stream_putl(s
, iflp
->max_delay
);
1394 w
+= stream_putl(s
, iflp
->delay_var
);
1396 w
+= stream_putf(s
, iflp
->pkt_loss
);
1397 w
+= stream_putf(s
, iflp
->res_bw
);
1398 w
+= stream_putf(s
, iflp
->ava_bw
);
1399 w
+= stream_putf(s
, iflp
->use_bw
);
1405 * format of message for address additon is:
1409 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1410 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1419 * | ifc_flags | flags for connected address
1427 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1435 static int memconstant(const void *s
, int c
, size_t n
)
1437 const u_char
*p
= s
;
1446 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1450 struct interface
*ifp
;
1451 struct connected
*ifc
;
1452 struct prefix p
, d
, *dp
;
1456 memset(&p
, 0, sizeof(p
));
1457 memset(&d
, 0, sizeof(d
));
1459 /* Get interface index. */
1460 ifindex
= stream_getl(s
);
1463 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1465 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1466 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1472 ifc_flags
= stream_getc(s
);
1474 /* Fetch interface address. */
1475 d
.family
= p
.family
= stream_getc(s
);
1476 plen
= prefix_blen(&d
);
1478 zclient_stream_get_prefix(s
, &p
);
1480 /* Fetch destination address. */
1481 stream_get(&d
.u
.prefix
, s
, plen
);
1483 /* N.B. NULL destination pointers are encoded as all zeroes */
1484 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1486 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1487 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1489 /* N.B. NULL destination pointers are encoded as all
1491 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1494 ifc
->flags
= ifc_flags
;
1495 if (ifc
->destination
)
1496 ifc
->destination
->prefixlen
=
1497 ifc
->address
->prefixlen
;
1498 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1499 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1501 char buf
[PREFIX_STRLEN
];
1503 "warning: interface %s address %s "
1504 "with peer flag set, but no peer address!",
1505 ifp
->name
, prefix2str(ifc
->address
, buf
,
1507 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1511 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1512 ifc
= connected_delete_by_prefix(ifp
, &p
);
1519 * format of message for neighbor connected address is:
1523 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1524 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1539 * | addr_len | len of addr.
1542 struct nbr_connected
*
1543 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1545 unsigned int ifindex
;
1546 struct interface
*ifp
;
1548 struct nbr_connected
*ifc
;
1550 /* Get interface index. */
1551 ifindex
= stream_getl(s
);
1554 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1556 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1557 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1563 p
.family
= stream_getc(s
);
1564 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1565 p
.prefixlen
= stream_getc(s
);
1567 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1568 /* Currently only supporting P2P links, so any new RA source
1570 considered as the replacement of the previously learnt
1571 Link-Local address. */
1572 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1573 ifc
= nbr_connected_new();
1574 ifc
->address
= prefix_new();
1576 listnode_add(ifp
->nbr_connected
, ifc
);
1579 prefix_copy(ifc
->address
, &p
);
1581 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1583 ifc
= nbr_connected_check(ifp
, &p
);
1585 listnode_delete(ifp
->nbr_connected
, ifc
);
1591 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1593 vrf_id_t
*new_vrf_id
)
1595 unsigned int ifindex
;
1596 struct interface
*ifp
;
1597 vrf_id_t new_id
= VRF_DEFAULT
;
1599 /* Get interface index. */
1600 ifindex
= stream_getl(s
);
1602 /* Lookup interface. */
1603 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1605 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1610 /* Fetch new VRF Id. */
1611 new_id
= stream_getw(s
);
1613 *new_vrf_id
= new_id
;
1617 /* filter unwanted messages until the expected one arrives */
1618 static int zclient_read_sync_response(struct zclient
*zclient
,
1619 u_int16_t expected_cmd
)
1631 cmd
= expected_cmd
+ 1;
1632 while (ret
== 0 && cmd
!= expected_cmd
) {
1636 /* wait until response arrives */
1638 FD_SET(zclient
->sock
, &readfds
);
1639 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1640 if (!FD_ISSET(zclient
->sock
, &readfds
))
1643 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1644 &version
, &vrf_id
, &cmd
);
1646 zlog_debug("%s: Response (%d bytes) received", __func__
,
1650 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1657 * Connect to label manager in a syncronous way
1659 * It first writes the request to zcient output buffer and then
1660 * immediately reads the answer from the input buffer.
1662 * @param zclient Zclient used to connect to label manager (zebra)
1663 * @result Result of response
1665 int lm_label_manager_connect(struct zclient
*zclient
)
1672 zlog_debug("Connecting to Label Manager");
1674 if (zclient
->sock
< 0)
1680 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1683 stream_putc(s
, zclient
->redist_default
);
1685 stream_putw(s
, zclient
->instance
);
1687 /* Put length at the first point of the stream. */
1688 stream_putw_at(s
, 0, stream_get_endp(s
));
1690 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1692 zlog_err("%s: can't write to zclient->sock", __func__
);
1693 close(zclient
->sock
);
1698 zlog_err("%s: zclient->sock connection closed", __func__
);
1699 close(zclient
->sock
);
1704 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1708 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1714 result
= stream_getc(s
);
1717 "%s: Label Manager connect response received, result %u",
1724 * Function to request a label chunk in a syncronous way
1726 * It first writes the request to zlcient output buffer and then
1727 * immediately reads the answer from the input buffer.
1729 * @param zclient Zclient used to connect to label manager (zebra)
1730 * @param keep Avoid garbage collection
1731 * @param chunk_size Amount of labels requested
1732 * @param start To write first assigned chunk label to
1733 * @param end To write last assigned chunk label to
1734 * @result 0 on success, -1 otherwise
1736 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1737 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1741 u_char response_keep
;
1744 zlog_debug("Getting Label Chunk");
1746 if (zclient
->sock
< 0)
1752 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1754 stream_putc(s
, keep
);
1756 stream_putl(s
, chunk_size
);
1757 /* Put length at the first point of the stream. */
1758 stream_putw_at(s
, 0, stream_get_endp(s
));
1760 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1762 zlog_err("%s: can't write to zclient->sock", __func__
);
1763 close(zclient
->sock
);
1768 zlog_err("%s: zclient->sock connection closed", __func__
);
1769 close(zclient
->sock
);
1774 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1778 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1783 response_keep
= stream_getc(s
);
1784 /* start and end labels */
1785 *start
= stream_getl(s
);
1786 *end
= stream_getl(s
);
1788 /* not owning this response */
1789 if (keep
!= response_keep
) {
1791 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1792 __func__
, *start
, *end
, keep
, response_keep
);
1795 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1796 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1797 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1803 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1810 * Function to release a label chunk
1812 * @param zclient Zclient used to connect to label manager (zebra)
1813 * @param start First label of chunk
1814 * @param end Last label of chunk
1815 * @result 0 on success, -1 otherwise
1817 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1824 zlog_debug("Releasing Label Chunk");
1826 if (zclient
->sock
< 0)
1832 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1835 stream_putl(s
, start
);
1837 stream_putl(s
, end
);
1839 /* Put length at the first point of the stream. */
1840 stream_putw_at(s
, 0, stream_get_endp(s
));
1842 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1844 zlog_err("%s: can't write to zclient->sock", __func__
);
1845 close(zclient
->sock
);
1850 zlog_err("%s: zclient->sock connection closed", __func__
);
1851 close(zclient
->sock
);
1859 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1867 zclient_create_header(s
, command
, VRF_DEFAULT
);
1868 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1869 stream_putl(s
, pw
->ifindex
);
1872 stream_putl(s
, pw
->type
);
1875 stream_putl(s
, pw
->af
);
1878 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1881 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1884 zlog_err("%s: unknown af", __func__
);
1889 stream_putl(s
, pw
->local_label
);
1890 stream_putl(s
, pw
->remote_label
);
1893 stream_putc(s
, pw
->flags
);
1895 /* Protocol specific fields */
1896 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1898 /* Put length at the first point of the stream. */
1899 stream_putw_at(s
, 0, stream_get_endp(s
));
1901 return zclient_send_message(zclient
);
1905 * Receive PW status update from Zebra and send it to LDE process.
1907 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1908 zebra_size_t length
, vrf_id_t vrf_id
,
1909 struct zapi_pw_status
*pw
)
1913 memset(pw
, 0, sizeof(struct zapi_pw_status
));
1917 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
1918 pw
->ifindex
= stream_getl(s
);
1919 pw
->status
= stream_getl(s
);
1922 /* Zebra client message read function. */
1923 static int zclient_read(struct thread
*thread
)
1926 uint16_t length
, command
;
1927 uint8_t marker
, version
;
1929 struct zclient
*zclient
;
1931 /* Get socket to zebra. */
1932 zclient
= THREAD_ARG(thread
);
1933 zclient
->t_read
= NULL
;
1935 /* Read zebra header (if we don't have it already). */
1936 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
1938 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1939 ZEBRA_HEADER_SIZE
- already
))
1944 "zclient connection closed socket [%d].",
1946 return zclient_failed(zclient
);
1948 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
1949 /* Try again later. */
1950 zclient_event(ZCLIENT_READ
, zclient
);
1953 already
= ZEBRA_HEADER_SIZE
;
1956 /* Reset to read from the beginning of the incoming packet. */
1957 stream_set_getp(zclient
->ibuf
, 0);
1959 /* Fetch header values. */
1960 length
= stream_getw(zclient
->ibuf
);
1961 marker
= stream_getc(zclient
->ibuf
);
1962 version
= stream_getc(zclient
->ibuf
);
1963 vrf_id
= stream_getw(zclient
->ibuf
);
1964 command
= stream_getw(zclient
->ibuf
);
1966 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
1968 "%s: socket %d version mismatch, marker %d, version %d",
1969 __func__
, zclient
->sock
, marker
, version
);
1970 return zclient_failed(zclient
);
1973 if (length
< ZEBRA_HEADER_SIZE
) {
1974 zlog_err("%s: socket %d message length %u is less than %d ",
1975 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1976 return zclient_failed(zclient
);
1980 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
1983 "%s: message size %u exceeds buffer size %lu, expanding...",
1984 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1985 ns
= stream_new(length
);
1986 stream_copy(ns
, zclient
->ibuf
);
1987 stream_free(zclient
->ibuf
);
1991 /* Read rest of zebra packet. */
1992 if (already
< length
) {
1994 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2000 "zclient connection closed socket [%d].",
2002 return zclient_failed(zclient
);
2004 if (nbyte
!= (ssize_t
)(length
- already
)) {
2005 /* Try again later. */
2006 zclient_event(ZCLIENT_READ
, zclient
);
2011 length
-= ZEBRA_HEADER_SIZE
;
2014 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2015 (void *)zclient
, command
, vrf_id
);
2018 case ZEBRA_ROUTER_ID_UPDATE
:
2019 if (zclient
->router_id_update
)
2020 (*zclient
->router_id_update
)(command
, zclient
, length
,
2024 zclient_vrf_add(zclient
, vrf_id
);
2026 case ZEBRA_VRF_DELETE
:
2027 zclient_vrf_delete(zclient
, vrf_id
);
2029 case ZEBRA_INTERFACE_ADD
:
2030 if (zclient
->interface_add
)
2031 (*zclient
->interface_add
)(command
, zclient
, length
,
2034 case ZEBRA_INTERFACE_DELETE
:
2035 if (zclient
->interface_delete
)
2036 (*zclient
->interface_delete
)(command
, zclient
, length
,
2039 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2040 if (zclient
->interface_address_add
)
2041 (*zclient
->interface_address_add
)(command
, zclient
,
2044 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2045 if (zclient
->interface_address_delete
)
2046 (*zclient
->interface_address_delete
)(command
, zclient
,
2049 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2050 if (zclient
->interface_bfd_dest_update
)
2051 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2054 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2055 if (zclient
->interface_nbr_address_add
)
2056 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2059 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2060 if (zclient
->interface_nbr_address_delete
)
2061 (*zclient
->interface_nbr_address_delete
)(
2062 command
, zclient
, length
, vrf_id
);
2064 case ZEBRA_INTERFACE_UP
:
2065 if (zclient
->interface_up
)
2066 (*zclient
->interface_up
)(command
, zclient
, length
,
2069 case ZEBRA_INTERFACE_DOWN
:
2070 if (zclient
->interface_down
)
2071 (*zclient
->interface_down
)(command
, zclient
, length
,
2074 case ZEBRA_INTERFACE_VRF_UPDATE
:
2075 if (zclient
->interface_vrf_update
)
2076 (*zclient
->interface_vrf_update
)(command
, zclient
,
2079 case ZEBRA_NEXTHOP_UPDATE
:
2081 zlog_debug("zclient rcvd nexthop update\n");
2082 if (zclient
->nexthop_update
)
2083 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2086 case ZEBRA_IMPORT_CHECK_UPDATE
:
2088 zlog_debug("zclient rcvd import check update\n");
2089 if (zclient
->import_check_update
)
2090 (*zclient
->import_check_update
)(command
, zclient
,
2093 case ZEBRA_BFD_DEST_REPLAY
:
2094 if (zclient
->bfd_dest_replay
)
2095 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2098 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2099 if (zclient
->redistribute_route_add
)
2100 (*zclient
->redistribute_route_add
)(command
, zclient
,
2103 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2104 if (zclient
->redistribute_route_del
)
2105 (*zclient
->redistribute_route_del
)(command
, zclient
,
2108 case ZEBRA_INTERFACE_LINK_PARAMS
:
2109 if (zclient
->interface_link_params
)
2110 (*zclient
->interface_link_params
)(command
, zclient
,
2113 case ZEBRA_FEC_UPDATE
:
2115 zlog_debug("zclient rcvd fec update\n");
2116 if (zclient
->fec_update
)
2117 (*zclient
->fec_update
)(command
, zclient
, length
);
2120 if (zclient
->local_vni_add
)
2121 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2125 if (zclient
->local_vni_del
)
2126 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2129 case ZEBRA_MACIP_ADD
:
2130 if (zclient
->local_macip_add
)
2131 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2134 case ZEBRA_MACIP_DEL
:
2135 if (zclient
->local_macip_del
)
2136 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2139 case ZEBRA_PW_STATUS_UPDATE
:
2140 if (zclient
->pw_status_update
)
2141 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2148 if (zclient
->sock
< 0)
2149 /* Connection was closed during packet processing. */
2152 /* Register read thread. */
2153 stream_reset(zclient
->ibuf
);
2154 zclient_event(ZCLIENT_READ
, zclient
);
2159 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2160 int type
, u_short instance
, vrf_id_t vrf_id
)
2164 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2165 if (redist_check_instance(
2166 &zclient
->mi_redist
[afi
][type
], instance
))
2168 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2171 if (!redist_check_instance(
2172 &zclient
->mi_redist
[afi
][type
], instance
))
2174 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2179 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2180 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2183 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2185 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2188 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2192 if (zclient
->sock
> 0)
2193 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2198 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2202 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2203 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2205 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2207 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2209 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2212 if (zclient
->sock
> 0)
2213 zebra_message_send(zclient
, command
, vrf_id
);
2216 static void zclient_event(enum event event
, struct zclient
*zclient
)
2219 case ZCLIENT_SCHEDULE
:
2220 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2221 &zclient
->t_connect
);
2223 case ZCLIENT_CONNECT
:
2226 "zclient connect failures: %d schedule interval is now %d",
2227 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2228 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2229 zclient
->fail
< 3 ? 10 : 60,
2230 &zclient
->t_connect
);
2233 zclient
->t_read
= NULL
;
2234 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2235 zclient
->sock
, &zclient
->t_read
);
2240 void zclient_interface_set_master(struct zclient
*client
,
2241 struct interface
*master
,
2242 struct interface
*slave
)
2249 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2251 stream_putw(s
, master
->vrf_id
);
2252 stream_putl(s
, master
->ifindex
);
2253 stream_putw(s
, slave
->vrf_id
);
2254 stream_putl(s
, slave
->ifindex
);
2256 stream_putw_at(s
, 0, stream_get_endp(s
));
2257 zclient_send_message(client
);