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
,
190 zclient
->instance
, zclient
->privs
);
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
,
221 zlog_warn("%s connect failure: %d(%s)",
222 __PRETTY_FUNCTION__
, errno
,
223 safe_strerror(errno
));
228 zclient
->sock
= sock
;
232 static int zclient_failed(struct zclient
*zclient
)
235 zclient_stop(zclient
);
236 zclient_event(ZCLIENT_CONNECT
, zclient
);
240 static int zclient_flush_data(struct thread
*thread
)
242 struct zclient
*zclient
= THREAD_ARG(thread
);
244 zclient
->t_write
= NULL
;
245 if (zclient
->sock
< 0)
247 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
250 "%s: buffer_flush_available failed on zclient fd %d, closing",
251 __func__
, zclient
->sock
);
252 return zclient_failed(zclient
);
255 zclient
->t_write
= NULL
;
256 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
257 zclient
->sock
, &zclient
->t_write
);
265 int zclient_send_message(struct zclient
*zclient
)
267 if (zclient
->sock
< 0)
269 switch (buffer_write(zclient
->wb
, zclient
->sock
,
270 STREAM_DATA(zclient
->obuf
),
271 stream_get_endp(zclient
->obuf
))) {
273 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
274 __func__
, zclient
->sock
);
275 return zclient_failed(zclient
);
278 THREAD_OFF(zclient
->t_write
);
281 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
282 zclient
->sock
, &zclient
->t_write
);
288 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
290 /* length placeholder, caller can update */
291 stream_putw(s
, ZEBRA_HEADER_SIZE
);
292 stream_putc(s
, ZEBRA_HEADER_MARKER
);
293 stream_putc(s
, ZSERV_VERSION
);
294 stream_putl(s
, vrf_id
);
295 stream_putw(s
, command
);
298 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
299 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
302 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
305 STREAM_GETW(s
, *size
);
306 *size
-= ZEBRA_HEADER_SIZE
;
307 STREAM_GETC(s
, *marker
);
308 STREAM_GETC(s
, *version
);
309 STREAM_GETL(s
, *vrf_id
);
310 STREAM_GETW(s
, *cmd
);
312 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
314 "%s: socket %d version mismatch, marker %d, version %d",
315 __func__
, sock
, *marker
, *version
);
319 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
326 /* Send simple Zebra message. */
327 static int zebra_message_send(struct zclient
*zclient
, int command
,
332 /* Get zclient output buffer. */
336 /* Send very simple command only Zebra message. */
337 zclient_create_header(s
, command
, vrf_id
);
339 return zclient_send_message(zclient
);
342 static int zebra_hello_send(struct zclient
*zclient
)
346 if (zclient
->redist_default
) {
350 /* The VRF ID in the HELLO message is always 0. */
351 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
352 stream_putc(s
, zclient
->redist_default
);
353 stream_putw(s
, zclient
->instance
);
354 if (zclient
->receive_notify
)
359 stream_putw_at(s
, 0, stream_get_endp(s
));
360 return zclient_send_message(zclient
);
366 /* Send register requests to zebra daemon for the information in a VRF. */
367 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
372 /* If not connected to the zebra yet. */
373 if (zclient
->sock
< 0)
377 zlog_debug("%s: send register messages for VRF %u", __func__
,
380 /* We need router-id information. */
381 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
383 /* We need interface information. */
384 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
386 /* Set unwanted redistribute route. */
387 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
388 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
391 /* Flush all redistribute request. */
392 if (vrf_id
== VRF_DEFAULT
) {
393 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
394 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
395 if (!zclient
->mi_redist
[afi
][i
].enabled
)
398 struct listnode
*node
;
401 for (ALL_LIST_ELEMENTS_RO(
402 zclient
->mi_redist
[afi
][i
]
403 .instances
, node
, id
))
404 if (!(i
== zclient
->redist_default
405 && *id
== zclient
->instance
))
406 zebra_redistribute_send(
407 ZEBRA_REDISTRIBUTE_ADD
,
415 /* Flush all redistribute request. */
416 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
417 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
418 if (i
!= zclient
->redist_default
419 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
421 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
425 /* If default information is needed. */
426 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
427 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
431 /* Send unregister requests to zebra daemon for the information in a VRF. */
432 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
437 /* If not connected to the zebra yet. */
438 if (zclient
->sock
< 0)
442 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
445 /* We need router-id information. */
446 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
448 /* We need interface information. */
449 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
451 /* Set unwanted redistribute route. */
452 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
453 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
456 /* Flush all redistribute request. */
457 if (vrf_id
== VRF_DEFAULT
) {
458 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
459 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
460 if (!zclient
->mi_redist
[afi
][i
].enabled
)
463 struct listnode
*node
;
466 for (ALL_LIST_ELEMENTS_RO(
467 zclient
->mi_redist
[afi
][i
]
468 .instances
, node
, id
))
469 if (!(i
== zclient
->redist_default
470 && *id
== zclient
->instance
))
471 zebra_redistribute_send(
472 ZEBRA_REDISTRIBUTE_DELETE
,
480 /* Flush all redistribute request. */
481 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
482 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
483 if (i
!= zclient
->redist_default
484 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
486 zebra_redistribute_send(
487 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
490 /* If default information is needed. */
491 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
492 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
496 /* Send request to zebra daemon to start or stop RA. */
497 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
498 struct interface
*ifp
, int enable
,
503 /* If not connected to the zebra yet. */
504 if (zclient
->sock
< 0)
507 /* Form and send message. */
512 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
514 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
516 stream_putl(s
, ifp
->ifindex
);
517 stream_putl(s
, ra_interval
);
519 stream_putw_at(s
, 0, stream_get_endp(s
));
521 zclient_send_message(zclient
);
524 /* Make connection to zebra daemon. */
525 int zclient_start(struct zclient
*zclient
)
528 zlog_info("zclient_start is called");
530 /* If already connected to the zebra. */
531 if (zclient
->sock
>= 0)
534 /* Check connect thread. */
535 if (zclient
->t_connect
)
538 if (zclient_socket_connect(zclient
) < 0) {
540 zlog_debug("zclient connection fail");
542 zclient_event(ZCLIENT_CONNECT
, zclient
);
546 if (set_nonblocking(zclient
->sock
) < 0)
547 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
550 /* Clear fail count. */
553 zlog_debug("zclient connect success with socket [%d]",
556 /* Create read thread. */
557 zclient_event(ZCLIENT_READ
, zclient
);
559 zebra_hello_send(zclient
);
561 /* Inform the successful connection. */
562 if (zclient
->zebra_connected
)
563 (*zclient
->zebra_connected
)(zclient
);
568 /* Initialize zebra client. Argument redist_default is unwanted
569 redistribute route type. */
570 void zclient_init(struct zclient
*zclient
, int redist_default
,
571 u_short instance
, struct zebra_privs_t
*privs
)
575 /* Set -1 to the default socket value. */
577 zclient
->privs
= privs
;
579 /* Clear redistribution flags. */
580 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
581 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
582 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
584 /* Set unwanted redistribute route. bgpd does not need BGP route
586 zclient
->redist_default
= redist_default
;
587 zclient
->instance
= instance
;
588 /* Pending: make afi(s) an arg. */
589 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
590 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
593 /* Set default-information redistribute to zero. */
594 zclient
->default_information
= vrf_bitmap_init();
597 zlog_debug("zclient_start is called");
599 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
602 /* This function is a wrapper function for calling zclient_start from
603 timer or event thread. */
604 static int zclient_connect(struct thread
*t
)
606 struct zclient
*zclient
;
608 zclient
= THREAD_ARG(t
);
609 zclient
->t_connect
= NULL
;
612 zlog_debug("zclient_connect is called");
614 return zclient_start(zclient
);
617 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
618 bool exact_match
, vrf_id_t vrf_id
)
624 zclient_create_header(s
, command
, vrf_id
);
625 stream_putc(s
, (exact_match
) ? 1 : 0);
627 stream_putw(s
, PREFIX_FAMILY(p
));
628 stream_putc(s
, p
->prefixlen
);
629 switch (PREFIX_FAMILY(p
)) {
631 stream_put_in_addr(s
, &p
->u
.prefix4
);
634 stream_put(s
, &(p
->u
.prefix6
), 16);
639 stream_putw_at(s
, 0, stream_get_endp(s
));
641 return zclient_send_message(zclient
);
645 * "xdr_encode"-like interface that allows daemon (client) to send
646 * a message to zebra server for a route that needs to be
647 * added/deleted to the kernel. Info about the route is specified
648 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
649 * the info down the zclient socket using the stream_* functions.
651 * The corresponding read ("xdr_decode") function on the server
652 * side is zread_ipv4_add()/zread_ipv4_delete().
654 * 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
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * | Length (2) | Command | Route Type |
657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 * | ZEBRA Flags | Message Flags | Prefix length |
659 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
660 * | Destination IPv4 Prefix for route |
661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
666 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
667 * described, as per the Nexthop count. Each nexthop described as:
670 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672 * | IPv4 Nexthop address or Interface Index number |
673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * Alternatively, if the route is a blackhole route, then Nexthop count
676 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
679 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
680 * infrastructure was built around the traditional (32-bit "gate OR
681 * ifindex") nexthop data unit. A special encoding can be used to feed
682 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
683 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
685 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
686 * - .nexthop_num == .ifindex_num
687 * - .nexthop and .ifindex are filled with gate and ifindex parts of
688 * each compound nexthop, both in the same order
690 * zapi_ipv4_route() will produce two nexthop data units for each such
691 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
692 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
694 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
697 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
700 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
702 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
704 * XXX: No attention paid to alignment.
706 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
707 struct zapi_ipv4
*api
)
717 /* Some checks for labeled-unicast. The current expectation is that each
718 * nexthop is accompanied by a label in the case of labeled-unicast.
720 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
721 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
722 /* We expect prefixes installed with labels and the number to
724 * the number of nexthops.
726 assert(api
->label_num
== api
->nexthop_num
);
729 zclient_create_header(s
, cmd
, api
->vrf_id
);
731 /* Put type and nexthop. */
732 stream_putc(s
, api
->type
);
733 stream_putw(s
, api
->instance
);
734 stream_putl(s
, api
->flags
);
735 stream_putc(s
, api
->message
);
736 stream_putw(s
, api
->safi
);
738 /* Put prefix information. */
739 psize
= PSIZE(p
->prefixlen
);
740 stream_putc(s
, p
->prefixlen
);
741 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
743 /* Nexthop, ifindex, distance and metric information. */
744 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
745 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
747 for (i
= 0; i
< api
->nexthop_num
; i
++) {
748 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
749 stream_put_in_addr(s
, api
->nexthop
[i
]);
750 /* For labeled-unicast, each nexthop is followed by
752 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
753 stream_putl(s
, api
->label
[i
]);
755 for (i
= 0; i
< api
->ifindex_num
; i
++) {
756 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
757 stream_putl(s
, api
->ifindex
[i
]);
761 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
762 stream_putc(s
, api
->distance
);
763 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
764 stream_putl(s
, api
->metric
);
765 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
766 stream_putl(s
, api
->tag
);
767 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
768 stream_putl(s
, api
->mtu
);
770 /* Put length at the first point of the stream. */
771 stream_putw_at(s
, 0, stream_get_endp(s
));
773 return zclient_send_message(zclient
);
776 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
777 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
787 /* Some checks for labeled-unicast. The current expectation is that each
788 * nexthop is accompanied by a label in the case of labeled-unicast.
790 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
791 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
792 /* We expect prefixes installed with labels and the number to
794 * the number of nexthops.
796 assert(api
->label_num
== api
->nexthop_num
);
799 zclient_create_header(s
, cmd
, api
->vrf_id
);
801 /* Put type and nexthop. */
802 stream_putc(s
, api
->type
);
803 stream_putw(s
, api
->instance
);
804 stream_putl(s
, api
->flags
);
805 stream_putc(s
, api
->message
);
806 stream_putw(s
, api
->safi
);
808 /* Put prefix information. */
809 psize
= PSIZE(p
->prefixlen
);
810 stream_putc(s
, p
->prefixlen
);
811 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
813 /* Nexthop, ifindex, distance and metric information. */
814 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
815 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
817 for (i
= 0; i
< api
->nexthop_num
; i
++) {
818 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
819 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
820 /* For labeled-unicast, each nexthop is followed by
822 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
823 stream_putl(s
, api
->label
[i
]);
825 for (i
= 0; i
< api
->ifindex_num
; i
++) {
826 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
827 stream_putl(s
, api
->ifindex
[i
]);
831 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
832 stream_putc(s
, api
->distance
);
833 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
834 stream_putl(s
, api
->metric
);
835 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
836 stream_putl(s
, api
->tag
);
837 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
838 stream_putl(s
, api
->mtu
);
840 /* Put length at the first point of the stream. */
841 stream_putw_at(s
, 0, stream_get_endp(s
));
843 return zclient_send_message(zclient
);
846 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
847 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
853 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
855 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
861 /* Some checks for labeled-unicast. The current expectation is that each
862 * nexthop is accompanied by a label in the case of labeled-unicast.
864 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
865 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
866 /* We expect prefixes installed with labels and the number to
868 * the number of nexthops.
870 assert(api
->label_num
== api
->nexthop_num
);
873 zclient_create_header(s
, cmd
, api
->vrf_id
);
875 /* Put type and nexthop. */
876 stream_putc(s
, api
->type
);
877 stream_putw(s
, api
->instance
);
878 stream_putl(s
, api
->flags
);
879 stream_putc(s
, api
->message
);
880 stream_putw(s
, api
->safi
);
882 /* Put prefix information. */
883 psize
= PSIZE(p
->prefixlen
);
884 stream_putc(s
, p
->prefixlen
);
885 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
887 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
888 psize
= PSIZE(src_p
->prefixlen
);
889 stream_putc(s
, src_p
->prefixlen
);
890 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
893 /* Nexthop, ifindex, distance and metric information. */
894 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
895 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
897 for (i
= 0; i
< api
->nexthop_num
; i
++) {
898 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
899 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
900 /* For labeled-unicast, each nexthop is followed by
902 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
903 stream_putl(s
, api
->label
[i
]);
905 for (i
= 0; i
< api
->ifindex_num
; i
++) {
906 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
907 stream_putl(s
, api
->ifindex
[i
]);
911 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
912 stream_putc(s
, api
->distance
);
913 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
914 stream_putl(s
, api
->metric
);
915 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
916 stream_putl(s
, api
->tag
);
917 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
918 stream_putl(s
, api
->mtu
);
920 /* Put length at the first point of the stream. */
921 stream_putw_at(s
, 0, stream_get_endp(s
));
923 return zclient_send_message(zclient
);
926 int zclient_route_send(u_char cmd
, struct zclient
*zclient
,
927 struct zapi_route
*api
)
929 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
931 return zclient_send_message(zclient
);
934 int zapi_route_encode(u_char cmd
, struct stream
*s
, struct zapi_route
*api
)
936 struct zapi_nexthop
*api_nh
;
941 zclient_create_header(s
, cmd
, api
->vrf_id
);
943 stream_putc(s
, api
->type
);
944 stream_putw(s
, api
->instance
);
945 stream_putl(s
, api
->flags
);
946 stream_putc(s
, api
->message
);
947 stream_putc(s
, api
->safi
);
948 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
949 stream_put(s
, &(api
->rmac
), sizeof(struct ethaddr
));
951 /* Put prefix information. */
952 stream_putc(s
, api
->prefix
.family
);
953 psize
= PSIZE(api
->prefix
.prefixlen
);
954 stream_putc(s
, api
->prefix
.prefixlen
);
955 stream_write(s
, (u_char
*)&api
->prefix
.u
.prefix
, psize
);
957 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
958 psize
= PSIZE(api
->src_prefix
.prefixlen
);
959 stream_putc(s
, api
->src_prefix
.prefixlen
);
960 stream_write(s
, (u_char
*)&api
->src_prefix
.prefix
, psize
);
964 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
965 /* limit the number of nexthops if necessary */
966 if (api
->nexthop_num
> MULTIPATH_NUM
) {
967 char buf
[PREFIX2STR_BUFFER
];
969 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
971 "%s: prefix %s: can't encode %u nexthops "
973 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
977 stream_putw(s
, api
->nexthop_num
);
978 if (api
->nexthop_num
)
979 stream_putl(s
, api
->nh_vrf_id
);
981 for (i
= 0; i
< api
->nexthop_num
; i
++) {
982 api_nh
= &api
->nexthops
[i
];
984 stream_putc(s
, api_nh
->type
);
985 switch (api_nh
->type
) {
986 case NEXTHOP_TYPE_BLACKHOLE
:
987 stream_putc(s
, api_nh
->bh_type
);
989 case NEXTHOP_TYPE_IPV4
:
990 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
992 case NEXTHOP_TYPE_IPV4_IFINDEX
:
993 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
994 stream_putl(s
, api_nh
->ifindex
);
996 case NEXTHOP_TYPE_IFINDEX
:
997 stream_putl(s
, api_nh
->ifindex
);
999 case NEXTHOP_TYPE_IPV6
:
1000 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
1003 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1004 stream_write(s
, (u_char
*)&api_nh
->gate
.ipv6
,
1006 stream_putl(s
, api_nh
->ifindex
);
1009 zlog_warn("%s: Specified Nexthop type %d does not exist",
1010 __PRETTY_FUNCTION__
, api_nh
->type
);
1014 /* MPLS labels for BGP-LU or Segment Routing */
1015 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1016 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1017 char buf
[PREFIX2STR_BUFFER
];
1018 prefix2str(&api
->prefix
, buf
,
1021 "%s: prefix %s: can't encode "
1022 "%u labels (maximum is %u)",
1029 stream_putc(s
, api_nh
->label_num
);
1030 stream_put(s
, &api_nh
->labels
[0],
1032 * sizeof(mpls_label_t
));
1038 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1039 stream_putc(s
, api
->distance
);
1040 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1041 stream_putl(s
, api
->metric
);
1042 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1043 stream_putl(s
, api
->tag
);
1044 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1045 stream_putl(s
, api
->mtu
);
1047 /* Put length at the first point of the stream. */
1048 stream_putw_at(s
, 0, stream_get_endp(s
));
1053 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1055 struct zapi_nexthop
*api_nh
;
1058 memset(api
, 0, sizeof(*api
));
1060 /* Type, flags, message. */
1061 STREAM_GETC(s
, api
->type
);
1062 if (api
->type
> ZEBRA_ROUTE_MAX
) {
1063 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1064 __PRETTY_FUNCTION__
, api
->type
);
1068 STREAM_GETW(s
, api
->instance
);
1069 STREAM_GETL(s
, api
->flags
);
1070 STREAM_GETC(s
, api
->message
);
1071 STREAM_GETC(s
, api
->safi
);
1072 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1073 stream_get(&(api
->rmac
), s
, sizeof(struct ethaddr
));
1076 STREAM_GETC(s
, api
->prefix
.family
);
1077 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1078 switch (api
->prefix
.family
) {
1080 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1081 zlog_warn("%s: V4 prefixlen is %d which should not be more than 32",
1082 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1087 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1088 zlog_warn("%s: v6 prefixlen is %d which should not be more than 128",
1089 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1094 zlog_warn("%s: Specified family %d is not v4 or v6",
1095 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1098 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1100 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1101 api
->src_prefix
.family
= AF_INET6
;
1102 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1103 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1104 zlog_warn("%s: SRC Prefix prefixlen received: %d is too large",
1105 __PRETTY_FUNCTION__
,
1106 api
->src_prefix
.prefixlen
);
1109 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1110 PSIZE(api
->src_prefix
.prefixlen
));
1112 if (api
->prefix
.family
!= AF_INET6
1113 || api
->src_prefix
.prefixlen
== 0) {
1114 zlog_warn("%s: SRC prefix specified in some manner that makes no sense",
1115 __PRETTY_FUNCTION__
);
1121 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1122 STREAM_GETW(s
, api
->nexthop_num
);
1123 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1124 zlog_warn("%s: invalid number of nexthops (%u)",
1125 __func__
, api
->nexthop_num
);
1129 if (api
->nexthop_num
)
1130 STREAM_GETL(s
, api
->nh_vrf_id
);
1132 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1133 api_nh
= &api
->nexthops
[i
];
1135 STREAM_GETC(s
, api_nh
->type
);
1136 switch (api_nh
->type
) {
1137 case NEXTHOP_TYPE_BLACKHOLE
:
1138 STREAM_GETC(s
, api_nh
->bh_type
);
1140 case NEXTHOP_TYPE_IPV4
:
1141 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1144 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1145 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1147 STREAM_GETL(s
, api_nh
->ifindex
);
1149 case NEXTHOP_TYPE_IFINDEX
:
1150 STREAM_GETL(s
, api_nh
->ifindex
);
1152 case NEXTHOP_TYPE_IPV6
:
1153 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1155 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1156 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1157 STREAM_GETL(s
, api_nh
->ifindex
);
1160 zlog_warn("%s: Specified nexthop type %d does not exist",
1161 __PRETTY_FUNCTION__
,
1166 /* MPLS labels for BGP-LU or Segment Routing */
1167 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1168 STREAM_GETC(s
, api_nh
->label_num
);
1170 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1172 "%s: invalid number of MPLS "
1174 __func__
, api_nh
->label_num
);
1178 STREAM_GET(&api_nh
->labels
[0], s
,
1180 * sizeof(mpls_label_t
));
1186 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1187 STREAM_GETC(s
, api
->distance
);
1188 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1189 STREAM_GETL(s
, api
->metric
);
1190 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1191 STREAM_GETL(s
, api
->tag
);
1192 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1193 STREAM_GETL(s
, api
->mtu
);
1199 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1200 enum zapi_route_notify_owner
*note
)
1202 STREAM_GET(note
, s
, sizeof(*note
));
1204 STREAM_GETC(s
, p
->family
);
1205 STREAM_GETC(s
, p
->prefixlen
);
1206 STREAM_GET(&p
->u
.prefix
, s
,
1207 PSIZE(p
->prefixlen
));
1215 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1217 struct nexthop
*n
= nexthop_new();
1219 n
->type
= znh
->type
;
1220 n
->ifindex
= znh
->ifindex
;
1221 n
->gate
= znh
->gate
;
1224 * This function does not currently handle labels
1230 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1234 memset(nhr
, 0, sizeof(*nhr
));
1236 STREAM_GETW(s
, nhr
->prefix
.family
);
1237 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1238 switch(nhr
->prefix
.family
) {
1240 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1243 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1249 STREAM_GETC(s
, nhr
->distance
);
1250 STREAM_GETL(s
, nhr
->metric
);
1251 STREAM_GETC(s
, nhr
->nexthop_num
);
1253 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1254 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1255 switch (nhr
->nexthops
[i
].type
) {
1256 case NEXTHOP_TYPE_IPV4
:
1257 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1258 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
,
1259 s
, IPV4_MAX_BYTELEN
);
1260 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1262 case NEXTHOP_TYPE_IFINDEX
:
1263 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1265 case NEXTHOP_TYPE_IPV6
:
1266 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1267 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
,
1268 s
, IPV6_MAX_BYTELEN
);
1269 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1271 case NEXTHOP_TYPE_BLACKHOLE
:
1282 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1283 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1284 * then set/unset redist[type] in the client handle (a struct zserv) for the
1287 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1288 int type
, u_short instance
, vrf_id_t vrf_id
)
1295 zclient_create_header(s
, command
, vrf_id
);
1296 stream_putc(s
, afi
);
1297 stream_putc(s
, type
);
1298 stream_putw(s
, instance
);
1300 stream_putw_at(s
, 0, stream_get_endp(s
));
1302 return zclient_send_message(zclient
);
1305 /* Get prefix in ZServ format; family should be filled in on prefix */
1306 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1308 size_t plen
= prefix_blen(p
);
1315 stream_get(&p
->u
.prefix
, s
, plen
);
1317 p
->prefixlen
= MIN(plen
* 8, c
);
1323 /* Router-id update from zebra daemon. */
1324 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1326 /* Fetch interface address. */
1327 STREAM_GETC(s
, rid
->family
);
1329 zclient_stream_get_prefix(s
, rid
);
1335 /* Interface addition from zebra daemon. */
1337 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1338 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1340 * 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
1341 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1347 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1349 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1351 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1356 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1358 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1360 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1365 * | Link Layer Type |
1366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1367 * | Harware Address Length |
1368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1369 * | Hardware Address if HW lenght different from 0 |
1370 * | ... max INTERFACE_HWADDR_MAX |
1371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1372 * | Link_params? | Whether a link-params follows: 1 or 0.
1373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1374 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1375 * | .... (struct if_link_params). |
1376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1379 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1382 char vrfname_tmp
[VRF_NAMSIZ
];
1383 struct vrf_data data
;
1385 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1386 /* Read interface name. */
1387 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1389 /* Lookup/create vrf by vrf_id. */
1390 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1396 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1400 /* Lookup vrf by vrf_id. */
1401 vrf
= vrf_lookup_by_id(vrf_id
);
1404 * If a routing protocol doesn't know about a
1405 * vrf that is about to be deleted. There is
1406 * no point in attempting to delete it.
1414 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1416 struct interface
*ifp
;
1417 char ifname_tmp
[INTERFACE_NAMSIZ
];
1419 /* Read interface name. */
1420 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1422 /* Lookup/create interface by name. */
1423 ifp
= if_get_by_name(ifname_tmp
, vrf_id
, 0);
1425 zebra_interface_if_set_value(s
, ifp
);
1431 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1432 * from zebra server. The format of this message is the same as
1433 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1434 * comments for zebra_interface_add_read), except that no sockaddr_dl
1435 * is sent at the tail of the message.
1437 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1439 struct interface
*ifp
;
1440 char ifname_tmp
[INTERFACE_NAMSIZ
];
1442 /* Read interface name. */
1443 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1445 /* Lookup this by interface index. */
1446 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1448 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1449 ifname_tmp
, vrf_id
);
1453 zebra_interface_if_set_value(s
, ifp
);
1458 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1464 iflp
->lp_status
= stream_getl(s
);
1465 iflp
->te_metric
= stream_getl(s
);
1466 iflp
->max_bw
= stream_getf(s
);
1467 iflp
->max_rsv_bw
= stream_getf(s
);
1468 uint32_t bwclassnum
= stream_getl(s
);
1471 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1472 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1475 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1476 " - outdated library?",
1477 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1479 iflp
->admin_grp
= stream_getl(s
);
1480 iflp
->rmt_as
= stream_getl(s
);
1481 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1483 iflp
->av_delay
= stream_getl(s
);
1484 iflp
->min_delay
= stream_getl(s
);
1485 iflp
->max_delay
= stream_getl(s
);
1486 iflp
->delay_var
= stream_getl(s
);
1488 iflp
->pkt_loss
= stream_getf(s
);
1489 iflp
->res_bw
= stream_getf(s
);
1490 iflp
->ava_bw
= stream_getf(s
);
1491 iflp
->use_bw
= stream_getf(s
);
1494 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1496 struct if_link_params
*iflp
;
1501 ifindex
= stream_getl(s
);
1503 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1506 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1511 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1514 link_params_set_value(s
, iflp
);
1519 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1521 u_char link_params_status
= 0;
1523 /* Read interface's index. */
1524 if_set_index(ifp
, stream_getl(s
));
1525 ifp
->status
= stream_getc(s
);
1527 /* Read interface's value. */
1528 ifp
->flags
= stream_getq(s
);
1529 ifp
->ptm_enable
= stream_getc(s
);
1530 ifp
->ptm_status
= stream_getc(s
);
1531 ifp
->metric
= stream_getl(s
);
1532 ifp
->speed
= stream_getl(s
);
1533 ifp
->mtu
= stream_getl(s
);
1534 ifp
->mtu6
= stream_getl(s
);
1535 ifp
->bandwidth
= stream_getl(s
);
1536 ifp
->ll_type
= stream_getl(s
);
1537 ifp
->hw_addr_len
= stream_getl(s
);
1538 if (ifp
->hw_addr_len
)
1539 stream_get(ifp
->hw_addr
, s
,
1540 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1542 /* Read Traffic Engineering status */
1543 link_params_status
= stream_getc(s
);
1544 /* Then, Traffic Engineering parameters if any */
1545 if (link_params_status
) {
1546 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1547 link_params_set_value(s
, iflp
);
1551 size_t zebra_interface_link_params_write(struct stream
*s
,
1552 struct interface
*ifp
)
1555 struct if_link_params
*iflp
;
1558 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1561 iflp
= ifp
->link_params
;
1564 w
+= stream_putl(s
, iflp
->lp_status
);
1566 w
+= stream_putl(s
, iflp
->te_metric
);
1567 w
+= stream_putf(s
, iflp
->max_bw
);
1568 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1570 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1571 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1572 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1574 w
+= stream_putl(s
, iflp
->admin_grp
);
1575 w
+= stream_putl(s
, iflp
->rmt_as
);
1576 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1578 w
+= stream_putl(s
, iflp
->av_delay
);
1579 w
+= stream_putl(s
, iflp
->min_delay
);
1580 w
+= stream_putl(s
, iflp
->max_delay
);
1581 w
+= stream_putl(s
, iflp
->delay_var
);
1583 w
+= stream_putf(s
, iflp
->pkt_loss
);
1584 w
+= stream_putf(s
, iflp
->res_bw
);
1585 w
+= stream_putf(s
, iflp
->ava_bw
);
1586 w
+= stream_putf(s
, iflp
->use_bw
);
1592 * format of message for address additon is:
1596 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1597 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1606 * | ifc_flags | flags for connected address
1614 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1622 static int memconstant(const void *s
, int c
, size_t n
)
1624 const u_char
*p
= s
;
1633 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1637 struct interface
*ifp
;
1638 struct connected
*ifc
;
1639 struct prefix p
, d
, *dp
;
1643 memset(&p
, 0, sizeof(p
));
1644 memset(&d
, 0, sizeof(d
));
1646 /* Get interface index. */
1647 ifindex
= stream_getl(s
);
1650 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1652 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1653 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1659 ifc_flags
= stream_getc(s
);
1661 /* Fetch interface address. */
1662 d
.family
= p
.family
= stream_getc(s
);
1663 plen
= prefix_blen(&d
);
1665 zclient_stream_get_prefix(s
, &p
);
1667 /* Fetch destination address. */
1668 stream_get(&d
.u
.prefix
, s
, plen
);
1670 /* N.B. NULL destination pointers are encoded as all zeroes */
1671 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1673 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1674 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1676 /* N.B. NULL destination pointers are encoded as all
1678 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1681 ifc
->flags
= ifc_flags
;
1682 if (ifc
->destination
)
1683 ifc
->destination
->prefixlen
=
1684 ifc
->address
->prefixlen
;
1685 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1686 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1688 char buf
[PREFIX_STRLEN
];
1690 "warning: interface %s address %s "
1691 "with peer flag set, but no peer address!",
1692 ifp
->name
, prefix2str(ifc
->address
, buf
,
1694 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1698 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1699 ifc
= connected_delete_by_prefix(ifp
, &p
);
1706 * format of message for neighbor connected address is:
1710 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1711 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1726 * | addr_len | len of addr.
1729 struct nbr_connected
*
1730 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1732 unsigned int ifindex
;
1733 struct interface
*ifp
;
1735 struct nbr_connected
*ifc
;
1737 /* Get interface index. */
1738 ifindex
= stream_getl(s
);
1741 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1743 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1744 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1750 p
.family
= stream_getc(s
);
1751 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1752 p
.prefixlen
= stream_getc(s
);
1754 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1755 /* Currently only supporting P2P links, so any new RA source
1757 considered as the replacement of the previously learnt
1758 Link-Local address. */
1759 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1760 ifc
= nbr_connected_new();
1761 ifc
->address
= prefix_new();
1763 listnode_add(ifp
->nbr_connected
, ifc
);
1766 prefix_copy(ifc
->address
, &p
);
1768 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1770 ifc
= nbr_connected_check(ifp
, &p
);
1772 listnode_delete(ifp
->nbr_connected
, ifc
);
1778 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1780 vrf_id_t
*new_vrf_id
)
1782 unsigned int ifindex
;
1783 struct interface
*ifp
;
1786 /* Get interface index. */
1787 ifindex
= stream_getl(s
);
1789 /* Lookup interface. */
1790 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1792 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1797 /* Fetch new VRF Id. */
1798 new_id
= stream_getw(s
);
1800 *new_vrf_id
= new_id
;
1804 /* filter unwanted messages until the expected one arrives */
1805 static int zclient_read_sync_response(struct zclient
*zclient
,
1806 u_int16_t expected_cmd
)
1809 u_int16_t size
= -1;
1818 cmd
= expected_cmd
+ 1;
1819 while (ret
== 0 && cmd
!= expected_cmd
) {
1823 /* wait until response arrives */
1825 FD_SET(zclient
->sock
, &readfds
);
1826 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1827 if (!FD_ISSET(zclient
->sock
, &readfds
))
1830 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1831 &version
, &vrf_id
, &cmd
);
1833 zlog_debug("%s: Response (%d bytes) received", __func__
,
1837 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1844 * Connect to label manager in a syncronous way
1846 * It first writes the request to zcient output buffer and then
1847 * immediately reads the answer from the input buffer.
1849 * @param zclient Zclient used to connect to label manager (zebra)
1850 * @result Result of response
1852 int lm_label_manager_connect(struct zclient
*zclient
)
1859 zlog_debug("Connecting to Label Manager");
1861 if (zclient
->sock
< 0)
1867 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1870 stream_putc(s
, zclient
->redist_default
);
1872 stream_putw(s
, zclient
->instance
);
1874 /* Put length at the first point of the stream. */
1875 stream_putw_at(s
, 0, stream_get_endp(s
));
1877 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1879 zlog_err("%s: can't write to zclient->sock", __func__
);
1880 close(zclient
->sock
);
1885 zlog_err("%s: zclient->sock connection closed", __func__
);
1886 close(zclient
->sock
);
1891 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1895 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1901 result
= stream_getc(s
);
1904 "%s: Label Manager connect response received, result %u",
1911 * Function to request a label chunk in a syncronous way
1913 * It first writes the request to zlcient output buffer and then
1914 * immediately reads the answer from the input buffer.
1916 * @param zclient Zclient used to connect to label manager (zebra)
1917 * @param keep Avoid garbage collection
1918 * @param chunk_size Amount of labels requested
1919 * @param start To write first assigned chunk label to
1920 * @param end To write last assigned chunk label to
1921 * @result 0 on success, -1 otherwise
1923 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1924 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1928 u_char response_keep
;
1931 zlog_debug("Getting Label Chunk");
1933 if (zclient
->sock
< 0)
1939 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1941 stream_putc(s
, keep
);
1943 stream_putl(s
, chunk_size
);
1944 /* Put length at the first point of the stream. */
1945 stream_putw_at(s
, 0, stream_get_endp(s
));
1947 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1949 zlog_err("%s: can't write to zclient->sock", __func__
);
1950 close(zclient
->sock
);
1955 zlog_err("%s: zclient->sock connection closed", __func__
);
1956 close(zclient
->sock
);
1961 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1965 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1970 response_keep
= stream_getc(s
);
1971 /* start and end labels */
1972 *start
= stream_getl(s
);
1973 *end
= stream_getl(s
);
1975 /* not owning this response */
1976 if (keep
!= response_keep
) {
1978 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1979 __func__
, *start
, *end
, keep
, response_keep
);
1982 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1983 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1984 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1990 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1997 * Function to release a label chunk
1999 * @param zclient Zclient used to connect to label manager (zebra)
2000 * @param start First label of chunk
2001 * @param end Last label of chunk
2002 * @result 0 on success, -1 otherwise
2004 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2011 zlog_debug("Releasing Label Chunk");
2013 if (zclient
->sock
< 0)
2019 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2022 stream_putl(s
, start
);
2024 stream_putl(s
, end
);
2026 /* Put length at the first point of the stream. */
2027 stream_putw_at(s
, 0, stream_get_endp(s
));
2029 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2031 zlog_err("%s: can't write to zclient->sock", __func__
);
2032 close(zclient
->sock
);
2037 zlog_err("%s: zclient->sock connection closed", __func__
);
2038 close(zclient
->sock
);
2046 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2054 zclient_create_header(s
, command
, VRF_DEFAULT
);
2055 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2056 stream_putl(s
, pw
->ifindex
);
2059 stream_putl(s
, pw
->type
);
2062 stream_putl(s
, pw
->af
);
2065 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2068 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
2071 zlog_err("%s: unknown af", __func__
);
2076 stream_putl(s
, pw
->local_label
);
2077 stream_putl(s
, pw
->remote_label
);
2080 stream_putc(s
, pw
->flags
);
2082 /* Protocol specific fields */
2083 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2085 /* Put length at the first point of the stream. */
2086 stream_putw_at(s
, 0, stream_get_endp(s
));
2088 return zclient_send_message(zclient
);
2092 * Receive PW status update from Zebra and send it to LDE process.
2094 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2095 zebra_size_t length
, vrf_id_t vrf_id
,
2096 struct zapi_pw_status
*pw
)
2100 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2104 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2105 pw
->ifindex
= stream_getl(s
);
2106 pw
->status
= stream_getl(s
);
2109 /* Zebra client message read function. */
2110 static int zclient_read(struct thread
*thread
)
2113 uint16_t length
, command
;
2114 uint8_t marker
, version
;
2116 struct zclient
*zclient
;
2118 /* Get socket to zebra. */
2119 zclient
= THREAD_ARG(thread
);
2120 zclient
->t_read
= NULL
;
2122 /* Read zebra header (if we don't have it already). */
2123 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2125 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2126 ZEBRA_HEADER_SIZE
- already
))
2131 "zclient connection closed socket [%d].",
2133 return zclient_failed(zclient
);
2135 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2136 /* Try again later. */
2137 zclient_event(ZCLIENT_READ
, zclient
);
2140 already
= ZEBRA_HEADER_SIZE
;
2143 /* Reset to read from the beginning of the incoming packet. */
2144 stream_set_getp(zclient
->ibuf
, 0);
2146 /* Fetch header values. */
2147 length
= stream_getw(zclient
->ibuf
);
2148 marker
= stream_getc(zclient
->ibuf
);
2149 version
= stream_getc(zclient
->ibuf
);
2150 vrf_id
= stream_getl(zclient
->ibuf
);
2151 command
= stream_getw(zclient
->ibuf
);
2153 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2155 "%s: socket %d version mismatch, marker %d, version %d",
2156 __func__
, zclient
->sock
, marker
, version
);
2157 return zclient_failed(zclient
);
2160 if (length
< ZEBRA_HEADER_SIZE
) {
2161 zlog_err("%s: socket %d message length %u is less than %d ",
2162 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2163 return zclient_failed(zclient
);
2167 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2170 "%s: message size %u exceeds buffer size %lu, expanding...",
2171 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
2172 ns
= stream_new(length
);
2173 stream_copy(ns
, zclient
->ibuf
);
2174 stream_free(zclient
->ibuf
);
2178 /* Read rest of zebra packet. */
2179 if (already
< length
) {
2181 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2187 "zclient connection closed socket [%d].",
2189 return zclient_failed(zclient
);
2191 if (nbyte
!= (ssize_t
)(length
- already
)) {
2192 /* Try again later. */
2193 zclient_event(ZCLIENT_READ
, zclient
);
2198 length
-= ZEBRA_HEADER_SIZE
;
2201 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2202 (void *)zclient
, command
, vrf_id
);
2205 case ZEBRA_ROUTER_ID_UPDATE
:
2206 if (zclient
->router_id_update
)
2207 (*zclient
->router_id_update
)(command
, zclient
, length
,
2211 zclient_vrf_add(zclient
, vrf_id
);
2213 case ZEBRA_VRF_DELETE
:
2214 zclient_vrf_delete(zclient
, vrf_id
);
2216 case ZEBRA_INTERFACE_ADD
:
2217 if (zclient
->interface_add
)
2218 (*zclient
->interface_add
)(command
, zclient
, length
,
2221 case ZEBRA_INTERFACE_DELETE
:
2222 if (zclient
->interface_delete
)
2223 (*zclient
->interface_delete
)(command
, zclient
, length
,
2226 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2227 if (zclient
->interface_address_add
)
2228 (*zclient
->interface_address_add
)(command
, zclient
,
2231 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2232 if (zclient
->interface_address_delete
)
2233 (*zclient
->interface_address_delete
)(command
, zclient
,
2236 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2237 if (zclient
->interface_bfd_dest_update
)
2238 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2241 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2242 if (zclient
->interface_nbr_address_add
)
2243 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2246 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2247 if (zclient
->interface_nbr_address_delete
)
2248 (*zclient
->interface_nbr_address_delete
)(
2249 command
, zclient
, length
, vrf_id
);
2251 case ZEBRA_INTERFACE_UP
:
2252 if (zclient
->interface_up
)
2253 (*zclient
->interface_up
)(command
, zclient
, length
,
2256 case ZEBRA_INTERFACE_DOWN
:
2257 if (zclient
->interface_down
)
2258 (*zclient
->interface_down
)(command
, zclient
, length
,
2261 case ZEBRA_INTERFACE_VRF_UPDATE
:
2262 if (zclient
->interface_vrf_update
)
2263 (*zclient
->interface_vrf_update
)(command
, zclient
,
2266 case ZEBRA_NEXTHOP_UPDATE
:
2268 zlog_debug("zclient rcvd nexthop update\n");
2269 if (zclient
->nexthop_update
)
2270 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2273 case ZEBRA_IMPORT_CHECK_UPDATE
:
2275 zlog_debug("zclient rcvd import check update\n");
2276 if (zclient
->import_check_update
)
2277 (*zclient
->import_check_update
)(command
, zclient
,
2280 case ZEBRA_BFD_DEST_REPLAY
:
2281 if (zclient
->bfd_dest_replay
)
2282 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2285 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2286 if (zclient
->redistribute_route_add
)
2287 (*zclient
->redistribute_route_add
)(command
, zclient
,
2290 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2291 if (zclient
->redistribute_route_del
)
2292 (*zclient
->redistribute_route_del
)(command
, zclient
,
2295 case ZEBRA_INTERFACE_LINK_PARAMS
:
2296 if (zclient
->interface_link_params
)
2297 (*zclient
->interface_link_params
)(command
, zclient
,
2300 case ZEBRA_FEC_UPDATE
:
2302 zlog_debug("zclient rcvd fec update\n");
2303 if (zclient
->fec_update
)
2304 (*zclient
->fec_update
)(command
, zclient
, length
);
2307 if (zclient
->local_vni_add
)
2308 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2312 if (zclient
->local_vni_del
)
2313 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2316 case ZEBRA_L3VNI_ADD
:
2317 if (zclient
->local_l3vni_add
)
2318 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2321 case ZEBRA_L3VNI_DEL
:
2322 if (zclient
->local_l3vni_del
)
2323 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2326 case ZEBRA_MACIP_ADD
:
2327 if (zclient
->local_macip_add
)
2328 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2331 case ZEBRA_MACIP_DEL
:
2332 if (zclient
->local_macip_del
)
2333 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2336 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2337 if (zclient
->local_ip_prefix_add
)
2338 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2341 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2342 if (zclient
->local_ip_prefix_del
)
2343 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2346 case ZEBRA_PW_STATUS_UPDATE
:
2347 if (zclient
->pw_status_update
)
2348 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2351 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2352 if (zclient
->notify_owner
)
2353 (*zclient
->notify_owner
)(command
, zclient
,
2360 if (zclient
->sock
< 0)
2361 /* Connection was closed during packet processing. */
2364 /* Register read thread. */
2365 stream_reset(zclient
->ibuf
);
2366 zclient_event(ZCLIENT_READ
, zclient
);
2371 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2372 int type
, u_short instance
, vrf_id_t vrf_id
)
2376 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2377 if (redist_check_instance(
2378 &zclient
->mi_redist
[afi
][type
], instance
))
2380 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2383 if (!redist_check_instance(
2384 &zclient
->mi_redist
[afi
][type
], instance
))
2386 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2391 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2392 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2395 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2397 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2400 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2404 if (zclient
->sock
> 0)
2405 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2410 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2414 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2415 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2417 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2419 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2421 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2424 if (zclient
->sock
> 0)
2425 zebra_message_send(zclient
, command
, vrf_id
);
2428 static void zclient_event(enum event event
, struct zclient
*zclient
)
2431 case ZCLIENT_SCHEDULE
:
2432 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2433 &zclient
->t_connect
);
2435 case ZCLIENT_CONNECT
:
2438 "zclient connect failures: %d schedule interval is now %d",
2439 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2440 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2441 zclient
->fail
< 3 ? 10 : 60,
2442 &zclient
->t_connect
);
2445 zclient
->t_read
= NULL
;
2446 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2447 zclient
->sock
, &zclient
->t_read
);
2452 void zclient_interface_set_master(struct zclient
*client
,
2453 struct interface
*master
,
2454 struct interface
*slave
)
2461 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2463 stream_putl(s
, master
->vrf_id
);
2464 stream_putl(s
, master
->ifindex
);
2465 stream_putl(s
, slave
->vrf_id
);
2466 stream_putl(s
, slave
->ifindex
);
2468 stream_putw_at(s
, 0, stream_get_endp(s
));
2469 zclient_send_message(client
);