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
);
81 /* Initialize zebra client. Argument redist_default is unwanted
82 redistribute route type. */
84 zclient_init (struct zclient
*zclient
, int redist_default
)
88 /* Enable zebra client connection by default. */
91 /* Set -1 to the default socket value. */
94 /* Clear redistribution flags. */
95 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
96 zclient
->redist
[i
] = 0;
98 /* Set unwanted redistribute route. bgpd does not need BGP route
100 zclient
->redist_default
= redist_default
;
101 zclient
->redist
[redist_default
] = 1;
103 /* Set default-information redistribute to zero. */
104 zclient
->default_information
= 0;
106 /* Schedule first zclient connection. */
108 zlog_debug ("zclient start scheduled");
110 zclient_event (ZCLIENT_SCHEDULE
, zclient
);
113 /* Stop zebra client services. */
115 zclient_stop (struct zclient
*zclient
)
118 zlog_debug ("zclient stopped");
121 THREAD_OFF(zclient
->t_read
);
122 THREAD_OFF(zclient
->t_connect
);
123 THREAD_OFF(zclient
->t_write
);
126 stream_reset(zclient
->ibuf
);
127 stream_reset(zclient
->obuf
);
129 /* Empty the write buffer. */
130 buffer_reset(zclient
->wb
);
133 if (zclient
->sock
>= 0)
135 close (zclient
->sock
);
142 zclient_reset (struct zclient
*zclient
)
144 zclient_stop (zclient
);
145 zclient_init (zclient
, zclient
->redist_default
);
148 #ifdef HAVE_TCP_ZEBRA
150 /* Make socket to zebra daemon. Return zebra socket. */
156 struct sockaddr_in serv
;
158 /* We should think about IPv6 connection. */
159 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
163 /* Make server socket. */
164 memset (&serv
, 0, sizeof (struct sockaddr_in
));
165 serv
.sin_family
= AF_INET
;
166 serv
.sin_port
= htons (ZEBRA_PORT
);
167 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
168 serv
.sin_len
= sizeof (struct sockaddr_in
);
169 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
170 serv
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
172 /* Connect to zebra. */
173 ret
= connect (sock
, (struct sockaddr
*) &serv
, sizeof (serv
));
184 /* For sockaddr_un. */
188 zclient_socket_un (const char *path
)
192 struct sockaddr_un addr
;
194 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
198 /* Make server socket. */
199 memset (&addr
, 0, sizeof (struct sockaddr_un
));
200 addr
.sun_family
= AF_UNIX
;
201 strncpy (addr
.sun_path
, path
, strlen (path
));
202 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
203 len
= addr
.sun_len
= SUN_LEN(&addr
);
205 len
= sizeof (addr
.sun_family
) + strlen (addr
.sun_path
);
206 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
208 ret
= connect (sock
, (struct sockaddr
*) &addr
, len
);
217 #endif /* HAVE_TCP_ZEBRA */
220 * Connect to zebra daemon.
221 * @param zclient a pointer to zclient structure
222 * @return socket fd just to make sure that connection established
227 zclient_socket_connect (struct zclient
*zclient
)
229 #ifdef HAVE_TCP_ZEBRA
230 zclient
->sock
= zclient_socket ();
232 zclient
->sock
= zclient_socket_un (zclient_serv_path
? zclient_serv_path
: ZEBRA_SERV_PATH
);
234 return zclient
->sock
;
238 zclient_failed(struct zclient
*zclient
)
241 zclient_stop(zclient
);
242 zclient_event(ZCLIENT_CONNECT
, zclient
);
247 zclient_flush_data(struct thread
*thread
)
249 struct zclient
*zclient
= THREAD_ARG(thread
);
251 zclient
->t_write
= NULL
;
252 if (zclient
->sock
< 0)
254 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
))
257 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
258 __func__
, zclient
->sock
);
259 return zclient_failed(zclient
);
262 zclient
->t_write
= thread_add_write(master
, zclient_flush_data
,
263 zclient
, zclient
->sock
);
272 zclient_send_message(struct zclient
*zclient
)
274 if (zclient
->sock
< 0)
276 switch (buffer_write(zclient
->wb
, zclient
->sock
, STREAM_DATA(zclient
->obuf
),
277 stream_get_endp(zclient
->obuf
)))
280 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
281 __func__
, zclient
->sock
);
282 return zclient_failed(zclient
);
285 THREAD_OFF(zclient
->t_write
);
288 THREAD_WRITE_ON(master
, zclient
->t_write
,
289 zclient_flush_data
, zclient
, zclient
->sock
);
296 zclient_create_header (struct stream
*s
, uint16_t command
)
298 /* length placeholder, caller can update */
299 stream_putw (s
, ZEBRA_HEADER_SIZE
);
300 stream_putc (s
, ZEBRA_HEADER_MARKER
);
301 stream_putc (s
, ZSERV_VERSION
);
302 stream_putw (s
, command
);
305 /* Send simple Zebra message. */
307 zebra_message_send (struct zclient
*zclient
, int command
)
311 /* Get zclient output buffer. */
315 /* Send very simple command only Zebra message. */
316 zclient_create_header (s
, command
);
318 return zclient_send_message(zclient
);
322 zebra_hello_send (struct zclient
*zclient
)
326 if (zclient
->redist_default
)
331 zclient_create_header (s
, ZEBRA_HELLO
);
332 stream_putc (s
, zclient
->redist_default
);
333 stream_putw_at (s
, 0, stream_get_endp (s
));
334 return zclient_send_message(zclient
);
340 /* Make connection to zebra daemon. */
342 zclient_start (struct zclient
*zclient
)
347 zlog_debug ("zclient_start is called");
349 /* zclient is disabled. */
350 if (! zclient
->enable
)
353 /* If already connected to the zebra. */
354 if (zclient
->sock
>= 0)
357 /* Check connect thread. */
358 if (zclient
->t_connect
)
361 if (zclient_socket_connect(zclient
) < 0)
364 zlog_debug ("zclient connection fail");
366 zclient_event (ZCLIENT_CONNECT
, zclient
);
370 if (set_nonblocking(zclient
->sock
) < 0)
371 zlog_warn("%s: set_nonblocking(%d) failed", __func__
, zclient
->sock
);
373 /* Clear fail count. */
376 zlog_debug ("zclient connect success with socket [%d]", zclient
->sock
);
378 /* Create read thread. */
379 zclient_event (ZCLIENT_READ
, zclient
);
381 zebra_hello_send (zclient
);
383 /* We need router-id information. */
384 zebra_message_send (zclient
, ZEBRA_ROUTER_ID_ADD
);
386 /* We need interface information. */
387 zebra_message_send (zclient
, ZEBRA_INTERFACE_ADD
);
389 /* Flush all redistribute request. */
390 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
391 if (i
!= zclient
->redist_default
&& zclient
->redist
[i
])
392 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD
, zclient
, i
);
394 /* If default information is needed. */
395 if (zclient
->default_information
)
396 zebra_message_send (zclient
, ZEBRA_REDISTRIBUTE_DEFAULT_ADD
);
401 /* This function is a wrapper function for calling zclient_start from
402 timer or event thread. */
404 zclient_connect (struct thread
*t
)
406 struct zclient
*zclient
;
408 zclient
= THREAD_ARG (t
);
409 zclient
->t_connect
= NULL
;
412 zlog_debug ("zclient_connect is called");
414 return zclient_start (zclient
);
418 * "xdr_encode"-like interface that allows daemon (client) to send
419 * a message to zebra server for a route that needs to be
420 * added/deleted to the kernel. Info about the route is specified
421 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
422 * the info down the zclient socket using the stream_* functions.
424 * The corresponding read ("xdr_decode") function on the server
425 * side is zread_ipv4_add()/zread_ipv4_delete().
427 * 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
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 * | Length (2) | Command | Route Type |
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
431 * | ZEBRA Flags | Message Flags | Prefix length |
432 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433 * | Destination IPv4 Prefix for route |
434 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
439 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
440 * described, as per the Nexthop count. Each nexthop described as:
443 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
444 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
445 * | IPv4 Nexthop address or Interface Index number |
446 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
448 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
449 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
450 * nexthop information is provided, and the message describes a prefix
451 * to blackhole or reject route.
453 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
456 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
459 * XXX: No attention paid to alignment.
462 zapi_ipv4_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv4
*p
,
463 struct zapi_ipv4
*api
)
473 zclient_create_header (s
, cmd
);
475 /* Put type and nexthop. */
476 stream_putc (s
, api
->type
);
477 stream_putc (s
, api
->flags
);
478 stream_putc (s
, api
->message
);
479 stream_putw (s
, api
->safi
);
481 /* Put prefix information. */
482 psize
= PSIZE (p
->prefixlen
);
483 stream_putc (s
, p
->prefixlen
);
484 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
486 /* Nexthop, ifindex, distance and metric information. */
487 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
489 if (CHECK_FLAG (api
->flags
, ZEBRA_FLAG_BLACKHOLE
))
492 stream_putc (s
, ZEBRA_NEXTHOP_BLACKHOLE
);
493 /* XXX assert(api->nexthop_num == 0); */
494 /* XXX assert(api->ifindex_num == 0); */
497 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
499 for (i
= 0; i
< api
->nexthop_num
; i
++)
501 stream_putc (s
, ZEBRA_NEXTHOP_IPV4
);
502 stream_put_in_addr (s
, api
->nexthop
[i
]);
504 for (i
= 0; i
< api
->ifindex_num
; i
++)
506 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
507 stream_putl (s
, api
->ifindex
[i
]);
511 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
512 stream_putc (s
, api
->distance
);
513 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
514 stream_putl (s
, api
->metric
);
516 /* Put length at the first point of the stream. */
517 stream_putw_at (s
, 0, stream_get_endp (s
));
519 return zclient_send_message(zclient
);
524 zapi_ipv6_route (u_char cmd
, struct zclient
*zclient
, struct prefix_ipv6
*p
,
525 struct zapi_ipv6
*api
)
535 zclient_create_header (s
, cmd
);
537 /* Put type and nexthop. */
538 stream_putc (s
, api
->type
);
539 stream_putc (s
, api
->flags
);
540 stream_putc (s
, api
->message
);
541 stream_putw (s
, api
->safi
);
543 /* Put prefix information. */
544 psize
= PSIZE (p
->prefixlen
);
545 stream_putc (s
, p
->prefixlen
);
546 stream_write (s
, (u_char
*)&p
->prefix
, psize
);
548 /* Nexthop, ifindex, distance and metric information. */
549 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_NEXTHOP
))
551 stream_putc (s
, api
->nexthop_num
+ api
->ifindex_num
);
553 for (i
= 0; i
< api
->nexthop_num
; i
++)
555 stream_putc (s
, ZEBRA_NEXTHOP_IPV6
);
556 stream_write (s
, (u_char
*)api
->nexthop
[i
], 16);
558 for (i
= 0; i
< api
->ifindex_num
; i
++)
560 stream_putc (s
, ZEBRA_NEXTHOP_IFINDEX
);
561 stream_putl (s
, api
->ifindex
[i
]);
565 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_DISTANCE
))
566 stream_putc (s
, api
->distance
);
567 if (CHECK_FLAG (api
->message
, ZAPI_MESSAGE_METRIC
))
568 stream_putl (s
, api
->metric
);
570 /* Put length at the first point of the stream. */
571 stream_putw_at (s
, 0, stream_get_endp (s
));
573 return zclient_send_message(zclient
);
575 #endif /* HAVE_IPV6 */
578 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
579 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
580 * then set/unset redist[type] in the client handle (a struct zserv) for the
584 zebra_redistribute_send (int command
, struct zclient
*zclient
, int type
)
591 zclient_create_header (s
, command
);
592 stream_putc (s
, type
);
594 stream_putw_at (s
, 0, stream_get_endp (s
));
596 return zclient_send_message(zclient
);
599 /* Router-id update from zebra daemon. */
601 zebra_router_id_update_read (struct stream
*s
, struct prefix
*rid
)
605 /* Fetch interface address. */
606 rid
->family
= stream_getc (s
);
608 plen
= prefix_blen (rid
);
609 stream_get (&rid
->u
.prefix
, s
, plen
);
610 rid
->prefixlen
= stream_getc (s
);
613 /* Interface addition from zebra daemon. */
615 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
616 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
618 * 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
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646 zebra_interface_add_read (struct stream
*s
)
648 struct interface
*ifp
;
649 char ifname_tmp
[INTERFACE_NAMSIZ
];
651 /* Read interface name. */
652 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
654 /* Lookup/create interface by name. */
655 ifp
= if_get_by_name_len (ifname_tmp
, strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
657 zebra_interface_if_set_value (s
, ifp
);
663 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
664 * from zebra server. The format of this message is the same as
665 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
666 * comments for zebra_interface_add_read), except that no sockaddr_dl
667 * is sent at the tail of the message.
670 zebra_interface_state_read (struct stream
*s
)
672 struct interface
*ifp
;
673 char ifname_tmp
[INTERFACE_NAMSIZ
];
675 /* Read interface name. */
676 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
678 /* Lookup this by interface index. */
679 ifp
= if_lookup_by_name_len (ifname_tmp
,
680 strnlen(ifname_tmp
, INTERFACE_NAMSIZ
));
682 /* If such interface does not exist, indicate an error */
686 zebra_interface_if_set_value (s
, ifp
);
692 * format of message for address additon is:
696 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
697 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
706 * | ifc_flags | flags for connected address
714 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
724 zebra_interface_if_set_value (struct stream
*s
, struct interface
*ifp
)
726 /* Read interface's index. */
727 ifp
->ifindex
= stream_getl (s
);
728 ifp
->status
= stream_getc (s
);
730 /* Read interface's value. */
731 ifp
->flags
= stream_getq (s
);
732 ifp
->metric
= stream_getl (s
);
733 ifp
->mtu
= stream_getl (s
);
734 ifp
->mtu6
= stream_getl (s
);
735 ifp
->bandwidth
= stream_getl (s
);
736 #ifdef HAVE_STRUCT_SOCKADDR_DL
737 stream_get (&ifp
->sdl
, s
, sizeof (ifp
->sdl_storage
));
739 ifp
->hw_addr_len
= stream_getl (s
);
740 if (ifp
->hw_addr_len
)
741 stream_get (ifp
->hw_addr
, s
, ifp
->hw_addr_len
);
742 #endif /* HAVE_STRUCT_SOCKADDR_DL */
746 memconstant(const void *s
, int c
, size_t n
)
757 zebra_interface_address_read (int type
, struct stream
*s
)
759 unsigned int ifindex
;
760 struct interface
*ifp
;
761 struct connected
*ifc
;
767 memset (&p
, 0, sizeof(p
));
768 memset (&d
, 0, sizeof(d
));
770 /* Get interface index. */
771 ifindex
= stream_getl (s
);
774 ifp
= if_lookup_by_index (ifindex
);
777 zlog_warn ("zebra_interface_address_read(%s): "
778 "Can't find interface by ifindex: %d ",
779 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
? "ADD" : "DELETE"),
785 ifc_flags
= stream_getc (s
);
787 /* Fetch interface address. */
788 family
= p
.family
= stream_getc (s
);
790 plen
= prefix_blen (&p
);
791 stream_get (&p
.u
.prefix
, s
, plen
);
792 p
.prefixlen
= stream_getc (s
);
794 /* Fetch destination address. */
795 stream_get (&d
.u
.prefix
, s
, plen
);
798 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
)
800 /* N.B. NULL destination pointers are encoded as all zeroes */
801 ifc
= connected_add_by_prefix(ifp
, &p
,(memconstant(&d
.u
.prefix
,0,plen
) ?
805 ifc
->flags
= ifc_flags
;
806 if (ifc
->destination
)
807 ifc
->destination
->prefixlen
= ifc
->address
->prefixlen
;
808 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
810 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
812 prefix2str (ifc
->address
, buf
, sizeof(buf
));
813 zlog_warn("warning: interface %s address %s "
814 "with peer flag set, but no peer address!",
816 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
822 assert (type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
823 ifc
= connected_delete_by_prefix(ifp
, &p
);
830 * format of message for neighbor connected address is:
834 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
835 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
850 * | addr_len | len of addr.
853 struct nbr_connected
*
854 zebra_interface_nbr_address_read (int type
, struct stream
*s
)
856 unsigned int ifindex
;
857 struct interface
*ifp
;
859 struct nbr_connected
*ifc
;
861 /* Get interface index. */
862 ifindex
= stream_getl (s
);
865 ifp
= if_lookup_by_index (ifindex
);
868 zlog_warn ("zebra_nbr_interface_address_read(%s): "
869 "Can't find interface by ifindex: %d ",
870 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
? "ADD" : "DELETE"),
875 p
.family
= stream_getc (s
);
876 stream_get (&p
.u
.prefix
, s
, prefix_blen (&p
));
877 p
.prefixlen
= stream_getc (s
);
879 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
)
881 /* Currently only supporting P2P links, so any new RA source address is
882 considered as the replacement of the previously learnt Link-Local address. */
883 if (!(ifc
= listnode_head(ifp
->nbr_connected
)))
885 ifc
= nbr_connected_new ();
886 ifc
->address
= prefix_new ();
888 listnode_add (ifp
->nbr_connected
, ifc
);
891 prefix_copy(ifc
->address
, &p
);
895 assert (type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
897 ifc
= nbr_connected_check(ifp
, &p
);
899 listnode_delete (ifp
->nbr_connected
, ifc
);
905 /* Zebra client message read function. */
907 zclient_read (struct thread
*thread
)
910 uint16_t length
, command
;
911 uint8_t marker
, version
;
912 struct zclient
*zclient
;
914 /* Get socket to zebra. */
915 zclient
= THREAD_ARG (thread
);
916 zclient
->t_read
= NULL
;
918 /* Read zebra header (if we don't have it already). */
919 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
)
922 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
923 ZEBRA_HEADER_SIZE
-already
)) == 0) ||
927 zlog_debug ("zclient connection closed socket [%d].", zclient
->sock
);
928 return zclient_failed(zclient
);
930 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
-already
))
932 /* Try again later. */
933 zclient_event (ZCLIENT_READ
, zclient
);
936 already
= ZEBRA_HEADER_SIZE
;
939 /* Reset to read from the beginning of the incoming packet. */
940 stream_set_getp(zclient
->ibuf
, 0);
942 /* Fetch header values. */
943 length
= stream_getw (zclient
->ibuf
);
944 marker
= stream_getc (zclient
->ibuf
);
945 version
= stream_getc (zclient
->ibuf
);
946 command
= stream_getw (zclient
->ibuf
);
948 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
)
950 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
951 __func__
, zclient
->sock
, marker
, version
);
952 return zclient_failed(zclient
);
955 if (length
< ZEBRA_HEADER_SIZE
)
957 zlog_err("%s: socket %d message length %u is less than %d ",
958 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
959 return zclient_failed(zclient
);
963 if (length
> STREAM_SIZE(zclient
->ibuf
))
966 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
967 __func__
, length
, (u_long
)STREAM_SIZE(zclient
->ibuf
));
968 ns
= stream_new(length
);
969 stream_copy(ns
, zclient
->ibuf
);
970 stream_free (zclient
->ibuf
);
974 /* Read rest of zebra packet. */
975 if (already
< length
)
978 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
979 length
-already
)) == 0) ||
983 zlog_debug("zclient connection closed socket [%d].", zclient
->sock
);
984 return zclient_failed(zclient
);
986 if (nbyte
!= (ssize_t
)(length
-already
))
988 /* Try again later. */
989 zclient_event (ZCLIENT_READ
, zclient
);
994 length
-= ZEBRA_HEADER_SIZE
;
997 zlog_debug("zclient 0x%p command 0x%x \n", zclient
, command
);
1001 case ZEBRA_ROUTER_ID_UPDATE
:
1002 if (zclient
->router_id_update
)
1003 (*zclient
->router_id_update
) (command
, zclient
, length
);
1005 case ZEBRA_INTERFACE_ADD
:
1006 if (zclient
->interface_add
)
1007 (*zclient
->interface_add
) (command
, zclient
, length
);
1009 case ZEBRA_INTERFACE_DELETE
:
1010 if (zclient
->interface_delete
)
1011 (*zclient
->interface_delete
) (command
, zclient
, length
);
1013 case ZEBRA_INTERFACE_ADDRESS_ADD
:
1014 if (zclient
->interface_address_add
)
1015 (*zclient
->interface_address_add
) (command
, zclient
, length
);
1017 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
1018 if (zclient
->interface_address_delete
)
1019 (*zclient
->interface_address_delete
) (command
, zclient
, length
);
1021 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
1022 if (zclient
->interface_nbr_address_add
)
1023 (*zclient
->interface_nbr_address_add
) (command
, zclient
, length
);
1025 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
1026 if (zclient
->interface_nbr_address_delete
)
1027 (*zclient
->interface_nbr_address_delete
) (command
, zclient
, length
);
1029 case ZEBRA_INTERFACE_UP
:
1030 if (zclient
->interface_up
)
1031 (*zclient
->interface_up
) (command
, zclient
, length
);
1033 case ZEBRA_INTERFACE_DOWN
:
1034 if (zclient
->interface_down
)
1035 (*zclient
->interface_down
) (command
, zclient
, length
);
1037 case ZEBRA_IPV4_ROUTE_ADD
:
1038 if (zclient
->ipv4_route_add
)
1039 (*zclient
->ipv4_route_add
) (command
, zclient
, length
);
1041 case ZEBRA_IPV4_ROUTE_DELETE
:
1042 if (zclient
->ipv4_route_delete
)
1043 (*zclient
->ipv4_route_delete
) (command
, zclient
, length
);
1045 case ZEBRA_IPV6_ROUTE_ADD
:
1046 if (zclient
->ipv6_route_add
)
1047 (*zclient
->ipv6_route_add
) (command
, zclient
, length
);
1049 case ZEBRA_IPV6_ROUTE_DELETE
:
1050 if (zclient
->ipv6_route_delete
)
1051 (*zclient
->ipv6_route_delete
) (command
, zclient
, length
);
1053 case ZEBRA_NEXTHOP_UPDATE
:
1055 zlog_debug("zclient rcvd nexthop update\n");
1056 if (zclient
->nexthop_update
)
1057 (*zclient
->nexthop_update
) (command
, zclient
, length
);
1063 if (zclient
->sock
< 0)
1064 /* Connection was closed during packet processing. */
1067 /* Register read thread. */
1068 stream_reset(zclient
->ibuf
);
1069 zclient_event (ZCLIENT_READ
, zclient
);
1075 zclient_redistribute (int command
, struct zclient
*zclient
, int type
)
1078 if (command
== ZEBRA_REDISTRIBUTE_ADD
)
1080 if (zclient
->redist
[type
])
1082 zclient
->redist
[type
] = 1;
1086 if (!zclient
->redist
[type
])
1088 zclient
->redist
[type
] = 0;
1091 if (zclient
->sock
> 0)
1092 zebra_redistribute_send (command
, zclient
, type
);
1097 zclient_redistribute_default (int command
, struct zclient
*zclient
)
1100 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
)
1102 if (zclient
->default_information
)
1104 zclient
->default_information
= 1;
1108 if (!zclient
->default_information
)
1110 zclient
->default_information
= 0;
1113 if (zclient
->sock
> 0)
1114 zebra_message_send (zclient
, command
);
1118 zclient_event (enum event event
, struct zclient
*zclient
)
1122 case ZCLIENT_SCHEDULE
:
1123 if (! zclient
->t_connect
)
1124 zclient
->t_connect
=
1125 thread_add_event (master
, zclient_connect
, zclient
, 0);
1127 case ZCLIENT_CONNECT
:
1128 if (zclient
->fail
>= 10)
1131 zlog_debug ("zclient connect schedule interval is %d",
1132 zclient
->fail
< 3 ? 10 : 60);
1133 if (! zclient
->t_connect
)
1134 zclient
->t_connect
=
1135 thread_add_timer (master
, zclient_connect
, zclient
,
1136 zclient
->fail
< 3 ? 10 : 60);
1140 thread_add_read (master
, zclient_read
, zclient
, zclient
->sock
);
1146 zclient_serv_path_set (char *path
)
1151 zclient_serv_path
= NULL
;
1153 /* test if `path' is socket. don't set it otherwise. */
1154 if (stat(path
, &sb
) == -1)
1156 zlog_warn ("%s: zebra socket `%s' does not exist", __func__
, path
);
1160 if ((sb
.st_mode
& S_IFMT
) != S_IFSOCK
)
1162 zlog_warn ("%s: `%s' is not unix socket, sir", __func__
, path
);
1166 /* it seems that path is unix socket */
1167 zclient_serv_path
= path
;