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