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