1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
40 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
42 /* Zebra client events. */
43 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
45 /* Prototype for event manager. */
46 static void zclient_event (enum event
, struct zclient
*);
48 const char *zclient_serv_path
= NULL
;
50 /* This file local debug flag. */
51 int zclient_debug
= 0;
53 /* Allocate zclient structure. */
55 zclient_new (struct thread_master
*master
)
57 struct zclient
*zclient
;
58 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
60 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
61 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
62 zclient
->wb
= buffer_new(0);
63 zclient
->master
= master
;
68 /* This function is only called when exiting, because
69 many parts of the code do not check for I/O errors, so they could
70 reference an invalid pointer if the structure was ever freed.
72 Free zclient structure. */
74 zclient_free (struct zclient
*zclient
)
77 stream_free(zclient
->ibuf
);
79 stream_free(zclient
->obuf
);
81 buffer_free(zclient
->wb
);
83 XFREE (MTYPE_ZCLIENT
, zclient
);
87 redist_check_instance (struct redist_proto
*red
, u_short instance
)
89 struct listnode
*node
;
95 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
103 redist_add_instance (struct redist_proto
*red
, u_short instance
)
110 red
->instances
= list_new();
112 in
= XMALLOC (MTYPE_REDIST_INST
, sizeof(u_short
));
114 listnode_add (red
->instances
, in
);
118 redist_del_instance (struct redist_proto
*red
, u_short instance
)
122 id
= redist_check_instance (red
, instance
);
126 listnode_delete(red
->instances
, id
);
127 XFREE (MTYPE_REDIST_INST
, id
);
128 if (!red
->instances
->count
)
131 list_free(red
->instances
);
132 red
->instances
= NULL
;
136 /* Stop zebra client services. */
138 zclient_stop (struct zclient
*zclient
)
144 zlog_debug ("zclient stopped");
147 THREAD_OFF(zclient
->t_read
);
148 THREAD_OFF(zclient
->t_connect
);
149 THREAD_OFF(zclient
->t_write
);
152 stream_reset(zclient
->ibuf
);
153 stream_reset(zclient
->obuf
);
155 /* Empty the write buffer. */
156 buffer_reset(zclient
->wb
);
159 if (zclient
->sock
>= 0)
161 close (zclient
->sock
);
166 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
168 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
170 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
171 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
173 redist_del_instance(&zclient
->mi_redist
[afi
][zclient
->redist_default
],
177 vrf_bitmap_free(zclient
->default_information
);
178 zclient
->default_information
= VRF_BITMAP_NULL
;
182 zclient_reset (struct zclient
*zclient
)
186 zclient_stop (zclient
);
188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
189 redist_del_instance (&zclient
->mi_redist
[afi
][zclient
->redist_default
], zclient
->instance
);
191 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
194 #ifdef HAVE_TCP_ZEBRA
196 /* Make socket to zebra daemon. Return zebra socket. */
202 struct sockaddr_in serv
;
204 /* We should think about IPv6 connection. */
205 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
209 /* Make server socket. */
210 memset (&serv
, 0, sizeof (struct sockaddr_in
));
211 serv
.sin_family
= AF_INET
;
212 serv
.sin_port
= htons (ZEBRA_PORT
);
213 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
214 serv
.sin_len
= sizeof (struct sockaddr_in
);
215 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
216 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
218 /* Connect to zebra. */
219 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
223 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
224 errno
, safe_strerror (errno
));
233 /* For sockaddr_un. */
237 zclient_socket_un (const char *path
)
241 struct sockaddr_un addr
;
243 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
247 /* Make server socket. */
248 memset (&addr
, 0, sizeof (struct sockaddr_un
));
249 addr
.sun_family
= AF_UNIX
;
250 strncpy (addr
.sun_path
, path
, strlen (path
));
251 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
252 len
= addr
.sun_len
= SUN_LEN(&addr
);
254 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
255 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
257 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
261 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
262 errno
, safe_strerror (errno
));
269 #endif /* HAVE_TCP_ZEBRA */
272 * Connect to zebra daemon.
273 * @param zclient a pointer to zclient structure
274 * @return socket fd just to make sure that connection established
279 zclient_socket_connect (struct zclient
*zclient
)
281 #ifdef HAVE_TCP_ZEBRA
282 zclient
->sock
= zclient_socket ();
284 zclient
->sock
= zclient_socket_un (zclient_serv_path_get());
286 return zclient
->sock
;
290 zclient_failed(struct zclient
*zclient
)
293 zclient_stop(zclient
);
294 zclient_event(ZCLIENT_CONNECT
, zclient
);
299 zclient_flush_data(struct thread
*thread
)
301 struct zclient
*zclient
= THREAD_ARG(thread
);
303 zclient
->t_write
= NULL
;
304 if (zclient
->sock
< 0)
306 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
309 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
310 __func__
, zclient
->sock
);
311 return zclient_failed(zclient
);
314 zclient
->t_write
= NULL
;
315 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
, zclient
->sock
,
325 zclient_send_message(struct zclient
*zclient
)
327 if (zclient
->sock
< 0)
329 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
330 stream_get_endp(zclient
->obuf
)))
333 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
334 __func__
, zclient
->sock
);
335 return zclient_failed(zclient
);
338 THREAD_OFF(zclient
->t_write
);
341 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
342 zclient
->sock
, &zclient
->t_write
);
349 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
351 /* length placeholder, caller can update */
352 stream_putw (s
, ZEBRA_HEADER_SIZE
);
353 stream_putc (s
, ZEBRA_HEADER_MARKER
);
354 stream_putc (s
, ZSERV_VERSION
);
355 stream_putw (s
, vrf_id
);
356 stream_putw (s
, command
);
360 zclient_read_header (struct stream
*s
, int sock
, u_int16_t
*size
, u_char
*marker
,
361 u_char
*version
, vrf_id_t
*vrf_id
, u_int16_t
*cmd
)
363 if (stream_read (s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
366 *size
= stream_getw (s
) - ZEBRA_HEADER_SIZE
;
367 *marker
= stream_getc (s
);
368 *version
= stream_getc (s
);
369 *vrf_id
= stream_getw (s
);
370 *cmd
= stream_getw (s
);
372 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
)
374 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
375 __func__
, sock
, *marker
, *version
);
379 if (*size
&& stream_read (s
, sock
, *size
) != *size
)
385 /* Send simple Zebra message. */
387 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
391 /* Get zclient output buffer. */
395 /* Send very simple command only Zebra message. */
396 zclient_create_header (s
, command
, vrf_id
);
398 return zclient_send_message(zclient
);
402 zebra_hello_send (struct zclient
*zclient
)
406 if (zclient
->redist_default
)
411 /* The VRF ID in the HELLO message is always 0. */
412 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
413 stream_putc (s
, zclient
->redist_default
);
414 stream_putw (s
, zclient
->instance
);
415 stream_putw_at (s
, 0, stream_get_endp (s
));
416 return zclient_send_message(zclient
);
422 /* Send register requests to zebra daemon for the information in a VRF. */
424 zclient_send_reg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
429 /* zclient is disabled. */
430 if (! zclient
->enable
)
433 /* If not connected to the zebra yet. */
434 if (zclient
->sock
< 0)
438 zlog_debug ("%s: send register messages for VRF %u", __func__
, vrf_id
);
440 /* We need router-id information. */
441 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
443 /* We need interface information. */
444 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
446 /* Set unwanted redistribute route. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
448 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
450 /* Flush all redistribute request. */
451 if (vrf_id
== VRF_DEFAULT
)
452 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
453 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
454 if (zclient
->mi_redist
[afi
][i
].enabled
)
456 struct listnode
*node
;
459 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
460 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
461 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
465 /* Flush all redistribute request. */
466 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
467 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
468 if (i
!= zclient
->redist_default
&&
469 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
470 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
472 /* If default information is needed. */
473 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
474 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
477 /* Send unregister requests to zebra daemon for the information in a VRF. */
479 zclient_send_dereg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
484 /* zclient is disabled. */
485 if (! zclient
->enable
)
488 /* If not connected to the zebra yet. */
489 if (zclient
->sock
< 0)
493 zlog_debug ("%s: send deregister messages for VRF %u", __func__
, vrf_id
);
495 /* We need router-id information. */
496 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
498 /* We need interface information. */
499 zebra_message_send (zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
501 /* Set unwanted redistribute route. */
502 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
503 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
505 /* Flush all redistribute request. */
506 if (vrf_id
== VRF_DEFAULT
)
507 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
508 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
509 if (zclient
->mi_redist
[afi
][i
].enabled
)
511 struct listnode
*node
;
514 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
515 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
516 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
,
520 /* Flush all redistribute request. */
521 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
522 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
523 if (i
!= zclient
->redist_default
&&
524 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
525 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
, 0, vrf_id
);
527 /* If default information is needed. */
528 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
529 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, vrf_id
);
532 /* Send request to zebra daemon to start or stop RA. */
534 zclient_send_interface_radv_req (struct zclient
*zclient
, vrf_id_t vrf_id
,
535 struct interface
*ifp
, int enable
, int ra_interval
)
539 /* zclient is disabled. */
540 if (!zclient
->enable
)
543 /* If not connected to the zebra yet. */
544 if (zclient
->sock
< 0)
547 /* Form and send message. */
552 zclient_create_header (s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
554 zclient_create_header (s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
556 stream_putl (s
, ifp
->ifindex
);
557 stream_putl (s
, ra_interval
);
559 stream_putw_at (s
, 0, stream_get_endp (s
));
561 zclient_send_message(zclient
);
564 /* Make connection to zebra daemon. */
566 zclient_start (struct zclient
*zclient
)
569 zlog_info ("zclient_start is called");
571 /* zclient is disabled. */
572 if (! zclient
->enable
)
575 /* If already connected to the zebra. */
576 if (zclient
->sock
>= 0)
579 /* Check connect thread. */
580 if (zclient
->t_connect
)
583 if (zclient_socket_connect(zclient
) < 0)
586 zlog_debug ("zclient connection fail");
588 zclient_event (ZCLIENT_CONNECT
, zclient
);
592 if (set_nonblocking(zclient
->sock
) < 0)
593 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
595 /* Clear fail count. */
598 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
600 /* Create read thread. */
601 zclient_event (ZCLIENT_READ
, zclient
);
603 zebra_hello_send (zclient
);
605 /* Inform the successful connection. */
606 if (zclient
->zebra_connected
)
607 (*zclient
->zebra_connected
) (zclient
);
612 /* Initialize zebra client. Argument redist_default is unwanted
613 redistribute route type. */
615 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
619 /* Enable zebra client connection by default. */
622 /* Set -1 to the default socket value. */
625 /* Clear redistribution flags. */
626 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
627 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
628 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
630 /* Set unwanted redistribute route. bgpd does not need BGP route
632 zclient
->redist_default
= redist_default
;
633 zclient
->instance
= instance
;
634 /* Pending: make afi(s) an arg. */
635 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
636 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
638 /* Set default-information redistribute to zero. */
639 zclient
->default_information
= vrf_bitmap_init ();;
642 zlog_debug ("zclient_start is called");
644 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
647 /* This function is a wrapper function for calling zclient_start from
648 timer or event thread. */
650 zclient_connect (struct thread
*t
)
652 struct zclient
*zclient
;
654 zclient
= THREAD_ARG (t
);
655 zclient
->t_connect
= NULL
;
658 zlog_debug ("zclient_connect is called");
660 return zclient_start (zclient
);
664 * "xdr_encode"-like interface that allows daemon (client) to send
665 * a message to zebra server for a route that needs to be
666 * added/deleted to the kernel. Info about the route is specified
667 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
668 * the info down the zclient socket using the stream_* functions.
670 * The corresponding read ("xdr_decode") function on the server
671 * side is zread_ipv4_add()/zread_ipv4_delete().
673 * 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
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | Length (2) | Command | Route Type |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 * | ZEBRA Flags | Message Flags | Prefix length |
678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 * | Destination IPv4 Prefix for route |
680 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
686 * described, as per the Nexthop count. Each nexthop described as:
689 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | IPv4 Nexthop address or Interface Index number |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
694 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
695 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
696 * nexthop information is provided, and the message describes a prefix
697 * to blackhole or reject route.
699 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
700 * infrastructure was built around the traditional (32-bit "gate OR
701 * ifindex") nexthop data unit. A special encoding can be used to feed
702 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
703 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
705 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
706 * - .nexthop_num == .ifindex_num
707 * - .nexthop and .ifindex are filled with gate and ifindex parts of
708 * each compound nexthop, both in the same order
710 * zapi_ipv4_route() will produce two nexthop data units for each such
711 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
712 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
714 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
717 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
720 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
722 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
724 * XXX: No attention paid to alignment.
727 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
728 struct zapi_ipv4
*api
)
738 /* Some checks for labeled-unicast. The current expectation is that each
739 * nexthop is accompanied by a label in the case of labeled-unicast.
741 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
742 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
744 /* We expect prefixes installed with labels and the number to match
745 * the number of nexthops.
747 assert (api
->label_num
== api
->nexthop_num
);
750 zclient_create_header (s
, cmd
, api
->vrf_id
);
752 /* Put type and nexthop. */
753 stream_putc (s
, api
->type
);
754 stream_putw (s
, api
->instance
);
755 stream_putl (s
, api
->flags
);
756 stream_putc (s
, api
->message
);
757 stream_putw (s
, api
->safi
);
759 /* Put prefix information. */
760 psize
= PSIZE (p
->prefixlen
);
761 stream_putc (s
, p
->prefixlen
);
762 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
764 /* Nexthop, ifindex, distance and metric information. */
765 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
767 /* traditional 32-bit data units */
768 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
771 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
772 /* XXX assert(api->nexthop_num == 0); */
773 /* XXX assert(api->ifindex_num == 0); */
776 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
778 for (i
= 0; i
< api
->nexthop_num
; i
++)
780 stream_putc (s
, NEXTHOP_TYPE_IPV4
);
781 stream_put_in_addr (s
, api
->nexthop
[i
]);
782 /* For labeled-unicast, each nexthop is followed by label. */
783 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
784 stream_putl (s
, api
->label
[i
]);
786 for (i
= 0; i
< api
->ifindex_num
; i
++)
788 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
789 stream_putl (s
, api
->ifindex
[i
]);
793 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
794 stream_putc (s
, api
->distance
);
795 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
796 stream_putl (s
, api
->metric
);
797 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
798 stream_putl (s
, api
->tag
);
799 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
800 stream_putl (s
, api
->mtu
);
802 /* Put length at the first point of the stream. */
803 stream_putw_at (s
, 0, stream_get_endp (s
));
805 return zclient_send_message(zclient
);
809 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
810 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
820 /* Some checks for labeled-unicast. The current expectation is that each
821 * nexthop is accompanied by a label in the case of labeled-unicast.
823 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
824 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
826 /* We expect prefixes installed with labels and the number to match
827 * the number of nexthops.
829 assert (api
->label_num
== api
->nexthop_num
);
832 zclient_create_header (s
, cmd
, api
->vrf_id
);
834 /* Put type and nexthop. */
835 stream_putc (s
, api
->type
);
836 stream_putw (s
, api
->instance
);
837 stream_putl (s
, api
->flags
);
838 stream_putc (s
, api
->message
);
839 stream_putw (s
, api
->safi
);
841 /* Put prefix information. */
842 psize
= PSIZE (p
->prefixlen
);
843 stream_putc (s
, p
->prefixlen
);
844 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
846 /* Nexthop, ifindex, distance and metric information. */
847 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
849 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
852 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
853 /* XXX assert(api->nexthop_num == 0); */
854 /* XXX assert(api->ifindex_num == 0); */
857 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
859 for (i
= 0; i
< api
->nexthop_num
; i
++)
861 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
862 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
863 /* For labeled-unicast, each nexthop is followed by label. */
864 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
865 stream_putl (s
, api
->label
[i
]);
867 for (i
= 0; i
< api
->ifindex_num
; i
++)
869 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
870 stream_putl (s
, api
->ifindex
[i
]);
874 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
875 stream_putc (s
, api
->distance
);
876 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
877 stream_putl (s
, api
->metric
);
878 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
879 stream_putl (s
, api
->tag
);
880 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
881 stream_putl (s
, api
->mtu
);
883 /* Put length at the first point of the stream. */
884 stream_putw_at (s
, 0, stream_get_endp (s
));
886 return zclient_send_message(zclient
);
890 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
891 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
897 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
898 assert (!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
904 /* Some checks for labeled-unicast. The current expectation is that each
905 * nexthop is accompanied by a label in the case of labeled-unicast.
907 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
908 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
910 /* We expect prefixes installed with labels and the number to match
911 * the number of nexthops.
913 assert (api
->label_num
== api
->nexthop_num
);
916 zclient_create_header (s
, cmd
, api
->vrf_id
);
918 /* Put type and nexthop. */
919 stream_putc (s
, api
->type
);
920 stream_putw (s
, api
->instance
);
921 stream_putl (s
, api
->flags
);
922 stream_putc (s
, api
->message
);
923 stream_putw (s
, api
->safi
);
925 /* Put prefix information. */
926 psize
= PSIZE (p
->prefixlen
);
927 stream_putc (s
, p
->prefixlen
);
928 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
930 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_SRCPFX
))
932 psize
= PSIZE (src_p
->prefixlen
);
933 stream_putc (s
, src_p
->prefixlen
);
934 stream_write (s
, (u_char
*)&src_p
->prefix
, psize
);
937 /* Nexthop, ifindex, distance and metric information. */
938 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
940 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
943 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
944 /* XXX assert(api->nexthop_num == 0); */
945 /* XXX assert(api->ifindex_num == 0); */
948 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
950 for (i
= 0; i
< api
->nexthop_num
; i
++)
952 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
953 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
954 /* For labeled-unicast, each nexthop is followed by label. */
955 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
956 stream_putl (s
, api
->label
[i
]);
958 for (i
= 0; i
< api
->ifindex_num
; i
++)
960 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
961 stream_putl (s
, api
->ifindex
[i
]);
965 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
966 stream_putc (s
, api
->distance
);
967 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
968 stream_putl (s
, api
->metric
);
969 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
970 stream_putl (s
, api
->tag
);
971 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
972 stream_putl (s
, api
->mtu
);
974 /* Put length at the first point of the stream. */
975 stream_putw_at (s
, 0, stream_get_endp (s
));
977 return zclient_send_message(zclient
);
981 zapi_route (u_char cmd
, struct zclient
*zclient
, struct prefix
*p
,
982 struct prefix_ipv6
*src_p
, struct zapi_route
*api
)
988 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
989 assert (!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
995 zclient_create_header (s
, cmd
, api
->vrf_id
);
997 /* Put type and nexthop. */
998 stream_putc (s
, api
->type
);
999 stream_putw (s
, api
->instance
);
1000 stream_putl (s
, api
->flags
);
1001 stream_putc (s
, api
->message
);
1002 stream_putw (s
, api
->safi
);
1004 /* Put prefix information. */
1005 psize
= PSIZE (p
->prefixlen
);
1006 stream_putc (s
, p
->prefixlen
);
1007 stream_write (s
, (u_char
*)&p
->u
.prefix
, psize
);
1009 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_SRCPFX
))
1011 psize
= PSIZE (src_p
->prefixlen
);
1012 stream_putc (s
, src_p
->prefixlen
);
1013 stream_write (s
, (u_char
*)&src_p
->prefix
, psize
);
1016 /* Nexthop, ifindex, distance and metric information. */
1017 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
1019 stream_putc (s
, api
->nexthop_num
);
1021 for (i
= 0; i
< api
->nexthop_num
; i
++)
1023 stream_putc (s
, api
->nexthop
[i
]->type
);
1024 switch (api
->nexthop
[i
]->type
)
1026 case NEXTHOP_TYPE_BLACKHOLE
:
1028 case NEXTHOP_TYPE_IPV4
:
1029 stream_put_in_addr (s
, &api
->nexthop
[i
]->gate
.ipv4
);
1031 /* For labeled-unicast, each nexthop is followed by label. */
1032 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
1033 stream_putl (s
, api
->nexthop
[i
]->nh_label
->label
[0]);
1035 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1036 stream_put_in_addr (s
, &api
->nexthop
[i
]->gate
.ipv4
);
1037 stream_putl (s
, api
->nexthop
[i
]->ifindex
);
1039 case NEXTHOP_TYPE_IFINDEX
:
1040 stream_putl (s
, api
->nexthop
[i
]->ifindex
);
1042 case NEXTHOP_TYPE_IPV6
:
1043 stream_write (s
, (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
, 16);
1045 /* For labeled-unicast, each nexthop is followed by label. */
1046 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
1047 stream_putl (s
, api
->nexthop
[i
]->nh_label
->label
[0]);
1049 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1050 stream_write (s
, (u_char
*)&api
->nexthop
[i
]->gate
.ipv6
, 16);
1051 stream_putl (s
, api
->nexthop
[i
]->ifindex
);
1057 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
1058 stream_putc (s
, api
->distance
);
1059 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
1060 stream_putl (s
, api
->metric
);
1061 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
1062 stream_putl (s
, api
->tag
);
1063 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
1064 stream_putl (s
, api
->mtu
);
1066 /* Put length at the first point of the stream. */
1067 stream_putw_at (s
, 0, stream_get_endp (s
));
1069 return zclient_send_message(zclient
);
1073 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1074 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1075 * then set/unset redist[type] in the client handle (a struct zserv) for the
1079 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1080 u_short instance
, vrf_id_t vrf_id
)
1087 zclient_create_header (s
, command
, vrf_id
);
1088 stream_putc (s
, afi
);
1089 stream_putc (s
, type
);
1090 stream_putw (s
, instance
);
1092 stream_putw_at (s
, 0, stream_get_endp (s
));
1094 return zclient_send_message(zclient
);
1097 /* Get prefix in ZServ format; family should be filled in on prefix */
1099 zclient_stream_get_prefix (struct stream
*s
, struct prefix
*p
)
1101 size_t plen
= prefix_blen (p
);
1108 stream_get (&p
->u
.prefix
, s
, plen
);
1110 p
->prefixlen
= MIN(plen
* 8, c
);
1113 /* Router-id update from zebra daemon. */
1115 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
1117 /* Fetch interface address. */
1118 rid
->family
= stream_getc (s
);
1120 zclient_stream_get_prefix (s
, rid
);
1123 /* Interface addition from zebra daemon. */
1125 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1126 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1128 * 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
1129 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1150 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1153 * | Link Layer Type |
1154 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1155 * | Harware Address Length |
1156 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1157 * | Hardware Address if HW lenght different from 0 |
1158 * | ... max INTERFACE_HWADDR_MAX |
1159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1160 * | Link_params? | Whether a link-params follows: 1 or 0.
1161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1163 * | .... (struct if_link_params). |
1164 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1168 zclient_vrf_add (struct zclient
*zclient
, vrf_id_t vrf_id
)
1171 char vrfname_tmp
[VRF_NAMSIZ
];
1172 struct vrf_data data
;
1174 stream_get (&data
, zclient
->ibuf
, sizeof (struct vrf_data
));
1175 /* Read interface name. */
1176 stream_get (vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1178 /* Lookup/create vrf by vrf_id. */
1179 vrf
= vrf_get (vrf_id
, vrfname_tmp
);
1186 zclient_vrf_delete (struct zclient
*zclient
, vrf_id_t vrf_id
)
1190 /* Lookup vrf by vrf_id. */
1191 vrf
= vrf_lookup_by_id (vrf_id
);
1194 * If a routing protocol doesn't know about a
1195 * vrf that is about to be deleted. There is
1196 * no point in attempting to delete it.
1205 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
1207 struct interface
*ifp
;
1208 char ifname_tmp
[INTERFACE_NAMSIZ
];
1210 /* Read interface name. */
1211 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1213 /* Lookup/create interface by name. */
1214 ifp
= if_get_by_name_len (ifname_tmp
,
1215 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1218 zebra_interface_if_set_value (s
, ifp
);
1224 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1225 * from zebra server. The format of this message is the same as
1226 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1227 * comments for zebra_interface_add_read), except that no sockaddr_dl
1228 * is sent at the tail of the message.
1231 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
1233 struct interface
*ifp
;
1234 char ifname_tmp
[INTERFACE_NAMSIZ
];
1236 /* Read interface name. */
1237 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1239 /* Lookup this by interface index. */
1240 ifp
= if_lookup_by_name_len (ifname_tmp
,
1241 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1245 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1246 ifname_tmp
, vrf_id
);
1250 zebra_interface_if_set_value (s
, ifp
);
1256 link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1262 iflp
->lp_status
= stream_getl (s
);
1263 iflp
->te_metric
= stream_getl (s
);
1264 iflp
->max_bw
= stream_getf (s
);
1265 iflp
->max_rsv_bw
= stream_getf (s
);
1266 uint32_t bwclassnum
= stream_getl (s
);
1269 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1270 iflp
->unrsv_bw
[i
] = stream_getf (s
);
1272 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1273 " - outdated library?",
1274 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1276 iflp
->admin_grp
= stream_getl (s
);
1277 iflp
->rmt_as
= stream_getl (s
);
1278 iflp
->rmt_ip
.s_addr
= stream_get_ipv4 (s
);
1280 iflp
->av_delay
= stream_getl (s
);
1281 iflp
->min_delay
= stream_getl (s
);
1282 iflp
->max_delay
= stream_getl (s
);
1283 iflp
->delay_var
= stream_getl (s
);
1285 iflp
->pkt_loss
= stream_getf (s
);
1286 iflp
->res_bw
= stream_getf (s
);
1287 iflp
->ava_bw
= stream_getf (s
);
1288 iflp
->use_bw
= stream_getf (s
);
1292 zebra_interface_link_params_read (struct stream
*s
)
1294 struct if_link_params
*iflp
;
1299 ifindex
= stream_getl (s
);
1301 struct interface
*ifp
= if_lookup_by_index (ifindex
, VRF_DEFAULT
);
1305 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1310 if ((iflp
= if_link_params_get (ifp
)) == NULL
)
1313 link_params_set_value(s
, iflp
);
1319 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
1321 u_char link_params_status
= 0;
1323 /* Read interface's index. */
1324 ifp
->ifindex
= stream_getl (s
);
1325 ifp
->status
= stream_getc (s
);
1327 /* Read interface's value. */
1328 ifp
->flags
= stream_getq (s
);
1329 ifp
->ptm_enable
= stream_getc (s
);
1330 ifp
->ptm_status
= stream_getc (s
);
1331 ifp
->metric
= stream_getl (s
);
1332 ifp
->speed
= stream_getl (s
);
1333 ifp
->mtu
= stream_getl (s
);
1334 ifp
->mtu6
= stream_getl (s
);
1335 ifp
->bandwidth
= stream_getl (s
);
1336 ifp
->ll_type
= stream_getl (s
);
1337 ifp
->hw_addr_len
= stream_getl (s
);
1338 if (ifp
->hw_addr_len
)
1339 stream_get (ifp
->hw_addr
, s
, MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1341 /* Read Traffic Engineering status */
1342 link_params_status
= stream_getc (s
);
1343 /* Then, Traffic Engineering parameters if any */
1344 if (link_params_status
)
1346 struct if_link_params
*iflp
= if_link_params_get (ifp
);
1347 link_params_set_value(s
, iflp
);
1352 zebra_interface_link_params_write (struct stream
*s
, struct interface
*ifp
)
1355 struct if_link_params
*iflp
;
1358 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1361 iflp
= ifp
->link_params
;
1364 w
+= stream_putl (s
, iflp
->lp_status
);
1366 w
+= stream_putl (s
, iflp
->te_metric
);
1367 w
+= stream_putf (s
, iflp
->max_bw
);
1368 w
+= stream_putf (s
, iflp
->max_rsv_bw
);
1370 w
+= stream_putl (s
, MAX_CLASS_TYPE
);
1371 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1372 w
+= stream_putf (s
, iflp
->unrsv_bw
[i
]);
1374 w
+= stream_putl (s
, iflp
->admin_grp
);
1375 w
+= stream_putl (s
, iflp
->rmt_as
);
1376 w
+= stream_put_in_addr (s
, &iflp
->rmt_ip
);
1378 w
+= stream_putl (s
, iflp
->av_delay
);
1379 w
+= stream_putl (s
, iflp
->min_delay
);
1380 w
+= stream_putl (s
, iflp
->max_delay
);
1381 w
+= stream_putl (s
, iflp
->delay_var
);
1383 w
+= stream_putf (s
, iflp
->pkt_loss
);
1384 w
+= stream_putf (s
, iflp
->res_bw
);
1385 w
+= stream_putf (s
, iflp
->ava_bw
);
1386 w
+= stream_putf (s
, iflp
->use_bw
);
1392 * format of message for address additon is:
1396 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1397 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1406 * | ifc_flags | flags for connected address
1414 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1423 memconstant(const void *s
, int c
, size_t n
)
1425 const u_char
*p
= s
;
1435 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1438 struct interface
*ifp
;
1439 struct connected
*ifc
;
1440 struct prefix p
, d
, *dp
;
1444 memset (&p
, 0, sizeof(p
));
1445 memset (&d
, 0, sizeof(d
));
1447 /* Get interface index. */
1448 ifindex
= stream_getl (s
);
1451 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1454 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1455 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1461 ifc_flags
= stream_getc (s
);
1463 /* Fetch interface address. */
1464 d
.family
= p
.family
= stream_getc (s
);
1465 plen
= prefix_blen (&d
);
1467 zclient_stream_get_prefix (s
, &p
);
1469 /* Fetch destination address. */
1470 stream_get (&d
.u
.prefix
, s
, plen
);
1472 /* N.B. NULL destination pointers are encoded as all zeroes */
1473 dp
= memconstant(&d
.u
.prefix
,0,plen
) ? NULL
: &d
;
1475 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1477 ifc
= connected_lookup_prefix_exact (ifp
, &p
);
1480 /* N.B. NULL destination pointers are encoded as all zeroes */
1481 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1485 ifc
->flags
= ifc_flags
;
1486 if (ifc
->destination
)
1487 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1488 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1490 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1491 char buf
[PREFIX_STRLEN
];
1492 zlog_warn("warning: interface %s address %s "
1493 "with peer flag set, but no peer address!",
1495 prefix2str (ifc
->address
, buf
, sizeof buf
));
1496 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1502 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1503 ifc
= connected_delete_by_prefix(ifp
, &p
);
1510 * format of message for neighbor connected address is:
1514 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1515 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1530 * | addr_len | len of addr.
1533 struct nbr_connected
*
1534 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1536 unsigned int ifindex
;
1537 struct interface
*ifp
;
1539 struct nbr_connected
*ifc
;
1541 /* Get interface index. */
1542 ifindex
= stream_getl (s
);
1545 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1548 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1549 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD" : "DELETE",
1554 p
.family
= stream_getc (s
);
1555 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1556 p
.prefixlen
= stream_getc (s
);
1558 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1560 /* Currently only supporting P2P links, so any new RA source address is
1561 considered as the replacement of the previously learnt Link-Local address. */
1562 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1564 ifc
= nbr_connected_new ();
1565 ifc
->address
= prefix_new ();
1567 listnode_add (ifp
->nbr_connected
, ifc
);
1570 prefix_copy(ifc
->address
, &p
);
1574 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1576 ifc
= nbr_connected_check(ifp
, &p
);
1578 listnode_delete (ifp
->nbr_connected
, ifc
);
1585 zebra_interface_vrf_update_read (struct stream
*s
, vrf_id_t vrf_id
,
1586 vrf_id_t
*new_vrf_id
)
1588 unsigned int ifindex
;
1589 struct interface
*ifp
;
1590 vrf_id_t new_id
= VRF_DEFAULT
;
1592 /* Get interface index. */
1593 ifindex
= stream_getl (s
);
1595 /* Lookup interface. */
1596 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1599 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1604 /* Fetch new VRF Id. */
1605 new_id
= stream_getw (s
);
1607 *new_vrf_id
= new_id
;
1611 /* filter unwanted messages until the expected one arrives */
1613 zclient_read_sync_response (struct zclient
*zclient
, u_int16_t expected_cmd
)
1625 cmd
= expected_cmd
+ 1;
1626 while (ret
== 0 && cmd
!= expected_cmd
)
1631 /* wait until response arrives */
1633 FD_SET (zclient
->sock
, &readfds
);
1634 select (zclient
->sock
+1, &readfds
, NULL
, NULL
, NULL
);
1635 if (!FD_ISSET(zclient
->sock
, &readfds
))
1638 ret
= zclient_read_header (s
, zclient
->sock
, &size
, &marker
, &version
,
1641 zlog_debug ("%s: Response (%d bytes) received", __func__
, size
);
1645 zlog_err ("%s: Invalid Sync Message Reply", __func__
);
1652 * Connect to label manager in a syncronous way
1654 * It first writes the request to zcient output buffer and then
1655 * immediately reads the answer from the input buffer.
1657 * @param zclient Zclient used to connect to label manager (zebra)
1658 * @result Result of response
1661 lm_label_manager_connect (struct zclient
*zclient
)
1668 zlog_debug ("Connecting to Label Manager");
1670 if (zclient
->sock
< 0)
1676 zclient_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1679 stream_putc (s
, zclient
->redist_default
);
1681 stream_putw (s
, zclient
->instance
);
1683 /* Put length at the first point of the stream. */
1684 stream_putw_at(s
, 0, stream_get_endp(s
));
1686 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1689 zlog_err ("%s: can't write to zclient->sock", __func__
);
1690 close (zclient
->sock
);
1696 zlog_err ("%s: zclient->sock connection closed", __func__
);
1697 close (zclient
->sock
);
1702 zlog_debug ("%s: Label manager connect request (%d bytes) sent", __func__
, ret
);
1705 if (zclient_read_sync_response (zclient
, ZEBRA_LABEL_MANAGER_CONNECT
) != 0)
1710 result
= stream_getc(s
);
1712 zlog_debug ("%s: Label Manager connect response received, result %u",
1719 * Function to request a label chunk in a syncronous way
1721 * It first writes the request to zlcient output buffer and then
1722 * immediately reads the answer from the input buffer.
1724 * @param zclient Zclient used to connect to label manager (zebra)
1725 * @param keep Avoid garbage collection
1726 * @param chunk_size Amount of labels requested
1727 * @param start To write first assigned chunk label to
1728 * @param end To write last assigned chunk label to
1729 * @result 0 on success, -1 otherwise
1732 lm_get_label_chunk (struct zclient
*zclient
, u_char keep
, uint32_t chunk_size
,
1733 uint32_t *start
, uint32_t *end
)
1737 u_char response_keep
;
1740 zlog_debug ("Getting Label Chunk");
1742 if (zclient
->sock
< 0)
1748 zclient_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1750 stream_putc (s
, keep
);
1752 stream_putl (s
, chunk_size
);
1753 /* Put length at the first point of the stream. */
1754 stream_putw_at(s
, 0, stream_get_endp(s
));
1756 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1759 zlog_err ("%s: can't write to zclient->sock", __func__
);
1760 close (zclient
->sock
);
1766 zlog_err ("%s: zclient->sock connection closed", __func__
);
1767 close (zclient
->sock
);
1772 zlog_debug ("%s: Label chunk request (%d bytes) sent", __func__
, ret
);
1775 if (zclient_read_sync_response (zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
1780 response_keep
= stream_getc(s
);
1781 /* start and end labels */
1782 *start
= stream_getl(s
);
1783 *end
= stream_getl(s
);
1785 /* not owning this response */
1786 if (keep
!= response_keep
)
1788 zlog_err ("%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1789 __func__
, *start
, *end
, keep
, response_keep
);
1793 || *start
< MPLS_MIN_UNRESERVED_LABEL
1794 || *end
> MPLS_MAX_UNRESERVED_LABEL
)
1796 zlog_err ("%s: Invalid Label chunk: %u - %u", __func__
,
1802 zlog_debug ("Label Chunk assign: %u - %u (%u) ",
1803 *start
, *end
, response_keep
);
1809 * Function to release a label chunk
1811 * @param zclient Zclient used to connect to label manager (zebra)
1812 * @param start First label of chunk
1813 * @param end Last label of chunk
1814 * @result 0 on success, -1 otherwise
1817 lm_release_label_chunk (struct zclient
*zclient
, uint32_t start
, uint32_t end
)
1823 zlog_debug ("Releasing Label Chunk");
1825 if (zclient
->sock
< 0)
1831 zclient_create_header (s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1834 stream_putl (s
, start
);
1836 stream_putl (s
, end
);
1838 /* Put length at the first point of the stream. */
1839 stream_putw_at(s
, 0, stream_get_endp(s
));
1841 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1844 zlog_err ("%s: can't write to zclient->sock", __func__
);
1845 close (zclient
->sock
);
1851 zlog_err ("%s: zclient->sock connection closed", __func__
);
1852 close (zclient
->sock
);
1860 /* Zebra client message read function. */
1862 zclient_read (struct thread
*thread
)
1865 uint16_t length
, command
;
1866 uint8_t marker
, version
;
1868 struct zclient
*zclient
;
1870 /* Get socket to zebra. */
1871 zclient
= THREAD_ARG (thread
);
1872 zclient
->t_read
= NULL
;
1874 /* Read zebra header (if we don't have it already). */
1875 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1878 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1879 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1883 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1884 return zclient_failed(zclient
);
1886 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1888 /* Try again later. */
1889 zclient_event (ZCLIENT_READ
, zclient
);
1892 already
= ZEBRA_HEADER_SIZE
;
1895 /* Reset to read from the beginning of the incoming packet. */
1896 stream_set_getp(zclient
->ibuf
, 0);
1898 /* Fetch header values. */
1899 length
= stream_getw (zclient
->ibuf
);
1900 marker
= stream_getc (zclient
->ibuf
);
1901 version
= stream_getc (zclient
->ibuf
);
1902 vrf_id
= stream_getw (zclient
->ibuf
);
1903 command
= stream_getw (zclient
->ibuf
);
1905 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1907 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1908 __func__
, zclient
->sock
, marker
, version
);
1909 return zclient_failed(zclient
);
1912 if (length
< ZEBRA_HEADER_SIZE
)
1914 zlog_err("%s: socket %d message length %u is less than %d ",
1915 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1916 return zclient_failed(zclient
);
1920 if (length
> STREAM_SIZE(zclient
->ibuf
))
1923 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1924 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1925 ns
= stream_new(length
);
1926 stream_copy(ns
, zclient
->ibuf
);
1927 stream_free (zclient
->ibuf
);
1931 /* Read rest of zebra packet. */
1932 if (already
< length
)
1935 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1936 length
-already
)) == 0) ||
1940 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1941 return zclient_failed(zclient
);
1943 if (nbyte
!= (ssize_t
)(length
-already
))
1945 /* Try again later. */
1946 zclient_event (ZCLIENT_READ
, zclient
);
1951 length
-= ZEBRA_HEADER_SIZE
;
1954 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1958 case ZEBRA_ROUTER_ID_UPDATE
:
1959 if (zclient
->router_id_update
)
1960 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1963 zclient_vrf_add (zclient
, vrf_id
);
1965 case ZEBRA_VRF_DELETE
:
1966 zclient_vrf_delete (zclient
, vrf_id
);
1968 case ZEBRA_INTERFACE_ADD
:
1969 if (zclient
->interface_add
)
1970 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1972 case ZEBRA_INTERFACE_DELETE
:
1973 if (zclient
->interface_delete
)
1974 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1976 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1977 if (zclient
->interface_address_add
)
1978 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1980 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1981 if (zclient
->interface_address_delete
)
1982 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1984 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1985 if (zclient
->interface_bfd_dest_update
)
1986 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1988 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1989 if (zclient
->interface_nbr_address_add
)
1990 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1992 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1993 if (zclient
->interface_nbr_address_delete
)
1994 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1996 case ZEBRA_INTERFACE_UP
:
1997 if (zclient
->interface_up
)
1998 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
2000 case ZEBRA_INTERFACE_DOWN
:
2001 if (zclient
->interface_down
)
2002 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
2004 case ZEBRA_INTERFACE_VRF_UPDATE
:
2005 if (zclient
->interface_vrf_update
)
2006 (*zclient
->interface_vrf_update
) (command
, zclient
, length
, vrf_id
);
2008 case ZEBRA_NEXTHOP_UPDATE
:
2010 zlog_debug("zclient rcvd nexthop update\n");
2011 if (zclient
->nexthop_update
)
2012 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
2014 case ZEBRA_IMPORT_CHECK_UPDATE
:
2016 zlog_debug("zclient rcvd import check update\n");
2017 if (zclient
->import_check_update
)
2018 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
2020 case ZEBRA_BFD_DEST_REPLAY
:
2021 if (zclient
->bfd_dest_replay
)
2022 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
2024 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
2025 if (zclient
->redistribute_route_ipv4_add
)
2026 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
2028 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
2029 if (zclient
->redistribute_route_ipv4_del
)
2030 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
2032 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
2033 if (zclient
->redistribute_route_ipv6_add
)
2034 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
2036 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
2037 if (zclient
->redistribute_route_ipv6_del
)
2038 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
2040 case ZEBRA_INTERFACE_LINK_PARAMS
:
2041 if (zclient
->interface_link_params
)
2042 (*zclient
->interface_link_params
) (command
, zclient
, length
);
2044 case ZEBRA_FEC_UPDATE
:
2046 zlog_debug("zclient rcvd fec update\n");
2047 if (zclient
->fec_update
)
2048 (*zclient
->fec_update
) (command
, zclient
, length
);
2051 if (zclient
->local_vni_add
)
2052 (*zclient
->local_vni_add
) (command
, zclient
, length
, vrf_id
);
2055 if (zclient
->local_vni_del
)
2056 (*zclient
->local_vni_del
) (command
, zclient
, length
, vrf_id
);
2058 case ZEBRA_MACIP_ADD
:
2059 if (zclient
->local_macip_add
)
2060 (*zclient
->local_macip_add
) (command
, zclient
, length
, vrf_id
);
2062 case ZEBRA_MACIP_DEL
:
2063 if (zclient
->local_macip_del
)
2064 (*zclient
->local_macip_del
) (command
, zclient
, length
, vrf_id
);
2070 if (zclient
->sock
< 0)
2071 /* Connection was closed during packet processing. */
2074 /* Register read thread. */
2075 stream_reset(zclient
->ibuf
);
2076 zclient_event (ZCLIENT_READ
, zclient
);
2082 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
2083 u_short instance
, vrf_id_t vrf_id
)
2087 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
2089 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
2091 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
2095 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
2097 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
2101 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
2103 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
2105 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
2109 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
2111 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
2115 if (zclient
->sock
> 0)
2116 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
2121 zclient_redistribute_default (int command
, struct zclient
*zclient
,
2125 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
2127 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
2129 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
2133 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
2135 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
2138 if (zclient
->sock
> 0)
2139 zebra_message_send (zclient
, command
, vrf_id
);
2143 zclient_event (enum event event
, struct zclient
*zclient
)
2147 case ZCLIENT_SCHEDULE
:
2148 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2149 &zclient
->t_connect
);
2151 case ZCLIENT_CONNECT
:
2153 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
2154 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2155 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2156 zclient
->fail
< 3 ? 10 : 60, &zclient
->t_connect
);
2159 zclient
->t_read
= NULL
;
2160 thread_add_read(zclient
->master
, zclient_read
, zclient
, zclient
->sock
,
2166 const char *zclient_serv_path_get()
2168 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
2172 zclient_serv_path_set (char *path
)
2177 zclient_serv_path
= NULL
;
2179 /* test if `path' is socket. don't set it otherwise. */
2180 if (stat(path
, &sb
) == -1)
2182 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
2186 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
2188 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
2192 /* it seems that path is unix socket */
2193 zclient_serv_path
= path
;