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