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