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