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