1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
37 /* Zebra client events. */
38 enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
40 /* Prototype for event manager. */
41 static void zclient_event (enum event, struct zclient *);
43 /* This file local debug flag. */
44 int zclient_debug = 0;
46 /* Allocate zclient structure. */
50 struct zclient *zclient;
51 zclient = XMALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
52 memset (zclient, 0, sizeof (struct zclient));
54 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
55 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
60 /* Free zclient structure. */
62 zclient_free (struct zclient *zclient)
64 XFREE (MTYPE_ZCLIENT, zclient);
67 /* Initialize zebra client. Argument redist_default is unwanted
68 redistribute route type. */
70 zclient_init (struct zclient *zclient, int redist_default)
74 /* Enable zebra client connection by default. */
77 /* Set -1 to the default socket value. */
80 /* Clear redistribution flags. */
81 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
82 zclient->redist[i] = 0;
84 /* Set unwanted redistribute route. bgpd does not need BGP route
86 zclient->redist_default = redist_default;
87 zclient->redist[redist_default] = 1;
89 /* Set default-information redistribute to zero. */
90 zclient->default_information = 0;
92 /* Schedule first zclient connection. */
95 zlog_info ("zclient start scheduled");
97 zclient_event (ZCLIENT_SCHEDULE, zclient);
100 /* Stop zebra client services. */
102 zclient_stop (struct zclient *zclient)
105 #ifdef BRCM_RIP_DEBUG
106 zlog_info ("zclient stopped");
111 thread_cancel (zclient->t_read);
112 zclient->t_read = NULL;
114 if (zclient->t_connect)
116 thread_cancel (zclient->t_connect);
117 zclient->t_connect = NULL;
121 if (zclient->sock >= 0)
123 close (zclient->sock);
130 zclient_reset (struct zclient *zclient)
132 zclient_stop (zclient);
133 zclient_init (zclient, zclient->redist_default);
136 /* Make socket to zebra daemon. Return zebra socket. */
142 struct sockaddr_in serv;
144 /* We should think about IPv6 connection. */
145 sock = socket (AF_INET, SOCK_STREAM, 0);
149 /* Make server socket. */
150 memset (&serv, 0, sizeof (struct sockaddr_in));
151 serv.sin_family = AF_INET;
152 serv.sin_port = htons (ZEBRA_PORT);
154 serv.sin_len = sizeof (struct sockaddr_in);
155 #endif /* HAVE_SIN_LEN */
156 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
158 /* Connect to zebra. */
159 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
168 /* For sockaddr_un. */
172 zclient_socket_un (char *path)
176 struct sockaddr_un addr;
178 sock = socket (AF_UNIX, SOCK_STREAM, 0);
182 /* Make server socket. */
183 memset (&addr, 0, sizeof (struct sockaddr_un));
184 addr.sun_family = AF_UNIX;
185 strncpy (addr.sun_path, path, strlen (path));
187 len = addr.sun_len = SUN_LEN(&addr);
189 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
190 #endif /* HAVE_SUN_LEN */
192 ret = connect (sock, (struct sockaddr *) &addr, len);
200 #endif /* #ifdef BRCM_SUPPORT */
201 /* Send simple Zebra message. */
203 zebra_message_send (struct zclient *zclient, int command)
207 /* Get zclient output buffer. */
211 /* Send very simple command only Zebra message. */
213 stream_putc (s, command);
215 return writen (zclient->sock, s->data, 3);
218 /* Make connection to zebra daemon. */
220 zclient_start (struct zclient *zclient)
224 #ifdef BRCM_RIP_DEBUG
226 zlog_info ("zclient_start is called");
228 /* zclient is disabled. */
229 if (! zclient->enable)
232 /* If already connected to the zebra. */
233 if (zclient->sock >= 0)
236 /* Check connect thread. */
237 if (zclient->t_connect)
241 #ifdef HAVE_TCP_ZEBRA
242 zclient->sock = zclient_socket ();
244 zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
245 #endif /* HAVE_TCP_ZEBRA */
246 if (zclient->sock < 0)
248 #ifdef BRCM_RIP_DEBUG
250 zlog_info ("zclient connection fail");
253 zclient_event (ZCLIENT_CONNECT, zclient);
257 /* Clear fail count. */
259 #ifdef BRCM_RIP_DEBUG
261 zlog_info ("zclient connect success with socket [%d]", zclient->sock);
263 /* Create read thread. */
264 zclient_event (ZCLIENT_READ, zclient);
266 /* We need interface information. */
267 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD);
269 /* Flush all redistribute request. */
270 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
271 if (i != zclient->redist_default && zclient->redist[i])
272 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, i);
274 /* If default information is needed. */
275 if (zclient->default_information)
276 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
281 /* This function is a wrapper function for calling zclient_start from
282 timer or event thread. */
284 zclient_connect (struct thread *t)
286 struct zclient *zclient;
288 zclient = THREAD_ARG (t);
289 zclient->t_connect = NULL;
290 #ifdef BRCM_RIP_DEBUG
292 zlog_info ("zclient_connect is called");
294 return zclient_start (zclient);
298 zapi_ipv4_add (struct zclient *zclient, struct prefix_ipv4 *p,
299 struct zapi_ipv4 *api)
309 /* Length place holder. */
312 /* Put command, type and nexthop. */
313 stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
314 stream_putc (s, api->type);
315 stream_putc (s, api->flags);
316 stream_putc (s, api->message);
318 /* Put prefix information. */
319 psize = PSIZE (p->prefixlen);
320 stream_putc (s, p->prefixlen);
321 stream_write (s, (u_char *)&p->prefix, psize);
323 /* Nexthop, ifindex, distance and metric information. */
324 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
326 stream_putc (s, api->nexthop_num + api->ifindex_num);
328 for (i = 0; i < api->nexthop_num; i++)
330 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
331 stream_put_in_addr (s, api->nexthop[i]);
333 for (i = 0; i < api->ifindex_num; i++)
335 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
336 stream_putl (s, api->ifindex[i]);
340 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
341 stream_putc (s, api->distance);
342 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
343 stream_putl (s, api->metric);
345 /* Put length at the first point of the stream. */
346 stream_putw_at (s, 0, stream_get_endp (s));
348 return writen (zclient->sock, s->data, stream_get_endp (s));
352 zapi_ipv4_delete (struct zclient *zclient, struct prefix_ipv4 *p,
353 struct zapi_ipv4 *api)
363 /* Length place holder. */
366 /* Put command, type and nexthop. */
367 stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE);
368 stream_putc (s, api->type);
369 stream_putc (s, api->flags);
370 stream_putc (s, api->message);
372 /* Put prefix information. */
373 psize = PSIZE (p->prefixlen);
374 stream_putc (s, p->prefixlen);
375 stream_write (s, (u_char *)&p->prefix, psize);
377 /* Nexthop, ifindex, distance and metric information. */
378 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
380 stream_putc (s, api->nexthop_num + api->ifindex_num);
382 for (i = 0; i < api->nexthop_num; i++)
384 stream_putc (s, ZEBRA_NEXTHOP_IPV4);
385 stream_put_in_addr (s, api->nexthop[i]);
387 for (i = 0; i < api->ifindex_num; i++)
389 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
390 stream_putl (s, api->ifindex[i]);
394 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
395 stream_putc (s, api->distance);
396 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
397 stream_putl (s, api->metric);
399 /* Put length at the first point of the stream. */
400 stream_putw_at (s, 0, stream_get_endp (s));
402 return writen (zclient->sock, s->data, stream_get_endp (s));
407 zapi_ipv6_add (struct zclient *zclient, struct prefix_ipv6 *p,
408 struct zapi_ipv6 *api)
418 /* Length place holder. */
421 /* Put command, type and nexthop. */
422 stream_putc (s, ZEBRA_IPV6_ROUTE_ADD);
423 stream_putc (s, api->type);
424 stream_putc (s, api->flags);
425 stream_putc (s, api->message);
427 /* Put prefix information. */
428 psize = PSIZE (p->prefixlen);
429 stream_putc (s, p->prefixlen);
430 stream_write (s, (u_char *)&p->prefix, psize);
432 /* Nexthop, ifindex, distance and metric information. */
433 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
435 stream_putc (s, api->nexthop_num + api->ifindex_num);
437 for (i = 0; i < api->nexthop_num; i++)
439 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
440 stream_write (s, (u_char *)api->nexthop[i], 16);
442 for (i = 0; i < api->ifindex_num; i++)
444 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
445 stream_putl (s, api->ifindex[i]);
449 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
450 stream_putc (s, api->distance);
451 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
452 stream_putl (s, api->metric);
454 /* Put length at the first point of the stream. */
455 stream_putw_at (s, 0, stream_get_endp (s));
457 return writen (zclient->sock, s->data, stream_get_endp (s));
461 zapi_ipv6_delete (struct zclient *zclient, struct prefix_ipv6 *p,
462 struct zapi_ipv6 *api)
472 /* Length place holder. */
475 /* Put command, type and nexthop. */
476 stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE);
477 stream_putc (s, api->type);
478 stream_putc (s, api->flags);
479 stream_putc (s, api->message);
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 stream_putc (s, api->nexthop_num + api->ifindex_num);
491 for (i = 0; i < api->nexthop_num; i++)
493 stream_putc (s, ZEBRA_NEXTHOP_IPV6);
494 stream_write (s, (u_char *)api->nexthop[i], 16);
496 for (i = 0; i < api->ifindex_num; i++)
498 stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
499 stream_putl (s, api->ifindex[i]);
503 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
504 stream_putc (s, api->distance);
505 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
506 stream_putl (s, api->metric);
508 /* Put length at the first point of the stream. */
509 stream_putw_at (s, 0, stream_get_endp (s));
511 return writen (zclient->sock, s->data, stream_get_endp (s));
514 #endif /* HAVE_IPV6 */
517 zebra_redistribute_send (int command, int sock, int type)
522 s = stream_new (ZEBRA_MAX_PACKET_SIZ);
524 /* Total length of the messages. */
527 stream_putc (s, command);
528 stream_putc (s, type);
530 ret = writen (sock, s->data, 4);
537 /* Interface addition from zebra daemon. */
539 zebra_interface_add_read (struct stream *s)
541 struct interface *ifp;
542 u_char ifname_tmp[INTERFACE_NAMSIZ];
544 /* Read interface name. */
545 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
547 /* Lookup this by interface name. */
548 ifp = if_lookup_by_name (ifname_tmp);
550 /* If such interface does not exist, make new one. */
554 strncpy (ifp->name, ifname_tmp, IFNAMSIZ);
557 /* Read interface's index. */
558 ifp->ifindex = stream_getl (s);
560 /* Read interface's value. */
561 ifp->flags = stream_getl (s);
562 ifp->metric = stream_getl (s);
563 ifp->mtu = stream_getl (s);
564 ifp->bandwidth = stream_getl (s);
565 #ifdef HAVE_SOCKADDR_DL
566 stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
568 ifp->hw_addr_len = stream_getl (s);
569 if (ifp->hw_addr_len)
570 stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
571 #endif /* HAVE_SOCKADDR_DL */
576 /* Read interface up/down msg from zebra daemon. */
578 zebra_interface_state_read (struct stream *s)
580 struct interface *ifp;
581 u_char ifname_tmp[INTERFACE_NAMSIZ];
583 /* Read interface name. */
584 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
586 /* Lookup this by interface index. */
587 ifp = if_lookup_by_name (ifname_tmp);
589 /* If such interface does not exist, indicate an error */
593 /* Read interface's index. */
594 ifp->ifindex = stream_getl (s);
596 /* Read interface's value. */
597 ifp->flags = stream_getl (s);
598 ifp->metric = stream_getl (s);
599 ifp->mtu = stream_getl (s);
600 ifp->bandwidth = stream_getl (s);
606 zebra_interface_address_add_read (struct stream *s)
608 unsigned int ifindex;
609 struct interface *ifp;
610 struct connected *ifc;
615 /* Get interface index. */
616 ifindex = stream_getl (s);
619 ifp = if_lookup_by_index (ifindex);
622 #ifdef BRCM_RIP_DEBUG
623 zlog_warn ("zebra_interface_address_add_read: Can't find interface by ifindex: %d ", ifindex);
628 /* Allocate new connected address. */
629 ifc = connected_new ();
633 ifc->flags = stream_getc (s);
635 /* Fetch interface address. */
637 family = p->family = stream_getc (s);
639 plen = prefix_blen (p);
640 stream_get (&p->u.prefix, s, plen);
641 p->prefixlen = stream_getc (s);
644 /* Fetch destination address. */
646 stream_get (&p->u.prefix, s, plen);
649 ifc->destination = p;
653 /* Add connected address to the interface. */
654 listnode_add (ifp->connected, ifc);
660 zebra_interface_address_delete_read (struct stream *s)
662 unsigned int ifindex;
663 struct interface *ifp;
664 struct connected *ifc;
671 /* Get interface index. */
672 ifindex = stream_getl (s);
675 ifp = if_lookup_by_index (ifindex);
678 #ifdef BRCM_RIP_DEBUG
679 zlog_warn ("zebra_interface_address_delete_read: Can't find interface by ifindex: %d ", ifindex);
685 flags = stream_getc (s);
687 /* Fetch interface address. */
688 family = p.family = stream_getc (s);
690 len = prefix_blen (&p);
691 stream_get (&p.u.prefix, s, len);
692 p.prefixlen = stream_getc (s);
694 /* Fetch destination address. */
695 stream_get (&d.u.prefix, s, len);
698 ifc = connected_delete_by_prefix (ifp, &p);
703 /* Zebra client message read function. */
705 zclient_read (struct thread *thread)
711 zebra_command_t command;
712 struct zclient *zclient;
714 /* Get socket to zebra. */
715 sock = THREAD_FD (thread);
716 zclient = THREAD_ARG (thread);
717 zclient->t_read = NULL;
719 /* Clear input buffer. */
720 stream_reset (zclient->ibuf);
722 /* Read zebra header. */
723 nbytes = stream_read (zclient->ibuf, sock, ZEBRA_HEADER_SIZE);
725 /* zebra socket is closed. */
728 #ifdef BRCM_RIP_DEBUG
730 zlog_info ("zclient connection closed socket [%d].", sock);
733 zclient_stop (zclient);
734 zclient_event (ZCLIENT_CONNECT, zclient);
738 /* zebra read error. */
739 if (nbytes < 0 || nbytes != ZEBRA_HEADER_SIZE)
741 #ifdef BRCM_RIP_DEBUG
743 zlog_info ("Can't read all packet (length %d).", nbytes);
746 zclient_stop (zclient);
747 zclient_event (ZCLIENT_CONNECT, zclient);
751 /* Fetch length and command. */
752 length = stream_getw (zclient->ibuf);
753 command = stream_getc (zclient->ibuf);
756 if (length >= zclient->ibuf->size)
758 stream_free (zclient->ibuf);
759 zclient->ibuf = stream_new (length + 1);
761 length -= ZEBRA_HEADER_SIZE;
763 /* Read rest of zebra packet. */
764 nbytes = stream_read (zclient->ibuf, sock, length);
765 if (nbytes != length)
767 #ifdef BRCM_RIP_DEBUG
769 zlog_info ("zclient connection closed socket [%d].", sock);
772 zclient_stop (zclient);
773 zclient_event (ZCLIENT_CONNECT, zclient);
779 case ZEBRA_INTERFACE_ADD:
780 if (zclient->interface_add)
781 ret = (*zclient->interface_add) (command, zclient, length);
783 case ZEBRA_INTERFACE_DELETE:
784 if (zclient->interface_delete)
785 ret = (*zclient->interface_delete) (command, zclient, length);
787 case ZEBRA_INTERFACE_ADDRESS_ADD:
788 if (zclient->interface_address_add)
789 ret = (*zclient->interface_address_add) (command, zclient, length);
791 case ZEBRA_INTERFACE_ADDRESS_DELETE:
792 if (zclient->interface_address_delete)
793 ret = (*zclient->interface_address_delete) (command, zclient, length);
795 case ZEBRA_INTERFACE_UP:
796 if (zclient->interface_up)
797 ret = (*zclient->interface_up) (command, zclient, length);
799 case ZEBRA_INTERFACE_DOWN:
800 if (zclient->interface_down)
801 ret = (*zclient->interface_down) (command, zclient, length);
803 case ZEBRA_IPV4_ROUTE_ADD:
804 if (zclient->ipv4_route_add)
805 ret = (*zclient->ipv4_route_add) (command, zclient, length);
807 case ZEBRA_IPV4_ROUTE_DELETE:
808 if (zclient->ipv4_route_delete)
809 ret = (*zclient->ipv4_route_delete) (command, zclient, length);
812 case ZEBRA_IPV6_ROUTE_ADD:
813 if (zclient->ipv6_route_add)
814 ret = (*zclient->ipv6_route_add) (command, zclient, length);
816 case ZEBRA_IPV6_ROUTE_DELETE:
817 if (zclient->ipv6_route_delete)
818 ret = (*zclient->ipv6_route_delete) (command, zclient, length);
820 #endif /* HAVE_IPV6 */
825 /* Register read thread. */
826 zclient_event (ZCLIENT_READ, zclient);
832 zclient_redistribute_set (struct zclient *zclient, int type)
834 if (zclient->redist[type])
837 zclient->redist[type] = 1;
839 if (zclient->sock > 0)
840 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, type);
844 zclient_redistribute_unset (struct zclient *zclient, int type)
846 if (! zclient->redist[type])
849 zclient->redist[type] = 0;
851 if (zclient->sock > 0)
852 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient->sock, type);
856 zclient_redistribute_default_set (struct zclient *zclient)
858 if (zclient->default_information)
861 zclient->default_information = 1;
863 if (zclient->sock > 0)
864 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
868 zclient_redistribute_default_unset (struct zclient *zclient)
870 if (! zclient->default_information)
873 zclient->default_information = 0;
875 if (zclient->sock > 0)
876 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE);
879 extern struct thread_master *master;
882 zclient_event (enum event event, struct zclient *zclient)
886 case ZCLIENT_SCHEDULE:
887 if (! zclient->t_connect)
889 thread_add_event (master, zclient_connect, zclient, 0);
891 case ZCLIENT_CONNECT:
892 if (zclient->fail >= 10)
894 #ifdef BRCM_RIP_DEBUG
896 zlog_info ("zclient connect schedule interval is %d",
897 zclient->fail < 3 ? 10 : 60);
899 if (! zclient->t_connect)
901 thread_add_timer (master, zclient_connect, zclient,
902 zclient->fail < 3 ? 10 : 60);
906 thread_add_read (master, zclient_read, zclient, zclient->sock);