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