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