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