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