1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36 /* Zebra client events. */
37 enum event
{ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
39 /* Prototype for event manager. */
40 static void zclient_event (enum event
, struct zclient
*);
42 extern struct thread_master
*master
;
44 char *zclient_serv_path
= NULL
;
46 /* This file local debug flag. */
47 int zclient_debug
= 0;
49 /* Allocate zclient structure. */
53 struct zclient
*zclient
;
54 zclient
= XCALLOC (MTYPE_ZCLIENT
, sizeof (struct zclient
));
56 zclient
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
57 zclient
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
58 zclient
->wb
= buffer_new(0);
63 /* This function is only called when exiting, because
64 many parts of the code do not check for I/O errors, so they could
65 reference an invalid pointer if the structure was ever freed.
67 Free zclient structure. */
69 zclient_free (struct zclient
*zclient
)
72 stream_free(zclient
->ibuf
);
74 stream_free(zclient
->obuf
);
76 buffer_free(zclient
->wb
);
78 XFREE (MTYPE_ZCLIENT
, zclient
);
82 redist_check_instance (struct redist_proto
*red
, u_short instance
)
84 struct listnode
*node
;
90 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
98 redist_add_instance (struct redist_proto
*red
, u_short instance
)
105 red
->instances
= list_new();
107 in
= (u_short
*)calloc(1, sizeof(u_short
));
109 listnode_add (red
->instances
, in
);
113 redist_del_instance (struct redist_proto
*red
, u_short instance
)
115 struct listnode
*node
;
121 for (ALL_LIST_ELEMENTS_RO (red
->instances
, node
, id
))
127 listnode_delete(red
->instances
, id
);
128 if (!red
->instances
->count
)
131 list_free(red
->instances
);
132 red
->instances
= NULL
;
137 /* Stop zebra client services. */
139 zclient_stop (struct zclient
*zclient
)
142 zlog_debug ("zclient stopped");
145 THREAD_OFF(zclient
->t_read
);
146 THREAD_OFF(zclient
->t_connect
);
147 THREAD_OFF(zclient
->t_write
);
150 stream_reset(zclient
->ibuf
);
151 stream_reset(zclient
->obuf
);
153 /* Empty the write buffer. */
154 buffer_reset(zclient
->wb
);
157 if (zclient
->sock
>= 0)
159 close (zclient
->sock
);
166 zclient_reset (struct zclient
*zclient
)
170 zclient_stop (zclient
);
172 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
173 redist_del_instance (&zclient
->redist
[afi
][zclient
->redist_default
], zclient
->instance
);
175 zclient_init (zclient
, zclient
->redist_default
, zclient
->instance
);
178 #ifdef HAVE_TCP_ZEBRA
180 /* Make socket to zebra daemon. Return zebra socket. */
186 struct sockaddr_in serv
;
188 /* We should think about IPv6 connection. */
189 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
193 /* Make server socket. */
194 memset (&serv
, 0, sizeof (struct sockaddr_in
));
195 serv
.sin_family
= AF_INET
;
196 serv
.sin_port
= htons (ZEBRA_PORT
);
197 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
198 serv
.sin_len
= sizeof (struct sockaddr_in
);
199 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
200 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
202 /* Connect to zebra. */
203 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
214 /* For sockaddr_un. */
218 zclient_socket_un (const char *path
)
222 struct sockaddr_un addr
;
224 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
228 /* Make server socket. */
229 memset (&addr
, 0, sizeof (struct sockaddr_un
));
230 addr
.sun_family
= AF_UNIX
;
231 strncpy (addr
.sun_path
, path
, strlen (path
));
232 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
233 len
= addr
.sun_len
= SUN_LEN(&addr
);
235 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
236 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
238 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
247 #endif /* HAVE_TCP_ZEBRA */
250 * Connect to zebra daemon.
251 * @param zclient a pointer to zclient structure
252 * @return socket fd just to make sure that connection established
257 zclient_socket_connect (struct zclient
*zclient
)
259 #ifdef HAVE_TCP_ZEBRA
260 zclient
->sock
= zclient_socket ();
262 zclient
->sock
= zclient_socket_un (zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
);
264 return zclient
->sock
;
268 zclient_failed(struct zclient
*zclient
)
271 zclient_stop(zclient
);
272 zclient_event(ZCLIENT_CONNECT
, zclient
);
277 zclient_flush_data(struct thread
*thread
)
279 struct zclient
*zclient
= THREAD_ARG(thread
);
281 zclient
->t_write
= NULL
;
282 if (zclient
->sock
< 0)
284 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
287 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
288 __func__
, zclient
->sock
);
289 return zclient_failed(zclient
);
292 zclient
->t_write
= thread_add_write(master
, zclient_flush_data
,
293 zclient
, zclient
->sock
);
302 zclient_send_message(struct zclient
*zclient
)
304 if (zclient
->sock
< 0)
306 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
307 stream_get_endp(zclient
->obuf
)))
310 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
311 __func__
, zclient
->sock
);
312 return zclient_failed(zclient
);
315 THREAD_OFF(zclient
->t_write
);
318 THREAD_WRITE_ON(master
, zclient
->t_write
,
319 zclient_flush_data
, zclient
, zclient
->sock
);
326 zclient_create_header (struct stream
*s
, uint16_t command
)
328 /* length placeholder, caller can update */
329 stream_putw (s
, ZEBRA_HEADER_SIZE
);
330 stream_putc (s
, ZEBRA_HEADER_MARKER
);
331 stream_putc (s
, ZSERV_VERSION
);
332 stream_putw (s
, command
);
335 /* Send simple Zebra message. */
337 zebra_message_send (struct zclient
*zclient
, int command
)
341 /* Get zclient output buffer. */
345 /* Send very simple command only Zebra message. */
346 zclient_create_header (s
, command
);
348 return zclient_send_message(zclient
);
352 zebra_hello_send (struct zclient
*zclient
)
356 if (zclient
->redist_default
)
361 zclient_create_header (s
, ZEBRA_HELLO
);
362 stream_putc (s
, zclient
->redist_default
);
363 stream_putw (s
, zclient
->instance
);
364 stream_putw_at (s
, 0, stream_get_endp (s
));
365 return zclient_send_message(zclient
);
371 /* Make connection to zebra daemon. */
373 zclient_start (struct zclient
*zclient
)
378 /* If already connected to the zebra. */
379 if (zclient
->sock
>= 0)
382 /* Check connect thread. */
383 if (zclient
->t_connect
)
386 /* Flush all redistribute request. */
387 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
388 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
389 if (zclient
->redist
[afi
][i
].enabled
)
391 struct listnode
*node
;
394 for (ALL_LIST_ELEMENTS_RO(zclient
->redist
[afi
][i
].instances
, node
, id
))
395 if (!(i
== zclient
->redist_default
&& *id
== zclient
->instance
))
396 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, afi
, i
, *id
);
399 /* If default information is needed. */
400 if (zclient
->default_information
)
401 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
);
406 /* This function is a wrapper function for calling zclient_start from
407 timer or event thread. */
409 zclient_connect (struct thread
*t
)
411 struct zclient
*zclient
;
413 zclient
= THREAD_ARG (t
);
414 zclient
->t_connect
= NULL
;
417 zlog_debug ("zclient_connect is called");
419 return zclient_start (zclient
);
422 /* Initialize zebra client. Argument redist_default is unwanted
423 redistribute route type. */
425 zclient_init (struct zclient
*zclient
, int redist_default
, u_short instance
)
429 /* Enable zebra client connection by default. */
432 /* Set -1 to the default socket value. */
435 /* Clear redistribution flags. */
436 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
437 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
438 memset(&zclient
->redist
[afi
][i
], 0, sizeof(struct redist_proto
));
440 /* Set unwanted redistribute route. bgpd does not need BGP route
442 zclient
->redist_default
= redist_default
;
443 zclient
->instance
= instance
;
444 /* Pending: make afi(s) an arg. */
445 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
446 redist_add_instance (&zclient
->redist
[afi
][redist_default
], instance
);
448 /* Set default-information redistribute to zero. */
449 zclient
->default_information
= 0;
452 zlog_debug ("zclient_start is called");
454 /* zclient is disabled. */
455 if (! zclient
->enable
)
458 if (zclient_socket_connect(zclient
) < 0)
461 zlog_debug ("zclient connection fail");
463 zclient_event (ZCLIENT_CONNECT
, zclient
);
467 if (set_nonblocking(zclient
->sock
) < 0)
468 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
470 /* Clear fail count. */
473 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
475 /* Create read thread. */
476 zclient_event (ZCLIENT_READ
, zclient
);
478 zebra_hello_send (zclient
);
480 /* We need router-id information. */
481 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
);
483 /* We need interface information. */
484 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
);
486 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
490 * "xdr_encode"-like interface that allows daemon (client) to send
491 * a message to zebra server for a route that needs to be
492 * added/deleted to the kernel. Info about the route is specified
493 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
494 * the info down the zclient socket using the stream_* functions.
496 * The corresponding read ("xdr_decode") function on the server
497 * side is zread_ipv4_add()/zread_ipv4_delete().
499 * 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
500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501 * | Length (2) | Command | Route Type |
502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * | ZEBRA Flags | Message Flags | Prefix length |
504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * | Destination IPv4 Prefix for route |
506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
511 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
512 * described, as per the Nexthop count. Each nexthop described as:
515 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
516 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 * | IPv4 Nexthop address or Interface Index number |
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
521 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
522 * nexthop information is provided, and the message describes a prefix
523 * to blackhole or reject route.
525 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
526 * infrastructure was built around the traditional (32-bit "gate OR
527 * ifindex") nexthop data unit. A special encoding can be used to feed
528 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
529 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
531 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
532 * - .nexthop_num == .ifindex_num
533 * - .nexthop and .ifindex are filled with gate and ifindex parts of
534 * each compound nexthop, both in the same order
536 * zapi_ipv4_route() will produce two nexthop data units for each such
537 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
538 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
540 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
543 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
546 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 2 byte value
548 * XXX: No attention paid to alignment.
551 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
552 struct zapi_ipv4
*api
)
562 zclient_create_header (s
, cmd
);
564 /* Put type and nexthop. */
565 stream_putc (s
, api
->type
);
566 stream_putw (s
, api
->instance
);
567 stream_putc (s
, api
->flags
);
568 stream_putc (s
, api
->message
);
569 stream_putw (s
, api
->safi
);
571 /* Put prefix information. */
572 psize
= PSIZE (p
->prefixlen
);
573 stream_putc (s
, p
->prefixlen
);
574 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
576 /* Nexthop, ifindex, distance and metric information. */
577 /* ZAPI_MESSAGE_ONLINK implies interleaving */
578 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_ONLINK
))
580 /* ZAPI_MESSAGE_NEXTHOP is required for proper receiving */
581 assert (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
));
582 /* 64-bit data units, interleaved between nexthop[] and ifindex[] */
583 assert (api
->nexthop_num
== api
->ifindex_num
);
584 stream_putc (s
, api
->nexthop_num
* 2);
585 for (i
= 0; i
< api
->nexthop_num
; i
++)
587 stream_putc (s
, ZEBRA_NEXTHOP_IPV4_ONLINK
);
588 stream_put_in_addr (s
, api
->nexthop
[i
]);
589 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
590 stream_putl (s
, api
->ifindex
[i
]);
593 else if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
595 /* traditional 32-bit data units */
596 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
599 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
600 /* XXX assert(api->nexthop_num == 0); */
601 /* XXX assert(api->ifindex_num == 0); */
604 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
606 for (i
= 0; i
< api
->nexthop_num
; i
++)
608 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
609 stream_put_in_addr (s
, api
->nexthop
[i
]);
611 for (i
= 0; i
< api
->ifindex_num
; i
++)
613 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
614 stream_putl (s
, api
->ifindex
[i
]);
618 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
619 stream_putc (s
, api
->distance
);
620 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
621 stream_putl (s
, api
->metric
);
622 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
623 stream_putw (s
, api
->tag
);
625 /* Put length at the first point of the stream. */
626 stream_putw_at (s
, 0, stream_get_endp (s
));
628 return zclient_send_message(zclient
);
633 zapi_ipv4_route_ipv6_nexthop (u_char cmd
, struct zclient
*zclient
,
634 struct prefix_ipv4
*p
, struct zapi_ipv6
*api
)
644 zclient_create_header (s
, cmd
);
646 /* Put type and nexthop. */
647 stream_putc (s
, api
->type
);
648 stream_putw (s
, api
->instance
);
649 stream_putc (s
, api
->flags
);
650 stream_putc (s
, api
->message
);
651 stream_putw (s
, api
->safi
);
653 /* Put prefix information. */
654 psize
= PSIZE (p
->prefixlen
);
655 stream_putc (s
, p
->prefixlen
);
656 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
658 /* Nexthop, ifindex, distance and metric information. */
659 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
661 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
664 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
665 /* XXX assert(api->nexthop_num == 0); */
666 /* XXX assert(api->ifindex_num == 0); */
669 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
671 for (i
= 0; i
< api
->nexthop_num
; i
++)
673 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
674 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
676 for (i
= 0; i
< api
->ifindex_num
; i
++)
678 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
679 stream_putl (s
, api
->ifindex
[i
]);
683 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
684 stream_putc (s
, api
->distance
);
685 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
686 stream_putl (s
, api
->metric
);
687 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
688 stream_putw (s
, api
->tag
);
690 /* Put length at the first point of the stream. */
691 stream_putw_at (s
, 0, stream_get_endp (s
));
693 return zclient_send_message(zclient
);
697 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
698 struct zapi_ipv6
*api
)
708 zclient_create_header (s
, cmd
);
710 /* Put type and nexthop. */
711 stream_putc (s
, api
->type
);
712 stream_putw (s
, api
->instance
);
713 stream_putc (s
, api
->flags
);
714 stream_putc (s
, api
->message
);
715 stream_putw (s
, api
->safi
);
717 /* Put prefix information. */
718 psize
= PSIZE (p
->prefixlen
);
719 stream_putc (s
, p
->prefixlen
);
720 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
722 /* Nexthop, ifindex, distance and metric information. */
723 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
725 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
728 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
729 /* XXX assert(api->nexthop_num == 0); */
730 /* XXX assert(api->ifindex_num == 0); */
733 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
735 for (i
= 0; i
< api
->nexthop_num
; i
++)
737 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
738 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
740 for (i
= 0; i
< api
->ifindex_num
; i
++)
742 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
743 stream_putl (s
, api
->ifindex
[i
]);
747 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
748 stream_putc (s
, api
->distance
);
749 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
750 stream_putl (s
, api
->metric
);
751 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_TAG
))
752 stream_putw (s
, api
->tag
);
754 /* Put length at the first point of the stream. */
755 stream_putw_at (s
, 0, stream_get_endp (s
));
757 return zclient_send_message(zclient
);
759 #endif /* HAVE_IPV6 */
762 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
763 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
764 * then set/unset redist[type] in the client handle (a struct zserv) for the
768 zebra_redistribute_send (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
776 zclient_create_header (s
, command
);
777 stream_putc (s
, afi
);
778 stream_putc (s
, type
);
779 stream_putw (s
, instance
);
781 stream_putw_at (s
, 0, stream_get_endp (s
));
783 return zclient_send_message(zclient
);
786 /* Router-id update from zebra daemon. */
788 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
792 /* Fetch interface address. */
793 rid
->family
= stream_getc (s
);
795 plen
= prefix_blen (rid
);
796 stream_get (&rid
->u
.prefix
, s
, plen
);
797 rid
->prefixlen
= stream_getc (s
);
800 /* Interface addition from zebra daemon. */
802 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
803 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
805 * 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
808 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
814 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
816 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
819 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
821 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
823 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
825 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
827 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
829 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
833 zebra_interface_add_read (struct stream
*s
)
835 struct interface
*ifp
;
836 char ifname_tmp
[INTERFACE_NAMSIZ
];
838 /* Read interface name. */
839 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
841 /* Lookup/create interface by name. */
842 ifp
= if_get_by_name_len (ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
844 zebra_interface_if_set_value (s
, ifp
);
850 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
851 * from zebra server. The format of this message is the same as
852 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
853 * comments for zebra_interface_add_read), except that no sockaddr_dl
854 * is sent at the tail of the message.
857 zebra_interface_state_read (struct stream
*s
)
859 struct interface
*ifp
;
860 char ifname_tmp
[INTERFACE_NAMSIZ
];
862 /* Read interface name. */
863 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
865 /* Lookup this by interface index. */
866 ifp
= if_lookup_by_name_len (ifname_tmp
,
867 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
869 /* If such interface does not exist, indicate an error */
873 zebra_interface_if_set_value (s
, ifp
);
879 * format of message for address additon is:
883 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
884 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
893 * | ifc_flags | flags for connected address
901 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
911 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
913 /* Read interface's index. */
914 ifp
->ifindex
= stream_getl (s
);
915 ifp
->status
= stream_getc (s
);
917 /* Read interface's value. */
918 ifp
->flags
= stream_getq (s
);
919 ifp
->ptm_enable
= stream_getc (s
);
920 ifp
->ptm_status
= stream_getc (s
);
921 ifp
->metric
= stream_getl (s
);
922 ifp
->mtu
= stream_getl (s
);
923 ifp
->mtu6
= stream_getl (s
);
924 ifp
->bandwidth
= stream_getl (s
);
925 #ifdef HAVE_STRUCT_SOCKADDR_DL
926 stream_get (&ifp
->sdl
, s
, sizeof (ifp
->sdl_storage
));
928 ifp
->hw_addr_len
= stream_getl (s
);
929 if (ifp
->hw_addr_len
)
930 stream_get (ifp
->hw_addr
, s
, ifp
->hw_addr_len
);
931 #endif /* HAVE_STRUCT_SOCKADDR_DL */
935 memconstant(const void *s
, int c
, size_t n
)
946 zebra_interface_bfd_read (struct stream
*s
, struct prefix
*p
)
948 unsigned int ifindex
;
949 struct interface
*ifp
;
952 /* Get interface index. */
953 ifindex
= stream_getl (s
);
956 ifp
= if_lookup_by_index (ifindex
);
959 zlog_warn ("zebra_interface_bfd_read: "
960 "Can't find interface by ifindex: %d ", ifindex
);
964 /* Fetch interface address. */
965 p
->family
= stream_getc (s
);
967 plen
= prefix_blen (p
);
968 stream_get (&p
->u
.prefix
, s
, plen
);
969 p
->prefixlen
= stream_getc (s
);
975 zebra_interface_address_read (int type
, struct stream
*s
)
977 unsigned int ifindex
;
978 struct interface
*ifp
;
979 struct connected
*ifc
;
985 memset (&p
, 0, sizeof(p
));
986 memset (&d
, 0, sizeof(d
));
988 /* Get interface index. */
989 ifindex
= stream_getl (s
);
992 ifp
= if_lookup_by_index (ifindex
);
995 zlog_warn ("zebra_interface_address_read(%s): "
996 "Can't find interface by ifindex: %d ",
997 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
? "ADD" : "DELETE"),
1003 ifc_flags
= stream_getc (s
);
1005 /* Fetch interface address. */
1006 family
= p
.family
= stream_getc (s
);
1008 plen
= prefix_blen (&p
);
1009 stream_get (&p
.u
.prefix
, s
, plen
);
1010 p
.prefixlen
= stream_getc (s
);
1012 /* Fetch destination address. */
1013 stream_get (&d
.u
.prefix
, s
, plen
);
1016 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
1018 /* N.B. NULL destination pointers are encoded as all zeroes */
1019 ifc
= connected_add_by_prefix(ifp
, &p
,(memconstant(&d
.u
.prefix
,0,plen
) ?
1023 ifc
->flags
= ifc_flags
;
1024 if (ifc
->destination
)
1025 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
1026 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
1028 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1030 prefix2str (ifc
->address
, buf
, sizeof(buf
));
1031 zlog_warn("warning: interface %s address %s "
1032 "with peer flag set, but no peer address!",
1034 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1040 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1041 ifc
= connected_delete_by_prefix(ifp
, &p
);
1048 * format of message for neighbor connected address is:
1052 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1053 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1068 * | addr_len | len of addr.
1071 struct nbr_connected
*
1072 zebra_interface_nbr_address_read (int type
, struct stream
*s
)
1074 unsigned int ifindex
;
1075 struct interface
*ifp
;
1077 struct nbr_connected
*ifc
;
1079 /* Get interface index. */
1080 ifindex
= stream_getl (s
);
1083 ifp
= if_lookup_by_index (ifindex
);
1086 zlog_warn ("zebra_nbr_interface_address_read(%s): "
1087 "Can't find interface by ifindex: %d ",
1088 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
? "ADD" : "DELETE"),
1093 p
.family
= stream_getc (s
);
1094 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
1095 p
.prefixlen
= stream_getc (s
);
1097 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
1099 /* Currently only supporting P2P links, so any new RA source address is
1100 considered as the replacement of the previously learnt Link-Local address. */
1101 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
1103 ifc
= nbr_connected_new ();
1104 ifc
->address
= prefix_new ();
1106 listnode_add (ifp
->nbr_connected
, ifc
);
1109 prefix_copy(ifc
->address
, &p
);
1113 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1115 ifc
= nbr_connected_check(ifp
, &p
);
1117 listnode_delete (ifp
->nbr_connected
, ifc
);
1123 /* Zebra client message read function. */
1125 zclient_read (struct thread
*thread
)
1128 uint16_t length
, command
;
1129 uint8_t marker
, version
;
1130 struct zclient
*zclient
;
1132 /* Get socket to zebra. */
1133 zclient
= THREAD_ARG (thread
);
1134 zclient
->t_read
= NULL
;
1136 /* Read zebra header (if we don't have it already). */
1137 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
1140 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1141 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
1145 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
1146 return zclient_failed(zclient
);
1148 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
1150 /* Try again later. */
1151 zclient_event (ZCLIENT_READ
, zclient
);
1154 already
= ZEBRA_HEADER_SIZE
;
1157 /* Reset to read from the beginning of the incoming packet. */
1158 stream_set_getp(zclient
->ibuf
, 0);
1160 /* Fetch header values. */
1161 length
= stream_getw (zclient
->ibuf
);
1162 marker
= stream_getc (zclient
->ibuf
);
1163 version
= stream_getc (zclient
->ibuf
);
1164 command
= stream_getw (zclient
->ibuf
);
1166 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
1168 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1169 __func__
, zclient
->sock
, marker
, version
);
1170 return zclient_failed(zclient
);
1173 if (length
< ZEBRA_HEADER_SIZE
)
1175 zlog_err("%s: socket %d message length %u is less than %d ",
1176 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
1177 return zclient_failed(zclient
);
1181 if (length
> STREAM_SIZE(zclient
->ibuf
))
1184 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1185 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
1186 ns
= stream_new(length
);
1187 stream_copy(ns
, zclient
->ibuf
);
1188 stream_free (zclient
->ibuf
);
1192 /* Read rest of zebra packet. */
1193 if (already
< length
)
1196 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
1197 length
-already
)) == 0) ||
1201 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
1202 return zclient_failed(zclient
);
1204 if (nbyte
!= (ssize_t
)(length
-already
))
1206 /* Try again later. */
1207 zclient_event (ZCLIENT_READ
, zclient
);
1212 length
-= ZEBRA_HEADER_SIZE
;
1215 zlog_debug("zclient 0x%p command 0x%x \n", zclient
, command
);
1219 case ZEBRA_ROUTER_ID_UPDATE
:
1220 if (zclient
->router_id_update
)
1221 (*zclient
->router_id_update
) (command
, zclient
, length
);
1223 case ZEBRA_INTERFACE_ADD
:
1224 if (zclient
->interface_add
)
1225 (*zclient
->interface_add
) (command
, zclient
, length
);
1227 case ZEBRA_INTERFACE_DELETE
:
1228 if (zclient
->interface_delete
)
1229 (*zclient
->interface_delete
) (command
, zclient
, length
);
1231 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1232 if (zclient
->interface_address_add
)
1233 (*zclient
->interface_address_add
) (command
, zclient
, length
);
1235 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1236 if (zclient
->interface_address_delete
)
1237 (*zclient
->interface_address_delete
) (command
, zclient
, length
);
1239 case ZEBRA_INTERFACE_BFD_DEST_DOWN
:
1240 if (zclient
->interface_bfd_dest_down
)
1241 (*zclient
->interface_bfd_dest_down
) (command
, zclient
, length
);
1243 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1244 if (zclient
->interface_nbr_address_add
)
1245 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
);
1247 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1248 if (zclient
->interface_nbr_address_delete
)
1249 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
);
1251 case ZEBRA_INTERFACE_UP
:
1252 if (zclient
->interface_up
)
1253 (*zclient
->interface_up
) (command
, zclient
, length
);
1255 case ZEBRA_INTERFACE_DOWN
:
1256 if (zclient
->interface_down
)
1257 (*zclient
->interface_down
) (command
, zclient
, length
);
1259 case ZEBRA_IPV4_ROUTE_ADD
:
1260 if (zclient
->ipv4_route_add
)
1261 (*zclient
->ipv4_route_add
) (command
, zclient
, length
);
1263 case ZEBRA_IPV4_ROUTE_DELETE
:
1264 if (zclient
->ipv4_route_delete
)
1265 (*zclient
->ipv4_route_delete
) (command
, zclient
, length
);
1267 case ZEBRA_IPV6_ROUTE_ADD
:
1268 if (zclient
->ipv6_route_add
)
1269 (*zclient
->ipv6_route_add
) (command
, zclient
, length
);
1271 case ZEBRA_IPV6_ROUTE_DELETE
:
1272 if (zclient
->ipv6_route_delete
)
1273 (*zclient
->ipv6_route_delete
) (command
, zclient
, length
);
1275 case ZEBRA_NEXTHOP_UPDATE
:
1277 zlog_debug("zclient rcvd nexthop update\n");
1278 if (zclient
->nexthop_update
)
1279 (*zclient
->nexthop_update
) (command
, zclient
, length
);
1281 case ZEBRA_IMPORT_CHECK_UPDATE
:
1283 zlog_debug("zclient rcvd import check update\n");
1284 if (zclient
->import_check_update
)
1285 (*zclient
->import_check_update
) (command
, zclient
, length
);
1291 if (zclient
->sock
< 0)
1292 /* Connection was closed during packet processing. */
1295 /* Register read thread. */
1296 stream_reset(zclient
->ibuf
);
1297 zclient_event (ZCLIENT_READ
, zclient
);
1303 zclient_redistribute (int command
, struct zclient
*zclient
, afi_t afi
, int type
,
1307 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1309 if (redist_check_instance(&zclient
->redist
[afi
][type
], instance
))
1311 redist_add_instance(&zclient
->redist
[afi
][type
], instance
);
1315 if (!redist_check_instance(&zclient
->redist
[afi
][type
], instance
))
1317 redist_del_instance(&zclient
->redist
[afi
][type
], instance
);
1320 if (zclient
->sock
> 0)
1321 zebra_redistribute_send (command
, zclient
, afi
, type
, instance
);
1326 zclient_redistribute_default (int command
, struct zclient
*zclient
)
1329 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1331 if (zclient
->default_information
)
1333 zclient
->default_information
= 1;
1337 if (!zclient
->default_information
)
1339 zclient
->default_information
= 0;
1342 if (zclient
->sock
> 0)
1343 zebra_message_send (zclient
, command
);
1347 zclient_event (enum event event
, struct zclient
*zclient
)
1351 case ZCLIENT_SCHEDULE
:
1352 if (! zclient
->t_connect
)
1353 zclient
->t_connect
=
1354 thread_add_event (master
, zclient_connect
, zclient
, 0);
1356 case ZCLIENT_CONNECT
:
1357 if (zclient
->fail
>= 10)
1360 zlog_debug ("zclient connect schedule interval is %d",
1361 zclient
->fail
< 3 ? 10 : 60);
1362 if (! zclient
->t_connect
)
1363 zclient
->t_connect
=
1364 thread_add_timer (master
, zclient_connect
, zclient
,
1365 zclient
->fail
< 3 ? 10 : 60);
1369 thread_add_read (master
, zclient_read
, zclient
, zclient
->sock
);
1375 zclient_serv_path_set (char *path
)
1380 zclient_serv_path
= NULL
;
1382 /* test if `path' is socket. don't set it otherwise. */
1383 if (stat(path
, &sb
) == -1)
1385 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
1389 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
1391 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
1395 /* it seems that path is unix socket */
1396 zclient_serv_path
= path
;