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