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