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