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