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