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