]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
lib, zebra: Allow zapi to send down the tableid
[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)
996c9314
LB
192 strlcpy(data.l.netns_name, basename((char *)netns_name),
193 NS_NAMSIZ);
4691b65a
PG
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 720 if (p.prefixlen > IPV4_MAX_BITLEN) {
996c9314
LB
721 zlog_warn(
722 "%s: Specified prefix length %d is too large for a v4 address",
723 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
724 return -1;
725 }
726 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 727 l += IPV4_MAX_BYTELEN;
728 } else if (p.family == AF_INET6) {
ec93aa12 729 if (p.prefixlen > IPV6_MAX_BITLEN) {
996c9314
LB
730 zlog_warn(
731 "%s: Specified prefix length %d is to large for a v6 address",
732 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
733 return -1;
734 }
735 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
d62a17ae 736 l += IPV6_MAX_BYTELEN;
737 } else {
738 zlog_err(
739 "rnh_register: Received unknown family type %d\n",
740 p.family);
741 return -1;
742 }
743 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type);
744 if (type == RNH_NEXTHOP_TYPE) {
745 if (flags
746 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
747 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
748 else if (!flags
749 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
750 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
751 } else if (type == RNH_IMPORT_CHECK_TYPE) {
752 if (flags
753 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
754 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
9d303b37
DL
755 else if (!flags && CHECK_FLAG(rnh->flags,
756 ZEBRA_NHT_EXACT_MATCH))
d62a17ae 757 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
758 }
759
760 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
761 /* Anything not AF_INET/INET6 has been filtered out above */
762 zebra_evaluate_rnh(zvrf_id(zvrf), p.family, 1, type, &p);
078430f6 763 }
ec93aa12
DS
764
765stream_failure:
d62a17ae 766 return 0;
767}
768
769/* Nexthop register */
e4bd522a 770static int zserv_rnh_unregister(struct zserv *client, u_short length,
d62a17ae 771 rnh_type_t type, struct zebra_vrf *zvrf)
772{
773 struct rnh *rnh;
774 struct stream *s;
775 struct prefix p;
776 u_short l = 0;
777
778 if (IS_ZEBRA_DEBUG_NHT)
779 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
780 zebra_route_string(client->proto), length);
781
782 s = client->ibuf;
783
784 while (l < length) {
f49e0f01
DS
785 uint8_t flags;
786
787 STREAM_GETC(s, flags);
788 if (flags != 0)
789 goto stream_failure;
ec93aa12 790
ec93aa12
DS
791 STREAM_GETW(s, p.family);
792 STREAM_GETC(s, p.prefixlen);
d62a17ae 793 l += 4;
794 if (p.family == AF_INET) {
ec93aa12 795 if (p.prefixlen > IPV4_MAX_BITLEN) {
996c9314
LB
796 zlog_warn(
797 "%s: Specified prefix length %d is to large for a v4 address",
798 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
799 return -1;
800 }
801 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 802 l += IPV4_MAX_BYTELEN;
803 } else if (p.family == AF_INET6) {
ec93aa12 804 if (p.prefixlen > IPV6_MAX_BITLEN) {
996c9314
LB
805 zlog_warn(
806 "%s: Specified prefix length %d is to large for a v6 address",
807 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
808 return -1;
809 }
810 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
d62a17ae 811 l += IPV6_MAX_BYTELEN;
812 } else {
813 zlog_err(
814 "rnh_register: Received unknown family type %d\n",
815 p.family);
816 return -1;
817 }
818 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
819 if (rnh) {
820 client->nh_dereg_time = monotime(NULL);
821 zebra_remove_rnh_client(rnh, client, type);
822 }
04b02fda 823 }
ec93aa12 824stream_failure:
d62a17ae 825 return 0;
fb018d25
DS
826}
827
b5ab78e6 828#define ZEBRA_MIN_FEC_LENGTH 5
7abc04e6 829
5aba114a 830/* FEC register */
e4bd522a 831static int zserv_fec_register(struct zserv *client, u_short length)
d62a17ae 832{
833 struct stream *s;
834 struct zebra_vrf *zvrf;
835 u_short l = 0;
836 struct prefix p;
837 u_int16_t flags;
838 u_int32_t label_index = MPLS_INVALID_LABEL_INDEX;
839
840 s = client->ibuf;
841 zvrf = vrf_info_lookup(VRF_DEFAULT);
842 if (!zvrf)
843 return 0; // unexpected
844
845 /*
846 * The minimum amount of data that can be sent for one fec
847 * registration
848 */
849 if (length < ZEBRA_MIN_FEC_LENGTH) {
850 zlog_err(
851 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
852 length);
853 return -1;
854 }
855
856 while (l < length) {
ec93aa12 857 STREAM_GETW(s, flags);
ad4527eb 858 memset(&p, 0, sizeof(p));
ec93aa12 859 STREAM_GETW(s, p.family);
d62a17ae 860 if (p.family != AF_INET && p.family != AF_INET6) {
861 zlog_err(
862 "fec_register: Received unknown family type %d\n",
863 p.family);
864 return -1;
865 }
ec93aa12 866 STREAM_GETC(s, p.prefixlen);
996c9314
LB
867 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
868 || (p.family == AF_INET6
869 && p.prefixlen > IPV6_MAX_BITLEN)) {
870 zlog_warn(
871 "%s: Specified prefix length: %d is to long for %d",
872 __PRETTY_FUNCTION__, p.prefixlen, p.family);
ec93aa12
DS
873 return -1;
874 }
d62a17ae 875 l += 5;
ec93aa12 876 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
d62a17ae 877 l += PSIZE(p.prefixlen);
878 if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
ec93aa12 879 STREAM_GETL(s, label_index);
d62a17ae 880 l += 4;
881 } else
882 label_index = MPLS_INVALID_LABEL_INDEX;
883 zebra_mpls_fec_register(zvrf, &p, label_index, client);
884 }
885
ec93aa12 886stream_failure:
d62a17ae 887 return 0;
5aba114a
DS
888}
889
890/* FEC unregister */
e4bd522a 891static int zserv_fec_unregister(struct zserv *client, u_short length)
d62a17ae 892{
893 struct stream *s;
894 struct zebra_vrf *zvrf;
895 u_short l = 0;
896 struct prefix p;
ec93aa12 897 uint16_t flags;
d62a17ae 898
899 s = client->ibuf;
900 zvrf = vrf_info_lookup(VRF_DEFAULT);
901 if (!zvrf)
902 return 0; // unexpected
903
904 /*
905 * The minimum amount of data that can be sent for one
906 * fec unregistration
907 */
908 if (length < ZEBRA_MIN_FEC_LENGTH) {
909 zlog_err(
910 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
911 length);
912 return -1;
913 }
914
915 while (l < length) {
ec93aa12 916 STREAM_GETW(s, flags);
e0b84ba1
DS
917 if (flags != 0)
918 goto stream_failure;
919
ad4527eb 920 memset(&p, 0, sizeof(p));
ec93aa12 921 STREAM_GETW(s, p.family);
d62a17ae 922 if (p.family != AF_INET && p.family != AF_INET6) {
923 zlog_err(
924 "fec_unregister: Received unknown family type %d\n",
925 p.family);
926 return -1;
927 }
ec93aa12 928 STREAM_GETC(s, p.prefixlen);
996c9314
LB
929 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
930 || (p.family == AF_INET6
931 && p.prefixlen > IPV6_MAX_BITLEN)) {
932 zlog_warn(
933 "%s: Received prefix length %d which is greater than %d can support",
934 __PRETTY_FUNCTION__, p.prefixlen, p.family);
ec93aa12
DS
935 return -1;
936 }
d62a17ae 937 l += 5;
ec93aa12 938 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
d62a17ae 939 l += PSIZE(p.prefixlen);
940 zebra_mpls_fec_unregister(zvrf, &p, client);
941 }
942
ec93aa12 943stream_failure:
d62a17ae 944 return 0;
5aba114a
DS
945}
946
6f61a5a3
EM
947/*
948 Modified version of zsend_ipv4_nexthop_lookup():
949 Query unicast rib if nexthop is not found on mrib.
950 Returns both route metric and protocol distance.
951*/
d62a17ae 952static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
953 struct in_addr addr,
954 struct route_entry *re,
955 struct zebra_vrf *zvrf)
956{
957 struct stream *s;
958 unsigned long nump;
959 u_char num;
960 struct nexthop *nexthop;
961
962 /* Get output stream. */
963 s = client->obuf;
964 stream_reset(s);
965
966 /* Fill in result. */
7cf15b25 967 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
d62a17ae 968 stream_put_in_addr(s, &addr);
969
970 if (re) {
971 stream_putc(s, re->distance);
972 stream_putl(s, re->metric);
973 num = 0;
974 nump = stream_get_endp(
975 s); /* remember position for nexthop_num */
976 stream_putc(s, 0); /* reserve room for nexthop_num */
977 /* Only non-recursive routes are elegible to resolve the nexthop
978 * we
979 * are looking up. Therefore, we will just iterate over the top
980 * chain of nexthops. */
981 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
982 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
983 num += zsend_write_nexthop(s, nexthop);
984
985 stream_putc_at(s, nump, num); /* store nexthop_num */
986 } else {
987 stream_putc(s, 0); /* distance */
988 stream_putl(s, 0); /* metric */
989 stream_putc(s, 0); /* nexthop_num */
990 }
991
992 stream_putw_at(s, 0, stream_get_endp(s));
993
994 return zebra_server_send_message(client);
6f61a5a3
EM
995}
996
28610f7e 997int zsend_route_notify_owner(struct route_entry *re, struct prefix *p,
7ea7b86e
DS
998 enum zapi_route_notify_owner note)
999{
1000 struct zserv *client;
1001 struct stream *s;
1002 uint8_t blen;
1003
28610f7e 1004 client = zebra_find_client(re->type, re->instance);
e1a1880d
DS
1005 if (!client || !client->notify_owner) {
1006 if (IS_ZEBRA_DEBUG_PACKET) {
1007 char buff[PREFIX_STRLEN];
1008
28610f7e 1009 zlog_debug(
eaa23e02 1010 "Not Notifying Owner: %u about prefix %s(%u) %d",
28610f7e 1011 re->type, prefix2str(p, buff, sizeof(buff)),
eaa23e02 1012 re->table, note);
e1a1880d
DS
1013 }
1014 return 0;
7ea7b86e
DS
1015 }
1016
eaa23e02
DS
1017 if (IS_ZEBRA_DEBUG_PACKET) {
1018 char buff[PREFIX_STRLEN];
1019
1020 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d",
1021 re->type, prefix2str(p, buff, sizeof(buff)),
1022 re->table, note);
1023 }
1024
7ea7b86e
DS
1025 s = client->obuf;
1026 stream_reset(s);
1027
28610f7e 1028 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, re->vrf_id);
7ea7b86e
DS
1029
1030 stream_put(s, &note, sizeof(note));
1031
1032 stream_putc(s, p->family);
1033
1034 blen = prefix_blen(p);
1035 stream_putc(s, p->prefixlen);
1036 stream_put(s, &p->u.prefix, blen);
1037
28610f7e
DS
1038 stream_putl(s, re->table);
1039
7ea7b86e
DS
1040 stream_putw_at(s, 0, stream_get_endp(s));
1041
1042 return zebra_server_send_message(client);
1043}
1044
18a6dce6 1045/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
d62a17ae 1046int zsend_router_id_update(struct zserv *client, struct prefix *p,
1047 vrf_id_t vrf_id)
18a6dce6 1048{
d62a17ae 1049 struct stream *s;
1050 int blen;
18a6dce6 1051
d62a17ae 1052 /* Check this client need interface information. */
1053 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
1054 return 0;
18a6dce6 1055
d62a17ae 1056 s = client->obuf;
1057 stream_reset(s);
18a6dce6 1058
d62a17ae 1059 /* Message type. */
7cf15b25 1060 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
18a6dce6 1061
d62a17ae 1062 /* Prefix information. */
1063 stream_putc(s, p->family);
1064 blen = prefix_blen(p);
1065 stream_put(s, &p->u.prefix, blen);
1066 stream_putc(s, p->prefixlen);
18a6dce6 1067
d62a17ae 1068 /* Write packet size. */
1069 stream_putw_at(s, 0, stream_get_endp(s));
18a6dce6 1070
d62a17ae 1071 return zebra_server_send_message(client);
18a6dce6 1072}
6b0655a2 1073
6833ae01 1074/*
1075 * Function used by Zebra to send a PW status update to LDP daemon
1076 */
1077int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
1078{
1079 struct stream *s;
1080
1081 s = client->obuf;
1082 stream_reset(s);
1083
7cf15b25 1084 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
6833ae01 1085 stream_write(s, pw->ifname, IF_NAMESIZE);
1086 stream_putl(s, pw->ifindex);
1087 stream_putl(s, pw->status);
1088
1089 /* Put length at the first point of the stream. */
1090 stream_putw_at(s, 0, stream_get_endp(s));
1091
1092 return zebra_server_send_message(client);
1093}
1094
718e3744 1095/* Register zebra server interface information. Send current all
1096 interface and address information. */
d62a17ae 1097static int zread_interface_add(struct zserv *client, u_short length,
1098 struct zebra_vrf *zvrf)
718e3744 1099{
d62a17ae 1100 struct vrf *vrf;
d62a17ae 1101 struct interface *ifp;
718e3744 1102
d62a17ae 1103 /* Interface information is needed. */
1104 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
718e3744 1105
a2addae8 1106 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
451fda4f 1107 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 1108 /* Skip pseudo interface. */
1109 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1110 continue;
718e3744 1111
d62a17ae 1112 if (zsend_interface_add(client, ifp) < 0)
1113 return -1;
718e3744 1114
d62a17ae 1115 if (zsend_interface_addresses(client, ifp) < 0)
1116 return -1;
1117 }
1118 }
1119 return 0;
718e3744 1120}
1121
1122/* Unregister zebra server interface information. */
d62a17ae 1123static int zread_interface_delete(struct zserv *client, u_short length,
1124 struct zebra_vrf *zvrf)
718e3744 1125{
d62a17ae 1126 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1127 return 0;
718e3744 1128}
1129
d62a17ae 1130void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1131 const unsigned int nexthop_num)
6878b9db 1132{
d62a17ae 1133 if (nexthop_num > multipath_num) {
1134 char buff[PREFIX2STR_BUFFER];
1135 prefix2str(p, buff, sizeof(buff));
1136 zlog_warn(
1137 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1138 caller, buff, nexthop_num, multipath_num);
1139 }
6878b9db
DS
1140}
1141
0e51b4a3
RW
1142static int zread_route_add(struct zserv *client, u_short length,
1143 struct zebra_vrf *zvrf)
1144{
1145 struct stream *s;
1146 struct zapi_route api;
1147 struct zapi_nexthop *api_nh;
1148 afi_t afi;
1149 struct prefix_ipv6 *src_p = NULL;
1150 struct route_entry *re;
98ca91e9 1151 struct nexthop *nexthop = NULL;
0e51b4a3 1152 int i, ret;
2dbad57f 1153 vrf_id_t vrf_id = 0;
0e51b4a3
RW
1154
1155 s = client->ibuf;
1156 if (zapi_route_decode(s, &api) < 0)
1157 return -1;
1158
1159 /* Allocate new route. */
2dbad57f 1160 vrf_id = zvrf_id(zvrf);
0e51b4a3
RW
1161 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1162 re->type = api.type;
1163 re->instance = api.instance;
1164 re->flags = api.flags;
1165 re->uptime = time(NULL);
2dbad57f 1166 re->vrf_id = vrf_id;
ba1849ef
DS
1167 if (api.tableid && vrf_id == VRF_DEFAULT)
1168 re->table = api.tableid;
1169 else
1170 re->table = zvrf->table_id;
0e51b4a3
RW
1171
1172 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1173 for (i = 0; i < api.nexthop_num; i++) {
1174 api_nh = &api.nexthops[i];
d3135ba3 1175 ifindex_t ifindex = 0;
0e51b4a3
RW
1176
1177 switch (api_nh->type) {
1178 case NEXTHOP_TYPE_IFINDEX:
ec93aa12 1179 nexthop = route_entry_nexthop_ifindex_add(
c7bacffe 1180 re, api_nh->ifindex, api_nh->vrf_id);
0e51b4a3
RW
1181 break;
1182 case NEXTHOP_TYPE_IPV4:
1183 nexthop = route_entry_nexthop_ipv4_add(
4a7371e9 1184 re, &api_nh->gate.ipv4, NULL,
c7bacffe 1185 api_nh->vrf_id);
0e51b4a3 1186 break;
2dbad57f 1187 case NEXTHOP_TYPE_IPV4_IFINDEX: {
1188
1189 struct ipaddr vtep_ip;
1190
1191 memset(&vtep_ip, 0, sizeof(struct ipaddr));
d3135ba3 1192 if (CHECK_FLAG(api.flags,
90264d64 1193 ZEBRA_FLAG_EVPN_ROUTE)) {
996c9314 1194 ifindex = get_l3vni_svi_ifindex(vrf_id);
d3135ba3 1195 } else {
1196 ifindex = api_nh->ifindex;
1197 }
2dbad57f 1198
0e51b4a3 1199 nexthop = route_entry_nexthop_ipv4_ifindex_add(
4a7371e9 1200 re, &api_nh->gate.ipv4, NULL, ifindex,
c7bacffe 1201 api_nh->vrf_id);
2dbad57f 1202
1203 /* if this an EVPN route entry,
523cafc4 1204 program the nh as neigh
1205 */
2dbad57f 1206 if (CHECK_FLAG(api.flags,
90264d64 1207 ZEBRA_FLAG_EVPN_ROUTE)) {
19a847a9
MK
1208 SET_FLAG(nexthop->flags,
1209 NEXTHOP_FLAG_EVPN_RVTEP);
2dbad57f 1210 vtep_ip.ipa_type = IPADDR_V4;
1211 memcpy(&(vtep_ip.ipaddr_v4),
1212 &(api_nh->gate.ipv4),
1213 sizeof(struct in_addr));
1214 zebra_vxlan_evpn_vrf_route_add(
996c9314
LB
1215 vrf_id, &api.rmac, &vtep_ip,
1216 &api.prefix);
2dbad57f 1217 }
0e51b4a3 1218 break;
2dbad57f 1219 }
0e51b4a3
RW
1220 case NEXTHOP_TYPE_IPV6:
1221 nexthop = route_entry_nexthop_ipv6_add(
c7bacffe 1222 re, &api_nh->gate.ipv6, api_nh->vrf_id);
0e51b4a3
RW
1223 break;
1224 case NEXTHOP_TYPE_IPV6_IFINDEX:
1225 nexthop = route_entry_nexthop_ipv6_ifindex_add(
4a7371e9 1226 re, &api_nh->gate.ipv6, api_nh->ifindex,
c7bacffe 1227 api_nh->vrf_id);
0e51b4a3
RW
1228 break;
1229 case NEXTHOP_TYPE_BLACKHOLE:
ec93aa12 1230 nexthop = route_entry_nexthop_blackhole_add(
60466a63 1231 re, api_nh->bh_type);
0e51b4a3
RW
1232 break;
1233 }
1234
ec93aa12 1235 if (!nexthop) {
996c9314
LB
1236 zlog_warn(
1237 "%s: Nexthops Specified: %d but we failed to properly create one",
1238 __PRETTY_FUNCTION__, api.nexthop_num);
ec93aa12
DS
1239 nexthops_free(re->nexthop);
1240 XFREE(MTYPE_RE, re);
1241 return -1;
1242 }
0e51b4a3
RW
1243 /* MPLS labels for BGP-LU or Segment Routing */
1244 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1245 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1246 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1247 enum lsp_types_t label_type;
1248
1249 label_type =
1250 lsp_type_from_re_type(client->proto);
52dd3aa4
RW
1251 nexthop_add_labels(nexthop, label_type,
1252 api_nh->label_num,
1253 &api_nh->labels[0]);
0e51b4a3
RW
1254 }
1255 }
1256 }
1257
1258 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1259 re->distance = api.distance;
1260 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1261 re->metric = api.metric;
1262 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1263 re->tag = api.tag;
1264 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1265 re->mtu = api.mtu;
1266
1267 afi = family2afi(api.prefix.family);
ec93aa12
DS
1268 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1269 zlog_warn("%s: Received SRC Prefix but afi is not v6",
1270 __PRETTY_FUNCTION__);
1271 nexthops_free(re->nexthop);
1272 XFREE(MTYPE_RE, re);
1273 return -1;
1274 }
0e51b4a3
RW
1275 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1276 src_p = &api.src_prefix;
1277
1278 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1279
1280 /* Stats */
1281 switch (api.prefix.family) {
1282 case AF_INET:
1283 if (ret > 0)
1284 client->v4_route_add_cnt++;
1285 else if (ret < 0)
1286 client->v4_route_upd8_cnt++;
1287 break;
1288 case AF_INET6:
1289 if (ret > 0)
1290 client->v6_route_add_cnt++;
1291 else if (ret < 0)
1292 client->v6_route_upd8_cnt++;
1293 break;
1294 }
1295
1296 return 0;
1297}
1298
1299static int zread_route_del(struct zserv *client, u_short length,
1300 struct zebra_vrf *zvrf)
1301{
1302 struct stream *s;
1303 struct zapi_route api;
1304 afi_t afi;
1305 struct prefix_ipv6 *src_p = NULL;
1306
1307 s = client->ibuf;
1308 if (zapi_route_decode(s, &api) < 0)
1309 return -1;
1310
1311 afi = family2afi(api.prefix.family);
ec93aa12
DS
1312 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1313 zlog_warn("%s: Received a src prefix while afi is not v6",
1314 __PRETTY_FUNCTION__);
1315 return -1;
1316 }
0e51b4a3
RW
1317 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1318 src_p = &api.src_prefix;
1319
1320 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
a8309422 1321 api.flags, &api.prefix, src_p, NULL, zvrf->table_id,
6134fd82 1322 api.metric, false, &api.rmac);
0e51b4a3
RW
1323
1324 /* Stats */
1325 switch (api.prefix.family) {
1326 case AF_INET:
1327 client->v4_route_del_cnt++;
1328 break;
1329 case AF_INET6:
1330 client->v6_route_del_cnt++;
1331 break;
1332 }
1333
1334 return 0;
1335}
1336
718e3744 1337/* This function support multiple nexthop. */
d62a17ae 1338/*
f0f77c9a 1339 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
d62a17ae 1340 * add kernel route.
b9df2d25 1341 */
d62a17ae 1342static int zread_ipv4_add(struct zserv *client, u_short length,
1343 struct zebra_vrf *zvrf)
1344{
1345 int i;
1346 struct route_entry *re;
1347 struct prefix p;
1348 u_char message;
1349 struct in_addr nhop_addr;
1350 u_char nexthop_num;
1351 u_char nexthop_type;
1352 struct stream *s;
1353 ifindex_t ifindex;
1354 safi_t safi;
1355 int ret;
a97986ff 1356 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1357 mpls_label_t label;
1358 struct nexthop *nexthop;
a8309422 1359 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1360
1361 /* Get input stream. */
1362 s = client->ibuf;
1363
1364 /* Allocate new re. */
1365 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1366
1367 /* Type, flags, message. */
ec93aa12
DS
1368 STREAM_GETC(s, re->type);
1369 if (re->type > ZEBRA_ROUTE_MAX) {
1370 zlog_warn("%s: Specified route type %d is not a legal value\n",
1371 __PRETTY_FUNCTION__, re->type);
1372 XFREE(MTYPE_RE, re);
1373 return -1;
1374 }
1375 STREAM_GETW(s, re->instance);
1376 STREAM_GETL(s, re->flags);
1377 STREAM_GETC(s, message);
1378 STREAM_GETW(s, safi);
d62a17ae 1379 re->uptime = time(NULL);
1380
1381 /* IPv4 prefix. */
1382 memset(&p, 0, sizeof(struct prefix_ipv4));
1383 p.family = AF_INET;
ec93aa12
DS
1384 STREAM_GETC(s, p.prefixlen);
1385 if (p.prefixlen > IPV4_MAX_BITLEN) {
996c9314
LB
1386 zlog_warn(
1387 "%s: Specified prefix length %d is greater than what v4 can be",
1388 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
1389 XFREE(MTYPE_RE, re);
1390 return -1;
1391 }
1392 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1393
1394 /* VRF ID */
1395 re->vrf_id = zvrf_id(zvrf);
1396
1397 /* Nexthop parse. */
1398 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
ec93aa12 1399 STREAM_GETC(s, nexthop_num);
d62a17ae 1400 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1401 nexthop_num);
1402
81c11e3f
RW
1403 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1404 label_type = lsp_type_from_re_type(client->proto);
1405
d62a17ae 1406 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1407 STREAM_GETC(s, nexthop_type);
d62a17ae 1408
1409 switch (nexthop_type) {
1410 case NEXTHOP_TYPE_IFINDEX:
ec93aa12 1411 STREAM_GETL(s, ifindex);
4a7371e9
DS
1412 route_entry_nexthop_ifindex_add(re, ifindex,
1413 re->vrf_id);
d62a17ae 1414 break;
1415 case NEXTHOP_TYPE_IPV4:
ec93aa12
DS
1416 STREAM_GET(&nhop_addr.s_addr, s,
1417 IPV4_MAX_BYTELEN);
d62a17ae 1418 nexthop = route_entry_nexthop_ipv4_add(
4a7371e9 1419 re, &nhop_addr, NULL, re->vrf_id);
d62a17ae 1420 /* For labeled-unicast, each nexthop is followed
1421 * by label. */
1422 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
ec93aa12 1423 STREAM_GETL(s, label);
81c11e3f
RW
1424 nexthop_add_labels(nexthop, label_type,
1425 1, &label);
d62a17ae 1426 }
1427 break;
1428 case NEXTHOP_TYPE_IPV4_IFINDEX:
ec93aa12
DS
1429 STREAM_GET(&nhop_addr.s_addr, s,
1430 IPV4_MAX_BYTELEN);
1431 STREAM_GETL(s, ifindex);
d62a17ae 1432 route_entry_nexthop_ipv4_ifindex_add(
4a7371e9
DS
1433 re, &nhop_addr, NULL, ifindex,
1434 re->vrf_id);
d62a17ae 1435 break;
1436 case NEXTHOP_TYPE_IPV6:
996c9314
LB
1437 zlog_warn(
1438 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass v6 nexthops",
1439 __PRETTY_FUNCTION__);
ec93aa12
DS
1440 nexthops_free(re->nexthop);
1441 XFREE(MTYPE_RE, re);
1442 return -1;
d62a17ae 1443 break;
1444 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1445 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1446 break;
ec93aa12 1447 default:
996c9314
LB
1448 zlog_warn(
1449 "%s: Specified nexthop type: %d does not exist",
1450 __PRETTY_FUNCTION__, nexthop_type);
ec93aa12
DS
1451 nexthops_free(re->nexthop);
1452 XFREE(MTYPE_RE, re);
1453 return -1;
d62a17ae 1454 }
1455 }
718e3744 1456 }
718e3744 1457
d62a17ae 1458 /* Distance. */
1459 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1460 STREAM_GETC(s, re->distance);
718e3744 1461
d62a17ae 1462 /* Metric. */
1463 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1464 STREAM_GETL(s, re->metric);
0d9551dc 1465
d62a17ae 1466 /* Tag */
1467 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1468 STREAM_GETL(s, re->tag);
d62a17ae 1469 else
1470 re->tag = 0;
c50ca33a 1471
d62a17ae 1472 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1473 STREAM_GETL(s, re->mtu);
d62a17ae 1474 else
1475 re->mtu = 0;
1476
1477 /* Table */
1478 re->table = zvrf->table_id;
12f6fb97 1479
d62a17ae 1480 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
04b02fda 1481
d62a17ae 1482 /* Stats */
1483 if (ret > 0)
1484 client->v4_route_add_cnt++;
1485 else if (ret < 0)
1486 client->v4_route_upd8_cnt++;
ec93aa12 1487
d62a17ae 1488 return 0;
ec93aa12
DS
1489
1490stream_failure:
1491 nexthops_free(re->nexthop);
1492 XFREE(MTYPE_RE, re);
1493 return -1;
718e3744 1494}
1495
1496/* Zebra server IPv4 prefix delete function. */
d62a17ae 1497static int zread_ipv4_delete(struct zserv *client, u_short length,
1498 struct zebra_vrf *zvrf)
1499{
d62a17ae 1500 struct stream *s;
1501 struct zapi_ipv4 api;
d62a17ae 1502 struct prefix p;
d62a17ae 1503 u_int32_t table_id;
1504
1505 s = client->ibuf;
d62a17ae 1506
1507 /* Type, flags, message. */
ec93aa12
DS
1508 STREAM_GETC(s, api.type);
1509 STREAM_GETW(s, api.instance);
1510 STREAM_GETL(s, api.flags);
1511 STREAM_GETC(s, api.message);
1512 STREAM_GETW(s, api.safi);
d62a17ae 1513
1514 /* IPv4 prefix. */
1515 memset(&p, 0, sizeof(struct prefix));
1516 p.family = AF_INET;
ec93aa12
DS
1517 STREAM_GETC(s, p.prefixlen);
1518 if (p.prefixlen > IPV4_MAX_BITLEN) {
1519 zlog_warn("%s: Passed in prefixlen %d is impossible",
1520 __PRETTY_FUNCTION__, p.prefixlen);
1521 return -1;
1522 }
1523 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1524
d62a17ae 1525 table_id = zvrf->table_id;
1526
1527 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
6134fd82 1528 api.flags, &p, NULL, NULL, table_id, 0, false, NULL);
d62a17ae 1529 client->v4_route_del_cnt++;
ec93aa12
DS
1530
1531stream_failure:
d62a17ae 1532 return 0;
718e3744 1533}
1534
6f61a5a3 1535/* MRIB Nexthop lookup for IPv4. */
d62a17ae 1536static int zread_ipv4_nexthop_lookup_mrib(struct zserv *client, u_short length,
1537 struct zebra_vrf *zvrf)
6f61a5a3 1538{
d62a17ae 1539 struct in_addr addr;
1540 struct route_entry *re;
6f61a5a3 1541
ec93aa12 1542 STREAM_GET(&addr.s_addr, client->ibuf, IPV4_MAX_BYTELEN);
d62a17ae 1543 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1544 return zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
ec93aa12
DS
1545
1546stream_failure:
1547 return -1;
6f61a5a3
EM
1548}
1549
8a92a8a0 1550/* Zebra server IPv6 prefix add function. */
d62a17ae 1551static int zread_ipv4_route_ipv6_nexthop_add(struct zserv *client,
1552 u_short length,
1553 struct zebra_vrf *zvrf)
1554{
1555 unsigned int i;
1556 struct stream *s;
1557 struct in6_addr nhop_addr;
1558 struct route_entry *re;
1559 u_char message;
1560 u_char nexthop_num;
1561 u_char nexthop_type;
1562 struct prefix p;
1563 safi_t safi;
1564 static struct in6_addr nexthops[MULTIPATH_NUM];
1565 static unsigned int ifindices[MULTIPATH_NUM];
1566 int ret;
1567 static mpls_label_t labels[MULTIPATH_NUM];
98ca91e9 1568 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1569 mpls_label_t label;
1570 struct nexthop *nexthop;
a8309422 1571 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1572
1573 /* Get input stream. */
1574 s = client->ibuf;
1575
1576 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1577
1578 /* Allocate new re. */
1579 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1580
1581 /* Type, flags, message. */
ec93aa12
DS
1582 STREAM_GETC(s, re->type);
1583 if (re->type > ZEBRA_ROUTE_MAX) {
1584 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1585 __PRETTY_FUNCTION__, re->type);
1586 XFREE(MTYPE_RE, re);
1587 return -1;
1588 }
1589 STREAM_GETW(s, re->instance);
1590 STREAM_GETL(s, re->flags);
1591 STREAM_GETC(s, message);
1592 STREAM_GETW(s, safi);
d62a17ae 1593 re->uptime = time(NULL);
1594
1595 /* IPv4 prefix. */
1596 memset(&p, 0, sizeof(struct prefix_ipv4));
1597 p.family = AF_INET;
ec93aa12
DS
1598 STREAM_GETC(s, p.prefixlen);
1599 if (p.prefixlen > IPV4_MAX_BITLEN) {
996c9314
LB
1600 zlog_warn(
1601 "%s: Prefix Length %d is greater than what a v4 address can use",
1602 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
1603 XFREE(MTYPE_RE, re);
1604 return -1;
1605 }
1606 STREAM_GET(&p.u.prefix4, s, PSIZE(p.prefixlen));
d62a17ae 1607
1608 /* VRF ID */
1609 re->vrf_id = zvrf_id(zvrf);
1610
1611 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1612 * to the re to ensure that IPv6 multipathing works; need to coalesce
1613 * these. Clients should send the same number of paired set of
1614 * next-hop-addr/next-hop-ifindices. */
1615 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1616 unsigned int nh_count = 0;
1617 unsigned int if_count = 0;
1618 unsigned int max_nh_if = 0;
1619
ec93aa12 1620 STREAM_GETC(s, nexthop_num);
d62a17ae 1621 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1622 nexthop_num);
81c11e3f
RW
1623
1624 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1625 label_type = lsp_type_from_re_type(client->proto);
1626
d62a17ae 1627 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1628 STREAM_GETC(s, nexthop_type);
d62a17ae 1629
1630 switch (nexthop_type) {
1631 case NEXTHOP_TYPE_IPV6:
ec93aa12 1632 STREAM_GET(&nhop_addr, s, 16);
d62a17ae 1633 if (nh_count < MULTIPATH_NUM) {
1634 /* For labeled-unicast, each nexthop is
1635 * followed by label. */
1636 if (CHECK_FLAG(message,
1637 ZAPI_MESSAGE_LABEL)) {
ec93aa12 1638 STREAM_GETL(s, label);
d62a17ae 1639 labels[nh_count] = label;
1640 }
1641 nexthops[nh_count] = nhop_addr;
1642 nh_count++;
1643 }
1644 break;
1645 case NEXTHOP_TYPE_IFINDEX:
1646 if (if_count < multipath_num) {
ec93aa12 1647 STREAM_GETL(s, ifindices[if_count++]);
d62a17ae 1648 }
1649 break;
1650 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1651 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1652 break;
ec93aa12 1653 default:
996c9314
LB
1654 zlog_warn(
1655 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1656 __PRETTY_FUNCTION__);
ec93aa12
DS
1657 nexthops_free(re->nexthop);
1658 XFREE(MTYPE_RE, re);
1659 return -1;
d62a17ae 1660 }
1661 }
1662
1663 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1664 for (i = 0; i < max_nh_if; i++) {
1665 if ((i < nh_count)
1666 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1667 if ((i < if_count) && ifindices[i])
1668 nexthop =
1669 route_entry_nexthop_ipv6_ifindex_add(
1670 re, &nexthops[i],
4a7371e9
DS
1671 ifindices[i],
1672 re->vrf_id);
d62a17ae 1673 else
1674 nexthop = route_entry_nexthop_ipv6_add(
4a7371e9 1675 re, &nexthops[i], re->vrf_id);
d62a17ae 1676
1677 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
81c11e3f
RW
1678 nexthop_add_labels(nexthop, label_type,
1679 1, &labels[i]);
d62a17ae 1680 } else {
1681 if ((i < if_count) && ifindices[i])
1682 route_entry_nexthop_ifindex_add(
4a7371e9 1683 re, ifindices[i], re->vrf_id);
d62a17ae 1684 }
1685 }
8a92a8a0
DS
1686 }
1687
d62a17ae 1688 /* Distance. */
1689 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1690 STREAM_GETC(s, re->distance);
41fc2714 1691
d62a17ae 1692 /* Metric. */
1693 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1694 STREAM_GETL(s, re->metric);
d62a17ae 1695
1696 /* Tag */
1697 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1698 STREAM_GETL(s, re->tag);
d62a17ae 1699 else
1700 re->tag = 0;
1701
1702 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1703 STREAM_GETL(s, re->mtu);
d62a17ae 1704 else
1705 re->mtu = 0;
1706
1707 /* Table */
1708 re->table = zvrf->table_id;
1709
1710 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1711 /* Stats */
1712 if (ret > 0)
1713 client->v4_route_add_cnt++;
1714 else if (ret < 0)
1715 client->v4_route_upd8_cnt++;
1716
1717 return 0;
ec93aa12
DS
1718
1719stream_failure:
1720 nexthops_free(re->nexthop);
1721 XFREE(MTYPE_RE, re);
1722 return -1;
d62a17ae 1723}
1724
1725static int zread_ipv6_add(struct zserv *client, u_short length,
1726 struct zebra_vrf *zvrf)
1727{
1728 unsigned int i;
1729 struct stream *s;
1730 struct in6_addr nhop_addr;
f38efb80 1731 ifindex_t ifindex;
d62a17ae 1732 struct route_entry *re;
1733 u_char message;
1734 u_char nexthop_num;
1735 u_char nexthop_type;
1736 struct prefix p;
1737 struct prefix_ipv6 src_p, *src_pp;
1738 safi_t safi;
1739 static struct in6_addr nexthops[MULTIPATH_NUM];
1740 static unsigned int ifindices[MULTIPATH_NUM];
1741 int ret;
1742 static mpls_label_t labels[MULTIPATH_NUM];
98ca91e9 1743 enum lsp_types_t label_type = ZEBRA_LSP_NONE;
d62a17ae 1744 mpls_label_t label;
1745 struct nexthop *nexthop;
a8309422 1746 enum blackhole_type bh_type = BLACKHOLE_NULL;
d62a17ae 1747
1748 /* Get input stream. */
1749 s = client->ibuf;
1750
1751 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1752
1753 /* Allocate new re. */
1754 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1755
1756 /* Type, flags, message. */
ec93aa12
DS
1757 STREAM_GETC(s, re->type);
1758 if (re->type > ZEBRA_ROUTE_MAX) {
1759 zlog_warn("%s: Specified route type: %d is not a legal value\n",
1760 __PRETTY_FUNCTION__, re->type);
1761 XFREE(MTYPE_RE, re);
1762 return -1;
1763 }
1764 STREAM_GETW(s, re->instance);
1765 STREAM_GETL(s, re->flags);
1766 STREAM_GETC(s, message);
1767 STREAM_GETW(s, safi);
d62a17ae 1768 re->uptime = time(NULL);
1769
1770 /* IPv6 prefix. */
ec93aa12 1771 memset(&p, 0, sizeof(p));
d62a17ae 1772 p.family = AF_INET6;
ec93aa12
DS
1773 STREAM_GETC(s, p.prefixlen);
1774 if (p.prefixlen > IPV6_MAX_BITLEN) {
996c9314
LB
1775 zlog_warn(
1776 "%s: Specified prefix length %d is to large for v6 prefix",
1777 __PRETTY_FUNCTION__, p.prefixlen);
ec93aa12
DS
1778 XFREE(MTYPE_RE, re);
1779 return -1;
1780 }
1781 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
d62a17ae 1782
1783 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
ec93aa12 1784 memset(&src_p, 0, sizeof(src_p));
d62a17ae 1785 src_p.family = AF_INET6;
ec93aa12
DS
1786 STREAM_GETC(s, src_p.prefixlen);
1787 if (src_p.prefixlen > IPV6_MAX_BITLEN) {
996c9314
LB
1788 zlog_warn(
1789 "%s: Specified src prefix length %d is to large for v6 prefix",
1790 __PRETTY_FUNCTION__, src_p.prefixlen);
ec93aa12
DS
1791 XFREE(MTYPE_RE, re);
1792 return -1;
1793 }
1794 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
d62a17ae 1795 src_pp = &src_p;
1796 } else
1797 src_pp = NULL;
1798
4a7371e9
DS
1799 /* VRF ID */
1800 re->vrf_id = zvrf_id(zvrf);
1801
d62a17ae 1802 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1803 * to the re to ensure that IPv6 multipathing works; need to coalesce
1804 * these. Clients should send the same number of paired set of
1805 * next-hop-addr/next-hop-ifindices. */
1806 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1807 unsigned int nh_count = 0;
1808 unsigned int if_count = 0;
1809 unsigned int max_nh_if = 0;
1810
ec93aa12 1811 STREAM_GETC(s, nexthop_num);
d62a17ae 1812 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1813 nexthop_num);
81c11e3f
RW
1814
1815 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1816 label_type = lsp_type_from_re_type(client->proto);
1817
d62a17ae 1818 for (i = 0; i < nexthop_num; i++) {
ec93aa12 1819 STREAM_GETC(s, nexthop_type);
d62a17ae 1820
1821 switch (nexthop_type) {
1822 case NEXTHOP_TYPE_IPV6:
ec93aa12 1823 STREAM_GET(&nhop_addr, s, 16);
d62a17ae 1824 if (nh_count < MULTIPATH_NUM) {
1825 /* For labeled-unicast, each nexthop is
1826 * followed by label. */
1827 if (CHECK_FLAG(message,
1828 ZAPI_MESSAGE_LABEL)) {
ec93aa12 1829 STREAM_GETL(s, label);
d62a17ae 1830 labels[nh_count] = label;
1831 }
1832 nexthops[nh_count++] = nhop_addr;
1833 }
1834 break;
f38efb80 1835 case NEXTHOP_TYPE_IPV6_IFINDEX:
ec93aa12
DS
1836 STREAM_GET(&nhop_addr, s, 16);
1837 STREAM_GETL(s, ifindex);
f38efb80 1838 route_entry_nexthop_ipv6_ifindex_add(
4a7371e9 1839 re, &nhop_addr, ifindex, re->vrf_id);
f38efb80 1840 break;
d62a17ae 1841 case NEXTHOP_TYPE_IFINDEX:
1842 if (if_count < multipath_num) {
ec93aa12 1843 STREAM_GETL(s, ifindices[if_count++]);
d62a17ae 1844 }
1845 break;
1846 case NEXTHOP_TYPE_BLACKHOLE:
a8309422 1847 route_entry_nexthop_blackhole_add(re, bh_type);
d62a17ae 1848 break;
ec93aa12 1849 default:
996c9314
LB
1850 zlog_warn(
1851 "%s: Please use ZEBRA_ROUTE_ADD if you want to pass non v6 nexthops",
1852 __PRETTY_FUNCTION__);
ec93aa12
DS
1853 nexthops_free(re->nexthop);
1854 XFREE(MTYPE_RE, re);
1855 return -1;
d62a17ae 1856 }
1857 }
1858
1859 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1860 for (i = 0; i < max_nh_if; i++) {
1861 if ((i < nh_count)
1862 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1863 if ((i < if_count) && ifindices[i])
1864 nexthop =
1865 route_entry_nexthop_ipv6_ifindex_add(
1866 re, &nexthops[i],
4a7371e9
DS
1867 ifindices[i],
1868 re->vrf_id);
d62a17ae 1869 else
1870 nexthop = route_entry_nexthop_ipv6_add(
4a7371e9 1871 re, &nexthops[i], re->vrf_id);
d62a17ae 1872 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
81c11e3f
RW
1873 nexthop_add_labels(nexthop, label_type,
1874 1, &labels[i]);
d62a17ae 1875 } else {
1876 if ((i < if_count) && ifindices[i])
1877 route_entry_nexthop_ifindex_add(
4a7371e9 1878 re, ifindices[i], re->vrf_id);
d62a17ae 1879 }
1880 }
41fc2714 1881 }
718e3744 1882
d62a17ae 1883 /* Distance. */
1884 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
ec93aa12 1885 STREAM_GETC(s, re->distance);
d62a17ae 1886
1887 /* Metric. */
1888 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
ec93aa12 1889 STREAM_GETL(s, re->metric);
718e3744 1890
d62a17ae 1891 /* Tag */
1892 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
ec93aa12 1893 STREAM_GETL(s, re->tag);
d62a17ae 1894 else
1895 re->tag = 0;
c50ca33a 1896
d62a17ae 1897 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
ec93aa12 1898 STREAM_GETL(s, re->mtu);
d62a17ae 1899 else
1900 re->mtu = 0;
0d9551dc 1901
d62a17ae 1902 re->table = zvrf->table_id;
154caaed 1903
d62a17ae 1904 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
1905 /* Stats */
1906 if (ret > 0)
1907 client->v6_route_add_cnt++;
1908 else if (ret < 0)
1909 client->v6_route_upd8_cnt++;
04b02fda 1910
d62a17ae 1911 return 0;
ec93aa12
DS
1912
1913stream_failure:
1914 nexthops_free(re->nexthop);
1915 XFREE(MTYPE_RE, re);
1916
1917 return -1;
718e3744 1918}
1919
1920/* Zebra server IPv6 prefix delete function. */
d62a17ae 1921static int zread_ipv6_delete(struct zserv *client, u_short length,
1922 struct zebra_vrf *zvrf)
1923{
d62a17ae 1924 struct stream *s;
1925 struct zapi_ipv6 api;
d62a17ae 1926 struct prefix p;
1927 struct prefix_ipv6 src_p, *src_pp;
1928
1929 s = client->ibuf;
d62a17ae 1930
1931 /* Type, flags, message. */
ec93aa12
DS
1932 STREAM_GETC(s, api.type);
1933 STREAM_GETW(s, api.instance);
1934 STREAM_GETL(s, api.flags);
1935 STREAM_GETC(s, api.message);
1936 STREAM_GETW(s, api.safi);
d62a17ae 1937
1938 /* IPv4 prefix. */
1e9f448f 1939 memset(&p, 0, sizeof(struct prefix));
d62a17ae 1940 p.family = AF_INET6;
ec93aa12
DS
1941 STREAM_GETC(s, p.prefixlen);
1942 STREAM_GET(&p.u.prefix6, s, PSIZE(p.prefixlen));
d62a17ae 1943
1944 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1945 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1946 src_p.family = AF_INET6;
ec93aa12
DS
1947 STREAM_GETC(s, src_p.prefixlen);
1948 STREAM_GET(&src_p.prefix, s, PSIZE(src_p.prefixlen));
d62a17ae 1949 src_pp = &src_p;
1950 } else
1951 src_pp = NULL;
1952
d51b9e45 1953 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type, api.instance,
6134fd82 1954 api.flags, &p, src_pp, NULL, client->rtm_table, 0, false,
1955 NULL);
d62a17ae 1956
1957 client->v6_route_del_cnt++;
ec93aa12
DS
1958
1959stream_failure:
d62a17ae 1960 return 0;
718e3744 1961}
1962
18a6dce6 1963/* Register zebra server router-id information. Send current router-id */
d62a17ae 1964static int zread_router_id_add(struct zserv *client, u_short length,
1965 struct zebra_vrf *zvrf)
18a6dce6 1966{
d62a17ae 1967 struct prefix p;
18a6dce6 1968
d62a17ae 1969 /* Router-id information is needed. */
1970 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
18a6dce6 1971
d62a17ae 1972 router_id_get(&p, zvrf_id(zvrf));
18a6dce6 1973
d62a17ae 1974 return zsend_router_id_update(client, &p, zvrf_id(zvrf));
18a6dce6 1975}
1976
1977/* Unregister zebra server router-id information. */
d62a17ae 1978static int zread_router_id_delete(struct zserv *client, u_short length,
1979 struct zebra_vrf *zvrf)
18a6dce6 1980{
d62a17ae 1981 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1982 return 0;
18a6dce6 1983}
1984
2ea1ab1c 1985/* Tie up route-type and client->sock */
d62a17ae 1986static void zread_hello(struct zserv *client)
2ea1ab1c 1987{
d62a17ae 1988 /* type of protocol (lib/zebra.h) */
1989 u_char proto;
1990 u_short instance;
e1a1880d 1991 u_char notify;
7c8ff89e 1992
ec93aa12
DS
1993 STREAM_GETC(client->ibuf, proto);
1994 STREAM_GETW(client->ibuf, instance);
e1a1880d
DS
1995 STREAM_GETC(client->ibuf, notify);
1996 if (notify)
1997 client->notify_owner = true;
2ea1ab1c 1998
d62a17ae 1999 /* accept only dynamic routing protocols */
2000 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
2001 zlog_notice(
2002 "client %d says hello and bids fair to announce only %s routes",
2003 client->sock, zebra_route_string(proto));
2004 if (instance)
2005 zlog_notice("client protocol instance %d", instance);
2ea1ab1c 2006
d62a17ae 2007 client->proto = proto;
2008 client->instance = instance;
2009 }
ec93aa12
DS
2010
2011stream_failure:
2012 return;
2ea1ab1c
VT
2013}
2014
7076bb2f 2015/* Unregister all information in a VRF. */
d62a17ae 2016static int zread_vrf_unregister(struct zserv *client, u_short length,
2017 struct zebra_vrf *zvrf)
2018{
2019 int i;
2020 afi_t afi;
2021
2022 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2023 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2024 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
2025 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
2026 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
2027 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
2028
2029 return 0;
2030}
2031
2032static void zread_mpls_labels(int command, struct zserv *client, u_short length,
6680688c 2033 struct zebra_vrf *zvrf)
d62a17ae 2034{
2035 struct stream *s;
2036 enum lsp_types_t type;
2037 struct prefix prefix;
2038 enum nexthop_types_t gtype;
2039 union g_addr gate;
2040 ifindex_t ifindex;
2041 mpls_label_t in_label, out_label;
2042 u_int8_t distance;
d62a17ae 2043
2044 /* Get input stream. */
2045 s = client->ibuf;
2046
2047 /* Get data. */
ec93aa12
DS
2048 STREAM_GETC(s, type);
2049 STREAM_GETL(s, prefix.family);
d62a17ae 2050 switch (prefix.family) {
2051 case AF_INET:
ec93aa12
DS
2052 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
2053 STREAM_GETC(s, prefix.prefixlen);
2054 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
996c9314
LB
2055 zlog_warn(
2056 "%s: Specified prefix length %d is greater than a v4 address can support",
2057 __PRETTY_FUNCTION__, prefix.prefixlen);
ec93aa12
DS
2058 return;
2059 }
2060 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
d62a17ae 2061 break;
2062 case AF_INET6:
ec93aa12
DS
2063 STREAM_GET(&prefix.u.prefix6, s, 16);
2064 STREAM_GETC(s, prefix.prefixlen);
2065 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
996c9314
LB
2066 zlog_warn(
2067 "%s: Specified prefix length %d is greater than a v6 address can support",
2068 __PRETTY_FUNCTION__, prefix.prefixlen);
ec93aa12
DS
2069 return;
2070 }
2071 STREAM_GET(&gate.ipv6, s, 16);
d62a17ae 2072 break;
2073 default:
ec93aa12
DS
2074 zlog_warn("%s: Specified AF %d is not supported for this call",
2075 __PRETTY_FUNCTION__, prefix.family);
d62a17ae 2076 return;
2077 }
ec93aa12
DS
2078 STREAM_GETL(s, ifindex);
2079 STREAM_GETC(s, distance);
2080 STREAM_GETL(s, in_label);
2081 STREAM_GETL(s, out_label);
d62a17ae 2082
2083 switch (prefix.family) {
2084 case AF_INET:
2085 if (ifindex)
2086 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
2087 else
2088 gtype = NEXTHOP_TYPE_IPV4;
2089 break;
2090 case AF_INET6:
2091 if (ifindex)
2092 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
2093 else
2094 gtype = NEXTHOP_TYPE_IPV6;
2095 break;
2096 default:
2097 return;
2098 }
2099
2100 if (!mpls_enabled)
2101 return;
2102
2103 if (command == ZEBRA_MPLS_LABELS_ADD) {
2104 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
2105 ifindex);
2f9c59f0
RW
2106 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
2107 distance, out_label);
d62a17ae 2108 } else if (command == ZEBRA_MPLS_LABELS_DELETE) {
2109 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
2f9c59f0
RW
2110 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
2111 distance, out_label);
d62a17ae 2112 }
ec93aa12
DS
2113stream_failure:
2114 return;
ce549947 2115}
fea12efb 2116/* Send response to a label manager connect request to client */
d62a17ae 2117static int zsend_label_manager_connect_response(struct zserv *client,
2118 vrf_id_t vrf_id, u_short result)
fea12efb 2119{
d62a17ae 2120 struct stream *s;
fea12efb 2121
d62a17ae 2122 s = client->obuf;
2123 stream_reset(s);
fea12efb 2124
7cf15b25 2125 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
fea12efb 2126
d62a17ae 2127 /* result */
2128 stream_putc(s, result);
fea12efb 2129
d62a17ae 2130 /* Write packet size. */
2131 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 2132
d62a17ae 2133 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 2134}
2135
d62a17ae 2136static void zread_label_manager_connect(struct zserv *client, vrf_id_t vrf_id)
fea12efb 2137{
d62a17ae 2138 struct stream *s;
2139 /* type of protocol (lib/zebra.h) */
2140 u_char proto;
2141 u_short instance;
fea12efb 2142
d62a17ae 2143 /* Get input stream. */
2144 s = client->ibuf;
fea12efb 2145
d62a17ae 2146 /* Get data. */
ec93aa12
DS
2147 STREAM_GETC(s, proto);
2148 STREAM_GETW(s, instance);
fea12efb 2149
d62a17ae 2150 /* accept only dynamic routing protocols */
2151 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2152 zlog_err("client %d has wrong protocol %s", client->sock,
2153 zebra_route_string(proto));
2154 zsend_label_manager_connect_response(client, vrf_id, 1);
2155 return;
2156 }
2157 zlog_notice("client %d with instance %u connected as %s", client->sock,
2158 instance, zebra_route_string(proto));
2159 client->proto = proto;
2160 client->instance = instance;
2161
2162 /*
2163 Release previous labels of same protocol and instance.
2164 This is done in case it restarted from an unexpected shutdown.
2165 */
2166 release_daemon_chunks(proto, instance);
2167
2168 zlog_debug(
2169 " Label Manager client connected: sock %d, proto %s, instance %u",
2170 client->sock, zebra_route_string(proto), instance);
2171 /* send response back */
2172 zsend_label_manager_connect_response(client, vrf_id, 0);
ec93aa12
DS
2173
2174stream_failure:
2175 return;
fea12efb 2176}
2177/* Send response to a get label chunk request to client */
d62a17ae 2178static int zsend_assign_label_chunk_response(struct zserv *client,
2179 vrf_id_t vrf_id,
2180 struct label_manager_chunk *lmc)
fea12efb 2181{
d62a17ae 2182 struct stream *s;
fea12efb 2183
d62a17ae 2184 s = client->obuf;
2185 stream_reset(s);
fea12efb 2186
7cf15b25 2187 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
fea12efb 2188
d62a17ae 2189 if (lmc) {
2190 /* keep */
2191 stream_putc(s, lmc->keep);
2192 /* start and end labels */
2193 stream_putl(s, lmc->start);
2194 stream_putl(s, lmc->end);
2195 }
fea12efb 2196
d62a17ae 2197 /* Write packet size. */
2198 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 2199
d62a17ae 2200 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 2201}
2202
d62a17ae 2203static void zread_get_label_chunk(struct zserv *client, vrf_id_t vrf_id)
fea12efb 2204{
d62a17ae 2205 struct stream *s;
2206 u_char keep;
2207 uint32_t size;
2208 struct label_manager_chunk *lmc;
fea12efb 2209
d62a17ae 2210 /* Get input stream. */
2211 s = client->ibuf;
fea12efb 2212
d62a17ae 2213 /* Get data. */
ec93aa12
DS
2214 STREAM_GETC(s, keep);
2215 STREAM_GETL(s, size);
fea12efb 2216
d62a17ae 2217 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
2218 if (!lmc)
2219 zlog_err("%s: Unable to assign Label Chunk of size %u",
2220 __func__, size);
2221 else
2222 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
2223 lmc->end, keep);
2224 /* send response back */
2225 zsend_assign_label_chunk_response(client, vrf_id, lmc);
ec93aa12
DS
2226
2227stream_failure:
2228 return;
d62a17ae 2229}
2230
2231static void zread_release_label_chunk(struct zserv *client)
2232{
2233 struct stream *s;
2234 uint32_t start, end;
2235
2236 /* Get input stream. */
2237 s = client->ibuf;
2238
2239 /* Get data. */
ec93aa12
DS
2240 STREAM_GETL(s, start);
2241 STREAM_GETL(s, end);
d62a17ae 2242
2243 release_label_chunk(client->proto, client->instance, start, end);
ec93aa12
DS
2244
2245stream_failure:
2246 return;
d62a17ae 2247}
2248static void zread_label_manager_request(int cmd, struct zserv *client,
6680688c 2249 struct zebra_vrf *zvrf)
d62a17ae 2250{
2251 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2252 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2253 client->is_synchronous = 1;
2254
2255 /* external label manager */
2256 if (lm_is_external)
996c9314 2257 zread_relay_label_manager_request(cmd, client, zvrf_id(zvrf));
d62a17ae 2258 /* this is a label manager */
2259 else {
2260 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
996c9314 2261 zread_label_manager_connect(client, zvrf_id(zvrf));
d62a17ae 2262 else {
2263 /* Sanity: don't allow 'unidentified' requests */
2264 if (!client->proto) {
2265 zlog_err(
2266 "Got label request from an unidentified client");
2267 return;
2268 }
2269 if (cmd == ZEBRA_GET_LABEL_CHUNK)
996c9314 2270 zread_get_label_chunk(client, zvrf_id(zvrf));
d62a17ae 2271 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2272 zread_release_label_chunk(client);
2273 }
2274 }
fea12efb 2275}
2276
6833ae01 2277static int zread_pseudowire(int command, struct zserv *client, u_short length,
6680688c 2278 struct zebra_vrf *zvrf)
6833ae01 2279{
2280 struct stream *s;
6833ae01 2281 char ifname[IF_NAMESIZE];
2282 ifindex_t ifindex;
2283 int type;
2284 int af;
2285 union g_addr nexthop;
2286 uint32_t local_label;
2287 uint32_t remote_label;
2288 uint8_t flags;
2289 union pw_protocol_fields data;
2290 uint8_t protocol;
2291 struct zebra_pw *pw;
2292
6833ae01 2293 /* Get input stream. */
2294 s = client->ibuf;
2295
2296 /* Get data. */
ec93aa12
DS
2297 STREAM_GET(ifname, s, IF_NAMESIZE);
2298 STREAM_GETL(s, ifindex);
2299 STREAM_GETL(s, type);
2300 STREAM_GETL(s, af);
6833ae01 2301 switch (af) {
2302 case AF_INET:
ec93aa12 2303 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
6833ae01 2304 break;
2305 case AF_INET6:
ec93aa12 2306 STREAM_GET(&nexthop.ipv6, s, 16);
6833ae01 2307 break;
2308 default:
2309 return -1;
2310 }
ec93aa12
DS
2311 STREAM_GETL(s, local_label);
2312 STREAM_GETL(s, remote_label);
2313 STREAM_GETC(s, flags);
2314 STREAM_GET(&data, s, sizeof(data));
6833ae01 2315 protocol = client->proto;
2316
2317 pw = zebra_pw_find(zvrf, ifname);
2318 switch (command) {
2319 case ZEBRA_PW_ADD:
2320 if (pw) {
2321 zlog_warn("%s: pseudowire %s already exists [%s]",
2322 __func__, ifname,
2323 zserv_command_string(command));
2324 return -1;
2325 }
2326
2327 zebra_pw_add(zvrf, ifname, protocol, client);
2328 break;
2329 case ZEBRA_PW_DELETE:
2330 if (!pw) {
2331 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2332 ifname, zserv_command_string(command));
2333 return -1;
2334 }
2335
2336 zebra_pw_del(zvrf, pw);
2337 break;
2338 case ZEBRA_PW_SET:
2339 case ZEBRA_PW_UNSET:
2340 if (!pw) {
2341 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2342 ifname, zserv_command_string(command));
2343 return -1;
2344 }
2345
2346 switch (command) {
2347 case ZEBRA_PW_SET:
2348 pw->enabled = 1;
2349 break;
2350 case ZEBRA_PW_UNSET:
2351 pw->enabled = 0;
2352 break;
2353 }
2354
2355 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2356 remote_label, flags, &data);
2357 break;
2358 }
2359
ec93aa12 2360stream_failure:
6833ae01 2361 return 0;
2362}
2363
d62a17ae 2364/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2365static void zebra_client_close_cleanup_rnh(struct zserv *client)
fea12efb 2366{
d62a17ae 2367 struct vrf *vrf;
2368 struct zebra_vrf *zvrf;
fea12efb 2369
a2addae8 2370 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
d62a17ae 2371 if ((zvrf = vrf->info) != NULL) {
2372 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2373 RNH_NEXTHOP_TYPE);
2374 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2375 client, RNH_NEXTHOP_TYPE);
2376 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2377 RNH_IMPORT_CHECK_TYPE);
2378 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2379 client, RNH_IMPORT_CHECK_TYPE);
2380 if (client->proto == ZEBRA_ROUTE_LDP) {
2381 hash_iterate(zvrf->lsp_table,
2382 mpls_ldp_lsp_uninstall_all,
2383 zvrf->lsp_table);
2384 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2385 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2386 }
2387 }
2388 }
fea12efb 2389}
d62a17ae 2390
972019ae
DS
2391/* free zebra client information. */
2392static void zebra_client_free(struct zserv *client)
fea12efb 2393{
d62a17ae 2394 /* Send client de-registration to BFD */
2395 zebra_ptm_bfd_client_deregister(client->proto);
fea12efb 2396
d62a17ae 2397 /* Cleanup any registered nexthops - across all VRFs. */
2398 zebra_client_close_cleanup_rnh(client);
ce549947 2399
d62a17ae 2400 /* Release Label Manager chunks */
2401 release_daemon_chunks(client->proto, client->instance);
4b33b75a 2402
d62a17ae 2403 /* Cleanup any FECs registered by this client. */
2404 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT),
2405 client);
2406
6833ae01 2407 /* Remove pseudowires associated with this client */
2408 zebra_pw_client_close(client);
2409
d62a17ae 2410 /* Close file descriptor. */
2411 if (client->sock) {
2412 unsigned long nroutes;
2413
2414 close(client->sock);
2415 nroutes = rib_score_proto(client->proto, client->instance);
2416 zlog_notice(
2417 "client %d disconnected. %lu %s routes removed from the rib",
2418 client->sock, nroutes,
2419 zebra_route_string(client->proto));
2420 client->sock = -1;
2421 }
2422
2423 /* Free stream buffers. */
2424 if (client->ibuf)
2425 stream_free(client->ibuf);
2426 if (client->obuf)
2427 stream_free(client->obuf);
2428 if (client->wb)
2429 buffer_free(client->wb);
2430
2431 /* Release threads. */
2432 if (client->t_read)
2433 thread_cancel(client->t_read);
2434 if (client->t_write)
2435 thread_cancel(client->t_write);
2436 if (client->t_suicide)
2437 thread_cancel(client->t_suicide);
2438
2439 /* Free bitmaps. */
2440 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
2441 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
2442 vrf_bitmap_free(client->redist[afi][i]);
2443
2444 vrf_bitmap_free(client->redist_default);
2445 vrf_bitmap_free(client->ifinfo);
2446 vrf_bitmap_free(client->ridinfo);
2447
d62a17ae 2448 XFREE(MTYPE_TMP, client);
718e3744 2449}
2450
972019ae
DS
2451static void zebra_client_close(struct zserv *client)
2452{
2453 listnode_delete(zebrad.client_list, client);
2454 zebra_client_free(client);
2455}
2456
718e3744 2457/* Make new client. */
d62a17ae 2458static void zebra_client_create(int sock)
718e3744 2459{
d62a17ae 2460 struct zserv *client;
2461 int i;
2462 afi_t afi;
2463
2464 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
718e3744 2465
d62a17ae 2466 /* Make client input/output buffer. */
2467 client->sock = sock;
2468 client->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2469 client->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2470 client->wb = buffer_new(0);
718e3744 2471
d62a17ae 2472 /* Set table number. */
2473 client->rtm_table = zebrad.rtm_table_default;
718e3744 2474
d62a17ae 2475 client->connect_time = monotime(NULL);
2476 /* Initialize flags */
2477 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2478 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2479 client->redist[afi][i] = vrf_bitmap_init();
2480 client->redist_default = vrf_bitmap_init();
2481 client->ifinfo = vrf_bitmap_init();
2482 client->ridinfo = vrf_bitmap_init();
718e3744 2483
d62a17ae 2484 /* by default, it's not a synchronous client */
2485 client->is_synchronous = 0;
04b02fda 2486
d62a17ae 2487 /* Add this client to linked list. */
2488 listnode_add(zebrad.client_list, client);
fea12efb 2489
d62a17ae 2490 /* Make new read thread. */
2491 zebra_event(ZEBRA_READ, sock, client);
12f6fb97 2492
d62a17ae 2493 zebra_vrf_update_all(client);
718e3744 2494}
2495
996c9314 2496static int zread_interface_set_master(struct zserv *client, u_short length)
e0ae31b8
DS
2497{
2498 struct interface *master;
2499 struct interface *slave;
2500 struct stream *s = client->ibuf;
2501 int ifindex;
2502 vrf_id_t vrf_id;
2503
a9ff90c4 2504 STREAM_GETL(s, vrf_id);
ec93aa12 2505 STREAM_GETL(s, ifindex);
e0ae31b8
DS
2506 master = if_lookup_by_index(ifindex, vrf_id);
2507
a9ff90c4 2508 STREAM_GETL(s, vrf_id);
ec93aa12 2509 STREAM_GETL(s, ifindex);
e0ae31b8
DS
2510 slave = if_lookup_by_index(ifindex, vrf_id);
2511
2512 if (!master || !slave)
2513 return 0;
2514
2515 kernel_interface_set_master(master, slave);
2516
ec93aa12 2517stream_failure:
e0ae31b8
DS
2518 return 1;
2519}
2520
7d061b3c 2521
996c9314 2522static void zread_vrf_label(struct zserv *client, struct zebra_vrf *zvrf)
c83c5e44
DS
2523{
2524 struct interface *ifp;
2525 mpls_label_t nlabel;
7d061b3c 2526 afi_t afi;
c83c5e44
DS
2527 struct stream *s;
2528 struct zebra_vrf *def_zvrf;
339e36d2 2529 enum lsp_types_t ltype;
c83c5e44
DS
2530
2531 s = client->ibuf;
2532 STREAM_GETL(s, nlabel);
7d061b3c
DS
2533 STREAM_GETC(s, afi);
2534 if (nlabel == zvrf->label[afi]) {
c83c5e44
DS
2535 /*
2536 * Nothing to do here move along
2537 */
2538 return;
2539 }
7d061b3c 2540
339e36d2 2541 STREAM_GETC(s, ltype);
c83c5e44
DS
2542
2543 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2544 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2545 else
2546 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2547
2548 if (!ifp) {
2549 zlog_debug("Unable to find specified Interface for %s",
2550 zvrf->vrf->name);
2551 return;
2552 }
2553
2554 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2555
8fd9db58
DS
2556 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2557 afi_t scrubber;
2558 bool really_remove;
2559
2560 really_remove = true;
996c9314 2561 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
8fd9db58
DS
2562 if (scrubber == afi)
2563 continue;
2564
2565 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2566 continue;
2567
2568 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2569 really_remove = false;
2570 break;
2571 }
2572 }
2573
2574 if (really_remove)
2575 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2576 NEXTHOP_TYPE_IFINDEX, NULL,
2577 ifp->ifindex);
2578 }
c83c5e44 2579
42567e00 2580 if (nlabel != MPLS_LABEL_NONE)
996c9314
LB
2581 mpls_lsp_install(def_zvrf, ltype, nlabel,
2582 MPLS_LABEL_IMPLICIT_NULL, NEXTHOP_TYPE_IFINDEX,
2583 NULL, ifp->ifindex);
c83c5e44 2584
7d061b3c 2585 zvrf->label[afi] = nlabel;
c83c5e44
DS
2586stream_failure:
2587 return;
2588}
2589
996c9314 2590static inline void zserv_handle_commands(struct zserv *client, uint16_t command,
0c5e7be5
DS
2591 uint16_t length,
2592 struct zebra_vrf *zvrf)
d62a17ae 2593{
d62a17ae 2594 switch (command) {
2595 case ZEBRA_ROUTER_ID_ADD:
2596 zread_router_id_add(client, length, zvrf);
2597 break;
2598 case ZEBRA_ROUTER_ID_DELETE:
2599 zread_router_id_delete(client, length, zvrf);
2600 break;
2601 case ZEBRA_INTERFACE_ADD:
2602 zread_interface_add(client, length, zvrf);
2603 break;
2604 case ZEBRA_INTERFACE_DELETE:
2605 zread_interface_delete(client, length, zvrf);
2606 break;
0e51b4a3
RW
2607 case ZEBRA_ROUTE_ADD:
2608 zread_route_add(client, length, zvrf);
2609 break;
2610 case ZEBRA_ROUTE_DELETE:
2611 zread_route_del(client, length, zvrf);
2612 break;
d62a17ae 2613 case ZEBRA_IPV4_ROUTE_ADD:
2614 zread_ipv4_add(client, length, zvrf);
2615 break;
2616 case ZEBRA_IPV4_ROUTE_DELETE:
2617 zread_ipv4_delete(client, length, zvrf);
2618 break;
2619 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
2620 zread_ipv4_route_ipv6_nexthop_add(client, length, zvrf);
2621 break;
d62a17ae 2622 case ZEBRA_IPV6_ROUTE_ADD:
2623 zread_ipv6_add(client, length, zvrf);
2624 break;
2625 case ZEBRA_IPV6_ROUTE_DELETE:
2626 zread_ipv6_delete(client, length, zvrf);
2627 break;
2628 case ZEBRA_REDISTRIBUTE_ADD:
2629 zebra_redistribute_add(command, client, length, zvrf);
2630 break;
2631 case ZEBRA_REDISTRIBUTE_DELETE:
2632 zebra_redistribute_delete(command, client, length, zvrf);
2633 break;
2634 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
2635 zebra_redistribute_default_add(command, client, length, zvrf);
2636 break;
2637 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
2638 zebra_redistribute_default_delete(command, client, length,
2639 zvrf);
2640 break;
2641 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2642 zread_ipv4_nexthop_lookup_mrib(client, length, zvrf);
2643 break;
2644 case ZEBRA_HELLO:
2645 zread_hello(client);
2646 break;
2647 case ZEBRA_NEXTHOP_REGISTER:
996c9314 2648 zserv_rnh_register(client, length, RNH_NEXTHOP_TYPE, zvrf);
d62a17ae 2649 break;
2650 case ZEBRA_NEXTHOP_UNREGISTER:
996c9314 2651 zserv_rnh_unregister(client, length, RNH_NEXTHOP_TYPE, zvrf);
d62a17ae 2652 break;
2653 case ZEBRA_IMPORT_ROUTE_REGISTER:
996c9314 2654 zserv_rnh_register(client, length, RNH_IMPORT_CHECK_TYPE, zvrf);
d62a17ae 2655 break;
2656 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
996c9314
LB
2657 zserv_rnh_unregister(client, length, RNH_IMPORT_CHECK_TYPE,
2658 zvrf);
d62a17ae 2659 break;
2660 case ZEBRA_BFD_DEST_UPDATE:
2661 case ZEBRA_BFD_DEST_REGISTER:
e4bd522a 2662 zebra_ptm_bfd_dst_register(client, length, command, zvrf);
d62a17ae 2663 break;
2664 case ZEBRA_BFD_DEST_DEREGISTER:
e4bd522a 2665 zebra_ptm_bfd_dst_deregister(client, length, zvrf);
d62a17ae 2666 break;
2667 case ZEBRA_VRF_UNREGISTER:
2668 zread_vrf_unregister(client, length, zvrf);
2669 break;
c83c5e44
DS
2670 case ZEBRA_VRF_LABEL:
2671 zread_vrf_label(client, zvrf);
2672 break;
d62a17ae 2673 case ZEBRA_BFD_CLIENT_REGISTER:
e4bd522a 2674 zebra_ptm_bfd_client_register(client, length);
d62a17ae 2675 break;
2676 case ZEBRA_INTERFACE_ENABLE_RADV:
2677#if defined(HAVE_RTADV)
e4bd522a 2678 zebra_interface_radv_set(client, length, zvrf, 1);
9c3bf1ce 2679#endif
d62a17ae 2680 break;
2681 case ZEBRA_INTERFACE_DISABLE_RADV:
2682#if defined(HAVE_RTADV)
e4bd522a 2683 zebra_interface_radv_set(client, length, zvrf, 0);
9c3bf1ce 2684#endif
d62a17ae 2685 break;
2686 case ZEBRA_MPLS_LABELS_ADD:
2687 case ZEBRA_MPLS_LABELS_DELETE:
6680688c 2688 zread_mpls_labels(command, client, length, zvrf);
d62a17ae 2689 break;
2690 case ZEBRA_IPMR_ROUTE_STATS:
e4bd522a 2691 zebra_ipmr_route_stats(client, length, zvrf);
d62a17ae 2692 break;
2693 case ZEBRA_LABEL_MANAGER_CONNECT:
2694 case ZEBRA_GET_LABEL_CHUNK:
2695 case ZEBRA_RELEASE_LABEL_CHUNK:
6680688c 2696 zread_label_manager_request(command, client, zvrf);
d62a17ae 2697 break;
2698 case ZEBRA_FEC_REGISTER:
e4bd522a 2699 zserv_fec_register(client, length);
d62a17ae 2700 break;
2701 case ZEBRA_FEC_UNREGISTER:
e4bd522a 2702 zserv_fec_unregister(client, length);
d62a17ae 2703 break;
1a98c087 2704 case ZEBRA_ADVERTISE_DEFAULT_GW:
e4bd522a 2705 zebra_vxlan_advertise_gw_macip(client, length, zvrf);
1a98c087 2706 break;
31310b25
MK
2707 case ZEBRA_ADVERTISE_SUBNET:
2708 zebra_vxlan_advertise_subnet(client, length, zvrf);
2709 break;
d62a17ae 2710 case ZEBRA_ADVERTISE_ALL_VNI:
e4bd522a 2711 zebra_vxlan_advertise_all_vni(client, length, zvrf);
d62a17ae 2712 break;
2713 case ZEBRA_REMOTE_VTEP_ADD:
e4bd522a 2714 zebra_vxlan_remote_vtep_add(client, length, zvrf);
d62a17ae 2715 break;
2716 case ZEBRA_REMOTE_VTEP_DEL:
e4bd522a 2717 zebra_vxlan_remote_vtep_del(client, length, zvrf);
d62a17ae 2718 break;
2719 case ZEBRA_REMOTE_MACIP_ADD:
e4bd522a 2720 zebra_vxlan_remote_macip_add(client, length, zvrf);
d62a17ae 2721 break;
2722 case ZEBRA_REMOTE_MACIP_DEL:
e4bd522a 2723 zebra_vxlan_remote_macip_del(client, length, zvrf);
baca8cd4 2724 break;
e0ae31b8 2725 case ZEBRA_INTERFACE_SET_MASTER:
e4bd522a 2726 zread_interface_set_master(client, length);
d62a17ae 2727 break;
6833ae01 2728 case ZEBRA_PW_ADD:
2729 case ZEBRA_PW_DELETE:
2730 case ZEBRA_PW_SET:
2731 case ZEBRA_PW_UNSET:
6680688c 2732 zread_pseudowire(command, client, length, zvrf);
6833ae01 2733 break;
d62a17ae 2734 default:
2735 zlog_info("Zebra received unknown command %d", command);
2736 break;
2737 }
0c5e7be5
DS
2738}
2739
411314ed
DS
2740#if defined(HANDLE_ZAPI_FUZZING)
2741static void zserv_write_incoming(struct stream *orig, uint16_t command)
2742{
2743 char fname[MAXPATHLEN];
2744 struct stream *copy;
2745 int fd = -1;
2746
2747 copy = stream_dup(orig);
2748 stream_set_getp(copy, 0);
2749
2750 zserv_privs.change(ZPRIVS_RAISE);
2751 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
2752 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2753 stream_flush(copy, fd);
2754 close(fd);
2755 zserv_privs.change(ZPRIVS_LOWER);
2756 stream_free(copy);
2757}
2758#endif
2759
0c5e7be5
DS
2760/* Handler of zebra service request. */
2761static int zebra_client_read(struct thread *thread)
2762{
2763 int sock;
2764 struct zserv *client;
2765 size_t already;
2766 uint16_t length, command;
2767 uint8_t marker, version;
2768 vrf_id_t vrf_id;
2769 struct zebra_vrf *zvrf;
411314ed
DS
2770#if defined(HANDLE_ZAPI_FUZZING)
2771 int packets = 1;
2772#else
a37ef435 2773 int packets = zebrad.packets_to_process;
411314ed 2774#endif
0c5e7be5
DS
2775
2776 /* Get thread data. Reset reading thread because I'm running. */
2777 sock = THREAD_FD(thread);
2778 client = THREAD_ARG(thread);
2779 client->t_read = NULL;
2780
2781 if (client->t_suicide) {
2782 zebra_client_close(client);
2783 return -1;
2784 }
2785
5a762c8a
DS
2786 while (packets) {
2787 /* Read length and command (if we don't have it already). */
2788 if ((already = stream_get_endp(client->ibuf))
2789 < ZEBRA_HEADER_SIZE) {
2790 ssize_t nbyte;
996c9314
LB
2791 if (((nbyte = stream_read_try(client->ibuf, sock,
2792 ZEBRA_HEADER_SIZE
2793 - already))
5a762c8a
DS
2794 == 0)
2795 || (nbyte == -1)) {
2796 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
2797 zlog_debug(
2798 "connection closed socket [%d]",
2799 sock);
5a762c8a
DS
2800 zebra_client_close(client);
2801 return -1;
2802 }
2803 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2804 /* Try again later. */
2805 zebra_event(ZEBRA_READ, sock, client);
2806 return 0;
2807 }
2808 already = ZEBRA_HEADER_SIZE;
0c5e7be5 2809 }
0c5e7be5 2810
5a762c8a
DS
2811 /* Reset to read from the beginning of the incoming packet. */
2812 stream_set_getp(client->ibuf, 0);
0c5e7be5 2813
5a762c8a 2814 /* Fetch header values */
ec93aa12
DS
2815 STREAM_GETW(client->ibuf, length);
2816 STREAM_GETC(client->ibuf, marker);
2817 STREAM_GETC(client->ibuf, version);
a9ff90c4 2818 STREAM_GETL(client->ibuf, vrf_id);
ec93aa12 2819 STREAM_GETW(client->ibuf, command);
0c5e7be5 2820
5a762c8a
DS
2821 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2822 zlog_err(
2823 "%s: socket %d version mismatch, marker %d, version %d",
2824 __func__, sock, marker, version);
0c5e7be5
DS
2825 zebra_client_close(client);
2826 return -1;
2827 }
5a762c8a
DS
2828 if (length < ZEBRA_HEADER_SIZE) {
2829 zlog_warn(
2830 "%s: socket %d message length %u is less than header size %d",
2831 __func__, sock, length, ZEBRA_HEADER_SIZE);
2832 zebra_client_close(client);
2833 return -1;
2834 }
2835 if (length > STREAM_SIZE(client->ibuf)) {
2836 zlog_warn(
2837 "%s: socket %d message length %u exceeds buffer size %lu",
2838 __func__, sock, length,
2839 (u_long)STREAM_SIZE(client->ibuf));
2840 zebra_client_close(client);
2841 return -1;
0c5e7be5 2842 }
0c5e7be5 2843
5a762c8a
DS
2844 /* Read rest of data. */
2845 if (already < length) {
2846 ssize_t nbyte;
2847 if (((nbyte = stream_read_try(client->ibuf, sock,
2848 length - already))
2849 == 0)
2850 || (nbyte == -1)) {
2851 if (IS_ZEBRA_DEBUG_EVENT)
2852 zlog_debug(
2853 "connection closed [%d] when reading zebra data",
2854 sock);
2855 zebra_client_close(client);
2856 return -1;
2857 }
2858 if (nbyte != (ssize_t)(length - already)) {
2859 /* Try again later. */
2860 zebra_event(ZEBRA_READ, sock, client);
2861 return 0;
2862 }
2863 }
0c5e7be5 2864
411314ed
DS
2865#if defined(HANDLE_ZAPI_FUZZING)
2866 zserv_write_incoming(client->ibuf, command);
2867#endif
5a762c8a 2868 length -= ZEBRA_HEADER_SIZE;
0c5e7be5 2869
5a762c8a
DS
2870 /* Debug packet information. */
2871 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
2872 zlog_debug("zebra message comes from socket [%d]",
2873 sock);
0c5e7be5 2874
0c5e7be5 2875 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
5a762c8a 2876 zlog_debug("zebra message received [%s] %d in VRF %u",
996c9314
LB
2877 zserv_command_string(command), length,
2878 vrf_id);
0c5e7be5 2879
5a762c8a
DS
2880 client->last_read_time = monotime(NULL);
2881 client->last_read_cmd = command;
d62a17ae 2882
5a762c8a
DS
2883 zvrf = zebra_vrf_lookup_by_id(vrf_id);
2884 if (!zvrf) {
2885 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
996c9314
LB
2886 zlog_debug("zebra received unknown VRF[%u]",
2887 vrf_id);
5a762c8a
DS
2888 goto zclient_read_out;
2889 }
2890
2891 zserv_handle_commands(client, command, length, zvrf);
2892
2893 if (client->t_suicide) {
996c9314
LB
2894 /* No need to wait for thread callback, just kill
2895 * immediately.
5a762c8a
DS
2896 */
2897 zebra_client_close(client);
2898 return -1;
2899 }
2900 packets -= 1;
2901 stream_reset(client->ibuf);
d62a17ae 2902 }
2903
ec93aa12 2904stream_failure:
d62a17ae 2905zclient_read_out:
2906 stream_reset(client->ibuf);
2907 zebra_event(ZEBRA_READ, sock, client);
2908 return 0;
718e3744 2909}
2910
718e3744 2911
2912/* Accept code of zebra server socket. */
d62a17ae 2913static int zebra_accept(struct thread *thread)
718e3744 2914{
d62a17ae 2915 int accept_sock;
2916 int client_sock;
2917 struct sockaddr_in client;
2918 socklen_t len;
2919
2920 accept_sock = THREAD_FD(thread);
718e3744 2921
d62a17ae 2922 /* Reregister myself. */
2923 zebra_event(ZEBRA_SERV, accept_sock, NULL);
718e3744 2924
d62a17ae 2925 len = sizeof(struct sockaddr_in);
2926 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
719e9741 2927
d62a17ae 2928 if (client_sock < 0) {
2929 zlog_warn("Can't accept zebra socket: %s",
2930 safe_strerror(errno));
2931 return -1;
2932 }
718e3744 2933
d62a17ae 2934 /* Make client socket non-blocking. */
2935 set_nonblocking(client_sock);
718e3744 2936
d62a17ae 2937 /* Create new zebra client. */
2938 zebra_client_create(client_sock);
718e3744 2939
d62a17ae 2940 return 0;
718e3744 2941}
2942
689f5a8c
DL
2943/* Make zebra server socket, wiping any existing one (see bug #403). */
2944void zebra_zserv_socket_init(char *path)
d62a17ae 2945{
2946 int ret;
689f5a8c 2947 int sock;
d62a17ae 2948 mode_t old_mask;
689f5a8c
DL
2949 struct sockaddr_storage sa;
2950 socklen_t sa_len;
d62a17ae 2951
689f5a8c
DL
2952 if (!frr_zclient_addr(&sa, &sa_len, path))
2953 /* should be caught in zebra main() */
2954 return;
d62a17ae 2955
2956 /* Set umask */
2957 old_mask = umask(0077);
2958
2959 /* Make UNIX domain socket. */
689f5a8c 2960 sock = socket(sa.ss_family, SOCK_STREAM, 0);
d62a17ae 2961 if (sock < 0) {
689f5a8c 2962 zlog_warn("Can't create zserv socket: %s",
d62a17ae 2963 safe_strerror(errno));
2964 zlog_warn(
2965 "zebra can't provide full functionality due to above error");
2966 return;
2967 }
2968
689f5a8c
DL
2969 if (sa.ss_family != AF_UNIX) {
2970 sockopt_reuseaddr(sock);
2971 sockopt_reuseport(sock);
2972 } else {
2973 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
2974 if (suna->sun_path[0])
2975 unlink(suna->sun_path);
2976 }
2977
2c73b258
DS
2978 zserv_privs.change(ZPRIVS_RAISE);
2979 setsockopt_so_recvbuf(sock, 1048576);
2980 setsockopt_so_sendbuf(sock, 1048576);
2981 zserv_privs.change(ZPRIVS_LOWER);
2982
e6c1975a 2983 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_RAISE))
689f5a8c
DL
2984 zlog_err("Can't raise privileges");
2985
2986 ret = bind(sock, (struct sockaddr *)&sa, sa_len);
d62a17ae 2987 if (ret < 0) {
689f5a8c 2988 zlog_warn("Can't bind zserv socket on %s: %s", path,
d62a17ae 2989 safe_strerror(errno));
2990 zlog_warn(
2991 "zebra can't provide full functionality due to above error");
2992 close(sock);
2993 return;
2994 }
e6c1975a 2995 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_LOWER))
689f5a8c 2996 zlog_err("Can't lower privileges");
d62a17ae 2997
2998 ret = listen(sock, 5);
2999 if (ret < 0) {
689f5a8c 3000 zlog_warn("Can't listen to zserv socket %s: %s", path,
d62a17ae 3001 safe_strerror(errno));
3002 zlog_warn(
3003 "zebra can't provide full functionality due to above error");
3004 close(sock);
3005 return;
3006 }
3007
3008 umask(old_mask);
3009
3010 zebra_event(ZEBRA_SERV, sock, NULL);
718e3744 3011}
6b0655a2 3012
718e3744 3013
d62a17ae 3014static void zebra_event(enum event event, int sock, struct zserv *client)
3015{
3016 switch (event) {
3017 case ZEBRA_SERV:
3018 thread_add_read(zebrad.master, zebra_accept, client, sock,
3019 NULL);
3020 break;
3021 case ZEBRA_READ:
3022 client->t_read = NULL;
3023 thread_add_read(zebrad.master, zebra_client_read, client, sock,
3024 &client->t_read);
3025 break;
3026 case ZEBRA_WRITE:
3027 /**/
3028 break;
3029 }
718e3744 3030}
6b0655a2 3031
04b02fda 3032#define ZEBRA_TIME_BUF 32
d62a17ae 3033static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
04b02fda 3034{
d62a17ae 3035 struct tm *tm;
3036 time_t now;
04b02fda 3037
d62a17ae 3038 assert(buf != NULL);
3039 assert(buflen >= ZEBRA_TIME_BUF);
3040 assert(time1 != NULL);
04b02fda 3041
d62a17ae 3042 if (!*time1) {
3043 snprintf(buf, buflen, "never ");
3044 return (buf);
3045 }
04b02fda 3046
d62a17ae 3047 now = monotime(NULL);
3048 now -= *time1;
3049 tm = gmtime(&now);
04b02fda 3050
d62a17ae 3051 if (now < ONE_DAY_SECOND)
3052 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3053 tm->tm_sec);
3054 else if (now < ONE_WEEK_SECOND)
3055 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
3056 tm->tm_min);
96ade3ed 3057 else
d62a17ae 3058 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
3059 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
3060 return buf;
3061}
3062
3063static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
3064{
3065 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
3066 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
3067
3068 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
3069 if (client->instance)
3070 vty_out(vty, " Instance: %d", client->instance);
3071 vty_out(vty, "\n");
3072
3073 vty_out(vty, "------------------------ \n");
3074 vty_out(vty, "FD: %d \n", client->sock);
3075 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
3076
3077 vty_out(vty, "Connect Time: %s \n",
3078 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
3079 if (client->nh_reg_time) {
3080 vty_out(vty, "Nexthop Registry Time: %s \n",
3081 zserv_time_buf(&client->nh_reg_time, nhbuf,
3082 ZEBRA_TIME_BUF));
3083 if (client->nh_last_upd_time)
3084 vty_out(vty, "Nexthop Last Update Time: %s \n",
3085 zserv_time_buf(&client->nh_last_upd_time, mbuf,
3086 ZEBRA_TIME_BUF));
3087 else
3088 vty_out(vty, "No Nexthop Update sent\n");
3089 } else
3090 vty_out(vty, "Not registered for Nexthop Updates\n");
3091
3092 vty_out(vty, "Last Msg Rx Time: %s \n",
3093 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
3094 vty_out(vty, "Last Msg Tx Time: %s \n",
3095 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
3096 if (client->last_read_time)
3097 vty_out(vty, "Last Rcvd Cmd: %s \n",
3098 zserv_command_string(client->last_read_cmd));
3099 if (client->last_write_time)
3100 vty_out(vty, "Last Sent Cmd: %s \n",
3101 zserv_command_string(client->last_write_cmd));
3102 vty_out(vty, "\n");
3103
3104 vty_out(vty, "Type Add Update Del \n");
3105 vty_out(vty, "================================================== \n");
3106 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
3107 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
3108 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
3109 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
3110 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
3111 0, client->redist_v4_del_cnt);
3112 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
3113 0, client->redist_v6_del_cnt);
3114 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
3115 client->ifdel_cnt);
3116 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
3117 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
3118 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
3119 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
3120 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
3121 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
b7cfce93
MK
3122 vty_out(vty, "L3-VNI add notifications: %d\n", client->l3vniadd_cnt);
3123 vty_out(vty, "L3-VNI delete notifications: %d\n", client->l3vnidel_cnt);
d62a17ae 3124 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
3125 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
3126
3127 vty_out(vty, "\n");
3128 return;
3129}
3130
3131static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
3132{
3133 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
3134 char wbuf[ZEBRA_TIME_BUF];
3135
3136 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
3137 zebra_route_string(client->proto),
3138 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
3139 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
3140 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
3141 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
3142 client->v4_route_del_cnt,
3143 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
3144 client->v6_route_del_cnt);
3145}
3146
019a82cb 3147struct zserv *zebra_find_client(u_char proto, u_short instance)
d62a17ae 3148{
3149 struct listnode *node, *nnode;
3150 struct zserv *client;
3151
3152 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
996c9314 3153 if (client->proto == proto && client->instance == instance)
d62a17ae 3154 return client;
3155 }
3156
3157 return NULL;
8ed6821e 3158}
3159
718e3744 3160/* This command is for debugging purpose. */
3161DEFUN (show_zebra_client,
3162 show_zebra_client_cmd,
3163 "show zebra client",
3164 SHOW_STR
41e7fb80 3165 ZEBRA_STR
b9ee4999 3166 "Client information\n")
718e3744 3167{
d62a17ae 3168 struct listnode *node;
3169 struct zserv *client;
718e3744 3170
d62a17ae 3171 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3172 zebra_show_client_detail(vty, client);
04b02fda 3173
d62a17ae 3174 return CMD_SUCCESS;
04b02fda
DS
3175}
3176
3177/* This command is for debugging purpose. */
3178DEFUN (show_zebra_client_summary,
3179 show_zebra_client_summary_cmd,
3180 "show zebra client summary",
3181 SHOW_STR
41e7fb80 3182 ZEBRA_STR
b9ee4999
DS
3183 "Client information brief\n"
3184 "Brief Summary\n")
04b02fda 3185{
d62a17ae 3186 struct listnode *node;
3187 struct zserv *client;
04b02fda 3188
d62a17ae 3189 vty_out(vty,
3190 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
3191 vty_out(vty,
3192 "--------------------------------------------------------------------------------\n");
04b02fda 3193
d62a17ae 3194 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
3195 zebra_show_client_brief(vty, client);
fb018d25 3196
d62a17ae 3197 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
3198 return CMD_SUCCESS;
718e3744 3199}
3200
411314ed
DS
3201#if defined(HANDLE_ZAPI_FUZZING)
3202void zserv_read_file(char *input)
3203{
3204 int fd;
3205 struct zserv *client = NULL;
3206 struct thread t;
3207
3208 zebra_client_create(-1);
3209 client = zebrad.client_list->head->data;
3210 t.arg = client;
3211
996c9314 3212 fd = open(input, O_RDONLY | O_NONBLOCK);
411314ed
DS
3213 t.u.fd = fd;
3214
3215 zebra_client_read(&t);
3216
3217 close(fd);
3218}
3219#endif
3220
5f145fb8 3221void zserv_init(void)
718e3744 3222{
d62a17ae 3223 /* Client list init. */
3224 zebrad.client_list = list_new();
972019ae 3225 zebrad.client_list->del = (void (*)(void *))zebra_client_free;
718e3744 3226
d62a17ae 3227 install_element(ENABLE_NODE, &show_zebra_client_cmd);
3228 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 3229}