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,
36 /* Zebra client events. */
37 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
39 /* Prototype for event manager. */
40 static void zclient_event (enum event
, struct zclient
*);
42 const char *zclient_serv_path
= NULL
;
44 /* This file local debug flag. */
45 int zclient_debug
= 0;
47 /* Allocate zclient structure. */
49 zclient_new (struct thread_master
*master
)
51 struct zclient
*zclient
;
52 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
54 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
55 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
56 zclient
->wb
= buffer_new(0);
57 zclient
->master
= master
;
62 /* This function is only called when exiting, because
63 many parts of the code do not check for I/O errors, so they could
64 reference an invalid pointer if the structure was ever freed.
66 Free zclient structure. */
68 zclient_free (struct zclient
*zclient
)
71 stream_free(zclient
->ibuf
);
73 stream_free(zclient
->obuf
);
75 buffer_free(zclient
->wb
);
77 XFREE (MTYPE_ZCLIENT
, zclient
);
81 redist_check_instance (struct redist_proto
*red
, u_short instance
)
83 struct listnode
*node
;
89 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
97 redist_add_instance (struct redist_proto
*red
, u_short instance
)
104 red
->instances
= list_new();
106 in
= (u_short
*)calloc(1, sizeof(u_short
));
108 listnode_add (red
->instances
, in
);
112 redist_del_instance (struct redist_proto
*red
, u_short instance
)
114 struct listnode
*node
;
120 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
126 listnode_delete(red
->instances
, id
);
127 if (!red
->instances
->count
)
130 list_free(red
->instances
);
131 red
->instances
= NULL
;
136 /* Stop zebra client services. */
138 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
);
165 zclient_reset (struct zclient
*zclient
)
169 zclient_stop (zclient
);
171 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
172 redist_del_instance (&zclient
->mi_redist
[afi
][zclient
->redist_default
], zclient
->instance
);
174 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
177 #ifdef HAVE_TCP_ZEBRA
179 /* Make socket to zebra daemon. Return zebra socket. */
185 struct sockaddr_in serv
;
187 /* We should think about IPv6 connection. */
188 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
192 /* Make server socket. */
193 memset (&serv
, 0, sizeof (struct sockaddr_in
));
194 serv
.sin_family
= AF_INET
;
195 serv
.sin_port
= htons (ZEBRA_PORT
);
196 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
197 serv
.sin_len
= sizeof (struct sockaddr_in
);
198 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
199 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
201 /* Connect to zebra. */
202 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
205 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__
, errno
);
214 /* For sockaddr_un. */
218 zclient_socket_un (const char *path
)
222 struct sockaddr_un addr
;
224 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
228 /* Make server socket. */
229 memset (&addr
, 0, sizeof (struct sockaddr_un
));
230 addr
.sun_family
= AF_UNIX
;
231 strncpy (addr
.sun_path
, path
, strlen (path
));
232 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
233 len
= addr
.sun_len
= SUN_LEN(&addr
);
235 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
236 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
238 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
241 zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__
, errno
);
248 #endif /* HAVE_TCP_ZEBRA */
251 * Connect to zebra daemon.
252 * @param zclient a pointer to zclient structure
253 * @return socket fd just to make sure that connection established
258 zclient_socket_connect (struct zclient
*zclient
)
260 #ifdef HAVE_TCP_ZEBRA
261 zclient
->sock
= zclient_socket ();
263 zclient
->sock
= zclient_socket_un (zclient_serv_path_get());
265 return zclient
->sock
;
269 zclient_failed(struct zclient
*zclient
)
272 zclient_stop(zclient
);
273 zclient_event(ZCLIENT_CONNECT
, zclient
);
278 zclient_flush_data(struct thread
*thread
)
280 struct zclient
*zclient
= THREAD_ARG(thread
);
282 zclient
->t_write
= NULL
;
283 if (zclient
->sock
< 0)
285 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
288 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
289 __func__
, zclient
->sock
);
290 return zclient_failed(zclient
);
293 zclient
->t_write
= thread_add_write(zclient
->master
, zclient_flush_data
,
294 zclient
, zclient
->sock
);
303 zclient_send_message(struct zclient
*zclient
)
305 if (zclient
->sock
< 0)
307 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
308 stream_get_endp(zclient
->obuf
)))
311 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
312 __func__
, zclient
->sock
);
313 return zclient_failed(zclient
);
316 THREAD_OFF(zclient
->t_write
);
319 THREAD_WRITE_ON(zclient
->master
, zclient
->t_write
,
320 zclient_flush_data
, zclient
, zclient
->sock
);
327 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
329 /* length placeholder, caller can update */
330 stream_putw (s
, ZEBRA_HEADER_SIZE
);
331 stream_putc (s
, ZEBRA_HEADER_MARKER
);
332 stream_putc (s
, ZSERV_VERSION
);
333 stream_putw (s
, vrf_id
);
334 stream_putw (s
, command
);
338 zclient_read_header (struct stream
*s
, int sock
, u_int16_t
*size
, u_char
*marker
,
339 u_char
*version
, vrf_id_t
*vrf_id
, u_int16_t
*cmd
)
341 if (stream_read (s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
344 *size
= stream_getw (s
) - ZEBRA_HEADER_SIZE
;
345 *marker
= stream_getc (s
);
346 *version
= stream_getc (s
);
347 *vrf_id
= stream_getw (s
);
348 *cmd
= stream_getw (s
);
350 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
)
352 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
353 __func__
, sock
, *marker
, *version
);
357 if (*size
&& stream_read (s
, sock
, *size
) != *size
)
363 /* Send simple Zebra message. */
365 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
369 /* Get zclient output buffer. */
373 /* Send very simple command only Zebra message. */
374 zclient_create_header (s
, command
, vrf_id
);
376 return zclient_send_message(zclient
);
380 zebra_hello_send (struct zclient
*zclient
)
384 if (zclient
->redist_default
)
389 /* The VRF ID in the HELLO message is always 0. */
390 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
391 stream_putc (s
, zclient
->redist_default
);
392 stream_putw (s
, zclient
->instance
);
393 stream_putw_at (s
, 0, stream_get_endp (s
));
394 return zclient_send_message(zclient
);
400 /* Send register requests to zebra daemon for the information in a VRF. */
402 zclient_send_reg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
407 /* zclient is disabled. */
408 if (! zclient
->enable
)
411 /* If not connected to the zebra yet. */
412 if (zclient
->sock
< 0)
416 zlog_debug ("%s: send register messages for VRF %u", __func__
, vrf_id
);
418 /* We need router-id information. */
419 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
421 /* We need interface information. */
422 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
424 /* Set unwanted redistribute route. */
425 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
426 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
428 /* Flush all redistribute request. */
429 if (vrf_id
== VRF_DEFAULT
)
430 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
431 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
432 if (zclient
->mi_redist
[afi
][i
].enabled
)
434 struct listnode
*node
;
437 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
438 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
439 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
443 /* Flush all redistribute request. */
444 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
445 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
446 if (i
!= zclient
->redist_default
&&
447 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
448 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
450 /* If default information is needed. */
451 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
452 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
455 /* Send unregister requests to zebra daemon for the information in a VRF. */
457 zclient_send_dereg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
462 /* zclient is disabled. */
463 if (! zclient
->enable
)
466 /* If not connected to the zebra yet. */
467 if (zclient
->sock
< 0)
471 zlog_debug ("%s: send deregister messages for VRF %u", __func__
, vrf_id
);
473 /* We need router-id information. */
474 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
476 /* We need interface information. */
477 zebra_message_send (zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
479 /* Set unwanted redistribute route. */
480 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
481 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
483 /* Flush all redistribute request. */
484 if (vrf_id
== VRF_DEFAULT
)
485 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
486 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
487 if (zclient
->mi_redist
[afi
][i
].enabled
)
489 struct listnode
*node
;
492 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
493 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
494 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
,
498 /* Flush all redistribute request. */
499 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
500 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
501 if (i
!= zclient
->redist_default
&&
502 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
503 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
, 0, vrf_id
);
505 /* If default information is needed. */
506 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
507 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, vrf_id
);
510 /* Send request to zebra daemon to start or stop RA. */
512 zclient_send_interface_radv_req (struct zclient
*zclient
, vrf_id_t vrf_id
,
513 struct interface
*ifp
, int enable
, int ra_interval
)
517 /* zclient is disabled. */
518 if (!zclient
->enable
)
521 /* If not connected to the zebra yet. */
522 if (zclient
->sock
< 0)
525 /* Form and send message. */
530 zclient_create_header (s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
532 zclient_create_header (s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
534 stream_putl (s
, ifp
->ifindex
);
535 stream_putl (s
, ra_interval
);
537 stream_putw_at (s
, 0, stream_get_endp (s
));
539 zclient_send_message(zclient
);
542 /* Make connection to zebra daemon. */
544 zclient_start (struct zclient
*zclient
)
547 zlog_info ("zclient_start is called");
549 /* zclient is disabled. */
550 if (! zclient
->enable
)
553 /* If already connected to the zebra. */
554 if (zclient
->sock
>= 0)
557 /* Check connect thread. */
558 if (zclient
->t_connect
)
562 * If we fail to connect to the socket on initialization,
563 * Let's wait a second and see if we can reconnect.
564 * Cause if we don't connect, we never attempt to
565 * reconnect. On startup if zebra is slow we
566 * can get into this situation.
568 while (zclient_socket_connect(zclient
) < 0 && zclient
->fail
< 5)
571 zlog_debug ("zclient connection fail");
576 if (zclient
->sock
< 0)
578 zclient_event (ZCLIENT_CONNECT
, zclient
);
582 if (set_nonblocking(zclient
->sock
) < 0)
583 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
585 /* Clear fail count. */
588 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
590 /* Create read thread. */
591 zclient_event (ZCLIENT_READ
, zclient
);
593 zebra_hello_send (zclient
);
595 /* Inform the successful connection. */
596 if (zclient
->zebra_connected
)
597 (*zclient
->zebra_connected
) (zclient
);
602 /* Initialize zebra client. Argument redist_default is unwanted
603 redistribute route type. */
605 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
609 /* Enable zebra client connection by default. */
612 /* Set -1 to the default socket value. */
615 /* Clear redistribution flags. */
616 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
617 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
618 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
620 /* Set unwanted redistribute route. bgpd does not need BGP route
622 zclient
->redist_default
= redist_default
;
623 zclient
->instance
= instance
;
624 /* Pending: make afi(s) an arg. */
625 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
626 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
628 /* Set default-information redistribute to zero. */
629 zclient
->default_information
= vrf_bitmap_init ();;
632 zlog_debug ("zclient_start is called");
634 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
637 /* This function is a wrapper function for calling zclient_start from
638 timer or event thread. */
640 zclient_connect (struct thread
*t
)
642 struct zclient
*zclient
;
644 zclient
= THREAD_ARG (t
);
645 zclient
->t_connect
= NULL
;
648 zlog_debug ("zclient_connect is called");
650 return zclient_start (zclient
);
654 * "xdr_encode"-like interface that allows daemon (client) to send
655 * a message to zebra server for a route that needs to be
656 * added/deleted to the kernel. Info about the route is specified
657 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
658 * the info down the zclient socket using the stream_* functions.
660 * The corresponding read ("xdr_decode") function on the server
661 * side is zread_ipv4_add()/zread_ipv4_delete().
663 * 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
664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665 * | Length (2) | Command | Route Type |
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * | ZEBRA Flags | Message Flags | Prefix length |
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 * | Destination IPv4 Prefix for route |
670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
676 * described, as per the Nexthop count. Each nexthop described as:
679 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
680 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 * | IPv4 Nexthop address or Interface Index number |
682 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
685 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
686 * nexthop information is provided, and the message describes a prefix
687 * to blackhole or reject route.
689 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
690 * infrastructure was built around the traditional (32-bit "gate OR
691 * ifindex") nexthop data unit. A special encoding can be used to feed
692 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
693 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
695 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
696 * - .nexthop_num == .ifindex_num
697 * - .nexthop and .ifindex are filled with gate and ifindex parts of
698 * each compound nexthop, both in the same order
700 * zapi_ipv4_route() will produce two nexthop data units for each such
701 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
702 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
704 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
707 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
710 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value
712 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
714 * XXX: No attention paid to alignment.
717 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
718 struct zapi_ipv4
*api
)
728 zclient_create_header (s
, cmd
, api
->vrf_id
);
730 /* Put type and nexthop. */
731 stream_putc (s
, api
->type
);
732 stream_putw (s
, api
->instance
);
733 stream_putc (s
, api
->flags
);
734 stream_putc (s
, api
->message
);
735 stream_putw (s
, api
->safi
);
737 /* Put prefix information. */
738 psize
= PSIZE (p
->prefixlen
);
739 stream_putc (s
, p
->prefixlen
);
740 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
742 /* Nexthop, ifindex, distance and metric information. */
743 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
745 /* traditional 32-bit data units */
746 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
749 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
750 /* XXX assert(api->nexthop_num == 0); */
751 /* XXX assert(api->ifindex_num == 0); */
754 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
756 for (i
= 0; i
< api
->nexthop_num
; i
++)
758 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
759 stream_put_in_addr (s
, api
->nexthop
[i
]);
761 for (i
= 0; i
< api
->ifindex_num
; i
++)
763 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
764 stream_putl (s
, api
->ifindex
[i
]);
768 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
769 stream_putc (s
, api
->distance
);
770 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
771 stream_putl (s
, api
->metric
);
772 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
773 stream_putw (s
, api
->tag
);
774 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
775 stream_putl (s
, api
->mtu
);
777 /* Put length at the first point of the stream. */
778 stream_putw_at (s
, 0, stream_get_endp (s
));
780 return zclient_send_message(zclient
);
785 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
786 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
796 zclient_create_header (s
, cmd
, api
->vrf_id
);
798 /* Put type and nexthop. */
799 stream_putc (s
, api
->type
);
800 stream_putw (s
, api
->instance
);
801 stream_putc (s
, api
->flags
);
802 stream_putc (s
, api
->message
);
803 stream_putw (s
, api
->safi
);
805 /* Put prefix information. */
806 psize
= PSIZE (p
->prefixlen
);
807 stream_putc (s
, p
->prefixlen
);
808 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
810 /* Nexthop, ifindex, distance and metric information. */
811 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
813 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
816 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
817 /* XXX assert(api->nexthop_num == 0); */
818 /* XXX assert(api->ifindex_num == 0); */
821 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
823 for (i
= 0; i
< api
->nexthop_num
; i
++)
825 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
826 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
828 for (i
= 0; i
< api
->ifindex_num
; i
++)
830 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
831 stream_putl (s
, api
->ifindex
[i
]);
835 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
836 stream_putc (s
, api
->distance
);
837 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
838 stream_putl (s
, api
->metric
);
839 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
840 stream_putw (s
, api
->tag
);
841 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
842 stream_putl (s
, api
->mtu
);
844 /* Put length at the first point of the stream. */
845 stream_putw_at (s
, 0, stream_get_endp (s
));
847 return zclient_send_message(zclient
);
851 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
852 struct zapi_ipv6
*api
)
862 zclient_create_header (s
, cmd
, api
->vrf_id
);
864 /* Put type and nexthop. */
865 stream_putc (s
, api
->type
);
866 stream_putw (s
, api
->instance
);
867 stream_putc (s
, api
->flags
);
868 stream_putc (s
, api
->message
);
869 stream_putw (s
, api
->safi
);
871 /* Put prefix information. */
872 psize
= PSIZE (p
->prefixlen
);
873 stream_putc (s
, p
->prefixlen
);
874 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
876 /* Nexthop, ifindex, distance and metric information. */
877 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
879 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
882 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
883 /* XXX assert(api->nexthop_num == 0); */
884 /* XXX assert(api->ifindex_num == 0); */
887 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
889 for (i
= 0; i
< api
->nexthop_num
; i
++)
891 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
892 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
894 for (i
= 0; i
< api
->ifindex_num
; i
++)
896 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
897 stream_putl (s
, api
->ifindex
[i
]);
901 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
902 stream_putc (s
, api
->distance
);
903 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
904 stream_putl (s
, api
->metric
);
905 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
906 stream_putw (s
, api
->tag
);
907 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
908 stream_putl (s
, api
->mtu
);
910 /* Put length at the first point of the stream. */
911 stream_putw_at (s
, 0, stream_get_endp (s
));
913 return zclient_send_message(zclient
);
915 #endif /* HAVE_IPV6 */
918 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
919 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
920 * then set/unset redist[type] in the client handle (a struct zserv) for the
924 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
925 u_short instance
, vrf_id_t vrf_id
)
932 zclient_create_header (s
, command
, vrf_id
);
933 stream_putc (s
, afi
);
934 stream_putc (s
, type
);
935 stream_putw (s
, instance
);
937 stream_putw_at (s
, 0, stream_get_endp (s
));
939 return zclient_send_message(zclient
);
942 /* Router-id update from zebra daemon. */
944 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
948 /* Fetch interface address. */
949 rid
->family
= stream_getc (s
);
951 plen
= prefix_blen (rid
);
952 stream_get (&rid
->u
.prefix
, s
, plen
);
953 rid
->prefixlen
= stream_getc (s
);
956 /* Interface addition from zebra daemon. */
958 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
959 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
961 * 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
964 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
970 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
972 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
975 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
977 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
979 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
981 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
983 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
985 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
989 zclient_vrf_add (struct zclient
*zclient
, vrf_id_t vrf_id
)
992 char vrfname_tmp
[VRF_NAMSIZ
];
994 /* Read interface name. */
995 stream_get (vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
997 /* Lookup/create vrf by vrf_id. */
998 vrf
= vrf_get (vrf_id
, vrfname_tmp
);
1004 zclient_vrf_delete (struct zclient
*zclient
, vrf_id_t vrf_id
)
1008 /* Lookup vrf by vrf_id. */
1009 vrf
= vrf_lookup (vrf_id
);
1012 * If a routing protocol doesn't know about a
1013 * vrf that is about to be deleted. There is
1014 * no point in attempting to delete it.
1023 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
1025 struct interface
*ifp
;
1026 char ifname_tmp
[INTERFACE_NAMSIZ
];
1028 /* Read interface name. */
1029 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1031 /* Lookup/create interface by name. */
1032 ifp
= if_get_by_name_len_vrf (ifname_tmp
,
1033 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1036 zebra_interface_if_set_value (s
, ifp
);
1042 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1043 * from zebra server. The format of this message is the same as
1044 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1045 * comments for zebra_interface_add_read), except that no sockaddr_dl
1046 * is sent at the tail of the message.
1049 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
1051 struct interface
*ifp
;
1052 char ifname_tmp
[INTERFACE_NAMSIZ
];
1054 /* Read interface name. */
1055 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1057 /* Lookup this by interface index. */
1058 ifp
= if_lookup_by_name_len_vrf (ifname_tmp
,
1059 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1063 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1064 ifname_tmp
, vrf_id
);
1068 zebra_interface_if_set_value (s
, ifp
);
1074 * format of message for address additon is:
1078 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1079 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1088 * | ifc_flags | flags for connected address
1096 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1106 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
1108 /* Read interface's index. */
1109 ifp
->ifindex
= stream_getl (s
);
1110 ifp
->status
= stream_getc (s
);
1112 /* Read interface's value. */
1113 ifp
->flags
= stream_getq (s
);
1114 ifp
->ptm_enable
= stream_getc (s
);
1115 ifp
->ptm_status
= stream_getc (s
);
1116 ifp
->metric
= stream_getl (s
);
1117 ifp
->mtu
= stream_getl (s
);
1118 ifp
->mtu6
= stream_getl (s
);
1119 ifp
->bandwidth
= stream_getl (s
);
1120 #ifdef HAVE_STRUCT_SOCKADDR_DL
1121 stream_get (&ifp
->sdl
, s
, sizeof (ifp
->sdl_storage
));
1123 ifp
->hw_addr_len
= stream_getl (s
);
1124 if (ifp
->hw_addr_len
)
1125 stream_get (ifp
->hw_addr
, s
, MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1126 #endif /* HAVE_STRUCT_SOCKADDR_DL */
1130 memconstant(const void *s
, int c
, size_t n
)
1132 const u_char
*p
= s
;
1142 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1145 struct interface
*ifp
;
1146 struct connected
*ifc
;
1152 memset (&p
, 0, sizeof(p
));
1153 memset (&d
, 0, sizeof(d
));
1155 /* Get interface index. */
1156 ifindex
= stream_getl (s
);
1159 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1162 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1163 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1169 ifc_flags
= stream_getc (s
);
1171 /* Fetch interface address. */
1172 family
= p
.family
= stream_getc (s
);
1174 plen
= prefix_blen (&p
);
1175 stream_get (&p
.u
.prefix
, s
, plen
);
1176 p
.prefixlen
= stream_getc (s
);
1178 /* Fetch destination address. */
1179 stream_get (&d
.u
.prefix
, s
, plen
);
1182 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1184 ifc
= connected_lookup_prefix_exact (ifp
, &p
);
1187 /* N.B. NULL destination pointers are encoded as all zeroes */
1188 ifc
= connected_add_by_prefix(ifp
, &p
, (memconstant(&d
.u
.prefix
,0,plen
) ?
1193 ifc
->flags
= ifc_flags
;
1194 if (ifc
->destination
)
1195 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1196 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1198 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1199 char buf
[PREFIX_STRLEN
];
1200 zlog_warn("warning: interface %s address %s "
1201 "with peer flag set, but no peer address!",
1203 prefix2str (ifc
->address
, buf
, sizeof buf
));
1204 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1210 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1211 ifc
= connected_delete_by_prefix(ifp
, &p
);
1218 * format of message for neighbor connected address is:
1222 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1223 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1238 * | addr_len | len of addr.
1241 struct nbr_connected
*
1242 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1244 unsigned int ifindex
;
1245 struct interface
*ifp
;
1247 struct nbr_connected
*ifc
;
1249 /* Get interface index. */
1250 ifindex
= stream_getl (s
);
1253 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1256 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1257 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD" : "DELETE",
1262 p
.family
= stream_getc (s
);
1263 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1264 p
.prefixlen
= stream_getc (s
);
1266 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1268 /* Currently only supporting P2P links, so any new RA source address is
1269 considered as the replacement of the previously learnt Link-Local address. */
1270 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1272 ifc
= nbr_connected_new ();
1273 ifc
->address
= prefix_new ();
1275 listnode_add (ifp
->nbr_connected
, ifc
);
1278 prefix_copy(ifc
->address
, &p
);
1282 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1284 ifc
= nbr_connected_check(ifp
, &p
);
1286 listnode_delete (ifp
->nbr_connected
, ifc
);
1293 zebra_interface_vrf_update_read (struct stream
*s
, vrf_id_t vrf_id
,
1294 vrf_id_t
*new_vrf_id
)
1296 unsigned int ifindex
;
1297 struct interface
*ifp
;
1298 vrf_id_t new_id
= VRF_DEFAULT
;
1300 /* Get interface index. */
1301 ifindex
= stream_getl (s
);
1303 /* Lookup interface. */
1304 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1307 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1312 /* Fetch new VRF Id. */
1313 new_id
= stream_getw (s
);
1315 *new_vrf_id
= new_id
;
1319 /* Zebra client message read function. */
1321 zclient_read (struct thread
*thread
)
1324 uint16_t length
, command
;
1325 uint8_t marker
, version
;
1327 struct zclient
*zclient
;
1329 /* Get socket to zebra. */
1330 zclient
= THREAD_ARG (thread
);
1331 zclient
->t_read
= NULL
;
1333 /* Read zebra header (if we don't have it already). */
1334 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1337 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1338 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1342 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1343 return zclient_failed(zclient
);
1345 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1347 /* Try again later. */
1348 zclient_event (ZCLIENT_READ
, zclient
);
1351 already
= ZEBRA_HEADER_SIZE
;
1354 /* Reset to read from the beginning of the incoming packet. */
1355 stream_set_getp(zclient
->ibuf
, 0);
1357 /* Fetch header values. */
1358 length
= stream_getw (zclient
->ibuf
);
1359 marker
= stream_getc (zclient
->ibuf
);
1360 version
= stream_getc (zclient
->ibuf
);
1361 vrf_id
= stream_getw (zclient
->ibuf
);
1362 command
= stream_getw (zclient
->ibuf
);
1364 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1366 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1367 __func__
, zclient
->sock
, marker
, version
);
1368 return zclient_failed(zclient
);
1371 if (length
< ZEBRA_HEADER_SIZE
)
1373 zlog_err("%s: socket %d message length %u is less than %d ",
1374 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1375 return zclient_failed(zclient
);
1379 if (length
> STREAM_SIZE(zclient
->ibuf
))
1382 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1383 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1384 ns
= stream_new(length
);
1385 stream_copy(ns
, zclient
->ibuf
);
1386 stream_free (zclient
->ibuf
);
1390 /* Read rest of zebra packet. */
1391 if (already
< length
)
1394 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1395 length
-already
)) == 0) ||
1399 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1400 return zclient_failed(zclient
);
1402 if (nbyte
!= (ssize_t
)(length
-already
))
1404 /* Try again later. */
1405 zclient_event (ZCLIENT_READ
, zclient
);
1410 length
-= ZEBRA_HEADER_SIZE
;
1413 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1417 case ZEBRA_ROUTER_ID_UPDATE
:
1418 if (zclient
->router_id_update
)
1419 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1422 zclient_vrf_add (zclient
, vrf_id
);
1424 case ZEBRA_VRF_DELETE
:
1425 zclient_vrf_delete (zclient
, vrf_id
);
1427 case ZEBRA_INTERFACE_ADD
:
1428 if (zclient
->interface_add
)
1429 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1431 case ZEBRA_INTERFACE_DELETE
:
1432 if (zclient
->interface_delete
)
1433 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1435 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1436 if (zclient
->interface_address_add
)
1437 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1439 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1440 if (zclient
->interface_address_delete
)
1441 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1443 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1444 if (zclient
->interface_bfd_dest_update
)
1445 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1447 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1448 if (zclient
->interface_nbr_address_add
)
1449 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1451 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1452 if (zclient
->interface_nbr_address_delete
)
1453 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1455 case ZEBRA_INTERFACE_UP
:
1456 if (zclient
->interface_up
)
1457 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
1459 case ZEBRA_INTERFACE_DOWN
:
1460 if (zclient
->interface_down
)
1461 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
1463 case ZEBRA_INTERFACE_VRF_UPDATE
:
1464 if (zclient
->interface_vrf_update
)
1465 (*zclient
->interface_vrf_update
) (command
, zclient
, length
, vrf_id
);
1467 case ZEBRA_IPV4_ROUTE_ADD
:
1468 if (zclient
->ipv4_route_add
)
1469 (*zclient
->ipv4_route_add
) (command
, zclient
, length
, vrf_id
);
1471 case ZEBRA_IPV4_ROUTE_DELETE
:
1472 if (zclient
->ipv4_route_delete
)
1473 (*zclient
->ipv4_route_delete
) (command
, zclient
, length
, vrf_id
);
1475 case ZEBRA_IPV6_ROUTE_ADD
:
1476 if (zclient
->ipv6_route_add
)
1477 (*zclient
->ipv6_route_add
) (command
, zclient
, length
, vrf_id
);
1479 case ZEBRA_IPV6_ROUTE_DELETE
:
1480 if (zclient
->ipv6_route_delete
)
1481 (*zclient
->ipv6_route_delete
) (command
, zclient
, length
, vrf_id
);
1483 case ZEBRA_NEXTHOP_UPDATE
:
1485 zlog_debug("zclient rcvd nexthop update\n");
1486 if (zclient
->nexthop_update
)
1487 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
1489 case ZEBRA_IMPORT_CHECK_UPDATE
:
1491 zlog_debug("zclient rcvd import check update\n");
1492 if (zclient
->import_check_update
)
1493 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
1495 case ZEBRA_BFD_DEST_REPLAY
:
1496 if (zclient
->bfd_dest_replay
)
1497 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
1499 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
1500 if (zclient
->redistribute_route_ipv4_add
)
1501 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
1503 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
1504 if (zclient
->redistribute_route_ipv4_del
)
1505 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
1507 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
1508 if (zclient
->redistribute_route_ipv6_add
)
1509 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
1511 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
1512 if (zclient
->redistribute_route_ipv6_del
)
1513 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
1519 if (zclient
->sock
< 0)
1520 /* Connection was closed during packet processing. */
1523 /* Register read thread. */
1524 stream_reset(zclient
->ibuf
);
1525 zclient_event (ZCLIENT_READ
, zclient
);
1531 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1532 u_short instance
, vrf_id_t vrf_id
)
1536 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1538 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1540 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1544 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1546 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1550 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1552 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1554 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
1558 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1560 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
1564 if (zclient
->sock
> 0)
1565 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
1570 zclient_redistribute_default (int command
, struct zclient
*zclient
,
1574 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1576 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1578 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
1582 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1584 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
1587 if (zclient
->sock
> 0)
1588 zebra_message_send (zclient
, command
, vrf_id
);
1592 zclient_event (enum event event
, struct zclient
*zclient
)
1596 case ZCLIENT_SCHEDULE
:
1597 if (! zclient
->t_connect
)
1598 zclient
->t_connect
=
1599 thread_add_event (zclient
->master
, zclient_connect
, zclient
, 0);
1601 case ZCLIENT_CONNECT
:
1602 if (zclient
->fail
>= 10)
1605 zlog_debug ("zclient connect schedule interval is %d",
1606 zclient
->fail
< 3 ? 10 : 60);
1607 if (! zclient
->t_connect
)
1608 zclient
->t_connect
=
1609 thread_add_timer (zclient
->master
, zclient_connect
, zclient
,
1610 zclient
->fail
< 3 ? 10 : 60);
1614 thread_add_read (zclient
->master
, zclient_read
, zclient
, zclient
->sock
);
1619 const char *zclient_serv_path_get()
1621 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
1625 zclient_serv_path_set (char *path
)
1630 zclient_serv_path
= NULL
;
1632 /* test if `path' is socket. don't set it otherwise. */
1633 if (stat(path
, &sb
) == -1)
1635 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
1639 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
1641 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
1645 /* it seems that path is unix socket */
1646 zclient_serv_path
= path
;