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