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,
40 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
41 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
43 /* Zebra client events. */
44 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
46 /* Prototype for event manager. */
47 static void zclient_event (enum event
, struct zclient
*);
49 const char *zclient_serv_path
= NULL
;
51 /* This file local debug flag. */
52 int zclient_debug
= 0;
54 /* Allocate zclient structure. */
56 zclient_new (struct thread_master
*master
)
58 struct zclient
*zclient
;
59 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
61 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
62 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
63 zclient
->wb
= buffer_new(0);
64 zclient
->master
= master
;
69 /* This function is only called when exiting, because
70 many parts of the code do not check for I/O errors, so they could
71 reference an invalid pointer if the structure was ever freed.
73 Free zclient structure. */
75 zclient_free (struct zclient
*zclient
)
78 stream_free(zclient
->ibuf
);
80 stream_free(zclient
->obuf
);
82 buffer_free(zclient
->wb
);
84 XFREE (MTYPE_ZCLIENT
, zclient
);
88 redist_check_instance (struct redist_proto
*red
, u_short instance
)
90 struct listnode
*node
;
96 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
104 redist_add_instance (struct redist_proto
*red
, u_short instance
)
111 red
->instances
= list_new();
113 in
= XMALLOC (MTYPE_REDIST_INST
, sizeof(u_short
));
115 listnode_add (red
->instances
, in
);
119 redist_del_instance (struct redist_proto
*red
, u_short instance
)
123 id
= redist_check_instance (red
, instance
);
127 listnode_delete(red
->instances
, id
);
128 XFREE (MTYPE_REDIST_INST
, id
);
129 if (!red
->instances
->count
)
132 list_free(red
->instances
);
133 red
->instances
= NULL
;
137 /* Stop zebra client services. */
139 zclient_stop (struct zclient
*zclient
)
145 zlog_debug ("zclient stopped");
148 THREAD_OFF(zclient
->t_read
);
149 THREAD_OFF(zclient
->t_connect
);
150 THREAD_OFF(zclient
->t_write
);
153 stream_reset(zclient
->ibuf
);
154 stream_reset(zclient
->obuf
);
156 /* Empty the write buffer. */
157 buffer_reset(zclient
->wb
);
160 if (zclient
->sock
>= 0)
162 close (zclient
->sock
);
167 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
169 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
171 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
172 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
174 redist_del_instance(&zclient
->mi_redist
[afi
][zclient
->redist_default
],
178 vrf_bitmap_free(zclient
->default_information
);
179 zclient
->default_information
= VRF_BITMAP_NULL
;
183 zclient_reset (struct zclient
*zclient
)
187 zclient_stop (zclient
);
189 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
190 redist_del_instance (&zclient
->mi_redist
[afi
][zclient
->redist_default
], zclient
->instance
);
192 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
195 #ifdef HAVE_TCP_ZEBRA
197 /* Make socket to zebra daemon. Return zebra socket. */
203 struct sockaddr_in serv
;
205 /* We should think about IPv6 connection. */
206 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
210 /* Make server socket. */
211 memset (&serv
, 0, sizeof (struct sockaddr_in
));
212 serv
.sin_family
= AF_INET
;
213 serv
.sin_port
= htons (ZEBRA_PORT
);
214 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
215 serv
.sin_len
= sizeof (struct sockaddr_in
);
216 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
217 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
219 /* Connect to zebra. */
220 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
224 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
225 errno
, safe_strerror (errno
));
234 /* For sockaddr_un. */
238 zclient_socket_un (const char *path
)
242 struct sockaddr_un addr
;
244 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
248 /* Make server socket. */
249 memset (&addr
, 0, sizeof (struct sockaddr_un
));
250 addr
.sun_family
= AF_UNIX
;
251 strncpy (addr
.sun_path
, path
, strlen (path
));
252 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
253 len
= addr
.sun_len
= SUN_LEN(&addr
);
255 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
256 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
258 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
262 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__
,
263 errno
, safe_strerror (errno
));
270 #endif /* HAVE_TCP_ZEBRA */
273 * Connect to zebra daemon.
274 * @param zclient a pointer to zclient structure
275 * @return socket fd just to make sure that connection established
280 zclient_socket_connect (struct zclient
*zclient
)
282 #ifdef HAVE_TCP_ZEBRA
283 zclient
->sock
= zclient_socket ();
285 zclient
->sock
= zclient_socket_un (zclient_serv_path_get());
287 return zclient
->sock
;
291 zclient_failed(struct zclient
*zclient
)
294 zclient_stop(zclient
);
295 zclient_event(ZCLIENT_CONNECT
, zclient
);
300 zclient_flush_data(struct thread
*thread
)
302 struct zclient
*zclient
= THREAD_ARG(thread
);
304 zclient
->t_write
= NULL
;
305 if (zclient
->sock
< 0)
307 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
310 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
311 __func__
, zclient
->sock
);
312 return zclient_failed(zclient
);
315 zclient
->t_write
= NULL
;
316 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
, zclient
->sock
,
326 zclient_send_message(struct zclient
*zclient
)
328 if (zclient
->sock
< 0)
330 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
331 stream_get_endp(zclient
->obuf
)))
334 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
335 __func__
, zclient
->sock
);
336 return zclient_failed(zclient
);
339 THREAD_OFF(zclient
->t_write
);
342 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
343 zclient
->sock
, &zclient
->t_write
);
350 zclient_create_header (struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
352 /* length placeholder, caller can update */
353 stream_putw (s
, ZEBRA_HEADER_SIZE
);
354 stream_putc (s
, ZEBRA_HEADER_MARKER
);
355 stream_putc (s
, ZSERV_VERSION
);
356 stream_putw (s
, vrf_id
);
357 stream_putw (s
, command
);
361 zclient_read_header (struct stream
*s
, int sock
, u_int16_t
*size
, u_char
*marker
,
362 u_char
*version
, vrf_id_t
*vrf_id
, u_int16_t
*cmd
)
364 if (stream_read (s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
367 *size
= stream_getw (s
) - ZEBRA_HEADER_SIZE
;
368 *marker
= stream_getc (s
);
369 *version
= stream_getc (s
);
370 *vrf_id
= stream_getw (s
);
371 *cmd
= stream_getw (s
);
373 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
)
375 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
376 __func__
, sock
, *marker
, *version
);
380 if (*size
&& stream_read (s
, sock
, *size
) != *size
)
386 /* Send simple Zebra message. */
388 zebra_message_send (struct zclient
*zclient
, int command
, vrf_id_t vrf_id
)
392 /* Get zclient output buffer. */
396 /* Send very simple command only Zebra message. */
397 zclient_create_header (s
, command
, vrf_id
);
399 return zclient_send_message(zclient
);
403 zebra_hello_send (struct zclient
*zclient
)
407 if (zclient
->redist_default
)
412 /* The VRF ID in the HELLO message is always 0. */
413 zclient_create_header (s
, ZEBRA_HELLO
, VRF_DEFAULT
);
414 stream_putc (s
, zclient
->redist_default
);
415 stream_putw (s
, zclient
->instance
);
416 stream_putw_at (s
, 0, stream_get_endp (s
));
417 return zclient_send_message(zclient
);
423 /* Send register requests to zebra daemon for the information in a VRF. */
425 zclient_send_reg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
430 /* zclient is disabled. */
431 if (! zclient
->enable
)
434 /* If not connected to the zebra yet. */
435 if (zclient
->sock
< 0)
439 zlog_debug ("%s: send register messages for VRF %u", __func__
, vrf_id
);
441 /* We need router-id information. */
442 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
444 /* We need interface information. */
445 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
447 /* Set unwanted redistribute route. */
448 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
449 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
451 /* Flush all redistribute request. */
452 if (vrf_id
== VRF_DEFAULT
)
453 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
454 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
455 if (zclient
->mi_redist
[afi
][i
].enabled
)
457 struct listnode
*node
;
460 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
461 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
462 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
,
466 /* Flush all redistribute request. */
467 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
468 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
469 if (i
!= zclient
->redist_default
&&
470 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
471 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, 0, vrf_id
);
473 /* If default information is needed. */
474 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
475 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, vrf_id
);
478 /* Send unregister requests to zebra daemon for the information in a VRF. */
480 zclient_send_dereg_requests (struct zclient
*zclient
, vrf_id_t vrf_id
)
485 /* zclient is disabled. */
486 if (! zclient
->enable
)
489 /* If not connected to the zebra yet. */
490 if (zclient
->sock
< 0)
494 zlog_debug ("%s: send deregister messages for VRF %u", __func__
, vrf_id
);
496 /* We need router-id information. */
497 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
499 /* We need interface information. */
500 zebra_message_send (zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
502 /* Set unwanted redistribute route. */
503 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
504 vrf_bitmap_set (zclient
->redist
[afi
][zclient
->redist_default
], vrf_id
);
506 /* Flush all redistribute request. */
507 if (vrf_id
== VRF_DEFAULT
)
508 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
509 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
510 if (zclient
->mi_redist
[afi
][i
].enabled
)
512 struct listnode
*node
;
515 for (ALL_LIST_ELEMENTS_RO(zclient
->mi_redist
[afi
][i
].instances
, node
, id
))
516 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
517 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
,
521 /* Flush all redistribute request. */
522 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
523 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
524 if (i
!= zclient
->redist_default
&&
525 vrf_bitmap_check (zclient
->redist
[afi
][i
], vrf_id
))
526 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
, i
, 0, vrf_id
);
528 /* If default information is needed. */
529 if (vrf_bitmap_check (zclient
->default_information
, VRF_DEFAULT
))
530 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, vrf_id
);
533 /* Send request to zebra daemon to start or stop RA. */
535 zclient_send_interface_radv_req (struct zclient
*zclient
, vrf_id_t vrf_id
,
536 struct interface
*ifp
, int enable
, int ra_interval
)
540 /* zclient is disabled. */
541 if (!zclient
->enable
)
544 /* If not connected to the zebra yet. */
545 if (zclient
->sock
< 0)
548 /* Form and send message. */
553 zclient_create_header (s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
555 zclient_create_header (s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
557 stream_putl (s
, ifp
->ifindex
);
558 stream_putl (s
, ra_interval
);
560 stream_putw_at (s
, 0, stream_get_endp (s
));
562 zclient_send_message(zclient
);
565 /* Make connection to zebra daemon. */
567 zclient_start (struct zclient
*zclient
)
570 zlog_info ("zclient_start is called");
572 /* zclient is disabled. */
573 if (! zclient
->enable
)
576 /* If already connected to the zebra. */
577 if (zclient
->sock
>= 0)
580 /* Check connect thread. */
581 if (zclient
->t_connect
)
584 if (zclient_socket_connect(zclient
) < 0)
587 zlog_debug ("zclient connection fail");
589 zclient_event (ZCLIENT_CONNECT
, zclient
);
593 if (set_nonblocking(zclient
->sock
) < 0)
594 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
596 /* Clear fail count. */
599 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
601 /* Create read thread. */
602 zclient_event (ZCLIENT_READ
, zclient
);
604 zebra_hello_send (zclient
);
606 /* Inform the successful connection. */
607 if (zclient
->zebra_connected
)
608 (*zclient
->zebra_connected
) (zclient
);
613 /* Initialize zebra client. Argument redist_default is unwanted
614 redistribute route type. */
616 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
620 /* Enable zebra client connection by default. */
623 /* Set -1 to the default socket value. */
626 /* Clear redistribution flags. */
627 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
628 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
629 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
631 /* Set unwanted redistribute route. bgpd does not need BGP route
633 zclient
->redist_default
= redist_default
;
634 zclient
->instance
= instance
;
635 /* Pending: make afi(s) an arg. */
636 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
637 redist_add_instance (&zclient
->mi_redist
[afi
][redist_default
], instance
);
639 /* Set default-information redistribute to zero. */
640 zclient
->default_information
= vrf_bitmap_init ();;
643 zlog_debug ("zclient_start is called");
645 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
648 /* This function is a wrapper function for calling zclient_start from
649 timer or event thread. */
651 zclient_connect (struct thread
*t
)
653 struct zclient
*zclient
;
655 zclient
= THREAD_ARG (t
);
656 zclient
->t_connect
= NULL
;
659 zlog_debug ("zclient_connect is called");
661 return zclient_start (zclient
);
665 * "xdr_encode"-like interface that allows daemon (client) to send
666 * a message to zebra server for a route that needs to be
667 * added/deleted to the kernel. Info about the route is specified
668 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
669 * the info down the zclient socket using the stream_* functions.
671 * The corresponding read ("xdr_decode") function on the server
672 * side is zread_ipv4_add()/zread_ipv4_delete().
674 * 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
675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * | Length (2) | Command | Route Type |
677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
678 * | ZEBRA Flags | Message Flags | Prefix length |
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * | Destination IPv4 Prefix for route |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
687 * described, as per the Nexthop count. Each nexthop described as:
690 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
691 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
692 * | IPv4 Nexthop address or Interface Index number |
693 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
696 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
697 * nexthop information is provided, and the message describes a prefix
698 * to blackhole or reject route.
700 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
701 * infrastructure was built around the traditional (32-bit "gate OR
702 * ifindex") nexthop data unit. A special encoding can be used to feed
703 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
704 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
706 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
707 * - .nexthop_num == .ifindex_num
708 * - .nexthop and .ifindex are filled with gate and ifindex parts of
709 * each compound nexthop, both in the same order
711 * zapi_ipv4_route() will produce two nexthop data units for each such
712 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
713 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
715 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
718 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
721 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
723 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
725 * XXX: No attention paid to alignment.
728 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
729 struct zapi_ipv4
*api
)
739 /* Some checks for labeled-unicast. The current expectation is that each
740 * nexthop is accompanied by a label in the case of labeled-unicast.
742 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
743 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
745 /* We expect prefixes installed with labels and the number to match
746 * the number of nexthops.
748 assert (api
->label_num
== api
->nexthop_num
);
751 zclient_create_header (s
, cmd
, api
->vrf_id
);
753 /* Put type and nexthop. */
754 stream_putc (s
, api
->type
);
755 stream_putw (s
, api
->instance
);
756 stream_putl (s
, api
->flags
);
757 stream_putc (s
, api
->message
);
758 stream_putw (s
, api
->safi
);
760 /* Put prefix information. */
761 psize
= PSIZE (p
->prefixlen
);
762 stream_putc (s
, p
->prefixlen
);
763 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
765 /* Nexthop, ifindex, distance and metric information. */
766 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
768 /* traditional 32-bit data units */
769 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
772 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
773 /* XXX assert(api->nexthop_num == 0); */
774 /* XXX assert(api->ifindex_num == 0); */
777 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
779 for (i
= 0; i
< api
->nexthop_num
; i
++)
781 stream_putc (s
, NEXTHOP_TYPE_IPV4
);
782 stream_put_in_addr (s
, api
->nexthop
[i
]);
783 /* For labeled-unicast, each nexthop is followed by label. */
784 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
785 stream_putl (s
, api
->label
[i
]);
787 for (i
= 0; i
< api
->ifindex_num
; i
++)
789 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
790 stream_putl (s
, api
->ifindex
[i
]);
794 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
795 stream_putc (s
, api
->distance
);
796 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
797 stream_putl (s
, api
->metric
);
798 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
799 stream_putl (s
, api
->tag
);
800 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
801 stream_putl (s
, api
->mtu
);
803 /* Put length at the first point of the stream. */
804 stream_putw_at (s
, 0, stream_get_endp (s
));
806 return zclient_send_message(zclient
);
810 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
811 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
821 /* Some checks for labeled-unicast. The current expectation is that each
822 * nexthop is accompanied by a label in the case of labeled-unicast.
824 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
825 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
827 /* We expect prefixes installed with labels and the number to match
828 * the number of nexthops.
830 assert (api
->label_num
== api
->nexthop_num
);
833 zclient_create_header (s
, cmd
, api
->vrf_id
);
835 /* Put type and nexthop. */
836 stream_putc (s
, api
->type
);
837 stream_putw (s
, api
->instance
);
838 stream_putl (s
, api
->flags
);
839 stream_putc (s
, api
->message
);
840 stream_putw (s
, api
->safi
);
842 /* Put prefix information. */
843 psize
= PSIZE (p
->prefixlen
);
844 stream_putc (s
, p
->prefixlen
);
845 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
847 /* Nexthop, ifindex, distance and metric information. */
848 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
850 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
853 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
854 /* XXX assert(api->nexthop_num == 0); */
855 /* XXX assert(api->ifindex_num == 0); */
858 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
860 for (i
= 0; i
< api
->nexthop_num
; i
++)
862 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
863 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
864 /* For labeled-unicast, each nexthop is followed by label. */
865 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
866 stream_putl (s
, api
->label
[i
]);
868 for (i
= 0; i
< api
->ifindex_num
; i
++)
870 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
871 stream_putl (s
, api
->ifindex
[i
]);
875 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
876 stream_putc (s
, api
->distance
);
877 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
878 stream_putl (s
, api
->metric
);
879 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
880 stream_putl (s
, api
->tag
);
881 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
882 stream_putl (s
, api
->mtu
);
884 /* Put length at the first point of the stream. */
885 stream_putw_at (s
, 0, stream_get_endp (s
));
887 return zclient_send_message(zclient
);
891 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
892 struct prefix_ipv6
*src_p
, struct zapi_ipv6
*api
)
898 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
899 assert (!(api
->message
& ZAPI_MESSAGE_SRCPFX
) == !src_p
);
905 /* Some checks for labeled-unicast. The current expectation is that each
906 * nexthop is accompanied by a label in the case of labeled-unicast.
908 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
) &&
909 CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
911 /* We expect prefixes installed with labels and the number to match
912 * the number of nexthops.
914 assert (api
->label_num
== api
->nexthop_num
);
917 zclient_create_header (s
, cmd
, api
->vrf_id
);
919 /* Put type and nexthop. */
920 stream_putc (s
, api
->type
);
921 stream_putw (s
, api
->instance
);
922 stream_putl (s
, api
->flags
);
923 stream_putc (s
, api
->message
);
924 stream_putw (s
, api
->safi
);
926 /* Put prefix information. */
927 psize
= PSIZE (p
->prefixlen
);
928 stream_putc (s
, p
->prefixlen
);
929 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
931 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_SRCPFX
))
933 psize
= PSIZE (src_p
->prefixlen
);
934 stream_putc (s
, src_p
->prefixlen
);
935 stream_write (s
, (u_char
*)&src_p
->prefix
, psize
);
938 /* Nexthop, ifindex, distance and metric information. */
939 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
941 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
944 stream_putc (s
, NEXTHOP_TYPE_BLACKHOLE
);
945 /* XXX assert(api->nexthop_num == 0); */
946 /* XXX assert(api->ifindex_num == 0); */
949 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
951 for (i
= 0; i
< api
->nexthop_num
; i
++)
953 stream_putc (s
, NEXTHOP_TYPE_IPV6
);
954 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
955 /* For labeled-unicast, each nexthop is followed by label. */
956 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_LABEL
))
957 stream_putl (s
, api
->label
[i
]);
959 for (i
= 0; i
< api
->ifindex_num
; i
++)
961 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
962 stream_putl (s
, api
->ifindex
[i
]);
966 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
967 stream_putc (s
, api
->distance
);
968 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
969 stream_putl (s
, api
->metric
);
970 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
971 stream_putl (s
, api
->tag
);
972 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_MTU
))
973 stream_putl (s
, api
->mtu
);
975 /* Put length at the first point of the stream. */
976 stream_putw_at (s
, 0, stream_get_endp (s
));
978 return zclient_send_message(zclient
);
982 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
983 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
984 * then set/unset redist[type] in the client handle (a struct zserv) for the
988 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
989 u_short instance
, vrf_id_t vrf_id
)
996 zclient_create_header (s
, command
, vrf_id
);
997 stream_putc (s
, afi
);
998 stream_putc (s
, type
);
999 stream_putw (s
, instance
);
1001 stream_putw_at (s
, 0, stream_get_endp (s
));
1003 return zclient_send_message(zclient
);
1006 /* Get prefix in ZServ format; family should be filled in on prefix */
1008 zclient_stream_get_prefix (struct stream
*s
, struct prefix
*p
)
1010 size_t plen
= prefix_blen (p
);
1017 stream_get (&p
->u
.prefix
, s
, plen
);
1019 p
->prefixlen
= MIN(plen
* 8, c
);
1022 /* Router-id update from zebra daemon. */
1024 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
1026 /* Fetch interface address. */
1027 rid
->family
= stream_getc (s
);
1029 zclient_stream_get_prefix (s
, rid
);
1032 /* Interface addition from zebra daemon. */
1034 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1035 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1037 * 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
1038 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1046 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1048 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1051 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1053 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1055 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1057 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1059 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1061 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1062 * | Link Layer Type |
1063 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1064 * | Harware Address Length |
1065 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1066 * | Hardware Address if HW lenght different from 0 |
1067 * | ... max INTERFACE_HWADDR_MAX |
1068 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1069 * | Link_params? | Whether a link-params follows: 1 or 0.
1070 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1071 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1072 * | .... (struct if_link_params). |
1073 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1077 zclient_vrf_add (struct zclient
*zclient
, vrf_id_t vrf_id
)
1080 char vrfname_tmp
[VRF_NAMSIZ
];
1082 /* Read interface name. */
1083 stream_get (vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1085 /* Lookup/create vrf by vrf_id. */
1086 vrf
= vrf_get (vrf_id
, vrfname_tmp
);
1092 zclient_vrf_delete (struct zclient
*zclient
, vrf_id_t vrf_id
)
1096 /* Lookup vrf by vrf_id. */
1097 vrf
= vrf_lookup_by_id (vrf_id
);
1100 * If a routing protocol doesn't know about a
1101 * vrf that is about to be deleted. There is
1102 * no point in attempting to delete it.
1111 zebra_interface_add_read (struct stream
*s
, vrf_id_t vrf_id
)
1113 struct interface
*ifp
;
1114 char ifname_tmp
[INTERFACE_NAMSIZ
];
1116 /* Read interface name. */
1117 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1119 /* Lookup/create interface by name. */
1120 ifp
= if_get_by_name_len (ifname_tmp
,
1121 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1124 zebra_interface_if_set_value (s
, ifp
);
1130 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1131 * from zebra server. The format of this message is the same as
1132 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1133 * comments for zebra_interface_add_read), except that no sockaddr_dl
1134 * is sent at the tail of the message.
1137 zebra_interface_state_read (struct stream
*s
, vrf_id_t vrf_id
)
1139 struct interface
*ifp
;
1140 char ifname_tmp
[INTERFACE_NAMSIZ
];
1142 /* Read interface name. */
1143 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1145 /* Lookup this by interface index. */
1146 ifp
= if_lookup_by_name_len (ifname_tmp
,
1147 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
1151 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1152 ifname_tmp
, vrf_id
);
1156 zebra_interface_if_set_value (s
, ifp
);
1162 link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1168 iflp
->lp_status
= stream_getl (s
);
1169 iflp
->te_metric
= stream_getl (s
);
1170 iflp
->max_bw
= stream_getf (s
);
1171 iflp
->max_rsv_bw
= stream_getf (s
);
1172 uint32_t bwclassnum
= stream_getl (s
);
1175 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1176 iflp
->unrsv_bw
[i
] = stream_getf (s
);
1178 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1179 " - outdated library?",
1180 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1182 iflp
->admin_grp
= stream_getl (s
);
1183 iflp
->rmt_as
= stream_getl (s
);
1184 iflp
->rmt_ip
.s_addr
= stream_get_ipv4 (s
);
1186 iflp
->av_delay
= stream_getl (s
);
1187 iflp
->min_delay
= stream_getl (s
);
1188 iflp
->max_delay
= stream_getl (s
);
1189 iflp
->delay_var
= stream_getl (s
);
1191 iflp
->pkt_loss
= stream_getf (s
);
1192 iflp
->res_bw
= stream_getf (s
);
1193 iflp
->ava_bw
= stream_getf (s
);
1194 iflp
->use_bw
= stream_getf (s
);
1198 zebra_interface_link_params_read (struct stream
*s
)
1200 struct if_link_params
*iflp
;
1205 ifindex
= stream_getl (s
);
1207 struct interface
*ifp
= if_lookup_by_index (ifindex
, VRF_DEFAULT
);
1211 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1216 if ((iflp
= if_link_params_get (ifp
)) == NULL
)
1219 link_params_set_value(s
, iflp
);
1225 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
1227 u_char link_params_status
= 0;
1229 /* Read interface's index. */
1230 ifp
->ifindex
= stream_getl (s
);
1231 ifp
->status
= stream_getc (s
);
1233 /* Read interface's value. */
1234 ifp
->flags
= stream_getq (s
);
1235 ifp
->ptm_enable
= stream_getc (s
);
1236 ifp
->ptm_status
= stream_getc (s
);
1237 ifp
->metric
= stream_getl (s
);
1238 ifp
->speed
= stream_getl (s
);
1239 ifp
->mtu
= stream_getl (s
);
1240 ifp
->mtu6
= stream_getl (s
);
1241 ifp
->bandwidth
= stream_getl (s
);
1242 ifp
->ll_type
= stream_getl (s
);
1243 ifp
->hw_addr_len
= stream_getl (s
);
1244 if (ifp
->hw_addr_len
)
1245 stream_get (ifp
->hw_addr
, s
, MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1247 /* Read Traffic Engineering status */
1248 link_params_status
= stream_getc (s
);
1249 /* Then, Traffic Engineering parameters if any */
1250 if (link_params_status
)
1252 struct if_link_params
*iflp
= if_link_params_get (ifp
);
1253 link_params_set_value(s
, iflp
);
1258 zebra_interface_link_params_write (struct stream
*s
, struct interface
*ifp
)
1261 struct if_link_params
*iflp
;
1264 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1267 iflp
= ifp
->link_params
;
1270 w
+= stream_putl (s
, iflp
->lp_status
);
1272 w
+= stream_putl (s
, iflp
->te_metric
);
1273 w
+= stream_putf (s
, iflp
->max_bw
);
1274 w
+= stream_putf (s
, iflp
->max_rsv_bw
);
1276 w
+= stream_putl (s
, MAX_CLASS_TYPE
);
1277 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1278 w
+= stream_putf (s
, iflp
->unrsv_bw
[i
]);
1280 w
+= stream_putl (s
, iflp
->admin_grp
);
1281 w
+= stream_putl (s
, iflp
->rmt_as
);
1282 w
+= stream_put_in_addr (s
, &iflp
->rmt_ip
);
1284 w
+= stream_putl (s
, iflp
->av_delay
);
1285 w
+= stream_putl (s
, iflp
->min_delay
);
1286 w
+= stream_putl (s
, iflp
->max_delay
);
1287 w
+= stream_putl (s
, iflp
->delay_var
);
1289 w
+= stream_putf (s
, iflp
->pkt_loss
);
1290 w
+= stream_putf (s
, iflp
->res_bw
);
1291 w
+= stream_putf (s
, iflp
->ava_bw
);
1292 w
+= stream_putf (s
, iflp
->use_bw
);
1298 * format of message for address additon is:
1302 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1303 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1312 * | ifc_flags | flags for connected address
1320 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1329 memconstant(const void *s
, int c
, size_t n
)
1331 const u_char
*p
= s
;
1341 zebra_interface_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1344 struct interface
*ifp
;
1345 struct connected
*ifc
;
1346 struct prefix p
, d
, *dp
;
1350 memset (&p
, 0, sizeof(p
));
1351 memset (&d
, 0, sizeof(d
));
1353 /* Get interface index. */
1354 ifindex
= stream_getl (s
);
1357 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1360 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1361 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1367 ifc_flags
= stream_getc (s
);
1369 /* Fetch interface address. */
1370 d
.family
= p
.family
= stream_getc (s
);
1371 plen
= prefix_blen (&d
);
1373 zclient_stream_get_prefix (s
, &p
);
1375 /* Fetch destination address. */
1376 stream_get (&d
.u
.prefix
, s
, plen
);
1378 /* N.B. NULL destination pointers are encoded as all zeroes */
1379 dp
= memconstant(&d
.u
.prefix
,0,plen
) ? NULL
: &d
;
1381 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1383 ifc
= connected_lookup_prefix_exact (ifp
, &p
);
1386 /* N.B. NULL destination pointers are encoded as all zeroes */
1387 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1391 ifc
->flags
= ifc_flags
;
1392 if (ifc
->destination
)
1393 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1394 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1396 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1397 char buf
[PREFIX_STRLEN
];
1398 zlog_warn("warning: interface %s address %s "
1399 "with peer flag set, but no peer address!",
1401 prefix2str (ifc
->address
, buf
, sizeof buf
));
1402 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1408 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1409 ifc
= connected_delete_by_prefix(ifp
, &p
);
1416 * format of message for neighbor connected address is:
1420 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1421 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1436 * | addr_len | len of addr.
1439 struct nbr_connected
*
1440 zebra_interface_nbr_address_read (int type
, struct stream
*s
, vrf_id_t vrf_id
)
1442 unsigned int ifindex
;
1443 struct interface
*ifp
;
1445 struct nbr_connected
*ifc
;
1447 /* Get interface index. */
1448 ifindex
= stream_getl (s
);
1451 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1454 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1455 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD" : "DELETE",
1460 p
.family
= stream_getc (s
);
1461 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1462 p
.prefixlen
= stream_getc (s
);
1464 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1466 /* Currently only supporting P2P links, so any new RA source address is
1467 considered as the replacement of the previously learnt Link-Local address. */
1468 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1470 ifc
= nbr_connected_new ();
1471 ifc
->address
= prefix_new ();
1473 listnode_add (ifp
->nbr_connected
, ifc
);
1476 prefix_copy(ifc
->address
, &p
);
1480 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1482 ifc
= nbr_connected_check(ifp
, &p
);
1484 listnode_delete (ifp
->nbr_connected
, ifc
);
1491 zebra_interface_vrf_update_read (struct stream
*s
, vrf_id_t vrf_id
,
1492 vrf_id_t
*new_vrf_id
)
1494 unsigned int ifindex
;
1495 struct interface
*ifp
;
1496 vrf_id_t new_id
= VRF_DEFAULT
;
1498 /* Get interface index. */
1499 ifindex
= stream_getl (s
);
1501 /* Lookup interface. */
1502 ifp
= if_lookup_by_index (ifindex
, vrf_id
);
1505 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1510 /* Fetch new VRF Id. */
1511 new_id
= stream_getw (s
);
1513 *new_vrf_id
= new_id
;
1517 * Connect to label manager in a syncronous way
1519 * It first writes the request to zcient output buffer and then
1520 * immediately reads the answer from the input buffer.
1522 * @param zclient Zclient used to connect to label manager (zebra)
1523 * @result Result of response
1526 lm_label_manager_connect (struct zclient
*zclient
)
1538 zlog_debug ("Connecting to Label Manager");
1540 if (zclient
->sock
< 0)
1546 zclient_create_header (s
, ZEBRA_LABEL_MANAGER_CONNECT
, VRF_DEFAULT
);
1549 stream_putc (s
, zclient
->redist_default
);
1551 stream_putw (s
, zclient
->instance
);
1553 /* Put length at the first point of the stream. */
1554 stream_putw_at(s
, 0, stream_get_endp(s
));
1556 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1559 zlog_err ("%s: can't write to zclient->sock", __func__
);
1560 close (zclient
->sock
);
1566 zlog_err ("%s: zclient->sock connection closed", __func__
);
1567 close (zclient
->sock
);
1572 zlog_debug ("%s: Label manager connect request (%d bytes) sent", __func__
, ret
);
1578 ret
= zclient_read_header (s
, zclient
->sock
, &size
, &marker
, &version
,
1580 if (ret
!= 0 || cmd
!= ZEBRA_LABEL_MANAGER_CONNECT
) {
1581 zlog_err ("%s: Invalid Label Manager Connect Message Reply Header", __func__
);
1585 result
= stream_getc(s
);
1587 zlog_debug ("%s: Label Manager connect response (%d bytes) received, result %u",
1588 __func__
, size
, result
);
1594 * Function to request a label chunk in a syncronous way
1596 * It first writes the request to zlcient output buffer and then
1597 * immediately reads the answer from the input buffer.
1599 * @param zclient Zclient used to connect to label manager (zebra)
1600 * @param keep Avoid garbage collection
1601 * @param chunk_size Amount of labels requested
1602 * @param start To write first assigned chunk label to
1603 * @param end To write last assigned chunk label to
1604 * @result 0 on success, -1 otherwise
1607 lm_get_label_chunk (struct zclient
*zclient
, u_char keep
, uint32_t chunk_size
,
1608 uint32_t *start
, uint32_t *end
)
1617 u_char response_keep
;
1620 zlog_debug ("Getting Label Chunk");
1622 if (zclient
->sock
< 0)
1628 zclient_create_header (s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1630 stream_putc (s
, keep
);
1632 stream_putl (s
, chunk_size
);
1633 /* Put length at the first point of the stream. */
1634 stream_putw_at(s
, 0, stream_get_endp(s
));
1636 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1639 zlog_err ("%s: can't write to zclient->sock", __func__
);
1640 close (zclient
->sock
);
1646 zlog_err ("%s: zclient->sock connection closed", __func__
);
1647 close (zclient
->sock
);
1652 zlog_debug ("%s: Label chunk request (%d bytes) sent", __func__
, ret
);
1658 ret
= zclient_read_header (s
, zclient
->sock
, &size
, &marker
, &version
,
1660 if (ret
!= 0 || cmd
!= ZEBRA_GET_LABEL_CHUNK
) {
1661 zlog_err ("%s: Invalid Get Label Chunk Message Reply Header", __func__
);
1665 zlog_debug ("%s: Label chunk response (%d bytes) received", __func__
, size
);
1668 response_keep
= stream_getc(s
);
1669 /* start and end labels */
1670 *start
= stream_getl(s
);
1671 *end
= stream_getl(s
);
1673 /* not owning this response */
1674 if (keep
!= response_keep
) {
1675 zlog_err ("%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1676 __func__
, *start
, *end
, keep
, response_keep
);
1680 || *start
< MPLS_MIN_UNRESERVED_LABEL
1681 || *end
> MPLS_MAX_UNRESERVED_LABEL
) {
1682 zlog_err ("%s: Invalid Label chunk: %u - %u", __func__
,
1688 zlog_debug ("Label Chunk assign: %u - %u (%u) ",
1689 *start
, *end
, response_keep
);
1695 * Function to release a label chunk
1697 * @param zclient Zclient used to connect to label manager (zebra)
1698 * @param start First label of chunk
1699 * @param end Last label of chunk
1700 * @result 0 on success, -1 otherwise
1703 lm_release_label_chunk (struct zclient
*zclient
, uint32_t start
, uint32_t end
)
1709 zlog_debug ("Releasing Label Chunk");
1711 if (zclient
->sock
< 0)
1717 zclient_create_header (s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
1720 stream_putl (s
, start
);
1722 stream_putl (s
, end
);
1724 /* Put length at the first point of the stream. */
1725 stream_putw_at(s
, 0, stream_get_endp(s
));
1727 ret
= writen (zclient
->sock
, s
->data
, stream_get_endp (s
));
1730 zlog_err ("%s: can't write to zclient->sock", __func__
);
1731 close (zclient
->sock
);
1737 zlog_err ("%s: zclient->sock connection closed", __func__
);
1738 close (zclient
->sock
);
1746 /* Zebra client message read function. */
1748 zclient_read (struct thread
*thread
)
1751 uint16_t length
, command
;
1752 uint8_t marker
, version
;
1754 struct zclient
*zclient
;
1756 /* Get socket to zebra. */
1757 zclient
= THREAD_ARG (thread
);
1758 zclient
->t_read
= NULL
;
1760 /* Read zebra header (if we don't have it already). */
1761 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1764 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1765 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1769 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1770 return zclient_failed(zclient
);
1772 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1774 /* Try again later. */
1775 zclient_event (ZCLIENT_READ
, zclient
);
1778 already
= ZEBRA_HEADER_SIZE
;
1781 /* Reset to read from the beginning of the incoming packet. */
1782 stream_set_getp(zclient
->ibuf
, 0);
1784 /* Fetch header values. */
1785 length
= stream_getw (zclient
->ibuf
);
1786 marker
= stream_getc (zclient
->ibuf
);
1787 version
= stream_getc (zclient
->ibuf
);
1788 vrf_id
= stream_getw (zclient
->ibuf
);
1789 command
= stream_getw (zclient
->ibuf
);
1791 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1793 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1794 __func__
, zclient
->sock
, marker
, version
);
1795 return zclient_failed(zclient
);
1798 if (length
< ZEBRA_HEADER_SIZE
)
1800 zlog_err("%s: socket %d message length %u is less than %d ",
1801 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1802 return zclient_failed(zclient
);
1806 if (length
> STREAM_SIZE(zclient
->ibuf
))
1809 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1810 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1811 ns
= stream_new(length
);
1812 stream_copy(ns
, zclient
->ibuf
);
1813 stream_free (zclient
->ibuf
);
1817 /* Read rest of zebra packet. */
1818 if (already
< length
)
1821 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1822 length
-already
)) == 0) ||
1826 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1827 return zclient_failed(zclient
);
1829 if (nbyte
!= (ssize_t
)(length
-already
))
1831 /* Try again later. */
1832 zclient_event (ZCLIENT_READ
, zclient
);
1837 length
-= ZEBRA_HEADER_SIZE
;
1840 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient
, command
, vrf_id
);
1844 case ZEBRA_ROUTER_ID_UPDATE
:
1845 if (zclient
->router_id_update
)
1846 (*zclient
->router_id_update
) (command
, zclient
, length
, vrf_id
);
1849 zclient_vrf_add (zclient
, vrf_id
);
1851 case ZEBRA_VRF_DELETE
:
1852 zclient_vrf_delete (zclient
, vrf_id
);
1854 case ZEBRA_INTERFACE_ADD
:
1855 if (zclient
->interface_add
)
1856 (*zclient
->interface_add
) (command
, zclient
, length
, vrf_id
);
1858 case ZEBRA_INTERFACE_DELETE
:
1859 if (zclient
->interface_delete
)
1860 (*zclient
->interface_delete
) (command
, zclient
, length
, vrf_id
);
1862 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1863 if (zclient
->interface_address_add
)
1864 (*zclient
->interface_address_add
) (command
, zclient
, length
, vrf_id
);
1866 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1867 if (zclient
->interface_address_delete
)
1868 (*zclient
->interface_address_delete
) (command
, zclient
, length
, vrf_id
);
1870 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
1871 if (zclient
->interface_bfd_dest_update
)
1872 (*zclient
->interface_bfd_dest_update
) (command
, zclient
, length
, vrf_id
);
1874 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1875 if (zclient
->interface_nbr_address_add
)
1876 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
, vrf_id
);
1878 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1879 if (zclient
->interface_nbr_address_delete
)
1880 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
, vrf_id
);
1882 case ZEBRA_INTERFACE_UP
:
1883 if (zclient
->interface_up
)
1884 (*zclient
->interface_up
) (command
, zclient
, length
, vrf_id
);
1886 case ZEBRA_INTERFACE_DOWN
:
1887 if (zclient
->interface_down
)
1888 (*zclient
->interface_down
) (command
, zclient
, length
, vrf_id
);
1890 case ZEBRA_INTERFACE_VRF_UPDATE
:
1891 if (zclient
->interface_vrf_update
)
1892 (*zclient
->interface_vrf_update
) (command
, zclient
, length
, vrf_id
);
1894 case ZEBRA_NEXTHOP_UPDATE
:
1896 zlog_debug("zclient rcvd nexthop update\n");
1897 if (zclient
->nexthop_update
)
1898 (*zclient
->nexthop_update
) (command
, zclient
, length
, vrf_id
);
1900 case ZEBRA_IMPORT_CHECK_UPDATE
:
1902 zlog_debug("zclient rcvd import check update\n");
1903 if (zclient
->import_check_update
)
1904 (*zclient
->import_check_update
) (command
, zclient
, length
, vrf_id
);
1906 case ZEBRA_BFD_DEST_REPLAY
:
1907 if (zclient
->bfd_dest_replay
)
1908 (*zclient
->bfd_dest_replay
) (command
, zclient
, length
, vrf_id
);
1910 case ZEBRA_REDISTRIBUTE_IPV4_ADD
:
1911 if (zclient
->redistribute_route_ipv4_add
)
1912 (*zclient
->redistribute_route_ipv4_add
) (command
, zclient
, length
, vrf_id
);
1914 case ZEBRA_REDISTRIBUTE_IPV4_DEL
:
1915 if (zclient
->redistribute_route_ipv4_del
)
1916 (*zclient
->redistribute_route_ipv4_del
) (command
, zclient
, length
, vrf_id
);
1918 case ZEBRA_REDISTRIBUTE_IPV6_ADD
:
1919 if (zclient
->redistribute_route_ipv6_add
)
1920 (*zclient
->redistribute_route_ipv6_add
) (command
, zclient
, length
, vrf_id
);
1922 case ZEBRA_REDISTRIBUTE_IPV6_DEL
:
1923 if (zclient
->redistribute_route_ipv6_del
)
1924 (*zclient
->redistribute_route_ipv6_del
) (command
, zclient
, length
, vrf_id
);
1926 case ZEBRA_INTERFACE_LINK_PARAMS
:
1927 if (zclient
->interface_link_params
)
1928 (*zclient
->interface_link_params
) (command
, zclient
, length
);
1930 case ZEBRA_FEC_UPDATE
:
1932 zlog_debug("zclient rcvd fec update\n");
1933 if (zclient
->fec_update
)
1934 (*zclient
->fec_update
) (command
, zclient
, length
);
1940 if (zclient
->sock
< 0)
1941 /* Connection was closed during packet processing. */
1944 /* Register read thread. */
1945 stream_reset(zclient
->ibuf
);
1946 zclient_event (ZCLIENT_READ
, zclient
);
1952 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1953 u_short instance
, vrf_id_t vrf_id
)
1957 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1959 if (redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1961 redist_add_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1965 if (!redist_check_instance(&zclient
->mi_redist
[afi
][type
], instance
))
1967 redist_del_instance(&zclient
->mi_redist
[afi
][type
], instance
);
1971 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1973 if (vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1975 vrf_bitmap_set (zclient
->redist
[afi
][type
], vrf_id
);
1979 if (!vrf_bitmap_check (zclient
->redist
[afi
][type
], vrf_id
))
1981 vrf_bitmap_unset (zclient
->redist
[afi
][type
], vrf_id
);
1985 if (zclient
->sock
> 0)
1986 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
, vrf_id
);
1991 zclient_redistribute_default (int command
, struct zclient
*zclient
,
1995 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1997 if (vrf_bitmap_check (zclient
->default_information
, vrf_id
))
1999 vrf_bitmap_set (zclient
->default_information
, vrf_id
);
2003 if (!vrf_bitmap_check (zclient
->default_information
, vrf_id
))
2005 vrf_bitmap_unset (zclient
->default_information
, vrf_id
);
2008 if (zclient
->sock
> 0)
2009 zebra_message_send (zclient
, command
, vrf_id
);
2013 zclient_event (enum event event
, struct zclient
*zclient
)
2017 case ZCLIENT_SCHEDULE
:
2018 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2019 &zclient
->t_connect
);
2021 case ZCLIENT_CONNECT
:
2023 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
2024 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2025 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2026 zclient
->fail
< 3 ? 10 : 60, &zclient
->t_connect
);
2029 zclient
->t_read
= NULL
;
2030 thread_add_read(zclient
->master
, zclient_read
, zclient
, zclient
->sock
,
2036 const char *zclient_serv_path_get()
2038 return zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
;
2042 zclient_serv_path_set (char *path
)
2047 zclient_serv_path
= NULL
;
2049 /* test if `path' is socket. don't set it otherwise. */
2050 if (stat(path
, &sb
) == -1)
2052 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
2056 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
2058 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
2062 /* it seems that path is unix socket */
2063 zclient_serv_path
= path
;