]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zapi_msg.c
zebra: don't protodown a NULL interface
[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/label_manager.h"
61 #include "zebra/zebra_vxlan.h"
62 #include "zebra/rt.h"
63 #include "zebra/zebra_pbr.h"
64 #include "zebra/table_manager.h"
65 #include "zebra/zapi_msg.h"
66 #include "zebra/zebra_errors.h"
67 #include "zebra/zebra_mlag.h"
68
69 /* Encoding helpers -------------------------------------------------------- */
70
71 static void zserv_encode_interface(struct stream *s, struct interface *ifp)
72 {
73 /* Interface information. */
74 struct zebra_if *zif = ifp->info;
75 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, struct route_entry *re)
520 {
521 struct zapi_route api;
522 struct zapi_nexthop *api_nh;
523 struct nexthop *nexthop;
524 int count = 0;
525 afi_t afi;
526
527 memset(&api, 0, sizeof(api));
528 api.vrf_id = re->vrf_id;
529 api.type = re->type;
530 api.safi = SAFI_UNICAST;
531 api.instance = re->instance;
532 api.flags = re->flags;
533
534 afi = family2afi(p->family);
535 switch (afi) {
536 case AFI_IP:
537 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
538 client->redist_v4_add_cnt++;
539 else
540 client->redist_v4_del_cnt++;
541 break;
542 case AFI_IP6:
543 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
544 client->redist_v6_add_cnt++;
545 else
546 client->redist_v6_del_cnt++;
547 break;
548 default:
549 break;
550 }
551
552 /* Prefix. */
553 api.prefix = *p;
554 if (src_p) {
555 SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
556 memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
557 }
558
559 /* Nexthops. */
560 if (re->nexthop_active_num) {
561 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
562 api.nexthop_num = re->nexthop_active_num;
563 }
564 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
565 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
566 continue;
567
568 api_nh = &api.nexthops[count];
569 api_nh->vrf_id = nexthop->vrf_id;
570 api_nh->type = nexthop->type;
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 /* Attributes. */
596 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
597 api.distance = re->distance;
598 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
599 api.metric = re->metric;
600 if (re->tag) {
601 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
602 api.tag = re->tag;
603 }
604 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
605 api.mtu = re->mtu;
606
607 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
608
609 /* Encode route and send. */
610 if (zapi_route_encode(cmd, s, &api) < 0) {
611 stream_free(s);
612 return -1;
613 }
614
615 if (IS_ZEBRA_DEBUG_SEND) {
616 char buf_prefix[PREFIX_STRLEN];
617
618 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
619
620 zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
621 __func__, zserv_command_string(cmd),
622 zebra_route_string(client->proto),
623 zebra_route_string(api.type), api.vrf_id,
624 buf_prefix);
625 }
626 return zserv_send_message(client, s);
627 }
628
629 /*
630 * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
631 * nexthop is not found on mrib. Returns both route metric and protocol
632 * distance.
633 */
634 static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
635 struct in_addr addr,
636 struct route_entry *re,
637 struct zebra_vrf *zvrf)
638 {
639 struct stream *s;
640 unsigned long nump;
641 uint8_t num;
642 struct nexthop *nexthop;
643
644 /* Get output stream. */
645 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
646 stream_reset(s);
647
648 /* Fill in result. */
649 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
650 stream_put_in_addr(s, &addr);
651
652 if (re) {
653 stream_putc(s, re->distance);
654 stream_putl(s, re->metric);
655 num = 0;
656 /* remember position for nexthop_num */
657 nump = stream_get_endp(s);
658 /* reserve room for nexthop_num */
659 stream_putc(s, 0);
660 /*
661 * Only non-recursive routes are elegible to resolve the
662 * nexthop we are looking up. Therefore, we will just iterate
663 * over the top chain of nexthops.
664 */
665 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
666 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
667 num += zserv_encode_nexthop(s, nexthop);
668
669 /* store nexthop_num */
670 stream_putc_at(s, nump, num);
671 } else {
672 stream_putc(s, 0); /* distance */
673 stream_putl(s, 0); /* metric */
674 stream_putc(s, 0); /* nexthop_num */
675 }
676
677 stream_putw_at(s, 0, stream_get_endp(s));
678
679 return zserv_send_message(client, s);
680 }
681
682 /*
683 * Common utility send route notification, called from a path using a
684 * route_entry and from a path using a dataplane context.
685 */
686 static int route_notify_internal(const struct prefix *p, int type,
687 uint16_t instance, vrf_id_t vrf_id,
688 uint32_t table_id,
689 enum zapi_route_notify_owner note)
690 {
691 struct zserv *client;
692 struct stream *s;
693 uint8_t blen;
694
695 client = zserv_find_client(type, instance);
696 if (!client || !client->notify_owner) {
697 if (IS_ZEBRA_DEBUG_PACKET) {
698 char buff[PREFIX_STRLEN];
699
700 zlog_debug(
701 "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
702 type, prefix2str(p, buff, sizeof(buff)),
703 table_id, note, vrf_id);
704 }
705 return 0;
706 }
707
708 if (IS_ZEBRA_DEBUG_PACKET) {
709 char buff[PREFIX_STRLEN];
710
711 zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
712 type, prefix2str(p, buff, sizeof(buff)),
713 table_id, note, vrf_id);
714 }
715
716 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
717 stream_reset(s);
718
719 zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
720
721 stream_put(s, &note, sizeof(note));
722
723 stream_putc(s, p->family);
724
725 blen = prefix_blen(p);
726 stream_putc(s, p->prefixlen);
727 stream_put(s, &p->u.prefix, blen);
728
729 stream_putl(s, table_id);
730
731 stream_putw_at(s, 0, stream_get_endp(s));
732
733 return zserv_send_message(client, s);
734 }
735
736 int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p,
737 enum zapi_route_notify_owner note)
738 {
739 return (route_notify_internal(p, re->type, re->instance, re->vrf_id,
740 re->table, note));
741 }
742
743 /*
744 * Route-owner notification using info from dataplane update context.
745 */
746 int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
747 enum zapi_route_notify_owner note)
748 {
749 return (route_notify_internal(dplane_ctx_get_dest(ctx),
750 dplane_ctx_get_type(ctx),
751 dplane_ctx_get_instance(ctx),
752 dplane_ctx_get_vrf(ctx),
753 dplane_ctx_get_table(ctx),
754 note));
755 }
756
757 void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
758 enum zapi_rule_notify_owner note)
759 {
760 struct listnode *node;
761 struct zserv *client;
762 struct stream *s;
763
764 if (IS_ZEBRA_DEBUG_PACKET)
765 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
766 rule->rule.unique);
767
768 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
769 if (rule->sock == client->sock)
770 break;
771 }
772
773 if (!client)
774 return;
775
776 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
777
778 zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
779 stream_put(s, &note, sizeof(note));
780 stream_putl(s, rule->rule.seq);
781 stream_putl(s, rule->rule.priority);
782 stream_putl(s, rule->rule.unique);
783 if (rule->ifp)
784 stream_putl(s, rule->ifp->ifindex);
785 else
786 stream_putl(s, 0);
787
788 stream_putw_at(s, 0, stream_get_endp(s));
789
790 zserv_send_message(client, s);
791 }
792
793 void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
794 enum zapi_ipset_notify_owner note)
795 {
796 struct listnode *node;
797 struct zserv *client;
798 struct stream *s;
799
800 if (IS_ZEBRA_DEBUG_PACKET)
801 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
802 ipset->unique);
803
804 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
805 if (ipset->sock == client->sock)
806 break;
807 }
808
809 if (!client)
810 return;
811
812 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
813
814 zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
815 stream_put(s, &note, sizeof(note));
816 stream_putl(s, ipset->unique);
817 stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
818 stream_putw_at(s, 0, stream_get_endp(s));
819
820 zserv_send_message(client, s);
821 }
822
823 void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset,
824 enum zapi_ipset_entry_notify_owner note)
825 {
826 struct listnode *node;
827 struct zserv *client;
828 struct stream *s;
829
830 if (IS_ZEBRA_DEBUG_PACKET)
831 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
832 ipset->unique);
833
834 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
835 if (ipset->sock == client->sock)
836 break;
837 }
838
839 if (!client)
840 return;
841
842 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
843
844 zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT);
845 stream_put(s, &note, sizeof(note));
846 stream_putl(s, ipset->unique);
847 stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE);
848 stream_putw_at(s, 0, stream_get_endp(s));
849
850 zserv_send_message(client, s);
851 }
852
853 void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
854 enum zapi_iptable_notify_owner note)
855 {
856 struct listnode *node;
857 struct zserv *client;
858 struct stream *s;
859
860 if (IS_ZEBRA_DEBUG_PACKET)
861 zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
862 iptable->unique);
863
864 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
865 if (iptable->sock == client->sock)
866 break;
867 }
868
869 if (!client)
870 return;
871
872 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
873
874 zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
875 stream_put(s, &note, sizeof(note));
876 stream_putl(s, iptable->unique);
877 stream_putw_at(s, 0, stream_get_endp(s));
878
879 zserv_send_message(client, s);
880 }
881
882 /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
883 int zsend_router_id_update(struct zserv *client, struct prefix *p,
884 vrf_id_t vrf_id)
885 {
886 int blen;
887
888 /* Check this client need interface information. */
889 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
890 return 0;
891
892 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
893
894 /* Message type. */
895 zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
896
897 /* Prefix information. */
898 stream_putc(s, p->family);
899 blen = prefix_blen(p);
900 stream_put(s, &p->u.prefix, blen);
901 stream_putc(s, p->prefixlen);
902
903 /* Write packet size. */
904 stream_putw_at(s, 0, stream_get_endp(s));
905
906 return zserv_send_message(client, s);
907 }
908
909 /*
910 * Function used by Zebra to send a PW status update to LDP daemon
911 */
912 int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
913 {
914 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
915
916 zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
917 stream_write(s, pw->ifname, IF_NAMESIZE);
918 stream_putl(s, pw->ifindex);
919 stream_putl(s, pw->status);
920
921 /* Put length at the first point of the stream. */
922 stream_putw_at(s, 0, stream_get_endp(s));
923
924 return zserv_send_message(client, s);
925 }
926
927 /* Send response to a get label chunk request to client */
928 static int zsend_assign_label_chunk_response(struct zserv *client,
929 vrf_id_t vrf_id,
930 struct label_manager_chunk *lmc)
931 {
932 int ret;
933 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
934
935 zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
936
937 if (lmc) {
938 /* proto */
939 stream_putc(s, lmc->proto);
940 /* instance */
941 stream_putw(s, lmc->instance);
942 /* keep */
943 stream_putc(s, lmc->keep);
944 /* start and end labels */
945 stream_putl(s, lmc->start);
946 stream_putl(s, lmc->end);
947 }
948
949 /* Write packet size. */
950 stream_putw_at(s, 0, stream_get_endp(s));
951
952 ret = writen(client->sock, s->data, stream_get_endp(s));
953 stream_free(s);
954 return ret;
955 }
956
957 /* Send response to a label manager connect request to client */
958 static int zsend_label_manager_connect_response(struct zserv *client,
959 vrf_id_t vrf_id,
960 unsigned short result)
961 {
962 int ret;
963 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
964
965 zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
966
967 /* proto */
968 stream_putc(s, client->proto);
969
970 /* instance */
971 stream_putw(s, client->instance);
972
973 /* result */
974 stream_putc(s, result);
975
976 /* Write packet size. */
977 stream_putw_at(s, 0, stream_get_endp(s));
978
979 ret = writen(client->sock, s->data, stream_get_endp(s));
980 stream_free(s);
981
982 return ret;
983 }
984
985 /* Send response to a get table chunk request to client */
986 static int zsend_assign_table_chunk_response(struct zserv *client,
987 vrf_id_t vrf_id,
988 struct table_manager_chunk *tmc)
989 {
990 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
991
992 zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
993
994 if (tmc) {
995 /* start and end labels */
996 stream_putl(s, tmc->start);
997 stream_putl(s, tmc->end);
998 }
999
1000 /* Write packet size. */
1001 stream_putw_at(s, 0, stream_get_endp(s));
1002
1003 return zserv_send_message(client, s);
1004 }
1005
1006 static int zsend_table_manager_connect_response(struct zserv *client,
1007 vrf_id_t vrf_id,
1008 uint16_t result)
1009 {
1010 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1011
1012 zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id);
1013
1014 /* result */
1015 stream_putc(s, result);
1016
1017 stream_putw_at(s, 0, stream_get_endp(s));
1018
1019 return zserv_send_message(client, s);
1020 }
1021
1022 /* Inbound message handling ------------------------------------------------ */
1023
1024 int cmd2type[] = {
1025 [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE,
1026 [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE,
1027 [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE,
1028 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE,
1029 };
1030
1031 /* Nexthop register */
1032 static void zread_rnh_register(ZAPI_HANDLER_ARGS)
1033 {
1034 struct rnh *rnh;
1035 struct stream *s;
1036 struct prefix p;
1037 unsigned short l = 0;
1038 uint8_t flags = 0;
1039 uint16_t type = cmd2type[hdr->command];
1040 bool exist;
1041 bool flag_changed = false;
1042 uint8_t orig_flags;
1043
1044 if (IS_ZEBRA_DEBUG_NHT)
1045 zlog_debug(
1046 "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n",
1047 zebra_route_string(client->proto), hdr->length,
1048 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route",
1049 zvrf->vrf->vrf_id);
1050
1051 s = msg;
1052
1053 client->nh_reg_time = monotime(NULL);
1054
1055 while (l < hdr->length) {
1056 STREAM_GETC(s, flags);
1057 STREAM_GETW(s, p.family);
1058 STREAM_GETC(s, p.prefixlen);
1059 l += 4;
1060 if (p.family == AF_INET) {
1061 client->v4_nh_watch_add_cnt++;
1062 if (p.prefixlen > IPV4_MAX_BITLEN) {
1063 zlog_debug(
1064 "%s: Specified prefix hdr->length %d is too large for a v4 address",
1065 __PRETTY_FUNCTION__, p.prefixlen);
1066 return;
1067 }
1068 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1069 l += IPV4_MAX_BYTELEN;
1070 } else if (p.family == AF_INET6) {
1071 client->v6_nh_watch_add_cnt++;
1072 if (p.prefixlen > IPV6_MAX_BITLEN) {
1073 zlog_debug(
1074 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1075 __PRETTY_FUNCTION__, p.prefixlen);
1076 return;
1077 }
1078 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1079 l += IPV6_MAX_BYTELEN;
1080 } else {
1081 flog_err(
1082 EC_ZEBRA_UNKNOWN_FAMILY,
1083 "rnh_register: Received unknown family type %d\n",
1084 p.family);
1085 return;
1086 }
1087 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type, &exist);
1088 if (!rnh)
1089 return;
1090
1091 orig_flags = rnh->flags;
1092 if (type == RNH_NEXTHOP_TYPE) {
1093 if (flags
1094 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1095 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1096 else if (!flags
1097 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
1098 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
1099 } else if (type == RNH_IMPORT_CHECK_TYPE) {
1100 if (flags
1101 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
1102 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1103 else if (!flags
1104 && CHECK_FLAG(rnh->flags,
1105 ZEBRA_NHT_EXACT_MATCH))
1106 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
1107 }
1108
1109 if (orig_flags != rnh->flags)
1110 flag_changed = true;
1111
1112 /* Anything not AF_INET/INET6 has been filtered out above */
1113 if (!exist || flag_changed)
1114 zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, type,
1115 &p);
1116
1117 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
1118 }
1119
1120 stream_failure:
1121 return;
1122 }
1123
1124 /* Nexthop register */
1125 static void zread_rnh_unregister(ZAPI_HANDLER_ARGS)
1126 {
1127 struct rnh *rnh;
1128 struct stream *s;
1129 struct prefix p;
1130 unsigned short l = 0;
1131 uint16_t type = cmd2type[hdr->command];
1132
1133 if (IS_ZEBRA_DEBUG_NHT)
1134 zlog_debug(
1135 "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n",
1136 zebra_route_string(client->proto), hdr->length,
1137 zvrf->vrf->vrf_id);
1138
1139 s = msg;
1140
1141 while (l < hdr->length) {
1142 uint8_t flags;
1143
1144 STREAM_GETC(s, flags);
1145 if (flags != 0)
1146 goto stream_failure;
1147
1148 STREAM_GETW(s, p.family);
1149 STREAM_GETC(s, p.prefixlen);
1150 l += 4;
1151 if (p.family == AF_INET) {
1152 client->v4_nh_watch_rem_cnt++;
1153 if (p.prefixlen > IPV4_MAX_BITLEN) {
1154 zlog_debug(
1155 "%s: Specified prefix hdr->length %d is to large for a v4 address",
1156 __PRETTY_FUNCTION__, p.prefixlen);
1157 return;
1158 }
1159 STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1160 l += IPV4_MAX_BYTELEN;
1161 } else if (p.family == AF_INET6) {
1162 client->v6_nh_watch_rem_cnt++;
1163 if (p.prefixlen > IPV6_MAX_BITLEN) {
1164 zlog_debug(
1165 "%s: Specified prefix hdr->length %d is to large for a v6 address",
1166 __PRETTY_FUNCTION__, p.prefixlen);
1167 return;
1168 }
1169 STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
1170 l += IPV6_MAX_BYTELEN;
1171 } else {
1172 flog_err(
1173 EC_ZEBRA_UNKNOWN_FAMILY,
1174 "rnh_register: Received unknown family type %d\n",
1175 p.family);
1176 return;
1177 }
1178 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
1179 if (rnh) {
1180 client->nh_dereg_time = monotime(NULL);
1181 zebra_remove_rnh_client(rnh, client, type);
1182 }
1183 }
1184 stream_failure:
1185 return;
1186 }
1187
1188 #define ZEBRA_MIN_FEC_LENGTH 5
1189
1190 /* FEC register */
1191 static void zread_fec_register(ZAPI_HANDLER_ARGS)
1192 {
1193 struct stream *s;
1194 unsigned short l = 0;
1195 struct prefix p;
1196 uint16_t flags;
1197 uint32_t label = MPLS_INVALID_LABEL;
1198 uint32_t label_index = MPLS_INVALID_LABEL_INDEX;
1199
1200 s = msg;
1201 zvrf = vrf_info_lookup(VRF_DEFAULT);
1202 if (!zvrf)
1203 return;
1204
1205 /*
1206 * The minimum amount of data that can be sent for one fec
1207 * registration
1208 */
1209 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1210 flog_err(
1211 EC_ZEBRA_IRDP_LEN_MISMATCH,
1212 "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode",
1213 hdr->length);
1214 return;
1215 }
1216
1217 while (l < hdr->length) {
1218 STREAM_GETW(s, flags);
1219 memset(&p, 0, sizeof(p));
1220 STREAM_GETW(s, p.family);
1221 if (p.family != AF_INET && p.family != AF_INET6) {
1222 flog_err(
1223 EC_ZEBRA_UNKNOWN_FAMILY,
1224 "fec_register: Received unknown family type %d\n",
1225 p.family);
1226 return;
1227 }
1228 STREAM_GETC(s, p.prefixlen);
1229 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1230 || (p.family == AF_INET6
1231 && p.prefixlen > IPV6_MAX_BITLEN)) {
1232 zlog_debug(
1233 "%s: Specified prefix hdr->length: %d is to long for %d",
1234 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1235 return;
1236 }
1237 l += 5;
1238 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1239 l += PSIZE(p.prefixlen);
1240 if (flags & ZEBRA_FEC_REGISTER_LABEL) {
1241 STREAM_GETL(s, label);
1242 l += 4;
1243 } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
1244 STREAM_GETL(s, label_index);
1245 l += 4;
1246 }
1247
1248 zebra_mpls_fec_register(zvrf, &p, label, label_index, client);
1249 }
1250
1251 stream_failure:
1252 return;
1253 }
1254
1255 /* FEC unregister */
1256 static void zread_fec_unregister(ZAPI_HANDLER_ARGS)
1257 {
1258 struct stream *s;
1259 unsigned short l = 0;
1260 struct prefix p;
1261 uint16_t flags;
1262
1263 s = msg;
1264 zvrf = vrf_info_lookup(VRF_DEFAULT);
1265 if (!zvrf)
1266 return;
1267
1268 /*
1269 * The minimum amount of data that can be sent for one
1270 * fec unregistration
1271 */
1272 if (hdr->length < ZEBRA_MIN_FEC_LENGTH) {
1273 flog_err(
1274 EC_ZEBRA_IRDP_LEN_MISMATCH,
1275 "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode",
1276 hdr->length);
1277 return;
1278 }
1279
1280 while (l < hdr->length) {
1281 STREAM_GETW(s, flags);
1282 if (flags != 0)
1283 goto stream_failure;
1284
1285 memset(&p, 0, sizeof(p));
1286 STREAM_GETW(s, p.family);
1287 if (p.family != AF_INET && p.family != AF_INET6) {
1288 flog_err(
1289 EC_ZEBRA_UNKNOWN_FAMILY,
1290 "fec_unregister: Received unknown family type %d\n",
1291 p.family);
1292 return;
1293 }
1294 STREAM_GETC(s, p.prefixlen);
1295 if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN)
1296 || (p.family == AF_INET6
1297 && p.prefixlen > IPV6_MAX_BITLEN)) {
1298 zlog_debug(
1299 "%s: Received prefix hdr->length %d which is greater than %d can support",
1300 __PRETTY_FUNCTION__, p.prefixlen, p.family);
1301 return;
1302 }
1303 l += 5;
1304 STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen));
1305 l += PSIZE(p.prefixlen);
1306 zebra_mpls_fec_unregister(zvrf, &p, client);
1307 }
1308
1309 stream_failure:
1310 return;
1311 }
1312
1313
1314 /*
1315 * Register zebra server interface information.
1316 * Send current all interface and address information.
1317 */
1318 static void zread_interface_add(ZAPI_HANDLER_ARGS)
1319 {
1320 struct vrf *vrf;
1321 struct interface *ifp;
1322
1323 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1324 FOR_ALL_INTERFACES (vrf, ifp) {
1325 /* Skip pseudo interface. */
1326 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1327 continue;
1328
1329 zsend_interface_add(client, ifp);
1330 zsend_interface_link_params(client, ifp);
1331 zsend_interface_addresses(client, ifp);
1332 }
1333 }
1334 }
1335
1336 /* Unregister zebra server interface information. */
1337 static void zread_interface_delete(ZAPI_HANDLER_ARGS)
1338 {
1339 }
1340
1341 /*
1342 * Handle message requesting interface be set up or down.
1343 */
1344 static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS)
1345 {
1346 ifindex_t ifindex;
1347 struct interface *ifp;
1348 char down;
1349
1350 STREAM_GETL(msg, ifindex);
1351 STREAM_GETC(msg, down);
1352
1353 /* set ifdown */
1354 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex);
1355
1356 if (ifp) {
1357 zlog_info("Setting interface %s (%u): protodown %s", ifp->name,
1358 ifindex, down ? "on" : "off");
1359 zebra_if_set_protodown(ifp, down);
1360 } else {
1361 zlog_warn(
1362 "Cannot set protodown %s for interface %u; does not exist",
1363 down ? "on" : "off", ifindex);
1364 }
1365
1366
1367 stream_failure:
1368 return;
1369 }
1370
1371
1372 void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1373 const unsigned int nexthop_num)
1374 {
1375 if (nexthop_num > zrouter.multipath_num) {
1376 char buff[PREFIX2STR_BUFFER];
1377
1378 prefix2str(p, buff, sizeof(buff));
1379 flog_warn(
1380 EC_ZEBRA_MORE_NH_THAN_MULTIPATH,
1381 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1382 caller, buff, nexthop_num, zrouter.multipath_num);
1383 }
1384 }
1385
1386 static void zread_route_add(ZAPI_HANDLER_ARGS)
1387 {
1388 struct stream *s;
1389 struct zapi_route api;
1390 struct zapi_nexthop *api_nh;
1391 afi_t afi;
1392 struct prefix_ipv6 *src_p = NULL;
1393 struct route_entry *re;
1394 struct nexthop *nexthop = NULL;
1395 int i, ret;
1396 vrf_id_t vrf_id = 0;
1397 struct ipaddr vtep_ip;
1398
1399 s = msg;
1400 if (zapi_route_decode(s, &api) < 0) {
1401 if (IS_ZEBRA_DEBUG_RECV)
1402 zlog_debug("%s: Unable to decode zapi_route sent",
1403 __PRETTY_FUNCTION__);
1404 return;
1405 }
1406
1407 if (IS_ZEBRA_DEBUG_RECV) {
1408 char buf_prefix[PREFIX_STRLEN];
1409
1410 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1411 zlog_debug("%s: p=%s, ZAPI_MESSAGE_LABEL: %sset, flags=0x%x",
1412 __func__, buf_prefix,
1413 (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL) ? ""
1414 : "un"),
1415 api.flags);
1416 }
1417
1418 /* Allocate new route. */
1419 vrf_id = zvrf_id(zvrf);
1420 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1421 re->type = api.type;
1422 re->instance = api.instance;
1423 re->flags = api.flags;
1424 re->uptime = monotime(NULL);
1425 re->vrf_id = vrf_id;
1426 if (api.tableid && vrf_id == VRF_DEFAULT)
1427 re->table = api.tableid;
1428 else
1429 re->table = zvrf->table_id;
1430
1431 if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)
1432 || api.nexthop_num == 0) {
1433 char buf_prefix[PREFIX_STRLEN];
1434
1435 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1436 flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS,
1437 "%s: received a route without nexthops for prefix %s",
1438 __func__, buf_prefix);
1439 XFREE(MTYPE_RE, re);
1440 return;
1441 }
1442
1443 /*
1444 * TBD should _all_ of the nexthop add operations use
1445 * api_nh->vrf_id instead of re->vrf_id ? I only changed
1446 * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6.
1447 */
1448 for (i = 0; i < api.nexthop_num; i++) {
1449 api_nh = &api.nexthops[i];
1450 ifindex_t ifindex = 0;
1451
1452 if (IS_ZEBRA_DEBUG_RECV)
1453 zlog_debug("nh type %d", api_nh->type);
1454
1455 switch (api_nh->type) {
1456 case NEXTHOP_TYPE_IFINDEX:
1457 nexthop = route_entry_nexthop_ifindex_add(
1458 re, api_nh->ifindex, api_nh->vrf_id);
1459 break;
1460 case NEXTHOP_TYPE_IPV4:
1461 if (IS_ZEBRA_DEBUG_RECV) {
1462 char nhbuf[INET6_ADDRSTRLEN] = {0};
1463
1464 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1465 INET6_ADDRSTRLEN);
1466 zlog_debug("%s: nh=%s, vrf_id=%d", __func__,
1467 nhbuf, api_nh->vrf_id);
1468 }
1469 nexthop = route_entry_nexthop_ipv4_add(
1470 re, &api_nh->gate.ipv4, NULL, api_nh->vrf_id);
1471 break;
1472 case NEXTHOP_TYPE_IPV4_IFINDEX:
1473
1474 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1475 ifindex = api_nh->ifindex;
1476 if (IS_ZEBRA_DEBUG_RECV) {
1477 char nhbuf[INET6_ADDRSTRLEN] = {0};
1478
1479 inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf,
1480 INET6_ADDRSTRLEN);
1481 zlog_debug(
1482 "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d",
1483 __func__, nhbuf, api_nh->vrf_id,
1484 re->vrf_id, ifindex);
1485 }
1486 nexthop = route_entry_nexthop_ipv4_ifindex_add(
1487 re, &api_nh->gate.ipv4, NULL, ifindex,
1488 api_nh->vrf_id);
1489
1490 /* Special handling for IPv4 routes sourced from EVPN:
1491 * the nexthop and associated MAC need to be installed.
1492 */
1493 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1494 vtep_ip.ipa_type = IPADDR_V4;
1495 memcpy(&(vtep_ip.ipaddr_v4),
1496 &(api_nh->gate.ipv4),
1497 sizeof(struct in_addr));
1498 zebra_vxlan_evpn_vrf_route_add(
1499 api_nh->vrf_id, &api_nh->rmac,
1500 &vtep_ip, &api.prefix);
1501 }
1502 break;
1503 case NEXTHOP_TYPE_IPV6:
1504 nexthop = route_entry_nexthop_ipv6_add(
1505 re, &api_nh->gate.ipv6, api_nh->vrf_id);
1506 break;
1507 case NEXTHOP_TYPE_IPV6_IFINDEX:
1508 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1509 ifindex = api_nh->ifindex;
1510 nexthop = route_entry_nexthop_ipv6_ifindex_add(
1511 re, &api_nh->gate.ipv6, ifindex,
1512 api_nh->vrf_id);
1513
1514 /* Special handling for IPv6 routes sourced from EVPN:
1515 * the nexthop and associated MAC need to be installed.
1516 */
1517 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1518 vtep_ip.ipa_type = IPADDR_V6;
1519 memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6),
1520 sizeof(struct in6_addr));
1521 zebra_vxlan_evpn_vrf_route_add(
1522 api_nh->vrf_id, &api_nh->rmac,
1523 &vtep_ip, &api.prefix);
1524 }
1525 break;
1526 case NEXTHOP_TYPE_BLACKHOLE:
1527 nexthop = route_entry_nexthop_blackhole_add(
1528 re, api_nh->bh_type);
1529 break;
1530 }
1531
1532 if (!nexthop) {
1533 flog_warn(
1534 EC_ZEBRA_NEXTHOP_CREATION_FAILED,
1535 "%s: Nexthops Specified: %d but we failed to properly create one",
1536 __PRETTY_FUNCTION__, api.nexthop_num);
1537 nexthops_free(re->ng.nexthop);
1538 XFREE(MTYPE_RE, re);
1539 return;
1540 }
1541 if (api_nh->onlink)
1542 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
1543
1544 /* MPLS labels for BGP-LU or Segment Routing */
1545 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL)
1546 && api_nh->type != NEXTHOP_TYPE_IFINDEX
1547 && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) {
1548 enum lsp_types_t label_type;
1549
1550 label_type = lsp_type_from_re_type(client->proto);
1551
1552 if (IS_ZEBRA_DEBUG_RECV) {
1553 zlog_debug(
1554 "%s: adding %d labels of type %d (1st=%u)",
1555 __func__, api_nh->label_num, label_type,
1556 api_nh->labels[0]);
1557 }
1558
1559 nexthop_add_labels(nexthop, label_type,
1560 api_nh->label_num,
1561 &api_nh->labels[0]);
1562 }
1563 }
1564
1565 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1566 re->distance = api.distance;
1567 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1568 re->metric = api.metric;
1569 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1570 re->tag = api.tag;
1571 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU))
1572 re->mtu = api.mtu;
1573
1574 afi = family2afi(api.prefix.family);
1575 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1576 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1577 "%s: Received SRC Prefix but afi is not v6",
1578 __PRETTY_FUNCTION__);
1579 nexthops_free(re->ng.nexthop);
1580 XFREE(MTYPE_RE, re);
1581 return;
1582 }
1583 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1584 src_p = &api.src_prefix;
1585
1586 ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re);
1587
1588 /* Stats */
1589 switch (api.prefix.family) {
1590 case AF_INET:
1591 if (ret > 0)
1592 client->v4_route_add_cnt++;
1593 else if (ret < 0)
1594 client->v4_route_upd8_cnt++;
1595 break;
1596 case AF_INET6:
1597 if (ret > 0)
1598 client->v6_route_add_cnt++;
1599 else if (ret < 0)
1600 client->v6_route_upd8_cnt++;
1601 break;
1602 }
1603 }
1604
1605 static void zread_route_del(ZAPI_HANDLER_ARGS)
1606 {
1607 struct stream *s;
1608 struct zapi_route api;
1609 afi_t afi;
1610 struct prefix_ipv6 *src_p = NULL;
1611 uint32_t table_id;
1612
1613 s = msg;
1614 if (zapi_route_decode(s, &api) < 0)
1615 return;
1616
1617 afi = family2afi(api.prefix.family);
1618 if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1619 flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI,
1620 "%s: Received a src prefix while afi is not v6",
1621 __PRETTY_FUNCTION__);
1622 return;
1623 }
1624 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
1625 src_p = &api.src_prefix;
1626
1627 if (api.vrf_id == VRF_DEFAULT && api.tableid != 0)
1628 table_id = api.tableid;
1629 else
1630 table_id = zvrf->table_id;
1631
1632 rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance,
1633 api.flags, &api.prefix, src_p, NULL, table_id, api.metric,
1634 api.distance, false);
1635
1636 /* Stats */
1637 switch (api.prefix.family) {
1638 case AF_INET:
1639 client->v4_route_del_cnt++;
1640 break;
1641 case AF_INET6:
1642 client->v6_route_del_cnt++;
1643 break;
1644 }
1645 }
1646
1647 /* MRIB Nexthop lookup for IPv4. */
1648 static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS)
1649 {
1650 struct in_addr addr;
1651 struct route_entry *re;
1652
1653 STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN);
1654 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1655 zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
1656
1657 stream_failure:
1658 return;
1659 }
1660
1661 /* Register zebra server router-id information. Send current router-id */
1662 static void zread_router_id_add(ZAPI_HANDLER_ARGS)
1663 {
1664 struct prefix p;
1665
1666 /* Router-id information is needed. */
1667 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
1668
1669 router_id_get(&p, zvrf_id(zvrf));
1670
1671 zsend_router_id_update(client, &p, zvrf_id(zvrf));
1672 }
1673
1674 /* Unregister zebra server router-id information. */
1675 static void zread_router_id_delete(ZAPI_HANDLER_ARGS)
1676 {
1677 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1678 }
1679
1680 static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf)
1681 {
1682 struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1683
1684 zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id);
1685 stream_putl(s, vrf_get_backend());
1686 stream_putc(s, mpls_enabled);
1687 stream_putl(s, zrouter.multipath_num);
1688 stream_putc(s, zebra_mlag_get_role());
1689
1690 stream_putw_at(s, 0, stream_get_endp(s));
1691 zserv_send_message(client, s);
1692 }
1693
1694 void zsend_capabilities_all_clients(void)
1695 {
1696 struct listnode *node, *nnode;
1697 struct zebra_vrf *zvrf;
1698 struct zserv *client;
1699
1700 zvrf = vrf_info_lookup(VRF_DEFAULT);
1701 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
1702 zsend_capabilities(client, zvrf);
1703 }
1704 }
1705
1706 /* Tie up route-type and client->sock */
1707 static void zread_hello(ZAPI_HANDLER_ARGS)
1708 {
1709 /* type of protocol (lib/zebra.h) */
1710 uint8_t proto;
1711 unsigned short instance;
1712 uint8_t notify;
1713
1714 STREAM_GETC(msg, proto);
1715 STREAM_GETW(msg, instance);
1716 STREAM_GETC(msg, notify);
1717 if (notify)
1718 client->notify_owner = true;
1719
1720 /* accept only dynamic routing protocols */
1721 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) {
1722 zlog_notice(
1723 "client %d says hello and bids fair to announce only %s routes vrf=%u",
1724 client->sock, zebra_route_string(proto),
1725 zvrf->vrf->vrf_id);
1726 if (instance)
1727 zlog_notice("client protocol instance %d", instance);
1728
1729 client->proto = proto;
1730 client->instance = instance;
1731 }
1732
1733 zsend_capabilities(client, zvrf);
1734 zebra_vrf_update_all(client);
1735 stream_failure:
1736 return;
1737 }
1738
1739 /* Unregister all information in a VRF. */
1740 static void zread_vrf_unregister(ZAPI_HANDLER_ARGS)
1741 {
1742 int i;
1743 afi_t afi;
1744
1745 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
1746 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1747 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1748 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
1749 }
1750 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1751 }
1752
1753 static void zread_mpls_labels(ZAPI_HANDLER_ARGS)
1754 {
1755 struct stream *s;
1756 enum lsp_types_t type;
1757 struct prefix prefix;
1758 enum nexthop_types_t gtype;
1759 union g_addr gate;
1760 ifindex_t ifindex;
1761 mpls_label_t in_label, out_label;
1762 uint8_t distance;
1763
1764 /* Get input stream. */
1765 s = msg;
1766
1767 /* Get data. */
1768 STREAM_GETC(s, type);
1769 STREAM_GETL(s, prefix.family);
1770 switch (prefix.family) {
1771 case AF_INET:
1772 STREAM_GET(&prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN);
1773 STREAM_GETC(s, prefix.prefixlen);
1774 if (prefix.prefixlen > IPV4_MAX_BITLEN) {
1775 zlog_debug(
1776 "%s: Specified prefix length %d is greater than a v4 address can support",
1777 __PRETTY_FUNCTION__, prefix.prefixlen);
1778 return;
1779 }
1780 STREAM_GET(&gate.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
1781 break;
1782 case AF_INET6:
1783 STREAM_GET(&prefix.u.prefix6, s, 16);
1784 STREAM_GETC(s, prefix.prefixlen);
1785 if (prefix.prefixlen > IPV6_MAX_BITLEN) {
1786 zlog_debug(
1787 "%s: Specified prefix length %d is greater than a v6 address can support",
1788 __PRETTY_FUNCTION__, prefix.prefixlen);
1789 return;
1790 }
1791 STREAM_GET(&gate.ipv6, s, 16);
1792 break;
1793 default:
1794 zlog_debug("%s: Specified AF %d is not supported for this call",
1795 __PRETTY_FUNCTION__, prefix.family);
1796 return;
1797 }
1798 STREAM_GETL(s, ifindex);
1799 STREAM_GETC(s, distance);
1800 STREAM_GETL(s, in_label);
1801 STREAM_GETL(s, out_label);
1802
1803 switch (prefix.family) {
1804 case AF_INET:
1805 if (ifindex)
1806 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1807 else
1808 gtype = NEXTHOP_TYPE_IPV4;
1809 break;
1810 case AF_INET6:
1811 if (ifindex)
1812 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1813 else
1814 gtype = NEXTHOP_TYPE_IPV6;
1815 break;
1816 default:
1817 return;
1818 }
1819
1820 if (!mpls_enabled)
1821 return;
1822
1823 if (hdr->command == ZEBRA_MPLS_LABELS_ADD) {
1824 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
1825 ifindex);
1826 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
1827 distance, out_label);
1828 } else if (hdr->command == ZEBRA_MPLS_LABELS_DELETE) {
1829 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
1830 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
1831 distance, out_label);
1832 }
1833 stream_failure:
1834 return;
1835 }
1836
1837 /* Send response to a table manager connect request to client */
1838 static void zread_table_manager_connect(struct zserv *client,
1839 struct stream *msg, vrf_id_t vrf_id)
1840 {
1841 struct stream *s;
1842 uint8_t proto;
1843 uint16_t instance;
1844
1845 s = msg;
1846
1847 /* Get data. */
1848 STREAM_GETC(s, proto);
1849 STREAM_GETW(s, instance);
1850
1851 /* accept only dynamic routing protocols */
1852 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1853 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1854 "client %d has wrong protocol %s", client->sock,
1855 zebra_route_string(proto));
1856 zsend_table_manager_connect_response(client, vrf_id, 1);
1857 return;
1858 }
1859 zlog_notice("client %d with vrf %u instance %u connected as %s",
1860 client->sock, vrf_id, instance, zebra_route_string(proto));
1861 client->proto = proto;
1862 client->instance = instance;
1863
1864 /*
1865 * Release previous labels of same protocol and instance.
1866 * This is done in case it restarted from an unexpected shutdown.
1867 */
1868 release_daemon_table_chunks(client);
1869
1870 zsend_table_manager_connect_response(client, vrf_id, 0);
1871
1872 stream_failure:
1873 return;
1874 }
1875
1876 static void zread_label_manager_connect(struct zserv *client,
1877 struct stream *msg, vrf_id_t vrf_id)
1878 {
1879 struct stream *s;
1880 /* type of protocol (lib/zebra.h) */
1881 uint8_t proto;
1882 unsigned short instance;
1883
1884 /* Get input stream. */
1885 s = msg;
1886
1887 /* Get data. */
1888 STREAM_GETC(s, proto);
1889 STREAM_GETW(s, instance);
1890
1891 /* accept only dynamic routing protocols */
1892 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1893 flog_err(EC_ZEBRA_TM_WRONG_PROTO,
1894 "client %d has wrong protocol %s", client->sock,
1895 zebra_route_string(proto));
1896 if (client->is_synchronous)
1897 zsend_label_manager_connect_response(client, vrf_id, 1);
1898 return;
1899 }
1900 zlog_notice("client %d with vrf %u instance %u connected as %s",
1901 client->sock, vrf_id, instance, zebra_route_string(proto));
1902 client->proto = proto;
1903 client->instance = instance;
1904
1905 /*
1906 * Release previous labels of same protocol and instance.
1907 * This is done in case it restarted from an unexpected shutdown.
1908 */
1909 release_daemon_label_chunks(client);
1910
1911 zlog_debug(
1912 " Label Manager client connected: sock %d, proto %s, vrf %u instance %u",
1913 client->sock, zebra_route_string(proto), vrf_id, instance);
1914 /* send response back */
1915 if (client->is_synchronous)
1916 zsend_label_manager_connect_response(client, vrf_id, 0);
1917
1918 stream_failure:
1919 return;
1920 }
1921
1922 static void zread_get_label_chunk(struct zserv *client, struct stream *msg,
1923 vrf_id_t vrf_id)
1924 {
1925 struct stream *s;
1926 uint8_t keep;
1927 uint32_t size;
1928 struct label_manager_chunk *lmc;
1929 uint8_t proto;
1930 unsigned short instance;
1931
1932 /* Get input stream. */
1933 s = msg;
1934
1935 /* Get data. */
1936 STREAM_GETC(s, proto);
1937 STREAM_GETW(s, instance);
1938 STREAM_GETC(s, keep);
1939 STREAM_GETL(s, size);
1940
1941 lmc = assign_label_chunk(proto, instance, keep, size);
1942 if (!lmc)
1943 flog_err(
1944 EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK,
1945 "Unable to assign Label Chunk of size %u to %s instance %u",
1946 size, zebra_route_string(proto), instance);
1947 else
1948 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
1949 lmc->start, lmc->end,
1950 zebra_route_string(proto), instance);
1951 /* send response back */
1952 zsend_assign_label_chunk_response(client, vrf_id, lmc);
1953
1954 stream_failure:
1955 return;
1956 }
1957
1958 static void zread_release_label_chunk(struct zserv *client, struct stream *msg)
1959 {
1960 struct stream *s;
1961 uint32_t start, end;
1962 uint8_t proto;
1963 unsigned short instance;
1964
1965 /* Get input stream. */
1966 s = msg;
1967
1968 /* Get data. */
1969 STREAM_GETC(s, proto);
1970 STREAM_GETW(s, instance);
1971 STREAM_GETL(s, start);
1972 STREAM_GETL(s, end);
1973
1974 release_label_chunk(proto, instance, start, end);
1975
1976 stream_failure:
1977 return;
1978 }
1979 static void zread_label_manager_request(ZAPI_HANDLER_ARGS)
1980 {
1981 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1982 client->is_synchronous = hdr->command ==
1983 ZEBRA_LABEL_MANAGER_CONNECT;
1984
1985 /* external label manager */
1986 if (lm_is_external)
1987 zread_relay_label_manager_request(hdr->command, client, msg,
1988 zvrf_id(zvrf));
1989 /* this is a label manager */
1990 else {
1991 if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT ||
1992 hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC)
1993 zread_label_manager_connect(client, msg, zvrf_id(zvrf));
1994 else {
1995 if (hdr->command == ZEBRA_GET_LABEL_CHUNK)
1996 zread_get_label_chunk(client, msg,
1997 zvrf_id(zvrf));
1998 else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK)
1999 zread_release_label_chunk(client, msg);
2000 }
2001 }
2002 }
2003
2004 static void zread_get_table_chunk(struct zserv *client, struct stream *msg,
2005 vrf_id_t vrf_id)
2006 {
2007 struct stream *s;
2008 uint32_t size;
2009 struct table_manager_chunk *tmc;
2010
2011 /* Get input stream. */
2012 s = msg;
2013
2014 /* Get data. */
2015 STREAM_GETL(s, size);
2016
2017 tmc = assign_table_chunk(client->proto, client->instance, size);
2018 if (!tmc)
2019 flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK,
2020 "%s: Unable to assign Table Chunk of size %u",
2021 __func__, size);
2022 else
2023 zlog_debug("Assigned Table Chunk %u - %u", tmc->start,
2024 tmc->end);
2025 /* send response back */
2026 zsend_assign_table_chunk_response(client, vrf_id, tmc);
2027
2028 stream_failure:
2029 return;
2030 }
2031
2032 static void zread_release_table_chunk(struct zserv *client, struct stream *msg)
2033 {
2034 struct stream *s;
2035 uint32_t start, end;
2036
2037 /* Get input stream. */
2038 s = msg;
2039
2040 /* Get data. */
2041 STREAM_GETL(s, start);
2042 STREAM_GETL(s, end);
2043
2044 release_table_chunk(client->proto, client->instance, start, end);
2045
2046 stream_failure:
2047 return;
2048 }
2049
2050 static void zread_table_manager_request(ZAPI_HANDLER_ARGS)
2051 {
2052 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2053 if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT)
2054 zread_table_manager_connect(client, msg, zvrf_id(zvrf));
2055 else {
2056 /* Sanity: don't allow 'unidentified' requests */
2057 if (!client->proto) {
2058 flog_err(
2059 EC_ZEBRA_TM_ALIENS,
2060 "Got table request from an unidentified client");
2061 return;
2062 }
2063 if (hdr->command == ZEBRA_GET_TABLE_CHUNK)
2064 zread_get_table_chunk(client, msg, zvrf_id(zvrf));
2065 else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK)
2066 zread_release_table_chunk(client, msg);
2067 }
2068 }
2069
2070 static void zread_pseudowire(ZAPI_HANDLER_ARGS)
2071 {
2072 struct stream *s;
2073 char ifname[IF_NAMESIZE];
2074 ifindex_t ifindex;
2075 int type;
2076 int af;
2077 union g_addr nexthop;
2078 uint32_t local_label;
2079 uint32_t remote_label;
2080 uint8_t flags;
2081 union pw_protocol_fields data;
2082 uint8_t protocol;
2083 struct zebra_pw *pw;
2084
2085 /* Get input stream. */
2086 s = msg;
2087
2088 /* Get data. */
2089 STREAM_GET(ifname, s, IF_NAMESIZE);
2090 STREAM_GETL(s, ifindex);
2091 STREAM_GETL(s, type);
2092 STREAM_GETL(s, af);
2093 switch (af) {
2094 case AF_INET:
2095 STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN);
2096 break;
2097 case AF_INET6:
2098 STREAM_GET(&nexthop.ipv6, s, 16);
2099 break;
2100 default:
2101 return;
2102 }
2103 STREAM_GETL(s, local_label);
2104 STREAM_GETL(s, remote_label);
2105 STREAM_GETC(s, flags);
2106 STREAM_GET(&data, s, sizeof(data));
2107 protocol = client->proto;
2108
2109 pw = zebra_pw_find(zvrf, ifname);
2110 switch (hdr->command) {
2111 case ZEBRA_PW_ADD:
2112 if (pw) {
2113 flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS,
2114 "%s: pseudowire %s already exists [%s]",
2115 __func__, ifname,
2116 zserv_command_string(hdr->command));
2117 return;
2118 }
2119
2120 zebra_pw_add(zvrf, ifname, protocol, client);
2121 break;
2122 case ZEBRA_PW_DELETE:
2123 if (!pw) {
2124 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2125 "%s: pseudowire %s not found [%s]", __func__,
2126 ifname, zserv_command_string(hdr->command));
2127 return;
2128 }
2129
2130 zebra_pw_del(zvrf, pw);
2131 break;
2132 case ZEBRA_PW_SET:
2133 case ZEBRA_PW_UNSET:
2134 if (!pw) {
2135 flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT,
2136 "%s: pseudowire %s not found [%s]", __func__,
2137 ifname, zserv_command_string(hdr->command));
2138 return;
2139 }
2140
2141 switch (hdr->command) {
2142 case ZEBRA_PW_SET:
2143 pw->enabled = 1;
2144 break;
2145 case ZEBRA_PW_UNSET:
2146 pw->enabled = 0;
2147 break;
2148 }
2149
2150 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2151 remote_label, flags, &data);
2152 break;
2153 }
2154
2155 stream_failure:
2156 return;
2157 }
2158
2159 static void zread_interface_set_master(ZAPI_HANDLER_ARGS)
2160 {
2161 struct interface *master;
2162 struct interface *slave;
2163 struct stream *s = msg;
2164 int ifindex;
2165 vrf_id_t vrf_id;
2166
2167 STREAM_GETL(s, vrf_id);
2168 STREAM_GETL(s, ifindex);
2169 master = if_lookup_by_index(ifindex, vrf_id);
2170
2171 STREAM_GETL(s, vrf_id);
2172 STREAM_GETL(s, ifindex);
2173 slave = if_lookup_by_index(ifindex, vrf_id);
2174
2175 if (!master || !slave)
2176 return;
2177
2178 kernel_interface_set_master(master, slave);
2179
2180 stream_failure:
2181 return;
2182 }
2183
2184
2185 static void zread_vrf_label(ZAPI_HANDLER_ARGS)
2186 {
2187 struct interface *ifp;
2188 mpls_label_t nlabel;
2189 afi_t afi;
2190 struct stream *s;
2191 struct zebra_vrf *def_zvrf;
2192 enum lsp_types_t ltype;
2193
2194 s = msg;
2195 STREAM_GETL(s, nlabel);
2196 STREAM_GETC(s, afi);
2197 if (nlabel == zvrf->label[afi]) {
2198 /*
2199 * Nothing to do here move along
2200 */
2201 return;
2202 }
2203
2204 STREAM_GETC(s, ltype);
2205
2206 if (zvrf->vrf->vrf_id != VRF_DEFAULT)
2207 ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id);
2208 else
2209 ifp = if_lookup_by_name("lo", VRF_DEFAULT);
2210
2211 if (!ifp) {
2212 zlog_debug("Unable to find specified Interface for %s",
2213 zvrf->vrf->name);
2214 return;
2215 }
2216
2217 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
2218
2219 if (zvrf->label[afi] != MPLS_LABEL_NONE) {
2220 afi_t scrubber;
2221 bool really_remove;
2222
2223 really_remove = true;
2224 for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) {
2225 if (scrubber == afi)
2226 continue;
2227
2228 if (zvrf->label[scrubber] == MPLS_LABEL_NONE)
2229 continue;
2230
2231 if (zvrf->label[afi] == zvrf->label[scrubber]) {
2232 really_remove = false;
2233 break;
2234 }
2235 }
2236
2237 if (really_remove)
2238 mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
2239 NEXTHOP_TYPE_IFINDEX, NULL,
2240 ifp->ifindex);
2241 }
2242
2243 if (nlabel != MPLS_LABEL_NONE)
2244 mpls_lsp_install(def_zvrf, ltype, nlabel,
2245 MPLS_LABEL_IMPLICIT_NULL, NEXTHOP_TYPE_IFINDEX,
2246 NULL, ifp->ifindex);
2247
2248 zvrf->label[afi] = nlabel;
2249 stream_failure:
2250 return;
2251 }
2252
2253 static inline void zread_rule(ZAPI_HANDLER_ARGS)
2254 {
2255 struct zebra_pbr_rule zpr;
2256 struct stream *s;
2257 uint32_t total, i;
2258 ifindex_t ifindex;
2259
2260 s = msg;
2261 STREAM_GETL(s, total);
2262
2263 for (i = 0; i < total; i++) {
2264 memset(&zpr, 0, sizeof(zpr));
2265
2266 zpr.sock = client->sock;
2267 zpr.rule.vrf_id = hdr->vrf_id;
2268 STREAM_GETL(s, zpr.rule.seq);
2269 STREAM_GETL(s, zpr.rule.priority);
2270 STREAM_GETL(s, zpr.rule.unique);
2271 STREAM_GETC(s, zpr.rule.filter.src_ip.family);
2272 STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen);
2273 STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s,
2274 prefix_blen(&zpr.rule.filter.src_ip));
2275 STREAM_GETW(s, zpr.rule.filter.src_port);
2276 STREAM_GETC(s, zpr.rule.filter.dst_ip.family);
2277 STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen);
2278 STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s,
2279 prefix_blen(&zpr.rule.filter.dst_ip));
2280 STREAM_GETW(s, zpr.rule.filter.dst_port);
2281 STREAM_GETL(s, zpr.rule.filter.fwmark);
2282 STREAM_GETL(s, zpr.rule.action.table);
2283 STREAM_GETL(s, ifindex);
2284
2285 if (ifindex) {
2286 zpr.ifp = if_lookup_by_index_per_ns(
2287 zvrf->zns,
2288 ifindex);
2289 if (!zpr.ifp) {
2290 zlog_debug("Failed to lookup ifindex: %u",
2291 ifindex);
2292 return;
2293 }
2294 }
2295
2296 if (!is_default_prefix(&zpr.rule.filter.src_ip))
2297 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP;
2298
2299 if (!is_default_prefix(&zpr.rule.filter.dst_ip))
2300 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP;
2301
2302 if (zpr.rule.filter.src_port)
2303 zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT;
2304
2305 if (zpr.rule.filter.dst_port)
2306 zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT;
2307
2308 if (zpr.rule.filter.fwmark)
2309 zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK;
2310
2311 zpr.vrf_id = zvrf->vrf->vrf_id;
2312 if (hdr->command == ZEBRA_RULE_ADD)
2313 zebra_pbr_add_rule(&zpr);
2314 else
2315 zebra_pbr_del_rule(&zpr);
2316 }
2317
2318 stream_failure:
2319 return;
2320 }
2321
2322 static inline void zread_ipset(ZAPI_HANDLER_ARGS)
2323 {
2324 struct zebra_pbr_ipset zpi;
2325 struct stream *s;
2326 uint32_t total, i;
2327
2328 s = msg;
2329 STREAM_GETL(s, total);
2330
2331 for (i = 0; i < total; i++) {
2332 memset(&zpi, 0, sizeof(zpi));
2333
2334 zpi.sock = client->sock;
2335 zpi.vrf_id = zvrf->vrf->vrf_id;
2336 STREAM_GETL(s, zpi.unique);
2337 STREAM_GETL(s, zpi.type);
2338 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2339
2340 if (hdr->command == ZEBRA_IPSET_CREATE)
2341 zebra_pbr_create_ipset(&zpi);
2342 else
2343 zebra_pbr_destroy_ipset(&zpi);
2344 }
2345
2346 stream_failure:
2347 return;
2348 }
2349
2350 static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS)
2351 {
2352 struct zebra_pbr_ipset_entry zpi;
2353 struct zebra_pbr_ipset ipset;
2354 struct stream *s;
2355 uint32_t total, i;
2356
2357 s = msg;
2358 STREAM_GETL(s, total);
2359
2360 for (i = 0; i < total; i++) {
2361 memset(&zpi, 0, sizeof(zpi));
2362 memset(&ipset, 0, sizeof(ipset));
2363
2364 zpi.sock = client->sock;
2365 STREAM_GETL(s, zpi.unique);
2366 STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2367 STREAM_GETC(s, zpi.src.family);
2368 STREAM_GETC(s, zpi.src.prefixlen);
2369 STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src));
2370 STREAM_GETC(s, zpi.dst.family);
2371 STREAM_GETC(s, zpi.dst.prefixlen);
2372 STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst));
2373
2374 STREAM_GETW(s, zpi.src_port_min);
2375 STREAM_GETW(s, zpi.src_port_max);
2376 STREAM_GETW(s, zpi.dst_port_min);
2377 STREAM_GETW(s, zpi.dst_port_max);
2378 STREAM_GETC(s, zpi.proto);
2379 if (!is_default_prefix(&zpi.src))
2380 zpi.filter_bm |= PBR_FILTER_SRC_IP;
2381
2382 if (!is_default_prefix(&zpi.dst))
2383 zpi.filter_bm |= PBR_FILTER_DST_IP;
2384 if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2385 zpi.filter_bm |= PBR_FILTER_DST_PORT;
2386 if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP)
2387 zpi.filter_bm |= PBR_FILTER_SRC_PORT;
2388 if (zpi.dst_port_max != 0)
2389 zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE;
2390 if (zpi.src_port_max != 0)
2391 zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE;
2392 if (zpi.proto != 0)
2393 zpi.filter_bm |= PBR_FILTER_PROTO;
2394
2395 /* calculate backpointer */
2396 zpi.backpointer =
2397 zebra_pbr_lookup_ipset_pername(ipset.ipset_name);
2398 if (hdr->command == ZEBRA_IPSET_ENTRY_ADD)
2399 zebra_pbr_add_ipset_entry(&zpi);
2400 else
2401 zebra_pbr_del_ipset_entry(&zpi);
2402 }
2403
2404 stream_failure:
2405 return;
2406 }
2407
2408 static inline void zread_iptable(ZAPI_HANDLER_ARGS)
2409 {
2410 struct zebra_pbr_iptable zpi;
2411 struct stream *s;
2412
2413 s = msg;
2414
2415 memset(&zpi, 0, sizeof(zpi));
2416
2417 zpi.interface_name_list = list_new();
2418 zpi.sock = client->sock;
2419 zpi.vrf_id = zvrf->vrf->vrf_id;
2420 STREAM_GETL(s, zpi.unique);
2421 STREAM_GETL(s, zpi.type);
2422 STREAM_GETL(s, zpi.filter_bm);
2423 STREAM_GETL(s, zpi.action);
2424 STREAM_GETL(s, zpi.fwmark);
2425 STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE);
2426 STREAM_GETW(s, zpi.pkt_len_min);
2427 STREAM_GETW(s, zpi.pkt_len_max);
2428 STREAM_GETW(s, zpi.tcp_flags);
2429 STREAM_GETW(s, zpi.tcp_mask_flags);
2430 STREAM_GETC(s, zpi.dscp_value);
2431 STREAM_GETC(s, zpi.fragment);
2432 STREAM_GETL(s, zpi.nb_interface);
2433 zebra_pbr_iptable_update_interfacelist(s, &zpi);
2434
2435 if (hdr->command == ZEBRA_IPTABLE_ADD)
2436 zebra_pbr_add_iptable(&zpi);
2437 else
2438 zebra_pbr_del_iptable(&zpi);
2439 stream_failure:
2440 return;
2441 }
2442
2443 void (*zserv_handlers[])(ZAPI_HANDLER_ARGS) = {
2444 [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add,
2445 [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete,
2446 [ZEBRA_INTERFACE_ADD] = zread_interface_add,
2447 [ZEBRA_INTERFACE_DELETE] = zread_interface_delete,
2448 [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown,
2449 [ZEBRA_ROUTE_ADD] = zread_route_add,
2450 [ZEBRA_ROUTE_DELETE] = zread_route_del,
2451 [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add,
2452 [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete,
2453 [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add,
2454 [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete,
2455 [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib,
2456 [ZEBRA_HELLO] = zread_hello,
2457 [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register,
2458 [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister,
2459 [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register,
2460 [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister,
2461 [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register,
2462 [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register,
2463 [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister,
2464 #if HAVE_BFDD > 0
2465 [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay,
2466 #endif /* HAVE_BFDD */
2467 [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister,
2468 [ZEBRA_VRF_LABEL] = zread_vrf_label,
2469 [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register,
2470 #if defined(HAVE_RTADV)
2471 [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable,
2472 [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable,
2473 #else
2474 [ZEBRA_INTERFACE_ENABLE_RADV] = NULL,
2475 [ZEBRA_INTERFACE_DISABLE_RADV] = NULL,
2476 #endif
2477 [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels,
2478 [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels,
2479 [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats,
2480 [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request,
2481 [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request,
2482 [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request,
2483 [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request,
2484 [ZEBRA_FEC_REGISTER] = zread_fec_register,
2485 [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister,
2486 [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip,
2487 [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip,
2488 [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet,
2489 [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni,
2490 [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add,
2491 [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del,
2492 [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add,
2493 [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del,
2494 [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection,
2495 [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master,
2496 [ZEBRA_PW_ADD] = zread_pseudowire,
2497 [ZEBRA_PW_DELETE] = zread_pseudowire,
2498 [ZEBRA_PW_SET] = zread_pseudowire,
2499 [ZEBRA_PW_UNSET] = zread_pseudowire,
2500 [ZEBRA_RULE_ADD] = zread_rule,
2501 [ZEBRA_RULE_DELETE] = zread_rule,
2502 [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request,
2503 [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request,
2504 [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request,
2505 [ZEBRA_IPSET_CREATE] = zread_ipset,
2506 [ZEBRA_IPSET_DESTROY] = zread_ipset,
2507 [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry,
2508 [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry,
2509 [ZEBRA_IPTABLE_ADD] = zread_iptable,
2510 [ZEBRA_IPTABLE_DELETE] = zread_iptable,
2511 [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control,
2512 };
2513
2514 #if defined(HANDLE_ZAPI_FUZZING)
2515 extern struct zebra_privs_t zserv_privs;
2516
2517 static void zserv_write_incoming(struct stream *orig, uint16_t command)
2518 {
2519 char fname[MAXPATHLEN];
2520 struct stream *copy;
2521 int fd = -1;
2522
2523 copy = stream_dup(orig);
2524 stream_set_getp(copy, 0);
2525
2526 snprintf(fname, MAXPATHLEN, "%s/%u", DAEMON_VTY_DIR, command);
2527
2528 frr_elevate_privs(&zserv_privs) {
2529 fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
2530 }
2531 stream_flush(copy, fd);
2532 close(fd);
2533 stream_free(copy);
2534 }
2535 #endif
2536
2537 void zserv_handle_commands(struct zserv *client, struct stream *msg)
2538 {
2539 struct zmsghdr hdr;
2540 struct zebra_vrf *zvrf;
2541
2542 zapi_parse_header(msg, &hdr);
2543
2544 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2545 zserv_log_message(NULL, msg, &hdr);
2546
2547 #if defined(HANDLE_ZAPI_FUZZING)
2548 zserv_write_incoming(msg, hdr.command);
2549 #endif
2550
2551 hdr.length -= ZEBRA_HEADER_SIZE;
2552
2553 /* lookup vrf */
2554 zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id);
2555 if (!zvrf) {
2556 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2557 zlog_debug("ZAPI message specifies unknown VRF: %d",
2558 hdr.vrf_id);
2559 return;
2560 }
2561
2562 if (hdr.command >= array_size(zserv_handlers)
2563 || zserv_handlers[hdr.command] == NULL)
2564 zlog_info("Zebra received unknown command %d", hdr.command);
2565 else
2566 zserv_handlers[hdr.command](client, &hdr, msg, zvrf);
2567 }