]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zapi_msg.c
zebra: Add table id to debug output
[mirror_frr.git] / zebra / zapi_msg.c
1 /*
2 * Zebra API message creation & consumption.
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/stream.h"
28 #include "lib/memory.h"
29 #include "lib/table.h"
30 #include "lib/network.h"
31 #include "lib/log.h"
32 #include "lib/zclient.h"
33 #include "lib/privs.h"
34 #include "lib/nexthop.h"
35 #include "lib/vrf.h"
36 #include "lib/libfrr.h"
37 #include "lib/lib_errors.h"
38
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
41 #include "zebra/zebra_memory.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/router-id.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_ptm.h"
50 #include "zebra/rtadv.h"
51 #include "zebra/zebra_mpls.h"
52 #include "zebra/zebra_mroute.h"
53 #include "zebra/zebra_vxlan.h"
54 #include "zebra/zebra_evpn_mh.h"
55 #include "zebra/rt.h"
56 #include "zebra/zebra_pbr.h"
57 #include "zebra/table_manager.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_errors.h"
60 #include "zebra/zebra_mlag.h"
61 #include "zebra/connected.h"
62 #include "zebra/zebra_opaque.h"
63 #include "zebra/zebra_srte.h"
64
65 /* Encoding helpers -------------------------------------------------------- */
66
67 static void zserv_encode_interface(struct stream *s, struct interface *ifp)
68 {
69 /* Interface information. */
70 struct zebra_if *zif = ifp->info;
71
72 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
73 stream_putl(s, ifp->ifindex);
74 stream_putc(s, ifp->status);
75 stream_putq(s, ifp->flags);
76 stream_putc(s, ifp->ptm_enable);
77 stream_putc(s, ifp->ptm_status);
78 stream_putl(s, ifp->metric);
79 stream_putl(s, ifp->speed);
80 stream_putl(s, ifp->mtu);
81 stream_putl(s, ifp->mtu6);
82 stream_putl(s, ifp->bandwidth);
83 stream_putl(s, zif->link_ifindex);
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
100 static 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
120 static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
121 {
122 stream_putl(s, nexthop->vrf_id);
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 /*
148 * Zebra error addition adds error type.
149 *
150 *
151 * 0 1
152 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154 * | enum zebra_error_types |
155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 *
157 */
158 static void zserv_encode_error(struct stream *s, enum zebra_error_types error)
159 {
160 stream_put(s, &error, sizeof(error));
161
162 /* Write packet size. */
163 stream_putw_at(s, 0, stream_get_endp(s));
164 }
165
166 /* Send handlers ----------------------------------------------------------- */
167
168 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
169 /*
170 * This function is called in the following situations:
171 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
172 * from the client.
173 * - at startup, when zebra figures out the available interfaces
174 * - when an interface is added (where support for
175 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
176 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
177 * received)
178 */
179 int zsend_interface_add(struct zserv *client, struct interface *ifp)
180 {
181 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
182
183 zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
184 zserv_encode_interface(s, ifp);
185
186 client->ifadd_cnt++;
187 return zserv_send_message(client, s);
188 }
189
190 /* Interface deletion from zebra daemon. */
191 int zsend_interface_delete(struct zserv *client, struct interface *ifp)
192 {
193 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
194
195 zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
196 zserv_encode_interface(s, ifp);
197
198 client->ifdel_cnt++;
199 return zserv_send_message(client, s);
200 }
201
202 int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
203 {
204 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
205
206 zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
207 zserv_encode_vrf(s, zvrf);
208
209 client->vrfadd_cnt++;
210 return zserv_send_message(client, s);
211 }
212
213 /* VRF deletion from zebra daemon. */
214 int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
215
216 {
217 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
218
219 zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
220 zserv_encode_vrf(s, zvrf);
221
222 client->vrfdel_cnt++;
223 return zserv_send_message(client, s);
224 }
225
226 int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
227 {
228 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
229
230 if (!ifp->link_params) {
231 stream_free(s);
232 return 0;
233 }
234
235 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
236
237 /* Add Interface Index */
238 stream_putl(s, ifp->ifindex);
239
240 /* Then TE Link Parameters */
241 if (zebra_interface_link_params_write(s, ifp) == 0) {
242 stream_free(s);
243 return 0;
244 }
245
246 /* Write packet size. */
247 stream_putw_at(s, 0, stream_get_endp(s));
248
249 return zserv_send_message(client, s);
250 }
251
252 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
253 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
254 *
255 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
256 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
257 * from the client, after the ZEBRA_INTERFACE_ADD has been
258 * sent from zebra to the client
259 * - redistribute new address info to all clients in the following situations
260 * - at startup, when zebra figures out the available interfaces
261 * - when an interface is added (where support for
262 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
263 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
264 * received)
265 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
266 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
267 * - when an RTM_NEWADDR message is received from the kernel,
268 *
269 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
270 *
271 * zsend_interface_address(DELETE)
272 * ^
273 * |
274 * zebra_interface_address_delete_update
275 * ^ ^ ^
276 * | | if_delete_update
277 * | |
278 * ip_address_uninstall connected_delete_ipv4
279 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
280 * ^ ^
281 * | |
282 * | RTM_NEWADDR on routing/netlink socket
283 * |
284 * vty commands:
285 * "no ip address A.B.C.D/M [label LINE]"
286 * "no ip address A.B.C.D/M"
287 * ["no ipv6 address X:X::X:X/M"]
288 *
289 */
290 int zsend_interface_address(int cmd, struct zserv *client,
291 struct interface *ifp, struct connected *ifc)
292 {
293 int blen;
294 struct prefix *p;
295 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
296
297 zclient_create_header(s, cmd, ifp->vrf_id);
298 stream_putl(s, ifp->ifindex);
299
300 /* Interface address flag. */
301 stream_putc(s, ifc->flags);
302
303 /* Prefix information. */
304 p = ifc->address;
305 stream_putc(s, p->family);
306 blen = prefix_blen(p);
307 stream_put(s, &p->u.prefix, blen);
308
309 /*
310 * XXX gnu version does not send prefixlen for
311 * ZEBRA_INTERFACE_ADDRESS_DELETE
312 * but zebra_interface_address_delete_read() in the gnu version
313 * expects to find it
314 */
315 stream_putc(s, p->prefixlen);
316
317 /* Destination. */
318 p = ifc->destination;
319 if (p)
320 stream_put(s, &p->u.prefix, blen);
321 else
322 stream_put(s, NULL, blen);
323
324 /* Write packet size. */
325 stream_putw_at(s, 0, stream_get_endp(s));
326
327 client->connected_rt_add_cnt++;
328 return zserv_send_message(client, s);
329 }
330
331 static int zsend_interface_nbr_address(int cmd, struct zserv *client,
332 struct interface *ifp,
333 struct nbr_connected *ifc)
334 {
335 int blen;
336 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
337 struct prefix *p;
338
339 zclient_create_header(s, cmd, ifp->vrf_id);
340 stream_putl(s, ifp->ifindex);
341
342 /* Prefix information. */
343 p = ifc->address;
344 stream_putc(s, p->family);
345 blen = prefix_blen(p);
346 stream_put(s, &p->u.prefix, blen);
347
348 /*
349 * XXX gnu version does not send prefixlen for
350 * ZEBRA_INTERFACE_ADDRESS_DELETE
351 * but zebra_interface_address_delete_read() in the gnu version
352 * expects to find it
353 */
354 stream_putc(s, p->prefixlen);
355
356 /* Write packet size. */
357 stream_putw_at(s, 0, stream_get_endp(s));
358
359 return zserv_send_message(client, s);
360 }
361
362 /* Interface address addition. */
363 static void zebra_interface_nbr_address_add_update(struct interface *ifp,
364 struct nbr_connected *ifc)
365 {
366 struct listnode *node, *nnode;
367 struct zserv *client;
368 struct prefix *p;
369
370 if (IS_ZEBRA_DEBUG_EVENT) {
371 char buf[INET6_ADDRSTRLEN];
372
373 p = ifc->address;
374 zlog_debug(
375 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
376 inet_ntop(p->family, &p->u.prefix, buf,
377 INET6_ADDRSTRLEN),
378 p->prefixlen, ifc->ifp->name);
379 }
380
381 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
382 /* Do not send unsolicited messages to synchronous clients. */
383 if (client->synchronous)
384 continue;
385
386 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
387 client, ifp, ifc);
388 }
389 }
390
391 /* Interface address deletion. */
392 static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
393 struct nbr_connected *ifc)
394 {
395 struct listnode *node, *nnode;
396 struct zserv *client;
397 struct prefix *p;
398
399 if (IS_ZEBRA_DEBUG_EVENT) {
400 char buf[INET6_ADDRSTRLEN];
401
402 p = ifc->address;
403 zlog_debug(
404 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
405 inet_ntop(p->family, &p->u.prefix, buf,
406 INET6_ADDRSTRLEN),
407 p->prefixlen, ifc->ifp->name);
408 }
409
410 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
411 /* Do not send unsolicited messages to synchronous clients. */
412 if (client->synchronous)
413 continue;
414
415 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
416 client, ifp, ifc);
417 }
418 }
419
420 /* Send addresses on interface to client */
421 int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
422 {
423 struct listnode *cnode, *cnnode;
424 struct connected *c;
425 struct nbr_connected *nc;
426
427 /* Send interface addresses. */
428 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
429 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
430 continue;
431
432 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
433 ifp, c)
434 < 0)
435 return -1;
436 }
437
438 /* Send interface neighbors. */
439 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
440 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
441 client, ifp, nc)
442 < 0)
443 return -1;
444 }
445
446 return 0;
447 }
448
449 /* Notify client about interface moving from one VRF to another.
450 * Whether client is interested in old and new VRF is checked by caller.
451 */
452 int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
453 vrf_id_t vrf_id)
454 {
455 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
456
457 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
458
459 /* Fill in the name of the interface and its new VRF (id) */
460 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
461 stream_putl(s, vrf_id);
462
463 /* Write packet size. */
464 stream_putw_at(s, 0, stream_get_endp(s));
465
466 client->if_vrfchg_cnt++;
467 return zserv_send_message(client, s);
468 }
469
470 /* Add new nbr connected IPv6 address */
471 void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
472 {
473 struct nbr_connected *ifc;
474 struct prefix p;
475
476 p.family = AF_INET6;
477 IPV6_ADDR_COPY(&p.u.prefix6, address);
478 p.prefixlen = IPV6_MAX_PREFIXLEN;
479
480 ifc = listnode_head(ifp->nbr_connected);
481 if (!ifc) {
482 /* new addition */
483 ifc = nbr_connected_new();
484 ifc->address = prefix_new();
485 ifc->ifp = ifp;
486 listnode_add(ifp->nbr_connected, ifc);
487 }
488
489 prefix_copy(ifc->address, &p);
490
491 zebra_interface_nbr_address_add_update(ifp, ifc);
492
493 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
494 }
495
496 void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
497 {
498 struct nbr_connected *ifc;
499 struct prefix p;
500
501 p.family = AF_INET6;
502 IPV6_ADDR_COPY(&p.u.prefix6, address);
503 p.prefixlen = IPV6_MAX_PREFIXLEN;
504
505 ifc = nbr_connected_check(ifp, &p);
506 if (!ifc)
507 return;
508
509 listnode_delete(ifp->nbr_connected, ifc);
510
511 zebra_interface_nbr_address_delete_update(ifp, ifc);
512
513 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
514
515 nbr_connected_free(ifc);
516 }
517
518 /*
519 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
520 * ZEBRA_INTERFACE_DOWN.
521 *
522 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
523 * the clients in one of 2 situations:
524 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
525 * - a vty command modifying the bandwidth of an interface is received.
526 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
527 */
528 int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
529 {
530 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
531
532 zclient_create_header(s, cmd, ifp->vrf_id);
533 zserv_encode_interface(s, ifp);
534
535 if (cmd == ZEBRA_INTERFACE_UP)
536 client->ifup_cnt++;
537 else
538 client->ifdown_cnt++;
539
540 return zserv_send_message(client, s);
541 }
542
543 int zsend_redistribute_route(int cmd, struct zserv *client,
544 const struct prefix *p,
545 const struct prefix *src_p,
546 const struct route_entry *re)
547 {
548 struct zapi_route api;
549 struct zapi_nexthop *api_nh;
550 struct nexthop *nexthop;
551 uint8_t count = 0;
552 afi_t afi;
553 size_t stream_size =
554 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
555
556 memset(&api, 0, sizeof(api));
557 api.vrf_id = re->vrf_id;
558 api.type = re->type;
559 api.safi = SAFI_UNICAST;
560 api.instance = re->instance;
561 api.flags = re->flags;
562
563 afi = family2afi(p->family);
564 switch (afi) {
565 case AFI_IP:
566 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
567 client->redist_v4_add_cnt++;
568 else
569 client->redist_v4_del_cnt++;
570 break;
571 case AFI_IP6:
572 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
573 client->redist_v6_add_cnt++;
574 else
575 client->redist_v6_del_cnt++;
576 break;
577 default:
578 break;
579 }
580
581 /* Prefix. */
582 api.prefix = *p;
583 if (src_p) {
584 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
585 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
586 }
587
588 for (nexthop = re->nhe->nhg.nexthop;
589 nexthop; nexthop = nexthop->next) {
590 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
591 continue;
592
593 api_nh = &api.nexthops[count];
594 api_nh->vrf_id = nexthop->vrf_id;
595 api_nh->type = nexthop->type;
596 api_nh->weight = nexthop->weight;
597 switch (nexthop->type) {
598 case NEXTHOP_TYPE_BLACKHOLE:
599 api_nh->bh_type = nexthop->bh_type;
600 break;
601 case NEXTHOP_TYPE_IPV4:
602 api_nh->gate.ipv4 = nexthop->gate.ipv4;
603 break;
604 case NEXTHOP_TYPE_IPV4_IFINDEX:
605 api_nh->gate.ipv4 = nexthop->gate.ipv4;
606 api_nh->ifindex = nexthop->ifindex;
607 break;
608 case NEXTHOP_TYPE_IFINDEX:
609 api_nh->ifindex = nexthop->ifindex;
610 break;
611 case NEXTHOP_TYPE_IPV6:
612 api_nh->gate.ipv6 = nexthop->gate.ipv6;
613 break;
614 case NEXTHOP_TYPE_IPV6_IFINDEX:
615 api_nh->gate.ipv6 = nexthop->gate.ipv6;
616 api_nh->ifindex = nexthop->ifindex;
617 }
618 count++;
619 }
620
621 /* Nexthops. */
622 if (count) {
623 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
624 api.nexthop_num = count;
625 }
626
627 /* Attributes. */
628 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
629 api.distance = re->distance;
630 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
631 api.metric = re->metric;
632 if (re->tag) {
633 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
634 api.tag = re->tag;
635 }
636 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
637 api.mtu = re->mtu;
638
639 struct stream *s = stream_new(stream_size);
640
641 /* Encode route and send. */
642 if (zapi_route_encode(cmd, s, &api) < 0) {
643 stream_free(s);
644 return -1;
645 }
646
647 if (IS_ZEBRA_DEBUG_SEND) {
648 char buf_prefix[PREFIX_STRLEN];
649
650 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
651
652 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
653 __func__, zserv_command_string(cmd),
654 zebra_route_string(client->proto),
655 zebra_route_string(api.type), api.vrf_id,
656 buf_prefix);
657 }
658 return zserv_send_message(client, s);
659 }
660
661 /*
662 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
663 * nexthop is not found on mrib. Returns both route metric and protocol
664 * distance.
665 */
666 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
667 struct in_addr addr,
668 struct route_entry *re,
669 struct zebra_vrf *zvrf)
670 {
671 struct stream *s;
672 unsigned long nump;
673 uint8_t num;
674 struct nexthop *nexthop;
675
676 /* Get output stream. */
677 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
678 stream_reset(s);
679
680 /* Fill in result. */
681 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
682 stream_put_in_addr(s, &addr);
683
684 if (re) {
685 stream_putc(s, re->distance);
686 stream_putl(s, re->metric);
687 num = 0;
688 /* remember position for nexthop_num */
689 nump = stream_get_endp(s);
690 /* reserve room for nexthop_num */
691 stream_putc(s, 0);
692 /*
693 * Only non-recursive routes are elegible to resolve the
694 * nexthop we are looking up. Therefore, we will just iterate
695 * over the top chain of nexthops.
696 */
697 for (nexthop = re->nhe->nhg.nexthop; nexthop;
698 nexthop = nexthop->next)
699 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
700 num += zserv_encode_nexthop(s, nexthop);
701
702 /* store nexthop_num */
703 stream_putc_at(s, nump, num);
704 } else {
705 stream_putc(s, 0); /* distance */
706 stream_putl(s, 0); /* metric */
707 stream_putc(s, 0); /* nexthop_num */
708 }
709
710 stream_putw_at(s, 0, stream_get_endp(s));
711
712 return zserv_send_message(client, s);
713 }
714
715 /*
716 * Common utility send route notification, called from a path using a
717 * route_entry and from a path using a dataplane context.
718 */
719 static int route_notify_internal(const struct prefix *p, int type,
720 uint16_t instance, vrf_id_t vrf_id,
721 uint32_t table_id,
722 enum zapi_route_notify_owner note)
723 {
724 struct zserv *client;
725 struct stream *s;
726 uint8_t blen;
727
728 client = zserv_find_client(type, instance);
729 if (!client || !client->notify_owner) {
730 if (IS_ZEBRA_DEBUG_PACKET) {
731 char buff[PREFIX_STRLEN];
732
733 zlog_debug(
734 "Not Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
735 zebra_route_string(type),
736 prefix2str(p, buff, sizeof(buff)), table_id,
737 note, vrf_id);
738 }
739 return 0;
740 }
741
742 if (IS_ZEBRA_DEBUG_PACKET) {
743 char buff[PREFIX_STRLEN];
744
745 zlog_debug("Notifying Owner: %s about prefix %s(%u) %d vrf: %u",
746 zebra_route_string(type),
747 prefix2str(p, buff, sizeof(buff)), table_id, note,
748 vrf_id);
749 }
750
751 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
752 stream_reset(s);
753
754 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
755
756 stream_put(s, &note, sizeof(note));
757
758 stream_putc(s, p->family);
759
760 blen = prefix_blen(p);
761 stream_putc(s, p->prefixlen);
762 stream_put(s, &p->u.prefix, blen);
763
764 stream_putl(s, table_id);
765
766 stream_putw_at(s, 0, stream_get_endp(s));
767
768 return zserv_send_message(client, s);
769 }
770
771 int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p,
772 enum zapi_route_notify_owner note)
773 {
774 return (route_notify_internal(p, re->type, re->instance, re->vrf_id,
775 re->table, note));
776 }
777
778 /*
779 * Route-owner notification using info from dataplane update context.
780 */
781 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
782 enum zapi_route_notify_owner note)
783 {
784 return (route_notify_internal(dplane_ctx_get_dest(ctx),
785 dplane_ctx_get_type(ctx),
786 dplane_ctx_get_instance(ctx),
787 dplane_ctx_get_vrf(ctx),
788 dplane_ctx_get_table(ctx),
789 note));
790 }
791
792 void zsend_rule_notify_owner(const struct zebra_dplane_ctx *ctx,
793 enum zapi_rule_notify_owner note)
794 {
795 struct listnode *node;
796 struct zserv *client;
797 struct stream *s;
798
799 if (IS_ZEBRA_DEBUG_PACKET)
800 zlog_debug("%s: Notifying %u", __func__,
801 dplane_ctx_rule_get_unique(ctx));
802
803 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
804 if (dplane_ctx_rule_get_sock(ctx) == client->sock)
805 break;
806 }
807
808 if (!client)
809 return;
810
811 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
812
813 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
814 stream_put(s, &note, sizeof(note));
815 stream_putl(s, dplane_ctx_rule_get_seq(ctx));
816 stream_putl(s, dplane_ctx_rule_get_priority(ctx));
817 stream_putl(s, dplane_ctx_rule_get_unique(ctx));
818 stream_putl(s, dplane_ctx_get_ifindex(ctx));
819
820 stream_putw_at(s, 0, stream_get_endp(s));
821
822 zserv_send_message(client, s);
823 }
824
825 void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
826 enum zapi_ipset_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", __func__, ipset->unique);
834
835 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
836 if (ipset->sock == client->sock)
837 break;
838 }
839
840 if (!client)
841 return;
842
843 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
844
845 zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
846 stream_put(s, &note, sizeof(note));
847 stream_putl(s, ipset->unique);
848 stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
849 stream_putw_at(s, 0, stream_get_endp(s));
850
851 zserv_send_message(client, s);
852 }
853
854 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset,
855 enum zapi_ipset_entry_notify_owner note)
856 {
857 struct listnode *node;
858 struct zserv *client;
859 struct stream *s;
860
861 if (IS_ZEBRA_DEBUG_PACKET)
862 zlog_debug("%s: Notifying %u", __func__, ipset->unique);
863
864 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
865 if (ipset->sock == client->sock)
866 break;
867 }
868
869 if (!client)
870 return;
871
872 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
873
874 zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT);
875 stream_put(s, &note, sizeof(note));
876 stream_putl(s, ipset->unique);
877 stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE);
878 stream_putw_at(s, 0, stream_get_endp(s));
879
880 zserv_send_message(client, s);
881 }
882
883 void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
884 enum zapi_iptable_notify_owner note)
885 {
886 struct listnode *node;
887 struct zserv *client;
888 struct stream *s;
889
890 if (IS_ZEBRA_DEBUG_PACKET)
891 zlog_debug("%s: Notifying %u", __func__, iptable->unique);
892
893 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
894 if (iptable->sock == client->sock)
895 break;
896 }
897
898 if (!client)
899 return;
900
901 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
902
903 zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
904 stream_put(s, &note, sizeof(note));
905 stream_putl(s, iptable->unique);
906 stream_putw_at(s, 0, stream_get_endp(s));
907
908 zserv_send_message(client, s);
909 }
910
911 /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */
912 int zsend_router_id_update(struct zserv *client, afi_t afi, struct prefix *p,
913 vrf_id_t vrf_id)
914 {
915 int blen;
916 struct stream *s;
917
918 /* Check this client need interface information. */
919 if (!vrf_bitmap_check(client->ridinfo[afi], vrf_id))
920 return 0;
921
922 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
923
924 /* Message type. */
925 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
926
927 /* Prefix information. */
928 stream_putc(s, p->family);
929 blen = prefix_blen(p);
930 stream_put(s, &p->u.prefix, blen);
931 stream_putc(s, p->prefixlen);
932
933 /* Write packet size. */
934 stream_putw_at(s, 0, stream_get_endp(s));
935
936 return zserv_send_message(client, s);
937 }
938
939 /*
940 * Function used by Zebra to send a PW status update to LDP daemon
941 */
942 int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
943 {
944 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
945
946 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
947 stream_write(s, pw->ifname, IF_NAMESIZE);
948 stream_putl(s, pw->ifindex);
949 stream_putl(s, pw->status);
950
951 /* Put length at the first point of the stream. */
952 stream_putw_at(s, 0, stream_get_endp(s));
953
954 return zserv_send_message(client, s);
955 }
956
957 /* Send response to a get label chunk request to client */
958 int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id,
959 struct label_manager_chunk *lmc)
960 {
961 int ret;
962 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
963
964 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
965 /* proto */
966 stream_putc(s, client->proto);
967 /* instance */
968 stream_putw(s, client->instance);
969
970 if (lmc) {
971 /* keep */
972 stream_putc(s, lmc->keep);
973 /* start and end labels */
974 stream_putl(s, lmc->start);
975 stream_putl(s, lmc->end);
976 }
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 return ret;
984 }
985
986 /* Send response to a label manager connect request to client */
987 int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id,
988 unsigned short result)
989 {
990 int ret;
991 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
992
993 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
994
995 /* proto */
996 stream_putc(s, client->proto);
997
998 /* instance */
999 stream_putw(s, client->instance);
1000
1001 /* result */
1002 stream_putc(s, result);
1003
1004 /* Write packet size. */
1005 stream_putw_at(s, 0, stream_get_endp(s));
1006
1007 ret = writen(client->sock, s->data, stream_get_endp(s));
1008 stream_free(s);
1009
1010 return ret;
1011 }
1012
1013 /* Send response to a get table chunk request to client */
1014 static int zsend_assign_table_chunk_response(struct zserv *client,
1015 vrf_id_t vrf_id,
1016 struct table_manager_chunk *tmc)
1017 {
1018 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1019
1020 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
1021
1022 if (tmc) {
1023 /* start and end labels */
1024 stream_putl(s, tmc->start);
1025 stream_putl(s, tmc->end);
1026 }
1027
1028 /* Write packet size. */
1029 stream_putw_at(s, 0, stream_get_endp(s));
1030
1031 return zserv_send_message(client, s);
1032 }
1033
1034 static int zsend_table_manager_connect_response(struct zserv *client,
1035 vrf_id_t vrf_id,
1036 uint16_t result)
1037 {
1038 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1039
1040 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
1041
1042 /* result */
1043 stream_putc(s, result);
1044
1045 stream_putw_at(s, 0, stream_get_endp(s));
1046
1047 return zserv_send_message(client, s);
1048 }
1049
1050 /* Inbound message handling ------------------------------------------------ */
1051
1052 const int cmd2type[] = {
1053 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
1054 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
1055 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
1056 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
1057 };
1058
1059 /* Nexthop register */
1060 static void zread_rnh_register(ZAPI_HANDLER_ARGS)
1061 {
1062 struct rnh *rnh;
1063 struct stream *s;
1064 struct prefix p;
1065 unsigned short l = 0;
1066 uint8_t flags = 0;
1067 uint16_t type = cmd2type[hdr->command];
1068 bool exist;
1069 bool flag_changed = false;
1070 uint8_t orig_flags;
1071
1072 if (IS_ZEBRA_DEBUG_NHT)
1073 zlog_debug(
1074 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u",
1075 zebra_route_string(client->proto), hdr->length,
1076 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
1077 zvrf->vrf->vrf_id);
1078
1079 s = msg;
1080
1081 if (!client->nh_reg_time)
1082 client->nh_reg_time = monotime(NULL);
1083
1084 while (l < hdr->length) {
1085 STREAM_GETC(s, flags);
1086 STREAM_GETW(s, p.family);
1087 STREAM_GETC(s, p.prefixlen);
1088 l += 4;
1089 if (p.family == AF_INET) {
1090 client->v4_nh_watch_add_cnt++;
1091 if (p.prefixlen > IPV4_MAX_BITLEN) {
1092 zlog_debug(
1093 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1094 __func__, p.prefixlen);
1095 return;
1096 }
1097 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1098 l += IPV4_MAX_BYTELEN;
1099 } else if (p.family == AF_INET6) {
1100 client->v6_nh_watch_add_cnt++;
1101 if (p.prefixlen > IPV6_MAX_BITLEN) {
1102 zlog_debug(
1103 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1104 __func__, p.prefixlen);
1105 return;
1106 }
1107 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1108 l += IPV6_MAX_BYTELEN;
1109 } else {
1110 flog_err(
1111 EC_ZEBRA_UNKNOWN_FAMILY,
1112 "rnh_register: Received unknown family type %d\n",
1113 p.family);
1114 return;
1115 }
1116 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type, &exist);
1117 if (!rnh)
1118 return;
1119
1120 orig_flags = rnh->flags;
1121 if (type == RNH_NEXTHOP_TYPE) {
1122 if (flags
1123 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1124 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1125 else if (!flags
1126 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1127 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1128 } else if (type == RNH_IMPORT_CHECK_TYPE) {
1129 if (flags
1130 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
1131 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1132 else if (!flags
1133 && CHECK_FLAG(rnh->flags,
1134 ZEBRA_NHT_EXACT_MATCH))
1135 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1136 }
1137
1138 if (orig_flags != rnh->flags)
1139 flag_changed = true;
1140
1141 /* Anything not AF_INET/INET6 has been filtered out above */
1142 if (!exist || flag_changed)
1143 zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, type,
1144 &p);
1145
1146 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
1147 }
1148
1149 stream_failure:
1150 return;
1151 }
1152
1153 /* Nexthop register */
1154 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1155 {
1156 struct rnh *rnh;
1157 struct stream *s;
1158 struct prefix p;
1159 unsigned short l = 0;
1160 uint16_t type = cmd2type[hdr->command];
1161
1162 if (IS_ZEBRA_DEBUG_NHT)
1163 zlog_debug(
1164 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u",
1165 zebra_route_string(client->proto), hdr->length,
1166 zvrf->vrf->vrf_id);
1167
1168 s = msg;
1169
1170 while (l < hdr->length) {
1171 uint8_t flags;
1172
1173 STREAM_GETC(s, flags);
1174 if (flags != 0)
1175 goto stream_failure;
1176
1177 STREAM_GETW(s, p.family);
1178 STREAM_GETC(s, p.prefixlen);
1179 l += 4;
1180 if (p.family == AF_INET) {
1181 client->v4_nh_watch_rem_cnt++;
1182 if (p.prefixlen > IPV4_MAX_BITLEN) {
1183 zlog_debug(
1184 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1185 __func__, p.prefixlen);
1186 return;
1187 }
1188 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1189 l += IPV4_MAX_BYTELEN;
1190 } else if (p.family == AF_INET6) {
1191 client->v6_nh_watch_rem_cnt++;
1192 if (p.prefixlen > IPV6_MAX_BITLEN) {
1193 zlog_debug(
1194 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1195 __func__, p.prefixlen);
1196 return;
1197 }
1198 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1199 l += IPV6_MAX_BYTELEN;
1200 } else {
1201 flog_err(
1202 EC_ZEBRA_UNKNOWN_FAMILY,
1203 "rnh_register: Received unknown family type %d\n",
1204 p.family);
1205 return;
1206 }
1207 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
1208 if (rnh) {
1209 client->nh_dereg_time = monotime(NULL);
1210 zebra_remove_rnh_client(rnh, client, type);
1211 }
1212 }
1213 stream_failure:
1214 return;
1215 }
1216
1217 #define ZEBRA_MIN_FEC_LENGTH 5
1218
1219 /* FEC register */
1220 static void zread_fec_register(ZAPI_HANDLER_ARGS)
1221 {
1222 struct stream *s;
1223 unsigned short l = 0;
1224 struct prefix p;
1225 uint16_t flags;
1226 uint32_t label = MPLS_INVALID_LABEL;
1227 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1228
1229 s = msg;
1230 zvrf = vrf_info_lookup(VRF_DEFAULT);
1231 if (!zvrf)
1232 return;
1233
1234 /*
1235 * The minimum amount of data that can be sent for one fec
1236 * registration
1237 */
1238 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1239 flog_err(
1240 EC_ZEBRA_IRDP_LEN_MISMATCH,
1241 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1242 hdr->length);
1243 return;
1244 }
1245
1246 while (l < hdr->length) {
1247 STREAM_GETW(s, flags);
1248 memset(&p, 0, sizeof(p));
1249 STREAM_GETW(s, p.family);
1250 if (p.family != AF_INET && p.family != AF_INET6) {
1251 flog_err(
1252 EC_ZEBRA_UNKNOWN_FAMILY,
1253 "fec_register: Received unknown family type %d\n",
1254 p.family);
1255 return;
1256 }
1257 STREAM_GETC(s, p.prefixlen);
1258 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1259 || (p.family == AF_INET6
1260 && p.prefixlen > IPV6_MAX_BITLEN)) {
1261 zlog_debug(
1262 "%s: Specified prefix hdr->length: %d is to long for %d",
1263 __func__, p.prefixlen, p.family);
1264 return;
1265 }
1266 l += 5;
1267 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1268 l += PSIZE(p.prefixlen);
1269 if (flags & ZEBRA_FEC_REGISTER_LABEL) {
1270 STREAM_GETL(s, label);
1271 l += 4;
1272 } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
1273 STREAM_GETL(s, label_index);
1274 l += 4;
1275 }
1276
1277 zebra_mpls_fec_register(zvrf, &p, label, label_index, client);
1278 }
1279
1280 stream_failure:
1281 return;
1282 }
1283
1284 /* FEC unregister */
1285 static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1286 {
1287 struct stream *s;
1288 unsigned short l = 0;
1289 struct prefix p;
1290 uint16_t flags;
1291
1292 s = msg;
1293 zvrf = vrf_info_lookup(VRF_DEFAULT);
1294 if (!zvrf)
1295 return;
1296
1297 /*
1298 * The minimum amount of data that can be sent for one
1299 * fec unregistration
1300 */
1301 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1302 flog_err(
1303 EC_ZEBRA_IRDP_LEN_MISMATCH,
1304 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1305 hdr->length);
1306 return;
1307 }
1308
1309 while (l < hdr->length) {
1310 STREAM_GETW(s, flags);
1311 if (flags != 0)
1312 goto stream_failure;
1313
1314 memset(&p, 0, sizeof(p));
1315 STREAM_GETW(s, p.family);
1316 if (p.family != AF_INET && p.family != AF_INET6) {
1317 flog_err(
1318 EC_ZEBRA_UNKNOWN_FAMILY,
1319 "fec_unregister: Received unknown family type %d\n",
1320 p.family);
1321 return;
1322 }
1323 STREAM_GETC(s, p.prefixlen);
1324 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1325 || (p.family == AF_INET6
1326 && p.prefixlen > IPV6_MAX_BITLEN)) {
1327 zlog_debug(
1328 "%s: Received prefix hdr->length %d which is greater than %d can support",
1329 __func__, p.prefixlen, p.family);
1330 return;
1331 }
1332 l += 5;
1333 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1334 l += PSIZE(p.prefixlen);
1335 zebra_mpls_fec_unregister(zvrf, &p, client);
1336 }
1337
1338 stream_failure:
1339 return;
1340 }
1341
1342
1343 /*
1344 * Register zebra server interface information.
1345 * Send current all interface and address information.
1346 */
1347 static void zread_interface_add(ZAPI_HANDLER_ARGS)
1348 {
1349 struct vrf *vrf;
1350 struct interface *ifp;
1351
1352 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1353 FOR_ALL_INTERFACES (vrf, ifp) {
1354 /* Skip pseudo interface. */
1355 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1356 continue;
1357
1358 zsend_interface_add(client, ifp);
1359 zsend_interface_link_params(client, ifp);
1360 zsend_interface_addresses(client, ifp);
1361 }
1362 }
1363 }
1364
1365 /* Unregister zebra server interface information. */
1366 static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1367 {
1368 }
1369
1370 /*
1371 * Handle message requesting interface be set up or down.
1372 */
1373 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS)
1374 {
1375 ifindex_t ifindex;
1376 struct interface *ifp;
1377 char down;
1378
1379 STREAM_GETL(msg, ifindex);
1380 STREAM_GETC(msg, down);
1381
1382 /* set ifdown */
1383 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex);
1384
1385 if (ifp) {
1386 zlog_info("Setting interface %s (%u): protodown %s", ifp->name,
1387 ifindex, down ? "on" : "off");
1388 zebra_if_set_protodown(ifp, down);
1389 } else {
1390 zlog_warn(
1391 "Cannot set protodown %s for interface %u; does not exist",
1392 down ? "on" : "off", ifindex);
1393 }
1394
1395
1396 stream_failure:
1397 return;
1398 }
1399
1400
1401 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1402 const unsigned int nexthop_num)
1403 {
1404 if (nexthop_num > zrouter.multipath_num) {
1405 char buff[PREFIX2STR_BUFFER];
1406
1407 prefix2str(p, buff, sizeof(buff));
1408 flog_warn(
1409 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
1410 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1411 caller, buff, nexthop_num, zrouter.multipath_num);
1412 }
1413 }
1414
1415 /*
1416 * Create a new nexthop based on a zapi nexthop.
1417 */
1418 static struct nexthop *nexthop_from_zapi(struct route_entry *re,
1419 const struct zapi_nexthop *api_nh,
1420 const struct zapi_route *api)
1421 {
1422 struct nexthop *nexthop = NULL;
1423 struct ipaddr vtep_ip;
1424 struct interface *ifp;
1425 int i;
1426 char nhbuf[INET6_ADDRSTRLEN] = "";
1427
1428 switch (api_nh->type) {
1429 case NEXTHOP_TYPE_IFINDEX:
1430 nexthop = nexthop_from_ifindex(api_nh->ifindex, api_nh->vrf_id);
1431 break;
1432 case NEXTHOP_TYPE_IPV4:
1433 if (IS_ZEBRA_DEBUG_RECV) {
1434 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1435 sizeof(nhbuf));
1436 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1437 nhbuf, api_nh->vrf_id);
1438 }
1439 nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4, NULL,
1440 api_nh->vrf_id);
1441 break;
1442 case NEXTHOP_TYPE_IPV4_IFINDEX:
1443 if (IS_ZEBRA_DEBUG_RECV) {
1444 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1445 sizeof(nhbuf));
1446 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1447 __func__, nhbuf, api_nh->vrf_id,
1448 api_nh->ifindex);
1449 }
1450
1451 nexthop = nexthop_from_ipv4_ifindex(
1452 &api_nh->gate.ipv4, NULL, api_nh->ifindex,
1453 api_nh->vrf_id);
1454
1455 /* Special handling for IPv4 routes sourced from EVPN:
1456 * the nexthop and associated MAC need to be installed.
1457 */
1458 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1459 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1460 vtep_ip.ipa_type = IPADDR_V4;
1461 memcpy(&(vtep_ip.ipaddr_v4), &(api_nh->gate.ipv4),
1462 sizeof(struct in_addr));
1463 zebra_vxlan_evpn_vrf_route_add(
1464 api_nh->vrf_id, &api_nh->rmac,
1465 &vtep_ip, &api->prefix);
1466 }
1467 break;
1468 case NEXTHOP_TYPE_IPV6:
1469 if (IS_ZEBRA_DEBUG_RECV) {
1470 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1471 sizeof(nhbuf));
1472 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1473 nhbuf, api_nh->vrf_id);
1474 }
1475 nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6, api_nh->vrf_id);
1476 break;
1477 case NEXTHOP_TYPE_IPV6_IFINDEX:
1478 if (IS_ZEBRA_DEBUG_RECV) {
1479 inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf,
1480 sizeof(nhbuf));
1481 zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d",
1482 __func__, nhbuf, api_nh->vrf_id,
1483 api_nh->ifindex);
1484 }
1485 nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6,
1486 api_nh->ifindex,
1487 api_nh->vrf_id);
1488
1489 /* Special handling for IPv6 routes sourced from EVPN:
1490 * the nexthop and associated MAC need to be installed.
1491 */
1492 if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1493 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1494 vtep_ip.ipa_type = IPADDR_V6;
1495 memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6),
1496 sizeof(struct in6_addr));
1497 zebra_vxlan_evpn_vrf_route_add(
1498 api_nh->vrf_id, &api_nh->rmac,
1499 &vtep_ip, &api->prefix);
1500 }
1501 break;
1502 case NEXTHOP_TYPE_BLACKHOLE:
1503 if (IS_ZEBRA_DEBUG_RECV)
1504 zlog_debug("%s: nh blackhole %d",
1505 __func__, api_nh->bh_type);
1506
1507 nexthop = nexthop_from_blackhole(api_nh->bh_type);
1508 break;
1509 }
1510
1511 /* Return early if we couldn't process the zapi nexthop */
1512 if (nexthop == NULL) {
1513 goto done;
1514 }
1515
1516 /* Mark nexthop as onlink either if client has explicitly told us
1517 * to or if the nexthop is on an 'unnumbered' interface.
1518 */
1519 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK))
1520 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1521 else if (api_nh->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1522 ifp = if_lookup_by_index(api_nh->ifindex, api_nh->vrf_id);
1523 if (ifp && connected_is_unnumbered(ifp))
1524 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1525 }
1526
1527 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT))
1528 nexthop->weight = api_nh->weight;
1529
1530 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
1531 /* Validate count */
1532 if (api_nh->backup_num > NEXTHOP_MAX_BACKUPS) {
1533 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1534 zlog_debug("%s: invalid backup nh count %d",
1535 __func__, api_nh->backup_num);
1536 nexthop_free(nexthop);
1537 nexthop = NULL;
1538 goto done;
1539 }
1540
1541 /* Copy backup info */
1542 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1543 nexthop->backup_num = api_nh->backup_num;
1544
1545 for (i = 0; i < api_nh->backup_num; i++) {
1546 /* Validate backup index */
1547 if (api_nh->backup_idx[i] < api->backup_nexthop_num) {
1548 nexthop->backup_idx[i] = api_nh->backup_idx[i];
1549 } else {
1550 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1551 zlog_debug("%s: invalid backup nh idx %d",
1552 __func__,
1553 api_nh->backup_idx[i]);
1554 nexthop_free(nexthop);
1555 nexthop = NULL;
1556 goto done;
1557 }
1558 }
1559 }
1560
1561 done:
1562 return nexthop;
1563 }
1564
1565 static void zread_route_add(ZAPI_HANDLER_ARGS)
1566 {
1567 struct stream *s;
1568 struct zapi_route api;
1569 struct zapi_nexthop *api_nh;
1570 afi_t afi;
1571 struct prefix_ipv6 *src_p = NULL;
1572 struct route_entry *re;
1573 struct nexthop *nexthop = NULL, *last_nh;
1574 struct nexthop_group *ng = NULL;
1575 struct nhg_backup_info *bnhg = NULL;
1576 int i, ret;
1577 vrf_id_t vrf_id;
1578 struct nhg_hash_entry nhe;
1579 enum lsp_types_t label_type;
1580 char nhbuf[NEXTHOP_STRLEN];
1581 char labelbuf[MPLS_LABEL_STRLEN];
1582
1583 s = msg;
1584 if (zapi_route_decode(s, &api) < 0) {
1585 if (IS_ZEBRA_DEBUG_RECV)
1586 zlog_debug("%s: Unable to decode zapi_route sent",
1587 __func__);
1588 return;
1589 }
1590
1591 vrf_id = zvrf_id(zvrf);
1592
1593 if (IS_ZEBRA_DEBUG_RECV) {
1594 char buf_prefix[PREFIX_STRLEN];
1595
1596 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1597 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
1598 __func__, vrf_id, api.tableid, buf_prefix, (int)api.message, api.flags);
1599 }
1600
1601 /* Allocate new route. */
1602 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1603 re->type = api.type;
1604 re->instance = api.instance;
1605 re->flags = api.flags;
1606 re->uptime = monotime(NULL);
1607 re->vrf_id = vrf_id;
1608
1609 if (api.tableid)
1610 re->table = api.tableid;
1611 else
1612 re->table = zvrf->table_id;
1613
1614 if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)
1615 || api.nexthop_num == 0) {
1616 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS,
1617 "%s: received a route without nexthops for prefix %pFX from client %s",
1618 __func__, &api.prefix,
1619 zebra_route_string(client->proto));
1620
1621 XFREE(MTYPE_RE, re);
1622 return;
1623 }
1624
1625 /* Report misuse of the backup flag */
1626 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS) &&
1627 api.backup_nexthop_num == 0) {
1628 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT)
1629 zlog_debug("%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX",
1630 __func__,
1631 zebra_route_string(client->proto), &api.prefix);
1632 }
1633
1634 /* Use temporary list of nexthops */
1635 ng = nexthop_group_new();
1636
1637 /*
1638 * TBD should _all_ of the nexthop add operations use
1639 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1640 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1641 */
1642 for (i = 0; i < api.nexthop_num; i++) {
1643 api_nh = &api.nexthops[i];
1644
1645 /* Convert zapi nexthop */
1646 nexthop = nexthop_from_zapi(re, api_nh, &api);
1647 if (!nexthop) {
1648 flog_warn(
1649 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1650 "%s: Nexthops Specified: %d but we failed to properly create one",
1651 __func__, api.nexthop_num);
1652 nexthop_group_delete(&ng);
1653 XFREE(MTYPE_RE, re);
1654 return;
1655 }
1656
1657 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRTE)) {
1658 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE);
1659 nexthop->srte_color = api_nh->srte_color;
1660 }
1661
1662 /* MPLS labels for BGP-LU or Segment Routing */
1663 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1664 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1665 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE
1666 && api_nh->label_num > 0) {
1667
1668 label_type = lsp_type_from_re_type(client->proto);
1669 nexthop_add_labels(nexthop, label_type,
1670 api_nh->label_num,
1671 &api_nh->labels[0]);
1672 }
1673
1674 if (IS_ZEBRA_DEBUG_RECV) {
1675 labelbuf[0] = '\0';
1676 nhbuf[0] = '\0';
1677
1678 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1679
1680 if (nexthop->nh_label &&
1681 nexthop->nh_label->num_labels > 0) {
1682 mpls_label2str(nexthop->nh_label->num_labels,
1683 nexthop->nh_label->label,
1684 labelbuf, sizeof(labelbuf),
1685 false);
1686 }
1687
1688 zlog_debug("%s: nh=%s, vrf_id=%d %s",
1689 __func__, nhbuf, api_nh->vrf_id, labelbuf);
1690 }
1691
1692 /* Add new nexthop to temporary list. This list is
1693 * canonicalized - sorted - so that it can be hashed later
1694 * in route processing. We expect that the sender has sent
1695 * the list sorted, and the zapi client api attempts to enforce
1696 * that, so this should be inexpensive - but it is necessary
1697 * to support shared nexthop-groups.
1698 */
1699 nexthop_group_add_sorted(ng, nexthop);
1700 }
1701
1702 /* Allocate temporary list of backup nexthops, if necessary */
1703 if (api.backup_nexthop_num > 0) {
1704 if (IS_ZEBRA_DEBUG_RECV)
1705 zlog_debug("%s: adding %d backup nexthops",
1706 __func__, api.backup_nexthop_num);
1707
1708 bnhg = zebra_nhg_backup_alloc();
1709 nexthop = NULL;
1710 last_nh = NULL;
1711 }
1712
1713 /* Copy backup nexthops also, if present */
1714 for (i = 0; i < api.backup_nexthop_num; i++) {
1715 api_nh = &api.backup_nexthops[i];
1716
1717 /* Convert zapi backup nexthop */
1718 nexthop = nexthop_from_zapi(re, api_nh, &api);
1719 if (!nexthop) {
1720 flog_warn(
1721 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1722 "%s: Backup Nexthops Specified: %d but we failed to properly create one",
1723 __func__, api.backup_nexthop_num);
1724 nexthop_group_delete(&ng);
1725 zebra_nhg_backup_free(&bnhg);
1726 XFREE(MTYPE_RE, re);
1727 return;
1728 }
1729
1730 /* Backup nexthops can't have backups; that's not valid. */
1731 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
1732 if (IS_ZEBRA_DEBUG_RECV) {
1733 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1734 zlog_debug("%s: backup nh %s with BACKUP flag!",
1735 __func__, nhbuf);
1736 }
1737 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
1738 nexthop->backup_num = 0;
1739 }
1740
1741 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRTE)) {
1742 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE);
1743 nexthop->srte_color = api_nh->srte_color;
1744 }
1745
1746 /* MPLS labels for BGP-LU or Segment Routing */
1747 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1748 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1749 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE
1750 && api_nh->label_num > 0) {
1751
1752 label_type = lsp_type_from_re_type(client->proto);
1753 nexthop_add_labels(nexthop, label_type,
1754 api_nh->label_num,
1755 &api_nh->labels[0]);
1756 }
1757
1758 if (IS_ZEBRA_DEBUG_RECV) {
1759 labelbuf[0] = '\0';
1760 nhbuf[0] = '\0';
1761
1762 nexthop2str(nexthop, nhbuf, sizeof(nhbuf));
1763
1764 if (nexthop->nh_label &&
1765 nexthop->nh_label->num_labels > 0) {
1766 mpls_label2str(nexthop->nh_label->num_labels,
1767 nexthop->nh_label->label,
1768 labelbuf, sizeof(labelbuf),
1769 false);
1770 }
1771
1772 zlog_debug("%s: backup nh=%s, vrf_id=%d %s",
1773 __func__, nhbuf, api_nh->vrf_id, labelbuf);
1774 }
1775
1776 /* Note that the order of the backup nexthops is significant,
1777 * so we don't sort this list as we do the primary nexthops,
1778 * we just append.
1779 */
1780 if (last_nh)
1781 NEXTHOP_APPEND(last_nh, nexthop);
1782 else
1783 bnhg->nhe->nhg.nexthop = nexthop;
1784
1785 last_nh = nexthop;
1786 }
1787
1788 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1789 re->distance = api.distance;
1790 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1791 re->metric = api.metric;
1792 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1793 re->tag = api.tag;
1794 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1795 re->mtu = api.mtu;
1796
1797 afi = family2afi(api.prefix.family);
1798 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1799 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1800 "%s: Received SRC Prefix but afi is not v6",
1801 __func__);
1802 nexthop_group_delete(&ng);
1803 zebra_nhg_backup_free(&bnhg);
1804 XFREE(MTYPE_RE, re);
1805 return;
1806 }
1807 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1808 src_p = &api.src_prefix;
1809
1810 if (api.safi != SAFI_UNICAST && api.safi != SAFI_MULTICAST) {
1811 flog_warn(EC_LIB_ZAPI_MISSMATCH,
1812 "%s: Received safi: %d but we can only accept UNICAST or MULTICAST",
1813 __func__, api.safi);
1814 nexthop_group_delete(&ng);
1815 zebra_nhg_backup_free(&bnhg);
1816 XFREE(MTYPE_RE, re);
1817 return;
1818 }
1819
1820 /* Include backup info with the route. We use a temporary nhe here;
1821 * if this is a new/unknown nhe, a new copy will be allocated
1822 * and stored.
1823 */
1824 zebra_nhe_init(&nhe, afi, ng->nexthop);
1825 nhe.nhg.nexthop = ng->nexthop;
1826 nhe.backup_info = bnhg;
1827 ret = rib_add_multipath_nhe(afi, api.safi, &api.prefix, src_p,
1828 re, &nhe);
1829
1830 /* At this point, these allocations are not needed: 're' has been
1831 * retained or freed, and if 're' still exists, it is using
1832 * a reference to a shared group object.
1833 */
1834 nexthop_group_delete(&ng);
1835 if (bnhg)
1836 zebra_nhg_backup_free(&bnhg);
1837
1838 /* Stats */
1839 switch (api.prefix.family) {
1840 case AF_INET:
1841 if (ret > 0)
1842 client->v4_route_add_cnt++;
1843 else if (ret < 0)
1844 client->v4_route_upd8_cnt++;
1845 break;
1846 case AF_INET6:
1847 if (ret > 0)
1848 client->v6_route_add_cnt++;
1849 else if (ret < 0)
1850 client->v6_route_upd8_cnt++;
1851 break;
1852 }
1853 }
1854
1855 static void zread_route_del(ZAPI_HANDLER_ARGS)
1856 {
1857 struct stream *s;
1858 struct zapi_route api;
1859 afi_t afi;
1860 struct prefix_ipv6 *src_p = NULL;
1861 uint32_t table_id;
1862
1863 s = msg;
1864 if (zapi_route_decode(s, &api) < 0)
1865 return;
1866
1867 afi = family2afi(api.prefix.family);
1868 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1869 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1870 "%s: Received a src prefix while afi is not v6",
1871 __func__);
1872 return;
1873 }
1874 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1875 src_p = &api.src_prefix;
1876
1877 if (api.tableid)
1878 table_id = api.tableid;
1879 else
1880 table_id = zvrf->table_id;
1881
1882 if (IS_ZEBRA_DEBUG_RECV) {
1883 char buf_prefix[PREFIX_STRLEN];
1884
1885 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1886 zlog_debug("%s: p=(%u:%u)%s, msg flags=0x%x, flags=0x%x",
1887 __func__, zvrf_id(zvrf), table_id, buf_prefix,
1888 (int)api.message, api.flags);
1889 }
1890
1891 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1892 api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric,
1893 api.distance, false);
1894
1895 /* Stats */
1896 switch (api.prefix.family) {
1897 case AF_INET:
1898 client->v4_route_del_cnt++;
1899 break;
1900 case AF_INET6:
1901 client->v6_route_del_cnt++;
1902 break;
1903 }
1904 }
1905
1906 /* MRIB Nexthop lookup for IPv4. */
1907 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1908 {
1909 struct in_addr addr;
1910 struct route_entry *re;
1911
1912 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1913 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1914 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1915
1916 stream_failure:
1917 return;
1918 }
1919
1920 /* Register zebra server router-id information. Send current router-id */
1921 static void zread_router_id_add(ZAPI_HANDLER_ARGS)
1922 {
1923 afi_t afi;
1924
1925 struct prefix p;
1926
1927 STREAM_GETW(msg, afi);
1928
1929 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
1930 zlog_warn(
1931 "Invalid AFI %u while registering for router ID notifications",
1932 afi);
1933 goto stream_failure;
1934 }
1935
1936 /* Router-id information is needed. */
1937 vrf_bitmap_set(client->ridinfo[afi], zvrf_id(zvrf));
1938
1939 router_id_get(afi, &p, zvrf);
1940
1941 zsend_router_id_update(client, afi, &p, zvrf_id(zvrf));
1942
1943 stream_failure:
1944 return;
1945 }
1946
1947 /* Unregister zebra server router-id information. */
1948 static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
1949 {
1950 afi_t afi;
1951
1952 STREAM_GETW(msg, afi);
1953
1954 if (afi <= AFI_UNSPEC || afi >= AFI_MAX) {
1955 zlog_warn(
1956 "Invalid AFI %u while unregistering from router ID notifications",
1957 afi);
1958 goto stream_failure;
1959 }
1960
1961 vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf));
1962
1963 stream_failure:
1964 return;
1965 }
1966
1967 static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
1968 {
1969 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1970
1971 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
1972 stream_putl(s, vrf_get_backend());
1973 stream_putc(s, mpls_enabled);
1974 stream_putl(s, zrouter.multipath_num);
1975 stream_putc(s, zebra_mlag_get_role());
1976
1977 stream_putw_at(s, 0, stream_get_endp(s));
1978 zserv_send_message(client, s);
1979 }
1980
1981 void zsend_capabilities_all_clients(void)
1982 {
1983 struct listnode *node, *nnode;
1984 struct zebra_vrf *zvrf;
1985 struct zserv *client;
1986
1987 zvrf = vrf_info_lookup(VRF_DEFAULT);
1988 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
1989 /* Do not send unsolicited messages to synchronous clients. */
1990 if (client->synchronous)
1991 continue;
1992
1993 zsend_capabilities(client, zvrf);
1994 }
1995 }
1996
1997 /* Tie up route-type and client->sock */
1998 static void zread_hello(ZAPI_HANDLER_ARGS)
1999 {
2000 /* type of protocol (lib/zebra.h) */
2001 uint8_t proto;
2002 unsigned short instance;
2003 uint8_t notify;
2004 uint8_t synchronous;
2005 uint32_t session_id;
2006
2007 STREAM_GETC(msg, proto);
2008 STREAM_GETW(msg, instance);
2009 STREAM_GETL(msg, session_id);
2010 STREAM_GETC(msg, notify);
2011 STREAM_GETC(msg, synchronous);
2012 if (notify)
2013 client->notify_owner = true;
2014
2015 if (synchronous)
2016 client->synchronous = true;
2017
2018 /* accept only dynamic routing protocols */
2019 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
2020 zlog_notice(
2021 "client %d says hello and bids fair to announce only %s routes vrf=%u",
2022 client->sock, zebra_route_string(proto),
2023 zvrf->vrf->vrf_id);
2024 if (instance)
2025 zlog_notice("client protocol instance %d", instance);
2026
2027 client->proto = proto;
2028 client->instance = instance;
2029 client->session_id = session_id;
2030
2031 /* Graceful restart processing for client connect */
2032 zebra_gr_client_reconnect(client);
2033 }
2034
2035 if (!client->synchronous) {
2036 zsend_capabilities(client, zvrf);
2037 zebra_vrf_update_all(client);
2038 }
2039 stream_failure:
2040 return;
2041 }
2042
2043 /* Unregister all information in a VRF. */
2044 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
2045 {
2046 int i;
2047 afi_t afi;
2048
2049 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2050 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2051 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
2052 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
2053 vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf));
2054 }
2055 }
2056
2057 /*
2058 * Validate incoming zapi mpls lsp / labels message
2059 */
2060 static int zapi_labels_validate(const struct zapi_labels *zl)
2061 {
2062 int ret = -1;
2063 int i, j, idx;
2064 uint32_t bits[8];
2065 uint32_t ival;
2066 const struct zapi_nexthop *znh;
2067
2068 /* Validate backup info: no duplicates for a single primary */
2069 if (zl->backup_nexthop_num == 0) {
2070 ret = 0;
2071 goto done;
2072 }
2073
2074 for (j = 0; j < zl->nexthop_num; j++) {
2075 znh = &zl->nexthops[j];
2076
2077 memset(bits, 0, sizeof(bits));
2078
2079 for (i = 0; i < znh->backup_num; i++) {
2080 idx = znh->backup_idx[i] / 32;
2081
2082 ival = 1 << znh->backup_idx[i] % 32;
2083
2084 /* Check whether value is already used */
2085 if (ival & bits[idx]) {
2086 /* Fail */
2087
2088 if (IS_ZEBRA_DEBUG_RECV)
2089 zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d",
2090 __func__,
2091 znh->backup_idx[i]);
2092 goto done;
2093 }
2094
2095 /* Mark index value */
2096 bits[idx] |= ival;
2097 }
2098 }
2099
2100 ret = 0;
2101
2102 done:
2103
2104 return ret;
2105 }
2106
2107 /*
2108 * Handle request to create an MPLS LSP.
2109 *
2110 * A single message can fully specify an LSP with multiple nexthops.
2111 *
2112 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2113 * updated to use the received label(s).
2114 */
2115 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS)
2116 {
2117 struct stream *s;
2118 struct zapi_labels zl;
2119 int ret;
2120
2121 /* Get input stream. */
2122 s = msg;
2123 if (zapi_labels_decode(s, &zl) < 0) {
2124 if (IS_ZEBRA_DEBUG_RECV)
2125 zlog_debug("%s: Unable to decode zapi_labels sent",
2126 __func__);
2127 return;
2128 }
2129
2130 if (!mpls_enabled)
2131 return;
2132
2133 /* Validate; will debug on failure */
2134 if (zapi_labels_validate(&zl) < 0)
2135 return;
2136
2137 ret = mpls_zapi_labels_process(true, zvrf, &zl);
2138 if (ret < 0) {
2139 if (IS_ZEBRA_DEBUG_RECV)
2140 zlog_debug("%s: Error processing zapi request",
2141 __func__);
2142 }
2143 }
2144
2145 /*
2146 * Handle request to delete an MPLS LSP.
2147 *
2148 * An LSP is identified by its type and local label. When the received message
2149 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
2150 * listed LSP nexthops (aka NHLFEs) are deleted.
2151 *
2152 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
2153 * FEC (route) nexthops are deleted.
2154 */
2155 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS)
2156 {
2157 struct stream *s;
2158 struct zapi_labels zl;
2159 int ret;
2160
2161 /* Get input stream. */
2162 s = msg;
2163 if (zapi_labels_decode(s, &zl) < 0) {
2164 if (IS_ZEBRA_DEBUG_RECV)
2165 zlog_debug("%s: Unable to decode zapi_labels sent",
2166 __func__);
2167 return;
2168 }
2169
2170 if (!mpls_enabled)
2171 return;
2172
2173 if (zl.nexthop_num > 0) {
2174 ret = mpls_zapi_labels_process(false /*delete*/, zvrf, &zl);
2175 if (ret < 0) {
2176 if (IS_ZEBRA_DEBUG_RECV)
2177 zlog_debug("%s: Error processing zapi request",
2178 __func__);
2179 }
2180 } else {
2181 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2182
2183 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
2184 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2185 zl.route.type, zl.route.instance);
2186 }
2187 }
2188
2189 /*
2190 * Handle request to add an MPLS LSP or change an existing one.
2191 *
2192 * A single message can fully specify an LSP with multiple nexthops.
2193 *
2194 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
2195 * updated to use the received label(s).
2196 *
2197 * NOTE: zebra will use route replace semantics (make-before-break) to update
2198 * the LSP in the forwarding plane if that's supported by the underlying
2199 * platform.
2200 */
2201 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS)
2202 {
2203 struct stream *s;
2204 struct zapi_labels zl;
2205
2206 /* Get input stream. */
2207 s = msg;
2208 if (zapi_labels_decode(s, &zl) < 0) {
2209 if (IS_ZEBRA_DEBUG_RECV)
2210 zlog_debug("%s: Unable to decode zapi_labels sent",
2211 __func__);
2212 return;
2213 }
2214
2215 if (!mpls_enabled)
2216 return;
2217
2218 /* Validate; will debug on failure */
2219 if (zapi_labels_validate(&zl) < 0)
2220 return;
2221
2222 /* This removes everything, then re-adds from the client's
2223 * zapi message. Since the LSP will be processed later, on this
2224 * this same pthread, all of the changes will 'appear' at once.
2225 */
2226 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
2227 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
2228 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
2229 zl.route.type, zl.route.instance);
2230
2231 mpls_zapi_labels_process(true, zvrf, &zl);
2232 }
2233
2234 static void zread_sr_policy_set(ZAPI_HANDLER_ARGS)
2235 {
2236 struct stream *s;
2237 struct zapi_sr_policy zp;
2238 struct zapi_srte_tunnel *zt;
2239 struct zebra_sr_policy *policy;
2240
2241 /* Get input stream. */
2242 s = msg;
2243 if (zapi_sr_policy_decode(s, &zp) < 0) {
2244 if (IS_ZEBRA_DEBUG_RECV)
2245 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2246 __PRETTY_FUNCTION__);
2247 return;
2248 }
2249 zt = &zp.segment_list;
2250 if (zt->label_num < 1) {
2251 if (IS_ZEBRA_DEBUG_RECV)
2252 zlog_debug(
2253 "%s: SR-TE tunnel must contain at least one label",
2254 __PRETTY_FUNCTION__);
2255 return;
2256 }
2257
2258 if (!mpls_enabled)
2259 return;
2260
2261 policy = zebra_sr_policy_find(zp.color, &zp.endpoint);
2262 if (!policy)
2263 policy = zebra_sr_policy_add(zp.color, &zp.endpoint, zp.name);
2264 /* TODO: per-VRF list of SR-TE policies. */
2265 policy->zvrf = zvrf;
2266
2267 zebra_sr_policy_validate(policy, &zp.segment_list);
2268 }
2269
2270 static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS)
2271 {
2272 struct stream *s;
2273 struct zapi_sr_policy zp;
2274 struct zebra_sr_policy *policy;
2275
2276 /* Get input stream. */
2277 s = msg;
2278 if (zapi_sr_policy_decode(s, &zp) < 0) {
2279 if (IS_ZEBRA_DEBUG_RECV)
2280 zlog_debug("%s: Unable to decode zapi_sr_policy sent",
2281 __PRETTY_FUNCTION__);
2282 return;
2283 }
2284
2285 if (!mpls_enabled)
2286 return;
2287
2288 policy = zebra_sr_policy_find(zp.color, &zp.endpoint);
2289 if (!policy) {
2290 if (IS_ZEBRA_DEBUG_RECV)
2291 zlog_debug("%s: Unable to find SR-TE policy",
2292 __PRETTY_FUNCTION__);
2293 return;
2294 }
2295
2296 zebra_sr_policy_del(policy);
2297 }
2298
2299 int zsend_sr_policy_notify_status(uint32_t color, struct ipaddr *endpoint,
2300 char *name, int status)
2301 {
2302 struct zserv *client;
2303 struct stream *s;
2304
2305 client = zserv_find_client(ZEBRA_ROUTE_SRTE, 0);
2306 if (!client) {
2307 if (IS_ZEBRA_DEBUG_PACKET)
2308 zlog_debug(
2309 "Not notifying pathd about policy %s"
2310 " status change to %d",
2311 name, status);
2312 return 0;
2313 }
2314
2315 if (IS_ZEBRA_DEBUG_PACKET)
2316 zlog_debug(
2317 "Notifying pathd about policy %s status change"
2318 " to %d",
2319 name, status);
2320
2321 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
2322 stream_reset(s);
2323
2324 zclient_create_header(s, ZEBRA_SR_POLICY_NOTIFY_STATUS, VRF_DEFAULT);
2325 stream_putl(s, color);
2326 stream_put_ipaddr(s, endpoint);
2327 stream_write(s, name, SRTE_POLICY_NAME_MAX_LENGTH);
2328 stream_putl(s, status);
2329
2330 stream_putw_at(s, 0, stream_get_endp(s));
2331
2332 return zserv_send_message(client, s);
2333 }
2334
2335 /* Send response to a table manager connect request to client */
2336 static void zread_table_manager_connect(struct zserv *client,
2337 struct stream *msg, vrf_id_t vrf_id)
2338 {
2339 struct stream *s;
2340 uint8_t proto;
2341 uint16_t instance;
2342 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
2343
2344 s = msg;
2345
2346 /* Get data. */
2347 STREAM_GETC(s, proto);
2348 STREAM_GETW(s, instance);
2349
2350 /* accept only dynamic routing protocols */
2351 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2352 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
2353 "client %d has wrong protocol %s", client->sock,
2354 zebra_route_string(proto));
2355 zsend_table_manager_connect_response(client, vrf_id, 1);
2356 return;
2357 }
2358 zlog_notice("client %d with vrf %s(%u) instance %u connected as %s",
2359 client->sock, VRF_LOGNAME(vrf), vrf_id, instance,
2360 zebra_route_string(proto));
2361 client->proto = proto;
2362 client->instance = instance;
2363
2364 /*
2365 * Release previous labels of same protocol and instance.
2366 * This is done in case it restarted from an unexpected shutdown.
2367 */
2368 release_daemon_table_chunks(client);
2369
2370 zsend_table_manager_connect_response(client, vrf_id, 0);
2371
2372 stream_failure:
2373 return;
2374 }
2375
2376 static void zread_label_manager_connect(struct zserv *client,
2377 struct stream *msg, vrf_id_t vrf_id)
2378 {
2379 struct stream *s;
2380 /* type of protocol (lib/zebra.h) */
2381 uint8_t proto;
2382 unsigned short instance;
2383
2384 /* Get input stream. */
2385 s = msg;
2386
2387 /* Get data. */
2388 STREAM_GETC(s, proto);
2389 STREAM_GETW(s, instance);
2390
2391 /* accept only dynamic routing protocols */
2392 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
2393 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
2394 "client %d has wrong protocol %s", client->sock,
2395 zebra_route_string(proto));
2396 zsend_label_manager_connect_response(client, vrf_id, 1);
2397 return;
2398 }
2399
2400 /* recall proto and instance in this socket */
2401 client->proto = proto;
2402 client->instance = instance;
2403
2404 /* call hook for connection using wrapper */
2405 lm_client_connect_call(client, vrf_id);
2406
2407 stream_failure:
2408 return;
2409 }
2410
2411 static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
2412 vrf_id_t vrf_id)
2413 {
2414 struct stream *s;
2415 uint8_t keep;
2416 uint32_t size, base;
2417 struct label_manager_chunk *lmc = NULL;
2418 uint8_t proto;
2419 unsigned short instance;
2420
2421 /* Get input stream. */
2422 s = msg;
2423
2424 /* Get data. */
2425 STREAM_GETC(s, proto);
2426 STREAM_GETW(s, instance);
2427 STREAM_GETC(s, keep);
2428 STREAM_GETL(s, size);
2429 STREAM_GETL(s, base);
2430
2431 assert(proto == client->proto && instance == client->instance);
2432
2433 /* call hook to get a chunk using wrapper */
2434 lm_get_chunk_call(&lmc, client, keep, size, base, vrf_id);
2435
2436 stream_failure:
2437 return;
2438 }
2439
2440 static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2441 {
2442 struct stream *s;
2443 uint32_t start, end;
2444 uint8_t proto;
2445 unsigned short instance;
2446
2447 /* Get input stream. */
2448 s = msg;
2449
2450 /* Get data. */
2451 STREAM_GETC(s, proto);
2452 STREAM_GETW(s, instance);
2453 STREAM_GETL(s, start);
2454 STREAM_GETL(s, end);
2455
2456 assert(proto == client->proto && instance == client->instance);
2457
2458 /* call hook to release a chunk using wrapper */
2459 lm_release_chunk_call(client, start, end);
2460
2461 stream_failure:
2462 return;
2463 }
2464
2465 static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2466 {
2467 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT
2468 || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
2469 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
2470 else {
2471 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2472 zread_get_label_chunk(client, msg, zvrf_id(zvrf));
2473 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2474 zread_release_label_chunk(client, msg);
2475 }
2476 }
2477
2478 static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
2479 vrf_id_t vrf_id)
2480 {
2481 struct stream *s;
2482 uint32_t size;
2483 struct table_manager_chunk *tmc;
2484
2485 /* Get input stream. */
2486 s = msg;
2487
2488 /* Get data. */
2489 STREAM_GETL(s, size);
2490
2491 tmc = assign_table_chunk(client->proto, client->instance, size);
2492 if (!tmc)
2493 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
2494 "%s: Unable to assign Table Chunk of size %u",
2495 __func__, size);
2496 else
2497 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2498 tmc->end);
2499 /* send response back */
2500 zsend_assign_table_chunk_response(client, vrf_id, tmc);
2501
2502 stream_failure:
2503 return;
2504 }
2505
2506 static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
2507 {
2508 struct stream *s;
2509 uint32_t start, end;
2510
2511 /* Get input stream. */
2512 s = msg;
2513
2514 /* Get data. */
2515 STREAM_GETL(s, start);
2516 STREAM_GETL(s, end);
2517
2518 release_table_chunk(client->proto, client->instance, start, end);
2519
2520 stream_failure:
2521 return;
2522 }
2523
2524 static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2525 {
2526 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2527 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2528 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2529 else {
2530 /* Sanity: don't allow 'unidentified' requests */
2531 if (!client->proto) {
2532 flog_err(
2533 EC_ZEBRA_TM_ALIENS,
2534 "Got table request from an unidentified client");
2535 return;
2536 }
2537 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2538 zread_get_table_chunk(client, msg, zvrf_id(zvrf));
2539 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2540 zread_release_table_chunk(client, msg);
2541 }
2542 }
2543
2544 static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2545 {
2546 struct stream *s;
2547 char ifname[IF_NAMESIZE];
2548 ifindex_t ifindex;
2549 int type;
2550 int af;
2551 union g_addr nexthop;
2552 uint32_t local_label;
2553 uint32_t remote_label;
2554 uint8_t flags;
2555 union pw_protocol_fields data;
2556 uint8_t protocol;
2557 struct zebra_pw *pw;
2558
2559 /* Get input stream. */
2560 s = msg;
2561
2562 /* Get data. */
2563 STREAM_GET(ifname, s, IF_NAMESIZE);
2564 ifname[IF_NAMESIZE - 1] = '\0';
2565 STREAM_GETL(s, ifindex);
2566 STREAM_GETL(s, type);
2567 STREAM_GETL(s, af);
2568 switch (af) {
2569 case AF_INET:
2570 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2571 break;
2572 case AF_INET6:
2573 STREAM_GET(&nexthop.ipv6, s, 16);
2574 break;
2575 default:
2576 return;
2577 }
2578 STREAM_GETL(s, local_label);
2579 STREAM_GETL(s, remote_label);
2580 STREAM_GETC(s, flags);
2581 STREAM_GET(&data, s, sizeof(data));
2582 protocol = client->proto;
2583
2584 pw = zebra_pw_find(zvrf, ifname);
2585 switch (hdr->command) {
2586 case ZEBRA_PW_ADD:
2587 if (pw) {
2588 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
2589 "%s: pseudowire %s already exists [%s]",
2590 __func__, ifname,
2591 zserv_command_string(hdr->command));
2592 return;
2593 }
2594
2595 zebra_pw_add(zvrf, ifname, protocol, client);
2596 break;
2597 case ZEBRA_PW_DELETE:
2598 if (!pw) {
2599 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2600 "%s: pseudowire %s not found [%s]", __func__,
2601 ifname, zserv_command_string(hdr->command));
2602 return;
2603 }
2604
2605 zebra_pw_del(zvrf, pw);
2606 break;
2607 case ZEBRA_PW_SET:
2608 case ZEBRA_PW_UNSET:
2609 if (!pw) {
2610 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2611 "%s: pseudowire %s not found [%s]", __func__,
2612 ifname, zserv_command_string(hdr->command));
2613 return;
2614 }
2615
2616 switch (hdr->command) {
2617 case ZEBRA_PW_SET:
2618 pw->enabled = 1;
2619 break;
2620 case ZEBRA_PW_UNSET:
2621 pw->enabled = 0;
2622 break;
2623 }
2624
2625 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2626 remote_label, flags, &data);
2627 break;
2628 }
2629
2630 stream_failure:
2631 return;
2632 }
2633
2634 static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2635 {
2636 struct interface *master;
2637 struct interface *slave;
2638 struct stream *s = msg;
2639 int ifindex;
2640 vrf_id_t vrf_id;
2641
2642 STREAM_GETL(s, vrf_id);
2643 STREAM_GETL(s, ifindex);
2644 master = if_lookup_by_index(ifindex, vrf_id);
2645
2646 STREAM_GETL(s, vrf_id);
2647 STREAM_GETL(s, ifindex);
2648 slave = if_lookup_by_index(ifindex, vrf_id);
2649
2650 if (!master || !slave)
2651 return;
2652
2653 kernel_interface_set_master(master, slave);
2654
2655 stream_failure:
2656 return;
2657 }
2658
2659
2660 static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2661 {
2662 struct interface *ifp;
2663 mpls_label_t nlabel;
2664 afi_t afi;
2665 struct stream *s;
2666 struct zebra_vrf *def_zvrf;
2667 enum lsp_types_t ltype;
2668
2669 s = msg;
2670 STREAM_GETL(s, nlabel);
2671 STREAM_GETC(s, afi);
2672
2673 if (!(IS_VALID_AFI(afi))) {
2674 zlog_warn("Invalid AFI for VRF label: %u", afi);
2675 return;
2676 }
2677
2678 if (nlabel == zvrf->label[afi]) {
2679 /*
2680 * Nothing to do here move along
2681 */
2682 return;
2683 }
2684
2685 STREAM_GETC(s, ltype);
2686
2687 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2688 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2689 else
2690 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2691
2692 if (!ifp) {
2693 zlog_debug("Unable to find specified Interface for %s",
2694 zvrf->vrf->name);
2695 return;
2696 }
2697
2698 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2699
2700 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2701 afi_t scrubber;
2702 bool really_remove;
2703
2704 really_remove = true;
2705 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2706 if (scrubber == afi)
2707 continue;
2708
2709 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2710 continue;
2711
2712 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2713 really_remove = false;
2714 break;
2715 }
2716 }
2717
2718 if (really_remove)
2719 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2720 NEXTHOP_TYPE_IFINDEX, NULL,
2721 ifp->ifindex, false /*backup*/);
2722 }
2723
2724 if (nlabel != MPLS_LABEL_NONE) {
2725 mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL;
2726 mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label,
2727 NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex);
2728 }
2729
2730 zvrf->label[afi] = nlabel;
2731 stream_failure:
2732 return;
2733 }
2734
2735 static inline void zread_rule(ZAPI_HANDLER_ARGS)
2736 {
2737 struct zebra_pbr_rule zpr;
2738 struct stream *s;
2739 uint32_t total, i;
2740
2741 s = msg;
2742 STREAM_GETL(s, total);
2743
2744 for (i = 0; i < total; i++) {
2745 memset(&zpr, 0, sizeof(zpr));
2746
2747 zpr.sock = client->sock;
2748 zpr.rule.vrf_id = hdr->vrf_id;
2749 STREAM_GETL(s, zpr.rule.seq);
2750 STREAM_GETL(s, zpr.rule.priority);
2751 STREAM_GETL(s, zpr.rule.unique);
2752 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2753 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2754 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2755 prefix_blen(&zpr.rule.filter.src_ip));
2756 STREAM_GETW(s, zpr.rule.filter.src_port);
2757 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2758 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2759 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2760 prefix_blen(&zpr.rule.filter.dst_ip));
2761 STREAM_GETW(s, zpr.rule.filter.dst_port);
2762 STREAM_GETC(s, zpr.rule.filter.dsfield);
2763 STREAM_GETL(s, zpr.rule.filter.fwmark);
2764 STREAM_GETL(s, zpr.rule.action.table);
2765 STREAM_GETL(s, zpr.rule.ifindex);
2766
2767 if (zpr.rule.ifindex) {
2768 struct interface *ifp;
2769
2770 ifp = if_lookup_by_index_per_ns(zvrf->zns,
2771 zpr.rule.ifindex);
2772 if (!ifp) {
2773 zlog_debug("Failed to lookup ifindex: %u",
2774 zpr.rule.ifindex);
2775 return;
2776 }
2777
2778 strlcpy(zpr.ifname, ifp->name, sizeof(zpr.ifname));
2779 }
2780
2781 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2782 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2783
2784 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2785 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2786
2787 if (zpr.rule.filter.src_port)
2788 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2789
2790 if (zpr.rule.filter.dst_port)
2791 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2792
2793 if (zpr.rule.filter.dsfield)
2794 zpr.rule.filter.filter_bm |= PBR_FILTER_DSFIELD;
2795
2796 if (zpr.rule.filter.fwmark)
2797 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2798
2799 if (!(zpr.rule.filter.src_ip.family == AF_INET
2800 || zpr.rule.filter.src_ip.family == AF_INET6)) {
2801 zlog_warn(
2802 "Unsupported PBR source IP family: %s (%hhu)",
2803 family2str(zpr.rule.filter.src_ip.family),
2804 zpr.rule.filter.src_ip.family);
2805 return;
2806 }
2807 if (!(zpr.rule.filter.dst_ip.family == AF_INET
2808 || zpr.rule.filter.dst_ip.family == AF_INET6)) {
2809 zlog_warn(
2810 "Unsupported PBR destination IP family: %s (%hhu)",
2811 family2str(zpr.rule.filter.dst_ip.family),
2812 zpr.rule.filter.dst_ip.family);
2813 return;
2814 }
2815
2816
2817 zpr.vrf_id = zvrf->vrf->vrf_id;
2818 if (hdr->command == ZEBRA_RULE_ADD)
2819 zebra_pbr_add_rule(&zpr);
2820 else
2821 zebra_pbr_del_rule(&zpr);
2822 }
2823
2824 stream_failure:
2825 return;
2826 }
2827
2828 static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2829 {
2830 struct zebra_pbr_ipset zpi;
2831 struct stream *s;
2832 uint32_t total, i;
2833
2834 s = msg;
2835 STREAM_GETL(s, total);
2836
2837 for (i = 0; i < total; i++) {
2838 memset(&zpi, 0, sizeof(zpi));
2839
2840 zpi.sock = client->sock;
2841 zpi.vrf_id = zvrf->vrf->vrf_id;
2842 STREAM_GETL(s, zpi.unique);
2843 STREAM_GETL(s, zpi.type);
2844 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2845
2846 if (hdr->command == ZEBRA_IPSET_CREATE)
2847 zebra_pbr_create_ipset(&zpi);
2848 else
2849 zebra_pbr_destroy_ipset(&zpi);
2850 }
2851
2852 stream_failure:
2853 return;
2854 }
2855
2856 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2857 {
2858 struct zebra_pbr_ipset_entry zpi;
2859 struct zebra_pbr_ipset ipset;
2860 struct stream *s;
2861 uint32_t total, i;
2862
2863 s = msg;
2864 STREAM_GETL(s, total);
2865
2866 for (i = 0; i < total; i++) {
2867 memset(&zpi, 0, sizeof(zpi));
2868 memset(&ipset, 0, sizeof(ipset));
2869
2870 zpi.sock = client->sock;
2871 STREAM_GETL(s, zpi.unique);
2872 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2873 ipset.ipset_name[ZEBRA_IPSET_NAME_SIZE - 1] = '\0';
2874 STREAM_GETC(s, zpi.src.family);
2875 STREAM_GETC(s, zpi.src.prefixlen);
2876 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
2877 STREAM_GETC(s, zpi.dst.family);
2878 STREAM_GETC(s, zpi.dst.prefixlen);
2879 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
2880
2881 STREAM_GETW(s, zpi.src_port_min);
2882 STREAM_GETW(s, zpi.src_port_max);
2883 STREAM_GETW(s, zpi.dst_port_min);
2884 STREAM_GETW(s, zpi.dst_port_max);
2885 STREAM_GETC(s, zpi.proto);
2886 if (!is_default_prefix(&zpi.src))
2887 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2888
2889 if (!is_default_prefix(&zpi.dst))
2890 zpi.filter_bm |= PBR_FILTER_DST_IP;
2891 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2892 zpi.filter_bm |= PBR_FILTER_DST_PORT;
2893 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2894 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
2895 if (zpi.dst_port_max != 0)
2896 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
2897 if (zpi.src_port_max != 0)
2898 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
2899 if (zpi.proto != 0)
2900 zpi.filter_bm |= PBR_FILTER_PROTO;
2901
2902 if (!(zpi.dst.family == AF_INET
2903 || zpi.dst.family == AF_INET6)) {
2904 zlog_warn(
2905 "Unsupported PBR destination IP family: %s (%hhu)",
2906 family2str(zpi.dst.family), zpi.dst.family);
2907 goto stream_failure;
2908 }
2909 if (!(zpi.src.family == AF_INET
2910 || zpi.src.family == AF_INET6)) {
2911 zlog_warn(
2912 "Unsupported PBR source IP family: %s (%hhu)",
2913 family2str(zpi.src.family), zpi.src.family);
2914 goto stream_failure;
2915 }
2916
2917 /* calculate backpointer */
2918 zpi.backpointer =
2919 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
2920
2921 if (!zpi.backpointer) {
2922 zlog_warn("ipset name specified: %s does not exist",
2923 ipset.ipset_name);
2924 goto stream_failure;
2925 }
2926
2927 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
2928 zebra_pbr_add_ipset_entry(&zpi);
2929 else
2930 zebra_pbr_del_ipset_entry(&zpi);
2931 }
2932
2933 stream_failure:
2934 return;
2935 }
2936
2937 static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2938 {
2939 struct zebra_pbr_iptable *zpi =
2940 XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
2941 struct stream *s;
2942
2943 s = msg;
2944
2945 zpi->interface_name_list = list_new();
2946 zpi->sock = client->sock;
2947 zpi->vrf_id = zvrf->vrf->vrf_id;
2948 STREAM_GETL(s, zpi->unique);
2949 STREAM_GETL(s, zpi->type);
2950 STREAM_GETL(s, zpi->filter_bm);
2951 STREAM_GETL(s, zpi->action);
2952 STREAM_GETL(s, zpi->fwmark);
2953 STREAM_GET(&zpi->ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2954 STREAM_GETW(s, zpi->pkt_len_min);
2955 STREAM_GETW(s, zpi->pkt_len_max);
2956 STREAM_GETW(s, zpi->tcp_flags);
2957 STREAM_GETW(s, zpi->tcp_mask_flags);
2958 STREAM_GETC(s, zpi->dscp_value);
2959 STREAM_GETC(s, zpi->fragment);
2960 STREAM_GETC(s, zpi->protocol);
2961 STREAM_GETL(s, zpi->nb_interface);
2962 zebra_pbr_iptable_update_interfacelist(s, zpi);
2963
2964 if (hdr->command == ZEBRA_IPTABLE_ADD)
2965 zebra_pbr_add_iptable(zpi);
2966 else
2967 zebra_pbr_del_iptable(zpi);
2968
2969 stream_failure:
2970 zebra_pbr_iptable_free(zpi);
2971 zpi = NULL;
2972 return;
2973 }
2974
2975 static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS)
2976 {
2977 struct stream *s;
2978 ifindex_t ifindex;
2979 struct interface *ifp;
2980 struct prefix p;
2981 struct ipaddr ip;
2982
2983 s = msg;
2984
2985 STREAM_GETL(s, ifindex);
2986
2987 ifp = if_lookup_by_index_per_ns(zvrf->zns, ifindex);
2988 if (!ifp) {
2989 zlog_debug("Failed to lookup ifindex: %u", ifindex);
2990 return;
2991 }
2992
2993 STREAM_GETC(s, p.family);
2994 STREAM_GETC(s, p.prefixlen);
2995 STREAM_GET(&p.u.prefix, s, prefix_blen(&p));
2996
2997 if (p.family == AF_INET)
2998 SET_IPADDR_V4(&ip);
2999 else
3000 SET_IPADDR_V6(&ip);
3001
3002 memcpy(&ip.ip.addr, &p.u.prefix, prefix_blen(&p));
3003
3004 dplane_neigh_discover(ifp, &ip);
3005
3006 stream_failure:
3007 return;
3008 }
3009
3010 static void zsend_error_msg(struct zserv *client, enum zebra_error_types error,
3011 struct zmsghdr *bad_hdr)
3012 {
3013
3014 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
3015
3016 zclient_create_header(s, ZEBRA_ERROR, bad_hdr->vrf_id);
3017
3018 zserv_encode_error(s, error);
3019
3020 client->error_cnt++;
3021 zserv_send_message(client, s);
3022 }
3023
3024 static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS)
3025 {
3026 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3027 zlog_debug("ZAPI message specifies unknown VRF: %d",
3028 hdr->vrf_id);
3029
3030 zsend_error_msg(client, ZEBRA_NO_VRF, hdr);
3031 }
3032
3033 static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS)
3034 {
3035 zlog_info("Zebra received unknown command %d", hdr->command);
3036
3037 zsend_error_msg(client, ZEBRA_INVALID_MSG_TYPE, hdr);
3038 }
3039
3040 void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
3041 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
3042 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
3043 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
3044 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
3045 [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown,
3046 [ZEBRA_ROUTE_ADD] = zread_route_add,
3047 [ZEBRA_ROUTE_DELETE] = zread_route_del,
3048 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
3049 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
3050 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
3051 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
3052 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
3053 [ZEBRA_HELLO] = zread_hello,
3054 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
3055 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
3056 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
3057 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
3058 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
3059 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
3060 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
3061 #if HAVE_BFDD > 0
3062 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
3063 #endif /* HAVE_BFDD */
3064 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
3065 [ZEBRA_VRF_LABEL] = zread_vrf_label,
3066 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
3067 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
3068 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
3069 [ZEBRA_SR_POLICY_SET] = zread_sr_policy_set,
3070 [ZEBRA_SR_POLICY_DELETE] = zread_sr_policy_delete,
3071 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add,
3072 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete,
3073 [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace,
3074 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
3075 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
3076 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
3077 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
3078 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
3079 [ZEBRA_FEC_REGISTER] = zread_fec_register,
3080 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
3081 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
3082 [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip,
3083 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
3084 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
3085 [ZEBRA_REMOTE_ES_VTEP_ADD] = zebra_evpn_proc_remote_es,
3086 [ZEBRA_REMOTE_ES_VTEP_DEL] = zebra_evpn_proc_remote_es,
3087 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
3088 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
3089 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
3090 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
3091 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
3092 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
3093 [ZEBRA_PW_ADD] = zread_pseudowire,
3094 [ZEBRA_PW_DELETE] = zread_pseudowire,
3095 [ZEBRA_PW_SET] = zread_pseudowire,
3096 [ZEBRA_PW_UNSET] = zread_pseudowire,
3097 [ZEBRA_RULE_ADD] = zread_rule,
3098 [ZEBRA_RULE_DELETE] = zread_rule,
3099 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
3100 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
3101 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
3102 [ZEBRA_IPSET_CREATE] = zread_ipset,
3103 [ZEBRA_IPSET_DESTROY] = zread_ipset,
3104 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
3105 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
3106 [ZEBRA_IPTABLE_ADD] = zread_iptable,
3107 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
3108 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
3109 [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay,
3110 [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register,
3111 [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister,
3112 [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg,
3113 [ZEBRA_CLIENT_CAPABILITIES] = zread_client_capabilities,
3114 [ZEBRA_NEIGH_DISCOVER] = zread_neigh_discover};
3115
3116 #if defined(HANDLE_ZAPI_FUZZING)
3117 extern struct zebra_privs_t zserv_privs;
3118
3119 static void zserv_write_incoming(struct stream *orig, uint16_t command)
3120 {
3121 char fname[MAXPATHLEN];
3122 struct stream *copy;
3123 int fd = -1;
3124
3125 copy = stream_dup(orig);
3126 stream_set_getp(copy, 0);
3127
3128 snprintf(fname, MAXPATHLEN, "%s/%u", frr_vtydir, command);
3129
3130 frr_with_privs(&zserv_privs) {
3131 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
3132 }
3133 stream_flush(copy, fd);
3134 close(fd);
3135 stream_free(copy);
3136 }
3137 #endif
3138
3139 /*
3140 * Process a batch of zapi messages.
3141 */
3142 void zserv_handle_commands(struct zserv *client, struct stream_fifo *fifo)
3143 {
3144 struct zmsghdr hdr;
3145 struct zebra_vrf *zvrf;
3146 struct stream *msg;
3147 struct stream_fifo temp_fifo;
3148
3149 stream_fifo_init(&temp_fifo);
3150
3151 while (stream_fifo_head(fifo)) {
3152 msg = stream_fifo_pop(fifo);
3153
3154 if (STREAM_READABLE(msg) > ZEBRA_MAX_PACKET_SIZ) {
3155 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3156 zlog_debug(
3157 "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping",
3158 STREAM_READABLE(msg),
3159 ZEBRA_MAX_PACKET_SIZ);
3160 goto continue_loop;
3161 }
3162
3163 zapi_parse_header(msg, &hdr);
3164
3165 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
3166 zserv_log_message(NULL, msg, &hdr);
3167
3168 #if defined(HANDLE_ZAPI_FUZZING)
3169 zserv_write_incoming(msg, hdr.command);
3170 #endif
3171
3172 hdr.length -= ZEBRA_HEADER_SIZE;
3173
3174 /* Before checking for a handler function, check for
3175 * special messages that are handled in another module;
3176 * we'll treat these as opaque.
3177 */
3178 if (zebra_opaque_handles_msgid(hdr.command)) {
3179 /* Reset message buffer */
3180 stream_set_getp(msg, 0);
3181
3182 stream_fifo_push(&temp_fifo, msg);
3183
3184 /* Continue without freeing the message */
3185 msg = NULL;
3186 goto continue_loop;
3187 }
3188
3189 /* lookup vrf */
3190 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
3191 if (!zvrf) {
3192 zserv_error_no_vrf(client, &hdr, msg, zvrf);
3193 goto continue_loop;
3194 }
3195
3196 if (hdr.command >= array_size(zserv_handlers)
3197 || zserv_handlers[hdr.command] == NULL) {
3198 zserv_error_invalid_msg_type(client, &hdr, msg, zvrf);
3199 goto continue_loop;
3200 }
3201
3202 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
3203
3204 continue_loop:
3205 stream_free(msg);
3206 }
3207
3208 /* Dispatch any special messages from the temp fifo */
3209 if (stream_fifo_head(&temp_fifo) != NULL)
3210 zebra_opaque_enqueue_batch(&temp_fifo);
3211
3212 stream_fifo_deinit(&temp_fifo);
3213 }