]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
Merge branch 'master' into docs-user
[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_putw(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_GETW(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 /*
1216 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1217 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1218 * then set/unset redist[type] in the client handle (a struct zserv) for the
1219 * sending client
1220 */
1221 int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1222 int type, u_short instance, vrf_id_t vrf_id)
1223 {
1224 struct stream *s;
1225
1226 s = zclient->obuf;
1227 stream_reset(s);
1228
1229 zclient_create_header(s, command, vrf_id);
1230 stream_putc(s, afi);
1231 stream_putc(s, type);
1232 stream_putw(s, instance);
1233
1234 stream_putw_at(s, 0, stream_get_endp(s));
1235
1236 return zclient_send_message(zclient);
1237 }
1238
1239 /* Get prefix in ZServ format; family should be filled in on prefix */
1240 static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
1241 {
1242 size_t plen = prefix_blen(p);
1243 u_char c;
1244 p->prefixlen = 0;
1245
1246 if (plen == 0)
1247 return;
1248
1249 stream_get(&p->u.prefix, s, plen);
1250 STREAM_GETC(s, c);
1251 p->prefixlen = MIN(plen * 8, c);
1252
1253 stream_failure:
1254 return;
1255 }
1256
1257 /* Router-id update from zebra daemon. */
1258 void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
1259 {
1260 /* Fetch interface address. */
1261 STREAM_GETC(s, rid->family);
1262
1263 zclient_stream_get_prefix(s, rid);
1264
1265 stream_failure:
1266 return;
1267 }
1268
1269 /* Interface addition from zebra daemon. */
1270 /*
1271 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1272 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1273 * 0 1 2 3
1274 * 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
1275 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1276 * | ifname |
1277 * | |
1278 * | |
1279 * | |
1280 * | |
1281 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1282 * | ifindex |
1283 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1284 * | status |
1285 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1286 * | if_flags |
1287 * | |
1288 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1289 * | metric |
1290 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1291 * | speed |
1292 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1293 * | ifmtu |
1294 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1295 * | ifmtu6 |
1296 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1297 * | bandwidth |
1298 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1299 * | Link Layer Type |
1300 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1301 * | Harware Address Length |
1302 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1303 * | Hardware Address if HW lenght different from 0 |
1304 * | ... max INTERFACE_HWADDR_MAX |
1305 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1306 * | Link_params? | Whether a link-params follows: 1 or 0.
1307 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1308 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1309 * | .... (struct if_link_params). |
1310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1311 */
1312
1313 static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1314 {
1315 struct vrf *vrf;
1316 char vrfname_tmp[VRF_NAMSIZ];
1317 struct vrf_data data;
1318
1319 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1320 /* Read interface name. */
1321 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1322
1323 /* Lookup/create vrf by vrf_id. */
1324 vrf = vrf_get(vrf_id, vrfname_tmp);
1325 vrf->data = data;
1326
1327 vrf_enable(vrf);
1328 }
1329
1330 static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1331 {
1332 struct vrf *vrf;
1333
1334 /* Lookup vrf by vrf_id. */
1335 vrf = vrf_lookup_by_id(vrf_id);
1336
1337 /*
1338 * If a routing protocol doesn't know about a
1339 * vrf that is about to be deleted. There is
1340 * no point in attempting to delete it.
1341 */
1342 if (!vrf)
1343 return;
1344
1345 vrf_delete(vrf);
1346 }
1347
1348 struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
1349 {
1350 struct interface *ifp;
1351 char ifname_tmp[INTERFACE_NAMSIZ];
1352
1353 /* Read interface name. */
1354 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1355
1356 /* Lookup/create interface by name. */
1357 ifp = if_get_by_name(ifname_tmp, vrf_id, 0);
1358
1359 zebra_interface_if_set_value(s, ifp);
1360
1361 return ifp;
1362 }
1363
1364 /*
1365 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1366 * from zebra server. The format of this message is the same as
1367 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1368 * comments for zebra_interface_add_read), except that no sockaddr_dl
1369 * is sent at the tail of the message.
1370 */
1371 struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
1372 {
1373 struct interface *ifp;
1374 char ifname_tmp[INTERFACE_NAMSIZ];
1375
1376 /* Read interface name. */
1377 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1378
1379 /* Lookup this by interface index. */
1380 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
1381 if (ifp == NULL) {
1382 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1383 ifname_tmp, vrf_id);
1384 return NULL;
1385 }
1386
1387 zebra_interface_if_set_value(s, ifp);
1388
1389 return ifp;
1390 }
1391
1392 static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
1393 {
1394
1395 if (iflp == NULL)
1396 return;
1397
1398 iflp->lp_status = stream_getl(s);
1399 iflp->te_metric = stream_getl(s);
1400 iflp->max_bw = stream_getf(s);
1401 iflp->max_rsv_bw = stream_getf(s);
1402 uint32_t bwclassnum = stream_getl(s);
1403 {
1404 unsigned int i;
1405 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1406 iflp->unrsv_bw[i] = stream_getf(s);
1407 if (i < bwclassnum)
1408 zlog_err(
1409 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1410 " - outdated library?",
1411 __func__, bwclassnum, MAX_CLASS_TYPE);
1412 }
1413 iflp->admin_grp = stream_getl(s);
1414 iflp->rmt_as = stream_getl(s);
1415 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1416
1417 iflp->av_delay = stream_getl(s);
1418 iflp->min_delay = stream_getl(s);
1419 iflp->max_delay = stream_getl(s);
1420 iflp->delay_var = stream_getl(s);
1421
1422 iflp->pkt_loss = stream_getf(s);
1423 iflp->res_bw = stream_getf(s);
1424 iflp->ava_bw = stream_getf(s);
1425 iflp->use_bw = stream_getf(s);
1426 }
1427
1428 struct interface *zebra_interface_link_params_read(struct stream *s)
1429 {
1430 struct if_link_params *iflp;
1431 ifindex_t ifindex;
1432
1433 assert(s);
1434
1435 ifindex = stream_getl(s);
1436
1437 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
1438
1439 if (ifp == NULL) {
1440 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__,
1441 ifindex);
1442 return NULL;
1443 }
1444
1445 if ((iflp = if_link_params_get(ifp)) == NULL)
1446 return NULL;
1447
1448 link_params_set_value(s, iflp);
1449
1450 return ifp;
1451 }
1452
1453 void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
1454 {
1455 u_char link_params_status = 0;
1456
1457 /* Read interface's index. */
1458 if_set_index(ifp, stream_getl(s));
1459 ifp->status = stream_getc(s);
1460
1461 /* Read interface's value. */
1462 ifp->flags = stream_getq(s);
1463 ifp->ptm_enable = stream_getc(s);
1464 ifp->ptm_status = stream_getc(s);
1465 ifp->metric = stream_getl(s);
1466 ifp->speed = stream_getl(s);
1467 ifp->mtu = stream_getl(s);
1468 ifp->mtu6 = stream_getl(s);
1469 ifp->bandwidth = stream_getl(s);
1470 ifp->ll_type = stream_getl(s);
1471 ifp->hw_addr_len = stream_getl(s);
1472 if (ifp->hw_addr_len)
1473 stream_get(ifp->hw_addr, s,
1474 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1475
1476 /* Read Traffic Engineering status */
1477 link_params_status = stream_getc(s);
1478 /* Then, Traffic Engineering parameters if any */
1479 if (link_params_status) {
1480 struct if_link_params *iflp = if_link_params_get(ifp);
1481 link_params_set_value(s, iflp);
1482 }
1483 }
1484
1485 size_t zebra_interface_link_params_write(struct stream *s,
1486 struct interface *ifp)
1487 {
1488 size_t w;
1489 struct if_link_params *iflp;
1490 int i;
1491
1492 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1493 return 0;
1494
1495 iflp = ifp->link_params;
1496 w = 0;
1497
1498 w += stream_putl(s, iflp->lp_status);
1499
1500 w += stream_putl(s, iflp->te_metric);
1501 w += stream_putf(s, iflp->max_bw);
1502 w += stream_putf(s, iflp->max_rsv_bw);
1503
1504 w += stream_putl(s, MAX_CLASS_TYPE);
1505 for (i = 0; i < MAX_CLASS_TYPE; i++)
1506 w += stream_putf(s, iflp->unrsv_bw[i]);
1507
1508 w += stream_putl(s, iflp->admin_grp);
1509 w += stream_putl(s, iflp->rmt_as);
1510 w += stream_put_in_addr(s, &iflp->rmt_ip);
1511
1512 w += stream_putl(s, iflp->av_delay);
1513 w += stream_putl(s, iflp->min_delay);
1514 w += stream_putl(s, iflp->max_delay);
1515 w += stream_putl(s, iflp->delay_var);
1516
1517 w += stream_putf(s, iflp->pkt_loss);
1518 w += stream_putf(s, iflp->res_bw);
1519 w += stream_putf(s, iflp->ava_bw);
1520 w += stream_putf(s, iflp->use_bw);
1521
1522 return w;
1523 }
1524
1525 /*
1526 * format of message for address additon is:
1527 * 0
1528 * 0 1 2 3 4 5 6 7
1529 * +-+-+-+-+-+-+-+-+
1530 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1531 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1532 * | |
1533 * + +
1534 * | ifindex |
1535 * + +
1536 * | |
1537 * + +
1538 * | |
1539 * +-+-+-+-+-+-+-+-+
1540 * | ifc_flags | flags for connected address
1541 * +-+-+-+-+-+-+-+-+
1542 * | addr_family |
1543 * +-+-+-+-+-+-+-+-+
1544 * | addr... |
1545 * : :
1546 * | |
1547 * +-+-+-+-+-+-+-+-+
1548 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1549 * +-+-+-+-+-+-+-+-+
1550 * | daddr.. |
1551 * : :
1552 * | |
1553 * +-+-+-+-+-+-+-+-+
1554 */
1555
1556 static int memconstant(const void *s, int c, size_t n)
1557 {
1558 const u_char *p = s;
1559
1560 while (n-- > 0)
1561 if (*p++ != c)
1562 return 0;
1563 return 1;
1564 }
1565
1566
1567 struct connected *zebra_interface_address_read(int type, struct stream *s,
1568 vrf_id_t vrf_id)
1569 {
1570 ifindex_t ifindex;
1571 struct interface *ifp;
1572 struct connected *ifc;
1573 struct prefix p, d, *dp;
1574 int plen;
1575 u_char ifc_flags;
1576
1577 memset(&p, 0, sizeof(p));
1578 memset(&d, 0, sizeof(d));
1579
1580 /* Get interface index. */
1581 ifindex = stream_getl(s);
1582
1583 /* Lookup index. */
1584 ifp = if_lookup_by_index(ifindex, vrf_id);
1585 if (ifp == NULL) {
1586 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1587 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1588 ifindex, vrf_id);
1589 return NULL;
1590 }
1591
1592 /* Fetch flag. */
1593 ifc_flags = stream_getc(s);
1594
1595 /* Fetch interface address. */
1596 d.family = p.family = stream_getc(s);
1597 plen = prefix_blen(&d);
1598
1599 zclient_stream_get_prefix(s, &p);
1600
1601 /* Fetch destination address. */
1602 stream_get(&d.u.prefix, s, plen);
1603
1604 /* N.B. NULL destination pointers are encoded as all zeroes */
1605 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1606
1607 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1608 ifc = connected_lookup_prefix_exact(ifp, &p);
1609 if (!ifc) {
1610 /* N.B. NULL destination pointers are encoded as all
1611 * zeroes */
1612 ifc = connected_add_by_prefix(ifp, &p, dp);
1613 }
1614 if (ifc) {
1615 ifc->flags = ifc_flags;
1616 if (ifc->destination)
1617 ifc->destination->prefixlen =
1618 ifc->address->prefixlen;
1619 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1620 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1621 * "peer" */
1622 char buf[PREFIX_STRLEN];
1623 zlog_warn(
1624 "warning: interface %s address %s "
1625 "with peer flag set, but no peer address!",
1626 ifp->name, prefix2str(ifc->address, buf,
1627 sizeof buf));
1628 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1629 }
1630 }
1631 } else {
1632 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1633 ifc = connected_delete_by_prefix(ifp, &p);
1634 }
1635
1636 return ifc;
1637 }
1638
1639 /*
1640 * format of message for neighbor connected address is:
1641 * 0
1642 * 0 1 2 3 4 5 6 7
1643 * +-+-+-+-+-+-+-+-+
1644 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1645 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1646 * | |
1647 * + +
1648 * | ifindex |
1649 * + +
1650 * | |
1651 * + +
1652 * | |
1653 * +-+-+-+-+-+-+-+-+
1654 * | addr_family |
1655 * +-+-+-+-+-+-+-+-+
1656 * | addr... |
1657 * : :
1658 * | |
1659 * +-+-+-+-+-+-+-+-+
1660 * | addr_len | len of addr.
1661 * +-+-+-+-+-+-+-+-+
1662 */
1663 struct nbr_connected *
1664 zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
1665 {
1666 unsigned int ifindex;
1667 struct interface *ifp;
1668 struct prefix p;
1669 struct nbr_connected *ifc;
1670
1671 /* Get interface index. */
1672 ifindex = stream_getl(s);
1673
1674 /* Lookup index. */
1675 ifp = if_lookup_by_index(ifindex, vrf_id);
1676 if (ifp == NULL) {
1677 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1678 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1679 : "DELETE",
1680 ifindex, vrf_id);
1681 return NULL;
1682 }
1683
1684 p.family = stream_getc(s);
1685 stream_get(&p.u.prefix, s, prefix_blen(&p));
1686 p.prefixlen = stream_getc(s);
1687
1688 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1689 /* Currently only supporting P2P links, so any new RA source
1690 address is
1691 considered as the replacement of the previously learnt
1692 Link-Local address. */
1693 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1694 ifc = nbr_connected_new();
1695 ifc->address = prefix_new();
1696 ifc->ifp = ifp;
1697 listnode_add(ifp->nbr_connected, ifc);
1698 }
1699
1700 prefix_copy(ifc->address, &p);
1701 } else {
1702 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1703
1704 ifc = nbr_connected_check(ifp, &p);
1705 if (ifc)
1706 listnode_delete(ifp->nbr_connected, ifc);
1707 }
1708
1709 return ifc;
1710 }
1711
1712 struct interface *zebra_interface_vrf_update_read(struct stream *s,
1713 vrf_id_t vrf_id,
1714 vrf_id_t *new_vrf_id)
1715 {
1716 unsigned int ifindex;
1717 struct interface *ifp;
1718 vrf_id_t new_id;
1719
1720 /* Get interface index. */
1721 ifindex = stream_getl(s);
1722
1723 /* Lookup interface. */
1724 ifp = if_lookup_by_index(ifindex, vrf_id);
1725 if (ifp == NULL) {
1726 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
1727 ifindex, vrf_id);
1728 return NULL;
1729 }
1730
1731 /* Fetch new VRF Id. */
1732 new_id = stream_getw(s);
1733
1734 *new_vrf_id = new_id;
1735 return ifp;
1736 }
1737
1738 /* filter unwanted messages until the expected one arrives */
1739 static int zclient_read_sync_response(struct zclient *zclient,
1740 u_int16_t expected_cmd)
1741 {
1742 struct stream *s;
1743 u_int16_t size = -1;
1744 u_char marker;
1745 u_char version;
1746 vrf_id_t vrf_id;
1747 u_int16_t cmd;
1748 fd_set readfds;
1749 int ret;
1750
1751 ret = 0;
1752 cmd = expected_cmd + 1;
1753 while (ret == 0 && cmd != expected_cmd) {
1754 s = zclient->ibuf;
1755 stream_reset(s);
1756
1757 /* wait until response arrives */
1758 FD_ZERO(&readfds);
1759 FD_SET(zclient->sock, &readfds);
1760 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
1761 if (!FD_ISSET(zclient->sock, &readfds))
1762 continue;
1763 /* read response */
1764 ret = zclient_read_header(s, zclient->sock, &size, &marker,
1765 &version, &vrf_id, &cmd);
1766 if (zclient_debug)
1767 zlog_debug("%s: Response (%d bytes) received", __func__,
1768 size);
1769 }
1770 if (ret != 0) {
1771 zlog_err("%s: Invalid Sync Message Reply", __func__);
1772 return -1;
1773 }
1774
1775 return 0;
1776 }
1777 /**
1778 * Connect to label manager in a syncronous way
1779 *
1780 * It first writes the request to zcient output buffer and then
1781 * immediately reads the answer from the input buffer.
1782 *
1783 * @param zclient Zclient used to connect to label manager (zebra)
1784 * @result Result of response
1785 */
1786 int lm_label_manager_connect(struct zclient *zclient)
1787 {
1788 int ret;
1789 struct stream *s;
1790 u_char result;
1791
1792 if (zclient_debug)
1793 zlog_debug("Connecting to Label Manager");
1794
1795 if (zclient->sock < 0)
1796 return -1;
1797
1798 /* send request */
1799 s = zclient->obuf;
1800 stream_reset(s);
1801 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
1802
1803 /* proto */
1804 stream_putc(s, zclient->redist_default);
1805 /* instance */
1806 stream_putw(s, zclient->instance);
1807
1808 /* Put length at the first point of the stream. */
1809 stream_putw_at(s, 0, stream_get_endp(s));
1810
1811 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1812 if (ret < 0) {
1813 zlog_err("%s: can't write to zclient->sock", __func__);
1814 close(zclient->sock);
1815 zclient->sock = -1;
1816 return -1;
1817 }
1818 if (ret == 0) {
1819 zlog_err("%s: zclient->sock connection closed", __func__);
1820 close(zclient->sock);
1821 zclient->sock = -1;
1822 return -1;
1823 }
1824 if (zclient_debug)
1825 zlog_debug("%s: Label manager connect request (%d bytes) sent",
1826 __func__, ret);
1827
1828 /* read response */
1829 if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT)
1830 != 0)
1831 return -1;
1832
1833 /* result */
1834 s = zclient->ibuf;
1835 result = stream_getc(s);
1836 if (zclient_debug)
1837 zlog_debug(
1838 "%s: Label Manager connect response received, result %u",
1839 __func__, result);
1840
1841 return (int)result;
1842 }
1843
1844 /**
1845 * Function to request a label chunk in a syncronous way
1846 *
1847 * It first writes the request to zlcient output buffer and then
1848 * immediately reads the answer from the input buffer.
1849 *
1850 * @param zclient Zclient used to connect to label manager (zebra)
1851 * @param keep Avoid garbage collection
1852 * @param chunk_size Amount of labels requested
1853 * @param start To write first assigned chunk label to
1854 * @param end To write last assigned chunk label to
1855 * @result 0 on success, -1 otherwise
1856 */
1857 int lm_get_label_chunk(struct zclient *zclient, u_char keep,
1858 uint32_t chunk_size, uint32_t *start, uint32_t *end)
1859 {
1860 int ret;
1861 struct stream *s;
1862 u_char response_keep;
1863
1864 if (zclient_debug)
1865 zlog_debug("Getting Label Chunk");
1866
1867 if (zclient->sock < 0)
1868 return -1;
1869
1870 /* send request */
1871 s = zclient->obuf;
1872 stream_reset(s);
1873 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
1874 /* keep */
1875 stream_putc(s, keep);
1876 /* chunk size */
1877 stream_putl(s, chunk_size);
1878 /* Put length at the first point of the stream. */
1879 stream_putw_at(s, 0, stream_get_endp(s));
1880
1881 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1882 if (ret < 0) {
1883 zlog_err("%s: can't write to zclient->sock", __func__);
1884 close(zclient->sock);
1885 zclient->sock = -1;
1886 return -1;
1887 }
1888 if (ret == 0) {
1889 zlog_err("%s: zclient->sock connection closed", __func__);
1890 close(zclient->sock);
1891 zclient->sock = -1;
1892 return -1;
1893 }
1894 if (zclient_debug)
1895 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__,
1896 ret);
1897
1898 /* read response */
1899 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
1900 return -1;
1901
1902 s = zclient->ibuf;
1903 /* keep */
1904 response_keep = stream_getc(s);
1905 /* start and end labels */
1906 *start = stream_getl(s);
1907 *end = stream_getl(s);
1908
1909 /* not owning this response */
1910 if (keep != response_keep) {
1911 zlog_err(
1912 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
1913 __func__, *start, *end, keep, response_keep);
1914 }
1915 /* sanity */
1916 if (*start > *end || *start < MPLS_MIN_UNRESERVED_LABEL
1917 || *end > MPLS_MAX_UNRESERVED_LABEL) {
1918 zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start,
1919 *end);
1920 return -1;
1921 }
1922
1923 if (zclient_debug)
1924 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end,
1925 response_keep);
1926
1927 return 0;
1928 }
1929
1930 /**
1931 * Function to release a label chunk
1932 *
1933 * @param zclient Zclient used to connect to label manager (zebra)
1934 * @param start First label of chunk
1935 * @param end Last label of chunk
1936 * @result 0 on success, -1 otherwise
1937 */
1938 int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
1939 uint32_t end)
1940 {
1941 int ret;
1942 struct stream *s;
1943
1944 if (zclient_debug)
1945 zlog_debug("Releasing Label Chunk");
1946
1947 if (zclient->sock < 0)
1948 return -1;
1949
1950 /* send request */
1951 s = zclient->obuf;
1952 stream_reset(s);
1953 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
1954
1955 /* start */
1956 stream_putl(s, start);
1957 /* end */
1958 stream_putl(s, end);
1959
1960 /* Put length at the first point of the stream. */
1961 stream_putw_at(s, 0, stream_get_endp(s));
1962
1963 ret = writen(zclient->sock, s->data, stream_get_endp(s));
1964 if (ret < 0) {
1965 zlog_err("%s: can't write to zclient->sock", __func__);
1966 close(zclient->sock);
1967 zclient->sock = -1;
1968 return -1;
1969 }
1970 if (ret == 0) {
1971 zlog_err("%s: zclient->sock connection closed", __func__);
1972 close(zclient->sock);
1973 zclient->sock = -1;
1974 return -1;
1975 }
1976
1977 return 0;
1978 }
1979
1980 int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
1981 {
1982 struct stream *s;
1983
1984 /* Reset stream. */
1985 s = zclient->obuf;
1986 stream_reset(s);
1987
1988 zclient_create_header(s, command, VRF_DEFAULT);
1989 stream_write(s, pw->ifname, IF_NAMESIZE);
1990 stream_putl(s, pw->ifindex);
1991
1992 /* Put type */
1993 stream_putl(s, pw->type);
1994
1995 /* Put nexthop */
1996 stream_putl(s, pw->af);
1997 switch (pw->af) {
1998 case AF_INET:
1999 stream_put_in_addr(s, &pw->nexthop.ipv4);
2000 break;
2001 case AF_INET6:
2002 stream_write(s, (u_char *)&pw->nexthop.ipv6, 16);
2003 break;
2004 default:
2005 zlog_err("%s: unknown af", __func__);
2006 return -1;
2007 }
2008
2009 /* Put labels */
2010 stream_putl(s, pw->local_label);
2011 stream_putl(s, pw->remote_label);
2012
2013 /* Put flags */
2014 stream_putc(s, pw->flags);
2015
2016 /* Protocol specific fields */
2017 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2018
2019 /* Put length at the first point of the stream. */
2020 stream_putw_at(s, 0, stream_get_endp(s));
2021
2022 return zclient_send_message(zclient);
2023 }
2024
2025 /*
2026 * Receive PW status update from Zebra and send it to LDE process.
2027 */
2028 void zebra_read_pw_status_update(int command, struct zclient *zclient,
2029 zebra_size_t length, vrf_id_t vrf_id,
2030 struct zapi_pw_status *pw)
2031 {
2032 struct stream *s;
2033
2034 memset(pw, 0, sizeof(struct zapi_pw_status));
2035 s = zclient->ibuf;
2036
2037 /* Get data. */
2038 stream_get(pw->ifname, s, IF_NAMESIZE);
2039 pw->ifindex = stream_getl(s);
2040 pw->status = stream_getl(s);
2041 }
2042
2043 /* Zebra client message read function. */
2044 static int zclient_read(struct thread *thread)
2045 {
2046 size_t already;
2047 uint16_t length, command;
2048 uint8_t marker, version;
2049 vrf_id_t vrf_id;
2050 struct zclient *zclient;
2051
2052 /* Get socket to zebra. */
2053 zclient = THREAD_ARG(thread);
2054 zclient->t_read = NULL;
2055
2056 /* Read zebra header (if we don't have it already). */
2057 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2058 ssize_t nbyte;
2059 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2060 ZEBRA_HEADER_SIZE - already))
2061 == 0)
2062 || (nbyte == -1)) {
2063 if (zclient_debug)
2064 zlog_debug(
2065 "zclient connection closed socket [%d].",
2066 zclient->sock);
2067 return zclient_failed(zclient);
2068 }
2069 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2070 /* Try again later. */
2071 zclient_event(ZCLIENT_READ, zclient);
2072 return 0;
2073 }
2074 already = ZEBRA_HEADER_SIZE;
2075 }
2076
2077 /* Reset to read from the beginning of the incoming packet. */
2078 stream_set_getp(zclient->ibuf, 0);
2079
2080 /* Fetch header values. */
2081 length = stream_getw(zclient->ibuf);
2082 marker = stream_getc(zclient->ibuf);
2083 version = stream_getc(zclient->ibuf);
2084 vrf_id = stream_getl(zclient->ibuf);
2085 command = stream_getw(zclient->ibuf);
2086
2087 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2088 zlog_err(
2089 "%s: socket %d version mismatch, marker %d, version %d",
2090 __func__, zclient->sock, marker, version);
2091 return zclient_failed(zclient);
2092 }
2093
2094 if (length < ZEBRA_HEADER_SIZE) {
2095 zlog_err("%s: socket %d message length %u is less than %d ",
2096 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2097 return zclient_failed(zclient);
2098 }
2099
2100 /* Length check. */
2101 if (length > STREAM_SIZE(zclient->ibuf)) {
2102 struct stream *ns;
2103 zlog_warn(
2104 "%s: message size %u exceeds buffer size %lu, expanding...",
2105 __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
2106 ns = stream_new(length);
2107 stream_copy(ns, zclient->ibuf);
2108 stream_free(zclient->ibuf);
2109 zclient->ibuf = ns;
2110 }
2111
2112 /* Read rest of zebra packet. */
2113 if (already < length) {
2114 ssize_t nbyte;
2115 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2116 length - already))
2117 == 0)
2118 || (nbyte == -1)) {
2119 if (zclient_debug)
2120 zlog_debug(
2121 "zclient connection closed socket [%d].",
2122 zclient->sock);
2123 return zclient_failed(zclient);
2124 }
2125 if (nbyte != (ssize_t)(length - already)) {
2126 /* Try again later. */
2127 zclient_event(ZCLIENT_READ, zclient);
2128 return 0;
2129 }
2130 }
2131
2132 length -= ZEBRA_HEADER_SIZE;
2133
2134 if (zclient_debug)
2135 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2136 (void *)zclient, command, vrf_id);
2137
2138 switch (command) {
2139 case ZEBRA_ROUTER_ID_UPDATE:
2140 if (zclient->router_id_update)
2141 (*zclient->router_id_update)(command, zclient, length,
2142 vrf_id);
2143 break;
2144 case ZEBRA_VRF_ADD:
2145 zclient_vrf_add(zclient, vrf_id);
2146 break;
2147 case ZEBRA_VRF_DELETE:
2148 zclient_vrf_delete(zclient, vrf_id);
2149 break;
2150 case ZEBRA_INTERFACE_ADD:
2151 if (zclient->interface_add)
2152 (*zclient->interface_add)(command, zclient, length,
2153 vrf_id);
2154 break;
2155 case ZEBRA_INTERFACE_DELETE:
2156 if (zclient->interface_delete)
2157 (*zclient->interface_delete)(command, zclient, length,
2158 vrf_id);
2159 break;
2160 case ZEBRA_INTERFACE_ADDRESS_ADD:
2161 if (zclient->interface_address_add)
2162 (*zclient->interface_address_add)(command, zclient,
2163 length, vrf_id);
2164 break;
2165 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2166 if (zclient->interface_address_delete)
2167 (*zclient->interface_address_delete)(command, zclient,
2168 length, vrf_id);
2169 break;
2170 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2171 if (zclient->interface_bfd_dest_update)
2172 (*zclient->interface_bfd_dest_update)(command, zclient,
2173 length, vrf_id);
2174 break;
2175 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2176 if (zclient->interface_nbr_address_add)
2177 (*zclient->interface_nbr_address_add)(command, zclient,
2178 length, vrf_id);
2179 break;
2180 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2181 if (zclient->interface_nbr_address_delete)
2182 (*zclient->interface_nbr_address_delete)(
2183 command, zclient, length, vrf_id);
2184 break;
2185 case ZEBRA_INTERFACE_UP:
2186 if (zclient->interface_up)
2187 (*zclient->interface_up)(command, zclient, length,
2188 vrf_id);
2189 break;
2190 case ZEBRA_INTERFACE_DOWN:
2191 if (zclient->interface_down)
2192 (*zclient->interface_down)(command, zclient, length,
2193 vrf_id);
2194 break;
2195 case ZEBRA_INTERFACE_VRF_UPDATE:
2196 if (zclient->interface_vrf_update)
2197 (*zclient->interface_vrf_update)(command, zclient,
2198 length, vrf_id);
2199 break;
2200 case ZEBRA_NEXTHOP_UPDATE:
2201 if (zclient_debug)
2202 zlog_debug("zclient rcvd nexthop update\n");
2203 if (zclient->nexthop_update)
2204 (*zclient->nexthop_update)(command, zclient, length,
2205 vrf_id);
2206 break;
2207 case ZEBRA_IMPORT_CHECK_UPDATE:
2208 if (zclient_debug)
2209 zlog_debug("zclient rcvd import check update\n");
2210 if (zclient->import_check_update)
2211 (*zclient->import_check_update)(command, zclient,
2212 length, vrf_id);
2213 break;
2214 case ZEBRA_BFD_DEST_REPLAY:
2215 if (zclient->bfd_dest_replay)
2216 (*zclient->bfd_dest_replay)(command, zclient, length,
2217 vrf_id);
2218 break;
2219 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2220 if (zclient->redistribute_route_add)
2221 (*zclient->redistribute_route_add)(command, zclient,
2222 length, vrf_id);
2223 break;
2224 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2225 if (zclient->redistribute_route_del)
2226 (*zclient->redistribute_route_del)(command, zclient,
2227 length, vrf_id);
2228 break;
2229 case ZEBRA_INTERFACE_LINK_PARAMS:
2230 if (zclient->interface_link_params)
2231 (*zclient->interface_link_params)(command, zclient,
2232 length);
2233 break;
2234 case ZEBRA_FEC_UPDATE:
2235 if (zclient_debug)
2236 zlog_debug("zclient rcvd fec update\n");
2237 if (zclient->fec_update)
2238 (*zclient->fec_update)(command, zclient, length);
2239 break;
2240 case ZEBRA_VNI_ADD:
2241 if (zclient->local_vni_add)
2242 (*zclient->local_vni_add)(command, zclient, length,
2243 vrf_id);
2244 break;
2245 case ZEBRA_VNI_DEL:
2246 if (zclient->local_vni_del)
2247 (*zclient->local_vni_del)(command, zclient, length,
2248 vrf_id);
2249 break;
2250 case ZEBRA_L3VNI_ADD:
2251 if (zclient->local_l3vni_add)
2252 (*zclient->local_l3vni_add)(command, zclient, length,
2253 vrf_id);
2254 break;
2255 case ZEBRA_L3VNI_DEL:
2256 if (zclient->local_l3vni_del)
2257 (*zclient->local_l3vni_del)(command, zclient, length,
2258 vrf_id);
2259 break;
2260 case ZEBRA_MACIP_ADD:
2261 if (zclient->local_macip_add)
2262 (*zclient->local_macip_add)(command, zclient, length,
2263 vrf_id);
2264 break;
2265 case ZEBRA_MACIP_DEL:
2266 if (zclient->local_macip_del)
2267 (*zclient->local_macip_del)(command, zclient, length,
2268 vrf_id);
2269 break;
2270 case ZEBRA_PW_STATUS_UPDATE:
2271 if (zclient->pw_status_update)
2272 (*zclient->pw_status_update)(command, zclient, length,
2273 vrf_id);
2274 break;
2275 case ZEBRA_ROUTE_NOTIFY_OWNER:
2276 if (zclient->notify_owner)
2277 (*zclient->notify_owner)(command, zclient,
2278 length, vrf_id);
2279 break;
2280 default:
2281 break;
2282 }
2283
2284 if (zclient->sock < 0)
2285 /* Connection was closed during packet processing. */
2286 return -1;
2287
2288 /* Register read thread. */
2289 stream_reset(zclient->ibuf);
2290 zclient_event(ZCLIENT_READ, zclient);
2291
2292 return 0;
2293 }
2294
2295 void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2296 int type, u_short instance, vrf_id_t vrf_id)
2297 {
2298
2299 if (instance) {
2300 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2301 if (redist_check_instance(
2302 &zclient->mi_redist[afi][type], instance))
2303 return;
2304 redist_add_instance(&zclient->mi_redist[afi][type],
2305 instance);
2306 } else {
2307 if (!redist_check_instance(
2308 &zclient->mi_redist[afi][type], instance))
2309 return;
2310 redist_del_instance(&zclient->mi_redist[afi][type],
2311 instance);
2312 }
2313
2314 } else {
2315 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2316 if (vrf_bitmap_check(zclient->redist[afi][type],
2317 vrf_id))
2318 return;
2319 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2320 } else {
2321 if (!vrf_bitmap_check(zclient->redist[afi][type],
2322 vrf_id))
2323 return;
2324 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2325 }
2326 }
2327
2328 if (zclient->sock > 0)
2329 zebra_redistribute_send(command, zclient, afi, type, instance,
2330 vrf_id);
2331 }
2332
2333
2334 void zclient_redistribute_default(int command, struct zclient *zclient,
2335 vrf_id_t vrf_id)
2336 {
2337
2338 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2339 if (vrf_bitmap_check(zclient->default_information, vrf_id))
2340 return;
2341 vrf_bitmap_set(zclient->default_information, vrf_id);
2342 } else {
2343 if (!vrf_bitmap_check(zclient->default_information, vrf_id))
2344 return;
2345 vrf_bitmap_unset(zclient->default_information, vrf_id);
2346 }
2347
2348 if (zclient->sock > 0)
2349 zebra_message_send(zclient, command, vrf_id);
2350 }
2351
2352 static void zclient_event(enum event event, struct zclient *zclient)
2353 {
2354 switch (event) {
2355 case ZCLIENT_SCHEDULE:
2356 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2357 &zclient->t_connect);
2358 break;
2359 case ZCLIENT_CONNECT:
2360 if (zclient_debug)
2361 zlog_debug(
2362 "zclient connect failures: %d schedule interval is now %d",
2363 zclient->fail, zclient->fail < 3 ? 10 : 60);
2364 thread_add_timer(zclient->master, zclient_connect, zclient,
2365 zclient->fail < 3 ? 10 : 60,
2366 &zclient->t_connect);
2367 break;
2368 case ZCLIENT_READ:
2369 zclient->t_read = NULL;
2370 thread_add_read(zclient->master, zclient_read, zclient,
2371 zclient->sock, &zclient->t_read);
2372 break;
2373 }
2374 }
2375
2376 void zclient_interface_set_master(struct zclient *client,
2377 struct interface *master,
2378 struct interface *slave)
2379 {
2380 struct stream *s;
2381
2382 s = client->obuf;
2383 stream_reset(s);
2384
2385 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2386
2387 stream_putl(s, master->vrf_id);
2388 stream_putl(s, master->ifindex);
2389 stream_putl(s, slave->vrf_id);
2390 stream_putl(s, slave->ifindex);
2391
2392 stream_putw_at(s, 0, stream_get_endp(s));
2393 zclient_send_message(client);
2394 }