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. */
94
zlog_info ("zclient start scheduled");
96
zclient_event (ZCLIENT_SCHEDULE, zclient);
99
/* Stop zebra client services. */
101
zclient_stop (struct zclient *zclient)
104
zlog_info ("zclient stopped");
109
thread_cancel (zclient->t_read);
110
zclient->t_read = NULL;
112
if (zclient->t_connect)
114
thread_cancel (zclient->t_connect);
115
zclient->t_connect = NULL;
119
if (zclient->sock >= 0)
121
close (zclient->sock);
128
zclient_reset (struct zclient *zclient)
130
zclient_stop (zclient);
131
zclient_init (zclient, zclient->redist_default);
134
/* Make socket to zebra daemon. Return zebra socket. */
140
struct sockaddr_in serv;
142
/* We should think about IPv6 connection. */
143
sock = socket (AF_INET, SOCK_STREAM, 0);
147
/* Make server socket. */
148
memset (&serv, 0, sizeof (struct sockaddr_in));
149
serv.sin_family = AF_INET;
150
serv.sin_port = htons (ZEBRA_PORT);
152
serv.sin_len = sizeof (struct sockaddr_in);
153
#endif /* HAVE_SIN_LEN */
154
serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
156
/* Connect to zebra. */
157
ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
166
/* For sockaddr_un. */
170
zclient_socket_un (char *path)
174
struct sockaddr_un addr;
176
sock = socket (AF_UNIX, SOCK_STREAM, 0);
180
/* Make server socket. */
181
memset (&addr, 0, sizeof (struct sockaddr_un));
182
addr.sun_family = AF_UNIX;
183
strncpy (addr.sun_path, path, strlen (path));
185
len = addr.sun_len = SUN_LEN(&addr);
187
len = sizeof (addr.sun_family) + strlen (addr.sun_path);
188
#endif /* HAVE_SUN_LEN */
190
ret = connect (sock, (struct sockaddr *) &addr, len);
199
/* Send simple Zebra message. */
201
zebra_message_send (struct zclient *zclient, int command)
205
/* Get zclient output buffer. */
209
/* Send very simple command only Zebra message. */
211
stream_putc (s, command);
213
return writen (zclient->sock, s->data, 3);
216
/* Make connection to zebra daemon. */
218
zclient_start (struct zclient *zclient)
223
zlog_info ("zclient_start is called");
225
/* zclient is disabled. */
226
if (! zclient->enable)
229
/* If already connected to the zebra. */
230
if (zclient->sock >= 0)
233
/* Check connect thread. */
234
if (zclient->t_connect)
238
#ifdef HAVE_TCP_ZEBRA
239
zclient->sock = zclient_socket ();
241
zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH);
242
#endif /* HAVE_TCP_ZEBRA */
243
if (zclient->sock < 0)
246
zlog_info ("zclient connection fail");
248
zclient_event (ZCLIENT_CONNECT, zclient);
252
/* Clear fail count. */
255
zlog_info ("zclient connect success with socket [%d]", zclient->sock);
257
/* Create read thread. */
258
zclient_event (ZCLIENT_READ, zclient);
260
/* We need interface information. */
261
zebra_message_send (zclient, ZEBRA_INTERFACE_ADD);
263
/* Flush all redistribute request. */
264
for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
265
if (i != zclient->redist_default && zclient->redist[i])
266
zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, i);
268
/* If default information is needed. */
269
if (zclient->default_information)
270
zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
275
/* This function is a wrapper function for calling zclient_start from
276
timer or event thread. */
278
zclient_connect (struct thread *t)
280
struct zclient *zclient;
282
zclient = THREAD_ARG (t);
283
zclient->t_connect = NULL;
286
zlog_info ("zclient_connect is called");
288
return zclient_start (zclient);
292
zapi_ipv4_add (struct zclient *zclient, struct prefix_ipv4 *p,
293
struct zapi_ipv4 *api)
303
/* Length place holder. */
306
/* Put command, type and nexthop. */
307
stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
308
stream_putc (s, api->type);
309
stream_putc (s, api->flags);
310
stream_putc (s, api->message);
312
/* Put prefix information. */
313
psize = PSIZE (p->prefixlen);
314
stream_putc (s, p->prefixlen);
315
stream_write (s, (u_char *)&p->prefix, psize);
317
/* Nexthop, ifindex, distance and metric information. */
318
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
320
if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
323
stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
324
/* XXX assert(api->nexthop_num == 0); */
325
/* XXX assert(api->ifindex_num == 0); */
328
stream_putc (s, api->nexthop_num + api->ifindex_num);
330
for (i = 0; i < api->nexthop_num; i++)
332
stream_putc (s, ZEBRA_NEXTHOP_IPV4);
333
stream_put_in_addr (s, api->nexthop[i]);
335
for (i = 0; i < api->ifindex_num; i++)
337
stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
338
stream_putl (s, api->ifindex[i]);
342
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
343
stream_putc (s, api->distance);
344
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
345
stream_putl (s, api->metric);
347
/* Put length at the first point of the stream. */
348
stream_putw_at (s, 0, stream_get_endp (s));
350
return writen (zclient->sock, s->data, stream_get_endp (s));
354
zapi_ipv4_delete (struct zclient *zclient, struct prefix_ipv4 *p,
355
struct zapi_ipv4 *api)
365
/* Length place holder. */
368
/* Put command, type and nexthop. */
369
stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE);
370
stream_putc (s, api->type);
371
stream_putc (s, api->flags);
372
stream_putc (s, api->message);
374
/* Put prefix information. */
375
psize = PSIZE (p->prefixlen);
376
stream_putc (s, p->prefixlen);
377
stream_write (s, (u_char *)&p->prefix, psize);
379
/* Nexthop, ifindex, distance and metric information. */
380
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
382
if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
385
stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
386
/* XXX assert(api->nexthop_num == 0); */
387
/* XXX assert(api->ifindex_num == 0); */
390
stream_putc (s, api->nexthop_num + api->ifindex_num);
392
for (i = 0; i < api->nexthop_num; i++)
394
stream_putc (s, ZEBRA_NEXTHOP_IPV4);
395
stream_put_in_addr (s, api->nexthop[i]);
397
for (i = 0; i < api->ifindex_num; i++)
399
stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
400
stream_putl (s, api->ifindex[i]);
404
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
405
stream_putc (s, api->distance);
406
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
407
stream_putl (s, api->metric);
409
/* Put length at the first point of the stream. */
410
stream_putw_at (s, 0, stream_get_endp (s));
412
return writen (zclient->sock, s->data, stream_get_endp (s));
417
zapi_ipv6_add (struct zclient *zclient, struct prefix_ipv6 *p,
418
struct zapi_ipv6 *api)
428
/* Length place holder. */
431
/* Put command, type and nexthop. */
432
stream_putc (s, ZEBRA_IPV6_ROUTE_ADD);
433
stream_putc (s, api->type);
434
stream_putc (s, api->flags);
435
stream_putc (s, api->message);
437
/* Put prefix information. */
438
psize = PSIZE (p->prefixlen);
439
stream_putc (s, p->prefixlen);
440
stream_write (s, (u_char *)&p->prefix, psize);
442
/* Nexthop, ifindex, distance and metric information. */
443
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
445
stream_putc (s, api->nexthop_num + api->ifindex_num);
447
for (i = 0; i < api->nexthop_num; i++)
449
stream_putc (s, ZEBRA_NEXTHOP_IPV6);
450
stream_write (s, (u_char *)api->nexthop[i], 16);
452
for (i = 0; i < api->ifindex_num; i++)
454
stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
455
stream_putl (s, api->ifindex[i]);
459
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
460
stream_putc (s, api->distance);
461
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
462
stream_putl (s, api->metric);
464
/* Put length at the first point of the stream. */
465
stream_putw_at (s, 0, stream_get_endp (s));
467
return writen (zclient->sock, s->data, stream_get_endp (s));
471
zapi_ipv6_delete (struct zclient *zclient, struct prefix_ipv6 *p,
472
struct zapi_ipv6 *api)
482
/* Length place holder. */
485
/* Put command, type and nexthop. */
486
stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE);
487
stream_putc (s, api->type);
488
stream_putc (s, api->flags);
489
stream_putc (s, api->message);
491
/* Put prefix information. */
492
psize = PSIZE (p->prefixlen);
493
stream_putc (s, p->prefixlen);
494
stream_write (s, (u_char *)&p->prefix, psize);
496
/* Nexthop, ifindex, distance and metric information. */
497
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
499
stream_putc (s, api->nexthop_num + api->ifindex_num);
501
for (i = 0; i < api->nexthop_num; i++)
503
stream_putc (s, ZEBRA_NEXTHOP_IPV6);
504
stream_write (s, (u_char *)api->nexthop[i], 16);
506
for (i = 0; i < api->ifindex_num; i++)
508
stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
509
stream_putl (s, api->ifindex[i]);
513
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
514
stream_putc (s, api->distance);
515
if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
516
stream_putl (s, api->metric);
518
/* Put length at the first point of the stream. */
519
stream_putw_at (s, 0, stream_get_endp (s));
521
return writen (zclient->sock, s->data, stream_get_endp (s));
524
#endif /* HAVE_IPV6 */
527
zebra_redistribute_send (int command, int sock, int type)
532
s = stream_new (ZEBRA_MAX_PACKET_SIZ);
534
/* Total length of the messages. */
537
stream_putc (s, command);
538
stream_putc (s, type);
540
ret = writen (sock, s->data, 4);
547
/* Interface addition from zebra daemon. */
549
zebra_interface_add_read (struct stream *s)
551
struct interface *ifp;
552
u_char ifname_tmp[INTERFACE_NAMSIZ];
554
/* Read interface name. */
555
stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
557
/* Lookup this by interface name. */
558
ifp = if_lookup_by_name (ifname_tmp);
560
/* If such interface does not exist, make new one. */
562
ifp = if_create (ifname_tmp, INTERFACE_NAMSIZ);
564
/* Read interface's index. */
565
ifp->ifindex = stream_getl (s);
567
/* Read interface's value. */
568
ifp->status = stream_getc (s);
569
ifp->flags = stream_getl (s);
570
ifp->metric = stream_getl (s);
571
ifp->mtu = stream_getl (s);
572
ifp->bandwidth = stream_getl (s);
573
#ifdef HAVE_SOCKADDR_DL
574
stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
576
ifp->hw_addr_len = stream_getl (s);
577
if (ifp->hw_addr_len)
578
stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
579
#endif /* HAVE_SOCKADDR_DL */
584
/* Read interface up/down msg from zebra daemon. */
586
zebra_interface_state_read (struct stream *s)
588
struct interface *ifp;
589
u_char ifname_tmp[INTERFACE_NAMSIZ];
591
/* Read interface name. */
592
stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
594
/* Lookup this by interface index. */
595
ifp = if_lookup_by_name (ifname_tmp);
597
/* If such interface does not exist, indicate an error */
601
/* Read interface's index. */
602
ifp->ifindex = stream_getl (s);
604
/* Read interface's value. */
605
ifp->status = stream_getc (s);
606
ifp->flags = stream_getl (s);
607
ifp->metric = stream_getl (s);
608
ifp->mtu = stream_getl (s);
609
ifp->bandwidth = stream_getl (s);
615
zebra_interface_address_add_read (struct stream *s)
617
unsigned int ifindex;
618
struct interface *ifp;
619
struct connected *ifc;
624
/* Get interface index. */
625
ifindex = stream_getl (s);
628
ifp = if_lookup_by_index (ifindex);
631
zlog_warn ("zebra_interface_address_add_read: Can't find interface by ifindex: %d ", ifindex);
635
/* Allocate new connected address. */
636
ifc = connected_new ();
640
ifc->flags = stream_getc (s);
642
/* Fetch interface address. */
644
family = p->family = stream_getc (s);
646
plen = prefix_blen (p);
647
stream_get (&p->u.prefix, s, plen);
648
p->prefixlen = stream_getc (s);
651
/* Fetch destination address. */
653
stream_get (&p->u.prefix, s, plen);
656
ifc->destination = p;
660
/* Add connected address to the interface. */
661
listnode_add (ifp->connected, ifc);
667
zebra_interface_address_delete_read (struct stream *s)
669
unsigned int ifindex;
670
struct interface *ifp;
671
struct connected *ifc;
678
/* Get interface index. */
679
ifindex = stream_getl (s);
682
ifp = if_lookup_by_index (ifindex);
685
zlog_warn ("zebra_interface_address_delete_read: Can't find interface by ifindex: %d ", ifindex);
690
flags = stream_getc (s);
692
/* Fetch interface address. */
693
family = p.family = stream_getc (s);
695
len = prefix_blen (&p);
696
stream_get (&p.u.prefix, s, len);
697
p.prefixlen = stream_getc (s);
699
/* Fetch destination address. */
700
stream_get (&d.u.prefix, s, len);
703
ifc = connected_delete_by_prefix (ifp, &p);
708
/* Zebra client message read function. */
710
zclient_read (struct thread *thread)
716
zebra_command_t command;
717
struct zclient *zclient;
719
/* Get socket to zebra. */
720
sock = THREAD_FD (thread);
721
zclient = THREAD_ARG (thread);
722
zclient->t_read = NULL;
724
/* Clear input buffer. */
725
stream_reset (zclient->ibuf);
727
/* Read zebra header. */
728
nbytes = stream_read (zclient->ibuf, sock, ZEBRA_HEADER_SIZE);
730
/* zebra socket is closed. */
734
zlog_info ("zclient connection closed socket [%d].", sock);
736
zclient_stop (zclient);
737
zclient_event (ZCLIENT_CONNECT, zclient);
741
/* zebra read error. */
742
if (nbytes < 0 || nbytes != ZEBRA_HEADER_SIZE)
745
zlog_info ("Can't read all packet (length %d).", nbytes);
747
zclient_stop (zclient);
748
zclient_event (ZCLIENT_CONNECT, zclient);
752
/* Fetch length and command. */
753
length = stream_getw (zclient->ibuf);
754
command = stream_getc (zclient->ibuf);
757
if (length >= zclient->ibuf->size)
759
stream_free (zclient->ibuf);
760
zclient->ibuf = stream_new (length + 1);
762
length -= ZEBRA_HEADER_SIZE;
764
/* Read rest of zebra packet. */
765
nbytes = stream_read (zclient->ibuf, sock, length);
766
if (nbytes != length)
769
zlog_info ("zclient connection closed socket [%d].", sock);
771
zclient_stop (zclient);
772
zclient_event (ZCLIENT_CONNECT, zclient);
778
case ZEBRA_INTERFACE_ADD:
779
if (zclient->interface_add)
780
ret = (*zclient->interface_add) (command, zclient, length);
782
case ZEBRA_INTERFACE_DELETE:
783
if (zclient->interface_delete)
784
ret = (*zclient->interface_delete) (command, zclient, length);
786
case ZEBRA_INTERFACE_ADDRESS_ADD:
787
if (zclient->interface_address_add)
788
ret = (*zclient->interface_address_add) (command, zclient, length);
790
case ZEBRA_INTERFACE_ADDRESS_DELETE:
791
if (zclient->interface_address_delete)
792
ret = (*zclient->interface_address_delete) (command, zclient, length);
794
case ZEBRA_INTERFACE_UP:
795
if (zclient->interface_up)
796
ret = (*zclient->interface_up) (command, zclient, length);
798
case ZEBRA_INTERFACE_DOWN:
799
if (zclient->interface_down)
800
ret = (*zclient->interface_down) (command, zclient, length);
802
case ZEBRA_IPV4_ROUTE_ADD:
803
if (zclient->ipv4_route_add)
804
ret = (*zclient->ipv4_route_add) (command, zclient, length);
806
case ZEBRA_IPV4_ROUTE_DELETE:
807
if (zclient->ipv4_route_delete)
808
ret = (*zclient->ipv4_route_delete) (command, zclient, length);
810
case ZEBRA_IPV6_ROUTE_ADD:
811
if (zclient->ipv6_route_add)
812
ret = (*zclient->ipv6_route_add) (command, zclient, length);
814
case ZEBRA_IPV6_ROUTE_DELETE:
815
if (zclient->ipv6_route_delete)
816
ret = (*zclient->ipv6_route_delete) (command, zclient, length);
822
/* Register read thread. */
823
zclient_event (ZCLIENT_READ, zclient);
829
zclient_redistribute_set (struct zclient *zclient, int type)
831
if (zclient->redist[type])
834
zclient->redist[type] = 1;
836
if (zclient->sock > 0)
837
zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, type);
841
zclient_redistribute_unset (struct zclient *zclient, int type)
843
if (! zclient->redist[type])
846
zclient->redist[type] = 0;
848
if (zclient->sock > 0)
849
zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient->sock, type);
853
zclient_redistribute_default_set (struct zclient *zclient)
855
if (zclient->default_information)
858
zclient->default_information = 1;
860
if (zclient->sock > 0)
861
zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD);
865
zclient_redistribute_default_unset (struct zclient *zclient)
867
if (! zclient->default_information)
870
zclient->default_information = 0;
872
if (zclient->sock > 0)
873
zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE);
876
extern struct thread_master *master;
879
zclient_event (enum event event, struct zclient *zclient)
883
case ZCLIENT_SCHEDULE:
884
if (! zclient->t_connect)
886
thread_add_event (master, zclient_connect, zclient, 0);
888
case ZCLIENT_CONNECT:
889
if (zclient->fail >= 10)
892
zlog_info ("zclient connect schedule interval is %d",
893
zclient->fail < 3 ? 10 : 60);
894
if (! zclient->t_connect)
896
thread_add_timer (master, zclient_connect, zclient,
897
zclient->fail < 3 ? 10 : 60);
901
thread_add_read (master, zclient_read, zclient, zclient->sock);