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