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