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