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