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