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