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