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