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