]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
lib, zebra: Cleanup vrf api some
[mirror_frr.git] / lib / zclient.c
1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
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,
20 * MA 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "prefix.h"
26 #include "stream.h"
27 #include "buffer.h"
28 #include "network.h"
29 #include "vrf.h"
30 #include "vrf_int.h"
31 #include "if.h"
32 #include "log.h"
33 #include "thread.h"
34 #include "zclient.h"
35 #include "memory.h"
36 #include "table.h"
37 #include "nexthop.h"
38 #include "mpls.h"
39
40 DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient")
41 DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs")
42
43 /* Zebra client events. */
44 enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
45
46 /* Prototype for event manager. */
47 static void zclient_event (enum event, struct zclient *);
48
49 const char *zclient_serv_path = NULL;
50
51 /* This file local debug flag. */
52 int zclient_debug = 0;
53
54 /* Allocate zclient structure. */
55 struct zclient *
56 zclient_new (struct thread_master *master)
57 {
58 struct zclient *zclient;
59 zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
60
61 zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
62 zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
63 zclient->wb = buffer_new(0);
64 zclient->master = master;
65
66 return zclient;
67 }
68
69 /* This function is only called when exiting, because
70 many parts of the code do not check for I/O errors, so they could
71 reference an invalid pointer if the structure was ever freed.
72
73 Free zclient structure. */
74 void
75 zclient_free (struct zclient *zclient)
76 {
77 if (zclient->ibuf)
78 stream_free(zclient->ibuf);
79 if (zclient->obuf)
80 stream_free(zclient->obuf);
81 if (zclient->wb)
82 buffer_free(zclient->wb);
83
84 XFREE (MTYPE_ZCLIENT, zclient);
85 }
86
87 u_short *
88 redist_check_instance (struct redist_proto *red, u_short instance)
89 {
90 struct listnode *node;
91 u_short *id;
92
93 if (!red->instances)
94 return NULL;
95
96 for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
97 if (*id == instance)
98 return id;
99
100 return NULL;
101 }
102
103 void
104 redist_add_instance (struct redist_proto *red, u_short instance)
105 {
106 u_short *in;
107
108 red->enabled = 1;
109
110 if (!red->instances)
111 red->instances = list_new();
112
113 in = XMALLOC (MTYPE_REDIST_INST, sizeof(u_short));
114 *in = instance;
115 listnode_add (red->instances, in);
116 }
117
118 void
119 redist_del_instance (struct redist_proto *red, u_short instance)
120 {
121 u_short *id;
122
123 id = redist_check_instance (red, instance);
124 if (! id)
125 return;
126
127 listnode_delete(red->instances, id);
128 XFREE (MTYPE_REDIST_INST, id);
129 if (!red->instances->count)
130 {
131 red->enabled = 0;
132 list_free(red->instances);
133 red->instances = NULL;
134 }
135 }
136
137 /* Stop zebra client services. */
138 void
139 zclient_stop (struct zclient *zclient)
140 {
141 afi_t afi;
142 int i;
143
144 if (zclient_debug)
145 zlog_debug ("zclient stopped");
146
147 /* Stop threads. */
148 THREAD_OFF(zclient->t_read);
149 THREAD_OFF(zclient->t_connect);
150 THREAD_OFF(zclient->t_write);
151
152 /* Reset streams. */
153 stream_reset(zclient->ibuf);
154 stream_reset(zclient->obuf);
155
156 /* Empty the write buffer. */
157 buffer_reset(zclient->wb);
158
159 /* Close socket. */
160 if (zclient->sock >= 0)
161 {
162 close (zclient->sock);
163 zclient->sock = -1;
164 }
165 zclient->fail = 0;
166
167 for (afi = AFI_IP; afi < AFI_MAX; afi++)
168 {
169 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
170 {
171 vrf_bitmap_free(zclient->redist[afi][i]);
172 zclient->redist[afi][i] = VRF_BITMAP_NULL;
173 }
174 redist_del_instance(&zclient->mi_redist[afi][zclient->redist_default],
175 zclient->instance);
176 }
177
178 vrf_bitmap_free(zclient->default_information);
179 zclient->default_information = VRF_BITMAP_NULL;
180 }
181
182 void
183 zclient_reset (struct zclient *zclient)
184 {
185 afi_t afi;
186
187 zclient_stop (zclient);
188
189 for (afi = AFI_IP; afi < AFI_MAX; afi++)
190 redist_del_instance (&zclient->mi_redist[afi][zclient->redist_default], zclient->instance);
191
192 zclient_init (zclient, zclient->redist_default, zclient->instance);
193 }
194
195 #ifdef HAVE_TCP_ZEBRA
196
197 /* Make socket to zebra daemon. Return zebra socket. */
198 static int
199 zclient_socket(void)
200 {
201 int sock;
202 int ret;
203 struct sockaddr_in serv;
204
205 /* We should think about IPv6 connection. */
206 sock = socket (AF_INET, SOCK_STREAM, 0);
207 if (sock < 0)
208 return -1;
209
210 /* Make server socket. */
211 memset (&serv, 0, sizeof (struct sockaddr_in));
212 serv.sin_family = AF_INET;
213 serv.sin_port = htons (ZEBRA_PORT);
214 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
215 serv.sin_len = sizeof (struct sockaddr_in);
216 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
217 serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
218
219 /* Connect to zebra. */
220 ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
221 if (ret < 0)
222 {
223 if (zclient_debug)
224 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__,
225 errno, safe_strerror (errno));
226 close (sock);
227 return -1;
228 }
229 return sock;
230 }
231
232 #else
233
234 /* For sockaddr_un. */
235 #include <sys/un.h>
236
237 static int
238 zclient_socket_un (const char *path)
239 {
240 int ret;
241 int sock, len;
242 struct sockaddr_un addr;
243
244 sock = socket (AF_UNIX, SOCK_STREAM, 0);
245 if (sock < 0)
246 return -1;
247
248 /* Make server socket. */
249 memset (&addr, 0, sizeof (struct sockaddr_un));
250 addr.sun_family = AF_UNIX;
251 strncpy (addr.sun_path, path, strlen (path));
252 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
253 len = addr.sun_len = SUN_LEN(&addr);
254 #else
255 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
256 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
257
258 ret = connect (sock, (struct sockaddr *) &addr, len);
259 if (ret < 0)
260 {
261 if (zclient_debug)
262 zlog_warn ("%s connect failure: %d(%s)", __PRETTY_FUNCTION__,
263 errno, safe_strerror (errno));
264 close (sock);
265 return -1;
266 }
267 return sock;
268 }
269
270 #endif /* HAVE_TCP_ZEBRA */
271
272 /**
273 * Connect to zebra daemon.
274 * @param zclient a pointer to zclient structure
275 * @return socket fd just to make sure that connection established
276 * @see zclient_init
277 * @see zclient_new
278 */
279 int
280 zclient_socket_connect (struct zclient *zclient)
281 {
282 #ifdef HAVE_TCP_ZEBRA
283 zclient->sock = zclient_socket ();
284 #else
285 zclient->sock = zclient_socket_un (zclient_serv_path_get());
286 #endif
287 return zclient->sock;
288 }
289
290 static int
291 zclient_failed(struct zclient *zclient)
292 {
293 zclient->fail++;
294 zclient_stop(zclient);
295 zclient_event(ZCLIENT_CONNECT, zclient);
296 return -1;
297 }
298
299 static int
300 zclient_flush_data(struct thread *thread)
301 {
302 struct zclient *zclient = THREAD_ARG(thread);
303
304 zclient->t_write = NULL;
305 if (zclient->sock < 0)
306 return -1;
307 switch (buffer_flush_available(zclient->wb, zclient->sock))
308 {
309 case BUFFER_ERROR:
310 zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
311 __func__, zclient->sock);
312 return zclient_failed(zclient);
313 break;
314 case BUFFER_PENDING:
315 zclient->t_write = NULL;
316 thread_add_write(zclient->master, zclient_flush_data, zclient, zclient->sock,
317 &zclient->t_write);
318 break;
319 case BUFFER_EMPTY:
320 break;
321 }
322 return 0;
323 }
324
325 int
326 zclient_send_message(struct zclient *zclient)
327 {
328 if (zclient->sock < 0)
329 return -1;
330 switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
331 stream_get_endp(zclient->obuf)))
332 {
333 case BUFFER_ERROR:
334 zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
335 __func__, zclient->sock);
336 return zclient_failed(zclient);
337 break;
338 case BUFFER_EMPTY:
339 THREAD_OFF(zclient->t_write);
340 break;
341 case BUFFER_PENDING:
342 thread_add_write(zclient->master, zclient_flush_data, zclient,
343 zclient->sock, &zclient->t_write);
344 break;
345 }
346 return 0;
347 }
348
349 void
350 zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
351 {
352 /* length placeholder, caller can update */
353 stream_putw (s, ZEBRA_HEADER_SIZE);
354 stream_putc (s, ZEBRA_HEADER_MARKER);
355 stream_putc (s, ZSERV_VERSION);
356 stream_putw (s, vrf_id);
357 stream_putw (s, command);
358 }
359
360 int
361 zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
362 u_char *version, vrf_id_t *vrf_id, u_int16_t *cmd)
363 {
364 if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
365 return -1;
366
367 *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
368 *marker = stream_getc (s);
369 *version = stream_getc (s);
370 *vrf_id = stream_getw (s);
371 *cmd = stream_getw (s);
372
373 if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
374 {
375 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
376 __func__, sock, *marker, *version);
377 return -1;
378 }
379
380 if (*size && stream_read (s, sock, *size) != *size)
381 return -1;
382
383 return 0;
384 }
385
386 /* Send simple Zebra message. */
387 static int
388 zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
389 {
390 struct stream *s;
391
392 /* Get zclient output buffer. */
393 s = zclient->obuf;
394 stream_reset (s);
395
396 /* Send very simple command only Zebra message. */
397 zclient_create_header (s, command, vrf_id);
398
399 return zclient_send_message(zclient);
400 }
401
402 static int
403 zebra_hello_send (struct zclient *zclient)
404 {
405 struct stream *s;
406
407 if (zclient->redist_default)
408 {
409 s = zclient->obuf;
410 stream_reset (s);
411
412 /* The VRF ID in the HELLO message is always 0. */
413 zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
414 stream_putc (s, zclient->redist_default);
415 stream_putw (s, zclient->instance);
416 stream_putw_at (s, 0, stream_get_endp (s));
417 return zclient_send_message(zclient);
418 }
419
420 return 0;
421 }
422
423 /* Send register requests to zebra daemon for the information in a VRF. */
424 void
425 zclient_send_reg_requests (struct zclient *zclient, vrf_id_t vrf_id)
426 {
427 int i;
428 afi_t afi;
429
430 /* zclient is disabled. */
431 if (! zclient->enable)
432 return;
433
434 /* If not connected to the zebra yet. */
435 if (zclient->sock < 0)
436 return;
437
438 if (zclient_debug)
439 zlog_debug ("%s: send register messages for VRF %u", __func__, vrf_id);
440
441 /* We need router-id information. */
442 zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
443
444 /* We need interface information. */
445 zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
446
447 /* Set unwanted redistribute route. */
448 for (afi = AFI_IP; afi < AFI_MAX; afi++)
449 vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id);
450
451 /* Flush all redistribute request. */
452 if (vrf_id == VRF_DEFAULT)
453 for (afi = AFI_IP; afi < AFI_MAX; afi++)
454 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
455 if (zclient->mi_redist[afi][i].enabled)
456 {
457 struct listnode *node;
458 u_short *id;
459
460 for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id))
461 if (!(i == zclient->redist_default && *id == zclient->instance))
462 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i,
463 *id, VRF_DEFAULT);
464 }
465
466 /* Flush all redistribute request. */
467 for (afi = AFI_IP; afi < AFI_MAX; afi++)
468 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
469 if (i != zclient->redist_default &&
470 vrf_bitmap_check (zclient->redist[afi][i], vrf_id))
471 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, i, 0, vrf_id);
472
473 /* If default information is needed. */
474 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
475 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
476 }
477
478 /* Send unregister requests to zebra daemon for the information in a VRF. */
479 void
480 zclient_send_dereg_requests (struct zclient *zclient, vrf_id_t vrf_id)
481 {
482 int i;
483 afi_t afi;
484
485 /* zclient is disabled. */
486 if (! zclient->enable)
487 return;
488
489 /* If not connected to the zebra yet. */
490 if (zclient->sock < 0)
491 return;
492
493 if (zclient_debug)
494 zlog_debug ("%s: send deregister messages for VRF %u", __func__, vrf_id);
495
496 /* We need router-id information. */
497 zebra_message_send (zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id);
498
499 /* We need interface information. */
500 zebra_message_send (zclient, ZEBRA_INTERFACE_DELETE, vrf_id);
501
502 /* Set unwanted redistribute route. */
503 for (afi = AFI_IP; afi < AFI_MAX; afi++)
504 vrf_bitmap_set (zclient->redist[afi][zclient->redist_default], vrf_id);
505
506 /* Flush all redistribute request. */
507 if (vrf_id == VRF_DEFAULT)
508 for (afi = AFI_IP; afi < AFI_MAX; afi++)
509 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
510 if (zclient->mi_redist[afi][i].enabled)
511 {
512 struct listnode *node;
513 u_short *id;
514
515 for (ALL_LIST_ELEMENTS_RO(zclient->mi_redist[afi][i].instances, node, id))
516 if (!(i == zclient->redist_default && *id == zclient->instance))
517 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i,
518 *id, VRF_DEFAULT);
519 }
520
521 /* Flush all redistribute request. */
522 for (afi = AFI_IP; afi < AFI_MAX; afi++)
523 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
524 if (i != zclient->redist_default &&
525 vrf_bitmap_check (zclient->redist[afi][i], vrf_id))
526 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, i, 0, vrf_id);
527
528 /* If default information is needed. */
529 if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
530 zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, vrf_id);
531 }
532
533 /* Send request to zebra daemon to start or stop RA. */
534 void
535 zclient_send_interface_radv_req (struct zclient *zclient, vrf_id_t vrf_id,
536 struct interface *ifp, int enable, int ra_interval)
537 {
538 struct stream *s;
539
540 /* zclient is disabled. */
541 if (!zclient->enable)
542 return;
543
544 /* If not connected to the zebra yet. */
545 if (zclient->sock < 0)
546 return;
547
548 /* Form and send message. */
549 s = zclient->obuf;
550 stream_reset (s);
551
552 if (enable)
553 zclient_create_header (s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id);
554 else
555 zclient_create_header (s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id);
556
557 stream_putl (s, ifp->ifindex);
558 stream_putl (s, ra_interval);
559
560 stream_putw_at (s, 0, stream_get_endp (s));
561
562 zclient_send_message(zclient);
563 }
564
565 /* Make connection to zebra daemon. */
566 int
567 zclient_start (struct zclient *zclient)
568 {
569 if (zclient_debug)
570 zlog_info ("zclient_start is called");
571
572 /* zclient is disabled. */
573 if (! zclient->enable)
574 return 0;
575
576 /* If already connected to the zebra. */
577 if (zclient->sock >= 0)
578 return 0;
579
580 /* Check connect thread. */
581 if (zclient->t_connect)
582 return 0;
583
584 if (zclient_socket_connect(zclient) < 0)
585 {
586 if (zclient_debug)
587 zlog_debug ("zclient connection fail");
588 zclient->fail++;
589 zclient_event (ZCLIENT_CONNECT, zclient);
590 return -1;
591 }
592
593 if (set_nonblocking(zclient->sock) < 0)
594 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
595
596 /* Clear fail count. */
597 zclient->fail = 0;
598 if (zclient_debug)
599 zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
600
601 /* Create read thread. */
602 zclient_event (ZCLIENT_READ, zclient);
603
604 zebra_hello_send (zclient);
605
606 /* Inform the successful connection. */
607 if (zclient->zebra_connected)
608 (*zclient->zebra_connected) (zclient);
609
610 return 0;
611 }
612
613 /* Initialize zebra client. Argument redist_default is unwanted
614 redistribute route type. */
615 void
616 zclient_init (struct zclient *zclient, int redist_default, u_short instance)
617 {
618 int afi, i;
619
620 /* Enable zebra client connection by default. */
621 zclient->enable = 1;
622
623 /* Set -1 to the default socket value. */
624 zclient->sock = -1;
625
626 /* Clear redistribution flags. */
627 for (afi = AFI_IP; afi < AFI_MAX; afi++)
628 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
629 zclient->redist[afi][i] = vrf_bitmap_init();
630
631 /* Set unwanted redistribute route. bgpd does not need BGP route
632 redistribution. */
633 zclient->redist_default = redist_default;
634 zclient->instance = instance;
635 /* Pending: make afi(s) an arg. */
636 for (afi = AFI_IP; afi < AFI_MAX; afi++)
637 redist_add_instance (&zclient->mi_redist[afi][redist_default], instance);
638
639 /* Set default-information redistribute to zero. */
640 zclient->default_information = vrf_bitmap_init ();;
641
642 if (zclient_debug)
643 zlog_debug ("zclient_start is called");
644
645 zclient_event (ZCLIENT_SCHEDULE, zclient);
646 }
647
648 /* This function is a wrapper function for calling zclient_start from
649 timer or event thread. */
650 static int
651 zclient_connect (struct thread *t)
652 {
653 struct zclient *zclient;
654
655 zclient = THREAD_ARG (t);
656 zclient->t_connect = NULL;
657
658 if (zclient_debug)
659 zlog_debug ("zclient_connect is called");
660
661 return zclient_start (zclient);
662 }
663
664 /*
665 * "xdr_encode"-like interface that allows daemon (client) to send
666 * a message to zebra server for a route that needs to be
667 * added/deleted to the kernel. Info about the route is specified
668 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
669 * the info down the zclient socket using the stream_* functions.
670 *
671 * The corresponding read ("xdr_decode") function on the server
672 * side is zread_ipv4_add()/zread_ipv4_delete().
673 *
674 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * | Length (2) | Command | Route Type |
677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
678 * | ZEBRA Flags | Message Flags | Prefix length |
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * | Destination IPv4 Prefix for route |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 * | Nexthop count |
683 * +-+-+-+-+-+-+-+-+
684 *
685 *
686 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
687 * described, as per the Nexthop count. Each nexthop described as:
688 *
689 * +-+-+-+-+-+-+-+-+
690 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
691 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
692 * | IPv4 Nexthop address or Interface Index number |
693 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
694 *
695 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
696 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
697 * nexthop information is provided, and the message describes a prefix
698 * to blackhole or reject route.
699 *
700 * The original struct zapi_ipv4, zapi_ipv4_route() and zread_ipv4_*()
701 * infrastructure was built around the traditional (32-bit "gate OR
702 * ifindex") nexthop data unit. A special encoding can be used to feed
703 * onlink (64-bit "gate AND ifindex") nexthops into zapi_ipv4_route()
704 * using the same zapi_ipv4 structure. This is done by setting zapi_ipv4
705 * fields as follows:
706 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
707 * - .nexthop_num == .ifindex_num
708 * - .nexthop and .ifindex are filled with gate and ifindex parts of
709 * each compound nexthop, both in the same order
710 *
711 * zapi_ipv4_route() will produce two nexthop data units for each such
712 * interleaved 64-bit nexthop. On the zserv side of the socket it will be
713 * mapped to a singlle NEXTHOP_TYPE_IPV4_IFINDEX_OL RIB nexthop structure.
714 *
715 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
716 * byte value.
717 *
718 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
719 * byte value.
720 *
721 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
722 *
723 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
724 *
725 * XXX: No attention paid to alignment.
726 */
727 int
728 zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
729 struct zapi_ipv4 *api)
730 {
731 int i;
732 int psize;
733 struct stream *s;
734
735 /* Reset stream. */
736 s = zclient->obuf;
737 stream_reset (s);
738
739 /* Some checks for labeled-unicast. The current expectation is that each
740 * nexthop is accompanied by a label in the case of labeled-unicast.
741 */
742 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
743 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
744 {
745 /* We expect prefixes installed with labels and the number to match
746 * the number of nexthops.
747 */
748 assert (api->label_num == api->nexthop_num);
749 }
750
751 zclient_create_header (s, cmd, api->vrf_id);
752
753 /* Put type and nexthop. */
754 stream_putc (s, api->type);
755 stream_putw (s, api->instance);
756 stream_putl (s, api->flags);
757 stream_putc (s, api->message);
758 stream_putw (s, api->safi);
759
760 /* Put prefix information. */
761 psize = PSIZE (p->prefixlen);
762 stream_putc (s, p->prefixlen);
763 stream_write (s, (u_char *) & p->prefix, psize);
764
765 /* Nexthop, ifindex, distance and metric information. */
766 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
767 {
768 /* traditional 32-bit data units */
769 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
770 {
771 stream_putc (s, 1);
772 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
773 /* XXX assert(api->nexthop_num == 0); */
774 /* XXX assert(api->ifindex_num == 0); */
775 }
776 else
777 stream_putc (s, api->nexthop_num + api->ifindex_num);
778
779 for (i = 0; i < api->nexthop_num; i++)
780 {
781 stream_putc (s, NEXTHOP_TYPE_IPV4);
782 stream_put_in_addr (s, api->nexthop[i]);
783 /* For labeled-unicast, each nexthop is followed by label. */
784 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
785 stream_putl (s, api->label[i]);
786 }
787 for (i = 0; i < api->ifindex_num; i++)
788 {
789 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
790 stream_putl (s, api->ifindex[i]);
791 }
792 }
793
794 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
795 stream_putc (s, api->distance);
796 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
797 stream_putl (s, api->metric);
798 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
799 stream_putl (s, api->tag);
800 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
801 stream_putl (s, api->mtu);
802
803 /* Put length at the first point of the stream. */
804 stream_putw_at (s, 0, stream_get_endp (s));
805
806 return zclient_send_message(zclient);
807 }
808
809 int
810 zapi_ipv4_route_ipv6_nexthop (u_char cmd, struct zclient *zclient,
811 struct prefix_ipv4 *p, struct zapi_ipv6 *api)
812 {
813 int i;
814 int psize;
815 struct stream *s;
816
817 /* Reset stream. */
818 s = zclient->obuf;
819 stream_reset (s);
820
821 /* Some checks for labeled-unicast. The current expectation is that each
822 * nexthop is accompanied by a label in the case of labeled-unicast.
823 */
824 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
825 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
826 {
827 /* We expect prefixes installed with labels and the number to match
828 * the number of nexthops.
829 */
830 assert (api->label_num == api->nexthop_num);
831 }
832
833 zclient_create_header (s, cmd, api->vrf_id);
834
835 /* Put type and nexthop. */
836 stream_putc (s, api->type);
837 stream_putw (s, api->instance);
838 stream_putl (s, api->flags);
839 stream_putc (s, api->message);
840 stream_putw (s, api->safi);
841
842 /* Put prefix information. */
843 psize = PSIZE (p->prefixlen);
844 stream_putc (s, p->prefixlen);
845 stream_write (s, (u_char *) & p->prefix, psize);
846
847 /* Nexthop, ifindex, distance and metric information. */
848 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
849 {
850 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
851 {
852 stream_putc (s, 1);
853 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
854 /* XXX assert(api->nexthop_num == 0); */
855 /* XXX assert(api->ifindex_num == 0); */
856 }
857 else
858 stream_putc (s, api->nexthop_num + api->ifindex_num);
859
860 for (i = 0; i < api->nexthop_num; i++)
861 {
862 stream_putc (s, NEXTHOP_TYPE_IPV6);
863 stream_write (s, (u_char *)api->nexthop[i], 16);
864 /* For labeled-unicast, each nexthop is followed by label. */
865 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
866 stream_putl (s, api->label[i]);
867 }
868 for (i = 0; i < api->ifindex_num; i++)
869 {
870 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
871 stream_putl (s, api->ifindex[i]);
872 }
873 }
874
875 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
876 stream_putc (s, api->distance);
877 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
878 stream_putl (s, api->metric);
879 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
880 stream_putl (s, api->tag);
881 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
882 stream_putl (s, api->mtu);
883
884 /* Put length at the first point of the stream. */
885 stream_putw_at (s, 0, stream_get_endp (s));
886
887 return zclient_send_message(zclient);
888 }
889
890 int
891 zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
892 struct prefix_ipv6 *src_p, struct zapi_ipv6 *api)
893 {
894 int i;
895 int psize;
896 struct stream *s;
897
898 /* either we have !SRCPFX && src_p == NULL, or SRCPFX && src_p != NULL */
899 assert (!(api->message & ZAPI_MESSAGE_SRCPFX) == !src_p);
900
901 /* Reset stream. */
902 s = zclient->obuf;
903 stream_reset (s);
904
905 /* Some checks for labeled-unicast. The current expectation is that each
906 * nexthop is accompanied by a label in the case of labeled-unicast.
907 */
908 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL) &&
909 CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
910 {
911 /* We expect prefixes installed with labels and the number to match
912 * the number of nexthops.
913 */
914 assert (api->label_num == api->nexthop_num);
915 }
916
917 zclient_create_header (s, cmd, api->vrf_id);
918
919 /* Put type and nexthop. */
920 stream_putc (s, api->type);
921 stream_putw (s, api->instance);
922 stream_putl (s, api->flags);
923 stream_putc (s, api->message);
924 stream_putw (s, api->safi);
925
926 /* Put prefix information. */
927 psize = PSIZE (p->prefixlen);
928 stream_putc (s, p->prefixlen);
929 stream_write (s, (u_char *)&p->prefix, psize);
930
931 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_SRCPFX))
932 {
933 psize = PSIZE (src_p->prefixlen);
934 stream_putc (s, src_p->prefixlen);
935 stream_write (s, (u_char *)&src_p->prefix, psize);
936 }
937
938 /* Nexthop, ifindex, distance and metric information. */
939 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
940 {
941 if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
942 {
943 stream_putc (s, 1);
944 stream_putc (s, NEXTHOP_TYPE_BLACKHOLE);
945 /* XXX assert(api->nexthop_num == 0); */
946 /* XXX assert(api->ifindex_num == 0); */
947 }
948 else
949 stream_putc (s, api->nexthop_num + api->ifindex_num);
950
951 for (i = 0; i < api->nexthop_num; i++)
952 {
953 stream_putc (s, NEXTHOP_TYPE_IPV6);
954 stream_write (s, (u_char *)api->nexthop[i], 16);
955 /* For labeled-unicast, each nexthop is followed by label. */
956 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_LABEL))
957 stream_putl (s, api->label[i]);
958 }
959 for (i = 0; i < api->ifindex_num; i++)
960 {
961 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
962 stream_putl (s, api->ifindex[i]);
963 }
964 }
965
966 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
967 stream_putc (s, api->distance);
968 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
969 stream_putl (s, api->metric);
970 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
971 stream_putl (s, api->tag);
972 if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
973 stream_putl (s, api->mtu);
974
975 /* Put length at the first point of the stream. */
976 stream_putw_at (s, 0, stream_get_endp (s));
977
978 return zclient_send_message(zclient);
979 }
980
981 /*
982 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
983 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
984 * then set/unset redist[type] in the client handle (a struct zserv) for the
985 * sending client
986 */
987 int
988 zebra_redistribute_send (int command, struct zclient *zclient, afi_t afi, int type,
989 u_short instance, vrf_id_t vrf_id)
990 {
991 struct stream *s;
992
993 s = zclient->obuf;
994 stream_reset(s);
995
996 zclient_create_header (s, command, vrf_id);
997 stream_putc (s, afi);
998 stream_putc (s, type);
999 stream_putw (s, instance);
1000
1001 stream_putw_at (s, 0, stream_get_endp (s));
1002
1003 return zclient_send_message(zclient);
1004 }
1005
1006 /* Get prefix in ZServ format; family should be filled in on prefix */
1007 static void
1008 zclient_stream_get_prefix (struct stream *s, struct prefix *p)
1009 {
1010 size_t plen = prefix_blen (p);
1011 u_char c;
1012 p->prefixlen = 0;
1013
1014 if (plen == 0)
1015 return;
1016
1017 stream_get (&p->u.prefix, s, plen);
1018 c = stream_getc(s);
1019 p->prefixlen = MIN(plen * 8, c);
1020 }
1021
1022 /* Router-id update from zebra daemon. */
1023 void
1024 zebra_router_id_update_read (struct stream *s, struct prefix *rid)
1025 {
1026 /* Fetch interface address. */
1027 rid->family = stream_getc (s);
1028
1029 zclient_stream_get_prefix (s, rid);
1030 }
1031
1032 /* Interface addition from zebra daemon. */
1033 /*
1034 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1035 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1036 * 0 1 2 3
1037 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1038 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1039 * | ifname |
1040 * | |
1041 * | |
1042 * | |
1043 * | |
1044 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1045 * | ifindex |
1046 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1047 * | status |
1048 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1049 * | if_flags |
1050 * | |
1051 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1052 * | metric |
1053 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1054 * | speed |
1055 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1056 * | ifmtu |
1057 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1058 * | ifmtu6 |
1059 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1060 * | bandwidth |
1061 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1062 * | Link Layer Type |
1063 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1064 * | Harware Address Length |
1065 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1066 * | Hardware Address if HW lenght different from 0 |
1067 * | ... max INTERFACE_HWADDR_MAX |
1068 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1069 * | Link_params? | Whether a link-params follows: 1 or 0.
1070 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1071 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1072 * | .... (struct if_link_params). |
1073 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1074 */
1075
1076 static void
1077 zclient_vrf_add (struct zclient *zclient, vrf_id_t vrf_id)
1078 {
1079 struct vrf *vrf;
1080 char vrfname_tmp[VRF_NAMSIZ];
1081
1082 /* Read interface name. */
1083 stream_get (vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1084
1085 /* Lookup/create vrf by vrf_id. */
1086 vrf = vrf_get (vrf_id, vrfname_tmp);
1087
1088 vrf_enable (vrf);
1089 }
1090
1091 static void
1092 zclient_vrf_delete (struct zclient *zclient, vrf_id_t vrf_id)
1093 {
1094 struct vrf *vrf;
1095
1096 /* Lookup vrf by vrf_id. */
1097 vrf = vrf_lookup_by_id (vrf_id);
1098
1099 /*
1100 * If a routing protocol doesn't know about a
1101 * vrf that is about to be deleted. There is
1102 * no point in attempting to delete it.
1103 */
1104 if (!vrf)
1105 return;
1106
1107 vrf_delete (vrf);
1108 }
1109
1110 struct interface *
1111 zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
1112 {
1113 struct interface *ifp;
1114 char ifname_tmp[INTERFACE_NAMSIZ];
1115
1116 /* Read interface name. */
1117 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
1118
1119 /* Lookup/create interface by name. */
1120 ifp = if_get_by_name_len (ifname_tmp,
1121 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
1122 vrf_id, 0);
1123
1124 zebra_interface_if_set_value (s, ifp);
1125
1126 return ifp;
1127 }
1128
1129 /*
1130 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1131 * from zebra server. The format of this message is the same as
1132 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1133 * comments for zebra_interface_add_read), except that no sockaddr_dl
1134 * is sent at the tail of the message.
1135 */
1136 struct interface *
1137 zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
1138 {
1139 struct interface *ifp;
1140 char ifname_tmp[INTERFACE_NAMSIZ];
1141
1142 /* Read interface name. */
1143 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
1144
1145 /* Lookup this by interface index. */
1146 ifp = if_lookup_by_name_len (ifname_tmp,
1147 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
1148 vrf_id);
1149 if (ifp == NULL)
1150 {
1151 zlog_warn ("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1152 ifname_tmp, vrf_id);
1153 return NULL;
1154 }
1155
1156 zebra_interface_if_set_value (s, ifp);
1157
1158 return ifp;
1159 }
1160
1161 static void
1162 link_params_set_value(struct stream *s, struct if_link_params *iflp)
1163 {
1164
1165 if (iflp == NULL)
1166 return;
1167
1168 iflp->lp_status = stream_getl (s);
1169 iflp->te_metric = stream_getl (s);
1170 iflp->max_bw = stream_getf (s);
1171 iflp->max_rsv_bw = stream_getf (s);
1172 uint32_t bwclassnum = stream_getl (s);
1173 {
1174 unsigned int i;
1175 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1176 iflp->unrsv_bw[i] = stream_getf (s);
1177 if (i < bwclassnum)
1178 zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1179 " - outdated library?",
1180 __func__, bwclassnum, MAX_CLASS_TYPE);
1181 }
1182 iflp->admin_grp = stream_getl (s);
1183 iflp->rmt_as = stream_getl (s);
1184 iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
1185
1186 iflp->av_delay = stream_getl (s);
1187 iflp->min_delay = stream_getl (s);
1188 iflp->max_delay = stream_getl (s);
1189 iflp->delay_var = stream_getl (s);
1190
1191 iflp->pkt_loss = stream_getf (s);
1192 iflp->res_bw = stream_getf (s);
1193 iflp->ava_bw = stream_getf (s);
1194 iflp->use_bw = stream_getf (s);
1195 }
1196
1197 struct interface *
1198 zebra_interface_link_params_read (struct stream *s)
1199 {
1200 struct if_link_params *iflp;
1201 ifindex_t ifindex;
1202
1203 assert (s);
1204
1205 ifindex = stream_getl (s);
1206
1207 struct interface *ifp = if_lookup_by_index (ifindex, VRF_DEFAULT);
1208
1209 if (ifp == NULL)
1210 {
1211 zlog_err ("%s: unknown ifindex %u, shouldn't happen",
1212 __func__, ifindex);
1213 return NULL;
1214 }
1215
1216 if ((iflp = if_link_params_get (ifp)) == NULL)
1217 return NULL;
1218
1219 link_params_set_value(s, iflp);
1220
1221 return ifp;
1222 }
1223
1224 void
1225 zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
1226 {
1227 u_char link_params_status = 0;
1228
1229 /* Read interface's index. */
1230 ifp->ifindex = stream_getl (s);
1231 ifp->status = stream_getc (s);
1232
1233 /* Read interface's value. */
1234 ifp->flags = stream_getq (s);
1235 ifp->ptm_enable = stream_getc (s);
1236 ifp->ptm_status = stream_getc (s);
1237 ifp->metric = stream_getl (s);
1238 ifp->speed = stream_getl (s);
1239 ifp->mtu = stream_getl (s);
1240 ifp->mtu6 = stream_getl (s);
1241 ifp->bandwidth = stream_getl (s);
1242 ifp->ll_type = stream_getl (s);
1243 ifp->hw_addr_len = stream_getl (s);
1244 if (ifp->hw_addr_len)
1245 stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1246
1247 /* Read Traffic Engineering status */
1248 link_params_status = stream_getc (s);
1249 /* Then, Traffic Engineering parameters if any */
1250 if (link_params_status)
1251 {
1252 struct if_link_params *iflp = if_link_params_get (ifp);
1253 link_params_set_value(s, iflp);
1254 }
1255 }
1256
1257 size_t
1258 zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
1259 {
1260 size_t w;
1261 struct if_link_params *iflp;
1262 int i;
1263
1264 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1265 return 0;
1266
1267 iflp = ifp->link_params;
1268 w = 0;
1269
1270 w += stream_putl (s, iflp->lp_status);
1271
1272 w += stream_putl (s, iflp->te_metric);
1273 w += stream_putf (s, iflp->max_bw);
1274 w += stream_putf (s, iflp->max_rsv_bw);
1275
1276 w += stream_putl (s, MAX_CLASS_TYPE);
1277 for (i = 0; i < MAX_CLASS_TYPE; i++)
1278 w += stream_putf (s, iflp->unrsv_bw[i]);
1279
1280 w += stream_putl (s, iflp->admin_grp);
1281 w += stream_putl (s, iflp->rmt_as);
1282 w += stream_put_in_addr (s, &iflp->rmt_ip);
1283
1284 w += stream_putl (s, iflp->av_delay);
1285 w += stream_putl (s, iflp->min_delay);
1286 w += stream_putl (s, iflp->max_delay);
1287 w += stream_putl (s, iflp->delay_var);
1288
1289 w += stream_putf (s, iflp->pkt_loss);
1290 w += stream_putf (s, iflp->res_bw);
1291 w += stream_putf (s, iflp->ava_bw);
1292 w += stream_putf (s, iflp->use_bw);
1293
1294 return w;
1295 }
1296
1297 /*
1298 * format of message for address additon is:
1299 * 0
1300 * 0 1 2 3 4 5 6 7
1301 * +-+-+-+-+-+-+-+-+
1302 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1303 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1304 * | |
1305 * + +
1306 * | ifindex |
1307 * + +
1308 * | |
1309 * + +
1310 * | |
1311 * +-+-+-+-+-+-+-+-+
1312 * | ifc_flags | flags for connected address
1313 * +-+-+-+-+-+-+-+-+
1314 * | addr_family |
1315 * +-+-+-+-+-+-+-+-+
1316 * | addr... |
1317 * : :
1318 * | |
1319 * +-+-+-+-+-+-+-+-+
1320 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1321 * +-+-+-+-+-+-+-+-+
1322 * | daddr.. |
1323 * : :
1324 * | |
1325 * +-+-+-+-+-+-+-+-+
1326 */
1327
1328 static int
1329 memconstant(const void *s, int c, size_t n)
1330 {
1331 const u_char *p = s;
1332
1333 while (n-- > 0)
1334 if (*p++ != c)
1335 return 0;
1336 return 1;
1337 }
1338
1339
1340 struct connected *
1341 zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
1342 {
1343 ifindex_t ifindex;
1344 struct interface *ifp;
1345 struct connected *ifc;
1346 struct prefix p, d, *dp;
1347 int plen;
1348 u_char ifc_flags;
1349
1350 memset (&p, 0, sizeof(p));
1351 memset (&d, 0, sizeof(d));
1352
1353 /* Get interface index. */
1354 ifindex = stream_getl (s);
1355
1356 /* Lookup index. */
1357 ifp = if_lookup_by_index (ifindex, vrf_id);
1358 if (ifp == NULL)
1359 {
1360 zlog_warn ("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1361 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1362 ifindex, vrf_id);
1363 return NULL;
1364 }
1365
1366 /* Fetch flag. */
1367 ifc_flags = stream_getc (s);
1368
1369 /* Fetch interface address. */
1370 d.family = p.family = stream_getc (s);
1371 plen = prefix_blen (&d);
1372
1373 zclient_stream_get_prefix (s, &p);
1374
1375 /* Fetch destination address. */
1376 stream_get (&d.u.prefix, s, plen);
1377
1378 /* N.B. NULL destination pointers are encoded as all zeroes */
1379 dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
1380
1381 if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
1382 {
1383 ifc = connected_lookup_prefix_exact (ifp, &p);
1384 if (!ifc)
1385 {
1386 /* N.B. NULL destination pointers are encoded as all zeroes */
1387 ifc = connected_add_by_prefix(ifp, &p, dp);
1388 }
1389 if (ifc)
1390 {
1391 ifc->flags = ifc_flags;
1392 if (ifc->destination)
1393 ifc->destination->prefixlen = ifc->address->prefixlen;
1394 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
1395 {
1396 /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
1397 char buf[PREFIX_STRLEN];
1398 zlog_warn("warning: interface %s address %s "
1399 "with peer flag set, but no peer address!",
1400 ifp->name,
1401 prefix2str (ifc->address, buf, sizeof buf));
1402 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1403 }
1404 }
1405 }
1406 else
1407 {
1408 assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1409 ifc = connected_delete_by_prefix(ifp, &p);
1410 }
1411
1412 return ifc;
1413 }
1414
1415 /*
1416 * format of message for neighbor connected address is:
1417 * 0
1418 * 0 1 2 3 4 5 6 7
1419 * +-+-+-+-+-+-+-+-+
1420 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1421 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1422 * | |
1423 * + +
1424 * | ifindex |
1425 * + +
1426 * | |
1427 * + +
1428 * | |
1429 * +-+-+-+-+-+-+-+-+
1430 * | addr_family |
1431 * +-+-+-+-+-+-+-+-+
1432 * | addr... |
1433 * : :
1434 * | |
1435 * +-+-+-+-+-+-+-+-+
1436 * | addr_len | len of addr.
1437 * +-+-+-+-+-+-+-+-+
1438 */
1439 struct nbr_connected *
1440 zebra_interface_nbr_address_read (int type, struct stream *s, vrf_id_t vrf_id)
1441 {
1442 unsigned int ifindex;
1443 struct interface *ifp;
1444 struct prefix p;
1445 struct nbr_connected *ifc;
1446
1447 /* Get interface index. */
1448 ifindex = stream_getl (s);
1449
1450 /* Lookup index. */
1451 ifp = if_lookup_by_index (ifindex, vrf_id);
1452 if (ifp == NULL)
1453 {
1454 zlog_warn ("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1455 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD" : "DELETE",
1456 ifindex, vrf_id);
1457 return NULL;
1458 }
1459
1460 p.family = stream_getc (s);
1461 stream_get (&p.u.prefix, s, prefix_blen (&p));
1462 p.prefixlen = stream_getc (s);
1463
1464 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD)
1465 {
1466 /* Currently only supporting P2P links, so any new RA source address is
1467 considered as the replacement of the previously learnt Link-Local address. */
1468 if (!(ifc = listnode_head(ifp->nbr_connected)))
1469 {
1470 ifc = nbr_connected_new ();
1471 ifc->address = prefix_new ();
1472 ifc->ifp = ifp;
1473 listnode_add (ifp->nbr_connected, ifc);
1474 }
1475
1476 prefix_copy(ifc->address, &p);
1477 }
1478 else
1479 {
1480 assert (type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1481
1482 ifc = nbr_connected_check(ifp, &p);
1483 if (ifc)
1484 listnode_delete (ifp->nbr_connected, ifc);
1485 }
1486
1487 return ifc;
1488 }
1489
1490 struct interface *
1491 zebra_interface_vrf_update_read (struct stream *s, vrf_id_t vrf_id,
1492 vrf_id_t *new_vrf_id)
1493 {
1494 unsigned int ifindex;
1495 struct interface *ifp;
1496 vrf_id_t new_id = VRF_DEFAULT;
1497
1498 /* Get interface index. */
1499 ifindex = stream_getl (s);
1500
1501 /* Lookup interface. */
1502 ifp = if_lookup_by_index (ifindex, vrf_id);
1503 if (ifp == NULL)
1504 {
1505 zlog_warn ("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1506 ifindex, vrf_id);
1507 return NULL;
1508 }
1509
1510 /* Fetch new VRF Id. */
1511 new_id = stream_getw (s);
1512
1513 *new_vrf_id = new_id;
1514 return ifp;
1515 }
1516 /**
1517 * Connect to label manager in a syncronous way
1518 *
1519 * It first writes the request to zcient output buffer and then
1520 * immediately reads the answer from the input buffer.
1521 *
1522 * @param zclient Zclient used to connect to label manager (zebra)
1523 * @result Result of response
1524 */
1525 int
1526 lm_label_manager_connect (struct zclient *zclient)
1527 {
1528 int ret;
1529 struct stream *s;
1530 u_char result;
1531 u_int16_t size;
1532 u_char marker;
1533 u_char version;
1534 vrf_id_t vrf_id;
1535 u_int16_t cmd;
1536
1537 if (zclient_debug)
1538 zlog_debug ("Connecting to Label Manager");
1539
1540 if (zclient->sock < 0)
1541 return -1;
1542
1543 /* send request */
1544 s = zclient->obuf;
1545 stream_reset (s);
1546 zclient_create_header (s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
1547
1548 /* proto */
1549 stream_putc (s, zclient->redist_default);
1550 /* instance */
1551 stream_putw (s, zclient->instance);
1552
1553 /* Put length at the first point of the stream. */
1554 stream_putw_at(s, 0, stream_get_endp(s));
1555
1556 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1557 if (ret < 0)
1558 {
1559 zlog_err ("%s: can't write to zclient->sock", __func__);
1560 close (zclient->sock);
1561 zclient->sock = -1;
1562 return -1;
1563 }
1564 if (ret == 0)
1565 {
1566 zlog_err ("%s: zclient->sock connection closed", __func__);
1567 close (zclient->sock);
1568 zclient->sock = -1;
1569 return -1;
1570 }
1571 if (zclient_debug)
1572 zlog_debug ("%s: Label manager connect request (%d bytes) sent", __func__, ret);
1573
1574 /* read response */
1575 s = zclient->ibuf;
1576 stream_reset (s);
1577
1578 ret = zclient_read_header (s, zclient->sock, &size, &marker, &version,
1579 &vrf_id, &cmd);
1580 if (ret != 0 || cmd != ZEBRA_LABEL_MANAGER_CONNECT) {
1581 zlog_err ("%s: Invalid Label Manager Connect Message Reply Header", __func__);
1582 return -1;
1583 }
1584 /* result */
1585 result = stream_getc(s);
1586 if (zclient_debug)
1587 zlog_debug ("%s: Label Manager connect response (%d bytes) received, result %u",
1588 __func__, size, result);
1589
1590 return (int)result;
1591 }
1592
1593 /**
1594 * Function to request a label chunk in a syncronous way
1595 *
1596 * It first writes the request to zlcient output buffer and then
1597 * immediately reads the answer from the input buffer.
1598 *
1599 * @param zclient Zclient used to connect to label manager (zebra)
1600 * @param keep Avoid garbage collection
1601 * @param chunk_size Amount of labels requested
1602 * @param start To write first assigned chunk label to
1603 * @param end To write last assigned chunk label to
1604 * @result 0 on success, -1 otherwise
1605 */
1606 int
1607 lm_get_label_chunk (struct zclient *zclient, u_char keep, uint32_t chunk_size,
1608 uint32_t *start, uint32_t *end)
1609 {
1610 int ret;
1611 struct stream *s;
1612 u_int16_t size;
1613 u_char marker;
1614 u_char version;
1615 vrf_id_t vrf_id;
1616 u_int16_t cmd;
1617 u_char response_keep;
1618
1619 if (zclient_debug)
1620 zlog_debug ("Getting Label Chunk");
1621
1622 if (zclient->sock < 0)
1623 return -1;
1624
1625 /* send request */
1626 s = zclient->obuf;
1627 stream_reset (s);
1628 zclient_create_header (s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1629 /* keep */
1630 stream_putc (s, keep);
1631 /* chunk size */
1632 stream_putl (s, chunk_size);
1633 /* Put length at the first point of the stream. */
1634 stream_putw_at(s, 0, stream_get_endp(s));
1635
1636 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1637 if (ret < 0)
1638 {
1639 zlog_err ("%s: can't write to zclient->sock", __func__);
1640 close (zclient->sock);
1641 zclient->sock = -1;
1642 return -1;
1643 }
1644 if (ret == 0)
1645 {
1646 zlog_err ("%s: zclient->sock connection closed", __func__);
1647 close (zclient->sock);
1648 zclient->sock = -1;
1649 return -1;
1650 }
1651 if (zclient_debug)
1652 zlog_debug ("%s: Label chunk request (%d bytes) sent", __func__, ret);
1653
1654 /* read response */
1655 s = zclient->ibuf;
1656 stream_reset (s);
1657
1658 ret = zclient_read_header (s, zclient->sock, &size, &marker, &version,
1659 &vrf_id, &cmd);
1660 if (ret != 0 || cmd != ZEBRA_GET_LABEL_CHUNK) {
1661 zlog_err ("%s: Invalid Get Label Chunk Message Reply Header", __func__);
1662 return -1;
1663 }
1664 if (zclient_debug)
1665 zlog_debug ("%s: Label chunk response (%d bytes) received", __func__, size);
1666
1667 /* keep */
1668 response_keep = stream_getc(s);
1669 /* start and end labels */
1670 *start = stream_getl(s);
1671 *end = stream_getl(s);
1672
1673 /* not owning this response */
1674 if (keep != response_keep) {
1675 zlog_err ("%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1676 __func__, *start, *end, keep, response_keep);
1677 }
1678 /* sanity */
1679 if (*start > *end
1680 || *start < MPLS_MIN_UNRESERVED_LABEL
1681 || *end > MPLS_MAX_UNRESERVED_LABEL) {
1682 zlog_err ("%s: Invalid Label chunk: %u - %u", __func__,
1683 *start, *end);
1684 return -1;
1685 }
1686
1687 if (zclient_debug)
1688 zlog_debug ("Label Chunk assign: %u - %u (%u) ",
1689 *start, *end, response_keep);
1690
1691 return 0;
1692 }
1693
1694 /**
1695 * Function to release a label chunk
1696 *
1697 * @param zclient Zclient used to connect to label manager (zebra)
1698 * @param start First label of chunk
1699 * @param end Last label of chunk
1700 * @result 0 on success, -1 otherwise
1701 */
1702 int
1703 lm_release_label_chunk (struct zclient *zclient, uint32_t start, uint32_t end)
1704 {
1705 int ret;
1706 struct stream *s;
1707
1708 if (zclient_debug)
1709 zlog_debug ("Releasing Label Chunk");
1710
1711 if (zclient->sock < 0)
1712 return -1;
1713
1714 /* send request */
1715 s = zclient->obuf;
1716 stream_reset (s);
1717 zclient_create_header (s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
1718
1719 /* start */
1720 stream_putl (s, start);
1721 /* end */
1722 stream_putl (s, end);
1723
1724 /* Put length at the first point of the stream. */
1725 stream_putw_at(s, 0, stream_get_endp(s));
1726
1727 ret = writen (zclient->sock, s->data, stream_get_endp (s));
1728 if (ret < 0)
1729 {
1730 zlog_err ("%s: can't write to zclient->sock", __func__);
1731 close (zclient->sock);
1732 zclient->sock = -1;
1733 return -1;
1734 }
1735 if (ret == 0)
1736 {
1737 zlog_err ("%s: zclient->sock connection closed", __func__);
1738 close (zclient->sock);
1739 zclient->sock = -1;
1740 return -1;
1741 }
1742
1743 return 0;
1744 }
1745
1746 /* Zebra client message read function. */
1747 static int
1748 zclient_read (struct thread *thread)
1749 {
1750 size_t already;
1751 uint16_t length, command;
1752 uint8_t marker, version;
1753 vrf_id_t vrf_id;
1754 struct zclient *zclient;
1755
1756 /* Get socket to zebra. */
1757 zclient = THREAD_ARG (thread);
1758 zclient->t_read = NULL;
1759
1760 /* Read zebra header (if we don't have it already). */
1761 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
1762 {
1763 ssize_t nbyte;
1764 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1765 ZEBRA_HEADER_SIZE-already)) == 0) ||
1766 (nbyte == -1))
1767 {
1768 if (zclient_debug)
1769 zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
1770 return zclient_failed(zclient);
1771 }
1772 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
1773 {
1774 /* Try again later. */
1775 zclient_event (ZCLIENT_READ, zclient);
1776 return 0;
1777 }
1778 already = ZEBRA_HEADER_SIZE;
1779 }
1780
1781 /* Reset to read from the beginning of the incoming packet. */
1782 stream_set_getp(zclient->ibuf, 0);
1783
1784 /* Fetch header values. */
1785 length = stream_getw (zclient->ibuf);
1786 marker = stream_getc (zclient->ibuf);
1787 version = stream_getc (zclient->ibuf);
1788 vrf_id = stream_getw (zclient->ibuf);
1789 command = stream_getw (zclient->ibuf);
1790
1791 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1792 {
1793 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1794 __func__, zclient->sock, marker, version);
1795 return zclient_failed(zclient);
1796 }
1797
1798 if (length < ZEBRA_HEADER_SIZE)
1799 {
1800 zlog_err("%s: socket %d message length %u is less than %d ",
1801 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
1802 return zclient_failed(zclient);
1803 }
1804
1805 /* Length check. */
1806 if (length > STREAM_SIZE(zclient->ibuf))
1807 {
1808 struct stream *ns;
1809 zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
1810 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
1811 ns = stream_new(length);
1812 stream_copy(ns, zclient->ibuf);
1813 stream_free (zclient->ibuf);
1814 zclient->ibuf = ns;
1815 }
1816
1817 /* Read rest of zebra packet. */
1818 if (already < length)
1819 {
1820 ssize_t nbyte;
1821 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
1822 length-already)) == 0) ||
1823 (nbyte == -1))
1824 {
1825 if (zclient_debug)
1826 zlog_debug("zclient connection closed socket [%d].", zclient->sock);
1827 return zclient_failed(zclient);
1828 }
1829 if (nbyte != (ssize_t)(length-already))
1830 {
1831 /* Try again later. */
1832 zclient_event (ZCLIENT_READ, zclient);
1833 return 0;
1834 }
1835 }
1836
1837 length -= ZEBRA_HEADER_SIZE;
1838
1839 if (zclient_debug)
1840 zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
1841
1842 switch (command)
1843 {
1844 case ZEBRA_ROUTER_ID_UPDATE:
1845 if (zclient->router_id_update)
1846 (*zclient->router_id_update) (command, zclient, length, vrf_id);
1847 break;
1848 case ZEBRA_VRF_ADD:
1849 zclient_vrf_add (zclient, vrf_id);
1850 break;
1851 case ZEBRA_VRF_DELETE:
1852 zclient_vrf_delete (zclient, vrf_id);
1853 break;
1854 case ZEBRA_INTERFACE_ADD:
1855 if (zclient->interface_add)
1856 (*zclient->interface_add) (command, zclient, length, vrf_id);
1857 break;
1858 case ZEBRA_INTERFACE_DELETE:
1859 if (zclient->interface_delete)
1860 (*zclient->interface_delete) (command, zclient, length, vrf_id);
1861 break;
1862 case ZEBRA_INTERFACE_ADDRESS_ADD:
1863 if (zclient->interface_address_add)
1864 (*zclient->interface_address_add) (command, zclient, length, vrf_id);
1865 break;
1866 case ZEBRA_INTERFACE_ADDRESS_DELETE:
1867 if (zclient->interface_address_delete)
1868 (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
1869 break;
1870 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
1871 if (zclient->interface_bfd_dest_update)
1872 (*zclient->interface_bfd_dest_update) (command, zclient, length, vrf_id);
1873 break;
1874 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
1875 if (zclient->interface_nbr_address_add)
1876 (*zclient->interface_nbr_address_add) (command, zclient, length, vrf_id);
1877 break;
1878 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
1879 if (zclient->interface_nbr_address_delete)
1880 (*zclient->interface_nbr_address_delete) (command, zclient, length, vrf_id);
1881 break;
1882 case ZEBRA_INTERFACE_UP:
1883 if (zclient->interface_up)
1884 (*zclient->interface_up) (command, zclient, length, vrf_id);
1885 break;
1886 case ZEBRA_INTERFACE_DOWN:
1887 if (zclient->interface_down)
1888 (*zclient->interface_down) (command, zclient, length, vrf_id);
1889 break;
1890 case ZEBRA_INTERFACE_VRF_UPDATE:
1891 if (zclient->interface_vrf_update)
1892 (*zclient->interface_vrf_update) (command, zclient, length, vrf_id);
1893 break;
1894 case ZEBRA_NEXTHOP_UPDATE:
1895 if (zclient_debug)
1896 zlog_debug("zclient rcvd nexthop update\n");
1897 if (zclient->nexthop_update)
1898 (*zclient->nexthop_update) (command, zclient, length, vrf_id);
1899 break;
1900 case ZEBRA_IMPORT_CHECK_UPDATE:
1901 if (zclient_debug)
1902 zlog_debug("zclient rcvd import check update\n");
1903 if (zclient->import_check_update)
1904 (*zclient->import_check_update) (command, zclient, length, vrf_id);
1905 break;
1906 case ZEBRA_BFD_DEST_REPLAY:
1907 if (zclient->bfd_dest_replay)
1908 (*zclient->bfd_dest_replay) (command, zclient, length, vrf_id);
1909 break;
1910 case ZEBRA_REDISTRIBUTE_IPV4_ADD:
1911 if (zclient->redistribute_route_ipv4_add)
1912 (*zclient->redistribute_route_ipv4_add) (command, zclient, length, vrf_id);
1913 break;
1914 case ZEBRA_REDISTRIBUTE_IPV4_DEL:
1915 if (zclient->redistribute_route_ipv4_del)
1916 (*zclient->redistribute_route_ipv4_del) (command, zclient, length, vrf_id);
1917 break;
1918 case ZEBRA_REDISTRIBUTE_IPV6_ADD:
1919 if (zclient->redistribute_route_ipv6_add)
1920 (*zclient->redistribute_route_ipv6_add) (command, zclient, length, vrf_id);
1921 break;
1922 case ZEBRA_REDISTRIBUTE_IPV6_DEL:
1923 if (zclient->redistribute_route_ipv6_del)
1924 (*zclient->redistribute_route_ipv6_del) (command, zclient, length, vrf_id);
1925 break;
1926 case ZEBRA_INTERFACE_LINK_PARAMS:
1927 if (zclient->interface_link_params)
1928 (*zclient->interface_link_params) (command, zclient, length);
1929 break;
1930 case ZEBRA_FEC_UPDATE:
1931 if (zclient_debug)
1932 zlog_debug("zclient rcvd fec update\n");
1933 if (zclient->fec_update)
1934 (*zclient->fec_update) (command, zclient, length);
1935 break;
1936 default:
1937 break;
1938 }
1939
1940 if (zclient->sock < 0)
1941 /* Connection was closed during packet processing. */
1942 return -1;
1943
1944 /* Register read thread. */
1945 stream_reset(zclient->ibuf);
1946 zclient_event (ZCLIENT_READ, zclient);
1947
1948 return 0;
1949 }
1950
1951 void
1952 zclient_redistribute (int command, struct zclient *zclient, afi_t afi, int type,
1953 u_short instance, vrf_id_t vrf_id)
1954 {
1955
1956 if (instance) {
1957 if (command == ZEBRA_REDISTRIBUTE_ADD)
1958 {
1959 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1960 return;
1961 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1962 }
1963 else
1964 {
1965 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1966 return;
1967 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1968 }
1969
1970 } else {
1971 if (command == ZEBRA_REDISTRIBUTE_ADD)
1972 {
1973 if (vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1974 return;
1975 vrf_bitmap_set (zclient->redist[afi][type], vrf_id);
1976 }
1977 else
1978 {
1979 if (!vrf_bitmap_check (zclient->redist[afi][type], vrf_id))
1980 return;
1981 vrf_bitmap_unset (zclient->redist[afi][type], vrf_id);
1982 }
1983 }
1984
1985 if (zclient->sock > 0)
1986 zebra_redistribute_send (command, zclient, afi, type, instance, vrf_id);
1987 }
1988
1989
1990 void
1991 zclient_redistribute_default (int command, struct zclient *zclient,
1992 vrf_id_t vrf_id)
1993 {
1994
1995 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
1996 {
1997 if (vrf_bitmap_check (zclient->default_information, vrf_id))
1998 return;
1999 vrf_bitmap_set (zclient->default_information, vrf_id);
2000 }
2001 else
2002 {
2003 if (!vrf_bitmap_check (zclient->default_information, vrf_id))
2004 return;
2005 vrf_bitmap_unset (zclient->default_information, vrf_id);
2006 }
2007
2008 if (zclient->sock > 0)
2009 zebra_message_send (zclient, command, vrf_id);
2010 }
2011
2012 static void
2013 zclient_event (enum event event, struct zclient *zclient)
2014 {
2015 switch (event)
2016 {
2017 case ZCLIENT_SCHEDULE:
2018 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2019 &zclient->t_connect);
2020 break;
2021 case ZCLIENT_CONNECT:
2022 if (zclient_debug)
2023 zlog_debug ("zclient connect failures: %d schedule interval is now %d",
2024 zclient->fail, zclient->fail < 3 ? 10 : 60);
2025 thread_add_timer(zclient->master, zclient_connect, zclient,
2026 zclient->fail < 3 ? 10 : 60, &zclient->t_connect);
2027 break;
2028 case ZCLIENT_READ:
2029 zclient->t_read = NULL;
2030 thread_add_read(zclient->master, zclient_read, zclient, zclient->sock,
2031 &zclient->t_read);
2032 break;
2033 }
2034 }
2035
2036 const char *zclient_serv_path_get()
2037 {
2038 return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
2039 }
2040
2041 void
2042 zclient_serv_path_set (char *path)
2043 {
2044 struct stat sb;
2045
2046 /* reset */
2047 zclient_serv_path = NULL;
2048
2049 /* test if `path' is socket. don't set it otherwise. */
2050 if (stat(path, &sb) == -1)
2051 {
2052 zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
2053 return;
2054 }
2055
2056 if ((sb.st_mode & S_IFMT) != S_IFSOCK)
2057 {
2058 zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
2059 return;
2060 }
2061
2062 /* it seems that path is unix socket */
2063 zclient_serv_path = path;
2064 }
2065