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