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
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
39 /* Zebra client events. */
40 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
42 /* Prototype for event manager. */
43 static void zclient_event (enum event
, struct zclient
*);
45 const char *zclient_serv_path
= NULL
;
47 /* This file local debug flag. */
48 int zclient_debug
= 0;
50 /* Allocate zclient structure. */
52 zclient_new (struct thread_master
*master
)
54 struct zclient
*zclient
;
55 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
57 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
58 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
59 zclient
->wb
= buffer_new(0);
60 zclient
->master
= master
;
65 /* This function is only called when exiting, because
66 many parts of the code do not check for I/O errors, so they could
67 reference an invalid pointer if the structure was ever freed.
69 Free zclient structure. */
71 zclient_free (struct zclient
*zclient
)
74 stream_free(zclient
->ibuf
);
76 stream_free(zclient
->obuf
);
78 buffer_free(zclient
->wb
);
80 XFREE (MTYPE_ZCLIENT
, zclient
);
84 redist_check_instance (struct redist_proto
*red
, u_short instance
)
86 struct listnode
*node
;
92 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
100 redist_add_instance (struct redist_proto
*red
, u_short instance
)
107 red
->instances
= list_new();
109 in
= calloc (1, sizeof(u_short
));
111 listnode_add (red
->instances
, in
);
115 redist_del_instance (struct redist_proto
*red
, u_short instance
)
119 id
= redist_check_instance (red
, instance
);
123 listnode_delete(red
->instances
, id
);
125 if (!red
->instances
->count
)
128 list_free(red
->instances
);
129 red
->instances
= NULL
;
133 /* Stop zebra client services. */
135 zclient_stop (struct zclient
*zclient
)
141 zlog_debug ("zclient stopped");
144 THREAD_OFF(zclient
->t_read
);
145 THREAD_OFF(zclient
->t_connect
);
146 THREAD_OFF(zclient
->t_write
);
149 stream_reset(zclient
->ibuf
);
150 stream_reset(zclient
->obuf
);
152 /* Empty the write buffer. */
153 buffer_reset(zclient
->wb
);
156 if (zclient
->sock
>= 0)
158 close (zclient
->sock
);
163 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
165 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
167 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
168 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
170 redist_del_instance(&zclient
->mi_redist
[afi
][zclient
->redist_default
],
174 vrf_bitmap_free(zclient
->default_information
);
175 zclient
->default_information
= VRF_BITMAP_NULL
;
179 zclient_reset (struct zclient
*zclient
)
183 zclient_stop (zclient
);
185 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
186 redist_del_instance (&zclient
->mi_redist
[afi
][zclient
->redist_default
], zclient
->instance
);
188 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
191 #ifdef HAVE_TCP_ZEBRA
193 /* Make socket to zebra daemon. Return zebra socket. */
199 struct sockaddr_in serv
;
201 /* We should think about IPv6 connection. */
202 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
206 /* Make server socket. */
207 memset (&serv
, 0, sizeof (struct sockaddr_in
));
208 serv
.sin_family
= AF_INET
;
209 serv
.sin_port
= htons (ZEBRA_PORT
);
210 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
211 serv
.sin_len
= sizeof (struct sockaddr_in
);
212 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
213 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
215 /* Connect to zebra. */
216 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
220 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
221 errno
, safe_strerror (errno
));
230 /* For sockaddr_un. */
234 zclient_socket_un (const char *path
)
238 struct sockaddr_un addr
;
240 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
244 /* Make server socket. */
245 memset (&addr
, 0, sizeof (struct sockaddr_un
));
246 addr
.sun_family
= AF_UNIX
;
247 strncpy (addr
.sun_path
, path
, strlen (path
));
248 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
249 len
= addr
.sun_len
= SUN_LEN(&addr
);
251 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
252 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
254 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
258 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
259 errno
, safe_strerror (errno
));
266 #endif /* HAVE_TCP_ZEBRA */
269 * Connect to zebra daemon.
270 * @param zclient a pointer to zclient structure
271 * @return socket fd just to make sure that connection established
276 zclient_socket_connect (struct zclient
*zclient
)
278 #ifdef HAVE_TCP_ZEBRA
279 zclient
->sock
= zclient_socket ();
281 zclient
->sock
= zclient_socket_un (zclient_serv_path_get());
283 return zclient
->sock
;
287 zclient_failed(struct zclient
*zclient
)
290 zclient_stop(zclient
);
291 zclient_event(ZCLIENT_CONNECT
, zclient
);
296 zclient_flush_data(struct thread
*thread
)
298 struct zclient
*zclient
= THREAD_ARG(thread
);
300 zclient
->t_write
= NULL
;
301 if (zclient
->sock
< 0)
303 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
306 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
307 __func__
, zclient
->sock
);
308 return zclient_failed(zclient
);
311 zclient
->t_write
= thread_add_write(zclient
->master
, zclient_flush_data
,
312 zclient
, zclient
->sock
);
321 zclient_send_message(struct zclient
*zclient
)
323 if (zclient
->sock
< 0)
325 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
326 stream_get_endp(zclient
->obuf
)))
329 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
330 __func__
, zclient
->sock
);
331 return zclient_failed(zclient
);
334 THREAD_OFF(zclient
->t_write
);
337 THREAD_WRITE_ON(zclient
->master
, zclient
->t_write
,
338 zclient_flush_data
, zclient
, zclient
->sock
);
345 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
347 /* length placeholder, caller can update */
348 stream_putw (s
, ZEBRA_HEADER_SIZE
);
349 stream_putc (s
, ZEBRA_HEADER_MARKER
);
350 stream_putc (s
, ZSERV_VERSION
);
351 stream_putw (s
, vrf_id
);
352 stream_putw (s
, command
);
356 zclient_read_header (struct stream
*s
, int sock
, u_int16_t
*size
, u_char
*marker
,
357 u_char
*version
, vrf_id_t
*vrf_id
, u_int16_t
*cmd
)
359 if (stream_read (s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
362 *size
= stream_getw (s
) - ZEBRA_HEADER_SIZE
;
363 *marker
= stream_getc (s
);
364 *version
= stream_getc (s
);
365 *vrf_id
= stream_getw (s
);
366 *cmd
= stream_getw (s
);
368 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
)
370 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
371 __func__
, sock
, *marker
, *version
);
375 if (*size
&& stream_read (s
, sock
, *size
) != *size
)
381 /* Send simple Zebra message. */
383 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
387 /* Get zclient output buffer. */
391 /* Send very simple command only Zebra message. */
392 zclient_create_header (s
, command
, vrf_id
);
394 return zclient_send_message(zclient
);
398 zebra_hello_send (struct zclient
*zclient
)
402 if (zclient
->redist_default
)
407 /* The VRF ID in the HELLO message is always 0. */
408 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
409 stream_putc (s
, zclient
->redist_default
);
410 stream_putw (s
, zclient
->instance
);
411 stream_putw_at (s
, 0, stream_get_endp (s
));
412 return zclient_send_message(zclient
);
418 /* Send register requests to zebra daemon for the information in a VRF. */
420 zclient_send_reg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
425 /* zclient is disabled. */
426 if (! zclient
->enable
)
429 /* If not connected to the zebra yet. */
430 if (zclient
->sock
< 0)
434 zlog_debug ("%s: send register messages for VRF %u", __func__
, vrf_id
);
436 /* We need router-id information. */
437 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
439 /* We need interface information. */
440 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
442 /* Set unwanted redistribute route. */
443 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
444 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
446 /* Flush all redistribute request. */
447 if (vrf_id
== VRF_DEFAULT
)
448 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
449 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
450 if (zclient
->mi_redist
[afi
][i
].enabled
)
452 struct listnode
*node
;
455 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
456 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
457 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
461 /* Flush all redistribute request. */
462 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
463 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
464 if (i
!= zclient
->redist_default
&&
465 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
466 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
468 /* If default information is needed. */
469 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
470 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
473 /* Send unregister requests to zebra daemon for the information in a VRF. */
475 zclient_send_dereg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
480 /* zclient is disabled. */
481 if (! zclient
->enable
)
484 /* If not connected to the zebra yet. */
485 if (zclient
->sock
< 0)
489 zlog_debug ("%s: send deregister messages for VRF %u", __func__
, vrf_id
);
491 /* We need router-id information. */
492 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
494 /* We need interface information. */
495 zebra_message_send (zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
497 /* Set unwanted redistribute route. */
498 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
499 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
501 /* Flush all redistribute request. */
502 if (vrf_id
== VRF_DEFAULT
)
503 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
504 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
505 if (zclient
->mi_redist
[afi
][i
].enabled
)
507 struct listnode
*node
;
510 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
511 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
512 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
,
516 /* Flush all redistribute request. */
517 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
518 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
519 if (i
!= zclient
->redist_default
&&
520 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
521 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
, 0, vrf_id
);
523 /* If default information is needed. */
524 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
525 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, vrf_id
);
528 /* Send request to zebra daemon to start or stop RA. */
530 zclient_send_interface_radv_req (struct zclient
*zclient
, vrf_id_t vrf_id
,
531 struct interface
*ifp
, int enable
, int ra_interval
)
535 /* zclient is disabled. */
536 if (!zclient
->enable
)
539 /* If not connected to the zebra yet. */
540 if (zclient
->sock
< 0)
543 /* Form and send message. */
548 zclient_create_header (s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
550 zclient_create_header (s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
552 stream_putl (s
, ifp
->ifindex
);
553 stream_putl (s
, ra_interval
);
555 stream_putw_at (s
, 0, stream_get_endp (s
));
557 zclient_send_message(zclient
);
560 /* Make connection to zebra daemon. */
562 zclient_start (struct zclient
*zclient
)
565 zlog_info ("zclient_start is called");
567 /* zclient is disabled. */
568 if (! zclient
->enable
)
571 /* If already connected to the zebra. */
572 if (zclient
->sock
>= 0)
575 /* Check connect thread. */
576 if (zclient
->t_connect
)
579 if (zclient_socket_connect(zclient
) < 0)
582 zlog_debug ("zclient connection fail");
584 zclient_event (ZCLIENT_CONNECT
, zclient
);
588 if (set_nonblocking(zclient
->sock
) < 0)
589 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
591 /* Clear fail count. */
594 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
596 /* Create read thread. */
597 zclient_event (ZCLIENT_READ
, zclient
);
599 zebra_hello_send (zclient
);
601 /* Inform the successful connection. */
602 if (zclient
->zebra_connected
)
603 (*zclient
->zebra_connected
) (zclient
);
608 /* Initialize zebra client. Argument redist_default is unwanted
609 redistribute route type. */
611 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
615 /* Enable zebra client connection by default. */
618 /* Set -1 to the default socket value. */
621 /* Clear redistribution flags. */
622 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
623 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
624 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
626 /* Set unwanted redistribute route. bgpd does not need BGP route
628 zclient
->redist_default
= redist_default
;
629 zclient
->instance
= instance
;
630 /* Pending: make afi(s) an arg. */
631 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
632 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
634 /* Set default-information redistribute to zero. */
635 zclient
->default_information
= vrf_bitmap_init ();;
638 zlog_debug ("zclient_start is called");
640 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
643 /* This function is a wrapper function for calling zclient_start from
644 timer or event thread. */
646 zclient_connect (struct thread
*t
)
648 struct zclient
*zclient
;
650 zclient
= THREAD_ARG (t
);
651 zclient
->t_connect
= NULL
;
654 zlog_debug ("zclient_connect is called");
656 return zclient_start (zclient
);
660 * "xdr_encode"-like interface that allows daemon (client) to send
661 * a message to zebra server for a route that needs to be
662 * added/deleted to the kernel. Info about the route is specified
663 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
664 * the info down the zclient socket using the stream_* functions.
666 * The corresponding read ("xdr_decode") function on the server
667 * side is zread_ipv4_add()/zread_ipv4_delete().
669 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671 * | Length (2) | Command | Route Type |
672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 * | ZEBRA Flags | Message Flags | Prefix length |
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | Destination IPv4 Prefix for route |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
682 * described, as per the Nexthop count. Each nexthop described as:
685 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | IPv4 Nexthop address or Interface Index number |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
691 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
692 * nexthop information is provided, and the message describes a prefix
693 * to blackhole or reject route.
695 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
696 * infrastructure was built around the traditional (32-bit "gate OR
697 * ifindex") nexthop data unit. A special encoding can be used to feed
698 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
699 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
701 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
702 * - .nexthop_num == .ifindex_num
703 * - .nexthop and .ifindex are filled with gate and ifindex parts of
704 * each compound nexthop, both in the same order
706 * zapi_ipv4_route() will produce two nexthop data units for each such
707 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
708 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
710 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
713 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
716 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
718 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
720 * XXX: No attention paid to alignment.
723 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
724 struct zapi_ipv4
*api
)
734 zclient_create_header (s
, cmd
, api
->vrf_id
);
736 /* Put type and nexthop. */
737 stream_putc (s
, api
->type
);
738 stream_putw (s
, api
->instance
);
739 stream_putl (s
, api
->flags
);
740 stream_putc (s
, api
->message
);
741 stream_putw (s
, api
->safi
);
743 /* Put prefix information. */
744 psize
= PSIZE (p
->prefixlen
);
745 stream_putc (s
, p
->prefixlen
);
746 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
748 /* Nexthop, ifindex, distance and metric information. */
749 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
751 /* traditional 32-bit data units */
752 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
755 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
756 /* XXX assert(api->nexthop_num == 0); */
757 /* XXX assert(api->ifindex_num == 0); */
760 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
762 for (i
= 0; i
< api
->nexthop_num
; i
++)
764 stream_putc (s
, NEXTHOP_TYPE_IPV4
);
765 stream_put_in_addr (s
, api
->nexthop
[i
]);
767 for (i
= 0; i
< api
->ifindex_num
; i
++)
769 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
770 stream_putl (s
, api
->ifindex
[i
]);
774 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
775 stream_putc (s
, api
->distance
);
776 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
777 stream_putl (s
, api
->metric
);
778 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
779 stream_putl (s
, api
->tag
);
780 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
781 stream_putl (s
, api
->mtu
);
783 /* Put length at the first point of the stream. */
784 stream_putw_at (s
, 0, stream_get_endp (s
));
786 return zclient_send_message(zclient
);
790 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
791 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
801 zclient_create_header (s
, cmd
, api
->vrf_id
);
803 /* Put type and nexthop. */
804 stream_putc (s
, api
->type
);
805 stream_putw (s
, api
->instance
);
806 stream_putl (s
, api
->flags
);
807 stream_putc (s
, api
->message
);
808 stream_putw (s
, api
->safi
);
810 /* Put prefix information. */
811 psize
= PSIZE (p
->prefixlen
);
812 stream_putc (s
, p
->prefixlen
);
813 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
815 /* Nexthop, ifindex, distance and metric information. */
816 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
818 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
821 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
822 /* XXX assert(api->nexthop_num == 0); */
823 /* XXX assert(api->ifindex_num == 0); */
826 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
828 for (i
= 0; i
< api
->nexthop_num
; i
++)
830 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
831 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
833 for (i
= 0; i
< api
->ifindex_num
; i
++)
835 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
836 stream_putl (s
, api
->ifindex
[i
]);
840 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
841 stream_putc (s
, api
->distance
);
842 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
843 stream_putl (s
, api
->metric
);
844 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
845 stream_putl (s
, api
->tag
);
846 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
847 stream_putl (s
, api
->mtu
);
849 /* Put length at the first point of the stream. */
850 stream_putw_at (s
, 0, stream_get_endp (s
));
852 return zclient_send_message(zclient
);
856 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
857 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
863 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
864 assert (!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
870 zclient_create_header (s
, cmd
, api
->vrf_id
);
872 /* Put type and nexthop. */
873 stream_putc (s
, api
->type
);
874 stream_putw (s
, api
->instance
);
875 stream_putl (s
, api
->flags
);
876 stream_putc (s
, api
->message
);
877 stream_putw (s
, api
->safi
);
879 /* Put prefix information. */
880 psize
= PSIZE (p
->prefixlen
);
881 stream_putc (s
, p
->prefixlen
);
882 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
884 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_SRCPFX
))
886 psize
= PSIZE (src_p
->prefixlen
);
887 stream_putc (s
, src_p
->prefixlen
);
888 stream_write (s
, (u_char
*)&src_p
->prefix
, psize
);
891 /* Nexthop, ifindex, distance and metric information. */
892 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
894 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
897 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
898 /* XXX assert(api->nexthop_num == 0); */
899 /* XXX assert(api->ifindex_num == 0); */
902 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
904 for (i
= 0; i
< api
->nexthop_num
; i
++)
906 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
907 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
909 for (i
= 0; i
< api
->ifindex_num
; i
++)
911 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
912 stream_putl (s
, api
->ifindex
[i
]);
916 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
917 stream_putc (s
, api
->distance
);
918 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
919 stream_putl (s
, api
->metric
);
920 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
921 stream_putl (s
, api
->tag
);
922 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
923 stream_putl (s
, api
->mtu
);
925 /* Put length at the first point of the stream. */
926 stream_putw_at (s
, 0, stream_get_endp (s
));
928 return zclient_send_message(zclient
);
932 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
933 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
934 * then set/unset redist[type] in the client handle (a struct zserv) for the
938 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
939 u_short instance
, vrf_id_t vrf_id
)
946 zclient_create_header (s
, command
, vrf_id
);
947 stream_putc (s
, afi
);
948 stream_putc (s
, type
);
949 stream_putw (s
, instance
);
951 stream_putw_at (s
, 0, stream_get_endp (s
));
953 return zclient_send_message(zclient
);
956 /* Get prefix in ZServ format; family should be filled in on prefix */
958 zclient_stream_get_prefix (struct stream
*s
, struct prefix
*p
)
960 size_t plen
= prefix_blen (p
);
967 stream_get (&p
->u
.prefix
, s
, plen
);
969 p
->prefixlen
= MIN(plen
* 8, c
);
972 /* Router-id update from zebra daemon. */
974 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
976 /* Fetch interface address. */
977 rid
->family
= stream_getc (s
);
979 zclient_stream_get_prefix (s
, rid
);
982 /* Interface addition from zebra daemon. */
984 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
985 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
987 * 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
988 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
994 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
996 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
998 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1001 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1003 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1005 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1007 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1009 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1010 * | Link Layer Type |
1011 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1012 * | Harware Address Length |
1013 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1014 * | Hardware Address if HW lenght different from 0 |
1015 * | ... max INTERFACE_HWADDR_MAX |
1016 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1017 * | Link_params? | Whether a link-params follows: 1 or 0.
1018 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1019 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1020 * | .... (struct if_link_params). |
1021 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1025 zclient_vrf_add (struct zclient
*zclient
, vrf_id_t vrf_id
)
1028 char vrfname_tmp
[VRF_NAMSIZ
];
1030 /* Read interface name. */
1031 stream_get (vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1033 /* Lookup/create vrf by vrf_id. */
1034 vrf
= vrf_get (vrf_id
, vrfname_tmp
);
1040 zclient_vrf_delete (struct zclient
*zclient
, vrf_id_t vrf_id
)
1044 /* Lookup vrf by vrf_id. */
1045 vrf
= vrf_lookup_by_id (vrf_id
);
1048 * If a routing protocol doesn't know about a
1049 * vrf that is about to be deleted. There is
1050 * no point in attempting to delete it.
1059 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
1061 struct interface
*ifp
;
1062 char ifname_tmp
[INTERFACE_NAMSIZ
];
1064 /* Read interface name. */
1065 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1067 /* Lookup/create interface by name. */
1068 ifp
= if_get_by_name_len_vrf (ifname_tmp
,
1069 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1072 zebra_interface_if_set_value (s
, ifp
);
1078 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1079 * from zebra server. The format of this message is the same as
1080 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1081 * comments for zebra_interface_add_read), except that no sockaddr_dl
1082 * is sent at the tail of the message.
1085 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
1087 struct interface
*ifp
;
1088 char ifname_tmp
[INTERFACE_NAMSIZ
];
1090 /* Read interface name. */
1091 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1093 /* Lookup this by interface index. */
1094 ifp
= if_lookup_by_name_len_vrf (ifname_tmp
,
1095 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1099 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1100 ifname_tmp
, vrf_id
);
1104 zebra_interface_if_set_value (s
, ifp
);
1110 link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1116 iflp
->lp_status
= stream_getl (s
);
1117 iflp
->te_metric
= stream_getl (s
);
1118 iflp
->max_bw
= stream_getf (s
);
1119 iflp
->max_rsv_bw
= stream_getf (s
);
1120 uint32_t bwclassnum
= stream_getl (s
);
1123 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1124 iflp
->unrsv_bw
[i
] = stream_getf (s
);
1126 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1127 " - outdated library?",
1128 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1130 iflp
->admin_grp
= stream_getl (s
);
1131 iflp
->rmt_as
= stream_getl (s
);
1132 iflp
->rmt_ip
.s_addr
= stream_get_ipv4 (s
);
1134 iflp
->av_delay
= stream_getl (s
);
1135 iflp
->min_delay
= stream_getl (s
);
1136 iflp
->max_delay
= stream_getl (s
);
1137 iflp
->delay_var
= stream_getl (s
);
1139 iflp
->pkt_loss
= stream_getf (s
);
1140 iflp
->res_bw
= stream_getf (s
);
1141 iflp
->ava_bw
= stream_getf (s
);
1142 iflp
->use_bw
= stream_getf (s
);
1146 zebra_interface_link_params_read (struct stream
*s
)
1148 struct if_link_params
*iflp
;
1149 uint32_t ifindex
= stream_getl (s
);
1151 struct interface
*ifp
= if_lookup_by_index (ifindex
);
1153 if (ifp
== NULL
|| s
== NULL
)
1155 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1160 if ((iflp
= if_link_params_get (ifp
)) == NULL
)
1163 link_params_set_value(s
, iflp
);
1169 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
1171 u_char link_params_status
= 0;
1173 /* Read interface's index. */
1174 ifp
->ifindex
= stream_getl (s
);
1175 ifp
->status
= stream_getc (s
);
1177 /* Read interface's value. */
1178 ifp
->flags
= stream_getq (s
);
1179 ifp
->ptm_enable
= stream_getc (s
);
1180 ifp
->ptm_status
= stream_getc (s
);
1181 ifp
->metric
= stream_getl (s
);
1182 ifp
->mtu
= stream_getl (s
);
1183 ifp
->mtu6
= stream_getl (s
);
1184 ifp
->bandwidth
= stream_getl (s
);
1185 ifp
->ll_type
= stream_getl (s
);
1186 ifp
->hw_addr_len
= stream_getl (s
);
1187 if (ifp
->hw_addr_len
)
1188 stream_get (ifp
->hw_addr
, s
, MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1190 /* Read Traffic Engineering status */
1191 link_params_status
= stream_getc (s
);
1192 /* Then, Traffic Engineering parameters if any */
1193 if (link_params_status
)
1195 struct if_link_params
*iflp
= if_link_params_get (ifp
);
1196 link_params_set_value(s
, iflp
);
1201 zebra_interface_link_params_write (struct stream
*s
, struct interface
*ifp
)
1204 struct if_link_params
*iflp
;
1207 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1210 iflp
= ifp
->link_params
;
1213 w
+= stream_putl (s
, iflp
->lp_status
);
1215 w
+= stream_putl (s
, iflp
->te_metric
);
1216 w
+= stream_putf (s
, iflp
->max_bw
);
1217 w
+= stream_putf (s
, iflp
->max_rsv_bw
);
1219 w
+= stream_putl (s
, MAX_CLASS_TYPE
);
1220 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1221 w
+= stream_putf (s
, iflp
->unrsv_bw
[i
]);
1223 w
+= stream_putl (s
, iflp
->admin_grp
);
1224 w
+= stream_putl (s
, iflp
->rmt_as
);
1225 w
+= stream_put_in_addr (s
, &iflp
->rmt_ip
);
1227 w
+= stream_putl (s
, iflp
->av_delay
);
1228 w
+= stream_putl (s
, iflp
->min_delay
);
1229 w
+= stream_putl (s
, iflp
->max_delay
);
1230 w
+= stream_putl (s
, iflp
->delay_var
);
1232 w
+= stream_putf (s
, iflp
->pkt_loss
);
1233 w
+= stream_putf (s
, iflp
->res_bw
);
1234 w
+= stream_putf (s
, iflp
->ava_bw
);
1235 w
+= stream_putf (s
, iflp
->use_bw
);
1241 * format of message for address additon is:
1245 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1246 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1255 * | ifc_flags | flags for connected address
1263 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1272 memconstant(const void *s
, int c
, size_t n
)
1274 const u_char
*p
= s
;
1284 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1287 struct interface
*ifp
;
1288 struct connected
*ifc
;
1289 struct prefix p
, d
, *dp
;
1293 memset (&p
, 0, sizeof(p
));
1294 memset (&d
, 0, sizeof(d
));
1296 /* Get interface index. */
1297 ifindex
= stream_getl (s
);
1300 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1303 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1304 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1310 ifc_flags
= stream_getc (s
);
1312 /* Fetch interface address. */
1313 d
.family
= p
.family
= stream_getc (s
);
1314 plen
= prefix_blen (&d
);
1316 zclient_stream_get_prefix (s
, &p
);
1318 /* Fetch destination address. */
1319 stream_get (&d
.u
.prefix
, s
, plen
);
1321 /* N.B. NULL destination pointers are encoded as all zeroes */
1322 dp
= memconstant(&d
.u
.prefix
,0,plen
) ? NULL
: &d
;
1324 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1326 ifc
= connected_lookup_prefix_exact (ifp
, &p
);
1329 /* N.B. NULL destination pointers are encoded as all zeroes */
1330 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1334 ifc
->flags
= ifc_flags
;
1335 if (ifc
->destination
)
1336 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1337 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1339 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1340 char buf
[PREFIX_STRLEN
];
1341 zlog_warn("warning: interface %s address %s "
1342 "with peer flag set, but no peer address!",
1344 prefix2str (ifc
->address
, buf
, sizeof buf
));
1345 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1351 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1352 ifc
= connected_delete_by_prefix(ifp
, &p
);
1359 * format of message for neighbor connected address is:
1363 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1364 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1379 * | addr_len | len of addr.
1382 struct nbr_connected
*
1383 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1385 unsigned int ifindex
;
1386 struct interface
*ifp
;
1388 struct nbr_connected
*ifc
;
1390 /* Get interface index. */
1391 ifindex
= stream_getl (s
);
1394 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1397 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1398 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD" : "DELETE",
1403 p
.family
= stream_getc (s
);
1404 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1405 p
.prefixlen
= stream_getc (s
);
1407 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1409 /* Currently only supporting P2P links, so any new RA source address is
1410 considered as the replacement of the previously learnt Link-Local address. */
1411 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1413 ifc
= nbr_connected_new ();
1414 ifc
->address
= prefix_new ();
1416 listnode_add (ifp
->nbr_connected
, ifc
);
1419 prefix_copy(ifc
->address
, &p
);
1423 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1425 ifc
= nbr_connected_check(ifp
, &p
);
1427 listnode_delete (ifp
->nbr_connected
, ifc
);
1434 zebra_interface_vrf_update_read (struct stream
*s
, vrf_id_t vrf_id
,
1435 vrf_id_t
*new_vrf_id
)
1437 unsigned int ifindex
;
1438 struct interface
*ifp
;
1439 vrf_id_t new_id
= VRF_DEFAULT
;
1441 /* Get interface index. */
1442 ifindex
= stream_getl (s
);
1444 /* Lookup interface. */
1445 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1448 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1453 /* Fetch new VRF Id. */
1454 new_id
= stream_getw (s
);
1456 *new_vrf_id
= new_id
;
1460 /* Zebra client message read function. */
1462 zclient_read (struct thread
*thread
)
1465 uint16_t length
, command
;
1466 uint8_t marker
, version
;
1468 struct zclient
*zclient
;
1470 /* Get socket to zebra. */
1471 zclient
= THREAD_ARG (thread
);
1472 zclient
->t_read
= NULL
;
1474 /* Read zebra header (if we don't have it already). */
1475 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1478 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1479 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1483 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1484 return zclient_failed(zclient
);
1486 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1488 /* Try again later. */
1489 zclient_event (ZCLIENT_READ
, zclient
);
1492 already
= ZEBRA_HEADER_SIZE
;
1495 /* Reset to read from the beginning of the incoming packet. */
1496 stream_set_getp(zclient
->ibuf
, 0);
1498 /* Fetch header values. */
1499 length
= stream_getw (zclient
->ibuf
);
1500 marker
= stream_getc (zclient
->ibuf
);
1501 version
= stream_getc (zclient
->ibuf
);
1502 vrf_id
= stream_getw (zclient
->ibuf
);
1503 command
= stream_getw (zclient
->ibuf
);
1505 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1507 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1508 __func__
, zclient
->sock
, marker
, version
);
1509 return zclient_failed(zclient
);
1512 if (length
< ZEBRA_HEADER_SIZE
)
1514 zlog_err("%s: socket %d message length %u is less than %d ",
1515 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1516 return zclient_failed(zclient
);
1520 if (length
> STREAM_SIZE(zclient
->ibuf
))
1523 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1524 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1525 ns
= stream_new(length
);
1526 stream_copy(ns
, zclient
->ibuf
);
1527 stream_free (zclient
->ibuf
);
1531 /* Read rest of zebra packet. */
1532 if (already
< length
)
1535 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1536 length
-already
)) == 0) ||
1540 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1541 return zclient_failed(zclient
);
1543 if (nbyte
!= (ssize_t
)(length
-already
))
1545 /* Try again later. */
1546 zclient_event (ZCLIENT_READ
, zclient
);
1551 length
-= ZEBRA_HEADER_SIZE
;
1554 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1558 case ZEBRA_ROUTER_ID_UPDATE
:
1559 if (zclient
->router_id_update
)
1560 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1563 zclient_vrf_add (zclient
, vrf_id
);
1565 case ZEBRA_VRF_DELETE
:
1566 zclient_vrf_delete (zclient
, vrf_id
);
1568 case ZEBRA_INTERFACE_ADD
:
1569 if (zclient
->interface_add
)
1570 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1572 case ZEBRA_INTERFACE_DELETE
:
1573 if (zclient
->interface_delete
)
1574 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1576 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1577 if (zclient
->interface_address_add
)
1578 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1580 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1581 if (zclient
->interface_address_delete
)
1582 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1584 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1585 if (zclient
->interface_bfd_dest_update
)
1586 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1588 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1589 if (zclient
->interface_nbr_address_add
)
1590 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1592 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1593 if (zclient
->interface_nbr_address_delete
)
1594 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1596 case ZEBRA_INTERFACE_UP
:
1597 if (zclient
->interface_up
)
1598 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
1600 case ZEBRA_INTERFACE_DOWN
:
1601 if (zclient
->interface_down
)
1602 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
1604 case ZEBRA_INTERFACE_VRF_UPDATE
:
1605 if (zclient
->interface_vrf_update
)
1606 (*zclient
->interface_vrf_update
) (command
, zclient
, length
, vrf_id
);
1608 case ZEBRA_NEXTHOP_UPDATE
:
1610 zlog_debug("zclient rcvd nexthop update\n");
1611 if (zclient
->nexthop_update
)
1612 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
1614 case ZEBRA_IMPORT_CHECK_UPDATE
:
1616 zlog_debug("zclient rcvd import check update\n");
1617 if (zclient
->import_check_update
)
1618 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
1620 case ZEBRA_BFD_DEST_REPLAY
:
1621 if (zclient
->bfd_dest_replay
)
1622 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
1624 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
1625 if (zclient
->redistribute_route_ipv4_add
)
1626 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
1628 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
1629 if (zclient
->redistribute_route_ipv4_del
)
1630 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
1632 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
1633 if (zclient
->redistribute_route_ipv6_add
)
1634 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
1636 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
1637 if (zclient
->redistribute_route_ipv6_del
)
1638 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
1640 case ZEBRA_INTERFACE_LINK_PARAMS
:
1641 if (zclient
->interface_link_params
)
1642 (*zclient
->interface_link_params
) (command
, zclient
, length
);
1648 if (zclient
->sock
< 0)
1649 /* Connection was closed during packet processing. */
1652 /* Register read thread. */
1653 stream_reset(zclient
->ibuf
);
1654 zclient_event (ZCLIENT_READ
, zclient
);
1660 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1661 u_short instance
, vrf_id_t vrf_id
)
1665 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1667 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1669 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1673 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1675 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1679 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1681 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1683 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
1687 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1689 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
1693 if (zclient
->sock
> 0)
1694 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
1699 zclient_redistribute_default (int command
, struct zclient
*zclient
,
1703 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1705 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1707 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
1711 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1713 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
1716 if (zclient
->sock
> 0)
1717 zebra_message_send (zclient
, command
, vrf_id
);
1721 zclient_event (enum event event
, struct zclient
*zclient
)
1725 case ZCLIENT_SCHEDULE
:
1726 if (! zclient
->t_connect
)
1727 zclient
->t_connect
=
1728 thread_add_event (zclient
->master
, zclient_connect
, zclient
, 0);
1730 case ZCLIENT_CONNECT
:
1732 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
1733 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
1734 if (! zclient
->t_connect
)
1735 zclient
->t_connect
=
1736 thread_add_timer (zclient
->master
, zclient_connect
, zclient
,
1737 zclient
->fail
< 3 ? 10 : 60);
1741 thread_add_read (zclient
->master
, zclient_read
, zclient
, zclient
->sock
);
1746 const char *zclient_serv_path_get()
1748 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
1752 zclient_serv_path_set (char *path
)
1757 zclient_serv_path
= NULL
;
1759 /* test if `path' is socket. don't set it otherwise. */
1760 if (stat(path
, &sb
) == -1)
1762 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
1766 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
1768 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
1772 /* it seems that path is unix socket */
1773 zclient_serv_path
= path
;