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