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