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