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