1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
41 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
43 /* Zebra client events. */
44 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
46 /* Prototype for event manager. */
47 static void zclient_event(enum event
, struct zclient
*);
49 struct sockaddr_storage zclient_addr
;
50 socklen_t zclient_addr_len
;
52 /* This file local debug flag. */
53 int zclient_debug
= 0;
55 struct zclient_options zclient_options_default
= {.receive_notify
= false};
57 /* Allocate zclient structure. */
58 struct zclient
*zclient_new_notify(struct thread_master
*master
,
59 struct zclient_options
*opt
)
61 struct zclient
*zclient
;
62 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
64 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
65 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
66 zclient
->wb
= buffer_new(0);
67 zclient
->master
= master
;
69 zclient
->receive_notify
= opt
->receive_notify
;
74 /* This function is only called when exiting, because
75 many parts of the code do not check for I/O errors, so they could
76 reference an invalid pointer if the structure was ever freed.
78 Free zclient structure. */
79 void zclient_free(struct zclient
*zclient
)
82 stream_free(zclient
->ibuf
);
84 stream_free(zclient
->obuf
);
86 buffer_free(zclient
->wb
);
88 XFREE(MTYPE_ZCLIENT
, zclient
);
91 u_short
*redist_check_instance(struct redist_proto
*red
, u_short instance
)
93 struct listnode
*node
;
99 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
106 void redist_add_instance(struct redist_proto
*red
, u_short instance
)
113 red
->instances
= list_new();
115 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(u_short
));
117 listnode_add(red
->instances
, in
);
120 void redist_del_instance(struct redist_proto
*red
, u_short instance
)
124 id
= redist_check_instance(red
, instance
);
128 listnode_delete(red
->instances
, id
);
129 XFREE(MTYPE_REDIST_INST
, id
);
130 if (!red
->instances
->count
) {
132 list_delete_and_null(&red
->instances
);
136 /* Stop zebra client services. */
137 void zclient_stop(struct zclient
*zclient
)
143 zlog_debug("zclient stopped");
146 THREAD_OFF(zclient
->t_read
);
147 THREAD_OFF(zclient
->t_connect
);
148 THREAD_OFF(zclient
->t_write
);
151 stream_reset(zclient
->ibuf
);
152 stream_reset(zclient
->obuf
);
154 /* Empty the write buffer. */
155 buffer_reset(zclient
->wb
);
158 if (zclient
->sock
>= 0) {
159 close(zclient
->sock
);
164 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
165 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
166 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
167 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
170 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
174 vrf_bitmap_free(zclient
->default_information
);
175 zclient
->default_information
= VRF_BITMAP_NULL
;
178 void zclient_reset(struct zclient
*zclient
)
182 zclient_stop(zclient
);
184 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
186 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
189 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
194 * Connect to zebra daemon.
195 * @param zclient a pointer to zclient structure
196 * @return socket fd just to make sure that connection established
198 * @see zclient_new_notify
200 int zclient_socket_connect(struct zclient
*zclient
)
205 /* We should think about IPv6 connection. */
206 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
212 zclient
->privs
->change(ZPRIVS_RAISE
);
213 setsockopt_so_sendbuf(sock
, 1048576);
214 zclient
->privs
->change(ZPRIVS_LOWER
);
216 /* Connect to zebra. */
217 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
220 zlog_warn("%s connect failure: %d(%s)",
221 __PRETTY_FUNCTION__
, errno
,
222 safe_strerror(errno
));
227 zclient
->sock
= sock
;
231 static int zclient_failed(struct zclient
*zclient
)
234 zclient_stop(zclient
);
235 zclient_event(ZCLIENT_CONNECT
, zclient
);
239 static int zclient_flush_data(struct thread
*thread
)
241 struct zclient
*zclient
= THREAD_ARG(thread
);
243 zclient
->t_write
= NULL
;
244 if (zclient
->sock
< 0)
246 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
249 "%s: buffer_flush_available failed on zclient fd %d, closing",
250 __func__
, zclient
->sock
);
251 return zclient_failed(zclient
);
254 zclient
->t_write
= NULL
;
255 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
256 zclient
->sock
, &zclient
->t_write
);
264 int zclient_send_message(struct zclient
*zclient
)
266 if (zclient
->sock
< 0)
268 switch (buffer_write(zclient
->wb
, zclient
->sock
,
269 STREAM_DATA(zclient
->obuf
),
270 stream_get_endp(zclient
->obuf
))) {
272 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
273 __func__
, zclient
->sock
);
274 return zclient_failed(zclient
);
277 THREAD_OFF(zclient
->t_write
);
280 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
281 zclient
->sock
, &zclient
->t_write
);
287 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
289 /* length placeholder, caller can update */
290 stream_putw(s
, ZEBRA_HEADER_SIZE
);
291 stream_putc(s
, ZEBRA_HEADER_MARKER
);
292 stream_putc(s
, ZSERV_VERSION
);
293 stream_putl(s
, vrf_id
);
294 stream_putw(s
, command
);
297 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
298 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
301 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
304 STREAM_GETW(s
, *size
);
305 *size
-= ZEBRA_HEADER_SIZE
;
306 STREAM_GETC(s
, *marker
);
307 STREAM_GETC(s
, *version
);
308 STREAM_GETL(s
, *vrf_id
);
309 STREAM_GETW(s
, *cmd
);
311 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
313 "%s: socket %d version mismatch, marker %d, version %d",
314 __func__
, sock
, *marker
, *version
);
318 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
325 /* Send simple Zebra message. */
326 static int zebra_message_send(struct zclient
*zclient
, int command
,
331 /* Get zclient output buffer. */
335 /* Send very simple command only Zebra message. */
336 zclient_create_header(s
, command
, vrf_id
);
338 return zclient_send_message(zclient
);
341 static int zebra_hello_send(struct zclient
*zclient
)
345 if (zclient
->redist_default
) {
349 /* The VRF ID in the HELLO message is always 0. */
350 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
351 stream_putc(s
, zclient
->redist_default
);
352 stream_putw(s
, zclient
->instance
);
353 if (zclient
->receive_notify
)
358 stream_putw_at(s
, 0, stream_get_endp(s
));
359 return zclient_send_message(zclient
);
365 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
366 mpls_label_t label
, enum lsp_types_t ltype
)
373 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
374 stream_putl(s
, label
);
376 stream_putc(s
, ltype
);
377 stream_putw_at(s
, 0, stream_get_endp(s
));
378 zclient_send_message(zclient
);
381 /* Send register requests to zebra daemon for the information in a VRF. */
382 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
387 /* If not connected to the zebra yet. */
388 if (zclient
->sock
< 0)
392 zlog_debug("%s: send register messages for VRF %u", __func__
,
395 /* We need router-id information. */
396 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
398 /* We need interface information. */
399 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
401 /* Set unwanted redistribute route. */
402 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
403 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
406 /* Flush all redistribute request. */
407 if (vrf_id
== VRF_DEFAULT
) {
408 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
409 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
410 if (!zclient
->mi_redist
[afi
][i
].enabled
)
413 struct listnode
*node
;
416 for (ALL_LIST_ELEMENTS_RO(
417 zclient
->mi_redist
[afi
][i
]
420 if (!(i
== zclient
->redist_default
421 && *id
== zclient
->instance
))
422 zebra_redistribute_send(
423 ZEBRA_REDISTRIBUTE_ADD
,
424 zclient
, afi
, i
, *id
,
430 /* Flush all redistribute request. */
431 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
432 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
433 if (i
!= zclient
->redist_default
434 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
436 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
440 /* If default information is needed. */
441 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
442 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
446 /* Send unregister requests to zebra daemon for the information in a VRF. */
447 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
452 /* If not connected to the zebra yet. */
453 if (zclient
->sock
< 0)
457 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
460 /* We need router-id information. */
461 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
463 /* We need interface information. */
464 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
466 /* Set unwanted redistribute route. */
467 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
468 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
471 /* Flush all redistribute request. */
472 if (vrf_id
== VRF_DEFAULT
) {
473 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
474 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
475 if (!zclient
->mi_redist
[afi
][i
].enabled
)
478 struct listnode
*node
;
481 for (ALL_LIST_ELEMENTS_RO(
482 zclient
->mi_redist
[afi
][i
]
485 if (!(i
== zclient
->redist_default
486 && *id
== zclient
->instance
))
487 zebra_redistribute_send(
488 ZEBRA_REDISTRIBUTE_DELETE
,
489 zclient
, afi
, i
, *id
,
495 /* Flush all redistribute request. */
496 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
497 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
498 if (i
!= zclient
->redist_default
499 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
501 zebra_redistribute_send(
502 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
505 /* If default information is needed. */
506 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
507 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
511 /* Send request to zebra daemon to start or stop RA. */
512 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
513 struct interface
*ifp
, int enable
,
518 /* If not connected to the zebra yet. */
519 if (zclient
->sock
< 0)
522 /* Form and send message. */
527 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
529 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
531 stream_putl(s
, ifp
->ifindex
);
532 stream_putl(s
, ra_interval
);
534 stream_putw_at(s
, 0, stream_get_endp(s
));
536 zclient_send_message(zclient
);
539 /* Make connection to zebra daemon. */
540 int zclient_start(struct zclient
*zclient
)
543 zlog_info("zclient_start is called");
545 /* If already connected to the zebra. */
546 if (zclient
->sock
>= 0)
549 /* Check connect thread. */
550 if (zclient
->t_connect
)
553 if (zclient_socket_connect(zclient
) < 0) {
555 zlog_debug("zclient connection fail");
557 zclient_event(ZCLIENT_CONNECT
, zclient
);
561 if (set_nonblocking(zclient
->sock
) < 0)
562 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
565 /* Clear fail count. */
568 zlog_debug("zclient connect success with socket [%d]",
571 /* Create read thread. */
572 zclient_event(ZCLIENT_READ
, zclient
);
574 zebra_hello_send(zclient
);
576 /* Inform the successful connection. */
577 if (zclient
->zebra_connected
)
578 (*zclient
->zebra_connected
)(zclient
);
583 /* Initialize zebra client. Argument redist_default is unwanted
584 redistribute route type. */
585 void zclient_init(struct zclient
*zclient
, int redist_default
, u_short instance
,
586 struct zebra_privs_t
*privs
)
590 /* Set -1 to the default socket value. */
592 zclient
->privs
= privs
;
594 /* Clear redistribution flags. */
595 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
596 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
597 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
599 /* Set unwanted redistribute route. bgpd does not need BGP route
601 zclient
->redist_default
= redist_default
;
602 zclient
->instance
= instance
;
603 /* Pending: make afi(s) an arg. */
604 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
605 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
608 /* Set default-information redistribute to zero. */
609 zclient
->default_information
= vrf_bitmap_init();
612 zlog_debug("zclient_start is called");
614 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
617 /* This function is a wrapper function for calling zclient_start from
618 timer or event thread. */
619 static int zclient_connect(struct thread
*t
)
621 struct zclient
*zclient
;
623 zclient
= THREAD_ARG(t
);
624 zclient
->t_connect
= NULL
;
627 zlog_debug("zclient_connect is called");
629 return zclient_start(zclient
);
632 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
633 bool exact_match
, vrf_id_t vrf_id
)
639 zclient_create_header(s
, command
, vrf_id
);
640 stream_putc(s
, (exact_match
) ? 1 : 0);
642 stream_putw(s
, PREFIX_FAMILY(p
));
643 stream_putc(s
, p
->prefixlen
);
644 switch (PREFIX_FAMILY(p
)) {
646 stream_put_in_addr(s
, &p
->u
.prefix4
);
649 stream_put(s
, &(p
->u
.prefix6
), 16);
654 stream_putw_at(s
, 0, stream_get_endp(s
));
656 return zclient_send_message(zclient
);
660 * "xdr_encode"-like interface that allows daemon (client) to send
661 * a message to zebra server for a route that needs to be
662 * added/deleted to the kernel. Info about the route is specified
663 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
664 * the info down the zclient socket using the stream_* functions.
666 * The corresponding read ("xdr_decode") function on the server
667 * side is zread_ipv4_add()/zread_ipv4_delete().
669 * 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
670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671 * | Length (2) | Command | Route Type |
672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 * | ZEBRA Flags | Message Flags | Prefix length |
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | Destination IPv4 Prefix for route |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
682 * described, as per the Nexthop count. Each nexthop described as:
685 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | IPv4 Nexthop address or Interface Index number |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * Alternatively, if the route is a blackhole route, then Nexthop count
691 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
694 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
695 * infrastructure was built around the traditional (32-bit "gate OR
696 * ifindex") nexthop data unit. A special encoding can be used to feed
697 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
698 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
700 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
701 * - .nexthop_num == .ifindex_num
702 * - .nexthop and .ifindex are filled with gate and ifindex parts of
703 * each compound nexthop, both in the same order
705 * zapi_ipv4_route() will produce two nexthop data units for each such
706 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
707 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
709 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
712 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
715 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
717 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
719 * XXX: No attention paid to alignment.
721 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
722 struct zapi_ipv4
*api
)
732 /* Some checks for labeled-unicast. The current expectation is that each
733 * nexthop is accompanied by a label in the case of labeled-unicast.
735 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
736 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
737 /* We expect prefixes installed with labels and the number to
739 * the number of nexthops.
741 assert(api
->label_num
== api
->nexthop_num
);
744 zclient_create_header(s
, cmd
, api
->vrf_id
);
746 /* Put type and nexthop. */
747 stream_putc(s
, api
->type
);
748 stream_putw(s
, api
->instance
);
749 stream_putl(s
, api
->flags
);
750 stream_putc(s
, api
->message
);
751 stream_putw(s
, api
->safi
);
753 /* Put prefix information. */
754 psize
= PSIZE(p
->prefixlen
);
755 stream_putc(s
, p
->prefixlen
);
756 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
758 /* Nexthop, ifindex, distance and metric information. */
759 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
760 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
762 for (i
= 0; i
< api
->nexthop_num
; i
++) {
763 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
764 stream_put_in_addr(s
, api
->nexthop
[i
]);
765 /* For labeled-unicast, each nexthop is followed by
767 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
768 stream_putl(s
, api
->label
[i
]);
770 for (i
= 0; i
< api
->ifindex_num
; i
++) {
771 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
772 stream_putl(s
, api
->ifindex
[i
]);
776 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
777 stream_putc(s
, api
->distance
);
778 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
779 stream_putl(s
, api
->metric
);
780 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
781 stream_putl(s
, api
->tag
);
782 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
783 stream_putl(s
, api
->mtu
);
785 /* Put length at the first point of the stream. */
786 stream_putw_at(s
, 0, stream_get_endp(s
));
788 return zclient_send_message(zclient
);
791 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
792 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
802 /* Some checks for labeled-unicast. The current expectation is that each
803 * nexthop is accompanied by a label in the case of labeled-unicast.
805 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
806 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
807 /* We expect prefixes installed with labels and the number to
809 * the number of nexthops.
811 assert(api
->label_num
== api
->nexthop_num
);
814 zclient_create_header(s
, cmd
, api
->vrf_id
);
816 /* Put type and nexthop. */
817 stream_putc(s
, api
->type
);
818 stream_putw(s
, api
->instance
);
819 stream_putl(s
, api
->flags
);
820 stream_putc(s
, api
->message
);
821 stream_putw(s
, api
->safi
);
823 /* Put prefix information. */
824 psize
= PSIZE(p
->prefixlen
);
825 stream_putc(s
, p
->prefixlen
);
826 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
828 /* Nexthop, ifindex, distance and metric information. */
829 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
830 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
832 for (i
= 0; i
< api
->nexthop_num
; i
++) {
833 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
834 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
835 /* For labeled-unicast, each nexthop is followed by
837 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
838 stream_putl(s
, api
->label
[i
]);
840 for (i
= 0; i
< api
->ifindex_num
; i
++) {
841 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
842 stream_putl(s
, api
->ifindex
[i
]);
846 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
847 stream_putc(s
, api
->distance
);
848 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
849 stream_putl(s
, api
->metric
);
850 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
851 stream_putl(s
, api
->tag
);
852 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
853 stream_putl(s
, api
->mtu
);
855 /* Put length at the first point of the stream. */
856 stream_putw_at(s
, 0, stream_get_endp(s
));
858 return zclient_send_message(zclient
);
861 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
862 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
868 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
870 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
876 /* Some checks for labeled-unicast. The current expectation is that each
877 * nexthop is accompanied by a label in the case of labeled-unicast.
879 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
880 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
881 /* We expect prefixes installed with labels and the number to
883 * the number of nexthops.
885 assert(api
->label_num
== api
->nexthop_num
);
888 zclient_create_header(s
, cmd
, api
->vrf_id
);
890 /* Put type and nexthop. */
891 stream_putc(s
, api
->type
);
892 stream_putw(s
, api
->instance
);
893 stream_putl(s
, api
->flags
);
894 stream_putc(s
, api
->message
);
895 stream_putw(s
, api
->safi
);
897 /* Put prefix information. */
898 psize
= PSIZE(p
->prefixlen
);
899 stream_putc(s
, p
->prefixlen
);
900 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
902 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
903 psize
= PSIZE(src_p
->prefixlen
);
904 stream_putc(s
, src_p
->prefixlen
);
905 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
908 /* Nexthop, ifindex, distance and metric information. */
909 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
910 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
912 for (i
= 0; i
< api
->nexthop_num
; i
++) {
913 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
914 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
915 /* For labeled-unicast, each nexthop is followed by
917 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
918 stream_putl(s
, api
->label
[i
]);
920 for (i
= 0; i
< api
->ifindex_num
; i
++) {
921 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
922 stream_putl(s
, api
->ifindex
[i
]);
926 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
927 stream_putc(s
, api
->distance
);
928 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
929 stream_putl(s
, api
->metric
);
930 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
931 stream_putl(s
, api
->tag
);
932 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
933 stream_putl(s
, api
->mtu
);
935 /* Put length at the first point of the stream. */
936 stream_putw_at(s
, 0, stream_get_endp(s
));
938 return zclient_send_message(zclient
);
941 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
942 struct zapi_route
*api
)
944 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
946 return zclient_send_message(zclient
);
949 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
951 struct zapi_nexthop
*api_nh
;
956 zclient_create_header(s
, cmd
, api
->vrf_id
);
958 stream_putc(s
, api
->type
);
959 stream_putw(s
, api
->instance
);
960 stream_putl(s
, api
->flags
);
961 stream_putc(s
, api
->message
);
962 stream_putc(s
, api
->safi
);
963 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
964 stream_put(s
, &(api
->rmac
), sizeof(struct ethaddr
));
966 /* Put prefix information. */
967 stream_putc(s
, api
->prefix
.family
);
968 psize
= PSIZE(api
->prefix
.prefixlen
);
969 stream_putc(s
, api
->prefix
.prefixlen
);
970 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
972 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
973 psize
= PSIZE(api
->src_prefix
.prefixlen
);
974 stream_putc(s
, api
->src_prefix
.prefixlen
);
975 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
979 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
980 /* limit the number of nexthops if necessary */
981 if (api
->nexthop_num
> MULTIPATH_NUM
) {
982 char buf
[PREFIX2STR_BUFFER
];
984 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
986 "%s: prefix %s: can't encode %u nexthops "
988 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
992 stream_putw(s
, api
->nexthop_num
);
994 for (i
= 0; i
< api
->nexthop_num
; i
++) {
995 api_nh
= &api
->nexthops
[i
];
997 stream_putl(s
, api_nh
->vrf_id
);
998 stream_putc(s
, api_nh
->type
);
999 switch (api_nh
->type
) {
1000 case NEXTHOP_TYPE_BLACKHOLE
:
1001 stream_putc(s
, api_nh
->bh_type
);
1003 case NEXTHOP_TYPE_IPV4
:
1004 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1006 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1007 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
1008 stream_putl(s
, api_nh
->ifindex
);
1010 case NEXTHOP_TYPE_IFINDEX
:
1011 stream_putl(s
, api_nh
->ifindex
);
1013 case NEXTHOP_TYPE_IPV6
:
1014 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
1017 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1018 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
1020 stream_putl(s
, api_nh
->ifindex
);
1024 "%s: Specified Nexthop type %d does not exist",
1025 __PRETTY_FUNCTION__
, api_nh
->type
);
1029 /* MPLS labels for BGP-LU or Segment Routing */
1030 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1031 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1032 char buf
[PREFIX2STR_BUFFER
];
1033 prefix2str(&api
->prefix
, buf
,
1036 "%s: prefix %s: can't encode "
1037 "%u labels (maximum is %u)",
1044 stream_putc(s
, api_nh
->label_num
);
1045 stream_put(s
, &api_nh
->labels
[0],
1047 * sizeof(mpls_label_t
));
1053 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1054 stream_putc(s
, api
->distance
);
1055 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1056 stream_putl(s
, api
->metric
);
1057 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1058 stream_putl(s
, api
->tag
);
1059 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1060 stream_putl(s
, api
->mtu
);
1062 /* Put length at the first point of the stream. */
1063 stream_putw_at(s
, 0, stream_get_endp(s
));
1068 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1070 struct zapi_nexthop
*api_nh
;
1073 memset(api
, 0, sizeof(*api
));
1075 /* Type, flags, message. */
1076 STREAM_GETC(s
, api
->type
);
1077 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1078 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1079 __PRETTY_FUNCTION__
, api
->type
);
1083 STREAM_GETW(s
, api
->instance
);
1084 STREAM_GETL(s
, api
->flags
);
1085 STREAM_GETC(s
, api
->message
);
1086 STREAM_GETC(s
, api
->safi
);
1087 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1088 STREAM_GET(&(api
->rmac
), s
, sizeof(struct ethaddr
));
1091 STREAM_GETC(s
, api
->prefix
.family
);
1092 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1093 switch (api
->prefix
.family
) {
1095 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1097 "%s: V4 prefixlen is %d which should not be more than 32",
1098 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1103 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1105 "%s: v6 prefixlen is %d which should not be more than 128",
1106 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1111 zlog_warn("%s: Specified family %d is not v4 or v6",
1112 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1115 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1117 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1118 api
->src_prefix
.family
= AF_INET6
;
1119 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1120 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1122 "%s: SRC Prefix prefixlen received: %d is too large",
1123 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1126 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1127 PSIZE(api
->src_prefix
.prefixlen
));
1129 if (api
->prefix
.family
!= AF_INET6
1130 || api
->src_prefix
.prefixlen
== 0) {
1132 "%s: SRC prefix specified in some manner that makes no sense",
1133 __PRETTY_FUNCTION__
);
1139 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1140 STREAM_GETW(s
, api
->nexthop_num
);
1141 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1142 zlog_warn("%s: invalid number of nexthops (%u)",
1143 __func__
, api
->nexthop_num
);
1147 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1148 api_nh
= &api
->nexthops
[i
];
1150 STREAM_GETL(s
, api_nh
->vrf_id
);
1151 STREAM_GETC(s
, api_nh
->type
);
1152 switch (api_nh
->type
) {
1153 case NEXTHOP_TYPE_BLACKHOLE
:
1154 STREAM_GETC(s
, api_nh
->bh_type
);
1156 case NEXTHOP_TYPE_IPV4
:
1157 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1160 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1161 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1163 STREAM_GETL(s
, api_nh
->ifindex
);
1165 case NEXTHOP_TYPE_IFINDEX
:
1166 STREAM_GETL(s
, api_nh
->ifindex
);
1168 case NEXTHOP_TYPE_IPV6
:
1169 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1171 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1172 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1173 STREAM_GETL(s
, api_nh
->ifindex
);
1177 "%s: Specified nexthop type %d does not exist",
1178 __PRETTY_FUNCTION__
, api_nh
->type
);
1182 /* MPLS labels for BGP-LU or Segment Routing */
1183 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1184 STREAM_GETC(s
, api_nh
->label_num
);
1186 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1188 "%s: invalid number of MPLS "
1190 __func__
, api_nh
->label_num
);
1194 STREAM_GET(&api_nh
->labels
[0], s
,
1196 * sizeof(mpls_label_t
));
1202 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1203 STREAM_GETC(s
, api
->distance
);
1204 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1205 STREAM_GETL(s
, api
->metric
);
1206 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1207 STREAM_GETL(s
, api
->tag
);
1208 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1209 STREAM_GETL(s
, api
->mtu
);
1215 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1217 enum zapi_route_notify_owner
*note
)
1221 STREAM_GET(note
, s
, sizeof(*note
));
1223 STREAM_GETC(s
, p
->family
);
1224 STREAM_GETC(s
, p
->prefixlen
);
1225 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1236 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1238 struct nexthop
*n
= nexthop_new();
1240 n
->type
= znh
->type
;
1241 n
->vrf_id
= znh
->vrf_id
;
1242 n
->ifindex
= znh
->ifindex
;
1243 n
->gate
= znh
->gate
;
1246 * This function does not currently handle labels
1252 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1256 memset(nhr
, 0, sizeof(*nhr
));
1258 STREAM_GETW(s
, nhr
->prefix
.family
);
1259 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1260 switch (nhr
->prefix
.family
) {
1262 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1265 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1271 STREAM_GETC(s
, nhr
->type
);
1272 STREAM_GETW(s
, nhr
->instance
);
1273 STREAM_GETC(s
, nhr
->distance
);
1274 STREAM_GETL(s
, nhr
->metric
);
1275 STREAM_GETC(s
, nhr
->nexthop_num
);
1277 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1278 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1279 switch (nhr
->nexthops
[i
].type
) {
1280 case NEXTHOP_TYPE_IPV4
:
1281 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1282 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1284 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1286 case NEXTHOP_TYPE_IFINDEX
:
1287 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1289 case NEXTHOP_TYPE_IPV6
:
1290 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1291 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1293 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1295 case NEXTHOP_TYPE_BLACKHOLE
:
1306 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1307 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1308 * then set/unset redist[type] in the client handle (a struct zserv) for the
1311 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1312 int type
, u_short instance
, vrf_id_t vrf_id
)
1319 zclient_create_header(s
, command
, vrf_id
);
1320 stream_putc(s
, afi
);
1321 stream_putc(s
, type
);
1322 stream_putw(s
, instance
);
1324 stream_putw_at(s
, 0, stream_get_endp(s
));
1326 return zclient_send_message(zclient
);
1329 /* Get prefix in ZServ format; family should be filled in on prefix */
1330 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1332 size_t plen
= prefix_blen(p
);
1339 stream_get(&p
->u
.prefix
, s
, plen
);
1341 p
->prefixlen
= MIN(plen
* 8, c
);
1347 /* Router-id update from zebra daemon. */
1348 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1350 /* Fetch interface address. */
1351 STREAM_GETC(s
, rid
->family
);
1353 zclient_stream_get_prefix(s
, rid
);
1359 /* Interface addition from zebra daemon. */
1361 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1362 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1364 * 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
1365 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1389 * | Link Layer Type |
1390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 * | Harware Address Length |
1392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1393 * | Hardware Address if HW lenght different from 0 |
1394 * | ... max INTERFACE_HWADDR_MAX |
1395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1396 * | Link_params? | Whether a link-params follows: 1 or 0.
1397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1398 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1399 * | .... (struct if_link_params). |
1400 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1403 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1406 char vrfname_tmp
[VRF_NAMSIZ
];
1407 struct vrf_data data
;
1409 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1410 /* Read interface name. */
1411 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1413 /* Lookup/create vrf by vrf_id. */
1414 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1415 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1416 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1420 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1424 /* Lookup vrf by vrf_id. */
1425 vrf
= vrf_lookup_by_id(vrf_id
);
1428 * If a routing protocol doesn't know about a
1429 * vrf that is about to be deleted. There is
1430 * no point in attempting to delete it.
1438 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1440 struct interface
*ifp
;
1441 char ifname_tmp
[INTERFACE_NAMSIZ
];
1443 /* Read interface name. */
1444 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1446 /* Lookup/create interface by name. */
1447 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1449 zebra_interface_if_set_value(s
, ifp
);
1455 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1456 * from zebra server. The format of this message is the same as
1457 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1458 * comments for zebra_interface_add_read), except that no sockaddr_dl
1459 * is sent at the tail of the message.
1461 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1463 struct interface
*ifp
;
1464 char ifname_tmp
[INTERFACE_NAMSIZ
];
1466 /* Read interface name. */
1467 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1469 /* Lookup this by interface index. */
1470 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1472 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1473 ifname_tmp
, vrf_id
);
1477 zebra_interface_if_set_value(s
, ifp
);
1482 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1488 iflp
->lp_status
= stream_getl(s
);
1489 iflp
->te_metric
= stream_getl(s
);
1490 iflp
->max_bw
= stream_getf(s
);
1491 iflp
->max_rsv_bw
= stream_getf(s
);
1492 uint32_t bwclassnum
= stream_getl(s
);
1495 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1496 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1499 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1500 " - outdated library?",
1501 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1503 iflp
->admin_grp
= stream_getl(s
);
1504 iflp
->rmt_as
= stream_getl(s
);
1505 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1507 iflp
->av_delay
= stream_getl(s
);
1508 iflp
->min_delay
= stream_getl(s
);
1509 iflp
->max_delay
= stream_getl(s
);
1510 iflp
->delay_var
= stream_getl(s
);
1512 iflp
->pkt_loss
= stream_getf(s
);
1513 iflp
->res_bw
= stream_getf(s
);
1514 iflp
->ava_bw
= stream_getf(s
);
1515 iflp
->use_bw
= stream_getf(s
);
1518 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1520 struct if_link_params
*iflp
;
1525 ifindex
= stream_getl(s
);
1527 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1530 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1535 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1538 link_params_set_value(s
, iflp
);
1543 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1545 u_char link_params_status
= 0;
1547 /* Read interface's index. */
1548 if_set_index(ifp
, stream_getl(s
));
1549 ifp
->status
= stream_getc(s
);
1551 /* Read interface's value. */
1552 ifp
->flags
= stream_getq(s
);
1553 ifp
->ptm_enable
= stream_getc(s
);
1554 ifp
->ptm_status
= stream_getc(s
);
1555 ifp
->metric
= stream_getl(s
);
1556 ifp
->speed
= stream_getl(s
);
1557 ifp
->mtu
= stream_getl(s
);
1558 ifp
->mtu6
= stream_getl(s
);
1559 ifp
->bandwidth
= stream_getl(s
);
1560 ifp
->ll_type
= stream_getl(s
);
1561 ifp
->hw_addr_len
= stream_getl(s
);
1562 if (ifp
->hw_addr_len
)
1563 stream_get(ifp
->hw_addr
, s
,
1564 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1566 /* Read Traffic Engineering status */
1567 link_params_status
= stream_getc(s
);
1568 /* Then, Traffic Engineering parameters if any */
1569 if (link_params_status
) {
1570 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1571 link_params_set_value(s
, iflp
);
1575 size_t zebra_interface_link_params_write(struct stream
*s
,
1576 struct interface
*ifp
)
1579 struct if_link_params
*iflp
;
1582 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1585 iflp
= ifp
->link_params
;
1588 w
+= stream_putl(s
, iflp
->lp_status
);
1590 w
+= stream_putl(s
, iflp
->te_metric
);
1591 w
+= stream_putf(s
, iflp
->max_bw
);
1592 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1594 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1595 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1596 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1598 w
+= stream_putl(s
, iflp
->admin_grp
);
1599 w
+= stream_putl(s
, iflp
->rmt_as
);
1600 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1602 w
+= stream_putl(s
, iflp
->av_delay
);
1603 w
+= stream_putl(s
, iflp
->min_delay
);
1604 w
+= stream_putl(s
, iflp
->max_delay
);
1605 w
+= stream_putl(s
, iflp
->delay_var
);
1607 w
+= stream_putf(s
, iflp
->pkt_loss
);
1608 w
+= stream_putf(s
, iflp
->res_bw
);
1609 w
+= stream_putf(s
, iflp
->ava_bw
);
1610 w
+= stream_putf(s
, iflp
->use_bw
);
1616 * format of message for address additon is:
1620 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1621 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1630 * | ifc_flags | flags for connected address
1638 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1646 static int memconstant(const void *s
, int c
, size_t n
)
1648 const u_char
*p
= s
;
1657 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1661 struct interface
*ifp
;
1662 struct connected
*ifc
;
1663 struct prefix p
, d
, *dp
;
1667 memset(&p
, 0, sizeof(p
));
1668 memset(&d
, 0, sizeof(d
));
1670 /* Get interface index. */
1671 ifindex
= stream_getl(s
);
1674 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1676 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1677 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1683 ifc_flags
= stream_getc(s
);
1685 /* Fetch interface address. */
1686 d
.family
= p
.family
= stream_getc(s
);
1687 plen
= prefix_blen(&d
);
1689 zclient_stream_get_prefix(s
, &p
);
1691 /* Fetch destination address. */
1692 stream_get(&d
.u
.prefix
, s
, plen
);
1694 /* N.B. NULL destination pointers are encoded as all zeroes */
1695 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1697 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1698 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1700 /* N.B. NULL destination pointers are encoded as all
1702 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1705 ifc
->flags
= ifc_flags
;
1706 if (ifc
->destination
)
1707 ifc
->destination
->prefixlen
=
1708 ifc
->address
->prefixlen
;
1709 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1710 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1712 char buf
[PREFIX_STRLEN
];
1714 "warning: interface %s address %s "
1715 "with peer flag set, but no peer address!",
1716 ifp
->name
, prefix2str(ifc
->address
, buf
,
1718 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1722 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1723 ifc
= connected_delete_by_prefix(ifp
, &p
);
1730 * format of message for neighbor connected address is:
1734 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1735 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1750 * | addr_len | len of addr.
1753 struct nbr_connected
*
1754 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1756 unsigned int ifindex
;
1757 struct interface
*ifp
;
1759 struct nbr_connected
*ifc
;
1761 /* Get interface index. */
1762 ifindex
= stream_getl(s
);
1765 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1767 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1768 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1774 p
.family
= stream_getc(s
);
1775 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1776 p
.prefixlen
= stream_getc(s
);
1778 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1779 /* Currently only supporting P2P links, so any new RA source
1781 considered as the replacement of the previously learnt
1782 Link-Local address. */
1783 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1784 ifc
= nbr_connected_new();
1785 ifc
->address
= prefix_new();
1787 listnode_add(ifp
->nbr_connected
, ifc
);
1790 prefix_copy(ifc
->address
, &p
);
1792 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1794 ifc
= nbr_connected_check(ifp
, &p
);
1796 listnode_delete(ifp
->nbr_connected
, ifc
);
1802 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1804 vrf_id_t
*new_vrf_id
)
1806 unsigned int ifindex
;
1807 struct interface
*ifp
;
1810 /* Get interface index. */
1811 ifindex
= stream_getl(s
);
1813 /* Lookup interface. */
1814 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1816 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1821 /* Fetch new VRF Id. */
1822 new_id
= stream_getw(s
);
1824 *new_vrf_id
= new_id
;
1828 /* filter unwanted messages until the expected one arrives */
1829 static int zclient_read_sync_response(struct zclient
*zclient
,
1830 u_int16_t expected_cmd
)
1833 u_int16_t size
= -1;
1842 cmd
= expected_cmd
+ 1;
1843 while (ret
== 0 && cmd
!= expected_cmd
) {
1847 /* wait until response arrives */
1849 FD_SET(zclient
->sock
, &readfds
);
1850 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1851 if (!FD_ISSET(zclient
->sock
, &readfds
))
1854 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1855 &version
, &vrf_id
, &cmd
);
1857 zlog_debug("%s: Response (%d bytes) received", __func__
,
1861 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1868 * Connect to label manager in a syncronous way
1870 * It first writes the request to zcient output buffer and then
1871 * immediately reads the answer from the input buffer.
1873 * @param zclient Zclient used to connect to label manager (zebra)
1874 * @result Result of response
1876 int lm_label_manager_connect(struct zclient
*zclient
)
1883 zlog_debug("Connecting to Label Manager");
1885 if (zclient
->sock
< 0)
1891 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1894 stream_putc(s
, zclient
->redist_default
);
1896 stream_putw(s
, zclient
->instance
);
1898 /* Put length at the first point of the stream. */
1899 stream_putw_at(s
, 0, stream_get_endp(s
));
1901 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1903 zlog_err("%s: can't write to zclient->sock", __func__
);
1904 close(zclient
->sock
);
1909 zlog_err("%s: zclient->sock connection closed", __func__
);
1910 close(zclient
->sock
);
1915 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1919 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1925 result
= stream_getc(s
);
1928 "%s: Label Manager connect response received, result %u",
1935 * Function to request a label chunk in a syncronous way
1937 * It first writes the request to zlcient output buffer and then
1938 * immediately reads the answer from the input buffer.
1940 * @param zclient Zclient used to connect to label manager (zebra)
1941 * @param keep Avoid garbage collection
1942 * @param chunk_size Amount of labels requested
1943 * @param start To write first assigned chunk label to
1944 * @param end To write last assigned chunk label to
1945 * @result 0 on success, -1 otherwise
1947 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1948 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1952 u_char response_keep
;
1955 zlog_debug("Getting Label Chunk");
1957 if (zclient
->sock
< 0)
1963 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1965 stream_putc(s
, keep
);
1967 stream_putl(s
, chunk_size
);
1968 /* Put length at the first point of the stream. */
1969 stream_putw_at(s
, 0, stream_get_endp(s
));
1971 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1973 zlog_err("%s: can't write to zclient->sock", __func__
);
1974 close(zclient
->sock
);
1979 zlog_err("%s: zclient->sock connection closed", __func__
);
1980 close(zclient
->sock
);
1985 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1989 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1994 response_keep
= stream_getc(s
);
1995 /* start and end labels */
1996 *start
= stream_getl(s
);
1997 *end
= stream_getl(s
);
1999 /* not owning this response */
2000 if (keep
!= response_keep
) {
2002 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2003 __func__
, *start
, *end
, keep
, response_keep
);
2006 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2007 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2008 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
2014 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
2021 * Function to release a label chunk
2023 * @param zclient Zclient used to connect to label manager (zebra)
2024 * @param start First label of chunk
2025 * @param end Last label of chunk
2026 * @result 0 on success, -1 otherwise
2028 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2035 zlog_debug("Releasing Label Chunk");
2037 if (zclient
->sock
< 0)
2043 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2046 stream_putl(s
, start
);
2048 stream_putl(s
, end
);
2050 /* Put length at the first point of the stream. */
2051 stream_putw_at(s
, 0, stream_get_endp(s
));
2053 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2055 zlog_err("%s: can't write to zclient->sock", __func__
);
2056 close(zclient
->sock
);
2061 zlog_err("%s: zclient->sock connection closed", __func__
);
2062 close(zclient
->sock
);
2070 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2078 zclient_create_header(s
, command
, VRF_DEFAULT
);
2079 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2080 stream_putl(s
, pw
->ifindex
);
2083 stream_putl(s
, pw
->type
);
2086 stream_putl(s
, pw
->af
);
2089 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2092 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
2095 zlog_err("%s: unknown af", __func__
);
2100 stream_putl(s
, pw
->local_label
);
2101 stream_putl(s
, pw
->remote_label
);
2104 stream_putc(s
, pw
->flags
);
2106 /* Protocol specific fields */
2107 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2109 /* Put length at the first point of the stream. */
2110 stream_putw_at(s
, 0, stream_get_endp(s
));
2112 return zclient_send_message(zclient
);
2116 * Receive PW status update from Zebra and send it to LDE process.
2118 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2119 zebra_size_t length
, vrf_id_t vrf_id
,
2120 struct zapi_pw_status
*pw
)
2124 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2128 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2129 pw
->ifindex
= stream_getl(s
);
2130 pw
->status
= stream_getl(s
);
2133 /* Zebra client message read function. */
2134 static int zclient_read(struct thread
*thread
)
2137 uint16_t length
, command
;
2138 uint8_t marker
, version
;
2140 struct zclient
*zclient
;
2142 /* Get socket to zebra. */
2143 zclient
= THREAD_ARG(thread
);
2144 zclient
->t_read
= NULL
;
2146 /* Read zebra header (if we don't have it already). */
2147 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2149 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2150 ZEBRA_HEADER_SIZE
- already
))
2155 "zclient connection closed socket [%d].",
2157 return zclient_failed(zclient
);
2159 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2160 /* Try again later. */
2161 zclient_event(ZCLIENT_READ
, zclient
);
2164 already
= ZEBRA_HEADER_SIZE
;
2167 /* Reset to read from the beginning of the incoming packet. */
2168 stream_set_getp(zclient
->ibuf
, 0);
2170 /* Fetch header values. */
2171 length
= stream_getw(zclient
->ibuf
);
2172 marker
= stream_getc(zclient
->ibuf
);
2173 version
= stream_getc(zclient
->ibuf
);
2174 vrf_id
= stream_getl(zclient
->ibuf
);
2175 command
= stream_getw(zclient
->ibuf
);
2177 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2179 "%s: socket %d version mismatch, marker %d, version %d",
2180 __func__
, zclient
->sock
, marker
, version
);
2181 return zclient_failed(zclient
);
2184 if (length
< ZEBRA_HEADER_SIZE
) {
2185 zlog_err("%s: socket %d message length %u is less than %d ",
2186 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2187 return zclient_failed(zclient
);
2191 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2194 "%s: message size %u exceeds buffer size %lu, expanding...",
2195 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
2196 ns
= stream_new(length
);
2197 stream_copy(ns
, zclient
->ibuf
);
2198 stream_free(zclient
->ibuf
);
2202 /* Read rest of zebra packet. */
2203 if (already
< length
) {
2205 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2211 "zclient connection closed socket [%d].",
2213 return zclient_failed(zclient
);
2215 if (nbyte
!= (ssize_t
)(length
- already
)) {
2216 /* Try again later. */
2217 zclient_event(ZCLIENT_READ
, zclient
);
2222 length
-= ZEBRA_HEADER_SIZE
;
2225 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2226 (void *)zclient
, command
, vrf_id
);
2229 case ZEBRA_ROUTER_ID_UPDATE
:
2230 if (zclient
->router_id_update
)
2231 (*zclient
->router_id_update
)(command
, zclient
, length
,
2235 zclient_vrf_add(zclient
, vrf_id
);
2237 case ZEBRA_VRF_DELETE
:
2238 zclient_vrf_delete(zclient
, vrf_id
);
2240 case ZEBRA_INTERFACE_ADD
:
2241 if (zclient
->interface_add
)
2242 (*zclient
->interface_add
)(command
, zclient
, length
,
2245 case ZEBRA_INTERFACE_DELETE
:
2246 if (zclient
->interface_delete
)
2247 (*zclient
->interface_delete
)(command
, zclient
, length
,
2250 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2251 if (zclient
->interface_address_add
)
2252 (*zclient
->interface_address_add
)(command
, zclient
,
2255 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2256 if (zclient
->interface_address_delete
)
2257 (*zclient
->interface_address_delete
)(command
, zclient
,
2260 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2261 if (zclient
->interface_bfd_dest_update
)
2262 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2265 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2266 if (zclient
->interface_nbr_address_add
)
2267 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2270 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2271 if (zclient
->interface_nbr_address_delete
)
2272 (*zclient
->interface_nbr_address_delete
)(
2273 command
, zclient
, length
, vrf_id
);
2275 case ZEBRA_INTERFACE_UP
:
2276 if (zclient
->interface_up
)
2277 (*zclient
->interface_up
)(command
, zclient
, length
,
2280 case ZEBRA_INTERFACE_DOWN
:
2281 if (zclient
->interface_down
)
2282 (*zclient
->interface_down
)(command
, zclient
, length
,
2285 case ZEBRA_INTERFACE_VRF_UPDATE
:
2286 if (zclient
->interface_vrf_update
)
2287 (*zclient
->interface_vrf_update
)(command
, zclient
,
2290 case ZEBRA_NEXTHOP_UPDATE
:
2292 zlog_debug("zclient rcvd nexthop update\n");
2293 if (zclient
->nexthop_update
)
2294 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2297 case ZEBRA_IMPORT_CHECK_UPDATE
:
2299 zlog_debug("zclient rcvd import check update\n");
2300 if (zclient
->import_check_update
)
2301 (*zclient
->import_check_update
)(command
, zclient
,
2304 case ZEBRA_BFD_DEST_REPLAY
:
2305 if (zclient
->bfd_dest_replay
)
2306 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2309 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2310 if (zclient
->redistribute_route_add
)
2311 (*zclient
->redistribute_route_add
)(command
, zclient
,
2314 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2315 if (zclient
->redistribute_route_del
)
2316 (*zclient
->redistribute_route_del
)(command
, zclient
,
2319 case ZEBRA_INTERFACE_LINK_PARAMS
:
2320 if (zclient
->interface_link_params
)
2321 (*zclient
->interface_link_params
)(command
, zclient
,
2324 case ZEBRA_FEC_UPDATE
:
2326 zlog_debug("zclient rcvd fec update\n");
2327 if (zclient
->fec_update
)
2328 (*zclient
->fec_update
)(command
, zclient
, length
);
2331 if (zclient
->local_vni_add
)
2332 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2336 if (zclient
->local_vni_del
)
2337 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2340 case ZEBRA_L3VNI_ADD
:
2341 if (zclient
->local_l3vni_add
)
2342 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2345 case ZEBRA_L3VNI_DEL
:
2346 if (zclient
->local_l3vni_del
)
2347 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2350 case ZEBRA_MACIP_ADD
:
2351 if (zclient
->local_macip_add
)
2352 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2355 case ZEBRA_MACIP_DEL
:
2356 if (zclient
->local_macip_del
)
2357 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2360 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2361 if (zclient
->local_ip_prefix_add
)
2362 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2365 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2366 if (zclient
->local_ip_prefix_del
)
2367 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2370 case ZEBRA_PW_STATUS_UPDATE
:
2371 if (zclient
->pw_status_update
)
2372 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2375 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2376 if (zclient
->route_notify_owner
)
2377 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2384 if (zclient
->sock
< 0)
2385 /* Connection was closed during packet processing. */
2388 /* Register read thread. */
2389 stream_reset(zclient
->ibuf
);
2390 zclient_event(ZCLIENT_READ
, zclient
);
2395 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2396 int type
, u_short instance
, vrf_id_t vrf_id
)
2400 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2401 if (redist_check_instance(
2402 &zclient
->mi_redist
[afi
][type
], instance
))
2404 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2407 if (!redist_check_instance(
2408 &zclient
->mi_redist
[afi
][type
], instance
))
2410 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2415 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2416 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2419 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2421 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2424 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2428 if (zclient
->sock
> 0)
2429 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2434 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2438 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2439 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2441 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2443 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2445 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2448 if (zclient
->sock
> 0)
2449 zebra_message_send(zclient
, command
, vrf_id
);
2452 static void zclient_event(enum event event
, struct zclient
*zclient
)
2455 case ZCLIENT_SCHEDULE
:
2456 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2457 &zclient
->t_connect
);
2459 case ZCLIENT_CONNECT
:
2462 "zclient connect failures: %d schedule interval is now %d",
2463 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2464 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2465 zclient
->fail
< 3 ? 10 : 60,
2466 &zclient
->t_connect
);
2469 zclient
->t_read
= NULL
;
2470 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2471 zclient
->sock
, &zclient
->t_read
);
2476 void zclient_interface_set_master(struct zclient
*client
,
2477 struct interface
*master
,
2478 struct interface
*slave
)
2485 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2487 stream_putl(s
, master
->vrf_id
);
2488 stream_putl(s
, master
->ifindex
);
2489 stream_putl(s
, slave
->vrf_id
);
2490 stream_putl(s
, slave
->ifindex
);
2492 stream_putw_at(s
, 0, stream_get_endp(s
));
2493 zclient_send_message(client
);