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