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