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