]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
zebra: Allow zebra_find_client to match on instance as well
[mirror_frr.git] / zebra / zserv.c
CommitLineData
718e3744 1/* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
689f5a8c 22#include <sys/un.h>
718e3744 23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
4a1ab8e4 30#include "zebra_memory.h"
718e3744 31#include "table.h"
32#include "rib.h"
33#include "network.h"
34#include "sockunion.h"
35#include "log.h"
36#include "zclient.h"
edd7c245 37#include "privs.h"
719e9741 38#include "network.h"
39#include "buffer.h"
fb018d25 40#include "nexthop.h"
78104b9b 41#include "vrf.h"
689f5a8c 42#include "libfrr.h"
2c73b258 43#include "sockopt.h"
718e3744 44
45#include "zebra/zserv.h"
7c551956
DS
46#include "zebra/zebra_ns.h"
47#include "zebra/zebra_vrf.h"
18a6dce6 48#include "zebra/router-id.h"
718e3744 49#include "zebra/redistribute.h"
50#include "zebra/debug.h"
51#include "zebra/ipforward.h"
fb018d25 52#include "zebra/zebra_rnh.h"
5c610faf 53#include "zebra/rt_netlink.h"
88177fe3
DS
54#include "zebra/interface.h"
55#include "zebra/zebra_ptm.h"
4a04e5f7 56#include "zebra/rtadv.h"
ce549947 57#include "zebra/zebra_mpls.h"
e3be0432 58#include "zebra/zebra_mroute.h"
fea12efb 59#include "zebra/label_manager.h"
13d60d35 60#include "zebra/zebra_vxlan.h"
e0ae31b8 61#include "zebra/rt.h"
6b0655a2 62
718e3744 63/* Event list of zebra. */
64enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
65
d62a17ae 66static void zebra_event(enum event event, int sock, struct zserv *client);
ccf3557b 67
edd7c245 68extern struct zebra_privs_t zserv_privs;
6b0655a2 69
d62a17ae 70static void zebra_client_close(struct zserv *client);
71
72static int zserv_delayed_close(struct thread *thread)
73{
74 struct zserv *client = THREAD_ARG(thread);
75
76 client->t_suicide = NULL;
77 zebra_client_close(client);
78 return 0;
79}
80
81static int zserv_flush_data(struct thread *thread)
82{
83 struct zserv *client = THREAD_ARG(thread);
84
85 client->t_write = NULL;
86 if (client->t_suicide) {
87 zebra_client_close(client);
88 return -1;
89 }
90 switch (buffer_flush_available(client->wb, client->sock)) {
91 case BUFFER_ERROR:
92 zlog_warn(
93 "%s: buffer_flush_available failed on zserv client fd %d, "
94 "closing",
95 __func__, client->sock);
96 zebra_client_close(client);
97 client = NULL;
98 break;
99 case BUFFER_PENDING:
100 client->t_write = NULL;
101 thread_add_write(zebrad.master, zserv_flush_data, client,
102 client->sock, &client->t_write);
103 break;
104 case BUFFER_EMPTY:
105 break;
106 }
107
108 if (client)
109 client->last_write_time = monotime(NULL);
110 return 0;
111}
112
113int zebra_server_send_message(struct zserv *client)
114{
115 if (client->t_suicide)
116 return -1;
117
118 if (client->is_synchronous)
119 return 0;
120
121 stream_set_getp(client->obuf, 0);
122 client->last_write_cmd = stream_getw_from(client->obuf, 6);
123 switch (buffer_write(client->wb, client->sock,
124 STREAM_DATA(client->obuf),
125 stream_get_endp(client->obuf))) {
126 case BUFFER_ERROR:
127 zlog_warn(
128 "%s: buffer_write failed to zserv client fd %d, closing",
129 __func__, client->sock);
130 /* Schedule a delayed close since many of the functions that
131 call this
132 one do not check the return code. They do not allow for the
133 possibility that an I/O error may have caused the client to
134 be
135 deleted. */
136 client->t_suicide = NULL;
137 thread_add_event(zebrad.master, zserv_delayed_close, client, 0,
138 &client->t_suicide);
139 return -1;
140 case BUFFER_EMPTY:
141 THREAD_OFF(client->t_write);
142 break;
143 case BUFFER_PENDING:
144 thread_add_write(zebrad.master, zserv_flush_data, client,
145 client->sock, &client->t_write);
146 break;
147 }
148
149 client->last_write_time = monotime(NULL);
150 return 0;
151}
152
153void zserv_create_header(struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
154{
155 /* length placeholder, caller can update */
156 stream_putw(s, ZEBRA_HEADER_SIZE);
157 stream_putc(s, ZEBRA_HEADER_MARKER);
158 stream_putc(s, ZSERV_VERSION);
159 stream_putw(s, vrf_id);
160 stream_putw(s, cmd);
161}
162
163static void zserv_encode_interface(struct stream *s, struct interface *ifp)
164{
165 /* Interface information. */
166 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
167 stream_putl(s, ifp->ifindex);
168 stream_putc(s, ifp->status);
169 stream_putq(s, ifp->flags);
170 stream_putc(s, ifp->ptm_enable);
171 stream_putc(s, ifp->ptm_status);
172 stream_putl(s, ifp->metric);
173 stream_putl(s, ifp->speed);
174 stream_putl(s, ifp->mtu);
175 stream_putl(s, ifp->mtu6);
176 stream_putl(s, ifp->bandwidth);
177 stream_putl(s, ifp->ll_type);
178 stream_putl(s, ifp->hw_addr_len);
179 if (ifp->hw_addr_len)
180 stream_put(s, ifp->hw_addr, ifp->hw_addr_len);
181
182 /* Then, Traffic Engineering parameters if any */
183 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) {
184 stream_putc(s, 1);
185 zebra_interface_link_params_write(s, ifp);
186 } else
187 stream_putc(s, 0);
188
189 /* Write packet size. */
190 stream_putw_at(s, 0, stream_get_endp(s));
191}
192
193static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf)
194{
195 struct vrf_data data;
196
197 data.l.table_id = zvrf->table_id;
198 /* Pass the tableid */
199 stream_put(s, &data, sizeof(struct vrf_data));
200 /* Interface information. */
201 stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
202
203 /* Write packet size. */
204 stream_putw_at(s, 0, stream_get_endp(s));
12f6fb97
DS
205}
206
718e3744 207/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
b9df2d25 208/*
209 * This function is called in the following situations:
210 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
211 * from the client.
212 * - at startup, when zebra figures out the available interfaces
213 * - when an interface is added (where support for
214 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
215 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
216 * received)
217 */
d62a17ae 218int zsend_interface_add(struct zserv *client, struct interface *ifp)
718e3744 219{
d62a17ae 220 struct stream *s;
718e3744 221
d62a17ae 222 s = client->obuf;
223 stream_reset(s);
718e3744 224
d62a17ae 225 zserv_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
226 zserv_encode_interface(s, ifp);
718e3744 227
d62a17ae 228 client->ifadd_cnt++;
229 return zebra_server_send_message(client);
718e3744 230}
231
232/* Interface deletion from zebra daemon. */
d62a17ae 233int zsend_interface_delete(struct zserv *client, struct interface *ifp)
718e3744 234{
d62a17ae 235 struct stream *s;
718e3744 236
d62a17ae 237 s = client->obuf;
238 stream_reset(s);
718e3744 239
d62a17ae 240 zserv_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
241 zserv_encode_interface(s, ifp);
718e3744 242
d62a17ae 243 client->ifdel_cnt++;
244 return zebra_server_send_message(client);
718e3744 245}
246
d62a17ae 247int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
12f6fb97 248{
d62a17ae 249 struct stream *s;
12f6fb97 250
d62a17ae 251 s = client->obuf;
252 stream_reset(s);
12f6fb97 253
d62a17ae 254 zserv_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
255 zserv_encode_vrf(s, zvrf);
12f6fb97 256
d62a17ae 257 client->vrfadd_cnt++;
258 return zebra_server_send_message(client);
12f6fb97
DS
259}
260
261/* VRF deletion from zebra daemon. */
d62a17ae 262int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
12f6fb97 263{
d62a17ae 264 struct stream *s;
12f6fb97 265
d62a17ae 266 s = client->obuf;
267 stream_reset(s);
12f6fb97 268
d62a17ae 269 zserv_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
270 zserv_encode_vrf(s, zvrf);
12f6fb97 271
d62a17ae 272 client->vrfdel_cnt++;
273 return zebra_server_send_message(client);
12f6fb97
DS
274}
275
d62a17ae 276int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
16f1b9ee 277{
d62a17ae 278 struct stream *s;
16f1b9ee 279
d62a17ae 280 /* Check this client need interface information. */
281 if (!client->ifinfo)
282 return 0;
16f1b9ee 283
d62a17ae 284 if (!ifp->link_params)
285 return 0;
286 s = client->obuf;
287 stream_reset(s);
16f1b9ee 288
d62a17ae 289 zserv_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
16f1b9ee 290
d62a17ae 291 /* Add Interface Index */
292 stream_putl(s, ifp->ifindex);
16f1b9ee 293
d62a17ae 294 /* Then TE Link Parameters */
295 if (zebra_interface_link_params_write(s, ifp) == 0)
296 return 0;
16f1b9ee 297
d62a17ae 298 /* Write packet size. */
299 stream_putw_at(s, 0, stream_get_endp(s));
16f1b9ee 300
d62a17ae 301 return zebra_server_send_message(client);
16f1b9ee
OD
302}
303
b9df2d25 304/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
d62a17ae 305 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
b9df2d25 306 *
307 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
308 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
309 * from the client, after the ZEBRA_INTERFACE_ADD has been
310 * sent from zebra to the client
311 * - redistribute new address info to all clients in the following situations
312 * - at startup, when zebra figures out the available interfaces
313 * - when an interface is added (where support for
314 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
315 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
316 * received)
317 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
318 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
319 * - when an RTM_NEWADDR message is received from the kernel,
d62a17ae 320 *
321 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
b9df2d25 322 *
323 * zsend_interface_address(DELETE)
d62a17ae 324 * ^
325 * |
326 * zebra_interface_address_delete_update
b9df2d25 327 * ^ ^ ^
6eb8827d 328 * | | if_delete_update
329 * | |
b9df2d25 330 * ip_address_uninstall connected_delete_ipv4
331 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
332 * ^ ^
333 * | |
334 * | RTM_NEWADDR on routing/netlink socket
335 * |
336 * vty commands:
337 * "no ip address A.B.C.D/M [label LINE]"
338 * "no ip address A.B.C.D/M secondary"
339 * ["no ipv6 address X:X::X:X/M"]
340 *
341 */
d62a17ae 342int zsend_interface_address(int cmd, struct zserv *client,
343 struct interface *ifp, struct connected *ifc)
344{
345 int blen;
346 struct stream *s;
347 struct prefix *p;
348
349 s = client->obuf;
350 stream_reset(s);
351
352 zserv_create_header(s, cmd, ifp->vrf_id);
353 stream_putl(s, ifp->ifindex);
354
355 /* Interface address flag. */
356 stream_putc(s, ifc->flags);
357
358 /* Prefix information. */
359 p = ifc->address;
360 stream_putc(s, p->family);
361 blen = prefix_blen(p);
362 stream_put(s, &p->u.prefix, blen);
363
364 /*
365 * XXX gnu version does not send prefixlen for
366 * ZEBRA_INTERFACE_ADDRESS_DELETE
367 * but zebra_interface_address_delete_read() in the gnu version
368 * expects to find it
369 */
370 stream_putc(s, p->prefixlen);
371
372 /* Destination. */
373 p = ifc->destination;
374 if (p)
375 stream_put(s, &p->u.prefix, blen);
376 else
377 stream_put(s, NULL, blen);
718e3744 378
d62a17ae 379 /* Write packet size. */
380 stream_putw_at(s, 0, stream_get_endp(s));
718e3744 381
d62a17ae 382 client->connected_rt_add_cnt++;
383 return zebra_server_send_message(client);
718e3744 384}
385
d62a17ae 386static int zsend_interface_nbr_address(int cmd, struct zserv *client,
387 struct interface *ifp,
388 struct nbr_connected *ifc)
a80beece 389{
d62a17ae 390 int blen;
391 struct stream *s;
392 struct prefix *p;
a80beece 393
d62a17ae 394 s = client->obuf;
395 stream_reset(s);
a80beece 396
d62a17ae 397 zserv_create_header(s, cmd, ifp->vrf_id);
398 stream_putl(s, ifp->ifindex);
a80beece 399
d62a17ae 400 /* Prefix information. */
401 p = ifc->address;
402 stream_putc(s, p->family);
403 blen = prefix_blen(p);
404 stream_put(s, &p->u.prefix, blen);
a80beece 405
d62a17ae 406 /*
407 * XXX gnu version does not send prefixlen for
408 * ZEBRA_INTERFACE_ADDRESS_DELETE
409 * but zebra_interface_address_delete_read() in the gnu version
410 * expects to find it
411 */
412 stream_putc(s, p->prefixlen);
a80beece 413
d62a17ae 414 /* Write packet size. */
415 stream_putw_at(s, 0, stream_get_endp(s));
a80beece 416
d62a17ae 417 return zebra_server_send_message(client);
a80beece
DS
418}
419
420/* Interface address addition. */
d62a17ae 421static void zebra_interface_nbr_address_add_update(struct interface *ifp,
422 struct nbr_connected *ifc)
423{
424 struct listnode *node, *nnode;
425 struct zserv *client;
426 struct prefix *p;
427
428 if (IS_ZEBRA_DEBUG_EVENT) {
429 char buf[INET6_ADDRSTRLEN];
430
431 p = ifc->address;
432 zlog_debug(
433 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
434 inet_ntop(p->family, &p->u.prefix, buf,
435 INET6_ADDRSTRLEN),
436 p->prefixlen, ifc->ifp->name);
437 }
a80beece 438
d62a17ae 439 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
440 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
441 client, ifp, ifc);
a80beece
DS
442}
443
444/* Interface address deletion. */
d62a17ae 445static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
446 struct nbr_connected *ifc)
447{
448 struct listnode *node, *nnode;
449 struct zserv *client;
450 struct prefix *p;
451
452 if (IS_ZEBRA_DEBUG_EVENT) {
453 char buf[INET6_ADDRSTRLEN];
454
455 p = ifc->address;
456 zlog_debug(
457 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
458 inet_ntop(p->family, &p->u.prefix, buf,
459 INET6_ADDRSTRLEN),
460 p->prefixlen, ifc->ifp->name);
461 }
a80beece 462
d62a17ae 463 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
464 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
465 client, ifp, ifc);
a80beece
DS
466}
467
c8e264b6 468/* Send addresses on interface to client */
d62a17ae 469int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
c8e264b6 470{
d62a17ae 471 struct listnode *cnode, *cnnode;
472 struct connected *c;
473 struct nbr_connected *nc;
c8e264b6 474
d62a17ae 475 /* Send interface addresses. */
476 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
477 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
478 continue;
c8e264b6 479
d62a17ae 480 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
481 ifp, c)
482 < 0)
483 return -1;
484 }
c8e264b6 485
d62a17ae 486 /* Send interface neighbors. */
487 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
488 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
489 client, ifp, nc)
490 < 0)
491 return -1;
492 }
c8e264b6 493
d62a17ae 494 return 0;
c8e264b6 495}
496
497/* Notify client about interface moving from one VRF to another.
498 * Whether client is interested in old and new VRF is checked by caller.
499 */
d62a17ae 500int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
501 vrf_id_t vrf_id)
c8e264b6 502{
d62a17ae 503 struct stream *s;
c8e264b6 504
d62a17ae 505 s = client->obuf;
506 stream_reset(s);
c8e264b6 507
d62a17ae 508 zserv_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
c8e264b6 509
d62a17ae 510 /* Fill in the ifIndex of the interface and its new VRF (id) */
511 stream_putl(s, ifp->ifindex);
512 stream_putw(s, vrf_id);
c8e264b6 513
d62a17ae 514 /* Write packet size. */
515 stream_putw_at(s, 0, stream_get_endp(s));
c8e264b6 516
d62a17ae 517 client->if_vrfchg_cnt++;
518 return zebra_server_send_message(client);
c8e264b6 519}
520
1d20ccf3 521/* Add new nbr connected IPv6 address */
d62a17ae 522void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
a80beece 523{
d62a17ae 524 struct nbr_connected *ifc;
525 struct prefix p;
a80beece 526
d62a17ae 527 p.family = AF_INET6;
528 IPV6_ADDR_COPY(&p.u.prefix, address);
529 p.prefixlen = IPV6_MAX_PREFIXLEN;
a80beece 530
d62a17ae 531 if (!(ifc = listnode_head(ifp->nbr_connected))) {
532 /* new addition */
533 ifc = nbr_connected_new();
534 ifc->address = prefix_new();
535 ifc->ifp = ifp;
536 listnode_add(ifp->nbr_connected, ifc);
537 }
a80beece 538
d62a17ae 539 prefix_copy(ifc->address, &p);
a80beece 540
d62a17ae 541 zebra_interface_nbr_address_add_update(ifp, ifc);
5c610faf 542
d62a17ae 543 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
a80beece
DS
544}
545
d62a17ae 546void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
a80beece 547{
d62a17ae 548 struct nbr_connected *ifc;
549 struct prefix p;
a80beece 550
d62a17ae 551 p.family = AF_INET6;
552 IPV6_ADDR_COPY(&p.u.prefix, address);
553 p.prefixlen = IPV6_MAX_PREFIXLEN;
a80beece 554
d62a17ae 555 ifc = nbr_connected_check(ifp, &p);
556 if (!ifc)
557 return;
a80beece 558
d62a17ae 559 listnode_delete(ifp->nbr_connected, ifc);
a80beece 560
d62a17ae 561 zebra_interface_nbr_address_delete_update(ifp, ifc);
a80beece 562
d62a17ae 563 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
5c610faf 564
d62a17ae 565 nbr_connected_free(ifc);
a80beece
DS
566}
567
b9df2d25 568/*
569 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
570 * ZEBRA_INTERFACE_DOWN.
571 *
572 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
573 * the clients in one of 2 situations:
574 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
575 * - a vty command modifying the bandwidth of an interface is received.
576 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
577 */
d62a17ae 578int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
718e3744 579{
d62a17ae 580 struct stream *s;
718e3744 581
d62a17ae 582 s = client->obuf;
583 stream_reset(s);
718e3744 584
d62a17ae 585 zserv_create_header(s, cmd, ifp->vrf_id);
586 zserv_encode_interface(s, ifp);
718e3744 587
d62a17ae 588 if (cmd == ZEBRA_INTERFACE_UP)
589 client->ifup_cnt++;
590 else
591 client->ifdown_cnt++;
04b02fda 592
d62a17ae 593 return zebra_server_send_message(client);
718e3744 594}
595
74489921 596int zsend_redistribute_route(int cmd, struct zserv *client, struct prefix *p,
d62a17ae 597 struct prefix *src_p, struct route_entry *re)
598{
74489921
RW
599 struct zapi_route api;
600 struct zapi_nexthop *api_nh;
d62a17ae 601 struct nexthop *nexthop;
74489921 602 int count = 0;
5048fe14 603
74489921 604 memset(&api, 0, sizeof(api));
3d536c7c 605 api.vrf_id = re->vrf_id;
74489921
RW
606 api.type = re->type;
607 api.instance = re->instance;
608 api.flags = re->flags;
d62a17ae 609
610 /* Prefix. */
74489921 611 api.prefix = *p;
d62a17ae 612 if (src_p) {
74489921
RW
613 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
614 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
078430f6 615 }
d62a17ae 616
74489921
RW
617 /* Nexthops. */
618 if (re->nexthop_active_num) {
619 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
620 api.nexthop_num = re->nexthop_active_num;
621 }
d62a17ae 622 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next) {
74489921
RW
623 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
624 continue;
d62a17ae 625
74489921
RW
626 api_nh = &api.nexthops[count];
627 api_nh->type = nexthop->type;
628 switch (nexthop->type) {
629 case NEXTHOP_TYPE_BLACKHOLE:
94758e66 630 api_nh->bh_type = nexthop->bh_type;
74489921
RW
631 break;
632 case NEXTHOP_TYPE_IPV4:
633 api_nh->gate.ipv4 = nexthop->gate.ipv4;
634 break;
635 case NEXTHOP_TYPE_IPV4_IFINDEX:
636 api_nh->gate.ipv4 = nexthop->gate.ipv4;
637 api_nh->ifindex = nexthop->ifindex;
638 break;
639 case NEXTHOP_TYPE_IFINDEX:
640 api_nh->ifindex = nexthop->ifindex;
641 break;
642 case NEXTHOP_TYPE_IPV6:
643 api_nh->gate.ipv6 = nexthop->gate.ipv6;
644 break;
645 case NEXTHOP_TYPE_IPV6_IFINDEX:
646 api_nh->gate.ipv6 = nexthop->gate.ipv6;
647 api_nh->ifindex = nexthop->ifindex;
d62a17ae 648 }
74489921 649 count++;
078430f6 650 }
d62a17ae 651
74489921
RW
652 /* Attributes. */
653 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
654 api.distance = re->distance;
655 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
656 api.metric = re->metric;
d62a17ae 657 if (re->tag) {
74489921
RW
658 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
659 api.tag = re->tag;
078430f6 660 }
74489921
RW
661 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
662 api.mtu = re->mtu;
078430f6 663
74489921
RW
664 /* Encode route and send. */
665 if (zapi_route_encode(cmd, client->obuf, &api) < 0)
666 return -1;
d62a17ae 667 return zebra_server_send_message(client);
668}
669
670static int zsend_write_nexthop(struct stream *s, struct nexthop *nexthop)
671{
672 stream_putc(s, nexthop->type);
673 switch (nexthop->type) {
674 case NEXTHOP_TYPE_IPV4:
675 case NEXTHOP_TYPE_IPV4_IFINDEX:
676 stream_put_in_addr(s, &nexthop->gate.ipv4);
677 stream_putl(s, nexthop->ifindex);
678 break;
679 case NEXTHOP_TYPE_IPV6:
680 stream_put(s, &nexthop->gate.ipv6, 16);
681 break;
682 case NEXTHOP_TYPE_IPV6_IFINDEX:
683 stream_put(s, &nexthop->gate.ipv6, 16);
684 stream_putl(s, nexthop->ifindex);
685 break;
686 case NEXTHOP_TYPE_IFINDEX:
687 stream_putl(s, nexthop->ifindex);
688 break;
689 default:
690 /* do nothing */
691 break;
692 }
693 return 1;
fb018d25
DS
694}
695
696/* Nexthop register */
e4bd522a 697static int zserv_rnh_register(struct zserv *client, u_short length,
d62a17ae 698 rnh_type_t type, struct zebra_vrf *zvrf)
699{
700 struct rnh *rnh;
701 struct stream *s;
702 struct prefix p;
703 u_short l = 0;
704 u_char flags = 0;
705
706 if (IS_ZEBRA_DEBUG_NHT)
707 zlog_debug(
708 "rnh_register msg from client %s: length=%d, type=%s\n",
709 zebra_route_string(client->proto), length,
710 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route");
711
712 s = client->ibuf;
713
714 client->nh_reg_time = monotime(NULL);
715
716 while (l < length) {
ec93aa12
DS
717 STREAM_GETC(s, flags);
718 STREAM_GETW(s, p.family);
719 STREAM_GETC(s, p.prefixlen);
d62a17ae 720 l += 4;
721 if (p.family == AF_INET) {
ec93aa12
DS
722 if (p.prefixlen > IPV4_MAX_BITLEN) {
723 zlog_warn("%s: Specified prefix length %d is too large for a v4 address",
724 __PRETTY_FUNCTION__, p.prefixlen);
725 return -1;
726 }
727 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 728 l += IPV4_MAX_BYTELEN;
729 } else if (p.family == AF_INET6) {
ec93aa12
DS
730 if (p.prefixlen > IPV6_MAX_BITLEN) {
731 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
732 __PRETTY_FUNCTION__, p.prefixlen);
733 return -1;
734 }
735 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
d62a17ae 736 l += IPV6_MAX_BYTELEN;
737 } else {
738 zlog_err(
739 "rnh_register: Received unknown family type %d\n",
740 p.family);
741 return -1;
742 }
743 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type);
744 if (type == RNH_NEXTHOP_TYPE) {
745 if (flags
746 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
747 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
748 else if (!flags
749 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
750 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
751 } else if (type == RNH_IMPORT_CHECK_TYPE) {
752 if (flags
753 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
754 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
9d303b37
DL
755 else if (!flags && CHECK_FLAG(rnh->flags,
756 ZEBRA_NHT_EXACT_MATCH))
d62a17ae 757 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
758 }
759
760 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
761 /* Anything not AF_INET/INET6 has been filtered out above */
762 zebra_evaluate_rnh(zvrf_id(zvrf), p.family, 1, type, &p);
078430f6 763 }
ec93aa12
DS
764
765stream_failure:
d62a17ae 766 return 0;
767}
768
769/* Nexthop register */
e4bd522a 770static int zserv_rnh_unregister(struct zserv *client, u_short length,
d62a17ae 771 rnh_type_t type, struct zebra_vrf *zvrf)
772{
773 struct rnh *rnh;
774 struct stream *s;
775 struct prefix p;
776 u_short l = 0;
777
778 if (IS_ZEBRA_DEBUG_NHT)
779 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
780 zebra_route_string(client->proto), length);
781
782 s = client->ibuf;
783
784 while (l < length) {
f49e0f01
DS
785 uint8_t flags;
786
787 STREAM_GETC(s, flags);
788 if (flags != 0)
789 goto stream_failure;
ec93aa12 790
ec93aa12
DS
791 STREAM_GETW(s, p.family);
792 STREAM_GETC(s, p.prefixlen);
d62a17ae 793 l += 4;
794 if (p.family == AF_INET) {
ec93aa12
DS
795 if (p.prefixlen > IPV4_MAX_BITLEN) {
796 zlog_warn("%s: Specified prefix length %d is to large for a v4 address",
797 __PRETTY_FUNCTION__, p.prefixlen);
798 return -1;
799 }
800 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 801 l += IPV4_MAX_BYTELEN;
802 } else if (p.family == AF_INET6) {
ec93aa12
DS
803 if (p.prefixlen > IPV6_MAX_BITLEN) {
804 zlog_warn("%s: Specified prefix length %d is to large for a v6 address",
805 __PRETTY_FUNCTION__, p.prefixlen);
806 return -1;
807 }
808 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
d62a17ae 809 l += IPV6_MAX_BYTELEN;
810 } else {
811 zlog_err(
812 "rnh_register: Received unknown family type %d\n",
813 p.family);
814 return -1;
815 }
816 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
817 if (rnh) {
818 client->nh_dereg_time = monotime(NULL);
819 zebra_remove_rnh_client(rnh, client, type);
820 }
04b02fda 821 }
ec93aa12 822stream_failure:
d62a17ae 823 return 0;
fb018d25
DS
824}
825
b5ab78e6 826#define ZEBRA_MIN_FEC_LENGTH 5
7abc04e6 827
5aba114a 828/* FEC register */
e4bd522a 829static int zserv_fec_register(struct zserv *client, u_short length)
d62a17ae 830{
831 struct stream *s;
832 struct zebra_vrf *zvrf;
833 u_short l = 0;
834 struct prefix p;
835 u_int16_t flags;
836 u_int32_t label_index = MPLS_INVALID_LABEL_INDEX;
837
838 s = client->ibuf;
839 zvrf = vrf_info_lookup(VRF_DEFAULT);
840 if (!zvrf)
841 return 0; // unexpected
842
843 /*
844 * The minimum amount of data that can be sent for one fec
845 * registration
846 */
847 if (length < ZEBRA_MIN_FEC_LENGTH) {
848 zlog_err(
849 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
850 length);
851 return -1;
852 }
853
854 while (l < length) {
ec93aa12 855 STREAM_GETW(s, flags);
ad4527eb 856 memset(&p, 0, sizeof(p));
ec93aa12 857 STREAM_GETW(s, p.family);
d62a17ae 858 if (p.family != AF_INET && p.family != AF_INET6) {
859 zlog_err(
860 "fec_register: Received unknown family type %d\n",
861 p.family);
862 return -1;
863 }
ec93aa12
DS
864 STREAM_GETC(s, p.prefixlen);
865 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) ||
866 (p.family == AF_INET6 && p.prefixlen > IPV6_MAX_BITLEN)) {
867 zlog_warn("%s: Specified prefix length: %d is to long for %d",
868 __PRETTY_FUNCTION__, p.prefixlen, p.family);
869 return -1;
870 }
d62a17ae 871 l += 5;
ec93aa12 872 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
d62a17ae 873 l += PSIZE(p.prefixlen);
874 if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
ec93aa12 875 STREAM_GETL(s, label_index);
d62a17ae 876 l += 4;
877 } else
878 label_index = MPLS_INVALID_LABEL_INDEX;
879 zebra_mpls_fec_register(zvrf, &p, label_index, client);
880 }
881
ec93aa12 882stream_failure:
d62a17ae 883 return 0;
5aba114a
DS
884}
885
886/* FEC unregister */
e4bd522a 887static int zserv_fec_unregister(struct zserv *client, u_short length)
d62a17ae 888{
889 struct stream *s;
890 struct zebra_vrf *zvrf;
891 u_short l = 0;
892 struct prefix p;
ec93aa12 893 uint16_t flags;
d62a17ae 894
895 s = client->ibuf;
896 zvrf = vrf_info_lookup(VRF_DEFAULT);
897 if (!zvrf)
898 return 0; // unexpected
899
900 /*
901 * The minimum amount of data that can be sent for one
902 * fec unregistration
903 */
904 if (length < ZEBRA_MIN_FEC_LENGTH) {
905 zlog_err(
906 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
907 length);
908 return -1;
909 }
910
911 while (l < length) {
ec93aa12 912 STREAM_GETW(s, flags);
e0b84ba1
DS
913 if (flags != 0)
914 goto stream_failure;
915
ad4527eb 916 memset(&p, 0, sizeof(p));
ec93aa12 917 STREAM_GETW(s, p.family);
d62a17ae 918 if (p.family != AF_INET && p.family != AF_INET6) {
919 zlog_err(
920 "fec_unregister: Received unknown family type %d\n",
921 p.family);
922 return -1;
923 }
ec93aa12
DS
924 STREAM_GETC(s, p.prefixlen);
925 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) ||
926 (p.family == AF_INET6 && p.prefixlen > IPV6_MAX_BITLEN)) {
927 zlog_warn("%s: Received prefix length %d which is greater than %d can support",
928 __PRETTY_FUNCTION__, p.prefixlen, p.family);
929 return -1;
930 }
d62a17ae 931 l += 5;
ec93aa12 932 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
d62a17ae 933 l += PSIZE(p.prefixlen);
934 zebra_mpls_fec_unregister(zvrf, &p, client);
935 }
936
ec93aa12 937stream_failure:
d62a17ae 938 return 0;
5aba114a
DS
939}
940
6f61a5a3
EM
941/*
942 Modified version of zsend_ipv4_nexthop_lookup():
943 Query unicast rib if nexthop is not found on mrib.
944 Returns both route metric and protocol distance.
945*/
d62a17ae 946static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
947 struct in_addr addr,
948 struct route_entry *re,
949 struct zebra_vrf *zvrf)
950{
951 struct stream *s;
952 unsigned long nump;
953 u_char num;
954 struct nexthop *nexthop;
955
956 /* Get output stream. */
957 s = client->obuf;
958 stream_reset(s);
959
960 /* Fill in result. */
961 zserv_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
962 stream_put_in_addr(s, &addr);
963
964 if (re) {
965 stream_putc(s, re->distance);
966 stream_putl(s, re->metric);
967 num = 0;
968 nump = stream_get_endp(
969 s); /* remember position for nexthop_num */
970 stream_putc(s, 0); /* reserve room for nexthop_num */
971 /* Only non-recursive routes are elegible to resolve the nexthop
972 * we
973 * are looking up. Therefore, we will just iterate over the top
974 * chain of nexthops. */
975 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
976 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
977 num += zsend_write_nexthop(s, nexthop);
978
979 stream_putc_at(s, nump, num); /* store nexthop_num */
980 } else {
981 stream_putc(s, 0); /* distance */
982 stream_putl(s, 0); /* metric */
983 stream_putc(s, 0); /* nexthop_num */
984 }
985
986 stream_putw_at(s, 0, stream_get_endp(s));
987
988 return zebra_server_send_message(client);
6f61a5a3
EM
989}
990
019a82cb
DS
991int zsend_route_notify_owner(u_char proto, u_short instance,
992 vrf_id_t vrf_id, struct prefix *p,
7ea7b86e
DS
993 enum zapi_route_notify_owner note)
994{
995 struct zserv *client;
996 struct stream *s;
997 uint8_t blen;
998
019a82cb 999 client = zebra_find_client(proto, instance);
e1a1880d
DS
1000 if (!client || !client->notify_owner) {
1001 if (IS_ZEBRA_DEBUG_PACKET) {
1002 char buff[PREFIX_STRLEN];
1003
1004 zlog_debug("Not Notifying Owner: %u about prefix %s",
1005 proto, prefix2str(p, buff, sizeof(buff)));
1006 }
1007 return 0;
7ea7b86e
DS
1008 }
1009
1010 s = client->obuf;
1011 stream_reset(s);
1012
1013 zserv_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
1014
1015 stream_put(s, &note, sizeof(note));
1016
1017 stream_putc(s, p->family);
1018
1019 blen = prefix_blen(p);
1020 stream_putc(s, p->prefixlen);
1021 stream_put(s, &p->u.prefix, blen);
1022
1023 stream_putw_at(s, 0, stream_get_endp(s));
1024
1025 return zebra_server_send_message(client);
1026}
1027
18a6dce6 1028/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
d62a17ae 1029int zsend_router_id_update(struct zserv *client, struct prefix *p,
1030 vrf_id_t vrf_id)
18a6dce6 1031{
d62a17ae 1032 struct stream *s;
1033 int blen;
18a6dce6 1034
d62a17ae 1035 /* Check this client need interface information. */
1036 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
1037 return 0;
18a6dce6 1038
d62a17ae 1039 s = client->obuf;
1040 stream_reset(s);
18a6dce6 1041
d62a17ae 1042 /* Message type. */
1043 zserv_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
18a6dce6 1044
d62a17ae 1045 /* Prefix information. */
1046 stream_putc(s, p->family);
1047 blen = prefix_blen(p);
1048 stream_put(s, &p->u.prefix, blen);
1049 stream_putc(s, p->prefixlen);
18a6dce6 1050
d62a17ae 1051 /* Write packet size. */
1052 stream_putw_at(s, 0, stream_get_endp(s));
18a6dce6 1053
d62a17ae 1054 return zebra_server_send_message(client);
18a6dce6 1055}
6b0655a2 1056
6833ae01 1057/*
1058 * Function used by Zebra to send a PW status update to LDP daemon
1059 */
1060int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
1061{
1062 struct stream *s;
1063
1064 s = client->obuf;
1065 stream_reset(s);
1066
1067 zserv_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
1068 stream_write(s, pw->ifname, IF_NAMESIZE);
1069 stream_putl(s, pw->ifindex);
1070 stream_putl(s, pw->status);
1071
1072 /* Put length at the first point of the stream. */
1073 stream_putw_at(s, 0, stream_get_endp(s));
1074
1075 return zebra_server_send_message(client);
1076}
1077
718e3744 1078/* Register zebra server interface information. Send current all
1079 interface and address information. */
d62a17ae 1080static int zread_interface_add(struct zserv *client, u_short length,
1081 struct zebra_vrf *zvrf)
718e3744 1082{
d62a17ae 1083 struct vrf *vrf;
d62a17ae 1084 struct interface *ifp;
718e3744 1085
d62a17ae 1086 /* Interface information is needed. */
1087 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
718e3744 1088
a2addae8 1089 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
451fda4f 1090 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 1091 /* Skip pseudo interface. */
1092 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1093 continue;
718e3744 1094
d62a17ae 1095 if (zsend_interface_add(client, ifp) < 0)
1096 return -1;
718e3744 1097
d62a17ae 1098 if (zsend_interface_addresses(client, ifp) < 0)
1099 return -1;
1100 }
1101 }
1102 return 0;
718e3744 1103}
1104
1105/* Unregister zebra server interface information. */
d62a17ae 1106static int zread_interface_delete(struct zserv *client, u_short length,
1107 struct zebra_vrf *zvrf)
718e3744 1108{
d62a17ae 1109 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1110 return 0;
718e3744 1111}
1112
d62a17ae 1113void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1114 const unsigned int nexthop_num)
6878b9db 1115{
d62a17ae 1116 if (nexthop_num > multipath_num) {
1117 char buff[PREFIX2STR_BUFFER];
1118 prefix2str(p, buff, sizeof(buff));
1119 zlog_warn(
1120 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1121 caller, buff, nexthop_num, multipath_num);
1122 }
6878b9db
DS
1123}
1124
0e51b4a3
RW
1125static int zread_route_add(struct zserv *client, u_short length,
1126 struct zebra_vrf *zvrf)
1127{
1128 struct stream *s;
1129 struct zapi_route api;
1130 struct zapi_nexthop *api_nh;
1131 afi_t afi;
1132 struct prefix_ipv6 *src_p = NULL;
1133 struct route_entry *re;
98ca91e9 1134 struct nexthop *nexthop = NULL;
0e51b4a3
RW
1135 int i, ret;
1136
1137 s = client->ibuf;
1138 if (zapi_route_decode(s, &api) < 0)
1139 return -1;
1140
1141 /* Allocate new route. */
1142 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1143 re->type = api.type;
1144 re->instance = api.instance;
1145 re->flags = api.flags;
1146 re->uptime = time(NULL);
1147 re->vrf_id = zvrf_id(zvrf);
1148 re->table = zvrf->table_id;
1149
1150 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1151 for (i = 0; i < api.nexthop_num; i++) {
1152 api_nh = &api.nexthops[i];
1153
1154 switch (api_nh->type) {
1155 case NEXTHOP_TYPE_IFINDEX:
ec93aa12 1156 nexthop = route_entry_nexthop_ifindex_add(
0e51b4a3
RW
1157 re, api_nh->ifindex);
1158 break;
1159 case NEXTHOP_TYPE_IPV4:
1160 nexthop = route_entry_nexthop_ipv4_add(
1161 re, &api_nh->gate.ipv4, NULL);
1162 break;
1163 case NEXTHOP_TYPE_IPV4_IFINDEX:
1164 nexthop = route_entry_nexthop_ipv4_ifindex_add(
1165 re, &api_nh->gate.ipv4, NULL,
1166 api_nh->ifindex);
1167 break;
1168 case NEXTHOP_TYPE_IPV6:
1169 nexthop = route_entry_nexthop_ipv6_add(
1170 re, &api_nh->gate.ipv6);
1171 break;
1172 case NEXTHOP_TYPE_IPV6_IFINDEX:
1173 nexthop = route_entry_nexthop_ipv6_ifindex_add(
1174 re, &api_nh->gate.ipv6,
1175 api_nh->ifindex);
1176 break;
1177 case NEXTHOP_TYPE_BLACKHOLE:
ec93aa12 1178 nexthop = route_entry_nexthop_blackhole_add(
60466a63 1179 re, api_nh->bh_type);
0e51b4a3
RW
1180 break;
1181 }
1182
ec93aa12
DS
1183 if (!nexthop) {
1184 zlog_warn("%s: Nexthops Specified: %d but we failed to properly create one",
1185 __PRETTY_FUNCTION__, api.nexthop_num);
1186 nexthops_free(re->nexthop);
1187 XFREE(MTYPE_RE, re);
1188 return -1;
1189 }
0e51b4a3
RW
1190 /* MPLS labels for BGP-LU or Segment Routing */
1191 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1192 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1193 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1194 enum lsp_types_t label_type;
1195
1196 label_type =
1197 lsp_type_from_re_type(client->proto);
52dd3aa4
RW
1198 nexthop_add_labels(nexthop, label_type,
1199 api_nh->label_num,
1200 &api_nh->labels[0]);
0e51b4a3
RW
1201 }
1202 }
1203 }
1204
1205 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1206 re->distance = api.distance;
1207 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1208 re->metric = api.metric;
1209 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1210 re->tag = api.tag;
1211 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1212 re->mtu = api.mtu;
1213
1214 afi = family2afi(api.prefix.family);
ec93aa12
DS
1215 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1216 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1217 __PRETTY_FUNCTION__);
1218 nexthops_free(re->nexthop);
1219 XFREE(MTYPE_RE, re);
1220 return -1;
1221 }
0e51b4a3
RW
1222 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1223 src_p = &api.src_prefix;
1224
1225 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1226
1227 /* Stats */
1228 switch (api.prefix.family) {
1229 case AF_INET:
1230 if (ret > 0)
1231 client->v4_route_add_cnt++;
1232 else if (ret < 0)
1233 client->v4_route_upd8_cnt++;
1234 break;
1235 case AF_INET6:
1236 if (ret > 0)
1237 client->v6_route_add_cnt++;
1238 else if (ret < 0)
1239 client->v6_route_upd8_cnt++;
1240 break;
1241 }
1242
1243 return 0;
1244}
1245
1246static int zread_route_del(struct zserv *client, u_short length,
1247 struct zebra_vrf *zvrf)
1248{
1249 struct stream *s;
1250 struct zapi_route api;
1251 afi_t afi;
1252 struct prefix_ipv6 *src_p = NULL;
1253
1254 s = client->ibuf;
1255 if (zapi_route_decode(s, &api) < 0)
1256 return -1;
1257
1258 afi = family2afi(api.prefix.family);
ec93aa12
DS
1259 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1260 zlog_warn("%s: Received a src prefix while afi is not v6",
1261 __PRETTY_FUNCTION__);
1262 return -1;
1263 }
0e51b4a3
RW
1264 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1265 src_p = &api.src_prefix;
1266
1267 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
a8309422 1268 api.flags, &api.prefix, src_p, NULL, zvrf->table_id,
5dfeba19 1269 api.metric, false);
0e51b4a3
RW
1270
1271 /* Stats */
1272 switch (api.prefix.family) {
1273 case AF_INET:
1274 client->v4_route_del_cnt++;
1275 break;
1276 case AF_INET6:
1277 client->v6_route_del_cnt++;
1278 break;
1279 }
1280
1281 return 0;
1282}
1283
718e3744 1284/* This function support multiple nexthop. */
d62a17ae 1285/*
f0f77c9a 1286 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
d62a17ae 1287 * add kernel route.
b9df2d25 1288 */
d62a17ae 1289static int zread_ipv4_add(struct zserv *client, u_short length,
1290 struct zebra_vrf *zvrf)
1291{
1292 int i;
1293 struct route_entry *re;
1294 struct prefix p;
1295 u_char message;
1296 struct in_addr nhop_addr;
1297 u_char nexthop_num;
1298 u_char nexthop_type;
1299 struct stream *s;
1300 ifindex_t ifindex;
1301 safi_t safi;
1302 int ret;
a97986ff 1303 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1304 mpls_label_t label;
1305 struct nexthop *nexthop;
a8309422 1306 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1307
1308 /* Get input stream. */
1309 s = client->ibuf;
1310
1311 /* Allocate new re. */
1312 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1313
1314 /* Type, flags, message. */
ec93aa12
DS
1315 STREAM_GETC(s, re->type);
1316 if (re->type > ZEBRA_ROUTE_MAX) {
1317 zlog_warn("%s: Specified route type %d is not a legal value\n",
1318 __PRETTY_FUNCTION__, re->type);
1319 XFREE(MTYPE_RE, re);
1320 return -1;
1321 }
1322 STREAM_GETW(s, re->instance);
1323 STREAM_GETL(s, re->flags);
1324 STREAM_GETC(s, message);
1325 STREAM_GETW(s, safi);
d62a17ae 1326 re->uptime = time(NULL);
1327
1328 /* IPv4 prefix. */
1329 memset(&p, 0, sizeof(struct prefix_ipv4));
1330 p.family = AF_INET;
ec93aa12
DS
1331 STREAM_GETC(s, p.prefixlen);
1332 if (p.prefixlen > IPV4_MAX_BITLEN) {
1333 zlog_warn("%s: Specified prefix length %d is greater than what v4 can be",
1334 __PRETTY_FUNCTION__, p.prefixlen);
1335 XFREE(MTYPE_RE, re);
1336 return -1;
1337 }
1338 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1339
1340 /* VRF ID */
1341 re->vrf_id = zvrf_id(zvrf);
1342
1343 /* Nexthop parse. */
1344 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
ec93aa12 1345 STREAM_GETC(s, nexthop_num);
d62a17ae 1346 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1347 nexthop_num);
1348
81c11e3f
RW
1349 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1350 label_type = lsp_type_from_re_type(client->proto);
1351
d62a17ae 1352 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1353 STREAM_GETC(s, nexthop_type);
d62a17ae 1354
1355 switch (nexthop_type) {
1356 case NEXTHOP_TYPE_IFINDEX:
ec93aa12 1357 STREAM_GETL(s, ifindex);
d62a17ae 1358 route_entry_nexthop_ifindex_add(re, ifindex);
1359 break;
1360 case NEXTHOP_TYPE_IPV4:
ec93aa12
DS
1361 STREAM_GET(&nhop_addr.s_addr, s,
1362 IPV4_MAX_BYTELEN);
d62a17ae 1363 nexthop = route_entry_nexthop_ipv4_add(
1364 re, &nhop_addr, NULL);
1365 /* For labeled-unicast, each nexthop is followed
1366 * by label. */
1367 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
ec93aa12 1368 STREAM_GETL(s, label);
81c11e3f
RW
1369 nexthop_add_labels(nexthop, label_type,
1370 1, &label);
d62a17ae 1371 }
1372 break;
1373 case NEXTHOP_TYPE_IPV4_IFINDEX:
ec93aa12
DS
1374 STREAM_GET(&nhop_addr.s_addr, s,
1375 IPV4_MAX_BYTELEN);
1376 STREAM_GETL(s, ifindex);
d62a17ae 1377 route_entry_nexthop_ipv4_ifindex_add(
1378 re, &nhop_addr, NULL, ifindex);
1379 break;
1380 case NEXTHOP_TYPE_IPV6:
ec93aa12
DS
1381 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1382 __PRETTY_FUNCTION__);
1383 nexthops_free(re->nexthop);
1384 XFREE(MTYPE_RE, re);
1385 return -1;
d62a17ae 1386 break;
1387 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1388 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1389 break;
ec93aa12
DS
1390 default:
1391 zlog_warn("%s: Specified nexthop type: %d does not exist",
1392 __PRETTY_FUNCTION__, nexthop_type);
1393 nexthops_free(re->nexthop);
1394 XFREE(MTYPE_RE, re);
1395 return -1;
d62a17ae 1396 }
1397 }
718e3744 1398 }
718e3744 1399
d62a17ae 1400 /* Distance. */
1401 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1402 STREAM_GETC(s, re->distance);
718e3744 1403
d62a17ae 1404 /* Metric. */
1405 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1406 STREAM_GETL(s, re->metric);
0d9551dc 1407
d62a17ae 1408 /* Tag */
1409 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1410 STREAM_GETL(s, re->tag);
d62a17ae 1411 else
1412 re->tag = 0;
c50ca33a 1413
d62a17ae 1414 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1415 STREAM_GETL(s, re->mtu);
d62a17ae 1416 else
1417 re->mtu = 0;
1418
1419 /* Table */
1420 re->table = zvrf->table_id;
12f6fb97 1421
d62a17ae 1422 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
04b02fda 1423
d62a17ae 1424 /* Stats */
1425 if (ret > 0)
1426 client->v4_route_add_cnt++;
1427 else if (ret < 0)
1428 client->v4_route_upd8_cnt++;
ec93aa12 1429
d62a17ae 1430 return 0;
ec93aa12
DS
1431
1432stream_failure:
1433 nexthops_free(re->nexthop);
1434 XFREE(MTYPE_RE, re);
1435 return -1;
718e3744 1436}
1437
1438/* Zebra server IPv4 prefix delete function. */
d62a17ae 1439static int zread_ipv4_delete(struct zserv *client, u_short length,
1440 struct zebra_vrf *zvrf)
1441{
d62a17ae 1442 struct stream *s;
1443 struct zapi_ipv4 api;
d62a17ae 1444 struct prefix p;
d62a17ae 1445 u_int32_t table_id;
1446
1447 s = client->ibuf;
d62a17ae 1448
1449 /* Type, flags, message. */
ec93aa12
DS
1450 STREAM_GETC(s, api.type);
1451 STREAM_GETW(s, api.instance);
1452 STREAM_GETL(s, api.flags);
1453 STREAM_GETC(s, api.message);
1454 STREAM_GETW(s, api.safi);
d62a17ae 1455
1456 /* IPv4 prefix. */
1457 memset(&p, 0, sizeof(struct prefix));
1458 p.family = AF_INET;
ec93aa12
DS
1459 STREAM_GETC(s, p.prefixlen);
1460 if (p.prefixlen > IPV4_MAX_BITLEN) {
1461 zlog_warn("%s: Passed in prefixlen %d is impossible",
1462 __PRETTY_FUNCTION__, p.prefixlen);
1463 return -1;
1464 }
1465 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1466
d62a17ae 1467 table_id = zvrf->table_id;
1468
1469 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
5dfeba19 1470 api.flags, &p, NULL, NULL, table_id, 0, false);
d62a17ae 1471 client->v4_route_del_cnt++;
ec93aa12
DS
1472
1473stream_failure:
d62a17ae 1474 return 0;
718e3744 1475}
1476
6f61a5a3 1477/* MRIB Nexthop lookup for IPv4. */
d62a17ae 1478static int zread_ipv4_nexthop_lookup_mrib(struct zserv *client, u_short length,
1479 struct zebra_vrf *zvrf)
6f61a5a3 1480{
d62a17ae 1481 struct in_addr addr;
1482 struct route_entry *re;
6f61a5a3 1483
ec93aa12 1484 STREAM_GET(&addr.s_addr, client->ibuf, IPV4_MAX_BYTELEN);
d62a17ae 1485 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1486 return zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
ec93aa12
DS
1487
1488stream_failure:
1489 return -1;
6f61a5a3
EM
1490}
1491
8a92a8a0 1492/* Zebra server IPv6 prefix add function. */
d62a17ae 1493static int zread_ipv4_route_ipv6_nexthop_add(struct zserv *client,
1494 u_short length,
1495 struct zebra_vrf *zvrf)
1496{
1497 unsigned int i;
1498 struct stream *s;
1499 struct in6_addr nhop_addr;
1500 struct route_entry *re;
1501 u_char message;
1502 u_char nexthop_num;
1503 u_char nexthop_type;
1504 struct prefix p;
1505 safi_t safi;
1506 static struct in6_addr nexthops[MULTIPATH_NUM];
1507 static unsigned int ifindices[MULTIPATH_NUM];
1508 int ret;
1509 static mpls_label_t labels[MULTIPATH_NUM];
98ca91e9 1510 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1511 mpls_label_t label;
1512 struct nexthop *nexthop;
a8309422 1513 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1514
1515 /* Get input stream. */
1516 s = client->ibuf;
1517
1518 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1519
1520 /* Allocate new re. */
1521 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1522
1523 /* Type, flags, message. */
ec93aa12
DS
1524 STREAM_GETC(s, re->type);
1525 if (re->type > ZEBRA_ROUTE_MAX) {
1526 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1527 __PRETTY_FUNCTION__, re->type);
1528 XFREE(MTYPE_RE, re);
1529 return -1;
1530 }
1531 STREAM_GETW(s, re->instance);
1532 STREAM_GETL(s, re->flags);
1533 STREAM_GETC(s, message);
1534 STREAM_GETW(s, safi);
d62a17ae 1535 re->uptime = time(NULL);
1536
1537 /* IPv4 prefix. */
1538 memset(&p, 0, sizeof(struct prefix_ipv4));
1539 p.family = AF_INET;
ec93aa12
DS
1540 STREAM_GETC(s, p.prefixlen);
1541 if (p.prefixlen > IPV4_MAX_BITLEN) {
1542 zlog_warn("%s: Prefix Length %d is greater than what a v4 address can use",
1543 __PRETTY_FUNCTION__, p.prefixlen);
1544 XFREE(MTYPE_RE, re);
1545 return -1;
1546 }
1547 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1548
1549 /* VRF ID */
1550 re->vrf_id = zvrf_id(zvrf);
1551
1552 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1553 * to the re to ensure that IPv6 multipathing works; need to coalesce
1554 * these. Clients should send the same number of paired set of
1555 * next-hop-addr/next-hop-ifindices. */
1556 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1557 unsigned int nh_count = 0;
1558 unsigned int if_count = 0;
1559 unsigned int max_nh_if = 0;
1560
ec93aa12 1561 STREAM_GETC(s, nexthop_num);
d62a17ae 1562 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1563 nexthop_num);
81c11e3f
RW
1564
1565 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1566 label_type = lsp_type_from_re_type(client->proto);
1567
d62a17ae 1568 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1569 STREAM_GETC(s, nexthop_type);
d62a17ae 1570
1571 switch (nexthop_type) {
1572 case NEXTHOP_TYPE_IPV6:
ec93aa12 1573 STREAM_GET(&nhop_addr, s, 16);
d62a17ae 1574 if (nh_count < MULTIPATH_NUM) {
1575 /* For labeled-unicast, each nexthop is
1576 * followed by label. */
1577 if (CHECK_FLAG(message,
1578 ZAPI_MESSAGE_LABEL)) {
ec93aa12 1579 STREAM_GETL(s, label);
d62a17ae 1580 labels[nh_count] = label;
1581 }
1582 nexthops[nh_count] = nhop_addr;
1583 nh_count++;
1584 }
1585 break;
1586 case NEXTHOP_TYPE_IFINDEX:
1587 if (if_count < multipath_num) {
ec93aa12 1588 STREAM_GETL(s, ifindices[if_count++]);
d62a17ae 1589 }
1590 break;
1591 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1592 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1593 break;
ec93aa12
DS
1594 default:
1595 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1596 __PRETTY_FUNCTION__);
1597 nexthops_free(re->nexthop);
1598 XFREE(MTYPE_RE, re);
1599 return -1;
d62a17ae 1600 }
1601 }
1602
1603 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1604 for (i = 0; i < max_nh_if; i++) {
1605 if ((i < nh_count)
1606 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1607 if ((i < if_count) && ifindices[i])
1608 nexthop =
1609 route_entry_nexthop_ipv6_ifindex_add(
1610 re, &nexthops[i],
1611 ifindices[i]);
1612 else
1613 nexthop = route_entry_nexthop_ipv6_add(
1614 re, &nexthops[i]);
1615
1616 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
81c11e3f
RW
1617 nexthop_add_labels(nexthop, label_type,
1618 1, &labels[i]);
d62a17ae 1619 } else {
1620 if ((i < if_count) && ifindices[i])
1621 route_entry_nexthop_ifindex_add(
1622 re, ifindices[i]);
1623 }
1624 }
8a92a8a0
DS
1625 }
1626
d62a17ae 1627 /* Distance. */
1628 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1629 STREAM_GETC(s, re->distance);
41fc2714 1630
d62a17ae 1631 /* Metric. */
1632 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1633 STREAM_GETL(s, re->metric);
d62a17ae 1634
1635 /* Tag */
1636 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1637 STREAM_GETL(s, re->tag);
d62a17ae 1638 else
1639 re->tag = 0;
1640
1641 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1642 STREAM_GETL(s, re->mtu);
d62a17ae 1643 else
1644 re->mtu = 0;
1645
1646 /* Table */
1647 re->table = zvrf->table_id;
1648
1649 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1650 /* Stats */
1651 if (ret > 0)
1652 client->v4_route_add_cnt++;
1653 else if (ret < 0)
1654 client->v4_route_upd8_cnt++;
1655
1656 return 0;
ec93aa12
DS
1657
1658stream_failure:
1659 nexthops_free(re->nexthop);
1660 XFREE(MTYPE_RE, re);
1661 return -1;
d62a17ae 1662}
1663
1664static int zread_ipv6_add(struct zserv *client, u_short length,
1665 struct zebra_vrf *zvrf)
1666{
1667 unsigned int i;
1668 struct stream *s;
1669 struct in6_addr nhop_addr;
f38efb80 1670 ifindex_t ifindex;
d62a17ae 1671 struct route_entry *re;
1672 u_char message;
1673 u_char nexthop_num;
1674 u_char nexthop_type;
1675 struct prefix p;
1676 struct prefix_ipv6 src_p, *src_pp;
1677 safi_t safi;
1678 static struct in6_addr nexthops[MULTIPATH_NUM];
1679 static unsigned int ifindices[MULTIPATH_NUM];
1680 int ret;
1681 static mpls_label_t labels[MULTIPATH_NUM];
98ca91e9 1682 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1683 mpls_label_t label;
1684 struct nexthop *nexthop;
a8309422 1685 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1686
1687 /* Get input stream. */
1688 s = client->ibuf;
1689
1690 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1691
1692 /* Allocate new re. */
1693 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1694
1695 /* Type, flags, message. */
ec93aa12
DS
1696 STREAM_GETC(s, re->type);
1697 if (re->type > ZEBRA_ROUTE_MAX) {
1698 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1699 __PRETTY_FUNCTION__, re->type);
1700 XFREE(MTYPE_RE, re);
1701 return -1;
1702 }
1703 STREAM_GETW(s, re->instance);
1704 STREAM_GETL(s, re->flags);
1705 STREAM_GETC(s, message);
1706 STREAM_GETW(s, safi);
d62a17ae 1707 re->uptime = time(NULL);
1708
1709 /* IPv6 prefix. */
ec93aa12 1710 memset(&p, 0, sizeof(p));
d62a17ae 1711 p.family = AF_INET6;
ec93aa12
DS
1712 STREAM_GETC(s, p.prefixlen);
1713 if (p.prefixlen > IPV6_MAX_BITLEN) {
1714 zlog_warn("%s: Specified prefix length %d is to large for v6 prefix",
1715 __PRETTY_FUNCTION__, p.prefixlen);
1716 XFREE(MTYPE_RE, re);
1717 return -1;
1718 }
1719 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
d62a17ae 1720
1721 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
ec93aa12 1722 memset(&src_p, 0, sizeof(src_p));
d62a17ae 1723 src_p.family = AF_INET6;
ec93aa12
DS
1724 STREAM_GETC(s, src_p.prefixlen);
1725 if (src_p.prefixlen > IPV6_MAX_BITLEN) {
1726 zlog_warn("%s: Specified src prefix length %d is to large for v6 prefix",
1727 __PRETTY_FUNCTION__, src_p.prefixlen);
1728 XFREE(MTYPE_RE, re);
1729 return -1;
1730 }
1731 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
d62a17ae 1732 src_pp = &src_p;
1733 } else
1734 src_pp = NULL;
1735
1736 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1737 * to the re to ensure that IPv6 multipathing works; need to coalesce
1738 * these. Clients should send the same number of paired set of
1739 * next-hop-addr/next-hop-ifindices. */
1740 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1741 unsigned int nh_count = 0;
1742 unsigned int if_count = 0;
1743 unsigned int max_nh_if = 0;
1744
ec93aa12 1745 STREAM_GETC(s, nexthop_num);
d62a17ae 1746 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1747 nexthop_num);
81c11e3f
RW
1748
1749 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1750 label_type = lsp_type_from_re_type(client->proto);
1751
d62a17ae 1752 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1753 STREAM_GETC(s, nexthop_type);
d62a17ae 1754
1755 switch (nexthop_type) {
1756 case NEXTHOP_TYPE_IPV6:
ec93aa12 1757 STREAM_GET(&nhop_addr, s, 16);
d62a17ae 1758 if (nh_count < MULTIPATH_NUM) {
1759 /* For labeled-unicast, each nexthop is
1760 * followed by label. */
1761 if (CHECK_FLAG(message,
1762 ZAPI_MESSAGE_LABEL)) {
ec93aa12 1763 STREAM_GETL(s, label);
d62a17ae 1764 labels[nh_count] = label;
1765 }
1766 nexthops[nh_count++] = nhop_addr;
1767 }
1768 break;
f38efb80 1769 case NEXTHOP_TYPE_IPV6_IFINDEX:
ec93aa12
DS
1770 STREAM_GET(&nhop_addr, s, 16);
1771 STREAM_GETL(s, ifindex);
f38efb80
RW
1772 route_entry_nexthop_ipv6_ifindex_add(
1773 re, &nhop_addr, ifindex);
1774 break;
d62a17ae 1775 case NEXTHOP_TYPE_IFINDEX:
1776 if (if_count < multipath_num) {
ec93aa12 1777 STREAM_GETL(s, ifindices[if_count++]);
d62a17ae 1778 }
1779 break;
1780 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1781 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1782 break;
ec93aa12
DS
1783 default:
1784 zlog_warn("%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1785 __PRETTY_FUNCTION__);
1786 nexthops_free(re->nexthop);
1787 XFREE(MTYPE_RE, re);
1788 return -1;
d62a17ae 1789 }
1790 }
1791
1792 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1793 for (i = 0; i < max_nh_if; i++) {
1794 if ((i < nh_count)
1795 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1796 if ((i < if_count) && ifindices[i])
1797 nexthop =
1798 route_entry_nexthop_ipv6_ifindex_add(
1799 re, &nexthops[i],
1800 ifindices[i]);
1801 else
1802 nexthop = route_entry_nexthop_ipv6_add(
1803 re, &nexthops[i]);
1804 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
81c11e3f
RW
1805 nexthop_add_labels(nexthop, label_type,
1806 1, &labels[i]);
d62a17ae 1807 } else {
1808 if ((i < if_count) && ifindices[i])
1809 route_entry_nexthop_ifindex_add(
1810 re, ifindices[i]);
1811 }
1812 }
41fc2714 1813 }
718e3744 1814
d62a17ae 1815 /* Distance. */
1816 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1817 STREAM_GETC(s, re->distance);
d62a17ae 1818
1819 /* Metric. */
1820 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1821 STREAM_GETL(s, re->metric);
718e3744 1822
d62a17ae 1823 /* Tag */
1824 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1825 STREAM_GETL(s, re->tag);
d62a17ae 1826 else
1827 re->tag = 0;
c50ca33a 1828
d62a17ae 1829 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1830 STREAM_GETL(s, re->mtu);
d62a17ae 1831 else
1832 re->mtu = 0;
0d9551dc 1833
d62a17ae 1834 /* VRF ID */
1835 re->vrf_id = zvrf_id(zvrf);
1836 re->table = zvrf->table_id;
154caaed 1837
d62a17ae 1838 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
1839 /* Stats */
1840 if (ret > 0)
1841 client->v6_route_add_cnt++;
1842 else if (ret < 0)
1843 client->v6_route_upd8_cnt++;
04b02fda 1844
d62a17ae 1845 return 0;
ec93aa12
DS
1846
1847stream_failure:
1848 nexthops_free(re->nexthop);
1849 XFREE(MTYPE_RE, re);
1850
1851 return -1;
718e3744 1852}
1853
1854/* Zebra server IPv6 prefix delete function. */
d62a17ae 1855static int zread_ipv6_delete(struct zserv *client, u_short length,
1856 struct zebra_vrf *zvrf)
1857{
d62a17ae 1858 struct stream *s;
1859 struct zapi_ipv6 api;
d62a17ae 1860 struct prefix p;
1861 struct prefix_ipv6 src_p, *src_pp;
1862
1863 s = client->ibuf;
d62a17ae 1864
1865 /* Type, flags, message. */
ec93aa12
DS
1866 STREAM_GETC(s, api.type);
1867 STREAM_GETW(s, api.instance);
1868 STREAM_GETL(s, api.flags);
1869 STREAM_GETC(s, api.message);
1870 STREAM_GETW(s, api.safi);
d62a17ae 1871
1872 /* IPv4 prefix. */
1e9f448f 1873 memset(&p, 0, sizeof(struct prefix));
d62a17ae 1874 p.family = AF_INET6;
ec93aa12
DS
1875 STREAM_GETC(s, p.prefixlen);
1876 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
d62a17ae 1877
1878 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1879 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1880 src_p.family = AF_INET6;
ec93aa12
DS
1881 STREAM_GETC(s, src_p.prefixlen);
1882 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
d62a17ae 1883 src_pp = &src_p;
1884 } else
1885 src_pp = NULL;
1886
d51b9e45 1887 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type, api.instance,
5dfeba19 1888 api.flags, &p, src_pp, NULL, client->rtm_table, 0, false);
d62a17ae 1889
1890 client->v6_route_del_cnt++;
ec93aa12
DS
1891
1892stream_failure:
d62a17ae 1893 return 0;
718e3744 1894}
1895
18a6dce6 1896/* Register zebra server router-id information. Send current router-id */
d62a17ae 1897static int zread_router_id_add(struct zserv *client, u_short length,
1898 struct zebra_vrf *zvrf)
18a6dce6 1899{
d62a17ae 1900 struct prefix p;
18a6dce6 1901
d62a17ae 1902 /* Router-id information is needed. */
1903 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
18a6dce6 1904
d62a17ae 1905 router_id_get(&p, zvrf_id(zvrf));
18a6dce6 1906
d62a17ae 1907 return zsend_router_id_update(client, &p, zvrf_id(zvrf));
18a6dce6 1908}
1909
1910/* Unregister zebra server router-id information. */
d62a17ae 1911static int zread_router_id_delete(struct zserv *client, u_short length,
1912 struct zebra_vrf *zvrf)
18a6dce6 1913{
d62a17ae 1914 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1915 return 0;
18a6dce6 1916}
1917
2ea1ab1c 1918/* Tie up route-type and client->sock */
d62a17ae 1919static void zread_hello(struct zserv *client)
2ea1ab1c 1920{
d62a17ae 1921 /* type of protocol (lib/zebra.h) */
1922 u_char proto;
1923 u_short instance;
e1a1880d 1924 u_char notify;
7c8ff89e 1925
ec93aa12
DS
1926 STREAM_GETC(client->ibuf, proto);
1927 STREAM_GETW(client->ibuf, instance);
e1a1880d
DS
1928 STREAM_GETC(client->ibuf, notify);
1929 if (notify)
1930 client->notify_owner = true;
2ea1ab1c 1931
d62a17ae 1932 /* accept only dynamic routing protocols */
1933 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
1934 zlog_notice(
1935 "client %d says hello and bids fair to announce only %s routes",
1936 client->sock, zebra_route_string(proto));
1937 if (instance)
1938 zlog_notice("client protocol instance %d", instance);
2ea1ab1c 1939
d62a17ae 1940 client->proto = proto;
1941 client->instance = instance;
1942 }
ec93aa12
DS
1943
1944stream_failure:
1945 return;
2ea1ab1c
VT
1946}
1947
7076bb2f 1948/* Unregister all information in a VRF. */
d62a17ae 1949static int zread_vrf_unregister(struct zserv *client, u_short length,
1950 struct zebra_vrf *zvrf)
1951{
1952 int i;
1953 afi_t afi;
1954
1955 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1956 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1957 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1958 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
1959 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1960 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1961
1962 return 0;
1963}
1964
1965static void zread_mpls_labels(int command, struct zserv *client, u_short length,
6680688c 1966 struct zebra_vrf *zvrf)
d62a17ae 1967{
1968 struct stream *s;
1969 enum lsp_types_t type;
1970 struct prefix prefix;
1971 enum nexthop_types_t gtype;
1972 union g_addr gate;
1973 ifindex_t ifindex;
1974 mpls_label_t in_label, out_label;
1975 u_int8_t distance;
d62a17ae 1976
1977 /* Get input stream. */
1978 s = client->ibuf;
1979
1980 /* Get data. */
ec93aa12
DS
1981 STREAM_GETC(s, type);
1982 STREAM_GETL(s, prefix.family);
d62a17ae 1983 switch (prefix.family) {
1984 case AF_INET:
ec93aa12
DS
1985 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1986 STREAM_GETC(s, prefix.prefixlen);
1987 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
1988 zlog_warn("%s: Specified prefix length %d is greater than a v4 address can support",
1989 __PRETTY_FUNCTION__,
1990 prefix.prefixlen);
1991 return;
1992 }
1993 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 1994 break;
1995 case AF_INET6:
ec93aa12
DS
1996 STREAM_GET(&prefix.u.prefix6, s, 16);
1997 STREAM_GETC(s, prefix.prefixlen);
1998 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
1999 zlog_warn("%s: Specified prefix length %d is greater than a v6 address can support",
2000 __PRETTY_FUNCTION__,
2001 prefix.prefixlen);
2002 return;
2003 }
2004 STREAM_GET(&gate.ipv6, s, 16);
d62a17ae 2005 break;
2006 default:
ec93aa12
DS
2007 zlog_warn("%s: Specified AF %d is not supported for this call",
2008 __PRETTY_FUNCTION__, prefix.family);
d62a17ae 2009 return;
2010 }
ec93aa12
DS
2011 STREAM_GETL(s, ifindex);
2012 STREAM_GETC(s, distance);
2013 STREAM_GETL(s, in_label);
2014 STREAM_GETL(s, out_label);
d62a17ae 2015
2016 switch (prefix.family) {
2017 case AF_INET:
2018 if (ifindex)
2019 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
2020 else
2021 gtype = NEXTHOP_TYPE_IPV4;
2022 break;
2023 case AF_INET6:
2024 if (ifindex)
2025 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
2026 else
2027 gtype = NEXTHOP_TYPE_IPV6;
2028 break;
2029 default:
2030 return;
2031 }
2032
2033 if (!mpls_enabled)
2034 return;
2035
2036 if (command == ZEBRA_MPLS_LABELS_ADD) {
2037 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
2038 ifindex);
2f9c59f0
RW
2039 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
2040 distance, out_label);
d62a17ae 2041 } else if (command == ZEBRA_MPLS_LABELS_DELETE) {
2042 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
2f9c59f0
RW
2043 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
2044 distance, out_label);
d62a17ae 2045 }
ec93aa12
DS
2046stream_failure:
2047 return;
ce549947 2048}
fea12efb 2049/* Send response to a label manager connect request to client */
d62a17ae 2050static int zsend_label_manager_connect_response(struct zserv *client,
2051 vrf_id_t vrf_id, u_short result)
fea12efb 2052{
d62a17ae 2053 struct stream *s;
fea12efb 2054
d62a17ae 2055 s = client->obuf;
2056 stream_reset(s);
fea12efb 2057
d62a17ae 2058 zserv_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
fea12efb 2059
d62a17ae 2060 /* result */
2061 stream_putc(s, result);
fea12efb 2062
d62a17ae 2063 /* Write packet size. */
2064 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 2065
d62a17ae 2066 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 2067}
2068
d62a17ae 2069static void zread_label_manager_connect(struct zserv *client, vrf_id_t vrf_id)
fea12efb 2070{
d62a17ae 2071 struct stream *s;
2072 /* type of protocol (lib/zebra.h) */
2073 u_char proto;
2074 u_short instance;
fea12efb 2075
d62a17ae 2076 /* Get input stream. */
2077 s = client->ibuf;
fea12efb 2078
d62a17ae 2079 /* Get data. */
ec93aa12
DS
2080 STREAM_GETC(s, proto);
2081 STREAM_GETW(s, instance);
fea12efb 2082
d62a17ae 2083 /* accept only dynamic routing protocols */
2084 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2085 zlog_err("client %d has wrong protocol %s", client->sock,
2086 zebra_route_string(proto));
2087 zsend_label_manager_connect_response(client, vrf_id, 1);
2088 return;
2089 }
2090 zlog_notice("client %d with instance %u connected as %s", client->sock,
2091 instance, zebra_route_string(proto));
2092 client->proto = proto;
2093 client->instance = instance;
2094
2095 /*
2096 Release previous labels of same protocol and instance.
2097 This is done in case it restarted from an unexpected shutdown.
2098 */
2099 release_daemon_chunks(proto, instance);
2100
2101 zlog_debug(
2102 " Label Manager client connected: sock %d, proto %s, instance %u",
2103 client->sock, zebra_route_string(proto), instance);
2104 /* send response back */
2105 zsend_label_manager_connect_response(client, vrf_id, 0);
ec93aa12
DS
2106
2107stream_failure:
2108 return;
fea12efb 2109}
2110/* Send response to a get label chunk request to client */
d62a17ae 2111static int zsend_assign_label_chunk_response(struct zserv *client,
2112 vrf_id_t vrf_id,
2113 struct label_manager_chunk *lmc)
fea12efb 2114{
d62a17ae 2115 struct stream *s;
fea12efb 2116
d62a17ae 2117 s = client->obuf;
2118 stream_reset(s);
fea12efb 2119
d62a17ae 2120 zserv_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
fea12efb 2121
d62a17ae 2122 if (lmc) {
2123 /* keep */
2124 stream_putc(s, lmc->keep);
2125 /* start and end labels */
2126 stream_putl(s, lmc->start);
2127 stream_putl(s, lmc->end);
2128 }
fea12efb 2129
d62a17ae 2130 /* Write packet size. */
2131 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 2132
d62a17ae 2133 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 2134}
2135
d62a17ae 2136static void zread_get_label_chunk(struct zserv *client, vrf_id_t vrf_id)
fea12efb 2137{
d62a17ae 2138 struct stream *s;
2139 u_char keep;
2140 uint32_t size;
2141 struct label_manager_chunk *lmc;
fea12efb 2142
d62a17ae 2143 /* Get input stream. */
2144 s = client->ibuf;
fea12efb 2145
d62a17ae 2146 /* Get data. */
ec93aa12
DS
2147 STREAM_GETC(s, keep);
2148 STREAM_GETL(s, size);
fea12efb 2149
d62a17ae 2150 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
2151 if (!lmc)
2152 zlog_err("%s: Unable to assign Label Chunk of size %u",
2153 __func__, size);
2154 else
2155 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
2156 lmc->end, keep);
2157 /* send response back */
2158 zsend_assign_label_chunk_response(client, vrf_id, lmc);
ec93aa12
DS
2159
2160stream_failure:
2161 return;
d62a17ae 2162}
2163
2164static void zread_release_label_chunk(struct zserv *client)
2165{
2166 struct stream *s;
2167 uint32_t start, end;
2168
2169 /* Get input stream. */
2170 s = client->ibuf;
2171
2172 /* Get data. */
ec93aa12
DS
2173 STREAM_GETL(s, start);
2174 STREAM_GETL(s, end);
d62a17ae 2175
2176 release_label_chunk(client->proto, client->instance, start, end);
ec93aa12
DS
2177
2178stream_failure:
2179 return;
d62a17ae 2180}
2181static void zread_label_manager_request(int cmd, struct zserv *client,
6680688c 2182 struct zebra_vrf *zvrf)
d62a17ae 2183{
2184 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2185 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2186 client->is_synchronous = 1;
2187
2188 /* external label manager */
2189 if (lm_is_external)
6680688c
DS
2190 zread_relay_label_manager_request(cmd, client,
2191 zvrf_id(zvrf));
d62a17ae 2192 /* this is a label manager */
2193 else {
2194 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
6680688c
DS
2195 zread_label_manager_connect(client,
2196 zvrf_id(zvrf));
d62a17ae 2197 else {
2198 /* Sanity: don't allow 'unidentified' requests */
2199 if (!client->proto) {
2200 zlog_err(
2201 "Got label request from an unidentified client");
2202 return;
2203 }
2204 if (cmd == ZEBRA_GET_LABEL_CHUNK)
6680688c
DS
2205 zread_get_label_chunk(client,
2206 zvrf_id(zvrf));
d62a17ae 2207 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2208 zread_release_label_chunk(client);
2209 }
2210 }
fea12efb 2211}
2212
6833ae01 2213static int zread_pseudowire(int command, struct zserv *client, u_short length,
6680688c 2214 struct zebra_vrf *zvrf)
6833ae01 2215{
2216 struct stream *s;
6833ae01 2217 char ifname[IF_NAMESIZE];
2218 ifindex_t ifindex;
2219 int type;
2220 int af;
2221 union g_addr nexthop;
2222 uint32_t local_label;
2223 uint32_t remote_label;
2224 uint8_t flags;
2225 union pw_protocol_fields data;
2226 uint8_t protocol;
2227 struct zebra_pw *pw;
2228
6833ae01 2229 /* Get input stream. */
2230 s = client->ibuf;
2231
2232 /* Get data. */
ec93aa12
DS
2233 STREAM_GET(ifname, s, IF_NAMESIZE);
2234 STREAM_GETL(s, ifindex);
2235 STREAM_GETL(s, type);
2236 STREAM_GETL(s, af);
6833ae01 2237 switch (af) {
2238 case AF_INET:
ec93aa12 2239 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
6833ae01 2240 break;
2241 case AF_INET6:
ec93aa12 2242 STREAM_GET(&nexthop.ipv6, s, 16);
6833ae01 2243 break;
2244 default:
2245 return -1;
2246 }
ec93aa12
DS
2247 STREAM_GETL(s, local_label);
2248 STREAM_GETL(s, remote_label);
2249 STREAM_GETC(s, flags);
2250 STREAM_GET(&data, s, sizeof(data));
6833ae01 2251 protocol = client->proto;
2252
2253 pw = zebra_pw_find(zvrf, ifname);
2254 switch (command) {
2255 case ZEBRA_PW_ADD:
2256 if (pw) {
2257 zlog_warn("%s: pseudowire %s already exists [%s]",
2258 __func__, ifname,
2259 zserv_command_string(command));
2260 return -1;
2261 }
2262
2263 zebra_pw_add(zvrf, ifname, protocol, client);
2264 break;
2265 case ZEBRA_PW_DELETE:
2266 if (!pw) {
2267 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2268 ifname, zserv_command_string(command));
2269 return -1;
2270 }
2271
2272 zebra_pw_del(zvrf, pw);
2273 break;
2274 case ZEBRA_PW_SET:
2275 case ZEBRA_PW_UNSET:
2276 if (!pw) {
2277 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2278 ifname, zserv_command_string(command));
2279 return -1;
2280 }
2281
2282 switch (command) {
2283 case ZEBRA_PW_SET:
2284 pw->enabled = 1;
2285 break;
2286 case ZEBRA_PW_UNSET:
2287 pw->enabled = 0;
2288 break;
2289 }
2290
2291 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2292 remote_label, flags, &data);
2293 break;
2294 }
2295
ec93aa12 2296stream_failure:
6833ae01 2297 return 0;
2298}
2299
d62a17ae 2300/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2301static void zebra_client_close_cleanup_rnh(struct zserv *client)
fea12efb 2302{
d62a17ae 2303 struct vrf *vrf;
2304 struct zebra_vrf *zvrf;
fea12efb 2305
a2addae8 2306 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
d62a17ae 2307 if ((zvrf = vrf->info) != NULL) {
2308 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2309 RNH_NEXTHOP_TYPE);
2310 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2311 client, RNH_NEXTHOP_TYPE);
2312 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2313 RNH_IMPORT_CHECK_TYPE);
2314 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2315 client, RNH_IMPORT_CHECK_TYPE);
2316 if (client->proto == ZEBRA_ROUTE_LDP) {
2317 hash_iterate(zvrf->lsp_table,
2318 mpls_ldp_lsp_uninstall_all,
2319 zvrf->lsp_table);
2320 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2321 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2322 }
2323 }
2324 }
fea12efb 2325}
d62a17ae 2326
972019ae
DS
2327/* free zebra client information. */
2328static void zebra_client_free(struct zserv *client)
fea12efb 2329{
d62a17ae 2330 /* Send client de-registration to BFD */
2331 zebra_ptm_bfd_client_deregister(client->proto);
fea12efb 2332
d62a17ae 2333 /* Cleanup any registered nexthops - across all VRFs. */
2334 zebra_client_close_cleanup_rnh(client);
ce549947 2335
d62a17ae 2336 /* Release Label Manager chunks */
2337 release_daemon_chunks(client->proto, client->instance);
4b33b75a 2338
d62a17ae 2339 /* Cleanup any FECs registered by this client. */
2340 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT),
2341 client);
2342
6833ae01 2343 /* Remove pseudowires associated with this client */
2344 zebra_pw_client_close(client);
2345
d62a17ae 2346 /* Close file descriptor. */
2347 if (client->sock) {
2348 unsigned long nroutes;
2349
2350 close(client->sock);
2351 nroutes = rib_score_proto(client->proto, client->instance);
2352 zlog_notice(
2353 "client %d disconnected. %lu %s routes removed from the rib",
2354 client->sock, nroutes,
2355 zebra_route_string(client->proto));
2356 client->sock = -1;
2357 }
2358
2359 /* Free stream buffers. */
2360 if (client->ibuf)
2361 stream_free(client->ibuf);
2362 if (client->obuf)
2363 stream_free(client->obuf);
2364 if (client->wb)
2365 buffer_free(client->wb);
2366
2367 /* Release threads. */
2368 if (client->t_read)
2369 thread_cancel(client->t_read);
2370 if (client->t_write)
2371 thread_cancel(client->t_write);
2372 if (client->t_suicide)
2373 thread_cancel(client->t_suicide);
2374
2375 /* Free bitmaps. */
2376 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
2377 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
2378 vrf_bitmap_free(client->redist[afi][i]);
2379
2380 vrf_bitmap_free(client->redist_default);
2381 vrf_bitmap_free(client->ifinfo);
2382 vrf_bitmap_free(client->ridinfo);
2383
d62a17ae 2384 XFREE(MTYPE_TMP, client);
718e3744 2385}
2386
972019ae
DS
2387static void zebra_client_close(struct zserv *client)
2388{
2389 listnode_delete(zebrad.client_list, client);
2390 zebra_client_free(client);
2391}
2392
718e3744 2393/* Make new client. */
d62a17ae 2394static void zebra_client_create(int sock)
718e3744 2395{
d62a17ae 2396 struct zserv *client;
2397 int i;
2398 afi_t afi;
2399
2400 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
718e3744 2401
d62a17ae 2402 /* Make client input/output buffer. */
2403 client->sock = sock;
2404 client->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2405 client->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2406 client->wb = buffer_new(0);
718e3744 2407
d62a17ae 2408 /* Set table number. */
2409 client->rtm_table = zebrad.rtm_table_default;
718e3744 2410
d62a17ae 2411 client->connect_time = monotime(NULL);
2412 /* Initialize flags */
2413 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2414 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2415 client->redist[afi][i] = vrf_bitmap_init();
2416 client->redist_default = vrf_bitmap_init();
2417 client->ifinfo = vrf_bitmap_init();
2418 client->ridinfo = vrf_bitmap_init();
718e3744 2419
d62a17ae 2420 /* by default, it's not a synchronous client */
2421 client->is_synchronous = 0;
04b02fda 2422
d62a17ae 2423 /* Add this client to linked list. */
2424 listnode_add(zebrad.client_list, client);
fea12efb 2425
d62a17ae 2426 /* Make new read thread. */
2427 zebra_event(ZEBRA_READ, sock, client);
12f6fb97 2428
d62a17ae 2429 zebra_vrf_update_all(client);
718e3744 2430}
2431
e4bd522a 2432static int zread_interface_set_master(struct zserv *client,
e0ae31b8
DS
2433 u_short length)
2434{
2435 struct interface *master;
2436 struct interface *slave;
2437 struct stream *s = client->ibuf;
2438 int ifindex;
2439 vrf_id_t vrf_id;
2440
ec93aa12
DS
2441 STREAM_GETW(s, vrf_id);
2442 STREAM_GETL(s, ifindex);
e0ae31b8
DS
2443 master = if_lookup_by_index(ifindex, vrf_id);
2444
ec93aa12
DS
2445 STREAM_GETW(s, vrf_id);
2446 STREAM_GETL(s, ifindex);
e0ae31b8
DS
2447 slave = if_lookup_by_index(ifindex, vrf_id);
2448
2449 if (!master || !slave)
2450 return 0;
2451
2452 kernel_interface_set_master(master, slave);
2453
ec93aa12 2454stream_failure:
e0ae31b8
DS
2455 return 1;
2456}
2457
0c5e7be5
DS
2458static inline void zserv_handle_commands(struct zserv *client,
2459 uint16_t command,
2460 uint16_t length,
2461 struct zebra_vrf *zvrf)
d62a17ae 2462{
d62a17ae 2463 switch (command) {
2464 case ZEBRA_ROUTER_ID_ADD:
2465 zread_router_id_add(client, length, zvrf);
2466 break;
2467 case ZEBRA_ROUTER_ID_DELETE:
2468 zread_router_id_delete(client, length, zvrf);
2469 break;
2470 case ZEBRA_INTERFACE_ADD:
2471 zread_interface_add(client, length, zvrf);
2472 break;
2473 case ZEBRA_INTERFACE_DELETE:
2474 zread_interface_delete(client, length, zvrf);
2475 break;
0e51b4a3
RW
2476 case ZEBRA_ROUTE_ADD:
2477 zread_route_add(client, length, zvrf);
2478 break;
2479 case ZEBRA_ROUTE_DELETE:
2480 zread_route_del(client, length, zvrf);
2481 break;
d62a17ae 2482 case ZEBRA_IPV4_ROUTE_ADD:
2483 zread_ipv4_add(client, length, zvrf);
2484 break;
2485 case ZEBRA_IPV4_ROUTE_DELETE:
2486 zread_ipv4_delete(client, length, zvrf);
2487 break;
2488 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
2489 zread_ipv4_route_ipv6_nexthop_add(client, length, zvrf);
2490 break;
d62a17ae 2491 case ZEBRA_IPV6_ROUTE_ADD:
2492 zread_ipv6_add(client, length, zvrf);
2493 break;
2494 case ZEBRA_IPV6_ROUTE_DELETE:
2495 zread_ipv6_delete(client, length, zvrf);
2496 break;
2497 case ZEBRA_REDISTRIBUTE_ADD:
2498 zebra_redistribute_add(command, client, length, zvrf);
2499 break;
2500 case ZEBRA_REDISTRIBUTE_DELETE:
2501 zebra_redistribute_delete(command, client, length, zvrf);
2502 break;
2503 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
2504 zebra_redistribute_default_add(command, client, length, zvrf);
2505 break;
2506 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
2507 zebra_redistribute_default_delete(command, client, length,
2508 zvrf);
2509 break;
2510 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2511 zread_ipv4_nexthop_lookup_mrib(client, length, zvrf);
2512 break;
2513 case ZEBRA_HELLO:
2514 zread_hello(client);
2515 break;
2516 case ZEBRA_NEXTHOP_REGISTER:
e4bd522a 2517 zserv_rnh_register(client, length, RNH_NEXTHOP_TYPE,
d62a17ae 2518 zvrf);
2519 break;
2520 case ZEBRA_NEXTHOP_UNREGISTER:
e4bd522a 2521 zserv_rnh_unregister(client, length, RNH_NEXTHOP_TYPE,
d62a17ae 2522 zvrf);
2523 break;
2524 case ZEBRA_IMPORT_ROUTE_REGISTER:
e4bd522a 2525 zserv_rnh_register(client, length, RNH_IMPORT_CHECK_TYPE,
d62a17ae 2526 zvrf);
2527 break;
2528 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
e4bd522a 2529 zserv_rnh_unregister(client, length,
d62a17ae 2530 RNH_IMPORT_CHECK_TYPE, zvrf);
2531 break;
2532 case ZEBRA_BFD_DEST_UPDATE:
2533 case ZEBRA_BFD_DEST_REGISTER:
e4bd522a 2534 zebra_ptm_bfd_dst_register(client, length, command, zvrf);
d62a17ae 2535 break;
2536 case ZEBRA_BFD_DEST_DEREGISTER:
e4bd522a 2537 zebra_ptm_bfd_dst_deregister(client, length, zvrf);
d62a17ae 2538 break;
2539 case ZEBRA_VRF_UNREGISTER:
2540 zread_vrf_unregister(client, length, zvrf);
2541 break;
2542 case ZEBRA_BFD_CLIENT_REGISTER:
e4bd522a 2543 zebra_ptm_bfd_client_register(client, length);
d62a17ae 2544 break;
2545 case ZEBRA_INTERFACE_ENABLE_RADV:
2546#if defined(HAVE_RTADV)
e4bd522a 2547 zebra_interface_radv_set(client, length, zvrf, 1);
9c3bf1ce 2548#endif
d62a17ae 2549 break;
2550 case ZEBRA_INTERFACE_DISABLE_RADV:
2551#if defined(HAVE_RTADV)
e4bd522a 2552 zebra_interface_radv_set(client, length, zvrf, 0);
9c3bf1ce 2553#endif
d62a17ae 2554 break;
2555 case ZEBRA_MPLS_LABELS_ADD:
2556 case ZEBRA_MPLS_LABELS_DELETE:
6680688c 2557 zread_mpls_labels(command, client, length, zvrf);
d62a17ae 2558 break;
2559 case ZEBRA_IPMR_ROUTE_STATS:
e4bd522a 2560 zebra_ipmr_route_stats(client, length, zvrf);
d62a17ae 2561 break;
2562 case ZEBRA_LABEL_MANAGER_CONNECT:
2563 case ZEBRA_GET_LABEL_CHUNK:
2564 case ZEBRA_RELEASE_LABEL_CHUNK:
6680688c 2565 zread_label_manager_request(command, client, zvrf);
d62a17ae 2566 break;
2567 case ZEBRA_FEC_REGISTER:
e4bd522a 2568 zserv_fec_register(client, length);
d62a17ae 2569 break;
2570 case ZEBRA_FEC_UNREGISTER:
e4bd522a 2571 zserv_fec_unregister(client, length);
d62a17ae 2572 break;
1a98c087 2573 case ZEBRA_ADVERTISE_DEFAULT_GW:
e4bd522a 2574 zebra_vxlan_advertise_gw_macip(client, length, zvrf);
1a98c087 2575 break;
d62a17ae 2576 case ZEBRA_ADVERTISE_ALL_VNI:
e4bd522a 2577 zebra_vxlan_advertise_all_vni(client, length, zvrf);
d62a17ae 2578 break;
2579 case ZEBRA_REMOTE_VTEP_ADD:
e4bd522a 2580 zebra_vxlan_remote_vtep_add(client, length, zvrf);
d62a17ae 2581 break;
2582 case ZEBRA_REMOTE_VTEP_DEL:
e4bd522a 2583 zebra_vxlan_remote_vtep_del(client, length, zvrf);
d62a17ae 2584 break;
2585 case ZEBRA_REMOTE_MACIP_ADD:
e4bd522a 2586 zebra_vxlan_remote_macip_add(client, length, zvrf);
d62a17ae 2587 break;
2588 case ZEBRA_REMOTE_MACIP_DEL:
e4bd522a 2589 zebra_vxlan_remote_macip_del(client, length, zvrf);
baca8cd4 2590 break;
e0ae31b8 2591 case ZEBRA_INTERFACE_SET_MASTER:
e4bd522a 2592 zread_interface_set_master(client, length);
d62a17ae 2593 break;
6833ae01 2594 case ZEBRA_PW_ADD:
2595 case ZEBRA_PW_DELETE:
2596 case ZEBRA_PW_SET:
2597 case ZEBRA_PW_UNSET:
6680688c 2598 zread_pseudowire(command, client, length, zvrf);
6833ae01 2599 break;
d62a17ae 2600 default:
2601 zlog_info("Zebra received unknown command %d", command);
2602 break;
2603 }
0c5e7be5
DS
2604}
2605
411314ed
DS
2606#if defined(HANDLE_ZAPI_FUZZING)
2607static void zserv_write_incoming(struct stream *orig, uint16_t command)
2608{
2609 char fname[MAXPATHLEN];
2610 struct stream *copy;
2611 int fd = -1;
2612
2613 copy = stream_dup(orig);
2614 stream_set_getp(copy, 0);
2615
2616 zserv_privs.change(ZPRIVS_RAISE);
2617 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
2618 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2619 stream_flush(copy, fd);
2620 close(fd);
2621 zserv_privs.change(ZPRIVS_LOWER);
2622 stream_free(copy);
2623}
2624#endif
2625
0c5e7be5
DS
2626/* Handler of zebra service request. */
2627static int zebra_client_read(struct thread *thread)
2628{
2629 int sock;
2630 struct zserv *client;
2631 size_t already;
2632 uint16_t length, command;
2633 uint8_t marker, version;
2634 vrf_id_t vrf_id;
2635 struct zebra_vrf *zvrf;
411314ed
DS
2636#if defined(HANDLE_ZAPI_FUZZING)
2637 int packets = 1;
2638#else
a37ef435 2639 int packets = zebrad.packets_to_process;
411314ed 2640#endif
0c5e7be5
DS
2641
2642 /* Get thread data. Reset reading thread because I'm running. */
2643 sock = THREAD_FD(thread);
2644 client = THREAD_ARG(thread);
2645 client->t_read = NULL;
2646
2647 if (client->t_suicide) {
2648 zebra_client_close(client);
2649 return -1;
2650 }
2651
5a762c8a
DS
2652 while (packets) {
2653 /* Read length and command (if we don't have it already). */
2654 if ((already = stream_get_endp(client->ibuf))
2655 < ZEBRA_HEADER_SIZE) {
2656 ssize_t nbyte;
2657 if (((nbyte =
2658 stream_read_try(client->ibuf, sock,
0c5e7be5 2659 ZEBRA_HEADER_SIZE - already))
5a762c8a
DS
2660 == 0)
2661 || (nbyte == -1)) {
2662 if (IS_ZEBRA_DEBUG_EVENT)
2663 zlog_debug("connection closed socket [%d]",
2664 sock);
2665 zebra_client_close(client);
2666 return -1;
2667 }
2668 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2669 /* Try again later. */
2670 zebra_event(ZEBRA_READ, sock, client);
2671 return 0;
2672 }
2673 already = ZEBRA_HEADER_SIZE;
0c5e7be5 2674 }
0c5e7be5 2675
5a762c8a
DS
2676 /* Reset to read from the beginning of the incoming packet. */
2677 stream_set_getp(client->ibuf, 0);
0c5e7be5 2678
5a762c8a 2679 /* Fetch header values */
ec93aa12
DS
2680 STREAM_GETW(client->ibuf, length);
2681 STREAM_GETC(client->ibuf, marker);
2682 STREAM_GETC(client->ibuf, version);
2683 STREAM_GETW(client->ibuf, vrf_id);
2684 STREAM_GETW(client->ibuf, command);
0c5e7be5 2685
5a762c8a
DS
2686 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2687 zlog_err(
2688 "%s: socket %d version mismatch, marker %d, version %d",
2689 __func__, sock, marker, version);
0c5e7be5
DS
2690 zebra_client_close(client);
2691 return -1;
2692 }
5a762c8a
DS
2693 if (length < ZEBRA_HEADER_SIZE) {
2694 zlog_warn(
2695 "%s: socket %d message length %u is less than header size %d",
2696 __func__, sock, length, ZEBRA_HEADER_SIZE);
2697 zebra_client_close(client);
2698 return -1;
2699 }
2700 if (length > STREAM_SIZE(client->ibuf)) {
2701 zlog_warn(
2702 "%s: socket %d message length %u exceeds buffer size %lu",
2703 __func__, sock, length,
2704 (u_long)STREAM_SIZE(client->ibuf));
2705 zebra_client_close(client);
2706 return -1;
0c5e7be5 2707 }
0c5e7be5 2708
5a762c8a
DS
2709 /* Read rest of data. */
2710 if (already < length) {
2711 ssize_t nbyte;
2712 if (((nbyte = stream_read_try(client->ibuf, sock,
2713 length - already))
2714 == 0)
2715 || (nbyte == -1)) {
2716 if (IS_ZEBRA_DEBUG_EVENT)
2717 zlog_debug(
2718 "connection closed [%d] when reading zebra data",
2719 sock);
2720 zebra_client_close(client);
2721 return -1;
2722 }
2723 if (nbyte != (ssize_t)(length - already)) {
2724 /* Try again later. */
2725 zebra_event(ZEBRA_READ, sock, client);
2726 return 0;
2727 }
2728 }
0c5e7be5 2729
411314ed
DS
2730#if defined(HANDLE_ZAPI_FUZZING)
2731 zserv_write_incoming(client->ibuf, command);
2732#endif
5a762c8a 2733 length -= ZEBRA_HEADER_SIZE;
0c5e7be5 2734
5a762c8a
DS
2735 /* Debug packet information. */
2736 if (IS_ZEBRA_DEBUG_EVENT)
2737 zlog_debug("zebra message comes from socket [%d]", sock);
0c5e7be5 2738
0c5e7be5 2739 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
5a762c8a
DS
2740 zlog_debug("zebra message received [%s] %d in VRF %u",
2741 zserv_command_string(command), length, vrf_id);
0c5e7be5 2742
5a762c8a
DS
2743 client->last_read_time = monotime(NULL);
2744 client->last_read_cmd = command;
d62a17ae 2745
5a762c8a
DS
2746 zvrf = zebra_vrf_lookup_by_id(vrf_id);
2747 if (!zvrf) {
2748 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2749 zlog_debug("zebra received unknown VRF[%u]", vrf_id);
2750 goto zclient_read_out;
2751 }
2752
2753 zserv_handle_commands(client, command, length, zvrf);
2754
2755 if (client->t_suicide) {
2756 /* No need to wait for thread callback, just kill immediately.
2757 */
2758 zebra_client_close(client);
2759 return -1;
2760 }
2761 packets -= 1;
2762 stream_reset(client->ibuf);
d62a17ae 2763 }
2764
ec93aa12 2765stream_failure:
d62a17ae 2766zclient_read_out:
2767 stream_reset(client->ibuf);
2768 zebra_event(ZEBRA_READ, sock, client);
2769 return 0;
718e3744 2770}
2771
718e3744 2772
2773/* Accept code of zebra server socket. */
d62a17ae 2774static int zebra_accept(struct thread *thread)
718e3744 2775{
d62a17ae 2776 int accept_sock;
2777 int client_sock;
2778 struct sockaddr_in client;
2779 socklen_t len;
2780
2781 accept_sock = THREAD_FD(thread);
718e3744 2782
d62a17ae 2783 /* Reregister myself. */
2784 zebra_event(ZEBRA_SERV, accept_sock, NULL);
718e3744 2785
d62a17ae 2786 len = sizeof(struct sockaddr_in);
2787 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
719e9741 2788
d62a17ae 2789 if (client_sock < 0) {
2790 zlog_warn("Can't accept zebra socket: %s",
2791 safe_strerror(errno));
2792 return -1;
2793 }
718e3744 2794
d62a17ae 2795 /* Make client socket non-blocking. */
2796 set_nonblocking(client_sock);
718e3744 2797
d62a17ae 2798 /* Create new zebra client. */
2799 zebra_client_create(client_sock);
718e3744 2800
d62a17ae 2801 return 0;
718e3744 2802}
2803
689f5a8c
DL
2804/* Make zebra server socket, wiping any existing one (see bug #403). */
2805void zebra_zserv_socket_init(char *path)
d62a17ae 2806{
2807 int ret;
689f5a8c 2808 int sock;
d62a17ae 2809 mode_t old_mask;
689f5a8c
DL
2810 struct sockaddr_storage sa;
2811 socklen_t sa_len;
d62a17ae 2812
689f5a8c
DL
2813 if (!frr_zclient_addr(&sa, &sa_len, path))
2814 /* should be caught in zebra main() */
2815 return;
d62a17ae 2816
2817 /* Set umask */
2818 old_mask = umask(0077);
2819
2820 /* Make UNIX domain socket. */
689f5a8c 2821 sock = socket(sa.ss_family, SOCK_STREAM, 0);
d62a17ae 2822 if (sock < 0) {
689f5a8c 2823 zlog_warn("Can't create zserv socket: %s",
d62a17ae 2824 safe_strerror(errno));
2825 zlog_warn(
2826 "zebra can't provide full functionality due to above error");
2827 return;
2828 }
2829
689f5a8c
DL
2830 if (sa.ss_family != AF_UNIX) {
2831 sockopt_reuseaddr(sock);
2832 sockopt_reuseport(sock);
2833 } else {
2834 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
2835 if (suna->sun_path[0])
2836 unlink(suna->sun_path);
2837 }
2838
2c73b258
DS
2839 zserv_privs.change(ZPRIVS_RAISE);
2840 setsockopt_so_recvbuf(sock, 1048576);
2841 setsockopt_so_sendbuf(sock, 1048576);
2842 zserv_privs.change(ZPRIVS_LOWER);
2843
e6c1975a 2844 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_RAISE))
689f5a8c
DL
2845 zlog_err("Can't raise privileges");
2846
2847 ret = bind(sock, (struct sockaddr *)&sa, sa_len);
d62a17ae 2848 if (ret < 0) {
689f5a8c 2849 zlog_warn("Can't bind zserv socket on %s: %s", path,
d62a17ae 2850 safe_strerror(errno));
2851 zlog_warn(
2852 "zebra can't provide full functionality due to above error");
2853 close(sock);
2854 return;
2855 }
e6c1975a 2856 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_LOWER))
689f5a8c 2857 zlog_err("Can't lower privileges");
d62a17ae 2858
2859 ret = listen(sock, 5);
2860 if (ret < 0) {
689f5a8c 2861 zlog_warn("Can't listen to zserv socket %s: %s", path,
d62a17ae 2862 safe_strerror(errno));
2863 zlog_warn(
2864 "zebra can't provide full functionality due to above error");
2865 close(sock);
2866 return;
2867 }
2868
2869 umask(old_mask);
2870
2871 zebra_event(ZEBRA_SERV, sock, NULL);
718e3744 2872}
6b0655a2 2873
718e3744 2874
d62a17ae 2875static void zebra_event(enum event event, int sock, struct zserv *client)
2876{
2877 switch (event) {
2878 case ZEBRA_SERV:
2879 thread_add_read(zebrad.master, zebra_accept, client, sock,
2880 NULL);
2881 break;
2882 case ZEBRA_READ:
2883 client->t_read = NULL;
2884 thread_add_read(zebrad.master, zebra_client_read, client, sock,
2885 &client->t_read);
2886 break;
2887 case ZEBRA_WRITE:
2888 /**/
2889 break;
2890 }
718e3744 2891}
6b0655a2 2892
04b02fda 2893#define ZEBRA_TIME_BUF 32
d62a17ae 2894static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
04b02fda 2895{
d62a17ae 2896 struct tm *tm;
2897 time_t now;
04b02fda 2898
d62a17ae 2899 assert(buf != NULL);
2900 assert(buflen >= ZEBRA_TIME_BUF);
2901 assert(time1 != NULL);
04b02fda 2902
d62a17ae 2903 if (!*time1) {
2904 snprintf(buf, buflen, "never ");
2905 return (buf);
2906 }
04b02fda 2907
d62a17ae 2908 now = monotime(NULL);
2909 now -= *time1;
2910 tm = gmtime(&now);
04b02fda 2911
d62a17ae 2912 if (now < ONE_DAY_SECOND)
2913 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
2914 tm->tm_sec);
2915 else if (now < ONE_WEEK_SECOND)
2916 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
2917 tm->tm_min);
96ade3ed 2918 else
d62a17ae 2919 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
2920 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
2921 return buf;
2922}
2923
2924static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
2925{
2926 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2927 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2928
2929 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
2930 if (client->instance)
2931 vty_out(vty, " Instance: %d", client->instance);
2932 vty_out(vty, "\n");
2933
2934 vty_out(vty, "------------------------ \n");
2935 vty_out(vty, "FD: %d \n", client->sock);
2936 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
2937
2938 vty_out(vty, "Connect Time: %s \n",
2939 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
2940 if (client->nh_reg_time) {
2941 vty_out(vty, "Nexthop Registry Time: %s \n",
2942 zserv_time_buf(&client->nh_reg_time, nhbuf,
2943 ZEBRA_TIME_BUF));
2944 if (client->nh_last_upd_time)
2945 vty_out(vty, "Nexthop Last Update Time: %s \n",
2946 zserv_time_buf(&client->nh_last_upd_time, mbuf,
2947 ZEBRA_TIME_BUF));
2948 else
2949 vty_out(vty, "No Nexthop Update sent\n");
2950 } else
2951 vty_out(vty, "Not registered for Nexthop Updates\n");
2952
2953 vty_out(vty, "Last Msg Rx Time: %s \n",
2954 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
2955 vty_out(vty, "Last Msg Tx Time: %s \n",
2956 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
2957 if (client->last_read_time)
2958 vty_out(vty, "Last Rcvd Cmd: %s \n",
2959 zserv_command_string(client->last_read_cmd));
2960 if (client->last_write_time)
2961 vty_out(vty, "Last Sent Cmd: %s \n",
2962 zserv_command_string(client->last_write_cmd));
2963 vty_out(vty, "\n");
2964
2965 vty_out(vty, "Type Add Update Del \n");
2966 vty_out(vty, "================================================== \n");
2967 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
2968 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
2969 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
2970 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
2971 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
2972 0, client->redist_v4_del_cnt);
2973 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
2974 0, client->redist_v6_del_cnt);
2975 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
2976 client->ifdel_cnt);
2977 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
2978 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
2979 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
2980 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
2981 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
2982 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
2983 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
2984 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
2985
2986 vty_out(vty, "\n");
2987 return;
2988}
2989
2990static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
2991{
2992 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2993 char wbuf[ZEBRA_TIME_BUF];
2994
2995 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2996 zebra_route_string(client->proto),
2997 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2998 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2999 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
3000 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
3001 client->v4_route_del_cnt,
3002 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
3003 client->v6_route_del_cnt);
3004}
3005
019a82cb 3006struct zserv *zebra_find_client(u_char proto, u_short instance)
d62a17ae 3007{
3008 struct listnode *node, *nnode;
3009 struct zserv *client;
3010
3011 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
019a82cb
DS
3012 if (client->proto == proto &&
3013 client->instance == instance)
d62a17ae 3014 return client;
3015 }
3016
3017 return NULL;
8ed6821e 3018}
3019
9bf75362 3020#ifdef HAVE_NETLINK
718e3744 3021/* Display default rtm_table for all clients. */
3022DEFUN (show_table,
3023 show_table_cmd,
3024 "show table",
3025 SHOW_STR
3026 "default routing table to use for all clients\n")
3027{
d62a17ae 3028 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
3029 return CMD_SUCCESS;
718e3744 3030}
3031
6147e2c6 3032DEFUN (config_table,
718e3744 3033 config_table_cmd,
3034 "table TABLENO",
3035 "Configure target kernel routing table\n"
3036 "TABLE integer\n")
3037{
d62a17ae 3038 zebrad.rtm_table_default = strtol(argv[1]->arg, (char **)0, 10);
3039 return CMD_SUCCESS;
718e3744 3040}
3041
813d4307
DW
3042DEFUN (no_config_table,
3043 no_config_table_cmd,
6af6be86 3044 "no table [TABLENO]",
813d4307
DW
3045 NO_STR
3046 "Configure target kernel routing table\n"
3047 "TABLE integer\n")
3048{
d62a17ae 3049 zebrad.rtm_table_default = 0;
3050 return CMD_SUCCESS;
813d4307 3051}
9bf75362 3052#endif
813d4307 3053
647e4f1f 3054DEFUN (ip_forwarding,
3055 ip_forwarding_cmd,
3056 "ip forwarding",
3057 IP_STR
efd7904e 3058 "Turn on IP forwarding\n")
647e4f1f 3059{
d62a17ae 3060 int ret;
647e4f1f 3061
d62a17ae 3062 ret = ipforward();
3063 if (ret == 0)
3064 ret = ipforward_on();
647e4f1f 3065
d62a17ae 3066 if (ret == 0) {
3067 vty_out(vty, "Can't turn on IP forwarding\n");
3068 return CMD_WARNING_CONFIG_FAILED;
3069 }
647e4f1f 3070
d62a17ae 3071 return CMD_SUCCESS;
647e4f1f 3072}
3073
718e3744 3074DEFUN (no_ip_forwarding,
3075 no_ip_forwarding_cmd,
3076 "no ip forwarding",
3077 NO_STR
3078 IP_STR
efd7904e 3079 "Turn off IP forwarding\n")
718e3744 3080{
d62a17ae 3081 int ret;
718e3744 3082
d62a17ae 3083 ret = ipforward();
3084 if (ret != 0)
3085 ret = ipforward_off();
718e3744 3086
d62a17ae 3087 if (ret != 0) {
3088 vty_out(vty, "Can't turn off IP forwarding\n");
3089 return CMD_WARNING_CONFIG_FAILED;
3090 }
718e3744 3091
d62a17ae 3092 return CMD_SUCCESS;
718e3744 3093}
3094
57282a31
DS
3095DEFUN (show_zebra,
3096 show_zebra_cmd,
3097 "show zebra",
3098 SHOW_STR
41e7fb80 3099 ZEBRA_STR)
57282a31 3100{
d62a17ae 3101 struct vrf *vrf;
57282a31 3102
d62a17ae 3103 vty_out(vty,
3104 " Route Route Neighbor LSP LSP\n");
3105 vty_out(vty,
3106 "VRF Installs Removals Updates Installs Removals\n");
a2addae8 3107 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 3108 struct zebra_vrf *zvrf = vrf->info;
9d303b37
DL
3109 vty_out(vty, "%-25s %10" PRIu64 " %10" PRIu64 " %10" PRIu64
3110 " %10" PRIu64 " %10" PRIu64 "\n",
d62a17ae 3111 vrf->name, zvrf->installs, zvrf->removals,
3112 zvrf->neigh_updates, zvrf->lsp_installs,
3113 zvrf->lsp_removals);
3114 }
57282a31 3115
d62a17ae 3116 return CMD_SUCCESS;
57282a31
DS
3117}
3118
718e3744 3119/* This command is for debugging purpose. */
3120DEFUN (show_zebra_client,
3121 show_zebra_client_cmd,
3122 "show zebra client",
3123 SHOW_STR
41e7fb80 3124 ZEBRA_STR
b9ee4999 3125 "Client information\n")
718e3744 3126{
d62a17ae 3127 struct listnode *node;
3128 struct zserv *client;
718e3744 3129
d62a17ae 3130 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3131 zebra_show_client_detail(vty, client);
04b02fda 3132
d62a17ae 3133 return CMD_SUCCESS;
04b02fda
DS
3134}
3135
3136/* This command is for debugging purpose. */
3137DEFUN (show_zebra_client_summary,
3138 show_zebra_client_summary_cmd,
3139 "show zebra client summary",
3140 SHOW_STR
41e7fb80 3141 ZEBRA_STR
b9ee4999
DS
3142 "Client information brief\n"
3143 "Brief Summary\n")
04b02fda 3144{
d62a17ae 3145 struct listnode *node;
3146 struct zserv *client;
04b02fda 3147
d62a17ae 3148 vty_out(vty,
3149 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3150 vty_out(vty,
3151 "--------------------------------------------------------------------------------\n");
04b02fda 3152
d62a17ae 3153 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3154 zebra_show_client_brief(vty, client);
fb018d25 3155
d62a17ae 3156 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
3157 return CMD_SUCCESS;
718e3744 3158}
3159
3160/* Table configuration write function. */
d62a17ae 3161static int config_write_table(struct vty *vty)
718e3744 3162{
d62a17ae 3163 if (zebrad.rtm_table_default)
3164 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
3165 return 0;
718e3744 3166}
3167
3168/* table node for routing tables. */
d62a17ae 3169static struct cmd_node table_node = {TABLE_NODE,
3170 "", /* This node has no interface. */
3171 1};
6b0655a2 3172
718e3744 3173/* Only display ip forwarding is enabled or not. */
3174DEFUN (show_ip_forwarding,
3175 show_ip_forwarding_cmd,
3176 "show ip forwarding",
3177 SHOW_STR
3178 IP_STR
3179 "IP forwarding status\n")
3180{
d62a17ae 3181 int ret;
718e3744 3182
d62a17ae 3183 ret = ipforward();
718e3744 3184
d62a17ae 3185 if (ret == 0)
3186 vty_out(vty, "IP forwarding is off\n");
3187 else
3188 vty_out(vty, "IP forwarding is on\n");
3189 return CMD_SUCCESS;
718e3744 3190}
3191
718e3744 3192/* Only display ipv6 forwarding is enabled or not. */
3193DEFUN (show_ipv6_forwarding,
3194 show_ipv6_forwarding_cmd,
3195 "show ipv6 forwarding",
3196 SHOW_STR
3197 "IPv6 information\n"
3198 "Forwarding status\n")
3199{
d62a17ae 3200 int ret;
718e3744 3201
d62a17ae 3202 ret = ipforward_ipv6();
718e3744 3203
d62a17ae 3204 switch (ret) {
3205 case -1:
3206 vty_out(vty, "ipv6 forwarding is unknown\n");
3207 break;
3208 case 0:
3209 vty_out(vty, "ipv6 forwarding is %s\n", "off");
3210 break;
3211 case 1:
3212 vty_out(vty, "ipv6 forwarding is %s\n", "on");
3213 break;
3214 default:
3215 vty_out(vty, "ipv6 forwarding is %s\n", "off");
3216 break;
3217 }
3218 return CMD_SUCCESS;
718e3744 3219}
3220
55906724 3221DEFUN (ipv6_forwarding,
3222 ipv6_forwarding_cmd,
3223 "ipv6 forwarding",
3224 IPV6_STR
efd7904e 3225 "Turn on IPv6 forwarding\n")
55906724 3226{
d62a17ae 3227 int ret;
55906724 3228
d62a17ae 3229 ret = ipforward_ipv6();
3230 if (ret == 0)
3231 ret = ipforward_ipv6_on();
41d3fc96 3232
d62a17ae 3233 if (ret == 0) {
3234 vty_out(vty, "Can't turn on IPv6 forwarding\n");
3235 return CMD_WARNING_CONFIG_FAILED;
3236 }
55906724 3237
d62a17ae 3238 return CMD_SUCCESS;
55906724 3239}
3240
718e3744 3241DEFUN (no_ipv6_forwarding,
3242 no_ipv6_forwarding_cmd,
3243 "no ipv6 forwarding",
3244 NO_STR
55906724 3245 IPV6_STR
efd7904e 3246 "Turn off IPv6 forwarding\n")
718e3744 3247{
d62a17ae 3248 int ret;
718e3744 3249
d62a17ae 3250 ret = ipforward_ipv6();
3251 if (ret != 0)
3252 ret = ipforward_ipv6_off();
41d3fc96 3253
d62a17ae 3254 if (ret != 0) {
3255 vty_out(vty, "Can't turn off IPv6 forwarding\n");
3256 return CMD_WARNING_CONFIG_FAILED;
3257 }
718e3744 3258
d62a17ae 3259 return CMD_SUCCESS;
718e3744 3260}
3261
718e3744 3262/* IPForwarding configuration write function. */
d62a17ae 3263static int config_write_forwarding(struct vty *vty)
718e3744 3264{
d62a17ae 3265 /* FIXME: Find better place for that. */
3266 router_id_write(vty);
18a6dce6 3267
d62a17ae 3268 if (!ipforward())
3269 vty_out(vty, "no ip forwarding\n");
3270 if (!ipforward_ipv6())
3271 vty_out(vty, "no ipv6 forwarding\n");
3272 vty_out(vty, "!\n");
3273 return 0;
718e3744 3274}
3275
3276/* table node for routing tables. */
d62a17ae 3277static struct cmd_node forwarding_node = {FORWARDING_NODE,
3278 "", /* This node has no interface. */
3279 1};
718e3744 3280
411314ed
DS
3281#if defined(HANDLE_ZAPI_FUZZING)
3282void zserv_read_file(char *input)
3283{
3284 int fd;
3285 struct zserv *client = NULL;
3286 struct thread t;
3287
3288 zebra_client_create(-1);
3289 client = zebrad.client_list->head->data;
3290 t.arg = client;
3291
3292 fd = open(input, O_RDONLY|O_NONBLOCK);
3293 t.u.fd = fd;
3294
3295 zebra_client_read(&t);
3296
3297 close(fd);
3298}
3299#endif
3300
718e3744 3301/* Initialisation of zebra and installation of commands. */
d62a17ae 3302void zebra_init(void)
718e3744 3303{
d62a17ae 3304 /* Client list init. */
3305 zebrad.client_list = list_new();
972019ae 3306 zebrad.client_list->del = (void (*)(void *))zebra_client_free;
718e3744 3307
d62a17ae 3308 /* Install configuration write function. */
3309 install_node(&table_node, config_write_table);
3310 install_node(&forwarding_node, config_write_forwarding);
718e3744 3311
d62a17ae 3312 install_element(VIEW_NODE, &show_ip_forwarding_cmd);
3313 install_element(CONFIG_NODE, &ip_forwarding_cmd);
3314 install_element(CONFIG_NODE, &no_ip_forwarding_cmd);
3315 install_element(ENABLE_NODE, &show_zebra_cmd);
3316 install_element(ENABLE_NODE, &show_zebra_client_cmd);
3317 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 3318
3319#ifdef HAVE_NETLINK
d62a17ae 3320 install_element(VIEW_NODE, &show_table_cmd);
3321 install_element(CONFIG_NODE, &config_table_cmd);
3322 install_element(CONFIG_NODE, &no_config_table_cmd);
718e3744 3323#endif /* HAVE_NETLINK */
3324
d62a17ae 3325 install_element(VIEW_NODE, &show_ipv6_forwarding_cmd);
3326 install_element(CONFIG_NODE, &ipv6_forwarding_cmd);
3327 install_element(CONFIG_NODE, &no_ipv6_forwarding_cmd);
7514fb77 3328
d62a17ae 3329 /* Route-map */
3330 zebra_route_map_init();
718e3744 3331}