]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.c
Merge pull request #2211 from chiragshah6/ospf_vrf_dev
[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
979 /* Put prefix information. */
980 stream_putc(s, api->prefix.family);
981 psize = PSIZE(api->prefix.prefixlen);
982 stream_putc(s, api->prefix.prefixlen);
983 stream_write(s, (uint8_t *)&api->prefix.u.prefix, psize);
984
985 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
986 psize = PSIZE(api->src_prefix.prefixlen);
987 stream_putc(s, api->src_prefix.prefixlen);
988 stream_write(s, (uint8_t *)&api->src_prefix.prefix, psize);
989 }
990
991 /* Nexthops. */
992 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
993 /* limit the number of nexthops if necessary */
994 if (api->nexthop_num > MULTIPATH_NUM) {
995 char buf[PREFIX2STR_BUFFER];
996
997 prefix2str(&api->prefix, buf, sizeof(buf));
998 zlog_warn(
999 "%s: prefix %s: can't encode %u nexthops "
1000 "(maximum is %u)",
1001 __func__, buf, api->nexthop_num, MULTIPATH_NUM);
1002 return -1;
1003 }
1004
1005 stream_putw(s, api->nexthop_num);
1006
1007 for (i = 0; i < api->nexthop_num; i++) {
1008 api_nh = &api->nexthops[i];
1009
1010 stream_putl(s, api_nh->vrf_id);
1011 stream_putc(s, api_nh->type);
1012 switch (api_nh->type) {
1013 case NEXTHOP_TYPE_BLACKHOLE:
1014 stream_putc(s, api_nh->bh_type);
1015 break;
1016 case NEXTHOP_TYPE_IPV4:
1017 stream_put_in_addr(s, &api_nh->gate.ipv4);
1018 break;
1019 case NEXTHOP_TYPE_IPV4_IFINDEX:
1020 stream_put_in_addr(s, &api_nh->gate.ipv4);
1021 stream_putl(s, api_nh->ifindex);
1022 break;
1023 case NEXTHOP_TYPE_IFINDEX:
1024 stream_putl(s, api_nh->ifindex);
1025 break;
1026 case NEXTHOP_TYPE_IPV6:
1027 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
1028 16);
1029 break;
1030 case NEXTHOP_TYPE_IPV6_IFINDEX:
1031 stream_write(s, (uint8_t *)&api_nh->gate.ipv6,
1032 16);
1033 stream_putl(s, api_nh->ifindex);
1034 break;
1035 default:
1036 zlog_warn(
1037 "%s: Specified Nexthop type %d does not exist",
1038 __PRETTY_FUNCTION__, api_nh->type);
1039 return -1;
1040 }
1041
1042 /* MPLS labels for BGP-LU or Segment Routing */
1043 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
1044 if (api_nh->label_num > MPLS_MAX_LABELS) {
1045 char buf[PREFIX2STR_BUFFER];
1046 prefix2str(&api->prefix, buf,
1047 sizeof(buf));
1048 zlog_err(
1049 "%s: prefix %s: can't encode "
1050 "%u labels (maximum is %u)",
1051 __func__, buf,
1052 api_nh->label_num,
1053 MPLS_MAX_LABELS);
1054 return -1;
1055 }
1056
1057 stream_putc(s, api_nh->label_num);
1058 stream_put(s, &api_nh->labels[0],
1059 api_nh->label_num
1060 * sizeof(mpls_label_t));
1061 }
1062
1063 /* Router MAC for EVPN routes. */
1064 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
1065 stream_put(s, &(api_nh->rmac),
1066 sizeof(struct ethaddr));
1067 }
1068 }
1069
1070 /* Attributes. */
1071 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1072 stream_putc(s, api->distance);
1073 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1074 stream_putl(s, api->metric);
1075 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1076 stream_putl(s, api->tag);
1077 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1078 stream_putl(s, api->mtu);
1079 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1080 stream_putl(s, api->tableid);
1081
1082 /* Put length at the first point of the stream. */
1083 stream_putw_at(s, 0, stream_get_endp(s));
1084
1085 return 0;
1086 }
1087
1088 int zapi_route_decode(struct stream *s, struct zapi_route *api)
1089 {
1090 struct zapi_nexthop *api_nh;
1091 int i;
1092
1093 memset(api, 0, sizeof(*api));
1094
1095 /* Type, flags, message. */
1096 STREAM_GETC(s, api->type);
1097 if (api->type > ZEBRA_ROUTE_MAX) {
1098 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1099 __PRETTY_FUNCTION__, api->type);
1100 return -1;
1101 }
1102
1103 STREAM_GETW(s, api->instance);
1104 STREAM_GETL(s, api->flags);
1105 STREAM_GETC(s, api->message);
1106 STREAM_GETC(s, api->safi);
1107
1108 /* Prefix. */
1109 STREAM_GETC(s, api->prefix.family);
1110 STREAM_GETC(s, api->prefix.prefixlen);
1111 switch (api->prefix.family) {
1112 case AF_INET:
1113 if (api->prefix.prefixlen > IPV4_MAX_PREFIXLEN) {
1114 zlog_warn(
1115 "%s: V4 prefixlen is %d which should not be more than 32",
1116 __PRETTY_FUNCTION__, api->prefix.prefixlen);
1117 return -1;
1118 }
1119 break;
1120 case AF_INET6:
1121 if (api->prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
1122 zlog_warn(
1123 "%s: v6 prefixlen is %d which should not be more than 128",
1124 __PRETTY_FUNCTION__, api->prefix.prefixlen);
1125 return -1;
1126 }
1127 break;
1128 default:
1129 zlog_warn("%s: Specified family %d is not v4 or v6",
1130 __PRETTY_FUNCTION__, api->prefix.family);
1131 return -1;
1132 }
1133 STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen));
1134
1135 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) {
1136 api->src_prefix.family = AF_INET6;
1137 STREAM_GETC(s, api->src_prefix.prefixlen);
1138 if (api->src_prefix.prefixlen > IPV6_MAX_PREFIXLEN) {
1139 zlog_warn(
1140 "%s: SRC Prefix prefixlen received: %d is too large",
1141 __PRETTY_FUNCTION__, api->src_prefix.prefixlen);
1142 return -1;
1143 }
1144 STREAM_GET(&api->src_prefix.prefix, s,
1145 PSIZE(api->src_prefix.prefixlen));
1146
1147 if (api->prefix.family != AF_INET6
1148 || api->src_prefix.prefixlen == 0) {
1149 zlog_warn(
1150 "%s: SRC prefix specified in some manner that makes no sense",
1151 __PRETTY_FUNCTION__);
1152 return -1;
1153 }
1154 }
1155
1156 /* Nexthops. */
1157 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) {
1158 STREAM_GETW(s, api->nexthop_num);
1159 if (api->nexthop_num > MULTIPATH_NUM) {
1160 zlog_warn("%s: invalid number of nexthops (%u)",
1161 __func__, api->nexthop_num);
1162 return -1;
1163 }
1164
1165 for (i = 0; i < api->nexthop_num; i++) {
1166 api_nh = &api->nexthops[i];
1167
1168 STREAM_GETL(s, api_nh->vrf_id);
1169 STREAM_GETC(s, api_nh->type);
1170 switch (api_nh->type) {
1171 case NEXTHOP_TYPE_BLACKHOLE:
1172 STREAM_GETC(s, api_nh->bh_type);
1173 break;
1174 case NEXTHOP_TYPE_IPV4:
1175 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
1176 IPV4_MAX_BYTELEN);
1177 break;
1178 case NEXTHOP_TYPE_IPV4_IFINDEX:
1179 STREAM_GET(&api_nh->gate.ipv4.s_addr, s,
1180 IPV4_MAX_BYTELEN);
1181 STREAM_GETL(s, api_nh->ifindex);
1182 break;
1183 case NEXTHOP_TYPE_IFINDEX:
1184 STREAM_GETL(s, api_nh->ifindex);
1185 break;
1186 case NEXTHOP_TYPE_IPV6:
1187 STREAM_GET(&api_nh->gate.ipv6, s, 16);
1188 break;
1189 case NEXTHOP_TYPE_IPV6_IFINDEX:
1190 STREAM_GET(&api_nh->gate.ipv6, s, 16);
1191 STREAM_GETL(s, api_nh->ifindex);
1192 break;
1193 default:
1194 zlog_warn(
1195 "%s: Specified nexthop type %d does not exist",
1196 __PRETTY_FUNCTION__, api_nh->type);
1197 return -1;
1198 }
1199
1200 /* MPLS labels for BGP-LU or Segment Routing */
1201 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) {
1202 STREAM_GETC(s, api_nh->label_num);
1203
1204 if (api_nh->label_num > MPLS_MAX_LABELS) {
1205 zlog_warn(
1206 "%s: invalid number of MPLS "
1207 "labels (%u)",
1208 __func__, api_nh->label_num);
1209 return -1;
1210 }
1211
1212 STREAM_GET(&api_nh->labels[0], s,
1213 api_nh->label_num
1214 * sizeof(mpls_label_t));
1215 }
1216
1217 /* Router MAC for EVPN routes. */
1218 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE))
1219 stream_get(&(api_nh->rmac), s,
1220 sizeof(struct ethaddr));
1221 }
1222 }
1223
1224 /* Attributes. */
1225 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE))
1226 STREAM_GETC(s, api->distance);
1227 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC))
1228 STREAM_GETL(s, api->metric);
1229 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG))
1230 STREAM_GETL(s, api->tag);
1231 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU))
1232 STREAM_GETL(s, api->mtu);
1233 if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID))
1234 STREAM_GETL(s, api->tableid);
1235
1236 stream_failure:
1237 return 0;
1238 }
1239
1240 static void zapi_encode_prefix(struct stream *s,
1241 struct prefix *p,
1242 uint8_t family)
1243 {
1244 struct prefix any;
1245
1246 if (!p) {
1247 memset(&any, 0, sizeof(any));
1248 any.family = family;
1249 p = &any;
1250 }
1251
1252 stream_putc(s, p->family);
1253 stream_putc(s, p->prefixlen);
1254 stream_put(s, &p->u.prefix, prefix_blen(p));
1255 }
1256
1257 int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s,
1258 struct pbr_rule *zrule)
1259 {
1260 stream_reset(s);
1261 zclient_create_header(s, cmd, zrule->vrf_id);
1262
1263 /*
1264 * We are sending one item at a time at the moment
1265 */
1266 stream_putl(s, 1);
1267
1268 stream_putl(s, zrule->seq);
1269 stream_putl(s, zrule->priority);
1270 stream_putl(s, zrule->unique);
1271
1272 zapi_encode_prefix(s, &(zrule->filter.src_ip),
1273 zrule->filter.src_ip.family);
1274 stream_putw(s, zrule->filter.src_port); /* src port */
1275 zapi_encode_prefix(s, &(zrule->filter.dst_ip),
1276 zrule->filter.src_ip.family);
1277 stream_putw(s, zrule->filter.dst_port); /* dst port */
1278 stream_putw(s, zrule->filter.fwmark); /* fwmark */
1279
1280 stream_putl(s, zrule->action.table);
1281 stream_putl(s, zrule->ifindex);
1282
1283 /* Put length at the first point of the stream. */
1284 stream_putw_at(s, 0, stream_get_endp(s));
1285
1286 return 0;
1287 }
1288
1289 bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
1290 uint32_t *tableid,
1291 enum zapi_route_notify_owner *note)
1292 {
1293 uint32_t t;
1294
1295 STREAM_GET(note, s, sizeof(*note));
1296
1297 STREAM_GETC(s, p->family);
1298 STREAM_GETC(s, p->prefixlen);
1299 STREAM_GET(&p->u.prefix, s, prefix_blen(p));
1300 STREAM_GETL(s, t);
1301
1302 *tableid = t;
1303
1304 return true;
1305
1306 stream_failure:
1307 return false;
1308 }
1309
1310 bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
1311 uint32_t *priority, uint32_t *unique,
1312 ifindex_t *ifindex,
1313 enum zapi_rule_notify_owner *note)
1314 {
1315 uint32_t prio, seq, uni;
1316 ifindex_t ifi;
1317
1318 STREAM_GET(note, s, sizeof(*note));
1319
1320 STREAM_GETL(s, seq);
1321 STREAM_GETL(s, prio);
1322 STREAM_GETL(s, uni);
1323 STREAM_GETL(s, ifi);
1324
1325 if (zclient_debug)
1326 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__,
1327 seq, prio, uni, ifi);
1328 *seqno = seq;
1329 *priority = prio;
1330 *unique = uni;
1331 *ifindex = ifi;
1332
1333 return true;
1334
1335 stream_failure:
1336 return false;
1337 }
1338
1339 bool zapi_ipset_notify_decode(struct stream *s,
1340 uint32_t *unique,
1341 enum zapi_ipset_notify_owner *note)
1342 {
1343 uint32_t uni;
1344
1345 STREAM_GET(note, s, sizeof(*note));
1346
1347 STREAM_GETL(s, uni);
1348
1349 if (zclient_debug)
1350 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1351 *unique = uni;
1352
1353 return true;
1354
1355 stream_failure:
1356 return false;
1357 }
1358
1359 bool zapi_ipset_entry_notify_decode(struct stream *s,
1360 uint32_t *unique,
1361 char *ipset_name,
1362 enum zapi_ipset_entry_notify_owner *note)
1363 {
1364 uint32_t uni;
1365
1366 STREAM_GET(note, s, sizeof(*note));
1367
1368 STREAM_GETL(s, uni);
1369
1370 STREAM_GET(ipset_name, s,
1371 ZEBRA_IPSET_NAME_SIZE);
1372
1373 if (zclient_debug)
1374 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1375 *unique = uni;
1376
1377 return true;
1378
1379 stream_failure:
1380 return false;
1381 }
1382
1383 bool zapi_iptable_notify_decode(struct stream *s,
1384 uint32_t *unique,
1385 enum zapi_iptable_notify_owner *note)
1386 {
1387 uint32_t uni;
1388
1389 STREAM_GET(note, s, sizeof(*note));
1390
1391 STREAM_GETL(s, uni);
1392
1393 if (zclient_debug)
1394 zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni);
1395 *unique = uni;
1396
1397 return true;
1398
1399 stream_failure:
1400 return false;
1401 }
1402
1403 struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh)
1404 {
1405 struct nexthop *n = nexthop_new();
1406
1407 n->type = znh->type;
1408 n->vrf_id = znh->vrf_id;
1409 n->ifindex = znh->ifindex;
1410 n->gate = znh->gate;
1411
1412 /*
1413 * This function currently handles labels
1414 */
1415 if (znh->label_num) {
1416 nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num,
1417 znh->labels);
1418 }
1419
1420 return n;
1421 }
1422
1423 bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr)
1424 {
1425 uint32_t i;
1426
1427 memset(nhr, 0, sizeof(*nhr));
1428
1429 STREAM_GETW(s, nhr->prefix.family);
1430 STREAM_GETC(s, nhr->prefix.prefixlen);
1431 switch (nhr->prefix.family) {
1432 case AF_INET:
1433 STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1434 break;
1435 case AF_INET6:
1436 STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN);
1437 break;
1438 default:
1439 break;
1440 }
1441
1442 STREAM_GETC(s, nhr->type);
1443 STREAM_GETW(s, nhr->instance);
1444 STREAM_GETC(s, nhr->distance);
1445 STREAM_GETL(s, nhr->metric);
1446 STREAM_GETC(s, nhr->nexthop_num);
1447
1448 for (i = 0; i < nhr->nexthop_num; i++) {
1449 STREAM_GETC(s, nhr->nexthops[i].type);
1450 switch (nhr->nexthops[i].type) {
1451 case NEXTHOP_TYPE_IPV4:
1452 case NEXTHOP_TYPE_IPV4_IFINDEX:
1453 STREAM_GET(&nhr->nexthops[i].gate.ipv4.s_addr, s,
1454 IPV4_MAX_BYTELEN);
1455 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1456 break;
1457 case NEXTHOP_TYPE_IFINDEX:
1458 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1459 break;
1460 case NEXTHOP_TYPE_IPV6:
1461 case NEXTHOP_TYPE_IPV6_IFINDEX:
1462 STREAM_GET(&nhr->nexthops[i].gate.ipv6, s,
1463 IPV6_MAX_BYTELEN);
1464 STREAM_GETL(s, nhr->nexthops[i].ifindex);
1465 break;
1466 case NEXTHOP_TYPE_BLACKHOLE:
1467 break;
1468 }
1469 STREAM_GETC(s, nhr->nexthops[i].label_num);
1470 if (nhr->nexthops[i].label_num > MPLS_MAX_LABELS) {
1471 zlog_warn("%s: invalid number of MPLS labels (%u)",
1472 __func__, nhr->nexthops[i].label_num);
1473 return false;
1474 }
1475 if (nhr->nexthops[i].label_num)
1476 STREAM_GET(&nhr->nexthops[i].labels[0], s,
1477 nhr->nexthops[i].label_num
1478 * sizeof(mpls_label_t));
1479 }
1480
1481 return true;
1482 stream_failure:
1483 return false;
1484 }
1485
1486 /*
1487 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1488 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1489 * then set/unset redist[type] in the client handle (a struct zserv) for the
1490 * sending client
1491 */
1492 int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi,
1493 int type, unsigned short instance, vrf_id_t vrf_id)
1494 {
1495 struct stream *s;
1496
1497 s = zclient->obuf;
1498 stream_reset(s);
1499
1500 zclient_create_header(s, command, vrf_id);
1501 stream_putc(s, afi);
1502 stream_putc(s, type);
1503 stream_putw(s, instance);
1504
1505 stream_putw_at(s, 0, stream_get_endp(s));
1506
1507 return zclient_send_message(zclient);
1508 }
1509
1510 /* Get prefix in ZServ format; family should be filled in on prefix */
1511 static void zclient_stream_get_prefix(struct stream *s, struct prefix *p)
1512 {
1513 size_t plen = prefix_blen(p);
1514 uint8_t c;
1515 p->prefixlen = 0;
1516
1517 if (plen == 0)
1518 return;
1519
1520 stream_get(&p->u.prefix, s, plen);
1521 STREAM_GETC(s, c);
1522 p->prefixlen = MIN(plen * 8, c);
1523
1524 stream_failure:
1525 return;
1526 }
1527
1528 /* Router-id update from zebra daemon. */
1529 void zebra_router_id_update_read(struct stream *s, struct prefix *rid)
1530 {
1531 /* Fetch interface address. */
1532 STREAM_GETC(s, rid->family);
1533
1534 zclient_stream_get_prefix(s, rid);
1535
1536 stream_failure:
1537 return;
1538 }
1539
1540 /* Interface addition from zebra daemon. */
1541 /*
1542 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1543 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1544 * 0 1 2 3
1545 * 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
1546 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1547 * | ifname |
1548 * | |
1549 * | |
1550 * | |
1551 * | |
1552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1553 * | ifindex |
1554 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1555 * | status |
1556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1557 * | if_flags |
1558 * | |
1559 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1560 * | metric |
1561 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1562 * | speed |
1563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1564 * | ifmtu |
1565 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1566 * | ifmtu6 |
1567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1568 * | bandwidth |
1569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1570 * | Link Layer Type |
1571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1572 * | Harware Address Length |
1573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1574 * | Hardware Address if HW lenght different from 0 |
1575 * | ... max INTERFACE_HWADDR_MAX |
1576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1577 * | Link_params? | Whether a link-params follows: 1 or 0.
1578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1579 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1580 * | .... (struct if_link_params). |
1581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1582 */
1583
1584 static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id)
1585 {
1586 struct vrf *vrf;
1587 char vrfname_tmp[VRF_NAMSIZ];
1588 struct vrf_data data;
1589
1590 stream_get(&data, zclient->ibuf, sizeof(struct vrf_data));
1591 /* Read interface name. */
1592 stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ);
1593
1594 /* Lookup/create vrf by vrf_id. */
1595 vrf = vrf_get(vrf_id, vrfname_tmp);
1596 vrf->data.l.table_id = data.l.table_id;
1597 memcpy(vrf->data.l.netns_name, data.l.netns_name, NS_NAMSIZ);
1598 vrf_enable(vrf);
1599 }
1600
1601 static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id)
1602 {
1603 struct vrf *vrf;
1604
1605 /* Lookup vrf by vrf_id. */
1606 vrf = vrf_lookup_by_id(vrf_id);
1607
1608 /*
1609 * If a routing protocol doesn't know about a
1610 * vrf that is about to be deleted. There is
1611 * no point in attempting to delete it.
1612 */
1613 if (!vrf)
1614 return;
1615
1616 vrf_delete(vrf);
1617 }
1618
1619 struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
1620 {
1621 struct interface *ifp;
1622 char ifname_tmp[INTERFACE_NAMSIZ];
1623
1624 /* Read interface name. */
1625 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1626
1627 /* Lookup/create interface by name. */
1628 ifp = if_get_by_name(ifname_tmp, vrf_id, 0);
1629
1630 zebra_interface_if_set_value(s, ifp);
1631
1632 return ifp;
1633 }
1634
1635 /*
1636 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1637 * from zebra server. The format of this message is the same as
1638 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1639 * comments for zebra_interface_add_read), except that no sockaddr_dl
1640 * is sent at the tail of the message.
1641 */
1642 struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
1643 {
1644 struct interface *ifp;
1645 char ifname_tmp[INTERFACE_NAMSIZ];
1646
1647 /* Read interface name. */
1648 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
1649
1650 /* Lookup this by interface index. */
1651 ifp = if_lookup_by_name(ifname_tmp, vrf_id);
1652 if (ifp == NULL) {
1653 zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
1654 ifname_tmp, vrf_id);
1655 return NULL;
1656 }
1657
1658 zebra_interface_if_set_value(s, ifp);
1659
1660 return ifp;
1661 }
1662
1663 static void link_params_set_value(struct stream *s, struct if_link_params *iflp)
1664 {
1665
1666 if (iflp == NULL)
1667 return;
1668
1669 iflp->lp_status = stream_getl(s);
1670 iflp->te_metric = stream_getl(s);
1671 iflp->max_bw = stream_getf(s);
1672 iflp->max_rsv_bw = stream_getf(s);
1673 uint32_t bwclassnum = stream_getl(s);
1674 {
1675 unsigned int i;
1676 for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
1677 iflp->unrsv_bw[i] = stream_getf(s);
1678 if (i < bwclassnum)
1679 zlog_err(
1680 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1681 " - outdated library?",
1682 __func__, bwclassnum, MAX_CLASS_TYPE);
1683 }
1684 iflp->admin_grp = stream_getl(s);
1685 iflp->rmt_as = stream_getl(s);
1686 iflp->rmt_ip.s_addr = stream_get_ipv4(s);
1687
1688 iflp->av_delay = stream_getl(s);
1689 iflp->min_delay = stream_getl(s);
1690 iflp->max_delay = stream_getl(s);
1691 iflp->delay_var = stream_getl(s);
1692
1693 iflp->pkt_loss = stream_getf(s);
1694 iflp->res_bw = stream_getf(s);
1695 iflp->ava_bw = stream_getf(s);
1696 iflp->use_bw = stream_getf(s);
1697 }
1698
1699 struct interface *zebra_interface_link_params_read(struct stream *s)
1700 {
1701 struct if_link_params *iflp;
1702 ifindex_t ifindex;
1703
1704 assert(s);
1705
1706 ifindex = stream_getl(s);
1707
1708 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
1709
1710 if (ifp == NULL) {
1711 zlog_err("%s: unknown ifindex %u, shouldn't happen", __func__,
1712 ifindex);
1713 return NULL;
1714 }
1715
1716 if ((iflp = if_link_params_get(ifp)) == NULL)
1717 return NULL;
1718
1719 link_params_set_value(s, iflp);
1720
1721 return ifp;
1722 }
1723
1724 void zebra_interface_if_set_value(struct stream *s, struct interface *ifp)
1725 {
1726 uint8_t link_params_status = 0;
1727 ifindex_t old_ifindex;
1728
1729 old_ifindex = ifp->ifindex;
1730 /* Read interface's index. */
1731 if_set_index(ifp, stream_getl(s));
1732 ifp->status = stream_getc(s);
1733
1734 /* Read interface's value. */
1735 ifp->flags = stream_getq(s);
1736 ifp->ptm_enable = stream_getc(s);
1737 ifp->ptm_status = stream_getc(s);
1738 ifp->metric = stream_getl(s);
1739 ifp->speed = stream_getl(s);
1740 ifp->mtu = stream_getl(s);
1741 ifp->mtu6 = stream_getl(s);
1742 ifp->bandwidth = stream_getl(s);
1743 ifp->ll_type = stream_getl(s);
1744 ifp->hw_addr_len = stream_getl(s);
1745 if (ifp->hw_addr_len)
1746 stream_get(ifp->hw_addr, s,
1747 MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
1748
1749 /* Read Traffic Engineering status */
1750 link_params_status = stream_getc(s);
1751 /* Then, Traffic Engineering parameters if any */
1752 if (link_params_status) {
1753 struct if_link_params *iflp = if_link_params_get(ifp);
1754 link_params_set_value(s, iflp);
1755 }
1756
1757 nexthop_group_interface_state_change(ifp, old_ifindex);
1758 }
1759
1760 size_t zebra_interface_link_params_write(struct stream *s,
1761 struct interface *ifp)
1762 {
1763 size_t w;
1764 struct if_link_params *iflp;
1765 int i;
1766
1767 if (s == NULL || ifp == NULL || ifp->link_params == NULL)
1768 return 0;
1769
1770 iflp = ifp->link_params;
1771 w = 0;
1772
1773 w += stream_putl(s, iflp->lp_status);
1774
1775 w += stream_putl(s, iflp->te_metric);
1776 w += stream_putf(s, iflp->max_bw);
1777 w += stream_putf(s, iflp->max_rsv_bw);
1778
1779 w += stream_putl(s, MAX_CLASS_TYPE);
1780 for (i = 0; i < MAX_CLASS_TYPE; i++)
1781 w += stream_putf(s, iflp->unrsv_bw[i]);
1782
1783 w += stream_putl(s, iflp->admin_grp);
1784 w += stream_putl(s, iflp->rmt_as);
1785 w += stream_put_in_addr(s, &iflp->rmt_ip);
1786
1787 w += stream_putl(s, iflp->av_delay);
1788 w += stream_putl(s, iflp->min_delay);
1789 w += stream_putl(s, iflp->max_delay);
1790 w += stream_putl(s, iflp->delay_var);
1791
1792 w += stream_putf(s, iflp->pkt_loss);
1793 w += stream_putf(s, iflp->res_bw);
1794 w += stream_putf(s, iflp->ava_bw);
1795 w += stream_putf(s, iflp->use_bw);
1796
1797 return w;
1798 }
1799
1800 /*
1801 * format of message for address additon is:
1802 * 0
1803 * 0 1 2 3 4 5 6 7
1804 * +-+-+-+-+-+-+-+-+
1805 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1806 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1807 * | |
1808 * + +
1809 * | ifindex |
1810 * + +
1811 * | |
1812 * + +
1813 * | |
1814 * +-+-+-+-+-+-+-+-+
1815 * | ifc_flags | flags for connected address
1816 * +-+-+-+-+-+-+-+-+
1817 * | addr_family |
1818 * +-+-+-+-+-+-+-+-+
1819 * | addr... |
1820 * : :
1821 * | |
1822 * +-+-+-+-+-+-+-+-+
1823 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1824 * +-+-+-+-+-+-+-+-+
1825 * | daddr.. |
1826 * : :
1827 * | |
1828 * +-+-+-+-+-+-+-+-+
1829 */
1830
1831 static int memconstant(const void *s, int c, size_t n)
1832 {
1833 const uint8_t *p = s;
1834
1835 while (n-- > 0)
1836 if (*p++ != c)
1837 return 0;
1838 return 1;
1839 }
1840
1841
1842 struct connected *zebra_interface_address_read(int type, struct stream *s,
1843 vrf_id_t vrf_id)
1844 {
1845 ifindex_t ifindex;
1846 struct interface *ifp;
1847 struct connected *ifc;
1848 struct prefix p, d, *dp;
1849 int plen;
1850 uint8_t ifc_flags;
1851
1852 memset(&p, 0, sizeof(p));
1853 memset(&d, 0, sizeof(d));
1854
1855 /* Get interface index. */
1856 ifindex = stream_getl(s);
1857
1858 /* Lookup index. */
1859 ifp = if_lookup_by_index(ifindex, vrf_id);
1860 if (ifp == NULL) {
1861 zlog_warn("INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1862 (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL",
1863 ifindex, vrf_id);
1864 return NULL;
1865 }
1866
1867 /* Fetch flag. */
1868 ifc_flags = stream_getc(s);
1869
1870 /* Fetch interface address. */
1871 d.family = p.family = stream_getc(s);
1872 plen = prefix_blen(&d);
1873
1874 zclient_stream_get_prefix(s, &p);
1875
1876 /* Fetch destination address. */
1877 stream_get(&d.u.prefix, s, plen);
1878
1879 /* N.B. NULL destination pointers are encoded as all zeroes */
1880 dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d;
1881
1882 if (type == ZEBRA_INTERFACE_ADDRESS_ADD) {
1883 ifc = connected_lookup_prefix_exact(ifp, &p);
1884 if (!ifc) {
1885 /* N.B. NULL destination pointers are encoded as all
1886 * zeroes */
1887 ifc = connected_add_by_prefix(ifp, &p, dp);
1888 }
1889 if (ifc) {
1890 ifc->flags = ifc_flags;
1891 if (ifc->destination)
1892 ifc->destination->prefixlen =
1893 ifc->address->prefixlen;
1894 else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) {
1895 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1896 * "peer" */
1897 char buf[PREFIX_STRLEN];
1898 zlog_warn(
1899 "warning: interface %s address %s "
1900 "with peer flag set, but no peer address!",
1901 ifp->name, prefix2str(ifc->address, buf,
1902 sizeof buf));
1903 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
1904 }
1905 }
1906 } else {
1907 assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE);
1908 ifc = connected_delete_by_prefix(ifp, &p);
1909 }
1910
1911 return ifc;
1912 }
1913
1914 /*
1915 * format of message for neighbor connected address is:
1916 * 0
1917 * 0 1 2 3 4 5 6 7
1918 * +-+-+-+-+-+-+-+-+
1919 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1920 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1921 * | |
1922 * + +
1923 * | ifindex |
1924 * + +
1925 * | |
1926 * + +
1927 * | |
1928 * +-+-+-+-+-+-+-+-+
1929 * | addr_family |
1930 * +-+-+-+-+-+-+-+-+
1931 * | addr... |
1932 * : :
1933 * | |
1934 * +-+-+-+-+-+-+-+-+
1935 * | addr_len | len of addr.
1936 * +-+-+-+-+-+-+-+-+
1937 */
1938 struct nbr_connected *
1939 zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id)
1940 {
1941 unsigned int ifindex;
1942 struct interface *ifp;
1943 struct prefix p;
1944 struct nbr_connected *ifc;
1945
1946 /* Get interface index. */
1947 ifindex = stream_getl(s);
1948
1949 /* Lookup index. */
1950 ifp = if_lookup_by_index(ifindex, vrf_id);
1951 if (ifp == NULL) {
1952 zlog_warn("INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1953 (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD"
1954 : "DELETE",
1955 ifindex, vrf_id);
1956 return NULL;
1957 }
1958
1959 p.family = stream_getc(s);
1960 stream_get(&p.u.prefix, s, prefix_blen(&p));
1961 p.prefixlen = stream_getc(s);
1962
1963 if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) {
1964 /* Currently only supporting P2P links, so any new RA source
1965 address is
1966 considered as the replacement of the previously learnt
1967 Link-Local address. */
1968 if (!(ifc = listnode_head(ifp->nbr_connected))) {
1969 ifc = nbr_connected_new();
1970 ifc->address = prefix_new();
1971 ifc->ifp = ifp;
1972 listnode_add(ifp->nbr_connected, ifc);
1973 }
1974
1975 prefix_copy(ifc->address, &p);
1976 } else {
1977 assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE);
1978
1979 ifc = nbr_connected_check(ifp, &p);
1980 if (ifc)
1981 listnode_delete(ifp->nbr_connected, ifc);
1982 }
1983
1984 return ifc;
1985 }
1986
1987 struct interface *zebra_interface_vrf_update_read(struct stream *s,
1988 vrf_id_t vrf_id,
1989 vrf_id_t *new_vrf_id)
1990 {
1991 unsigned int ifindex;
1992 struct interface *ifp;
1993 vrf_id_t new_id;
1994
1995 /* Get interface index. */
1996 ifindex = stream_getl(s);
1997
1998 /* Lookup interface. */
1999 ifp = if_lookup_by_index(ifindex, vrf_id);
2000 if (ifp == NULL) {
2001 zlog_warn("INTERFACE_VRF_UPDATE: Cannot find IF %u in VRF %d",
2002 ifindex, vrf_id);
2003 return NULL;
2004 }
2005
2006 /* Fetch new VRF Id. */
2007 new_id = stream_getw(s);
2008
2009 *new_vrf_id = new_id;
2010 return ifp;
2011 }
2012
2013 /* filter unwanted messages until the expected one arrives */
2014 static int zclient_read_sync_response(struct zclient *zclient,
2015 uint16_t expected_cmd)
2016 {
2017 struct stream *s;
2018 uint16_t size = -1;
2019 uint8_t marker;
2020 uint8_t version;
2021 vrf_id_t vrf_id;
2022 uint16_t cmd;
2023 fd_set readfds;
2024 int ret;
2025
2026 ret = 0;
2027 cmd = expected_cmd + 1;
2028 while (ret == 0 && cmd != expected_cmd) {
2029 s = zclient->ibuf;
2030 stream_reset(s);
2031
2032 /* wait until response arrives */
2033 FD_ZERO(&readfds);
2034 FD_SET(zclient->sock, &readfds);
2035 select(zclient->sock + 1, &readfds, NULL, NULL, NULL);
2036 if (!FD_ISSET(zclient->sock, &readfds))
2037 continue;
2038 /* read response */
2039 ret = zclient_read_header(s, zclient->sock, &size, &marker,
2040 &version, &vrf_id, &cmd);
2041 if (zclient_debug)
2042 zlog_debug("%s: Response (%d bytes) received", __func__,
2043 size);
2044 }
2045 if (ret != 0) {
2046 zlog_err("%s: Invalid Sync Message Reply", __func__);
2047 return -1;
2048 }
2049
2050 return 0;
2051 }
2052 /**
2053 * Connect to label manager in a syncronous way
2054 *
2055 * It first writes the request to zcient output buffer and then
2056 * immediately reads the answer from the input buffer.
2057 *
2058 * @param zclient Zclient used to connect to label manager (zebra)
2059 * @result Result of response
2060 */
2061 int lm_label_manager_connect(struct zclient *zclient)
2062 {
2063 int ret;
2064 struct stream *s;
2065 uint8_t result;
2066
2067 if (zclient_debug)
2068 zlog_debug("Connecting to Label Manager");
2069
2070 if (zclient->sock < 0)
2071 return -1;
2072
2073 /* send request */
2074 s = zclient->obuf;
2075 stream_reset(s);
2076 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, VRF_DEFAULT);
2077
2078 /* proto */
2079 stream_putc(s, zclient->redist_default);
2080 /* instance */
2081 stream_putw(s, zclient->instance);
2082
2083 /* Put length at the first point of the stream. */
2084 stream_putw_at(s, 0, stream_get_endp(s));
2085
2086 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2087 if (ret < 0) {
2088 zlog_err("%s: can't write to zclient->sock", __func__);
2089 close(zclient->sock);
2090 zclient->sock = -1;
2091 return -1;
2092 }
2093 if (ret == 0) {
2094 zlog_err("%s: zclient->sock connection closed", __func__);
2095 close(zclient->sock);
2096 zclient->sock = -1;
2097 return -1;
2098 }
2099 if (zclient_debug)
2100 zlog_debug("%s: Label manager connect request (%d bytes) sent",
2101 __func__, ret);
2102
2103 /* read response */
2104 if (zclient_read_sync_response(zclient, ZEBRA_LABEL_MANAGER_CONNECT)
2105 != 0)
2106 return -1;
2107
2108 /* result */
2109 s = zclient->ibuf;
2110 result = stream_getc(s);
2111 if (zclient_debug)
2112 zlog_debug(
2113 "%s: Label Manager connect response received, result %u",
2114 __func__, result);
2115
2116 return (int)result;
2117 }
2118
2119 /*
2120 * Asynchronous label chunk request
2121 *
2122 * @param zclient Zclient used to connect to label manager (zebra)
2123 * @param keep Avoid garbage collection
2124 * @param chunk_size Amount of labels requested
2125 * @result 0 on success, -1 otherwise
2126 */
2127 int zclient_send_get_label_chunk(
2128 struct zclient *zclient,
2129 uint8_t keep,
2130 uint32_t chunk_size)
2131 {
2132 struct stream *s;
2133
2134 if (zclient_debug)
2135 zlog_debug("Getting Label Chunk");
2136
2137 if (zclient->sock < 0)
2138 return -1;
2139
2140 s = zclient->obuf;
2141 stream_reset(s);
2142
2143 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2144 stream_putc(s, keep);
2145 stream_putl(s, chunk_size);
2146
2147 /* Put length at the first point of the stream. */
2148 stream_putw_at(s, 0, stream_get_endp(s));
2149
2150 return zclient_send_message(zclient);
2151 }
2152
2153 /**
2154 * Function to request a label chunk in a syncronous way
2155 *
2156 * It first writes the request to zlcient output buffer and then
2157 * immediately reads the answer from the input buffer.
2158 *
2159 * @param zclient Zclient used to connect to label manager (zebra)
2160 * @param keep Avoid garbage collection
2161 * @param chunk_size Amount of labels requested
2162 * @param start To write first assigned chunk label to
2163 * @param end To write last assigned chunk label to
2164 * @result 0 on success, -1 otherwise
2165 */
2166 int lm_get_label_chunk(struct zclient *zclient, uint8_t keep,
2167 uint32_t chunk_size, uint32_t *start, uint32_t *end)
2168 {
2169 int ret;
2170 struct stream *s;
2171 uint8_t response_keep;
2172
2173 if (zclient_debug)
2174 zlog_debug("Getting Label Chunk");
2175
2176 if (zclient->sock < 0)
2177 return -1;
2178
2179 /* send request */
2180 s = zclient->obuf;
2181 stream_reset(s);
2182 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT);
2183 /* keep */
2184 stream_putc(s, keep);
2185 /* chunk size */
2186 stream_putl(s, chunk_size);
2187 /* Put length at the first point of the stream. */
2188 stream_putw_at(s, 0, stream_get_endp(s));
2189
2190 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2191 if (ret < 0) {
2192 zlog_err("%s: can't write to zclient->sock", __func__);
2193 close(zclient->sock);
2194 zclient->sock = -1;
2195 return -1;
2196 }
2197 if (ret == 0) {
2198 zlog_err("%s: zclient->sock connection closed", __func__);
2199 close(zclient->sock);
2200 zclient->sock = -1;
2201 return -1;
2202 }
2203 if (zclient_debug)
2204 zlog_debug("%s: Label chunk request (%d bytes) sent", __func__,
2205 ret);
2206
2207 /* read response */
2208 if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0)
2209 return -1;
2210
2211 s = zclient->ibuf;
2212 /* keep */
2213 response_keep = stream_getc(s);
2214 /* start and end labels */
2215 *start = stream_getl(s);
2216 *end = stream_getl(s);
2217
2218 /* not owning this response */
2219 if (keep != response_keep) {
2220 zlog_err(
2221 "%s: Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2222 __func__, *start, *end, keep, response_keep);
2223 }
2224 /* sanity */
2225 if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN
2226 || *end > MPLS_LABEL_UNRESERVED_MAX) {
2227 zlog_err("%s: Invalid Label chunk: %u - %u", __func__, *start,
2228 *end);
2229 return -1;
2230 }
2231
2232 if (zclient_debug)
2233 zlog_debug("Label Chunk assign: %u - %u (%u) ", *start, *end,
2234 response_keep);
2235
2236 return 0;
2237 }
2238
2239 /**
2240 * Function to release a label chunk
2241 *
2242 * @param zclient Zclient used to connect to label manager (zebra)
2243 * @param start First label of chunk
2244 * @param end Last label of chunk
2245 * @result 0 on success, -1 otherwise
2246 */
2247 int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
2248 uint32_t end)
2249 {
2250 int ret;
2251 struct stream *s;
2252
2253 if (zclient_debug)
2254 zlog_debug("Releasing Label Chunk");
2255
2256 if (zclient->sock < 0)
2257 return -1;
2258
2259 /* send request */
2260 s = zclient->obuf;
2261 stream_reset(s);
2262 zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT);
2263
2264 /* start */
2265 stream_putl(s, start);
2266 /* end */
2267 stream_putl(s, end);
2268
2269 /* Put length at the first point of the stream. */
2270 stream_putw_at(s, 0, stream_get_endp(s));
2271
2272 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2273 if (ret < 0) {
2274 zlog_err("%s: can't write to zclient->sock", __func__);
2275 close(zclient->sock);
2276 zclient->sock = -1;
2277 return -1;
2278 }
2279 if (ret == 0) {
2280 zlog_err("%s: zclient->sock connection closed", __func__);
2281 close(zclient->sock);
2282 zclient->sock = -1;
2283 return -1;
2284 }
2285
2286 return 0;
2287 }
2288
2289 /**
2290 * Connect to table manager in a syncronous way
2291 *
2292 * It first writes the request to zcient output buffer and then
2293 * immediately reads the answer from the input buffer.
2294 *
2295 * @param zclient Zclient used to connect to table manager (zebra)
2296 * @result Result of response
2297 */
2298 int tm_table_manager_connect(struct zclient *zclient)
2299 {
2300 int ret;
2301 struct stream *s;
2302 uint8_t result;
2303
2304 if (zclient_debug)
2305 zlog_debug("Connecting to Table Manager");
2306
2307 if (zclient->sock < 0)
2308 return -1;
2309
2310 /* send request */
2311 s = zclient->obuf;
2312 stream_reset(s);
2313 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT);
2314
2315 /* proto */
2316 stream_putc(s, zclient->redist_default);
2317 /* instance */
2318 stream_putw(s, zclient->instance);
2319
2320 /* Put length at the first point of the stream. */
2321 stream_putw_at(s, 0, stream_get_endp(s));
2322
2323 ret = zclient_send_message(zclient);
2324 if (ret < 0)
2325 return -1;
2326
2327 if (zclient_debug)
2328 zlog_debug("%s: Table manager connect request sent",
2329 __func__);
2330
2331 /* read response */
2332 if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT)
2333 != 0)
2334 return -1;
2335
2336 /* result */
2337 s = zclient->ibuf;
2338 STREAM_GETC(s, result);
2339 if (zclient_debug)
2340 zlog_debug(
2341 "%s: Table Manager connect response received, result %u",
2342 __func__, result);
2343
2344 return (int)result;
2345 stream_failure:
2346 return 0;
2347 }
2348
2349 /**
2350 * Function to request a table chunk in a syncronous way
2351 *
2352 * It first writes the request to zclient output buffer and then
2353 * immediately reads the answer from the input buffer.
2354 *
2355 * @param zclient Zclient used to connect to table manager (zebra)
2356 * @param chunk_size Amount of table requested
2357 * @param start to write first assigned chunk table RT ID to
2358 * @param end To write last assigned chunk table RT ID to
2359 * @result 0 on success, -1 otherwise
2360 */
2361 int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
2362 uint32_t *start, uint32_t *end)
2363 {
2364 int ret;
2365 struct stream *s;
2366
2367 if (zclient_debug)
2368 zlog_debug("Getting Table Chunk");
2369
2370 if (zclient->sock < 0)
2371 return -1;
2372
2373 /* send request */
2374 s = zclient->obuf;
2375 stream_reset(s);
2376 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT);
2377 /* chunk size */
2378 stream_putl(s, chunk_size);
2379 /* Put length at the first point of the stream. */
2380 stream_putw_at(s, 0, stream_get_endp(s));
2381
2382 ret = writen(zclient->sock, s->data, stream_get_endp(s));
2383 if (ret < 0) {
2384 zlog_err("%s: can't write to zclient->sock", __func__);
2385 close(zclient->sock);
2386 zclient->sock = -1;
2387 return -1;
2388 }
2389 if (ret == 0) {
2390 zlog_err("%s: zclient->sock connection closed", __func__);
2391 close(zclient->sock);
2392 zclient->sock = -1;
2393 return -1;
2394 }
2395 if (zclient_debug)
2396 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__,
2397 ret);
2398
2399 /* read response */
2400 if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0)
2401 return -1;
2402
2403 s = zclient->ibuf;
2404 /* start and end table IDs */
2405 STREAM_GETL(s, *start);
2406 STREAM_GETL(s, *end);
2407
2408 if (zclient_debug)
2409 zlog_debug("Table Chunk assign: %u - %u ", *start, *end);
2410
2411 stream_failure:
2412 return 0;
2413 }
2414
2415 /**
2416 * Function to release a table chunk
2417 *
2418 * @param zclient Zclient used to connect to table manager (zebra)
2419 * @param start First label of table
2420 * @param end Last label of chunk
2421 * @result 0 on success, -1 otherwise
2422 */
2423 int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
2424 uint32_t end)
2425 {
2426 struct stream *s;
2427
2428 if (zclient_debug)
2429 zlog_debug("Releasing Table Chunk");
2430
2431 if (zclient->sock < 0)
2432 return -1;
2433
2434 /* send request */
2435 s = zclient->obuf;
2436 stream_reset(s);
2437 zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT);
2438
2439 /* start */
2440 stream_putl(s, start);
2441 /* end */
2442 stream_putl(s, end);
2443
2444 /* Put length at the first point of the stream. */
2445 stream_putw_at(s, 0, stream_get_endp(s));
2446
2447 return zclient_send_message(zclient);
2448 }
2449
2450
2451 int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw)
2452 {
2453 struct stream *s;
2454
2455 /* Reset stream. */
2456 s = zclient->obuf;
2457 stream_reset(s);
2458
2459 zclient_create_header(s, command, VRF_DEFAULT);
2460 stream_write(s, pw->ifname, IF_NAMESIZE);
2461 stream_putl(s, pw->ifindex);
2462
2463 /* Put type */
2464 stream_putl(s, pw->type);
2465
2466 /* Put nexthop */
2467 stream_putl(s, pw->af);
2468 switch (pw->af) {
2469 case AF_INET:
2470 stream_put_in_addr(s, &pw->nexthop.ipv4);
2471 break;
2472 case AF_INET6:
2473 stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16);
2474 break;
2475 default:
2476 zlog_err("%s: unknown af", __func__);
2477 return -1;
2478 }
2479
2480 /* Put labels */
2481 stream_putl(s, pw->local_label);
2482 stream_putl(s, pw->remote_label);
2483
2484 /* Put flags */
2485 stream_putc(s, pw->flags);
2486
2487 /* Protocol specific fields */
2488 stream_write(s, &pw->data, sizeof(union pw_protocol_fields));
2489
2490 /* Put length at the first point of the stream. */
2491 stream_putw_at(s, 0, stream_get_endp(s));
2492
2493 return zclient_send_message(zclient);
2494 }
2495
2496 /*
2497 * Receive PW status update from Zebra and send it to LDE process.
2498 */
2499 void zebra_read_pw_status_update(int command, struct zclient *zclient,
2500 zebra_size_t length, vrf_id_t vrf_id,
2501 struct zapi_pw_status *pw)
2502 {
2503 struct stream *s;
2504
2505 memset(pw, 0, sizeof(struct zapi_pw_status));
2506 s = zclient->ibuf;
2507
2508 /* Get data. */
2509 stream_get(pw->ifname, s, IF_NAMESIZE);
2510 pw->ifindex = stream_getl(s);
2511 pw->status = stream_getl(s);
2512 }
2513
2514 static void zclient_capability_decode(int command, struct zclient *zclient,
2515 zebra_size_t length, vrf_id_t vrf_id)
2516 {
2517 struct zclient_capabilities cap;
2518 struct stream *s = zclient->ibuf;
2519 uint8_t mpls_enabled;
2520
2521 memset(&cap, 0, sizeof(cap));
2522 STREAM_GETC(s, mpls_enabled);
2523 cap.mpls_enabled = !!mpls_enabled;
2524 STREAM_GETL(s, cap.ecmp);
2525
2526 if (zclient->zebra_capabilities)
2527 (*zclient->zebra_capabilities)(&cap);
2528
2529 stream_failure:
2530 return;
2531 }
2532
2533 /* Zebra client message read function. */
2534 static int zclient_read(struct thread *thread)
2535 {
2536 size_t already;
2537 uint16_t length, command;
2538 uint8_t marker, version;
2539 vrf_id_t vrf_id;
2540 struct zclient *zclient;
2541
2542 /* Get socket to zebra. */
2543 zclient = THREAD_ARG(thread);
2544 zclient->t_read = NULL;
2545
2546 /* Read zebra header (if we don't have it already). */
2547 if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) {
2548 ssize_t nbyte;
2549 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2550 ZEBRA_HEADER_SIZE - already))
2551 == 0)
2552 || (nbyte == -1)) {
2553 if (zclient_debug)
2554 zlog_debug(
2555 "zclient connection closed socket [%d].",
2556 zclient->sock);
2557 return zclient_failed(zclient);
2558 }
2559 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2560 /* Try again later. */
2561 zclient_event(ZCLIENT_READ, zclient);
2562 return 0;
2563 }
2564 already = ZEBRA_HEADER_SIZE;
2565 }
2566
2567 /* Reset to read from the beginning of the incoming packet. */
2568 stream_set_getp(zclient->ibuf, 0);
2569
2570 /* Fetch header values. */
2571 length = stream_getw(zclient->ibuf);
2572 marker = stream_getc(zclient->ibuf);
2573 version = stream_getc(zclient->ibuf);
2574 vrf_id = stream_getl(zclient->ibuf);
2575 command = stream_getw(zclient->ibuf);
2576
2577 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2578 zlog_err(
2579 "%s: socket %d version mismatch, marker %d, version %d",
2580 __func__, zclient->sock, marker, version);
2581 return zclient_failed(zclient);
2582 }
2583
2584 if (length < ZEBRA_HEADER_SIZE) {
2585 zlog_err("%s: socket %d message length %u is less than %d ",
2586 __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
2587 return zclient_failed(zclient);
2588 }
2589
2590 /* Length check. */
2591 if (length > STREAM_SIZE(zclient->ibuf)) {
2592 struct stream *ns;
2593 zlog_warn(
2594 "%s: message size %u exceeds buffer size %lu, expanding...",
2595 __func__, length,
2596 (unsigned long)STREAM_SIZE(zclient->ibuf));
2597 ns = stream_new(length);
2598 stream_copy(ns, zclient->ibuf);
2599 stream_free(zclient->ibuf);
2600 zclient->ibuf = ns;
2601 }
2602
2603 /* Read rest of zebra packet. */
2604 if (already < length) {
2605 ssize_t nbyte;
2606 if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
2607 length - already))
2608 == 0)
2609 || (nbyte == -1)) {
2610 if (zclient_debug)
2611 zlog_debug(
2612 "zclient connection closed socket [%d].",
2613 zclient->sock);
2614 return zclient_failed(zclient);
2615 }
2616 if (nbyte != (ssize_t)(length - already)) {
2617 /* Try again later. */
2618 zclient_event(ZCLIENT_READ, zclient);
2619 return 0;
2620 }
2621 }
2622
2623 length -= ZEBRA_HEADER_SIZE;
2624
2625 if (zclient_debug)
2626 zlog_debug("zclient 0x%p command 0x%x VRF %u\n",
2627 (void *)zclient, command, vrf_id);
2628
2629 switch (command) {
2630 case ZEBRA_CAPABILITIES:
2631 zclient_capability_decode(command, zclient, length, vrf_id);
2632 break;
2633 case ZEBRA_ROUTER_ID_UPDATE:
2634 if (zclient->router_id_update)
2635 (*zclient->router_id_update)(command, zclient, length,
2636 vrf_id);
2637 break;
2638 case ZEBRA_VRF_ADD:
2639 zclient_vrf_add(zclient, vrf_id);
2640 break;
2641 case ZEBRA_VRF_DELETE:
2642 zclient_vrf_delete(zclient, vrf_id);
2643 break;
2644 case ZEBRA_INTERFACE_ADD:
2645 if (zclient->interface_add)
2646 (*zclient->interface_add)(command, zclient, length,
2647 vrf_id);
2648 break;
2649 case ZEBRA_INTERFACE_DELETE:
2650 if (zclient->interface_delete)
2651 (*zclient->interface_delete)(command, zclient, length,
2652 vrf_id);
2653 break;
2654 case ZEBRA_INTERFACE_ADDRESS_ADD:
2655 if (zclient->interface_address_add)
2656 (*zclient->interface_address_add)(command, zclient,
2657 length, vrf_id);
2658 break;
2659 case ZEBRA_INTERFACE_ADDRESS_DELETE:
2660 if (zclient->interface_address_delete)
2661 (*zclient->interface_address_delete)(command, zclient,
2662 length, vrf_id);
2663 break;
2664 case ZEBRA_INTERFACE_BFD_DEST_UPDATE:
2665 if (zclient->interface_bfd_dest_update)
2666 (*zclient->interface_bfd_dest_update)(command, zclient,
2667 length, vrf_id);
2668 break;
2669 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD:
2670 if (zclient->interface_nbr_address_add)
2671 (*zclient->interface_nbr_address_add)(command, zclient,
2672 length, vrf_id);
2673 break;
2674 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE:
2675 if (zclient->interface_nbr_address_delete)
2676 (*zclient->interface_nbr_address_delete)(
2677 command, zclient, length, vrf_id);
2678 break;
2679 case ZEBRA_INTERFACE_UP:
2680 if (zclient->interface_up)
2681 (*zclient->interface_up)(command, zclient, length,
2682 vrf_id);
2683 break;
2684 case ZEBRA_INTERFACE_DOWN:
2685 if (zclient->interface_down)
2686 (*zclient->interface_down)(command, zclient, length,
2687 vrf_id);
2688 break;
2689 case ZEBRA_INTERFACE_VRF_UPDATE:
2690 if (zclient->interface_vrf_update)
2691 (*zclient->interface_vrf_update)(command, zclient,
2692 length, vrf_id);
2693 break;
2694 case ZEBRA_NEXTHOP_UPDATE:
2695 if (zclient_debug)
2696 zlog_debug("zclient rcvd nexthop update\n");
2697 if (zclient->nexthop_update)
2698 (*zclient->nexthop_update)(command, zclient, length,
2699 vrf_id);
2700 break;
2701 case ZEBRA_IMPORT_CHECK_UPDATE:
2702 if (zclient_debug)
2703 zlog_debug("zclient rcvd import check update\n");
2704 if (zclient->import_check_update)
2705 (*zclient->import_check_update)(command, zclient,
2706 length, vrf_id);
2707 break;
2708 case ZEBRA_BFD_DEST_REPLAY:
2709 if (zclient->bfd_dest_replay)
2710 (*zclient->bfd_dest_replay)(command, zclient, length,
2711 vrf_id);
2712 break;
2713 case ZEBRA_REDISTRIBUTE_ROUTE_ADD:
2714 if (zclient->redistribute_route_add)
2715 (*zclient->redistribute_route_add)(command, zclient,
2716 length, vrf_id);
2717 break;
2718 case ZEBRA_REDISTRIBUTE_ROUTE_DEL:
2719 if (zclient->redistribute_route_del)
2720 (*zclient->redistribute_route_del)(command, zclient,
2721 length, vrf_id);
2722 break;
2723 case ZEBRA_INTERFACE_LINK_PARAMS:
2724 if (zclient->interface_link_params)
2725 (*zclient->interface_link_params)(command, zclient,
2726 length);
2727 break;
2728 case ZEBRA_FEC_UPDATE:
2729 if (zclient_debug)
2730 zlog_debug("zclient rcvd fec update\n");
2731 if (zclient->fec_update)
2732 (*zclient->fec_update)(command, zclient, length);
2733 break;
2734 case ZEBRA_VNI_ADD:
2735 if (zclient->local_vni_add)
2736 (*zclient->local_vni_add)(command, zclient, length,
2737 vrf_id);
2738 break;
2739 case ZEBRA_VNI_DEL:
2740 if (zclient->local_vni_del)
2741 (*zclient->local_vni_del)(command, zclient, length,
2742 vrf_id);
2743 break;
2744 case ZEBRA_L3VNI_ADD:
2745 if (zclient->local_l3vni_add)
2746 (*zclient->local_l3vni_add)(command, zclient, length,
2747 vrf_id);
2748 break;
2749 case ZEBRA_L3VNI_DEL:
2750 if (zclient->local_l3vni_del)
2751 (*zclient->local_l3vni_del)(command, zclient, length,
2752 vrf_id);
2753 break;
2754 case ZEBRA_MACIP_ADD:
2755 if (zclient->local_macip_add)
2756 (*zclient->local_macip_add)(command, zclient, length,
2757 vrf_id);
2758 break;
2759 case ZEBRA_MACIP_DEL:
2760 if (zclient->local_macip_del)
2761 (*zclient->local_macip_del)(command, zclient, length,
2762 vrf_id);
2763 break;
2764 case ZEBRA_IP_PREFIX_ROUTE_ADD:
2765 if (zclient->local_ip_prefix_add)
2766 (*zclient->local_ip_prefix_add)(command, zclient,
2767 length, vrf_id);
2768 break;
2769 case ZEBRA_IP_PREFIX_ROUTE_DEL:
2770 if (zclient->local_ip_prefix_del)
2771 (*zclient->local_ip_prefix_del)(command, zclient,
2772 length, vrf_id);
2773 break;
2774 case ZEBRA_PW_STATUS_UPDATE:
2775 if (zclient->pw_status_update)
2776 (*zclient->pw_status_update)(command, zclient, length,
2777 vrf_id);
2778 break;
2779 case ZEBRA_ROUTE_NOTIFY_OWNER:
2780 if (zclient->route_notify_owner)
2781 (*zclient->route_notify_owner)(command, zclient, length,
2782 vrf_id);
2783 break;
2784 case ZEBRA_RULE_NOTIFY_OWNER:
2785 if (zclient->rule_notify_owner)
2786 (*zclient->rule_notify_owner)(command, zclient, length,
2787 vrf_id);
2788 break;
2789 case ZEBRA_GET_LABEL_CHUNK:
2790 if (zclient->label_chunk)
2791 (*zclient->label_chunk)(command, zclient, length,
2792 vrf_id);
2793 break;
2794 case ZEBRA_IPSET_NOTIFY_OWNER:
2795 if (zclient->ipset_notify_owner)
2796 (*zclient->ipset_notify_owner)(command, zclient, length,
2797 vrf_id);
2798 break;
2799 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER:
2800 if (zclient->ipset_entry_notify_owner)
2801 (*zclient->ipset_entry_notify_owner)(command,
2802 zclient, length,
2803 vrf_id);
2804 break;
2805 case ZEBRA_IPTABLE_NOTIFY_OWNER:
2806 if (zclient->iptable_notify_owner)
2807 (*zclient->iptable_notify_owner)(command,
2808 zclient, length,
2809 vrf_id);
2810 default:
2811 break;
2812 }
2813
2814 if (zclient->sock < 0)
2815 /* Connection was closed during packet processing. */
2816 return -1;
2817
2818 /* Register read thread. */
2819 stream_reset(zclient->ibuf);
2820 zclient_event(ZCLIENT_READ, zclient);
2821
2822 return 0;
2823 }
2824
2825 void zclient_redistribute(int command, struct zclient *zclient, afi_t afi,
2826 int type, unsigned short instance, vrf_id_t vrf_id)
2827 {
2828
2829 if (instance) {
2830 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2831 if (redist_check_instance(
2832 &zclient->mi_redist[afi][type], instance))
2833 return;
2834 redist_add_instance(&zclient->mi_redist[afi][type],
2835 instance);
2836 } else {
2837 if (!redist_check_instance(
2838 &zclient->mi_redist[afi][type], instance))
2839 return;
2840 redist_del_instance(&zclient->mi_redist[afi][type],
2841 instance);
2842 }
2843
2844 } else {
2845 if (command == ZEBRA_REDISTRIBUTE_ADD) {
2846 if (vrf_bitmap_check(zclient->redist[afi][type],
2847 vrf_id))
2848 return;
2849 vrf_bitmap_set(zclient->redist[afi][type], vrf_id);
2850 } else {
2851 if (!vrf_bitmap_check(zclient->redist[afi][type],
2852 vrf_id))
2853 return;
2854 vrf_bitmap_unset(zclient->redist[afi][type], vrf_id);
2855 }
2856 }
2857
2858 if (zclient->sock > 0)
2859 zebra_redistribute_send(command, zclient, afi, type, instance,
2860 vrf_id);
2861 }
2862
2863
2864 void zclient_redistribute_default(int command, struct zclient *zclient,
2865 vrf_id_t vrf_id)
2866 {
2867
2868 if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) {
2869 if (vrf_bitmap_check(zclient->default_information, vrf_id))
2870 return;
2871 vrf_bitmap_set(zclient->default_information, vrf_id);
2872 } else {
2873 if (!vrf_bitmap_check(zclient->default_information, vrf_id))
2874 return;
2875 vrf_bitmap_unset(zclient->default_information, vrf_id);
2876 }
2877
2878 if (zclient->sock > 0)
2879 zebra_message_send(zclient, command, vrf_id);
2880 }
2881
2882 static void zclient_event(enum event event, struct zclient *zclient)
2883 {
2884 switch (event) {
2885 case ZCLIENT_SCHEDULE:
2886 thread_add_event(zclient->master, zclient_connect, zclient, 0,
2887 &zclient->t_connect);
2888 break;
2889 case ZCLIENT_CONNECT:
2890 if (zclient_debug)
2891 zlog_debug(
2892 "zclient connect failures: %d schedule interval is now %d",
2893 zclient->fail, zclient->fail < 3 ? 10 : 60);
2894 thread_add_timer(zclient->master, zclient_connect, zclient,
2895 zclient->fail < 3 ? 10 : 60,
2896 &zclient->t_connect);
2897 break;
2898 case ZCLIENT_READ:
2899 zclient->t_read = NULL;
2900 thread_add_read(zclient->master, zclient_read, zclient,
2901 zclient->sock, &zclient->t_read);
2902 break;
2903 }
2904 }
2905
2906 void zclient_interface_set_master(struct zclient *client,
2907 struct interface *master,
2908 struct interface *slave)
2909 {
2910 struct stream *s;
2911
2912 s = client->obuf;
2913 stream_reset(s);
2914
2915 zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
2916
2917 stream_putl(s, master->vrf_id);
2918 stream_putl(s, master->ifindex);
2919 stream_putl(s, slave->vrf_id);
2920 stream_putl(s, slave->ifindex);
2921
2922 stream_putw_at(s, 0, stream_get_endp(s));
2923 zclient_send_message(client);
2924 }