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