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
41 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
42 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
44 /* Zebra client events. */
45 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
47 /* Prototype for event manager. */
48 static void zclient_event(enum event
, struct zclient
*);
50 struct sockaddr_storage zclient_addr
;
51 socklen_t zclient_addr_len
;
53 /* This file local debug flag. */
54 int zclient_debug
= 0;
56 struct zclient_options zclient_options_default
= {.receive_notify
= false};
58 /* Allocate zclient structure. */
59 struct zclient
*zclient_new_notify(struct thread_master
*master
,
60 struct zclient_options
*opt
)
62 struct zclient
*zclient
;
63 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
65 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
66 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
67 zclient
->wb
= buffer_new(0);
68 zclient
->master
= master
;
70 zclient
->receive_notify
= opt
->receive_notify
;
75 /* This function is only called when exiting, because
76 many parts of the code do not check for I/O errors, so they could
77 reference an invalid pointer if the structure was ever freed.
79 Free zclient structure. */
80 void zclient_free(struct zclient
*zclient
)
83 stream_free(zclient
->ibuf
);
85 stream_free(zclient
->obuf
);
87 buffer_free(zclient
->wb
);
89 XFREE(MTYPE_ZCLIENT
, zclient
);
92 unsigned short *redist_check_instance(struct redist_proto
*red
,
93 unsigned short instance
)
95 struct listnode
*node
;
101 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
108 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
115 red
->instances
= list_new();
117 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
119 listnode_add(red
->instances
, in
);
122 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
126 id
= redist_check_instance(red
, instance
);
130 listnode_delete(red
->instances
, id
);
131 XFREE(MTYPE_REDIST_INST
, id
);
132 if (!red
->instances
->count
) {
134 list_delete_and_null(&red
->instances
);
138 /* Stop zebra client services. */
139 void zclient_stop(struct zclient
*zclient
)
145 zlog_debug("zclient stopped");
148 THREAD_OFF(zclient
->t_read
);
149 THREAD_OFF(zclient
->t_connect
);
150 THREAD_OFF(zclient
->t_write
);
153 stream_reset(zclient
->ibuf
);
154 stream_reset(zclient
->obuf
);
156 /* Empty the write buffer. */
157 buffer_reset(zclient
->wb
);
160 if (zclient
->sock
>= 0) {
161 close(zclient
->sock
);
166 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
167 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
168 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
169 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
172 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
176 vrf_bitmap_free(zclient
->default_information
);
177 zclient
->default_information
= VRF_BITMAP_NULL
;
180 void zclient_reset(struct zclient
*zclient
)
184 zclient_stop(zclient
);
186 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
188 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
191 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
196 * Connect to zebra daemon.
197 * @param zclient a pointer to zclient structure
198 * @return socket fd just to make sure that connection established
200 * @see zclient_new_notify
202 int zclient_socket_connect(struct zclient
*zclient
)
207 /* We should think about IPv6 connection. */
208 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
214 zclient
->privs
->change(ZPRIVS_RAISE
);
215 setsockopt_so_sendbuf(sock
, 1048576);
216 zclient
->privs
->change(ZPRIVS_LOWER
);
218 /* Connect to zebra. */
219 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
222 zlog_warn("%s connect failure: %d(%s)",
223 __PRETTY_FUNCTION__
, errno
,
224 safe_strerror(errno
));
229 zclient
->sock
= sock
;
233 static int zclient_failed(struct zclient
*zclient
)
236 zclient_stop(zclient
);
237 zclient_event(ZCLIENT_CONNECT
, zclient
);
241 static int zclient_flush_data(struct thread
*thread
)
243 struct zclient
*zclient
= THREAD_ARG(thread
);
245 zclient
->t_write
= NULL
;
246 if (zclient
->sock
< 0)
248 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
251 "%s: buffer_flush_available failed on zclient fd %d, closing",
252 __func__
, zclient
->sock
);
253 return zclient_failed(zclient
);
256 zclient
->t_write
= NULL
;
257 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
258 zclient
->sock
, &zclient
->t_write
);
266 int zclient_send_message(struct zclient
*zclient
)
268 if (zclient
->sock
< 0)
270 switch (buffer_write(zclient
->wb
, zclient
->sock
,
271 STREAM_DATA(zclient
->obuf
),
272 stream_get_endp(zclient
->obuf
))) {
274 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
275 __func__
, zclient
->sock
);
276 return zclient_failed(zclient
);
279 THREAD_OFF(zclient
->t_write
);
282 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
283 zclient
->sock
, &zclient
->t_write
);
289 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
291 /* length placeholder, caller can update */
292 stream_putw(s
, ZEBRA_HEADER_SIZE
);
293 stream_putc(s
, ZEBRA_HEADER_MARKER
);
294 stream_putc(s
, ZSERV_VERSION
);
295 stream_putl(s
, vrf_id
);
296 stream_putw(s
, command
);
299 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
300 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
303 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
306 STREAM_GETW(s
, *size
);
307 *size
-= ZEBRA_HEADER_SIZE
;
308 STREAM_GETC(s
, *marker
);
309 STREAM_GETC(s
, *version
);
310 STREAM_GETL(s
, *vrf_id
);
311 STREAM_GETW(s
, *cmd
);
313 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
315 "%s: socket %d version mismatch, marker %d, version %d",
316 __func__
, sock
, *marker
, *version
);
320 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
327 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
329 STREAM_GETW(zmsg
, hdr
->length
);
330 STREAM_GETC(zmsg
, hdr
->marker
);
331 STREAM_GETC(zmsg
, hdr
->version
);
332 STREAM_GETL(zmsg
, hdr
->vrf_id
);
333 STREAM_GETW(zmsg
, hdr
->command
);
339 /* Send simple Zebra message. */
340 static int zebra_message_send(struct zclient
*zclient
, int command
,
345 /* Get zclient output buffer. */
349 /* Send very simple command only Zebra message. */
350 zclient_create_header(s
, command
, vrf_id
);
352 return zclient_send_message(zclient
);
355 static int zebra_hello_send(struct zclient
*zclient
)
359 if (zclient
->redist_default
) {
363 /* The VRF ID in the HELLO message is always 0. */
364 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
365 stream_putc(s
, zclient
->redist_default
);
366 stream_putw(s
, zclient
->instance
);
367 if (zclient
->receive_notify
)
372 stream_putw_at(s
, 0, stream_get_endp(s
));
373 return zclient_send_message(zclient
);
379 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
380 mpls_label_t label
, enum lsp_types_t ltype
)
387 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
388 stream_putl(s
, label
);
390 stream_putc(s
, ltype
);
391 stream_putw_at(s
, 0, stream_get_endp(s
));
392 zclient_send_message(zclient
);
395 /* Send register requests to zebra daemon for the information in a VRF. */
396 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
401 /* If not connected to the zebra yet. */
402 if (zclient
->sock
< 0)
406 zlog_debug("%s: send register messages for VRF %u", __func__
,
409 /* We need router-id information. */
410 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
412 /* We need interface information. */
413 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
415 /* Set unwanted redistribute route. */
416 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
417 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
420 /* Flush all redistribute request. */
421 if (vrf_id
== VRF_DEFAULT
) {
422 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
423 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
424 if (!zclient
->mi_redist
[afi
][i
].enabled
)
427 struct listnode
*node
;
430 for (ALL_LIST_ELEMENTS_RO(
431 zclient
->mi_redist
[afi
][i
]
434 if (!(i
== zclient
->redist_default
435 && *id
== zclient
->instance
))
436 zebra_redistribute_send(
437 ZEBRA_REDISTRIBUTE_ADD
,
438 zclient
, afi
, i
, *id
,
444 /* Resend all redistribute request. */
445 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
446 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
447 if (i
!= zclient
->redist_default
448 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
450 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
454 /* If default information is needed. */
455 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
456 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
460 /* Send unregister requests to zebra daemon for the information in a VRF. */
461 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
466 /* If not connected to the zebra yet. */
467 if (zclient
->sock
< 0)
471 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
474 /* We need router-id information. */
475 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
477 /* We need interface information. */
478 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
480 /* Set unwanted redistribute route. */
481 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
482 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
485 /* Flush all redistribute request. */
486 if (vrf_id
== VRF_DEFAULT
) {
487 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
488 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
489 if (!zclient
->mi_redist
[afi
][i
].enabled
)
492 struct listnode
*node
;
495 for (ALL_LIST_ELEMENTS_RO(
496 zclient
->mi_redist
[afi
][i
]
499 if (!(i
== zclient
->redist_default
500 && *id
== zclient
->instance
))
501 zebra_redistribute_send(
502 ZEBRA_REDISTRIBUTE_DELETE
,
503 zclient
, afi
, i
, *id
,
509 /* Flush all redistribute request. */
510 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
511 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
512 if (i
!= zclient
->redist_default
513 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
515 zebra_redistribute_send(
516 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
519 /* If default information is needed. */
520 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
521 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
525 /* Send request to zebra daemon to start or stop RA. */
526 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
527 struct interface
*ifp
, int enable
,
532 /* If not connected to the zebra yet. */
533 if (zclient
->sock
< 0)
536 /* Form and send message. */
541 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
543 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
545 stream_putl(s
, ifp
->ifindex
);
546 stream_putl(s
, ra_interval
);
548 stream_putw_at(s
, 0, stream_get_endp(s
));
550 zclient_send_message(zclient
);
553 /* Make connection to zebra daemon. */
554 int zclient_start(struct zclient
*zclient
)
557 zlog_info("zclient_start is called");
559 /* If already connected to the zebra. */
560 if (zclient
->sock
>= 0)
563 /* Check connect thread. */
564 if (zclient
->t_connect
)
567 if (zclient_socket_connect(zclient
) < 0) {
569 zlog_debug("zclient connection fail");
571 zclient_event(ZCLIENT_CONNECT
, zclient
);
575 if (set_nonblocking(zclient
->sock
) < 0)
576 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
579 /* Clear fail count. */
582 zlog_debug("zclient connect success with socket [%d]",
585 /* Create read thread. */
586 zclient_event(ZCLIENT_READ
, zclient
);
588 zebra_hello_send(zclient
);
590 /* Inform the successful connection. */
591 if (zclient
->zebra_connected
)
592 (*zclient
->zebra_connected
)(zclient
);
597 /* Initialize zebra client. Argument redist_default is unwanted
598 redistribute route type. */
599 void zclient_init(struct zclient
*zclient
, int redist_default
,
600 unsigned short instance
, struct zebra_privs_t
*privs
)
604 /* Set -1 to the default socket value. */
606 zclient
->privs
= privs
;
608 /* Clear redistribution flags. */
609 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
610 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
611 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
613 /* Set unwanted redistribute route. bgpd does not need BGP route
615 zclient
->redist_default
= redist_default
;
616 zclient
->instance
= instance
;
617 /* Pending: make afi(s) an arg. */
618 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
619 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
622 /* Set default-information redistribute to zero. */
623 zclient
->default_information
= vrf_bitmap_init();
626 zlog_debug("zclient_start is called");
628 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
631 /* This function is a wrapper function for calling zclient_start from
632 timer or event thread. */
633 static int zclient_connect(struct thread
*t
)
635 struct zclient
*zclient
;
637 zclient
= THREAD_ARG(t
);
638 zclient
->t_connect
= NULL
;
641 zlog_debug("zclient_connect is called");
643 return zclient_start(zclient
);
646 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
647 bool exact_match
, vrf_id_t vrf_id
)
653 zclient_create_header(s
, command
, vrf_id
);
654 stream_putc(s
, (exact_match
) ? 1 : 0);
656 stream_putw(s
, PREFIX_FAMILY(p
));
657 stream_putc(s
, p
->prefixlen
);
658 switch (PREFIX_FAMILY(p
)) {
660 stream_put_in_addr(s
, &p
->u
.prefix4
);
663 stream_put(s
, &(p
->u
.prefix6
), 16);
668 stream_putw_at(s
, 0, stream_get_endp(s
));
670 return zclient_send_message(zclient
);
674 * "xdr_encode"-like interface that allows daemon (client) to send
675 * a message to zebra server for a route that needs to be
676 * added/deleted to the kernel. Info about the route is specified
677 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
678 * the info down the zclient socket using the stream_* functions.
680 * The corresponding read ("xdr_decode") function on the server
681 * side is zread_ipv4_add()/zread_ipv4_delete().
683 * 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
684 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * | Length (2) | Command | Route Type |
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | ZEBRA Flags | Message Flags | Prefix length |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | Destination IPv4 Prefix for route |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
696 * described, as per the Nexthop count. Each nexthop described as:
699 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
700 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 * | IPv4 Nexthop address or Interface Index number |
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704 * Alternatively, if the route is a blackhole route, then Nexthop count
705 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
708 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
709 * infrastructure was built around the traditional (32-bit "gate OR
710 * ifindex") nexthop data unit. A special encoding can be used to feed
711 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
712 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
714 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
715 * - .nexthop_num == .ifindex_num
716 * - .nexthop and .ifindex are filled with gate and ifindex parts of
717 * each compound nexthop, both in the same order
719 * zapi_ipv4_route() will produce two nexthop data units for each such
720 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
721 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
723 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
726 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
729 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
731 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
733 * XXX: No attention paid to alignment.
735 int zapi_ipv4_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
736 struct zapi_ipv4
*api
)
746 /* Some checks for labeled-unicast. The current expectation is that each
747 * nexthop is accompanied by a label in the case of labeled-unicast.
749 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
750 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
751 /* We expect prefixes installed with labels and the number to
753 * the number of nexthops.
755 assert(api
->label_num
== api
->nexthop_num
);
758 zclient_create_header(s
, cmd
, api
->vrf_id
);
760 /* Put type and nexthop. */
761 stream_putc(s
, api
->type
);
762 stream_putw(s
, api
->instance
);
763 stream_putl(s
, api
->flags
);
764 stream_putc(s
, api
->message
);
765 stream_putw(s
, api
->safi
);
767 /* Put prefix information. */
768 psize
= PSIZE(p
->prefixlen
);
769 stream_putc(s
, p
->prefixlen
);
770 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
772 /* Nexthop, ifindex, distance and metric information. */
773 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
774 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
776 for (i
= 0; i
< api
->nexthop_num
; i
++) {
777 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
778 stream_put_in_addr(s
, api
->nexthop
[i
]);
779 /* For labeled-unicast, each nexthop is followed by
781 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
782 stream_putl(s
, api
->label
[i
]);
784 for (i
= 0; i
< api
->ifindex_num
; i
++) {
785 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
786 stream_putl(s
, api
->ifindex
[i
]);
790 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
791 stream_putc(s
, api
->distance
);
792 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
793 stream_putl(s
, api
->metric
);
794 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
795 stream_putl(s
, api
->tag
);
796 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
797 stream_putl(s
, api
->mtu
);
799 /* Put length at the first point of the stream. */
800 stream_putw_at(s
, 0, stream_get_endp(s
));
802 return zclient_send_message(zclient
);
805 int zapi_ipv4_route_ipv6_nexthop(uint8_t cmd
, struct zclient
*zclient
,
806 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
816 /* Some checks for labeled-unicast. The current expectation is that each
817 * nexthop is accompanied by a label in the case of labeled-unicast.
819 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
820 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
821 /* We expect prefixes installed with labels and the number to
823 * the number of nexthops.
825 assert(api
->label_num
== api
->nexthop_num
);
828 zclient_create_header(s
, cmd
, api
->vrf_id
);
830 /* Put type and nexthop. */
831 stream_putc(s
, api
->type
);
832 stream_putw(s
, api
->instance
);
833 stream_putl(s
, api
->flags
);
834 stream_putc(s
, api
->message
);
835 stream_putw(s
, api
->safi
);
837 /* Put prefix information. */
838 psize
= PSIZE(p
->prefixlen
);
839 stream_putc(s
, p
->prefixlen
);
840 stream_write(s
, (uint8_t *)&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
, (uint8_t *)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 zapi_ipv6_route(uint8_t cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
876 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
882 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
884 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
890 /* Some checks for labeled-unicast. The current expectation is that each
891 * nexthop is accompanied by a label in the case of labeled-unicast.
893 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
894 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
895 /* We expect prefixes installed with labels and the number to
897 * the number of nexthops.
899 assert(api
->label_num
== api
->nexthop_num
);
902 zclient_create_header(s
, cmd
, api
->vrf_id
);
904 /* Put type and nexthop. */
905 stream_putc(s
, api
->type
);
906 stream_putw(s
, api
->instance
);
907 stream_putl(s
, api
->flags
);
908 stream_putc(s
, api
->message
);
909 stream_putw(s
, api
->safi
);
911 /* Put prefix information. */
912 psize
= PSIZE(p
->prefixlen
);
913 stream_putc(s
, p
->prefixlen
);
914 stream_write(s
, (uint8_t *)&p
->prefix
, psize
);
916 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
917 psize
= PSIZE(src_p
->prefixlen
);
918 stream_putc(s
, src_p
->prefixlen
);
919 stream_write(s
, (uint8_t *)&src_p
->prefix
, psize
);
922 /* Nexthop, ifindex, distance and metric information. */
923 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
924 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
926 for (i
= 0; i
< api
->nexthop_num
; i
++) {
927 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
928 stream_write(s
, (uint8_t *)api
->nexthop
[i
], 16);
929 /* For labeled-unicast, each nexthop is followed by
931 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
932 stream_putl(s
, api
->label
[i
]);
934 for (i
= 0; i
< api
->ifindex_num
; i
++) {
935 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
936 stream_putl(s
, api
->ifindex
[i
]);
940 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
941 stream_putc(s
, api
->distance
);
942 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
943 stream_putl(s
, api
->metric
);
944 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
945 stream_putl(s
, api
->tag
);
946 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
947 stream_putl(s
, api
->mtu
);
949 /* Put length at the first point of the stream. */
950 stream_putw_at(s
, 0, stream_get_endp(s
));
952 return zclient_send_message(zclient
);
955 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
956 struct zapi_route
*api
)
958 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
960 return zclient_send_message(zclient
);
963 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
965 struct zapi_nexthop
*api_nh
;
970 zclient_create_header(s
, cmd
, api
->vrf_id
);
972 stream_putc(s
, api
->type
);
973 stream_putw(s
, api
->instance
);
974 stream_putl(s
, api
->flags
);
975 stream_putc(s
, api
->message
);
976 stream_putc(s
, api
->safi
);
977 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
978 stream_put(s
, &(api
->rmac
), sizeof(struct ethaddr
));
980 /* Put prefix information. */
981 stream_putc(s
, api
->prefix
.family
);
982 psize
= PSIZE(api
->prefix
.prefixlen
);
983 stream_putc(s
, api
->prefix
.prefixlen
);
984 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
986 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
987 psize
= PSIZE(api
->src_prefix
.prefixlen
);
988 stream_putc(s
, api
->src_prefix
.prefixlen
);
989 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
993 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
994 /* limit the number of nexthops if necessary */
995 if (api
->nexthop_num
> MULTIPATH_NUM
) {
996 char buf
[PREFIX2STR_BUFFER
];
998 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1000 "%s: prefix %s: can't encode %u nexthops "
1002 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1006 stream_putw(s
, api
->nexthop_num
);
1008 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1009 api_nh
= &api
->nexthops
[i
];
1011 stream_putl(s
, api_nh
->vrf_id
);
1012 stream_putc(s
, api_nh
->type
);
1013 switch (api_nh
->type
) {
1014 case NEXTHOP_TYPE_BLACKHOLE
:
1015 stream_putc(s
, api_nh
->bh_type
);
1017 case NEXTHOP_TYPE_IPV4
:
1018 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1020 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1021 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1022 stream_putl(s
, api_nh
->ifindex
);
1024 case NEXTHOP_TYPE_IFINDEX
:
1025 stream_putl(s
, api_nh
->ifindex
);
1027 case NEXTHOP_TYPE_IPV6
:
1028 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1031 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1032 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
1034 stream_putl(s
, api_nh
->ifindex
);
1038 "%s: Specified Nexthop type %d does not exist",
1039 __PRETTY_FUNCTION__
, api_nh
->type
);
1043 /* MPLS labels for BGP-LU or Segment Routing */
1044 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1045 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1046 char buf
[PREFIX2STR_BUFFER
];
1047 prefix2str(&api
->prefix
, buf
,
1050 "%s: prefix %s: can't encode "
1051 "%u labels (maximum is %u)",
1058 stream_putc(s
, api_nh
->label_num
);
1059 stream_put(s
, &api_nh
->labels
[0],
1061 * sizeof(mpls_label_t
));
1067 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1068 stream_putc(s
, api
->distance
);
1069 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1070 stream_putl(s
, api
->metric
);
1071 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1072 stream_putl(s
, api
->tag
);
1073 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1074 stream_putl(s
, api
->mtu
);
1075 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1076 stream_putl(s
, api
->tableid
);
1078 /* Put length at the first point of the stream. */
1079 stream_putw_at(s
, 0, stream_get_endp(s
));
1084 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1086 struct zapi_nexthop
*api_nh
;
1089 memset(api
, 0, sizeof(*api
));
1091 /* Type, flags, message. */
1092 STREAM_GETC(s
, api
->type
);
1093 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1094 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1095 __PRETTY_FUNCTION__
, api
->type
);
1099 STREAM_GETW(s
, api
->instance
);
1100 STREAM_GETL(s
, api
->flags
);
1101 STREAM_GETC(s
, api
->message
);
1102 STREAM_GETC(s
, api
->safi
);
1103 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1104 STREAM_GET(&(api
->rmac
), s
, sizeof(struct ethaddr
));
1107 STREAM_GETC(s
, api
->prefix
.family
);
1108 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1109 switch (api
->prefix
.family
) {
1111 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1113 "%s: V4 prefixlen is %d which should not be more than 32",
1114 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1119 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1121 "%s: v6 prefixlen is %d which should not be more than 128",
1122 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1127 zlog_warn("%s: Specified family %d is not v4 or v6",
1128 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1131 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1133 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1134 api
->src_prefix
.family
= AF_INET6
;
1135 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1136 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1138 "%s: SRC Prefix prefixlen received: %d is too large",
1139 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1142 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1143 PSIZE(api
->src_prefix
.prefixlen
));
1145 if (api
->prefix
.family
!= AF_INET6
1146 || api
->src_prefix
.prefixlen
== 0) {
1148 "%s: SRC prefix specified in some manner that makes no sense",
1149 __PRETTY_FUNCTION__
);
1155 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1156 STREAM_GETW(s
, api
->nexthop_num
);
1157 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1158 zlog_warn("%s: invalid number of nexthops (%u)",
1159 __func__
, api
->nexthop_num
);
1163 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1164 api_nh
= &api
->nexthops
[i
];
1166 STREAM_GETL(s
, api_nh
->vrf_id
);
1167 STREAM_GETC(s
, api_nh
->type
);
1168 switch (api_nh
->type
) {
1169 case NEXTHOP_TYPE_BLACKHOLE
:
1170 STREAM_GETC(s
, api_nh
->bh_type
);
1172 case NEXTHOP_TYPE_IPV4
:
1173 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1176 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1177 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1179 STREAM_GETL(s
, api_nh
->ifindex
);
1181 case NEXTHOP_TYPE_IFINDEX
:
1182 STREAM_GETL(s
, api_nh
->ifindex
);
1184 case NEXTHOP_TYPE_IPV6
:
1185 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1187 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1188 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1189 STREAM_GETL(s
, api_nh
->ifindex
);
1193 "%s: Specified nexthop type %d does not exist",
1194 __PRETTY_FUNCTION__
, api_nh
->type
);
1198 /* MPLS labels for BGP-LU or Segment Routing */
1199 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1200 STREAM_GETC(s
, api_nh
->label_num
);
1202 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1204 "%s: invalid number of MPLS "
1206 __func__
, api_nh
->label_num
);
1210 STREAM_GET(&api_nh
->labels
[0], s
,
1212 * sizeof(mpls_label_t
));
1218 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1219 STREAM_GETC(s
, api
->distance
);
1220 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1221 STREAM_GETL(s
, api
->metric
);
1222 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1223 STREAM_GETL(s
, api
->tag
);
1224 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1225 STREAM_GETL(s
, api
->mtu
);
1226 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1227 STREAM_GETL(s
, api
->tableid
);
1233 static void zapi_encode_prefix(struct stream
*s
,
1240 memset(&any
, 0, sizeof(any
));
1241 any
.family
= family
;
1245 stream_putc(s
, p
->family
);
1246 stream_putc(s
, p
->prefixlen
);
1247 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1250 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
,
1251 struct pbr_rule
*zrule
)
1254 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1257 * We are sending one item at a time at the moment
1261 stream_putl(s
, zrule
->seq
);
1262 stream_putl(s
, zrule
->priority
);
1263 stream_putl(s
, zrule
->unique
);
1265 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1266 zrule
->filter
.src_ip
.family
);
1267 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1268 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1269 zrule
->filter
.src_ip
.family
);
1270 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1271 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1273 stream_putl(s
, zrule
->action
.table
);
1274 stream_putl(s
, zrule
->ifindex
);
1276 /* Put length at the first point of the stream. */
1277 stream_putw_at(s
, 0, stream_get_endp(s
));
1282 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1284 enum zapi_route_notify_owner
*note
)
1288 STREAM_GET(note
, s
, sizeof(*note
));
1290 STREAM_GETC(s
, p
->family
);
1291 STREAM_GETC(s
, p
->prefixlen
);
1292 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1303 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1304 uint32_t *priority
, uint32_t *unique
,
1306 enum zapi_rule_notify_owner
*note
)
1308 uint32_t prio
, seq
, uni
;
1311 STREAM_GET(note
, s
, sizeof(*note
));
1313 STREAM_GETL(s
, seq
);
1314 STREAM_GETL(s
, prio
);
1315 STREAM_GETL(s
, uni
);
1316 STREAM_GETL(s
, ifi
);
1319 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
,
1320 seq
, prio
, uni
, ifi
);
1332 bool zapi_ipset_notify_decode(struct stream
*s
,
1334 enum zapi_ipset_notify_owner
*note
)
1338 STREAM_GET(note
, s
, sizeof(*note
));
1340 STREAM_GETL(s
, uni
);
1343 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1352 bool zapi_ipset_entry_notify_decode(struct stream
*s
,
1355 enum zapi_ipset_entry_notify_owner
*note
)
1359 STREAM_GET(note
, s
, sizeof(*note
));
1361 STREAM_GETL(s
, uni
);
1363 STREAM_GET(ipset_name
, s
,
1364 ZEBRA_IPSET_NAME_SIZE
);
1367 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1376 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1378 struct nexthop
*n
= nexthop_new();
1380 n
->type
= znh
->type
;
1381 n
->vrf_id
= znh
->vrf_id
;
1382 n
->ifindex
= znh
->ifindex
;
1383 n
->gate
= znh
->gate
;
1386 * This function currently handles labels
1388 if (znh
->label_num
) {
1389 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1396 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1400 memset(nhr
, 0, sizeof(*nhr
));
1402 STREAM_GETW(s
, nhr
->prefix
.family
);
1403 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1404 switch (nhr
->prefix
.family
) {
1406 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1409 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1415 STREAM_GETC(s
, nhr
->type
);
1416 STREAM_GETW(s
, nhr
->instance
);
1417 STREAM_GETC(s
, nhr
->distance
);
1418 STREAM_GETL(s
, nhr
->metric
);
1419 STREAM_GETC(s
, nhr
->nexthop_num
);
1421 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1422 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1423 switch (nhr
->nexthops
[i
].type
) {
1424 case NEXTHOP_TYPE_IPV4
:
1425 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1426 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1428 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1430 case NEXTHOP_TYPE_IFINDEX
:
1431 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1433 case NEXTHOP_TYPE_IPV6
:
1434 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1435 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1437 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1439 case NEXTHOP_TYPE_BLACKHOLE
:
1442 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1443 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1444 zlog_warn("%s: invalid number of MPLS labels (%u)",
1445 __func__
, nhr
->nexthops
[i
].label_num
);
1448 if (nhr
->nexthops
[i
].label_num
)
1449 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1450 nhr
->nexthops
[i
].label_num
1451 * sizeof(mpls_label_t
));
1460 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1461 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1462 * then set/unset redist[type] in the client handle (a struct zserv) for the
1465 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1466 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1473 zclient_create_header(s
, command
, vrf_id
);
1474 stream_putc(s
, afi
);
1475 stream_putc(s
, type
);
1476 stream_putw(s
, instance
);
1478 stream_putw_at(s
, 0, stream_get_endp(s
));
1480 return zclient_send_message(zclient
);
1483 /* Get prefix in ZServ format; family should be filled in on prefix */
1484 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1486 size_t plen
= prefix_blen(p
);
1493 stream_get(&p
->u
.prefix
, s
, plen
);
1495 p
->prefixlen
= MIN(plen
* 8, c
);
1501 /* Router-id update from zebra daemon. */
1502 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1504 /* Fetch interface address. */
1505 STREAM_GETC(s
, rid
->family
);
1507 zclient_stream_get_prefix(s
, rid
);
1513 /* Interface addition from zebra daemon. */
1515 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1516 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1518 * 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
1519 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1525 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1527 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1529 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1532 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1534 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1536 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1538 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1540 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1542 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1543 * | Link Layer Type |
1544 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1545 * | Harware Address Length |
1546 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1547 * | Hardware Address if HW lenght different from 0 |
1548 * | ... max INTERFACE_HWADDR_MAX |
1549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1550 * | Link_params? | Whether a link-params follows: 1 or 0.
1551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1552 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1553 * | .... (struct if_link_params). |
1554 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1557 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1560 char vrfname_tmp
[VRF_NAMSIZ
];
1561 struct vrf_data data
;
1563 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1564 /* Read interface name. */
1565 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1567 /* Lookup/create vrf by vrf_id. */
1568 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1569 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1570 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1574 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1578 /* Lookup vrf by vrf_id. */
1579 vrf
= vrf_lookup_by_id(vrf_id
);
1582 * If a routing protocol doesn't know about a
1583 * vrf that is about to be deleted. There is
1584 * no point in attempting to delete it.
1592 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1594 struct interface
*ifp
;
1595 char ifname_tmp
[INTERFACE_NAMSIZ
];
1597 /* Read interface name. */
1598 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1600 /* Lookup/create interface by name. */
1601 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1603 zebra_interface_if_set_value(s
, ifp
);
1609 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1610 * from zebra server. The format of this message is the same as
1611 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1612 * comments for zebra_interface_add_read), except that no sockaddr_dl
1613 * is sent at the tail of the message.
1615 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1617 struct interface
*ifp
;
1618 char ifname_tmp
[INTERFACE_NAMSIZ
];
1620 /* Read interface name. */
1621 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1623 /* Lookup this by interface index. */
1624 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1626 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1627 ifname_tmp
, vrf_id
);
1631 zebra_interface_if_set_value(s
, ifp
);
1636 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1642 iflp
->lp_status
= stream_getl(s
);
1643 iflp
->te_metric
= stream_getl(s
);
1644 iflp
->max_bw
= stream_getf(s
);
1645 iflp
->max_rsv_bw
= stream_getf(s
);
1646 uint32_t bwclassnum
= stream_getl(s
);
1649 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1650 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1653 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1654 " - outdated library?",
1655 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1657 iflp
->admin_grp
= stream_getl(s
);
1658 iflp
->rmt_as
= stream_getl(s
);
1659 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1661 iflp
->av_delay
= stream_getl(s
);
1662 iflp
->min_delay
= stream_getl(s
);
1663 iflp
->max_delay
= stream_getl(s
);
1664 iflp
->delay_var
= stream_getl(s
);
1666 iflp
->pkt_loss
= stream_getf(s
);
1667 iflp
->res_bw
= stream_getf(s
);
1668 iflp
->ava_bw
= stream_getf(s
);
1669 iflp
->use_bw
= stream_getf(s
);
1672 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1674 struct if_link_params
*iflp
;
1679 ifindex
= stream_getl(s
);
1681 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1684 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1689 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1692 link_params_set_value(s
, iflp
);
1697 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1699 uint8_t link_params_status
= 0;
1701 /* Read interface's index. */
1702 if_set_index(ifp
, stream_getl(s
));
1703 ifp
->status
= stream_getc(s
);
1705 /* Read interface's value. */
1706 ifp
->flags
= stream_getq(s
);
1707 ifp
->ptm_enable
= stream_getc(s
);
1708 ifp
->ptm_status
= stream_getc(s
);
1709 ifp
->metric
= stream_getl(s
);
1710 ifp
->speed
= stream_getl(s
);
1711 ifp
->mtu
= stream_getl(s
);
1712 ifp
->mtu6
= stream_getl(s
);
1713 ifp
->bandwidth
= stream_getl(s
);
1714 ifp
->ll_type
= stream_getl(s
);
1715 ifp
->hw_addr_len
= stream_getl(s
);
1716 if (ifp
->hw_addr_len
)
1717 stream_get(ifp
->hw_addr
, s
,
1718 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1720 /* Read Traffic Engineering status */
1721 link_params_status
= stream_getc(s
);
1722 /* Then, Traffic Engineering parameters if any */
1723 if (link_params_status
) {
1724 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1725 link_params_set_value(s
, iflp
);
1729 size_t zebra_interface_link_params_write(struct stream
*s
,
1730 struct interface
*ifp
)
1733 struct if_link_params
*iflp
;
1736 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1739 iflp
= ifp
->link_params
;
1742 w
+= stream_putl(s
, iflp
->lp_status
);
1744 w
+= stream_putl(s
, iflp
->te_metric
);
1745 w
+= stream_putf(s
, iflp
->max_bw
);
1746 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1748 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1749 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1750 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1752 w
+= stream_putl(s
, iflp
->admin_grp
);
1753 w
+= stream_putl(s
, iflp
->rmt_as
);
1754 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1756 w
+= stream_putl(s
, iflp
->av_delay
);
1757 w
+= stream_putl(s
, iflp
->min_delay
);
1758 w
+= stream_putl(s
, iflp
->max_delay
);
1759 w
+= stream_putl(s
, iflp
->delay_var
);
1761 w
+= stream_putf(s
, iflp
->pkt_loss
);
1762 w
+= stream_putf(s
, iflp
->res_bw
);
1763 w
+= stream_putf(s
, iflp
->ava_bw
);
1764 w
+= stream_putf(s
, iflp
->use_bw
);
1770 * format of message for address additon is:
1774 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1775 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1784 * | ifc_flags | flags for connected address
1792 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1800 static int memconstant(const void *s
, int c
, size_t n
)
1802 const uint8_t *p
= s
;
1811 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1815 struct interface
*ifp
;
1816 struct connected
*ifc
;
1817 struct prefix p
, d
, *dp
;
1821 memset(&p
, 0, sizeof(p
));
1822 memset(&d
, 0, sizeof(d
));
1824 /* Get interface index. */
1825 ifindex
= stream_getl(s
);
1828 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1830 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1831 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1837 ifc_flags
= stream_getc(s
);
1839 /* Fetch interface address. */
1840 d
.family
= p
.family
= stream_getc(s
);
1841 plen
= prefix_blen(&d
);
1843 zclient_stream_get_prefix(s
, &p
);
1845 /* Fetch destination address. */
1846 stream_get(&d
.u
.prefix
, s
, plen
);
1848 /* N.B. NULL destination pointers are encoded as all zeroes */
1849 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1851 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1852 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1854 /* N.B. NULL destination pointers are encoded as all
1856 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1859 ifc
->flags
= ifc_flags
;
1860 if (ifc
->destination
)
1861 ifc
->destination
->prefixlen
=
1862 ifc
->address
->prefixlen
;
1863 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1864 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1866 char buf
[PREFIX_STRLEN
];
1868 "warning: interface %s address %s "
1869 "with peer flag set, but no peer address!",
1870 ifp
->name
, prefix2str(ifc
->address
, buf
,
1872 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1876 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1877 ifc
= connected_delete_by_prefix(ifp
, &p
);
1884 * format of message for neighbor connected address is:
1888 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1889 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1904 * | addr_len | len of addr.
1907 struct nbr_connected
*
1908 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1910 unsigned int ifindex
;
1911 struct interface
*ifp
;
1913 struct nbr_connected
*ifc
;
1915 /* Get interface index. */
1916 ifindex
= stream_getl(s
);
1919 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1921 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1922 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1928 p
.family
= stream_getc(s
);
1929 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1930 p
.prefixlen
= stream_getc(s
);
1932 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1933 /* Currently only supporting P2P links, so any new RA source
1935 considered as the replacement of the previously learnt
1936 Link-Local address. */
1937 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1938 ifc
= nbr_connected_new();
1939 ifc
->address
= prefix_new();
1941 listnode_add(ifp
->nbr_connected
, ifc
);
1944 prefix_copy(ifc
->address
, &p
);
1946 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1948 ifc
= nbr_connected_check(ifp
, &p
);
1950 listnode_delete(ifp
->nbr_connected
, ifc
);
1956 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1958 vrf_id_t
*new_vrf_id
)
1960 unsigned int ifindex
;
1961 struct interface
*ifp
;
1964 /* Get interface index. */
1965 ifindex
= stream_getl(s
);
1967 /* Lookup interface. */
1968 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1970 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1975 /* Fetch new VRF Id. */
1976 new_id
= stream_getw(s
);
1978 *new_vrf_id
= new_id
;
1982 /* filter unwanted messages until the expected one arrives */
1983 static int zclient_read_sync_response(struct zclient
*zclient
,
1984 uint16_t expected_cmd
)
1996 cmd
= expected_cmd
+ 1;
1997 while (ret
== 0 && cmd
!= expected_cmd
) {
2001 /* wait until response arrives */
2003 FD_SET(zclient
->sock
, &readfds
);
2004 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2005 if (!FD_ISSET(zclient
->sock
, &readfds
))
2008 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2009 &version
, &vrf_id
, &cmd
);
2011 zlog_debug("%s: Response (%d bytes) received", __func__
,
2015 zlog_err("%s: Invalid Sync Message Reply", __func__
);
2022 * Connect to label manager in a syncronous way
2024 * It first writes the request to zcient output buffer and then
2025 * immediately reads the answer from the input buffer.
2027 * @param zclient Zclient used to connect to label manager (zebra)
2028 * @result Result of response
2030 int lm_label_manager_connect(struct zclient
*zclient
)
2037 zlog_debug("Connecting to Label Manager");
2039 if (zclient
->sock
< 0)
2045 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
2048 stream_putc(s
, zclient
->redist_default
);
2050 stream_putw(s
, zclient
->instance
);
2052 /* Put length at the first point of the stream. */
2053 stream_putw_at(s
, 0, stream_get_endp(s
));
2055 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2057 zlog_err("%s: can't write to zclient->sock", __func__
);
2058 close(zclient
->sock
);
2063 zlog_err("%s: zclient->sock connection closed", __func__
);
2064 close(zclient
->sock
);
2069 zlog_debug("%s: Label manager connect request (%d bytes) sent",
2073 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
2079 result
= stream_getc(s
);
2082 "%s: Label Manager connect response received, result %u",
2089 * Asynchronous label chunk request
2091 * @param zclient Zclient used to connect to label manager (zebra)
2092 * @param keep Avoid garbage collection
2093 * @param chunk_size Amount of labels requested
2094 * @result 0 on success, -1 otherwise
2096 int zclient_send_get_label_chunk(
2097 struct zclient
*zclient
,
2099 uint32_t chunk_size
)
2104 zlog_debug("Getting Label Chunk");
2106 if (zclient
->sock
< 0)
2112 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2113 stream_putc(s
, keep
);
2114 stream_putl(s
, chunk_size
);
2116 /* Put length at the first point of the stream. */
2117 stream_putw_at(s
, 0, stream_get_endp(s
));
2119 return zclient_send_message(zclient
);
2123 * Function to request a label chunk in a syncronous way
2125 * It first writes the request to zlcient output buffer and then
2126 * immediately reads the answer from the input buffer.
2128 * @param zclient Zclient used to connect to label manager (zebra)
2129 * @param keep Avoid garbage collection
2130 * @param chunk_size Amount of labels requested
2131 * @param start To write first assigned chunk label to
2132 * @param end To write last assigned chunk label to
2133 * @result 0 on success, -1 otherwise
2135 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2136 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2140 uint8_t response_keep
;
2143 zlog_debug("Getting Label Chunk");
2145 if (zclient
->sock
< 0)
2151 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2153 stream_putc(s
, keep
);
2155 stream_putl(s
, chunk_size
);
2156 /* Put length at the first point of the stream. */
2157 stream_putw_at(s
, 0, stream_get_endp(s
));
2159 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2161 zlog_err("%s: can't write to zclient->sock", __func__
);
2162 close(zclient
->sock
);
2167 zlog_err("%s: zclient->sock connection closed", __func__
);
2168 close(zclient
->sock
);
2173 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
2177 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2182 response_keep
= stream_getc(s
);
2183 /* start and end labels */
2184 *start
= stream_getl(s
);
2185 *end
= stream_getl(s
);
2187 /* not owning this response */
2188 if (keep
!= response_keep
) {
2190 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2191 __func__
, *start
, *end
, keep
, response_keep
);
2194 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2195 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2196 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
2202 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
2209 * Function to release a label chunk
2211 * @param zclient Zclient used to connect to label manager (zebra)
2212 * @param start First label of chunk
2213 * @param end Last label of chunk
2214 * @result 0 on success, -1 otherwise
2216 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2223 zlog_debug("Releasing Label Chunk");
2225 if (zclient
->sock
< 0)
2231 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2234 stream_putl(s
, start
);
2236 stream_putl(s
, end
);
2238 /* Put length at the first point of the stream. */
2239 stream_putw_at(s
, 0, stream_get_endp(s
));
2241 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2243 zlog_err("%s: can't write to zclient->sock", __func__
);
2244 close(zclient
->sock
);
2249 zlog_err("%s: zclient->sock connection closed", __func__
);
2250 close(zclient
->sock
);
2259 * Connect to table manager in a syncronous way
2261 * It first writes the request to zcient output buffer and then
2262 * immediately reads the answer from the input buffer.
2264 * @param zclient Zclient used to connect to table manager (zebra)
2265 * @result Result of response
2267 int tm_table_manager_connect(struct zclient
*zclient
)
2274 zlog_debug("Connecting to Table Manager");
2276 if (zclient
->sock
< 0)
2282 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2285 stream_putc(s
, zclient
->redist_default
);
2287 stream_putw(s
, zclient
->instance
);
2289 /* Put length at the first point of the stream. */
2290 stream_putw_at(s
, 0, stream_get_endp(s
));
2292 ret
= zclient_send_message(zclient
);
2297 zlog_debug("%s: Table manager connect request sent",
2301 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2307 STREAM_GETC(s
, result
);
2310 "%s: Table Manager connect response received, result %u",
2319 * Function to request a table chunk in a syncronous way
2321 * It first writes the request to zclient output buffer and then
2322 * immediately reads the answer from the input buffer.
2324 * @param zclient Zclient used to connect to table manager (zebra)
2325 * @param chunk_size Amount of table requested
2326 * @param start to write first assigned chunk table RT ID to
2327 * @param end To write last assigned chunk table RT ID to
2328 * @result 0 on success, -1 otherwise
2330 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2331 uint32_t *start
, uint32_t *end
)
2337 zlog_debug("Getting Table Chunk");
2339 if (zclient
->sock
< 0)
2345 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2347 stream_putl(s
, chunk_size
);
2348 /* Put length at the first point of the stream. */
2349 stream_putw_at(s
, 0, stream_get_endp(s
));
2351 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2353 zlog_err("%s: can't write to zclient->sock", __func__
);
2354 close(zclient
->sock
);
2359 zlog_err("%s: zclient->sock connection closed", __func__
);
2360 close(zclient
->sock
);
2365 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2369 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2373 /* start and end table IDs */
2374 STREAM_GETL(s
, *start
);
2375 STREAM_GETL(s
, *end
);
2378 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2385 * Function to release a table chunk
2387 * @param zclient Zclient used to connect to table manager (zebra)
2388 * @param start First label of table
2389 * @param end Last label of chunk
2390 * @result 0 on success, -1 otherwise
2392 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2398 zlog_debug("Releasing Table Chunk");
2400 if (zclient
->sock
< 0)
2406 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2409 stream_putl(s
, start
);
2411 stream_putl(s
, end
);
2413 /* Put length at the first point of the stream. */
2414 stream_putw_at(s
, 0, stream_get_endp(s
));
2416 return zclient_send_message(zclient
);
2420 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2428 zclient_create_header(s
, command
, VRF_DEFAULT
);
2429 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2430 stream_putl(s
, pw
->ifindex
);
2433 stream_putl(s
, pw
->type
);
2436 stream_putl(s
, pw
->af
);
2439 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2442 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2445 zlog_err("%s: unknown af", __func__
);
2450 stream_putl(s
, pw
->local_label
);
2451 stream_putl(s
, pw
->remote_label
);
2454 stream_putc(s
, pw
->flags
);
2456 /* Protocol specific fields */
2457 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2459 /* Put length at the first point of the stream. */
2460 stream_putw_at(s
, 0, stream_get_endp(s
));
2462 return zclient_send_message(zclient
);
2466 * Receive PW status update from Zebra and send it to LDE process.
2468 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2469 zebra_size_t length
, vrf_id_t vrf_id
,
2470 struct zapi_pw_status
*pw
)
2474 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2478 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2479 pw
->ifindex
= stream_getl(s
);
2480 pw
->status
= stream_getl(s
);
2483 /* Zebra client message read function. */
2484 static int zclient_read(struct thread
*thread
)
2487 uint16_t length
, command
;
2488 uint8_t marker
, version
;
2490 struct zclient
*zclient
;
2492 /* Get socket to zebra. */
2493 zclient
= THREAD_ARG(thread
);
2494 zclient
->t_read
= NULL
;
2496 /* Read zebra header (if we don't have it already). */
2497 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2499 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2500 ZEBRA_HEADER_SIZE
- already
))
2505 "zclient connection closed socket [%d].",
2507 return zclient_failed(zclient
);
2509 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2510 /* Try again later. */
2511 zclient_event(ZCLIENT_READ
, zclient
);
2514 already
= ZEBRA_HEADER_SIZE
;
2517 /* Reset to read from the beginning of the incoming packet. */
2518 stream_set_getp(zclient
->ibuf
, 0);
2520 /* Fetch header values. */
2521 length
= stream_getw(zclient
->ibuf
);
2522 marker
= stream_getc(zclient
->ibuf
);
2523 version
= stream_getc(zclient
->ibuf
);
2524 vrf_id
= stream_getl(zclient
->ibuf
);
2525 command
= stream_getw(zclient
->ibuf
);
2527 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2529 "%s: socket %d version mismatch, marker %d, version %d",
2530 __func__
, zclient
->sock
, marker
, version
);
2531 return zclient_failed(zclient
);
2534 if (length
< ZEBRA_HEADER_SIZE
) {
2535 zlog_err("%s: socket %d message length %u is less than %d ",
2536 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2537 return zclient_failed(zclient
);
2541 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2544 "%s: message size %u exceeds buffer size %lu, expanding...",
2546 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2547 ns
= stream_new(length
);
2548 stream_copy(ns
, zclient
->ibuf
);
2549 stream_free(zclient
->ibuf
);
2553 /* Read rest of zebra packet. */
2554 if (already
< length
) {
2556 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2562 "zclient connection closed socket [%d].",
2564 return zclient_failed(zclient
);
2566 if (nbyte
!= (ssize_t
)(length
- already
)) {
2567 /* Try again later. */
2568 zclient_event(ZCLIENT_READ
, zclient
);
2573 length
-= ZEBRA_HEADER_SIZE
;
2576 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2577 (void *)zclient
, command
, vrf_id
);
2580 case ZEBRA_ROUTER_ID_UPDATE
:
2581 if (zclient
->router_id_update
)
2582 (*zclient
->router_id_update
)(command
, zclient
, length
,
2586 zclient_vrf_add(zclient
, vrf_id
);
2588 case ZEBRA_VRF_DELETE
:
2589 zclient_vrf_delete(zclient
, vrf_id
);
2591 case ZEBRA_INTERFACE_ADD
:
2592 if (zclient
->interface_add
)
2593 (*zclient
->interface_add
)(command
, zclient
, length
,
2596 case ZEBRA_INTERFACE_DELETE
:
2597 if (zclient
->interface_delete
)
2598 (*zclient
->interface_delete
)(command
, zclient
, length
,
2601 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2602 if (zclient
->interface_address_add
)
2603 (*zclient
->interface_address_add
)(command
, zclient
,
2606 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2607 if (zclient
->interface_address_delete
)
2608 (*zclient
->interface_address_delete
)(command
, zclient
,
2611 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2612 if (zclient
->interface_bfd_dest_update
)
2613 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2616 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2617 if (zclient
->interface_nbr_address_add
)
2618 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2621 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2622 if (zclient
->interface_nbr_address_delete
)
2623 (*zclient
->interface_nbr_address_delete
)(
2624 command
, zclient
, length
, vrf_id
);
2626 case ZEBRA_INTERFACE_UP
:
2627 if (zclient
->interface_up
)
2628 (*zclient
->interface_up
)(command
, zclient
, length
,
2631 case ZEBRA_INTERFACE_DOWN
:
2632 if (zclient
->interface_down
)
2633 (*zclient
->interface_down
)(command
, zclient
, length
,
2636 case ZEBRA_INTERFACE_VRF_UPDATE
:
2637 if (zclient
->interface_vrf_update
)
2638 (*zclient
->interface_vrf_update
)(command
, zclient
,
2641 case ZEBRA_NEXTHOP_UPDATE
:
2643 zlog_debug("zclient rcvd nexthop update\n");
2644 if (zclient
->nexthop_update
)
2645 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2648 case ZEBRA_IMPORT_CHECK_UPDATE
:
2650 zlog_debug("zclient rcvd import check update\n");
2651 if (zclient
->import_check_update
)
2652 (*zclient
->import_check_update
)(command
, zclient
,
2655 case ZEBRA_BFD_DEST_REPLAY
:
2656 if (zclient
->bfd_dest_replay
)
2657 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2660 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2661 if (zclient
->redistribute_route_add
)
2662 (*zclient
->redistribute_route_add
)(command
, zclient
,
2665 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2666 if (zclient
->redistribute_route_del
)
2667 (*zclient
->redistribute_route_del
)(command
, zclient
,
2670 case ZEBRA_INTERFACE_LINK_PARAMS
:
2671 if (zclient
->interface_link_params
)
2672 (*zclient
->interface_link_params
)(command
, zclient
,
2675 case ZEBRA_FEC_UPDATE
:
2677 zlog_debug("zclient rcvd fec update\n");
2678 if (zclient
->fec_update
)
2679 (*zclient
->fec_update
)(command
, zclient
, length
);
2682 if (zclient
->local_vni_add
)
2683 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2687 if (zclient
->local_vni_del
)
2688 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2691 case ZEBRA_L3VNI_ADD
:
2692 if (zclient
->local_l3vni_add
)
2693 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2696 case ZEBRA_L3VNI_DEL
:
2697 if (zclient
->local_l3vni_del
)
2698 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2701 case ZEBRA_MACIP_ADD
:
2702 if (zclient
->local_macip_add
)
2703 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2706 case ZEBRA_MACIP_DEL
:
2707 if (zclient
->local_macip_del
)
2708 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2711 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2712 if (zclient
->local_ip_prefix_add
)
2713 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2716 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2717 if (zclient
->local_ip_prefix_del
)
2718 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2721 case ZEBRA_PW_STATUS_UPDATE
:
2722 if (zclient
->pw_status_update
)
2723 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2726 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2727 if (zclient
->route_notify_owner
)
2728 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2731 case ZEBRA_RULE_NOTIFY_OWNER
:
2732 if (zclient
->rule_notify_owner
)
2733 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2736 case ZEBRA_GET_LABEL_CHUNK
:
2737 if (zclient
->label_chunk
)
2738 (*zclient
->label_chunk
)(command
, zclient
, length
,
2745 if (zclient
->sock
< 0)
2746 /* Connection was closed during packet processing. */
2749 /* Register read thread. */
2750 stream_reset(zclient
->ibuf
);
2751 zclient_event(ZCLIENT_READ
, zclient
);
2756 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2757 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2761 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2762 if (redist_check_instance(
2763 &zclient
->mi_redist
[afi
][type
], instance
))
2765 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2768 if (!redist_check_instance(
2769 &zclient
->mi_redist
[afi
][type
], instance
))
2771 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2776 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2777 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2780 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2782 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2785 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2789 if (zclient
->sock
> 0)
2790 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2795 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2799 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2800 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2802 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2804 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2806 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2809 if (zclient
->sock
> 0)
2810 zebra_message_send(zclient
, command
, vrf_id
);
2813 static void zclient_event(enum event event
, struct zclient
*zclient
)
2816 case ZCLIENT_SCHEDULE
:
2817 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2818 &zclient
->t_connect
);
2820 case ZCLIENT_CONNECT
:
2823 "zclient connect failures: %d schedule interval is now %d",
2824 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2825 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2826 zclient
->fail
< 3 ? 10 : 60,
2827 &zclient
->t_connect
);
2830 zclient
->t_read
= NULL
;
2831 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2832 zclient
->sock
, &zclient
->t_read
);
2837 void zclient_interface_set_master(struct zclient
*client
,
2838 struct interface
*master
,
2839 struct interface
*slave
)
2846 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2848 stream_putl(s
, master
->vrf_id
);
2849 stream_putl(s
, master
->ifindex
);
2850 stream_putl(s
, slave
->vrf_id
);
2851 stream_putl(s
, slave
->ifindex
);
2853 stream_putw_at(s
, 0, stream_get_endp(s
));
2854 zclient_send_message(client
);