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,
38 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
39 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
41 /* Zebra client events. */
42 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
44 /* Prototype for event manager. */
45 static void zclient_event (enum event
, struct zclient
*);
47 const char *zclient_serv_path
= NULL
;
49 /* This file local debug flag. */
50 int zclient_debug
= 0;
52 /* Allocate zclient structure. */
54 zclient_new (struct thread_master
*master
)
56 struct zclient
*zclient
;
57 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
59 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
60 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
61 zclient
->wb
= buffer_new(0);
62 zclient
->master
= master
;
67 /* This function is only called when exiting, because
68 many parts of the code do not check for I/O errors, so they could
69 reference an invalid pointer if the structure was ever freed.
71 Free zclient structure. */
73 zclient_free (struct zclient
*zclient
)
76 stream_free(zclient
->ibuf
);
78 stream_free(zclient
->obuf
);
80 buffer_free(zclient
->wb
);
82 XFREE (MTYPE_ZCLIENT
, zclient
);
86 redist_check_instance (struct redist_proto
*red
, u_short instance
)
88 struct listnode
*node
;
94 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
102 redist_add_instance (struct redist_proto
*red
, u_short instance
)
109 red
->instances
= list_new();
111 in
= XMALLOC (MTYPE_REDIST_INST
, sizeof(u_short
));
113 listnode_add (red
->instances
, in
);
117 redist_del_instance (struct redist_proto
*red
, u_short instance
)
121 id
= redist_check_instance (red
, instance
);
125 listnode_delete(red
->instances
, id
);
126 XFREE (MTYPE_REDIST_INST
, id
);
127 if (!red
->instances
->count
)
130 list_free(red
->instances
);
131 red
->instances
= NULL
;
135 /* Stop zebra client services. */
137 zclient_stop (struct zclient
*zclient
)
143 zlog_debug ("zclient stopped");
146 THREAD_OFF(zclient
->t_read
);
147 THREAD_OFF(zclient
->t_connect
);
148 THREAD_OFF(zclient
->t_write
);
151 stream_reset(zclient
->ibuf
);
152 stream_reset(zclient
->obuf
);
154 /* Empty the write buffer. */
155 buffer_reset(zclient
->wb
);
158 if (zclient
->sock
>= 0)
160 close (zclient
->sock
);
165 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
167 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
169 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
170 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
172 redist_del_instance(&zclient
->mi_redist
[afi
][zclient
->redist_default
],
176 vrf_bitmap_free(zclient
->default_information
);
177 zclient
->default_information
= VRF_BITMAP_NULL
;
181 zclient_reset (struct zclient
*zclient
)
185 zclient_stop (zclient
);
187 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
188 redist_del_instance (&zclient
->mi_redist
[afi
][zclient
->redist_default
], zclient
->instance
);
190 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
193 #ifdef HAVE_TCP_ZEBRA
195 /* Make socket to zebra daemon. Return zebra socket. */
201 struct sockaddr_in serv
;
203 /* We should think about IPv6 connection. */
204 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
208 /* Make server socket. */
209 memset (&serv
, 0, sizeof (struct sockaddr_in
));
210 serv
.sin_family
= AF_INET
;
211 serv
.sin_port
= htons (ZEBRA_PORT
);
212 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
213 serv
.sin_len
= sizeof (struct sockaddr_in
);
214 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
215 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
217 /* Connect to zebra. */
218 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
222 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
223 errno
, safe_strerror (errno
));
232 /* For sockaddr_un. */
236 zclient_socket_un (const char *path
)
240 struct sockaddr_un addr
;
242 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
246 /* Make server socket. */
247 memset (&addr
, 0, sizeof (struct sockaddr_un
));
248 addr
.sun_family
= AF_UNIX
;
249 strncpy (addr
.sun_path
, path
, strlen (path
));
250 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
251 len
= addr
.sun_len
= SUN_LEN(&addr
);
253 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
254 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
256 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
260 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
261 errno
, safe_strerror (errno
));
268 #endif /* HAVE_TCP_ZEBRA */
271 * Connect to zebra daemon.
272 * @param zclient a pointer to zclient structure
273 * @return socket fd just to make sure that connection established
278 zclient_socket_connect (struct zclient
*zclient
)
280 #ifdef HAVE_TCP_ZEBRA
281 zclient
->sock
= zclient_socket ();
283 zclient
->sock
= zclient_socket_un (zclient_serv_path_get());
285 return zclient
->sock
;
289 zclient_failed(struct zclient
*zclient
)
292 zclient_stop(zclient
);
293 zclient_event(ZCLIENT_CONNECT
, zclient
);
298 zclient_flush_data(struct thread
*thread
)
300 struct zclient
*zclient
= THREAD_ARG(thread
);
302 zclient
->t_write
= NULL
;
303 if (zclient
->sock
< 0)
305 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
308 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
309 __func__
, zclient
->sock
);
310 return zclient_failed(zclient
);
313 zclient
->t_write
= thread_add_write(zclient
->master
, zclient_flush_data
,
314 zclient
, zclient
->sock
);
323 zclient_send_message(struct zclient
*zclient
)
325 if (zclient
->sock
< 0)
327 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
328 stream_get_endp(zclient
->obuf
)))
331 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
332 __func__
, zclient
->sock
);
333 return zclient_failed(zclient
);
336 THREAD_OFF(zclient
->t_write
);
339 THREAD_WRITE_ON(zclient
->master
, zclient
->t_write
,
340 zclient_flush_data
, zclient
, zclient
->sock
);
347 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
349 /* length placeholder, caller can update */
350 stream_putw (s
, ZEBRA_HEADER_SIZE
);
351 stream_putc (s
, ZEBRA_HEADER_MARKER
);
352 stream_putc (s
, ZSERV_VERSION
);
353 stream_putw (s
, vrf_id
);
354 stream_putw (s
, command
);
358 zclient_read_header (struct stream
*s
, int sock
, u_int16_t
*size
, u_char
*marker
,
359 u_char
*version
, vrf_id_t
*vrf_id
, u_int16_t
*cmd
)
361 if (stream_read (s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
364 *size
= stream_getw (s
) - ZEBRA_HEADER_SIZE
;
365 *marker
= stream_getc (s
);
366 *version
= stream_getc (s
);
367 *vrf_id
= stream_getw (s
);
368 *cmd
= stream_getw (s
);
370 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
)
372 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
373 __func__
, sock
, *marker
, *version
);
377 if (*size
&& stream_read (s
, sock
, *size
) != *size
)
383 /* Send simple Zebra message. */
385 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
389 /* Get zclient output buffer. */
393 /* Send very simple command only Zebra message. */
394 zclient_create_header (s
, command
, vrf_id
);
396 return zclient_send_message(zclient
);
400 zebra_hello_send (struct zclient
*zclient
)
404 if (zclient
->redist_default
)
409 /* The VRF ID in the HELLO message is always 0. */
410 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
411 stream_putc (s
, zclient
->redist_default
);
412 stream_putw (s
, zclient
->instance
);
413 stream_putw_at (s
, 0, stream_get_endp (s
));
414 return zclient_send_message(zclient
);
420 /* Send register requests to zebra daemon for the information in a VRF. */
422 zclient_send_reg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
427 /* zclient is disabled. */
428 if (! zclient
->enable
)
431 /* If not connected to the zebra yet. */
432 if (zclient
->sock
< 0)
436 zlog_debug ("%s: send register messages for VRF %u", __func__
, vrf_id
);
438 /* We need router-id information. */
439 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
441 /* We need interface information. */
442 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
444 /* Set unwanted redistribute route. */
445 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
446 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
448 /* Flush all redistribute request. */
449 if (vrf_id
== VRF_DEFAULT
)
450 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
451 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
452 if (zclient
->mi_redist
[afi
][i
].enabled
)
454 struct listnode
*node
;
457 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
458 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
459 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
463 /* Flush all redistribute request. */
464 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
465 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
466 if (i
!= zclient
->redist_default
&&
467 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
468 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
470 /* If default information is needed. */
471 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
472 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
475 /* Send unregister requests to zebra daemon for the information in a VRF. */
477 zclient_send_dereg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
482 /* zclient is disabled. */
483 if (! zclient
->enable
)
486 /* If not connected to the zebra yet. */
487 if (zclient
->sock
< 0)
491 zlog_debug ("%s: send deregister messages for VRF %u", __func__
, vrf_id
);
493 /* We need router-id information. */
494 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
496 /* We need interface information. */
497 zebra_message_send (zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
499 /* Set unwanted redistribute route. */
500 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
501 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
503 /* Flush all redistribute request. */
504 if (vrf_id
== VRF_DEFAULT
)
505 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
506 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
507 if (zclient
->mi_redist
[afi
][i
].enabled
)
509 struct listnode
*node
;
512 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
513 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
514 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
,
518 /* Flush all redistribute request. */
519 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
520 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
521 if (i
!= zclient
->redist_default
&&
522 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
523 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
, 0, vrf_id
);
525 /* If default information is needed. */
526 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
527 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, vrf_id
);
530 /* Send request to zebra daemon to start or stop RA. */
532 zclient_send_interface_radv_req (struct zclient
*zclient
, vrf_id_t vrf_id
,
533 struct interface
*ifp
, int enable
, int ra_interval
)
537 /* zclient is disabled. */
538 if (!zclient
->enable
)
541 /* If not connected to the zebra yet. */
542 if (zclient
->sock
< 0)
545 /* Form and send message. */
550 zclient_create_header (s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
552 zclient_create_header (s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
554 stream_putl (s
, ifp
->ifindex
);
555 stream_putl (s
, ra_interval
);
557 stream_putw_at (s
, 0, stream_get_endp (s
));
559 zclient_send_message(zclient
);
562 /* Make connection to zebra daemon. */
564 zclient_start (struct zclient
*zclient
)
567 zlog_info ("zclient_start is called");
569 /* zclient is disabled. */
570 if (! zclient
->enable
)
573 /* If already connected to the zebra. */
574 if (zclient
->sock
>= 0)
577 /* Check connect thread. */
578 if (zclient
->t_connect
)
581 if (zclient_socket_connect(zclient
) < 0)
584 zlog_debug ("zclient connection fail");
586 zclient_event (ZCLIENT_CONNECT
, zclient
);
590 if (set_nonblocking(zclient
->sock
) < 0)
591 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
593 /* Clear fail count. */
596 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
598 /* Create read thread. */
599 zclient_event (ZCLIENT_READ
, zclient
);
601 zebra_hello_send (zclient
);
603 /* Inform the successful connection. */
604 if (zclient
->zebra_connected
)
605 (*zclient
->zebra_connected
) (zclient
);
610 /* Initialize zebra client. Argument redist_default is unwanted
611 redistribute route type. */
613 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
617 /* Enable zebra client connection by default. */
620 /* Set -1 to the default socket value. */
623 /* Clear redistribution flags. */
624 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
625 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
626 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
628 /* Set unwanted redistribute route. bgpd does not need BGP route
630 zclient
->redist_default
= redist_default
;
631 zclient
->instance
= instance
;
632 /* Pending: make afi(s) an arg. */
633 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
634 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
636 /* Set default-information redistribute to zero. */
637 zclient
->default_information
= vrf_bitmap_init ();;
640 zlog_debug ("zclient_start is called");
642 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
645 /* This function is a wrapper function for calling zclient_start from
646 timer or event thread. */
648 zclient_connect (struct thread
*t
)
650 struct zclient
*zclient
;
652 zclient
= THREAD_ARG (t
);
653 zclient
->t_connect
= NULL
;
656 zlog_debug ("zclient_connect is called");
658 return zclient_start (zclient
);
662 * "xdr_encode"-like interface that allows daemon (client) to send
663 * a message to zebra server for a route that needs to be
664 * added/deleted to the kernel. Info about the route is specified
665 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
666 * the info down the zclient socket using the stream_* functions.
668 * The corresponding read ("xdr_decode") function on the server
669 * side is zread_ipv4_add()/zread_ipv4_delete().
671 * 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
672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673 * | Length (2) | Command | Route Type |
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * | ZEBRA Flags | Message Flags | Prefix length |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 * | Destination IPv4 Prefix for route |
678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
683 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
684 * described, as per the Nexthop count. Each nexthop described as:
687 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | IPv4 Nexthop address or Interface Index number |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
692 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
693 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
694 * nexthop information is provided, and the message describes a prefix
695 * to blackhole or reject route.
697 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
698 * infrastructure was built around the traditional (32-bit "gate OR
699 * ifindex") nexthop data unit. A special encoding can be used to feed
700 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
701 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
703 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
704 * - .nexthop_num == .ifindex_num
705 * - .nexthop and .ifindex are filled with gate and ifindex parts of
706 * each compound nexthop, both in the same order
708 * zapi_ipv4_route() will produce two nexthop data units for each such
709 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
710 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
712 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
715 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
718 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
720 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
722 * XXX: No attention paid to alignment.
725 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
726 struct zapi_ipv4
*api
)
736 /* Some checks for labeled-unicast. The current expectation is that each
737 * nexthop is accompanied by a label in the case of labeled-unicast.
739 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
740 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
742 /* We expect prefixes installed with labels and the number to match
743 * the number of nexthops.
745 assert (api
->label_num
== api
->nexthop_num
);
748 zclient_create_header (s
, cmd
, api
->vrf_id
);
750 /* Put type and nexthop. */
751 stream_putc (s
, api
->type
);
752 stream_putw (s
, api
->instance
);
753 stream_putl (s
, api
->flags
);
754 stream_putc (s
, api
->message
);
755 stream_putw (s
, api
->safi
);
757 /* Put prefix information. */
758 psize
= PSIZE (p
->prefixlen
);
759 stream_putc (s
, p
->prefixlen
);
760 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
762 /* Nexthop, ifindex, distance and metric information. */
763 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
765 /* traditional 32-bit data units */
766 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
769 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
770 /* XXX assert(api->nexthop_num == 0); */
771 /* XXX assert(api->ifindex_num == 0); */
774 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
776 for (i
= 0; i
< api
->nexthop_num
; i
++)
778 stream_putc (s
, NEXTHOP_TYPE_IPV4
);
779 stream_put_in_addr (s
, api
->nexthop
[i
]);
780 /* For labeled-unicast, each nexthop is followed by label. */
781 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
782 stream_putl (s
, api
->label
[i
]);
784 for (i
= 0; i
< api
->ifindex_num
; i
++)
786 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
787 stream_putl (s
, api
->ifindex
[i
]);
791 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
792 stream_putc (s
, api
->distance
);
793 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
794 stream_putl (s
, api
->metric
);
795 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
796 stream_putl (s
, api
->tag
);
797 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
798 stream_putl (s
, api
->mtu
);
800 /* Put length at the first point of the stream. */
801 stream_putw_at (s
, 0, stream_get_endp (s
));
803 return zclient_send_message(zclient
);
807 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
808 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
818 /* Some checks for labeled-unicast. The current expectation is that each
819 * nexthop is accompanied by a label in the case of labeled-unicast.
821 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
822 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
824 /* We expect prefixes installed with labels and the number to match
825 * the number of nexthops.
827 assert (api
->label_num
== api
->nexthop_num
);
830 zclient_create_header (s
, cmd
, api
->vrf_id
);
832 /* Put type and nexthop. */
833 stream_putc (s
, api
->type
);
834 stream_putw (s
, api
->instance
);
835 stream_putl (s
, api
->flags
);
836 stream_putc (s
, api
->message
);
837 stream_putw (s
, api
->safi
);
839 /* Put prefix information. */
840 psize
= PSIZE (p
->prefixlen
);
841 stream_putc (s
, p
->prefixlen
);
842 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
844 /* Nexthop, ifindex, distance and metric information. */
845 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
847 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
850 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
851 /* XXX assert(api->nexthop_num == 0); */
852 /* XXX assert(api->ifindex_num == 0); */
855 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
857 for (i
= 0; i
< api
->nexthop_num
; i
++)
859 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
860 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
861 /* For labeled-unicast, each nexthop is followed by label. */
862 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
863 stream_putl (s
, api
->label
[i
]);
865 for (i
= 0; i
< api
->ifindex_num
; i
++)
867 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
868 stream_putl (s
, api
->ifindex
[i
]);
872 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
873 stream_putc (s
, api
->distance
);
874 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
875 stream_putl (s
, api
->metric
);
876 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
877 stream_putl (s
, api
->tag
);
878 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
879 stream_putl (s
, api
->mtu
);
881 /* Put length at the first point of the stream. */
882 stream_putw_at (s
, 0, stream_get_endp (s
));
884 return zclient_send_message(zclient
);
888 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
889 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
895 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
896 assert (!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
902 /* Some checks for labeled-unicast. The current expectation is that each
903 * nexthop is accompanied by a label in the case of labeled-unicast.
905 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
906 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
908 /* We expect prefixes installed with labels and the number to match
909 * the number of nexthops.
911 assert (api
->label_num
== api
->nexthop_num
);
914 zclient_create_header (s
, cmd
, api
->vrf_id
);
916 /* Put type and nexthop. */
917 stream_putc (s
, api
->type
);
918 stream_putw (s
, api
->instance
);
919 stream_putl (s
, api
->flags
);
920 stream_putc (s
, api
->message
);
921 stream_putw (s
, api
->safi
);
923 /* Put prefix information. */
924 psize
= PSIZE (p
->prefixlen
);
925 stream_putc (s
, p
->prefixlen
);
926 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
928 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_SRCPFX
))
930 psize
= PSIZE (src_p
->prefixlen
);
931 stream_putc (s
, src_p
->prefixlen
);
932 stream_write (s
, (u_char
*)&src_p
->prefix
, psize
);
935 /* Nexthop, ifindex, distance and metric information. */
936 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
938 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
941 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
942 /* XXX assert(api->nexthop_num == 0); */
943 /* XXX assert(api->ifindex_num == 0); */
946 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
948 for (i
= 0; i
< api
->nexthop_num
; i
++)
950 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
951 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
952 /* For labeled-unicast, each nexthop is followed by label. */
953 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
954 stream_putl (s
, api
->label
[i
]);
956 for (i
= 0; i
< api
->ifindex_num
; i
++)
958 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
959 stream_putl (s
, api
->ifindex
[i
]);
963 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
964 stream_putc (s
, api
->distance
);
965 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
966 stream_putl (s
, api
->metric
);
967 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
968 stream_putl (s
, api
->tag
);
969 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
970 stream_putl (s
, api
->mtu
);
972 /* Put length at the first point of the stream. */
973 stream_putw_at (s
, 0, stream_get_endp (s
));
975 return zclient_send_message(zclient
);
979 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
980 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
981 * then set/unset redist[type] in the client handle (a struct zserv) for the
985 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
986 u_short instance
, vrf_id_t vrf_id
)
993 zclient_create_header (s
, command
, vrf_id
);
994 stream_putc (s
, afi
);
995 stream_putc (s
, type
);
996 stream_putw (s
, instance
);
998 stream_putw_at (s
, 0, stream_get_endp (s
));
1000 return zclient_send_message(zclient
);
1003 /* Get prefix in ZServ format; family should be filled in on prefix */
1005 zclient_stream_get_prefix (struct stream
*s
, struct prefix
*p
)
1007 size_t plen
= prefix_blen (p
);
1014 stream_get (&p
->u
.prefix
, s
, plen
);
1016 p
->prefixlen
= MIN(plen
* 8, c
);
1019 /* Router-id update from zebra daemon. */
1021 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
1023 /* Fetch interface address. */
1024 rid
->family
= stream_getc (s
);
1026 zclient_stream_get_prefix (s
, rid
);
1029 /* Interface addition from zebra daemon. */
1031 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1032 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1034 * 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
1035 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1041 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1043 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1045 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1048 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1050 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1052 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1054 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1056 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1058 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1059 * | Link Layer Type |
1060 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1061 * | Harware Address Length |
1062 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1063 * | Hardware Address if HW lenght different from 0 |
1064 * | ... max INTERFACE_HWADDR_MAX |
1065 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1066 * | Link_params? | Whether a link-params follows: 1 or 0.
1067 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1068 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1069 * | .... (struct if_link_params). |
1070 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1074 zclient_vrf_add (struct zclient
*zclient
, vrf_id_t vrf_id
)
1077 char vrfname_tmp
[VRF_NAMSIZ
];
1079 /* Read interface name. */
1080 stream_get (vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1082 /* Lookup/create vrf by vrf_id. */
1083 vrf
= vrf_get (vrf_id
, vrfname_tmp
);
1089 zclient_vrf_delete (struct zclient
*zclient
, vrf_id_t vrf_id
)
1093 /* Lookup vrf by vrf_id. */
1094 vrf
= vrf_lookup_by_id (vrf_id
);
1097 * If a routing protocol doesn't know about a
1098 * vrf that is about to be deleted. There is
1099 * no point in attempting to delete it.
1108 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
1110 struct interface
*ifp
;
1111 char ifname_tmp
[INTERFACE_NAMSIZ
];
1113 /* Read interface name. */
1114 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1116 /* Lookup/create interface by name. */
1117 ifp
= if_get_by_name_len (ifname_tmp
,
1118 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1121 zebra_interface_if_set_value (s
, ifp
);
1127 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1128 * from zebra server. The format of this message is the same as
1129 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1130 * comments for zebra_interface_add_read), except that no sockaddr_dl
1131 * is sent at the tail of the message.
1134 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
1136 struct interface
*ifp
;
1137 char ifname_tmp
[INTERFACE_NAMSIZ
];
1139 /* Read interface name. */
1140 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1142 /* Lookup this by interface index. */
1143 ifp
= if_lookup_by_name_len (ifname_tmp
,
1144 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1148 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1149 ifname_tmp
, vrf_id
);
1153 zebra_interface_if_set_value (s
, ifp
);
1159 link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1165 iflp
->lp_status
= stream_getl (s
);
1166 iflp
->te_metric
= stream_getl (s
);
1167 iflp
->max_bw
= stream_getf (s
);
1168 iflp
->max_rsv_bw
= stream_getf (s
);
1169 uint32_t bwclassnum
= stream_getl (s
);
1172 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1173 iflp
->unrsv_bw
[i
] = stream_getf (s
);
1175 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1176 " - outdated library?",
1177 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1179 iflp
->admin_grp
= stream_getl (s
);
1180 iflp
->rmt_as
= stream_getl (s
);
1181 iflp
->rmt_ip
.s_addr
= stream_get_ipv4 (s
);
1183 iflp
->av_delay
= stream_getl (s
);
1184 iflp
->min_delay
= stream_getl (s
);
1185 iflp
->max_delay
= stream_getl (s
);
1186 iflp
->delay_var
= stream_getl (s
);
1188 iflp
->pkt_loss
= stream_getf (s
);
1189 iflp
->res_bw
= stream_getf (s
);
1190 iflp
->ava_bw
= stream_getf (s
);
1191 iflp
->use_bw
= stream_getf (s
);
1195 zebra_interface_link_params_read (struct stream
*s
)
1197 struct if_link_params
*iflp
;
1202 ifindex
= stream_getl (s
);
1204 struct interface
*ifp
= if_lookup_by_index (ifindex
, VRF_DEFAULT
);
1208 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1213 if ((iflp
= if_link_params_get (ifp
)) == NULL
)
1216 link_params_set_value(s
, iflp
);
1222 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
1224 u_char link_params_status
= 0;
1226 /* Read interface's index. */
1227 ifp
->ifindex
= stream_getl (s
);
1228 ifp
->status
= stream_getc (s
);
1230 /* Read interface's value. */
1231 ifp
->flags
= stream_getq (s
);
1232 ifp
->ptm_enable
= stream_getc (s
);
1233 ifp
->ptm_status
= stream_getc (s
);
1234 ifp
->metric
= stream_getl (s
);
1235 ifp
->speed
= stream_getl (s
);
1236 ifp
->mtu
= stream_getl (s
);
1237 ifp
->mtu6
= stream_getl (s
);
1238 ifp
->bandwidth
= stream_getl (s
);
1239 ifp
->ll_type
= stream_getl (s
);
1240 ifp
->hw_addr_len
= stream_getl (s
);
1241 if (ifp
->hw_addr_len
)
1242 stream_get (ifp
->hw_addr
, s
, MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1244 /* Read Traffic Engineering status */
1245 link_params_status
= stream_getc (s
);
1246 /* Then, Traffic Engineering parameters if any */
1247 if (link_params_status
)
1249 struct if_link_params
*iflp
= if_link_params_get (ifp
);
1250 link_params_set_value(s
, iflp
);
1255 zebra_interface_link_params_write (struct stream
*s
, struct interface
*ifp
)
1258 struct if_link_params
*iflp
;
1261 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1264 iflp
= ifp
->link_params
;
1267 w
+= stream_putl (s
, iflp
->lp_status
);
1269 w
+= stream_putl (s
, iflp
->te_metric
);
1270 w
+= stream_putf (s
, iflp
->max_bw
);
1271 w
+= stream_putf (s
, iflp
->max_rsv_bw
);
1273 w
+= stream_putl (s
, MAX_CLASS_TYPE
);
1274 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1275 w
+= stream_putf (s
, iflp
->unrsv_bw
[i
]);
1277 w
+= stream_putl (s
, iflp
->admin_grp
);
1278 w
+= stream_putl (s
, iflp
->rmt_as
);
1279 w
+= stream_put_in_addr (s
, &iflp
->rmt_ip
);
1281 w
+= stream_putl (s
, iflp
->av_delay
);
1282 w
+= stream_putl (s
, iflp
->min_delay
);
1283 w
+= stream_putl (s
, iflp
->max_delay
);
1284 w
+= stream_putl (s
, iflp
->delay_var
);
1286 w
+= stream_putf (s
, iflp
->pkt_loss
);
1287 w
+= stream_putf (s
, iflp
->res_bw
);
1288 w
+= stream_putf (s
, iflp
->ava_bw
);
1289 w
+= stream_putf (s
, iflp
->use_bw
);
1295 * format of message for address additon is:
1299 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1300 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1309 * | ifc_flags | flags for connected address
1317 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1326 memconstant(const void *s
, int c
, size_t n
)
1328 const u_char
*p
= s
;
1338 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1341 struct interface
*ifp
;
1342 struct connected
*ifc
;
1343 struct prefix p
, d
, *dp
;
1347 memset (&p
, 0, sizeof(p
));
1348 memset (&d
, 0, sizeof(d
));
1350 /* Get interface index. */
1351 ifindex
= stream_getl (s
);
1354 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1357 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1358 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1364 ifc_flags
= stream_getc (s
);
1366 /* Fetch interface address. */
1367 d
.family
= p
.family
= stream_getc (s
);
1368 plen
= prefix_blen (&d
);
1370 zclient_stream_get_prefix (s
, &p
);
1372 /* Fetch destination address. */
1373 stream_get (&d
.u
.prefix
, s
, plen
);
1375 /* N.B. NULL destination pointers are encoded as all zeroes */
1376 dp
= memconstant(&d
.u
.prefix
,0,plen
) ? NULL
: &d
;
1378 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1380 ifc
= connected_lookup_prefix_exact (ifp
, &p
);
1383 /* N.B. NULL destination pointers are encoded as all zeroes */
1384 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1388 ifc
->flags
= ifc_flags
;
1389 if (ifc
->destination
)
1390 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1391 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1393 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1394 char buf
[PREFIX_STRLEN
];
1395 zlog_warn("warning: interface %s address %s "
1396 "with peer flag set, but no peer address!",
1398 prefix2str (ifc
->address
, buf
, sizeof buf
));
1399 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1405 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1406 ifc
= connected_delete_by_prefix(ifp
, &p
);
1413 * format of message for neighbor connected address is:
1417 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1418 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1433 * | addr_len | len of addr.
1436 struct nbr_connected
*
1437 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1439 unsigned int ifindex
;
1440 struct interface
*ifp
;
1442 struct nbr_connected
*ifc
;
1444 /* Get interface index. */
1445 ifindex
= stream_getl (s
);
1448 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1451 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1452 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD" : "DELETE",
1457 p
.family
= stream_getc (s
);
1458 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1459 p
.prefixlen
= stream_getc (s
);
1461 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1463 /* Currently only supporting P2P links, so any new RA source address is
1464 considered as the replacement of the previously learnt Link-Local address. */
1465 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1467 ifc
= nbr_connected_new ();
1468 ifc
->address
= prefix_new ();
1470 listnode_add (ifp
->nbr_connected
, ifc
);
1473 prefix_copy(ifc
->address
, &p
);
1477 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1479 ifc
= nbr_connected_check(ifp
, &p
);
1481 listnode_delete (ifp
->nbr_connected
, ifc
);
1488 zebra_interface_vrf_update_read (struct stream
*s
, vrf_id_t vrf_id
,
1489 vrf_id_t
*new_vrf_id
)
1491 unsigned int ifindex
;
1492 struct interface
*ifp
;
1493 vrf_id_t new_id
= VRF_DEFAULT
;
1495 /* Get interface index. */
1496 ifindex
= stream_getl (s
);
1498 /* Lookup interface. */
1499 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1502 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1507 /* Fetch new VRF Id. */
1508 new_id
= stream_getw (s
);
1510 *new_vrf_id
= new_id
;
1514 * Connect to label manager in a syncronous way
1516 * It first writes the request to zcient output buffer and then
1517 * immediately reads the answer from the input buffer.
1519 * @param zclient Zclient used to connect to label manager (zebra)
1520 * @result Result of response
1523 lm_label_manager_connect (struct zclient
*zclient
)
1534 zlog_debug ("Connecting to Label Manager");
1535 if (zclient
->sock
< 0)
1541 zclient_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1544 stream_putc (s
, zclient
->redist_default
);
1546 stream_putw (s
, zclient
->instance
);
1548 /* Put length at the first point of the stream. */
1549 stream_putw_at(s
, 0, stream_get_endp(s
));
1551 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1554 zlog_err ("%s: can't write to zclient->sock", __func__
);
1555 close (zclient
->sock
);
1561 zlog_err ("%s: zclient->sock connection closed", __func__
);
1562 close (zclient
->sock
);
1566 zlog_debug ("%s: Label manager connect request (%d bytes) sent", __func__
, ret
);
1572 ret
= zclient_read_header (s
, zclient
->sock
, &size
, &marker
, &version
,
1574 if (ret
!= 0 || cmd
!= ZEBRA_LABEL_MANAGER_CONNECT
) {
1575 zlog_err ("%s: Invalid Label Manager Connect Message Reply Header", __func__
);
1579 result
= stream_getc(s
);
1580 zlog_debug ("%s: Label Manager connect response (%d bytes) received, result %u",
1581 __func__
, size
, result
);
1587 * Function to request a label chunk in a syncronous way
1589 * It first writes the request to zlcient output buffer and then
1590 * immediately reads the answer from the input buffer.
1592 * @param zclient Zclient used to connect to label manager (zebra)
1593 * @param keep Avoid garbage collection
1594 * @param chunk_size Amount of labels requested
1595 * @param start To write first assigned chunk label to
1596 * @param end To write last assigned chunk label to
1597 * @result 0 on success, -1 otherwise
1600 lm_get_label_chunk (struct zclient
*zclient
, u_char keep
, uint32_t chunk_size
,
1601 uint32_t *start
, uint32_t *end
)
1610 u_char response_keep
;
1612 zlog_debug ("Getting Label Chunk");
1613 if (zclient
->sock
< 0)
1619 zclient_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1621 stream_putc (s
, keep
);
1623 stream_putl (s
, chunk_size
);
1624 /* Put length at the first point of the stream. */
1625 stream_putw_at(s
, 0, stream_get_endp(s
));
1627 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1630 zlog_err ("%s: can't write to zclient->sock", __func__
);
1631 close (zclient
->sock
);
1637 zlog_err ("%s: zclient->sock connection closed", __func__
);
1638 close (zclient
->sock
);
1642 zlog_debug ("%s: Label chunk request (%d bytes) sent", __func__
, ret
);
1648 ret
= zclient_read_header (s
, zclient
->sock
, &size
, &marker
, &version
,
1650 if (ret
!= 0 || cmd
!= ZEBRA_GET_LABEL_CHUNK
) {
1651 zlog_err ("%s: Invalid Get Label Chunk Message Reply Header", __func__
);
1654 zlog_debug ("%s: Label chunk response (%d bytes) received", __func__
, size
);
1656 response_keep
= stream_getc(s
);
1657 /* start and end labels */
1658 *start
= stream_getl(s
);
1659 *end
= stream_getl(s
);
1661 /* not owning this response */
1662 if (keep
!= response_keep
) {
1663 zlog_err ("%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1664 __func__
, *start
, *end
, keep
, response_keep
);
1668 || *start
< MPLS_MIN_UNRESERVED_LABEL
1669 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1670 zlog_err ("%s: Invalid Label chunk: %u - %u", __func__
,
1675 zlog_debug ("Label Chunk assign: %u - %u (%u) ",
1676 *start
, *end
, response_keep
);
1682 * Function to release a label chunk
1684 * @param zclient Zclient used to connect to label manager (zebra)
1685 * @param start First label of chunk
1686 * @param end Last label of chunk
1687 * @result 0 on success, -1 otherwise
1690 lm_release_label_chunk (struct zclient
*zclient
, uint32_t start
, uint32_t end
)
1695 zlog_debug ("Releasing Label Chunk");
1696 if (zclient
->sock
< 0)
1702 zclient_create_header (s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1705 stream_putl (s
, start
);
1707 stream_putl (s
, end
);
1709 /* Put length at the first point of the stream. */
1710 stream_putw_at(s
, 0, stream_get_endp(s
));
1712 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1715 zlog_err ("%s: can't write to zclient->sock", __func__
);
1716 close (zclient
->sock
);
1722 zlog_err ("%s: zclient->sock connection closed", __func__
);
1723 close (zclient
->sock
);
1731 /* Zebra client message read function. */
1733 zclient_read (struct thread
*thread
)
1736 uint16_t length
, command
;
1737 uint8_t marker
, version
;
1739 struct zclient
*zclient
;
1741 /* Get socket to zebra. */
1742 zclient
= THREAD_ARG (thread
);
1743 zclient
->t_read
= NULL
;
1745 /* Read zebra header (if we don't have it already). */
1746 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1749 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1750 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1754 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1755 return zclient_failed(zclient
);
1757 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1759 /* Try again later. */
1760 zclient_event (ZCLIENT_READ
, zclient
);
1763 already
= ZEBRA_HEADER_SIZE
;
1766 /* Reset to read from the beginning of the incoming packet. */
1767 stream_set_getp(zclient
->ibuf
, 0);
1769 /* Fetch header values. */
1770 length
= stream_getw (zclient
->ibuf
);
1771 marker
= stream_getc (zclient
->ibuf
);
1772 version
= stream_getc (zclient
->ibuf
);
1773 vrf_id
= stream_getw (zclient
->ibuf
);
1774 command
= stream_getw (zclient
->ibuf
);
1776 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1778 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1779 __func__
, zclient
->sock
, marker
, version
);
1780 return zclient_failed(zclient
);
1783 if (length
< ZEBRA_HEADER_SIZE
)
1785 zlog_err("%s: socket %d message length %u is less than %d ",
1786 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1787 return zclient_failed(zclient
);
1791 if (length
> STREAM_SIZE(zclient
->ibuf
))
1794 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1795 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1796 ns
= stream_new(length
);
1797 stream_copy(ns
, zclient
->ibuf
);
1798 stream_free (zclient
->ibuf
);
1802 /* Read rest of zebra packet. */
1803 if (already
< length
)
1806 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1807 length
-already
)) == 0) ||
1811 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1812 return zclient_failed(zclient
);
1814 if (nbyte
!= (ssize_t
)(length
-already
))
1816 /* Try again later. */
1817 zclient_event (ZCLIENT_READ
, zclient
);
1822 length
-= ZEBRA_HEADER_SIZE
;
1825 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1829 case ZEBRA_ROUTER_ID_UPDATE
:
1830 if (zclient
->router_id_update
)
1831 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1834 zclient_vrf_add (zclient
, vrf_id
);
1836 case ZEBRA_VRF_DELETE
:
1837 zclient_vrf_delete (zclient
, vrf_id
);
1839 case ZEBRA_INTERFACE_ADD
:
1840 if (zclient
->interface_add
)
1841 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1843 case ZEBRA_INTERFACE_DELETE
:
1844 if (zclient
->interface_delete
)
1845 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1847 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1848 if (zclient
->interface_address_add
)
1849 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1851 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1852 if (zclient
->interface_address_delete
)
1853 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1855 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1856 if (zclient
->interface_bfd_dest_update
)
1857 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1859 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1860 if (zclient
->interface_nbr_address_add
)
1861 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1863 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1864 if (zclient
->interface_nbr_address_delete
)
1865 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1867 case ZEBRA_INTERFACE_UP
:
1868 if (zclient
->interface_up
)
1869 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
1871 case ZEBRA_INTERFACE_DOWN
:
1872 if (zclient
->interface_down
)
1873 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
1875 case ZEBRA_INTERFACE_VRF_UPDATE
:
1876 if (zclient
->interface_vrf_update
)
1877 (*zclient
->interface_vrf_update
) (command
, zclient
, length
, vrf_id
);
1879 case ZEBRA_NEXTHOP_UPDATE
:
1881 zlog_debug("zclient rcvd nexthop update\n");
1882 if (zclient
->nexthop_update
)
1883 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
1885 case ZEBRA_IMPORT_CHECK_UPDATE
:
1887 zlog_debug("zclient rcvd import check update\n");
1888 if (zclient
->import_check_update
)
1889 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
1891 case ZEBRA_BFD_DEST_REPLAY
:
1892 if (zclient
->bfd_dest_replay
)
1893 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
1895 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
1896 if (zclient
->redistribute_route_ipv4_add
)
1897 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
1899 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
1900 if (zclient
->redistribute_route_ipv4_del
)
1901 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
1903 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
1904 if (zclient
->redistribute_route_ipv6_add
)
1905 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
1907 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
1908 if (zclient
->redistribute_route_ipv6_del
)
1909 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
1911 case ZEBRA_INTERFACE_LINK_PARAMS
:
1912 if (zclient
->interface_link_params
)
1913 (*zclient
->interface_link_params
) (command
, zclient
, length
);
1915 case ZEBRA_FEC_UPDATE
:
1917 zlog_debug("zclient rcvd fec update\n");
1918 if (zclient
->fec_update
)
1919 (*zclient
->fec_update
) (command
, zclient
, length
);
1925 if (zclient
->sock
< 0)
1926 /* Connection was closed during packet processing. */
1929 /* Register read thread. */
1930 stream_reset(zclient
->ibuf
);
1931 zclient_event (ZCLIENT_READ
, zclient
);
1937 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1938 u_short instance
, vrf_id_t vrf_id
)
1942 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1944 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1946 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1950 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1952 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1956 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1958 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1960 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
1964 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1966 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
1970 if (zclient
->sock
> 0)
1971 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
1976 zclient_redistribute_default (int command
, struct zclient
*zclient
,
1980 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1982 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1984 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
1988 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1990 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
1993 if (zclient
->sock
> 0)
1994 zebra_message_send (zclient
, command
, vrf_id
);
1998 zclient_event (enum event event
, struct zclient
*zclient
)
2002 case ZCLIENT_SCHEDULE
:
2003 if (! zclient
->t_connect
)
2004 zclient
->t_connect
=
2005 thread_add_event (zclient
->master
, zclient_connect
, zclient
, 0);
2007 case ZCLIENT_CONNECT
:
2009 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
2010 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2011 if (! zclient
->t_connect
)
2012 zclient
->t_connect
=
2013 thread_add_timer (zclient
->master
, zclient_connect
, zclient
,
2014 zclient
->fail
< 3 ? 10 : 60);
2018 thread_add_read (zclient
->master
, zclient_read
, zclient
, zclient
->sock
);
2023 const char *zclient_serv_path_get()
2025 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
2029 zclient_serv_path_set (char *path
)
2034 zclient_serv_path
= NULL
;
2036 /* test if `path' is socket. don't set it otherwise. */
2037 if (stat(path
, &sb
) == -1)
2039 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
2043 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
2045 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
2049 /* it seems that path is unix socket */
2050 zclient_serv_path
= path
;