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