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