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