1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
42 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
43 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
45 /* Zebra client events. */
46 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
48 /* Prototype for event manager. */
49 static void zclient_event(enum event
, struct zclient
*);
51 struct sockaddr_storage zclient_addr
;
52 socklen_t zclient_addr_len
;
54 /* This file local debug flag. */
55 int zclient_debug
= 0;
57 struct zclient_options zclient_options_default
= {.receive_notify
= false};
59 /* Allocate zclient structure. */
60 struct zclient
*zclient_new_notify(struct thread_master
*master
,
61 struct zclient_options
*opt
)
63 struct zclient
*zclient
;
64 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
66 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
67 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
68 zclient
->wb
= buffer_new(0);
69 zclient
->master
= master
;
71 zclient
->receive_notify
= opt
->receive_notify
;
76 /* This function is only called when exiting, because
77 many parts of the code do not check for I/O errors, so they could
78 reference an invalid pointer if the structure was ever freed.
80 Free zclient structure. */
81 void zclient_free(struct zclient
*zclient
)
84 stream_free(zclient
->ibuf
);
86 stream_free(zclient
->obuf
);
88 buffer_free(zclient
->wb
);
90 XFREE(MTYPE_ZCLIENT
, zclient
);
93 unsigned short *redist_check_instance(struct redist_proto
*red
,
94 unsigned short instance
)
96 struct listnode
*node
;
102 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
109 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
116 red
->instances
= list_new();
118 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
120 listnode_add(red
->instances
, in
);
123 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
127 id
= redist_check_instance(red
, instance
);
131 listnode_delete(red
->instances
, id
);
132 XFREE(MTYPE_REDIST_INST
, id
);
133 if (!red
->instances
->count
) {
135 list_delete_and_null(&red
->instances
);
139 /* Stop zebra client services. */
140 void zclient_stop(struct zclient
*zclient
)
146 zlog_debug("zclient stopped");
149 THREAD_OFF(zclient
->t_read
);
150 THREAD_OFF(zclient
->t_connect
);
151 THREAD_OFF(zclient
->t_write
);
154 stream_reset(zclient
->ibuf
);
155 stream_reset(zclient
->obuf
);
157 /* Empty the write buffer. */
158 buffer_reset(zclient
->wb
);
161 if (zclient
->sock
>= 0) {
162 close(zclient
->sock
);
167 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
168 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
169 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
170 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
173 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
177 vrf_bitmap_free(zclient
->default_information
);
178 zclient
->default_information
= VRF_BITMAP_NULL
;
181 void zclient_reset(struct zclient
*zclient
)
185 zclient_stop(zclient
);
187 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
189 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
192 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
197 * Connect to zebra daemon.
198 * @param zclient a pointer to zclient structure
199 * @return socket fd just to make sure that connection established
201 * @see zclient_new_notify
203 int zclient_socket_connect(struct zclient
*zclient
)
208 /* We should think about IPv6 connection. */
209 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
215 zclient
->privs
->change(ZPRIVS_RAISE
);
216 setsockopt_so_sendbuf(sock
, 1048576);
217 zclient
->privs
->change(ZPRIVS_LOWER
);
219 /* Connect to zebra. */
220 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
223 zlog_warn("%s connect failure: %d(%s)",
224 __PRETTY_FUNCTION__
, errno
,
225 safe_strerror(errno
));
230 zclient
->sock
= sock
;
234 static int zclient_failed(struct zclient
*zclient
)
237 zclient_stop(zclient
);
238 zclient_event(ZCLIENT_CONNECT
, zclient
);
242 static int zclient_flush_data(struct thread
*thread
)
244 struct zclient
*zclient
= THREAD_ARG(thread
);
246 zclient
->t_write
= NULL
;
247 if (zclient
->sock
< 0)
249 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
252 "%s: buffer_flush_available failed on zclient fd %d, closing",
253 __func__
, zclient
->sock
);
254 return zclient_failed(zclient
);
257 zclient
->t_write
= NULL
;
258 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
259 zclient
->sock
, &zclient
->t_write
);
267 int zclient_send_message(struct zclient
*zclient
)
269 if (zclient
->sock
< 0)
271 switch (buffer_write(zclient
->wb
, zclient
->sock
,
272 STREAM_DATA(zclient
->obuf
),
273 stream_get_endp(zclient
->obuf
))) {
275 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
276 __func__
, zclient
->sock
);
277 return zclient_failed(zclient
);
280 THREAD_OFF(zclient
->t_write
);
283 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
284 zclient
->sock
, &zclient
->t_write
);
290 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
292 /* length placeholder, caller can update */
293 stream_putw(s
, ZEBRA_HEADER_SIZE
);
294 stream_putc(s
, ZEBRA_HEADER_MARKER
);
295 stream_putc(s
, ZSERV_VERSION
);
296 stream_putl(s
, vrf_id
);
297 stream_putw(s
, command
);
300 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
301 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
304 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
307 STREAM_GETW(s
, *size
);
308 *size
-= ZEBRA_HEADER_SIZE
;
309 STREAM_GETC(s
, *marker
);
310 STREAM_GETC(s
, *version
);
311 STREAM_GETL(s
, *vrf_id
);
312 STREAM_GETW(s
, *cmd
);
314 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
316 "%s: socket %d version mismatch, marker %d, version %d",
317 __func__
, sock
, *marker
, *version
);
321 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
328 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
330 STREAM_GETW(zmsg
, hdr
->length
);
331 STREAM_GETC(zmsg
, hdr
->marker
);
332 STREAM_GETC(zmsg
, hdr
->version
);
333 STREAM_GETL(zmsg
, hdr
->vrf_id
);
334 STREAM_GETW(zmsg
, hdr
->command
);
340 /* Send simple Zebra message. */
341 static int zebra_message_send(struct zclient
*zclient
, int command
,
346 /* Get zclient output buffer. */
350 /* Send very simple command only Zebra message. */
351 zclient_create_header(s
, command
, vrf_id
);
353 return zclient_send_message(zclient
);
356 static int zebra_hello_send(struct zclient
*zclient
)
360 if (zclient
->redist_default
) {
364 /* The VRF ID in the HELLO message is always 0. */
365 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
366 stream_putc(s
, zclient
->redist_default
);
367 stream_putw(s
, zclient
->instance
);
368 if (zclient
->receive_notify
)
373 stream_putw_at(s
, 0, stream_get_endp(s
));
374 return zclient_send_message(zclient
);
380 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
381 mpls_label_t label
, enum lsp_types_t ltype
)
388 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
389 stream_putl(s
, label
);
391 stream_putc(s
, ltype
);
392 stream_putw_at(s
, 0, stream_get_endp(s
));
393 zclient_send_message(zclient
);
396 /* Send register requests to zebra daemon for the information in a VRF. */
397 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
402 /* If not connected to the zebra yet. */
403 if (zclient
->sock
< 0)
407 zlog_debug("%s: send register messages for VRF %u", __func__
,
410 /* We need router-id information. */
411 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
413 /* We need interface information. */
414 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
416 /* Set unwanted redistribute route. */
417 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
418 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
421 /* Flush all redistribute request. */
422 if (vrf_id
== VRF_DEFAULT
) {
423 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
424 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
425 if (!zclient
->mi_redist
[afi
][i
].enabled
)
428 struct listnode
*node
;
431 for (ALL_LIST_ELEMENTS_RO(
432 zclient
->mi_redist
[afi
][i
]
435 if (!(i
== zclient
->redist_default
436 && *id
== zclient
->instance
))
437 zebra_redistribute_send(
438 ZEBRA_REDISTRIBUTE_ADD
,
439 zclient
, afi
, i
, *id
,
445 /* Resend all redistribute request. */
446 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
447 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
448 if (i
!= zclient
->redist_default
449 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
451 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
455 /* If default information is needed. */
456 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
457 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
461 /* Send unregister requests to zebra daemon for the information in a VRF. */
462 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
467 /* If not connected to the zebra yet. */
468 if (zclient
->sock
< 0)
472 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
475 /* We need router-id information. */
476 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
478 /* We need interface information. */
479 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
481 /* Set unwanted redistribute route. */
482 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
483 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
486 /* Flush all redistribute request. */
487 if (vrf_id
== VRF_DEFAULT
) {
488 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
489 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
490 if (!zclient
->mi_redist
[afi
][i
].enabled
)
493 struct listnode
*node
;
496 for (ALL_LIST_ELEMENTS_RO(
497 zclient
->mi_redist
[afi
][i
]
500 if (!(i
== zclient
->redist_default
501 && *id
== zclient
->instance
))
502 zebra_redistribute_send(
503 ZEBRA_REDISTRIBUTE_DELETE
,
504 zclient
, afi
, i
, *id
,
510 /* Flush all redistribute request. */
511 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
512 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
513 if (i
!= zclient
->redist_default
514 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
516 zebra_redistribute_send(
517 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
520 /* If default information is needed. */
521 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
522 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
526 /* Send request to zebra daemon to start or stop RA. */
527 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
528 struct interface
*ifp
, int enable
,
533 /* If not connected to the zebra yet. */
534 if (zclient
->sock
< 0)
537 /* Form and send message. */
542 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
544 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
546 stream_putl(s
, ifp
->ifindex
);
547 stream_putl(s
, ra_interval
);
549 stream_putw_at(s
, 0, stream_get_endp(s
));
551 zclient_send_message(zclient
);
554 /* Make connection to zebra daemon. */
555 int zclient_start(struct zclient
*zclient
)
558 zlog_info("zclient_start is called");
560 /* If already connected to the zebra. */
561 if (zclient
->sock
>= 0)
564 /* Check connect thread. */
565 if (zclient
->t_connect
)
568 if (zclient_socket_connect(zclient
) < 0) {
570 zlog_debug("zclient connection fail");
572 zclient_event(ZCLIENT_CONNECT
, zclient
);
576 if (set_nonblocking(zclient
->sock
) < 0)
577 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
580 /* Clear fail count. */
583 zlog_debug("zclient connect success with socket [%d]",
586 /* Create read thread. */
587 zclient_event(ZCLIENT_READ
, zclient
);
589 zebra_hello_send(zclient
);
591 /* Inform the successful connection. */
592 if (zclient
->zebra_connected
)
593 (*zclient
->zebra_connected
)(zclient
);
598 /* Initialize zebra client. Argument redist_default is unwanted
599 redistribute route type. */
600 void zclient_init(struct zclient
*zclient
, int redist_default
,
601 unsigned short instance
, struct zebra_privs_t
*privs
)
605 /* Set -1 to the default socket value. */
607 zclient
->privs
= privs
;
609 /* Clear redistribution flags. */
610 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
611 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
612 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
614 /* Set unwanted redistribute route. bgpd does not need BGP route
616 zclient
->redist_default
= redist_default
;
617 zclient
->instance
= instance
;
618 /* Pending: make afi(s) an arg. */
619 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
620 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
623 /* Set default-information redistribute to zero. */
624 zclient
->default_information
= vrf_bitmap_init();
627 zlog_debug("zclient_start is called");
629 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
632 /* This function is a wrapper function for calling zclient_start from
633 timer or event thread. */
634 static int zclient_connect(struct thread
*t
)
636 struct zclient
*zclient
;
638 zclient
= THREAD_ARG(t
);
639 zclient
->t_connect
= NULL
;
642 zlog_debug("zclient_connect is called");
644 return zclient_start(zclient
);
647 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
648 bool exact_match
, vrf_id_t vrf_id
)
654 zclient_create_header(s
, command
, vrf_id
);
655 stream_putc(s
, (exact_match
) ? 1 : 0);
657 stream_putw(s
, PREFIX_FAMILY(p
));
658 stream_putc(s
, p
->prefixlen
);
659 switch (PREFIX_FAMILY(p
)) {
661 stream_put_in_addr(s
, &p
->u
.prefix4
);
664 stream_put(s
, &(p
->u
.prefix6
), 16);
669 stream_putw_at(s
, 0, stream_get_endp(s
));
671 return zclient_send_message(zclient
);
675 * "xdr_encode"-like interface that allows daemon (client) to send
676 * a message to zebra server for a route that needs to be
677 * added/deleted to the kernel. Info about the route is specified
678 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
679 * the info down the zclient socket using the stream_* functions.
681 * The corresponding read ("xdr_decode") function on the server
682 * side is zread_ipv4_add()/zread_ipv4_delete().
684 * 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
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | Length (2) | Command | Route Type |
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 * | ZEBRA Flags | Message Flags | Prefix length |
689 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * | Destination IPv4 Prefix for route |
691 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
696 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
697 * described, as per the Nexthop count. Each nexthop described as:
700 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
702 * | IPv4 Nexthop address or Interface Index number |
703 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
705 * Alternatively, if the route is a blackhole route, then Nexthop count
706 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
709 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
710 * infrastructure was built around the traditional (32-bit "gate OR
711 * ifindex") nexthop data unit. A special encoding can be used to feed
712 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
713 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
715 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
716 * - .nexthop_num == .ifindex_num
717 * - .nexthop and .ifindex are filled with gate and ifindex parts of
718 * each compound nexthop, both in the same order
720 * zapi_ipv4_route() will produce two nexthop data units for each such
721 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
722 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
724 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
727 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
730 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
732 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
734 * XXX: No attention paid to alignment.
736 int zapi_ipv4_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
737 struct zapi_ipv4
*api
)
747 /* Some checks for labeled-unicast. The current expectation is that each
748 * nexthop is accompanied by a label in the case of labeled-unicast.
750 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
751 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
752 /* We expect prefixes installed with labels and the number to
754 * the number of nexthops.
756 assert(api
->label_num
== api
->nexthop_num
);
759 zclient_create_header(s
, cmd
, api
->vrf_id
);
761 /* Put type and nexthop. */
762 stream_putc(s
, api
->type
);
763 stream_putw(s
, api
->instance
);
764 stream_putl(s
, api
->flags
);
765 stream_putc(s
, api
->message
);
766 stream_putw(s
, api
->safi
);
768 /* Put prefix information. */
769 psize
= PSIZE(p
->prefixlen
);
770 stream_putc(s
, p
->prefixlen
);
771 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
773 /* Nexthop, ifindex, distance and metric information. */
774 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
775 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
777 for (i
= 0; i
< api
->nexthop_num
; i
++) {
778 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
779 stream_put_in_addr(s
, api
->nexthop
[i
]);
780 /* For labeled-unicast, each nexthop is followed by
782 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
783 stream_putl(s
, api
->label
[i
]);
785 for (i
= 0; i
< api
->ifindex_num
; i
++) {
786 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
787 stream_putl(s
, api
->ifindex
[i
]);
791 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
792 stream_putc(s
, api
->distance
);
793 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
794 stream_putl(s
, api
->metric
);
795 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
796 stream_putl(s
, api
->tag
);
797 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
798 stream_putl(s
, api
->mtu
);
800 /* Put length at the first point of the stream. */
801 stream_putw_at(s
, 0, stream_get_endp(s
));
803 return zclient_send_message(zclient
);
806 int zapi_ipv4_route_ipv6_nexthop(uint8_t cmd
, struct zclient
*zclient
,
807 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
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
, (uint8_t *)&p
->prefix
, psize
);
843 /* Nexthop, ifindex, distance and metric information. */
844 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
845 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
847 for (i
= 0; i
< api
->nexthop_num
; i
++) {
848 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
849 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
850 /* For labeled-unicast, each nexthop is followed by
852 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
853 stream_putl(s
, api
->label
[i
]);
855 for (i
= 0; i
< api
->ifindex_num
; i
++) {
856 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
857 stream_putl(s
, api
->ifindex
[i
]);
861 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
862 stream_putc(s
, api
->distance
);
863 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
864 stream_putl(s
, api
->metric
);
865 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
866 stream_putl(s
, api
->tag
);
867 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
868 stream_putl(s
, api
->mtu
);
870 /* Put length at the first point of the stream. */
871 stream_putw_at(s
, 0, stream_get_endp(s
));
873 return zclient_send_message(zclient
);
876 int zapi_ipv6_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
877 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
883 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
885 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
891 /* Some checks for labeled-unicast. The current expectation is that each
892 * nexthop is accompanied by a label in the case of labeled-unicast.
894 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
895 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
896 /* We expect prefixes installed with labels and the number to
898 * the number of nexthops.
900 assert(api
->label_num
== api
->nexthop_num
);
903 zclient_create_header(s
, cmd
, api
->vrf_id
);
905 /* Put type and nexthop. */
906 stream_putc(s
, api
->type
);
907 stream_putw(s
, api
->instance
);
908 stream_putl(s
, api
->flags
);
909 stream_putc(s
, api
->message
);
910 stream_putw(s
, api
->safi
);
912 /* Put prefix information. */
913 psize
= PSIZE(p
->prefixlen
);
914 stream_putc(s
, p
->prefixlen
);
915 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
917 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
918 psize
= PSIZE(src_p
->prefixlen
);
919 stream_putc(s
, src_p
->prefixlen
);
920 stream_write(s
, (uint8_t *)&src_p
->prefix
, psize
);
923 /* Nexthop, ifindex, distance and metric information. */
924 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
925 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
927 for (i
= 0; i
< api
->nexthop_num
; i
++) {
928 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
929 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
930 /* For labeled-unicast, each nexthop is followed by
932 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
933 stream_putl(s
, api
->label
[i
]);
935 for (i
= 0; i
< api
->ifindex_num
; i
++) {
936 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
937 stream_putl(s
, api
->ifindex
[i
]);
941 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
942 stream_putc(s
, api
->distance
);
943 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
944 stream_putl(s
, api
->metric
);
945 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
946 stream_putl(s
, api
->tag
);
947 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
948 stream_putl(s
, api
->mtu
);
950 /* Put length at the first point of the stream. */
951 stream_putw_at(s
, 0, stream_get_endp(s
));
953 return zclient_send_message(zclient
);
956 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
957 struct zapi_route
*api
)
959 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
961 return zclient_send_message(zclient
);
964 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
966 struct zapi_nexthop
*api_nh
;
971 zclient_create_header(s
, cmd
, api
->vrf_id
);
973 stream_putc(s
, api
->type
);
974 stream_putw(s
, api
->instance
);
975 stream_putl(s
, api
->flags
);
976 stream_putc(s
, api
->message
);
977 stream_putc(s
, api
->safi
);
979 /* Put prefix information. */
980 stream_putc(s
, api
->prefix
.family
);
981 psize
= PSIZE(api
->prefix
.prefixlen
);
982 stream_putc(s
, api
->prefix
.prefixlen
);
983 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
985 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
986 psize
= PSIZE(api
->src_prefix
.prefixlen
);
987 stream_putc(s
, api
->src_prefix
.prefixlen
);
988 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
992 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
993 /* limit the number of nexthops if necessary */
994 if (api
->nexthop_num
> MULTIPATH_NUM
) {
995 char buf
[PREFIX2STR_BUFFER
];
997 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
999 "%s: prefix %s: can't encode %u nexthops "
1001 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1005 stream_putw(s
, api
->nexthop_num
);
1007 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1008 api_nh
= &api
->nexthops
[i
];
1010 stream_putl(s
, api_nh
->vrf_id
);
1011 stream_putc(s
, api_nh
->type
);
1012 switch (api_nh
->type
) {
1013 case NEXTHOP_TYPE_BLACKHOLE
:
1014 stream_putc(s
, api_nh
->bh_type
);
1016 case NEXTHOP_TYPE_IPV4
:
1017 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1019 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1020 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1021 stream_putl(s
, api_nh
->ifindex
);
1023 case NEXTHOP_TYPE_IFINDEX
:
1024 stream_putl(s
, api_nh
->ifindex
);
1026 case NEXTHOP_TYPE_IPV6
:
1027 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1030 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1031 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1033 stream_putl(s
, api_nh
->ifindex
);
1037 "%s: Specified Nexthop type %d does not exist",
1038 __PRETTY_FUNCTION__
, api_nh
->type
);
1042 /* MPLS labels for BGP-LU or Segment Routing */
1043 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1044 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1045 char buf
[PREFIX2STR_BUFFER
];
1046 prefix2str(&api
->prefix
, buf
,
1049 "%s: prefix %s: can't encode "
1050 "%u labels (maximum is %u)",
1057 stream_putc(s
, api_nh
->label_num
);
1058 stream_put(s
, &api_nh
->labels
[0],
1060 * sizeof(mpls_label_t
));
1063 /* Router MAC for EVPN routes. */
1064 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1065 stream_put(s
, &(api_nh
->rmac
),
1066 sizeof(struct ethaddr
));
1071 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1072 stream_putc(s
, api
->distance
);
1073 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1074 stream_putl(s
, api
->metric
);
1075 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1076 stream_putl(s
, api
->tag
);
1077 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1078 stream_putl(s
, api
->mtu
);
1079 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1080 stream_putl(s
, api
->tableid
);
1082 /* Put length at the first point of the stream. */
1083 stream_putw_at(s
, 0, stream_get_endp(s
));
1088 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1090 struct zapi_nexthop
*api_nh
;
1093 memset(api
, 0, sizeof(*api
));
1095 /* Type, flags, message. */
1096 STREAM_GETC(s
, api
->type
);
1097 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1098 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1099 __PRETTY_FUNCTION__
, api
->type
);
1103 STREAM_GETW(s
, api
->instance
);
1104 STREAM_GETL(s
, api
->flags
);
1105 STREAM_GETC(s
, api
->message
);
1106 STREAM_GETC(s
, api
->safi
);
1109 STREAM_GETC(s
, api
->prefix
.family
);
1110 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1111 switch (api
->prefix
.family
) {
1113 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1115 "%s: V4 prefixlen is %d which should not be more than 32",
1116 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1121 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1123 "%s: v6 prefixlen is %d which should not be more than 128",
1124 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1129 zlog_warn("%s: Specified family %d is not v4 or v6",
1130 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1133 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1135 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1136 api
->src_prefix
.family
= AF_INET6
;
1137 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1138 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1140 "%s: SRC Prefix prefixlen received: %d is too large",
1141 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1144 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1145 PSIZE(api
->src_prefix
.prefixlen
));
1147 if (api
->prefix
.family
!= AF_INET6
1148 || api
->src_prefix
.prefixlen
== 0) {
1150 "%s: SRC prefix specified in some manner that makes no sense",
1151 __PRETTY_FUNCTION__
);
1157 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1158 STREAM_GETW(s
, api
->nexthop_num
);
1159 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1160 zlog_warn("%s: invalid number of nexthops (%u)",
1161 __func__
, api
->nexthop_num
);
1165 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1166 api_nh
= &api
->nexthops
[i
];
1168 STREAM_GETL(s
, api_nh
->vrf_id
);
1169 STREAM_GETC(s
, api_nh
->type
);
1170 switch (api_nh
->type
) {
1171 case NEXTHOP_TYPE_BLACKHOLE
:
1172 STREAM_GETC(s
, api_nh
->bh_type
);
1174 case NEXTHOP_TYPE_IPV4
:
1175 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1178 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1179 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1181 STREAM_GETL(s
, api_nh
->ifindex
);
1183 case NEXTHOP_TYPE_IFINDEX
:
1184 STREAM_GETL(s
, api_nh
->ifindex
);
1186 case NEXTHOP_TYPE_IPV6
:
1187 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1189 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1190 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1191 STREAM_GETL(s
, api_nh
->ifindex
);
1195 "%s: Specified nexthop type %d does not exist",
1196 __PRETTY_FUNCTION__
, api_nh
->type
);
1200 /* MPLS labels for BGP-LU or Segment Routing */
1201 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1202 STREAM_GETC(s
, api_nh
->label_num
);
1204 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1206 "%s: invalid number of MPLS "
1208 __func__
, api_nh
->label_num
);
1212 STREAM_GET(&api_nh
->labels
[0], s
,
1214 * sizeof(mpls_label_t
));
1217 /* Router MAC for EVPN routes. */
1218 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1219 stream_get(&(api_nh
->rmac
), s
,
1220 sizeof(struct ethaddr
));
1225 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1226 STREAM_GETC(s
, api
->distance
);
1227 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1228 STREAM_GETL(s
, api
->metric
);
1229 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1230 STREAM_GETL(s
, api
->tag
);
1231 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1232 STREAM_GETL(s
, api
->mtu
);
1233 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1234 STREAM_GETL(s
, api
->tableid
);
1240 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1246 memset(&any
, 0, sizeof(any
));
1247 any
.family
= family
;
1251 stream_putc(s
, p
->family
);
1252 stream_putc(s
, p
->prefixlen
);
1253 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1256 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1259 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1262 * We are sending one item at a time at the moment
1266 stream_putl(s
, zrule
->seq
);
1267 stream_putl(s
, zrule
->priority
);
1268 stream_putl(s
, zrule
->unique
);
1270 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1271 zrule
->filter
.src_ip
.family
);
1272 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1273 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1274 zrule
->filter
.src_ip
.family
);
1275 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1276 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1278 stream_putl(s
, zrule
->action
.table
);
1279 stream_putl(s
, zrule
->ifindex
);
1281 /* Put length at the first point of the stream. */
1282 stream_putw_at(s
, 0, stream_get_endp(s
));
1287 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1289 enum zapi_route_notify_owner
*note
)
1293 STREAM_GET(note
, s
, sizeof(*note
));
1295 STREAM_GETC(s
, p
->family
);
1296 STREAM_GETC(s
, p
->prefixlen
);
1297 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1308 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1309 uint32_t *priority
, uint32_t *unique
,
1311 enum zapi_rule_notify_owner
*note
)
1313 uint32_t prio
, seq
, uni
;
1316 STREAM_GET(note
, s
, sizeof(*note
));
1318 STREAM_GETL(s
, seq
);
1319 STREAM_GETL(s
, prio
);
1320 STREAM_GETL(s
, uni
);
1321 STREAM_GETL(s
, ifi
);
1324 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1337 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1338 enum zapi_ipset_notify_owner
*note
)
1342 STREAM_GET(note
, s
, sizeof(*note
));
1344 STREAM_GETL(s
, uni
);
1347 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1356 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1358 enum zapi_ipset_entry_notify_owner
*note
)
1362 STREAM_GET(note
, s
, sizeof(*note
));
1364 STREAM_GETL(s
, uni
);
1366 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1369 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1378 bool zapi_iptable_notify_decode(struct stream
*s
,
1380 enum zapi_iptable_notify_owner
*note
)
1384 STREAM_GET(note
, s
, sizeof(*note
));
1386 STREAM_GETL(s
, uni
);
1389 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1398 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1400 struct nexthop
*n
= nexthop_new();
1402 n
->type
= znh
->type
;
1403 n
->vrf_id
= znh
->vrf_id
;
1404 n
->ifindex
= znh
->ifindex
;
1405 n
->gate
= znh
->gate
;
1408 * This function currently handles labels
1410 if (znh
->label_num
) {
1411 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1418 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1422 memset(nhr
, 0, sizeof(*nhr
));
1424 STREAM_GETW(s
, nhr
->prefix
.family
);
1425 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1426 switch (nhr
->prefix
.family
) {
1428 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1431 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1437 STREAM_GETC(s
, nhr
->type
);
1438 STREAM_GETW(s
, nhr
->instance
);
1439 STREAM_GETC(s
, nhr
->distance
);
1440 STREAM_GETL(s
, nhr
->metric
);
1441 STREAM_GETC(s
, nhr
->nexthop_num
);
1443 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1444 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1445 switch (nhr
->nexthops
[i
].type
) {
1446 case NEXTHOP_TYPE_IPV4
:
1447 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1448 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1450 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1452 case NEXTHOP_TYPE_IFINDEX
:
1453 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1455 case NEXTHOP_TYPE_IPV6
:
1456 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1457 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1459 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1461 case NEXTHOP_TYPE_BLACKHOLE
:
1464 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1465 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1466 zlog_warn("%s: invalid number of MPLS labels (%u)",
1467 __func__
, nhr
->nexthops
[i
].label_num
);
1470 if (nhr
->nexthops
[i
].label_num
)
1471 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1472 nhr
->nexthops
[i
].label_num
1473 * sizeof(mpls_label_t
));
1482 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1483 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1484 * then set/unset redist[type] in the client handle (a struct zserv) for the
1487 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1488 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1495 zclient_create_header(s
, command
, vrf_id
);
1496 stream_putc(s
, afi
);
1497 stream_putc(s
, type
);
1498 stream_putw(s
, instance
);
1500 stream_putw_at(s
, 0, stream_get_endp(s
));
1502 return zclient_send_message(zclient
);
1505 /* Get prefix in ZServ format; family should be filled in on prefix */
1506 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1508 size_t plen
= prefix_blen(p
);
1515 stream_get(&p
->u
.prefix
, s
, plen
);
1517 p
->prefixlen
= MIN(plen
* 8, c
);
1523 /* Router-id update from zebra daemon. */
1524 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1526 /* Fetch interface address. */
1527 STREAM_GETC(s
, rid
->family
);
1529 zclient_stream_get_prefix(s
, rid
);
1535 /* Interface addition from zebra daemon. */
1537 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1538 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1540 * 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
1541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1547 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1554 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1558 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1562 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1564 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1565 * | Link Layer Type |
1566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1567 * | Harware Address Length |
1568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1569 * | Hardware Address if HW lenght different from 0 |
1570 * | ... max INTERFACE_HWADDR_MAX |
1571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1572 * | Link_params? | Whether a link-params follows: 1 or 0.
1573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1574 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1575 * | .... (struct if_link_params). |
1576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1579 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1582 char vrfname_tmp
[VRF_NAMSIZ
];
1583 struct vrf_data data
;
1585 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1586 /* Read interface name. */
1587 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1589 /* Lookup/create vrf by vrf_id. */
1590 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1591 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1592 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1596 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1600 /* Lookup vrf by vrf_id. */
1601 vrf
= vrf_lookup_by_id(vrf_id
);
1604 * If a routing protocol doesn't know about a
1605 * vrf that is about to be deleted. There is
1606 * no point in attempting to delete it.
1614 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1616 struct interface
*ifp
;
1617 char ifname_tmp
[INTERFACE_NAMSIZ
];
1619 /* Read interface name. */
1620 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1622 /* Lookup/create interface by name. */
1623 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1625 zebra_interface_if_set_value(s
, ifp
);
1631 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1632 * from zebra server. The format of this message is the same as
1633 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1634 * comments for zebra_interface_add_read), except that no sockaddr_dl
1635 * is sent at the tail of the message.
1637 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1639 struct interface
*ifp
;
1640 char ifname_tmp
[INTERFACE_NAMSIZ
];
1642 /* Read interface name. */
1643 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1645 /* Lookup this by interface index. */
1646 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1648 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1649 ifname_tmp
, vrf_id
);
1653 zebra_interface_if_set_value(s
, ifp
);
1658 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1664 iflp
->lp_status
= stream_getl(s
);
1665 iflp
->te_metric
= stream_getl(s
);
1666 iflp
->max_bw
= stream_getf(s
);
1667 iflp
->max_rsv_bw
= stream_getf(s
);
1668 uint32_t bwclassnum
= stream_getl(s
);
1671 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1672 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1675 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1676 " - outdated library?",
1677 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1679 iflp
->admin_grp
= stream_getl(s
);
1680 iflp
->rmt_as
= stream_getl(s
);
1681 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1683 iflp
->av_delay
= stream_getl(s
);
1684 iflp
->min_delay
= stream_getl(s
);
1685 iflp
->max_delay
= stream_getl(s
);
1686 iflp
->delay_var
= stream_getl(s
);
1688 iflp
->pkt_loss
= stream_getf(s
);
1689 iflp
->res_bw
= stream_getf(s
);
1690 iflp
->ava_bw
= stream_getf(s
);
1691 iflp
->use_bw
= stream_getf(s
);
1694 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1696 struct if_link_params
*iflp
;
1701 ifindex
= stream_getl(s
);
1703 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1706 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1711 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1714 link_params_set_value(s
, iflp
);
1719 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1721 uint8_t link_params_status
= 0;
1722 ifindex_t old_ifindex
;
1724 old_ifindex
= ifp
->ifindex
;
1725 /* Read interface's index. */
1726 if_set_index(ifp
, stream_getl(s
));
1727 ifp
->status
= stream_getc(s
);
1729 /* Read interface's value. */
1730 ifp
->flags
= stream_getq(s
);
1731 ifp
->ptm_enable
= stream_getc(s
);
1732 ifp
->ptm_status
= stream_getc(s
);
1733 ifp
->metric
= stream_getl(s
);
1734 ifp
->speed
= stream_getl(s
);
1735 ifp
->mtu
= stream_getl(s
);
1736 ifp
->mtu6
= stream_getl(s
);
1737 ifp
->bandwidth
= stream_getl(s
);
1738 ifp
->ll_type
= stream_getl(s
);
1739 ifp
->hw_addr_len
= stream_getl(s
);
1740 if (ifp
->hw_addr_len
)
1741 stream_get(ifp
->hw_addr
, s
,
1742 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1744 /* Read Traffic Engineering status */
1745 link_params_status
= stream_getc(s
);
1746 /* Then, Traffic Engineering parameters if any */
1747 if (link_params_status
) {
1748 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1749 link_params_set_value(s
, iflp
);
1752 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1755 size_t zebra_interface_link_params_write(struct stream
*s
,
1756 struct interface
*ifp
)
1759 struct if_link_params
*iflp
;
1762 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1765 iflp
= ifp
->link_params
;
1768 w
+= stream_putl(s
, iflp
->lp_status
);
1770 w
+= stream_putl(s
, iflp
->te_metric
);
1771 w
+= stream_putf(s
, iflp
->max_bw
);
1772 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1774 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1775 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1776 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1778 w
+= stream_putl(s
, iflp
->admin_grp
);
1779 w
+= stream_putl(s
, iflp
->rmt_as
);
1780 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1782 w
+= stream_putl(s
, iflp
->av_delay
);
1783 w
+= stream_putl(s
, iflp
->min_delay
);
1784 w
+= stream_putl(s
, iflp
->max_delay
);
1785 w
+= stream_putl(s
, iflp
->delay_var
);
1787 w
+= stream_putf(s
, iflp
->pkt_loss
);
1788 w
+= stream_putf(s
, iflp
->res_bw
);
1789 w
+= stream_putf(s
, iflp
->ava_bw
);
1790 w
+= stream_putf(s
, iflp
->use_bw
);
1796 * format of message for address additon is:
1800 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1801 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1810 * | ifc_flags | flags for connected address
1818 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1826 static int memconstant(const void *s
, int c
, size_t n
)
1828 const uint8_t *p
= s
;
1837 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1841 struct interface
*ifp
;
1842 struct connected
*ifc
;
1843 struct prefix p
, d
, *dp
;
1847 memset(&p
, 0, sizeof(p
));
1848 memset(&d
, 0, sizeof(d
));
1850 /* Get interface index. */
1851 ifindex
= stream_getl(s
);
1854 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1856 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1857 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1863 ifc_flags
= stream_getc(s
);
1865 /* Fetch interface address. */
1866 d
.family
= p
.family
= stream_getc(s
);
1867 plen
= prefix_blen(&d
);
1869 zclient_stream_get_prefix(s
, &p
);
1871 /* Fetch destination address. */
1872 stream_get(&d
.u
.prefix
, s
, plen
);
1874 /* N.B. NULL destination pointers are encoded as all zeroes */
1875 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1877 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1878 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1880 /* N.B. NULL destination pointers are encoded as all
1882 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1885 ifc
->flags
= ifc_flags
;
1886 if (ifc
->destination
)
1887 ifc
->destination
->prefixlen
=
1888 ifc
->address
->prefixlen
;
1889 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1890 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1892 char buf
[PREFIX_STRLEN
];
1894 "warning: interface %s address %s "
1895 "with peer flag set, but no peer address!",
1897 prefix2str(ifc
->address
, buf
,
1899 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1903 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1904 ifc
= connected_delete_by_prefix(ifp
, &p
);
1911 * format of message for neighbor connected address is:
1915 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1916 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1931 * | addr_len | len of addr.
1934 struct nbr_connected
*
1935 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1937 unsigned int ifindex
;
1938 struct interface
*ifp
;
1940 struct nbr_connected
*ifc
;
1942 /* Get interface index. */
1943 ifindex
= stream_getl(s
);
1946 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1948 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1949 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1955 p
.family
= stream_getc(s
);
1956 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1957 p
.prefixlen
= stream_getc(s
);
1959 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1960 /* Currently only supporting P2P links, so any new RA source
1962 considered as the replacement of the previously learnt
1963 Link-Local address. */
1964 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1965 ifc
= nbr_connected_new();
1966 ifc
->address
= prefix_new();
1968 listnode_add(ifp
->nbr_connected
, ifc
);
1971 prefix_copy(ifc
->address
, &p
);
1973 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1975 ifc
= nbr_connected_check(ifp
, &p
);
1977 listnode_delete(ifp
->nbr_connected
, ifc
);
1983 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1985 vrf_id_t
*new_vrf_id
)
1987 unsigned int ifindex
;
1988 struct interface
*ifp
;
1991 /* Get interface index. */
1992 ifindex
= stream_getl(s
);
1994 /* Lookup interface. */
1995 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1997 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
2002 /* Fetch new VRF Id. */
2003 new_id
= stream_getw(s
);
2005 *new_vrf_id
= new_id
;
2009 /* filter unwanted messages until the expected one arrives */
2010 static int zclient_read_sync_response(struct zclient
*zclient
,
2011 uint16_t expected_cmd
)
2023 cmd
= expected_cmd
+ 1;
2024 while (ret
== 0 && cmd
!= expected_cmd
) {
2028 /* wait until response arrives */
2030 FD_SET(zclient
->sock
, &readfds
);
2031 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2032 if (!FD_ISSET(zclient
->sock
, &readfds
))
2035 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2036 &version
, &vrf_id
, &cmd
);
2038 zlog_debug("%s: Response (%d bytes) received", __func__
,
2042 zlog_err("%s: Invalid Sync Message Reply", __func__
);
2049 * Connect to label manager in a syncronous way
2051 * It first writes the request to zcient output buffer and then
2052 * immediately reads the answer from the input buffer.
2054 * @param zclient Zclient used to connect to label manager (zebra)
2055 * @result Result of response
2057 int lm_label_manager_connect(struct zclient
*zclient
)
2064 zlog_debug("Connecting to Label Manager (LM)");
2066 if (zclient
->sock
< 0)
2072 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
2075 stream_putc(s
, zclient
->redist_default
);
2077 stream_putw(s
, zclient
->instance
);
2079 /* Put length at the first point of the stream. */
2080 stream_putw_at(s
, 0, stream_get_endp(s
));
2082 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2084 zlog_err("Can't write to zclient sock");
2085 close(zclient
->sock
);
2090 zlog_err("Zclient sock closed");
2091 close(zclient
->sock
);
2096 zlog_debug("LM connect request sent (%d bytes)", ret
);
2099 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
2105 /* read instance and proto */
2106 uint8_t proto
= stream_getc(s
);
2107 uint16_t instance
= stream_getw(s
);
2110 if (proto
!= zclient
->redist_default
)
2112 "Wrong proto (%u) in LM connect response. Should be %u",
2113 proto
, zclient
->redist_default
);
2114 if (instance
!= zclient
->instance
)
2116 "Wrong instId (%u) in LM connect response. Should be %u",
2117 instance
, zclient
->instance
);
2120 result
= stream_getc(s
);
2122 zlog_debug("LM connect-response received, result %u", result
);
2128 * Asynchronous label chunk request
2130 * @param zclient Zclient used to connect to label manager (zebra)
2131 * @param keep Avoid garbage collection
2132 * @param chunk_size Amount of labels requested
2133 * @result 0 on success, -1 otherwise
2135 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2136 uint32_t chunk_size
)
2141 zlog_debug("Getting Label Chunk");
2143 if (zclient
->sock
< 0)
2149 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2151 stream_putc(s
, zclient
->redist_default
);
2153 stream_putw(s
, zclient
->instance
);
2154 stream_putc(s
, keep
);
2155 stream_putl(s
, chunk_size
);
2157 /* Put length at the first point of the stream. */
2158 stream_putw_at(s
, 0, stream_get_endp(s
));
2160 return zclient_send_message(zclient
);
2164 * Function to request a label chunk in a syncronous way
2166 * It first writes the request to zlcient output buffer and then
2167 * immediately reads the answer from the input buffer.
2169 * @param zclient Zclient used to connect to label manager (zebra)
2170 * @param keep Avoid garbage collection
2171 * @param chunk_size Amount of labels requested
2172 * @param start To write first assigned chunk label to
2173 * @param end To write last assigned chunk label to
2174 * @result 0 on success, -1 otherwise
2176 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2177 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2181 uint8_t response_keep
;
2184 zlog_debug("Getting Label Chunk");
2186 if (zclient
->sock
< 0)
2192 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2194 stream_putc(s
, zclient
->redist_default
);
2196 stream_putw(s
, zclient
->instance
);
2198 stream_putc(s
, keep
);
2200 stream_putl(s
, chunk_size
);
2201 /* Put length at the first point of the stream. */
2202 stream_putw_at(s
, 0, stream_get_endp(s
));
2204 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2206 zlog_err("Can't write to zclient sock");
2207 close(zclient
->sock
);
2212 zlog_err("Zclient sock closed");
2213 close(zclient
->sock
);
2218 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2221 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2224 /* parse response */
2227 /* read proto and instance */
2228 uint8_t proto
= stream_getc(s
);
2229 uint16_t instance
= stream_getw(s
);
2232 if (proto
!= zclient
->redist_default
)
2233 zlog_err("Wrong proto (%u) in get chunk response. Should be %u",
2234 proto
, zclient
->redist_default
);
2235 if (instance
!= zclient
->instance
)
2236 zlog_err("Wrong instId (%u) in get chunk response Should be %u",
2237 instance
, zclient
->instance
);
2240 response_keep
= stream_getc(s
);
2241 /* start and end labels */
2242 *start
= stream_getl(s
);
2243 *end
= stream_getl(s
);
2245 /* not owning this response */
2246 if (keep
!= response_keep
) {
2248 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2249 *start
, *end
, keep
, response_keep
);
2252 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2253 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2254 zlog_err("Invalid Label chunk: %u - %u", *start
, *end
);
2259 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2266 * Function to release a label chunk
2268 * @param zclient Zclient used to connect to label manager (zebra)
2269 * @param start First label of chunk
2270 * @param end Last label of chunk
2271 * @result 0 on success, -1 otherwise
2273 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2280 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2282 if (zclient
->sock
< 0)
2288 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2291 stream_putc(s
, zclient
->redist_default
);
2293 stream_putw(s
, zclient
->instance
);
2295 stream_putl(s
, start
);
2297 stream_putl(s
, end
);
2299 /* Put length at the first point of the stream. */
2300 stream_putw_at(s
, 0, stream_get_endp(s
));
2302 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2304 zlog_err("Can't write to zclient sock");
2305 close(zclient
->sock
);
2310 zlog_err("Zclient sock connection closed");
2311 close(zclient
->sock
);
2320 * Connect to table manager in a syncronous way
2322 * It first writes the request to zcient output buffer and then
2323 * immediately reads the answer from the input buffer.
2325 * @param zclient Zclient used to connect to table manager (zebra)
2326 * @result Result of response
2328 int tm_table_manager_connect(struct zclient
*zclient
)
2335 zlog_debug("Connecting to Table Manager");
2337 if (zclient
->sock
< 0)
2343 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2346 stream_putc(s
, zclient
->redist_default
);
2348 stream_putw(s
, zclient
->instance
);
2350 /* Put length at the first point of the stream. */
2351 stream_putw_at(s
, 0, stream_get_endp(s
));
2353 ret
= zclient_send_message(zclient
);
2358 zlog_debug("%s: Table manager connect request sent", __func__
);
2361 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2367 STREAM_GETC(s
, result
);
2370 "%s: Table Manager connect response received, result %u",
2379 * Function to request a table chunk in a syncronous way
2381 * It first writes the request to zclient output buffer and then
2382 * immediately reads the answer from the input buffer.
2384 * @param zclient Zclient used to connect to table manager (zebra)
2385 * @param chunk_size Amount of table requested
2386 * @param start to write first assigned chunk table RT ID to
2387 * @param end To write last assigned chunk table RT ID to
2388 * @result 0 on success, -1 otherwise
2390 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2391 uint32_t *start
, uint32_t *end
)
2397 zlog_debug("Getting Table Chunk");
2399 if (zclient
->sock
< 0)
2405 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2407 stream_putl(s
, chunk_size
);
2408 /* Put length at the first point of the stream. */
2409 stream_putw_at(s
, 0, stream_get_endp(s
));
2411 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2413 zlog_err("%s: can't write to zclient->sock", __func__
);
2414 close(zclient
->sock
);
2419 zlog_err("%s: zclient->sock connection closed", __func__
);
2420 close(zclient
->sock
);
2425 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2429 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2433 /* start and end table IDs */
2434 STREAM_GETL(s
, *start
);
2435 STREAM_GETL(s
, *end
);
2438 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2445 * Function to release a table chunk
2447 * @param zclient Zclient used to connect to table manager (zebra)
2448 * @param start First label of table
2449 * @param end Last label of chunk
2450 * @result 0 on success, -1 otherwise
2452 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2458 zlog_debug("Releasing Table Chunk");
2460 if (zclient
->sock
< 0)
2466 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2469 stream_putl(s
, start
);
2471 stream_putl(s
, end
);
2473 /* Put length at the first point of the stream. */
2474 stream_putw_at(s
, 0, stream_get_endp(s
));
2476 return zclient_send_message(zclient
);
2480 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2488 zclient_create_header(s
, command
, VRF_DEFAULT
);
2489 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2490 stream_putl(s
, pw
->ifindex
);
2493 stream_putl(s
, pw
->type
);
2496 stream_putl(s
, pw
->af
);
2499 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2502 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2505 zlog_err("%s: unknown af", __func__
);
2510 stream_putl(s
, pw
->local_label
);
2511 stream_putl(s
, pw
->remote_label
);
2514 stream_putc(s
, pw
->flags
);
2516 /* Protocol specific fields */
2517 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2519 /* Put length at the first point of the stream. */
2520 stream_putw_at(s
, 0, stream_get_endp(s
));
2522 return zclient_send_message(zclient
);
2526 * Receive PW status update from Zebra and send it to LDE process.
2528 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2529 zebra_size_t length
, vrf_id_t vrf_id
,
2530 struct zapi_pw_status
*pw
)
2534 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2538 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2539 pw
->ifindex
= stream_getl(s
);
2540 pw
->status
= stream_getl(s
);
2543 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2544 zebra_size_t length
, vrf_id_t vrf_id
)
2546 struct zclient_capabilities cap
;
2547 struct stream
*s
= zclient
->ibuf
;
2548 uint8_t mpls_enabled
;
2550 memset(&cap
, 0, sizeof(cap
));
2551 STREAM_GETC(s
, mpls_enabled
);
2552 cap
.mpls_enabled
= !!mpls_enabled
;
2553 STREAM_GETL(s
, cap
.ecmp
);
2555 if (zclient
->zebra_capabilities
)
2556 (*zclient
->zebra_capabilities
)(&cap
);
2562 /* Zebra client message read function. */
2563 static int zclient_read(struct thread
*thread
)
2566 uint16_t length
, command
;
2567 uint8_t marker
, version
;
2569 struct zclient
*zclient
;
2571 /* Get socket to zebra. */
2572 zclient
= THREAD_ARG(thread
);
2573 zclient
->t_read
= NULL
;
2575 /* Read zebra header (if we don't have it already). */
2576 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2578 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2579 ZEBRA_HEADER_SIZE
- already
))
2584 "zclient connection closed socket [%d].",
2586 return zclient_failed(zclient
);
2588 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2589 /* Try again later. */
2590 zclient_event(ZCLIENT_READ
, zclient
);
2593 already
= ZEBRA_HEADER_SIZE
;
2596 /* Reset to read from the beginning of the incoming packet. */
2597 stream_set_getp(zclient
->ibuf
, 0);
2599 /* Fetch header values. */
2600 length
= stream_getw(zclient
->ibuf
);
2601 marker
= stream_getc(zclient
->ibuf
);
2602 version
= stream_getc(zclient
->ibuf
);
2603 vrf_id
= stream_getl(zclient
->ibuf
);
2604 command
= stream_getw(zclient
->ibuf
);
2606 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2608 "%s: socket %d version mismatch, marker %d, version %d",
2609 __func__
, zclient
->sock
, marker
, version
);
2610 return zclient_failed(zclient
);
2613 if (length
< ZEBRA_HEADER_SIZE
) {
2614 zlog_err("%s: socket %d message length %u is less than %d ",
2615 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2616 return zclient_failed(zclient
);
2620 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2623 "%s: message size %u exceeds buffer size %lu, expanding...",
2625 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2626 ns
= stream_new(length
);
2627 stream_copy(ns
, zclient
->ibuf
);
2628 stream_free(zclient
->ibuf
);
2632 /* Read rest of zebra packet. */
2633 if (already
< length
) {
2635 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2641 "zclient connection closed socket [%d].",
2643 return zclient_failed(zclient
);
2645 if (nbyte
!= (ssize_t
)(length
- already
)) {
2646 /* Try again later. */
2647 zclient_event(ZCLIENT_READ
, zclient
);
2652 length
-= ZEBRA_HEADER_SIZE
;
2655 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2656 (void *)zclient
, command
, vrf_id
);
2659 case ZEBRA_CAPABILITIES
:
2660 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2662 case ZEBRA_ROUTER_ID_UPDATE
:
2663 if (zclient
->router_id_update
)
2664 (*zclient
->router_id_update
)(command
, zclient
, length
,
2668 zclient_vrf_add(zclient
, vrf_id
);
2670 case ZEBRA_VRF_DELETE
:
2671 zclient_vrf_delete(zclient
, vrf_id
);
2673 case ZEBRA_INTERFACE_ADD
:
2674 if (zclient
->interface_add
)
2675 (*zclient
->interface_add
)(command
, zclient
, length
,
2678 case ZEBRA_INTERFACE_DELETE
:
2679 if (zclient
->interface_delete
)
2680 (*zclient
->interface_delete
)(command
, zclient
, length
,
2683 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2684 if (zclient
->interface_address_add
)
2685 (*zclient
->interface_address_add
)(command
, zclient
,
2688 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2689 if (zclient
->interface_address_delete
)
2690 (*zclient
->interface_address_delete
)(command
, zclient
,
2693 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2694 if (zclient
->interface_bfd_dest_update
)
2695 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2698 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2699 if (zclient
->interface_nbr_address_add
)
2700 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2703 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2704 if (zclient
->interface_nbr_address_delete
)
2705 (*zclient
->interface_nbr_address_delete
)(
2706 command
, zclient
, length
, vrf_id
);
2708 case ZEBRA_INTERFACE_UP
:
2709 if (zclient
->interface_up
)
2710 (*zclient
->interface_up
)(command
, zclient
, length
,
2713 case ZEBRA_INTERFACE_DOWN
:
2714 if (zclient
->interface_down
)
2715 (*zclient
->interface_down
)(command
, zclient
, length
,
2718 case ZEBRA_INTERFACE_VRF_UPDATE
:
2719 if (zclient
->interface_vrf_update
)
2720 (*zclient
->interface_vrf_update
)(command
, zclient
,
2723 case ZEBRA_NEXTHOP_UPDATE
:
2725 zlog_debug("zclient rcvd nexthop update\n");
2726 if (zclient
->nexthop_update
)
2727 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2730 case ZEBRA_IMPORT_CHECK_UPDATE
:
2732 zlog_debug("zclient rcvd import check update\n");
2733 if (zclient
->import_check_update
)
2734 (*zclient
->import_check_update
)(command
, zclient
,
2737 case ZEBRA_BFD_DEST_REPLAY
:
2738 if (zclient
->bfd_dest_replay
)
2739 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2742 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2743 if (zclient
->redistribute_route_add
)
2744 (*zclient
->redistribute_route_add
)(command
, zclient
,
2747 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2748 if (zclient
->redistribute_route_del
)
2749 (*zclient
->redistribute_route_del
)(command
, zclient
,
2752 case ZEBRA_INTERFACE_LINK_PARAMS
:
2753 if (zclient
->interface_link_params
)
2754 (*zclient
->interface_link_params
)(command
, zclient
,
2757 case ZEBRA_FEC_UPDATE
:
2759 zlog_debug("zclient rcvd fec update\n");
2760 if (zclient
->fec_update
)
2761 (*zclient
->fec_update
)(command
, zclient
, length
);
2763 case ZEBRA_LOCAL_ES_ADD
:
2764 if (zclient
->local_es_add
)
2765 (*zclient
->local_es_add
)(command
, zclient
, length
,
2768 case ZEBRA_LOCAL_ES_DEL
:
2769 if (zclient
->local_es_del
)
2770 (*zclient
->local_es_del
)(command
, zclient
, length
,
2774 if (zclient
->local_vni_add
)
2775 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2779 if (zclient
->local_vni_del
)
2780 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2783 case ZEBRA_L3VNI_ADD
:
2784 if (zclient
->local_l3vni_add
)
2785 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2788 case ZEBRA_L3VNI_DEL
:
2789 if (zclient
->local_l3vni_del
)
2790 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2793 case ZEBRA_MACIP_ADD
:
2794 if (zclient
->local_macip_add
)
2795 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2798 case ZEBRA_MACIP_DEL
:
2799 if (zclient
->local_macip_del
)
2800 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2803 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2804 if (zclient
->local_ip_prefix_add
)
2805 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2808 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2809 if (zclient
->local_ip_prefix_del
)
2810 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2813 case ZEBRA_PW_STATUS_UPDATE
:
2814 if (zclient
->pw_status_update
)
2815 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2818 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2819 if (zclient
->route_notify_owner
)
2820 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2823 case ZEBRA_RULE_NOTIFY_OWNER
:
2824 if (zclient
->rule_notify_owner
)
2825 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2828 case ZEBRA_GET_LABEL_CHUNK
:
2829 if (zclient
->label_chunk
)
2830 (*zclient
->label_chunk
)(command
, zclient
, length
,
2833 case ZEBRA_IPSET_NOTIFY_OWNER
:
2834 if (zclient
->ipset_notify_owner
)
2835 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2838 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2839 if (zclient
->ipset_entry_notify_owner
)
2840 (*zclient
->ipset_entry_notify_owner
)(command
,
2844 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2845 if (zclient
->iptable_notify_owner
)
2846 (*zclient
->iptable_notify_owner
)(command
,
2853 if (zclient
->sock
< 0)
2854 /* Connection was closed during packet processing. */
2857 /* Register read thread. */
2858 stream_reset(zclient
->ibuf
);
2859 zclient_event(ZCLIENT_READ
, zclient
);
2864 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2865 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2869 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2870 if (redist_check_instance(
2871 &zclient
->mi_redist
[afi
][type
], instance
))
2873 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2876 if (!redist_check_instance(
2877 &zclient
->mi_redist
[afi
][type
], instance
))
2879 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2884 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2885 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2888 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2890 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2893 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2897 if (zclient
->sock
> 0)
2898 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2903 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2907 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2908 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2910 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2912 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2914 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2917 if (zclient
->sock
> 0)
2918 zebra_message_send(zclient
, command
, vrf_id
);
2921 static void zclient_event(enum event event
, struct zclient
*zclient
)
2924 case ZCLIENT_SCHEDULE
:
2925 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2926 &zclient
->t_connect
);
2928 case ZCLIENT_CONNECT
:
2931 "zclient connect failures: %d schedule interval is now %d",
2932 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2933 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2934 zclient
->fail
< 3 ? 10 : 60,
2935 &zclient
->t_connect
);
2938 zclient
->t_read
= NULL
;
2939 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2940 zclient
->sock
, &zclient
->t_read
);
2945 void zclient_interface_set_master(struct zclient
*client
,
2946 struct interface
*master
,
2947 struct interface
*slave
)
2954 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2956 stream_putl(s
, master
->vrf_id
);
2957 stream_putl(s
, master
->ifindex
);
2958 stream_putl(s
, slave
->vrf_id
);
2959 stream_putl(s
, slave
->ifindex
);
2961 stream_putw_at(s
, 0, stream_get_endp(s
));
2962 zclient_send_message(client
);