]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_zebra.c
bgpd: Fix peer determination from parent for imported routes
[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"
8196f13d 47#include "bgpd/bgp_mpath.h"
fb018d25 48#include "bgpd/bgp_nexthop.h"
ffd0c037 49#include "bgpd/bgp_nht.h"
8c4f6381 50#include "bgpd/bgp_bfd.h"
cd1964ff 51#include "bgpd/bgp_label.h"
65efcfce 52#if ENABLE_BGP_VNC
d62a17ae 53#include "bgpd/rfapi/rfapi_backend.h"
54#include "bgpd/rfapi/vnc_export_bgp.h"
65efcfce 55#endif
128ea8ab 56#include "bgpd/bgp_evpn.h"
ddb5b488 57#include "bgpd/bgp_mplsvpn.h"
955bfd98 58#include "bgpd/bgp_labelpool.h"
6b0655a2 59
718e3744 60/* All information about zebra. */
228da428 61struct zclient *zclient = NULL;
718e3744 62
ad4cbda1 63/* Can we install into zebra? */
d62a17ae 64static inline int bgp_install_info_to_zebra(struct bgp *bgp)
ad4cbda1 65{
d62a17ae 66 if (zclient->sock <= 0)
67 return 0;
ad4cbda1 68
d62a17ae 69 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
70 return 0;
ad4cbda1 71
d62a17ae 72 return 1;
ad4cbda1 73}
74
afbb1c59
LB
75int zclient_num_connects;
76
18a6dce6 77/* Router-id update message from zebra. */
d62a17ae 78static int bgp_router_id_update(int command, struct zclient *zclient,
79 zebra_size_t length, vrf_id_t vrf_id)
718e3744 80{
d62a17ae 81 struct prefix router_id;
718e3744 82
d62a17ae 83 zebra_router_id_update_read(zclient->ibuf, &router_id);
a39275d7 84
d62a17ae 85 if (BGP_DEBUG(zebra, ZEBRA)) {
86 char buf[PREFIX2STR_BUFFER];
87 prefix2str(&router_id, buf, sizeof(buf));
88 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
89 }
a39275d7 90
d62a17ae 91 bgp_router_id_zebra_bump(vrf_id, &router_id);
92 return 0;
718e3744 93}
94
fb018d25 95/* Nexthop update message from zebra. */
d62a17ae 96static int bgp_read_nexthop_update(int command, struct zclient *zclient,
97 zebra_size_t length, vrf_id_t vrf_id)
fb018d25 98{
d62a17ae 99 bgp_parse_nexthop_update(command, vrf_id);
100 return 0;
078430f6
DS
101}
102
d62a17ae 103static int bgp_read_import_check_update(int command, struct zclient *zclient,
104 zebra_size_t length, vrf_id_t vrf_id)
078430f6 105{
d62a17ae 106 bgp_parse_nexthop_update(command, vrf_id);
107 return 0;
fb018d25
DS
108}
109
4a04e5f7 110/* Set or clear interface on which unnumbered neighbor is configured. This
111 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
112 * interface.
113 */
d62a17ae 114static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
115 struct interface *upd_ifp)
4a04e5f7 116{
d62a17ae 117 struct listnode *node, *nnode;
118 struct peer *peer;
119
120 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
121 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
122 if (upd_ifp) {
123 peer->ifp = upd_ifp;
124 bgp_zebra_initiate_radv(bgp, peer);
125 } else {
126 bgp_zebra_terminate_radv(bgp, peer);
127 peer->ifp = upd_ifp;
128 }
129 }
130 }
4a04e5f7 131}
132
d62a17ae 133static int bgp_read_fec_update(int command, struct zclient *zclient,
134 zebra_size_t length)
cd1964ff 135{
d62a17ae 136 bgp_parse_fec_update();
137 return 0;
cd1964ff
DS
138}
139
d62a17ae 140static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
a80beece 141{
d62a17ae 142 struct listnode *node, *nnode;
143 struct peer *peer;
144
145 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
146 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
147 && peer->status != Established) {
148 if (peer_active(peer))
149 BGP_EVENT_ADD(peer, BGP_Stop);
150 BGP_EVENT_ADD(peer, BGP_Start);
151 }
152 }
a80beece
DS
153}
154
d62a17ae 155static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
a197c47c 156{
d62a17ae 157 struct listnode *node;
158 struct connected *connected;
159 struct interface *ifp;
160 struct prefix *p;
161
162 /* Kick-off the FSM for any relevant peers only if there is a
163 * valid local address on the interface.
164 */
165 ifp = ifc->ifp;
166 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
167 p = connected->address;
168 if (p->family == AF_INET6
169 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
170 break;
171 }
172 if (!connected)
173 return;
174
175 bgp_start_interface_nbrs(bgp, ifp);
a197c47c
DS
176}
177
d62a17ae 178static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
179 int del)
a80beece 180{
d62a17ae 181 struct listnode *node, *nnode;
182 struct peer *peer;
183 struct interface *ifp;
184
185 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
186 if (peer->conf_if
187 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
188 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
189 BGP_EVENT_ADD(peer, BGP_Stop);
190 }
191 }
192 /* Free neighbor also, if we're asked to. */
193 if (del) {
194 ifp = ifc->ifp;
195 listnode_delete(ifp->nbr_connected, ifc);
196 nbr_connected_free(ifc);
197 }
a80beece
DS
198}
199
718e3744 200/* Inteface addition message from zebra. */
d62a17ae 201static int bgp_interface_add(int command, struct zclient *zclient,
202 zebra_size_t length, vrf_id_t vrf_id)
718e3744 203{
d62a17ae 204 struct interface *ifp;
205 struct bgp *bgp;
718e3744 206
d62a17ae 207 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
208 if (!ifp) // unexpected
209 return 0;
718e3744 210
d62a17ae 211 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
212 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
a39275d7 213
d62a17ae 214 bgp = bgp_lookup_by_vrf_id(vrf_id);
215 if (!bgp)
216 return 0;
4a04e5f7 217
d62a17ae 218 bgp_update_interface_nbrs(bgp, ifp, ifp);
219 return 0;
718e3744 220}
221
d62a17ae 222static int bgp_interface_delete(int command, struct zclient *zclient,
223 zebra_size_t length, vrf_id_t vrf_id)
718e3744 224{
d62a17ae 225 struct stream *s;
226 struct interface *ifp;
227 struct bgp *bgp;
718e3744 228
2f9123e0
DS
229 bgp = bgp_lookup_by_vrf_id(vrf_id);
230 if (!bgp)
231 return 0;
232
d62a17ae 233 s = zclient->ibuf;
234 ifp = zebra_interface_state_read(s, vrf_id);
235 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
236 return 0;
a4499b83 237
d62a17ae 238 if (BGP_DEBUG(zebra, ZEBRA))
239 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
a39275d7 240
d62a17ae 241 bgp_update_interface_nbrs(bgp, ifp, NULL);
64745052 242
ff880b78 243 if_set_index(ifp, IFINDEX_INTERNAL);
d62a17ae 244 return 0;
718e3744 245}
246
d62a17ae 247static int bgp_interface_up(int command, struct zclient *zclient,
248 zebra_size_t length, vrf_id_t vrf_id)
718e3744 249{
d62a17ae 250 struct stream *s;
251 struct interface *ifp;
252 struct connected *c;
253 struct nbr_connected *nc;
254 struct listnode *node, *nnode;
255 struct bgp *bgp;
6aeb9e78 256
2f9123e0
DS
257 bgp = bgp_lookup_by_vrf_id(vrf_id);
258 if (!bgp)
259 return 0;
260
d62a17ae 261 s = zclient->ibuf;
262 ifp = zebra_interface_state_read(s, vrf_id);
718e3744 263
d62a17ae 264 if (!ifp)
265 return 0;
718e3744 266
d62a17ae 267 if (BGP_DEBUG(zebra, ZEBRA))
268 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
ad4cbda1 269
d62a17ae 270 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
271 bgp_connected_add(bgp, c);
718e3744 272
d62a17ae 273 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
274 bgp_nbr_connected_add(bgp, nc);
a80beece 275
d62a17ae 276 return 0;
718e3744 277}
278
d62a17ae 279static int bgp_interface_down(int command, struct zclient *zclient,
280 zebra_size_t length, vrf_id_t vrf_id)
718e3744 281{
d62a17ae 282 struct stream *s;
283 struct interface *ifp;
284 struct connected *c;
285 struct nbr_connected *nc;
286 struct listnode *node, *nnode;
287 struct bgp *bgp;
6aeb9e78 288
2f9123e0
DS
289 bgp = bgp_lookup_by_vrf_id(vrf_id);
290 if (!bgp)
291 return 0;
292
d62a17ae 293 s = zclient->ibuf;
294 ifp = zebra_interface_state_read(s, vrf_id);
295 if (!ifp)
296 return 0;
718e3744 297
d62a17ae 298 if (BGP_DEBUG(zebra, ZEBRA))
299 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
ad4cbda1 300
d62a17ae 301 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
302 bgp_connected_delete(bgp, c);
718e3744 303
d62a17ae 304 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
305 bgp_nbr_connected_delete(bgp, nc, 1);
a80beece 306
d62a17ae 307 /* Fast external-failover */
308 {
309 struct peer *peer;
718e3744 310
d62a17ae 311 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
312 return 0;
718e3744 313
d62a17ae 314 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
8c4f6381 315#if defined(HAVE_CUMULUS)
d62a17ae 316 /* Take down directly connected EBGP peers as well as
317 * 1-hop BFD
318 * tracked (directly connected) IBGP peers.
319 */
320 if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
321 && (!peer->bfd_info
322 || bgp_bfd_is_peer_multihop(peer)))
8c4f6381 323#else
d62a17ae 324 /* Take down directly connected EBGP peers */
325 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
8c4f6381 326#endif
d62a17ae 327 continue;
718e3744 328
d62a17ae 329 if (ifp == peer->nexthop.ifp) {
330 BGP_EVENT_ADD(peer, BGP_Stop);
331 peer->last_reset = PEER_DOWN_IF_DOWN;
332 }
333 }
334 }
718e3744 335
d62a17ae 336 return 0;
718e3744 337}
338
d62a17ae 339static int bgp_interface_address_add(int command, struct zclient *zclient,
340 zebra_size_t length, vrf_id_t vrf_id)
718e3744 341{
d62a17ae 342 struct connected *ifc;
2f9123e0
DS
343 struct bgp *bgp;
344
345 bgp = bgp_lookup_by_vrf_id(vrf_id);
346 if (!bgp)
347 return 0;
d62a17ae 348
349 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
350
351 if (ifc == NULL)
352 return 0;
353
354 if (bgp_debug_zebra(ifc->address)) {
355 char buf[PREFIX2STR_BUFFER];
356 prefix2str(ifc->address, buf, sizeof(buf));
357 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
358 ifc->ifp->name, buf);
359 }
360
361 if (if_is_operative(ifc->ifp)) {
d62a17ae 362 bgp_connected_add(bgp, ifc);
2f9123e0 363
d62a17ae 364 /* If we have learnt of any neighbors on this interface,
365 * check to kick off any BGP interface-based neighbors,
366 * but only if this is a link-local address.
367 */
368 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
369 && !list_isempty(ifc->ifp->nbr_connected))
370 bgp_start_interface_nbrs(bgp, ifc->ifp);
371 }
372
373 return 0;
718e3744 374}
375
d62a17ae 376static int bgp_interface_address_delete(int command, struct zclient *zclient,
377 zebra_size_t length, vrf_id_t vrf_id)
718e3744 378{
d62a17ae 379 struct connected *ifc;
380 struct bgp *bgp;
6aeb9e78 381
2f9123e0
DS
382 bgp = bgp_lookup_by_vrf_id(vrf_id);
383 if (!bgp)
384 return 0;
385
d62a17ae 386 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
718e3744 387
d62a17ae 388 if (ifc == NULL)
389 return 0;
718e3744 390
d62a17ae 391 if (bgp_debug_zebra(ifc->address)) {
392 char buf[PREFIX2STR_BUFFER];
393 prefix2str(ifc->address, buf, sizeof(buf));
394 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
395 ifc->ifp->name, buf);
396 }
a39275d7 397
d62a17ae 398 if (if_is_operative(ifc->ifp)) {
2f9123e0 399 bgp_connected_delete(bgp, ifc);
d62a17ae 400 }
718e3744 401
d62a17ae 402 connected_free(ifc);
718e3744 403
d62a17ae 404 return 0;
718e3744 405}
406
d62a17ae 407static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
408 zebra_size_t length, vrf_id_t vrf_id)
a80beece 409{
d62a17ae 410 struct nbr_connected *ifc = NULL;
411 struct bgp *bgp;
412
413 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
414
415 if (ifc == NULL)
416 return 0;
417
418 if (bgp_debug_zebra(ifc->address)) {
419 char buf[PREFIX2STR_BUFFER];
420 prefix2str(ifc->address, buf, sizeof(buf));
421 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
422 ifc->ifp->name, buf);
423 }
424
425 if (if_is_operative(ifc->ifp)) {
426 bgp = bgp_lookup_by_vrf_id(vrf_id);
427 if (bgp)
428 bgp_nbr_connected_add(bgp, ifc);
429 }
430
431 return 0;
a80beece
DS
432}
433
d62a17ae 434static int bgp_interface_nbr_address_delete(int command,
435 struct zclient *zclient,
436 zebra_size_t length,
437 vrf_id_t vrf_id)
a80beece 438{
d62a17ae 439 struct nbr_connected *ifc = NULL;
440 struct bgp *bgp;
6aeb9e78 441
d62a17ae 442 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
a80beece 443
d62a17ae 444 if (ifc == NULL)
445 return 0;
a80beece 446
d62a17ae 447 if (bgp_debug_zebra(ifc->address)) {
448 char buf[PREFIX2STR_BUFFER];
449 prefix2str(ifc->address, buf, sizeof(buf));
450 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id,
451 ifc->ifp->name, buf);
452 }
a80beece 453
d62a17ae 454 if (if_is_operative(ifc->ifp)) {
455 bgp = bgp_lookup_by_vrf_id(vrf_id);
456 if (bgp)
457 bgp_nbr_connected_delete(bgp, ifc, 0);
458 }
a80beece 459
d62a17ae 460 nbr_connected_free(ifc);
a80beece 461
d62a17ae 462 return 0;
a80beece
DS
463}
464
bfcd43b2 465/* VRF update for an interface. */
d62a17ae 466static int bgp_interface_vrf_update(int command, struct zclient *zclient,
467 zebra_size_t length, vrf_id_t vrf_id)
bfcd43b2 468{
d62a17ae 469 struct interface *ifp;
470 vrf_id_t new_vrf_id;
471 struct connected *c;
472 struct nbr_connected *nc;
473 struct listnode *node, *nnode;
474 struct bgp *bgp;
bfcd43b2 475
d62a17ae 476 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
477 &new_vrf_id);
478 if (!ifp)
479 return 0;
bfcd43b2 480
d62a17ae 481 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
482 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
483 ifp->name, new_vrf_id);
bfcd43b2 484
d62a17ae 485 bgp = bgp_lookup_by_vrf_id(vrf_id);
486 if (!bgp)
487 return 0;
bfcd43b2 488
d62a17ae 489 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
490 bgp_connected_delete(bgp, c);
bfcd43b2 491
d62a17ae 492 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
493 bgp_nbr_connected_delete(bgp, nc, 1);
bfcd43b2 494
d62a17ae 495 /* Fast external-failover */
496 {
497 struct peer *peer;
bfcd43b2 498
d62a17ae 499 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
500 return 0;
bfcd43b2 501
d62a17ae 502 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
503 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
504 continue;
bfcd43b2 505
d62a17ae 506 if (ifp == peer->nexthop.ifp)
507 BGP_EVENT_ADD(peer, BGP_Stop);
508 }
509 }
bfcd43b2 510
d62a17ae 511 if_update_to_new_vrf(ifp, new_vrf_id);
bfcd43b2 512
d62a17ae 513 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
514 if (!bgp)
515 return 0;
bfcd43b2 516
d62a17ae 517 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
518 bgp_connected_add(bgp, c);
bfcd43b2 519
d62a17ae 520 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
521 bgp_nbr_connected_add(bgp, nc);
522 return 0;
bfcd43b2 523}
524
718e3744 525/* Zebra route add and delete treatment. */
74489921
RW
526static int zebra_read_route(int command, struct zclient *zclient,
527 zebra_size_t length, vrf_id_t vrf_id)
718e3744 528{
9de1f7ff 529 enum nexthop_types_t nhtype;
74489921
RW
530 struct zapi_route api;
531 union g_addr nexthop;
9de1f7ff 532 ifindex_t ifindex;
74489921 533 int add, i;
d62a17ae 534 struct bgp *bgp;
535
536 bgp = bgp_lookup_by_vrf_id(vrf_id);
537 if (!bgp)
538 return 0;
539
74489921
RW
540 if (zapi_route_decode(zclient->ibuf, &api) < 0)
541 return -1;
d62a17ae 542
74489921
RW
543 /* we completely ignore srcdest routes for now. */
544 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
545 return 0;
d62a17ae 546
74489921
RW
547 /* ignore link-local address. */
548 if (api.prefix.family == AF_INET6
549 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
550 return 0;
d62a17ae 551
74489921
RW
552 nexthop = api.nexthops[0].gate;
553 ifindex = api.nexthops[0].ifindex;
9de1f7ff 554 nhtype = api.nexthops[0].type;
d62a17ae 555
74489921
RW
556 add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
557 if (add) {
d62a17ae 558 /*
559 * The ADD message is actually an UPDATE and there is no
560 * explicit DEL
561 * for a prior redistributed route, if any. So, perform an
562 * implicit
563 * DEL processing for the same redistributed route from any
564 * other
565 * source type.
566 */
567 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
568 if (i != api.type)
74489921 569 bgp_redistribute_delete(bgp, &api.prefix, i,
d62a17ae 570 api.instance);
571 }
572
573 /* Now perform the add/update. */
74489921 574 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
a4d82a8a
PZ
575 nhtype, api.metric, api.type, api.instance,
576 api.tag);
d62a17ae 577 } else {
74489921
RW
578 bgp_redistribute_delete(bgp, &api.prefix, api.type,
579 api.instance);
a39275d7 580 }
d62a17ae 581
74489921
RW
582 if (bgp_debug_zebra(&api.prefix)) {
583 char buf[2][PREFIX_STRLEN];
584
585 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
586 inet_ntop(api.prefix.family, &nexthop, buf[1], sizeof(buf[1]));
587 zlog_debug(
588 "Rx route %s VRF %u %s[%d] %s "
589 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
590 (add) ? "add" : "delete", vrf_id,
591 zebra_route_string(api.type), api.instance, buf[0],
592 buf[1], api.metric, api.tag);
d62a17ae 593 }
594
595 return 0;
718e3744 596}
6b0655a2 597
d62a17ae 598struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
718e3744 599{
f4e14fdb 600 struct vrf *vrf;
d62a17ae 601 struct listnode *cnode;
602 struct interface *ifp;
603 struct connected *connected;
604 struct prefix_ipv4 p;
605 struct prefix *cp;
606
f4e14fdb
RW
607 vrf = vrf_lookup_by_id(vrf_id);
608 if (!vrf)
609 return NULL;
610
d62a17ae 611 p.family = AF_INET;
612 p.prefix = *addr;
613 p.prefixlen = IPV4_MAX_BITLEN;
614
451fda4f 615 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 616 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
617 cp = connected->address;
618
619 if (cp->family == AF_INET)
620 if (prefix_match(cp, (struct prefix *)&p))
621 return ifp;
622 }
718e3744 623 }
d62a17ae 624 return NULL;
718e3744 625}
626
d62a17ae 627struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
718e3744 628{
f4e14fdb 629 struct vrf *vrf;
d62a17ae 630 struct listnode *cnode;
631 struct interface *ifp;
632 struct connected *connected;
633 struct prefix *cp;
634
f4e14fdb
RW
635 vrf = vrf_lookup_by_id(vrf_id);
636 if (!vrf)
637 return NULL;
638
451fda4f 639 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 640 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
641 cp = connected->address;
642
643 if (cp->family == AF_INET)
644 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
645 return ifp;
646 }
718e3744 647 }
d62a17ae 648 return NULL;
718e3744 649}
650
d62a17ae 651struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
652 vrf_id_t vrf_id)
718e3744 653{
f4e14fdb 654 struct vrf *vrf;
d62a17ae 655 struct listnode *cnode;
656 struct interface *ifp;
657 struct connected *connected;
658 struct prefix_ipv6 p;
659 struct prefix *cp;
660
f4e14fdb
RW
661 vrf = vrf_lookup_by_id(vrf_id);
662 if (!vrf)
663 return NULL;
664
d62a17ae 665 p.family = AF_INET6;
666 p.prefix = *addr;
667 p.prefixlen = IPV6_MAX_BITLEN;
668
451fda4f 669 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 670 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
671 cp = connected->address;
672
673 if (cp->family == AF_INET6)
674 if (prefix_match(cp, (struct prefix *)&p)) {
675 if (IN6_IS_ADDR_LINKLOCAL(
676 &cp->u.prefix6)) {
677 if (ifindex == ifp->ifindex)
678 return ifp;
679 } else
680 return ifp;
681 }
682 }
718e3744 683 }
d62a17ae 684 return NULL;
718e3744 685}
686
d62a17ae 687struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
688 ifindex_t ifindex, vrf_id_t vrf_id)
718e3744 689{
f4e14fdb 690 struct vrf *vrf;
d62a17ae 691 struct listnode *cnode;
692 struct interface *ifp;
693 struct connected *connected;
694 struct prefix *cp;
695
f4e14fdb
RW
696 vrf = vrf_lookup_by_id(vrf_id);
697 if (!vrf)
698 return NULL;
699
451fda4f 700 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 701 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
702 cp = connected->address;
703
704 if (cp->family == AF_INET6)
705 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
706 if (IN6_IS_ADDR_LINKLOCAL(
707 &cp->u.prefix6)) {
708 if (ifindex == ifp->ifindex)
709 return ifp;
710 } else
711 return ifp;
712 }
713 }
718e3744 714 }
d62a17ae 715 return NULL;
718e3744 716}
717
d62a17ae 718static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
718e3744 719{
d62a17ae 720 struct listnode *cnode;
721 struct connected *connected;
722 struct prefix *cp;
723
724 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
725 cp = connected->address;
726
727 if (cp->family == AF_INET6)
728 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
729 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
730 return 1;
731 }
732 }
733 return 0;
718e3744 734}
735
d62a17ae 736static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
718e3744 737{
d62a17ae 738 struct listnode *cnode;
739 struct connected *connected;
740 struct prefix *cp;
741
742 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
743 cp = connected->address;
744
745 if (cp->family == AF_INET6)
746 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
747 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
748 return 1;
749 }
750 }
751 return 0;
718e3744 752}
718e3744 753
d62a17ae 754static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
6ee06fa9 755{
d62a17ae 756 struct listnode *cnode;
757 struct connected *connected;
758 struct prefix *cp;
759
760 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
761 cp = connected->address;
762 if ((cp->family == AF_INET)
763 && !ipv4_martian(&(cp->u.prefix4))) {
764 *addr = cp->u.prefix4;
765 return 1;
766 }
767 }
768 return 0;
6ee06fa9
PM
769}
770
d62a17ae 771int bgp_nexthop_set(union sockunion *local, union sockunion *remote,
772 struct bgp_nexthop *nexthop, struct peer *peer)
718e3744 773{
d62a17ae 774 int ret = 0;
775 struct interface *ifp = NULL;
776
777 memset(nexthop, 0, sizeof(struct bgp_nexthop));
778
779 if (!local)
780 return -1;
781 if (!remote)
782 return -1;
783
784 if (local->sa.sa_family == AF_INET) {
785 nexthop->v4 = local->sin.sin_addr;
786 if (peer->update_if)
787 ifp = if_lookup_by_name(peer->update_if,
788 peer->bgp->vrf_id);
789 else
790 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
791 peer->bgp->vrf_id);
718e3744 792 }
d62a17ae 793 if (local->sa.sa_family == AF_INET6) {
794 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
795 if (peer->conf_if || peer->ifname)
796 ifp = if_lookup_by_name(peer->conf_if
797 ? peer->conf_if
798 : peer->ifname,
799 peer->bgp->vrf_id);
800 } else if (peer->update_if)
801 ifp = if_lookup_by_name(peer->update_if,
802 peer->bgp->vrf_id);
803 else
804 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
805 local->sin6.sin6_scope_id,
806 peer->bgp->vrf_id);
718e3744 807 }
d62a17ae 808
809 if (!ifp)
810 return -1;
811
812 nexthop->ifp = ifp;
813
814 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
815 if (local->sa.sa_family == AF_INET) {
816 /* IPv6 nexthop*/
817 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
818
819 if (!ret) {
820 /* There is no global nexthop. Use link-local address as
821 * both the
822 * global and link-local nexthop. In this scenario, the
823 * expectation
824 * for interop is that the network admin would use a
825 * route-map to
826 * specify the global IPv6 nexthop.
827 */
828 if_get_ipv6_local(ifp, &nexthop->v6_global);
829 memcpy(&nexthop->v6_local, &nexthop->v6_global,
830 IPV6_MAX_BYTELEN);
831 } else
832 if_get_ipv6_local(ifp, &nexthop->v6_local);
833
834 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
835 peer->shared_network = 1;
836 else
837 peer->shared_network = 0;
718e3744 838 }
718e3744 839
d62a17ae 840 /* IPv6 connection, fetch and store IPv4 local address if any. */
841 if (local->sa.sa_family == AF_INET6) {
842 struct interface *direct = NULL;
843
844 /* IPv4 nexthop. */
845 ret = if_get_ipv4_address(ifp, &nexthop->v4);
846 if (!ret && peer->local_id.s_addr)
847 nexthop->v4 = peer->local_id;
848
849 /* Global address*/
850 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
851 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
852 IPV6_MAX_BYTELEN);
853
854 /* If directory connected set link-local address. */
855 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
856 remote->sin6.sin6_scope_id,
857 peer->bgp->vrf_id);
858 if (direct)
859 if_get_ipv6_local(ifp, &nexthop->v6_local);
860 } else
861 /* Link-local address. */
862 {
863 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
864
865 /* If there is no global address. Set link-local
866 address as
867 global. I know this break RFC specification... */
868 /* In this scenario, the expectation for interop is that
869 * the
870 * network admin would use a route-map to specify the
871 * global
872 * IPv6 nexthop.
873 */
874 if (!ret)
875 memcpy(&nexthop->v6_global,
876 &local->sin6.sin6_addr,
877 IPV6_MAX_BYTELEN);
878 /* Always set the link-local address */
879 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
880 IPV6_MAX_BYTELEN);
881 }
882
883 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
884 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
885 remote->sin6.sin6_scope_id,
886 peer->bgp->vrf_id))
887 peer->shared_network = 1;
888 else
889 peer->shared_network = 0;
890 }
718e3744 891
d62a17ae 892/* KAME stack specific treatment. */
718e3744 893#ifdef KAME
d62a17ae 894 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
895 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
896 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
897 }
898 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
899 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
900 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
901 }
718e3744 902#endif /* KAME */
e33a4880 903
d62a17ae 904 /* If we have identified the local interface, there is no error for now.
905 */
906 return 0;
718e3744 907}
908
d62a17ae 909static struct in6_addr *bgp_info_to_ipv6_nexthop(struct bgp_info *info)
73ac8160 910{
d62a17ae 911 struct in6_addr *nexthop = NULL;
912
913 /* Only global address nexthop exists. */
914 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
915 nexthop = &info->attr->mp_nexthop_global;
916
917 /* If both global and link-local address present. */
918 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
919 /* Check if route-map is set to prefer global over link-local */
920 if (info->attr->mp_nexthop_prefer_global)
921 nexthop = &info->attr->mp_nexthop_global;
922 else {
923 /* Workaround for Cisco's nexthop bug. */
924 if (IN6_IS_ADDR_UNSPECIFIED(
925 &info->attr->mp_nexthop_global)
926 && info->peer->su_remote->sa.sa_family == AF_INET6)
927 nexthop =
928 &info->peer->su_remote->sin6.sin6_addr;
929 else
930 nexthop = &info->attr->mp_nexthop_local;
931 }
932 }
933
934 return nexthop;
73ac8160
DS
935}
936
d62a17ae 937static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
938 struct bgp_info *info)
73ac8160 939{
b4cb15c6
DL
940 route_map_result_t ret;
941
942 ret = route_map_apply(map, p, RMAP_BGP, info);
943 bgp_attr_flush(info->attr);
944
945 if (ret != RMAP_DENYMATCH)
d62a17ae 946 return 1;
947
948 if (bgp_debug_zebra(p)) {
949 if (p->family == AF_INET) {
950 char buf[2][INET_ADDRSTRLEN];
951 zlog_debug(
952 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
953 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
954 sizeof(buf[0])),
955 p->prefixlen,
956 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
957 sizeof(buf[1])));
958 }
959 if (p->family == AF_INET6) {
960 char buf[2][INET6_ADDRSTRLEN];
961 zlog_debug(
962 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
963 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
964 sizeof(buf[0])),
965 p->prefixlen,
966 inet_ntop(AF_INET6,
967 bgp_info_to_ipv6_nexthop(info),
968 buf[1], sizeof(buf[1])));
969 }
970 }
971 return 0;
73ac8160
DS
972}
973
b98f7728
PG
974static struct thread *bgp_tm_thread_connect;
975static bool bgp_tm_status_connected;
976
977static int bgp_zebra_tm_connect(struct thread *t)
978{
979 struct zclient *zclient;
980 int delay = 10, ret = 0;
981
982 zclient = THREAD_ARG(t);
983 if (bgp_tm_status_connected && zclient->sock > 0)
984 delay = 60;
985 else {
986 bgp_tm_status_connected = false;
987 ret = tm_table_manager_connect(zclient);
988 }
989 if (ret < 0) {
990 zlog_warn("Error connecting to table manager!");
991 bgp_tm_status_connected = false;
992 } else {
993 if (!bgp_tm_status_connected)
994 zlog_debug("Connecting to table manager. Success");
995 bgp_tm_status_connected = true;
996 }
997 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
998 &bgp_tm_thread_connect);
999 return 0;
1000}
1001
1002void bgp_zebra_init_tm_connect(void)
1003{
1004 int delay = 1;
1005
1006 /* if already set, do nothing
1007 */
1008 if (bgp_tm_thread_connect != NULL)
1009 return;
1010 bgp_tm_status_connected = false;
1011 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1012 &bgp_tm_thread_connect);
1013}
1014
1015int bgp_zebra_get_table_range(uint32_t chunk_size,
1016 uint32_t *start, uint32_t *end)
1017{
1018 int ret;
1019
1020 if (!bgp_tm_status_connected)
1021 return -1;
1022 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1023 if (ret < 0) {
1024 zlog_err("BGP: Error getting table chunk %u", chunk_size);
1025 return -1;
1026 }
1027 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1028 chunk_size, *start, *end);
1029 return 0;
1030}
1031
d62a17ae 1032void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
1033 struct bgp_info *info, struct bgp *bgp, afi_t afi,
1034 safi_t safi)
718e3744 1035{
9913029c
RW
1036 struct zapi_route api;
1037 struct zapi_nexthop *api_nh;
2ad4f093 1038 int nh_family;
a74e593b 1039 unsigned int valid_nh_count = 0;
2ad4f093 1040 int has_valid_label = 0;
d7c0a89a 1041 uint8_t distance;
d62a17ae 1042 struct peer *peer;
1043 struct bgp_info *mpinfo;
d7c0a89a 1044 uint32_t metric;
b4cb15c6 1045 struct attr local_attr;
d62a17ae 1046 struct bgp_info local_info;
b4cb15c6 1047 struct bgp_info *mpinfo_cp = &local_info;
d62a17ae 1048 route_tag_t tag;
1049 mpls_label_t label;
ddb5b488 1050 int nh_othervrf = 0;
960035b2 1051 char buf_prefix[PREFIX_STRLEN]; /* filled in if we are debugging */
d62a17ae 1052
1053 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1054 * know of this instance.
1055 */
1056 if (!bgp_install_info_to_zebra(bgp))
1057 return;
1058
d62a17ae 1059 if (bgp->main_zebra_update_hold)
1060 return;
1061
960035b2
PZ
1062 if (bgp_debug_zebra(p))
1063 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1064
ddb5b488
PZ
1065 /*
1066 * vrf leaking support (will have only one nexthop)
1067 */
1068 if (info->extra && info->extra->bgp_orig)
1069 nh_othervrf = 1;
1070
9913029c
RW
1071 /* Make Zebra API structure. */
1072 memset(&api, 0, sizeof(api));
2dbad57f 1073 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
9913029c
RW
1074 api.vrf_id = bgp->vrf_id;
1075 api.type = ZEBRA_ROUTE_BGP;
1076 api.safi = safi;
1077 api.prefix = *p;
1078 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1079
d62a17ae 1080 peer = info->peer;
1081
ddb5b488
PZ
1082 if (info->type == ZEBRA_ROUTE_BGP
1083 && info->sub_type == BGP_ROUTE_IMPORTED) {
1084
1ec90b5e 1085 /* Obtain peer from parent */
1086 if (info->extra && info->extra->parent)
1087 peer = ((struct bgp_info *)(info->extra->parent))->peer;
ddb5b488
PZ
1088 }
1089
d62a17ae 1090 tag = info->attr->tag;
1091
1092 /* When we create an aggregate route we must also install a Null0 route
1093 * in
1094 * the RIB */
1095 if (info->sub_type == BGP_ROUTE_AGGREGATE)
09a484dd 1096 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
d62a17ae 1097
88493076 1098 /* If the route's source is EVPN, flag as such. */
1099 if (is_route_parent_evpn(info))
90264d64 1100 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
d3135ba3 1101
d62a17ae 1102 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1103 || info->sub_type == BGP_ROUTE_AGGREGATE) {
9913029c 1104 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
4e8b02f4 1105 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
718e3744 1106 }
a39275d7 1107
d62a17ae 1108 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1109 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1110 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1111
4e8b02f4 1112 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
d62a17ae 1113
2ad4f093
RW
1114 /* Metric is currently based on the best-path only */
1115 metric = info->attr->med;
1116 for (mpinfo = info; mpinfo; mpinfo = bgp_info_mpath_next(mpinfo)) {
a74e593b
RW
1117 if (valid_nh_count >= multipath_num)
1118 break;
1119
b4cb15c6
DL
1120 *mpinfo_cp = *mpinfo;
1121
d0d695f4
RW
1122 /* Get nexthop address-family */
1123 if (p->family == AF_INET
1124 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1125 nh_family = AF_INET;
1126 else if (p->family == AF_INET6
1127 || (p->family == AF_INET
1128 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1129 nh_family = AF_INET6;
1130 else
1131 continue;
1132
1374aec9 1133 api_nh = &api.nexthops[valid_nh_count];
a12495e7
DS
1134 api_nh->vrf_id = nh_othervrf ? info->extra->bgp_orig->vrf_id
1135 : bgp->vrf_id;
2ad4f093
RW
1136 if (nh_family == AF_INET) {
1137 struct in_addr *nexthop;
d62a17ae 1138
ddb5b488 1139 if (bgp_debug_zebra(&api.prefix)) {
ddb5b488
PZ
1140 if (mpinfo->extra) {
1141 zlog_debug(
1142 "%s: p=%s, bgp_is_valid_label: %d",
1143 __func__, buf_prefix,
1144 bgp_is_valid_label(
1145 &mpinfo->extra
1146 ->label[0]));
1147 } else {
1148 zlog_debug(
1149 "%s: p=%s, extra is NULL, no label",
1150 __func__, buf_prefix);
1151 }
1152 }
1153
1154 if (bgp->table_map[afi][safi].name || nh_othervrf) {
d62a17ae 1155 /* Copy info and attributes, so the route-map
b4cb15c6
DL
1156 apply doesn't modify the BGP route info. */
1157 local_attr = *mpinfo->attr;
1158 mpinfo_cp->attr = &local_attr;
ddb5b488
PZ
1159 if (nh_othervrf) {
1160 /* allow route-map to modify */
1161 local_attr.nexthop =
1162 info->extra->nexthop_orig.u
1163 .prefix4;
1164 }
1165 }
b4cb15c6 1166
ddb5b488 1167 if (bgp->table_map[afi][safi].name) {
b4cb15c6 1168 if (!bgp_table_map_apply(
d62a17ae 1169 bgp->table_map[afi][safi].map, p,
b4cb15c6
DL
1170 mpinfo_cp))
1171 continue;
1172
1173 /* metric/tag is only allowed to be
1174 * overridden on 1st nexthop */
1175 if (mpinfo == info) {
1176 metric = mpinfo_cp->attr->med;
1177 tag = mpinfo_cp->attr->tag;
d62a17ae 1178 }
b4cb15c6 1179 }
d62a17ae 1180
b4cb15c6 1181 nexthop = &mpinfo_cp->attr->nexthop;
9913029c 1182 api_nh->gate.ipv4 = *nexthop;
a12495e7 1183
d3135ba3 1184 /* EVPN type-2 routes are
523cafc4 1185 programmed as onlink on l3-vni SVI
1186 */
90264d64 1187 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
d3135ba3 1188 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1189 else
1190 api_nh->type = NEXTHOP_TYPE_IPV4;
2ad4f093
RW
1191 } else {
1192 ifindex_t ifindex;
1193 struct in6_addr *nexthop;
9913029c 1194
d62a17ae 1195 ifindex = 0;
d62a17ae 1196
ddb5b488
PZ
1197 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1198 /* Copy info and attributes, so the route-map
1199 apply doesn't modify the BGP route info. */
1200 local_attr = *mpinfo->attr;
1201 mpinfo_cp->attr = &local_attr;
1202 if (nh_othervrf) {
1203 /* allow route-map to modify */
1204 local_attr.mp_nexthop_global =
1205 info->extra->nexthop_orig.u
1206 .prefix6;
1207 local_attr.mp_nexthop_len =
1208 BGP_ATTR_NHLEN_IPV6_GLOBAL;
1209 }
1210 }
1211
d62a17ae 1212 if (bgp->table_map[afi][safi].name) {
1213 /* Copy info and attributes, so the route-map
b4cb15c6
DL
1214 apply doesn't modify the BGP route info. */
1215 local_attr = *mpinfo->attr;
1216 mpinfo_cp->attr = &local_attr;
1217
1218 if (!bgp_table_map_apply(
d62a17ae 1219 bgp->table_map[afi][safi].map, p,
b4cb15c6
DL
1220 mpinfo_cp))
1221 continue;
1222
1223 /* metric/tag is only allowed to be
1224 * overridden on 1st nexthop */
1225 if (mpinfo == info) {
1226 metric = mpinfo_cp->attr->med;
1227 tag = mpinfo_cp->attr->tag;
d62a17ae 1228 }
b4cb15c6
DL
1229 }
1230 nexthop = bgp_info_to_ipv6_nexthop(mpinfo_cp);
d62a17ae 1231
1232 if ((mpinfo == info)
1233 && mpinfo->attr->mp_nexthop_len
1234 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1235 if (mpinfo->peer->nexthop.ifp)
1236 ifindex = mpinfo->peer->nexthop.ifp
1237 ->ifindex;
1238
1239 if (!ifindex) {
afb6d3f5
DS
1240 if (mpinfo->peer->conf_if)
1241 ifindex = mpinfo->peer->ifp->ifindex;
1242 else if (mpinfo->peer->ifname)
d62a17ae 1243 ifindex = ifname2ifindex(
afb6d3f5 1244 mpinfo->peer->ifname,
d62a17ae 1245 bgp->vrf_id);
1246 else if (mpinfo->peer->nexthop.ifp)
1247 ifindex = mpinfo->peer->nexthop.ifp
1248 ->ifindex;
1249 }
d4f8783d
DS
1250
1251 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1252 if (ifindex == 0)
1253 continue;
1254 } else
1255 ifindex = 0;
d62a17ae 1256
9913029c
RW
1257 api_nh->gate.ipv6 = *nexthop;
1258 api_nh->ifindex = ifindex;
d4f8783d
DS
1259 api_nh->type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1260 : NEXTHOP_TYPE_IPV6;
2ad4f093 1261 }
d62a17ae 1262
a4d82a8a
PZ
1263 if (mpinfo->extra
1264 && bgp_is_valid_label(&mpinfo->extra->label[0])
90264d64 1265 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
2ad4f093 1266 has_valid_label = 1;
b57ba6d2 1267 label = label_pton(&mpinfo->extra->label[0]);
9913029c 1268
2ad4f093
RW
1269 api_nh->label_num = 1;
1270 api_nh->labels[0] = label;
d62a17ae 1271 }
2ad4f093
RW
1272 valid_nh_count++;
1273 }
d62a17ae 1274
a89b49cc 1275 /* if this is a evpn route we don't have to include the label */
a4d82a8a 1276 if (has_valid_label && !(CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)))
2ad4f093 1277 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
d62a17ae 1278
09a484dd 1279 if (info->sub_type != BGP_ROUTE_AGGREGATE)
2ad4f093 1280 api.nexthop_num = valid_nh_count;
d62a17ae 1281
2ad4f093
RW
1282 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1283 api.metric = metric;
d62a17ae 1284
2ad4f093
RW
1285 if (tag) {
1286 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1287 api.tag = tag;
1288 }
d62a17ae 1289
2ad4f093
RW
1290 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1291 if (distance) {
1292 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1293 api.distance = distance;
1294 }
d62a17ae 1295
2ad4f093
RW
1296 if (bgp_debug_zebra(p)) {
1297 char prefix_buf[PREFIX_STRLEN];
1298 char nh_buf[INET6_ADDRSTRLEN];
1299 char label_buf[20];
1300 int i;
1301
1302 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1303 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1304 " count %d",
1305 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1306 prefix_buf, api.metric, api.tag, api.nexthop_num);
1307 for (i = 0; i < api.nexthop_num; i++) {
1308 api_nh = &api.nexthops[i];
1309
d0d695f4
RW
1310 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1311 nh_family = AF_INET;
1312 else
1313 nh_family = AF_INET6;
2ad4f093
RW
1314 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1315 sizeof(nh_buf));
1316
1317 label_buf[0] = '\0';
a4d82a8a
PZ
1318 if (has_valid_label
1319 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
2ad4f093
RW
1320 sprintf(label_buf, "label %u",
1321 api_nh->labels[0]);
1322 zlog_debug(" nhop [%d]: %s %s", i + 1, nh_buf,
1323 label_buf);
d62a17ae 1324 }
1325 }
2ad4f093 1326
960035b2
PZ
1327 if (bgp_debug_zebra(p)) {
1328 int recursion_flag = 0;
1329
1330 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1331 recursion_flag = 1;
1332
1333 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1334 __func__, buf_prefix,
1335 (recursion_flag ? "" : "NOT "));
1336 }
2ad4f093
RW
1337 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1338 : ZEBRA_ROUTE_DELETE,
1339 zclient, &api);
718e3744 1340}
1341
73ac8160 1342/* Announce all routes of a table to zebra */
d62a17ae 1343void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
73ac8160 1344{
d62a17ae 1345 struct bgp_node *rn;
1346 struct bgp_table *table;
1347 struct bgp_info *ri;
1348
1349 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1350 * know of this instance.
1351 */
1352 if (!bgp_install_info_to_zebra(bgp))
1353 return;
1354
1355 table = bgp->rib[afi][safi];
1356 if (!table)
1357 return;
1358
1359 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1360 for (ri = rn->info; ri; ri = ri->next)
ddb5b488
PZ
1361 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) &&
1362
1363 (ri->type == ZEBRA_ROUTE_BGP
1364 && (ri->sub_type == BGP_ROUTE_NORMAL
1365 || ri->sub_type == BGP_ROUTE_IMPORTED)))
1366
d62a17ae 1367 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1368 safi);
73ac8160
DS
1369}
1370
568e10ca 1371void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info,
1372 struct bgp *bgp, safi_t safi)
718e3744 1373{
2ad4f093 1374 struct zapi_route api;
ddb5b488 1375
d62a17ae 1376 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1377 * know of this instance.
1378 */
568e10ca 1379 if (!bgp_install_info_to_zebra(bgp))
d62a17ae 1380 return;
1381
2ad4f093 1382 memset(&api, 0, sizeof(api));
01740ff4 1383 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
568e10ca 1384 api.vrf_id = bgp->vrf_id;
2ad4f093
RW
1385 api.type = ZEBRA_ROUTE_BGP;
1386 api.safi = safi;
1387 api.prefix = *p;
d62a17ae 1388
88493076 1389 /* If the route's source is EVPN, flag as such. */
1390 if (is_route_parent_evpn(info))
90264d64 1391 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
01740ff4 1392
2ad4f093
RW
1393 if (bgp_debug_zebra(p)) {
1394 char buf[PREFIX_STRLEN];
d62a17ae 1395
2ad4f093 1396 prefix2str(&api.prefix, buf, sizeof(buf));
568e10ca 1397 zlog_debug("Tx route delete VRF %u %s", bgp->vrf_id, buf);
d62a17ae 1398 }
d62a17ae 1399
2ad4f093 1400 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
718e3744 1401}
56c1f7d8 1402
d7c0a89a
QY
1403struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1404 unsigned short instance)
7c8ff89e 1405{
d62a17ae 1406 struct list *red_list;
1407 struct listnode *node;
1408 struct bgp_redist *red;
7c8ff89e 1409
d62a17ae 1410 red_list = bgp->redist[afi][type];
1411 if (!red_list)
1412 return (NULL);
7c8ff89e 1413
d62a17ae 1414 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1415 if (red->instance == instance)
1416 return red;
7c8ff89e 1417
d62a17ae 1418 return NULL;
7c8ff89e
DS
1419}
1420
d7c0a89a
QY
1421struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1422 unsigned short instance)
7c8ff89e 1423{
d62a17ae 1424 struct list *red_list;
1425 struct bgp_redist *red;
7c8ff89e 1426
d62a17ae 1427 red = bgp_redist_lookup(bgp, afi, type, instance);
1428 if (red)
1429 return red;
7c8ff89e 1430
d62a17ae 1431 if (!bgp->redist[afi][type])
1432 bgp->redist[afi][type] = list_new();
7c8ff89e 1433
d62a17ae 1434 red_list = bgp->redist[afi][type];
1435 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1436 sizeof(struct bgp_redist));
1437 red->instance = instance;
7c8ff89e 1438
d62a17ae 1439 listnode_add(red_list, red);
7c8ff89e 1440
d62a17ae 1441 return red;
7c8ff89e
DS
1442}
1443
d7c0a89a
QY
1444static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1445 unsigned short instance)
7c8ff89e 1446{
d62a17ae 1447 struct bgp_redist *red;
1448
1449 red = bgp_redist_lookup(bgp, afi, type, instance);
1450
1451 if (red) {
1452 listnode_delete(bgp->redist[afi][type], red);
1453 XFREE(MTYPE_BGP_REDIST, red);
acdf5e25
DS
1454 if (!bgp->redist[afi][type]->count)
1455 list_delete_and_null(&bgp->redist[afi][type]);
d62a17ae 1456 }
7c8ff89e 1457}
6b0655a2 1458
718e3744 1459/* Other routes redistribution into BGP. */
d7c0a89a
QY
1460int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1461 unsigned short instance)
718e3744 1462{
718e3744 1463
d62a17ae 1464 /* Return if already redistribute flag is set. */
1465 if (instance) {
1466 if (redist_check_instance(&zclient->mi_redist[afi][type],
1467 instance))
1468 return CMD_WARNING;
718e3744 1469
d62a17ae 1470 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1471 } else {
1472 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1473 return CMD_WARNING;
7076bb2f 1474
65efcfce 1475#if ENABLE_BGP_VNC
d62a17ae 1476 if (bgp->vrf_id == VRF_DEFAULT
1477 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1478 vnc_export_bgp_enable(
1479 bgp, afi); /* only enables if mode bits cfg'd */
1480 }
65efcfce
LB
1481#endif
1482
d62a17ae 1483 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1484 }
718e3744 1485
ea12cf11
DS
1486 /*
1487 * Don't try to register if we're not connected to Zebra or Zebra
1488 * doesn't know of this instance.
1489 *
1490 * When we come up later well resend if needed.
d62a17ae 1491 */
1492 if (!bgp_install_info_to_zebra(bgp))
ea12cf11 1493 return CMD_SUCCESS;
a39275d7 1494
d62a17ae 1495 if (BGP_DEBUG(zebra, ZEBRA))
1496 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1497 bgp->vrf_id, afi, zebra_route_string(type),
1498 instance);
518f0eb1 1499
d62a17ae 1500 /* Send distribute add message to zebra. */
1501 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1502 instance, bgp->vrf_id);
718e3744 1503
d62a17ae 1504 return CMD_SUCCESS;
718e3744 1505}
1506
d62a17ae 1507int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1508 unsigned short instance)
518f0eb1 1509{
d62a17ae 1510 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1511 * know of this instance.
1512 */
1513 if (!bgp_install_info_to_zebra(bgp))
1514 return -1;
1515
1516 if (BGP_DEBUG(zebra, ZEBRA))
1517 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1518 bgp->vrf_id, afi, zebra_route_string(type),
1519 instance);
1520
1521 /* Send distribute add message to zebra. */
1522 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1523 instance, bgp->vrf_id);
1524 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1525 instance, bgp->vrf_id);
1526
1527 return 0;
518f0eb1
DS
1528}
1529
718e3744 1530/* Redistribute with route-map specification. */
d62a17ae 1531int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
718e3744 1532{
d62a17ae 1533 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1534 return 0;
718e3744 1535
d62a17ae 1536 if (red->rmap.name)
1537 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1538 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1539 red->rmap.map = route_map_lookup_by_name(name);
718e3744 1540
d62a17ae 1541 return 1;
718e3744 1542}
1543
1544/* Redistribute with metric specification. */
d62a17ae 1545int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
d7c0a89a 1546 afi_t afi, int type, uint32_t metric)
718e3744 1547{
d62a17ae 1548 struct bgp_node *rn;
1549 struct bgp_info *ri;
1550
1551 if (red->redist_metric_flag && red->redist_metric == metric)
1552 return 0;
1553
1554 red->redist_metric_flag = 1;
1555 red->redist_metric = metric;
1556
1557 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1558 rn = bgp_route_next(rn)) {
1559 for (ri = rn->info; ri; ri = ri->next) {
1560 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1561 && ri->type == type
1562 && ri->instance == red->instance) {
1563 struct attr *old_attr;
1564 struct attr new_attr;
1565
1566 bgp_attr_dup(&new_attr, ri->attr);
1567 new_attr.med = red->redist_metric;
1568 old_attr = ri->attr;
1569 ri->attr = bgp_attr_intern(&new_attr);
1570 bgp_attr_unintern(&old_attr);
1571
1572 bgp_info_set_flag(rn, ri,
1573 BGP_INFO_ATTR_CHANGED);
1574 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1575 }
1576 }
1577 }
1578
1579 return 1;
718e3744 1580}
1581
1582/* Unset redistribution. */
d62a17ae 1583int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1584 unsigned short instance)
718e3744 1585{
d62a17ae 1586 struct bgp_redist *red;
1587
1588 red = bgp_redist_lookup(bgp, afi, type, instance);
1589 if (!red)
1590 return CMD_SUCCESS;
1591
1592 /* Return if zebra connection is disabled. */
1593 if (instance) {
1594 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1595 instance))
1596 return CMD_WARNING;
1597 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1598 } else {
1599 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1600 return CMD_WARNING;
1601 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1602 }
718e3744 1603
65efcfce 1604
d62a17ae 1605 if (bgp_install_info_to_zebra(bgp)) {
1606 /* Send distribute delete message to zebra. */
1607 if (BGP_DEBUG(zebra, ZEBRA))
1608 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1609 bgp->vrf_id, afi, zebra_route_string(type),
1610 instance);
1611 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1612 type, instance, bgp->vrf_id);
1613 }
1614
1615 /* Withdraw redistributed routes from current BGP's routing table. */
1616 bgp_redistribute_withdraw(bgp, afi, type, instance);
1617
1618 return CMD_SUCCESS;
718e3744 1619}
1620
6aeb9e78 1621/* Unset redistribution. */
d62a17ae 1622int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1623 unsigned short instance)
6aeb9e78 1624{
d62a17ae 1625 struct bgp_redist *red;
6aeb9e78 1626
ddb5b488
PZ
1627/*
1628 * vnc and vpn->vrf checks must be before red check because
1629 * they operate within bgpd irrespective of zebra connection
1630 * status. red lookup fails if there is no zebra connection.
1631 */
1632#if ENABLE_BGP_VNC
1633 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1634 vnc_export_bgp_disable(bgp, afi);
1635 }
1636#endif
ddb5b488 1637
d62a17ae 1638 red = bgp_redist_lookup(bgp, afi, type, instance);
1639 if (!red)
1640 return CMD_SUCCESS;
6aeb9e78 1641
d62a17ae 1642 bgp_redistribute_unreg(bgp, afi, type, instance);
6aeb9e78 1643
d62a17ae 1644 /* Unset route-map. */
1645 if (red->rmap.name)
1646 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1647 red->rmap.name = NULL;
1648 red->rmap.map = NULL;
6aeb9e78 1649
d62a17ae 1650 /* Unset metric. */
1651 red->redist_metric_flag = 0;
1652 red->redist_metric = 0;
6aeb9e78 1653
d62a17ae 1654 bgp_redist_del(bgp, afi, type, instance);
6aeb9e78 1655
d62a17ae 1656 return CMD_SUCCESS;
6aeb9e78
DS
1657}
1658
eb117f29
SK
1659/* Update redistribute vrf bitmap during triggers like
1660 restart networking or delete/add VRFs */
d62a17ae 1661void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
eb117f29 1662{
d62a17ae 1663 int i;
1664 afi_t afi;
1665
1666 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1667 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
48c74f88
DS
1668 if ((old_vrf_id == VRF_UNKNOWN)
1669 || vrf_bitmap_check(zclient->redist[afi][i],
1670 old_vrf_id)) {
d62a17ae 1671 vrf_bitmap_unset(zclient->redist[afi][i],
1672 old_vrf_id);
1673 vrf_bitmap_set(zclient->redist[afi][i],
1674 bgp->vrf_id);
1675 }
1676 return;
eb117f29
SK
1677}
1678
d62a17ae 1679void bgp_zclient_reset(void)
718e3744 1680{
d62a17ae 1681 zclient_reset(zclient);
718e3744 1682}
1683
ad4cbda1 1684/* Register this instance with Zebra. Invoked upon connect (for
1685 * default instance) and when other VRFs are learnt (or created and
1686 * already learnt).
1687 */
d62a17ae 1688void bgp_zebra_instance_register(struct bgp *bgp)
ad4cbda1 1689{
d62a17ae 1690 /* Don't try to register if we're not connected to Zebra */
1691 if (!zclient || zclient->sock < 0)
1692 return;
ad4cbda1 1693
d62a17ae 1694 if (BGP_DEBUG(zebra, ZEBRA))
1695 zlog_debug("Registering VRF %u", bgp->vrf_id);
ad4cbda1 1696
d62a17ae 1697 /* Register for router-id, interfaces, redistributed routes. */
1698 zclient_send_reg_requests(zclient, bgp->vrf_id);
7724c0a1 1699
d62a17ae 1700 /* For default instance, register to learn about VNIs, if appropriate.
1701 */
a4d82a8a 1702 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
d62a17ae 1703 bgp_zebra_advertise_all_vni(bgp, 1);
ad4cbda1 1704}
1705
1706/* Deregister this instance with Zebra. Invoked upon the instance
1707 * being deleted (default or VRF) and it is already registered.
1708 */
d62a17ae 1709void bgp_zebra_instance_deregister(struct bgp *bgp)
ad4cbda1 1710{
d62a17ae 1711 /* Don't try to deregister if we're not connected to Zebra */
1712 if (zclient->sock < 0)
1713 return;
ad4cbda1 1714
d62a17ae 1715 if (BGP_DEBUG(zebra, ZEBRA))
1716 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
ad4cbda1 1717
d62a17ae 1718 /* For default instance, unregister learning about VNIs, if appropriate.
1719 */
a4d82a8a 1720 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
d62a17ae 1721 bgp_zebra_advertise_all_vni(bgp, 0);
7724c0a1 1722
d62a17ae 1723 /* Deregister for router-id, interfaces, redistributed routes. */
1724 zclient_send_dereg_requests(zclient, bgp->vrf_id);
ad4cbda1 1725}
1726
d62a17ae 1727void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 1728{
d62a17ae 1729 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
5c81b96a 1730
d62a17ae 1731 /* Don't try to initiate if we're not connected to Zebra */
1732 if (zclient->sock < 0)
1733 return;
4a04e5f7 1734
d62a17ae 1735 if (BGP_DEBUG(zebra, ZEBRA))
1736 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1737 peer->host);
4a04e5f7 1738
d62a17ae 1739 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1740 ra_interval);
4a04e5f7 1741}
1742
d62a17ae 1743void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 1744{
d62a17ae 1745 /* Don't try to terminate if we're not connected to Zebra */
1746 if (zclient->sock < 0)
1747 return;
4a04e5f7 1748
d62a17ae 1749 if (BGP_DEBUG(zebra, ZEBRA))
1750 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1751 peer->host);
4a04e5f7 1752
d62a17ae 1753 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
4a04e5f7 1754}
1755
31310b25
MK
1756int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1757{
1758 struct stream *s = NULL;
1759
1760 /* Check socket. */
1761 if (!zclient || zclient->sock < 0)
1762 return 0;
1763
1764 /* Don't try to register if Zebra doesn't know of this instance. */
1765 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1766 return 0;
1767
1768 s = zclient->obuf;
1769 stream_reset(s);
1770
1771 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1772 stream_putc(s, advertise);
1773 stream_put3(s, vni);
1774 stream_putw_at(s, 0, stream_get_endp(s));
1775
1776 return zclient_send_message(zclient);
1777}
1778
1a98c087
MK
1779int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1780{
1781 struct stream *s = NULL;
1782
1783 /* Check socket. */
1784 if (!zclient || zclient->sock < 0)
1785 return 0;
1786
1787 /* Don't try to register if Zebra doesn't know of this instance. */
1788 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1789 return 0;
1790
1791 s = zclient->obuf;
1792 stream_reset(s);
1793
1794 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1795 stream_putc(s, advertise);
1796 stream_put3(s, vni);
1797 stream_putw_at(s, 0, stream_get_endp(s));
1798
1799 return zclient_send_message(zclient);
1800}
1801
d62a17ae 1802int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
7724c0a1 1803{
d62a17ae 1804 struct stream *s;
7724c0a1 1805
d62a17ae 1806 /* Check socket. */
1807 if (!zclient || zclient->sock < 0)
1808 return 0;
7724c0a1 1809
d62a17ae 1810 /* Don't try to register if Zebra doesn't know of this instance. */
1811 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1812 return 0;
7724c0a1 1813
d62a17ae 1814 s = zclient->obuf;
1815 stream_reset(s);
7724c0a1 1816
d62a17ae 1817 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1818 stream_putc(s, advertise);
1819 stream_putw_at(s, 0, stream_get_endp(s));
7724c0a1 1820
d62a17ae 1821 return zclient_send_message(zclient);
7724c0a1 1822}
1823
ad4cbda1 1824/* BGP has established connection with Zebra. */
d62a17ae 1825static void bgp_zebra_connected(struct zclient *zclient)
7076bb2f 1826{
d62a17ae 1827 struct bgp *bgp;
7076bb2f 1828
d62a17ae 1829 zclient_num_connects++; /* increment even if not responding */
afbb1c59 1830
d62a17ae 1831 /* At this point, we may or may not have BGP instances configured, but
1832 * we're only interested in the default VRF (others wouldn't have learnt
1833 * the VRF from Zebra yet.)
1834 */
1835 bgp = bgp_get_default();
1836 if (!bgp)
1837 return;
ad4cbda1 1838
d62a17ae 1839 bgp_zebra_instance_register(bgp);
ad4cbda1 1840
d62a17ae 1841 /* Send the client registration */
1842 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2376c3f2 1843
955bfd98 1844 /* tell label pool that zebra is connected */
e70e9f8e 1845 bgp_lp_event_zebra_up();
955bfd98 1846
d62a17ae 1847 /* TODO - What if we have peers and networks configured, do we have to
1848 * kick-start them?
1849 */
7076bb2f
FL
1850}
1851
fe1dc5a3
MK
1852static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
1853 zebra_size_t length, vrf_id_t vrf_id)
1854{
c48d9f5f 1855 int filter = 0;
fe1dc5a3 1856 char buf[ETHER_ADDR_STRLEN];
b67a60d2 1857 vni_t l3vni = 0;
fe1dc5a3 1858 struct ethaddr rmac;
b67a60d2 1859 struct in_addr originator_ip;
fe1dc5a3
MK
1860 struct stream *s;
1861
1862 memset(&rmac, 0, sizeof(struct ethaddr));
b67a60d2 1863 memset(&originator_ip, 0, sizeof(struct in_addr));
fe1dc5a3
MK
1864 s = zclient->ibuf;
1865 l3vni = stream_getl(s);
b67a60d2 1866 if (cmd == ZEBRA_L3VNI_ADD) {
fe1dc5a3 1867 stream_get(&rmac, s, sizeof(struct ethaddr));
b67a60d2 1868 originator_ip.s_addr = stream_get_ipv4(s);
c48d9f5f 1869 stream_get(&filter, s, sizeof(int));
b67a60d2 1870 }
fe1dc5a3
MK
1871
1872 if (BGP_DEBUG(zebra, ZEBRA))
c48d9f5f 1873 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
fe1dc5a3 1874 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
996c9314 1875 vrf_id_to_name(vrf_id), l3vni,
c48d9f5f
MK
1876 prefix_mac2str(&rmac, buf, sizeof(buf)),
1877 filter ? "prefix-routes-only" : "none");
fe1dc5a3
MK
1878
1879 if (cmd == ZEBRA_L3VNI_ADD)
c48d9f5f
MK
1880 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
1881 filter);
fe1dc5a3
MK
1882 else
1883 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
1884
1885 return 0;
1886}
1887
d62a17ae 1888static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
1889 zebra_size_t length, vrf_id_t vrf_id)
128ea8ab 1890{
d62a17ae 1891 struct stream *s;
1892 vni_t vni;
1893 struct bgp *bgp;
a4d82a8a 1894 struct in_addr vtep_ip = {INADDR_ANY};
29c53922 1895 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
d62a17ae 1896
1897 s = zclient->ibuf;
1898 vni = stream_getl(s);
29c53922 1899 if (command == ZEBRA_VNI_ADD) {
d62a17ae 1900 vtep_ip.s_addr = stream_get_ipv4(s);
29c53922
MK
1901 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
1902 }
1903
d62a17ae 1904 bgp = bgp_lookup_by_vrf_id(vrf_id);
1905 if (!bgp)
1906 return 0;
1907
1908 if (BGP_DEBUG(zebra, ZEBRA))
29c53922
MK
1909 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1910 (command == ZEBRA_VNI_ADD) ? "add" : "del",
a4d82a8a
PZ
1911 vrf_id_to_name(vrf_id), vni,
1912 vrf_id_to_name(tenant_vrf_id));
d62a17ae 1913
1914 if (command == ZEBRA_VNI_ADD)
1915 return bgp_evpn_local_vni_add(
29c53922
MK
1916 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
1917 tenant_vrf_id);
d62a17ae 1918 else
1919 return bgp_evpn_local_vni_del(bgp, vni);
128ea8ab 1920}
1921
d62a17ae 1922static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
1923 zebra_size_t length, vrf_id_t vrf_id)
128ea8ab 1924{
d62a17ae 1925 struct stream *s;
1926 vni_t vni;
1927 struct bgp *bgp;
1928 struct ethaddr mac;
1929 struct ipaddr ip;
1930 int ipa_len;
1931 char buf[ETHER_ADDR_STRLEN];
1932 char buf1[INET6_ADDRSTRLEN];
d7c0a89a 1933 uint8_t flags;
d62a17ae 1934
1935 memset(&ip, 0, sizeof(ip));
1936 s = zclient->ibuf;
1937 vni = stream_getl(s);
28328ea9 1938 stream_get(&mac.octet, s, ETH_ALEN);
d62a17ae 1939 ipa_len = stream_getl(s);
1940 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
1941 && ipa_len != IPV6_MAX_BYTELEN) {
1942 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1943 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1944 ipa_len);
1945 return -1;
1946 }
1947
1948 if (ipa_len) {
1949 ip.ipa_type =
1950 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
1951 stream_get(&ip.ip.addr, s, ipa_len);
1952 }
1a98c087 1953 flags = stream_getc(s);
d62a17ae 1954
1955 bgp = bgp_lookup_by_vrf_id(vrf_id);
1956 if (!bgp)
1957 return 0;
1958
1959 if (BGP_DEBUG(zebra, ZEBRA))
1a98c087
MK
1960 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1961 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1962 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
d62a17ae 1963 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
1964
1965 if (command == ZEBRA_MACIP_ADD)
1a98c087 1966 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, flags);
d62a17ae 1967 else
1968 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
128ea8ab 1969}
6aeb9e78 1970
a4d82a8a 1971static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
31310b25
MK
1972 zebra_size_t length,
1973 vrf_id_t vrf_id)
1974{
1975 struct stream *s = NULL;
1976 struct bgp *bgp_vrf = NULL;
1977 struct prefix p;
1978 char buf[PREFIX_STRLEN];
1979
1980 memset(&p, 0, sizeof(struct prefix));
1981 s = zclient->ibuf;
1982 stream_get(&p, s, sizeof(struct prefix));
1983
1984 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
1985 if (!bgp_vrf)
1986 return;
1987
1988 if (BGP_DEBUG(zebra, ZEBRA))
1989 zlog_debug("Recv prefix %s %s on vrf %s",
1990 prefix2str(&p, buf, sizeof(buf)),
1991 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
1992 vrf_id_to_name(vrf_id));
1993
1994 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
1995
1996 if (p.family == AF_INET)
a4d82a8a
PZ
1997 return bgp_evpn_advertise_type5_route(
1998 bgp_vrf, &p, NULL, AFI_IP, SAFI_UNICAST);
31310b25 1999 else
a4d82a8a
PZ
2000 return bgp_evpn_advertise_type5_route(
2001 bgp_vrf, &p, NULL, AFI_IP6, SAFI_UNICAST);
31310b25
MK
2002
2003 } else {
2004 if (p.family == AF_INET)
a4d82a8a
PZ
2005 return bgp_evpn_withdraw_type5_route(
2006 bgp_vrf, &p, AFI_IP, SAFI_UNICAST);
31310b25 2007 else
a4d82a8a
PZ
2008 return bgp_evpn_withdraw_type5_route(
2009 bgp_vrf, &p, AFI_IP6, SAFI_UNICAST);
31310b25
MK
2010 }
2011}
2012
955bfd98
PZ
2013static void bgp_zebra_process_label_chunk(
2014 int cmd,
2015 struct zclient *zclient,
2016 zebra_size_t length,
2017 vrf_id_t vrf_id)
2018{
2019 struct stream *s = NULL;
2020 uint8_t response_keep;
2021 uint32_t first;
2022 uint32_t last;
2023
2024 s = zclient->ibuf;
2025 STREAM_GETC(s, response_keep);
2026 STREAM_GETL(s, first);
2027 STREAM_GETL(s, last);
2028
2029 if (first > last ||
2030 first < MPLS_LABEL_UNRESERVED_MIN ||
2031 last > MPLS_LABEL_UNRESERVED_MAX) {
2032
2033 zlog_err("%s: Invalid Label chunk: %u - %u",
2034 __func__, first, last);
2035 return;
2036 }
2037 if (BGP_DEBUG(zebra, ZEBRA)) {
2038 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2039 first, last, response_keep);
2040 }
2041
e70e9f8e 2042 bgp_lp_event_chunk(response_keep, first, last);
955bfd98
PZ
2043
2044stream_failure: /* for STREAM_GETX */
2045 return;
2046}
2047
342213ea
DS
2048extern struct zebra_privs_t bgpd_privs;
2049
d62a17ae 2050void bgp_zebra_init(struct thread_master *master)
718e3744 2051{
d62a17ae 2052 zclient_num_connects = 0;
2053
2054 /* Set default values. */
e1a1880d 2055 zclient = zclient_new_notify(master, &zclient_options_default);
342213ea 2056 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
d62a17ae 2057 zclient->zebra_connected = bgp_zebra_connected;
2058 zclient->router_id_update = bgp_router_id_update;
2059 zclient->interface_add = bgp_interface_add;
2060 zclient->interface_delete = bgp_interface_delete;
2061 zclient->interface_address_add = bgp_interface_address_add;
2062 zclient->interface_address_delete = bgp_interface_address_delete;
2063 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2064 zclient->interface_nbr_address_delete =
2065 bgp_interface_nbr_address_delete;
2066 zclient->interface_vrf_update = bgp_interface_vrf_update;
74489921
RW
2067 zclient->redistribute_route_add = zebra_read_route;
2068 zclient->redistribute_route_del = zebra_read_route;
d62a17ae 2069 zclient->interface_up = bgp_interface_up;
2070 zclient->interface_down = bgp_interface_down;
d62a17ae 2071 zclient->nexthop_update = bgp_read_nexthop_update;
2072 zclient->import_check_update = bgp_read_import_check_update;
2073 zclient->fec_update = bgp_read_fec_update;
2074 zclient->local_vni_add = bgp_zebra_process_local_vni;
2075 zclient->local_vni_del = bgp_zebra_process_local_vni;
2076 zclient->local_macip_add = bgp_zebra_process_local_macip;
2077 zclient->local_macip_del = bgp_zebra_process_local_macip;
fe1dc5a3
MK
2078 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2079 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
31310b25
MK
2080 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2081 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
955bfd98 2082 zclient->label_chunk = bgp_zebra_process_label_chunk;
718e3744 2083}
bb86c601 2084
d62a17ae 2085void bgp_zebra_destroy(void)
bb86c601 2086{
d62a17ae 2087 if (zclient == NULL)
2088 return;
2089 zclient_stop(zclient);
2090 zclient_free(zclient);
2091 zclient = NULL;
bb86c601 2092}
afbb1c59 2093
d62a17ae 2094int bgp_zebra_num_connects(void)
afbb1c59 2095{
d62a17ae 2096 return zclient_num_connects;
afbb1c59 2097}