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