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