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