1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
40 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
42 /* Zebra client events. */
43 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
45 /* Prototype for event manager. */
46 static void zclient_event(enum event
, struct zclient
*);
48 const char *zclient_serv_path
= NULL
;
50 /* This file local debug flag. */
51 int zclient_debug
= 0;
53 /* Allocate zclient structure. */
54 struct zclient
*zclient_new(struct thread_master
*master
)
56 struct zclient
*zclient
;
57 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
59 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
60 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
61 zclient
->wb
= buffer_new(0);
62 zclient
->master
= master
;
67 /* This function is only called when exiting, because
68 many parts of the code do not check for I/O errors, so they could
69 reference an invalid pointer if the structure was ever freed.
71 Free zclient structure. */
72 void zclient_free(struct zclient
*zclient
)
75 stream_free(zclient
->ibuf
);
77 stream_free(zclient
->obuf
);
79 buffer_free(zclient
->wb
);
81 XFREE(MTYPE_ZCLIENT
, zclient
);
84 u_short
*redist_check_instance(struct redist_proto
*red
, u_short instance
)
86 struct listnode
*node
;
92 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
99 void redist_add_instance(struct redist_proto
*red
, u_short instance
)
106 red
->instances
= list_new();
108 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(u_short
));
110 listnode_add(red
->instances
, in
);
113 void redist_del_instance(struct redist_proto
*red
, u_short instance
)
117 id
= redist_check_instance(red
, instance
);
121 listnode_delete(red
->instances
, id
);
122 XFREE(MTYPE_REDIST_INST
, id
);
123 if (!red
->instances
->count
) {
125 list_free(red
->instances
);
126 red
->instances
= NULL
;
130 /* Stop zebra client services. */
131 void zclient_stop(struct zclient
*zclient
)
137 zlog_debug("zclient stopped");
140 THREAD_OFF(zclient
->t_read
);
141 THREAD_OFF(zclient
->t_connect
);
142 THREAD_OFF(zclient
->t_write
);
145 stream_reset(zclient
->ibuf
);
146 stream_reset(zclient
->obuf
);
148 /* Empty the write buffer. */
149 buffer_reset(zclient
->wb
);
152 if (zclient
->sock
>= 0) {
153 close(zclient
->sock
);
158 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
159 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
160 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
161 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
164 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
168 vrf_bitmap_free(zclient
->default_information
);
169 zclient
->default_information
= VRF_BITMAP_NULL
;
172 void zclient_reset(struct zclient
*zclient
)
176 zclient_stop(zclient
);
178 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
180 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
183 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
);
186 #ifdef HAVE_TCP_ZEBRA
188 /* Make socket to zebra daemon. Return zebra socket. */
189 static int zclient_socket(void)
193 struct sockaddr_in serv
;
195 /* We should think about IPv6 connection. */
196 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
200 /* Make server socket. */
201 memset(&serv
, 0, sizeof(struct sockaddr_in
));
202 serv
.sin_family
= AF_INET
;
203 serv
.sin_port
= htons(ZEBRA_PORT
);
204 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
205 serv
.sin_len
= sizeof(struct sockaddr_in
);
206 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
207 serv
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
209 /* Connect to zebra. */
210 ret
= connect(sock
, (struct sockaddr
*)&serv
, sizeof(serv
));
213 zlog_warn("%s connect failure: %d(%s)",
214 __PRETTY_FUNCTION__
, errno
,
215 safe_strerror(errno
));
224 /* For sockaddr_un. */
227 static int zclient_socket_un(const char *path
)
231 struct sockaddr_un addr
;
233 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
237 /* Make server socket. */
238 memset(&addr
, 0, sizeof(struct sockaddr_un
));
239 addr
.sun_family
= AF_UNIX
;
240 strncpy(addr
.sun_path
, path
, strlen(path
));
241 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
242 len
= addr
.sun_len
= SUN_LEN(&addr
);
244 len
= sizeof(addr
.sun_family
) + strlen(addr
.sun_path
);
245 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
247 ret
= connect(sock
, (struct sockaddr
*)&addr
, len
);
250 zlog_warn("%s connect failure: %d(%s)",
251 __PRETTY_FUNCTION__
, errno
,
252 safe_strerror(errno
));
259 #endif /* HAVE_TCP_ZEBRA */
262 * Connect to zebra daemon.
263 * @param zclient a pointer to zclient structure
264 * @return socket fd just to make sure that connection established
268 int zclient_socket_connect(struct zclient
*zclient
)
270 #ifdef HAVE_TCP_ZEBRA
271 zclient
->sock
= zclient_socket();
273 zclient
->sock
= zclient_socket_un(zclient_serv_path_get());
275 return zclient
->sock
;
278 static int zclient_failed(struct zclient
*zclient
)
281 zclient_stop(zclient
);
282 zclient_event(ZCLIENT_CONNECT
, zclient
);
286 static int zclient_flush_data(struct thread
*thread
)
288 struct zclient
*zclient
= THREAD_ARG(thread
);
290 zclient
->t_write
= NULL
;
291 if (zclient
->sock
< 0)
293 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
296 "%s: buffer_flush_available failed on zclient fd %d, closing",
297 __func__
, zclient
->sock
);
298 return zclient_failed(zclient
);
301 zclient
->t_write
= NULL
;
302 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
303 zclient
->sock
, &zclient
->t_write
);
311 int zclient_send_message(struct zclient
*zclient
)
313 if (zclient
->sock
< 0)
315 switch (buffer_write(zclient
->wb
, zclient
->sock
,
316 STREAM_DATA(zclient
->obuf
),
317 stream_get_endp(zclient
->obuf
))) {
319 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
320 __func__
, zclient
->sock
);
321 return zclient_failed(zclient
);
324 THREAD_OFF(zclient
->t_write
);
327 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
328 zclient
->sock
, &zclient
->t_write
);
334 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
336 /* length placeholder, caller can update */
337 stream_putw(s
, ZEBRA_HEADER_SIZE
);
338 stream_putc(s
, ZEBRA_HEADER_MARKER
);
339 stream_putc(s
, ZSERV_VERSION
);
340 stream_putw(s
, vrf_id
);
341 stream_putw(s
, command
);
344 int zclient_read_header(struct stream
*s
, int sock
, u_int16_t
*size
,
345 u_char
*marker
, u_char
*version
, vrf_id_t
*vrf_id
,
348 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
351 *size
= stream_getw(s
) - ZEBRA_HEADER_SIZE
;
352 *marker
= stream_getc(s
);
353 *version
= stream_getc(s
);
354 *vrf_id
= stream_getw(s
);
355 *cmd
= stream_getw(s
);
357 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
359 "%s: socket %d version mismatch, marker %d, version %d",
360 __func__
, sock
, *marker
, *version
);
364 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
370 /* Send simple Zebra message. */
371 static int zebra_message_send(struct zclient
*zclient
, int command
,
376 /* Get zclient output buffer. */
380 /* Send very simple command only Zebra message. */
381 zclient_create_header(s
, command
, vrf_id
);
383 return zclient_send_message(zclient
);
386 static int zebra_hello_send(struct zclient
*zclient
)
390 if (zclient
->redist_default
) {
394 /* The VRF ID in the HELLO message is always 0. */
395 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
396 stream_putc(s
, zclient
->redist_default
);
397 stream_putw(s
, zclient
->instance
);
398 stream_putw_at(s
, 0, stream_get_endp(s
));
399 return zclient_send_message(zclient
);
405 /* Send register requests to zebra daemon for the information in a VRF. */
406 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
411 /* zclient is disabled. */
412 if (!zclient
->enable
)
415 /* If not connected to the zebra yet. */
416 if (zclient
->sock
< 0)
420 zlog_debug("%s: send register messages for VRF %u", __func__
,
423 /* We need router-id information. */
424 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
426 /* We need interface information. */
427 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
429 /* Set unwanted redistribute route. */
430 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
431 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
434 /* Flush all redistribute request. */
435 if (vrf_id
== VRF_DEFAULT
)
436 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
437 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
438 if (zclient
->mi_redist
[afi
][i
].enabled
) {
439 struct listnode
*node
;
442 for (ALL_LIST_ELEMENTS_RO(
443 zclient
->mi_redist
[afi
][i
]
446 if (!(i
== zclient
->redist_default
447 && *id
== zclient
->instance
))
448 zebra_redistribute_send(
449 ZEBRA_REDISTRIBUTE_ADD
,
455 /* Flush all redistribute request. */
456 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
457 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
458 if (i
!= zclient
->redist_default
459 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
461 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
465 /* If default information is needed. */
466 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
467 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
,
471 /* Send unregister requests to zebra daemon for the information in a VRF. */
472 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
477 /* zclient is disabled. */
478 if (!zclient
->enable
)
481 /* If not connected to the zebra yet. */
482 if (zclient
->sock
< 0)
486 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
489 /* We need router-id information. */
490 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
492 /* We need interface information. */
493 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
495 /* Set unwanted redistribute route. */
496 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
497 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
500 /* Flush all redistribute request. */
501 if (vrf_id
== VRF_DEFAULT
)
502 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
503 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
504 if (zclient
->mi_redist
[afi
][i
].enabled
) {
505 struct listnode
*node
;
508 for (ALL_LIST_ELEMENTS_RO(
509 zclient
->mi_redist
[afi
][i
]
512 if (!(i
== zclient
->redist_default
513 && *id
== zclient
->instance
))
514 zebra_redistribute_send(
515 ZEBRA_REDISTRIBUTE_DELETE
,
521 /* Flush all redistribute request. */
522 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
523 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
524 if (i
!= zclient
->redist_default
525 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
527 zebra_redistribute_send(
528 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
531 /* If default information is needed. */
532 if (vrf_bitmap_check(zclient
->default_information
, VRF_DEFAULT
))
533 zebra_message_send(zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
,
537 /* Send request to zebra daemon to start or stop RA. */
538 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
539 struct interface
*ifp
, int enable
,
544 /* zclient is disabled. */
545 if (!zclient
->enable
)
548 /* If not connected to the zebra yet. */
549 if (zclient
->sock
< 0)
552 /* Form and send message. */
557 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
559 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
561 stream_putl(s
, ifp
->ifindex
);
562 stream_putl(s
, ra_interval
);
564 stream_putw_at(s
, 0, stream_get_endp(s
));
566 zclient_send_message(zclient
);
569 /* Make connection to zebra daemon. */
570 int zclient_start(struct zclient
*zclient
)
573 zlog_info("zclient_start is called");
575 /* zclient is disabled. */
576 if (!zclient
->enable
)
579 /* If already connected to the zebra. */
580 if (zclient
->sock
>= 0)
583 /* Check connect thread. */
584 if (zclient
->t_connect
)
587 if (zclient_socket_connect(zclient
) < 0) {
589 zlog_debug("zclient connection fail");
591 zclient_event(ZCLIENT_CONNECT
, zclient
);
595 if (set_nonblocking(zclient
->sock
) < 0)
596 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
599 /* Clear fail count. */
602 zlog_debug("zclient connect success with socket [%d]",
605 /* Create read thread. */
606 zclient_event(ZCLIENT_READ
, zclient
);
608 zebra_hello_send(zclient
);
610 /* Inform the successful connection. */
611 if (zclient
->zebra_connected
)
612 (*zclient
->zebra_connected
)(zclient
);
617 /* Initialize zebra client. Argument redist_default is unwanted
618 redistribute route type. */
619 void zclient_init(struct zclient
*zclient
, int redist_default
, u_short instance
)
623 /* Enable zebra client connection by default. */
626 /* Set -1 to the default socket value. */
629 /* Clear redistribution flags. */
630 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
631 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
632 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
634 /* Set unwanted redistribute route. bgpd does not need BGP route
636 zclient
->redist_default
= redist_default
;
637 zclient
->instance
= instance
;
638 /* Pending: make afi(s) an arg. */
639 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
640 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
643 /* Set default-information redistribute to zero. */
644 zclient
->default_information
= vrf_bitmap_init();
648 zlog_debug("zclient_start is called");
650 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
653 /* This function is a wrapper function for calling zclient_start from
654 timer or event thread. */
655 static int zclient_connect(struct thread
*t
)
657 struct zclient
*zclient
;
659 zclient
= THREAD_ARG(t
);
660 zclient
->t_connect
= NULL
;
663 zlog_debug("zclient_connect is called");
665 return zclient_start(zclient
);
669 * "xdr_encode"-like interface that allows daemon (client) to send
670 * a message to zebra server for a route that needs to be
671 * added/deleted to the kernel. Info about the route is specified
672 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
673 * the info down the zclient socket using the stream_* functions.
675 * The corresponding read ("xdr_decode") function on the server
676 * side is zread_ipv4_add()/zread_ipv4_delete().
678 * 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
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * | Length (2) | Command | Route Type |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 * | ZEBRA Flags | Message Flags | Prefix length |
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * | Destination IPv4 Prefix for route |
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
691 * described, as per the Nexthop count. Each nexthop described as:
694 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
695 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
696 * | IPv4 Nexthop address or Interface Index number |
697 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
700 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
701 * nexthop information is provided, and the message describes a prefix
702 * to blackhole or reject route.
704 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
705 * infrastructure was built around the traditional (32-bit "gate OR
706 * ifindex") nexthop data unit. A special encoding can be used to feed
707 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
708 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
710 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
711 * - .nexthop_num == .ifindex_num
712 * - .nexthop and .ifindex are filled with gate and ifindex parts of
713 * each compound nexthop, both in the same order
715 * zapi_ipv4_route() will produce two nexthop data units for each such
716 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
717 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
719 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
722 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
725 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
727 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
729 * XXX: No attention paid to alignment.
731 int zapi_ipv4_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
732 struct zapi_ipv4
*api
)
742 /* Some checks for labeled-unicast. The current expectation is that each
743 * nexthop is accompanied by a label in the case of labeled-unicast.
745 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
746 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
747 /* We expect prefixes installed with labels and the number to
749 * the number of nexthops.
751 assert(api
->label_num
== api
->nexthop_num
);
754 zclient_create_header(s
, cmd
, api
->vrf_id
);
756 /* Put type and nexthop. */
757 stream_putc(s
, api
->type
);
758 stream_putw(s
, api
->instance
);
759 stream_putl(s
, api
->flags
);
760 stream_putc(s
, api
->message
);
761 stream_putw(s
, api
->safi
);
763 /* Put prefix information. */
764 psize
= PSIZE(p
->prefixlen
);
765 stream_putc(s
, p
->prefixlen
);
766 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
768 /* Nexthop, ifindex, distance and metric information. */
769 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
770 /* traditional 32-bit data units */
771 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
773 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
774 /* XXX assert(api->nexthop_num == 0); */
775 /* XXX assert(api->ifindex_num == 0); */
777 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
779 for (i
= 0; i
< api
->nexthop_num
; i
++) {
780 stream_putc(s
, NEXTHOP_TYPE_IPV4
);
781 stream_put_in_addr(s
, api
->nexthop
[i
]);
782 /* For labeled-unicast, each nexthop is followed by
784 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
785 stream_putl(s
, api
->label
[i
]);
787 for (i
= 0; i
< api
->ifindex_num
; i
++) {
788 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
789 stream_putl(s
, api
->ifindex
[i
]);
793 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
794 stream_putc(s
, api
->distance
);
795 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
796 stream_putl(s
, api
->metric
);
797 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
798 stream_putl(s
, api
->tag
);
799 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
800 stream_putl(s
, api
->mtu
);
802 /* Put length at the first point of the stream. */
803 stream_putw_at(s
, 0, stream_get_endp(s
));
805 return zclient_send_message(zclient
);
808 int zapi_ipv4_route_ipv6_nexthop(u_char cmd
, struct zclient
*zclient
,
809 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
819 /* Some checks for labeled-unicast. The current expectation is that each
820 * nexthop is accompanied by a label in the case of labeled-unicast.
822 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
823 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
824 /* We expect prefixes installed with labels and the number to
826 * the number of nexthops.
828 assert(api
->label_num
== api
->nexthop_num
);
831 zclient_create_header(s
, cmd
, api
->vrf_id
);
833 /* Put type and nexthop. */
834 stream_putc(s
, api
->type
);
835 stream_putw(s
, api
->instance
);
836 stream_putl(s
, api
->flags
);
837 stream_putc(s
, api
->message
);
838 stream_putw(s
, api
->safi
);
840 /* Put prefix information. */
841 psize
= PSIZE(p
->prefixlen
);
842 stream_putc(s
, p
->prefixlen
);
843 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
845 /* Nexthop, ifindex, distance and metric information. */
846 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
847 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
849 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
850 /* XXX assert(api->nexthop_num == 0); */
851 /* XXX assert(api->ifindex_num == 0); */
853 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
855 for (i
= 0; i
< api
->nexthop_num
; i
++) {
856 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
857 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
858 /* For labeled-unicast, each nexthop is followed by
860 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
861 stream_putl(s
, api
->label
[i
]);
863 for (i
= 0; i
< api
->ifindex_num
; i
++) {
864 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
865 stream_putl(s
, api
->ifindex
[i
]);
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
870 stream_putc(s
, api
->distance
);
871 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
872 stream_putl(s
, api
->metric
);
873 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
874 stream_putl(s
, api
->tag
);
875 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
876 stream_putl(s
, api
->mtu
);
878 /* Put length at the first point of the stream. */
879 stream_putw_at(s
, 0, stream_get_endp(s
));
881 return zclient_send_message(zclient
);
884 int zapi_ipv6_route(u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
885 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
891 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
893 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
899 /* Some checks for labeled-unicast. The current expectation is that each
900 * nexthop is accompanied by a label in the case of labeled-unicast.
902 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)
903 && CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
904 /* We expect prefixes installed with labels and the number to
906 * the number of nexthops.
908 assert(api
->label_num
== api
->nexthop_num
);
911 zclient_create_header(s
, cmd
, api
->vrf_id
);
913 /* Put type and nexthop. */
914 stream_putc(s
, api
->type
);
915 stream_putw(s
, api
->instance
);
916 stream_putl(s
, api
->flags
);
917 stream_putc(s
, api
->message
);
918 stream_putw(s
, api
->safi
);
920 /* Put prefix information. */
921 psize
= PSIZE(p
->prefixlen
);
922 stream_putc(s
, p
->prefixlen
);
923 stream_write(s
, (u_char
*)&p
->prefix
, psize
);
925 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
926 psize
= PSIZE(src_p
->prefixlen
);
927 stream_putc(s
, src_p
->prefixlen
);
928 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
931 /* Nexthop, ifindex, distance and metric information. */
932 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
933 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_BLACKHOLE
)) {
935 stream_putc(s
, NEXTHOP_TYPE_BLACKHOLE
);
936 /* XXX assert(api->nexthop_num == 0); */
937 /* XXX assert(api->ifindex_num == 0); */
939 stream_putc(s
, api
->nexthop_num
+ api
->ifindex_num
);
941 for (i
= 0; i
< api
->nexthop_num
; i
++) {
942 stream_putc(s
, NEXTHOP_TYPE_IPV6
);
943 stream_write(s
, (u_char
*)api
->nexthop
[i
], 16);
944 /* For labeled-unicast, each nexthop is followed by
946 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
))
947 stream_putl(s
, api
->label
[i
]);
949 for (i
= 0; i
< api
->ifindex_num
; i
++) {
950 stream_putc(s
, NEXTHOP_TYPE_IFINDEX
);
951 stream_putl(s
, api
->ifindex
[i
]);
955 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
956 stream_putc(s
, api
->distance
);
957 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
958 stream_putl(s
, api
->metric
);
959 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
960 stream_putl(s
, api
->tag
);
961 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
962 stream_putl(s
, api
->mtu
);
964 /* Put length at the first point of the stream. */
965 stream_putw_at(s
, 0, stream_get_endp(s
));
967 return zclient_send_message(zclient
);
970 int zapi_route(u_char cmd
, struct zclient
*zclient
, struct prefix
*p
,
971 struct prefix_ipv6
*src_p
, struct zapi_route
*api
)
977 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL
979 assert(!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
985 zclient_create_header(s
, cmd
, api
->vrf_id
);
987 /* Put type and nexthop. */
988 stream_putc(s
, api
->type
);
989 stream_putw(s
, api
->instance
);
990 stream_putl(s
, api
->flags
);
991 stream_putc(s
, api
->message
);
992 stream_putw(s
, api
->safi
);
994 /* Put prefix information. */
995 psize
= PSIZE(p
->prefixlen
);
996 stream_putc(s
, p
->prefixlen
);
997 stream_write(s
, (u_char
*)&p
->u
.prefix
, psize
);
999 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1000 psize
= PSIZE(src_p
->prefixlen
);
1001 stream_putc(s
, src_p
->prefixlen
);
1002 stream_write(s
, (u_char
*)&src_p
->prefix
, psize
);
1005 /* Nexthop, ifindex, distance and metric information. */
1006 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1007 stream_putc(s
, api
->nexthop_num
);
1009 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1010 stream_putc(s
, api
->nexthop
[i
]->type
);
1011 switch (api
->nexthop
[i
]->type
) {
1012 case NEXTHOP_TYPE_BLACKHOLE
:
1014 case NEXTHOP_TYPE_IPV4
:
1015 stream_put_in_addr(s
,
1016 &api
->nexthop
[i
]->gate
.ipv4
);
1018 /* For labeled-unicast, each nexthop is followed
1020 if (CHECK_FLAG(api
->message
,
1021 ZAPI_MESSAGE_LABEL
))
1025 ->nh_label
->label
[0]);
1027 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1028 stream_put_in_addr(s
,
1029 &api
->nexthop
[i
]->gate
.ipv4
);
1030 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
1032 case NEXTHOP_TYPE_IFINDEX
:
1033 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
1035 case NEXTHOP_TYPE_IPV6
:
1038 (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
,
1041 /* For labeled-unicast, each nexthop is followed
1043 if (CHECK_FLAG(api
->message
,
1044 ZAPI_MESSAGE_LABEL
))
1048 ->nh_label
->label
[0]);
1050 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1053 (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
,
1055 stream_putl(s
, api
->nexthop
[i
]->ifindex
);
1061 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1062 stream_putc(s
, api
->distance
);
1063 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1064 stream_putl(s
, api
->metric
);
1065 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1066 stream_putl(s
, api
->tag
);
1067 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1068 stream_putl(s
, api
->mtu
);
1070 /* Put length at the first point of the stream. */
1071 stream_putw_at(s
, 0, stream_get_endp(s
));
1073 return zclient_send_message(zclient
);
1077 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1078 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1079 * then set/unset redist[type] in the client handle (a struct zserv) for the
1082 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1083 int type
, u_short instance
, vrf_id_t vrf_id
)
1090 zclient_create_header(s
, command
, vrf_id
);
1091 stream_putc(s
, afi
);
1092 stream_putc(s
, type
);
1093 stream_putw(s
, instance
);
1095 stream_putw_at(s
, 0, stream_get_endp(s
));
1097 return zclient_send_message(zclient
);
1100 /* Get prefix in ZServ format; family should be filled in on prefix */
1101 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1103 size_t plen
= prefix_blen(p
);
1110 stream_get(&p
->u
.prefix
, s
, plen
);
1112 p
->prefixlen
= MIN(plen
* 8, c
);
1115 /* Router-id update from zebra daemon. */
1116 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1118 /* Fetch interface address. */
1119 rid
->family
= stream_getc(s
);
1121 zclient_stream_get_prefix(s
, rid
);
1124 /* Interface addition from zebra daemon. */
1126 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1127 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1129 * 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
1130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1147 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1151 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1154 * | Link Layer Type |
1155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1156 * | Harware Address Length |
1157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1158 * | Hardware Address if HW lenght different from 0 |
1159 * | ... max INTERFACE_HWADDR_MAX |
1160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1161 * | Link_params? | Whether a link-params follows: 1 or 0.
1162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1163 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1164 * | .... (struct if_link_params). |
1165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1168 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1171 char vrfname_tmp
[VRF_NAMSIZ
];
1172 struct vrf_data data
;
1174 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1175 /* Read interface name. */
1176 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1178 /* Lookup/create vrf by vrf_id. */
1179 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1185 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1189 /* Lookup vrf by vrf_id. */
1190 vrf
= vrf_lookup_by_id(vrf_id
);
1193 * If a routing protocol doesn't know about a
1194 * vrf that is about to be deleted. There is
1195 * no point in attempting to delete it.
1203 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1205 struct interface
*ifp
;
1206 char ifname_tmp
[INTERFACE_NAMSIZ
];
1208 /* Read interface name. */
1209 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1211 /* Lookup/create interface by name. */
1212 ifp
= if_get_by_name_len(
1213 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
, 0);
1215 zebra_interface_if_set_value(s
, ifp
);
1221 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1222 * from zebra server. The format of this message is the same as
1223 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1224 * comments for zebra_interface_add_read), except that no sockaddr_dl
1225 * is sent at the tail of the message.
1227 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1229 struct interface
*ifp
;
1230 char ifname_tmp
[INTERFACE_NAMSIZ
];
1232 /* Read interface name. */
1233 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1235 /* Lookup this by interface index. */
1236 ifp
= if_lookup_by_name_len(
1237 ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
), vrf_id
);
1239 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1240 ifname_tmp
, vrf_id
);
1244 zebra_interface_if_set_value(s
, ifp
);
1249 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1255 iflp
->lp_status
= stream_getl(s
);
1256 iflp
->te_metric
= stream_getl(s
);
1257 iflp
->max_bw
= stream_getf(s
);
1258 iflp
->max_rsv_bw
= stream_getf(s
);
1259 uint32_t bwclassnum
= stream_getl(s
);
1262 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1263 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1266 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1267 " - outdated library?",
1268 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1270 iflp
->admin_grp
= stream_getl(s
);
1271 iflp
->rmt_as
= stream_getl(s
);
1272 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1274 iflp
->av_delay
= stream_getl(s
);
1275 iflp
->min_delay
= stream_getl(s
);
1276 iflp
->max_delay
= stream_getl(s
);
1277 iflp
->delay_var
= stream_getl(s
);
1279 iflp
->pkt_loss
= stream_getf(s
);
1280 iflp
->res_bw
= stream_getf(s
);
1281 iflp
->ava_bw
= stream_getf(s
);
1282 iflp
->use_bw
= stream_getf(s
);
1285 struct interface
*zebra_interface_link_params_read(struct stream
*s
)
1287 struct if_link_params
*iflp
;
1292 ifindex
= stream_getl(s
);
1294 struct interface
*ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
1297 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__
,
1302 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1305 link_params_set_value(s
, iflp
);
1310 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1312 u_char link_params_status
= 0;
1314 /* Read interface's index. */
1315 ifp
->ifindex
= stream_getl(s
);
1316 ifp
->status
= stream_getc(s
);
1318 /* Read interface's value. */
1319 ifp
->flags
= stream_getq(s
);
1320 ifp
->ptm_enable
= stream_getc(s
);
1321 ifp
->ptm_status
= stream_getc(s
);
1322 ifp
->metric
= stream_getl(s
);
1323 ifp
->speed
= stream_getl(s
);
1324 ifp
->mtu
= stream_getl(s
);
1325 ifp
->mtu6
= stream_getl(s
);
1326 ifp
->bandwidth
= stream_getl(s
);
1327 ifp
->ll_type
= stream_getl(s
);
1328 ifp
->hw_addr_len
= stream_getl(s
);
1329 if (ifp
->hw_addr_len
)
1330 stream_get(ifp
->hw_addr
, s
,
1331 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1333 /* Read Traffic Engineering status */
1334 link_params_status
= stream_getc(s
);
1335 /* Then, Traffic Engineering parameters if any */
1336 if (link_params_status
) {
1337 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1338 link_params_set_value(s
, iflp
);
1342 size_t zebra_interface_link_params_write(struct stream
*s
,
1343 struct interface
*ifp
)
1346 struct if_link_params
*iflp
;
1349 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1352 iflp
= ifp
->link_params
;
1355 w
+= stream_putl(s
, iflp
->lp_status
);
1357 w
+= stream_putl(s
, iflp
->te_metric
);
1358 w
+= stream_putf(s
, iflp
->max_bw
);
1359 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1361 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1362 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1363 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1365 w
+= stream_putl(s
, iflp
->admin_grp
);
1366 w
+= stream_putl(s
, iflp
->rmt_as
);
1367 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1369 w
+= stream_putl(s
, iflp
->av_delay
);
1370 w
+= stream_putl(s
, iflp
->min_delay
);
1371 w
+= stream_putl(s
, iflp
->max_delay
);
1372 w
+= stream_putl(s
, iflp
->delay_var
);
1374 w
+= stream_putf(s
, iflp
->pkt_loss
);
1375 w
+= stream_putf(s
, iflp
->res_bw
);
1376 w
+= stream_putf(s
, iflp
->ava_bw
);
1377 w
+= stream_putf(s
, iflp
->use_bw
);
1383 * format of message for address additon is:
1387 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1388 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1397 * | ifc_flags | flags for connected address
1405 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1413 static int memconstant(const void *s
, int c
, size_t n
)
1415 const u_char
*p
= s
;
1424 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1428 struct interface
*ifp
;
1429 struct connected
*ifc
;
1430 struct prefix p
, d
, *dp
;
1434 memset(&p
, 0, sizeof(p
));
1435 memset(&d
, 0, sizeof(d
));
1437 /* Get interface index. */
1438 ifindex
= stream_getl(s
);
1441 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1443 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1444 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1450 ifc_flags
= stream_getc(s
);
1452 /* Fetch interface address. */
1453 d
.family
= p
.family
= stream_getc(s
);
1454 plen
= prefix_blen(&d
);
1456 zclient_stream_get_prefix(s
, &p
);
1458 /* Fetch destination address. */
1459 stream_get(&d
.u
.prefix
, s
, plen
);
1461 /* N.B. NULL destination pointers are encoded as all zeroes */
1462 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1464 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1465 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1467 /* N.B. NULL destination pointers are encoded as all
1469 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1472 ifc
->flags
= ifc_flags
;
1473 if (ifc
->destination
)
1474 ifc
->destination
->prefixlen
=
1475 ifc
->address
->prefixlen
;
1476 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1477 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1479 char buf
[PREFIX_STRLEN
];
1481 "warning: interface %s address %s "
1482 "with peer flag set, but no peer address!",
1483 ifp
->name
, prefix2str(ifc
->address
, buf
,
1485 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1489 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1490 ifc
= connected_delete_by_prefix(ifp
, &p
);
1497 * format of message for neighbor connected address is:
1501 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1502 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1517 * | addr_len | len of addr.
1520 struct nbr_connected
*
1521 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1523 unsigned int ifindex
;
1524 struct interface
*ifp
;
1526 struct nbr_connected
*ifc
;
1528 /* Get interface index. */
1529 ifindex
= stream_getl(s
);
1532 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1534 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1535 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1541 p
.family
= stream_getc(s
);
1542 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1543 p
.prefixlen
= stream_getc(s
);
1545 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1546 /* Currently only supporting P2P links, so any new RA source
1548 considered as the replacement of the previously learnt
1549 Link-Local address. */
1550 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1551 ifc
= nbr_connected_new();
1552 ifc
->address
= prefix_new();
1554 listnode_add(ifp
->nbr_connected
, ifc
);
1557 prefix_copy(ifc
->address
, &p
);
1559 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1561 ifc
= nbr_connected_check(ifp
, &p
);
1563 listnode_delete(ifp
->nbr_connected
, ifc
);
1569 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1571 vrf_id_t
*new_vrf_id
)
1573 unsigned int ifindex
;
1574 struct interface
*ifp
;
1575 vrf_id_t new_id
= VRF_DEFAULT
;
1577 /* Get interface index. */
1578 ifindex
= stream_getl(s
);
1580 /* Lookup interface. */
1581 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1583 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1588 /* Fetch new VRF Id. */
1589 new_id
= stream_getw(s
);
1591 *new_vrf_id
= new_id
;
1595 /* filter unwanted messages until the expected one arrives */
1596 static int zclient_read_sync_response(struct zclient
*zclient
,
1597 u_int16_t expected_cmd
)
1609 cmd
= expected_cmd
+ 1;
1610 while (ret
== 0 && cmd
!= expected_cmd
) {
1614 /* wait until response arrives */
1616 FD_SET(zclient
->sock
, &readfds
);
1617 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1618 if (!FD_ISSET(zclient
->sock
, &readfds
))
1621 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1622 &version
, &vrf_id
, &cmd
);
1624 zlog_debug("%s: Response (%d bytes) received", __func__
,
1628 zlog_err("%s: Invalid Sync Message Reply", __func__
);
1635 * Connect to label manager in a syncronous way
1637 * It first writes the request to zcient output buffer and then
1638 * immediately reads the answer from the input buffer.
1640 * @param zclient Zclient used to connect to label manager (zebra)
1641 * @result Result of response
1643 int lm_label_manager_connect(struct zclient
*zclient
)
1650 zlog_debug("Connecting to Label Manager");
1652 if (zclient
->sock
< 0)
1658 zclient_create_header(s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1661 stream_putc(s
, zclient
->redist_default
);
1663 stream_putw(s
, zclient
->instance
);
1665 /* Put length at the first point of the stream. */
1666 stream_putw_at(s
, 0, stream_get_endp(s
));
1668 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1670 zlog_err("%s: can't write to zclient->sock", __func__
);
1671 close(zclient
->sock
);
1676 zlog_err("%s: zclient->sock connection closed", __func__
);
1677 close(zclient
->sock
);
1682 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1686 if (zclient_read_sync_response(zclient
, ZEBRA_LABEL_MANAGER_CONNECT
)
1692 result
= stream_getc(s
);
1695 "%s: Label Manager connect response received, result %u",
1702 * Function to request a label chunk in a syncronous way
1704 * It first writes the request to zlcient output buffer and then
1705 * immediately reads the answer from the input buffer.
1707 * @param zclient Zclient used to connect to label manager (zebra)
1708 * @param keep Avoid garbage collection
1709 * @param chunk_size Amount of labels requested
1710 * @param start To write first assigned chunk label to
1711 * @param end To write last assigned chunk label to
1712 * @result 0 on success, -1 otherwise
1714 int lm_get_label_chunk(struct zclient
*zclient
, u_char keep
,
1715 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
1719 u_char response_keep
;
1722 zlog_debug("Getting Label Chunk");
1724 if (zclient
->sock
< 0)
1730 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1732 stream_putc(s
, keep
);
1734 stream_putl(s
, chunk_size
);
1735 /* Put length at the first point of the stream. */
1736 stream_putw_at(s
, 0, stream_get_endp(s
));
1738 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1740 zlog_err("%s: can't write to zclient->sock", __func__
);
1741 close(zclient
->sock
);
1746 zlog_err("%s: zclient->sock connection closed", __func__
);
1747 close(zclient
->sock
);
1752 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__
,
1756 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1761 response_keep
= stream_getc(s
);
1762 /* start and end labels */
1763 *start
= stream_getl(s
);
1764 *end
= stream_getl(s
);
1766 /* not owning this response */
1767 if (keep
!= response_keep
) {
1769 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1770 __func__
, *start
, *end
, keep
, response_keep
);
1773 if (*start
> *end
|| *start
< MPLS_MIN_UNRESERVED_LABEL
1774 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1775 zlog_err("%s: Invalid Label chunk: %u - %u", __func__
, *start
,
1781 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start
, *end
,
1788 * Function to release a label chunk
1790 * @param zclient Zclient used to connect to label manager (zebra)
1791 * @param start First label of chunk
1792 * @param end Last label of chunk
1793 * @result 0 on success, -1 otherwise
1795 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
1802 zlog_debug("Releasing Label Chunk");
1804 if (zclient
->sock
< 0)
1810 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1813 stream_putl(s
, start
);
1815 stream_putl(s
, end
);
1817 /* Put length at the first point of the stream. */
1818 stream_putw_at(s
, 0, stream_get_endp(s
));
1820 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1822 zlog_err("%s: can't write to zclient->sock", __func__
);
1823 close(zclient
->sock
);
1828 zlog_err("%s: zclient->sock connection closed", __func__
);
1829 close(zclient
->sock
);
1837 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
1845 zclient_create_header(s
, command
, VRF_DEFAULT
);
1846 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
1847 stream_putl(s
, pw
->ifindex
);
1850 stream_putl(s
, pw
->type
);
1853 stream_putl(s
, pw
->af
);
1856 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
1859 stream_write(s
, (u_char
*)&pw
->nexthop
.ipv6
, 16);
1862 zlog_err("%s: unknown af", __func__
);
1867 stream_putl(s
, pw
->local_label
);
1868 stream_putl(s
, pw
->remote_label
);
1871 stream_putc(s
, pw
->flags
);
1873 /* Protocol specific fields */
1874 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
1876 /* Put length at the first point of the stream. */
1877 stream_putw_at(s
, 0, stream_get_endp(s
));
1879 return zclient_send_message(zclient
);
1883 * Receive PW status update from Zebra and send it to LDE process.
1885 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
1886 zebra_size_t length
, vrf_id_t vrf_id
,
1887 struct zapi_pw_status
*pw
)
1891 memset(pw
, 0, sizeof(struct zapi_pw_status
));
1895 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
1896 pw
->ifindex
= stream_getl(s
);
1897 pw
->status
= stream_getl(s
);
1900 /* Zebra client message read function. */
1901 static int zclient_read(struct thread
*thread
)
1904 uint16_t length
, command
;
1905 uint8_t marker
, version
;
1907 struct zclient
*zclient
;
1909 /* Get socket to zebra. */
1910 zclient
= THREAD_ARG(thread
);
1911 zclient
->t_read
= NULL
;
1913 /* Read zebra header (if we don't have it already). */
1914 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
1916 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1917 ZEBRA_HEADER_SIZE
- already
))
1922 "zclient connection closed socket [%d].",
1924 return zclient_failed(zclient
);
1926 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
1927 /* Try again later. */
1928 zclient_event(ZCLIENT_READ
, zclient
);
1931 already
= ZEBRA_HEADER_SIZE
;
1934 /* Reset to read from the beginning of the incoming packet. */
1935 stream_set_getp(zclient
->ibuf
, 0);
1937 /* Fetch header values. */
1938 length
= stream_getw(zclient
->ibuf
);
1939 marker
= stream_getc(zclient
->ibuf
);
1940 version
= stream_getc(zclient
->ibuf
);
1941 vrf_id
= stream_getw(zclient
->ibuf
);
1942 command
= stream_getw(zclient
->ibuf
);
1944 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
1946 "%s: socket %d version mismatch, marker %d, version %d",
1947 __func__
, zclient
->sock
, marker
, version
);
1948 return zclient_failed(zclient
);
1951 if (length
< ZEBRA_HEADER_SIZE
) {
1952 zlog_err("%s: socket %d message length %u is less than %d ",
1953 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1954 return zclient_failed(zclient
);
1958 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
1961 "%s: message size %u exceeds buffer size %lu, expanding...",
1962 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1963 ns
= stream_new(length
);
1964 stream_copy(ns
, zclient
->ibuf
);
1965 stream_free(zclient
->ibuf
);
1969 /* Read rest of zebra packet. */
1970 if (already
< length
) {
1972 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1978 "zclient connection closed socket [%d].",
1980 return zclient_failed(zclient
);
1982 if (nbyte
!= (ssize_t
)(length
- already
)) {
1983 /* Try again later. */
1984 zclient_event(ZCLIENT_READ
, zclient
);
1989 length
-= ZEBRA_HEADER_SIZE
;
1992 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
1993 (void *)zclient
, command
, vrf_id
);
1996 case ZEBRA_ROUTER_ID_UPDATE
:
1997 if (zclient
->router_id_update
)
1998 (*zclient
->router_id_update
)(command
, zclient
, length
,
2002 zclient_vrf_add(zclient
, vrf_id
);
2004 case ZEBRA_VRF_DELETE
:
2005 zclient_vrf_delete(zclient
, vrf_id
);
2007 case ZEBRA_INTERFACE_ADD
:
2008 if (zclient
->interface_add
)
2009 (*zclient
->interface_add
)(command
, zclient
, length
,
2012 case ZEBRA_INTERFACE_DELETE
:
2013 if (zclient
->interface_delete
)
2014 (*zclient
->interface_delete
)(command
, zclient
, length
,
2017 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2018 if (zclient
->interface_address_add
)
2019 (*zclient
->interface_address_add
)(command
, zclient
,
2022 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2023 if (zclient
->interface_address_delete
)
2024 (*zclient
->interface_address_delete
)(command
, zclient
,
2027 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2028 if (zclient
->interface_bfd_dest_update
)
2029 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2032 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2033 if (zclient
->interface_nbr_address_add
)
2034 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2037 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2038 if (zclient
->interface_nbr_address_delete
)
2039 (*zclient
->interface_nbr_address_delete
)(
2040 command
, zclient
, length
, vrf_id
);
2042 case ZEBRA_INTERFACE_UP
:
2043 if (zclient
->interface_up
)
2044 (*zclient
->interface_up
)(command
, zclient
, length
,
2047 case ZEBRA_INTERFACE_DOWN
:
2048 if (zclient
->interface_down
)
2049 (*zclient
->interface_down
)(command
, zclient
, length
,
2052 case ZEBRA_INTERFACE_VRF_UPDATE
:
2053 if (zclient
->interface_vrf_update
)
2054 (*zclient
->interface_vrf_update
)(command
, zclient
,
2057 case ZEBRA_NEXTHOP_UPDATE
:
2059 zlog_debug("zclient rcvd nexthop update\n");
2060 if (zclient
->nexthop_update
)
2061 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2064 case ZEBRA_IMPORT_CHECK_UPDATE
:
2066 zlog_debug("zclient rcvd import check update\n");
2067 if (zclient
->import_check_update
)
2068 (*zclient
->import_check_update
)(command
, zclient
,
2071 case ZEBRA_BFD_DEST_REPLAY
:
2072 if (zclient
->bfd_dest_replay
)
2073 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2076 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
2077 if (zclient
->redistribute_route_ipv4_add
)
2078 (*zclient
->redistribute_route_ipv4_add
)(
2079 command
, zclient
, length
, vrf_id
);
2081 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
2082 if (zclient
->redistribute_route_ipv4_del
)
2083 (*zclient
->redistribute_route_ipv4_del
)(
2084 command
, zclient
, length
, vrf_id
);
2086 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
2087 if (zclient
->redistribute_route_ipv6_add
)
2088 (*zclient
->redistribute_route_ipv6_add
)(
2089 command
, zclient
, length
, vrf_id
);
2091 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
2092 if (zclient
->redistribute_route_ipv6_del
)
2093 (*zclient
->redistribute_route_ipv6_del
)(
2094 command
, zclient
, length
, vrf_id
);
2096 case ZEBRA_INTERFACE_LINK_PARAMS
:
2097 if (zclient
->interface_link_params
)
2098 (*zclient
->interface_link_params
)(command
, zclient
,
2101 case ZEBRA_FEC_UPDATE
:
2103 zlog_debug("zclient rcvd fec update\n");
2104 if (zclient
->fec_update
)
2105 (*zclient
->fec_update
)(command
, zclient
, length
);
2108 if (zclient
->local_vni_add
)
2109 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2113 if (zclient
->local_vni_del
)
2114 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2117 case ZEBRA_MACIP_ADD
:
2118 if (zclient
->local_macip_add
)
2119 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2122 case ZEBRA_MACIP_DEL
:
2123 if (zclient
->local_macip_del
)
2124 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2127 case ZEBRA_PW_STATUS_UPDATE
:
2128 if (zclient
->pw_status_update
)
2129 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2136 if (zclient
->sock
< 0)
2137 /* Connection was closed during packet processing. */
2140 /* Register read thread. */
2141 stream_reset(zclient
->ibuf
);
2142 zclient_event(ZCLIENT_READ
, zclient
);
2147 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2148 int type
, u_short instance
, vrf_id_t vrf_id
)
2152 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2153 if (redist_check_instance(
2154 &zclient
->mi_redist
[afi
][type
], instance
))
2156 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2159 if (!redist_check_instance(
2160 &zclient
->mi_redist
[afi
][type
], instance
))
2162 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2167 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2168 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2171 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2173 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2176 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2180 if (zclient
->sock
> 0)
2181 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2186 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2190 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2191 if (vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2193 vrf_bitmap_set(zclient
->default_information
, vrf_id
);
2195 if (!vrf_bitmap_check(zclient
->default_information
, vrf_id
))
2197 vrf_bitmap_unset(zclient
->default_information
, vrf_id
);
2200 if (zclient
->sock
> 0)
2201 zebra_message_send(zclient
, command
, vrf_id
);
2204 static void zclient_event(enum event event
, struct zclient
*zclient
)
2207 case ZCLIENT_SCHEDULE
:
2208 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2209 &zclient
->t_connect
);
2211 case ZCLIENT_CONNECT
:
2214 "zclient connect failures: %d schedule interval is now %d",
2215 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2216 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2217 zclient
->fail
< 3 ? 10 : 60,
2218 &zclient
->t_connect
);
2221 zclient
->t_read
= NULL
;
2222 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2223 zclient
->sock
, &zclient
->t_read
);
2228 const char *zclient_serv_path_get()
2230 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
2233 void zclient_serv_path_set(char *path
)
2238 zclient_serv_path
= NULL
;
2240 /* test if `path' is socket. don't set it otherwise. */
2241 if (stat(path
, &sb
) == -1) {
2242 zlog_warn("%s: zebra socket `%s' does not exist", __func__
,
2247 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
) {
2248 zlog_warn("%s: `%s' is not unix socket, sir", __func__
, path
);
2252 /* it seems that path is unix socket */
2253 zclient_serv_path
= path
;
2256 void zclient_interface_set_master(struct zclient
*client
,
2257 struct interface
*master
,
2258 struct interface
*slave
)
2265 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2267 stream_putw(s
, master
->vrf_id
);
2268 stream_putl(s
, master
->ifindex
);
2269 stream_putw(s
, slave
->vrf_id
);
2270 stream_putl(s
, slave
->ifindex
);
2272 stream_putw_at(s
, 0, stream_get_endp(s
));
2273 zclient_send_message(client
);