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