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