]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_zebra.c
Merge pull request #10615 from mobash-rasool/fixes
[mirror_frr.git] / bgpd / bgp_zebra.c
CommitLineData
718e3744 1/* zebra client
896014f4
DL
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#include <zebra.h>
22
23#include "command.h"
24#include "stream.h"
25#include "network.h"
26#include "prefix.h"
27#include "log.h"
28#include "sockunion.h"
29#include "zclient.h"
30#include "routemap.h"
31#include "thread.h"
3f9c7369 32#include "queue.h"
6e919709 33#include "memory.h"
856ca177 34#include "lib/json.h"
2376c3f2 35#include "lib/bfd.h"
94effaf0 36#include "lib/route_opaque.h"
039f3a34 37#include "filter.h"
cd1964ff 38#include "mpls.h"
128ea8ab 39#include "vxlan.h"
6cfe5d15 40#include "pbr.h"
718e3744 41
42#include "bgpd/bgpd.h"
43#include "bgpd/bgp_route.h"
44#include "bgpd/bgp_attr.h"
e46723a5 45#include "bgpd/bgp_aspath.h"
718e3744 46#include "bgpd/bgp_nexthop.h"
47#include "bgpd/bgp_zebra.h"
48#include "bgpd/bgp_fsm.h"
a39275d7 49#include "bgpd/bgp_debug.h"
14454c9f 50#include "bgpd/bgp_errors.h"
8196f13d 51#include "bgpd/bgp_mpath.h"
fb018d25 52#include "bgpd/bgp_nexthop.h"
ffd0c037 53#include "bgpd/bgp_nht.h"
8c4f6381 54#include "bgpd/bgp_bfd.h"
cd1964ff 55#include "bgpd/bgp_label.h"
49e5a4a0 56#ifdef ENABLE_BGP_VNC
d62a17ae 57#include "bgpd/rfapi/rfapi_backend.h"
58#include "bgpd/rfapi/vnc_export_bgp.h"
65efcfce 59#endif
128ea8ab 60#include "bgpd/bgp_evpn.h"
ddb5b488 61#include "bgpd/bgp_mplsvpn.h"
955bfd98 62#include "bgpd/bgp_labelpool.h"
30d50e6d 63#include "bgpd/bgp_pbr.h"
0b9d9cd0 64#include "bgpd/bgp_evpn_private.h"
c44ab6f1 65#include "bgpd/bgp_evpn_mh.h"
6a69ac51 66#include "bgpd/bgp_mac.h"
a383bfc7 67#include "bgpd/bgp_trace.h"
959331a3
RW
68#include "bgpd/bgp_community.h"
69#include "bgpd/bgp_lcommunity.h"
6b0655a2 70
718e3744 71/* All information about zebra. */
228da428 72struct zclient *zclient = NULL;
718e3744 73
0d020cd6
PR
74/* hook to indicate vrf status change for SNMP */
75DEFINE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp),
8451921b 76 (bgp, ifp));
0d020cd6 77
ad4cbda1 78/* Can we install into zebra? */
3dc339cd 79static inline bool bgp_install_info_to_zebra(struct bgp *bgp)
ad4cbda1 80{
d62a17ae 81 if (zclient->sock <= 0)
3dc339cd 82 return false;
ad4cbda1 83
bb4ef1ae 84 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
15569c58
DA
85 zlog_debug(
86 "%s: No zebra instance to talk to, not installing information",
87 __func__);
3dc339cd 88 return false;
bb4ef1ae 89 }
ad4cbda1 90
3dc339cd 91 return true;
ad4cbda1 92}
93
afbb1c59
LB
94int zclient_num_connects;
95
18a6dce6 96/* Router-id update message from zebra. */
121f9dee 97static int bgp_router_id_update(ZAPI_CALLBACK_ARGS)
718e3744 98{
d62a17ae 99 struct prefix router_id;
718e3744 100
d62a17ae 101 zebra_router_id_update_read(zclient->ibuf, &router_id);
a39275d7 102
2dbe669b
DA
103 if (BGP_DEBUG(zebra, ZEBRA))
104 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id,
105 &router_id);
a39275d7 106
d62a17ae 107 bgp_router_id_zebra_bump(vrf_id, &router_id);
108 return 0;
718e3744 109}
110
fb018d25 111/* Nexthop update message from zebra. */
121f9dee 112static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS)
fb018d25 113{
121f9dee 114 bgp_parse_nexthop_update(cmd, vrf_id);
d62a17ae 115 return 0;
078430f6
DS
116}
117
4a04e5f7 118/* Set or clear interface on which unnumbered neighbor is configured. This
119 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
120 * interface.
121 */
d62a17ae 122static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
123 struct interface *upd_ifp)
4a04e5f7 124{
d62a17ae 125 struct listnode *node, *nnode;
126 struct peer *peer;
127
128 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
129 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
130 if (upd_ifp) {
131 peer->ifp = upd_ifp;
132 bgp_zebra_initiate_radv(bgp, peer);
133 } else {
134 bgp_zebra_terminate_radv(bgp, peer);
135 peer->ifp = upd_ifp;
136 }
137 }
138 }
4a04e5f7 139}
140
a243d1db 141static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS)
cd1964ff 142{
d62a17ae 143 bgp_parse_fec_update();
144 return 0;
cd1964ff
DS
145}
146
d62a17ae 147static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
a80beece 148{
d62a17ae 149 struct listnode *node, *nnode;
150 struct peer *peer;
151
152 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
153 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
feb17238 154 && !peer_established(peer)) {
d62a17ae 155 if (peer_active(peer))
156 BGP_EVENT_ADD(peer, BGP_Stop);
157 BGP_EVENT_ADD(peer, BGP_Start);
158 }
159 }
a80beece
DS
160}
161
d62a17ae 162static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
a197c47c 163{
d62a17ae 164 struct listnode *node;
165 struct connected *connected;
166 struct interface *ifp;
167 struct prefix *p;
168
169 /* Kick-off the FSM for any relevant peers only if there is a
170 * valid local address on the interface.
171 */
172 ifp = ifc->ifp;
173 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
174 p = connected->address;
175 if (p->family == AF_INET6
176 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
177 break;
178 }
179 if (!connected)
180 return;
181
182 bgp_start_interface_nbrs(bgp, ifp);
a197c47c
DS
183}
184
d62a17ae 185static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
186 int del)
a80beece 187{
d62a17ae 188 struct listnode *node, *nnode;
189 struct peer *peer;
190 struct interface *ifp;
191
192 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
193 if (peer->conf_if
194 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
195 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
196 BGP_EVENT_ADD(peer, BGP_Stop);
197 }
198 }
199 /* Free neighbor also, if we're asked to. */
200 if (del) {
201 ifp = ifc->ifp;
202 listnode_delete(ifp->nbr_connected, ifc);
203 nbr_connected_free(ifc);
204 }
a80beece
DS
205}
206
3c3c3252 207static int bgp_ifp_destroy(struct interface *ifp)
718e3744 208{
d62a17ae 209 struct bgp *bgp;
718e3744 210
096f7609 211 bgp = ifp->vrf->info;
a4499b83 212
d62a17ae 213 if (BGP_DEBUG(zebra, ZEBRA))
096f7609
IR
214 zlog_debug("Rx Intf del VRF %u IF %s", ifp->vrf->vrf_id,
215 ifp->name);
a39275d7 216
0d020cd6 217 if (bgp) {
85751d1d 218 bgp_update_interface_nbrs(bgp, ifp, NULL);
0d020cd6
PR
219 hook_call(bgp_vrf_status_changed, bgp, ifp);
220 }
64745052 221
6a69ac51
DS
222 bgp_mac_del_mac_entry(ifp);
223
d62a17ae 224 return 0;
718e3744 225}
226
ddbf3e60 227static int bgp_ifp_up(struct interface *ifp)
718e3744 228{
d62a17ae 229 struct connected *c;
230 struct nbr_connected *nc;
231 struct listnode *node, *nnode;
232 struct bgp *bgp;
6aeb9e78 233
096f7609 234 bgp = ifp->vrf->info;
718e3744 235
6a69ac51
DS
236 bgp_mac_add_mac_entry(ifp);
237
d62a17ae 238 if (BGP_DEBUG(zebra, ZEBRA))
096f7609
IR
239 zlog_debug("Rx Intf up VRF %u IF %s", ifp->vrf->vrf_id,
240 ifp->name);
ad4cbda1 241
85751d1d
DS
242 if (!bgp)
243 return 0;
244
d62a17ae 245 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
246 bgp_connected_add(bgp, c);
718e3744 247
d62a17ae 248 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
249 bgp_nbr_connected_add(bgp, nc);
a80beece 250
0d020cd6 251 hook_call(bgp_vrf_status_changed, bgp, ifp);
8761cd6d
DS
252 bgp_nht_ifp_up(ifp);
253
d62a17ae 254 return 0;
718e3744 255}
256
b0b69e59 257static int bgp_ifp_down(struct interface *ifp)
718e3744 258{
d62a17ae 259 struct connected *c;
260 struct nbr_connected *nc;
261 struct listnode *node, *nnode;
262 struct bgp *bgp;
c19fe3c7 263 struct peer *peer;
6aeb9e78 264
096f7609 265 bgp = ifp->vrf->info;
718e3744 266
6a69ac51
DS
267 bgp_mac_del_mac_entry(ifp);
268
d62a17ae 269 if (BGP_DEBUG(zebra, ZEBRA))
096f7609
IR
270 zlog_debug("Rx Intf down VRF %u IF %s", ifp->vrf->vrf_id,
271 ifp->name);
ad4cbda1 272
85751d1d
DS
273 if (!bgp)
274 return 0;
275
d62a17ae 276 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
277 bgp_connected_delete(bgp, c);
718e3744 278
d62a17ae 279 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
280 bgp_nbr_connected_delete(bgp, nc, 1);
a80beece 281
d62a17ae 282 /* Fast external-failover */
c19fe3c7 283 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
718e3744 284
d62a17ae 285 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
50bd8995 286 /* Take down directly connected peers. */
c8d6f0d6 287 if ((peer->ttl != BGP_DEFAULT_TTL)
e2521429 288 && (peer->gtsm_hops != BGP_GTSM_HOPS_CONNECTED))
d62a17ae 289 continue;
718e3744 290
d62a17ae 291 if (ifp == peer->nexthop.ifp) {
292 BGP_EVENT_ADD(peer, BGP_Stop);
293 peer->last_reset = PEER_DOWN_IF_DOWN;
294 }
295 }
296 }
718e3744 297
0d020cd6 298 hook_call(bgp_vrf_status_changed, bgp, ifp);
8761cd6d
DS
299 bgp_nht_ifp_down(ifp);
300
d62a17ae 301 return 0;
718e3744 302}
303
121f9dee 304static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS)
718e3744 305{
d62a17ae 306 struct connected *ifc;
2f9123e0
DS
307 struct bgp *bgp;
308
309 bgp = bgp_lookup_by_vrf_id(vrf_id);
d62a17ae 310
121f9dee 311 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
d62a17ae 312
313 if (ifc == NULL)
314 return 0;
315
2dbe669b
DA
316 if (bgp_debug_zebra(ifc->address))
317 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id,
318 ifc->ifp->name, ifc->address);
d62a17ae 319
85751d1d
DS
320 if (!bgp)
321 return 0;
322
d62a17ae 323 if (if_is_operative(ifc->ifp)) {
d62a17ae 324 bgp_connected_add(bgp, ifc);
2f9123e0 325
d62a17ae 326 /* If we have learnt of any neighbors on this interface,
327 * check to kick off any BGP interface-based neighbors,
328 * but only if this is a link-local address.
329 */
330 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
331 && !list_isempty(ifc->ifp->nbr_connected))
332 bgp_start_interface_nbrs(bgp, ifc->ifp);
333 }
334
335 return 0;
718e3744 336}
337
121f9dee 338static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS)
718e3744 339{
f3d20a2a 340 struct listnode *node, *nnode;
d62a17ae 341 struct connected *ifc;
f3d20a2a 342 struct peer *peer;
d62a17ae 343 struct bgp *bgp;
f3d20a2a 344 struct prefix *addr;
6aeb9e78 345
2f9123e0 346 bgp = bgp_lookup_by_vrf_id(vrf_id);
2f9123e0 347
121f9dee 348 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
718e3744 349
d62a17ae 350 if (ifc == NULL)
351 return 0;
718e3744 352
2dbe669b
DA
353 if (bgp_debug_zebra(ifc->address))
354 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id,
355 ifc->ifp->name, ifc->address);
a39275d7 356
85751d1d 357 if (bgp && if_is_operative(ifc->ifp)) {
2f9123e0 358 bgp_connected_delete(bgp, ifc);
d62a17ae 359 }
718e3744 360
f3d20a2a
DS
361 addr = ifc->address;
362
363 if (bgp) {
364 /*
365 * When we are using the v6 global as part of the peering
366 * nexthops and we are removing it, then we need to
367 * clear the peer data saved for that nexthop and
368 * cause a re-announcement of the route. Since
369 * we do not want the peering to bounce.
370 */
371 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
372 afi_t afi;
373 safi_t safi;
374
375 if (addr->family == AF_INET)
376 continue;
377
378 if (!IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6)
379 && memcmp(&peer->nexthop.v6_global,
380 &addr->u.prefix6, 16)
381 == 0) {
382 memset(&peer->nexthop.v6_global, 0, 16);
383 FOREACH_AFI_SAFI (afi, safi)
384 bgp_announce_route(peer, afi, safi,
385 true);
386 }
387 }
388 }
389
721c0857 390 connected_free(&ifc);
718e3744 391
d62a17ae 392 return 0;
718e3744 393}
394
121f9dee 395static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS)
a80beece 396{
d62a17ae 397 struct nbr_connected *ifc = NULL;
398 struct bgp *bgp;
399
121f9dee 400 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
d62a17ae 401
402 if (ifc == NULL)
403 return 0;
404
2dbe669b
DA
405 if (bgp_debug_zebra(ifc->address))
406 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
407 vrf_id, ifc->ifp->name, ifc->address);
d62a17ae 408
409 if (if_is_operative(ifc->ifp)) {
410 bgp = bgp_lookup_by_vrf_id(vrf_id);
411 if (bgp)
412 bgp_nbr_connected_add(bgp, ifc);
413 }
414
415 return 0;
a80beece
DS
416}
417
121f9dee 418static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS)
a80beece 419{
d62a17ae 420 struct nbr_connected *ifc = NULL;
421 struct bgp *bgp;
6aeb9e78 422
121f9dee 423 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
a80beece 424
d62a17ae 425 if (ifc == NULL)
426 return 0;
a80beece 427
2dbe669b
DA
428 if (bgp_debug_zebra(ifc->address))
429 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
430 vrf_id, ifc->ifp->name, ifc->address);
a80beece 431
d62a17ae 432 if (if_is_operative(ifc->ifp)) {
433 bgp = bgp_lookup_by_vrf_id(vrf_id);
434 if (bgp)
435 bgp_nbr_connected_delete(bgp, ifc, 0);
436 }
a80beece 437
d62a17ae 438 nbr_connected_free(ifc);
a80beece 439
d62a17ae 440 return 0;
a80beece
DS
441}
442
bfcd43b2 443/* VRF update for an interface. */
121f9dee 444static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS)
bfcd43b2 445{
d62a17ae 446 struct interface *ifp;
447 vrf_id_t new_vrf_id;
448 struct connected *c;
449 struct nbr_connected *nc;
450 struct listnode *node, *nnode;
451 struct bgp *bgp;
c19fe3c7 452 struct peer *peer;
bfcd43b2 453
d62a17ae 454 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
455 &new_vrf_id);
456 if (!ifp)
457 return 0;
bfcd43b2 458
d62a17ae 459 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
460 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
461 ifp->name, new_vrf_id);
bfcd43b2 462
d62a17ae 463 bgp = bgp_lookup_by_vrf_id(vrf_id);
bfcd43b2 464
85751d1d
DS
465 if (bgp) {
466 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
467 bgp_connected_delete(bgp, c);
bfcd43b2 468
85751d1d
DS
469 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
470 bgp_nbr_connected_delete(bgp, nc, 1);
bfcd43b2 471
85751d1d
DS
472 /* Fast external-failover */
473 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
474 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
c8d6f0d6 475 if ((peer->ttl != BGP_DEFAULT_TTL)
e2521429
DA
476 && (peer->gtsm_hops
477 != BGP_GTSM_HOPS_CONNECTED))
85751d1d 478 continue;
bfcd43b2 479
85751d1d
DS
480 if (ifp == peer->nexthop.ifp)
481 BGP_EVENT_ADD(peer, BGP_Stop);
482 }
d62a17ae 483 }
484 }
bfcd43b2 485
a36898e7 486 if_update_to_new_vrf(ifp, new_vrf_id);
bfcd43b2 487
d62a17ae 488 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
489 if (!bgp)
490 return 0;
bfcd43b2 491
d62a17ae 492 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
493 bgp_connected_add(bgp, c);
bfcd43b2 494
d62a17ae 495 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
496 bgp_nbr_connected_add(bgp, nc);
0d020cd6
PR
497
498 hook_call(bgp_vrf_status_changed, bgp, ifp);
d62a17ae 499 return 0;
bfcd43b2 500}
501
718e3744 502/* Zebra route add and delete treatment. */
121f9dee 503static int zebra_read_route(ZAPI_CALLBACK_ARGS)
718e3744 504{
9de1f7ff 505 enum nexthop_types_t nhtype;
0789eb69 506 enum blackhole_type bhtype = BLACKHOLE_UNSPEC;
74489921 507 struct zapi_route api;
0789eb69 508 union g_addr nexthop = {};
9de1f7ff 509 ifindex_t ifindex;
74489921 510 int add, i;
d62a17ae 511 struct bgp *bgp;
512
513 bgp = bgp_lookup_by_vrf_id(vrf_id);
514 if (!bgp)
515 return 0;
516
74489921
RW
517 if (zapi_route_decode(zclient->ibuf, &api) < 0)
518 return -1;
d62a17ae 519
74489921
RW
520 /* we completely ignore srcdest routes for now. */
521 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
522 return 0;
d62a17ae 523
74489921
RW
524 /* ignore link-local address. */
525 if (api.prefix.family == AF_INET6
526 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
527 return 0;
d62a17ae 528
74489921 529 ifindex = api.nexthops[0].ifindex;
9de1f7ff 530 nhtype = api.nexthops[0].type;
d62a17ae 531
0789eb69
KM
532 /* api_nh structure has union of gate and bh_type */
533 if (nhtype == NEXTHOP_TYPE_BLACKHOLE) {
534 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
535 bhtype = api.nexthops[0].bh_type;
536 } else
537 nexthop = api.nexthops[0].gate;
538
121f9dee 539 add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
74489921 540 if (add) {
d62a17ae 541 /*
542 * The ADD message is actually an UPDATE and there is no
543 * explicit DEL
544 * for a prior redistributed route, if any. So, perform an
545 * implicit
546 * DEL processing for the same redistributed route from any
547 * other
548 * source type.
549 */
550 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
551 if (i != api.type)
74489921 552 bgp_redistribute_delete(bgp, &api.prefix, i,
d62a17ae 553 api.instance);
554 }
555
556 /* Now perform the add/update. */
74489921 557 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
0789eb69
KM
558 nhtype, bhtype, api.distance, api.metric,
559 api.type, api.instance, api.tag);
d62a17ae 560 } else {
74489921
RW
561 bgp_redistribute_delete(bgp, &api.prefix, api.type,
562 api.instance);
a39275d7 563 }
d62a17ae 564
74489921 565 if (bgp_debug_zebra(&api.prefix)) {
2dbe669b 566 char buf[PREFIX_STRLEN];
74489921 567
77e62f2b 568 if (add) {
2dbe669b
DA
569 inet_ntop(api.prefix.family, &nexthop, buf,
570 sizeof(buf));
77e62f2b 571 zlog_debug(
801bb996 572 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI,
77e62f2b 573 vrf_id, zebra_route_string(api.type),
2dbe669b 574 api.instance, &api.prefix, buf, nhtype, ifindex,
801bb996 575 api.metric, api.distance, api.tag);
77e62f2b 576 } else {
6bdbcbf1 577 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id,
2dbe669b 578 zebra_route_string(api.type), api.instance,
6bdbcbf1 579 &api.prefix);
77e62f2b 580 }
d62a17ae 581 }
582
583 return 0;
718e3744 584}
6b0655a2 585
d62a17ae 586struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
718e3744 587{
f4e14fdb 588 struct vrf *vrf;
d62a17ae 589 struct listnode *cnode;
590 struct interface *ifp;
591 struct connected *connected;
592 struct prefix_ipv4 p;
593 struct prefix *cp;
594
f4e14fdb
RW
595 vrf = vrf_lookup_by_id(vrf_id);
596 if (!vrf)
597 return NULL;
598
d62a17ae 599 p.family = AF_INET;
600 p.prefix = *addr;
601 p.prefixlen = IPV4_MAX_BITLEN;
602
451fda4f 603 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 604 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
605 cp = connected->address;
606
607 if (cp->family == AF_INET)
608 if (prefix_match(cp, (struct prefix *)&p))
609 return ifp;
610 }
718e3744 611 }
d62a17ae 612 return NULL;
718e3744 613}
614
d62a17ae 615struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
718e3744 616{
f4e14fdb 617 struct vrf *vrf;
d62a17ae 618 struct listnode *cnode;
619 struct interface *ifp;
620 struct connected *connected;
621 struct prefix *cp;
622
f4e14fdb
RW
623 vrf = vrf_lookup_by_id(vrf_id);
624 if (!vrf)
625 return NULL;
626
451fda4f 627 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 628 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
629 cp = connected->address;
630
631 if (cp->family == AF_INET)
632 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
633 return ifp;
634 }
718e3744 635 }
d62a17ae 636 return NULL;
718e3744 637}
638
d62a17ae 639struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
640 vrf_id_t vrf_id)
718e3744 641{
f4e14fdb 642 struct vrf *vrf;
d62a17ae 643 struct listnode *cnode;
644 struct interface *ifp;
645 struct connected *connected;
646 struct prefix_ipv6 p;
647 struct prefix *cp;
648
f4e14fdb
RW
649 vrf = vrf_lookup_by_id(vrf_id);
650 if (!vrf)
651 return NULL;
652
d62a17ae 653 p.family = AF_INET6;
654 p.prefix = *addr;
655 p.prefixlen = IPV6_MAX_BITLEN;
656
451fda4f 657 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 658 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
659 cp = connected->address;
660
661 if (cp->family == AF_INET6)
662 if (prefix_match(cp, (struct prefix *)&p)) {
663 if (IN6_IS_ADDR_LINKLOCAL(
664 &cp->u.prefix6)) {
665 if (ifindex == ifp->ifindex)
666 return ifp;
667 } else
668 return ifp;
669 }
670 }
718e3744 671 }
d62a17ae 672 return NULL;
718e3744 673}
674
d62a17ae 675struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
676 ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 677{
f4e14fdb 678 struct vrf *vrf;
d62a17ae 679 struct listnode *cnode;
680 struct interface *ifp;
681 struct connected *connected;
682 struct prefix *cp;
683
f4e14fdb
RW
684 vrf = vrf_lookup_by_id(vrf_id);
685 if (!vrf)
686 return NULL;
687
451fda4f 688 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 689 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
690 cp = connected->address;
691
692 if (cp->family == AF_INET6)
693 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
694 if (IN6_IS_ADDR_LINKLOCAL(
695 &cp->u.prefix6)) {
696 if (ifindex == ifp->ifindex)
697 return ifp;
698 } else
699 return ifp;
700 }
701 }
718e3744 702 }
d62a17ae 703 return NULL;
718e3744 704}
705
d62a17ae 706static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
718e3744 707{
d62a17ae 708 struct listnode *cnode;
709 struct connected *connected;
710 struct prefix *cp;
711
712 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
713 cp = connected->address;
714
715 if (cp->family == AF_INET6)
716 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
717 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
718 return 1;
719 }
720 }
721 return 0;
718e3744 722}
723
dac42f2e 724static bool if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
718e3744 725{
d62a17ae 726 struct listnode *cnode;
727 struct connected *connected;
728 struct prefix *cp;
729
730 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
731 cp = connected->address;
732
733 if (cp->family == AF_INET6)
734 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
735 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
dac42f2e 736 return true;
d62a17ae 737 }
738 }
dac42f2e 739 return false;
718e3744 740}
718e3744 741
d62a17ae 742static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
6ee06fa9 743{
d62a17ae 744 struct listnode *cnode;
745 struct connected *connected;
746 struct prefix *cp;
747
748 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
749 cp = connected->address;
750 if ((cp->family == AF_INET)
751 && !ipv4_martian(&(cp->u.prefix4))) {
752 *addr = cp->u.prefix4;
753 return 1;
754 }
755 }
756 return 0;
6ee06fa9
PM
757}
758
17cdd31e
DS
759
760bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
761 struct bgp_nexthop *nexthop, struct peer *peer)
718e3744 762{
d62a17ae 763 int ret = 0;
764 struct interface *ifp = NULL;
dac42f2e 765 bool v6_ll_avail = true;
d62a17ae 766
767 memset(nexthop, 0, sizeof(struct bgp_nexthop));
768
769 if (!local)
17cdd31e 770 return false;
d62a17ae 771 if (!remote)
17cdd31e 772 return false;
d62a17ae 773
774 if (local->sa.sa_family == AF_INET) {
775 nexthop->v4 = local->sin.sin_addr;
776 if (peer->update_if)
777 ifp = if_lookup_by_name(peer->update_if,
a36898e7 778 peer->bgp->vrf_id);
d62a17ae 779 else
780 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
781 peer->bgp->vrf_id);
718e3744 782 }
d62a17ae 783 if (local->sa.sa_family == AF_INET6) {
8f2b2139 784 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
d62a17ae 785 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
786 if (peer->conf_if || peer->ifname)
787 ifp = if_lookup_by_name(peer->conf_if
788 ? peer->conf_if
789 : peer->ifname,
a36898e7 790 peer->bgp->vrf_id);
abe68054
PG
791 else if (peer->update_if)
792 ifp = if_lookup_by_name(peer->update_if,
793 peer->bgp->vrf_id);
d62a17ae 794 } else if (peer->update_if)
795 ifp = if_lookup_by_name(peer->update_if,
a36898e7 796 peer->bgp->vrf_id);
d62a17ae 797 else
798 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
799 local->sin6.sin6_scope_id,
800 peer->bgp->vrf_id);
718e3744 801 }
d62a17ae 802
17cdd31e
DS
803 if (!ifp) {
804 /*
805 * BGP views do not currently get proper data
806 * from zebra( when attached ) to be able to
807 * properly resolve nexthops, so give this
808 * instance type a pass.
809 */
810 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
811 return true;
812 /*
813 * If we have no interface data but we have established
814 * some connection w/ zebra than something has gone
815 * terribly terribly wrong here, so say this failed
816 * If we do not any zebra connection then not
817 * having a ifp pointer is ok.
818 */
819 return zclient_num_connects ? false : true;
820 }
d62a17ae 821
822 nexthop->ifp = ifp;
823
824 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
825 if (local->sa.sa_family == AF_INET) {
826 /* IPv6 nexthop*/
827 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
828
829 if (!ret) {
830 /* There is no global nexthop. Use link-local address as
831 * both the
832 * global and link-local nexthop. In this scenario, the
833 * expectation
834 * for interop is that the network admin would use a
835 * route-map to
836 * specify the global IPv6 nexthop.
837 */
dac42f2e
DS
838 v6_ll_avail =
839 if_get_ipv6_local(ifp, &nexthop->v6_global);
d62a17ae 840 memcpy(&nexthop->v6_local, &nexthop->v6_global,
841 IPV6_MAX_BYTELEN);
842 } else
dac42f2e
DS
843 v6_ll_avail =
844 if_get_ipv6_local(ifp, &nexthop->v6_local);
d62a17ae 845
dac42f2e
DS
846 /*
847 * If we are a v4 connection and we are not doing unnumbered
848 * not having a v6 LL address is ok
849 */
850 if (!v6_ll_avail && !peer->conf_if)
851 v6_ll_avail = true;
d62a17ae 852 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
853 peer->shared_network = 1;
854 else
855 peer->shared_network = 0;
718e3744 856 }
718e3744 857
d62a17ae 858 /* IPv6 connection, fetch and store IPv4 local address if any. */
859 if (local->sa.sa_family == AF_INET6) {
860 struct interface *direct = NULL;
861
862 /* IPv4 nexthop. */
863 ret = if_get_ipv4_address(ifp, &nexthop->v4);
975a328e 864 if (!ret && peer->local_id.s_addr != INADDR_ANY)
d62a17ae 865 nexthop->v4 = peer->local_id;
866
867 /* Global address*/
868 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
869 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
870 IPV6_MAX_BYTELEN);
871
872 /* If directory connected set link-local address. */
873 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
874 remote->sin6.sin6_scope_id,
875 peer->bgp->vrf_id);
876 if (direct)
dac42f2e
DS
877 v6_ll_avail = if_get_ipv6_local(
878 ifp, &nexthop->v6_local);
2ebb354c
IR
879 /*
880 * It's fine to not have a v6 LL when using
881 * update-source loopback/vrf
882 */
608c8870 883 if (!v6_ll_avail && if_is_loopback(ifp))
2ebb354c 884 v6_ll_avail = true;
d62a17ae 885 } else
886 /* Link-local address. */
887 {
888 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
889
890 /* If there is no global address. Set link-local
891 address as
892 global. I know this break RFC specification... */
893 /* In this scenario, the expectation for interop is that
894 * the
895 * network admin would use a route-map to specify the
896 * global
897 * IPv6 nexthop.
898 */
899 if (!ret)
900 memcpy(&nexthop->v6_global,
901 &local->sin6.sin6_addr,
902 IPV6_MAX_BYTELEN);
903 /* Always set the link-local address */
904 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
905 IPV6_MAX_BYTELEN);
906 }
907
908 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
909 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
910 remote->sin6.sin6_scope_id,
911 peer->bgp->vrf_id))
912 peer->shared_network = 1;
913 else
914 peer->shared_network = 0;
915 }
718e3744 916
d62a17ae 917/* KAME stack specific treatment. */
718e3744 918#ifdef KAME
d62a17ae 919 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
920 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
921 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
922 }
923 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
924 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
925 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
926 }
718e3744 927#endif /* KAME */
e33a4880 928
d62a17ae 929 /* If we have identified the local interface, there is no error for now.
930 */
dac42f2e 931 return v6_ll_avail;
718e3744 932}
933
18ee8310 934static struct in6_addr *
40381db7 935bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
73ac8160 936{
d62a17ae 937 struct in6_addr *nexthop = NULL;
938
939 /* Only global address nexthop exists. */
dc94fe42
RW
940 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
941 || path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
40381db7 942 nexthop = &path->attr->mp_nexthop_global;
77e62f2b 943 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
40381db7 944 *ifindex = path->attr->nh_ifindex;
77e62f2b 945 }
d62a17ae 946
947 /* If both global and link-local address present. */
dc94fe42
RW
948 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
949 || path->attr->mp_nexthop_len
950 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
d62a17ae 951 /* Check if route-map is set to prefer global over link-local */
40381db7
DS
952 if (path->attr->mp_nexthop_prefer_global) {
953 nexthop = &path->attr->mp_nexthop_global;
77e62f2b 954 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
40381db7 955 *ifindex = path->attr->nh_ifindex;
77e62f2b 956 } else {
d62a17ae 957 /* Workaround for Cisco's nexthop bug. */
958 if (IN6_IS_ADDR_UNSPECIFIED(
40381db7 959 &path->attr->mp_nexthop_global)
ab0e0f73 960 && path->peer->su_remote
40381db7 961 && path->peer->su_remote->sa.sa_family
59a0f1cb 962 == AF_INET6) {
d62a17ae 963 nexthop =
40381db7 964 &path->peer->su_remote->sin6.sin6_addr;
77e62f2b 965 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
40381db7 966 *ifindex = path->peer->nexthop.ifp
59a0f1cb 967 ->ifindex;
77e62f2b 968 } else {
40381db7 969 nexthop = &path->attr->mp_nexthop_local;
77e62f2b 970 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
40381db7 971 *ifindex = path->attr->nh_lla_ifindex;
77e62f2b 972 }
d62a17ae 973 }
974 }
975
976 return nexthop;
73ac8160
DS
977}
978
b8685f9b 979static bool bgp_table_map_apply(struct route_map *map, const struct prefix *p,
3dc339cd 980 struct bgp_path_info *path)
73ac8160 981{
b4cb15c6
DL
982 route_map_result_t ret;
983
1782514f 984 ret = route_map_apply(map, p, path);
40381db7 985 bgp_attr_flush(path->attr);
b4cb15c6
DL
986
987 if (ret != RMAP_DENYMATCH)
3dc339cd 988 return true;
d62a17ae 989
990 if (bgp_debug_zebra(p)) {
991 if (p->family == AF_INET) {
d62a17ae 992 zlog_debug(
c0d72166
DS
993 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
994 p, &path->attr->nexthop);
d62a17ae 995 }
996 if (p->family == AF_INET6) {
997 char buf[2][INET6_ADDRSTRLEN];
77e62f2b 998 ifindex_t ifindex;
999 struct in6_addr *nexthop;
1000
40381db7 1001 nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex);
d62a17ae 1002 zlog_debug(
8228a9a7
DS
1003 "Zebra rmap deny: IPv6 route %pFX nexthop %s",
1004 p,
6b9265a3
DA
1005 nexthop ? inet_ntop(AF_INET6, nexthop, buf[1],
1006 sizeof(buf[1]))
1007 : inet_ntop(AF_INET,
1008 &path->attr->nexthop,
1009 buf[1], sizeof(buf[1])));
d62a17ae 1010 }
1011 }
3dc339cd 1012 return false;
73ac8160
DS
1013}
1014
b98f7728
PG
1015static struct thread *bgp_tm_thread_connect;
1016static bool bgp_tm_status_connected;
31c28cd7
PG
1017static bool bgp_tm_chunk_obtained;
1018#define BGP_FLOWSPEC_TABLE_CHUNK 100000
1019static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size;
6818e7e5 1020struct bgp *bgp_tm_bgp;
b98f7728
PG
1021
1022static int bgp_zebra_tm_connect(struct thread *t)
1023{
1024 struct zclient *zclient;
1025 int delay = 10, ret = 0;
1026
1027 zclient = THREAD_ARG(t);
1028 if (bgp_tm_status_connected && zclient->sock > 0)
1029 delay = 60;
1030 else {
1031 bgp_tm_status_connected = false;
1032 ret = tm_table_manager_connect(zclient);
1033 }
1034 if (ret < 0) {
b7cd3069 1035 zlog_info("Error connecting to table manager!");
b98f7728
PG
1036 bgp_tm_status_connected = false;
1037 } else {
1038 if (!bgp_tm_status_connected)
1039 zlog_debug("Connecting to table manager. Success");
1040 bgp_tm_status_connected = true;
31c28cd7
PG
1041 if (!bgp_tm_chunk_obtained) {
1042 if (bgp_zebra_get_table_range(bgp_tm_chunk_size,
1043 &bgp_tm_min,
6818e7e5 1044 &bgp_tm_max) >= 0) {
31c28cd7 1045 bgp_tm_chunk_obtained = true;
6818e7e5
PG
1046 /* parse non installed entries */
1047 bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC);
1048 }
31c28cd7 1049 }
b98f7728
PG
1050 }
1051 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1052 &bgp_tm_thread_connect);
1053 return 0;
1054}
1055
6818e7e5
PG
1056bool bgp_zebra_tm_chunk_obtained(void)
1057{
1058 return bgp_tm_chunk_obtained;
1059}
1060
31c28cd7
PG
1061uint32_t bgp_zebra_tm_get_id(void)
1062{
1063 static int table_id;
1064
1065 if (!bgp_tm_chunk_obtained)
1066 return ++table_id;
1067 return bgp_tm_min++;
1068}
1069
6818e7e5 1070void bgp_zebra_init_tm_connect(struct bgp *bgp)
b98f7728
PG
1071{
1072 int delay = 1;
1073
1074 /* if already set, do nothing
1075 */
1076 if (bgp_tm_thread_connect != NULL)
1077 return;
1078 bgp_tm_status_connected = false;
31c28cd7
PG
1079 bgp_tm_chunk_obtained = false;
1080 bgp_tm_min = bgp_tm_max = 0;
1081 bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
6818e7e5 1082 bgp_tm_bgp = bgp;
b98f7728
PG
1083 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1084 &bgp_tm_thread_connect);
1085}
1086
1087int bgp_zebra_get_table_range(uint32_t chunk_size,
1088 uint32_t *start, uint32_t *end)
1089{
1090 int ret;
1091
1092 if (!bgp_tm_status_connected)
1093 return -1;
1094 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1095 if (ret < 0) {
e50f7cfd 1096 flog_err(EC_BGP_TABLE_CHUNK,
1c50c1c0 1097 "BGP: Error getting table chunk %u", chunk_size);
b98f7728
PG
1098 return -1;
1099 }
1100 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1101 chunk_size, *start, *end);
1102 return 0;
1103}
1104
3dc339cd
DA
1105static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1106 struct in_addr *nexthop,
1107 struct attr *attr, bool is_evpn,
1108 struct zapi_nexthop *api_nh)
77e62f2b 1109{
1110 api_nh->gate.ipv4 = *nexthop;
e1e71450 1111 api_nh->vrf_id = nh_bgp->vrf_id;
77e62f2b 1112
1113 /* Need to set fields appropriately for EVPN routes imported into
1114 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1115 * connected routes leaked into a VRF.
1116 */
0789eb69
KM
1117 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1118 api_nh->type = attr->nh_type;
1119 api_nh->bh_type = attr->bh_type;
1120 } else if (is_evpn) {
a2299aba
AD
1121 /*
1122 * If the nexthop is EVPN overlay index gateway IP,
1123 * treat the nexthop as NEXTHOP_TYPE_IPV4
1124 * Else, mark the nexthop as onlink.
1125 */
1126 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1127 api_nh->type = NEXTHOP_TYPE_IPV4;
1128 else {
1129 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1130 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1131 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1132 }
0789eb69 1133 } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) {
77e62f2b 1134 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1135 api_nh->ifindex = attr->nh_ifindex;
1136 } else
1137 api_nh->type = NEXTHOP_TYPE_IPV4;
1138
3dc339cd 1139 return true;
77e62f2b 1140}
1141
3dc339cd
DA
1142static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1143 struct in6_addr *nexthop,
1144 ifindex_t ifindex,
1145 struct bgp_path_info *pi,
1146 struct bgp_path_info *best_pi,
1147 bool is_evpn,
1148 struct zapi_nexthop *api_nh)
77e62f2b 1149{
1150 struct attr *attr;
1151
40381db7 1152 attr = pi->attr;
e1e71450 1153 api_nh->vrf_id = nh_bgp->vrf_id;
77e62f2b 1154
0789eb69
KM
1155 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1156 api_nh->type = attr->nh_type;
1157 api_nh->bh_type = attr->bh_type;
1158 } else if (is_evpn) {
a2299aba
AD
1159 /*
1160 * If the nexthop is EVPN overlay index gateway IP,
1161 * treat the nexthop as NEXTHOP_TYPE_IPV4
1162 * Else, mark the nexthop as onlink.
1163 */
1164 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1165 api_nh->type = NEXTHOP_TYPE_IPV6;
1166 else {
1167 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1168 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1169 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1170 }
e1e71450 1171 } else if (nh_othervrf) {
77e62f2b 1172 if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1173 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1174 api_nh->ifindex = attr->nh_ifindex;
1175 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1176 if (ifindex == 0)
3dc339cd 1177 return false;
77e62f2b 1178 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1179 api_nh->ifindex = ifindex;
1180 } else {
1181 api_nh->type = NEXTHOP_TYPE_IPV6;
1182 api_nh->ifindex = 0;
1183 }
1184 } else {
1185 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
40381db7
DS
1186 if (pi == best_pi
1187 && attr->mp_nexthop_len
1188 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1189 if (pi->peer->nexthop.ifp)
1190 ifindex =
1191 pi->peer->nexthop.ifp->ifindex;
77e62f2b 1192 if (!ifindex) {
40381db7
DS
1193 if (pi->peer->conf_if)
1194 ifindex = pi->peer->ifp->ifindex;
1195 else if (pi->peer->ifname)
77e62f2b 1196 ifindex = ifname2ifindex(
40381db7
DS
1197 pi->peer->ifname,
1198 pi->peer->bgp->vrf_id);
1199 else if (pi->peer->nexthop.ifp)
1200 ifindex =
1201 pi->peer->nexthop.ifp->ifindex;
77e62f2b 1202 }
1203
1204 if (ifindex == 0)
3dc339cd 1205 return false;
77e62f2b 1206 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1207 api_nh->ifindex = ifindex;
1208 } else {
1209 api_nh->type = NEXTHOP_TYPE_IPV6;
1210 api_nh->ifindex = 0;
1211 }
1212 }
0789eb69
KM
1213 /* api_nh structure has union of gate and bh_type */
1214 if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE)
c2ca3e25 1215 api_nh->gate.ipv6 = *nexthop;
77e62f2b 1216
3dc339cd 1217 return true;
77e62f2b 1218}
1219
f7e1c681 1220static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr,
1221 uint64_t tot_bw, uint32_t *nh_weight)
4e30bc2b 1222{
f7e1c681 1223 uint32_t bw;
1224 uint64_t tmp;
1225
1226 bw = attr->link_bw;
1227 /* zero link-bandwidth and link-bandwidth not present are treated
1228 * as the same situation.
1229 */
1230 if (!bw) {
1231 /* the only situations should be if we're either told
1232 * to skip or use default weight.
1233 */
1234 if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING)
1235 return false;
1236 *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT;
1237 } else {
1238 tmp = (uint64_t)bw * 100;
1239 *nh_weight = ((uint32_t)(tmp / tot_bw));
1240 }
1241
1242 return true;
4e30bc2b 1243}
1244
9bcb3eef 1245void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
4b7e6066 1246 struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
d62a17ae 1247 safi_t safi)
718e3744 1248{
7cc27d41 1249 struct zapi_route api = { 0 };
9913029c 1250 struct zapi_nexthop *api_nh;
2ad4f093 1251 int nh_family;
a74e593b 1252 unsigned int valid_nh_count = 0;
1a9cb083 1253 bool allow_recursion = false;
d7c0a89a 1254 uint8_t distance;
d62a17ae 1255 struct peer *peer;
4b7e6066 1256 struct bgp_path_info *mpinfo;
ad1844f7 1257 struct bgp *bgp_orig;
d7c0a89a 1258 uint32_t metric;
b4cb15c6 1259 struct attr local_attr;
4b7e6066
DS
1260 struct bgp_path_info local_info;
1261 struct bgp_path_info *mpinfo_cp = &local_info;
d62a17ae 1262 route_tag_t tag;
1263 mpls_label_t label;
ddb5b488 1264 int nh_othervrf = 0;
3f54c705 1265 bool is_evpn;
06f16b2c 1266 bool nh_updated = false;
4e30bc2b 1267 bool do_wt_ecmp;
1268 uint64_t cum_bw = 0;
6348981a
AK
1269 uint32_t nhg_id = 0;
1270 bool is_add;
d62a17ae 1271
1272 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1273 * know of this instance.
1274 */
1275 if (!bgp_install_info_to_zebra(bgp))
1276 return;
1277
d62a17ae 1278 if (bgp->main_zebra_update_hold)
1279 return;
1280
d90b788e 1281 if (safi == SAFI_FLOWSPEC) {
9bcb3eef
DS
1282 bgp_pbr_update_entry(bgp, bgp_dest_get_prefix(dest), info, afi,
1283 safi, true);
d90b788e
A
1284 return;
1285 }
529efa23 1286
ddb5b488
PZ
1287 /*
1288 * vrf leaking support (will have only one nexthop)
1289 */
1290 if (info->extra && info->extra->bgp_orig)
1291 nh_othervrf = 1;
1292
9913029c 1293 /* Make Zebra API structure. */
9913029c
RW
1294 api.vrf_id = bgp->vrf_id;
1295 api.type = ZEBRA_ROUTE_BGP;
1296 api.safi = safi;
1297 api.prefix = *p;
1298 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1299
d62a17ae 1300 peer = info->peer;
1301
ddb5b488
PZ
1302 if (info->type == ZEBRA_ROUTE_BGP
1303 && info->sub_type == BGP_ROUTE_IMPORTED) {
1304
1ec90b5e 1305 /* Obtain peer from parent */
1306 if (info->extra && info->extra->parent)
4b7e6066
DS
1307 peer = ((struct bgp_path_info *)(info->extra->parent))
1308 ->peer;
ddb5b488
PZ
1309 }
1310
d62a17ae 1311 tag = info->attr->tag;
1312
88493076 1313 /* If the route's source is EVPN, flag as such. */
77e62f2b 1314 is_evpn = is_route_parent_evpn(info);
1315 if (is_evpn)
90264d64 1316 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
d3135ba3 1317
d62a17ae 1318 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1319 || info->sub_type == BGP_ROUTE_AGGREGATE) {
9913029c 1320 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
4e8b02f4 1321 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
718e3744 1322 }
a39275d7 1323
c8d6f0d6 1324 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != BGP_DEFAULT_TTL)
d62a17ae 1325 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
892fedb6 1326 || CHECK_FLAG(bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
d62a17ae 1327
1a9cb083 1328 allow_recursion = true;
d62a17ae 1329
951745bd
PG
1330 if (info->attr->rmap_table_id) {
1331 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1332 api.tableid = info->attr->rmap_table_id;
1333 }
1334
ef3e0d04
SM
1335 if (CHECK_FLAG(info->attr->flag, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1336 SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
1337
2ad4f093
RW
1338 /* Metric is currently based on the best-path only */
1339 metric = info->attr->med;
4e30bc2b 1340
1341 /* Determine if we're doing weighted ECMP or not */
f7e1c681 1342 do_wt_ecmp = bgp_path_info_mpath_chkwtd(bgp, info);
4e30bc2b 1343 if (do_wt_ecmp)
1344 cum_bw = bgp_path_info_mpath_cumbw(info);
1345
6348981a 1346 /* EVPN MAC-IP routes are installed with a L3 NHG id */
8bcb09a1 1347 if (bgp_evpn_path_es_use_nhg(bgp, info, &nhg_id)) {
6348981a 1348 mpinfo = NULL;
8bcb09a1
AK
1349 api.nhgid = nhg_id;
1350 if (nhg_id)
1351 SET_FLAG(api.message, ZAPI_MESSAGE_NHG);
1352 } else {
6348981a 1353 mpinfo = info;
8bcb09a1 1354 }
6348981a
AK
1355
1356 for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
f7e1c681 1357 uint32_t nh_weight;
1358
a74e593b
RW
1359 if (valid_nh_count >= multipath_num)
1360 break;
1361
b4cb15c6 1362 *mpinfo_cp = *mpinfo;
f7e1c681 1363 nh_weight = 0;
b4cb15c6 1364
d0d695f4
RW
1365 /* Get nexthop address-family */
1366 if (p->family == AF_INET
1367 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1368 nh_family = AF_INET;
1369 else if (p->family == AF_INET6
1370 || (p->family == AF_INET
1371 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1372 nh_family = AF_INET6;
1373 else
1374 continue;
1375
f7e1c681 1376 /* If processing for weighted ECMP, determine the next hop's
1377 * weight. Based on user setting, we may skip the next hop
1378 * in some situations.
1379 */
1380 if (do_wt_ecmp) {
1381 if (!bgp_zebra_use_nhop_weighted(bgp, mpinfo->attr,
1382 cum_bw, &nh_weight))
1383 continue;
1384 }
1374aec9 1385 api_nh = &api.nexthops[valid_nh_count];
ef3e0d04
SM
1386
1387 if (CHECK_FLAG(info->attr->flag,
1388 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1389 api_nh->srte_color = info->attr->srte_color;
1390
257b7b6e
DS
1391 if (bgp_debug_zebra(&api.prefix)) {
1392 if (mpinfo->extra) {
70492dea
DS
1393 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1394 __func__, p,
257b7b6e
DS
1395 bgp_is_valid_label(
1396 &mpinfo->extra->label[0]));
1397 } else {
70492dea
DS
1398 zlog_debug(
1399 "%s: p=%pFX, extra is NULL, no label",
1400 __func__, p);
ddb5b488 1401 }
257b7b6e 1402 }
ddb5b488 1403
257b7b6e
DS
1404 if (bgp->table_map[afi][safi].name) {
1405 /* Copy info and attributes, so the route-map
1406 apply doesn't modify the BGP route info. */
1407 local_attr = *mpinfo->attr;
1408 mpinfo_cp->attr = &local_attr;
1409 if (!bgp_table_map_apply(bgp->table_map[afi][safi].map,
1410 p, mpinfo_cp))
1411 continue;
b4cb15c6 1412
257b7b6e
DS
1413 /* metric/tag is only allowed to be
1414 * overridden on 1st nexthop */
1415 if (mpinfo == info) {
1416 metric = mpinfo_cp->attr->med;
1417 tag = mpinfo_cp->attr->tag;
b4cb15c6 1418 }
257b7b6e 1419 }
d62a17ae 1420
ad1844f7
IS
1421 BGP_ORIGINAL_UPDATE(bgp_orig, mpinfo, bgp);
1422
257b7b6e 1423 if (nh_family == AF_INET) {
77e62f2b 1424 nh_updated = update_ipv4nh_for_route_install(
ad1844f7
IS
1425 nh_othervrf, bgp_orig,
1426 &mpinfo_cp->attr->nexthop, mpinfo_cp->attr,
1427 is_evpn, api_nh);
2ad4f093 1428 } else {
f220da99 1429 ifindex_t ifindex = IFINDEX_INTERNAL;
2ad4f093 1430 struct in6_addr *nexthop;
9913029c 1431
18ee8310
DS
1432 nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp,
1433 &ifindex);
00c10664
DA
1434
1435 if (!nexthop)
1436 nh_updated = update_ipv4nh_for_route_install(
ad1844f7 1437 nh_othervrf, bgp_orig,
00c10664
DA
1438 &mpinfo_cp->attr->nexthop,
1439 mpinfo_cp->attr, is_evpn, api_nh);
1440 else
1441 nh_updated = update_ipv6nh_for_route_install(
ad1844f7
IS
1442 nh_othervrf, bgp_orig, nexthop, ifindex,
1443 mpinfo, info, is_evpn, api_nh);
2ad4f093 1444 }
d62a17ae 1445
77e62f2b 1446 /* Did we get proper nexthop info to update zebra? */
1447 if (!nh_updated)
1448 continue;
1449
1a9cb083
JM
1450 /* Allow recursion if it is a multipath group with both
1451 * eBGP and iBGP paths.
1452 */
1453 if (!allow_recursion
1454 && CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX)
1455 && (mpinfo->peer->sort == BGP_PEER_IBGP
1456 || mpinfo->peer->sort == BGP_PEER_CONFED))
1457 allow_recursion = true;
1458
a4d82a8a
PZ
1459 if (mpinfo->extra
1460 && bgp_is_valid_label(&mpinfo->extra->label[0])
90264d64 1461 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
b57ba6d2 1462 label = label_pton(&mpinfo->extra->label[0]);
9913029c 1463
68a02e06
MS
1464 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
1465
2ad4f093
RW
1466 api_nh->label_num = 1;
1467 api_nh->labels[0] = label;
d62a17ae 1468 }
a2299aba
AD
1469
1470 if (is_evpn
1471 && mpinfo->attr->evpn_overlay.type
1472 != OVERLAY_INDEX_GATEWAY_IP)
1473 memcpy(&api_nh->rmac, &(mpinfo->attr->rmac),
1474 sizeof(struct ethaddr));
1475
f7e1c681 1476 api_nh->weight = nh_weight;
4e30bc2b 1477
16f3db2d 1478 if (mpinfo->extra && !sid_zero(&mpinfo->extra->sid[0].sid)
53a4de82 1479 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
16f3db2d 1480 memcpy(&api_nh->seg6_segs, &mpinfo->extra->sid[0].sid,
53a4de82 1481 sizeof(api_nh->seg6_segs));
09e06fcf
RS
1482
1483 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
53a4de82
HS
1484 }
1485
2ad4f093
RW
1486 valid_nh_count++;
1487 }
d62a17ae 1488
6348981a
AK
1489 is_add = (valid_nh_count || nhg_id) ? true : false;
1490
ec0acb80 1491 if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) {
94effaf0 1492 struct bgp_zebra_opaque bzo = {};
1d7260a1
DA
1493 const char *reason =
1494 bgp_path_selection_reason2str(dest->reason);
94effaf0
DA
1495
1496 strlcpy(bzo.aspath, info->attr->aspath->str,
1497 sizeof(bzo.aspath));
1498
1499 if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
1500 strlcpy(bzo.community, info->attr->community->str,
1501 sizeof(bzo.community));
1502
1503 if (info->attr->flag
1504 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))
1bcf3a96
DA
1505 strlcpy(bzo.lcommunity,
1506 bgp_attr_get_lcommunity(info->attr)->str,
94effaf0 1507 sizeof(bzo.lcommunity));
e46723a5 1508
1d7260a1
DA
1509 strlcpy(bzo.selection_reason, reason,
1510 sizeof(bzo.selection_reason));
1511
e46723a5 1512 SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE);
94effaf0
DA
1513 api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque),
1514 ZAPI_MESSAGE_OPAQUE_LENGTH);
1515 memcpy(api.opaque.data, &bzo, api.opaque.length);
e46723a5
DS
1516 }
1517
1a9cb083
JM
1518 if (allow_recursion)
1519 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1520
5cc347c4
DS
1521 /*
1522 * When we create an aggregate route we must also
1523 * install a Null0 route in the RIB, so overwrite
1524 * what was written into api with a blackhole route
1525 */
1526 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1527 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1528 else
2ad4f093 1529 api.nexthop_num = valid_nh_count;
d62a17ae 1530
2ad4f093
RW
1531 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1532 api.metric = metric;
d62a17ae 1533
2ad4f093
RW
1534 if (tag) {
1535 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1536 api.tag = tag;
1537 }
d62a17ae 1538
2ad4f093
RW
1539 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1540 if (distance) {
1541 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1542 api.distance = distance;
1543 }
d62a17ae 1544
2ad4f093 1545 if (bgp_debug_zebra(p)) {
2ad4f093 1546 char nh_buf[INET6_ADDRSTRLEN];
d8744f72
CS
1547 char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'};
1548 char buf1[ETHER_ADDR_STRLEN];
2ad4f093 1549 char label_buf[20];
53a4de82
HS
1550 char sid_buf[20];
1551 char segs_buf[256];
2ad4f093
RW
1552 int i;
1553
2dbe669b
DA
1554 zlog_debug(
1555 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
6348981a 1556 " count %d nhg %d",
2dbe669b 1557 valid_nh_count ? "add" : "delete", bgp->vrf_id,
6348981a
AK
1558 &api.prefix, api.metric, api.tag, api.nexthop_num,
1559 nhg_id);
2ad4f093
RW
1560 for (i = 0; i < api.nexthop_num; i++) {
1561 api_nh = &api.nexthops[i];
1562
1ee069db
CS
1563 switch (api_nh->type) {
1564 case NEXTHOP_TYPE_IFINDEX:
77e62f2b 1565 nh_buf[0] = '\0';
1ee069db
CS
1566 break;
1567 case NEXTHOP_TYPE_IPV4:
1568 case NEXTHOP_TYPE_IPV4_IFINDEX:
1569 nh_family = AF_INET;
77e62f2b 1570 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1571 sizeof(nh_buf));
1ee069db
CS
1572 break;
1573 case NEXTHOP_TYPE_IPV6:
1574 case NEXTHOP_TYPE_IPV6_IFINDEX:
1575 nh_family = AF_INET6;
1576 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1577 sizeof(nh_buf));
1578 break;
1579 case NEXTHOP_TYPE_BLACKHOLE:
1580 strlcpy(nh_buf, "blackhole", sizeof(nh_buf));
1581 break;
1582 default:
1583 /* Note: add new nexthop case */
1584 assert(0);
1585 break;
77e62f2b 1586 }
2ad4f093
RW
1587
1588 label_buf[0] = '\0';
d8744f72 1589 eth_buf[0] = '\0';
bbf32574 1590 segs_buf[0] = '\0';
cd18af00 1591 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL)
a4d82a8a 1592 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
a7799788
CS
1593 snprintf(label_buf, sizeof(label_buf),
1594 "label %u", api_nh->labels[0]);
09e06fcf 1595 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6)
53a4de82
HS
1596 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1597 inet_ntop(AF_INET6, &api_nh->seg6_segs,
1598 sid_buf, sizeof(sid_buf));
1599 snprintf(segs_buf, sizeof(segs_buf), "segs %s",
1600 sid_buf);
1601 }
d8744f72
CS
1602 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)
1603 && !is_zero_mac(&api_nh->rmac))
a7799788
CS
1604 snprintf(eth_buf, sizeof(eth_buf), " RMAC %s",
1605 prefix_mac2str(&api_nh->rmac,
1606 buf1, sizeof(buf1)));
53a4de82 1607 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
77e62f2b 1608 i + 1, nh_buf, api_nh->ifindex,
4e30bc2b 1609 api_nh->vrf_id, api_nh->weight,
53a4de82 1610 label_buf, segs_buf, eth_buf);
d62a17ae 1611 }
2ad4f093 1612
960035b2
PZ
1613 int recursion_flag = 0;
1614
1615 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1616 recursion_flag = 1;
1617
70492dea
DS
1618 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1619 __func__, p, (recursion_flag ? "" : "NOT "));
960035b2 1620 }
6348981a 1621 zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
2ad4f093 1622 zclient, &api);
718e3744 1623}
1624
73ac8160 1625/* Announce all routes of a table to zebra */
d62a17ae 1626void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
73ac8160 1627{
9bcb3eef 1628 struct bgp_dest *dest;
d62a17ae 1629 struct bgp_table *table;
40381db7 1630 struct bgp_path_info *pi;
d62a17ae 1631
1632 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1633 * know of this instance.
1634 */
1635 if (!bgp_install_info_to_zebra(bgp))
1636 return;
1637
1638 table = bgp->rib[afi][safi];
1639 if (!table)
1640 return;
1641
9bcb3eef
DS
1642 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1643 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
40381db7 1644 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
ddb5b488 1645
40381db7
DS
1646 (pi->type == ZEBRA_ROUTE_BGP
1647 && (pi->sub_type == BGP_ROUTE_NORMAL
1648 || pi->sub_type == BGP_ROUTE_IMPORTED)))
ddb5b488 1649
9bcb3eef
DS
1650 bgp_zebra_announce(dest,
1651 bgp_dest_get_prefix(dest),
b54892e0 1652 pi, bgp, afi, safi);
73ac8160
DS
1653}
1654
c163f297
DS
1655/* Announce routes of any bgp subtype of a table to zebra */
1656void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi,
1657 safi_t safi)
1658{
1659 struct bgp_dest *dest;
1660 struct bgp_table *table;
1661 struct bgp_path_info *pi;
1662
1663 if (!bgp_install_info_to_zebra(bgp))
1664 return;
1665
1666 table = bgp->rib[afi][safi];
1667 if (!table)
1668 return;
1669
1670 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1671 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1672 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1673 pi->type == ZEBRA_ROUTE_BGP)
1674 bgp_zebra_announce(dest,
1675 bgp_dest_get_prefix(dest),
1676 pi, bgp, afi, safi);
1677}
1678
5a1ae2c2 1679void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info,
568e10ca 1680 struct bgp *bgp, safi_t safi)
718e3744 1681{
2ad4f093 1682 struct zapi_route api;
f7df1907 1683 struct peer *peer;
ddb5b488 1684
d62a17ae 1685 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1686 * know of this instance.
1687 */
568e10ca 1688 if (!bgp_install_info_to_zebra(bgp))
d62a17ae 1689 return;
1690
f7df1907
PG
1691 if (safi == SAFI_FLOWSPEC) {
1692 peer = info->peer;
d90b788e
A
1693 bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1694 return;
f7df1907 1695 }
529efa23 1696
2ad4f093 1697 memset(&api, 0, sizeof(api));
568e10ca 1698 api.vrf_id = bgp->vrf_id;
2ad4f093
RW
1699 api.type = ZEBRA_ROUTE_BGP;
1700 api.safi = safi;
1701 api.prefix = *p;
d62a17ae 1702
1276ce38
PG
1703 if (info->attr->rmap_table_id) {
1704 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1705 api.tableid = info->attr->rmap_table_id;
1706 }
1707
88493076 1708 /* If the route's source is EVPN, flag as such. */
1709 if (is_route_parent_evpn(info))
90264d64 1710 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
01740ff4 1711
2dbe669b
DA
1712 if (bgp_debug_zebra(p))
1713 zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id,
1714 &api.prefix);
d62a17ae 1715
2ad4f093 1716 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
718e3744 1717}
56c1f7d8 1718
c163f297
DS
1719/* Withdraw all entries in a BGP instances RIB table from Zebra */
1720void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi)
1721{
1722 struct bgp_dest *dest;
1723 struct bgp_table *table;
1724 struct bgp_path_info *pi;
1725
1726 if (!bgp_install_info_to_zebra(bgp))
1727 return;
1728
1729 table = bgp->rib[afi][safi];
1730 if (!table)
1731 return;
1732
1733 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
1734 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1735 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
1736 && (pi->type == ZEBRA_ROUTE_BGP))
1737 bgp_zebra_withdraw(bgp_dest_get_prefix(dest),
1738 pi, bgp, safi);
1739 }
1740 }
1741}
1742
d7c0a89a
QY
1743struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1744 unsigned short instance)
7c8ff89e 1745{
d62a17ae 1746 struct list *red_list;
1747 struct listnode *node;
1748 struct bgp_redist *red;
7c8ff89e 1749
d62a17ae 1750 red_list = bgp->redist[afi][type];
1751 if (!red_list)
1752 return (NULL);
7c8ff89e 1753
d62a17ae 1754 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1755 if (red->instance == instance)
1756 return red;
7c8ff89e 1757
d62a17ae 1758 return NULL;
7c8ff89e
DS
1759}
1760
d7c0a89a
QY
1761struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1762 unsigned short instance)
7c8ff89e 1763{
d62a17ae 1764 struct list *red_list;
1765 struct bgp_redist *red;
7c8ff89e 1766
d62a17ae 1767 red = bgp_redist_lookup(bgp, afi, type, instance);
1768 if (red)
1769 return red;
7c8ff89e 1770
d62a17ae 1771 if (!bgp->redist[afi][type])
1772 bgp->redist[afi][type] = list_new();
7c8ff89e 1773
d62a17ae 1774 red_list = bgp->redist[afi][type];
9f5dc319 1775 red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
d62a17ae 1776 red->instance = instance;
7c8ff89e 1777
d62a17ae 1778 listnode_add(red_list, red);
7c8ff89e 1779
d62a17ae 1780 return red;
7c8ff89e
DS
1781}
1782
d7c0a89a
QY
1783static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1784 unsigned short instance)
7c8ff89e 1785{
d62a17ae 1786 struct bgp_redist *red;
1787
1788 red = bgp_redist_lookup(bgp, afi, type, instance);
1789
1790 if (red) {
1791 listnode_delete(bgp->redist[afi][type], red);
1792 XFREE(MTYPE_BGP_REDIST, red);
acdf5e25 1793 if (!bgp->redist[afi][type]->count)
6a154c88 1794 list_delete(&bgp->redist[afi][type]);
d62a17ae 1795 }
7c8ff89e 1796}
6b0655a2 1797
718e3744 1798/* Other routes redistribution into BGP. */
d7c0a89a 1799int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
e923dd62 1800 unsigned short instance, bool changed)
718e3744 1801{
e923dd62 1802 /* If redistribute options are changed call
1803 * bgp_redistribute_unreg() to reset the option and withdraw
1804 * the routes
1805 */
1806 if (changed)
1807 bgp_redistribute_unreg(bgp, afi, type, instance);
718e3744 1808
d62a17ae 1809 /* Return if already redistribute flag is set. */
1810 if (instance) {
1811 if (redist_check_instance(&zclient->mi_redist[afi][type],
1812 instance))
1813 return CMD_WARNING;
718e3744 1814
d62a17ae 1815 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1816 } else {
d9083050
IR
1817 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1818 return CMD_WARNING;
1819
49e5a4a0 1820#ifdef ENABLE_BGP_VNC
f920dd6d 1821 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
d62a17ae 1822 vnc_export_bgp_enable(
1823 bgp, afi); /* only enables if mode bits cfg'd */
1824 }
65efcfce
LB
1825#endif
1826
d62a17ae 1827 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1828 }
718e3744 1829
ea12cf11
DS
1830 /*
1831 * Don't try to register if we're not connected to Zebra or Zebra
1832 * doesn't know of this instance.
1833 *
1834 * When we come up later well resend if needed.
d62a17ae 1835 */
1836 if (!bgp_install_info_to_zebra(bgp))
ea12cf11 1837 return CMD_SUCCESS;
a39275d7 1838
d62a17ae 1839 if (BGP_DEBUG(zebra, ZEBRA))
1840 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1841 bgp->vrf_id, afi, zebra_route_string(type),
1842 instance);
518f0eb1 1843
d62a17ae 1844 /* Send distribute add message to zebra. */
1845 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1846 instance, bgp->vrf_id);
718e3744 1847
d62a17ae 1848 return CMD_SUCCESS;
718e3744 1849}
1850
d62a17ae 1851int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1852 unsigned short instance)
518f0eb1 1853{
d62a17ae 1854 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1855 * know of this instance.
1856 */
1857 if (!bgp_install_info_to_zebra(bgp))
1858 return -1;
1859
1860 if (BGP_DEBUG(zebra, ZEBRA))
1861 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1862 bgp->vrf_id, afi, zebra_route_string(type),
1863 instance);
1864
1865 /* Send distribute add message to zebra. */
1866 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1867 instance, bgp->vrf_id);
1868 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1869 instance, bgp->vrf_id);
1870
1871 return 0;
518f0eb1
DS
1872}
1873
718e3744 1874/* Redistribute with route-map specification. */
3dc339cd
DA
1875bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1876 struct route_map *route_map)
718e3744 1877{
d62a17ae 1878 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
3dc339cd 1879 return false;
718e3744 1880
0a22ddfb 1881 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
b4897fa5 1882 /* Decrement the count for existing routemap and
1883 * increment the count for new route map.
1884 */
1885 route_map_counter_decrement(red->rmap.map);
d62a17ae 1886 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1de27621 1887 red->rmap.map = route_map;
b4897fa5 1888 route_map_counter_increment(red->rmap.map);
718e3744 1889
3dc339cd 1890 return true;
718e3744 1891}
1892
1893/* Redistribute with metric specification. */
3dc339cd
DA
1894bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1895 afi_t afi, int type, uint32_t metric)
718e3744 1896{
9bcb3eef 1897 struct bgp_dest *dest;
40381db7 1898 struct bgp_path_info *pi;
d62a17ae 1899
1900 if (red->redist_metric_flag && red->redist_metric == metric)
3dc339cd 1901 return false;
d62a17ae 1902
1903 red->redist_metric_flag = 1;
1904 red->redist_metric = metric;
1905
9bcb3eef
DS
1906 for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
1907 dest = bgp_route_next(dest)) {
1908 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
40381db7
DS
1909 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1910 && pi->type == type
1911 && pi->instance == red->instance) {
d62a17ae 1912 struct attr *old_attr;
1913 struct attr new_attr;
1914
6f4f49b2 1915 new_attr = *pi->attr;
d62a17ae 1916 new_attr.med = red->redist_metric;
40381db7
DS
1917 old_attr = pi->attr;
1918 pi->attr = bgp_attr_intern(&new_attr);
d62a17ae 1919 bgp_attr_unintern(&old_attr);
1920
9bcb3eef 1921 bgp_path_info_set_flag(dest, pi,
18ee8310 1922 BGP_PATH_ATTR_CHANGED);
9bcb3eef 1923 bgp_process(bgp, dest, afi, SAFI_UNICAST);
d62a17ae 1924 }
1925 }
1926 }
1927
3dc339cd 1928 return true;
718e3744 1929}
1930
1931/* Unset redistribution. */
d62a17ae 1932int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1933 unsigned short instance)
718e3744 1934{
d62a17ae 1935 struct bgp_redist *red;
1936
1937 red = bgp_redist_lookup(bgp, afi, type, instance);
1938 if (!red)
1939 return CMD_SUCCESS;
1940
1941 /* Return if zebra connection is disabled. */
1942 if (instance) {
1943 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1944 instance))
1945 return CMD_WARNING;
1946 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1947 } else {
1948 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1949 return CMD_WARNING;
1950 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1951 }
718e3744 1952
d62a17ae 1953 if (bgp_install_info_to_zebra(bgp)) {
1954 /* Send distribute delete message to zebra. */
1955 if (BGP_DEBUG(zebra, ZEBRA))
1956 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1957 bgp->vrf_id, afi, zebra_route_string(type),
1958 instance);
1959 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1960 type, instance, bgp->vrf_id);
1961 }
1962
1963 /* Withdraw redistributed routes from current BGP's routing table. */
1964 bgp_redistribute_withdraw(bgp, afi, type, instance);
1965
1966 return CMD_SUCCESS;
718e3744 1967}
1968
6aeb9e78 1969/* Unset redistribution. */
d62a17ae 1970int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1971 unsigned short instance)
6aeb9e78 1972{
d62a17ae 1973 struct bgp_redist *red;
6aeb9e78 1974
ddb5b488
PZ
1975/*
1976 * vnc and vpn->vrf checks must be before red check because
1977 * they operate within bgpd irrespective of zebra connection
1978 * status. red lookup fails if there is no zebra connection.
1979 */
49e5a4a0 1980#ifdef ENABLE_BGP_VNC
f920dd6d 1981 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
ddb5b488
PZ
1982 vnc_export_bgp_disable(bgp, afi);
1983 }
1984#endif
ddb5b488 1985
d62a17ae 1986 red = bgp_redist_lookup(bgp, afi, type, instance);
1987 if (!red)
1988 return CMD_SUCCESS;
6aeb9e78 1989
d62a17ae 1990 bgp_redistribute_unreg(bgp, afi, type, instance);
6aeb9e78 1991
d62a17ae 1992 /* Unset route-map. */
0a22ddfb 1993 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
b4897fa5 1994 route_map_counter_decrement(red->rmap.map);
d62a17ae 1995 red->rmap.map = NULL;
6aeb9e78 1996
d62a17ae 1997 /* Unset metric. */
1998 red->redist_metric_flag = 0;
1999 red->redist_metric = 0;
6aeb9e78 2000
d62a17ae 2001 bgp_redist_del(bgp, afi, type, instance);
6aeb9e78 2002
d62a17ae 2003 return CMD_SUCCESS;
6aeb9e78
DS
2004}
2005
fc2408ec
DS
2006void bgp_redistribute_redo(struct bgp *bgp)
2007{
2008 afi_t afi;
2009 int i;
2010 struct list *red_list;
2011 struct listnode *node;
2012 struct bgp_redist *red;
2013
2014 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2015 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
2016
2017 red_list = bgp->redist[afi][i];
2018 if (!red_list)
2019 continue;
2020
2021 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
2022 bgp_redistribute_resend(bgp, afi, i,
2023 red->instance);
2024 }
2025 }
2026 }
2027}
2028
d62a17ae 2029void bgp_zclient_reset(void)
718e3744 2030{
d62a17ae 2031 zclient_reset(zclient);
718e3744 2032}
2033
ad4cbda1 2034/* Register this instance with Zebra. Invoked upon connect (for
2035 * default instance) and when other VRFs are learnt (or created and
2036 * already learnt).
2037 */
d62a17ae 2038void bgp_zebra_instance_register(struct bgp *bgp)
ad4cbda1 2039{
d62a17ae 2040 /* Don't try to register if we're not connected to Zebra */
2041 if (!zclient || zclient->sock < 0)
2042 return;
ad4cbda1 2043
d62a17ae 2044 if (BGP_DEBUG(zebra, ZEBRA))
2045 zlog_debug("Registering VRF %u", bgp->vrf_id);
ad4cbda1 2046
d62a17ae 2047 /* Register for router-id, interfaces, redistributed routes. */
2048 zclient_send_reg_requests(zclient, bgp->vrf_id);
7724c0a1 2049
e2f3a930
T
2050 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2051 if (bgp->advertise_all_vni)
d62a17ae 2052 bgp_zebra_advertise_all_vni(bgp, 1);
1ee0a2df
DS
2053
2054 bgp_nht_register_nexthops(bgp);
ad4cbda1 2055}
2056
2057/* Deregister this instance with Zebra. Invoked upon the instance
2058 * being deleted (default or VRF) and it is already registered.
2059 */
d62a17ae 2060void bgp_zebra_instance_deregister(struct bgp *bgp)
ad4cbda1 2061{
d62a17ae 2062 /* Don't try to deregister if we're not connected to Zebra */
2063 if (zclient->sock < 0)
2064 return;
ad4cbda1 2065
d62a17ae 2066 if (BGP_DEBUG(zebra, ZEBRA))
2067 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
ad4cbda1 2068
e2f3a930
T
2069 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2070 if (bgp->advertise_all_vni)
d62a17ae 2071 bgp_zebra_advertise_all_vni(bgp, 0);
7724c0a1 2072
d62a17ae 2073 /* Deregister for router-id, interfaces, redistributed routes. */
2074 zclient_send_dereg_requests(zclient, bgp->vrf_id);
ad4cbda1 2075}
2076
d62a17ae 2077void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 2078{
bbad0276 2079 uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
5c81b96a 2080
d62a17ae 2081 /* Don't try to initiate if we're not connected to Zebra */
2082 if (zclient->sock < 0)
2083 return;
4a04e5f7 2084
d62a17ae 2085 if (BGP_DEBUG(zebra, ZEBRA))
2086 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2087 peer->host);
4a04e5f7 2088
b3a3290e
DS
2089 /*
2090 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2091 * If we don't have an ifp pointer, call function to find the
2092 * ifps for a numbered enhe peer to turn RAs on.
2093 */
2094 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2095 peer->ifp, 1, ra_interval)
2096 : bgp_nht_reg_enhe_cap_intfs(peer);
4a04e5f7 2097}
2098
d62a17ae 2099void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 2100{
d62a17ae 2101 /* Don't try to terminate if we're not connected to Zebra */
2102 if (zclient->sock < 0)
2103 return;
4a04e5f7 2104
d62a17ae 2105 if (BGP_DEBUG(zebra, ZEBRA))
2106 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2107 peer->host);
4a04e5f7 2108
b3a3290e
DS
2109 /*
2110 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2111 * If we don't have an ifp pointer, call function to find the
2112 * ifps for a numbered enhe peer to turn RAs off.
2113 */
2114 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2115 peer->ifp, 0, 0)
2116 : bgp_nht_dereg_enhe_cap_intfs(peer);
4a04e5f7 2117}
2118
31310b25
MK
2119int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
2120{
2121 struct stream *s = NULL;
2122
2123 /* Check socket. */
2124 if (!zclient || zclient->sock < 0)
2125 return 0;
2126
2127 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
2128 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2129 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
2130 zlog_debug(
2131 "%s: No zebra instance to talk to, cannot advertise subnet",
2132 __func__);
31310b25 2133 return 0;
bb4ef1ae 2134 }
31310b25
MK
2135
2136 s = zclient->obuf;
2137 stream_reset(s);
2138
2139 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
2140 stream_putc(s, advertise);
2141 stream_put3(s, vni);
2142 stream_putw_at(s, 0, stream_get_endp(s));
2143
2144 return zclient_send_message(zclient);
2145}
2146
a8016157
CS
2147int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni)
2148{
2149 struct stream *s = NULL;
2150
2151 /* Check socket. */
2152 if (!zclient || zclient->sock < 0)
2153 return 0;
2154
2155 /* Don't try to register if Zebra doesn't know of this instance. */
2156 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2157 return 0;
2158
2159 s = zclient->obuf;
2160 stream_reset(s);
2161
2162 zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id);
2163 stream_putc(s, advertise);
2164 stream_putl(s, vni);
2165 stream_putw_at(s, 0, stream_get_endp(s));
2166
2167 return zclient_send_message(zclient);
2168}
2169
1a98c087
MK
2170int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
2171{
2172 struct stream *s = NULL;
2173
2174 /* Check socket. */
2175 if (!zclient || zclient->sock < 0)
2176 return 0;
2177
2178 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
2179 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2180 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
2181 zlog_debug(
2182 "%s: No zebra instance to talk to, not installing gw_macip",
2183 __func__);
1a98c087 2184 return 0;
bb4ef1ae 2185 }
1a98c087
MK
2186
2187 s = zclient->obuf;
2188 stream_reset(s);
2189
2190 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
2191 stream_putc(s, advertise);
cc6d5476 2192 stream_putl(s, vni);
1a98c087
MK
2193 stream_putw_at(s, 0, stream_get_endp(s));
2194
2195 return zclient_send_message(zclient);
2196}
2197
fd069644
DS
2198int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
2199 enum vxlan_flood_control flood_ctrl)
2200{
2201 struct stream *s;
2202
2203 /* Check socket. */
2204 if (!zclient || zclient->sock < 0)
2205 return 0;
2206
2207 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
2208 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2209 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
2210 zlog_debug(
2211 "%s: No zebra instance to talk to, not installing all vni",
2212 __func__);
fd069644 2213 return 0;
bb4ef1ae 2214 }
fd069644
DS
2215
2216 s = zclient->obuf;
2217 stream_reset(s);
2218
2219 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
2220 stream_putc(s, flood_ctrl);
2221 stream_putw_at(s, 0, stream_get_endp(s));
2222
2223 return zclient_send_message(zclient);
2224}
2225
d62a17ae 2226int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
7724c0a1 2227{
d62a17ae 2228 struct stream *s;
7724c0a1 2229
d62a17ae 2230 /* Check socket. */
2231 if (!zclient || zclient->sock < 0)
2232 return 0;
7724c0a1 2233
d62a17ae 2234 /* Don't try to register if Zebra doesn't know of this instance. */
2235 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2236 return 0;
7724c0a1 2237
d62a17ae 2238 s = zclient->obuf;
2239 stream_reset(s);
7724c0a1 2240
d62a17ae 2241 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2242 stream_putc(s, advertise);
fbac9605
DS
2243 /* Also inform current BUM handling setting. This is really
2244 * relevant only when 'advertise' is set.
2245 */
fd069644 2246 stream_putc(s, bgp->vxlan_flood_ctrl);
d62a17ae 2247 stream_putw_at(s, 0, stream_get_endp(s));
7724c0a1 2248
d62a17ae 2249 return zclient_send_message(zclient);
7724c0a1 2250}
2251
0b9d9cd0
CS
2252int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2253{
2254 struct stream *s;
2255
2256 /* Check socket. */
2257 if (!zclient || zclient->sock < 0)
2258 return 0;
2259
2260 /* Don't try to register if Zebra doesn't know of this instance. */
2261 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2262 return 0;
2263
2264 if (BGP_DEBUG(zebra, ZEBRA))
2265 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2266 bgp->evpn_info->dup_addr_detect ?
2267 "enable" : "disable",
2268 bgp->evpn_info->dad_max_moves,
2269 bgp->evpn_info->dad_time,
2270 bgp->evpn_info->dad_freeze ?
2271 "enable" : "disable",
2272 bgp->evpn_info->dad_freeze_time);
2273
2274 s = zclient->obuf;
2275 stream_reset(s);
2276 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2277 bgp->vrf_id);
2278 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2279 stream_putl(s, bgp->evpn_info->dad_time);
2280 stream_putl(s, bgp->evpn_info->dad_max_moves);
2281 stream_putl(s, bgp->evpn_info->dad_freeze);
2282 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2283 stream_putw_at(s, 0, stream_get_endp(s));
2284
2285 return zclient_send_message(zclient);
2286}
2287
121f9dee 2288static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
30d50e6d
PG
2289{
2290 uint32_t seqno, priority, unique;
2291 enum zapi_rule_notify_owner note;
2292 struct bgp_pbr_action *bgp_pbra;
ffee150e 2293 struct bgp_pbr_rule *bgp_pbr = NULL;
58a1d249 2294 char ifname[INTERFACE_NAMSIZ + 1];
30d50e6d
PG
2295
2296 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
58a1d249 2297 ifname, &note))
30d50e6d
PG
2298 return -1;
2299
70eabd12 2300 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
30d50e6d 2301 if (!bgp_pbra) {
ffee150e
PG
2302 /* look in bgp pbr rule */
2303 bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2304 if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2305 if (BGP_DEBUG(zebra, ZEBRA))
2306 zlog_debug("%s: Fail to look BGP rule (%u)",
15569c58 2307 __func__, unique);
ffee150e
PG
2308 return 0;
2309 }
30d50e6d
PG
2310 }
2311
2312 switch (note) {
2313 case ZAPI_RULE_FAIL_INSTALL:
2314 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2315 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__);
ffee150e
PG
2316 if (bgp_pbra) {
2317 bgp_pbra->installed = false;
2318 bgp_pbra->install_in_progress = false;
2319 } else {
2320 bgp_pbr->installed = false;
2321 bgp_pbr->install_in_progress = false;
2322 }
30d50e6d
PG
2323 break;
2324 case ZAPI_RULE_INSTALLED:
ffee150e
PG
2325 if (bgp_pbra) {
2326 bgp_pbra->installed = true;
2327 bgp_pbra->install_in_progress = false;
2328 } else {
ce3c0614
PG
2329 struct bgp_path_info *path;
2330 struct bgp_path_info_extra *extra;
2331
ffee150e
PG
2332 bgp_pbr->installed = true;
2333 bgp_pbr->install_in_progress = false;
2334 bgp_pbr->action->refcnt++;
ce3c0614
PG
2335 /* link bgp_info to bgp_pbr */
2336 path = (struct bgp_path_info *)bgp_pbr->path;
2337 extra = bgp_path_info_extra_get(path);
f5925234
PG
2338 listnode_add_force(&extra->bgp_fs_iprule,
2339 bgp_pbr);
ffee150e 2340 }
30d50e6d 2341 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2342 zlog_debug("%s: Received RULE_INSTALLED", __func__);
30d50e6d 2343 break;
f18a08f5 2344 case ZAPI_RULE_FAIL_REMOVE:
30d50e6d
PG
2345 case ZAPI_RULE_REMOVED:
2346 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2347 zlog_debug("%s: Received RULE REMOVED", __func__);
30d50e6d
PG
2348 break;
2349 }
2350
2351 return 0;
2352}
2353
121f9dee 2354static int ipset_notify_owner(ZAPI_CALLBACK_ARGS)
30d50e6d
PG
2355{
2356 uint32_t unique;
2357 enum zapi_ipset_notify_owner note;
2358 struct bgp_pbr_match *bgp_pbim;
2359
2360 if (!zapi_ipset_notify_decode(zclient->ibuf,
2361 &unique,
2362 &note))
2363 return -1;
2364
2365 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2366 if (!bgp_pbim) {
2367 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2368 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
15569c58 2369 __func__, note, unique);
30d50e6d
PG
2370 return 0;
2371 }
2372
2373 switch (note) {
2374 case ZAPI_IPSET_FAIL_INSTALL:
2375 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2376 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__);
30d50e6d
PG
2377 bgp_pbim->installed = false;
2378 bgp_pbim->install_in_progress = false;
2379 break;
2380 case ZAPI_IPSET_INSTALLED:
2381 bgp_pbim->installed = true;
2382 bgp_pbim->install_in_progress = false;
2383 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2384 zlog_debug("%s: Received IPSET_INSTALLED", __func__);
30d50e6d 2385 break;
f18a08f5 2386 case ZAPI_IPSET_FAIL_REMOVE:
30d50e6d
PG
2387 case ZAPI_IPSET_REMOVED:
2388 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2389 zlog_debug("%s: Received IPSET REMOVED", __func__);
30d50e6d
PG
2390 break;
2391 }
2392
2393 return 0;
2394}
2395
121f9dee 2396static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS)
30d50e6d
PG
2397{
2398 uint32_t unique;
2399 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2400 enum zapi_ipset_entry_notify_owner note;
2401 struct bgp_pbr_match_entry *bgp_pbime;
2402
2403 if (!zapi_ipset_entry_notify_decode(
2404 zclient->ibuf,
2405 &unique,
2406 ipset_name,
2407 &note))
2408 return -1;
2409 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2410 ipset_name,
2411 unique);
2412 if (!bgp_pbime) {
2413 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
2414 zlog_debug(
2415 "%s: Fail to look BGP match entry (%u, ID %u)",
2416 __func__, note, unique);
30d50e6d
PG
2417 return 0;
2418 }
2419
2420 switch (note) {
2421 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2422 if (BGP_DEBUG(zebra, ZEBRA))
2423 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
15569c58 2424 __func__);
30d50e6d
PG
2425 bgp_pbime->installed = false;
2426 bgp_pbime->install_in_progress = false;
2427 break;
2428 case ZAPI_IPSET_ENTRY_INSTALLED:
b588b642 2429 {
9b6d8fcf 2430 struct bgp_path_info *path;
4b7e6066
DS
2431 struct bgp_path_info_extra *extra;
2432
2433 bgp_pbime->installed = true;
2434 bgp_pbime->install_in_progress = false;
2435 if (BGP_DEBUG(zebra, ZEBRA))
2436 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
15569c58 2437 __func__);
9b6d8fcf
DS
2438 /* link bgp_path_info to bpme */
2439 path = (struct bgp_path_info *)bgp_pbime->path;
2440 extra = bgp_path_info_extra_get(path);
f5925234 2441 listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime);
b588b642 2442 }
30d50e6d 2443 break;
f18a08f5 2444 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
30d50e6d
PG
2445 case ZAPI_IPSET_ENTRY_REMOVED:
2446 if (BGP_DEBUG(zebra, ZEBRA))
2447 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
15569c58 2448 __func__);
30d50e6d
PG
2449 break;
2450 }
2451 return 0;
2452}
2453
121f9dee 2454static int iptable_notify_owner(ZAPI_CALLBACK_ARGS)
c16a0a62
PG
2455{
2456 uint32_t unique;
2457 enum zapi_iptable_notify_owner note;
2458 struct bgp_pbr_match *bgpm;
2459
2460 if (!zapi_iptable_notify_decode(
2461 zclient->ibuf,
2462 &unique,
2463 &note))
2464 return -1;
2465 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2466 if (!bgpm) {
2467 if (BGP_DEBUG(zebra, ZEBRA))
82e194ed 2468 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
15569c58 2469 __func__, note, unique);
c16a0a62
PG
2470 return 0;
2471 }
2472 switch (note) {
2473 case ZAPI_IPTABLE_FAIL_INSTALL:
2474 if (BGP_DEBUG(zebra, ZEBRA))
2475 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
15569c58 2476 __func__);
c16a0a62
PG
2477 bgpm->installed_in_iptable = false;
2478 bgpm->install_iptable_in_progress = false;
2479 break;
2480 case ZAPI_IPTABLE_INSTALLED:
2481 bgpm->installed_in_iptable = true;
2482 bgpm->install_iptable_in_progress = false;
2483 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2484 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__);
a6b07429 2485 bgpm->action->refcnt++;
c16a0a62 2486 break;
f18a08f5 2487 case ZAPI_IPTABLE_FAIL_REMOVE:
c16a0a62
PG
2488 case ZAPI_IPTABLE_REMOVED:
2489 if (BGP_DEBUG(zebra, ZEBRA))
15569c58 2490 zlog_debug("%s: Received IPTABLE REMOVED", __func__);
c16a0a62
PG
2491 break;
2492 }
2493 return 0;
2494}
2495
a77e2f4b
S
2496/* Process route notification messages from RIB */
2497static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient,
2498 zebra_size_t length, vrf_id_t vrf_id)
2499{
2500 struct prefix p;
2501 enum zapi_route_notify_owner note;
2502 uint32_t table_id;
a77e2f4b
S
2503 afi_t afi;
2504 safi_t safi;
2505 struct bgp_dest *dest;
2506 struct bgp *bgp;
2507 struct bgp_path_info *pi, *new_select;
2508
2509 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
2510 &afi, &safi)) {
cc42c4f0 2511 zlog_err("%s : error in msg decode", __func__);
a77e2f4b
S
2512 return -1;
2513 }
2514
2515 /* Get the bgp instance */
2516 bgp = bgp_lookup_by_vrf_id(vrf_id);
2517 if (!bgp) {
2518 flog_err(EC_BGP_INVALID_BGP_INSTANCE,
cc42c4f0
DS
2519 "%s : bgp instance not found vrf %d", __func__,
2520 vrf_id);
a77e2f4b
S
2521 return -1;
2522 }
2523
a77e2f4b
S
2524 /* Find the bgp route node */
2525 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p,
2526 &bgp->vrf_prd);
2527 if (!dest)
2528 return -1;
2529
a77e2f4b
S
2530 switch (note) {
2531 case ZAPI_ROUTE_INSTALLED:
2532 new_select = NULL;
2533 /* Clear the flags so that route can be processed */
be785e35
DS
2534 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2535 SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2536 if (BGP_DEBUG(zebra, ZEBRA))
2537 zlog_debug("route %pRN : INSTALLED", dest);
2538 /* Find the best route */
2539 for (pi = dest->info; pi; pi = pi->next) {
2540 /* Process aggregate route */
2541 bgp_aggregate_increment(bgp, &p, pi, afi, safi);
2542 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2543 new_select = pi;
2544 }
2545 /* Advertise the route */
2546 if (new_select)
2547 group_announce_route(bgp, afi, safi, dest, new_select);
2548 else {
2549 flog_err(EC_BGP_INVALID_ROUTE,
2550 "selected route %pRN not found", dest);
2551
2552 bgp_dest_unlock_node(dest);
2553 return -1;
a77e2f4b
S
2554 }
2555 break;
2556 case ZAPI_ROUTE_REMOVED:
2557 /* Route deleted from dataplane, reset the installed flag
2558 * so that route can be reinstalled when client sends
2559 * route add later
2560 */
2561 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
1a3519b6
DS
2562 if (BGP_DEBUG(zebra, ZEBRA))
2563 zlog_debug("route %pRN: Removed from Fib", dest);
a77e2f4b
S
2564 break;
2565 case ZAPI_ROUTE_FAIL_INSTALL:
be785e35 2566 new_select = NULL;
1a3519b6
DS
2567 if (BGP_DEBUG(zebra, ZEBRA))
2568 zlog_debug("route: %pRN Failed to Install into Fib",
2569 dest);
be785e35
DS
2570 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2571 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2572 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2573 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2574 new_select = pi;
2575 }
2576 if (new_select)
2577 group_announce_route(bgp, afi, safi, dest, new_select);
a77e2f4b
S
2578 /* Error will be logged by zebra module */
2579 break;
2580 case ZAPI_ROUTE_BETTER_ADMIN_WON:
1a3519b6
DS
2581 if (BGP_DEBUG(zebra, ZEBRA))
2582 zlog_debug("route: %pRN removed due to better admin won",
2583 dest);
be785e35
DS
2584 new_select = NULL;
2585 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2586 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2587 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2588 bgp_aggregate_decrement(bgp, &p, pi, afi, safi);
2589 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2590 new_select = pi;
2591 }
2592 if (new_select)
2593 group_announce_route(bgp, afi, safi, dest, new_select);
a77e2f4b
S
2594 /* No action required */
2595 break;
2596 case ZAPI_ROUTE_REMOVE_FAIL:
1a3519b6
DS
2597 zlog_warn("%s: Route %pRN failure to remove",
2598 __func__, dest);
a77e2f4b
S
2599 break;
2600 }
e71ad4b6
DA
2601
2602 bgp_dest_unlock_node(dest);
a77e2f4b
S
2603 return 0;
2604}
2605
6cfe5d15
PG
2606/* this function is used to forge ip rule,
2607 * - either for iptable/ipset using fwmark id
121f9dee 2608 * - or for sample ip rule cmd
6cfe5d15 2609 */
30d50e6d 2610static void bgp_encode_pbr_rule_action(struct stream *s,
6cfe5d15
PG
2611 struct bgp_pbr_action *pbra,
2612 struct bgp_pbr_rule *pbr)
30d50e6d 2613{
6cfe5d15 2614 struct prefix pfx;
f01e580f 2615 uint8_t fam = AF_INET;
e95666cb 2616 char ifname[INTERFACE_NAMSIZ];
30d50e6d 2617
9f1f03ec 2618 if (pbra->nh.type == NEXTHOP_TYPE_IPV6)
f01e580f 2619 fam = AF_INET6;
30d50e6d 2620 stream_putl(s, 0); /* seqno unused */
8112a7a0
PG
2621 if (pbr)
2622 stream_putl(s, pbr->priority);
2623 else
2624 stream_putl(s, 0);
2625 /* ruleno unused - priority change
2626 * ruleno permits distinguishing various FS PBR entries
2627 * - FS PBR entries based on ipset/iptables
2628 * - FS PBR entries based on iprule
2629 * the latter may contain default routing information injected by FS
2630 */
6cfe5d15
PG
2631 if (pbr)
2632 stream_putl(s, pbr->unique);
2633 else
2634 stream_putl(s, pbra->unique);
f56697ef 2635 stream_putc(s, 0); /* ip protocol being used */
6cfe5d15
PG
2636 if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2637 memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2638 else {
2639 memset(&pfx, 0, sizeof(pfx));
f01e580f 2640 pfx.family = fam;
6cfe5d15
PG
2641 }
2642 stream_putc(s, pfx.family);
2643 stream_putc(s, pfx.prefixlen);
2644 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
30d50e6d
PG
2645
2646 stream_putw(s, 0); /* src port */
2647
6cfe5d15
PG
2648 if (pbr && pbr->flags & MATCH_IP_DST_SET)
2649 memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2650 else {
2651 memset(&pfx, 0, sizeof(pfx));
f01e580f 2652 pfx.family = fam;
6cfe5d15
PG
2653 }
2654 stream_putc(s, pfx.family);
2655 stream_putc(s, pfx.prefixlen);
2656 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
30d50e6d
PG
2657
2658 stream_putw(s, 0); /* dst port */
e95666cb 2659 stream_putc(s, 0); /* dsfield */
6cfe5d15
PG
2660 /* if pbr present, fwmark is not used */
2661 if (pbr)
2662 stream_putl(s, 0);
2663 else
2664 stream_putl(s, pbra->fwmark); /* fwmark */
30d50e6d
PG
2665
2666 stream_putl(s, pbra->table_id);
2667
e95666cb
DS
2668 memset(ifname, 0, sizeof(ifname));
2669 stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */
30d50e6d
PG
2670}
2671
2672static void bgp_encode_pbr_ipset_match(struct stream *s,
2673 struct bgp_pbr_match *pbim)
2674{
2675 stream_putl(s, pbim->unique);
2676 stream_putl(s, pbim->type);
a60b7031 2677 stream_putc(s, pbim->family);
30d50e6d
PG
2678 stream_put(s, pbim->ipset_name,
2679 ZEBRA_IPSET_NAME_SIZE);
30d50e6d
PG
2680}
2681
2682static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2683 struct bgp_pbr_match_entry *pbime)
2684{
2685 stream_putl(s, pbime->unique);
2686 /* check that back pointer is not null */
2687 stream_put(s, pbime->backpointer->ipset_name,
2688 ZEBRA_IPSET_NAME_SIZE);
2689
2690 stream_putc(s, pbime->src.family);
2691 stream_putc(s, pbime->src.prefixlen);
2692 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2693
2694 stream_putc(s, pbime->dst.family);
2695 stream_putc(s, pbime->dst.prefixlen);
2696 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
f730e566
PG
2697
2698 stream_putw(s, pbime->src_port_min);
2699 stream_putw(s, pbime->src_port_max);
2700 stream_putw(s, pbime->dst_port_min);
2701 stream_putw(s, pbime->dst_port_max);
2702 stream_putc(s, pbime->proto);
30d50e6d
PG
2703}
2704
c16a0a62
PG
2705static void bgp_encode_pbr_iptable_match(struct stream *s,
2706 struct bgp_pbr_action *bpa,
2707 struct bgp_pbr_match *pbm)
2708{
2709 stream_putl(s, pbm->unique2);
2710
2711 stream_putl(s, pbm->type);
2712
2713 stream_putl(s, pbm->flags);
2714
2715 /* TODO: correlate with what is contained
2716 * into bgp_pbr_action.
2717 * currently only forward supported
2718 */
2719 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2720 stream_putl(s, ZEBRA_IPTABLES_DROP);
2721 else
2722 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2723 stream_putl(s, bpa->fwmark);
2724 stream_put(s, pbm->ipset_name,
2725 ZEBRA_IPSET_NAME_SIZE);
a60b7031 2726 stream_putc(s, pbm->family);
83360720
PG
2727 stream_putw(s, pbm->pkt_len_min);
2728 stream_putw(s, pbm->pkt_len_max);
2da7d62e
PG
2729 stream_putw(s, pbm->tcp_flags);
2730 stream_putw(s, pbm->tcp_mask_flags);
4977bd6c 2731 stream_putc(s, pbm->dscp_value);
6f5617d8 2732 stream_putc(s, pbm->fragment);
f449d223 2733 stream_putc(s, pbm->protocol);
a60b7031 2734 stream_putw(s, pbm->flow_label);
c16a0a62
PG
2735}
2736
ad4cbda1 2737/* BGP has established connection with Zebra. */
d62a17ae 2738static void bgp_zebra_connected(struct zclient *zclient)
7076bb2f 2739{
d62a17ae 2740 struct bgp *bgp;
7076bb2f 2741
d62a17ae 2742 zclient_num_connects++; /* increment even if not responding */
afbb1c59 2743
6bfcd0f1
IR
2744 /* Send the client registration */
2745 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2746
d62a17ae 2747 /* At this point, we may or may not have BGP instances configured, but
2748 * we're only interested in the default VRF (others wouldn't have learnt
2749 * the VRF from Zebra yet.)
2750 */
2751 bgp = bgp_get_default();
2752 if (!bgp)
2753 return;
ad4cbda1 2754
d62a17ae 2755 bgp_zebra_instance_register(bgp);
ad4cbda1 2756
955bfd98 2757 /* tell label pool that zebra is connected */
e70e9f8e 2758 bgp_lp_event_zebra_up();
955bfd98 2759
d62a17ae 2760 /* TODO - What if we have peers and networks configured, do we have to
2761 * kick-start them?
2762 */
85ef4179 2763 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
7076bb2f
FL
2764}
2765
c44ab6f1
AK
2766static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS)
2767{
2768 esi_t esi;
2769 struct bgp *bgp = NULL;
2770 struct stream *s = NULL;
2771 char buf[ESI_STR_LEN];
2772 struct in_addr originator_ip;
2773 uint8_t active;
74be8313 2774 uint8_t bypass;
74e2bd89 2775 uint16_t df_pref;
c44ab6f1
AK
2776
2777 bgp = bgp_lookup_by_vrf_id(vrf_id);
2778 if (!bgp)
2779 return 0;
2780
2781 s = zclient->ibuf;
2782 stream_get(&esi, s, sizeof(esi_t));
2783 originator_ip.s_addr = stream_get_ipv4(s);
2784 active = stream_getc(s);
74e2bd89 2785 df_pref = stream_getw(s);
74be8313 2786 bypass = stream_getc(s);
c44ab6f1
AK
2787
2788 if (BGP_DEBUG(zebra, ZEBRA))
74e2bd89 2789 zlog_debug(
74be8313
AK
2790 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2791 esi_to_str(&esi, buf, sizeof(buf)), &originator_ip,
2792 active, df_pref, bypass ? "bypass" : "");
c44ab6f1 2793
a383bfc7
AK
2794 frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip,
2795 active, bypass, df_pref);
2796
74be8313
AK
2797 bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref,
2798 !!bypass);
c44ab6f1
AK
2799
2800 return 0;
2801}
2802
2803static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS)
50f74cf1 2804{
2805 esi_t esi;
2806 struct bgp *bgp = NULL;
2807 struct stream *s = NULL;
2808 char buf[ESI_STR_LEN];
50f74cf1 2809
2810 memset(&esi, 0, sizeof(esi_t));
c44ab6f1
AK
2811 bgp = bgp_lookup_by_vrf_id(vrf_id);
2812 if (!bgp)
2813 return 0;
2814
2815 s = zclient->ibuf;
2816 stream_get(&esi, s, sizeof(esi_t));
2817
2818 if (BGP_DEBUG(zebra, ZEBRA))
2819 zlog_debug("Rx del ESI %s",
2820 esi_to_str(&esi, buf, sizeof(buf)));
2821
a383bfc7
AK
2822 frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi);
2823
c44ab6f1
AK
2824 bgp_evpn_local_es_del(bgp, &esi);
2825
2826 return 0;
2827}
2828
2829static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS)
2830{
2831 esi_t esi;
2832 vni_t vni;
2833 struct bgp *bgp;
2834 struct stream *s;
2835 char buf[ESI_STR_LEN];
50f74cf1 2836
2837 bgp = bgp_lookup_by_vrf_id(vrf_id);
2838 if (!bgp)
2839 return 0;
2840
2841 s = zclient->ibuf;
2842 stream_get(&esi, s, sizeof(esi_t));
c44ab6f1 2843 vni = stream_getl(s);
50f74cf1 2844
2845 if (BGP_DEBUG(zebra, ZEBRA))
c44ab6f1 2846 zlog_debug("Rx %s ESI %s VNI %u",
3756b9ac
IS
2847 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2848 esi_to_str(&esi, buf, sizeof(buf)), vni);
50f74cf1 2849
a383bfc7
AK
2850 if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) {
2851 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni);
2852
c44ab6f1 2853 bgp_evpn_local_es_evi_add(bgp, &esi, vni);
a383bfc7
AK
2854 } else {
2855 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni);
2856
c44ab6f1 2857 bgp_evpn_local_es_evi_del(bgp, &esi, vni);
a383bfc7 2858 }
c44ab6f1 2859
50f74cf1 2860 return 0;
2861}
2862
121f9dee 2863static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS)
fe1dc5a3 2864{
c48d9f5f 2865 int filter = 0;
b67a60d2 2866 vni_t l3vni = 0;
14e814ea 2867 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
b67a60d2 2868 struct in_addr originator_ip;
fe1dc5a3 2869 struct stream *s;
0483af6e 2870 ifindex_t svi_ifindex;
14e814ea 2871 bool is_anycast_mac = false;
fe1dc5a3 2872
14e814ea 2873 memset(&svi_rmac, 0, sizeof(struct ethaddr));
b67a60d2 2874 memset(&originator_ip, 0, sizeof(struct in_addr));
fe1dc5a3
MK
2875 s = zclient->ibuf;
2876 l3vni = stream_getl(s);
b67a60d2 2877 if (cmd == ZEBRA_L3VNI_ADD) {
14e814ea 2878 stream_get(&svi_rmac, s, sizeof(struct ethaddr));
b67a60d2 2879 originator_ip.s_addr = stream_get_ipv4(s);
c48d9f5f 2880 stream_get(&filter, s, sizeof(int));
0483af6e 2881 svi_ifindex = stream_getl(s);
14e814ea
CS
2882 stream_get(&vrr_rmac, s, sizeof(struct ethaddr));
2883 is_anycast_mac = stream_getl(s);
fe1dc5a3 2884
0483af6e 2885 if (BGP_DEBUG(zebra, ZEBRA))
c0d72166
DS
2886 zlog_debug(
2887 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2888 vrf_id_to_name(vrf_id), l3vni, &svi_rmac,
2889 &vrr_rmac,
2890 filter ? "prefix-routes-only" : "none",
2891 svi_ifindex);
fe1dc5a3 2892
a383bfc7
AK
2893 frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id,
2894 &svi_rmac, &vrr_rmac, filter, originator_ip,
2895 svi_ifindex, is_anycast_mac);
2896
14e814ea
CS
2897 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac,
2898 originator_ip, filter, svi_ifindex,
2899 is_anycast_mac);
0483af6e 2900 } else {
2901 if (BGP_DEBUG(zebra, ZEBRA))
2902 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2903 vrf_id_to_name(vrf_id), l3vni);
2904
a383bfc7
AK
2905 frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id);
2906
fe1dc5a3 2907 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
0483af6e 2908 }
fe1dc5a3
MK
2909
2910 return 0;
2911}
2912
121f9dee 2913static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS)
128ea8ab 2914{
d62a17ae 2915 struct stream *s;
2916 vni_t vni;
2917 struct bgp *bgp;
a4d82a8a 2918 struct in_addr vtep_ip = {INADDR_ANY};
29c53922 2919 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
76d07c7a 2920 struct in_addr mcast_grp = {INADDR_ANY};
9daa5d47 2921 ifindex_t svi_ifindex = 0;
d62a17ae 2922
2923 s = zclient->ibuf;
2924 vni = stream_getl(s);
121f9dee 2925 if (cmd == ZEBRA_VNI_ADD) {
d62a17ae 2926 vtep_ip.s_addr = stream_get_ipv4(s);
29c53922 2927 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
76d07c7a 2928 mcast_grp.s_addr = stream_get_ipv4(s);
9daa5d47 2929 stream_get(&svi_ifindex, s, sizeof(ifindex_t));
29c53922
MK
2930 }
2931
d62a17ae 2932 bgp = bgp_lookup_by_vrf_id(vrf_id);
2933 if (!bgp)
2934 return 0;
2935
2936 if (BGP_DEBUG(zebra, ZEBRA))
9daa5d47
AD
2937 zlog_debug(
2938 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
2939 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2940 vrf_id_to_name(vrf_id), vni,
2941 vrf_id_to_name(tenant_vrf_id), svi_ifindex);
d62a17ae 2942
a383bfc7
AK
2943 if (cmd == ZEBRA_VNI_ADD) {
2944 frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip,
2945 tenant_vrf_id, mcast_grp);
2946
d62a17ae 2947 return bgp_evpn_local_vni_add(
975a328e
DA
2948 bgp, vni,
2949 vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id,
9daa5d47 2950 tenant_vrf_id, mcast_grp, svi_ifindex);
a383bfc7
AK
2951 } else {
2952 frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni);
2953
d62a17ae 2954 return bgp_evpn_local_vni_del(bgp, vni);
a383bfc7 2955 }
128ea8ab 2956}
2957
121f9dee 2958static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS)
128ea8ab 2959{
d62a17ae 2960 struct stream *s;
2961 vni_t vni;
2962 struct bgp *bgp;
2963 struct ethaddr mac;
2964 struct ipaddr ip;
2965 int ipa_len;
f07e1c99 2966 uint8_t flags = 0;
2967 uint32_t seqnum = 0;
ec0ab544 2968 int state = 0;
c44ab6f1
AK
2969 char buf2[ESI_STR_LEN];
2970 esi_t esi;
d62a17ae 2971
2972 memset(&ip, 0, sizeof(ip));
2973 s = zclient->ibuf;
2974 vni = stream_getl(s);
28328ea9 2975 stream_get(&mac.octet, s, ETH_ALEN);
d62a17ae 2976 ipa_len = stream_getl(s);
2977 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2978 && ipa_len != IPV6_MAX_BYTELEN) {
e50f7cfd 2979 flog_err(EC_BGP_MACIP_LEN,
1c50c1c0 2980 "%u:Recv MACIP %s with invalid IP addr length %d",
121f9dee 2981 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1c50c1c0 2982 ipa_len);
d62a17ae 2983 return -1;
2984 }
2985
2986 if (ipa_len) {
2987 ip.ipa_type =
2988 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
2989 stream_get(&ip.ip.addr, s, ipa_len);
2990 }
121f9dee 2991 if (cmd == ZEBRA_MACIP_ADD) {
f07e1c99 2992 flags = stream_getc(s);
2993 seqnum = stream_getl(s);
c44ab6f1 2994 stream_get(&esi, s, sizeof(esi_t));
ec0ab544
AK
2995 } else {
2996 state = stream_getl(s);
35fb444b 2997 memset(&esi, 0, sizeof(esi_t));
f07e1c99 2998 }
d62a17ae 2999
3000 bgp = bgp_lookup_by_vrf_id(vrf_id);
3001 if (!bgp)
3002 return 0;
3003
3004 if (BGP_DEBUG(zebra, ZEBRA))
c0d72166 3005 zlog_debug(
2dd0dde7 3006 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
c0d72166
DS
3007 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
3008 &mac, &ip, vni, seqnum, state,
3009 esi_to_str(&esi, buf2, sizeof(buf2)));
d62a17ae 3010
a383bfc7
AK
3011 if (cmd == ZEBRA_MACIP_ADD) {
3012 frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip,
3013 flags, seqnum, &esi);
3014
f07e1c99 3015 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
c44ab6f1 3016 flags, seqnum, &esi);
a383bfc7
AK
3017 } else {
3018 frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip,
3019 state);
3020
ec0ab544 3021 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state);
a383bfc7 3022 }
128ea8ab 3023}
6aeb9e78 3024
a243d1db 3025static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS)
31310b25
MK
3026{
3027 struct stream *s = NULL;
3028 struct bgp *bgp_vrf = NULL;
3029 struct prefix p;
31310b25
MK
3030
3031 memset(&p, 0, sizeof(struct prefix));
3032 s = zclient->ibuf;
3033 stream_get(&p, s, sizeof(struct prefix));
3034
3035 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3036 if (!bgp_vrf)
a243d1db 3037 return 0;
31310b25
MK
3038
3039 if (BGP_DEBUG(zebra, ZEBRA))
2dbe669b 3040 zlog_debug("Recv prefix %pFX %s on vrf %s", &p,
31310b25
MK
3041 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
3042 vrf_id_to_name(vrf_id));
3043
3044 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
3045
3046 if (p.family == AF_INET)
d90b788e
A
3047 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3048 AFI_IP, SAFI_UNICAST);
31310b25 3049 else
d90b788e
A
3050 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3051 AFI_IP6, SAFI_UNICAST);
31310b25
MK
3052
3053 } else {
3054 if (p.family == AF_INET)
d90b788e
A
3055 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
3056 SAFI_UNICAST);
31310b25 3057 else
d90b788e
A
3058 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
3059 SAFI_UNICAST);
31310b25 3060 }
a243d1db 3061 return 0;
31310b25
MK
3062}
3063
a243d1db 3064static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS)
955bfd98
PZ
3065{
3066 struct stream *s = NULL;
3067 uint8_t response_keep;
3068 uint32_t first;
3069 uint32_t last;
aec865e4
FR
3070 uint8_t proto;
3071 unsigned short instance;
955bfd98
PZ
3072
3073 s = zclient->ibuf;
aec865e4
FR
3074 STREAM_GETC(s, proto);
3075 STREAM_GETW(s, instance);
955bfd98
PZ
3076 STREAM_GETC(s, response_keep);
3077 STREAM_GETL(s, first);
3078 STREAM_GETL(s, last);
3079
aec865e4 3080 if (zclient->redist_default != proto) {
e50f7cfd 3081 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
1c50c1c0 3082 proto);
a243d1db 3083 return 0;
aec865e4
FR
3084 }
3085 if (zclient->instance != instance) {
e50f7cfd 3086 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
1c50c1c0 3087 proto);
a243d1db 3088 return 0;
aec865e4
FR
3089 }
3090
955bfd98
PZ
3091 if (first > last ||
3092 first < MPLS_LABEL_UNRESERVED_MIN ||
3093 last > MPLS_LABEL_UNRESERVED_MAX) {
3094
e50f7cfd 3095 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
1c50c1c0 3096 __func__, first, last);
a243d1db 3097 return 0;
955bfd98
PZ
3098 }
3099 if (BGP_DEBUG(zebra, ZEBRA)) {
3100 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3101 first, last, response_keep);
3102 }
3103
e70e9f8e 3104 bgp_lp_event_chunk(response_keep, first, last);
955bfd98 3105
a243d1db
DL
3106 return 0;
3107
955bfd98 3108stream_failure: /* for STREAM_GETX */
a243d1db 3109 return -1;
955bfd98
PZ
3110}
3111
342213ea
DS
3112extern struct zebra_privs_t bgpd_privs;
3113
138c5a74
DS
3114static int bgp_ifp_create(struct interface *ifp)
3115{
ef7bd2a3
DS
3116 struct bgp *bgp;
3117
3118 if (BGP_DEBUG(zebra, ZEBRA))
096f7609
IR
3119 zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id,
3120 ifp->name);
ef7bd2a3 3121
096f7609 3122 bgp = ifp->vrf->info;
ef7bd2a3
DS
3123 if (!bgp)
3124 return 0;
3125
3126 bgp_mac_add_mac_entry(ifp);
3127
3128 bgp_update_interface_nbrs(bgp, ifp, ifp);
0d020cd6 3129 hook_call(bgp_vrf_status_changed, bgp, ifp);
138c5a74
DS
3130 return 0;
3131}
3132
a243d1db 3133static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS)
a0281b2e
HS
3134{
3135 struct stream *s = NULL;
7de4c885
HS
3136 struct bgp *bgp = bgp_get_default();
3137 struct listnode *node;
3138 struct prefix_ipv6 *c;
1bda3e62 3139 struct srv6_locator_chunk s6c = {};
3a0220e4 3140 struct prefix_ipv6 *chunk = NULL;
a0281b2e
HS
3141
3142 s = zclient->ibuf;
3a0220e4 3143 zapi_srv6_locator_chunk_decode(s, &s6c);
a0281b2e 3144
3a0220e4
HS
3145 if (strcmp(bgp->srv6_locator_name, s6c.locator_name) != 0) {
3146 zlog_err("%s: Locator name unmatch %s:%s", __func__,
3147 bgp->srv6_locator_name, s6c.locator_name);
a243d1db 3148 return 0;
a0281b2e
HS
3149 }
3150
a0281b2e 3151 for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) {
3a0220e4 3152 if (!prefix_cmp(c, &s6c.prefix))
a243d1db 3153 return 0;
a0281b2e
HS
3154 }
3155
3a0220e4
HS
3156 chunk = prefix_ipv6_new();
3157 *chunk = s6c.prefix;
a0281b2e
HS
3158 listnode_add(bgp->srv6_locator_chunks, chunk);
3159 vpn_leak_postchange_all();
a243d1db 3160 return 0;
a0281b2e
HS
3161}
3162
d79ff732
HS
3163static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS)
3164{
3165 struct srv6_locator loc = {};
3166 struct bgp *bgp = bgp_get_default();
3167 const char *loc_name = bgp->srv6_locator_name;
3168
3169 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3170 return -1;
3171
3172 if (!bgp || !bgp->srv6_enabled)
3173 return 0;
3174
3175 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0)
3176 return -1;
3177
3178 return 0;
3179}
3180
3181static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
3182{
3183 struct srv6_locator loc = {};
3184 struct bgp *bgp = bgp_get_default();
3185 struct listnode *node, *nnode;
3186 struct prefix_ipv6 *chunk;
3187 struct bgp_srv6_function *func;
3188 struct bgp *bgp_vrf;
3189 struct in6_addr *tovpn_sid;
3190 struct prefix_ipv6 tmp_prefi;
3191
3192 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3193 return -1;
3194
3195 // refresh chunks
3196 for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
3197 if (prefix_match((struct prefix *)&loc.prefix,
3198 (struct prefix *)chunk))
3199 listnode_delete(bgp->srv6_locator_chunks, chunk);
3200
3201 // refresh functions
3202 for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
3203 tmp_prefi.family = AF_INET6;
3204 tmp_prefi.prefixlen = 128;
3205 tmp_prefi.prefix = func->sid;
3206 if (prefix_match((struct prefix *)&loc.prefix,
3207 (struct prefix *)&tmp_prefi))
3208 listnode_delete(bgp->srv6_functions, func);
3209 }
3210
3211 // refresh tovpn_sid
3212 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3213 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3214 continue;
3215
3216 // refresh vpnv4 tovpn_sid
3217 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid;
3218 if (tovpn_sid) {
3219 tmp_prefi.family = AF_INET6;
3220 tmp_prefi.prefixlen = 128;
3221 tmp_prefi.prefix = *tovpn_sid;
3222 if (prefix_match((struct prefix *)&loc.prefix,
3223 (struct prefix *)&tmp_prefi))
3224 XFREE(MTYPE_BGP_SRV6_SID,
3225 bgp_vrf->vpn_policy[AFI_IP].tovpn_sid);
3226 }
3227
3228 // refresh vpnv6 tovpn_sid
3229 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid;
3230 if (tovpn_sid) {
3231 tmp_prefi.family = AF_INET6;
3232 tmp_prefi.prefixlen = 128;
3233 tmp_prefi.prefix = *tovpn_sid;
3234 if (prefix_match((struct prefix *)&loc.prefix,
3235 (struct prefix *)&tmp_prefi))
3236 XFREE(MTYPE_BGP_SRV6_SID,
3237 bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid);
3238 }
3239 }
3240
3241 vpn_leak_postchange_all();
3242 return 0;
3243}
3244
a243d1db
DL
3245static zclient_handler *const bgp_handlers[] = {
3246 [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update,
3247 [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add,
3248 [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete,
3249 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add,
3250 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete,
3251 [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update,
3252 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route,
3253 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route,
3254 [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update,
3255 [ZEBRA_FEC_UPDATE] = bgp_read_fec_update,
3256 [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add,
3257 [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del,
3258 [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni,
3259 [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi,
3260 [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi,
3261 [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni,
3262 [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip,
3263 [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip,
3264 [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni,
3265 [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni,
3266 [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix,
3267 [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix,
3268 [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk,
3269 [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
3270 [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner,
3271 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner,
3272 [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner,
3273 [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner,
3274 [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add,
3275 [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete,
3276 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] =
3277 bgp_zebra_process_srv6_locator_chunk,
3278};
3279
f533be73 3280void bgp_zebra_init(struct thread_master *master, unsigned short instance)
718e3744 3281{
d62a17ae 3282 zclient_num_connects = 0;
3283
138c5a74
DS
3284 if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up,
3285 bgp_ifp_down, bgp_ifp_destroy);
3286
d62a17ae 3287 /* Set default values. */
a243d1db
DL
3288 zclient = zclient_new(master, &zclient_options_default, bgp_handlers,
3289 array_size(bgp_handlers));
342213ea 3290 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
d62a17ae 3291 zclient->zebra_connected = bgp_zebra_connected;
f533be73 3292 zclient->instance = instance;
718e3744 3293}
bb86c601 3294
d62a17ae 3295void bgp_zebra_destroy(void)
bb86c601 3296{
d62a17ae 3297 if (zclient == NULL)
3298 return;
3299 zclient_stop(zclient);
3300 zclient_free(zclient);
3301 zclient = NULL;
bb86c601 3302}
afbb1c59 3303
d62a17ae 3304int bgp_zebra_num_connects(void)
afbb1c59 3305{
d62a17ae 3306 return zclient_num_connects;
afbb1c59 3307}
30d50e6d 3308
6cfe5d15
PG
3309void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
3310 struct bgp_pbr_rule *pbr,
3311 bool install)
30d50e6d
PG
3312{
3313 struct stream *s;
3314
6cfe5d15 3315 if (pbra->install_in_progress && !pbr)
30d50e6d 3316 return;
6cfe5d15
PG
3317 if (pbr && pbr->install_in_progress)
3318 return;
3319 if (BGP_DEBUG(zebra, ZEBRA)) {
3320 if (pbr)
15569c58 3321 zlog_debug("%s: table %d (ip rule) %d", __func__,
6cfe5d15
PG
3322 pbra->table_id, install);
3323 else
15569c58 3324 zlog_debug("%s: table %d fwmark %d %d", __func__,
6cfe5d15
PG
3325 pbra->table_id, pbra->fwmark, install);
3326 }
30d50e6d
PG
3327 s = zclient->obuf;
3328 stream_reset(s);
3329
3330 zclient_create_header(s,
3331 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
3332 VRF_DEFAULT);
3333 stream_putl(s, 1); /* send one pbr action */
3334
6cfe5d15 3335 bgp_encode_pbr_rule_action(s, pbra, pbr);
30d50e6d
PG
3336
3337 stream_putw_at(s, 0, stream_get_endp(s));
8a3f8f2e
DS
3338 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE)
3339 && install) {
6cfe5d15
PG
3340 if (!pbr)
3341 pbra->install_in_progress = true;
3342 else
3343 pbr->install_in_progress = true;
3344 }
30d50e6d
PG
3345}
3346
3347void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
3348{
3349 struct stream *s;
3350
3351 if (pbrim->install_in_progress)
3352 return;
f0936054 3353 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
3354 zlog_debug("%s: name %s type %d %d, ID %u", __func__,
3355 pbrim->ipset_name, pbrim->type, install,
3356 pbrim->unique);
30d50e6d
PG
3357 s = zclient->obuf;
3358 stream_reset(s);
3359
3360 zclient_create_header(s,
3361 install ? ZEBRA_IPSET_CREATE :
3362 ZEBRA_IPSET_DESTROY,
3363 VRF_DEFAULT);
3364
3365 stream_putl(s, 1); /* send one pbr action */
3366
3367 bgp_encode_pbr_ipset_match(s, pbrim);
3368
3369 stream_putw_at(s, 0, stream_get_endp(s));
8a3f8f2e 3370 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
30d50e6d
PG
3371 pbrim->install_in_progress = true;
3372}
3373
3374void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
3375 bool install)
3376{
3377 struct stream *s;
3378
3379 if (pbrime->install_in_progress)
3380 return;
f0936054 3381 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
3382 zlog_debug("%s: name %s %d %d, ID %u", __func__,
3383 pbrime->backpointer->ipset_name, pbrime->unique,
3384 install, pbrime->unique);
30d50e6d
PG
3385 s = zclient->obuf;
3386 stream_reset(s);
3387
3388 zclient_create_header(s,
3389 install ? ZEBRA_IPSET_ENTRY_ADD :
3390 ZEBRA_IPSET_ENTRY_DELETE,
3391 VRF_DEFAULT);
3392
3393 stream_putl(s, 1); /* send one pbr action */
3394
3395 bgp_encode_pbr_ipset_entry_match(s, pbrime);
3396
3397 stream_putw_at(s, 0, stream_get_endp(s));
8a3f8f2e 3398 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
30d50e6d
PG
3399 pbrime->install_in_progress = true;
3400}
c16a0a62 3401
8f242187
PG
3402static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s,
3403 uint8_t family)
4762c213
PG
3404{
3405 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3406 struct bgp_pbr_interface_head *head;
3407 struct bgp_pbr_interface *pbr_if;
3408 struct interface *ifp;
3409
3410 if (!bgp_pbr_cfg)
3411 return;
8f242187
PG
3412 if (family == AF_INET)
3413 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3414 else
3415 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
4762c213 3416 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
a36898e7 3417 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
4762c213
PG
3418 if (ifp)
3419 stream_putl(s, ifp->ifindex);
3420 }
3421}
3422
8f242187 3423static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family)
4762c213
PG
3424{
3425 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3426 struct bgp_pbr_interface_head *head;
3427 struct bgp_pbr_interface *pbr_if;
3428 int cnt = 0;
3429
3430 if (!bgp_pbr_cfg)
3431 return 0;
8f242187
PG
3432 if (family == AF_INET)
3433 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3434 else
3435 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
4762c213 3436 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
a36898e7 3437 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
4762c213
PG
3438 cnt++;
3439 }
3440 return cnt;
3441}
3442
c16a0a62
PG
3443void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
3444 struct bgp_pbr_match *pbm,
3445 bool install)
3446{
3447 struct stream *s;
b5c40105 3448 int ret = 0;
4762c213 3449 int nb_interface;
c16a0a62
PG
3450
3451 if (pbm->install_iptable_in_progress)
3452 return;
f0936054 3453 if (BGP_DEBUG(zebra, ZEBRA))
15569c58
DA
3454 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__,
3455 pbm->ipset_name, pbm->type, pba->fwmark, install,
88055124 3456 pbm->unique2);
c16a0a62
PG
3457 s = zclient->obuf;
3458 stream_reset(s);
3459
3460 zclient_create_header(s,
3461 install ? ZEBRA_IPTABLE_ADD :
3462 ZEBRA_IPTABLE_DELETE,
3463 VRF_DEFAULT);
3464
3465 bgp_encode_pbr_iptable_match(s, pba, pbm);
8f242187 3466 nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family);
4762c213
PG
3467 stream_putl(s, nb_interface);
3468 if (nb_interface)
8f242187 3469 bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family);
c16a0a62 3470 stream_putw_at(s, 0, stream_get_endp(s));
b5c40105
PG
3471 ret = zclient_send_message(zclient);
3472 if (install) {
8a3f8f2e 3473 if (ret != ZCLIENT_SEND_FAILURE)
b5c40105
PG
3474 pba->refcnt++;
3475 else
3476 pbm->install_iptable_in_progress = true;
a6b07429 3477 }
c16a0a62 3478}
f7df1907
PG
3479
3480/* inject in table <table_id> a default route to:
3481 * - if nexthop IP is present : to this nexthop
3482 * - if vrf is different from local : to the matching VRF
3483 */
3484void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
3485 afi_t afi, uint32_t table_id, bool announce)
3486{
3487 struct zapi_nexthop *api_nh;
3488 struct zapi_route api;
3489 struct prefix p;
3490
f01e580f
PG
3491 if (!nh || (nh->type != NEXTHOP_TYPE_IPV4
3492 && nh->type != NEXTHOP_TYPE_IPV6)
f7df1907
PG
3493 || nh->vrf_id == VRF_UNKNOWN)
3494 return;
7afeaffa
PG
3495
3496 /* in vrf-lite, no default route has to be announced
3497 * the table id of vrf is directly used to divert traffic
3498 */
3499 if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id)
3500 return;
3501
f7df1907 3502 memset(&p, 0, sizeof(struct prefix));
f01e580f 3503 if (afi != AFI_IP && afi != AFI_IP6)
f7df1907 3504 return;
f01e580f 3505 p.family = afi2family(afi);
f7df1907
PG
3506 memset(&api, 0, sizeof(api));
3507 api.vrf_id = bgp->vrf_id;
3508 api.type = ZEBRA_ROUTE_BGP;
3509 api.safi = SAFI_UNICAST;
3510 api.prefix = p;
3511 api.tableid = table_id;
3512 api.nexthop_num = 1;
3513 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
3514 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
3515 api_nh = &api.nexthops[0];
3516
38a8c751
DS
3517 api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT;
3518 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
3519
f7df1907 3520 /* redirect IP */
f01e580f 3521 if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) {
f7df1907
PG
3522 char buff[PREFIX_STRLEN];
3523
3524 api_nh->vrf_id = nh->vrf_id;
3525 api_nh->gate.ipv4 = nh->gate.ipv4;
3526 api_nh->type = NEXTHOP_TYPE_IPV4;
3527
3528 inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN);
3529 if (BGP_DEBUG(zebra, ZEBRA))
f01e580f
PG
3530 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
3531 announce ? "adding" : "withdrawing",
3532 buff, table_id);
3533 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3534 : ZEBRA_ROUTE_DELETE,
3535 zclient, &api);
3536 } else if (afi == AFI_IP6 &&
3537 memcmp(&nh->gate.ipv6,
3538 &in6addr_any, sizeof(struct in6_addr))) {
3539 char buff[PREFIX_STRLEN];
3540
3541 api_nh->vrf_id = nh->vrf_id;
3542 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6,
3543 sizeof(struct in6_addr));
3544 api_nh->type = NEXTHOP_TYPE_IPV6;
3545
3546 inet_ntop(AF_INET6, &(nh->gate.ipv6), buff, INET_ADDRSTRLEN);
3547 if (BGP_DEBUG(zebra, ZEBRA))
3548 zlog_debug("BGP: %s default route to %s table %d (redirect IP)",
d887503c 3549 announce ? "adding" : "withdrawing",
f7df1907
PG
3550 buff, table_id);
3551 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3552 : ZEBRA_ROUTE_DELETE,
3553 zclient, &api);
3554 } else if (nh->vrf_id != bgp->vrf_id) {
3555 struct vrf *vrf;
eb4244f8 3556 struct interface *ifp;
f7df1907 3557
eb4244f8 3558 vrf = vrf_lookup_by_id(nh->vrf_id);
f7df1907
PG
3559 if (!vrf)
3560 return;
eb4244f8
PG
3561 /* create default route with interface <VRF>
3562 * with nexthop-vrf <VRF>
f7df1907 3563 */
de4f1a66 3564 ifp = if_lookup_by_name_vrf(vrf->name, vrf);
eb4244f8
PG
3565 if (!ifp)
3566 return;
3567 api_nh->vrf_id = nh->vrf_id;
3568 api_nh->type = NEXTHOP_TYPE_IFINDEX;
3569 api_nh->ifindex = ifp->ifindex;
3570 if (BGP_DEBUG(zebra, ZEBRA))
d887503c
PG
3571 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3572 announce ? "adding" : "withdrawing",
eb4244f8
PG
3573 vrf->name, table_id);
3574 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3575 : ZEBRA_ROUTE_DELETE,
3576 zclient, &api);
f7df1907
PG
3577 return;
3578 }
3579}
85ef4179 3580
3581/* Send capabilities to RIB */
3582int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable)
3583{
3584 struct zapi_cap api;
3585 int ret = BGP_GR_SUCCESS;
3586
3587 if (zclient == NULL) {
3588 if (BGP_DEBUG(zebra, ZEBRA))
3589 zlog_debug("zclient invalid");
3590 return BGP_GR_FAILURE;
3591 }
3592
3593 /* Check if the client is connected */
3594 if ((zclient->sock < 0) || (zclient->t_connect)) {
3595 if (BGP_DEBUG(zebra, ZEBRA))
3596 zlog_debug("client not connected");
3597 return BGP_GR_FAILURE;
3598 }
3599
3600 /* Check if capability is already sent. If the flag force is set
3601 * send the capability since this can be initial bgp configuration
3602 */
3603 memset(&api, 0, sizeof(struct zapi_cap));
3604 if (disable) {
3605 api.cap = ZEBRA_CLIENT_GR_DISABLE;
3606 api.vrf_id = bgp->vrf_id;
3607 } else {
3608 api.cap = ZEBRA_CLIENT_GR_CAPABILITIES;
3609 api.stale_removal_time = bgp->rib_stale_time;
3610 api.vrf_id = bgp->vrf_id;
3611 }
3612
36235319 3613 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
7cfdb485 3614 == ZCLIENT_SEND_FAILURE) {
2ba1fe69 3615 zlog_err("error sending capability");
85ef4179 3616 ret = BGP_GR_FAILURE;
3617 } else {
3618 if (disable)
3619 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
3620 else
3621 bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE;
3622
3623 if (BGP_DEBUG(zebra, ZEBRA))
3624 zlog_debug("send capabilty success");
3625 ret = BGP_GR_SUCCESS;
3626 }
3627 return ret;
3628}
3629
3630/* Send route update pesding or completed status to RIB for the
3631 * specific AFI, SAFI
3632 */
3633int bgp_zebra_update(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type)
3634{
2ba1fe69 3635 struct zapi_cap api = {0};
85ef4179 3636
3637 if (zclient == NULL) {
3638 if (BGP_DEBUG(zebra, ZEBRA))
2ba1fe69 3639 zlog_debug("zclient == NULL, invalid");
85ef4179 3640 return BGP_GR_FAILURE;
3641 }
3642
3643 /* Check if the client is connected */
3644 if ((zclient->sock < 0) || (zclient->t_connect)) {
3645 if (BGP_DEBUG(zebra, ZEBRA))
3646 zlog_debug("client not connected");
3647 return BGP_GR_FAILURE;
3648 }
3649
85ef4179 3650 api.afi = afi;
3651 api.safi = safi;
3652 api.vrf_id = vrf_id;
3653 api.cap = type;
3654
36235319 3655 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
7cfdb485 3656 == ZCLIENT_SEND_FAILURE) {
85ef4179 3657 if (BGP_DEBUG(zebra, ZEBRA))
3658 zlog_debug("error sending capability");
3659 return BGP_GR_FAILURE;
3660 }
3661 return BGP_GR_SUCCESS;
3662}
3663
3664
85ef4179 3665/* Send RIB stale timer update */
3666int bgp_zebra_stale_timer_update(struct bgp *bgp)
3667{
3668 struct zapi_cap api;
3669
3670 if (zclient == NULL) {
3671 if (BGP_DEBUG(zebra, ZEBRA))
3672 zlog_debug("zclient invalid");
3673 return BGP_GR_FAILURE;
3674 }
3675
3676 /* Check if the client is connected */
3677 if ((zclient->sock < 0) || (zclient->t_connect)) {
3678 if (BGP_DEBUG(zebra, ZEBRA))
3679 zlog_debug("client not connected");
3680 return BGP_GR_FAILURE;
3681 }
3682
3683 memset(&api, 0, sizeof(struct zapi_cap));
3684 api.cap = ZEBRA_CLIENT_RIB_STALE_TIME;
3685 api.stale_removal_time = bgp->rib_stale_time;
3686 api.vrf_id = bgp->vrf_id;
36235319 3687 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
7cfdb485 3688 == ZCLIENT_SEND_FAILURE) {
85ef4179 3689 if (BGP_DEBUG(zebra, ZEBRA))
3690 zlog_debug("error sending capability");
3691 return BGP_GR_FAILURE;
3692 }
3693 if (BGP_DEBUG(zebra, ZEBRA))
3694 zlog_debug("send capabilty success");
3695 return BGP_GR_SUCCESS;
3696}
a0281b2e
HS
3697
3698int bgp_zebra_srv6_manager_get_locator_chunk(const char *name)
3699{
3700 return srv6_manager_get_locator_chunk(zclient, name);
3701}
0249b8b6
HS
3702
3703int bgp_zebra_srv6_manager_release_locator_chunk(const char *name)
3704{
3705 return srv6_manager_release_locator_chunk(zclient, name);
3706}