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