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