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