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