]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zapi_msg.c
Merge pull request #5514 from Jafaral/nhrp-docs
[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/command.h"
28 #include "lib/if.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
35 #include "lib/log.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
41 #include "lib/vrf.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
44
45 #include "zebra/zebra_router.h"
46 #include "zebra/rib.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
50 #include "zebra/router-id.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/debug.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/interface.h"
56 #include "zebra/zebra_ptm.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/zebra_mpls.h"
59 #include "zebra/zebra_mroute.h"
60 #include "zebra/zebra_vxlan.h"
61 #include "zebra/rt.h"
62 #include "zebra/zebra_pbr.h"
63 #include "zebra/table_manager.h"
64 #include "zebra/zapi_msg.h"
65 #include "zebra/zebra_errors.h"
66 #include "zebra/zebra_mlag.h"
67
68 /* Encoding helpers -------------------------------------------------------- */
69
70 static void zserv_encode_interface(struct stream *s, struct interface *ifp)
71 {
72 /* Interface information. */
73 struct zebra_if *zif = ifp->info;
74
75 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
76 stream_putl(s, ifp->ifindex);
77 stream_putc(s, ifp->status);
78 stream_putq(s, ifp->flags);
79 stream_putc(s, ifp->ptm_enable);
80 stream_putc(s, ifp->ptm_status);
81 stream_putl(s, ifp->metric);
82 stream_putl(s, ifp->speed);
83 stream_putl(s, ifp->mtu);
84 stream_putl(s, ifp->mtu6);
85 stream_putl(s, ifp->bandwidth);
86 stream_putl(s, zif->link_ifindex);
87 stream_putl(s, ifp->ll_type);
88 stream_putl(s, ifp->hw_addr_len);
89 if (ifp->hw_addr_len)
90 stream_put(s, ifp->hw_addr, ifp->hw_addr_len);
91
92 /* Then, Traffic Engineering parameters if any */
93 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) {
94 stream_putc(s, 1);
95 zebra_interface_link_params_write(s, ifp);
96 } else
97 stream_putc(s, 0);
98
99 /* Write packet size. */
100 stream_putw_at(s, 0, stream_get_endp(s));
101 }
102
103 static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf)
104 {
105 struct vrf_data data;
106 const char *netns_name = zvrf_ns_name(zvrf);
107
108 data.l.table_id = zvrf->table_id;
109
110 if (netns_name)
111 strlcpy(data.l.netns_name, basename((char *)netns_name),
112 NS_NAMSIZ);
113 else
114 memset(data.l.netns_name, 0, NS_NAMSIZ);
115 /* Pass the tableid and the netns NAME */
116 stream_put(s, &data, sizeof(struct vrf_data));
117 /* Interface information. */
118 stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
119 /* Write packet size. */
120 stream_putw_at(s, 0, stream_get_endp(s));
121 }
122
123 static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
124 {
125 stream_putl(s, nexthop->vrf_id);
126 stream_putc(s, nexthop->type);
127 switch (nexthop->type) {
128 case NEXTHOP_TYPE_IPV4:
129 case NEXTHOP_TYPE_IPV4_IFINDEX:
130 stream_put_in_addr(s, &nexthop->gate.ipv4);
131 stream_putl(s, nexthop->ifindex);
132 break;
133 case NEXTHOP_TYPE_IPV6:
134 stream_put(s, &nexthop->gate.ipv6, 16);
135 break;
136 case NEXTHOP_TYPE_IPV6_IFINDEX:
137 stream_put(s, &nexthop->gate.ipv6, 16);
138 stream_putl(s, nexthop->ifindex);
139 break;
140 case NEXTHOP_TYPE_IFINDEX:
141 stream_putl(s, nexthop->ifindex);
142 break;
143 default:
144 /* do nothing */
145 break;
146 }
147 return 1;
148 }
149
150 /* Send handlers ----------------------------------------------------------- */
151
152 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
153 /*
154 * This function is called in the following situations:
155 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
156 * from the client.
157 * - at startup, when zebra figures out the available interfaces
158 * - when an interface is added (where support for
159 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
160 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
161 * received)
162 */
163 int zsend_interface_add(struct zserv *client, struct interface *ifp)
164 {
165 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
166
167 zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
168 zserv_encode_interface(s, ifp);
169
170 client->ifadd_cnt++;
171 return zserv_send_message(client, s);
172 }
173
174 /* Interface deletion from zebra daemon. */
175 int zsend_interface_delete(struct zserv *client, struct interface *ifp)
176 {
177 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
178
179 zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
180 zserv_encode_interface(s, ifp);
181
182 client->ifdel_cnt++;
183 return zserv_send_message(client, s);
184 }
185
186 int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
187 {
188 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
189
190 zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
191 zserv_encode_vrf(s, zvrf);
192
193 client->vrfadd_cnt++;
194 return zserv_send_message(client, s);
195 }
196
197 /* VRF deletion from zebra daemon. */
198 int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
199
200 {
201 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
202
203 zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
204 zserv_encode_vrf(s, zvrf);
205
206 client->vrfdel_cnt++;
207 return zserv_send_message(client, s);
208 }
209
210 int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
211 {
212 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
213
214 if (!ifp->link_params) {
215 stream_free(s);
216 return 0;
217 }
218
219 zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
220
221 /* Add Interface Index */
222 stream_putl(s, ifp->ifindex);
223
224 /* Then TE Link Parameters */
225 if (zebra_interface_link_params_write(s, ifp) == 0) {
226 stream_free(s);
227 return 0;
228 }
229
230 /* Write packet size. */
231 stream_putw_at(s, 0, stream_get_endp(s));
232
233 return zserv_send_message(client, s);
234 }
235
236 /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
237 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
238 *
239 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
240 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
241 * from the client, after the ZEBRA_INTERFACE_ADD has been
242 * sent from zebra to the client
243 * - redistribute new address info to all clients in the following situations
244 * - at startup, when zebra figures out the available interfaces
245 * - when an interface is added (where support for
246 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
247 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
248 * received)
249 * - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
250 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
251 * - when an RTM_NEWADDR message is received from the kernel,
252 *
253 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
254 *
255 * zsend_interface_address(DELETE)
256 * ^
257 * |
258 * zebra_interface_address_delete_update
259 * ^ ^ ^
260 * | | if_delete_update
261 * | |
262 * ip_address_uninstall connected_delete_ipv4
263 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
264 * ^ ^
265 * | |
266 * | RTM_NEWADDR on routing/netlink socket
267 * |
268 * vty commands:
269 * "no ip address A.B.C.D/M [label LINE]"
270 * "no ip address A.B.C.D/M"
271 * ["no ipv6 address X:X::X:X/M"]
272 *
273 */
274 int zsend_interface_address(int cmd, struct zserv *client,
275 struct interface *ifp, struct connected *ifc)
276 {
277 int blen;
278 struct prefix *p;
279 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
280
281 zclient_create_header(s, cmd, ifp->vrf_id);
282 stream_putl(s, ifp->ifindex);
283
284 /* Interface address flag. */
285 stream_putc(s, ifc->flags);
286
287 /* Prefix information. */
288 p = ifc->address;
289 stream_putc(s, p->family);
290 blen = prefix_blen(p);
291 stream_put(s, &p->u.prefix, blen);
292
293 /*
294 * XXX gnu version does not send prefixlen for
295 * ZEBRA_INTERFACE_ADDRESS_DELETE
296 * but zebra_interface_address_delete_read() in the gnu version
297 * expects to find it
298 */
299 stream_putc(s, p->prefixlen);
300
301 /* Destination. */
302 p = ifc->destination;
303 if (p)
304 stream_put(s, &p->u.prefix, blen);
305 else
306 stream_put(s, NULL, blen);
307
308 /* Write packet size. */
309 stream_putw_at(s, 0, stream_get_endp(s));
310
311 client->connected_rt_add_cnt++;
312 return zserv_send_message(client, s);
313 }
314
315 static int zsend_interface_nbr_address(int cmd, struct zserv *client,
316 struct interface *ifp,
317 struct nbr_connected *ifc)
318 {
319 int blen;
320 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
321 struct prefix *p;
322
323 zclient_create_header(s, cmd, ifp->vrf_id);
324 stream_putl(s, ifp->ifindex);
325
326 /* Prefix information. */
327 p = ifc->address;
328 stream_putc(s, p->family);
329 blen = prefix_blen(p);
330 stream_put(s, &p->u.prefix, blen);
331
332 /*
333 * XXX gnu version does not send prefixlen for
334 * ZEBRA_INTERFACE_ADDRESS_DELETE
335 * but zebra_interface_address_delete_read() in the gnu version
336 * expects to find it
337 */
338 stream_putc(s, p->prefixlen);
339
340 /* Write packet size. */
341 stream_putw_at(s, 0, stream_get_endp(s));
342
343 return zserv_send_message(client, s);
344 }
345
346 /* Interface address addition. */
347 static void zebra_interface_nbr_address_add_update(struct interface *ifp,
348 struct nbr_connected *ifc)
349 {
350 struct listnode *node, *nnode;
351 struct zserv *client;
352 struct prefix *p;
353
354 if (IS_ZEBRA_DEBUG_EVENT) {
355 char buf[INET6_ADDRSTRLEN];
356
357 p = ifc->address;
358 zlog_debug(
359 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
360 inet_ntop(p->family, &p->u.prefix, buf,
361 INET6_ADDRSTRLEN),
362 p->prefixlen, ifc->ifp->name);
363 }
364
365 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
366 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
367 client, ifp, ifc);
368 }
369
370 /* Interface address deletion. */
371 static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
372 struct nbr_connected *ifc)
373 {
374 struct listnode *node, *nnode;
375 struct zserv *client;
376 struct prefix *p;
377
378 if (IS_ZEBRA_DEBUG_EVENT) {
379 char buf[INET6_ADDRSTRLEN];
380
381 p = ifc->address;
382 zlog_debug(
383 "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
384 inet_ntop(p->family, &p->u.prefix, buf,
385 INET6_ADDRSTRLEN),
386 p->prefixlen, ifc->ifp->name);
387 }
388
389 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
390 zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
391 client, ifp, ifc);
392 }
393
394 /* Send addresses on interface to client */
395 int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
396 {
397 struct listnode *cnode, *cnnode;
398 struct connected *c;
399 struct nbr_connected *nc;
400
401 /* Send interface addresses. */
402 for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
403 if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
404 continue;
405
406 if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
407 ifp, c)
408 < 0)
409 return -1;
410 }
411
412 /* Send interface neighbors. */
413 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
414 if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
415 client, ifp, nc)
416 < 0)
417 return -1;
418 }
419
420 return 0;
421 }
422
423 /* Notify client about interface moving from one VRF to another.
424 * Whether client is interested in old and new VRF is checked by caller.
425 */
426 int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
427 vrf_id_t vrf_id)
428 {
429 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
430
431 zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
432
433 /* Fill in the name of the interface and its new VRF (id) */
434 stream_put(s, ifp->name, INTERFACE_NAMSIZ);
435 stream_putl(s, vrf_id);
436
437 /* Write packet size. */
438 stream_putw_at(s, 0, stream_get_endp(s));
439
440 client->if_vrfchg_cnt++;
441 return zserv_send_message(client, s);
442 }
443
444 /* Add new nbr connected IPv6 address */
445 void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
446 {
447 struct nbr_connected *ifc;
448 struct prefix p;
449
450 p.family = AF_INET6;
451 IPV6_ADDR_COPY(&p.u.prefix6, address);
452 p.prefixlen = IPV6_MAX_PREFIXLEN;
453
454 ifc = listnode_head(ifp->nbr_connected);
455 if (!ifc) {
456 /* new addition */
457 ifc = nbr_connected_new();
458 ifc->address = prefix_new();
459 ifc->ifp = ifp;
460 listnode_add(ifp->nbr_connected, ifc);
461 }
462
463 prefix_copy(ifc->address, &p);
464
465 zebra_interface_nbr_address_add_update(ifp, ifc);
466
467 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
468 }
469
470 void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
471 {
472 struct nbr_connected *ifc;
473 struct prefix p;
474
475 p.family = AF_INET6;
476 IPV6_ADDR_COPY(&p.u.prefix6, address);
477 p.prefixlen = IPV6_MAX_PREFIXLEN;
478
479 ifc = nbr_connected_check(ifp, &p);
480 if (!ifc)
481 return;
482
483 listnode_delete(ifp->nbr_connected, ifc);
484
485 zebra_interface_nbr_address_delete_update(ifp, ifc);
486
487 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
488
489 nbr_connected_free(ifc);
490 }
491
492 /*
493 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
494 * ZEBRA_INTERFACE_DOWN.
495 *
496 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
497 * the clients in one of 2 situations:
498 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
499 * - a vty command modifying the bandwidth of an interface is received.
500 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
501 */
502 int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
503 {
504 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
505
506 zclient_create_header(s, cmd, ifp->vrf_id);
507 zserv_encode_interface(s, ifp);
508
509 if (cmd == ZEBRA_INTERFACE_UP)
510 client->ifup_cnt++;
511 else
512 client->ifdown_cnt++;
513
514 return zserv_send_message(client, s);
515 }
516
517 int zsend_redistribute_route(int cmd, struct zserv *client,
518 const struct prefix *p,
519 const struct prefix *src_p,
520 const struct route_entry *re)
521 {
522 struct zapi_route api;
523 struct zapi_nexthop *api_nh;
524 struct nexthop *nexthop;
525 uint8_t count = 0;
526 afi_t afi;
527 size_t stream_size =
528 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
529
530 memset(&api, 0, sizeof(api));
531 api.vrf_id = re->vrf_id;
532 api.type = re->type;
533 api.safi = SAFI_UNICAST;
534 api.instance = re->instance;
535 api.flags = re->flags;
536
537 afi = family2afi(p->family);
538 switch (afi) {
539 case AFI_IP:
540 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
541 client->redist_v4_add_cnt++;
542 else
543 client->redist_v4_del_cnt++;
544 break;
545 case AFI_IP6:
546 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
547 client->redist_v6_add_cnt++;
548 else
549 client->redist_v6_del_cnt++;
550 break;
551 default:
552 break;
553 }
554
555 /* Prefix. */
556 api.prefix = *p;
557 if (src_p) {
558 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
559 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
560 }
561
562 for (nexthop = re->nhe->nhg->nexthop;
563 nexthop; nexthop = nexthop->next) {
564 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
565 continue;
566
567 api_nh = &api.nexthops[count];
568 api_nh->vrf_id = nexthop->vrf_id;
569 api_nh->type = nexthop->type;
570 api_nh->weight = nexthop->weight;
571 switch (nexthop->type) {
572 case NEXTHOP_TYPE_BLACKHOLE:
573 api_nh->bh_type = nexthop->bh_type;
574 break;
575 case NEXTHOP_TYPE_IPV4:
576 api_nh->gate.ipv4 = nexthop->gate.ipv4;
577 break;
578 case NEXTHOP_TYPE_IPV4_IFINDEX:
579 api_nh->gate.ipv4 = nexthop->gate.ipv4;
580 api_nh->ifindex = nexthop->ifindex;
581 break;
582 case NEXTHOP_TYPE_IFINDEX:
583 api_nh->ifindex = nexthop->ifindex;
584 break;
585 case NEXTHOP_TYPE_IPV6:
586 api_nh->gate.ipv6 = nexthop->gate.ipv6;
587 break;
588 case NEXTHOP_TYPE_IPV6_IFINDEX:
589 api_nh->gate.ipv6 = nexthop->gate.ipv6;
590 api_nh->ifindex = nexthop->ifindex;
591 }
592 count++;
593 }
594
595 /* Nexthops. */
596 if (count) {
597 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
598 api.nexthop_num = count;
599 }
600
601 /* Attributes. */
602 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
603 api.distance = re->distance;
604 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
605 api.metric = re->metric;
606 if (re->tag) {
607 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
608 api.tag = re->tag;
609 }
610 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
611 api.mtu = re->mtu;
612
613 struct stream *s = stream_new(stream_size);
614
615 /* Encode route and send. */
616 if (zapi_route_encode(cmd, s, &api) < 0) {
617 stream_free(s);
618 return -1;
619 }
620
621 if (IS_ZEBRA_DEBUG_SEND) {
622 char buf_prefix[PREFIX_STRLEN];
623
624 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
625
626 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
627 __func__, zserv_command_string(cmd),
628 zebra_route_string(client->proto),
629 zebra_route_string(api.type), api.vrf_id,
630 buf_prefix);
631 }
632 return zserv_send_message(client, s);
633 }
634
635 /*
636 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
637 * nexthop is not found on mrib. Returns both route metric and protocol
638 * distance.
639 */
640 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
641 struct in_addr addr,
642 struct route_entry *re,
643 struct zebra_vrf *zvrf)
644 {
645 struct stream *s;
646 unsigned long nump;
647 uint8_t num;
648 struct nexthop *nexthop;
649
650 /* Get output stream. */
651 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
652 stream_reset(s);
653
654 /* Fill in result. */
655 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
656 stream_put_in_addr(s, &addr);
657
658 if (re) {
659 stream_putc(s, re->distance);
660 stream_putl(s, re->metric);
661 num = 0;
662 /* remember position for nexthop_num */
663 nump = stream_get_endp(s);
664 /* reserve room for nexthop_num */
665 stream_putc(s, 0);
666 /*
667 * Only non-recursive routes are elegible to resolve the
668 * nexthop we are looking up. Therefore, we will just iterate
669 * over the top chain of nexthops.
670 */
671 for (nexthop = re->nhe->nhg->nexthop; nexthop;
672 nexthop = nexthop->next)
673 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
674 num += zserv_encode_nexthop(s, nexthop);
675
676 /* store nexthop_num */
677 stream_putc_at(s, nump, num);
678 } else {
679 stream_putc(s, 0); /* distance */
680 stream_putl(s, 0); /* metric */
681 stream_putc(s, 0); /* nexthop_num */
682 }
683
684 stream_putw_at(s, 0, stream_get_endp(s));
685
686 return zserv_send_message(client, s);
687 }
688
689 /*
690 * Common utility send route notification, called from a path using a
691 * route_entry and from a path using a dataplane context.
692 */
693 static int route_notify_internal(const struct prefix *p, int type,
694 uint16_t instance, vrf_id_t vrf_id,
695 uint32_t table_id,
696 enum zapi_route_notify_owner note)
697 {
698 struct zserv *client;
699 struct stream *s;
700 uint8_t blen;
701
702 client = zserv_find_client(type, instance);
703 if (!client || !client->notify_owner) {
704 if (IS_ZEBRA_DEBUG_PACKET) {
705 char buff[PREFIX_STRLEN];
706
707 zlog_debug(
708 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
709 type, prefix2str(p, buff, sizeof(buff)),
710 table_id, note, vrf_id);
711 }
712 return 0;
713 }
714
715 if (IS_ZEBRA_DEBUG_PACKET) {
716 char buff[PREFIX_STRLEN];
717
718 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
719 type, prefix2str(p, buff, sizeof(buff)),
720 table_id, note, vrf_id);
721 }
722
723 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
724 stream_reset(s);
725
726 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
727
728 stream_put(s, &note, sizeof(note));
729
730 stream_putc(s, p->family);
731
732 blen = prefix_blen(p);
733 stream_putc(s, p->prefixlen);
734 stream_put(s, &p->u.prefix, blen);
735
736 stream_putl(s, table_id);
737
738 stream_putw_at(s, 0, stream_get_endp(s));
739
740 return zserv_send_message(client, s);
741 }
742
743 int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p,
744 enum zapi_route_notify_owner note)
745 {
746 return (route_notify_internal(p, re->type, re->instance, re->vrf_id,
747 re->table, note));
748 }
749
750 /*
751 * Route-owner notification using info from dataplane update context.
752 */
753 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
754 enum zapi_route_notify_owner note)
755 {
756 return (route_notify_internal(dplane_ctx_get_dest(ctx),
757 dplane_ctx_get_type(ctx),
758 dplane_ctx_get_instance(ctx),
759 dplane_ctx_get_vrf(ctx),
760 dplane_ctx_get_table(ctx),
761 note));
762 }
763
764 void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
765 enum zapi_rule_notify_owner note)
766 {
767 struct listnode *node;
768 struct zserv *client;
769 struct stream *s;
770
771 if (IS_ZEBRA_DEBUG_PACKET)
772 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
773 rule->rule.unique);
774
775 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
776 if (rule->sock == client->sock)
777 break;
778 }
779
780 if (!client)
781 return;
782
783 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
784
785 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
786 stream_put(s, &note, sizeof(note));
787 stream_putl(s, rule->rule.seq);
788 stream_putl(s, rule->rule.priority);
789 stream_putl(s, rule->rule.unique);
790 stream_putl(s, rule->rule.ifindex);
791
792 stream_putw_at(s, 0, stream_get_endp(s));
793
794 zserv_send_message(client, s);
795 }
796
797 void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
798 enum zapi_ipset_notify_owner note)
799 {
800 struct listnode *node;
801 struct zserv *client;
802 struct stream *s;
803
804 if (IS_ZEBRA_DEBUG_PACKET)
805 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
806 ipset->unique);
807
808 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
809 if (ipset->sock == client->sock)
810 break;
811 }
812
813 if (!client)
814 return;
815
816 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
817
818 zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
819 stream_put(s, &note, sizeof(note));
820 stream_putl(s, ipset->unique);
821 stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
822 stream_putw_at(s, 0, stream_get_endp(s));
823
824 zserv_send_message(client, s);
825 }
826
827 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset,
828 enum zapi_ipset_entry_notify_owner note)
829 {
830 struct listnode *node;
831 struct zserv *client;
832 struct stream *s;
833
834 if (IS_ZEBRA_DEBUG_PACKET)
835 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
836 ipset->unique);
837
838 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
839 if (ipset->sock == client->sock)
840 break;
841 }
842
843 if (!client)
844 return;
845
846 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
847
848 zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT);
849 stream_put(s, &note, sizeof(note));
850 stream_putl(s, ipset->unique);
851 stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE);
852 stream_putw_at(s, 0, stream_get_endp(s));
853
854 zserv_send_message(client, s);
855 }
856
857 void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
858 enum zapi_iptable_notify_owner note)
859 {
860 struct listnode *node;
861 struct zserv *client;
862 struct stream *s;
863
864 if (IS_ZEBRA_DEBUG_PACKET)
865 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
866 iptable->unique);
867
868 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
869 if (iptable->sock == client->sock)
870 break;
871 }
872
873 if (!client)
874 return;
875
876 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
877
878 zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
879 stream_put(s, &note, sizeof(note));
880 stream_putl(s, iptable->unique);
881 stream_putw_at(s, 0, stream_get_endp(s));
882
883 zserv_send_message(client, s);
884 }
885
886 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
887 int zsend_router_id_update(struct zserv *client, struct prefix *p,
888 vrf_id_t vrf_id)
889 {
890 int blen;
891
892 /* Check this client need interface information. */
893 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
894 return 0;
895
896 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
897
898 /* Message type. */
899 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
900
901 /* Prefix information. */
902 stream_putc(s, p->family);
903 blen = prefix_blen(p);
904 stream_put(s, &p->u.prefix, blen);
905 stream_putc(s, p->prefixlen);
906
907 /* Write packet size. */
908 stream_putw_at(s, 0, stream_get_endp(s));
909
910 return zserv_send_message(client, s);
911 }
912
913 /*
914 * Function used by Zebra to send a PW status update to LDP daemon
915 */
916 int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
917 {
918 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
919
920 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
921 stream_write(s, pw->ifname, IF_NAMESIZE);
922 stream_putl(s, pw->ifindex);
923 stream_putl(s, pw->status);
924
925 /* Put length at the first point of the stream. */
926 stream_putw_at(s, 0, stream_get_endp(s));
927
928 return zserv_send_message(client, s);
929 }
930
931 /* Send response to a get label chunk request to client */
932 int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id,
933 uint8_t proto, uint16_t instance,
934 struct label_manager_chunk *lmc)
935 {
936 int ret;
937 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
938
939 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
940 /* proto */
941 stream_putc(s, proto);
942 /* instance */
943 stream_putw(s, instance);
944
945 if (lmc) {
946 /* keep */
947 stream_putc(s, lmc->keep);
948 /* start and end labels */
949 stream_putl(s, lmc->start);
950 stream_putl(s, lmc->end);
951 }
952
953 /* Write packet size. */
954 stream_putw_at(s, 0, stream_get_endp(s));
955
956 ret = writen(client->sock, s->data, stream_get_endp(s));
957 stream_free(s);
958 return ret;
959 }
960
961 /* Send response to a label manager connect request to client */
962 int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id,
963 unsigned short result)
964 {
965 int ret;
966 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
967
968 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
969
970 /* proto */
971 stream_putc(s, client->proto);
972
973 /* instance */
974 stream_putw(s, client->instance);
975
976 /* result */
977 stream_putc(s, result);
978
979 /* Write packet size. */
980 stream_putw_at(s, 0, stream_get_endp(s));
981
982 ret = writen(client->sock, s->data, stream_get_endp(s));
983 stream_free(s);
984
985 return ret;
986 }
987
988 /* Send response to a get table chunk request to client */
989 static int zsend_assign_table_chunk_response(struct zserv *client,
990 vrf_id_t vrf_id,
991 struct table_manager_chunk *tmc)
992 {
993 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
994
995 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
996
997 if (tmc) {
998 /* start and end labels */
999 stream_putl(s, tmc->start);
1000 stream_putl(s, tmc->end);
1001 }
1002
1003 /* Write packet size. */
1004 stream_putw_at(s, 0, stream_get_endp(s));
1005
1006 return zserv_send_message(client, s);
1007 }
1008
1009 static int zsend_table_manager_connect_response(struct zserv *client,
1010 vrf_id_t vrf_id,
1011 uint16_t result)
1012 {
1013 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1014
1015 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
1016
1017 /* result */
1018 stream_putc(s, result);
1019
1020 stream_putw_at(s, 0, stream_get_endp(s));
1021
1022 return zserv_send_message(client, s);
1023 }
1024
1025 /* Inbound message handling ------------------------------------------------ */
1026
1027 const int cmd2type[] = {
1028 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
1029 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
1030 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
1031 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
1032 };
1033
1034 /* Nexthop register */
1035 static void zread_rnh_register(ZAPI_HANDLER_ARGS)
1036 {
1037 struct rnh *rnh;
1038 struct stream *s;
1039 struct prefix p;
1040 unsigned short l = 0;
1041 uint8_t flags = 0;
1042 uint16_t type = cmd2type[hdr->command];
1043 bool exist;
1044 bool flag_changed = false;
1045 uint8_t orig_flags;
1046
1047 if (IS_ZEBRA_DEBUG_NHT)
1048 zlog_debug(
1049 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1050 zebra_route_string(client->proto), hdr->length,
1051 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
1052 zvrf->vrf->vrf_id);
1053
1054 s = msg;
1055
1056 client->nh_reg_time = monotime(NULL);
1057
1058 while (l < hdr->length) {
1059 STREAM_GETC(s, flags);
1060 STREAM_GETW(s, p.family);
1061 STREAM_GETC(s, p.prefixlen);
1062 l += 4;
1063 if (p.family == AF_INET) {
1064 client->v4_nh_watch_add_cnt++;
1065 if (p.prefixlen > IPV4_MAX_BITLEN) {
1066 zlog_debug(
1067 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1068 __PRETTY_FUNCTION__, p.prefixlen);
1069 return;
1070 }
1071 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1072 l += IPV4_MAX_BYTELEN;
1073 } else if (p.family == AF_INET6) {
1074 client->v6_nh_watch_add_cnt++;
1075 if (p.prefixlen > IPV6_MAX_BITLEN) {
1076 zlog_debug(
1077 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1078 __PRETTY_FUNCTION__, p.prefixlen);
1079 return;
1080 }
1081 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1082 l += IPV6_MAX_BYTELEN;
1083 } else {
1084 flog_err(
1085 EC_ZEBRA_UNKNOWN_FAMILY,
1086 "rnh_register: Received unknown family type %d\n",
1087 p.family);
1088 return;
1089 }
1090 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type, &exist);
1091 if (!rnh)
1092 return;
1093
1094 orig_flags = rnh->flags;
1095 if (type == RNH_NEXTHOP_TYPE) {
1096 if (flags
1097 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1098 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1099 else if (!flags
1100 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1101 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1102 } else if (type == RNH_IMPORT_CHECK_TYPE) {
1103 if (flags
1104 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
1105 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1106 else if (!flags
1107 && CHECK_FLAG(rnh->flags,
1108 ZEBRA_NHT_EXACT_MATCH))
1109 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1110 }
1111
1112 if (orig_flags != rnh->flags)
1113 flag_changed = true;
1114
1115 /* Anything not AF_INET/INET6 has been filtered out above */
1116 if (!exist || flag_changed)
1117 zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, type,
1118 &p);
1119
1120 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
1121 }
1122
1123 stream_failure:
1124 return;
1125 }
1126
1127 /* Nexthop register */
1128 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1129 {
1130 struct rnh *rnh;
1131 struct stream *s;
1132 struct prefix p;
1133 unsigned short l = 0;
1134 uint16_t type = cmd2type[hdr->command];
1135
1136 if (IS_ZEBRA_DEBUG_NHT)
1137 zlog_debug(
1138 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1139 zebra_route_string(client->proto), hdr->length,
1140 zvrf->vrf->vrf_id);
1141
1142 s = msg;
1143
1144 while (l < hdr->length) {
1145 uint8_t flags;
1146
1147 STREAM_GETC(s, flags);
1148 if (flags != 0)
1149 goto stream_failure;
1150
1151 STREAM_GETW(s, p.family);
1152 STREAM_GETC(s, p.prefixlen);
1153 l += 4;
1154 if (p.family == AF_INET) {
1155 client->v4_nh_watch_rem_cnt++;
1156 if (p.prefixlen > IPV4_MAX_BITLEN) {
1157 zlog_debug(
1158 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1159 __PRETTY_FUNCTION__, p.prefixlen);
1160 return;
1161 }
1162 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1163 l += IPV4_MAX_BYTELEN;
1164 } else if (p.family == AF_INET6) {
1165 client->v6_nh_watch_rem_cnt++;
1166 if (p.prefixlen > IPV6_MAX_BITLEN) {
1167 zlog_debug(
1168 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1169 __PRETTY_FUNCTION__, p.prefixlen);
1170 return;
1171 }
1172 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1173 l += IPV6_MAX_BYTELEN;
1174 } else {
1175 flog_err(
1176 EC_ZEBRA_UNKNOWN_FAMILY,
1177 "rnh_register: Received unknown family type %d\n",
1178 p.family);
1179 return;
1180 }
1181 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
1182 if (rnh) {
1183 client->nh_dereg_time = monotime(NULL);
1184 zebra_remove_rnh_client(rnh, client, type);
1185 }
1186 }
1187 stream_failure:
1188 return;
1189 }
1190
1191 #define ZEBRA_MIN_FEC_LENGTH 5
1192
1193 /* FEC register */
1194 static void zread_fec_register(ZAPI_HANDLER_ARGS)
1195 {
1196 struct stream *s;
1197 unsigned short l = 0;
1198 struct prefix p;
1199 uint16_t flags;
1200 uint32_t label = MPLS_INVALID_LABEL;
1201 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1202
1203 s = msg;
1204 zvrf = vrf_info_lookup(VRF_DEFAULT);
1205 if (!zvrf)
1206 return;
1207
1208 /*
1209 * The minimum amount of data that can be sent for one fec
1210 * registration
1211 */
1212 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1213 flog_err(
1214 EC_ZEBRA_IRDP_LEN_MISMATCH,
1215 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1216 hdr->length);
1217 return;
1218 }
1219
1220 while (l < hdr->length) {
1221 STREAM_GETW(s, flags);
1222 memset(&p, 0, sizeof(p));
1223 STREAM_GETW(s, p.family);
1224 if (p.family != AF_INET && p.family != AF_INET6) {
1225 flog_err(
1226 EC_ZEBRA_UNKNOWN_FAMILY,
1227 "fec_register: Received unknown family type %d\n",
1228 p.family);
1229 return;
1230 }
1231 STREAM_GETC(s, p.prefixlen);
1232 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1233 || (p.family == AF_INET6
1234 && p.prefixlen > IPV6_MAX_BITLEN)) {
1235 zlog_debug(
1236 "%s: Specified prefix hdr->length: %d is to long for %d",
1237 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1238 return;
1239 }
1240 l += 5;
1241 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1242 l += PSIZE(p.prefixlen);
1243 if (flags & ZEBRA_FEC_REGISTER_LABEL) {
1244 STREAM_GETL(s, label);
1245 l += 4;
1246 } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
1247 STREAM_GETL(s, label_index);
1248 l += 4;
1249 }
1250
1251 zebra_mpls_fec_register(zvrf, &p, label, label_index, client);
1252 }
1253
1254 stream_failure:
1255 return;
1256 }
1257
1258 /* FEC unregister */
1259 static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1260 {
1261 struct stream *s;
1262 unsigned short l = 0;
1263 struct prefix p;
1264 uint16_t flags;
1265
1266 s = msg;
1267 zvrf = vrf_info_lookup(VRF_DEFAULT);
1268 if (!zvrf)
1269 return;
1270
1271 /*
1272 * The minimum amount of data that can be sent for one
1273 * fec unregistration
1274 */
1275 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1276 flog_err(
1277 EC_ZEBRA_IRDP_LEN_MISMATCH,
1278 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1279 hdr->length);
1280 return;
1281 }
1282
1283 while (l < hdr->length) {
1284 STREAM_GETW(s, flags);
1285 if (flags != 0)
1286 goto stream_failure;
1287
1288 memset(&p, 0, sizeof(p));
1289 STREAM_GETW(s, p.family);
1290 if (p.family != AF_INET && p.family != AF_INET6) {
1291 flog_err(
1292 EC_ZEBRA_UNKNOWN_FAMILY,
1293 "fec_unregister: Received unknown family type %d\n",
1294 p.family);
1295 return;
1296 }
1297 STREAM_GETC(s, p.prefixlen);
1298 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1299 || (p.family == AF_INET6
1300 && p.prefixlen > IPV6_MAX_BITLEN)) {
1301 zlog_debug(
1302 "%s: Received prefix hdr->length %d which is greater than %d can support",
1303 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1304 return;
1305 }
1306 l += 5;
1307 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1308 l += PSIZE(p.prefixlen);
1309 zebra_mpls_fec_unregister(zvrf, &p, client);
1310 }
1311
1312 stream_failure:
1313 return;
1314 }
1315
1316
1317 /*
1318 * Register zebra server interface information.
1319 * Send current all interface and address information.
1320 */
1321 static void zread_interface_add(ZAPI_HANDLER_ARGS)
1322 {
1323 struct vrf *vrf;
1324 struct interface *ifp;
1325
1326 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1327 FOR_ALL_INTERFACES (vrf, ifp) {
1328 /* Skip pseudo interface. */
1329 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1330 continue;
1331
1332 zsend_interface_add(client, ifp);
1333 zsend_interface_link_params(client, ifp);
1334 zsend_interface_addresses(client, ifp);
1335 }
1336 }
1337 }
1338
1339 /* Unregister zebra server interface information. */
1340 static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1341 {
1342 }
1343
1344 /*
1345 * Handle message requesting interface be set up or down.
1346 */
1347 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS)
1348 {
1349 ifindex_t ifindex;
1350 struct interface *ifp;
1351 char down;
1352
1353 STREAM_GETL(msg, ifindex);
1354 STREAM_GETC(msg, down);
1355
1356 /* set ifdown */
1357 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex);
1358
1359 if (ifp) {
1360 zlog_info("Setting interface %s (%u): protodown %s", ifp->name,
1361 ifindex, down ? "on" : "off");
1362 zebra_if_set_protodown(ifp, down);
1363 } else {
1364 zlog_warn(
1365 "Cannot set protodown %s for interface %u; does not exist",
1366 down ? "on" : "off", ifindex);
1367 }
1368
1369
1370 stream_failure:
1371 return;
1372 }
1373
1374
1375 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1376 const unsigned int nexthop_num)
1377 {
1378 if (nexthop_num > zrouter.multipath_num) {
1379 char buff[PREFIX2STR_BUFFER];
1380
1381 prefix2str(p, buff, sizeof(buff));
1382 flog_warn(
1383 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
1384 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1385 caller, buff, nexthop_num, zrouter.multipath_num);
1386 }
1387 }
1388
1389 static void zread_route_add(ZAPI_HANDLER_ARGS)
1390 {
1391 struct stream *s;
1392 struct zapi_route api;
1393 struct zapi_nexthop *api_nh;
1394 afi_t afi;
1395 struct prefix_ipv6 *src_p = NULL;
1396 struct route_entry *re;
1397 struct nexthop *nexthop = NULL;
1398 struct nexthop_group *ng = NULL;
1399 int i, ret;
1400 vrf_id_t vrf_id = 0;
1401 struct ipaddr vtep_ip;
1402
1403 s = msg;
1404 if (zapi_route_decode(s, &api) < 0) {
1405 if (IS_ZEBRA_DEBUG_RECV)
1406 zlog_debug("%s: Unable to decode zapi_route sent",
1407 __PRETTY_FUNCTION__);
1408 return;
1409 }
1410
1411 if (IS_ZEBRA_DEBUG_RECV) {
1412 char buf_prefix[PREFIX_STRLEN];
1413
1414 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1415 zlog_debug("%s: p=%s, flags=0x%x",
1416 __func__, buf_prefix, api.flags);
1417 }
1418
1419 /* Allocate new route. */
1420 vrf_id = zvrf_id(zvrf);
1421 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1422 re->type = api.type;
1423 re->instance = api.instance;
1424 re->flags = api.flags;
1425 re->uptime = monotime(NULL);
1426 re->vrf_id = vrf_id;
1427
1428 if (api.tableid)
1429 re->table = api.tableid;
1430 else
1431 re->table = zvrf->table_id;
1432
1433 if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)
1434 || api.nexthop_num == 0) {
1435 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS,
1436 "%s: received a route without nexthops for prefix %pFX from client %s",
1437 __func__, &api.prefix,
1438 zebra_route_string(client->proto));
1439
1440 XFREE(MTYPE_RE, re);
1441 return;
1442 }
1443
1444 /* Use temporary list of nexthops */
1445 ng = nexthop_group_new();
1446
1447 /*
1448 * TBD should _all_ of the nexthop add operations use
1449 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1450 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1451 */
1452 for (i = 0; i < api.nexthop_num; i++) {
1453 api_nh = &api.nexthops[i];
1454 ifindex_t ifindex = 0;
1455
1456 if (IS_ZEBRA_DEBUG_RECV)
1457 zlog_debug("nh type %d", api_nh->type);
1458
1459 switch (api_nh->type) {
1460 case NEXTHOP_TYPE_IFINDEX:
1461 nexthop = nexthop_from_ifindex(api_nh->ifindex,
1462 api_nh->vrf_id);
1463 break;
1464 case NEXTHOP_TYPE_IPV4:
1465 if (IS_ZEBRA_DEBUG_RECV) {
1466 char nhbuf[INET6_ADDRSTRLEN] = {0};
1467
1468 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1469 INET6_ADDRSTRLEN);
1470 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1471 nhbuf, api_nh->vrf_id);
1472 }
1473 nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4,
1474 NULL, api_nh->vrf_id);
1475 break;
1476 case NEXTHOP_TYPE_IPV4_IFINDEX:
1477
1478 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1479 ifindex = api_nh->ifindex;
1480 if (IS_ZEBRA_DEBUG_RECV) {
1481 char nhbuf[INET6_ADDRSTRLEN] = {0};
1482
1483 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1484 INET6_ADDRSTRLEN);
1485 zlog_debug(
1486 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1487 __func__, nhbuf, api_nh->vrf_id,
1488 re->vrf_id, ifindex);
1489 }
1490 nexthop = nexthop_from_ipv4_ifindex(
1491 &api_nh->gate.ipv4, NULL, ifindex,
1492 api_nh->vrf_id);
1493
1494 /* Special handling for IPv4 routes sourced from EVPN:
1495 * the nexthop and associated MAC need to be installed.
1496 */
1497 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1498 vtep_ip.ipa_type = IPADDR_V4;
1499 memcpy(&(vtep_ip.ipaddr_v4),
1500 &(api_nh->gate.ipv4),
1501 sizeof(struct in_addr));
1502 zebra_vxlan_evpn_vrf_route_add(
1503 api_nh->vrf_id, &api_nh->rmac,
1504 &vtep_ip, &api.prefix);
1505 }
1506 break;
1507 case NEXTHOP_TYPE_IPV6:
1508 nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6,
1509 api_nh->vrf_id);
1510 break;
1511 case NEXTHOP_TYPE_IPV6_IFINDEX:
1512 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1513 ifindex = api_nh->ifindex;
1514 nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6,
1515 ifindex,
1516 api_nh->vrf_id);
1517
1518 /* Special handling for IPv6 routes sourced from EVPN:
1519 * the nexthop and associated MAC need to be installed.
1520 */
1521 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1522 vtep_ip.ipa_type = IPADDR_V6;
1523 memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6),
1524 sizeof(struct in6_addr));
1525 zebra_vxlan_evpn_vrf_route_add(
1526 api_nh->vrf_id, &api_nh->rmac,
1527 &vtep_ip, &api.prefix);
1528 }
1529 break;
1530 case NEXTHOP_TYPE_BLACKHOLE:
1531 nexthop = nexthop_from_blackhole(api_nh->bh_type);
1532 break;
1533 }
1534
1535 if (!nexthop) {
1536 flog_warn(
1537 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1538 "%s: Nexthops Specified: %d but we failed to properly create one",
1539 __PRETTY_FUNCTION__, api.nexthop_num);
1540 nexthop_group_delete(&ng);
1541 XFREE(MTYPE_RE, re);
1542 return;
1543 }
1544
1545 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK))
1546 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1547
1548 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT))
1549 nexthop->weight = api_nh->weight;
1550
1551 /* MPLS labels for BGP-LU or Segment Routing */
1552 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
1553 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1554 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1555 enum lsp_types_t label_type;
1556
1557 label_type = lsp_type_from_re_type(client->proto);
1558
1559 if (IS_ZEBRA_DEBUG_RECV) {
1560 zlog_debug(
1561 "%s: adding %d labels of type %d (1st=%u)",
1562 __func__, api_nh->label_num, label_type,
1563 api_nh->labels[0]);
1564 }
1565
1566 nexthop_add_labels(nexthop, label_type,
1567 api_nh->label_num,
1568 &api_nh->labels[0]);
1569 }
1570
1571 /* Add new nexthop to temporary list */
1572 nexthop_group_add_sorted(ng, nexthop);
1573 }
1574
1575 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1576 re->distance = api.distance;
1577 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1578 re->metric = api.metric;
1579 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1580 re->tag = api.tag;
1581 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1582 re->mtu = api.mtu;
1583
1584 afi = family2afi(api.prefix.family);
1585 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1586 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1587 "%s: Received SRC Prefix but afi is not v6",
1588 __PRETTY_FUNCTION__);
1589 nexthop_group_delete(&ng);
1590 XFREE(MTYPE_RE, re);
1591 return;
1592 }
1593 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1594 src_p = &api.src_prefix;
1595
1596 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re, ng);
1597
1598 /* Stats */
1599 switch (api.prefix.family) {
1600 case AF_INET:
1601 if (ret > 0)
1602 client->v4_route_add_cnt++;
1603 else if (ret < 0)
1604 client->v4_route_upd8_cnt++;
1605 break;
1606 case AF_INET6:
1607 if (ret > 0)
1608 client->v6_route_add_cnt++;
1609 else if (ret < 0)
1610 client->v6_route_upd8_cnt++;
1611 break;
1612 }
1613 }
1614
1615 static void zread_route_del(ZAPI_HANDLER_ARGS)
1616 {
1617 struct stream *s;
1618 struct zapi_route api;
1619 afi_t afi;
1620 struct prefix_ipv6 *src_p = NULL;
1621 uint32_t table_id;
1622
1623 s = msg;
1624 if (zapi_route_decode(s, &api) < 0)
1625 return;
1626
1627 afi = family2afi(api.prefix.family);
1628 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1629 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1630 "%s: Received a src prefix while afi is not v6",
1631 __PRETTY_FUNCTION__);
1632 return;
1633 }
1634 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1635 src_p = &api.src_prefix;
1636
1637 if (api.tableid)
1638 table_id = api.tableid;
1639 else
1640 table_id = zvrf->table_id;
1641
1642 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1643 api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric,
1644 api.distance, false);
1645
1646 /* Stats */
1647 switch (api.prefix.family) {
1648 case AF_INET:
1649 client->v4_route_del_cnt++;
1650 break;
1651 case AF_INET6:
1652 client->v6_route_del_cnt++;
1653 break;
1654 }
1655 }
1656
1657 /* MRIB Nexthop lookup for IPv4. */
1658 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1659 {
1660 struct in_addr addr;
1661 struct route_entry *re;
1662
1663 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1664 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1665 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1666
1667 stream_failure:
1668 return;
1669 }
1670
1671 /* Register zebra server router-id information. Send current router-id */
1672 static void zread_router_id_add(ZAPI_HANDLER_ARGS)
1673 {
1674 struct prefix p;
1675
1676 /* Router-id information is needed. */
1677 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
1678
1679 router_id_get(&p, zvrf_id(zvrf));
1680
1681 zsend_router_id_update(client, &p, zvrf_id(zvrf));
1682 }
1683
1684 /* Unregister zebra server router-id information. */
1685 static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
1686 {
1687 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1688 }
1689
1690 static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
1691 {
1692 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1693
1694 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
1695 stream_putl(s, vrf_get_backend());
1696 stream_putc(s, mpls_enabled);
1697 stream_putl(s, zrouter.multipath_num);
1698 stream_putc(s, zebra_mlag_get_role());
1699
1700 stream_putw_at(s, 0, stream_get_endp(s));
1701 zserv_send_message(client, s);
1702 }
1703
1704 void zsend_capabilities_all_clients(void)
1705 {
1706 struct listnode *node, *nnode;
1707 struct zebra_vrf *zvrf;
1708 struct zserv *client;
1709
1710 zvrf = vrf_info_lookup(VRF_DEFAULT);
1711 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
1712 zsend_capabilities(client, zvrf);
1713 }
1714 }
1715
1716 /* Tie up route-type and client->sock */
1717 static void zread_hello(ZAPI_HANDLER_ARGS)
1718 {
1719 /* type of protocol (lib/zebra.h) */
1720 uint8_t proto;
1721 unsigned short instance;
1722 uint8_t notify;
1723
1724 STREAM_GETC(msg, proto);
1725 STREAM_GETW(msg, instance);
1726 STREAM_GETC(msg, notify);
1727 if (notify)
1728 client->notify_owner = true;
1729
1730 /* accept only dynamic routing protocols */
1731 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
1732 zlog_notice(
1733 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1734 client->sock, zebra_route_string(proto),
1735 zvrf->vrf->vrf_id);
1736 if (instance)
1737 zlog_notice("client protocol instance %d", instance);
1738
1739 client->proto = proto;
1740 client->instance = instance;
1741 }
1742
1743 zsend_capabilities(client, zvrf);
1744 zebra_vrf_update_all(client);
1745 stream_failure:
1746 return;
1747 }
1748
1749 /* Unregister all information in a VRF. */
1750 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
1751 {
1752 int i;
1753 afi_t afi;
1754
1755 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
1756 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1757 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1758 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
1759 }
1760 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1761 }
1762
1763 /*
1764 * Handle request to create an MPLS LSP.
1765 *
1766 * A single message can fully specify an LSP with multiple nexthops.
1767 *
1768 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1769 * updated to use the received label(s).
1770 */
1771 static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS)
1772 {
1773 struct stream *s;
1774 struct zapi_labels zl;
1775
1776 /* Get input stream. */
1777 s = msg;
1778 if (zapi_labels_decode(s, &zl) < 0) {
1779 if (IS_ZEBRA_DEBUG_RECV)
1780 zlog_debug("%s: Unable to decode zapi_labels sent",
1781 __PRETTY_FUNCTION__);
1782 return;
1783 }
1784
1785 if (!mpls_enabled)
1786 return;
1787
1788 for (int i = 0; i < zl.nexthop_num; i++) {
1789 struct zapi_nexthop_label *znh;
1790
1791 znh = &zl.nexthops[i];
1792 mpls_lsp_install(zvrf, zl.type, zl.local_label, 1, &znh->label,
1793 znh->type, &znh->address, znh->ifindex);
1794
1795 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
1796 mpls_ftn_update(1, zvrf, zl.type, &zl.route.prefix,
1797 znh->type, &znh->address, znh->ifindex,
1798 zl.route.type, zl.route.instance,
1799 znh->label);
1800 }
1801 }
1802
1803 /*
1804 * Handle request to delete an MPLS LSP.
1805 *
1806 * An LSP is identified by its type and local label. When the received message
1807 * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the
1808 * listed LSP nexthops (aka NHLFEs) are deleted.
1809 *
1810 * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified
1811 * FEC (route) nexthops are deleted.
1812 */
1813 static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS)
1814 {
1815 struct stream *s;
1816 struct zapi_labels zl;
1817
1818 /* Get input stream. */
1819 s = msg;
1820 if (zapi_labels_decode(s, &zl) < 0) {
1821 if (IS_ZEBRA_DEBUG_RECV)
1822 zlog_debug("%s: Unable to decode zapi_labels sent",
1823 __PRETTY_FUNCTION__);
1824 return;
1825 }
1826
1827 if (!mpls_enabled)
1828 return;
1829
1830 if (zl.nexthop_num > 0) {
1831 for (int i = 0; i < zl.nexthop_num; i++) {
1832 struct zapi_nexthop_label *znh;
1833
1834 znh = &zl.nexthops[i];
1835 mpls_lsp_uninstall(zvrf, zl.type, zl.local_label,
1836 znh->type, &znh->address,
1837 znh->ifindex);
1838
1839 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
1840 mpls_ftn_update(0, zvrf, zl.type,
1841 &zl.route.prefix, znh->type,
1842 &znh->address, znh->ifindex,
1843 zl.route.type,
1844 zl.route.instance, znh->label);
1845 }
1846 } else {
1847 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
1848
1849 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
1850 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
1851 zl.route.type, zl.route.instance);
1852 }
1853 }
1854
1855 /*
1856 * Handle request to add an MPLS LSP or change an existing one.
1857 *
1858 * A single message can fully specify an LSP with multiple nexthops.
1859 *
1860 * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is
1861 * updated to use the received label(s).
1862 *
1863 * NOTE: zebra will use route replace semantics (make-before-break) to update
1864 * the LSP in the forwarding plane if that's supported by the underlying
1865 * platform.
1866 */
1867 static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS)
1868 {
1869 struct stream *s;
1870 struct zapi_labels zl;
1871
1872 /* Get input stream. */
1873 s = msg;
1874 if (zapi_labels_decode(s, &zl) < 0) {
1875 if (IS_ZEBRA_DEBUG_RECV)
1876 zlog_debug("%s: Unable to decode zapi_labels sent",
1877 __PRETTY_FUNCTION__);
1878 return;
1879 }
1880
1881 if (!mpls_enabled)
1882 return;
1883
1884 mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label);
1885 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN))
1886 mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix,
1887 zl.route.type, zl.route.instance);
1888
1889 for (int i = 0; i < zl.nexthop_num; i++) {
1890 struct zapi_nexthop_label *znh;
1891
1892 znh = &zl.nexthops[i];
1893 mpls_lsp_install(zvrf, zl.type, zl.local_label, 1, &znh->label,
1894 znh->type, &znh->address, znh->ifindex);
1895
1896 if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) {
1897 mpls_ftn_update(1, zvrf, zl.type, &zl.route.prefix,
1898 znh->type, &znh->address, znh->ifindex,
1899 zl.route.type, zl.route.instance,
1900 znh->label);
1901 }
1902 }
1903 }
1904
1905 /* Send response to a table manager connect request to client */
1906 static void zread_table_manager_connect(struct zserv *client,
1907 struct stream *msg, vrf_id_t vrf_id)
1908 {
1909 struct stream *s;
1910 uint8_t proto;
1911 uint16_t instance;
1912
1913 s = msg;
1914
1915 /* Get data. */
1916 STREAM_GETC(s, proto);
1917 STREAM_GETW(s, instance);
1918
1919 /* accept only dynamic routing protocols */
1920 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1921 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1922 "client %d has wrong protocol %s", client->sock,
1923 zebra_route_string(proto));
1924 zsend_table_manager_connect_response(client, vrf_id, 1);
1925 return;
1926 }
1927 zlog_notice("client %d with vrf %u instance %u connected as %s",
1928 client->sock, vrf_id, instance, zebra_route_string(proto));
1929 client->proto = proto;
1930 client->instance = instance;
1931
1932 /*
1933 * Release previous labels of same protocol and instance.
1934 * This is done in case it restarted from an unexpected shutdown.
1935 */
1936 release_daemon_table_chunks(client);
1937
1938 zsend_table_manager_connect_response(client, vrf_id, 0);
1939
1940 stream_failure:
1941 return;
1942 }
1943
1944 static void zread_label_manager_connect(struct zserv *client,
1945 struct stream *msg, vrf_id_t vrf_id)
1946 {
1947 struct stream *s;
1948 /* type of protocol (lib/zebra.h) */
1949 uint8_t proto;
1950 unsigned short instance;
1951
1952 /* Get input stream. */
1953 s = msg;
1954
1955 /* Get data. */
1956 STREAM_GETC(s, proto);
1957 STREAM_GETW(s, instance);
1958
1959 /* accept only dynamic routing protocols */
1960 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1961 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1962 "client %d has wrong protocol %s", client->sock,
1963 zebra_route_string(proto));
1964 zsend_label_manager_connect_response(client, vrf_id, 1);
1965 return;
1966 }
1967
1968 /* recall proto and instance in this socket */
1969 client->proto = proto;
1970 client->instance = instance;
1971
1972 /* call hook for connection using wrapper */
1973 lm_client_connect_call(proto, instance, vrf_id);
1974
1975 stream_failure:
1976 return;
1977 }
1978
1979 static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
1980 vrf_id_t vrf_id)
1981 {
1982 struct stream *s;
1983 uint8_t keep;
1984 uint32_t size, base;
1985 struct label_manager_chunk *lmc = NULL;
1986 uint8_t proto;
1987 unsigned short instance;
1988
1989 /* Get input stream. */
1990 s = msg;
1991
1992 /* Get data. */
1993 STREAM_GETC(s, proto);
1994 STREAM_GETW(s, instance);
1995 STREAM_GETC(s, keep);
1996 STREAM_GETL(s, size);
1997 STREAM_GETL(s, base);
1998
1999 /* call hook to get a chunk using wrapper */
2000 lm_get_chunk_call(&lmc, proto, instance, keep, size, base, vrf_id);
2001
2002 if (!lmc)
2003 flog_err(
2004 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK,
2005 "Unable to assign Label Chunk of size %u to %s instance %u",
2006 size, zebra_route_string(proto), instance);
2007 else
2008 if (IS_ZEBRA_DEBUG_PACKET)
2009 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
2010 lmc->start, lmc->end,
2011 zebra_route_string(proto), instance);
2012
2013 stream_failure:
2014 return;
2015 }
2016
2017 static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
2018 {
2019 struct stream *s;
2020 uint32_t start, end;
2021 uint8_t proto;
2022 unsigned short instance;
2023
2024 /* Get input stream. */
2025 s = msg;
2026
2027 /* Get data. */
2028 STREAM_GETC(s, proto);
2029 STREAM_GETW(s, instance);
2030 STREAM_GETL(s, start);
2031 STREAM_GETL(s, end);
2032
2033 /* call hook to release a chunk using wrapper */
2034 lm_release_chunk_call(proto, instance, start, end);
2035
2036 stream_failure:
2037 return;
2038 }
2039
2040 static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
2041 {
2042 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT
2043 || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
2044 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
2045 else {
2046 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
2047 zread_get_label_chunk(client, msg, zvrf_id(zvrf));
2048 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
2049 zread_release_label_chunk(client, msg);
2050 }
2051 }
2052
2053 static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
2054 vrf_id_t vrf_id)
2055 {
2056 struct stream *s;
2057 uint32_t size;
2058 struct table_manager_chunk *tmc;
2059
2060 /* Get input stream. */
2061 s = msg;
2062
2063 /* Get data. */
2064 STREAM_GETL(s, size);
2065
2066 tmc = assign_table_chunk(client->proto, client->instance, size);
2067 if (!tmc)
2068 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
2069 "%s: Unable to assign Table Chunk of size %u",
2070 __func__, size);
2071 else
2072 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2073 tmc->end);
2074 /* send response back */
2075 zsend_assign_table_chunk_response(client, vrf_id, tmc);
2076
2077 stream_failure:
2078 return;
2079 }
2080
2081 static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
2082 {
2083 struct stream *s;
2084 uint32_t start, end;
2085
2086 /* Get input stream. */
2087 s = msg;
2088
2089 /* Get data. */
2090 STREAM_GETL(s, start);
2091 STREAM_GETL(s, end);
2092
2093 release_table_chunk(client->proto, client->instance, start, end);
2094
2095 stream_failure:
2096 return;
2097 }
2098
2099 static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2100 {
2101 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2102 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2103 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2104 else {
2105 /* Sanity: don't allow 'unidentified' requests */
2106 if (!client->proto) {
2107 flog_err(
2108 EC_ZEBRA_TM_ALIENS,
2109 "Got table request from an unidentified client");
2110 return;
2111 }
2112 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2113 zread_get_table_chunk(client, msg, zvrf_id(zvrf));
2114 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2115 zread_release_table_chunk(client, msg);
2116 }
2117 }
2118
2119 static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2120 {
2121 struct stream *s;
2122 char ifname[IF_NAMESIZE];
2123 ifindex_t ifindex;
2124 int type;
2125 int af;
2126 union g_addr nexthop;
2127 uint32_t local_label;
2128 uint32_t remote_label;
2129 uint8_t flags;
2130 union pw_protocol_fields data;
2131 uint8_t protocol;
2132 struct zebra_pw *pw;
2133
2134 /* Get input stream. */
2135 s = msg;
2136
2137 /* Get data. */
2138 STREAM_GET(ifname, s, IF_NAMESIZE);
2139 STREAM_GETL(s, ifindex);
2140 STREAM_GETL(s, type);
2141 STREAM_GETL(s, af);
2142 switch (af) {
2143 case AF_INET:
2144 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2145 break;
2146 case AF_INET6:
2147 STREAM_GET(&nexthop.ipv6, s, 16);
2148 break;
2149 default:
2150 return;
2151 }
2152 STREAM_GETL(s, local_label);
2153 STREAM_GETL(s, remote_label);
2154 STREAM_GETC(s, flags);
2155 STREAM_GET(&data, s, sizeof(data));
2156 protocol = client->proto;
2157
2158 pw = zebra_pw_find(zvrf, ifname);
2159 switch (hdr->command) {
2160 case ZEBRA_PW_ADD:
2161 if (pw) {
2162 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
2163 "%s: pseudowire %s already exists [%s]",
2164 __func__, ifname,
2165 zserv_command_string(hdr->command));
2166 return;
2167 }
2168
2169 zebra_pw_add(zvrf, ifname, protocol, client);
2170 break;
2171 case ZEBRA_PW_DELETE:
2172 if (!pw) {
2173 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2174 "%s: pseudowire %s not found [%s]", __func__,
2175 ifname, zserv_command_string(hdr->command));
2176 return;
2177 }
2178
2179 zebra_pw_del(zvrf, pw);
2180 break;
2181 case ZEBRA_PW_SET:
2182 case ZEBRA_PW_UNSET:
2183 if (!pw) {
2184 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2185 "%s: pseudowire %s not found [%s]", __func__,
2186 ifname, zserv_command_string(hdr->command));
2187 return;
2188 }
2189
2190 switch (hdr->command) {
2191 case ZEBRA_PW_SET:
2192 pw->enabled = 1;
2193 break;
2194 case ZEBRA_PW_UNSET:
2195 pw->enabled = 0;
2196 break;
2197 }
2198
2199 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2200 remote_label, flags, &data);
2201 break;
2202 }
2203
2204 stream_failure:
2205 return;
2206 }
2207
2208 static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2209 {
2210 struct interface *master;
2211 struct interface *slave;
2212 struct stream *s = msg;
2213 int ifindex;
2214 vrf_id_t vrf_id;
2215
2216 STREAM_GETL(s, vrf_id);
2217 STREAM_GETL(s, ifindex);
2218 master = if_lookup_by_index(ifindex, vrf_id);
2219
2220 STREAM_GETL(s, vrf_id);
2221 STREAM_GETL(s, ifindex);
2222 slave = if_lookup_by_index(ifindex, vrf_id);
2223
2224 if (!master || !slave)
2225 return;
2226
2227 kernel_interface_set_master(master, slave);
2228
2229 stream_failure:
2230 return;
2231 }
2232
2233
2234 static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2235 {
2236 struct interface *ifp;
2237 mpls_label_t nlabel;
2238 afi_t afi;
2239 struct stream *s;
2240 struct zebra_vrf *def_zvrf;
2241 enum lsp_types_t ltype;
2242
2243 s = msg;
2244 STREAM_GETL(s, nlabel);
2245 STREAM_GETC(s, afi);
2246 if (nlabel == zvrf->label[afi]) {
2247 /*
2248 * Nothing to do here move along
2249 */
2250 return;
2251 }
2252
2253 STREAM_GETC(s, ltype);
2254
2255 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2256 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2257 else
2258 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2259
2260 if (!ifp) {
2261 zlog_debug("Unable to find specified Interface for %s",
2262 zvrf->vrf->name);
2263 return;
2264 }
2265
2266 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2267
2268 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2269 afi_t scrubber;
2270 bool really_remove;
2271
2272 really_remove = true;
2273 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2274 if (scrubber == afi)
2275 continue;
2276
2277 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2278 continue;
2279
2280 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2281 really_remove = false;
2282 break;
2283 }
2284 }
2285
2286 if (really_remove)
2287 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2288 NEXTHOP_TYPE_IFINDEX, NULL,
2289 ifp->ifindex);
2290 }
2291
2292 if (nlabel != MPLS_LABEL_NONE) {
2293 mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL;
2294 mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label,
2295 NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex);
2296 }
2297
2298 zvrf->label[afi] = nlabel;
2299 stream_failure:
2300 return;
2301 }
2302
2303 static inline void zread_rule(ZAPI_HANDLER_ARGS)
2304 {
2305 struct zebra_pbr_rule zpr;
2306 struct stream *s;
2307 uint32_t total, i;
2308
2309 s = msg;
2310 STREAM_GETL(s, total);
2311
2312 for (i = 0; i < total; i++) {
2313 memset(&zpr, 0, sizeof(zpr));
2314
2315 zpr.sock = client->sock;
2316 zpr.rule.vrf_id = hdr->vrf_id;
2317 STREAM_GETL(s, zpr.rule.seq);
2318 STREAM_GETL(s, zpr.rule.priority);
2319 STREAM_GETL(s, zpr.rule.unique);
2320 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2321 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2322 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2323 prefix_blen(&zpr.rule.filter.src_ip));
2324 STREAM_GETW(s, zpr.rule.filter.src_port);
2325 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2326 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2327 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2328 prefix_blen(&zpr.rule.filter.dst_ip));
2329 STREAM_GETW(s, zpr.rule.filter.dst_port);
2330 STREAM_GETL(s, zpr.rule.filter.fwmark);
2331 STREAM_GETL(s, zpr.rule.action.table);
2332 STREAM_GETL(s, zpr.rule.ifindex);
2333
2334 if (zpr.rule.ifindex) {
2335 struct interface *ifp;
2336
2337 ifp = if_lookup_by_index_per_ns(zvrf->zns,
2338 zpr.rule.ifindex);
2339 if (!ifp) {
2340 zlog_debug("Failed to lookup ifindex: %u",
2341 zpr.rule.ifindex);
2342 return;
2343 }
2344
2345 strlcpy(zpr.ifname, ifp->name, sizeof(zpr.ifname));
2346 }
2347
2348 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2349 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2350
2351 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2352 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2353
2354 if (zpr.rule.filter.src_port)
2355 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2356
2357 if (zpr.rule.filter.dst_port)
2358 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2359
2360 if (zpr.rule.filter.fwmark)
2361 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2362
2363 zpr.vrf_id = zvrf->vrf->vrf_id;
2364 if (hdr->command == ZEBRA_RULE_ADD)
2365 zebra_pbr_add_rule(&zpr);
2366 else
2367 zebra_pbr_del_rule(&zpr);
2368 }
2369
2370 stream_failure:
2371 return;
2372 }
2373
2374 static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2375 {
2376 struct zebra_pbr_ipset zpi;
2377 struct stream *s;
2378 uint32_t total, i;
2379
2380 s = msg;
2381 STREAM_GETL(s, total);
2382
2383 for (i = 0; i < total; i++) {
2384 memset(&zpi, 0, sizeof(zpi));
2385
2386 zpi.sock = client->sock;
2387 zpi.vrf_id = zvrf->vrf->vrf_id;
2388 STREAM_GETL(s, zpi.unique);
2389 STREAM_GETL(s, zpi.type);
2390 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2391
2392 if (hdr->command == ZEBRA_IPSET_CREATE)
2393 zebra_pbr_create_ipset(&zpi);
2394 else
2395 zebra_pbr_destroy_ipset(&zpi);
2396 }
2397
2398 stream_failure:
2399 return;
2400 }
2401
2402 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2403 {
2404 struct zebra_pbr_ipset_entry zpi;
2405 struct zebra_pbr_ipset ipset;
2406 struct stream *s;
2407 uint32_t total, i;
2408
2409 s = msg;
2410 STREAM_GETL(s, total);
2411
2412 for (i = 0; i < total; i++) {
2413 memset(&zpi, 0, sizeof(zpi));
2414 memset(&ipset, 0, sizeof(ipset));
2415
2416 zpi.sock = client->sock;
2417 STREAM_GETL(s, zpi.unique);
2418 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2419 STREAM_GETC(s, zpi.src.family);
2420 STREAM_GETC(s, zpi.src.prefixlen);
2421 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
2422 STREAM_GETC(s, zpi.dst.family);
2423 STREAM_GETC(s, zpi.dst.prefixlen);
2424 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
2425
2426 STREAM_GETW(s, zpi.src_port_min);
2427 STREAM_GETW(s, zpi.src_port_max);
2428 STREAM_GETW(s, zpi.dst_port_min);
2429 STREAM_GETW(s, zpi.dst_port_max);
2430 STREAM_GETC(s, zpi.proto);
2431 if (!is_default_prefix(&zpi.src))
2432 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2433
2434 if (!is_default_prefix(&zpi.dst))
2435 zpi.filter_bm |= PBR_FILTER_DST_IP;
2436 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2437 zpi.filter_bm |= PBR_FILTER_DST_PORT;
2438 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2439 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
2440 if (zpi.dst_port_max != 0)
2441 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
2442 if (zpi.src_port_max != 0)
2443 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
2444 if (zpi.proto != 0)
2445 zpi.filter_bm |= PBR_FILTER_PROTO;
2446
2447 /* calculate backpointer */
2448 zpi.backpointer =
2449 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
2450 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
2451 zebra_pbr_add_ipset_entry(&zpi);
2452 else
2453 zebra_pbr_del_ipset_entry(&zpi);
2454 }
2455
2456 stream_failure:
2457 return;
2458 }
2459
2460 static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2461 {
2462 struct zebra_pbr_iptable zpi;
2463 struct stream *s;
2464
2465 s = msg;
2466
2467 memset(&zpi, 0, sizeof(zpi));
2468
2469 zpi.interface_name_list = list_new();
2470 zpi.sock = client->sock;
2471 zpi.vrf_id = zvrf->vrf->vrf_id;
2472 STREAM_GETL(s, zpi.unique);
2473 STREAM_GETL(s, zpi.type);
2474 STREAM_GETL(s, zpi.filter_bm);
2475 STREAM_GETL(s, zpi.action);
2476 STREAM_GETL(s, zpi.fwmark);
2477 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2478 STREAM_GETW(s, zpi.pkt_len_min);
2479 STREAM_GETW(s, zpi.pkt_len_max);
2480 STREAM_GETW(s, zpi.tcp_flags);
2481 STREAM_GETW(s, zpi.tcp_mask_flags);
2482 STREAM_GETC(s, zpi.dscp_value);
2483 STREAM_GETC(s, zpi.fragment);
2484 STREAM_GETC(s, zpi.protocol);
2485 STREAM_GETL(s, zpi.nb_interface);
2486 zebra_pbr_iptable_update_interfacelist(s, &zpi);
2487
2488 if (hdr->command == ZEBRA_IPTABLE_ADD)
2489 zebra_pbr_add_iptable(&zpi);
2490 else
2491 zebra_pbr_del_iptable(&zpi);
2492 stream_failure:
2493 return;
2494 }
2495
2496 void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
2497 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2498 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2499 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2500 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
2501 [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown,
2502 [ZEBRA_ROUTE_ADD] = zread_route_add,
2503 [ZEBRA_ROUTE_DELETE] = zread_route_del,
2504 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2505 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2506 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2507 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2508 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2509 [ZEBRA_HELLO] = zread_hello,
2510 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2511 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2512 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2513 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2514 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2515 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2516 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
2517 #if HAVE_BFDD > 0
2518 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
2519 #endif /* HAVE_BFDD */
2520 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2521 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2522 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
2523 #if defined(HAVE_RTADV)
2524 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2525 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
2526 #else
2527 [ZEBRA_INTERFACE_ENABLE_RADV] = NULL,
2528 [ZEBRA_INTERFACE_DISABLE_RADV] = NULL,
2529 #endif
2530 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add,
2531 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete,
2532 [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace,
2533 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2534 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
2535 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
2536 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2537 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2538 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2539 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2540 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
2541 [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip,
2542 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2543 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2544 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2545 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2546 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2547 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
2548 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
2549 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2550 [ZEBRA_PW_ADD] = zread_pseudowire,
2551 [ZEBRA_PW_DELETE] = zread_pseudowire,
2552 [ZEBRA_PW_SET] = zread_pseudowire,
2553 [ZEBRA_PW_UNSET] = zread_pseudowire,
2554 [ZEBRA_RULE_ADD] = zread_rule,
2555 [ZEBRA_RULE_DELETE] = zread_rule,
2556 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
2557 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
2558 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
2559 [ZEBRA_IPSET_CREATE] = zread_ipset,
2560 [ZEBRA_IPSET_DESTROY] = zread_ipset,
2561 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
2562 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
2563 [ZEBRA_IPTABLE_ADD] = zread_iptable,
2564 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
2565 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
2566 [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay,
2567 [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register,
2568 [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister,
2569 [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg,
2570 };
2571
2572 #if defined(HANDLE_ZAPI_FUZZING)
2573 extern struct zebra_privs_t zserv_privs;
2574
2575 static void zserv_write_incoming(struct stream *orig, uint16_t command)
2576 {
2577 char fname[MAXPATHLEN];
2578 struct stream *copy;
2579 int fd = -1;
2580
2581 copy = stream_dup(orig);
2582 stream_set_getp(copy, 0);
2583
2584 snprintf(fname, MAXPATHLEN, "%s/%u", frr_vtydir, command);
2585
2586 frr_with_privs(&zserv_privs) {
2587 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2588 }
2589 stream_flush(copy, fd);
2590 close(fd);
2591 stream_free(copy);
2592 }
2593 #endif
2594
2595 void zserv_handle_commands(struct zserv *client, struct stream *msg)
2596 {
2597 struct zmsghdr hdr;
2598 struct zebra_vrf *zvrf;
2599
2600 zapi_parse_header(msg, &hdr);
2601
2602 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2603 zserv_log_message(NULL, msg, &hdr);
2604
2605 #if defined(HANDLE_ZAPI_FUZZING)
2606 zserv_write_incoming(msg, hdr.command);
2607 #endif
2608
2609 hdr.length -= ZEBRA_HEADER_SIZE;
2610
2611 /* lookup vrf */
2612 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
2613 if (!zvrf) {
2614 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2615 zlog_debug("ZAPI message specifies unknown VRF: %d",
2616 hdr.vrf_id);
2617 return;
2618 }
2619
2620 if (hdr.command >= array_size(zserv_handlers)
2621 || zserv_handlers[hdr.command] == NULL)
2622 zlog_info("Zebra received unknown command %d", hdr.command);
2623 else
2624 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
2625 }