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