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