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