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