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