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