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