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