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