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