]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zapi_msg.c
zebra: add lock and busy counter for zclients
[mirror_frr.git] / zebra / zapi_msg.c
CommitLineData
bf094f69 1/*
d8647095 2 * Zebra API message creation & consumption.
bf094f69
QY
3 * Portions:
4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
6 * et al.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <zebra.h>
24#include <libgen.h>
25
26#include "lib/prefix.h"
27#include "lib/command.h"
28#include "lib/if.h"
29#include "lib/thread.h"
30#include "lib/stream.h"
31#include "lib/memory.h"
32#include "lib/table.h"
33#include "lib/network.h"
34#include "lib/sockunion.h"
35#include "lib/log.h"
36#include "lib/zclient.h"
37#include "lib/privs.h"
38#include "lib/network.h"
39#include "lib/buffer.h"
40#include "lib/nexthop.h"
41#include "lib/vrf.h"
42#include "lib/libfrr.h"
43#include "lib/sockopt.h"
5e7939a5 44#include "lib/lib_errors.h"
bf094f69 45
161e9ab7 46#include "zebra/zebra_router.h"
bf094f69
QY
47#include "zebra/rib.h"
48#include "zebra/zebra_memory.h"
49#include "zebra/zebra_ns.h"
50#include "zebra/zebra_vrf.h"
51#include "zebra/router-id.h"
52#include "zebra/redistribute.h"
53#include "zebra/debug.h"
54#include "zebra/zebra_rnh.h"
55#include "zebra/rt_netlink.h"
56#include "zebra/interface.h"
57#include "zebra/zebra_ptm.h"
58#include "zebra/rtadv.h"
59#include "zebra/zebra_mpls.h"
60#include "zebra/zebra_mroute.h"
bf094f69
QY
61#include "zebra/zebra_vxlan.h"
62#include "zebra/rt.h"
63#include "zebra/zebra_pbr.h"
64#include "zebra/table_manager.h"
65#include "zebra/zapi_msg.h"
364fed6b 66#include "zebra/zebra_errors.h"
02c0866d 67#include "zebra/zebra_mlag.h"
54bea4e5 68#include "zebra/connected.h"
bf094f69
QY
69
70/* Encoding helpers -------------------------------------------------------- */
71
72static void zserv_encode_interface(struct stream *s, struct interface *ifp)
73{
74 /* Interface information. */
53e60e5c 75 struct zebra_if *zif = ifp->info;
19c38250 76
bf094f69
QY
77 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
78 stream_putl(s, ifp->ifindex);
79 stream_putc(s, ifp->status);
80 stream_putq(s, ifp->flags);
81 stream_putc(s, ifp->ptm_enable);
82 stream_putc(s, ifp->ptm_status);
83 stream_putl(s, ifp->metric);
84 stream_putl(s, ifp->speed);
85 stream_putl(s, ifp->mtu);
86 stream_putl(s, ifp->mtu6);
87 stream_putl(s, ifp->bandwidth);
53e60e5c 88 stream_putl(s, zif->link_ifindex);
bf094f69
QY
89 stream_putl(s, ifp->ll_type);
90 stream_putl(s, ifp->hw_addr_len);
91 if (ifp->hw_addr_len)
92 stream_put(s, ifp->hw_addr, ifp->hw_addr_len);
93
94 /* Then, Traffic Engineering parameters if any */
95 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) {
96 stream_putc(s, 1);
97 zebra_interface_link_params_write(s, ifp);
98 } else
99 stream_putc(s, 0);
100
101 /* Write packet size. */
102 stream_putw_at(s, 0, stream_get_endp(s));
103}
104
105static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf)
106{
107 struct vrf_data data;
108 const char *netns_name = zvrf_ns_name(zvrf);
109
110 data.l.table_id = zvrf->table_id;
111
112 if (netns_name)
113 strlcpy(data.l.netns_name, basename((char *)netns_name),
114 NS_NAMSIZ);
115 else
116 memset(data.l.netns_name, 0, NS_NAMSIZ);
117 /* Pass the tableid and the netns NAME */
118 stream_put(s, &data, sizeof(struct vrf_data));
119 /* Interface information. */
120 stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
121 /* Write packet size. */
122 stream_putw_at(s, 0, stream_get_endp(s));
123}
124
125static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
126{
a756969d 127 stream_putl(s, nexthop->vrf_id);
bf094f69
QY
128 stream_putc(s, nexthop->type);
129 switch (nexthop->type) {
130 case NEXTHOP_TYPE_IPV4:
131 case NEXTHOP_TYPE_IPV4_IFINDEX:
132 stream_put_in_addr(s, &nexthop->gate.ipv4);
133 stream_putl(s, nexthop->ifindex);
134 break;
135 case NEXTHOP_TYPE_IPV6:
136 stream_put(s, &nexthop->gate.ipv6, 16);
137 break;
138 case NEXTHOP_TYPE_IPV6_IFINDEX:
139 stream_put(s, &nexthop->gate.ipv6, 16);
140 stream_putl(s, nexthop->ifindex);
141 break;
142 case NEXTHOP_TYPE_IFINDEX:
143 stream_putl(s, nexthop->ifindex);
144 break;
145 default:
146 /* do nothing */
147 break;
148 }
149 return 1;
150}
151
9ab0b2a3
SW
152/*
153 * Zebra error addition adds error type.
154 *
155 *
156 * 0 1
157 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * | enum zebra_error_types |
160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 *
162 */
163static void zserv_encode_error(struct stream *s, enum zebra_error_types error)
164{
165 stream_put(s, &error, sizeof(error));
166
167 /* Write packet size. */
168 stream_putw_at(s, 0, stream_get_endp(s));
169}
170
bf094f69
QY
171/* Send handlers ----------------------------------------------------------- */
172
173/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
174/*
175 * This function is called in the following situations:
176 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
177 * from the client.
178 * - at startup, when zebra figures out the available interfaces
179 * - when an interface is added (where support for
180 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
181 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
182 * received)
183 */
184int zsend_interface_add(struct zserv *client, struct interface *ifp)
185{
186 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
187
a36898e7 188 zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
bf094f69
QY
189 zserv_encode_interface(s, ifp);
190
191 client->ifadd_cnt++;
21ccc0cf 192 return zserv_send_message(client, s);
bf094f69
QY
193}
194
195/* Interface deletion from zebra daemon. */
196int zsend_interface_delete(struct zserv *client, struct interface *ifp)
197{
198 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
199
a36898e7 200 zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
bf094f69
QY
201 zserv_encode_interface(s, ifp);
202
203 client->ifdel_cnt++;
21ccc0cf 204 return zserv_send_message(client, s);
bf094f69
QY
205}
206
207int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
208{
209 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
210
211 zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
212 zserv_encode_vrf(s, zvrf);
213
214 client->vrfadd_cnt++;
21ccc0cf 215 return zserv_send_message(client, s);
bf094f69
QY
216}
217
218/* VRF deletion from zebra daemon. */
219int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
220
221{
222 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
223
224 zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
225 zserv_encode_vrf(s, zvrf);
226
227 client->vrfdel_cnt++;
21ccc0cf 228 return zserv_send_message(client, s);
bf094f69
QY
229}
230
231int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
232{
233 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
234
bf094f69
QY
235 if (!ifp->link_params) {
236 stream_free(s);
237 return 0;
238 }
239
a36898e7 240 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
bf094f69
QY
241
242 /* Add Interface Index */
243 stream_putl(s, ifp->ifindex);
244
245 /* Then TE Link Parameters */
246 if (zebra_interface_link_params_write(s, ifp) == 0) {
247 stream_free(s);
248 return 0;
249 }
250
251 /* Write packet size. */
252 stream_putw_at(s, 0, stream_get_endp(s));
253
21ccc0cf 254 return zserv_send_message(client, s);
bf094f69
QY
255}
256
257/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
258 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
259 *
260 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
261 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
262 * from the client, after the ZEBRA_INTERFACE_ADD has been
263 * sent from zebra to the client
264 * - redistribute new address info to all clients in the following situations
265 * - at startup, when zebra figures out the available interfaces
266 * - when an interface is added (where support for
267 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
268 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
269 * received)
b1bd1015 270 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
bf094f69
QY
271 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
272 * - when an RTM_NEWADDR message is received from the kernel,
273 *
274 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
275 *
276 * zsend_interface_address(DELETE)
277 * ^
278 * |
279 * zebra_interface_address_delete_update
280 * ^ ^ ^
281 * | | if_delete_update
282 * | |
283 * ip_address_uninstall connected_delete_ipv4
284 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
285 * ^ ^
286 * | |
287 * | RTM_NEWADDR on routing/netlink socket
288 * |
289 * vty commands:
290 * "no ip address A.B.C.D/M [label LINE]"
b1bd1015 291 * "no ip address A.B.C.D/M"
bf094f69
QY
292 * ["no ipv6 address X:X::X:X/M"]
293 *
294 */
295int zsend_interface_address(int cmd, struct zserv *client,
296 struct interface *ifp, struct connected *ifc)
297{
298 int blen;
299 struct prefix *p;
300 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
301
a36898e7 302 zclient_create_header(s, cmd, ifp->vrf_id);
bf094f69
QY
303 stream_putl(s, ifp->ifindex);
304
305 /* Interface address flag. */
306 stream_putc(s, ifc->flags);
307
308 /* Prefix information. */
309 p = ifc->address;
310 stream_putc(s, p->family);
311 blen = prefix_blen(p);
312 stream_put(s, &p->u.prefix, blen);
313
314 /*
315 * XXX gnu version does not send prefixlen for
316 * ZEBRA_INTERFACE_ADDRESS_DELETE
317 * but zebra_interface_address_delete_read() in the gnu version
318 * expects to find it
319 */
320 stream_putc(s, p->prefixlen);
321
322 /* Destination. */
323 p = ifc->destination;
324 if (p)
325 stream_put(s, &p->u.prefix, blen);
326 else
327 stream_put(s, NULL, blen);
328
329 /* Write packet size. */
330 stream_putw_at(s, 0, stream_get_endp(s));
331
332 client->connected_rt_add_cnt++;
21ccc0cf 333 return zserv_send_message(client, s);
bf094f69
QY
334}
335
336static int zsend_interface_nbr_address(int cmd, struct zserv *client,
337 struct interface *ifp,
338 struct nbr_connected *ifc)
339{
340 int blen;
341 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
342 struct prefix *p;
343
a36898e7 344 zclient_create_header(s, cmd, ifp->vrf_id);
bf094f69
QY
345 stream_putl(s, ifp->ifindex);
346
347 /* Prefix information. */
348 p = ifc->address;
349 stream_putc(s, p->family);
350 blen = prefix_blen(p);
351 stream_put(s, &p->u.prefix, blen);
352
353 /*
354 * XXX gnu version does not send prefixlen for
355 * ZEBRA_INTERFACE_ADDRESS_DELETE
356 * but zebra_interface_address_delete_read() in the gnu version
357 * expects to find it
358 */
359 stream_putc(s, p->prefixlen);
360
361 /* Write packet size. */
362 stream_putw_at(s, 0, stream_get_endp(s));
363
21ccc0cf 364 return zserv_send_message(client, s);
bf094f69
QY
365}
366
367/* Interface address addition. */
368static void zebra_interface_nbr_address_add_update(struct interface *ifp,
369 struct nbr_connected *ifc)
370{
371 struct listnode *node, *nnode;
372 struct zserv *client;
373 struct prefix *p;
374
375 if (IS_ZEBRA_DEBUG_EVENT) {
376 char buf[INET6_ADDRSTRLEN];
377
378 p = ifc->address;
379 zlog_debug(
380 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
381 inet_ntop(p->family, &p->u.prefix, buf,
382 INET6_ADDRSTRLEN),
383 p->prefixlen, ifc->ifp->name);
384 }
385
17da84a4
KS
386 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
387 /* Do not send unsolicited messages to synchronous clients. */
388 if (client->synchronous)
389 continue;
390
bf094f69
QY
391 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
392 client, ifp, ifc);
17da84a4 393 }
bf094f69
QY
394}
395
396/* Interface address deletion. */
397static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
398 struct nbr_connected *ifc)
399{
400 struct listnode *node, *nnode;
401 struct zserv *client;
402 struct prefix *p;
403
404 if (IS_ZEBRA_DEBUG_EVENT) {
405 char buf[INET6_ADDRSTRLEN];
406
407 p = ifc->address;
408 zlog_debug(
409 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
410 inet_ntop(p->family, &p->u.prefix, buf,
411 INET6_ADDRSTRLEN),
412 p->prefixlen, ifc->ifp->name);
413 }
414
17da84a4
KS
415 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
416 /* Do not send unsolicited messages to synchronous clients. */
417 if (client->synchronous)
418 continue;
419
bf094f69
QY
420 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
421 client, ifp, ifc);
17da84a4 422 }
bf094f69
QY
423}
424
425/* Send addresses on interface to client */
426int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
427{
428 struct listnode *cnode, *cnnode;
429 struct connected *c;
430 struct nbr_connected *nc;
431
432 /* Send interface addresses. */
433 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
434 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
435 continue;
436
437 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
438 ifp, c)
439 < 0)
440 return -1;
441 }
442
443 /* Send interface neighbors. */
444 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
445 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
446 client, ifp, nc)
447 < 0)
448 return -1;
449 }
450
451 return 0;
452}
453
454/* Notify client about interface moving from one VRF to another.
455 * Whether client is interested in old and new VRF is checked by caller.
456 */
457int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
458 vrf_id_t vrf_id)
459{
460 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
461
a36898e7 462 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
bf094f69 463
91d227b7
RW
464 /* Fill in the name of the interface and its new VRF (id) */
465 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
bf094f69
QY
466 stream_putl(s, vrf_id);
467
468 /* Write packet size. */
469 stream_putw_at(s, 0, stream_get_endp(s));
470
471 client->if_vrfchg_cnt++;
21ccc0cf 472 return zserv_send_message(client, s);
bf094f69
QY
473}
474
475/* Add new nbr connected IPv6 address */
476void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
477{
478 struct nbr_connected *ifc;
479 struct prefix p;
480
481 p.family = AF_INET6;
a85297a7 482 IPV6_ADDR_COPY(&p.u.prefix6, address);
bf094f69
QY
483 p.prefixlen = IPV6_MAX_PREFIXLEN;
484
8b1766b1
QY
485 ifc = listnode_head(ifp->nbr_connected);
486 if (!ifc) {
bf094f69
QY
487 /* new addition */
488 ifc = nbr_connected_new();
489 ifc->address = prefix_new();
490 ifc->ifp = ifp;
491 listnode_add(ifp->nbr_connected, ifc);
492 }
493
494 prefix_copy(ifc->address, &p);
495
496 zebra_interface_nbr_address_add_update(ifp, ifc);
497
498 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
499}
500
501void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
502{
503 struct nbr_connected *ifc;
504 struct prefix p;
505
506 p.family = AF_INET6;
a85297a7 507 IPV6_ADDR_COPY(&p.u.prefix6, address);
bf094f69
QY
508 p.prefixlen = IPV6_MAX_PREFIXLEN;
509
510 ifc = nbr_connected_check(ifp, &p);
511 if (!ifc)
512 return;
513
514 listnode_delete(ifp->nbr_connected, ifc);
515
516 zebra_interface_nbr_address_delete_update(ifp, ifc);
517
518 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
519
520 nbr_connected_free(ifc);
521}
522
523/*
524 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
525 * ZEBRA_INTERFACE_DOWN.
526 *
527 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
528 * the clients in one of 2 situations:
529 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
530 * - a vty command modifying the bandwidth of an interface is received.
531 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
532 */
533int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
534{
535 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
536
a36898e7 537 zclient_create_header(s, cmd, ifp->vrf_id);
bf094f69
QY
538 zserv_encode_interface(s, ifp);
539
540 if (cmd == ZEBRA_INTERFACE_UP)
541 client->ifup_cnt++;
542 else
543 client->ifdown_cnt++;
544
21ccc0cf 545 return zserv_send_message(client, s);
bf094f69
QY
546}
547
86391e56
MS
548int zsend_redistribute_route(int cmd, struct zserv *client,
549 const struct prefix *p,
40f321c0
MS
550 const struct prefix *src_p,
551 const struct route_entry *re)
bf094f69
QY
552{
553 struct zapi_route api;
554 struct zapi_nexthop *api_nh;
555 struct nexthop *nexthop;
9a0d4dd3 556 uint8_t count = 0;
34fa0870 557 afi_t afi;
f3f45626
DS
558 size_t stream_size =
559 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
bf094f69
QY
560
561 memset(&api, 0, sizeof(api));
562 api.vrf_id = re->vrf_id;
563 api.type = re->type;
e4081c0e 564 api.safi = SAFI_UNICAST;
bf094f69
QY
565 api.instance = re->instance;
566 api.flags = re->flags;
567
34fa0870
DS
568 afi = family2afi(p->family);
569 switch (afi) {
570 case AFI_IP:
571 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
572 client->redist_v4_add_cnt++;
573 else
574 client->redist_v4_del_cnt++;
575 break;
576 case AFI_IP6:
577 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
578 client->redist_v6_add_cnt++;
579 else
580 client->redist_v6_del_cnt++;
581 break;
582 default:
583 break;
584 }
585
bf094f69
QY
586 /* Prefix. */
587 api.prefix = *p;
588 if (src_p) {
589 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
590 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
591 }
592
c415d895 593 for (nexthop = re->nhe->nhg.nexthop;
0eb97b86 594 nexthop; nexthop = nexthop->next) {
bf094f69
QY
595 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
596 continue;
597
598 api_nh = &api.nexthops[count];
599 api_nh->vrf_id = nexthop->vrf_id;
600 api_nh->type = nexthop->type;
bd054c1a 601 api_nh->weight = nexthop->weight;
bf094f69
QY
602 switch (nexthop->type) {
603 case NEXTHOP_TYPE_BLACKHOLE:
604 api_nh->bh_type = nexthop->bh_type;
605 break;
606 case NEXTHOP_TYPE_IPV4:
607 api_nh->gate.ipv4 = nexthop->gate.ipv4;
608 break;
609 case NEXTHOP_TYPE_IPV4_IFINDEX:
610 api_nh->gate.ipv4 = nexthop->gate.ipv4;
611 api_nh->ifindex = nexthop->ifindex;
612 break;
613 case NEXTHOP_TYPE_IFINDEX:
614 api_nh->ifindex = nexthop->ifindex;
615 break;
616 case NEXTHOP_TYPE_IPV6:
617 api_nh->gate.ipv6 = nexthop->gate.ipv6;
618 break;
619 case NEXTHOP_TYPE_IPV6_IFINDEX:
620 api_nh->gate.ipv6 = nexthop->gate.ipv6;
621 api_nh->ifindex = nexthop->ifindex;
622 }
623 count++;
624 }
625
9a0d4dd3
DS
626 /* Nexthops. */
627 if (count) {
628 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
629 api.nexthop_num = count;
630 }
631
bf094f69
QY
632 /* Attributes. */
633 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
634 api.distance = re->distance;
635 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
636 api.metric = re->metric;
637 if (re->tag) {
638 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
639 api.tag = re->tag;
640 }
641 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
642 api.mtu = re->mtu;
643
f3f45626 644 struct stream *s = stream_new(stream_size);
bf094f69
QY
645
646 /* Encode route and send. */
647 if (zapi_route_encode(cmd, s, &api) < 0) {
648 stream_free(s);
649 return -1;
650 }
651
652 if (IS_ZEBRA_DEBUG_SEND) {
653 char buf_prefix[PREFIX_STRLEN];
8b1766b1 654
bf094f69
QY
655 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
656
657 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
658 __func__, zserv_command_string(cmd),
659 zebra_route_string(client->proto),
660 zebra_route_string(api.type), api.vrf_id,
661 buf_prefix);
662 }
21ccc0cf 663 return zserv_send_message(client, s);
bf094f69
QY
664}
665
666/*
667 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
668 * nexthop is not found on mrib. Returns both route metric and protocol
669 * distance.
670 */
671static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
672 struct in_addr addr,
673 struct route_entry *re,
674 struct zebra_vrf *zvrf)
675{
676 struct stream *s;
677 unsigned long nump;
678 uint8_t num;
679 struct nexthop *nexthop;
680
681 /* Get output stream. */
682 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
683 stream_reset(s);
684
685 /* Fill in result. */
686 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
687 stream_put_in_addr(s, &addr);
688
689 if (re) {
690 stream_putc(s, re->distance);
691 stream_putl(s, re->metric);
692 num = 0;
8b1766b1
QY
693 /* remember position for nexthop_num */
694 nump = stream_get_endp(s);
695 /* reserve room for nexthop_num */
696 stream_putc(s, 0);
697 /*
698 * Only non-recursive routes are elegible to resolve the
699 * nexthop we are looking up. Therefore, we will just iterate
700 * over the top chain of nexthops.
701 */
c415d895 702 for (nexthop = re->nhe->nhg.nexthop; nexthop;
6b468511 703 nexthop = nexthop->next)
bf094f69
QY
704 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
705 num += zserv_encode_nexthop(s, nexthop);
706
8b1766b1
QY
707 /* store nexthop_num */
708 stream_putc_at(s, nump, num);
bf094f69
QY
709 } else {
710 stream_putc(s, 0); /* distance */
711 stream_putl(s, 0); /* metric */
712 stream_putc(s, 0); /* nexthop_num */
713 }
714
715 stream_putw_at(s, 0, stream_get_endp(s));
716
21ccc0cf 717 return zserv_send_message(client, s);
bf094f69
QY
718}
719
86391e56
MS
720/*
721 * Common utility send route notification, called from a path using a
722 * route_entry and from a path using a dataplane context.
723 */
724static int route_notify_internal(const struct prefix *p, int type,
725 uint16_t instance, vrf_id_t vrf_id,
726 uint32_t table_id,
727 enum zapi_route_notify_owner note)
bf094f69
QY
728{
729 struct zserv *client;
730 struct stream *s;
731 uint8_t blen;
732
86391e56 733 client = zserv_find_client(type, instance);
bf094f69
QY
734 if (!client || !client->notify_owner) {
735 if (IS_ZEBRA_DEBUG_PACKET) {
736 char buff[PREFIX_STRLEN];
737
738 zlog_debug(
739 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
86391e56
MS
740 type, prefix2str(p, buff, sizeof(buff)),
741 table_id, note, vrf_id);
bf094f69
QY
742 }
743 return 0;
744 }
745
746 if (IS_ZEBRA_DEBUG_PACKET) {
747 char buff[PREFIX_STRLEN];
748
749 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
86391e56
MS
750 type, prefix2str(p, buff, sizeof(buff)),
751 table_id, note, vrf_id);
bf094f69
QY
752 }
753
754 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
755 stream_reset(s);
756
86391e56 757 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
bf094f69
QY
758
759 stream_put(s, &note, sizeof(note));
760
761 stream_putc(s, p->family);
762
763 blen = prefix_blen(p);
764 stream_putc(s, p->prefixlen);
765 stream_put(s, &p->u.prefix, blen);
766
86391e56 767 stream_putl(s, table_id);
bf094f69
QY
768
769 stream_putw_at(s, 0, stream_get_endp(s));
770
21ccc0cf 771 return zserv_send_message(client, s);
bf094f69
QY
772}
773
86391e56
MS
774int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p,
775 enum zapi_route_notify_owner note)
776{
777 return (route_notify_internal(p, re->type, re->instance, re->vrf_id,
778 re->table, note));
779}
780
7cdb1a84
MS
781/*
782 * Route-owner notification using info from dataplane update context.
783 */
25779064 784int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
7cdb1a84
MS
785 enum zapi_route_notify_owner note)
786{
787 return (route_notify_internal(dplane_ctx_get_dest(ctx),
788 dplane_ctx_get_type(ctx),
789 dplane_ctx_get_instance(ctx),
790 dplane_ctx_get_vrf(ctx),
791 dplane_ctx_get_table(ctx),
792 note));
86391e56
MS
793}
794
bf094f69
QY
795void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
796 enum zapi_rule_notify_owner note)
797{
798 struct listnode *node;
799 struct zserv *client;
800 struct stream *s;
801
802 if (IS_ZEBRA_DEBUG_PACKET)
5e81f5dd 803 zlog_debug("%s: Notifying %u", __func__, rule->rule.unique);
bf094f69 804
161e9ab7 805 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
bf094f69
QY
806 if (rule->sock == client->sock)
807 break;
808 }
809
810 if (!client)
811 return;
812
813 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
814
815 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
816 stream_put(s, &note, sizeof(note));
817 stream_putl(s, rule->rule.seq);
818 stream_putl(s, rule->rule.priority);
819 stream_putl(s, rule->rule.unique);
b19d55d0 820 stream_putl(s, rule->rule.ifindex);
bf094f69
QY
821
822 stream_putw_at(s, 0, stream_get_endp(s));
823
21ccc0cf 824 zserv_send_message(client, s);
bf094f69
QY
825}
826
827void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
828 enum zapi_ipset_notify_owner note)
829{
830 struct listnode *node;
831 struct zserv *client;
832 struct stream *s;
833
834 if (IS_ZEBRA_DEBUG_PACKET)
5e81f5dd 835 zlog_debug("%s: Notifying %u", __func__, ipset->unique);
bf094f69 836
161e9ab7 837 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
bf094f69
QY
838 if (ipset->sock == client->sock)
839 break;
840 }
841
842 if (!client)
843 return;
844
845 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
846
847 zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
848 stream_put(s, &note, sizeof(note));
849 stream_putl(s, ipset->unique);
850 stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
851 stream_putw_at(s, 0, stream_get_endp(s));
852
21ccc0cf 853 zserv_send_message(client, s);
bf094f69
QY
854}
855
856void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset,
857 enum zapi_ipset_entry_notify_owner note)
858{
859 struct listnode *node;
860 struct zserv *client;
861 struct stream *s;
862
863 if (IS_ZEBRA_DEBUG_PACKET)
5e81f5dd 864 zlog_debug("%s: Notifying %u", __func__, ipset->unique);
bf094f69 865
161e9ab7 866 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
bf094f69
QY
867 if (ipset->sock == client->sock)
868 break;
869 }
870
871 if (!client)
872 return;
873
874 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
875
876 zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT);
877 stream_put(s, &note, sizeof(note));
878 stream_putl(s, ipset->unique);
879 stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE);
880 stream_putw_at(s, 0, stream_get_endp(s));
881
21ccc0cf 882 zserv_send_message(client, s);
bf094f69
QY
883}
884
885void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
886 enum zapi_iptable_notify_owner note)
887{
888 struct listnode *node;
889 struct zserv *client;
890 struct stream *s;
891
892 if (IS_ZEBRA_DEBUG_PACKET)
5e81f5dd 893 zlog_debug("%s: Notifying %u", __func__, iptable->unique);
bf094f69 894
161e9ab7 895 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
bf094f69
QY
896 if (iptable->sock == client->sock)
897 break;
898 }
899
900 if (!client)
901 return;
902
903 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
904
905 zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
906 stream_put(s, &note, sizeof(note));
907 stream_putl(s, iptable->unique);
908 stream_putw_at(s, 0, stream_get_endp(s));
909
21ccc0cf 910 zserv_send_message(client, s);
bf094f69
QY
911}
912
913/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
914int zsend_router_id_update(struct zserv *client, struct prefix *p,
915 vrf_id_t vrf_id)
916{
917 int blen;
918
919 /* Check this client need interface information. */
920 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
921 return 0;
922
923 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
924
925 /* Message type. */
926 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
927
928 /* Prefix information. */
929 stream_putc(s, p->family);
930 blen = prefix_blen(p);
931 stream_put(s, &p->u.prefix, blen);
932 stream_putc(s, p->prefixlen);
933
934 /* Write packet size. */
935 stream_putw_at(s, 0, stream_get_endp(s));
936
21ccc0cf 937 return zserv_send_message(client, s);
bf094f69
QY
938}
939
940/*
941 * Function used by Zebra to send a PW status update to LDP daemon
942 */
943int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
944{
945 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
946
947 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
948 stream_write(s, pw->ifname, IF_NAMESIZE);
949 stream_putl(s, pw->ifindex);
950 stream_putl(s, pw->status);
951
952 /* Put length at the first point of the stream. */
953 stream_putw_at(s, 0, stream_get_endp(s));
954
21ccc0cf 955 return zserv_send_message(client, s);
bf094f69
QY
956}
957
958/* Send response to a get label chunk request to client */
e11d7c96 959int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id,
e11d7c96 960 struct label_manager_chunk *lmc)
bf094f69
QY
961{
962 int ret;
963 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
964
965 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
f004f7c3 966 /* proto */
4cebdb9b 967 stream_putc(s, client->proto);
f004f7c3 968 /* instance */
4cebdb9b 969 stream_putw(s, client->instance);
bf094f69
QY
970
971 if (lmc) {
972 /* keep */
973 stream_putc(s, lmc->keep);
974 /* start and end labels */
975 stream_putl(s, lmc->start);
976 stream_putl(s, lmc->end);
977 }
978
979 /* Write packet size. */
980 stream_putw_at(s, 0, stream_get_endp(s));
981
982 ret = writen(client->sock, s->data, stream_get_endp(s));
983 stream_free(s);
984 return ret;
985}
986
987/* Send response to a label manager connect request to client */
e11d7c96
EDP
988int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id,
989 unsigned short result)
bf094f69
QY
990{
991 int ret;
992 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
993
994 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
995
5dffb0e9
FR
996 /* proto */
997 stream_putc(s, client->proto);
998
999 /* instance */
1000 stream_putw(s, client->instance);
1001
bf094f69
QY
1002 /* result */
1003 stream_putc(s, result);
1004
1005 /* Write packet size. */
1006 stream_putw_at(s, 0, stream_get_endp(s));
1007
1008 ret = writen(client->sock, s->data, stream_get_endp(s));
1009 stream_free(s);
1010
1011 return ret;
1012}
1013
1014/* Send response to a get table chunk request to client */
1015static int zsend_assign_table_chunk_response(struct zserv *client,
1016 vrf_id_t vrf_id,
1017 struct table_manager_chunk *tmc)
1018{
1019 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1020
1021 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
1022
1023 if (tmc) {
1024 /* start and end labels */
1025 stream_putl(s, tmc->start);
1026 stream_putl(s, tmc->end);
1027 }
1028
1029 /* Write packet size. */
1030 stream_putw_at(s, 0, stream_get_endp(s));
1031
21ccc0cf 1032 return zserv_send_message(client, s);
bf094f69
QY
1033}
1034
1035static int zsend_table_manager_connect_response(struct zserv *client,
1036 vrf_id_t vrf_id,
1037 uint16_t result)
1038{
1039 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1040
1041 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
1042
1043 /* result */
1044 stream_putc(s, result);
1045
1046 stream_putw_at(s, 0, stream_get_endp(s));
1047
21ccc0cf 1048 return zserv_send_message(client, s);
bf094f69
QY
1049}
1050
1051/* Inbound message handling ------------------------------------------------ */
1052
2b64873d 1053const int cmd2type[] = {
bf094f69
QY
1054 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
1055 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
1056 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
1057 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
1058};
1059
1060/* Nexthop register */
1061static void zread_rnh_register(ZAPI_HANDLER_ARGS)
1062{
1063 struct rnh *rnh;
1064 struct stream *s;
1065 struct prefix p;
1066 unsigned short l = 0;
1067 uint8_t flags = 0;
1068 uint16_t type = cmd2type[hdr->command];
1d30d1f4 1069 bool exist;
906b54dd
DS
1070 bool flag_changed = false;
1071 uint8_t orig_flags;
bf094f69
QY
1072
1073 if (IS_ZEBRA_DEBUG_NHT)
1074 zlog_debug(
63efca0e 1075 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
bf094f69
QY
1076 zebra_route_string(client->proto), hdr->length,
1077 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
1078 zvrf->vrf->vrf_id);
1079
1080 s = msg;
1081
1082 client->nh_reg_time = monotime(NULL);
1083
1084 while (l < hdr->length) {
1085 STREAM_GETC(s, flags);
1086 STREAM_GETW(s, p.family);
1087 STREAM_GETC(s, p.prefixlen);
1088 l += 4;
1089 if (p.family == AF_INET) {
ab5990d8 1090 client->v4_nh_watch_add_cnt++;
bf094f69 1091 if (p.prefixlen > IPV4_MAX_BITLEN) {
9df414fe 1092 zlog_debug(
bf094f69 1093 "%s: Specified prefix hdr->length %d is too large for a v4 address",
15569c58 1094 __func__, p.prefixlen);
bf094f69
QY
1095 return;
1096 }
1097 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1098 l += IPV4_MAX_BYTELEN;
1099 } else if (p.family == AF_INET6) {
ab5990d8 1100 client->v6_nh_watch_add_cnt++;
bf094f69 1101 if (p.prefixlen > IPV6_MAX_BITLEN) {
9df414fe 1102 zlog_debug(
bf094f69 1103 "%s: Specified prefix hdr->length %d is to large for a v6 address",
15569c58 1104 __func__, p.prefixlen);
bf094f69
QY
1105 return;
1106 }
1107 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1108 l += IPV6_MAX_BYTELEN;
1109 } else {
af4c2728 1110 flog_err(
e914ccbe 1111 EC_ZEBRA_UNKNOWN_FAMILY,
bf094f69
QY
1112 "rnh_register: Received unknown family type %d\n",
1113 p.family);
1114 return;
1115 }
1d30d1f4
DS
1116 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type, &exist);
1117 if (!rnh)
1118 return;
1119
906b54dd 1120 orig_flags = rnh->flags;
bf094f69
QY
1121 if (type == RNH_NEXTHOP_TYPE) {
1122 if (flags
1123 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1124 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1125 else if (!flags
1126 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1127 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1128 } else if (type == RNH_IMPORT_CHECK_TYPE) {
1129 if (flags
1130 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
1131 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1132 else if (!flags
1133 && CHECK_FLAG(rnh->flags,
1134 ZEBRA_NHT_EXACT_MATCH))
1135 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1136 }
1137
906b54dd
DS
1138 if (orig_flags != rnh->flags)
1139 flag_changed = true;
1140
bf094f69 1141 /* Anything not AF_INET/INET6 has been filtered out above */
906b54dd 1142 if (!exist || flag_changed)
73bf60a0
RW
1143 zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, type,
1144 &p);
dd25a6b3
DS
1145
1146 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
bf094f69
QY
1147 }
1148
1149stream_failure:
1150 return;
1151}
1152
1153/* Nexthop register */
1154static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1155{
1156 struct rnh *rnh;
1157 struct stream *s;
1158 struct prefix p;
1159 unsigned short l = 0;
1160 uint16_t type = cmd2type[hdr->command];
1161
1162 if (IS_ZEBRA_DEBUG_NHT)
1163 zlog_debug(
63efca0e 1164 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
bf094f69
QY
1165 zebra_route_string(client->proto), hdr->length,
1166 zvrf->vrf->vrf_id);
1167
1168 s = msg;
1169
1170 while (l < hdr->length) {
1171 uint8_t flags;
1172
1173 STREAM_GETC(s, flags);
1174 if (flags != 0)
1175 goto stream_failure;
1176
1177 STREAM_GETW(s, p.family);
1178 STREAM_GETC(s, p.prefixlen);
1179 l += 4;
1180 if (p.family == AF_INET) {
ab5990d8 1181 client->v4_nh_watch_rem_cnt++;
bf094f69 1182 if (p.prefixlen > IPV4_MAX_BITLEN) {
9df414fe 1183 zlog_debug(
bf094f69 1184 "%s: Specified prefix hdr->length %d is to large for a v4 address",
15569c58 1185 __func__, p.prefixlen);
bf094f69
QY
1186 return;
1187 }
1188 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1189 l += IPV4_MAX_BYTELEN;
1190 } else if (p.family == AF_INET6) {
ab5990d8 1191 client->v6_nh_watch_rem_cnt++;
bf094f69 1192 if (p.prefixlen > IPV6_MAX_BITLEN) {
9df414fe 1193 zlog_debug(
bf094f69 1194 "%s: Specified prefix hdr->length %d is to large for a v6 address",
15569c58 1195 __func__, p.prefixlen);
bf094f69
QY
1196 return;
1197 }
1198 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1199 l += IPV6_MAX_BYTELEN;
1200 } else {
af4c2728 1201 flog_err(
e914ccbe 1202 EC_ZEBRA_UNKNOWN_FAMILY,
bf094f69
QY
1203 "rnh_register: Received unknown family type %d\n",
1204 p.family);
1205 return;
1206 }
1207 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
1208 if (rnh) {
1209 client->nh_dereg_time = monotime(NULL);
1210 zebra_remove_rnh_client(rnh, client, type);
1211 }
1212 }
1213stream_failure:
1214 return;
1215}
1216
1217#define ZEBRA_MIN_FEC_LENGTH 5
1218
1219/* FEC register */
1220static void zread_fec_register(ZAPI_HANDLER_ARGS)
1221{
1222 struct stream *s;
1223 unsigned short l = 0;
1224 struct prefix p;
1225 uint16_t flags;
57592a53 1226 uint32_t label = MPLS_INVALID_LABEL;
bf094f69
QY
1227 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1228
1229 s = msg;
1230 zvrf = vrf_info_lookup(VRF_DEFAULT);
1231 if (!zvrf)
8b1766b1 1232 return;
bf094f69
QY
1233
1234 /*
1235 * The minimum amount of data that can be sent for one fec
1236 * registration
1237 */
1238 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
af4c2728 1239 flog_err(
e914ccbe 1240 EC_ZEBRA_IRDP_LEN_MISMATCH,
bf094f69
QY
1241 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1242 hdr->length);
1243 return;
1244 }
1245
1246 while (l < hdr->length) {
1247 STREAM_GETW(s, flags);
1248 memset(&p, 0, sizeof(p));
1249 STREAM_GETW(s, p.family);
1250 if (p.family != AF_INET && p.family != AF_INET6) {
af4c2728 1251 flog_err(
e914ccbe 1252 EC_ZEBRA_UNKNOWN_FAMILY,
bf094f69
QY
1253 "fec_register: Received unknown family type %d\n",
1254 p.family);
1255 return;
1256 }
1257 STREAM_GETC(s, p.prefixlen);
1258 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1259 || (p.family == AF_INET6
1260 && p.prefixlen > IPV6_MAX_BITLEN)) {
9df414fe 1261 zlog_debug(
bf094f69 1262 "%s: Specified prefix hdr->length: %d is to long for %d",
15569c58 1263 __func__, p.prefixlen, p.family);
bf094f69
QY
1264 return;
1265 }
1266 l += 5;
1267 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1268 l += PSIZE(p.prefixlen);
57592a53
AD
1269 if (flags & ZEBRA_FEC_REGISTER_LABEL) {
1270 STREAM_GETL(s, label);
1271 l += 4;
1272 } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
bf094f69
QY
1273 STREAM_GETL(s, label_index);
1274 l += 4;
57592a53
AD
1275 }
1276
1277 zebra_mpls_fec_register(zvrf, &p, label, label_index, client);
bf094f69
QY
1278 }
1279
1280stream_failure:
1281 return;
1282}
1283
1284/* FEC unregister */
1285static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1286{
1287 struct stream *s;
1288 unsigned short l = 0;
1289 struct prefix p;
1290 uint16_t flags;
1291
1292 s = msg;
1293 zvrf = vrf_info_lookup(VRF_DEFAULT);
1294 if (!zvrf)
8b1766b1 1295 return;
bf094f69
QY
1296
1297 /*
1298 * The minimum amount of data that can be sent for one
1299 * fec unregistration
1300 */
1301 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
af4c2728 1302 flog_err(
e914ccbe 1303 EC_ZEBRA_IRDP_LEN_MISMATCH,
bf094f69
QY
1304 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1305 hdr->length);
1306 return;
1307 }
1308
1309 while (l < hdr->length) {
1310 STREAM_GETW(s, flags);
1311 if (flags != 0)
1312 goto stream_failure;
1313
1314 memset(&p, 0, sizeof(p));
1315 STREAM_GETW(s, p.family);
1316 if (p.family != AF_INET && p.family != AF_INET6) {
af4c2728 1317 flog_err(
e914ccbe 1318 EC_ZEBRA_UNKNOWN_FAMILY,
bf094f69
QY
1319 "fec_unregister: Received unknown family type %d\n",
1320 p.family);
1321 return;
1322 }
1323 STREAM_GETC(s, p.prefixlen);
1324 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1325 || (p.family == AF_INET6
1326 && p.prefixlen > IPV6_MAX_BITLEN)) {
9df414fe 1327 zlog_debug(
bf094f69 1328 "%s: Received prefix hdr->length %d which is greater than %d can support",
15569c58 1329 __func__, p.prefixlen, p.family);
bf094f69
QY
1330 return;
1331 }
1332 l += 5;
1333 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1334 l += PSIZE(p.prefixlen);
1335 zebra_mpls_fec_unregister(zvrf, &p, client);
1336 }
1337
1338stream_failure:
1339 return;
1340}
1341
1342
1343/*
1344 * Register zebra server interface information.
1345 * Send current all interface and address information.
1346 */
1347static void zread_interface_add(ZAPI_HANDLER_ARGS)
1348{
1349 struct vrf *vrf;
1350 struct interface *ifp;
1351
bf094f69
QY
1352 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1353 FOR_ALL_INTERFACES (vrf, ifp) {
1354 /* Skip pseudo interface. */
1355 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1356 continue;
1357
1358 zsend_interface_add(client, ifp);
27ecbea3 1359 zsend_interface_link_params(client, ifp);
bf094f69
QY
1360 zsend_interface_addresses(client, ifp);
1361 }
1362 }
1363}
1364
1365/* Unregister zebra server interface information. */
1366static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1367{
bf094f69
QY
1368}
1369
c3bd894e
QY
1370/*
1371 * Handle message requesting interface be set up or down.
1372 */
1373static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS)
1374{
1375 ifindex_t ifindex;
1376 struct interface *ifp;
1377 char down;
1378
1379 STREAM_GETL(msg, ifindex);
1380 STREAM_GETC(msg, down);
1381
1382 /* set ifdown */
1383 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex);
c3bd894e 1384
65dc7dd3
QY
1385 if (ifp) {
1386 zlog_info("Setting interface %s (%u): protodown %s", ifp->name,
1387 ifindex, down ? "on" : "off");
1388 zebra_if_set_protodown(ifp, down);
1389 } else {
1390 zlog_warn(
1391 "Cannot set protodown %s for interface %u; does not exist",
1392 down ? "on" : "off", ifindex);
1393 }
1394
c3bd894e
QY
1395
1396stream_failure:
1397 return;
1398}
1399
1400
bf094f69
QY
1401void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1402 const unsigned int nexthop_num)
1403{
b3f2b590 1404 if (nexthop_num > zrouter.multipath_num) {
bf094f69 1405 char buff[PREFIX2STR_BUFFER];
8b1766b1 1406
bf094f69 1407 prefix2str(p, buff, sizeof(buff));
9df414fe 1408 flog_warn(
e914ccbe 1409 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
bf094f69 1410 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
b3f2b590 1411 caller, buff, nexthop_num, zrouter.multipath_num);
bf094f69
QY
1412 }
1413}
1414
62e46303
MS
1415/*
1416 * Create a new nexthop based on a zapi nexthop.
1417 */
1418static struct nexthop *nexthop_from_zapi(struct route_entry *re,
1419 const struct zapi_nexthop *api_nh,
1420 const struct zapi_route *api)
1421{
1422 struct nexthop *nexthop = NULL;
1423 struct ipaddr vtep_ip;
1424 struct interface *ifp;
1425 char nhbuf[INET6_ADDRSTRLEN] = "";
1426
62e46303
MS
1427 switch (api_nh->type) {
1428 case NEXTHOP_TYPE_IFINDEX:
1429 nexthop = nexthop_from_ifindex(api_nh->ifindex, api_nh->vrf_id);
1430 break;
1431 case NEXTHOP_TYPE_IPV4:
1432 if (IS_ZEBRA_DEBUG_RECV) {
1433 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1434 sizeof(nhbuf));
1435 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1436 nhbuf, api_nh->vrf_id);
1437 }
1438 nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4, NULL,
1439 api_nh->vrf_id);
1440 break;
1441 case NEXTHOP_TYPE_IPV4_IFINDEX:
1442 if (IS_ZEBRA_DEBUG_RECV) {
1443 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1444 sizeof(nhbuf));
1445 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1446 __func__, nhbuf, api_nh->vrf_id,
1447 api_nh->ifindex);
1448 }
1449
1450 nexthop = nexthop_from_ipv4_ifindex(
1451 &api_nh->gate.ipv4, NULL, api_nh->ifindex,
1452 api_nh->vrf_id);
1453
62e46303
MS
1454 /* Special handling for IPv4 routes sourced from EVPN:
1455 * the nexthop and associated MAC need to be installed.
1456 */
1457 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1458 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1459 vtep_ip.ipa_type = IPADDR_V4;
1460 memcpy(&(vtep_ip.ipaddr_v4), &(api_nh->gate.ipv4),
1461 sizeof(struct in_addr));
1462 zebra_vxlan_evpn_vrf_route_add(
1463 api_nh->vrf_id, &api_nh->rmac,
1464 &vtep_ip, &api->prefix);
1465 }
1466 break;
1467 case NEXTHOP_TYPE_IPV6:
1468 if (IS_ZEBRA_DEBUG_RECV) {
1469 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1470 sizeof(nhbuf));
1471 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1472 nhbuf, api_nh->vrf_id);
1473 }
1474 nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6, api_nh->vrf_id);
1475 break;
1476 case NEXTHOP_TYPE_IPV6_IFINDEX:
1477 if (IS_ZEBRA_DEBUG_RECV) {
1478 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1479 sizeof(nhbuf));
1480 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1481 __func__, nhbuf, api_nh->vrf_id,
1482 api_nh->ifindex);
1483 }
1484 nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6,
1485 api_nh->ifindex,
1486 api_nh->vrf_id);
1487
1488 /* Special handling for IPv6 routes sourced from EVPN:
1489 * the nexthop and associated MAC need to be installed.
1490 */
1491 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1492 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1493 vtep_ip.ipa_type = IPADDR_V6;
1494 memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6),
1495 sizeof(struct in6_addr));
1496 zebra_vxlan_evpn_vrf_route_add(
1497 api_nh->vrf_id, &api_nh->rmac,
1498 &vtep_ip, &api->prefix);
1499 }
1500 break;
1501 case NEXTHOP_TYPE_BLACKHOLE:
1502 if (IS_ZEBRA_DEBUG_RECV)
1503 zlog_debug("%s: nh blackhole %d",
1504 __func__, api_nh->bh_type);
1505
1506 nexthop = nexthop_from_blackhole(api_nh->bh_type);
1507 break;
1508 }
1509
1510 /* Return early if we couldn't process the zapi nexthop */
1511 if (nexthop == NULL) {
1512 goto done;
1513 }
1514
12b4d77b 1515 /* Mark nexthop as onlink either if client has explicitly told us
1516 * to or if the nexthop is on an 'unnumbered' interface.
1517 */
62e46303
MS
1518 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK))
1519 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
12b4d77b 1520 else if (api_nh->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1521 ifp = if_lookup_by_index(api_nh->ifindex, api_nh->vrf_id);
1522 if (ifp && connected_is_unnumbered(ifp))
1523 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1524 }
62e46303
MS
1525
1526 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT))
1527 nexthop->weight = api_nh->weight;
1528
1d48702e
MS
1529 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
1530 if (api_nh->backup_idx < api->backup_nexthop_num) {
1531 /* Capture backup info */
1532 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1533 nexthop->backup_idx = api_nh->backup_idx;
1534 } else {
1535 /* Warn about invalid backup index */
1536 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1537 zlog_debug("%s: invalid backup nh idx %d",
1538 __func__, api_nh->backup_idx);
1539 }
1540 }
62e46303
MS
1541done:
1542 return nexthop;
1543}
1544
bf094f69
QY
1545static void zread_route_add(ZAPI_HANDLER_ARGS)
1546{
1547 struct stream *s;
1548 struct zapi_route api;
1549 struct zapi_nexthop *api_nh;
1550 afi_t afi;
1551 struct prefix_ipv6 *src_p = NULL;
1552 struct route_entry *re;
62e46303 1553 struct nexthop *nexthop = NULL, *last_nh;
0eb97b86 1554 struct nexthop_group *ng = NULL;
1d48702e 1555 struct nhg_backup_info *bnhg = NULL;
bf094f69 1556 int i, ret;
946de1b9 1557 vrf_id_t vrf_id;
1d48702e
MS
1558 struct nhg_hash_entry nhe;
1559 enum lsp_types_t label_type;
1560 char nhbuf[NEXTHOP_STRLEN];
1561 char labelbuf[MPLS_LABEL_STRLEN];
bf094f69
QY
1562
1563 s = msg;
1564 if (zapi_route_decode(s, &api) < 0) {
1565 if (IS_ZEBRA_DEBUG_RECV)
1566 zlog_debug("%s: Unable to decode zapi_route sent",
15569c58 1567 __func__);
bf094f69
QY
1568 return;
1569 }
1570
1571 if (IS_ZEBRA_DEBUG_RECV) {
1572 char buf_prefix[PREFIX_STRLEN];
8b1766b1 1573
bf094f69 1574 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
018c6488
MS
1575 zlog_debug("%s: p=%s, msg flags=0x%x, flags=0x%x",
1576 __func__, buf_prefix, (int)api.message, api.flags);
bf094f69
QY
1577 }
1578
1579 /* Allocate new route. */
1580 vrf_id = zvrf_id(zvrf);
1581 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1582 re->type = api.type;
1583 re->instance = api.instance;
1584 re->flags = api.flags;
98572489 1585 re->uptime = monotime(NULL);
bf094f69 1586 re->vrf_id = vrf_id;
6b468511 1587
60ca3cc2 1588 if (api.tableid)
bf094f69
QY
1589 re->table = api.tableid;
1590 else
1591 re->table = zvrf->table_id;
1592
7fcb24bb
RW
1593 if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)
1594 || api.nexthop_num == 0) {
7fcb24bb 1595 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS,
8ba70c4e
DS
1596 "%s: received a route without nexthops for prefix %pFX from client %s",
1597 __func__, &api.prefix,
1598 zebra_route_string(client->proto));
6b468511 1599
7fcb24bb
RW
1600 XFREE(MTYPE_RE, re);
1601 return;
1602 }
1603
62e46303
MS
1604 /* Report misuse of the backup flag */
1605 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS) &&
1606 api.backup_nexthop_num == 0) {
1607 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1608 zlog_debug("%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1609 __func__,
1610 zebra_route_string(client->proto), &api.prefix);
1611 }
1612
0eb97b86
MS
1613 /* Use temporary list of nexthops */
1614 ng = nexthop_group_new();
1615
bf094f69
QY
1616 /*
1617 * TBD should _all_ of the nexthop add operations use
1618 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1619 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1620 */
7fcb24bb
RW
1621 for (i = 0; i < api.nexthop_num; i++) {
1622 api_nh = &api.nexthops[i];
7fcb24bb 1623
62e46303
MS
1624 /* Convert zapi nexthop */
1625 nexthop = nexthop_from_zapi(re, api_nh, &api);
1626 if (!nexthop) {
1627 flog_warn(
1628 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1629 "%s: Nexthops Specified: %d but we failed to properly create one",
1630 __func__, api.nexthop_num);
1631 nexthop_group_delete(&ng);
1632 XFREE(MTYPE_RE, re);
1633 return;
1634 }
7fcb24bb 1635
62e46303
MS
1636 /* MPLS labels for BGP-LU or Segment Routing */
1637 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1638 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1639 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE
1640 && api_nh->label_num > 0) {
7fcb24bb 1641
62e46303 1642 label_type = lsp_type_from_re_type(client->proto);
62e46303
MS
1643 nexthop_add_labels(nexthop, label_type,
1644 api_nh->label_num,
1645 &api_nh->labels[0]);
bf094f69 1646 }
bf094f69 1647
1d48702e
MS
1648 if (IS_ZEBRA_DEBUG_RECV) {
1649 labelbuf[0] = '\0';
1650 nhbuf[0] = '\0';
1651
1652 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1653
1654 if (nexthop->nh_label &&
1655 nexthop->nh_label->num_labels > 0) {
1656 mpls_label2str(nexthop->nh_label->num_labels,
1657 nexthop->nh_label->label,
1658 labelbuf, sizeof(labelbuf),
1659 false);
1660 }
1661
1662 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1663 __func__, nhbuf, api_nh->vrf_id, labelbuf);
1664 }
1665
1666 /* Add new nexthop to temporary list. This list is
1667 * canonicalized - sorted - so that it can be hashed later
1668 * in route processing. We expect that the sender has sent
1669 * the list sorted, and the zapi client api attempts to enforce
1670 * that, so this should be inexpensive - but it is necessary
1671 * to support shared nexthop-groups.
1672 */
62e46303 1673 nexthop_group_add_sorted(ng, nexthop);
62e46303
MS
1674 }
1675
1676 /* Allocate temporary list of backup nexthops, if necessary */
1677 if (api.backup_nexthop_num > 0) {
1d48702e
MS
1678 if (IS_ZEBRA_DEBUG_RECV)
1679 zlog_debug("%s: adding %d backup nexthops",
1680 __func__, api.backup_nexthop_num);
1681
1682 bnhg = zebra_nhg_backup_alloc();
62e46303
MS
1683 nexthop = NULL;
1684 last_nh = NULL;
1685 }
1686
1687 /* Copy backup nexthops also, if present */
1688 for (i = 0; i < api.backup_nexthop_num; i++) {
1689 api_nh = &api.backup_nexthops[i];
1690
1691 /* Convert zapi backup nexthop */
1692 nexthop = nexthop_from_zapi(re, api_nh, &api);
7fcb24bb
RW
1693 if (!nexthop) {
1694 flog_warn(
1695 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
62e46303
MS
1696 "%s: Backup Nexthops Specified: %d but we failed to properly create one",
1697 __func__, api.backup_nexthop_num);
0eb97b86 1698 nexthop_group_delete(&ng);
1d48702e 1699 zebra_nhg_backup_free(&bnhg);
7fcb24bb
RW
1700 XFREE(MTYPE_RE, re);
1701 return;
1702 }
0eb97b86 1703
1d48702e
MS
1704 /* Backup nexthops can't have backups; that's not valid. */
1705 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
1706 if (IS_ZEBRA_DEBUG_RECV) {
1707 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1708 zlog_debug("%s: backup nh %s with BACKUP flag!",
1709 __func__, nhbuf);
1710 }
1711 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1712 nexthop->backup_idx = 0;
1713 }
1714
7fcb24bb 1715 /* MPLS labels for BGP-LU or Segment Routing */
68a02e06 1716 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
7fcb24bb 1717 && api_nh->type != NEXTHOP_TYPE_IFINDEX
62e46303
MS
1718 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE
1719 && api_nh->label_num > 0) {
7fcb24bb
RW
1720
1721 label_type = lsp_type_from_re_type(client->proto);
7fcb24bb
RW
1722 nexthop_add_labels(nexthop, label_type,
1723 api_nh->label_num,
1724 &api_nh->labels[0]);
1725 }
0eb97b86 1726
1d48702e
MS
1727 if (IS_ZEBRA_DEBUG_RECV) {
1728 labelbuf[0] = '\0';
1729 nhbuf[0] = '\0';
1730
1731 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1732
1733 if (nexthop->nh_label &&
1734 nexthop->nh_label->num_labels > 0) {
1735 mpls_label2str(nexthop->nh_label->num_labels,
1736 nexthop->nh_label->label,
1737 labelbuf, sizeof(labelbuf),
1738 false);
1739 }
1740
1741 zlog_debug("%s: backup nh=%s, vrf_id=%d %s",
1742 __func__, nhbuf, api_nh->vrf_id, labelbuf);
1743 }
1744
1745 /* Note that the order of the backup nexthops is significant,
1746 * so we don't sort this list as we do the primary nexthops,
1747 * we just append.
62e46303 1748 */
1d48702e 1749 if (last_nh)
62e46303 1750 NEXTHOP_APPEND(last_nh, nexthop);
1d48702e
MS
1751 else
1752 bnhg->nhe->nhg.nexthop = nexthop;
62e46303
MS
1753
1754 last_nh = nexthop;
7fcb24bb
RW
1755 }
1756
bf094f69
QY
1757 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1758 re->distance = api.distance;
1759 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1760 re->metric = api.metric;
1761 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1762 re->tag = api.tag;
1763 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1764 re->mtu = api.mtu;
1765
1766 afi = family2afi(api.prefix.family);
1767 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 1768 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 1769 "%s: Received SRC Prefix but afi is not v6",
15569c58 1770 __func__);
0eb97b86 1771 nexthop_group_delete(&ng);
1d48702e 1772 zebra_nhg_backup_free(&bnhg);
bf094f69
QY
1773 XFREE(MTYPE_RE, re);
1774 return;
1775 }
1776 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1777 src_p = &api.src_prefix;
1778
5e7939a5
DS
1779 if (api.safi != SAFI_UNICAST && api.safi != SAFI_MULTICAST) {
1780 flog_warn(EC_LIB_ZAPI_MISSMATCH,
1781 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1782 __func__, api.safi);
1783 nexthop_group_delete(&ng);
1d48702e 1784 zebra_nhg_backup_free(&bnhg);
5e7939a5
DS
1785 XFREE(MTYPE_RE, re);
1786 return;
1787 }
1d48702e 1788
377e29f7
MS
1789 /* Include backup info with the route. We use a temporary nhe here;
1790 * if this is a new/unknown nhe, a new copy will be allocated
1791 * and stored.
1792 */
1793 zebra_nhe_init(&nhe, afi, ng->nexthop);
1d48702e
MS
1794 nhe.nhg.nexthop = ng->nexthop;
1795 nhe.backup_info = bnhg;
1796 ret = rib_add_multipath_nhe(afi, api.safi, &api.prefix, src_p,
1797 re, &nhe);
bf094f69 1798
377e29f7
MS
1799 /* At this point, these allocations are not needed: 're' has been
1800 * retained or freed, and if 're' still exists, it is using
1801 * a reference to a shared group object.
1802 */
1803 nexthop_group_delete(&ng);
1804 if (bnhg)
1805 zebra_nhg_backup_free(&bnhg);
1806
bf094f69
QY
1807 /* Stats */
1808 switch (api.prefix.family) {
1809 case AF_INET:
1810 if (ret > 0)
1811 client->v4_route_add_cnt++;
1812 else if (ret < 0)
1813 client->v4_route_upd8_cnt++;
1814 break;
1815 case AF_INET6:
1816 if (ret > 0)
1817 client->v6_route_add_cnt++;
1818 else if (ret < 0)
1819 client->v6_route_upd8_cnt++;
1820 break;
1821 }
1822}
1823
1824static void zread_route_del(ZAPI_HANDLER_ARGS)
1825{
1826 struct stream *s;
1827 struct zapi_route api;
1828 afi_t afi;
1829 struct prefix_ipv6 *src_p = NULL;
1830 uint32_t table_id;
1831
1832 s = msg;
1833 if (zapi_route_decode(s, &api) < 0)
1834 return;
1835
1836 afi = family2afi(api.prefix.family);
1837 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 1838 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 1839 "%s: Received a src prefix while afi is not v6",
15569c58 1840 __func__);
bf094f69
QY
1841 return;
1842 }
1843 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1844 src_p = &api.src_prefix;
1845
60ca3cc2 1846 if (api.tableid)
bf094f69
QY
1847 table_id = api.tableid;
1848 else
1849 table_id = zvrf->table_id;
1850
1851 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
bc541126 1852 api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric,
40ecd8e4 1853 api.distance, false);
bf094f69
QY
1854
1855 /* Stats */
1856 switch (api.prefix.family) {
1857 case AF_INET:
1858 client->v4_route_del_cnt++;
1859 break;
1860 case AF_INET6:
1861 client->v6_route_del_cnt++;
1862 break;
1863 }
1864}
1865
bf094f69
QY
1866/* MRIB Nexthop lookup for IPv4. */
1867static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1868{
1869 struct in_addr addr;
1870 struct route_entry *re;
1871
1872 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1873 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1874 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1875
1876stream_failure:
1877 return;
1878}
1879
bf094f69
QY
1880/* Register zebra server router-id information. Send current router-id */
1881static void zread_router_id_add(ZAPI_HANDLER_ARGS)
1882{
1883 struct prefix p;
1884
1885 /* Router-id information is needed. */
1886 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
1887
1888 router_id_get(&p, zvrf_id(zvrf));
1889
1890 zsend_router_id_update(client, &p, zvrf_id(zvrf));
1891}
1892
1893/* Unregister zebra server router-id information. */
1894static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
1895{
1896 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1897}
1898
1899static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
1900{
1901 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1902
1903 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
bb6b7f79 1904 stream_putl(s, vrf_get_backend());
bf094f69 1905 stream_putc(s, mpls_enabled);
b3f2b590 1906 stream_putl(s, zrouter.multipath_num);
02c0866d 1907 stream_putc(s, zebra_mlag_get_role());
bf094f69
QY
1908
1909 stream_putw_at(s, 0, stream_get_endp(s));
21ccc0cf 1910 zserv_send_message(client, s);
bf094f69
QY
1911}
1912
b120fe3b
DS
1913void zsend_capabilities_all_clients(void)
1914{
1915 struct listnode *node, *nnode;
1916 struct zebra_vrf *zvrf;
1917 struct zserv *client;
1918
1919 zvrf = vrf_info_lookup(VRF_DEFAULT);
1920 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
1921 /* Do not send unsolicited messages to synchronous clients. */
1922 if (client->synchronous)
1923 continue;
1924
b120fe3b
DS
1925 zsend_capabilities(client, zvrf);
1926 }
1927}
1928
bf094f69
QY
1929/* Tie up route-type and client->sock */
1930static void zread_hello(ZAPI_HANDLER_ARGS)
1931{
1932 /* type of protocol (lib/zebra.h) */
1933 uint8_t proto;
1934 unsigned short instance;
1935 uint8_t notify;
17da84a4 1936 uint8_t synchronous;
4e0b5b31 1937 uint32_t session_id;
bf094f69
QY
1938
1939 STREAM_GETC(msg, proto);
1940 STREAM_GETW(msg, instance);
4e0b5b31 1941 STREAM_GETL(msg, session_id);
bf094f69 1942 STREAM_GETC(msg, notify);
17da84a4 1943 STREAM_GETC(msg, synchronous);
bf094f69
QY
1944 if (notify)
1945 client->notify_owner = true;
1946
17da84a4
KS
1947 if (synchronous)
1948 client->synchronous = true;
1949
bf094f69 1950 /* accept only dynamic routing protocols */
f23cbcda 1951 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
bf094f69
QY
1952 zlog_notice(
1953 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1954 client->sock, zebra_route_string(proto),
1955 zvrf->vrf->vrf_id);
1956 if (instance)
1957 zlog_notice("client protocol instance %d", instance);
1958
1959 client->proto = proto;
1960 client->instance = instance;
4e0b5b31 1961 client->session_id = session_id;
6f4aee61
S
1962
1963 /* Graceful restart processing for client connect */
1964 zebra_gr_client_reconnect(client);
bf094f69
QY
1965 }
1966
17da84a4
KS
1967 if (!client->synchronous) {
1968 zsend_capabilities(client, zvrf);
1969 zebra_vrf_update_all(client);
1970 }
bf094f69
QY
1971stream_failure:
1972 return;
1973}
1974
1975/* Unregister all information in a VRF. */
1976static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
1977{
1978 int i;
1979 afi_t afi;
1980
49db7a7b 1981 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
bf094f69
QY
1982 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1983 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
49db7a7b
RW
1984 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
1985 }
bf094f69
QY
1986 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1987}
1988
ea6b290b
RW
1989/*
1990 * Handle request to create an MPLS LSP.
1991 *
1992 * A single message can fully specify an LSP with multiple nexthops.
1993 *
1994 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1995 * updated to use the received label(s).
1996 */
1997static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS)
bf094f69
QY
1998{
1999 struct stream *s;
bad6b0e7 2000 struct zapi_labels zl;
f2e7f4eb 2001 int ret;
bf094f69
QY
2002
2003 /* Get input stream. */
2004 s = msg;
bad6b0e7
RW
2005 if (zapi_labels_decode(s, &zl) < 0) {
2006 if (IS_ZEBRA_DEBUG_RECV)
2007 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2008 __func__);
bf094f69
QY
2009 return;
2010 }
bf094f69 2011
bf094f69
QY
2012 if (!mpls_enabled)
2013 return;
2014
f2e7f4eb
MS
2015 ret = mpls_zapi_labels_process(true, zvrf, &zl);
2016 if (ret < 0) {
2017 if (IS_ZEBRA_DEBUG_RECV)
2018 zlog_debug("%s: Error processing zapi request",
2019 __func__);
ea6b290b
RW
2020 }
2021}
2022
2023/*
2024 * Handle request to delete an MPLS LSP.
2025 *
2026 * An LSP is identified by its type and local label. When the received message
2027 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2028 * listed LSP nexthops (aka NHLFEs) are deleted.
2029 *
2030 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2031 * FEC (route) nexthops are deleted.
2032 */
2033static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS)
2034{
2035 struct stream *s;
2036 struct zapi_labels zl;
f2e7f4eb 2037 int ret;
ea6b290b
RW
2038
2039 /* Get input stream. */
2040 s = msg;
2041 if (zapi_labels_decode(s, &zl) < 0) {
2042 if (IS_ZEBRA_DEBUG_RECV)
2043 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2044 __func__);
ea6b290b
RW
2045 return;
2046 }
2047
2048 if (!mpls_enabled)
2049 return;
2050
2051 if (zl.nexthop_num > 0) {
f2e7f4eb
MS
2052 ret = mpls_zapi_labels_process(false /*delete*/, zvrf, &zl);
2053 if (ret < 0) {
2054 if (IS_ZEBRA_DEBUG_RECV)
2055 zlog_debug("%s: Error processing zapi request",
2056 __func__);
ea6b290b
RW
2057 }
2058 } else {
2059 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2060
b3c49d0e 2061 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
ea6b290b
RW
2062 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2063 zl.route.type, zl.route.instance);
2064 }
2065}
2066
2067/*
2068 * Handle request to add an MPLS LSP or change an existing one.
2069 *
2070 * A single message can fully specify an LSP with multiple nexthops.
2071 *
2072 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2073 * updated to use the received label(s).
2074 *
2075 * NOTE: zebra will use route replace semantics (make-before-break) to update
2076 * the LSP in the forwarding plane if that's supported by the underlying
2077 * platform.
2078 */
2079static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS)
2080{
2081 struct stream *s;
2082 struct zapi_labels zl;
2083
2084 /* Get input stream. */
2085 s = msg;
2086 if (zapi_labels_decode(s, &zl) < 0) {
2087 if (IS_ZEBRA_DEBUG_RECV)
2088 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2089 __func__);
ea6b290b
RW
2090 return;
2091 }
2092
2093 if (!mpls_enabled)
2094 return;
2095
f2e7f4eb
MS
2096 /* This removes everything, then re-adds from the client's
2097 * zapi message. Since the LSP will be processed later, on this
2098 * this same pthread, all of the changes will 'appear' at once.
2099 */
ea6b290b
RW
2100 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2101 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
2102 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2103 zl.route.type, zl.route.instance);
2104
f2e7f4eb 2105 mpls_zapi_labels_process(true, zvrf, &zl);
bf094f69
QY
2106}
2107
2108/* Send response to a table manager connect request to client */
2109static void zread_table_manager_connect(struct zserv *client,
2110 struct stream *msg, vrf_id_t vrf_id)
2111{
2112 struct stream *s;
2113 uint8_t proto;
2114 uint16_t instance;
c479e756 2115 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
bf094f69
QY
2116
2117 s = msg;
2118
2119 /* Get data. */
2120 STREAM_GETC(s, proto);
2121 STREAM_GETW(s, instance);
2122
2123 /* accept only dynamic routing protocols */
2124 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 2125 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
2126 "client %d has wrong protocol %s", client->sock,
2127 zebra_route_string(proto));
bf094f69
QY
2128 zsend_table_manager_connect_response(client, vrf_id, 1);
2129 return;
2130 }
c479e756
DS
2131 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2132 client->sock, VRF_LOGNAME(vrf), vrf_id, instance,
2133 zebra_route_string(proto));
bf094f69
QY
2134 client->proto = proto;
2135 client->instance = instance;
2136
2137 /*
2138 * Release previous labels of same protocol and instance.
2139 * This is done in case it restarted from an unexpected shutdown.
2140 */
453844ab 2141 release_daemon_table_chunks(client);
bf094f69
QY
2142
2143 zsend_table_manager_connect_response(client, vrf_id, 0);
2144
2145stream_failure:
2146 return;
2147}
2148
2149static void zread_label_manager_connect(struct zserv *client,
2150 struct stream *msg, vrf_id_t vrf_id)
2151{
2152 struct stream *s;
2153 /* type of protocol (lib/zebra.h) */
2154 uint8_t proto;
2155 unsigned short instance;
2156
2157 /* Get input stream. */
2158 s = msg;
2159
2160 /* Get data. */
2161 STREAM_GETC(s, proto);
2162 STREAM_GETW(s, instance);
2163
2164 /* accept only dynamic routing protocols */
2165 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 2166 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
2167 "client %d has wrong protocol %s", client->sock,
2168 zebra_route_string(proto));
e11d7c96 2169 zsend_label_manager_connect_response(client, vrf_id, 1);
bf094f69
QY
2170 return;
2171 }
e11d7c96
EDP
2172
2173 /* recall proto and instance in this socket */
bf094f69
QY
2174 client->proto = proto;
2175 client->instance = instance;
2176
e11d7c96 2177 /* call hook for connection using wrapper */
4cebdb9b 2178 lm_client_connect_call(client, vrf_id);
bf094f69
QY
2179
2180stream_failure:
2181 return;
2182}
2183
2184static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
2185 vrf_id_t vrf_id)
2186{
2187 struct stream *s;
2188 uint8_t keep;
0e3b6a92 2189 uint32_t size, base;
e11d7c96 2190 struct label_manager_chunk *lmc = NULL;
5dffb0e9
FR
2191 uint8_t proto;
2192 unsigned short instance;
bf094f69
QY
2193
2194 /* Get input stream. */
2195 s = msg;
2196
2197 /* Get data. */
5dffb0e9
FR
2198 STREAM_GETC(s, proto);
2199 STREAM_GETW(s, instance);
bf094f69
QY
2200 STREAM_GETC(s, keep);
2201 STREAM_GETL(s, size);
0e3b6a92 2202 STREAM_GETL(s, base);
bf094f69 2203
4cebdb9b
MS
2204 assert(proto == client->proto && instance == client->instance);
2205
e11d7c96 2206 /* call hook to get a chunk using wrapper */
4cebdb9b 2207 lm_get_chunk_call(&lmc, client, keep, size, base, vrf_id);
bf094f69 2208
bf094f69
QY
2209stream_failure:
2210 return;
2211}
2212
2213static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2214{
2215 struct stream *s;
2216 uint32_t start, end;
5dffb0e9
FR
2217 uint8_t proto;
2218 unsigned short instance;
bf094f69
QY
2219
2220 /* Get input stream. */
2221 s = msg;
2222
2223 /* Get data. */
5dffb0e9
FR
2224 STREAM_GETC(s, proto);
2225 STREAM_GETW(s, instance);
bf094f69
QY
2226 STREAM_GETL(s, start);
2227 STREAM_GETL(s, end);
2228
4cebdb9b
MS
2229 assert(proto == client->proto && instance == client->instance);
2230
e11d7c96 2231 /* call hook to release a chunk using wrapper */
4cebdb9b 2232 lm_release_chunk_call(client, start, end);
bf094f69
QY
2233
2234stream_failure:
2235 return;
2236}
e11d7c96 2237
bf094f69
QY
2238static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2239{
e11d7c96
EDP
2240 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT
2241 || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
2242 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
bf094f69 2243 else {
e11d7c96
EDP
2244 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2245 zread_get_label_chunk(client, msg, zvrf_id(zvrf));
2246 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2247 zread_release_label_chunk(client, msg);
bf094f69
QY
2248 }
2249}
2250
2251static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
2252 vrf_id_t vrf_id)
2253{
2254 struct stream *s;
2255 uint32_t size;
2256 struct table_manager_chunk *tmc;
2257
2258 /* Get input stream. */
2259 s = msg;
2260
2261 /* Get data. */
2262 STREAM_GETL(s, size);
2263
2264 tmc = assign_table_chunk(client->proto, client->instance, size);
2265 if (!tmc)
e914ccbe 2266 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
1c50c1c0
QY
2267 "%s: Unable to assign Table Chunk of size %u",
2268 __func__, size);
bf094f69
QY
2269 else
2270 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2271 tmc->end);
2272 /* send response back */
2273 zsend_assign_table_chunk_response(client, vrf_id, tmc);
2274
2275stream_failure:
2276 return;
2277}
2278
2279static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
2280{
2281 struct stream *s;
2282 uint32_t start, end;
2283
2284 /* Get input stream. */
2285 s = msg;
2286
2287 /* Get data. */
2288 STREAM_GETL(s, start);
2289 STREAM_GETL(s, end);
2290
2291 release_table_chunk(client->proto, client->instance, start, end);
2292
2293stream_failure:
2294 return;
2295}
2296
2297static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2298{
2299 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2300 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2301 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2302 else {
2303 /* Sanity: don't allow 'unidentified' requests */
2304 if (!client->proto) {
af4c2728 2305 flog_err(
e914ccbe 2306 EC_ZEBRA_TM_ALIENS,
bf094f69
QY
2307 "Got table request from an unidentified client");
2308 return;
2309 }
2310 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2311 zread_get_table_chunk(client, msg, zvrf_id(zvrf));
2312 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2313 zread_release_table_chunk(client, msg);
2314 }
2315}
2316
2317static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2318{
2319 struct stream *s;
2320 char ifname[IF_NAMESIZE];
2321 ifindex_t ifindex;
2322 int type;
2323 int af;
2324 union g_addr nexthop;
2325 uint32_t local_label;
2326 uint32_t remote_label;
2327 uint8_t flags;
2328 union pw_protocol_fields data;
2329 uint8_t protocol;
2330 struct zebra_pw *pw;
2331
2332 /* Get input stream. */
2333 s = msg;
2334
2335 /* Get data. */
2336 STREAM_GET(ifname, s, IF_NAMESIZE);
f223b3d1 2337 ifname[IF_NAMESIZE - 1] = '\0';
bf094f69
QY
2338 STREAM_GETL(s, ifindex);
2339 STREAM_GETL(s, type);
2340 STREAM_GETL(s, af);
2341 switch (af) {
2342 case AF_INET:
2343 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2344 break;
2345 case AF_INET6:
2346 STREAM_GET(&nexthop.ipv6, s, 16);
2347 break;
2348 default:
2349 return;
2350 }
2351 STREAM_GETL(s, local_label);
2352 STREAM_GETL(s, remote_label);
2353 STREAM_GETC(s, flags);
2354 STREAM_GET(&data, s, sizeof(data));
2355 protocol = client->proto;
2356
2357 pw = zebra_pw_find(zvrf, ifname);
2358 switch (hdr->command) {
2359 case ZEBRA_PW_ADD:
2360 if (pw) {
e914ccbe 2361 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
9df414fe 2362 "%s: pseudowire %s already exists [%s]",
bf094f69
QY
2363 __func__, ifname,
2364 zserv_command_string(hdr->command));
2365 return;
2366 }
2367
2368 zebra_pw_add(zvrf, ifname, protocol, client);
2369 break;
2370 case ZEBRA_PW_DELETE:
2371 if (!pw) {
e914ccbe 2372 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 2373 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
2374 ifname, zserv_command_string(hdr->command));
2375 return;
2376 }
2377
2378 zebra_pw_del(zvrf, pw);
2379 break;
2380 case ZEBRA_PW_SET:
2381 case ZEBRA_PW_UNSET:
2382 if (!pw) {
e914ccbe 2383 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 2384 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
2385 ifname, zserv_command_string(hdr->command));
2386 return;
2387 }
2388
2389 switch (hdr->command) {
2390 case ZEBRA_PW_SET:
2391 pw->enabled = 1;
2392 break;
2393 case ZEBRA_PW_UNSET:
2394 pw->enabled = 0;
2395 break;
2396 }
2397
2398 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2399 remote_label, flags, &data);
2400 break;
2401 }
2402
2403stream_failure:
2404 return;
2405}
2406
2407static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2408{
2409 struct interface *master;
2410 struct interface *slave;
2411 struct stream *s = msg;
2412 int ifindex;
2413 vrf_id_t vrf_id;
2414
2415 STREAM_GETL(s, vrf_id);
2416 STREAM_GETL(s, ifindex);
2417 master = if_lookup_by_index(ifindex, vrf_id);
2418
2419 STREAM_GETL(s, vrf_id);
2420 STREAM_GETL(s, ifindex);
2421 slave = if_lookup_by_index(ifindex, vrf_id);
2422
2423 if (!master || !slave)
2424 return;
2425
2426 kernel_interface_set_master(master, slave);
2427
2428stream_failure:
2429 return;
2430}
2431
2432
2433static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2434{
2435 struct interface *ifp;
2436 mpls_label_t nlabel;
2437 afi_t afi;
2438 struct stream *s;
2439 struct zebra_vrf *def_zvrf;
2440 enum lsp_types_t ltype;
2441
2442 s = msg;
2443 STREAM_GETL(s, nlabel);
2444 STREAM_GETC(s, afi);
663d3a91
QY
2445
2446 if (!(IS_VALID_AFI(afi))) {
2447 zlog_warn("Invalid AFI for VRF label: %u", afi);
2448 return;
2449 }
2450
bf094f69
QY
2451 if (nlabel == zvrf->label[afi]) {
2452 /*
2453 * Nothing to do here move along
2454 */
2455 return;
2456 }
2457
2458 STREAM_GETC(s, ltype);
2459
2460 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
a36898e7 2461 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
bf094f69 2462 else
a36898e7 2463 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
bf094f69
QY
2464
2465 if (!ifp) {
2466 zlog_debug("Unable to find specified Interface for %s",
2467 zvrf->vrf->name);
2468 return;
2469 }
2470
2471 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2472
2473 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2474 afi_t scrubber;
2475 bool really_remove;
2476
2477 really_remove = true;
2478 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2479 if (scrubber == afi)
2480 continue;
2481
2482 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2483 continue;
2484
2485 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2486 really_remove = false;
2487 break;
2488 }
2489 }
2490
2491 if (really_remove)
2492 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2493 NEXTHOP_TYPE_IFINDEX, NULL,
2494 ifp->ifindex);
2495 }
2496
5065db0a
RW
2497 if (nlabel != MPLS_LABEL_NONE) {
2498 mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL;
2499 mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label,
2500 NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex);
2501 }
bf094f69
QY
2502
2503 zvrf->label[afi] = nlabel;
2504stream_failure:
2505 return;
2506}
2507
2508static inline void zread_rule(ZAPI_HANDLER_ARGS)
2509{
2510 struct zebra_pbr_rule zpr;
2511 struct stream *s;
2512 uint32_t total, i;
bf094f69
QY
2513
2514 s = msg;
2515 STREAM_GETL(s, total);
2516
2517 for (i = 0; i < total; i++) {
2518 memset(&zpr, 0, sizeof(zpr));
2519
2520 zpr.sock = client->sock;
2521 zpr.rule.vrf_id = hdr->vrf_id;
2522 STREAM_GETL(s, zpr.rule.seq);
2523 STREAM_GETL(s, zpr.rule.priority);
2524 STREAM_GETL(s, zpr.rule.unique);
2525 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2526 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2527 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2528 prefix_blen(&zpr.rule.filter.src_ip));
2529 STREAM_GETW(s, zpr.rule.filter.src_port);
2530 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2531 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2532 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2533 prefix_blen(&zpr.rule.filter.dst_ip));
2534 STREAM_GETW(s, zpr.rule.filter.dst_port);
2535 STREAM_GETL(s, zpr.rule.filter.fwmark);
2536 STREAM_GETL(s, zpr.rule.action.table);
b77a69bd 2537 STREAM_GETL(s, zpr.rule.ifindex);
bf094f69 2538
b77a69bd 2539 if (zpr.rule.ifindex) {
b19d55d0
SW
2540 struct interface *ifp;
2541
2542 ifp = if_lookup_by_index_per_ns(zvrf->zns,
2543 zpr.rule.ifindex);
2544 if (!ifp) {
bf094f69 2545 zlog_debug("Failed to lookup ifindex: %u",
b77a69bd 2546 zpr.rule.ifindex);
bf094f69
QY
2547 return;
2548 }
b19d55d0
SW
2549
2550 strlcpy(zpr.ifname, ifp->name, sizeof(zpr.ifname));
bf094f69
QY
2551 }
2552
2553 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2554 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2555
2556 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2557 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2558
2559 if (zpr.rule.filter.src_port)
2560 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2561
2562 if (zpr.rule.filter.dst_port)
2563 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2564
2565 if (zpr.rule.filter.fwmark)
2566 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2567
4719fd76
QY
2568 if (!(zpr.rule.filter.src_ip.family == AF_INET
2569 || zpr.rule.filter.src_ip.family == AF_INET6)) {
cc815be7
QY
2570 zlog_warn(
2571 "Unsupported PBR source IP family: %s (%" PRIu8
b7b05b46 2572 ")",
cc815be7
QY
2573 family2str(zpr.rule.filter.src_ip.family),
2574 zpr.rule.filter.src_ip.family);
4719fd76
QY
2575 return;
2576 }
2577 if (!(zpr.rule.filter.dst_ip.family == AF_INET
2578 || zpr.rule.filter.dst_ip.family == AF_INET6)) {
cec72d49
DA
2579 zlog_warn(
2580 "Unsupported PBR destination IP family: %s (%" PRIu8
2581 ")",
2582 family2str(zpr.rule.filter.dst_ip.family),
2583 zpr.rule.filter.dst_ip.family);
4719fd76
QY
2584 return;
2585 }
2586
2587
7f0ea8a4 2588 zpr.vrf_id = zvrf->vrf->vrf_id;
bf094f69 2589 if (hdr->command == ZEBRA_RULE_ADD)
7f0ea8a4 2590 zebra_pbr_add_rule(&zpr);
bf094f69 2591 else
7f0ea8a4 2592 zebra_pbr_del_rule(&zpr);
bf094f69
QY
2593 }
2594
2595stream_failure:
2596 return;
2597}
2598
2599static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2600{
2601 struct zebra_pbr_ipset zpi;
2602 struct stream *s;
2603 uint32_t total, i;
2604
2605 s = msg;
2606 STREAM_GETL(s, total);
2607
2608 for (i = 0; i < total; i++) {
2609 memset(&zpi, 0, sizeof(zpi));
2610
2611 zpi.sock = client->sock;
be2028d1 2612 zpi.vrf_id = zvrf->vrf->vrf_id;
bf094f69
QY
2613 STREAM_GETL(s, zpi.unique);
2614 STREAM_GETL(s, zpi.type);
2615 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2616
2617 if (hdr->command == ZEBRA_IPSET_CREATE)
62f20a52 2618 zebra_pbr_create_ipset(&zpi);
bf094f69 2619 else
62f20a52 2620 zebra_pbr_destroy_ipset(&zpi);
bf094f69
QY
2621 }
2622
2623stream_failure:
2624 return;
2625}
2626
2627static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2628{
2629 struct zebra_pbr_ipset_entry zpi;
2630 struct zebra_pbr_ipset ipset;
2631 struct stream *s;
2632 uint32_t total, i;
2633
2634 s = msg;
2635 STREAM_GETL(s, total);
2636
2637 for (i = 0; i < total; i++) {
2638 memset(&zpi, 0, sizeof(zpi));
2639 memset(&ipset, 0, sizeof(ipset));
2640
2641 zpi.sock = client->sock;
2642 STREAM_GETL(s, zpi.unique);
2643 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
261462c3 2644 ipset.ipset_name[ZEBRA_IPSET_NAME_SIZE - 1] = '\0';
bf094f69
QY
2645 STREAM_GETC(s, zpi.src.family);
2646 STREAM_GETC(s, zpi.src.prefixlen);
2647 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
2648 STREAM_GETC(s, zpi.dst.family);
2649 STREAM_GETC(s, zpi.dst.prefixlen);
2650 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
2651
25d760c5
PG
2652 STREAM_GETW(s, zpi.src_port_min);
2653 STREAM_GETW(s, zpi.src_port_max);
2654 STREAM_GETW(s, zpi.dst_port_min);
2655 STREAM_GETW(s, zpi.dst_port_max);
2656 STREAM_GETC(s, zpi.proto);
bf094f69
QY
2657 if (!is_default_prefix(&zpi.src))
2658 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2659
2660 if (!is_default_prefix(&zpi.dst))
2661 zpi.filter_bm |= PBR_FILTER_DST_IP;
be729dd7 2662 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5 2663 zpi.filter_bm |= PBR_FILTER_DST_PORT;
be729dd7 2664 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5
PG
2665 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
2666 if (zpi.dst_port_max != 0)
2667 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
2668 if (zpi.src_port_max != 0)
2669 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
2670 if (zpi.proto != 0)
2671 zpi.filter_bm |= PBR_FILTER_PROTO;
bf094f69 2672
9863725c
QY
2673 if (!(zpi.dst.family == AF_INET
2674 || zpi.dst.family == AF_INET6)) {
cec72d49
DA
2675 zlog_warn(
2676 "Unsupported PBR destination IP family: %s (%" PRIu8
2677 ")",
2678 family2str(zpi.dst.family), zpi.dst.family);
9863725c
QY
2679 goto stream_failure;
2680 }
2681 if (!(zpi.src.family == AF_INET
2682 || zpi.src.family == AF_INET6)) {
cec72d49
DA
2683 zlog_warn(
2684 "Unsupported PBR source IP family: %s (%" PRIu8
2685 ")",
2686 family2str(zpi.src.family), zpi.src.family);
9863725c
QY
2687 goto stream_failure;
2688 }
2689
bf094f69 2690 /* calculate backpointer */
62f20a52
DS
2691 zpi.backpointer =
2692 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
f096bae4
DS
2693
2694 if (!zpi.backpointer) {
2695 zlog_warn("ipset name specified: %s does not exist",
2696 ipset.ipset_name);
2697 goto stream_failure;
2698 }
2699
bf094f69 2700 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
62f20a52 2701 zebra_pbr_add_ipset_entry(&zpi);
bf094f69 2702 else
62f20a52 2703 zebra_pbr_del_ipset_entry(&zpi);
bf094f69
QY
2704 }
2705
2706stream_failure:
2707 return;
2708}
2709
2710static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2711{
8b5c4dce
QY
2712 struct zebra_pbr_iptable *zpi =
2713 XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
bf094f69
QY
2714 struct stream *s;
2715
2716 s = msg;
2717
8b5c4dce
QY
2718 zpi->interface_name_list = list_new();
2719 zpi->sock = client->sock;
2720 zpi->vrf_id = zvrf->vrf->vrf_id;
2721 STREAM_GETL(s, zpi->unique);
2722 STREAM_GETL(s, zpi->type);
2723 STREAM_GETL(s, zpi->filter_bm);
2724 STREAM_GETL(s, zpi->action);
2725 STREAM_GETL(s, zpi->fwmark);
2726 STREAM_GET(&zpi->ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2727 STREAM_GETW(s, zpi->pkt_len_min);
2728 STREAM_GETW(s, zpi->pkt_len_max);
2729 STREAM_GETW(s, zpi->tcp_flags);
2730 STREAM_GETW(s, zpi->tcp_mask_flags);
2731 STREAM_GETC(s, zpi->dscp_value);
2732 STREAM_GETC(s, zpi->fragment);
2733 STREAM_GETC(s, zpi->protocol);
2734 STREAM_GETL(s, zpi->nb_interface);
2735 zebra_pbr_iptable_update_interfacelist(s, zpi);
bf094f69
QY
2736
2737 if (hdr->command == ZEBRA_IPTABLE_ADD)
8b5c4dce 2738 zebra_pbr_add_iptable(zpi);
bf094f69 2739 else
8b5c4dce
QY
2740 zebra_pbr_del_iptable(zpi);
2741
bf094f69 2742stream_failure:
8b5c4dce
QY
2743 zebra_pbr_iptable_free(zpi);
2744 zpi = NULL;
bf094f69
QY
2745 return;
2746}
2747
9ab0b2a3
SW
2748static void zsend_error_msg(struct zserv *client, enum zebra_error_types error,
2749 struct zmsghdr *bad_hdr)
2750{
2751
2752 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2753
2754 zclient_create_header(s, ZEBRA_ERROR, bad_hdr->vrf_id);
2755
2756 zserv_encode_error(s, error);
2757
2758 client->error_cnt++;
2759 zserv_send_message(client, s);
2760}
2761
2762static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS)
2763{
2764 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2765 zlog_debug("ZAPI message specifies unknown VRF: %d",
2766 hdr->vrf_id);
2767
5ee080f0 2768 zsend_error_msg(client, ZEBRA_NO_VRF, hdr);
9ab0b2a3
SW
2769}
2770
2771static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS)
2772{
2773 zlog_info("Zebra received unknown command %d", hdr->command);
2774
5ee080f0 2775 zsend_error_msg(client, ZEBRA_INVALID_MSG_TYPE, hdr);
9ab0b2a3
SW
2776}
2777
2b64873d 2778void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
bf094f69
QY
2779 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2780 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2781 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2782 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
c3bd894e 2783 [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown,
bf094f69
QY
2784 [ZEBRA_ROUTE_ADD] = zread_route_add,
2785 [ZEBRA_ROUTE_DELETE] = zread_route_del,
bf094f69
QY
2786 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2787 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2788 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2789 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2790 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2791 [ZEBRA_HELLO] = zread_hello,
2792 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2793 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2794 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2795 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2796 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2797 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2798 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
d3af6147
RZ
2799#if HAVE_BFDD > 0
2800 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
2801#endif /* HAVE_BFDD */
bf094f69
QY
2802 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2803 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2804 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
bf094f69
QY
2805 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2806 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
ea6b290b
RW
2807 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add,
2808 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete,
2809 [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace,
bf094f69
QY
2810 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2811 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
f533be73 2812 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
bf094f69
QY
2813 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2814 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2815 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2816 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2817 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
278e26de 2818 [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip,
bf094f69
QY
2819 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2820 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2821 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2822 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2823 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2824 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
3950b52c 2825 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
bf094f69
QY
2826 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2827 [ZEBRA_PW_ADD] = zread_pseudowire,
2828 [ZEBRA_PW_DELETE] = zread_pseudowire,
2829 [ZEBRA_PW_SET] = zread_pseudowire,
2830 [ZEBRA_PW_UNSET] = zread_pseudowire,
2831 [ZEBRA_RULE_ADD] = zread_rule,
2832 [ZEBRA_RULE_DELETE] = zread_rule,
2833 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
2834 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
2835 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
2836 [ZEBRA_IPSET_CREATE] = zread_ipset,
2837 [ZEBRA_IPSET_DESTROY] = zread_ipset,
2838 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
2839 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
2840 [ZEBRA_IPTABLE_ADD] = zread_iptable,
2841 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
fbac9605 2842 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
ecbbc3a7 2843 [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay,
ee235396
SK
2844 [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register,
2845 [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister,
2846 [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg,
b9e6727a 2847 [ZEBRA_CLIENT_CAPABILITIES] = zread_client_capabilities
bf094f69
QY
2848};
2849
727c9b99
QY
2850#if defined(HANDLE_ZAPI_FUZZING)
2851extern struct zebra_privs_t zserv_privs;
2852
2853static void zserv_write_incoming(struct stream *orig, uint16_t command)
2854{
2855 char fname[MAXPATHLEN];
2856 struct stream *copy;
2857 int fd = -1;
2858
2859 copy = stream_dup(orig);
2860 stream_set_getp(copy, 0);
2861
3c649c71 2862 snprintf(fname, MAXPATHLEN, "%s/%u", frr_vtydir, command);
6bb30c2c 2863
0cf6db21 2864 frr_with_privs(&zserv_privs) {
6bb30c2c
DL
2865 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2866 }
727c9b99
QY
2867 stream_flush(copy, fd);
2868 close(fd);
727c9b99
QY
2869 stream_free(copy);
2870}
2871#endif
2872
904e0d88 2873void zserv_handle_commands(struct zserv *client, struct stream *msg)
bf094f69 2874{
904e0d88
QY
2875 struct zmsghdr hdr;
2876 struct zebra_vrf *zvrf;
2877
aa8cb964
QY
2878 if (STREAM_READABLE(msg) > ZEBRA_MAX_PACKET_SIZ) {
2879 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2880 zlog_debug(
2881 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
2882 STREAM_READABLE(msg), ZEBRA_MAX_PACKET_SIZ);
2883 return;
2884 }
2885
904e0d88 2886 zapi_parse_header(msg, &hdr);
bf094f69 2887
49b3b01f
QY
2888 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2889 zserv_log_message(NULL, msg, &hdr);
2890
727c9b99
QY
2891#if defined(HANDLE_ZAPI_FUZZING)
2892 zserv_write_incoming(msg, hdr.command);
2893#endif
2894
904e0d88
QY
2895 hdr.length -= ZEBRA_HEADER_SIZE;
2896
2897 /* lookup vrf */
2898 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
9ab0b2a3
SW
2899 if (!zvrf)
2900 return zserv_error_no_vrf(client, &hdr, msg, zvrf);
904e0d88 2901
aa360de7 2902 if (hdr.command >= array_size(zserv_handlers)
904e0d88 2903 || zserv_handlers[hdr.command] == NULL)
9ab0b2a3
SW
2904 return zserv_error_invalid_msg_type(client, &hdr, msg, zvrf);
2905
2906 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
bf094f69 2907}