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 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
));
213 /* For sockaddr_un. */
217 zclient_socket_un (const char *path
)
221 struct sockaddr_un addr
;
223 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
227 /* Make server socket. */
228 memset (&addr
, 0, sizeof (struct sockaddr_un
));
229 addr
.sun_family
= AF_UNIX
;
230 strncpy (addr
.sun_path
, path
, strlen (path
));
231 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
232 len
= addr
.sun_len
= SUN_LEN(&addr
);
234 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
235 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
237 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
246 #endif /* HAVE_TCP_ZEBRA */
249 * Connect to zebra daemon.
250 * @param zclient a pointer to zclient structure
251 * @return socket fd just to make sure that connection established
256 zclient_socket_connect (struct zclient
*zclient
)
258 #ifdef HAVE_TCP_ZEBRA
259 zclient
->sock
= zclient_socket ();
261 zclient
->sock
= zclient_socket_un (zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
);
263 return zclient
->sock
;
267 zclient_failed(struct zclient
*zclient
)
270 zclient_stop(zclient
);
271 zclient_event(ZCLIENT_CONNECT
, zclient
);
276 zclient_flush_data(struct thread
*thread
)
278 struct zclient
*zclient
= THREAD_ARG(thread
);
280 zclient
->t_write
= NULL
;
281 if (zclient
->sock
< 0)
283 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
286 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
287 __func__
, zclient
->sock
);
288 return zclient_failed(zclient
);
291 zclient
->t_write
= thread_add_write(zclient
->master
, zclient_flush_data
,
292 zclient
, zclient
->sock
);
301 zclient_send_message(struct zclient
*zclient
)
303 if (zclient
->sock
< 0)
305 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
306 stream_get_endp(zclient
->obuf
)))
309 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
310 __func__
, zclient
->sock
);
311 return zclient_failed(zclient
);
314 THREAD_OFF(zclient
->t_write
);
317 THREAD_WRITE_ON(zclient
->master
, zclient
->t_write
,
318 zclient_flush_data
, zclient
, zclient
->sock
);
325 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
327 /* length placeholder, caller can update */
328 stream_putw (s
, ZEBRA_HEADER_SIZE
);
329 stream_putc (s
, ZEBRA_HEADER_MARKER
);
330 stream_putc (s
, ZSERV_VERSION
);
331 stream_putw (s
, vrf_id
);
332 stream_putw (s
, command
);
335 /* Send simple Zebra message. */
337 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
341 /* Get zclient output buffer. */
345 /* Send very simple command only Zebra message. */
346 zclient_create_header (s
, command
, vrf_id
);
348 return zclient_send_message(zclient
);
352 zebra_hello_send (struct zclient
*zclient
)
356 if (zclient
->redist_default
)
361 /* The VRF ID in the HELLO message is always 0. */
362 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
363 stream_putc (s
, zclient
->redist_default
);
364 stream_putw (s
, zclient
->instance
);
365 stream_putw_at (s
, 0, stream_get_endp (s
));
366 return zclient_send_message(zclient
);
372 /* Send requests to zebra daemon for the information in a VRF. */
374 zclient_send_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
379 /* zclient is disabled. */
380 if (! zclient
->enable
)
383 /* If not connected to the zebra yet. */
384 if (zclient
->sock
< 0)
388 zlog_debug ("%s: send messages for VRF %u", __func__
, vrf_id
);
390 /* We need router-id information. */
391 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
393 /* We need interface information. */
394 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
396 /* Set unwanted redistribute route. */
397 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
398 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
400 /* Flush all redistribute request. */
401 if (vrf_id
== VRF_DEFAULT
)
402 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
403 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
404 if (zclient
->mi_redist
[afi
][i
].enabled
)
406 struct listnode
*node
;
409 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
410 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
411 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
415 /* Flush all redistribute request. */
416 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
417 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
418 if (i
!= zclient
->redist_default
&&
419 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
420 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
422 /* If default information is needed. */
423 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
424 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
427 /* Make connection to zebra daemon. */
429 zclient_start (struct zclient
*zclient
)
432 zlog_info ("zclient_start is called");
434 /* zclient is disabled. */
435 if (! zclient
->enable
)
438 /* If already connected to the zebra. */
439 if (zclient
->sock
>= 0)
442 /* Check connect thread. */
443 if (zclient
->t_connect
)
446 if (zclient_socket_connect(zclient
) < 0)
449 zlog_debug ("zclient connection fail");
451 zclient_event (ZCLIENT_CONNECT
, zclient
);
455 if (set_nonblocking(zclient
->sock
) < 0)
456 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
458 /* Clear fail count. */
461 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
463 /* Create read thread. */
464 zclient_event (ZCLIENT_READ
, zclient
);
466 zebra_hello_send (zclient
);
468 /* Inform the successful connection. */
469 if (zclient
->zebra_connected
)
470 (*zclient
->zebra_connected
) (zclient
);
475 /* Initialize zebra client. Argument redist_default is unwanted
476 redistribute route type. */
478 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
482 /* Enable zebra client connection by default. */
485 /* Set -1 to the default socket value. */
488 /* Clear redistribution flags. */
489 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
490 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
491 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
493 /* Set unwanted redistribute route. bgpd does not need BGP route
495 zclient
->redist_default
= redist_default
;
496 zclient
->instance
= instance
;
497 /* Pending: make afi(s) an arg. */
498 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
499 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
501 /* Set default-information redistribute to zero. */
502 zclient
->default_information
= vrf_bitmap_init ();;
505 zlog_debug ("zclient_start is called");
507 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
510 /* This function is a wrapper function for calling zclient_start from
511 timer or event thread. */
513 zclient_connect (struct thread
*t
)
515 struct zclient
*zclient
;
517 zclient
= THREAD_ARG (t
);
518 zclient
->t_connect
= NULL
;
521 zlog_debug ("zclient_connect is called");
523 return zclient_start (zclient
);
527 * "xdr_encode"-like interface that allows daemon (client) to send
528 * a message to zebra server for a route that needs to be
529 * added/deleted to the kernel. Info about the route is specified
530 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
531 * the info down the zclient socket using the stream_* functions.
533 * The corresponding read ("xdr_decode") function on the server
534 * side is zread_ipv4_add()/zread_ipv4_delete().
536 * 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
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 * | Length (2) | Command | Route Type |
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540 * | ZEBRA Flags | Message Flags | Prefix length |
541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542 * | Destination IPv4 Prefix for route |
543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
549 * described, as per the Nexthop count. Each nexthop described as:
552 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
554 * | IPv4 Nexthop address or Interface Index number |
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
557 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
558 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
559 * nexthop information is provided, and the message describes a prefix
560 * to blackhole or reject route.
562 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
563 * infrastructure was built around the traditional (32-bit "gate OR
564 * ifindex") nexthop data unit. A special encoding can be used to feed
565 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
566 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
568 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
569 * - .nexthop_num == .ifindex_num
570 * - .nexthop and .ifindex are filled with gate and ifindex parts of
571 * each compound nexthop, both in the same order
573 * zapi_ipv4_route() will produce two nexthop data units for each such
574 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
575 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
577 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
580 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
583 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value
585 * XXX: No attention paid to alignment.
588 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
589 struct zapi_ipv4
*api
)
599 zclient_create_header (s
, cmd
, api
->vrf_id
);
601 /* Put type and nexthop. */
602 stream_putc (s
, api
->type
);
603 stream_putw (s
, api
->instance
);
604 stream_putc (s
, api
->flags
);
605 stream_putc (s
, api
->message
);
606 stream_putw (s
, api
->safi
);
608 /* Put prefix information. */
609 psize
= PSIZE (p
->prefixlen
);
610 stream_putc (s
, p
->prefixlen
);
611 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
613 /* Nexthop, ifindex, distance and metric information. */
614 /* ZAPI_MESSAGE_ONLINK implies interleaving */
615 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_ONLINK
))
617 /* ZAPI_MESSAGE_NEXTHOP is required for proper receiving */
618 assert (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
));
619 /* 64-bit data units, interleaved between nexthop[] and ifindex[] */
620 assert (api
->nexthop_num
== api
->ifindex_num
);
621 stream_putc (s
, api
->nexthop_num
* 2);
622 for (i
= 0; i
< api
->nexthop_num
; i
++)
624 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_ONLINK
);
625 stream_put_in_addr (s
, api
->nexthop
[i
]);
626 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
627 stream_putl (s
, api
->ifindex
[i
]);
630 else if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
632 /* traditional 32-bit data units */
633 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
636 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
637 /* XXX assert(api->nexthop_num == 0); */
638 /* XXX assert(api->ifindex_num == 0); */
641 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
643 for (i
= 0; i
< api
->nexthop_num
; i
++)
645 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
646 stream_put_in_addr (s
, api
->nexthop
[i
]);
648 for (i
= 0; i
< api
->ifindex_num
; i
++)
650 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
651 stream_putl (s
, api
->ifindex
[i
]);
655 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
656 stream_putc (s
, api
->distance
);
657 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
658 stream_putl (s
, api
->metric
);
659 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
660 stream_putw (s
, api
->tag
);
662 /* Put length at the first point of the stream. */
663 stream_putw_at (s
, 0, stream_get_endp (s
));
665 return zclient_send_message(zclient
);
670 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
671 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
681 zclient_create_header (s
, cmd
, api
->vrf_id
);
683 /* Put type and nexthop. */
684 stream_putc (s
, api
->type
);
685 stream_putw (s
, api
->instance
);
686 stream_putc (s
, api
->flags
);
687 stream_putc (s
, api
->message
);
688 stream_putw (s
, api
->safi
);
690 /* Put prefix information. */
691 psize
= PSIZE (p
->prefixlen
);
692 stream_putc (s
, p
->prefixlen
);
693 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
695 /* Nexthop, ifindex, distance and metric information. */
696 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
698 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
701 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
702 /* XXX assert(api->nexthop_num == 0); */
703 /* XXX assert(api->ifindex_num == 0); */
706 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
708 for (i
= 0; i
< api
->nexthop_num
; i
++)
710 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
711 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
713 for (i
= 0; i
< api
->ifindex_num
; i
++)
715 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
716 stream_putl (s
, api
->ifindex
[i
]);
720 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
721 stream_putc (s
, api
->distance
);
722 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
723 stream_putl (s
, api
->metric
);
724 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
725 stream_putw (s
, api
->tag
);
727 /* Put length at the first point of the stream. */
728 stream_putw_at (s
, 0, stream_get_endp (s
));
730 return zclient_send_message(zclient
);
734 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
735 struct zapi_ipv6
*api
)
745 zclient_create_header (s
, cmd
, api
->vrf_id
);
747 /* Put type and nexthop. */
748 stream_putc (s
, api
->type
);
749 stream_putw (s
, api
->instance
);
750 stream_putc (s
, api
->flags
);
751 stream_putc (s
, api
->message
);
752 stream_putw (s
, api
->safi
);
754 /* Put prefix information. */
755 psize
= PSIZE (p
->prefixlen
);
756 stream_putc (s
, p
->prefixlen
);
757 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
759 /* Nexthop, ifindex, distance and metric information. */
760 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
762 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
765 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
766 /* XXX assert(api->nexthop_num == 0); */
767 /* XXX assert(api->ifindex_num == 0); */
770 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
772 for (i
= 0; i
< api
->nexthop_num
; i
++)
774 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
775 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
777 for (i
= 0; i
< api
->ifindex_num
; i
++)
779 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
780 stream_putl (s
, api
->ifindex
[i
]);
784 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
785 stream_putc (s
, api
->distance
);
786 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
787 stream_putl (s
, api
->metric
);
788 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
789 stream_putw (s
, api
->tag
);
791 /* Put length at the first point of the stream. */
792 stream_putw_at (s
, 0, stream_get_endp (s
));
794 return zclient_send_message(zclient
);
796 #endif /* HAVE_IPV6 */
799 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
800 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
801 * then set/unset redist[type] in the client handle (a struct zserv) for the
805 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
806 u_short instance
, vrf_id_t vrf_id
)
813 zclient_create_header (s
, command
, vrf_id
);
814 stream_putc (s
, afi
);
815 stream_putc (s
, type
);
816 stream_putw (s
, instance
);
818 stream_putw_at (s
, 0, stream_get_endp (s
));
820 return zclient_send_message(zclient
);
823 /* Router-id update from zebra daemon. */
825 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
829 /* Fetch interface address. */
830 rid
->family
= stream_getc (s
);
832 plen
= prefix_blen (rid
);
833 stream_get (&rid
->u
.prefix
, s
, plen
);
834 rid
->prefixlen
= stream_getc (s
);
837 /* Interface addition from zebra daemon. */
839 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
840 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
842 * 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
845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
851 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
853 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
856 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
858 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
862 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
864 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
866 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
870 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
872 struct interface
*ifp
;
873 char ifname_tmp
[INTERFACE_NAMSIZ
];
875 /* Read interface name. */
876 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
878 /* Lookup/create interface by name. */
879 ifp
= if_get_by_name_len_vrf (ifname_tmp
,
880 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
883 zebra_interface_if_set_value (s
, ifp
);
889 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
890 * from zebra server. The format of this message is the same as
891 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
892 * comments for zebra_interface_add_read), except that no sockaddr_dl
893 * is sent at the tail of the message.
896 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
898 struct interface
*ifp
;
899 char ifname_tmp
[INTERFACE_NAMSIZ
];
901 /* Read interface name. */
902 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
904 /* Lookup this by interface index. */
905 ifp
= if_lookup_by_name_len_vrf (ifname_tmp
,
906 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
909 /* If such interface does not exist, indicate an error */
913 zebra_interface_if_set_value (s
, ifp
);
919 * format of message for address additon is:
923 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
924 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
933 * | ifc_flags | flags for connected address
941 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
951 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
953 /* Read interface's index. */
954 ifp
->ifindex
= stream_getl (s
);
955 ifp
->status
= stream_getc (s
);
957 /* Read interface's value. */
958 ifp
->flags
= stream_getq (s
);
959 ifp
->ptm_enable
= stream_getc (s
);
960 ifp
->ptm_status
= stream_getc (s
);
961 ifp
->metric
= stream_getl (s
);
962 ifp
->mtu
= stream_getl (s
);
963 ifp
->mtu6
= stream_getl (s
);
964 ifp
->bandwidth
= stream_getl (s
);
965 #ifdef HAVE_STRUCT_SOCKADDR_DL
966 stream_get (&ifp
->sdl
, s
, sizeof (ifp
->sdl_storage
));
968 ifp
->hw_addr_len
= stream_getl (s
);
969 if (ifp
->hw_addr_len
)
970 stream_get (ifp
->hw_addr
, s
, ifp
->hw_addr_len
);
971 #endif /* HAVE_STRUCT_SOCKADDR_DL */
975 memconstant(const void *s
, int c
, size_t n
)
987 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
989 unsigned int ifindex
;
990 struct interface
*ifp
;
991 struct connected
*ifc
;
997 memset (&p
, 0, sizeof(p
));
998 memset (&d
, 0, sizeof(d
));
1000 /* Get interface index. */
1001 ifindex
= stream_getl (s
);
1004 ifp
= if_lookup_by_index_vrf (ifindex
, vrf_id
);
1007 zlog_warn ("zebra_interface_address_read(%s): "
1008 "Can't find interface by ifindex: %d ",
1009 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
? "ADD" : "DELETE"),
1015 ifc_flags
= stream_getc (s
);
1017 /* Fetch interface address. */
1018 family
= p
.family
= stream_getc (s
);
1020 plen
= prefix_blen (&p
);
1021 stream_get (&p
.u
.prefix
, s
, plen
);
1022 p
.prefixlen
= stream_getc (s
);
1024 /* Fetch destination address. */
1025 stream_get (&d
.u
.prefix
, s
, plen
);
1028 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1030 /* N.B. NULL destination pointers are encoded as all zeroes */
1031 ifc
= connected_add_by_prefix(ifp
, &p
,(memconstant(&d
.u
.prefix
,0,plen
) ?
1035 ifc
->flags
= ifc_flags
;
1036 if (ifc
->destination
)
1037 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1038 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1040 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1042 prefix2str (ifc
->address
, buf
, sizeof(buf
));
1043 zlog_warn("warning: interface %s address %s "
1044 "with peer flag set, but no peer address!",
1046 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1052 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1053 ifc
= connected_delete_by_prefix(ifp
, &p
);
1060 * format of message for neighbor connected address is:
1064 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1065 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1080 * | addr_len | len of addr.
1083 struct nbr_connected
*
1084 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1086 unsigned int ifindex
;
1087 struct interface
*ifp
;
1089 struct nbr_connected
*ifc
;
1091 /* Get interface index. */
1092 ifindex
= stream_getl (s
);
1095 ifp
= if_lookup_by_index (ifindex
);
1098 zlog_warn ("zebra_nbr_interface_address_read(%s): "
1099 "Can't find interface by ifindex: %d ",
1100 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
? "ADD" : "DELETE"),
1105 p
.family
= stream_getc (s
);
1106 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1107 p
.prefixlen
= stream_getc (s
);
1109 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1111 /* Currently only supporting P2P links, so any new RA source address is
1112 considered as the replacement of the previously learnt Link-Local address. */
1113 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1115 ifc
= nbr_connected_new ();
1116 ifc
->address
= prefix_new ();
1118 listnode_add (ifp
->nbr_connected
, ifc
);
1121 prefix_copy(ifc
->address
, &p
);
1125 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1127 ifc
= nbr_connected_check(ifp
, &p
);
1129 listnode_delete (ifp
->nbr_connected
, ifc
);
1135 /* Zebra client message read function. */
1137 zclient_read (struct thread
*thread
)
1140 uint16_t length
, command
;
1141 uint8_t marker
, version
;
1143 struct zclient
*zclient
;
1145 /* Get socket to zebra. */
1146 zclient
= THREAD_ARG (thread
);
1147 zclient
->t_read
= NULL
;
1149 /* Read zebra header (if we don't have it already). */
1150 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1153 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1154 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1158 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1159 return zclient_failed(zclient
);
1161 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1163 /* Try again later. */
1164 zclient_event (ZCLIENT_READ
, zclient
);
1167 already
= ZEBRA_HEADER_SIZE
;
1170 /* Reset to read from the beginning of the incoming packet. */
1171 stream_set_getp(zclient
->ibuf
, 0);
1173 /* Fetch header values. */
1174 length
= stream_getw (zclient
->ibuf
);
1175 marker
= stream_getc (zclient
->ibuf
);
1176 version
= stream_getc (zclient
->ibuf
);
1177 vrf_id
= stream_getw (zclient
->ibuf
);
1178 command
= stream_getw (zclient
->ibuf
);
1180 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1182 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1183 __func__
, zclient
->sock
, marker
, version
);
1184 return zclient_failed(zclient
);
1187 if (length
< ZEBRA_HEADER_SIZE
)
1189 zlog_err("%s: socket %d message length %u is less than %d ",
1190 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1191 return zclient_failed(zclient
);
1195 if (length
> STREAM_SIZE(zclient
->ibuf
))
1198 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1199 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1200 ns
= stream_new(length
);
1201 stream_copy(ns
, zclient
->ibuf
);
1202 stream_free (zclient
->ibuf
);
1206 /* Read rest of zebra packet. */
1207 if (already
< length
)
1210 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1211 length
-already
)) == 0) ||
1215 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1216 return zclient_failed(zclient
);
1218 if (nbyte
!= (ssize_t
)(length
-already
))
1220 /* Try again later. */
1221 zclient_event (ZCLIENT_READ
, zclient
);
1226 length
-= ZEBRA_HEADER_SIZE
;
1229 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1233 case ZEBRA_ROUTER_ID_UPDATE
:
1234 if (zclient
->router_id_update
)
1235 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1237 case ZEBRA_INTERFACE_ADD
:
1238 if (zclient
->interface_add
)
1239 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1241 case ZEBRA_INTERFACE_DELETE
:
1242 if (zclient
->interface_delete
)
1243 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1245 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1246 if (zclient
->interface_address_add
)
1247 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1249 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1250 if (zclient
->interface_address_delete
)
1251 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1253 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1254 if (zclient
->interface_bfd_dest_update
)
1255 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1257 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1258 if (zclient
->interface_nbr_address_add
)
1259 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1261 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1262 if (zclient
->interface_nbr_address_delete
)
1263 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1265 case ZEBRA_INTERFACE_UP
:
1266 if (zclient
->interface_up
)
1267 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
1269 case ZEBRA_INTERFACE_DOWN
:
1270 if (zclient
->interface_down
)
1271 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
1273 case ZEBRA_IPV4_ROUTE_ADD
:
1274 if (zclient
->ipv4_route_add
)
1275 (*zclient
->ipv4_route_add
) (command
, zclient
, length
, vrf_id
);
1277 case ZEBRA_IPV4_ROUTE_DELETE
:
1278 if (zclient
->ipv4_route_delete
)
1279 (*zclient
->ipv4_route_delete
) (command
, zclient
, length
, vrf_id
);
1281 case ZEBRA_IPV6_ROUTE_ADD
:
1282 if (zclient
->ipv6_route_add
)
1283 (*zclient
->ipv6_route_add
) (command
, zclient
, length
, vrf_id
);
1285 case ZEBRA_IPV6_ROUTE_DELETE
:
1286 if (zclient
->ipv6_route_delete
)
1287 (*zclient
->ipv6_route_delete
) (command
, zclient
, length
, vrf_id
);
1289 case ZEBRA_NEXTHOP_UPDATE
:
1291 zlog_debug("zclient rcvd nexthop update\n");
1292 if (zclient
->nexthop_update
)
1293 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
1295 case ZEBRA_IMPORT_CHECK_UPDATE
:
1297 zlog_debug("zclient rcvd import check update\n");
1298 if (zclient
->import_check_update
)
1299 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
1301 case ZEBRA_BFD_DEST_REPLAY
:
1302 if (zclient
->bfd_dest_replay
)
1303 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
1305 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
1306 if (zclient
->redistribute_route_ipv4_add
)
1307 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
1309 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
1310 if (zclient
->redistribute_route_ipv4_del
)
1311 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
1313 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
1314 if (zclient
->redistribute_route_ipv6_add
)
1315 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
1317 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
1318 if (zclient
->redistribute_route_ipv6_del
)
1319 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
1325 if (zclient
->sock
< 0)
1326 /* Connection was closed during packet processing. */
1329 /* Register read thread. */
1330 stream_reset(zclient
->ibuf
);
1331 zclient_event (ZCLIENT_READ
, zclient
);
1337 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1338 u_short instance
, vrf_id_t vrf_id
)
1342 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1344 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1346 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1350 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1352 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1356 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1358 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1360 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
1364 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1366 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
1370 if (zclient
->sock
> 0)
1371 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
1376 zclient_redistribute_default (int command
, struct zclient
*zclient
,
1380 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1382 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1384 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
1388 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1390 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
1393 if (zclient
->sock
> 0)
1394 zebra_message_send (zclient
, command
, vrf_id
);
1398 zclient_event (enum event event
, struct zclient
*zclient
)
1402 case ZCLIENT_SCHEDULE
:
1403 if (! zclient
->t_connect
)
1404 zclient
->t_connect
=
1405 thread_add_event (zclient
->master
, zclient_connect
, zclient
, 0);
1407 case ZCLIENT_CONNECT
:
1408 if (zclient
->fail
>= 10)
1411 zlog_debug ("zclient connect schedule interval is %d",
1412 zclient
->fail
< 3 ? 10 : 60);
1413 if (! zclient
->t_connect
)
1414 zclient
->t_connect
=
1415 thread_add_timer (zclient
->master
, zclient_connect
, zclient
,
1416 zclient
->fail
< 3 ? 10 : 60);
1420 thread_add_read (zclient
->master
, zclient_read
, zclient
, zclient
->sock
);
1426 zclient_serv_path_set (char *path
)
1431 zclient_serv_path
= NULL
;
1433 /* test if `path' is socket. don't set it otherwise. */
1434 if (stat(path
, &sb
) == -1)
1436 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
1440 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
1442 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
1446 /* it seems that path is unix socket */
1447 zclient_serv_path
= path
;