]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zapi_msg.c
Merge pull request #3605 from donaldsharp/bfd_24_minutes
[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. */
212 if (!client->ifinfo) {
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);
1327 zsend_interface_addresses(client, ifp);
1328 }
1329 }
1330}
1331
1332/* Unregister zebra server interface information. */
1333static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1334{
1335 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1336}
1337
1338void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1339 const unsigned int nexthop_num)
1340{
1341 if (nexthop_num > multipath_num) {
1342 char buff[PREFIX2STR_BUFFER];
8b1766b1 1343
bf094f69 1344 prefix2str(p, buff, sizeof(buff));
9df414fe 1345 flog_warn(
e914ccbe 1346 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
bf094f69
QY
1347 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1348 caller, buff, nexthop_num, multipath_num);
1349 }
1350}
1351
1352static void zread_route_add(ZAPI_HANDLER_ARGS)
1353{
1354 struct stream *s;
1355 struct zapi_route api;
1356 struct zapi_nexthop *api_nh;
1357 afi_t afi;
1358 struct prefix_ipv6 *src_p = NULL;
1359 struct route_entry *re;
1360 struct nexthop *nexthop = NULL;
1361 int i, ret;
1362 vrf_id_t vrf_id = 0;
1363 struct ipaddr vtep_ip;
1364
1365 s = msg;
1366 if (zapi_route_decode(s, &api) < 0) {
1367 if (IS_ZEBRA_DEBUG_RECV)
1368 zlog_debug("%s: Unable to decode zapi_route sent",
1369 __PRETTY_FUNCTION__);
1370 return;
1371 }
1372
1373 if (IS_ZEBRA_DEBUG_RECV) {
1374 char buf_prefix[PREFIX_STRLEN];
8b1766b1 1375
bf094f69
QY
1376 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1377 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1378 __func__, buf_prefix,
1379 (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL) ? ""
1380 : "un"),
1381 api.flags);
1382 }
1383
1384 /* Allocate new route. */
1385 vrf_id = zvrf_id(zvrf);
1386 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1387 re->type = api.type;
1388 re->instance = api.instance;
1389 re->flags = api.flags;
1390 re->uptime = time(NULL);
1391 re->vrf_id = vrf_id;
1392 if (api.tableid && vrf_id == VRF_DEFAULT)
1393 re->table = api.tableid;
1394 else
1395 re->table = zvrf->table_id;
1396
1397 /*
1398 * TBD should _all_ of the nexthop add operations use
1399 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1400 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1401 */
8b1766b1 1402 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP))
bf094f69
QY
1403 for (i = 0; i < api.nexthop_num; i++) {
1404 api_nh = &api.nexthops[i];
1405 ifindex_t ifindex = 0;
1406
8b1766b1 1407 if (IS_ZEBRA_DEBUG_RECV)
bf094f69 1408 zlog_debug("nh type %d", api_nh->type);
bf094f69
QY
1409
1410 switch (api_nh->type) {
1411 case NEXTHOP_TYPE_IFINDEX:
1412 nexthop = route_entry_nexthop_ifindex_add(
1413 re, api_nh->ifindex, api_nh->vrf_id);
1414 break;
1415 case NEXTHOP_TYPE_IPV4:
1416 if (IS_ZEBRA_DEBUG_RECV) {
1417 char nhbuf[INET6_ADDRSTRLEN] = {0};
8b1766b1 1418
bf094f69
QY
1419 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1420 nhbuf, INET6_ADDRSTRLEN);
1421 zlog_debug("%s: nh=%s, vrf_id=%d",
1422 __func__, nhbuf,
1423 api_nh->vrf_id);
1424 }
1425 nexthop = route_entry_nexthop_ipv4_add(
1426 re, &api_nh->gate.ipv4, NULL,
1427 api_nh->vrf_id);
1428 break;
1429 case NEXTHOP_TYPE_IPV4_IFINDEX:
1430
1431 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1432 if (CHECK_FLAG(api.flags,
1433 ZEBRA_FLAG_EVPN_ROUTE)) {
1434 ifindex = get_l3vni_svi_ifindex(vrf_id);
1435 } else {
1436 ifindex = api_nh->ifindex;
1437 }
1438
1439 if (IS_ZEBRA_DEBUG_RECV) {
1440 char nhbuf[INET6_ADDRSTRLEN] = {0};
8b1766b1 1441
bf094f69
QY
1442 inet_ntop(AF_INET, &api_nh->gate.ipv4,
1443 nhbuf, INET6_ADDRSTRLEN);
1444 zlog_debug(
1445 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1446 __func__, nhbuf, api_nh->vrf_id,
1447 re->vrf_id, ifindex);
1448 }
1449 nexthop = route_entry_nexthop_ipv4_ifindex_add(
1450 re, &api_nh->gate.ipv4, NULL, ifindex,
1451 api_nh->vrf_id);
1452
1453 /* if this an EVPN route entry,
1454 * program the nh as neigh
1455 */
1456 if (CHECK_FLAG(api.flags,
1457 ZEBRA_FLAG_EVPN_ROUTE)) {
1458 SET_FLAG(nexthop->flags,
1459 NEXTHOP_FLAG_EVPN_RVTEP);
1460 vtep_ip.ipa_type = IPADDR_V4;
1461 memcpy(&(vtep_ip.ipaddr_v4),
1462 &(api_nh->gate.ipv4),
1463 sizeof(struct in_addr));
1464 zebra_vxlan_evpn_vrf_route_add(
a317a9b9 1465 vrf_id, &api_nh->rmac, &vtep_ip,
bf094f69
QY
1466 &api.prefix);
1467 }
1468 break;
1469 case NEXTHOP_TYPE_IPV6:
1470 nexthop = route_entry_nexthop_ipv6_add(
1471 re, &api_nh->gate.ipv6, api_nh->vrf_id);
1472 break;
1473 case NEXTHOP_TYPE_IPV6_IFINDEX:
1474 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1475 if (CHECK_FLAG(api.flags,
1476 ZEBRA_FLAG_EVPN_ROUTE)) {
1477 ifindex = get_l3vni_svi_ifindex(vrf_id);
1478 } else {
1479 ifindex = api_nh->ifindex;
1480 }
1481
1482 nexthop = route_entry_nexthop_ipv6_ifindex_add(
1483 re, &api_nh->gate.ipv6, ifindex,
1484 api_nh->vrf_id);
1485
1486 /* if this an EVPN route entry,
1487 * program the nh as neigh
1488 */
1489 if (CHECK_FLAG(api.flags,
1490 ZEBRA_FLAG_EVPN_ROUTE)) {
1491 SET_FLAG(nexthop->flags,
1492 NEXTHOP_FLAG_EVPN_RVTEP);
1493 vtep_ip.ipa_type = IPADDR_V6;
1494 memcpy(&vtep_ip.ipaddr_v6,
1495 &(api_nh->gate.ipv6),
1496 sizeof(struct in6_addr));
1497 zebra_vxlan_evpn_vrf_route_add(
a317a9b9 1498 vrf_id, &api_nh->rmac, &vtep_ip,
bf094f69
QY
1499 &api.prefix);
1500 }
1501 break;
1502 case NEXTHOP_TYPE_BLACKHOLE:
1503 nexthop = route_entry_nexthop_blackhole_add(
1504 re, api_nh->bh_type);
1505 break;
1506 }
1507
1508 if (!nexthop) {
9df414fe 1509 flog_warn(
e914ccbe 1510 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
bf094f69
QY
1511 "%s: Nexthops Specified: %d but we failed to properly create one",
1512 __PRETTY_FUNCTION__, api.nexthop_num);
1513 nexthops_free(re->ng.nexthop);
1514 XFREE(MTYPE_RE, re);
1515 return;
1516 }
1517 /* MPLS labels for BGP-LU or Segment Routing */
1518 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1519 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1520 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1521 enum lsp_types_t label_type;
1522
1523 label_type =
1524 lsp_type_from_re_type(client->proto);
1525
1526 if (IS_ZEBRA_DEBUG_RECV) {
1527 zlog_debug(
1528 "%s: adding %d labels of type %d (1st=%u)",
1529 __func__, api_nh->label_num,
1530 label_type, api_nh->labels[0]);
1531 }
1532
1533 nexthop_add_labels(nexthop, label_type,
1534 api_nh->label_num,
1535 &api_nh->labels[0]);
1536 }
1537 }
bf094f69
QY
1538
1539 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1540 re->distance = api.distance;
1541 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1542 re->metric = api.metric;
1543 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1544 re->tag = api.tag;
1545 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1546 re->mtu = api.mtu;
1547
1548 afi = family2afi(api.prefix.family);
1549 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 1550 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 1551 "%s: Received SRC Prefix but afi is not v6",
bf094f69
QY
1552 __PRETTY_FUNCTION__);
1553 nexthops_free(re->ng.nexthop);
1554 XFREE(MTYPE_RE, re);
1555 return;
1556 }
1557 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1558 src_p = &api.src_prefix;
1559
1560 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1561
1562 /* Stats */
1563 switch (api.prefix.family) {
1564 case AF_INET:
1565 if (ret > 0)
1566 client->v4_route_add_cnt++;
1567 else if (ret < 0)
1568 client->v4_route_upd8_cnt++;
1569 break;
1570 case AF_INET6:
1571 if (ret > 0)
1572 client->v6_route_add_cnt++;
1573 else if (ret < 0)
1574 client->v6_route_upd8_cnt++;
1575 break;
1576 }
1577}
1578
1579static void zread_route_del(ZAPI_HANDLER_ARGS)
1580{
1581 struct stream *s;
1582 struct zapi_route api;
1583 afi_t afi;
1584 struct prefix_ipv6 *src_p = NULL;
1585 uint32_t table_id;
1586
1587 s = msg;
1588 if (zapi_route_decode(s, &api) < 0)
1589 return;
1590
1591 afi = family2afi(api.prefix.family);
1592 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
e914ccbe 1593 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
9df414fe 1594 "%s: Received a src prefix while afi is not v6",
bf094f69
QY
1595 __PRETTY_FUNCTION__);
1596 return;
1597 }
1598 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1599 src_p = &api.src_prefix;
1600
1601 if (api.vrf_id == VRF_DEFAULT && api.tableid != 0)
1602 table_id = api.tableid;
1603 else
1604 table_id = zvrf->table_id;
1605
1606 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1607 api.flags, &api.prefix, src_p, NULL, table_id, api.metric,
40ecd8e4 1608 api.distance, false);
bf094f69
QY
1609
1610 /* Stats */
1611 switch (api.prefix.family) {
1612 case AF_INET:
1613 client->v4_route_del_cnt++;
1614 break;
1615 case AF_INET6:
1616 client->v6_route_del_cnt++;
1617 break;
1618 }
1619}
1620
bf094f69
QY
1621/* MRIB Nexthop lookup for IPv4. */
1622static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1623{
1624 struct in_addr addr;
1625 struct route_entry *re;
1626
1627 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1628 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1629 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1630
1631stream_failure:
1632 return;
1633}
1634
bf094f69
QY
1635/* Register zebra server router-id information. Send current router-id */
1636static void zread_router_id_add(ZAPI_HANDLER_ARGS)
1637{
1638 struct prefix p;
1639
1640 /* Router-id information is needed. */
1641 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
1642
1643 router_id_get(&p, zvrf_id(zvrf));
1644
1645 zsend_router_id_update(client, &p, zvrf_id(zvrf));
1646}
1647
1648/* Unregister zebra server router-id information. */
1649static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
1650{
1651 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1652}
1653
1654static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
1655{
1656 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1657
1658 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
1659 stream_putc(s, mpls_enabled);
1660 stream_putl(s, multipath_num);
02c0866d 1661 stream_putc(s, zebra_mlag_get_role());
bf094f69
QY
1662
1663 stream_putw_at(s, 0, stream_get_endp(s));
21ccc0cf 1664 zserv_send_message(client, s);
bf094f69
QY
1665}
1666
1667/* Tie up route-type and client->sock */
1668static void zread_hello(ZAPI_HANDLER_ARGS)
1669{
1670 /* type of protocol (lib/zebra.h) */
1671 uint8_t proto;
1672 unsigned short instance;
1673 uint8_t notify;
1674
1675 STREAM_GETC(msg, proto);
1676 STREAM_GETW(msg, instance);
1677 STREAM_GETC(msg, notify);
1678 if (notify)
1679 client->notify_owner = true;
1680
1681 /* accept only dynamic routing protocols */
f23cbcda 1682 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
bf094f69
QY
1683 zlog_notice(
1684 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1685 client->sock, zebra_route_string(proto),
1686 zvrf->vrf->vrf_id);
1687 if (instance)
1688 zlog_notice("client protocol instance %d", instance);
1689
1690 client->proto = proto;
1691 client->instance = instance;
1692 }
1693
1694 zsend_capabilities(client, zvrf);
1695stream_failure:
1696 return;
1697}
1698
1699/* Unregister all information in a VRF. */
1700static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
1701{
1702 int i;
1703 afi_t afi;
1704
1705 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1706 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1707 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1708 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
1709 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1710 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1711}
1712
1713static void zread_mpls_labels(ZAPI_HANDLER_ARGS)
1714{
1715 struct stream *s;
1716 enum lsp_types_t type;
1717 struct prefix prefix;
1718 enum nexthop_types_t gtype;
1719 union g_addr gate;
1720 ifindex_t ifindex;
1721 mpls_label_t in_label, out_label;
1722 uint8_t distance;
1723
1724 /* Get input stream. */
1725 s = msg;
1726
1727 /* Get data. */
1728 STREAM_GETC(s, type);
1729 STREAM_GETL(s, prefix.family);
1730 switch (prefix.family) {
1731 case AF_INET:
1732 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1733 STREAM_GETC(s, prefix.prefixlen);
1734 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
9df414fe 1735 zlog_debug(
bf094f69
QY
1736 "%s: Specified prefix length %d is greater than a v4 address can support",
1737 __PRETTY_FUNCTION__, prefix.prefixlen);
1738 return;
1739 }
1740 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
1741 break;
1742 case AF_INET6:
1743 STREAM_GET(&prefix.u.prefix6, s, 16);
1744 STREAM_GETC(s, prefix.prefixlen);
1745 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
9df414fe 1746 zlog_debug(
bf094f69
QY
1747 "%s: Specified prefix length %d is greater than a v6 address can support",
1748 __PRETTY_FUNCTION__, prefix.prefixlen);
1749 return;
1750 }
1751 STREAM_GET(&gate.ipv6, s, 16);
1752 break;
1753 default:
9df414fe
QY
1754 zlog_debug("%s: Specified AF %d is not supported for this call",
1755 __PRETTY_FUNCTION__, prefix.family);
bf094f69
QY
1756 return;
1757 }
1758 STREAM_GETL(s, ifindex);
1759 STREAM_GETC(s, distance);
1760 STREAM_GETL(s, in_label);
1761 STREAM_GETL(s, out_label);
1762
1763 switch (prefix.family) {
1764 case AF_INET:
1765 if (ifindex)
1766 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1767 else
1768 gtype = NEXTHOP_TYPE_IPV4;
1769 break;
1770 case AF_INET6:
1771 if (ifindex)
1772 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1773 else
1774 gtype = NEXTHOP_TYPE_IPV6;
1775 break;
1776 default:
1777 return;
1778 }
1779
1780 if (!mpls_enabled)
1781 return;
1782
1783 if (hdr->command == ZEBRA_MPLS_LABELS_ADD) {
1784 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
1785 ifindex);
1786 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
1787 distance, out_label);
1788 } else if (hdr->command == ZEBRA_MPLS_LABELS_DELETE) {
1789 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
1790 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
1791 distance, out_label);
1792 }
1793stream_failure:
1794 return;
1795}
1796
1797/* Send response to a table manager connect request to client */
1798static void zread_table_manager_connect(struct zserv *client,
1799 struct stream *msg, vrf_id_t vrf_id)
1800{
1801 struct stream *s;
1802 uint8_t proto;
1803 uint16_t instance;
1804
1805 s = msg;
1806
1807 /* Get data. */
1808 STREAM_GETC(s, proto);
1809 STREAM_GETW(s, instance);
1810
1811 /* accept only dynamic routing protocols */
1812 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 1813 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
1814 "client %d has wrong protocol %s", client->sock,
1815 zebra_route_string(proto));
bf094f69
QY
1816 zsend_table_manager_connect_response(client, vrf_id, 1);
1817 return;
1818 }
1819 zlog_notice("client %d with vrf %u instance %u connected as %s",
1820 client->sock, vrf_id, instance, zebra_route_string(proto));
1821 client->proto = proto;
1822 client->instance = instance;
1823
1824 /*
1825 * Release previous labels of same protocol and instance.
1826 * This is done in case it restarted from an unexpected shutdown.
1827 */
453844ab 1828 release_daemon_table_chunks(client);
bf094f69
QY
1829
1830 zsend_table_manager_connect_response(client, vrf_id, 0);
1831
1832stream_failure:
1833 return;
1834}
1835
1836static void zread_label_manager_connect(struct zserv *client,
1837 struct stream *msg, vrf_id_t vrf_id)
1838{
1839 struct stream *s;
1840 /* type of protocol (lib/zebra.h) */
1841 uint8_t proto;
1842 unsigned short instance;
1843
1844 /* Get input stream. */
1845 s = msg;
1846
1847 /* Get data. */
1848 STREAM_GETC(s, proto);
1849 STREAM_GETW(s, instance);
1850
1851 /* accept only dynamic routing protocols */
1852 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
e914ccbe 1853 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1c50c1c0
QY
1854 "client %d has wrong protocol %s", client->sock,
1855 zebra_route_string(proto));
f533be73 1856 if (client->is_synchronous)
1857 zsend_label_manager_connect_response(client, vrf_id, 1);
bf094f69
QY
1858 return;
1859 }
1860 zlog_notice("client %d with vrf %u instance %u connected as %s",
1861 client->sock, vrf_id, instance, zebra_route_string(proto));
1862 client->proto = proto;
1863 client->instance = instance;
1864
1865 /*
453844ab
QY
1866 * Release previous labels of same protocol and instance.
1867 * This is done in case it restarted from an unexpected shutdown.
1868 */
1869 release_daemon_label_chunks(client);
bf094f69
QY
1870
1871 zlog_debug(
1872 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1873 client->sock, zebra_route_string(proto), vrf_id, instance);
1874 /* send response back */
f533be73 1875 if (client->is_synchronous)
1876 zsend_label_manager_connect_response(client, vrf_id, 0);
bf094f69
QY
1877
1878stream_failure:
1879 return;
1880}
1881
1882static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
1883 vrf_id_t vrf_id)
1884{
1885 struct stream *s;
1886 uint8_t keep;
1887 uint32_t size;
1888 struct label_manager_chunk *lmc;
5dffb0e9
FR
1889 uint8_t proto;
1890 unsigned short instance;
bf094f69
QY
1891
1892 /* Get input stream. */
1893 s = msg;
1894
1895 /* Get data. */
5dffb0e9
FR
1896 STREAM_GETC(s, proto);
1897 STREAM_GETW(s, instance);
bf094f69
QY
1898 STREAM_GETC(s, keep);
1899 STREAM_GETL(s, size);
1900
f533be73 1901 lmc = assign_label_chunk(proto, instance, keep, size);
bf094f69 1902 if (!lmc)
af4c2728 1903 flog_err(
e914ccbe 1904 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK,
0313523d 1905 "Unable to assign Label Chunk of size %u to %s instance %u",
f533be73 1906 size, zebra_route_string(proto), instance);
bf094f69 1907 else
0313523d
FR
1908 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1909 lmc->start, lmc->end,
f533be73 1910 zebra_route_string(proto), instance);
bf094f69
QY
1911 /* send response back */
1912 zsend_assign_label_chunk_response(client, vrf_id, lmc);
1913
1914stream_failure:
1915 return;
1916}
1917
1918static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
1919{
1920 struct stream *s;
1921 uint32_t start, end;
5dffb0e9
FR
1922 uint8_t proto;
1923 unsigned short instance;
bf094f69
QY
1924
1925 /* Get input stream. */
1926 s = msg;
1927
1928 /* Get data. */
5dffb0e9
FR
1929 STREAM_GETC(s, proto);
1930 STREAM_GETW(s, instance);
bf094f69
QY
1931 STREAM_GETL(s, start);
1932 STREAM_GETL(s, end);
1933
f533be73 1934 release_label_chunk(proto, instance, start, end);
bf094f69
QY
1935
1936stream_failure:
1937 return;
1938}
1939static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
1940{
1941 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
f533be73 1942 client->is_synchronous = hdr->command ==
1943 ZEBRA_LABEL_MANAGER_CONNECT;
bf094f69
QY
1944
1945 /* external label manager */
1946 if (lm_is_external)
0313523d 1947 zread_relay_label_manager_request(hdr->command, client, msg,
bf094f69
QY
1948 zvrf_id(zvrf));
1949 /* this is a label manager */
1950 else {
f533be73 1951 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT ||
1952 hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
bf094f69
QY
1953 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
1954 else {
bf094f69
QY
1955 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
1956 zread_get_label_chunk(client, msg,
1957 zvrf_id(zvrf));
1958 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
1959 zread_release_label_chunk(client, msg);
1960 }
1961 }
1962}
1963
1964static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
1965 vrf_id_t vrf_id)
1966{
1967 struct stream *s;
1968 uint32_t size;
1969 struct table_manager_chunk *tmc;
1970
1971 /* Get input stream. */
1972 s = msg;
1973
1974 /* Get data. */
1975 STREAM_GETL(s, size);
1976
1977 tmc = assign_table_chunk(client->proto, client->instance, size);
1978 if (!tmc)
e914ccbe 1979 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
1c50c1c0
QY
1980 "%s: Unable to assign Table Chunk of size %u",
1981 __func__, size);
bf094f69
QY
1982 else
1983 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
1984 tmc->end);
1985 /* send response back */
1986 zsend_assign_table_chunk_response(client, vrf_id, tmc);
1987
1988stream_failure:
1989 return;
1990}
1991
1992static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
1993{
1994 struct stream *s;
1995 uint32_t start, end;
1996
1997 /* Get input stream. */
1998 s = msg;
1999
2000 /* Get data. */
2001 STREAM_GETL(s, start);
2002 STREAM_GETL(s, end);
2003
2004 release_table_chunk(client->proto, client->instance, start, end);
2005
2006stream_failure:
2007 return;
2008}
2009
2010static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2011{
2012 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2013 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2014 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2015 else {
2016 /* Sanity: don't allow 'unidentified' requests */
2017 if (!client->proto) {
af4c2728 2018 flog_err(
e914ccbe 2019 EC_ZEBRA_TM_ALIENS,
bf094f69
QY
2020 "Got table request from an unidentified client");
2021 return;
2022 }
2023 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2024 zread_get_table_chunk(client, msg, zvrf_id(zvrf));
2025 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2026 zread_release_table_chunk(client, msg);
2027 }
2028}
2029
2030static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2031{
2032 struct stream *s;
2033 char ifname[IF_NAMESIZE];
2034 ifindex_t ifindex;
2035 int type;
2036 int af;
2037 union g_addr nexthop;
2038 uint32_t local_label;
2039 uint32_t remote_label;
2040 uint8_t flags;
2041 union pw_protocol_fields data;
2042 uint8_t protocol;
2043 struct zebra_pw *pw;
2044
2045 /* Get input stream. */
2046 s = msg;
2047
2048 /* Get data. */
2049 STREAM_GET(ifname, s, IF_NAMESIZE);
2050 STREAM_GETL(s, ifindex);
2051 STREAM_GETL(s, type);
2052 STREAM_GETL(s, af);
2053 switch (af) {
2054 case AF_INET:
2055 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2056 break;
2057 case AF_INET6:
2058 STREAM_GET(&nexthop.ipv6, s, 16);
2059 break;
2060 default:
2061 return;
2062 }
2063 STREAM_GETL(s, local_label);
2064 STREAM_GETL(s, remote_label);
2065 STREAM_GETC(s, flags);
2066 STREAM_GET(&data, s, sizeof(data));
2067 protocol = client->proto;
2068
2069 pw = zebra_pw_find(zvrf, ifname);
2070 switch (hdr->command) {
2071 case ZEBRA_PW_ADD:
2072 if (pw) {
e914ccbe 2073 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
9df414fe 2074 "%s: pseudowire %s already exists [%s]",
bf094f69
QY
2075 __func__, ifname,
2076 zserv_command_string(hdr->command));
2077 return;
2078 }
2079
2080 zebra_pw_add(zvrf, ifname, protocol, client);
2081 break;
2082 case ZEBRA_PW_DELETE:
2083 if (!pw) {
e914ccbe 2084 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 2085 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
2086 ifname, zserv_command_string(hdr->command));
2087 return;
2088 }
2089
2090 zebra_pw_del(zvrf, pw);
2091 break;
2092 case ZEBRA_PW_SET:
2093 case ZEBRA_PW_UNSET:
2094 if (!pw) {
e914ccbe 2095 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
9df414fe 2096 "%s: pseudowire %s not found [%s]", __func__,
bf094f69
QY
2097 ifname, zserv_command_string(hdr->command));
2098 return;
2099 }
2100
2101 switch (hdr->command) {
2102 case ZEBRA_PW_SET:
2103 pw->enabled = 1;
2104 break;
2105 case ZEBRA_PW_UNSET:
2106 pw->enabled = 0;
2107 break;
2108 }
2109
2110 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2111 remote_label, flags, &data);
2112 break;
2113 }
2114
2115stream_failure:
2116 return;
2117}
2118
2119static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2120{
2121 struct interface *master;
2122 struct interface *slave;
2123 struct stream *s = msg;
2124 int ifindex;
2125 vrf_id_t vrf_id;
2126
2127 STREAM_GETL(s, vrf_id);
2128 STREAM_GETL(s, ifindex);
2129 master = if_lookup_by_index(ifindex, vrf_id);
2130
2131 STREAM_GETL(s, vrf_id);
2132 STREAM_GETL(s, ifindex);
2133 slave = if_lookup_by_index(ifindex, vrf_id);
2134
2135 if (!master || !slave)
2136 return;
2137
2138 kernel_interface_set_master(master, slave);
2139
2140stream_failure:
2141 return;
2142}
2143
2144
2145static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2146{
2147 struct interface *ifp;
2148 mpls_label_t nlabel;
2149 afi_t afi;
2150 struct stream *s;
2151 struct zebra_vrf *def_zvrf;
2152 enum lsp_types_t ltype;
2153
2154 s = msg;
2155 STREAM_GETL(s, nlabel);
2156 STREAM_GETC(s, afi);
2157 if (nlabel == zvrf->label[afi]) {
2158 /*
2159 * Nothing to do here move along
2160 */
2161 return;
2162 }
2163
2164 STREAM_GETC(s, ltype);
2165
2166 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2167 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2168 else
2169 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2170
2171 if (!ifp) {
2172 zlog_debug("Unable to find specified Interface for %s",
2173 zvrf->vrf->name);
2174 return;
2175 }
2176
2177 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2178
2179 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2180 afi_t scrubber;
2181 bool really_remove;
2182
2183 really_remove = true;
2184 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2185 if (scrubber == afi)
2186 continue;
2187
2188 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2189 continue;
2190
2191 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2192 really_remove = false;
2193 break;
2194 }
2195 }
2196
2197 if (really_remove)
2198 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2199 NEXTHOP_TYPE_IFINDEX, NULL,
2200 ifp->ifindex);
2201 }
2202
2203 if (nlabel != MPLS_LABEL_NONE)
2204 mpls_lsp_install(def_zvrf, ltype, nlabel,
2205 MPLS_LABEL_IMPLICIT_NULL, NEXTHOP_TYPE_IFINDEX,
2206 NULL, ifp->ifindex);
2207
2208 zvrf->label[afi] = nlabel;
2209stream_failure:
2210 return;
2211}
2212
2213static inline void zread_rule(ZAPI_HANDLER_ARGS)
2214{
2215 struct zebra_pbr_rule zpr;
2216 struct stream *s;
2217 uint32_t total, i;
2218 ifindex_t ifindex;
2219
2220 s = msg;
2221 STREAM_GETL(s, total);
2222
2223 for (i = 0; i < total; i++) {
2224 memset(&zpr, 0, sizeof(zpr));
2225
2226 zpr.sock = client->sock;
2227 zpr.rule.vrf_id = hdr->vrf_id;
2228 STREAM_GETL(s, zpr.rule.seq);
2229 STREAM_GETL(s, zpr.rule.priority);
2230 STREAM_GETL(s, zpr.rule.unique);
2231 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2232 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2233 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2234 prefix_blen(&zpr.rule.filter.src_ip));
2235 STREAM_GETW(s, zpr.rule.filter.src_port);
2236 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2237 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2238 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2239 prefix_blen(&zpr.rule.filter.dst_ip));
2240 STREAM_GETW(s, zpr.rule.filter.dst_port);
2241 STREAM_GETL(s, zpr.rule.filter.fwmark);
2242 STREAM_GETL(s, zpr.rule.action.table);
2243 STREAM_GETL(s, ifindex);
2244
2245 if (ifindex) {
06432d4e
PG
2246 zpr.ifp = if_lookup_by_index_per_ns(
2247 zvrf->zns,
2248 ifindex);
bf094f69
QY
2249 if (!zpr.ifp) {
2250 zlog_debug("Failed to lookup ifindex: %u",
2251 ifindex);
2252 return;
2253 }
2254 }
2255
2256 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2257 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2258
2259 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2260 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2261
2262 if (zpr.rule.filter.src_port)
2263 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2264
2265 if (zpr.rule.filter.dst_port)
2266 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2267
2268 if (zpr.rule.filter.fwmark)
2269 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2270
7f0ea8a4 2271 zpr.vrf_id = zvrf->vrf->vrf_id;
bf094f69 2272 if (hdr->command == ZEBRA_RULE_ADD)
7f0ea8a4 2273 zebra_pbr_add_rule(&zpr);
bf094f69 2274 else
7f0ea8a4 2275 zebra_pbr_del_rule(&zpr);
bf094f69
QY
2276 }
2277
2278stream_failure:
2279 return;
2280}
2281
2282static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2283{
2284 struct zebra_pbr_ipset zpi;
2285 struct stream *s;
2286 uint32_t total, i;
2287
2288 s = msg;
2289 STREAM_GETL(s, total);
2290
2291 for (i = 0; i < total; i++) {
2292 memset(&zpi, 0, sizeof(zpi));
2293
2294 zpi.sock = client->sock;
be2028d1 2295 zpi.vrf_id = zvrf->vrf->vrf_id;
bf094f69
QY
2296 STREAM_GETL(s, zpi.unique);
2297 STREAM_GETL(s, zpi.type);
2298 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2299
2300 if (hdr->command == ZEBRA_IPSET_CREATE)
62f20a52 2301 zebra_pbr_create_ipset(&zpi);
bf094f69 2302 else
62f20a52 2303 zebra_pbr_destroy_ipset(&zpi);
bf094f69
QY
2304 }
2305
2306stream_failure:
2307 return;
2308}
2309
2310static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2311{
2312 struct zebra_pbr_ipset_entry zpi;
2313 struct zebra_pbr_ipset ipset;
2314 struct stream *s;
2315 uint32_t total, i;
2316
2317 s = msg;
2318 STREAM_GETL(s, total);
2319
2320 for (i = 0; i < total; i++) {
2321 memset(&zpi, 0, sizeof(zpi));
2322 memset(&ipset, 0, sizeof(ipset));
2323
2324 zpi.sock = client->sock;
2325 STREAM_GETL(s, zpi.unique);
2326 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2327 STREAM_GETC(s, zpi.src.family);
2328 STREAM_GETC(s, zpi.src.prefixlen);
2329 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
2330 STREAM_GETC(s, zpi.dst.family);
2331 STREAM_GETC(s, zpi.dst.prefixlen);
2332 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
2333
25d760c5
PG
2334 STREAM_GETW(s, zpi.src_port_min);
2335 STREAM_GETW(s, zpi.src_port_max);
2336 STREAM_GETW(s, zpi.dst_port_min);
2337 STREAM_GETW(s, zpi.dst_port_max);
2338 STREAM_GETC(s, zpi.proto);
bf094f69
QY
2339 if (!is_default_prefix(&zpi.src))
2340 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2341
2342 if (!is_default_prefix(&zpi.dst))
2343 zpi.filter_bm |= PBR_FILTER_DST_IP;
be729dd7 2344 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5 2345 zpi.filter_bm |= PBR_FILTER_DST_PORT;
be729dd7 2346 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
25d760c5
PG
2347 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
2348 if (zpi.dst_port_max != 0)
2349 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
2350 if (zpi.src_port_max != 0)
2351 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
2352 if (zpi.proto != 0)
2353 zpi.filter_bm |= PBR_FILTER_PROTO;
bf094f69
QY
2354
2355 /* calculate backpointer */
62f20a52
DS
2356 zpi.backpointer =
2357 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
bf094f69 2358 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
62f20a52 2359 zebra_pbr_add_ipset_entry(&zpi);
bf094f69 2360 else
62f20a52 2361 zebra_pbr_del_ipset_entry(&zpi);
bf094f69
QY
2362 }
2363
2364stream_failure:
2365 return;
2366}
2367
2368static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2369{
2370 struct zebra_pbr_iptable zpi;
2371 struct stream *s;
2372
2373 s = msg;
2374
2375 memset(&zpi, 0, sizeof(zpi));
2376
f80ec7e3 2377 zpi.interface_name_list = list_new();
bf094f69 2378 zpi.sock = client->sock;
be2028d1 2379 zpi.vrf_id = zvrf->vrf->vrf_id;
bf094f69
QY
2380 STREAM_GETL(s, zpi.unique);
2381 STREAM_GETL(s, zpi.type);
2382 STREAM_GETL(s, zpi.filter_bm);
2383 STREAM_GETL(s, zpi.action);
2384 STREAM_GETL(s, zpi.fwmark);
2385 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
e7f7dad4
PG
2386 STREAM_GETW(s, zpi.pkt_len_min);
2387 STREAM_GETW(s, zpi.pkt_len_max);
dc993e76
PG
2388 STREAM_GETW(s, zpi.tcp_flags);
2389 STREAM_GETW(s, zpi.tcp_mask_flags);
4977bd6c 2390 STREAM_GETC(s, zpi.dscp_value);
5ac5b7cc 2391 STREAM_GETC(s, zpi.fragment);
f80ec7e3
PG
2392 STREAM_GETL(s, zpi.nb_interface);
2393 zebra_pbr_iptable_update_interfacelist(s, &zpi);
bf094f69
QY
2394
2395 if (hdr->command == ZEBRA_IPTABLE_ADD)
62f20a52 2396 zebra_pbr_add_iptable(&zpi);
bf094f69 2397 else
62f20a52 2398 zebra_pbr_del_iptable(&zpi);
bf094f69
QY
2399stream_failure:
2400 return;
2401}
2402
2403void (*zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
2404 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2405 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2406 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2407 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
2408 [ZEBRA_ROUTE_ADD] = zread_route_add,
2409 [ZEBRA_ROUTE_DELETE] = zread_route_del,
bf094f69
QY
2410 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2411 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2412 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2413 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2414 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2415 [ZEBRA_HELLO] = zread_hello,
2416 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2417 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2418 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2419 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2420 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2421 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2422 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
d3af6147
RZ
2423#if HAVE_BFDD > 0
2424 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
2425#endif /* HAVE_BFDD */
bf094f69
QY
2426 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2427 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2428 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
2429#if defined(HAVE_RTADV)
2430 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2431 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
2432#else
2433 [ZEBRA_INTERFACE_ENABLE_RADV] = NULL,
2434 [ZEBRA_INTERFACE_DISABLE_RADV] = NULL,
2435#endif
2436 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels,
2437 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels,
2438 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2439 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
f533be73 2440 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
bf094f69
QY
2441 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2442 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2443 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2444 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2445 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
2446 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2447 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2448 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2449 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2450 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2451 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
3950b52c 2452 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
bf094f69
QY
2453 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2454 [ZEBRA_PW_ADD] = zread_pseudowire,
2455 [ZEBRA_PW_DELETE] = zread_pseudowire,
2456 [ZEBRA_PW_SET] = zread_pseudowire,
2457 [ZEBRA_PW_UNSET] = zread_pseudowire,
2458 [ZEBRA_RULE_ADD] = zread_rule,
2459 [ZEBRA_RULE_DELETE] = zread_rule,
2460 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
2461 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
2462 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
2463 [ZEBRA_IPSET_CREATE] = zread_ipset,
2464 [ZEBRA_IPSET_DESTROY] = zread_ipset,
2465 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
2466 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
2467 [ZEBRA_IPTABLE_ADD] = zread_iptable,
2468 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
fbac9605 2469 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
bf094f69
QY
2470};
2471
727c9b99
QY
2472#if defined(HANDLE_ZAPI_FUZZING)
2473extern struct zebra_privs_t zserv_privs;
2474
2475static void zserv_write_incoming(struct stream *orig, uint16_t command)
2476{
2477 char fname[MAXPATHLEN];
2478 struct stream *copy;
2479 int fd = -1;
2480
2481 copy = stream_dup(orig);
2482 stream_set_getp(copy, 0);
2483
727c9b99 2484 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
6bb30c2c
DL
2485
2486 frr_elevate_privs(&zserv_privs) {
2487 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2488 }
727c9b99
QY
2489 stream_flush(copy, fd);
2490 close(fd);
727c9b99
QY
2491 stream_free(copy);
2492}
2493#endif
2494
904e0d88 2495void zserv_handle_commands(struct zserv *client, struct stream *msg)
bf094f69 2496{
904e0d88
QY
2497 struct zmsghdr hdr;
2498 struct zebra_vrf *zvrf;
2499
2500 zapi_parse_header(msg, &hdr);
bf094f69 2501
727c9b99
QY
2502#if defined(HANDLE_ZAPI_FUZZING)
2503 zserv_write_incoming(msg, hdr.command);
2504#endif
2505
904e0d88
QY
2506 hdr.length -= ZEBRA_HEADER_SIZE;
2507
2508 /* lookup vrf */
2509 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
2510 if (!zvrf) {
2511 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
9df414fe
QY
2512 zlog_debug("ZAPI message specifies unknown VRF: %d",
2513 hdr.vrf_id);
904e0d88
QY
2514 return;
2515 }
2516
aa360de7 2517 if (hdr.command >= array_size(zserv_handlers)
904e0d88
QY
2518 || zserv_handlers[hdr.command] == NULL)
2519 zlog_info("Zebra received unknown command %d", hdr.command);
2520 else
2521 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
bf094f69 2522}