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