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