]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zapi_msg.c
lib: use specific MTYPE for interface descriptions
[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"
bf094f69
QY
27#include "lib/stream.h"
28#include "lib/memory.h"
29#include "lib/table.h"
30#include "lib/network.h"
bf094f69
QY
31#include "lib/log.h"
32#include "lib/zclient.h"
33#include "lib/privs.h"
bf094f69
QY
34#include "lib/nexthop.h"
35#include "lib/vrf.h"
36#include "lib/libfrr.h"
5e7939a5 37#include "lib/lib_errors.h"
bf094f69 38
161e9ab7 39#include "zebra/zebra_router.h"
bf094f69 40#include "zebra/rib.h"
bf094f69
QY
41#include "zebra/zebra_ns.h"
42#include "zebra/zebra_vrf.h"
43#include "zebra/router-id.h"
44#include "zebra/redistribute.h"
45#include "zebra/debug.h"
46#include "zebra/zebra_rnh.h"
bf094f69
QY
47#include "zebra/interface.h"
48#include "zebra/zebra_ptm.h"
49#include "zebra/rtadv.h"
50#include "zebra/zebra_mpls.h"
51#include "zebra/zebra_mroute.h"
bf094f69 52#include "zebra/zebra_vxlan.h"
ce5160c0 53#include "zebra/zebra_evpn_mh.h"
bf094f69
QY
54#include "zebra/rt.h"
55#include "zebra/zebra_pbr.h"
dfacea4a 56#include "zebra/zebra_tc.h"
bf094f69
QY
57#include "zebra/table_manager.h"
58#include "zebra/zapi_msg.h"
364fed6b 59#include "zebra/zebra_errors.h"
02c0866d 60#include "zebra/zebra_mlag.h"
54bea4e5 61#include "zebra/connected.h"
79b3664a 62#include "zebra/zebra_opaque.h"
31f937fb 63#include "zebra/zebra_srte.h"
6e68a084 64#include "zebra/zebra_srv6.h"
bf094f69 65
b86c1f4f 66DEFINE_MTYPE_STATIC(ZEBRA, RE_OPAQUE, "Route Opaque Data");
224ccf29 67
5898ce6f
MS
68static int zapi_nhg_decode(struct stream *s, int cmd, struct zapi_nhg *api_nhg);
69
bf094f69
QY
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
a013777a 110 memset(&data, 0, sizeof(data));
bf094f69
QY
111 data.l.table_id = zvrf->table_id;
112
113 if (netns_name)
114 strlcpy(data.l.netns_name, basename((char *)netns_name),
115 NS_NAMSIZ);
116 else
117 memset(data.l.netns_name, 0, NS_NAMSIZ);
118 /* Pass the tableid and the netns NAME */
119 stream_put(s, &data, sizeof(struct vrf_data));
120 /* Interface information. */
121 stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
122 /* Write packet size. */
123 stream_putw_at(s, 0, stream_get_endp(s));
124}
125
126static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
127{
a756969d 128 stream_putl(s, nexthop->vrf_id);
bf094f69
QY
129 stream_putc(s, nexthop->type);
130 switch (nexthop->type) {
131 case NEXTHOP_TYPE_IPV4:
132 case NEXTHOP_TYPE_IPV4_IFINDEX:
133 stream_put_in_addr(s, &nexthop->gate.ipv4);
134 stream_putl(s, nexthop->ifindex);
135 break;
136 case NEXTHOP_TYPE_IPV6:
137 stream_put(s, &nexthop->gate.ipv6, 16);
138 break;
139 case NEXTHOP_TYPE_IPV6_IFINDEX:
140 stream_put(s, &nexthop->gate.ipv6, 16);
141 stream_putl(s, nexthop->ifindex);
142 break;
143 case NEXTHOP_TYPE_IFINDEX:
144 stream_putl(s, nexthop->ifindex);
145 break;
146 default:
147 /* do nothing */
148 break;
149 }
150 return 1;
151}
152
9ab0b2a3
SW
153/*
154 * Zebra error addition adds error type.
155 *
156 *
157 * 0 1
158 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160 * | enum zebra_error_types |
161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 *
163 */
164static void zserv_encode_error(struct stream *s, enum zebra_error_types error)
165{
166 stream_put(s, &error, sizeof(error));
167
168 /* Write packet size. */
169 stream_putw_at(s, 0, stream_get_endp(s));
170}
171
bf094f69
QY
172/* Send handlers ----------------------------------------------------------- */
173
174/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
175/*
176 * This function is called in the following situations:
177 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
178 * from the client.
179 * - at startup, when zebra figures out the available interfaces
180 * - when an interface is added (where support for
181 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
182 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
183 * received)
184 */
185int zsend_interface_add(struct zserv *client, struct interface *ifp)
186{
187 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
188
096f7609 189 zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf->vrf_id);
bf094f69
QY
190 zserv_encode_interface(s, ifp);
191
192 client->ifadd_cnt++;
21ccc0cf 193 return zserv_send_message(client, s);
bf094f69
QY
194}
195
196/* Interface deletion from zebra daemon. */
197int zsend_interface_delete(struct zserv *client, struct interface *ifp)
198{
199 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
200
096f7609 201 zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf->vrf_id);
bf094f69
QY
202 zserv_encode_interface(s, ifp);
203
204 client->ifdel_cnt++;
21ccc0cf 205 return zserv_send_message(client, s);
bf094f69
QY
206}
207
208int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
209{
210 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
211
212 zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
213 zserv_encode_vrf(s, zvrf);
214
215 client->vrfadd_cnt++;
21ccc0cf 216 return zserv_send_message(client, s);
bf094f69
QY
217}
218
219/* VRF deletion from zebra daemon. */
220int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
221
222{
223 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
224
225 zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
226 zserv_encode_vrf(s, zvrf);
227
228 client->vrfdel_cnt++;
21ccc0cf 229 return zserv_send_message(client, s);
bf094f69
QY
230}
231
232int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
233{
234 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
235
096f7609 236 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf->vrf_id);
bf094f69
QY
237
238 /* Add Interface Index */
239 stream_putl(s, ifp->ifindex);
240
241 /* Then TE Link Parameters */
242 if (zebra_interface_link_params_write(s, ifp) == 0) {
243 stream_free(s);
244 return 0;
245 }
246
247 /* Write packet size. */
248 stream_putw_at(s, 0, stream_get_endp(s));
249
21ccc0cf 250 return zserv_send_message(client, s);
bf094f69
QY
251}
252
253/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
254 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 *
256 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
257 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
258 * from the client, after the ZEBRA_INTERFACE_ADD has been
259 * sent from zebra to the client
260 * - redistribute new address info to all clients in the following situations
261 * - at startup, when zebra figures out the available interfaces
262 * - when an interface is added (where support for
263 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
264 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * received)
b1bd1015 266 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
bf094f69
QY
267 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
268 * - when an RTM_NEWADDR message is received from the kernel,
269 *
270 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 *
272 * zsend_interface_address(DELETE)
273 * ^
274 * |
275 * zebra_interface_address_delete_update
276 * ^ ^ ^
277 * | | if_delete_update
278 * | |
279 * ip_address_uninstall connected_delete_ipv4
280 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
281 * ^ ^
282 * | |
283 * | RTM_NEWADDR on routing/netlink socket
284 * |
285 * vty commands:
286 * "no ip address A.B.C.D/M [label LINE]"
b1bd1015 287 * "no ip address A.B.C.D/M"
bf094f69
QY
288 * ["no ipv6 address X:X::X:X/M"]
289 *
290 */
291int zsend_interface_address(int cmd, struct zserv *client,
292 struct interface *ifp, struct connected *ifc)
293{
294 int blen;
295 struct prefix *p;
296 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
297
096f7609 298 zclient_create_header(s, cmd, ifp->vrf->vrf_id);
bf094f69
QY
299 stream_putl(s, ifp->ifindex);
300
301 /* Interface address flag. */
302 stream_putc(s, ifc->flags);
303
304 /* Prefix information. */
305 p = ifc->address;
306 stream_putc(s, p->family);
307 blen = prefix_blen(p);
308 stream_put(s, &p->u.prefix, blen);
309
310 /*
311 * XXX gnu version does not send prefixlen for
312 * ZEBRA_INTERFACE_ADDRESS_DELETE
313 * but zebra_interface_address_delete_read() in the gnu version
314 * expects to find it
315 */
316 stream_putc(s, p->prefixlen);
317
318 /* Destination. */
319 p = ifc->destination;
320 if (p)
321 stream_put(s, &p->u.prefix, blen);
322 else
323 stream_put(s, NULL, blen);
324
325 /* Write packet size. */
326 stream_putw_at(s, 0, stream_get_endp(s));
327
328 client->connected_rt_add_cnt++;
21ccc0cf 329 return zserv_send_message(client, s);
bf094f69
QY
330}
331
332static int zsend_interface_nbr_address(int cmd, struct zserv *client,
333 struct interface *ifp,
334 struct nbr_connected *ifc)
335{
336 int blen;
337 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
338 struct prefix *p;
339
096f7609 340 zclient_create_header(s, cmd, ifp->vrf->vrf_id);
bf094f69
QY
341 stream_putl(s, ifp->ifindex);
342
343 /* Prefix information. */
344 p = ifc->address;
345 stream_putc(s, p->family);
346 blen = prefix_blen(p);
347 stream_put(s, &p->u.prefix, blen);
348
349 /*
350 * XXX gnu version does not send prefixlen for
351 * ZEBRA_INTERFACE_ADDRESS_DELETE
352 * but zebra_interface_address_delete_read() in the gnu version
353 * expects to find it
354 */
355 stream_putc(s, p->prefixlen);
356
357 /* Write packet size. */
358 stream_putw_at(s, 0, stream_get_endp(s));
359
21ccc0cf 360 return zserv_send_message(client, s);
bf094f69
QY
361}
362
363/* Interface address addition. */
364static void zebra_interface_nbr_address_add_update(struct interface *ifp,
365 struct nbr_connected *ifc)
366{
367 struct listnode *node, *nnode;
368 struct zserv *client;
369 struct prefix *p;
370
371 if (IS_ZEBRA_DEBUG_EVENT) {
372 char buf[INET6_ADDRSTRLEN];
373
374 p = ifc->address;
375 zlog_debug(
376 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
377 inet_ntop(p->family, &p->u.prefix, buf,
378 INET6_ADDRSTRLEN),
379 p->prefixlen, ifc->ifp->name);
380 }
381
17da84a4
KS
382 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
383 /* Do not send unsolicited messages to synchronous clients. */
384 if (client->synchronous)
385 continue;
386
bf094f69
QY
387 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
388 client, ifp, ifc);
17da84a4 389 }
bf094f69
QY
390}
391
392/* Interface address deletion. */
393static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
394 struct nbr_connected *ifc)
395{
396 struct listnode *node, *nnode;
397 struct zserv *client;
398 struct prefix *p;
399
400 if (IS_ZEBRA_DEBUG_EVENT) {
401 char buf[INET6_ADDRSTRLEN];
402
403 p = ifc->address;
404 zlog_debug(
405 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
406 inet_ntop(p->family, &p->u.prefix, buf,
407 INET6_ADDRSTRLEN),
408 p->prefixlen, ifc->ifp->name);
409 }
410
17da84a4
KS
411 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
412 /* Do not send unsolicited messages to synchronous clients. */
413 if (client->synchronous)
414 continue;
415
bf094f69
QY
416 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
417 client, ifp, ifc);
17da84a4 418 }
bf094f69
QY
419}
420
421/* Send addresses on interface to client */
422int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
423{
424 struct listnode *cnode, *cnnode;
425 struct connected *c;
426 struct nbr_connected *nc;
427
428 /* Send interface addresses. */
429 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
430 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
431 continue;
432
433 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
434 ifp, c)
435 < 0)
436 return -1;
437 }
438
439 /* Send interface neighbors. */
440 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
441 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
442 client, ifp, nc)
443 < 0)
444 return -1;
445 }
446
447 return 0;
448}
449
450/* Notify client about interface moving from one VRF to another.
451 * Whether client is interested in old and new VRF is checked by caller.
452 */
453int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
454 vrf_id_t vrf_id)
455{
456 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
457
096f7609 458 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf->vrf_id);
bf094f69 459
91d227b7
RW
460 /* Fill in the name of the interface and its new VRF (id) */
461 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
bf094f69
QY
462 stream_putl(s, vrf_id);
463
464 /* Write packet size. */
465 stream_putw_at(s, 0, stream_get_endp(s));
466
467 client->if_vrfchg_cnt++;
21ccc0cf 468 return zserv_send_message(client, s);
bf094f69
QY
469}
470
471/* Add new nbr connected IPv6 address */
472void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
473{
474 struct nbr_connected *ifc;
475 struct prefix p;
476
477 p.family = AF_INET6;
a85297a7 478 IPV6_ADDR_COPY(&p.u.prefix6, address);
f4d81e55 479 p.prefixlen = IPV6_MAX_BITLEN;
bf094f69 480
8b1766b1
QY
481 ifc = listnode_head(ifp->nbr_connected);
482 if (!ifc) {
bf094f69
QY
483 /* new addition */
484 ifc = nbr_connected_new();
485 ifc->address = prefix_new();
486 ifc->ifp = ifp;
487 listnode_add(ifp->nbr_connected, ifc);
488 }
489
490 prefix_copy(ifc->address, &p);
491
492 zebra_interface_nbr_address_add_update(ifp, ifc);
493
494 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
495}
496
497void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
498{
499 struct nbr_connected *ifc;
500 struct prefix p;
501
502 p.family = AF_INET6;
a85297a7 503 IPV6_ADDR_COPY(&p.u.prefix6, address);
f4d81e55 504 p.prefixlen = IPV6_MAX_BITLEN;
bf094f69
QY
505
506 ifc = nbr_connected_check(ifp, &p);
507 if (!ifc)
508 return;
509
510 listnode_delete(ifp->nbr_connected, ifc);
511
512 zebra_interface_nbr_address_delete_update(ifp, ifc);
513
514 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
515
516 nbr_connected_free(ifc);
517}
518
519/*
520 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
521 * ZEBRA_INTERFACE_DOWN.
522 *
523 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
524 * the clients in one of 2 situations:
525 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
526 * - a vty command modifying the bandwidth of an interface is received.
527 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
528 */
529int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
530{
531 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
532
096f7609 533 zclient_create_header(s, cmd, ifp->vrf->vrf_id);
bf094f69
QY
534 zserv_encode_interface(s, ifp);
535
536 if (cmd == ZEBRA_INTERFACE_UP)
537 client->ifup_cnt++;
538 else
539 client->ifdown_cnt++;
540
21ccc0cf 541 return zserv_send_message(client, s);
bf094f69
QY
542}
543
86391e56 544int zsend_redistribute_route(int cmd, struct zserv *client,
84faa420 545 const struct route_node *rn,
40f321c0 546 const struct route_entry *re)
bf094f69
QY
547{
548 struct zapi_route api;
549 struct zapi_nexthop *api_nh;
550 struct nexthop *nexthop;
84faa420 551 const struct prefix *p, *src_p;
9a0d4dd3 552 uint8_t count = 0;
34fa0870 553 afi_t afi;
f3f45626
DS
554 size_t stream_size =
555 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
bf094f69 556
84faa420 557 srcdest_rnode_prefixes(rn, &p, &src_p);
bf094f69
QY
558 memset(&api, 0, sizeof(api));
559 api.vrf_id = re->vrf_id;
560 api.type = re->type;
e4081c0e 561 api.safi = SAFI_UNICAST;
bf094f69
QY
562 api.instance = re->instance;
563 api.flags = re->flags;
564
34fa0870
DS
565 afi = family2afi(p->family);
566 switch (afi) {
567 case AFI_IP:
568 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
569 client->redist_v4_add_cnt++;
570 else
571 client->redist_v4_del_cnt++;
572 break;
573 case AFI_IP6:
574 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
575 client->redist_v6_add_cnt++;
576 else
577 client->redist_v6_del_cnt++;
578 break;
579 default:
580 break;
581 }
582
bf094f69
QY
583 /* Prefix. */
584 api.prefix = *p;
585 if (src_p) {
586 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
587 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
588 }
589
c415d895 590 for (nexthop = re->nhe->nhg.nexthop;
0eb97b86 591 nexthop; nexthop = nexthop->next) {
bf094f69
QY
592 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
593 continue;
594
595 api_nh = &api.nexthops[count];
596 api_nh->vrf_id = nexthop->vrf_id;
597 api_nh->type = nexthop->type;
bd054c1a 598 api_nh->weight = nexthop->weight;
bf094f69
QY
599 switch (nexthop->type) {
600 case NEXTHOP_TYPE_BLACKHOLE:
601 api_nh->bh_type = nexthop->bh_type;
602 break;
603 case NEXTHOP_TYPE_IPV4:
bf094f69
QY
604 case NEXTHOP_TYPE_IPV4_IFINDEX:
605 api_nh->gate.ipv4 = nexthop->gate.ipv4;
606 api_nh->ifindex = nexthop->ifindex;
607 break;
608 case NEXTHOP_TYPE_IFINDEX:
609 api_nh->ifindex = nexthop->ifindex;
610 break;
611 case NEXTHOP_TYPE_IPV6:
bf094f69
QY
612 case NEXTHOP_TYPE_IPV6_IFINDEX:
613 api_nh->gate.ipv6 = nexthop->gate.ipv6;
614 api_nh->ifindex = nexthop->ifindex;
615 }
616 count++;
617 }
618
9a0d4dd3
DS
619 /* Nexthops. */
620 if (count) {
621 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
622 api.nexthop_num = count;
623 }
624
bf094f69
QY
625 /* Attributes. */
626 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
627 api.distance = re->distance;
628 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
629 api.metric = re->metric;
630 if (re->tag) {
631 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
632 api.tag = re->tag;
633 }
634 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
635 api.mtu = re->mtu;
636
f3f45626 637 struct stream *s = stream_new(stream_size);
bf094f69
QY
638
639 /* Encode route and send. */
640 if (zapi_route_encode(cmd, s, &api) < 0) {
641 stream_free(s);
642 return -1;
643 }
644
2dbe669b
DA
645 if (IS_ZEBRA_DEBUG_SEND)
646 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX",
bf094f69
QY
647 __func__, zserv_command_string(cmd),
648 zebra_route_string(client->proto),
649 zebra_route_string(api.type), api.vrf_id,
2dbe669b 650 &api.prefix);
21ccc0cf 651 return zserv_send_message(client, s);
bf094f69
QY
652}
653
654/*
655 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
656 * nexthop is not found on mrib. Returns both route metric and protocol
657 * distance.
34ee41c6
DL
658 *
659 * *XXX* this ZAPI call is slated to be removed at some point in the future
660 * since MRIB support in PIM is hopelessly broken in its interactions with NHT.
661 * The plan is to make pimd use NHT to receive URIB and MRIB in parallel and
662 * make the decision there, which will obsolete this ZAPI op.
663 * (Otherwise we would need to implement sending NHT updates for the result of
664 * this "URIB-MRIB-combined" table, but we only decide that here on the fly,
665 * so it'd be rather complex to do NHT for.)
bf094f69 666 */
34ee41c6
DL
667static int zsend_nexthop_lookup_mrib(struct zserv *client, struct ipaddr *addr,
668 struct route_entry *re,
669 struct zebra_vrf *zvrf)
bf094f69
QY
670{
671 struct stream *s;
672 unsigned long nump;
673 uint8_t num;
674 struct nexthop *nexthop;
675
676 /* Get output stream. */
677 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
678 stream_reset(s);
679
680 /* Fill in result. */
34ee41c6
DL
681 zclient_create_header(s, ZEBRA_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
682 stream_put_ipaddr(s, addr);
bf094f69
QY
683
684 if (re) {
b59839af
DS
685 struct nexthop_group *nhg;
686
bf094f69
QY
687 stream_putc(s, re->distance);
688 stream_putl(s, re->metric);
689 num = 0;
8b1766b1
QY
690 /* remember position for nexthop_num */
691 nump = stream_get_endp(s);
692 /* reserve room for nexthop_num */
693 stream_putc(s, 0);
b59839af
DS
694 nhg = rib_get_fib_nhg(re);
695 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
696 if (rnh_nexthop_valid(re, nexthop))
bf094f69 697 num += zserv_encode_nexthop(s, nexthop);
b59839af 698 }
bf094f69 699
8b1766b1
QY
700 /* store nexthop_num */
701 stream_putc_at(s, nump, num);
bf094f69
QY
702 } else {
703 stream_putc(s, 0); /* distance */
704 stream_putl(s, 0); /* metric */
705 stream_putc(s, 0); /* nexthop_num */
706 }
707
708 stream_putw_at(s, 0, stream_get_endp(s));
709
21ccc0cf 710 return zserv_send_message(client, s);
bf094f69
QY
711}
712
ee94437e
MS
713int zsend_nhg_notify(uint16_t type, uint16_t instance, uint32_t session_id,
714 uint32_t id, enum zapi_nhg_notify_owner note)
2f35a820
DS
715{
716 struct zserv *client;
717 struct stream *s;
718
ee94437e 719 client = zserv_find_client_session(type, instance, session_id);
2f35a820
DS
720 if (!client) {
721 if (IS_ZEBRA_DEBUG_PACKET) {
722 zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)",
723 type, instance, id, note);
724 }
725 return 0;
726 }
727
ee94437e 728 if (IS_ZEBRA_DEBUG_SEND)
04bec7b2
MS
729 zlog_debug("%s: type %d, id %d, note %s",
730 __func__, type, id, zapi_nhg_notify_owner2str(note));
ee94437e 731
2f35a820
DS
732 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
733 stream_reset(s);
734
735 zclient_create_header(s, ZEBRA_NHG_NOTIFY_OWNER, VRF_DEFAULT);
736
2f35a820 737 stream_put(s, &note, sizeof(note));
2c7819b9 738 stream_putl(s, id);
2f35a820
DS
739
740 stream_putw_at(s, 0, stream_get_endp(s));
741
742 return zserv_send_message(client, s);
743}
744
86391e56
MS
745/*
746 * Common utility send route notification, called from a path using a
747 * route_entry and from a path using a dataplane context.
748 */
a7704e1b 749static int route_notify_internal(const struct route_node *rn, int type,
86391e56
MS
750 uint16_t instance, vrf_id_t vrf_id,
751 uint32_t table_id,
a7704e1b
DS
752 enum zapi_route_notify_owner note, afi_t afi,
753 safi_t safi)
bf094f69
QY
754{
755 struct zserv *client;
756 struct stream *s;
757 uint8_t blen;
758
86391e56 759 client = zserv_find_client(type, instance);
bf094f69 760 if (!client || !client->notify_owner) {
2dbe669b 761 if (IS_ZEBRA_DEBUG_PACKET)
bf094f69 762 zlog_debug(
a7704e1b
DS
763 "Not Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
764 zebra_route_string(type), rn, table_id, note,
2dbe669b 765 vrf_id);
bf094f69
QY
766 return 0;
767 }
768
2dbe669b
DA
769 if (IS_ZEBRA_DEBUG_PACKET)
770 zlog_debug(
a7704e1b
DS
771 "Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u",
772 zebra_route_string(type), rn, table_id, note, vrf_id);
bf094f69 773
55e74ca9
MS
774 /* We're just allocating a small-ish buffer here, since we only
775 * encode a small amount of data.
776 */
777 s = stream_new(ZEBRA_SMALL_PACKET_SIZE);
778
bf094f69
QY
779 stream_reset(s);
780
86391e56 781 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
bf094f69
QY
782
783 stream_put(s, &note, sizeof(note));
784
a7704e1b 785 stream_putc(s, rn->p.family);
bf094f69 786
a7704e1b
DS
787 blen = prefix_blen(&rn->p);
788 stream_putc(s, rn->p.prefixlen);
789 stream_put(s, &rn->p.u.prefix, blen);
bf094f69 790
86391e56 791 stream_putl(s, table_id);
bf094f69 792
77b38a4a
S
793 /* Encode AFI, SAFI in the message */
794 stream_putc(s, afi);
795 stream_putc(s, safi);
796
bf094f69
QY
797 stream_putw_at(s, 0, stream_get_endp(s));
798
21ccc0cf 799 return zserv_send_message(client, s);
bf094f69
QY
800}
801
a7704e1b
DS
802int zsend_route_notify_owner(const struct route_node *rn,
803 struct route_entry *re,
804 enum zapi_route_notify_owner note, afi_t afi,
805 safi_t safi)
86391e56 806{
a7704e1b 807 return (route_notify_internal(rn, re->type, re->instance, re->vrf_id,
77b38a4a 808 re->table, note, afi, safi));
86391e56
MS
809}
810
7cdb1a84
MS
811/*
812 * Route-owner notification using info from dataplane update context.
813 */
25779064 814int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
7cdb1a84
MS
815 enum zapi_route_notify_owner note)
816{
a7704e1b
DS
817 return (route_notify_internal(
818 rib_find_rn_from_ctx(ctx), dplane_ctx_get_type(ctx),
819 dplane_ctx_get_instance(ctx), dplane_ctx_get_vrf(ctx),
820 dplane_ctx_get_table(ctx), note, dplane_ctx_get_afi(ctx),
821 dplane_ctx_get_safi(ctx)));
77b38a4a
S
822}
823
824static void zread_route_notify_request(ZAPI_HANDLER_ARGS)
825{
826 uint8_t notify;
827
828 STREAM_GETC(msg, notify);
829 client->notify_owner = notify;
830stream_failure:
831 return;
86391e56
MS
832}
833
f62e5480 834void zsend_rule_notify_owner(const struct zebra_dplane_ctx *ctx,
bf094f69
QY
835 enum zapi_rule_notify_owner note)
836{
837 struct listnode *node;
838 struct zserv *client;
839 struct stream *s;
840
841 if (IS_ZEBRA_DEBUG_PACKET)
f62e5480
JU
842 zlog_debug("%s: Notifying %u", __func__,
843 dplane_ctx_rule_get_unique(ctx));
bf094f69 844
161e9ab7 845 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
f62e5480 846 if (dplane_ctx_rule_get_sock(ctx) == client->sock)
bf094f69
QY
847 break;
848 }
849
850 if (!client)
851 return;
852
853 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
854
855 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
856 stream_put(s, &note, sizeof(note));
f62e5480
JU
857 stream_putl(s, dplane_ctx_rule_get_seq(ctx));
858 stream_putl(s, dplane_ctx_rule_get_priority(ctx));
859 stream_putl(s, dplane_ctx_rule_get_unique(ctx));
58a1d249 860 stream_put(s, dplane_ctx_rule_get_ifname(ctx), INTERFACE_NAMSIZ);
bf094f69
QY
861
862 stream_putw_at(s, 0, stream_get_endp(s));
863
21ccc0cf 864 zserv_send_message(client, s);
bf094f69
QY
865}
866
ef524230 867void zsend_iptable_notify_owner(const struct zebra_dplane_ctx *ctx,
33c08518 868 enum zapi_iptable_notify_owner note)
bf094f69
QY
869{
870 struct listnode *node;
871 struct zserv *client;
872 struct stream *s;
ef524230
PG
873 struct zebra_pbr_iptable ipt;
874 uint16_t cmd = ZEBRA_IPTABLE_NOTIFY_OWNER;
c9250e28
PG
875 struct zebra_pbr_iptable *ipt_hash;
876 enum dplane_op_e op = dplane_ctx_get_op(ctx);
ef524230 877
8d78e148 878 dplane_ctx_get_pbr_iptable(ctx, &ipt);
bf094f69 879
c9250e28
PG
880 ipt_hash = hash_lookup(zrouter.iptable_hash, &ipt);
881 if (ipt_hash) {
882 if (op == DPLANE_OP_IPTABLE_ADD &&
883 CHECK_FLAG(ipt_hash->internal_flags,
884 IPTABLE_INSTALL_QUEUED))
885 UNSET_FLAG(ipt_hash->internal_flags,
886 IPTABLE_INSTALL_QUEUED);
887 else if (op == DPLANE_OP_IPTABLE_DELETE &&
888 CHECK_FLAG(ipt_hash->internal_flags,
889 IPTABLE_UNINSTALL_QUEUED))
890 UNSET_FLAG(ipt_hash->internal_flags,
891 IPTABLE_UNINSTALL_QUEUED);
892 }
bf094f69 893 if (IS_ZEBRA_DEBUG_PACKET)
ef524230
PG
894 zlog_debug("%s: Notifying %s id %u note %u", __func__,
895 zserv_command_string(cmd), ipt.unique, note);
bf094f69 896
161e9ab7 897 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
ef524230 898 if (ipt.sock == client->sock)
bf094f69
QY
899 break;
900 }
901
902 if (!client)
903 return;
904
905 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
906
5162e000 907 zclient_create_header(s, cmd, VRF_DEFAULT);
e108096b 908 stream_putw(s, note);
ef524230
PG
909 stream_putl(s, ipt.unique);
910 stream_put(s, ipt.ipset_name, ZEBRA_IPSET_NAME_SIZE);
bf094f69
QY
911 stream_putw_at(s, 0, stream_get_endp(s));
912
21ccc0cf 913 zserv_send_message(client, s);
bf094f69
QY
914}
915
33c08518
DS
916void zsend_ipset_notify_owner(const struct zebra_dplane_ctx *ctx,
917 enum zapi_ipset_notify_owner note)
bf094f69
QY
918{
919 struct listnode *node;
920 struct zserv *client;
921 struct stream *s;
ef524230
PG
922 struct zebra_pbr_ipset ipset;
923 uint16_t cmd = ZEBRA_IPSET_NOTIFY_OWNER;
924
8249f96a 925 dplane_ctx_get_pbr_ipset(ctx, &ipset);
bf094f69
QY
926
927 if (IS_ZEBRA_DEBUG_PACKET)
ef524230
PG
928 zlog_debug("%s: Notifying %s id %u note %u", __func__,
929 zserv_command_string(cmd), ipset.unique, note);
bf094f69 930
161e9ab7 931 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
ef524230 932 if (ipset.sock == client->sock)
bf094f69
QY
933 break;
934 }
935
936 if (!client)
937 return;
938
939 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
940
ef524230 941 zclient_create_header(s, cmd, VRF_DEFAULT);
e108096b 942 stream_putw(s, note);
ef524230
PG
943 stream_putl(s, ipset.unique);
944 stream_put(s, ipset.ipset_name, ZEBRA_IPSET_NAME_SIZE);
bf094f69
QY
945 stream_putw_at(s, 0, stream_get_endp(s));
946
21ccc0cf 947 zserv_send_message(client, s);
bf094f69
QY
948}
949
ef524230 950void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx *ctx,
33c08518 951 enum zapi_ipset_entry_notify_owner note)
bf094f69
QY
952{
953 struct listnode *node;
954 struct zserv *client;
955 struct stream *s;
ef524230
PG
956 struct zebra_pbr_ipset_entry ipent;
957 struct zebra_pbr_ipset ipset;
958 uint16_t cmd = ZEBRA_IPSET_ENTRY_NOTIFY_OWNER;
5162e000 959
f284c132 960 dplane_ctx_get_pbr_ipset_entry(ctx, &ipent);
8249f96a 961 dplane_ctx_get_pbr_ipset(ctx, &ipset);
bf094f69
QY
962
963 if (IS_ZEBRA_DEBUG_PACKET)
5162e000 964 zlog_debug("%s: Notifying %s id %u note %u", __func__,
ef524230 965 zserv_command_string(cmd), ipent.unique, note);
bf094f69 966
161e9ab7 967 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
ef524230 968 if (ipent.sock == client->sock)
bf094f69
QY
969 break;
970 }
971
972 if (!client)
973 return;
974
975 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
976
5162e000 977 zclient_create_header(s, cmd, VRF_DEFAULT);
e108096b 978 stream_putw(s, note);
ef524230
PG
979 stream_putl(s, ipent.unique);
980 stream_put(s, ipset.ipset_name, ZEBRA_IPSET_NAME_SIZE);
bf094f69
QY
981 stream_putw_at(s, 0, stream_get_endp(s));
982
21ccc0cf 983 zserv_send_message(client, s);
bf094f69
QY
984}
985
7723e8d3
PG
986void zsend_nhrp_neighbor_notify(int cmd, struct interface *ifp,
987 struct ipaddr *ipaddr, int ndm_state,
d603c077 988 union sockunion *link_layer_ipv4)
7723e8d3
PG
989{
990 struct stream *s;
991 struct listnode *node, *nnode;
992 struct zserv *client;
993 afi_t afi;
d603c077 994 union sockunion ip;
7723e8d3
PG
995
996 if (IS_ZEBRA_DEBUG_PACKET)
7d7be47e 997 zlog_debug("%s: Notifying Neighbor entry (%u)", __func__, cmd);
7723e8d3 998
d603c077
PG
999 sockunion_family(&ip) = ipaddr_family(ipaddr);
1000 afi = family2afi(sockunion_family(&ip));
1001 memcpy((char *)sockunion_get_addr(&ip), &ipaddr->ip.addr,
1002 family2addrsize(sockunion_family(&ip)));
1003
7723e8d3 1004 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
096f7609
IR
1005 if (!vrf_bitmap_check(client->nhrp_neighinfo[afi],
1006 ifp->vrf->vrf_id))
7723e8d3
PG
1007 continue;
1008
1009 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
c4e1fd52
PG
1010 zclient_neigh_ip_encode(s, cmd, &ip, link_layer_ipv4, ifp,
1011 ndm_state);
7723e8d3 1012 stream_putw_at(s, 0, stream_get_endp(s));
7723e8d3
PG
1013 zserv_send_message(client, s);
1014 }
1015}
1016
1017
98a3fb0a
SM
1018/* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
1019int zsend_router_id_update(struct zserv *client, afi_t afi, struct prefix *p,
bf094f69
QY
1020 vrf_id_t vrf_id)
1021{
1022 int blen;
98a3fb0a 1023 struct stream *s;
bf094f69
QY
1024
1025 /* Check this client need interface information. */
98a3fb0a 1026 if (!vrf_bitmap_check(client->ridinfo[afi], vrf_id))
bf094f69
QY
1027 return 0;
1028
98a3fb0a 1029 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
bf094f69
QY
1030
1031 /* Message type. */
1032 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
1033
1034 /* Prefix information. */
1035 stream_putc(s, p->family);
1036 blen = prefix_blen(p);
1037 stream_put(s, &p->u.prefix, blen);
1038 stream_putc(s, p->prefixlen);
1039
1040 /* Write packet size. */
1041 stream_putw_at(s, 0, stream_get_endp(s));
1042
21ccc0cf 1043 return zserv_send_message(client, s);
bf094f69
QY
1044}
1045
1046/*
1047 * Function used by Zebra to send a PW status update to LDP daemon
1048 */
1049int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
1050{
1051 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1052
1053 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
3682bd90 1054 stream_write(s, pw->ifname, INTERFACE_NAMSIZ);
bf094f69
QY
1055 stream_putl(s, pw->ifindex);
1056 stream_putl(s, pw->status);
1057
1058 /* Put length at the first point of the stream. */
1059 stream_putw_at(s, 0, stream_get_endp(s));
1060
21ccc0cf 1061 return zserv_send_message(client, s);
bf094f69
QY
1062}
1063
1064/* Send response to a get label chunk request to client */
e11d7c96 1065int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id,
e11d7c96 1066 struct label_manager_chunk *lmc)
bf094f69 1067{
bf094f69
QY
1068 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1069
1070 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
f004f7c3 1071 /* proto */
4cebdb9b 1072 stream_putc(s, client->proto);
f004f7c3 1073 /* instance */
4cebdb9b 1074 stream_putw(s, client->instance);
bf094f69
QY
1075
1076 if (lmc) {
1077 /* keep */
1078 stream_putc(s, lmc->keep);
1079 /* start and end labels */
1080 stream_putl(s, lmc->start);
1081 stream_putl(s, lmc->end);
1082 }
1083
1084 /* Write packet size. */
1085 stream_putw_at(s, 0, stream_get_endp(s));
1086
732d22cb 1087 return zserv_send_message(client, s);
bf094f69
QY
1088}
1089
1090/* Send response to a label manager connect request to client */
e11d7c96
EDP
1091int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id,
1092 unsigned short result)
bf094f69 1093{
bf094f69
QY
1094 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1095
1096 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
1097
5dffb0e9
FR
1098 /* proto */
1099 stream_putc(s, client->proto);
1100
1101 /* instance */
1102 stream_putw(s, client->instance);
1103
bf094f69
QY
1104 /* result */
1105 stream_putc(s, result);
1106
1107 /* Write packet size. */
1108 stream_putw_at(s, 0, stream_get_endp(s));
1109
732d22cb 1110 return zserv_send_message(client, s);
bf094f69
QY
1111}
1112
1113/* Send response to a get table chunk request to client */
1114static int zsend_assign_table_chunk_response(struct zserv *client,
1115 vrf_id_t vrf_id,
1116 struct table_manager_chunk *tmc)
1117{
1118 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1119
1120 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
1121
1122 if (tmc) {
1123 /* start and end labels */
1124 stream_putl(s, tmc->start);
1125 stream_putl(s, tmc->end);
1126 }
1127
1128 /* Write packet size. */
1129 stream_putw_at(s, 0, stream_get_endp(s));
1130
21ccc0cf 1131 return zserv_send_message(client, s);
bf094f69
QY
1132}
1133
1134static int zsend_table_manager_connect_response(struct zserv *client,
1135 vrf_id_t vrf_id,
1136 uint16_t result)
1137{
1138 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1139
1140 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
1141
1142 /* result */
1143 stream_putc(s, result);
1144
1145 stream_putw_at(s, 0, stream_get_endp(s));
1146
21ccc0cf 1147 return zserv_send_message(client, s);
bf094f69
QY
1148}
1149
f5ca329b
HS
1150/* SRv6 locator add notification from zebra daemon. */
1151int zsend_zebra_srv6_locator_add(struct zserv *client, struct srv6_locator *loc)
1152{
1153 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1154
1155 zclient_create_header(s, ZEBRA_SRV6_LOCATOR_ADD, VRF_DEFAULT);
1156 zapi_srv6_locator_encode(s, loc);
1157 stream_putw_at(s, 0, stream_get_endp(s));
1158
1159 return zserv_send_message(client, s);
1160}
1161
1162/* SRv6 locator delete notification from zebra daemon. */
1163int zsend_zebra_srv6_locator_delete(struct zserv *client,
1164 struct srv6_locator *loc)
1165{
1166 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1167
1168 zclient_create_header(s, ZEBRA_SRV6_LOCATOR_DELETE, VRF_DEFAULT);
1169 zapi_srv6_locator_encode(s, loc);
1170 stream_putw_at(s, 0, stream_get_endp(s));
1171
1172 return zserv_send_message(client, s);
1173}
1174
bf094f69
QY
1175/* Inbound message handling ------------------------------------------------ */
1176
bf094f69
QY
1177/* Nexthop register */
1178static void zread_rnh_register(ZAPI_HANDLER_ARGS)
1179{
1180 struct rnh *rnh;
1181 struct stream *s;
1182 struct prefix p;
1183 unsigned short l = 0;
6cd9a93d 1184 uint8_t connected = 0;
ed6cec97 1185 uint8_t resolve_via_default;
1d30d1f4 1186 bool exist;
906b54dd
DS
1187 bool flag_changed = false;
1188 uint8_t orig_flags;
027db469 1189 safi_t safi;
bf094f69
QY
1190
1191 if (IS_ZEBRA_DEBUG_NHT)
1192 zlog_debug(
6071e5e9 1193 "rnh_register msg from client %s: hdr->length=%d vrf=%u",
bf094f69 1194 zebra_route_string(client->proto), hdr->length,
bf094f69
QY
1195 zvrf->vrf->vrf_id);
1196
1197 s = msg;
1198
469d6277
DS
1199 if (!client->nh_reg_time)
1200 client->nh_reg_time = monotime(NULL);
bf094f69
QY
1201
1202 while (l < hdr->length) {
6cd9a93d 1203 STREAM_GETC(s, connected);
ed6cec97 1204 STREAM_GETC(s, resolve_via_default);
027db469 1205 STREAM_GETW(s, safi);
bf094f69
QY
1206 STREAM_GETW(s, p.family);
1207 STREAM_GETC(s, p.prefixlen);
027db469 1208 l += 7;
bf094f69 1209 if (p.family == AF_INET) {
ab5990d8 1210 client->v4_nh_watch_add_cnt++;
bf094f69 1211 if (p.prefixlen > IPV4_MAX_BITLEN) {
9df414fe 1212 zlog_debug(
bf094f69 1213 "%s: Specified prefix hdr->length %d is too large for a v4 address",
15569c58 1214 __func__, p.prefixlen);
bf094f69
QY
1215 return;
1216 }
1217 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1218 l += IPV4_MAX_BYTELEN;
1219 } else if (p.family == AF_INET6) {
ab5990d8 1220 client->v6_nh_watch_add_cnt++;
bf094f69 1221 if (p.prefixlen > IPV6_MAX_BITLEN) {
9df414fe 1222 zlog_debug(
bf094f69 1223 "%s: Specified prefix hdr->length %d is to large for a v6 address",
15569c58 1224 __func__, p.prefixlen);
bf094f69
QY
1225 return;
1226 }
1227 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1228 l += IPV6_MAX_BYTELEN;
1229 } else {
af4c2728 1230 flog_err(
e914ccbe 1231 EC_ZEBRA_UNKNOWN_FAMILY,
1d5453d6 1232 "rnh_register: Received unknown family type %d",
bf094f69
QY
1233 p.family);
1234 return;
1235 }
e9ac2861 1236 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), safi, &exist);
1d30d1f4
DS
1237 if (!rnh)
1238 return;
1239
906b54dd 1240 orig_flags = rnh->flags;
6cd9a93d 1241 if (connected && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
6071e5e9 1242 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
6cd9a93d
DS
1243 else if (!connected
1244 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
6071e5e9 1245 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
bf094f69 1246
ed6cec97
DS
1247 if (resolve_via_default)
1248 SET_FLAG(rnh->flags, ZEBRA_NHT_RESOLVE_VIA_DEFAULT);
bf094f69 1249
906b54dd
DS
1250 if (orig_flags != rnh->flags)
1251 flag_changed = true;
1252
bf094f69 1253 /* Anything not AF_INET/INET6 has been filtered out above */
906b54dd 1254 if (!exist || flag_changed)
d597533a 1255 zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, &p,
027db469 1256 safi);
dd25a6b3 1257
6071e5e9 1258 zebra_add_rnh_client(rnh, client, zvrf_id(zvrf));
bf094f69
QY
1259 }
1260
1261stream_failure:
1262 return;
1263}
1264
1265/* Nexthop register */
1266static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1267{
1268 struct rnh *rnh;
1269 struct stream *s;
1270 struct prefix p;
1271 unsigned short l = 0;
027db469 1272 safi_t safi;
bf094f69
QY
1273
1274 if (IS_ZEBRA_DEBUG_NHT)
1275 zlog_debug(
63efca0e 1276 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
bf094f69
QY
1277 zebra_route_string(client->proto), hdr->length,
1278 zvrf->vrf->vrf_id);
1279
1280 s = msg;
1281
1282 while (l < hdr->length) {
6cd9a93d 1283 uint8_t ignore;
bf094f69 1284
6cd9a93d
DS
1285 STREAM_GETC(s, ignore);
1286 if (ignore != 0)
ed6cec97 1287 goto stream_failure;
6cd9a93d
DS
1288 STREAM_GETC(s, ignore);
1289 if (ignore != 0)
bf094f69
QY
1290 goto stream_failure;
1291
027db469 1292 STREAM_GETW(s, safi);
bf094f69
QY
1293 STREAM_GETW(s, p.family);
1294 STREAM_GETC(s, p.prefixlen);
027db469 1295 l += 7;
bf094f69 1296 if (p.family == AF_INET) {
ab5990d8 1297 client->v4_nh_watch_rem_cnt++;
bf094f69 1298 if (p.prefixlen > IPV4_MAX_BITLEN) {
9df414fe 1299 zlog_debug(
bf094f69 1300 "%s: Specified prefix hdr->length %d is to large for a v4 address",
15569c58 1301 __func__, p.prefixlen);
bf094f69
QY
1302 return;
1303 }
1304 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1305 l += IPV4_MAX_BYTELEN;
1306 } else if (p.family == AF_INET6) {
ab5990d8 1307 client->v6_nh_watch_rem_cnt++;
bf094f69 1308 if (p.prefixlen > IPV6_MAX_BITLEN) {
9df414fe 1309 zlog_debug(
bf094f69 1310 "%s: Specified prefix hdr->length %d is to large for a v6 address",
15569c58 1311 __func__, p.prefixlen);
bf094f69
QY
1312 return;
1313 }
1314 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1315 l += IPV6_MAX_BYTELEN;
1316 } else {
af4c2728 1317 flog_err(
e914ccbe 1318 EC_ZEBRA_UNKNOWN_FAMILY,
1d5453d6 1319 "rnh_register: Received unknown family type %d",
bf094f69
QY
1320 p.family);
1321 return;
1322 }
027db469 1323 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), safi);
bf094f69
QY
1324 if (rnh) {
1325 client->nh_dereg_time = monotime(NULL);
6071e5e9 1326 zebra_remove_rnh_client(rnh, client);
bf094f69
QY
1327 }
1328 }
1329stream_failure:
1330 return;
1331}
1332
1333#define ZEBRA_MIN_FEC_LENGTH 5
1334
1335/* FEC register */
1336static void zread_fec_register(ZAPI_HANDLER_ARGS)
1337{
1338 struct stream *s;
1339 unsigned short l = 0;
1340 struct prefix p;
1341 uint16_t flags;
57592a53 1342 uint32_t label = MPLS_INVALID_LABEL;
bf094f69
QY
1343 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1344
1345 s = msg;
1346 zvrf = vrf_info_lookup(VRF_DEFAULT);
1347 if (!zvrf)
8b1766b1 1348 return;
bf094f69
QY
1349
1350 /*
1351 * The minimum amount of data that can be sent for one fec
1352 * registration
1353 */
1354 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
af4c2728 1355 flog_err(
e914ccbe 1356 EC_ZEBRA_IRDP_LEN_MISMATCH,
bf094f69
QY
1357 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1358 hdr->length);
1359 return;
1360 }
1361
1362 while (l < hdr->length) {
1363 STREAM_GETW(s, flags);
1364 memset(&p, 0, sizeof(p));
1365 STREAM_GETW(s, p.family);
1366 if (p.family != AF_INET && p.family != AF_INET6) {
af4c2728 1367 flog_err(
e914ccbe 1368 EC_ZEBRA_UNKNOWN_FAMILY,
1d5453d6 1369 "fec_register: Received unknown family type %d",
bf094f69
QY
1370 p.family);
1371 return;
1372 }
1373 STREAM_GETC(s, p.prefixlen);
1374 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1375 || (p.family == AF_INET6
1376 && p.prefixlen > IPV6_MAX_BITLEN)) {
9df414fe 1377 zlog_debug(
bf094f69 1378 "%s: Specified prefix hdr->length: %d is to long for %d",
15569c58 1379 __func__, p.prefixlen, p.family);
bf094f69
QY
1380 return;
1381 }
1382 l += 5;
1383 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1384 l += PSIZE(p.prefixlen);
57592a53
AD
1385 if (flags & ZEBRA_FEC_REGISTER_LABEL) {
1386 STREAM_GETL(s, label);
1387 l += 4;
1388 } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
bf094f69
QY
1389 STREAM_GETL(s, label_index);
1390 l += 4;
57592a53
AD
1391 }
1392
1393 zebra_mpls_fec_register(zvrf, &p, label, label_index, client);
bf094f69
QY
1394 }
1395
1396stream_failure:
1397 return;
1398}
1399
1400/* FEC unregister */
1401static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1402{
1403 struct stream *s;
1404 unsigned short l = 0;
1405 struct prefix p;
1406 uint16_t flags;
1407
1408 s = msg;
1409 zvrf = vrf_info_lookup(VRF_DEFAULT);
1410 if (!zvrf)
8b1766b1 1411 return;
bf094f69
QY
1412
1413 /*
1414 * The minimum amount of data that can be sent for one
1415 * fec unregistration
1416 */
1417 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
af4c2728 1418 flog_err(
e914ccbe 1419 EC_ZEBRA_IRDP_LEN_MISMATCH,
bf094f69
QY
1420 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1421 hdr->length);
1422 return;
1423 }
1424
1425 while (l < hdr->length) {
1426 STREAM_GETW(s, flags);
1427 if (flags != 0)
1428 goto stream_failure;
1429
1430 memset(&p, 0, sizeof(p));
1431 STREAM_GETW(s, p.family);
1432 if (p.family != AF_INET && p.family != AF_INET6) {
af4c2728 1433 flog_err(
e914ccbe 1434 EC_ZEBRA_UNKNOWN_FAMILY,
1d5453d6 1435 "fec_unregister: Received unknown family type %d",
bf094f69
QY
1436 p.family);
1437 return;
1438 }
1439 STREAM_GETC(s, p.prefixlen);
1440 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1441 || (p.family == AF_INET6
1442 && p.prefixlen > IPV6_MAX_BITLEN)) {
9df414fe 1443 zlog_debug(
bf094f69 1444 "%s: Received prefix hdr->length %d which is greater than %d can support",
15569c58 1445 __func__, p.prefixlen, p.family);
bf094f69
QY
1446 return;
1447 }
1448 l += 5;
1449 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1450 l += PSIZE(p.prefixlen);
1451 zebra_mpls_fec_unregister(zvrf, &p, client);
1452 }
1453
1454stream_failure:
1455 return;
1456}
1457
1458
1459/*
1460 * Register zebra server interface information.
1461 * Send current all interface and address information.
1462 */
1463static void zread_interface_add(ZAPI_HANDLER_ARGS)
1464{
1465 struct vrf *vrf;
1466 struct interface *ifp;
1467
9212d1e1 1468 vrf_id_t vrf_id = zvrf_id(zvrf);
1469 if (vrf_id != VRF_DEFAULT && vrf_id != VRF_UNKNOWN) {
1470 FOR_ALL_INTERFACES (zvrf->vrf, ifp) {
1471 /* Skip pseudo interface. */
1472 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1473 continue;
1474
1475 zsend_interface_add(client, ifp);
1476 zsend_interface_link_params(client, ifp);
1477 zsend_interface_addresses(client, ifp);
1478 }
1479 return;
1480 }
1481
bf094f69
QY
1482 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1483 FOR_ALL_INTERFACES (vrf, ifp) {
1484 /* Skip pseudo interface. */
1485 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1486 continue;
1487
1488 zsend_interface_add(client, ifp);
27ecbea3 1489 zsend_interface_link_params(client, ifp);
bf094f69
QY
1490 zsend_interface_addresses(client, ifp);
1491 }
1492 }
1493}
1494
1495/* Unregister zebra server interface information. */
1496static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1497{
bf094f69
QY
1498}
1499
c3bd894e
QY
1500/*
1501 * Handle message requesting interface be set up or down.
1502 */
1503static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS)
1504{
1505 ifindex_t ifindex;
1506 struct interface *ifp;
1507 char down;
5d414138 1508 enum protodown_reasons reason;
c3bd894e
QY
1509
1510 STREAM_GETL(msg, ifindex);
1511 STREAM_GETC(msg, down);
1512
1513 /* set ifdown */
1514 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex);
c3bd894e 1515
5d414138 1516 if (!ifp) {
65dc7dd3
QY
1517 zlog_warn(
1518 "Cannot set protodown %s for interface %u; does not exist",
1519 down ? "on" : "off", ifindex);
5d414138
SW
1520
1521 return;
1522 }
1523
1524 switch (client->proto) {
1525 case ZEBRA_ROUTE_VRRP:
1526 reason = ZEBRA_PROTODOWN_VRRP;
1527 break;
1528 case ZEBRA_ROUTE_SHARP:
1529 reason = ZEBRA_PROTODOWN_SHARP;
1530 break;
1531 default:
1532 reason = 0;
1533 break;
65dc7dd3
QY
1534 }
1535
5d414138 1536 zebra_if_set_protodown(ifp, down, reason);
c3bd894e
QY
1537
1538stream_failure:
1539 return;
1540}
1541
786a9bd9 1542bool zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
bf094f69
QY
1543 const unsigned int nexthop_num)
1544{
b3f2b590 1545 if (nexthop_num > zrouter.multipath_num) {
bf094f69 1546 char buff[PREFIX2STR_BUFFER];
8b1766b1 1547
e270f004
SW
1548 if (p)
1549 prefix2str(p, buff, sizeof(buff));
1550
9df414fe 1551 flog_warn(
e914ccbe 1552 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
bf094f69 1553 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
e270f004
SW
1554 caller, (p ? buff : "(NULL)"), nexthop_num,
1555 zrouter.multipath_num);
786a9bd9 1556 return true;
bf094f69 1557 }
786a9bd9
DS
1558
1559 return false;
bf094f69
QY
1560}
1561
62e46303
MS
1562/*
1563 * Create a new nexthop based on a zapi nexthop.
1564 */
2f35a820
DS
1565static struct nexthop *nexthop_from_zapi(const struct zapi_nexthop *api_nh,
1566 uint32_t flags, struct prefix *p,
1567 uint16_t backup_nexthop_num)
62e46303
MS
1568{
1569 struct nexthop *nexthop = NULL;
1570 struct ipaddr vtep_ip;
1571 struct interface *ifp;
474aebd9 1572 int i;
62e46303
MS
1573 char nhbuf[INET6_ADDRSTRLEN] = "";
1574
62e46303
MS
1575 switch (api_nh->type) {
1576 case NEXTHOP_TYPE_IFINDEX:
1577 nexthop = nexthop_from_ifindex(api_nh->ifindex, api_nh->vrf_id);
1578 break;
1579 case NEXTHOP_TYPE_IPV4:
1580 if (IS_ZEBRA_DEBUG_RECV) {
1581 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1582 sizeof(nhbuf));
1583 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1584 nhbuf, api_nh->vrf_id);
1585 }
1586 nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4, NULL,
1587 api_nh->vrf_id);
1588 break;
1589 case NEXTHOP_TYPE_IPV4_IFINDEX:
1590 if (IS_ZEBRA_DEBUG_RECV) {
1591 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1592 sizeof(nhbuf));
1593 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1594 __func__, nhbuf, api_nh->vrf_id,
1595 api_nh->ifindex);
1596 }
1597
1598 nexthop = nexthop_from_ipv4_ifindex(
1599 &api_nh->gate.ipv4, NULL, api_nh->ifindex,
1600 api_nh->vrf_id);
1601
62e46303
MS
1602 /* Special handling for IPv4 routes sourced from EVPN:
1603 * the nexthop and associated MAC need to be installed.
1604 */
5609e70f 1605 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN)) {
6006b807 1606 memset(&vtep_ip, 0, sizeof(vtep_ip));
62e46303
MS
1607 vtep_ip.ipa_type = IPADDR_V4;
1608 memcpy(&(vtep_ip.ipaddr_v4), &(api_nh->gate.ipv4),
1609 sizeof(struct in_addr));
8244ba34 1610 zebra_rib_queue_evpn_route_add(
ff9aca4f 1611 api_nh->vrf_id, &api_nh->rmac, &vtep_ip, p);
5609e70f 1612 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN);
62e46303
MS
1613 }
1614 break;
1615 case NEXTHOP_TYPE_IPV6:
1616 if (IS_ZEBRA_DEBUG_RECV) {
1617 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1618 sizeof(nhbuf));
1619 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1620 nhbuf, api_nh->vrf_id);
1621 }
1622 nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6, api_nh->vrf_id);
1623 break;
1624 case NEXTHOP_TYPE_IPV6_IFINDEX:
1625 if (IS_ZEBRA_DEBUG_RECV) {
1626 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1627 sizeof(nhbuf));
1628 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1629 __func__, nhbuf, api_nh->vrf_id,
1630 api_nh->ifindex);
1631 }
1632 nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6,
1633 api_nh->ifindex,
1634 api_nh->vrf_id);
1635
1636 /* Special handling for IPv6 routes sourced from EVPN:
1637 * the nexthop and associated MAC need to be installed.
1638 */
5609e70f 1639 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN)) {
6006b807 1640 memset(&vtep_ip, 0, sizeof(vtep_ip));
62e46303
MS
1641 vtep_ip.ipa_type = IPADDR_V6;
1642 memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6),
1643 sizeof(struct in6_addr));
8244ba34 1644 zebra_rib_queue_evpn_route_add(
ff9aca4f 1645 api_nh->vrf_id, &api_nh->rmac, &vtep_ip, p);
5609e70f 1646 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN);
62e46303
MS
1647 }
1648 break;
1649 case NEXTHOP_TYPE_BLACKHOLE:
1650 if (IS_ZEBRA_DEBUG_RECV)
1651 zlog_debug("%s: nh blackhole %d",
1652 __func__, api_nh->bh_type);
1653
0789eb69
KM
1654 nexthop =
1655 nexthop_from_blackhole(api_nh->bh_type, api_nh->vrf_id);
62e46303
MS
1656 break;
1657 }
1658
1659 /* Return early if we couldn't process the zapi nexthop */
1660 if (nexthop == NULL) {
1661 goto done;
1662 }
1663
12b4d77b 1664 /* Mark nexthop as onlink either if client has explicitly told us
1665 * to or if the nexthop is on an 'unnumbered' interface.
1666 */
62e46303
MS
1667 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK))
1668 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
12b4d77b 1669 else if (api_nh->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1670 ifp = if_lookup_by_index(api_nh->ifindex, api_nh->vrf_id);
1671 if (ifp && connected_is_unnumbered(ifp))
1672 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1673 }
62e46303
MS
1674
1675 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT))
1676 nexthop->weight = api_nh->weight;
1677
1d48702e 1678 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
474aebd9
MS
1679 /* Validate count */
1680 if (api_nh->backup_num > NEXTHOP_MAX_BACKUPS) {
1d48702e 1681 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
474aebd9
MS
1682 zlog_debug("%s: invalid backup nh count %d",
1683 __func__, api_nh->backup_num);
1684 nexthop_free(nexthop);
1685 nexthop = NULL;
1686 goto done;
1687 }
1688
1689 /* Copy backup info */
1690 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1691 nexthop->backup_num = api_nh->backup_num;
1692
1693 for (i = 0; i < api_nh->backup_num; i++) {
1694 /* Validate backup index */
2f35a820 1695 if (api_nh->backup_idx[i] < backup_nexthop_num) {
474aebd9
MS
1696 nexthop->backup_idx[i] = api_nh->backup_idx[i];
1697 } else {
1698 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1699 zlog_debug("%s: invalid backup nh idx %d",
1700 __func__,
1701 api_nh->backup_idx[i]);
1702 nexthop_free(nexthop);
1703 nexthop = NULL;
1704 goto done;
1705 }
1d48702e
MS
1706 }
1707 }
474aebd9 1708
62e46303
MS
1709done:
1710 return nexthop;
1711}
1712
2f35a820
DS
1713static bool zapi_read_nexthops(struct zserv *client, struct prefix *p,
1714 struct zapi_nexthop *nhops, uint32_t flags,
1715 uint32_t message, uint16_t nexthop_num,
1716 uint16_t backup_nh_num,
f70da2a3
DS
1717 struct nexthop_group **png,
1718 struct nhg_backup_info **pbnhg)
bf094f69 1719{
0eb97b86 1720 struct nexthop_group *ng = NULL;
1d48702e 1721 struct nhg_backup_info *bnhg = NULL;
2f35a820 1722 uint16_t i;
f70da2a3 1723 struct nexthop *last_nh = NULL;
bf094f69 1724
f70da2a3 1725 assert(!(png && pbnhg));
c2c02b76 1726
2f35a820 1727 if (png)
66c28560 1728 ng = nexthop_group_new();
bf094f69 1729
2f35a820 1730 if (pbnhg && backup_nh_num > 0) {
f70da2a3 1731 if (IS_ZEBRA_DEBUG_RECV)
2f35a820
DS
1732 zlog_debug("%s: adding %d backup nexthops", __func__,
1733 backup_nh_num);
7fcb24bb 1734
66c28560 1735 bnhg = zebra_nhg_backup_alloc();
62e46303
MS
1736 }
1737
bf094f69
QY
1738 /*
1739 * TBD should _all_ of the nexthop add operations use
1740 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1741 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1742 */
f70da2a3
DS
1743 for (i = 0; i < nexthop_num; i++) {
1744 struct nexthop *nexthop;
1745 enum lsp_types_t label_type;
1746 char nhbuf[NEXTHOP_STRLEN];
1747 char labelbuf[MPLS_LABEL_STRLEN];
1748 struct zapi_nexthop *api_nh = &nhops[i];
7fcb24bb 1749
62e46303 1750 /* Convert zapi nexthop */
2f35a820 1751 nexthop = nexthop_from_zapi(api_nh, flags, p, backup_nh_num);
62e46303
MS
1752 if (!nexthop) {
1753 flog_warn(
1754 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
f70da2a3
DS
1755 "%s: Nexthops Specified: %u(%u) but we failed to properly create one",
1756 __func__, nexthop_num, i);
1757 if (ng)
1758 nexthop_group_delete(&ng);
1759 if (bnhg)
1760 zebra_nhg_backup_free(&bnhg);
1761 return false;
62e46303 1762 }
7fcb24bb 1763
ff9aca4f
SW
1764 if (bnhg
1765 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
f70da2a3
DS
1766 if (IS_ZEBRA_DEBUG_RECV) {
1767 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1768 zlog_debug("%s: backup nh %s with BACKUP flag!",
1769 __func__, nhbuf);
1770 }
1771 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1772 nexthop->backup_num = 0;
1773 }
1774
2f35a820 1775 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRTE)) {
31f937fb
SM
1776 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE);
1777 nexthop->srte_color = api_nh->srte_color;
1778 }
1779
62e46303
MS
1780 /* MPLS labels for BGP-LU or Segment Routing */
1781 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1782 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1783 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE
1784 && api_nh->label_num > 0) {
7fcb24bb 1785
62e46303 1786 label_type = lsp_type_from_re_type(client->proto);
62e46303
MS
1787 nexthop_add_labels(nexthop, label_type,
1788 api_nh->label_num,
1789 &api_nh->labels[0]);
bf094f69 1790 }
bf094f69 1791
c60c1ade 1792 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL)
8689b25a
HS
1793 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1794 if (IS_ZEBRA_DEBUG_RECV)
1795 zlog_debug("%s: adding seg6local action %s",
1796 __func__,
1797 seg6local_action2str(
1798 api_nh->seg6local_action));
1799
eab0f8f0
HS
1800 nexthop_add_srv6_seg6local(nexthop,
1801 api_nh->seg6local_action,
1802 &api_nh->seg6local_ctx);
8689b25a
HS
1803 }
1804
c60c1ade 1805 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6)
76fb7ae4
HS
1806 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1807 if (IS_ZEBRA_DEBUG_RECV)
1808 zlog_debug("%s: adding seg6", __func__);
1809
eab0f8f0 1810 nexthop_add_srv6_seg6(nexthop, &api_nh->seg6_segs);
76fb7ae4
HS
1811 }
1812
1d48702e
MS
1813 if (IS_ZEBRA_DEBUG_RECV) {
1814 labelbuf[0] = '\0';
1815 nhbuf[0] = '\0';
1816
1817 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1818
1819 if (nexthop->nh_label &&
1820 nexthop->nh_label->num_labels > 0) {
1821 mpls_label2str(nexthop->nh_label->num_labels,
1822 nexthop->nh_label->label,
1823 labelbuf, sizeof(labelbuf),
1824 false);
1825 }
1826
1827 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1828 __func__, nhbuf, api_nh->vrf_id, labelbuf);
1829 }
1830
66c28560 1831 if (ng) {
f70da2a3 1832 /* Add new nexthop to temporary list. This list is
ff9aca4f
SW
1833 * canonicalized - sorted - so that it can be hashed
1834 * later in route processing. We expect that the sender
1835 * has sent the list sorted, and the zapi client api
1836 * attempts to enforce that, so this should be
1837 * inexpensive - but it is necessary to support shared
1838 * nexthop-groups.
f70da2a3
DS
1839 */
1840 nexthop_group_add_sorted(ng, nexthop);
1841 }
1842 if (bnhg) {
ff9aca4f
SW
1843 /* Note that the order of the backup nexthops is
1844 * significant, so we don't sort this list as we do the
1845 * primary nexthops, we just append.
f70da2a3
DS
1846 */
1847 if (last_nh)
1848 NEXTHOP_APPEND(last_nh, nexthop);
1849 else
1850 bnhg->nhe->nhg.nexthop = nexthop;
1851
1852 last_nh = nexthop;
1853 }
62e46303
MS
1854 }
1855
66c28560
SW
1856
1857 /* succesfully read, set caller pointers now */
1858 if (png)
1859 *png = ng;
1860
1861 if (pbnhg)
1862 *pbnhg = bnhg;
1863
f70da2a3
DS
1864 return true;
1865}
62e46303 1866
5898ce6f 1867static int zapi_nhg_decode(struct stream *s, int cmd, struct zapi_nhg *api_nhg)
21735352
SW
1868{
1869 uint16_t i;
1870 struct zapi_nexthop *znh;
1871
1872 STREAM_GETW(s, api_nhg->proto);
1873 STREAM_GETL(s, api_nhg->id);
1874
1875 if (cmd == ZEBRA_NHG_DEL)
1876 goto done;
1877
ca2b3467
DS
1878 STREAM_GETW(s, api_nhg->resilience.buckets);
1879 STREAM_GETL(s, api_nhg->resilience.idle_timer);
1880 STREAM_GETL(s, api_nhg->resilience.unbalanced_timer);
1881
21735352
SW
1882 /* Nexthops */
1883 STREAM_GETW(s, api_nhg->nexthop_num);
1884
1885 if (zserv_nexthop_num_warn(__func__, NULL, api_nhg->nexthop_num))
1886 return -1;
1887
1888 if (api_nhg->nexthop_num <= 0) {
1889 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1890 "%s: No nexthops sent", __func__);
1891 return -1;
1892 }
1893
1894 for (i = 0; i < api_nhg->nexthop_num; i++) {
1895 znh = &((api_nhg->nexthops)[i]);
1896
1897 if (zapi_nexthop_decode(s, znh, 0, 0) != 0) {
1898 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1899 "%s: Nexthop creation failed", __func__);
1900 return -1;
1901 }
1902 }
1903
1904 /* Backup Nexthops */
1905 STREAM_GETW(s, api_nhg->backup_nexthop_num);
1906
1907 if (zserv_nexthop_num_warn(__func__, NULL, api_nhg->backup_nexthop_num))
1908 return -1;
1909
1910 for (i = 0; i < api_nhg->backup_nexthop_num; i++) {
1911 znh = &((api_nhg->backup_nexthops)[i]);
1912
1913 if (zapi_nexthop_decode(s, znh, 0, 0) != 0) {
1914 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1915 "%s: Backup Nexthop creation failed",
1916 __func__);
1917 return -1;
1918 }
1919 }
1920
1921done:
1922 return 0;
1923
1924stream_failure:
1925 flog_warn(
1926 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1927 "%s: Nexthop Group decode failed with some sort of stream read failure",
1928 __func__);
1929 return -1;
1930}
1931
2f35a820
DS
1932static void zread_nhg_del(ZAPI_HANDLER_ARGS)
1933{
21735352
SW
1934 struct stream *s;
1935 struct zapi_nhg api_nhg = {};
0885b1e3 1936 struct nhg_hash_entry *nhe;
2f35a820 1937
21735352
SW
1938 s = msg;
1939 if (zapi_nhg_decode(s, hdr->command, &api_nhg) < 0) {
1940 if (IS_ZEBRA_DEBUG_RECV)
1941 zlog_debug("%s: Unable to decode zapi_nhg sent",
1942 __func__);
1943 return;
1944 }
2f35a820
DS
1945
1946 /*
1947 * Delete the received nhg id
2f35a820 1948 */
21735352 1949 nhe = zebra_nhg_proto_del(api_nhg.id, api_nhg.proto);
0885b1e3
SW
1950
1951 if (nhe) {
6fae63d2 1952 zebra_nhg_decrement_ref(nhe);
ee94437e
MS
1953 zsend_nhg_notify(api_nhg.proto, client->instance,
1954 client->session_id, api_nhg.id,
1955 ZAPI_NHG_REMOVED);
0885b1e3 1956 } else
ee94437e
MS
1957 zsend_nhg_notify(api_nhg.proto, client->instance,
1958 client->session_id, api_nhg.id,
1959 ZAPI_NHG_REMOVE_FAIL);
2f35a820
DS
1960}
1961
8b2d3a0f 1962static void zread_nhg_add(ZAPI_HANDLER_ARGS)
2f35a820
DS
1963{
1964 struct stream *s;
21735352 1965 struct zapi_nhg api_nhg = {};
2f35a820 1966 struct nexthop_group *nhg = NULL;
21735352 1967 struct nhg_backup_info *bnhg = NULL;
0885b1e3 1968 struct nhg_hash_entry *nhe;
2f35a820 1969
2f35a820 1970 s = msg;
21735352
SW
1971 if (zapi_nhg_decode(s, hdr->command, &api_nhg) < 0) {
1972 if (IS_ZEBRA_DEBUG_RECV)
1973 zlog_debug("%s: Unable to decode zapi_nhg sent",
1974 __func__);
68671c74
SW
1975 return;
1976 }
1977
21735352
SW
1978 if ((!zapi_read_nexthops(client, NULL, api_nhg.nexthops, 0, 0,
1979 api_nhg.nexthop_num,
1980 api_nhg.backup_nexthop_num, &nhg, NULL))
1981 || (!zapi_read_nexthops(client, NULL, api_nhg.backup_nexthops, 0, 0,
1982 api_nhg.backup_nexthop_num,
1983 api_nhg.backup_nexthop_num, NULL, &bnhg))) {
2f35a820 1984
2f35a820 1985 flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED,
ff9aca4f 1986 "%s: Nexthop Group Creation failed", __func__);
fd99142a
MS
1987
1988 /* Free any local allocations */
1989 nexthop_group_delete(&nhg);
1990 zebra_nhg_backup_free(&bnhg);
1991
2f35a820
DS
1992 return;
1993 }
0885b1e3 1994
04bec7b2
MS
1995 /* Create a temporary nhe */
1996 nhe = zebra_nhg_alloc();
1997 nhe->id = api_nhg.id;
1998 nhe->type = api_nhg.proto;
1999 nhe->zapi_instance = client->instance;
2000 nhe->zapi_session = client->session_id;
0885b1e3 2001
04bec7b2
MS
2002 /* Take over the list(s) of nexthops */
2003 nhe->nhg.nexthop = nhg->nexthop;
2004 nhg->nexthop = NULL;
2005
ca2b3467
DS
2006 nhe->nhg.nhgr = api_nhg.resilience;
2007
04bec7b2
MS
2008 if (bnhg) {
2009 nhe->backup_info = bnhg;
2010 bnhg = NULL;
2011 }
0885b1e3
SW
2012
2013 /*
2014 * TODO:
2015 * Assume fully resolved for now and install.
0885b1e3
SW
2016 * Resolution is going to need some more work.
2017 */
ee94437e 2018
04bec7b2
MS
2019 /* Enqueue to workqueue for processing */
2020 rib_queue_nhe_add(nhe);
2021
2022 /* Free any local allocations */
2023 nexthop_group_delete(&nhg);
2024 zebra_nhg_backup_free(&bnhg);
2025
2f35a820
DS
2026}
2027
f70da2a3
DS
2028static void zread_route_add(ZAPI_HANDLER_ARGS)
2029{
2030 struct stream *s;
2031 struct zapi_route api;
2032 afi_t afi;
2033 struct prefix_ipv6 *src_p = NULL;
2034 struct route_entry *re;
2035 struct nexthop_group *ng = NULL;
2036 struct nhg_backup_info *bnhg = NULL;
2037 int ret;
2038 vrf_id_t vrf_id;
d7ac4c4d 2039 struct nhg_hash_entry nhe, *n = NULL;
0eb97b86 2040
f70da2a3
DS
2041 s = msg;
2042 if (zapi_route_decode(s, &api) < 0) {
2043 if (IS_ZEBRA_DEBUG_RECV)
2044 zlog_debug("%s: Unable to decode zapi_route sent",
2045 __func__);
2046 return;
2047 }
1d48702e 2048
f70da2a3 2049 vrf_id = zvrf_id(zvrf);
31f937fb 2050
2dbe669b
DA
2051 if (IS_ZEBRA_DEBUG_RECV)
2052 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2053 __func__, vrf_id, api.tableid, &api.prefix,
f70da2a3 2054 (int)api.message, api.flags);
0eb97b86 2055
f70da2a3 2056 /* Allocate new route. */
b0385873
DS
2057 re = zebra_rib_route_entry_new(
2058 vrf_id, api.type, api.instance, api.flags, api.nhgid,
2059 api.tableid ? api.tableid : zvrf->table_id, api.metric, api.mtu,
2060 api.distance, api.tag);
1d48702e 2061
27141ea9
DS
2062 if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NHG)
2063 && (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)
2064 || api.nexthop_num == 0)) {
ff9aca4f
SW
2065 flog_warn(
2066 EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS,
2067 "%s: received a route without nexthops for prefix %pFX from client %s",
2068 __func__, &api.prefix,
2069 zebra_route_string(client->proto));
1d48702e 2070
f70da2a3
DS
2071 XFREE(MTYPE_RE, re);
2072 return;
2073 }
1d48702e 2074
f70da2a3 2075 /* Report misuse of the backup flag */
ff9aca4f
SW
2076 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS)
2077 && api.backup_nexthop_num == 0) {
f70da2a3 2078 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
ff9aca4f
SW
2079 zlog_debug(
2080 "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
2081 __func__, zebra_route_string(client->proto),
2082 &api.prefix);
f70da2a3 2083 }
62e46303 2084
50db3f2f 2085 if (!re->nhe_id
2b5ecd4c
SW
2086 && (!zapi_read_nexthops(client, &api.prefix, api.nexthops,
2087 api.flags, api.message, api.nexthop_num,
2088 api.backup_nexthop_num, &ng, NULL)
2089 || !zapi_read_nexthops(client, &api.prefix, api.backup_nexthops,
2090 api.flags, api.message,
2091 api.backup_nexthop_num,
2092 api.backup_nexthop_num, NULL, &bnhg))) {
21735352
SW
2093
2094 nexthop_group_delete(&ng);
2095 zebra_nhg_backup_free(&bnhg);
f70da2a3
DS
2096 XFREE(MTYPE_RE, re);
2097 return;
7fcb24bb
RW
2098 }
2099
a29a6001 2100 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_OPAQUE)) {
b86c1f4f
MS
2101 re->opaque =
2102 XMALLOC(MTYPE_RE_OPAQUE,
2103 sizeof(struct re_opaque) + api.opaque.length);
a29a6001
DS
2104 re->opaque->length = api.opaque.length;
2105 memcpy(re->opaque->data, api.opaque.data, re->opaque->length);
2106 }
2107
bf094f69
QY
2108 afi = family2afi(api.prefix.family);
2109 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 2110 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 2111 "%s: Received SRC Prefix but afi is not v6",
15569c58 2112 __func__);
0eb97b86 2113 nexthop_group_delete(&ng);
1d48702e 2114 zebra_nhg_backup_free(&bnhg);
b86c1f4f 2115 XFREE(MTYPE_RE_OPAQUE, re->opaque);
bf094f69
QY
2116 XFREE(MTYPE_RE, re);
2117 return;
2118 }
2119 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
2120 src_p = &api.src_prefix;
2121
5e7939a5
DS
2122 if (api.safi != SAFI_UNICAST && api.safi != SAFI_MULTICAST) {
2123 flog_warn(EC_LIB_ZAPI_MISSMATCH,
2124 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
2125 __func__, api.safi);
2126 nexthop_group_delete(&ng);
1d48702e 2127 zebra_nhg_backup_free(&bnhg);
b86c1f4f 2128 XFREE(MTYPE_RE_OPAQUE, re->opaque);
5e7939a5
DS
2129 XFREE(MTYPE_RE, re);
2130 return;
2131 }
1d48702e 2132
50db3f2f
SW
2133 /*
2134 * If we have an ID, this proto owns the NHG it sent along with the
2135 * route, so we just send the ID into rib code with it.
2136 *
2137 * Havent figured out how to handle backup NHs with this yet, so lets
2138 * keep that separate.
2139 * Include backup info with the route. We use a temporary nhe here;
377e29f7
MS
2140 * if this is a new/unknown nhe, a new copy will be allocated
2141 * and stored.
2142 */
50db3f2f
SW
2143 if (!re->nhe_id) {
2144 zebra_nhe_init(&nhe, afi, ng->nexthop);
2145 nhe.nhg.nexthop = ng->nexthop;
2146 nhe.backup_info = bnhg;
d7ac4c4d 2147 n = zebra_nhe_copy(&nhe, 0);
50db3f2f 2148 }
d7ac4c4d
DS
2149 ret = rib_add_multipath_nhe(afi, api.safi, &api.prefix, src_p, re, n,
2150 false);
bf094f69 2151
f94a7703
DS
2152 /*
2153 * rib_add_multipath_nhe only fails in a couple spots
2154 * and in those spots we have not freed memory
2155 */
38c764dd
DS
2156 if (ret == -1) {
2157 client->error_cnt++;
b86c1f4f 2158 XFREE(MTYPE_RE_OPAQUE, re->opaque);
f94a7703 2159 XFREE(MTYPE_RE, re);
38c764dd 2160 }
f94a7703 2161
377e29f7
MS
2162 /* At this point, these allocations are not needed: 're' has been
2163 * retained or freed, and if 're' still exists, it is using
2164 * a reference to a shared group object.
2165 */
2166 nexthop_group_delete(&ng);
2167 if (bnhg)
2168 zebra_nhg_backup_free(&bnhg);
2169
bf094f69
QY
2170 /* Stats */
2171 switch (api.prefix.family) {
2172 case AF_INET:
38c764dd 2173 if (ret == 0)
bf094f69 2174 client->v4_route_add_cnt++;
38c764dd 2175 else if (ret == 1)
bf094f69
QY
2176 client->v4_route_upd8_cnt++;
2177 break;
2178 case AF_INET6:
38c764dd 2179 if (ret == 0)
bf094f69 2180 client->v6_route_add_cnt++;
38c764dd 2181 else if (ret == 1)
bf094f69
QY
2182 client->v6_route_upd8_cnt++;
2183 break;
2184 }
2185}
2186
b86c1f4f 2187void zapi_re_opaque_free(struct re_opaque *opaque)
224ccf29 2188{
b86c1f4f 2189 XFREE(MTYPE_RE_OPAQUE, opaque);
224ccf29
DL
2190}
2191
bf094f69
QY
2192static void zread_route_del(ZAPI_HANDLER_ARGS)
2193{
2194 struct stream *s;
2195 struct zapi_route api;
2196 afi_t afi;
2197 struct prefix_ipv6 *src_p = NULL;
2198 uint32_t table_id;
2199
2200 s = msg;
2201 if (zapi_route_decode(s, &api) < 0)
2202 return;
2203
2204 afi = family2afi(api.prefix.family);
2205 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 2206 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 2207 "%s: Received a src prefix while afi is not v6",
15569c58 2208 __func__);
bf094f69
QY
2209 return;
2210 }
2211 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
2212 src_p = &api.src_prefix;
2213
60ca3cc2 2214 if (api.tableid)
bf094f69
QY
2215 table_id = api.tableid;
2216 else
2217 table_id = zvrf->table_id;
2218
2dbe669b
DA
2219 if (IS_ZEBRA_DEBUG_RECV)
2220 zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x",
2221 __func__, zvrf_id(zvrf), table_id, &api.prefix,
c2c02b76 2222 (int)api.message, api.flags);
c2c02b76 2223
bf094f69 2224 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
bc541126 2225 api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric,
3ceae22b 2226 api.distance, false);
bf094f69
QY
2227
2228 /* Stats */
2229 switch (api.prefix.family) {
2230 case AF_INET:
2231 client->v4_route_del_cnt++;
2232 break;
2233 case AF_INET6:
2234 client->v6_route_del_cnt++;
2235 break;
2236 }
2237}
2238
bf094f69 2239/* MRIB Nexthop lookup for IPv4. */
34ee41c6 2240static void zread_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
bf094f69 2241{
34ee41c6
DL
2242 struct ipaddr addr;
2243 struct route_entry *re = NULL;
2244
2245 STREAM_GET_IPADDR(msg, &addr);
2246
2247 switch (addr.ipa_type) {
2248 case IPADDR_V4:
2249 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr.ipaddr_v4,
2250 NULL);
2251 break;
2252 case IPADDR_V6:
2253 re = rib_match_ipv6_multicast(zvrf_id(zvrf), addr.ipaddr_v6,
2254 NULL);
2255 break;
2256 case IPADDR_NONE:
2257 /* ??? */
2258 goto stream_failure;
2259 }
bf094f69 2260
34ee41c6 2261 zsend_nexthop_lookup_mrib(client, &addr, re, zvrf);
bf094f69
QY
2262
2263stream_failure:
2264 return;
2265}
2266
bf094f69
QY
2267/* Register zebra server router-id information. Send current router-id */
2268static void zread_router_id_add(ZAPI_HANDLER_ARGS)
2269{
98a3fb0a 2270 afi_t afi;
bf094f69 2271 struct prefix p;
01141358 2272 struct prefix zero;
bf094f69 2273
98a3fb0a
SM
2274 STREAM_GETW(msg, afi);
2275
2276 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
2277 zlog_warn(
2278 "Invalid AFI %u while registering for router ID notifications",
2279 afi);
2280 goto stream_failure;
2281 }
2282
bf094f69 2283 /* Router-id information is needed. */
98a3fb0a 2284 vrf_bitmap_set(client->ridinfo[afi], zvrf_id(zvrf));
bf094f69 2285
98a3fb0a 2286 router_id_get(afi, &p, zvrf);
bf094f69 2287
01141358
DS
2288 /*
2289 * If we have not officially setup a router-id let's not
2290 * tell the upper level protocol about it yet.
2291 */
2292 memset(&zero, 0, sizeof(zero));
2293 if ((p.family == AF_INET && p.u.prefix4.s_addr == INADDR_ANY)
2294 || (p.family == AF_INET6
2295 && memcmp(&p.u.prefix6, &zero.u.prefix6,
2296 sizeof(struct in6_addr))
2297 == 0))
2298 return;
2299
98a3fb0a
SM
2300 zsend_router_id_update(client, afi, &p, zvrf_id(zvrf));
2301
2302stream_failure:
2303 return;
bf094f69
QY
2304}
2305
2306/* Unregister zebra server router-id information. */
2307static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
2308{
98a3fb0a
SM
2309 afi_t afi;
2310
2311 STREAM_GETW(msg, afi);
2312
2313 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
2314 zlog_warn(
2315 "Invalid AFI %u while unregistering from router ID notifications",
2316 afi);
2317 goto stream_failure;
2318 }
2319
2320 vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf));
2321
2322stream_failure:
2323 return;
bf094f69
QY
2324}
2325
2326static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
2327{
2328 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2329
2330 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
bb6b7f79 2331 stream_putl(s, vrf_get_backend());
bf094f69 2332 stream_putc(s, mpls_enabled);
b3f2b590 2333 stream_putl(s, zrouter.multipath_num);
02c0866d 2334 stream_putc(s, zebra_mlag_get_role());
bf094f69
QY
2335
2336 stream_putw_at(s, 0, stream_get_endp(s));
21ccc0cf 2337 zserv_send_message(client, s);
bf094f69
QY
2338}
2339
b120fe3b
DS
2340void zsend_capabilities_all_clients(void)
2341{
2342 struct listnode *node, *nnode;
2343 struct zebra_vrf *zvrf;
2344 struct zserv *client;
2345
2346 zvrf = vrf_info_lookup(VRF_DEFAULT);
2347 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
17da84a4
KS
2348 /* Do not send unsolicited messages to synchronous clients. */
2349 if (client->synchronous)
2350 continue;
2351
b120fe3b
DS
2352 zsend_capabilities(client, zvrf);
2353 }
2354}
2355
bf094f69
QY
2356/* Tie up route-type and client->sock */
2357static void zread_hello(ZAPI_HANDLER_ARGS)
2358{
2359 /* type of protocol (lib/zebra.h) */
2360 uint8_t proto;
2361 unsigned short instance;
2362 uint8_t notify;
17da84a4 2363 uint8_t synchronous;
4e0b5b31 2364 uint32_t session_id;
bf094f69
QY
2365
2366 STREAM_GETC(msg, proto);
2367 STREAM_GETW(msg, instance);
4e0b5b31 2368 STREAM_GETL(msg, session_id);
bf094f69 2369 STREAM_GETC(msg, notify);
17da84a4 2370 STREAM_GETC(msg, synchronous);
bf094f69
QY
2371 if (notify)
2372 client->notify_owner = true;
2373
17da84a4
KS
2374 if (synchronous)
2375 client->synchronous = true;
2376
bf094f69 2377 /* accept only dynamic routing protocols */
f23cbcda 2378 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
bf094f69
QY
2379 zlog_notice(
2380 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2381 client->sock, zebra_route_string(proto),
2382 zvrf->vrf->vrf_id);
2383 if (instance)
2384 zlog_notice("client protocol instance %d", instance);
2385
2386 client->proto = proto;
2387 client->instance = instance;
4e0b5b31 2388 client->session_id = session_id;
6f4aee61
S
2389
2390 /* Graceful restart processing for client connect */
2391 zebra_gr_client_reconnect(client);
bf094f69
QY
2392 }
2393
17da84a4
KS
2394 if (!client->synchronous) {
2395 zsend_capabilities(client, zvrf);
2396 zebra_vrf_update_all(client);
2397 }
bf094f69
QY
2398stream_failure:
2399 return;
2400}
2401
2402/* Unregister all information in a VRF. */
2403static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
2404{
2405 int i;
2406 afi_t afi;
2407
49db7a7b 2408 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
bf094f69
QY
2409 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2410 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
49db7a7b 2411 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
98a3fb0a 2412 vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf));
7723e8d3 2413 vrf_bitmap_unset(client->nhrp_neighinfo[afi], zvrf_id(zvrf));
49db7a7b 2414 }
bf094f69
QY
2415}
2416
ff8d3c2d
MS
2417/*
2418 * Validate incoming zapi mpls lsp / labels message
2419 */
2420static int zapi_labels_validate(const struct zapi_labels *zl)
2421{
2422 int ret = -1;
2423 int i, j, idx;
2424 uint32_t bits[8];
2425 uint32_t ival;
2426 const struct zapi_nexthop *znh;
2427
2428 /* Validate backup info: no duplicates for a single primary */
2429 if (zl->backup_nexthop_num == 0) {
2430 ret = 0;
2431 goto done;
2432 }
2433
2434 for (j = 0; j < zl->nexthop_num; j++) {
2435 znh = &zl->nexthops[j];
2436
2437 memset(bits, 0, sizeof(bits));
2438
2439 for (i = 0; i < znh->backup_num; i++) {
2440 idx = znh->backup_idx[i] / 32;
2441
2442 ival = 1 << znh->backup_idx[i] % 32;
2443
2444 /* Check whether value is already used */
2445 if (ival & bits[idx]) {
2446 /* Fail */
2447
2448 if (IS_ZEBRA_DEBUG_RECV)
2449 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2450 __func__,
2451 znh->backup_idx[i]);
2452 goto done;
2453 }
2454
2455 /* Mark index value */
2456 bits[idx] |= ival;
2457 }
2458 }
2459
2460 ret = 0;
2461
2462done:
2463
2464 return ret;
2465}
2466
ea6b290b
RW
2467/*
2468 * Handle request to create an MPLS LSP.
2469 *
2470 * A single message can fully specify an LSP with multiple nexthops.
2471 *
2472 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2473 * updated to use the received label(s).
2474 */
2475static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS)
bf094f69
QY
2476{
2477 struct stream *s;
bad6b0e7 2478 struct zapi_labels zl;
bf094f69
QY
2479
2480 /* Get input stream. */
2481 s = msg;
bad6b0e7
RW
2482 if (zapi_labels_decode(s, &zl) < 0) {
2483 if (IS_ZEBRA_DEBUG_RECV)
2484 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2485 __func__);
bf094f69
QY
2486 return;
2487 }
bf094f69 2488
bf094f69
QY
2489 if (!mpls_enabled)
2490 return;
2491
ff8d3c2d
MS
2492 /* Validate; will debug on failure */
2493 if (zapi_labels_validate(&zl) < 0)
2494 return;
2495
1548fbbc 2496 mpls_zapi_labels_process(true, zvrf, &zl);
ea6b290b
RW
2497}
2498
2499/*
2500 * Handle request to delete an MPLS LSP.
2501 *
2502 * An LSP is identified by its type and local label. When the received message
2503 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2504 * listed LSP nexthops (aka NHLFEs) are deleted.
2505 *
2506 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2507 * FEC (route) nexthops are deleted.
2508 */
2509static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS)
2510{
2511 struct stream *s;
2512 struct zapi_labels zl;
2513
2514 /* Get input stream. */
2515 s = msg;
2516 if (zapi_labels_decode(s, &zl) < 0) {
2517 if (IS_ZEBRA_DEBUG_RECV)
2518 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2519 __func__);
ea6b290b
RW
2520 return;
2521 }
2522
2523 if (!mpls_enabled)
2524 return;
2525
2526 if (zl.nexthop_num > 0) {
1548fbbc 2527 mpls_zapi_labels_process(false /*delete*/, zvrf, &zl);
ea6b290b
RW
2528 } else {
2529 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2530
b3c49d0e 2531 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
ea6b290b
RW
2532 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2533 zl.route.type, zl.route.instance);
2534 }
2535}
2536
2537/*
2538 * Handle request to add an MPLS LSP or change an existing one.
2539 *
2540 * A single message can fully specify an LSP with multiple nexthops.
2541 *
2542 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2543 * updated to use the received label(s).
2544 *
2545 * NOTE: zebra will use route replace semantics (make-before-break) to update
2546 * the LSP in the forwarding plane if that's supported by the underlying
2547 * platform.
2548 */
2549static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS)
2550{
2551 struct stream *s;
2552 struct zapi_labels zl;
2553
2554 /* Get input stream. */
2555 s = msg;
2556 if (zapi_labels_decode(s, &zl) < 0) {
2557 if (IS_ZEBRA_DEBUG_RECV)
2558 zlog_debug("%s: Unable to decode zapi_labels sent",
15569c58 2559 __func__);
ea6b290b
RW
2560 return;
2561 }
2562
2563 if (!mpls_enabled)
2564 return;
2565
ff8d3c2d
MS
2566 /* Validate; will debug on failure */
2567 if (zapi_labels_validate(&zl) < 0)
2568 return;
2569
f2e7f4eb
MS
2570 /* This removes everything, then re-adds from the client's
2571 * zapi message. Since the LSP will be processed later, on this
2572 * this same pthread, all of the changes will 'appear' at once.
2573 */
ea6b290b
RW
2574 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2575 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
2576 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2577 zl.route.type, zl.route.instance);
2578
f2e7f4eb 2579 mpls_zapi_labels_process(true, zvrf, &zl);
bf094f69
QY
2580}
2581
31f937fb
SM
2582static void zread_sr_policy_set(ZAPI_HANDLER_ARGS)
2583{
2584 struct stream *s;
2585 struct zapi_sr_policy zp;
2586 struct zapi_srte_tunnel *zt;
2587 struct zebra_sr_policy *policy;
2588
2589 /* Get input stream. */
2590 s = msg;
2591 if (zapi_sr_policy_decode(s, &zp) < 0) {
2592 if (IS_ZEBRA_DEBUG_RECV)
2593 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
7d7be47e 2594 __func__);
31f937fb
SM
2595 return;
2596 }
2597 zt = &zp.segment_list;
2598 if (zt->label_num < 1) {
2599 if (IS_ZEBRA_DEBUG_RECV)
2600 zlog_debug(
2601 "%s: SR-TE tunnel must contain at least one label",
7d7be47e 2602 __func__);
31f937fb
SM
2603 return;
2604 }
2605
2606 if (!mpls_enabled)
2607 return;
2608
2609 policy = zebra_sr_policy_find(zp.color, &zp.endpoint);
6082fb42 2610 if (!policy) {
31f937fb 2611 policy = zebra_sr_policy_add(zp.color, &zp.endpoint, zp.name);
6082fb42
PG
2612 policy->sock = client->sock;
2613 }
31f937fb
SM
2614 /* TODO: per-VRF list of SR-TE policies. */
2615 policy->zvrf = zvrf;
2616
2617 zebra_sr_policy_validate(policy, &zp.segment_list);
2618}
2619
2620static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS)
2621{
2622 struct stream *s;
2623 struct zapi_sr_policy zp;
2624 struct zebra_sr_policy *policy;
2625
2626 /* Get input stream. */
2627 s = msg;
2628 if (zapi_sr_policy_decode(s, &zp) < 0) {
2629 if (IS_ZEBRA_DEBUG_RECV)
2630 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
7d7be47e 2631 __func__);
31f937fb
SM
2632 return;
2633 }
2634
2635 if (!mpls_enabled)
2636 return;
2637
2638 policy = zebra_sr_policy_find(zp.color, &zp.endpoint);
2639 if (!policy) {
2640 if (IS_ZEBRA_DEBUG_RECV)
7d7be47e 2641 zlog_debug("%s: Unable to find SR-TE policy", __func__);
31f937fb
SM
2642 return;
2643 }
2644
2645 zebra_sr_policy_del(policy);
2646}
2647
2648int zsend_sr_policy_notify_status(uint32_t color, struct ipaddr *endpoint,
2649 char *name, int status)
2650{
2651 struct zserv *client;
2652 struct stream *s;
2653
2654 client = zserv_find_client(ZEBRA_ROUTE_SRTE, 0);
2655 if (!client) {
2656 if (IS_ZEBRA_DEBUG_PACKET)
2657 zlog_debug(
2658 "Not notifying pathd about policy %s"
2659 " status change to %d",
2660 name, status);
2661 return 0;
2662 }
2663
2664 if (IS_ZEBRA_DEBUG_PACKET)
2665 zlog_debug(
2666 "Notifying pathd about policy %s status change"
2667 " to %d",
2668 name, status);
2669
2670 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2671 stream_reset(s);
2672
2673 zclient_create_header(s, ZEBRA_SR_POLICY_NOTIFY_STATUS, VRF_DEFAULT);
2674 stream_putl(s, color);
2675 stream_put_ipaddr(s, endpoint);
2676 stream_write(s, name, SRTE_POLICY_NAME_MAX_LENGTH);
2677 stream_putl(s, status);
2678
2679 stream_putw_at(s, 0, stream_get_endp(s));
2680
2681 return zserv_send_message(client, s);
2682}
2683
581e797e
KS
2684/* Send client close notify to client */
2685int zsend_client_close_notify(struct zserv *client, struct zserv *closed_client)
2686{
2687 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2688
2689 zclient_create_header(s, ZEBRA_CLIENT_CLOSE_NOTIFY, VRF_DEFAULT);
2690
2691 stream_putc(s, closed_client->proto);
2692 stream_putw(s, closed_client->instance);
2693 stream_putl(s, closed_client->session_id);
2694
2695 stream_putw_at(s, 0, stream_get_endp(s));
2696
2697 return zserv_send_message(client, s);
2698}
2699
6e68a084
HS
2700int zsend_srv6_manager_get_locator_chunk_response(struct zserv *client,
2701 vrf_id_t vrf_id,
2702 struct srv6_locator *loc)
2703{
1bda3e62 2704 struct srv6_locator_chunk chunk = {};
6e68a084
HS
2705 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2706
ac6a9479
HS
2707 strlcpy(chunk.locator_name, loc->name, sizeof(chunk.locator_name));
2708 chunk.prefix = loc->prefix;
2709 chunk.block_bits_length = loc->block_bits_length;
2710 chunk.node_bits_length = loc->node_bits_length;
2711 chunk.function_bits_length = loc->function_bits_length;
2712 chunk.argument_bits_length = loc->argument_bits_length;
2713 chunk.keep = 0;
2714 chunk.proto = client->proto;
2715 chunk.instance = client->instance;
d537287a 2716 chunk.flags = loc->flags;
6e68a084 2717
ac6a9479
HS
2718 zclient_create_header(s, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK, vrf_id);
2719 zapi_srv6_locator_chunk_encode(s, &chunk);
6e68a084 2720 stream_putw_at(s, 0, stream_get_endp(s));
6e68a084
HS
2721 return zserv_send_message(client, s);
2722}
2723
bf094f69
QY
2724/* Send response to a table manager connect request to client */
2725static void zread_table_manager_connect(struct zserv *client,
2726 struct stream *msg, vrf_id_t vrf_id)
2727{
2728 struct stream *s;
2729 uint8_t proto;
2730 uint16_t instance;
c479e756 2731 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
bf094f69
QY
2732
2733 s = msg;
2734
2735 /* Get data. */
2736 STREAM_GETC(s, proto);
2737 STREAM_GETW(s, instance);
2738
2739 /* accept only dynamic routing protocols */
2740 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 2741 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
2742 "client %d has wrong protocol %s", client->sock,
2743 zebra_route_string(proto));
bf094f69
QY
2744 zsend_table_manager_connect_response(client, vrf_id, 1);
2745 return;
2746 }
c479e756
DS
2747 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2748 client->sock, VRF_LOGNAME(vrf), vrf_id, instance,
2749 zebra_route_string(proto));
bf094f69
QY
2750 client->proto = proto;
2751 client->instance = instance;
2752
2753 /*
2754 * Release previous labels of same protocol and instance.
2755 * This is done in case it restarted from an unexpected shutdown.
2756 */
453844ab 2757 release_daemon_table_chunks(client);
bf094f69
QY
2758
2759 zsend_table_manager_connect_response(client, vrf_id, 0);
2760
2761stream_failure:
2762 return;
2763}
2764
2765static void zread_label_manager_connect(struct zserv *client,
2766 struct stream *msg, vrf_id_t vrf_id)
2767{
2768 struct stream *s;
2769 /* type of protocol (lib/zebra.h) */
2770 uint8_t proto;
2771 unsigned short instance;
2772
2773 /* Get input stream. */
2774 s = msg;
2775
2776 /* Get data. */
2777 STREAM_GETC(s, proto);
2778 STREAM_GETW(s, instance);
2779
2780 /* accept only dynamic routing protocols */
2781 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 2782 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
2783 "client %d has wrong protocol %s", client->sock,
2784 zebra_route_string(proto));
e11d7c96 2785 zsend_label_manager_connect_response(client, vrf_id, 1);
bf094f69
QY
2786 return;
2787 }
e11d7c96
EDP
2788
2789 /* recall proto and instance in this socket */
bf094f69
QY
2790 client->proto = proto;
2791 client->instance = instance;
2792
e11d7c96 2793 /* call hook for connection using wrapper */
4cebdb9b 2794 lm_client_connect_call(client, vrf_id);
bf094f69
QY
2795
2796stream_failure:
2797 return;
2798}
2799
2800static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
2801 vrf_id_t vrf_id)
2802{
2803 struct stream *s;
2804 uint8_t keep;
0e3b6a92 2805 uint32_t size, base;
e11d7c96 2806 struct label_manager_chunk *lmc = NULL;
5dffb0e9
FR
2807 uint8_t proto;
2808 unsigned short instance;
bf094f69
QY
2809
2810 /* Get input stream. */
2811 s = msg;
2812
2813 /* Get data. */
5dffb0e9
FR
2814 STREAM_GETC(s, proto);
2815 STREAM_GETW(s, instance);
bf094f69
QY
2816 STREAM_GETC(s, keep);
2817 STREAM_GETL(s, size);
0e3b6a92 2818 STREAM_GETL(s, base);
bf094f69 2819
4cebdb9b
MS
2820 assert(proto == client->proto && instance == client->instance);
2821
e11d7c96 2822 /* call hook to get a chunk using wrapper */
4cebdb9b 2823 lm_get_chunk_call(&lmc, client, keep, size, base, vrf_id);
bf094f69 2824
bf094f69
QY
2825stream_failure:
2826 return;
2827}
2828
2829static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2830{
2831 struct stream *s;
2832 uint32_t start, end;
5dffb0e9
FR
2833 uint8_t proto;
2834 unsigned short instance;
bf094f69
QY
2835
2836 /* Get input stream. */
2837 s = msg;
2838
2839 /* Get data. */
5dffb0e9
FR
2840 STREAM_GETC(s, proto);
2841 STREAM_GETW(s, instance);
bf094f69
QY
2842 STREAM_GETL(s, start);
2843 STREAM_GETL(s, end);
2844
4cebdb9b
MS
2845 assert(proto == client->proto && instance == client->instance);
2846
e11d7c96 2847 /* call hook to release a chunk using wrapper */
4cebdb9b 2848 lm_release_chunk_call(client, start, end);
bf094f69
QY
2849
2850stream_failure:
2851 return;
2852}
e11d7c96 2853
bf094f69
QY
2854static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2855{
e11d7c96
EDP
2856 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT
2857 || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
2858 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
bf094f69 2859 else {
e11d7c96
EDP
2860 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2861 zread_get_label_chunk(client, msg, zvrf_id(zvrf));
2862 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2863 zread_release_label_chunk(client, msg);
bf094f69
QY
2864 }
2865}
2866
2867static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
42d4b30e 2868 struct zebra_vrf *zvrf)
bf094f69
QY
2869{
2870 struct stream *s;
2871 uint32_t size;
2872 struct table_manager_chunk *tmc;
2873
2874 /* Get input stream. */
2875 s = msg;
2876
2877 /* Get data. */
2878 STREAM_GETL(s, size);
2879
42d4b30e 2880 tmc = assign_table_chunk(client->proto, client->instance, size, zvrf);
bf094f69 2881 if (!tmc)
e914ccbe 2882 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
1c50c1c0
QY
2883 "%s: Unable to assign Table Chunk of size %u",
2884 __func__, size);
bf094f69
QY
2885 else
2886 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2887 tmc->end);
2888 /* send response back */
42d4b30e 2889 zsend_assign_table_chunk_response(client, zvrf_id(zvrf), tmc);
bf094f69
QY
2890
2891stream_failure:
2892 return;
2893}
2894
42d4b30e
PG
2895static void zread_release_table_chunk(struct zserv *client, struct stream *msg,
2896 struct zebra_vrf *zvrf)
bf094f69
QY
2897{
2898 struct stream *s;
2899 uint32_t start, end;
2900
2901 /* Get input stream. */
2902 s = msg;
2903
2904 /* Get data. */
2905 STREAM_GETL(s, start);
2906 STREAM_GETL(s, end);
2907
42d4b30e 2908 release_table_chunk(client->proto, client->instance, start, end, zvrf);
bf094f69
QY
2909
2910stream_failure:
2911 return;
2912}
2913
2914static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2915{
16bd37d6 2916 /* to avoid sending other messages like ZEBRA_INTERFACE_UP */
bf094f69
QY
2917 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2918 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2919 else {
2920 /* Sanity: don't allow 'unidentified' requests */
2921 if (!client->proto) {
af4c2728 2922 flog_err(
e914ccbe 2923 EC_ZEBRA_TM_ALIENS,
16bd37d6 2924 "Got SRv6 request from an unidentified client");
bf094f69
QY
2925 return;
2926 }
2927 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
42d4b30e 2928 zread_get_table_chunk(client, msg, zvrf);
bf094f69 2929 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
42d4b30e 2930 zread_release_table_chunk(client, msg, zvrf);
bf094f69
QY
2931 }
2932}
2933
6e68a084
HS
2934static void zread_srv6_manager_get_locator_chunk(struct zserv *client,
2935 struct stream *msg,
2936 vrf_id_t vrf_id)
2937{
2938 struct stream *s = msg;
6e68a084
HS
2939 uint16_t len;
2940 char locator_name[SRV6_LOCNAME_SIZE] = {0};
2941
2942 /* Get data. */
6e68a084
HS
2943 STREAM_GETW(s, len);
2944 STREAM_GET(locator_name, s, len);
2945
6e68a084
HS
2946 /* call hook to get a chunk using wrapper */
2947 struct srv6_locator *loc = NULL;
2948 srv6_manager_get_locator_chunk_call(&loc, client, locator_name, vrf_id);
2949
2950stream_failure:
2951 return;
2952}
2953
2954static void zread_srv6_manager_release_locator_chunk(struct zserv *client,
2955 struct stream *msg,
2956 vrf_id_t vrf_id)
2957{
2958 struct stream *s = msg;
6e68a084
HS
2959 uint16_t len;
2960 char locator_name[SRV6_LOCNAME_SIZE] = {0};
2961
2962 /* Get data. */
6e68a084
HS
2963 STREAM_GETW(s, len);
2964 STREAM_GET(locator_name, s, len);
2965
6e68a084
HS
2966 /* call hook to release a chunk using wrapper */
2967 srv6_manager_release_locator_chunk_call(client, locator_name, vrf_id);
2968
2969stream_failure:
2970 return;
2971}
2972
2973static void zread_srv6_manager_request(ZAPI_HANDLER_ARGS)
2974{
2975 switch (hdr->command) {
6e68a084
HS
2976 case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK:
2977 zread_srv6_manager_get_locator_chunk(client, msg,
2978 zvrf_id(zvrf));
2979 break;
2980 case ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK:
2981 zread_srv6_manager_release_locator_chunk(client, msg,
2982 zvrf_id(zvrf));
2983 break;
2984 default:
9f900cda 2985 zlog_err("%s: unknown SRv6 Manager command", __func__);
6e68a084
HS
2986 break;
2987 }
2988}
2989
bf094f69
QY
2990static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2991{
2992 struct stream *s;
3682bd90 2993 char ifname[INTERFACE_NAMSIZ];
bf094f69
QY
2994 ifindex_t ifindex;
2995 int type;
2996 int af;
2997 union g_addr nexthop;
2998 uint32_t local_label;
2999 uint32_t remote_label;
3000 uint8_t flags;
3001 union pw_protocol_fields data;
3002 uint8_t protocol;
3003 struct zebra_pw *pw;
3004
3005 /* Get input stream. */
3006 s = msg;
3007
3008 /* Get data. */
3682bd90
RZ
3009 STREAM_GET(ifname, s, INTERFACE_NAMSIZ);
3010 ifname[INTERFACE_NAMSIZ - 1] = '\0';
bf094f69
QY
3011 STREAM_GETL(s, ifindex);
3012 STREAM_GETL(s, type);
3013 STREAM_GETL(s, af);
3014 switch (af) {
3015 case AF_INET:
3016 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
3017 break;
3018 case AF_INET6:
3019 STREAM_GET(&nexthop.ipv6, s, 16);
3020 break;
3021 default:
3022 return;
3023 }
3024 STREAM_GETL(s, local_label);
3025 STREAM_GETL(s, remote_label);
3026 STREAM_GETC(s, flags);
3027 STREAM_GET(&data, s, sizeof(data));
3028 protocol = client->proto;
3029
3030 pw = zebra_pw_find(zvrf, ifname);
3031 switch (hdr->command) {
3032 case ZEBRA_PW_ADD:
3033 if (pw) {
e914ccbe 3034 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
9df414fe 3035 "%s: pseudowire %s already exists [%s]",
bf094f69
QY
3036 __func__, ifname,
3037 zserv_command_string(hdr->command));
3038 return;
3039 }
3040
3041 zebra_pw_add(zvrf, ifname, protocol, client);
3042 break;
3043 case ZEBRA_PW_DELETE:
3044 if (!pw) {
e914ccbe 3045 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 3046 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
3047 ifname, zserv_command_string(hdr->command));
3048 return;
3049 }
3050
3051 zebra_pw_del(zvrf, pw);
3052 break;
3053 case ZEBRA_PW_SET:
3054 case ZEBRA_PW_UNSET:
3055 if (!pw) {
e914ccbe 3056 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 3057 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
3058 ifname, zserv_command_string(hdr->command));
3059 return;
3060 }
3061
3062 switch (hdr->command) {
3063 case ZEBRA_PW_SET:
3064 pw->enabled = 1;
3065 break;
3066 case ZEBRA_PW_UNSET:
3067 pw->enabled = 0;
3068 break;
3069 }
3070
3071 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
3072 remote_label, flags, &data);
3073 break;
3074 }
3075
3076stream_failure:
3077 return;
3078}
3079
3080static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
3081{
3082 struct interface *master;
3083 struct interface *slave;
3084 struct stream *s = msg;
3085 int ifindex;
3086 vrf_id_t vrf_id;
3087
3088 STREAM_GETL(s, vrf_id);
3089 STREAM_GETL(s, ifindex);
3090 master = if_lookup_by_index(ifindex, vrf_id);
3091
3092 STREAM_GETL(s, vrf_id);
3093 STREAM_GETL(s, ifindex);
3094 slave = if_lookup_by_index(ifindex, vrf_id);
3095
3096 if (!master || !slave)
3097 return;
3098
3099 kernel_interface_set_master(master, slave);
3100
3101stream_failure:
3102 return;
3103}
3104
3105
3106static void zread_vrf_label(ZAPI_HANDLER_ARGS)
3107{
3108 struct interface *ifp;
3109 mpls_label_t nlabel;
3110 afi_t afi;
3111 struct stream *s;
3112 struct zebra_vrf *def_zvrf;
3113 enum lsp_types_t ltype;
3114
3115 s = msg;
3116 STREAM_GETL(s, nlabel);
3117 STREAM_GETC(s, afi);
663d3a91
QY
3118
3119 if (!(IS_VALID_AFI(afi))) {
3120 zlog_warn("Invalid AFI for VRF label: %u", afi);
3121 return;
3122 }
3123
bf094f69
QY
3124 if (nlabel == zvrf->label[afi]) {
3125 /*
3126 * Nothing to do here move along
3127 */
3128 return;
3129 }
3130
3131 STREAM_GETC(s, ltype);
3132
3133 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
a36898e7 3134 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
bf094f69 3135 else
a36898e7 3136 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
bf094f69
QY
3137
3138 if (!ifp) {
3139 zlog_debug("Unable to find specified Interface for %s",
3140 zvrf->vrf->name);
3141 return;
3142 }
3143
3144 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
3145
3146 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
3147 afi_t scrubber;
3148 bool really_remove;
3149
3150 really_remove = true;
3151 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
3152 if (scrubber == afi)
3153 continue;
3154
3155 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
3156 continue;
3157
3158 if (zvrf->label[afi] == zvrf->label[scrubber]) {
3159 really_remove = false;
3160 break;
3161 }
3162 }
3163
3164 if (really_remove)
3165 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
3166 NEXTHOP_TYPE_IFINDEX, NULL,
cc1b9746 3167 ifp->ifindex, false /*backup*/);
bf094f69
QY
3168 }
3169
5065db0a
RW
3170 if (nlabel != MPLS_LABEL_NONE) {
3171 mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL;
3172 mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label,
3173 NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex);
3174 }
bf094f69
QY
3175
3176 zvrf->label[afi] = nlabel;
06302ecb
DS
3177 zvrf->label_proto[afi] = client->proto;
3178
bf094f69
QY
3179stream_failure:
3180 return;
3181}
3182
3183static inline void zread_rule(ZAPI_HANDLER_ARGS)
3184{
3185 struct zebra_pbr_rule zpr;
3186 struct stream *s;
3187 uint32_t total, i;
58a1d249 3188 char ifname[INTERFACE_NAMSIZ + 1] = {};
bf094f69
QY
3189
3190 s = msg;
3191 STREAM_GETL(s, total);
3192
3193 for (i = 0; i < total; i++) {
3194 memset(&zpr, 0, sizeof(zpr));
3195
3196 zpr.sock = client->sock;
3197 zpr.rule.vrf_id = hdr->vrf_id;
3198 STREAM_GETL(s, zpr.rule.seq);
3199 STREAM_GETL(s, zpr.rule.priority);
3200 STREAM_GETL(s, zpr.rule.unique);
f56697ef 3201 STREAM_GETC(s, zpr.rule.filter.ip_proto);
bf094f69
QY
3202 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
3203 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
3204 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
3205 prefix_blen(&zpr.rule.filter.src_ip));
3206 STREAM_GETW(s, zpr.rule.filter.src_port);
3207 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
3208 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
3209 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
3210 prefix_blen(&zpr.rule.filter.dst_ip));
3211 STREAM_GETW(s, zpr.rule.filter.dst_port);
01f23aff 3212 STREAM_GETC(s, zpr.rule.filter.dsfield);
bf094f69 3213 STREAM_GETL(s, zpr.rule.filter.fwmark);
d70a31a3
EB
3214
3215 STREAM_GETL(s, zpr.rule.action.queue_id);
3216 STREAM_GETW(s, zpr.rule.action.vlan_id);
3217 STREAM_GETW(s, zpr.rule.action.vlan_flags);
3218 STREAM_GETW(s, zpr.rule.action.pcp);
3219
bf094f69 3220 STREAM_GETL(s, zpr.rule.action.table);
58a1d249 3221 STREAM_GET(ifname, s, INTERFACE_NAMSIZ);
bf094f69 3222
58a1d249
DS
3223 strlcpy(zpr.ifname, ifname, sizeof(zpr.ifname));
3224 strlcpy(zpr.rule.ifname, ifname, sizeof(zpr.rule.ifname));
bf094f69
QY
3225
3226 if (!is_default_prefix(&zpr.rule.filter.src_ip))
3227 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
3228
3229 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
3230 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
3231
3232 if (zpr.rule.filter.src_port)
3233 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
3234
3235 if (zpr.rule.filter.dst_port)
3236 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
3237
01f23aff
WC
3238 if (zpr.rule.filter.dsfield)
3239 zpr.rule.filter.filter_bm |= PBR_FILTER_DSFIELD;
3240
f56697ef
DS
3241 if (zpr.rule.filter.ip_proto)
3242 zpr.rule.filter.filter_bm |= PBR_FILTER_IP_PROTOCOL;
3243
bf094f69
QY
3244 if (zpr.rule.filter.fwmark)
3245 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
3246
4719fd76
QY
3247 if (!(zpr.rule.filter.src_ip.family == AF_INET
3248 || zpr.rule.filter.src_ip.family == AF_INET6)) {
cc815be7 3249 zlog_warn(
6cde4b45 3250 "Unsupported PBR source IP family: %s (%hhu)",
cc815be7
QY
3251 family2str(zpr.rule.filter.src_ip.family),
3252 zpr.rule.filter.src_ip.family);
4719fd76
QY
3253 return;
3254 }
3255 if (!(zpr.rule.filter.dst_ip.family == AF_INET
3256 || zpr.rule.filter.dst_ip.family == AF_INET6)) {
cec72d49 3257 zlog_warn(
6cde4b45 3258 "Unsupported PBR destination IP family: %s (%hhu)",
cec72d49
DA
3259 family2str(zpr.rule.filter.dst_ip.family),
3260 zpr.rule.filter.dst_ip.family);
4719fd76
QY
3261 return;
3262 }
3263
3264
7f0ea8a4 3265 zpr.vrf_id = zvrf->vrf->vrf_id;
bf094f69 3266 if (hdr->command == ZEBRA_RULE_ADD)
7f0ea8a4 3267 zebra_pbr_add_rule(&zpr);
bf094f69 3268 else
7f0ea8a4 3269 zebra_pbr_del_rule(&zpr);
bf094f69
QY
3270 }
3271
3272stream_failure:
3273 return;
3274}
3275
dfacea4a
SY
3276static inline void zread_tc_qdisc(ZAPI_HANDLER_ARGS)
3277{
3278 struct zebra_tc_qdisc qdisc;
3279 struct stream *s;
3280 uint32_t total, i;
3281
3282 s = msg;
3283 STREAM_GETL(s, total);
3284
3285 for (i = 0; i < total; i++) {
3286 memset(&qdisc, 0, sizeof(qdisc));
3287
3288 qdisc.sock = client->sock;
3289 STREAM_GETL(s, qdisc.qdisc.ifindex);
3290 STREAM_GETL(s, qdisc.qdisc.kind);
3291
3292 if (hdr->command == ZEBRA_TC_QDISC_INSTALL)
3293 zebra_tc_qdisc_install(&qdisc);
3294 else
3295 zebra_tc_qdisc_uninstall(&qdisc);
3296 }
3297
3298stream_failure:
3299 return;
3300}
3301
3302static inline void zread_tc_class(ZAPI_HANDLER_ARGS)
3303{
3304 struct zebra_tc_class class;
3305 struct stream *s;
3306 uint32_t total, i;
3307
3308 s = msg;
3309 STREAM_GETL(s, total);
3310
3311 for (i = 0; i < total; i++) {
3312 memset(&class, 0, sizeof(class));
3313
3314 class.sock = client->sock;
3315 STREAM_GETL(s, class.class.ifindex);
3316 STREAM_GETL(s, class.class.handle);
3317 STREAM_GETL(s, class.class.kind);
3318 STREAM_GETQ(s, class.class.u.htb.rate);
3319 STREAM_GETQ(s, class.class.u.htb.ceil);
3320
3321 if (hdr->command == ZEBRA_TC_CLASS_ADD)
3322 zebra_tc_class_add(&class);
3323 else
3324 zebra_tc_class_delete(&class);
3325 }
3326
3327stream_failure:
3328 return;
3329}
3330
3331static inline void zread_tc_filter(ZAPI_HANDLER_ARGS)
3332{
3333 struct zebra_tc_filter filter;
3334 struct stream *s;
3335 uint32_t total, i;
3336
3337 s = msg;
3338 STREAM_GETL(s, total);
3339
3340 for (i = 0; i < total; i++) {
3341 memset(&filter, 0, sizeof(filter));
3342
3343 filter.sock = client->sock;
3344 STREAM_GETL(s, filter.filter.ifindex);
3345 STREAM_GETL(s, filter.filter.handle);
3346 STREAM_GETL(s, filter.filter.priority);
3347 STREAM_GETL(s, filter.filter.protocol);
3348 STREAM_GETL(s, filter.filter.kind);
3349 switch (filter.filter.kind) {
3350 case TC_FILTER_FLOWER: {
3351 STREAM_GETL(s, filter.filter.u.flower.filter_bm);
3352 uint32_t filter_bm = filter.filter.u.flower.filter_bm;
3353
3354 if (filter_bm & TC_FLOWER_IP_PROTOCOL)
3355 STREAM_GETC(s, filter.filter.u.flower.ip_proto);
3356 if (filter_bm & TC_FLOWER_SRC_IP) {
3357 STREAM_GETC(
3358 s,
3359 filter.filter.u.flower.src_ip.family);
3360 STREAM_GETC(s, filter.filter.u.flower.src_ip
3361 .prefixlen);
3362 STREAM_GET(
3363 &filter.filter.u.flower.src_ip.u.prefix,
3364 s,
3365 prefix_blen(&filter.filter.u.flower
3366 .src_ip));
3367
3368 if (!(filter.filter.u.flower.src_ip.family ==
3369 AF_INET ||
3370 filter.filter.u.flower.src_ip.family ==
3371 AF_INET6)) {
3372 zlog_warn(
3373 "Unsupported TC source IP family: %s (%hhu)",
3374 family2str(
3375 filter.filter.u.flower
3376 .src_ip.family),
3377 filter.filter.u.flower.src_ip
3378 .family);
3379 return;
3380 }
3381 }
3382 if (filter_bm & TC_FLOWER_SRC_PORT) {
3383 STREAM_GETW(
3384 s, filter.filter.u.flower.src_port_min);
3385 STREAM_GETW(
3386 s, filter.filter.u.flower.src_port_max);
3387 }
3388 if (filter_bm & TC_FLOWER_DST_IP) {
3389 STREAM_GETC(
3390 s,
3391 filter.filter.u.flower.dst_ip.family);
3392 STREAM_GETC(s, filter.filter.u.flower.dst_ip
3393 .prefixlen);
3394 STREAM_GET(
3395 &filter.filter.u.flower.dst_ip.u.prefix,
3396 s,
3397 prefix_blen(&filter.filter.u.flower
3398 .dst_ip));
3399 if (!(filter.filter.u.flower.dst_ip.family ==
3400 AF_INET ||
3401 filter.filter.u.flower.dst_ip.family ==
3402 AF_INET6)) {
3403 zlog_warn(
3404 "Unsupported TC destination IP family: %s (%hhu)",
3405 family2str(
3406 filter.filter.u.flower
3407 .dst_ip.family),
3408 filter.filter.u.flower.dst_ip
3409 .family);
3410 return;
3411 }
3412 }
3413 if (filter_bm & TC_FLOWER_DST_PORT) {
3414 STREAM_GETW(
3415 s, filter.filter.u.flower.dst_port_min);
3416 STREAM_GETW(
3417 s, filter.filter.u.flower.dst_port_max);
3418 }
3419 if (filter_bm & TC_FLOWER_DSFIELD) {
3420 STREAM_GETC(s, filter.filter.u.flower.dsfield);
3421 STREAM_GETC(
3422 s, filter.filter.u.flower.dsfield_mask);
3423 }
3424 STREAM_GETL(s, filter.filter.u.flower.classid);
3425 break;
3426 }
3427 default:
3428 break;
3429 }
3430
3431 if (hdr->command == ZEBRA_TC_FILTER_ADD)
3432 zebra_tc_filter_add(&filter);
3433 else
3434 zebra_tc_filter_delete(&filter);
3435 }
3436
3437stream_failure:
3438 return;
3439}
3440
bf094f69
QY
3441static inline void zread_ipset(ZAPI_HANDLER_ARGS)
3442{
3443 struct zebra_pbr_ipset zpi;
3444 struct stream *s;
3445 uint32_t total, i;
3446
3447 s = msg;
3448 STREAM_GETL(s, total);
3449
3450 for (i = 0; i < total; i++) {
3451 memset(&zpi, 0, sizeof(zpi));
3452
3453 zpi.sock = client->sock;
be2028d1 3454 zpi.vrf_id = zvrf->vrf->vrf_id;
bf094f69
QY
3455 STREAM_GETL(s, zpi.unique);
3456 STREAM_GETL(s, zpi.type);
a60b7031 3457 STREAM_GETC(s, zpi.family);
bf094f69
QY
3458 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
3459
3460 if (hdr->command == ZEBRA_IPSET_CREATE)
62f20a52 3461 zebra_pbr_create_ipset(&zpi);
bf094f69 3462 else
62f20a52 3463 zebra_pbr_destroy_ipset(&zpi);
bf094f69
QY
3464 }
3465
3466stream_failure:
3467 return;
3468}
3469
3470static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
3471{
3472 struct zebra_pbr_ipset_entry zpi;
3473 struct zebra_pbr_ipset ipset;
3474 struct stream *s;
3475 uint32_t total, i;
3476
3477 s = msg;
3478 STREAM_GETL(s, total);
3479
3480 for (i = 0; i < total; i++) {
3481 memset(&zpi, 0, sizeof(zpi));
3482 memset(&ipset, 0, sizeof(ipset));
3483
3484 zpi.sock = client->sock;
3485 STREAM_GETL(s, zpi.unique);
3486 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
261462c3 3487 ipset.ipset_name[ZEBRA_IPSET_NAME_SIZE - 1] = '\0';
bf094f69
QY
3488 STREAM_GETC(s, zpi.src.family);
3489 STREAM_GETC(s, zpi.src.prefixlen);
3490 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
3491 STREAM_GETC(s, zpi.dst.family);
3492 STREAM_GETC(s, zpi.dst.prefixlen);
3493 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
3494
25d760c5
PG
3495 STREAM_GETW(s, zpi.src_port_min);
3496 STREAM_GETW(s, zpi.src_port_max);
3497 STREAM_GETW(s, zpi.dst_port_min);
3498 STREAM_GETW(s, zpi.dst_port_max);
3499 STREAM_GETC(s, zpi.proto);
bf094f69
QY
3500 if (!is_default_prefix(&zpi.src))
3501 zpi.filter_bm |= PBR_FILTER_SRC_IP;
3502
3503 if (!is_default_prefix(&zpi.dst))
3504 zpi.filter_bm |= PBR_FILTER_DST_IP;
be729dd7 3505 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5 3506 zpi.filter_bm |= PBR_FILTER_DST_PORT;
be729dd7 3507 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5
PG
3508 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
3509 if (zpi.dst_port_max != 0)
3510 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
3511 if (zpi.src_port_max != 0)
3512 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
3513 if (zpi.proto != 0)
3514 zpi.filter_bm |= PBR_FILTER_PROTO;
bf094f69 3515
9863725c
QY
3516 if (!(zpi.dst.family == AF_INET
3517 || zpi.dst.family == AF_INET6)) {
cec72d49 3518 zlog_warn(
6cde4b45 3519 "Unsupported PBR destination IP family: %s (%hhu)",
cec72d49 3520 family2str(zpi.dst.family), zpi.dst.family);
9863725c
QY
3521 goto stream_failure;
3522 }
3523 if (!(zpi.src.family == AF_INET
3524 || zpi.src.family == AF_INET6)) {
cec72d49 3525 zlog_warn(
6cde4b45 3526 "Unsupported PBR source IP family: %s (%hhu)",
cec72d49 3527 family2str(zpi.src.family), zpi.src.family);
9863725c
QY
3528 goto stream_failure;
3529 }
3530
bf094f69 3531 /* calculate backpointer */
62f20a52
DS
3532 zpi.backpointer =
3533 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
f096bae4
DS
3534
3535 if (!zpi.backpointer) {
3536 zlog_warn("ipset name specified: %s does not exist",
3537 ipset.ipset_name);
3538 goto stream_failure;
3539 }
3540
bf094f69 3541 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
62f20a52 3542 zebra_pbr_add_ipset_entry(&zpi);
bf094f69 3543 else
62f20a52 3544 zebra_pbr_del_ipset_entry(&zpi);
bf094f69
QY
3545 }
3546
3547stream_failure:
3548 return;
3549}
3550
05657ec2 3551
7723e8d3
PG
3552static inline void zebra_neigh_register(ZAPI_HANDLER_ARGS)
3553{
3554 afi_t afi;
3555
3556 STREAM_GETW(msg, afi);
3557 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
3558 zlog_warn(
3559 "Invalid AFI %u while registering for neighbors notifications",
3560 afi);
3561 goto stream_failure;
3562 }
3563 vrf_bitmap_set(client->nhrp_neighinfo[afi], zvrf_id(zvrf));
3564stream_failure:
3565 return;
3566}
3567
3568static inline void zebra_neigh_unregister(ZAPI_HANDLER_ARGS)
3569{
3570 afi_t afi;
3571
3572 STREAM_GETW(msg, afi);
3573 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
3574 zlog_warn(
3575 "Invalid AFI %u while unregistering from neighbor notifications",
3576 afi);
3577 goto stream_failure;
3578 }
3579 vrf_bitmap_unset(client->nhrp_neighinfo[afi], zvrf_id(zvrf));
3580stream_failure:
3581 return;
3582}
3583
d17af8dd
PG
3584static inline void zebra_gre_get(ZAPI_HANDLER_ARGS)
3585{
3586 struct stream *s;
3587 ifindex_t idx;
3588 struct interface *ifp;
3589 struct zebra_if *zebra_if = NULL;
3590 struct zebra_l2info_gre *gre_info;
3591 struct interface *ifp_link = NULL;
3592 vrf_id_t vrf_id_link = VRF_UNKNOWN;
3593 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3594
3595 s = msg;
3596 STREAM_GETL(s, idx);
3597 ifp = if_lookup_by_index(idx, vrf_id);
3598
3599 if (ifp)
3600 zebra_if = ifp->info;
3601
3602 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
3603
3604 zclient_create_header(s, ZEBRA_GRE_UPDATE, vrf_id);
3605
3606 if (ifp && IS_ZEBRA_IF_GRE(ifp) && zebra_if) {
3607 gre_info = &zebra_if->l2info.gre;
3608
3609 stream_putl(s, idx);
3610 stream_putl(s, gre_info->ikey);
3611 stream_putl(s, gre_info->ikey);
3612 stream_putl(s, gre_info->ifindex_link);
3613
3614 ifp_link = if_lookup_by_index_per_ns(
3615 zebra_ns_lookup(gre_info->link_nsid),
3616 gre_info->ifindex_link);
3617 if (ifp_link)
096f7609 3618 vrf_id_link = ifp_link->vrf->vrf_id;
d17af8dd
PG
3619 stream_putl(s, vrf_id_link);
3620 stream_putl(s, gre_info->vtep_ip.s_addr);
3621 stream_putl(s, gre_info->vtep_ip_remote.s_addr);
3622 } else {
3623 stream_putl(s, idx);
3624 stream_putl(s, 0);
3625 stream_putl(s, 0);
3626 stream_putl(s, IFINDEX_INTERNAL);
3627 stream_putl(s, VRF_UNKNOWN);
3628 stream_putl(s, 0);
bcb68fe0 3629 stream_putl(s, 0);
d17af8dd
PG
3630 }
3631 /* Write packet size. */
3632 stream_putw_at(s, 0, stream_get_endp(s));
3633 zserv_send_message(client, s);
3634
3635 return;
3636 stream_failure:
3637 return;
3638}
3639
541025d6
PG
3640static inline void zebra_configure_arp(ZAPI_HANDLER_ARGS)
3641{
3642 struct stream *s;
e18747a9 3643 uint8_t fam;
541025d6
PG
3644 ifindex_t idx;
3645 struct interface *ifp;
541025d6
PG
3646
3647 s = msg;
3648 STREAM_GETC(s, fam);
3649 if (fam != AF_INET && fam != AF_INET6)
3650 return;
3651 STREAM_GETL(s, idx);
3652 ifp = if_lookup_by_index_per_ns(zvrf->zns, idx);
3653 if (!ifp)
3654 return;
e18747a9 3655 dplane_neigh_table_update(ifp, fam, 1, 0, 0);
541025d6
PG
3656stream_failure:
3657 return;
3658}
3659
d603c077 3660static inline void zebra_neigh_ip_add(ZAPI_HANDLER_ARGS)
05657ec2
PG
3661{
3662 struct stream *s;
d603c077 3663 struct zapi_neigh_ip api = {};
05657ec2 3664 int ret;
0a27a2fe 3665 const struct interface *ifp;
05657ec2
PG
3666
3667 s = msg;
d603c077 3668 ret = zclient_neigh_ip_decode(s, &api);
05657ec2
PG
3669 if (ret < 0)
3670 return;
0a27a2fe
PG
3671 ifp = if_lookup_by_index(api.index, zvrf_id(zvrf));
3672 if (!ifp)
3673 return;
3674 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_INSTALL, ifp, &api.ip_out,
d603c077 3675 &api.ip_in, api.ndm_state, client->proto);
05657ec2
PG
3676}
3677
0a27a2fe 3678
d603c077 3679static inline void zebra_neigh_ip_del(ZAPI_HANDLER_ARGS)
05657ec2
PG
3680{
3681 struct stream *s;
d603c077 3682 struct zapi_neigh_ip api = {};
05657ec2 3683 int ret;
0a27a2fe 3684 struct interface *ifp;
05657ec2 3685
05657ec2 3686 s = msg;
d603c077 3687 ret = zclient_neigh_ip_decode(s, &api);
05657ec2
PG
3688 if (ret < 0)
3689 return;
0a27a2fe
PG
3690 ifp = if_lookup_by_index(api.index, zvrf_id(zvrf));
3691 if (!ifp)
3692 return;
3693 dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_DELETE, ifp, &api.ip_out,
d603c077 3694 &api.ip_in, api.ndm_state, client->proto);
05657ec2
PG
3695}
3696
3697
bf094f69
QY
3698static inline void zread_iptable(ZAPI_HANDLER_ARGS)
3699{
8b5c4dce
QY
3700 struct zebra_pbr_iptable *zpi =
3701 XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
bf094f69
QY
3702 struct stream *s;
3703
3704 s = msg;
3705
8b5c4dce
QY
3706 zpi->interface_name_list = list_new();
3707 zpi->sock = client->sock;
3708 zpi->vrf_id = zvrf->vrf->vrf_id;
3709 STREAM_GETL(s, zpi->unique);
3710 STREAM_GETL(s, zpi->type);
3711 STREAM_GETL(s, zpi->filter_bm);
3712 STREAM_GETL(s, zpi->action);
3713 STREAM_GETL(s, zpi->fwmark);
3714 STREAM_GET(&zpi->ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
a60b7031 3715 STREAM_GETC(s, zpi->family);
8b5c4dce
QY
3716 STREAM_GETW(s, zpi->pkt_len_min);
3717 STREAM_GETW(s, zpi->pkt_len_max);
3718 STREAM_GETW(s, zpi->tcp_flags);
3719 STREAM_GETW(s, zpi->tcp_mask_flags);
3720 STREAM_GETC(s, zpi->dscp_value);
3721 STREAM_GETC(s, zpi->fragment);
3722 STREAM_GETC(s, zpi->protocol);
a60b7031 3723 STREAM_GETW(s, zpi->flow_label);
8b5c4dce
QY
3724 STREAM_GETL(s, zpi->nb_interface);
3725 zebra_pbr_iptable_update_interfacelist(s, zpi);
bf094f69
QY
3726
3727 if (hdr->command == ZEBRA_IPTABLE_ADD)
8b5c4dce 3728 zebra_pbr_add_iptable(zpi);
bf094f69 3729 else
8b5c4dce
QY
3730 zebra_pbr_del_iptable(zpi);
3731
bf094f69 3732stream_failure:
8b5c4dce
QY
3733 zebra_pbr_iptable_free(zpi);
3734 zpi = NULL;
bf094f69
QY
3735 return;
3736}
3737
d68e74b4
JU
3738static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS)
3739{
3740 struct stream *s;
3741 ifindex_t ifindex;
3742 struct interface *ifp;
3743 struct prefix p;
3744 struct ipaddr ip;
3745
3746 s = msg;
3747
3748 STREAM_GETL(s, ifindex);
3749
3750 ifp = if_lookup_by_index_per_ns(zvrf->zns, ifindex);
3751 if (!ifp) {
3752 zlog_debug("Failed to lookup ifindex: %u", ifindex);
3753 return;
3754 }
3755
3756 STREAM_GETC(s, p.family);
3757 STREAM_GETC(s, p.prefixlen);
3758 STREAM_GET(&p.u.prefix, s, prefix_blen(&p));
3759
3760 if (p.family == AF_INET)
3761 SET_IPADDR_V4(&ip);
3762 else
3763 SET_IPADDR_V6(&ip);
3764
3765 memcpy(&ip.ip.addr, &p.u.prefix, prefix_blen(&p));
3766
3767 dplane_neigh_discover(ifp, &ip);
3768
3769stream_failure:
3770 return;
3771}
3772
b716ab61
PG
3773static inline void zebra_gre_source_set(ZAPI_HANDLER_ARGS)
3774{
3775 struct stream *s;
3776 ifindex_t idx, link_idx;
3777 vrf_id_t link_vrf_id;
3778 struct interface *ifp;
3779 struct interface *ifp_link;
b716ab61 3780 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
62b4b7e4
PG
3781 struct zebra_if *zif, *gre_zif;
3782 struct zebra_l2info_gre *gre_info;
db51f0cd 3783 unsigned int mtu;
b716ab61
PG
3784
3785 s = msg;
3786 STREAM_GETL(s, idx);
3787 ifp = if_lookup_by_index(idx, vrf_id);
3788 STREAM_GETL(s, link_idx);
3789 STREAM_GETL(s, link_vrf_id);
db51f0cd 3790 STREAM_GETL(s, mtu);
62b4b7e4 3791
b716ab61
PG
3792 ifp_link = if_lookup_by_index(link_idx, link_vrf_id);
3793 if (!ifp_link || !ifp) {
3794 zlog_warn("GRE (index %u, VRF %u) or GRE link interface (index %u, VRF %u) not found, when setting GRE params",
3795 idx, vrf_id, link_idx, link_vrf_id);
3796 return;
3797 }
62b4b7e4
PG
3798
3799 if (!IS_ZEBRA_IF_GRE(ifp))
3800 return;
3801
3802 gre_zif = (struct zebra_if *)ifp->info;
3803 zif = (struct zebra_if *)ifp_link->info;
3804 if (!zif || !gre_zif)
3805 return;
3806
3807 gre_info = &zif->l2info.gre;
3808 if (!gre_info)
3809 return;
3810
db51f0cd
PG
3811 if (!mtu)
3812 mtu = ifp->mtu;
3813
3814 /* if gre link already set or mtu did not change, do not set it */
3815 if (gre_zif->link && gre_zif->link == ifp_link && mtu == ifp->mtu)
62b4b7e4
PG
3816 return;
3817
e3d3fa06 3818 dplane_gre_set(ifp, ifp_link, mtu, gre_info);
b716ab61
PG
3819
3820 stream_failure:
3821 return;
3822}
3823
9ab0b2a3
SW
3824static void zsend_error_msg(struct zserv *client, enum zebra_error_types error,
3825 struct zmsghdr *bad_hdr)
3826{
3827
3828 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
3829
3830 zclient_create_header(s, ZEBRA_ERROR, bad_hdr->vrf_id);
3831
3832 zserv_encode_error(s, error);
3833
3834 client->error_cnt++;
3835 zserv_send_message(client, s);
3836}
3837
3838static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS)
3839{
3840 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3841 zlog_debug("ZAPI message specifies unknown VRF: %d",
3842 hdr->vrf_id);
3843
5ee080f0 3844 zsend_error_msg(client, ZEBRA_NO_VRF, hdr);
9ab0b2a3
SW
3845}
3846
3847static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS)
3848{
3849 zlog_info("Zebra received unknown command %d", hdr->command);
3850
5ee080f0 3851 zsend_error_msg(client, ZEBRA_INVALID_MSG_TYPE, hdr);
9ab0b2a3
SW
3852}
3853
2b64873d 3854void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
bf094f69
QY
3855 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
3856 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
3857 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
3858 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
c3bd894e 3859 [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown,
bf094f69
QY
3860 [ZEBRA_ROUTE_ADD] = zread_route_add,
3861 [ZEBRA_ROUTE_DELETE] = zread_route_del,
bf094f69
QY
3862 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
3863 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
3864 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
3865 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
34ee41c6 3866 [ZEBRA_NEXTHOP_LOOKUP_MRIB] = zread_nexthop_lookup_mrib,
bf094f69
QY
3867 [ZEBRA_HELLO] = zread_hello,
3868 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
3869 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
bf094f69
QY
3870 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
3871 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
3872 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
d3af6147
RZ
3873#if HAVE_BFDD > 0
3874 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
3875#endif /* HAVE_BFDD */
bf094f69
QY
3876 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
3877 [ZEBRA_VRF_LABEL] = zread_vrf_label,
3878 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
bf094f69
QY
3879 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
3880 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
31f937fb
SM
3881 [ZEBRA_SR_POLICY_SET] = zread_sr_policy_set,
3882 [ZEBRA_SR_POLICY_DELETE] = zread_sr_policy_delete,
ea6b290b
RW
3883 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add,
3884 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete,
3885 [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace,
bf094f69
QY
3886 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
3887 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
f533be73 3888 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
bf094f69
QY
3889 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
3890 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
3891 [ZEBRA_FEC_REGISTER] = zread_fec_register,
3892 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
3893 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
278e26de 3894 [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip,
bf094f69
QY
3895 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
3896 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
ce5160c0
AK
3897 [ZEBRA_REMOTE_ES_VTEP_ADD] = zebra_evpn_proc_remote_es,
3898 [ZEBRA_REMOTE_ES_VTEP_DEL] = zebra_evpn_proc_remote_es,
7e5b0b2b
MS
3899 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add_zapi,
3900 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del_zapi,
bf094f69
QY
3901 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
3902 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
3950b52c 3903 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
bf094f69
QY
3904 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
3905 [ZEBRA_PW_ADD] = zread_pseudowire,
3906 [ZEBRA_PW_DELETE] = zread_pseudowire,
3907 [ZEBRA_PW_SET] = zread_pseudowire,
3908 [ZEBRA_PW_UNSET] = zread_pseudowire,
3909 [ZEBRA_RULE_ADD] = zread_rule,
3910 [ZEBRA_RULE_DELETE] = zread_rule,
3911 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
3912 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
3913 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
3914 [ZEBRA_IPSET_CREATE] = zread_ipset,
3915 [ZEBRA_IPSET_DESTROY] = zread_ipset,
3916 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
3917 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
3918 [ZEBRA_IPTABLE_ADD] = zread_iptable,
3919 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
fbac9605 3920 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
ecbbc3a7 3921 [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay,
ee235396
SK
3922 [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register,
3923 [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister,
3924 [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg,
6e68a084
HS
3925 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] = zread_srv6_manager_request,
3926 [ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK] = zread_srv6_manager_request,
d68e74b4 3927 [ZEBRA_CLIENT_CAPABILITIES] = zread_client_capabilities,
2f35a820 3928 [ZEBRA_NEIGH_DISCOVER] = zread_neigh_discover,
8b2d3a0f 3929 [ZEBRA_NHG_ADD] = zread_nhg_add,
2f35a820 3930 [ZEBRA_NHG_DEL] = zread_nhg_del,
77b38a4a 3931 [ZEBRA_ROUTE_NOTIFY_REQUEST] = zread_route_notify_request,
7bfa7d02
AK
3932 [ZEBRA_EVPN_REMOTE_NH_ADD] = zebra_evpn_proc_remote_nh,
3933 [ZEBRA_EVPN_REMOTE_NH_DEL] = zebra_evpn_proc_remote_nh,
d603c077
PG
3934 [ZEBRA_NEIGH_IP_ADD] = zebra_neigh_ip_add,
3935 [ZEBRA_NEIGH_IP_DEL] = zebra_neigh_ip_del,
7723e8d3
PG
3936 [ZEBRA_NHRP_NEIGH_REGISTER] = zebra_neigh_register,
3937 [ZEBRA_NHRP_NEIGH_UNREGISTER] = zebra_neigh_unregister,
541025d6 3938 [ZEBRA_CONFIGURE_ARP] = zebra_configure_arp,
d17af8dd 3939 [ZEBRA_GRE_GET] = zebra_gre_get,
b716ab61 3940 [ZEBRA_GRE_SOURCE_SET] = zebra_gre_source_set,
dfacea4a
SY
3941 [ZEBRA_TC_QDISC_INSTALL] = zread_tc_qdisc,
3942 [ZEBRA_TC_QDISC_UNINSTALL] = zread_tc_qdisc,
3943 [ZEBRA_TC_CLASS_ADD] = zread_tc_class,
3944 [ZEBRA_TC_CLASS_DELETE] = zread_tc_class,
3945 [ZEBRA_TC_FILTER_ADD] = zread_tc_filter,
3946 [ZEBRA_TC_FILTER_DELETE] = zread_tc_filter,
2f35a820 3947};
bf094f69 3948
79b3664a
MS
3949/*
3950 * Process a batch of zapi messages.
3951 */
3952void zserv_handle_commands(struct zserv *client, struct stream_fifo *fifo)
bf094f69 3953{
904e0d88
QY
3954 struct zmsghdr hdr;
3955 struct zebra_vrf *zvrf;
79b3664a
MS
3956 struct stream *msg;
3957 struct stream_fifo temp_fifo;
904e0d88 3958
79b3664a 3959 stream_fifo_init(&temp_fifo);
aa8cb964 3960
79b3664a
MS
3961 while (stream_fifo_head(fifo)) {
3962 msg = stream_fifo_pop(fifo);
bf094f69 3963
79b3664a
MS
3964 if (STREAM_READABLE(msg) > ZEBRA_MAX_PACKET_SIZ) {
3965 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3966 zlog_debug(
3967 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3968 STREAM_READABLE(msg),
3969 ZEBRA_MAX_PACKET_SIZ);
3970 goto continue_loop;
3971 }
3972
3973 zapi_parse_header(msg, &hdr);
3974
ec64a77b
DS
3975 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV
3976 && IS_ZEBRA_DEBUG_DETAIL)
79b3664a 3977 zserv_log_message(NULL, msg, &hdr);
49b3b01f 3978
79b3664a 3979 hdr.length -= ZEBRA_HEADER_SIZE;
904e0d88 3980
79b3664a
MS
3981 /* Before checking for a handler function, check for
3982 * special messages that are handled in another module;
3983 * we'll treat these as opaque.
3984 */
3985 if (zebra_opaque_handles_msgid(hdr.command)) {
3986 /* Reset message buffer */
3987 stream_set_getp(msg, 0);
3988
3989 stream_fifo_push(&temp_fifo, msg);
3990
3991 /* Continue without freeing the message */
3992 msg = NULL;
3993 goto continue_loop;
3994 }
3995
3996 /* lookup vrf */
3997 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
3998 if (!zvrf) {
3999 zserv_error_no_vrf(client, &hdr, msg, zvrf);
4000 goto continue_loop;
4001 }
4002
4003 if (hdr.command >= array_size(zserv_handlers)
4004 || zserv_handlers[hdr.command] == NULL) {
4005 zserv_error_invalid_msg_type(client, &hdr, msg, zvrf);
4006 goto continue_loop;
4007 }
4008
4009 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
4010
4011continue_loop:
4012 stream_free(msg);
4013 }
904e0d88 4014
79b3664a
MS
4015 /* Dispatch any special messages from the temp fifo */
4016 if (stream_fifo_head(&temp_fifo) != NULL)
4017 zebra_opaque_enqueue_batch(&temp_fifo);
9ab0b2a3 4018
79b3664a 4019 stream_fifo_deinit(&temp_fifo);
bf094f69 4020}