]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_zebra.c
bgpd: change priority of fs pbr rules
[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
d62a17ae 1684 if (red->rmap.name)
1685 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1686 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1de27621 1687 red->rmap.map = route_map;
718e3744 1688
d62a17ae 1689 return 1;
718e3744 1690}
1691
1692/* Redistribute with metric specification. */
d62a17ae 1693int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
d7c0a89a 1694 afi_t afi, int type, uint32_t metric)
718e3744 1695{
d62a17ae 1696 struct bgp_node *rn;
40381db7 1697 struct bgp_path_info *pi;
d62a17ae 1698
1699 if (red->redist_metric_flag && red->redist_metric == metric)
1700 return 0;
1701
1702 red->redist_metric_flag = 1;
1703 red->redist_metric = metric;
1704
1705 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1706 rn = bgp_route_next(rn)) {
6f94b685 1707 for (pi = bgp_node_get_bgp_path_info(rn); pi; pi = pi->next) {
40381db7
DS
1708 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1709 && pi->type == type
1710 && pi->instance == red->instance) {
d62a17ae 1711 struct attr *old_attr;
1712 struct attr new_attr;
1713
40381db7 1714 bgp_attr_dup(&new_attr, pi->attr);
d62a17ae 1715 new_attr.med = red->redist_metric;
40381db7
DS
1716 old_attr = pi->attr;
1717 pi->attr = bgp_attr_intern(&new_attr);
d62a17ae 1718 bgp_attr_unintern(&old_attr);
1719
40381db7 1720 bgp_path_info_set_flag(rn, pi,
18ee8310 1721 BGP_PATH_ATTR_CHANGED);
d62a17ae 1722 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1723 }
1724 }
1725 }
1726
1727 return 1;
718e3744 1728}
1729
1730/* Unset redistribution. */
d62a17ae 1731int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1732 unsigned short instance)
718e3744 1733{
d62a17ae 1734 struct bgp_redist *red;
1735
1736 red = bgp_redist_lookup(bgp, afi, type, instance);
1737 if (!red)
1738 return CMD_SUCCESS;
1739
1740 /* Return if zebra connection is disabled. */
1741 if (instance) {
1742 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1743 instance))
1744 return CMD_WARNING;
1745 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1746 } else {
1747 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1748 return CMD_WARNING;
1749 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1750 }
718e3744 1751
65efcfce 1752
d62a17ae 1753 if (bgp_install_info_to_zebra(bgp)) {
1754 /* Send distribute delete message to zebra. */
1755 if (BGP_DEBUG(zebra, ZEBRA))
1756 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1757 bgp->vrf_id, afi, zebra_route_string(type),
1758 instance);
1759 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1760 type, instance, bgp->vrf_id);
1761 }
1762
1763 /* Withdraw redistributed routes from current BGP's routing table. */
1764 bgp_redistribute_withdraw(bgp, afi, type, instance);
1765
1766 return CMD_SUCCESS;
718e3744 1767}
1768
6aeb9e78 1769/* Unset redistribution. */
d62a17ae 1770int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
d7c0a89a 1771 unsigned short instance)
6aeb9e78 1772{
d62a17ae 1773 struct bgp_redist *red;
6aeb9e78 1774
ddb5b488
PZ
1775/*
1776 * vnc and vpn->vrf checks must be before red check because
1777 * they operate within bgpd irrespective of zebra connection
1778 * status. red lookup fails if there is no zebra connection.
1779 */
1780#if ENABLE_BGP_VNC
1781 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1782 vnc_export_bgp_disable(bgp, afi);
1783 }
1784#endif
ddb5b488 1785
d62a17ae 1786 red = bgp_redist_lookup(bgp, afi, type, instance);
1787 if (!red)
1788 return CMD_SUCCESS;
6aeb9e78 1789
d62a17ae 1790 bgp_redistribute_unreg(bgp, afi, type, instance);
6aeb9e78 1791
d62a17ae 1792 /* Unset route-map. */
1793 if (red->rmap.name)
1794 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1795 red->rmap.name = NULL;
1796 red->rmap.map = NULL;
6aeb9e78 1797
d62a17ae 1798 /* Unset metric. */
1799 red->redist_metric_flag = 0;
1800 red->redist_metric = 0;
6aeb9e78 1801
d62a17ae 1802 bgp_redist_del(bgp, afi, type, instance);
6aeb9e78 1803
d62a17ae 1804 return CMD_SUCCESS;
6aeb9e78
DS
1805}
1806
eb117f29
SK
1807/* Update redistribute vrf bitmap during triggers like
1808 restart networking or delete/add VRFs */
d62a17ae 1809void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
eb117f29 1810{
d62a17ae 1811 int i;
1812 afi_t afi;
1813
1814 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1815 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
48c74f88
DS
1816 if ((old_vrf_id == VRF_UNKNOWN)
1817 || vrf_bitmap_check(zclient->redist[afi][i],
1818 old_vrf_id)) {
d62a17ae 1819 vrf_bitmap_unset(zclient->redist[afi][i],
1820 old_vrf_id);
1821 vrf_bitmap_set(zclient->redist[afi][i],
1822 bgp->vrf_id);
1823 }
1824 return;
eb117f29
SK
1825}
1826
d62a17ae 1827void bgp_zclient_reset(void)
718e3744 1828{
d62a17ae 1829 zclient_reset(zclient);
718e3744 1830}
1831
ad4cbda1 1832/* Register this instance with Zebra. Invoked upon connect (for
1833 * default instance) and when other VRFs are learnt (or created and
1834 * already learnt).
1835 */
d62a17ae 1836void bgp_zebra_instance_register(struct bgp *bgp)
ad4cbda1 1837{
d62a17ae 1838 /* Don't try to register if we're not connected to Zebra */
1839 if (!zclient || zclient->sock < 0)
1840 return;
ad4cbda1 1841
d62a17ae 1842 if (BGP_DEBUG(zebra, ZEBRA))
1843 zlog_debug("Registering VRF %u", bgp->vrf_id);
ad4cbda1 1844
d62a17ae 1845 /* Register for router-id, interfaces, redistributed routes. */
1846 zclient_send_reg_requests(zclient, bgp->vrf_id);
7724c0a1 1847
d62a17ae 1848 /* For default instance, register to learn about VNIs, if appropriate.
1849 */
a4d82a8a 1850 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
d62a17ae 1851 bgp_zebra_advertise_all_vni(bgp, 1);
1ee0a2df
DS
1852
1853 bgp_nht_register_nexthops(bgp);
ad4cbda1 1854}
1855
1856/* Deregister this instance with Zebra. Invoked upon the instance
1857 * being deleted (default or VRF) and it is already registered.
1858 */
d62a17ae 1859void bgp_zebra_instance_deregister(struct bgp *bgp)
ad4cbda1 1860{
d62a17ae 1861 /* Don't try to deregister if we're not connected to Zebra */
1862 if (zclient->sock < 0)
1863 return;
ad4cbda1 1864
d62a17ae 1865 if (BGP_DEBUG(zebra, ZEBRA))
1866 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
ad4cbda1 1867
d62a17ae 1868 /* For default instance, unregister learning about VNIs, if appropriate.
1869 */
a4d82a8a 1870 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
d62a17ae 1871 bgp_zebra_advertise_all_vni(bgp, 0);
7724c0a1 1872
d62a17ae 1873 /* Deregister for router-id, interfaces, redistributed routes. */
1874 zclient_send_dereg_requests(zclient, bgp->vrf_id);
ad4cbda1 1875}
1876
d62a17ae 1877void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 1878{
d62a17ae 1879 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
5c81b96a 1880
d62a17ae 1881 /* Don't try to initiate if we're not connected to Zebra */
1882 if (zclient->sock < 0)
1883 return;
4a04e5f7 1884
d62a17ae 1885 if (BGP_DEBUG(zebra, ZEBRA))
1886 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1887 peer->host);
4a04e5f7 1888
d62a17ae 1889 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1890 ra_interval);
4a04e5f7 1891}
1892
d62a17ae 1893void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
4a04e5f7 1894{
d62a17ae 1895 /* Don't try to terminate if we're not connected to Zebra */
1896 if (zclient->sock < 0)
1897 return;
4a04e5f7 1898
d62a17ae 1899 if (BGP_DEBUG(zebra, ZEBRA))
1900 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1901 peer->host);
4a04e5f7 1902
d62a17ae 1903 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
4a04e5f7 1904}
1905
31310b25
MK
1906int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1907{
1908 struct stream *s = NULL;
1909
1910 /* Check socket. */
1911 if (!zclient || zclient->sock < 0)
1912 return 0;
1913
1914 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
1915 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1916 if (BGP_DEBUG(zebra, ZEBRA))
1917 zlog_debug("%s: No zebra instance to talk to, cannot advertise subnet",
1918 __PRETTY_FUNCTION__);
31310b25 1919 return 0;
bb4ef1ae 1920 }
31310b25
MK
1921
1922 s = zclient->obuf;
1923 stream_reset(s);
1924
1925 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1926 stream_putc(s, advertise);
1927 stream_put3(s, vni);
1928 stream_putw_at(s, 0, stream_get_endp(s));
1929
1930 return zclient_send_message(zclient);
1931}
1932
1a98c087
MK
1933int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1934{
1935 struct stream *s = NULL;
1936
1937 /* Check socket. */
1938 if (!zclient || zclient->sock < 0)
1939 return 0;
1940
1941 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
1942 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1943 if (BGP_DEBUG(zebra, ZEBRA))
1944 zlog_debug("%s: No zebra instance to talk to, not installing gw_macip",
1945 __PRETTY_FUNCTION__);
1a98c087 1946 return 0;
bb4ef1ae 1947 }
1a98c087
MK
1948
1949 s = zclient->obuf;
1950 stream_reset(s);
1951
1952 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1953 stream_putc(s, advertise);
cc6d5476 1954 stream_putl(s, vni);
1a98c087
MK
1955 stream_putw_at(s, 0, stream_get_endp(s));
1956
1957 return zclient_send_message(zclient);
1958}
1959
fd069644
DS
1960int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
1961 enum vxlan_flood_control flood_ctrl)
1962{
1963 struct stream *s;
1964
1965 /* Check socket. */
1966 if (!zclient || zclient->sock < 0)
1967 return 0;
1968
1969 /* Don't try to register if Zebra doesn't know of this instance. */
bb4ef1ae
DS
1970 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1971 if (BGP_DEBUG(zebra, ZEBRA))
1972 zlog_debug("%s: No zebra instance to talk to, not installing all vni",
1973 __PRETTY_FUNCTION__);
fd069644 1974 return 0;
bb4ef1ae 1975 }
fd069644
DS
1976
1977 s = zclient->obuf;
1978 stream_reset(s);
1979
1980 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
1981 stream_putc(s, flood_ctrl);
1982 stream_putw_at(s, 0, stream_get_endp(s));
1983
1984 return zclient_send_message(zclient);
1985}
1986
d62a17ae 1987int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
7724c0a1 1988{
d62a17ae 1989 struct stream *s;
7724c0a1 1990
d62a17ae 1991 /* Check socket. */
1992 if (!zclient || zclient->sock < 0)
1993 return 0;
7724c0a1 1994
d62a17ae 1995 /* Don't try to register if Zebra doesn't know of this instance. */
1996 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1997 return 0;
7724c0a1 1998
d62a17ae 1999 s = zclient->obuf;
2000 stream_reset(s);
7724c0a1 2001
d62a17ae 2002 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2003 stream_putc(s, advertise);
fbac9605
DS
2004 /* Also inform current BUM handling setting. This is really
2005 * relevant only when 'advertise' is set.
2006 */
fd069644 2007 stream_putc(s, bgp->vxlan_flood_ctrl);
d62a17ae 2008 stream_putw_at(s, 0, stream_get_endp(s));
7724c0a1 2009
d62a17ae 2010 return zclient_send_message(zclient);
7724c0a1 2011}
2012
0b9d9cd0
CS
2013int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2014{
2015 struct stream *s;
2016
2017 /* Check socket. */
2018 if (!zclient || zclient->sock < 0)
2019 return 0;
2020
2021 /* Don't try to register if Zebra doesn't know of this instance. */
2022 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2023 return 0;
2024
2025 if (BGP_DEBUG(zebra, ZEBRA))
2026 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2027 bgp->evpn_info->dup_addr_detect ?
2028 "enable" : "disable",
2029 bgp->evpn_info->dad_max_moves,
2030 bgp->evpn_info->dad_time,
2031 bgp->evpn_info->dad_freeze ?
2032 "enable" : "disable",
2033 bgp->evpn_info->dad_freeze_time);
2034
2035 s = zclient->obuf;
2036 stream_reset(s);
2037 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2038 bgp->vrf_id);
2039 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2040 stream_putl(s, bgp->evpn_info->dad_time);
2041 stream_putl(s, bgp->evpn_info->dad_max_moves);
2042 stream_putl(s, bgp->evpn_info->dad_freeze);
2043 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2044 stream_putw_at(s, 0, stream_get_endp(s));
2045
2046 return zclient_send_message(zclient);
2047}
2048
30d50e6d
PG
2049static int rule_notify_owner(int command, struct zclient *zclient,
2050 zebra_size_t length, vrf_id_t vrf_id)
2051{
2052 uint32_t seqno, priority, unique;
2053 enum zapi_rule_notify_owner note;
2054 struct bgp_pbr_action *bgp_pbra;
ffee150e 2055 struct bgp_pbr_rule *bgp_pbr = NULL;
30d50e6d
PG
2056 ifindex_t ifi;
2057
2058 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2059 &ifi, &note))
2060 return -1;
2061
70eabd12 2062 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
30d50e6d 2063 if (!bgp_pbra) {
ffee150e
PG
2064 /* look in bgp pbr rule */
2065 bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2066 if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2067 if (BGP_DEBUG(zebra, ZEBRA))
2068 zlog_debug("%s: Fail to look BGP rule (%u)",
2069 __PRETTY_FUNCTION__, unique);
2070 return 0;
2071 }
30d50e6d
PG
2072 }
2073
2074 switch (note) {
2075 case ZAPI_RULE_FAIL_INSTALL:
2076 if (BGP_DEBUG(zebra, ZEBRA))
2077 zlog_debug("%s: Received RULE_FAIL_INSTALL",
2078 __PRETTY_FUNCTION__);
ffee150e
PG
2079 if (bgp_pbra) {
2080 bgp_pbra->installed = false;
2081 bgp_pbra->install_in_progress = false;
2082 } else {
2083 bgp_pbr->installed = false;
2084 bgp_pbr->install_in_progress = false;
2085 }
30d50e6d
PG
2086 break;
2087 case ZAPI_RULE_INSTALLED:
ffee150e
PG
2088 if (bgp_pbra) {
2089 bgp_pbra->installed = true;
2090 bgp_pbra->install_in_progress = false;
2091 } else {
2092 bgp_pbr->installed = true;
2093 bgp_pbr->install_in_progress = false;
2094 bgp_pbr->action->refcnt++;
2095 }
30d50e6d
PG
2096 if (BGP_DEBUG(zebra, ZEBRA))
2097 zlog_debug("%s: Received RULE_INSTALLED",
2098 __PRETTY_FUNCTION__);
2099 break;
f18a08f5 2100 case ZAPI_RULE_FAIL_REMOVE:
30d50e6d
PG
2101 case ZAPI_RULE_REMOVED:
2102 if (BGP_DEBUG(zebra, ZEBRA))
2103 zlog_debug("%s: Received RULE REMOVED",
2104 __PRETTY_FUNCTION__);
2105 break;
2106 }
2107
2108 return 0;
2109}
2110
2111static int ipset_notify_owner(int command, struct zclient *zclient,
2112 zebra_size_t length, vrf_id_t vrf_id)
2113{
2114 uint32_t unique;
2115 enum zapi_ipset_notify_owner note;
2116 struct bgp_pbr_match *bgp_pbim;
2117
2118 if (!zapi_ipset_notify_decode(zclient->ibuf,
2119 &unique,
2120 &note))
2121 return -1;
2122
2123 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2124 if (!bgp_pbim) {
2125 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2126 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
82e194ed 2127 __PRETTY_FUNCTION__, note, unique);
30d50e6d
PG
2128 return 0;
2129 }
2130
2131 switch (note) {
2132 case ZAPI_IPSET_FAIL_INSTALL:
2133 if (BGP_DEBUG(zebra, ZEBRA))
2134 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
2135 __PRETTY_FUNCTION__);
2136 bgp_pbim->installed = false;
2137 bgp_pbim->install_in_progress = false;
2138 break;
2139 case ZAPI_IPSET_INSTALLED:
2140 bgp_pbim->installed = true;
2141 bgp_pbim->install_in_progress = false;
2142 if (BGP_DEBUG(zebra, ZEBRA))
2143 zlog_debug("%s: Received IPSET_INSTALLED",
2144 __PRETTY_FUNCTION__);
2145 break;
f18a08f5 2146 case ZAPI_IPSET_FAIL_REMOVE:
30d50e6d
PG
2147 case ZAPI_IPSET_REMOVED:
2148 if (BGP_DEBUG(zebra, ZEBRA))
2149 zlog_debug("%s: Received IPSET REMOVED",
2150 __PRETTY_FUNCTION__);
2151 break;
2152 }
2153
2154 return 0;
2155}
2156
2157static int ipset_entry_notify_owner(int command, struct zclient *zclient,
2158 zebra_size_t length, vrf_id_t vrf_id)
2159{
2160 uint32_t unique;
2161 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2162 enum zapi_ipset_entry_notify_owner note;
2163 struct bgp_pbr_match_entry *bgp_pbime;
2164
2165 if (!zapi_ipset_entry_notify_decode(
2166 zclient->ibuf,
2167 &unique,
2168 ipset_name,
2169 &note))
2170 return -1;
2171 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2172 ipset_name,
2173 unique);
2174 if (!bgp_pbime) {
2175 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2176 zlog_debug("%s: Fail to look BGP match entry (%u, ID %u)",
82e194ed 2177 __PRETTY_FUNCTION__, note, unique);
30d50e6d
PG
2178 return 0;
2179 }
2180
2181 switch (note) {
2182 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2183 if (BGP_DEBUG(zebra, ZEBRA))
2184 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2185 __PRETTY_FUNCTION__);
2186 bgp_pbime->installed = false;
2187 bgp_pbime->install_in_progress = false;
2188 break;
2189 case ZAPI_IPSET_ENTRY_INSTALLED:
b588b642 2190 {
9b6d8fcf 2191 struct bgp_path_info *path;
4b7e6066
DS
2192 struct bgp_path_info_extra *extra;
2193
2194 bgp_pbime->installed = true;
2195 bgp_pbime->install_in_progress = false;
2196 if (BGP_DEBUG(zebra, ZEBRA))
2197 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2198 __PRETTY_FUNCTION__);
9b6d8fcf
DS
2199 /* link bgp_path_info to bpme */
2200 path = (struct bgp_path_info *)bgp_pbime->path;
2201 extra = bgp_path_info_extra_get(path);
4b7e6066
DS
2202 if (extra->bgp_fs_pbr == NULL)
2203 extra->bgp_fs_pbr = list_new();
2204 listnode_add(extra->bgp_fs_pbr, bgp_pbime);
b588b642 2205 }
30d50e6d 2206 break;
f18a08f5 2207 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
30d50e6d
PG
2208 case ZAPI_IPSET_ENTRY_REMOVED:
2209 if (BGP_DEBUG(zebra, ZEBRA))
2210 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2211 __PRETTY_FUNCTION__);
2212 break;
2213 }
2214 return 0;
2215}
2216
c16a0a62
PG
2217static int iptable_notify_owner(int command, struct zclient *zclient,
2218 zebra_size_t length, vrf_id_t vrf_id)
2219{
2220 uint32_t unique;
2221 enum zapi_iptable_notify_owner note;
2222 struct bgp_pbr_match *bgpm;
2223
2224 if (!zapi_iptable_notify_decode(
2225 zclient->ibuf,
2226 &unique,
2227 &note))
2228 return -1;
2229 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2230 if (!bgpm) {
2231 if (BGP_DEBUG(zebra, ZEBRA))
82e194ed
PG
2232 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2233 __PRETTY_FUNCTION__, note, unique);
c16a0a62
PG
2234 return 0;
2235 }
2236 switch (note) {
2237 case ZAPI_IPTABLE_FAIL_INSTALL:
2238 if (BGP_DEBUG(zebra, ZEBRA))
2239 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2240 __PRETTY_FUNCTION__);
2241 bgpm->installed_in_iptable = false;
2242 bgpm->install_iptable_in_progress = false;
2243 break;
2244 case ZAPI_IPTABLE_INSTALLED:
2245 bgpm->installed_in_iptable = true;
2246 bgpm->install_iptable_in_progress = false;
2247 if (BGP_DEBUG(zebra, ZEBRA))
2248 zlog_debug("%s: Received IPTABLE_INSTALLED",
2249 __PRETTY_FUNCTION__);
a6b07429 2250 bgpm->action->refcnt++;
c16a0a62 2251 break;
f18a08f5 2252 case ZAPI_IPTABLE_FAIL_REMOVE:
c16a0a62
PG
2253 case ZAPI_IPTABLE_REMOVED:
2254 if (BGP_DEBUG(zebra, ZEBRA))
2255 zlog_debug("%s: Received IPTABLE REMOVED",
2256 __PRETTY_FUNCTION__);
2257 break;
2258 }
2259 return 0;
2260}
2261
6cfe5d15
PG
2262/* this function is used to forge ip rule,
2263 * - either for iptable/ipset using fwmark id
2264 * - or for sample ip rule command
2265 */
30d50e6d 2266static void bgp_encode_pbr_rule_action(struct stream *s,
6cfe5d15
PG
2267 struct bgp_pbr_action *pbra,
2268 struct bgp_pbr_rule *pbr)
30d50e6d 2269{
6cfe5d15 2270 struct prefix pfx;
30d50e6d
PG
2271
2272 stream_putl(s, 0); /* seqno unused */
8112a7a0
PG
2273 if (pbr)
2274 stream_putl(s, pbr->priority);
2275 else
2276 stream_putl(s, 0);
2277 /* ruleno unused - priority change
2278 * ruleno permits distinguishing various FS PBR entries
2279 * - FS PBR entries based on ipset/iptables
2280 * - FS PBR entries based on iprule
2281 * the latter may contain default routing information injected by FS
2282 */
6cfe5d15
PG
2283 if (pbr)
2284 stream_putl(s, pbr->unique);
2285 else
2286 stream_putl(s, pbra->unique);
2287 if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2288 memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2289 else {
2290 memset(&pfx, 0, sizeof(pfx));
2291 pfx.family = AF_INET;
2292 }
2293 stream_putc(s, pfx.family);
2294 stream_putc(s, pfx.prefixlen);
2295 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
30d50e6d
PG
2296
2297 stream_putw(s, 0); /* src port */
2298
6cfe5d15
PG
2299 if (pbr && pbr->flags & MATCH_IP_DST_SET)
2300 memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2301 else {
2302 memset(&pfx, 0, sizeof(pfx));
2303 pfx.family = AF_INET;
2304 }
2305 stream_putc(s, pfx.family);
2306 stream_putc(s, pfx.prefixlen);
2307 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
30d50e6d
PG
2308
2309 stream_putw(s, 0); /* dst port */
2310
6cfe5d15
PG
2311 /* if pbr present, fwmark is not used */
2312 if (pbr)
2313 stream_putl(s, 0);
2314 else
2315 stream_putl(s, pbra->fwmark); /* fwmark */
30d50e6d
PG
2316
2317 stream_putl(s, pbra->table_id);
2318
2319 stream_putl(s, 0); /* ifindex unused */
2320}
2321
2322static void bgp_encode_pbr_ipset_match(struct stream *s,
2323 struct bgp_pbr_match *pbim)
2324{
2325 stream_putl(s, pbim->unique);
2326 stream_putl(s, pbim->type);
2327
2328 stream_put(s, pbim->ipset_name,
2329 ZEBRA_IPSET_NAME_SIZE);
30d50e6d
PG
2330}
2331
2332static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2333 struct bgp_pbr_match_entry *pbime)
2334{
2335 stream_putl(s, pbime->unique);
2336 /* check that back pointer is not null */
2337 stream_put(s, pbime->backpointer->ipset_name,
2338 ZEBRA_IPSET_NAME_SIZE);
2339
2340 stream_putc(s, pbime->src.family);
2341 stream_putc(s, pbime->src.prefixlen);
2342 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2343
2344 stream_putc(s, pbime->dst.family);
2345 stream_putc(s, pbime->dst.prefixlen);
2346 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
f730e566
PG
2347
2348 stream_putw(s, pbime->src_port_min);
2349 stream_putw(s, pbime->src_port_max);
2350 stream_putw(s, pbime->dst_port_min);
2351 stream_putw(s, pbime->dst_port_max);
2352 stream_putc(s, pbime->proto);
30d50e6d
PG
2353}
2354
c16a0a62
PG
2355static void bgp_encode_pbr_iptable_match(struct stream *s,
2356 struct bgp_pbr_action *bpa,
2357 struct bgp_pbr_match *pbm)
2358{
2359 stream_putl(s, pbm->unique2);
2360
2361 stream_putl(s, pbm->type);
2362
2363 stream_putl(s, pbm->flags);
2364
2365 /* TODO: correlate with what is contained
2366 * into bgp_pbr_action.
2367 * currently only forward supported
2368 */
2369 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2370 stream_putl(s, ZEBRA_IPTABLES_DROP);
2371 else
2372 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2373 stream_putl(s, bpa->fwmark);
2374 stream_put(s, pbm->ipset_name,
2375 ZEBRA_IPSET_NAME_SIZE);
83360720
PG
2376 stream_putw(s, pbm->pkt_len_min);
2377 stream_putw(s, pbm->pkt_len_max);
2da7d62e
PG
2378 stream_putw(s, pbm->tcp_flags);
2379 stream_putw(s, pbm->tcp_mask_flags);
4977bd6c 2380 stream_putc(s, pbm->dscp_value);
6f5617d8 2381 stream_putc(s, pbm->fragment);
c16a0a62
PG
2382}
2383
ad4cbda1 2384/* BGP has established connection with Zebra. */
d62a17ae 2385static void bgp_zebra_connected(struct zclient *zclient)
7076bb2f 2386{
d62a17ae 2387 struct bgp *bgp;
7076bb2f 2388
d62a17ae 2389 zclient_num_connects++; /* increment even if not responding */
afbb1c59 2390
d62a17ae 2391 /* At this point, we may or may not have BGP instances configured, but
2392 * we're only interested in the default VRF (others wouldn't have learnt
2393 * the VRF from Zebra yet.)
2394 */
2395 bgp = bgp_get_default();
2396 if (!bgp)
2397 return;
ad4cbda1 2398
d62a17ae 2399 bgp_zebra_instance_register(bgp);
ad4cbda1 2400
d62a17ae 2401 /* Send the client registration */
2402 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2376c3f2 2403
955bfd98 2404 /* tell label pool that zebra is connected */
e70e9f8e 2405 bgp_lp_event_zebra_up();
955bfd98 2406
d62a17ae 2407 /* TODO - What if we have peers and networks configured, do we have to
2408 * kick-start them?
2409 */
7076bb2f
FL
2410}
2411
50f74cf1 2412static int bgp_zebra_process_local_es(int cmd, struct zclient *zclient,
2413 zebra_size_t length, vrf_id_t vrf_id)
2414{
2415 esi_t esi;
2416 struct bgp *bgp = NULL;
2417 struct stream *s = NULL;
2418 char buf[ESI_STR_LEN];
2419 char buf1[INET6_ADDRSTRLEN];
2420 struct ipaddr originator_ip;
2421
2422 memset(&esi, 0, sizeof(esi_t));
2423 memset(&originator_ip, 0, sizeof(struct ipaddr));
2424
2425 bgp = bgp_lookup_by_vrf_id(vrf_id);
2426 if (!bgp)
2427 return 0;
2428
2429 s = zclient->ibuf;
2430 stream_get(&esi, s, sizeof(esi_t));
2431 stream_get(&originator_ip, s, sizeof(struct ipaddr));
2432
2433 if (BGP_DEBUG(zebra, ZEBRA))
2434 zlog_debug("Rx %s ESI %s originator-ip %s",
2435 (cmd == ZEBRA_LOCAL_ES_ADD) ? "add" : "del",
2436 esi_to_str(&esi, buf, sizeof(buf)),
2437 ipaddr2str(&originator_ip, buf1, sizeof(buf1)));
2438
2439 if (cmd == ZEBRA_LOCAL_ES_ADD)
2440 bgp_evpn_local_es_add(bgp, &esi, &originator_ip);
2441 else
2442 bgp_evpn_local_es_del(bgp, &esi, &originator_ip);
2443 return 0;
2444}
2445
fe1dc5a3
MK
2446static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
2447 zebra_size_t length, vrf_id_t vrf_id)
2448{
c48d9f5f 2449 int filter = 0;
fe1dc5a3 2450 char buf[ETHER_ADDR_STRLEN];
b67a60d2 2451 vni_t l3vni = 0;
fe1dc5a3 2452 struct ethaddr rmac;
b67a60d2 2453 struct in_addr originator_ip;
fe1dc5a3
MK
2454 struct stream *s;
2455
2456 memset(&rmac, 0, sizeof(struct ethaddr));
b67a60d2 2457 memset(&originator_ip, 0, sizeof(struct in_addr));
fe1dc5a3
MK
2458 s = zclient->ibuf;
2459 l3vni = stream_getl(s);
b67a60d2 2460 if (cmd == ZEBRA_L3VNI_ADD) {
fe1dc5a3 2461 stream_get(&rmac, s, sizeof(struct ethaddr));
b67a60d2 2462 originator_ip.s_addr = stream_get_ipv4(s);
c48d9f5f 2463 stream_get(&filter, s, sizeof(int));
b67a60d2 2464 }
fe1dc5a3
MK
2465
2466 if (BGP_DEBUG(zebra, ZEBRA))
c48d9f5f 2467 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
fe1dc5a3 2468 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
996c9314 2469 vrf_id_to_name(vrf_id), l3vni,
c48d9f5f
MK
2470 prefix_mac2str(&rmac, buf, sizeof(buf)),
2471 filter ? "prefix-routes-only" : "none");
fe1dc5a3
MK
2472
2473 if (cmd == ZEBRA_L3VNI_ADD)
c48d9f5f
MK
2474 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
2475 filter);
fe1dc5a3
MK
2476 else
2477 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
2478
2479 return 0;
2480}
2481
d62a17ae 2482static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
2483 zebra_size_t length, vrf_id_t vrf_id)
128ea8ab 2484{
d62a17ae 2485 struct stream *s;
2486 vni_t vni;
2487 struct bgp *bgp;
a4d82a8a 2488 struct in_addr vtep_ip = {INADDR_ANY};
29c53922 2489 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
d62a17ae 2490
2491 s = zclient->ibuf;
2492 vni = stream_getl(s);
29c53922 2493 if (command == ZEBRA_VNI_ADD) {
d62a17ae 2494 vtep_ip.s_addr = stream_get_ipv4(s);
29c53922
MK
2495 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
2496 }
2497
d62a17ae 2498 bgp = bgp_lookup_by_vrf_id(vrf_id);
2499 if (!bgp)
2500 return 0;
2501
2502 if (BGP_DEBUG(zebra, ZEBRA))
29c53922
MK
2503 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2504 (command == ZEBRA_VNI_ADD) ? "add" : "del",
a4d82a8a
PZ
2505 vrf_id_to_name(vrf_id), vni,
2506 vrf_id_to_name(tenant_vrf_id));
d62a17ae 2507
2508 if (command == ZEBRA_VNI_ADD)
2509 return bgp_evpn_local_vni_add(
29c53922
MK
2510 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
2511 tenant_vrf_id);
d62a17ae 2512 else
2513 return bgp_evpn_local_vni_del(bgp, vni);
128ea8ab 2514}
2515
d62a17ae 2516static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
2517 zebra_size_t length, vrf_id_t vrf_id)
128ea8ab 2518{
d62a17ae 2519 struct stream *s;
2520 vni_t vni;
2521 struct bgp *bgp;
2522 struct ethaddr mac;
2523 struct ipaddr ip;
2524 int ipa_len;
2525 char buf[ETHER_ADDR_STRLEN];
2526 char buf1[INET6_ADDRSTRLEN];
f07e1c99 2527 uint8_t flags = 0;
2528 uint32_t seqnum = 0;
d62a17ae 2529
2530 memset(&ip, 0, sizeof(ip));
2531 s = zclient->ibuf;
2532 vni = stream_getl(s);
28328ea9 2533 stream_get(&mac.octet, s, ETH_ALEN);
d62a17ae 2534 ipa_len = stream_getl(s);
2535 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2536 && ipa_len != IPV6_MAX_BYTELEN) {
e50f7cfd 2537 flog_err(EC_BGP_MACIP_LEN,
1c50c1c0
QY
2538 "%u:Recv MACIP %s with invalid IP addr length %d",
2539 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2540 ipa_len);
d62a17ae 2541 return -1;
2542 }
2543
2544 if (ipa_len) {
2545 ip.ipa_type =
2546 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
2547 stream_get(&ip.ip.addr, s, ipa_len);
2548 }
f07e1c99 2549 if (command == ZEBRA_MACIP_ADD) {
2550 flags = stream_getc(s);
2551 seqnum = stream_getl(s);
2552 }
d62a17ae 2553
2554 bgp = bgp_lookup_by_vrf_id(vrf_id);
2555 if (!bgp)
2556 return 0;
2557
2558 if (BGP_DEBUG(zebra, ZEBRA))
f07e1c99 2559 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u seq %u",
1a98c087
MK
2560 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2561 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
f07e1c99 2562 ipaddr2str(&ip, buf1, sizeof(buf1)), vni, seqnum);
d62a17ae 2563
2564 if (command == ZEBRA_MACIP_ADD)
f07e1c99 2565 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
2566 flags, seqnum);
d62a17ae 2567 else
2568 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
128ea8ab 2569}
6aeb9e78 2570
a4d82a8a 2571static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
31310b25
MK
2572 zebra_size_t length,
2573 vrf_id_t vrf_id)
2574{
2575 struct stream *s = NULL;
2576 struct bgp *bgp_vrf = NULL;
2577 struct prefix p;
2578 char buf[PREFIX_STRLEN];
2579
2580 memset(&p, 0, sizeof(struct prefix));
2581 s = zclient->ibuf;
2582 stream_get(&p, s, sizeof(struct prefix));
2583
2584 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
2585 if (!bgp_vrf)
2586 return;
2587
2588 if (BGP_DEBUG(zebra, ZEBRA))
2589 zlog_debug("Recv prefix %s %s on vrf %s",
2590 prefix2str(&p, buf, sizeof(buf)),
2591 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
2592 vrf_id_to_name(vrf_id));
2593
2594 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
2595
2596 if (p.family == AF_INET)
d90b788e
A
2597 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
2598 AFI_IP, SAFI_UNICAST);
31310b25 2599 else
d90b788e
A
2600 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
2601 AFI_IP6, SAFI_UNICAST);
31310b25
MK
2602
2603 } else {
2604 if (p.family == AF_INET)
d90b788e
A
2605 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
2606 SAFI_UNICAST);
31310b25 2607 else
d90b788e
A
2608 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
2609 SAFI_UNICAST);
31310b25
MK
2610 }
2611}
2612
955bfd98
PZ
2613static void bgp_zebra_process_label_chunk(
2614 int cmd,
2615 struct zclient *zclient,
2616 zebra_size_t length,
2617 vrf_id_t vrf_id)
2618{
2619 struct stream *s = NULL;
2620 uint8_t response_keep;
2621 uint32_t first;
2622 uint32_t last;
aec865e4
FR
2623 uint8_t proto;
2624 unsigned short instance;
955bfd98
PZ
2625
2626 s = zclient->ibuf;
aec865e4
FR
2627 STREAM_GETC(s, proto);
2628 STREAM_GETW(s, instance);
955bfd98
PZ
2629 STREAM_GETC(s, response_keep);
2630 STREAM_GETL(s, first);
2631 STREAM_GETL(s, last);
2632
aec865e4 2633 if (zclient->redist_default != proto) {
e50f7cfd 2634 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
1c50c1c0 2635 proto);
aec865e4
FR
2636 return;
2637 }
2638 if (zclient->instance != instance) {
e50f7cfd 2639 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
1c50c1c0 2640 proto);
aec865e4
FR
2641 return;
2642 }
2643
955bfd98
PZ
2644 if (first > last ||
2645 first < MPLS_LABEL_UNRESERVED_MIN ||
2646 last > MPLS_LABEL_UNRESERVED_MAX) {
2647
e50f7cfd 2648 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
1c50c1c0 2649 __func__, first, last);
955bfd98
PZ
2650 return;
2651 }
2652 if (BGP_DEBUG(zebra, ZEBRA)) {
2653 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2654 first, last, response_keep);
2655 }
2656
e70e9f8e 2657 bgp_lp_event_chunk(response_keep, first, last);
955bfd98
PZ
2658
2659stream_failure: /* for STREAM_GETX */
2660 return;
2661}
2662
342213ea
DS
2663extern struct zebra_privs_t bgpd_privs;
2664
f533be73 2665void bgp_zebra_init(struct thread_master *master, unsigned short instance)
718e3744 2666{
d62a17ae 2667 zclient_num_connects = 0;
2668
2669 /* Set default values. */
26f63a1e 2670 zclient = zclient_new(master, &zclient_options_default);
342213ea 2671 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
d62a17ae 2672 zclient->zebra_connected = bgp_zebra_connected;
2673 zclient->router_id_update = bgp_router_id_update;
2674 zclient->interface_add = bgp_interface_add;
2675 zclient->interface_delete = bgp_interface_delete;
2676 zclient->interface_address_add = bgp_interface_address_add;
2677 zclient->interface_address_delete = bgp_interface_address_delete;
2678 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2679 zclient->interface_nbr_address_delete =
2680 bgp_interface_nbr_address_delete;
2681 zclient->interface_vrf_update = bgp_interface_vrf_update;
74489921
RW
2682 zclient->redistribute_route_add = zebra_read_route;
2683 zclient->redistribute_route_del = zebra_read_route;
d62a17ae 2684 zclient->interface_up = bgp_interface_up;
2685 zclient->interface_down = bgp_interface_down;
d62a17ae 2686 zclient->nexthop_update = bgp_read_nexthop_update;
2687 zclient->import_check_update = bgp_read_import_check_update;
2688 zclient->fec_update = bgp_read_fec_update;
50f74cf1 2689 zclient->local_es_add = bgp_zebra_process_local_es;
2690 zclient->local_es_del = bgp_zebra_process_local_es;
d62a17ae 2691 zclient->local_vni_add = bgp_zebra_process_local_vni;
2692 zclient->local_vni_del = bgp_zebra_process_local_vni;
2693 zclient->local_macip_add = bgp_zebra_process_local_macip;
2694 zclient->local_macip_del = bgp_zebra_process_local_macip;
fe1dc5a3
MK
2695 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2696 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
31310b25
MK
2697 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2698 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
955bfd98 2699 zclient->label_chunk = bgp_zebra_process_label_chunk;
30d50e6d
PG
2700 zclient->rule_notify_owner = rule_notify_owner;
2701 zclient->ipset_notify_owner = ipset_notify_owner;
2702 zclient->ipset_entry_notify_owner = ipset_entry_notify_owner;
c16a0a62 2703 zclient->iptable_notify_owner = iptable_notify_owner;
f533be73 2704 zclient->instance = instance;
718e3744 2705}
bb86c601 2706
d62a17ae 2707void bgp_zebra_destroy(void)
bb86c601 2708{
d62a17ae 2709 if (zclient == NULL)
2710 return;
2711 zclient_stop(zclient);
2712 zclient_free(zclient);
2713 zclient = NULL;
bb86c601 2714}
afbb1c59 2715
d62a17ae 2716int bgp_zebra_num_connects(void)
afbb1c59 2717{
d62a17ae 2718 return zclient_num_connects;
afbb1c59 2719}
30d50e6d 2720
6cfe5d15
PG
2721void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
2722 struct bgp_pbr_rule *pbr,
2723 bool install)
30d50e6d
PG
2724{
2725 struct stream *s;
2726
6cfe5d15 2727 if (pbra->install_in_progress && !pbr)
30d50e6d 2728 return;
6cfe5d15
PG
2729 if (pbr && pbr->install_in_progress)
2730 return;
2731 if (BGP_DEBUG(zebra, ZEBRA)) {
2732 if (pbr)
2733 zlog_debug("%s: table %d (ip rule) %d",
2734 __PRETTY_FUNCTION__,
2735 pbra->table_id, install);
2736 else
2737 zlog_debug("%s: table %d fwmark %d %d",
2738 __PRETTY_FUNCTION__,
2739 pbra->table_id, pbra->fwmark, install);
2740 }
30d50e6d
PG
2741 s = zclient->obuf;
2742 stream_reset(s);
2743
2744 zclient_create_header(s,
2745 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
2746 VRF_DEFAULT);
2747 stream_putl(s, 1); /* send one pbr action */
2748
6cfe5d15 2749 bgp_encode_pbr_rule_action(s, pbra, pbr);
30d50e6d
PG
2750
2751 stream_putw_at(s, 0, stream_get_endp(s));
6cfe5d15
PG
2752 if (!zclient_send_message(zclient) && install) {
2753 if (!pbr)
2754 pbra->install_in_progress = true;
2755 else
2756 pbr->install_in_progress = true;
2757 }
30d50e6d
PG
2758}
2759
2760void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
2761{
2762 struct stream *s;
2763
2764 if (pbrim->install_in_progress)
2765 return;
f0936054 2766 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2767 zlog_debug("%s: name %s type %d %d, ID %u",
f0936054 2768 __PRETTY_FUNCTION__,
88055124
PG
2769 pbrim->ipset_name, pbrim->type,
2770 install, pbrim->unique);
30d50e6d
PG
2771 s = zclient->obuf;
2772 stream_reset(s);
2773
2774 zclient_create_header(s,
2775 install ? ZEBRA_IPSET_CREATE :
2776 ZEBRA_IPSET_DESTROY,
2777 VRF_DEFAULT);
2778
2779 stream_putl(s, 1); /* send one pbr action */
2780
2781 bgp_encode_pbr_ipset_match(s, pbrim);
2782
2783 stream_putw_at(s, 0, stream_get_endp(s));
2784 if (!zclient_send_message(zclient) && install)
2785 pbrim->install_in_progress = true;
2786}
2787
2788void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
2789 bool install)
2790{
2791 struct stream *s;
2792
2793 if (pbrime->install_in_progress)
2794 return;
f0936054 2795 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2796 zlog_debug("%s: name %s %d %d, ID %u", __PRETTY_FUNCTION__,
f0936054 2797 pbrime->backpointer->ipset_name,
88055124 2798 pbrime->unique, install, pbrime->unique);
30d50e6d
PG
2799 s = zclient->obuf;
2800 stream_reset(s);
2801
2802 zclient_create_header(s,
2803 install ? ZEBRA_IPSET_ENTRY_ADD :
2804 ZEBRA_IPSET_ENTRY_DELETE,
2805 VRF_DEFAULT);
2806
2807 stream_putl(s, 1); /* send one pbr action */
2808
2809 bgp_encode_pbr_ipset_entry_match(s, pbrime);
2810
2811 stream_putw_at(s, 0, stream_get_endp(s));
2812 if (!zclient_send_message(zclient) && install)
2813 pbrime->install_in_progress = true;
2814}
c16a0a62 2815
4762c213
PG
2816static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s)
2817{
2818 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2819 struct bgp_pbr_interface_head *head;
2820 struct bgp_pbr_interface *pbr_if;
2821 struct interface *ifp;
2822
2823 if (!bgp_pbr_cfg)
2824 return;
2825 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2826
2827 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
2828 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
2829 if (ifp)
2830 stream_putl(s, ifp->ifindex);
2831 }
2832}
2833
2834static int bgp_pbr_get_ifnumber(struct bgp *bgp)
2835{
2836 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2837 struct bgp_pbr_interface_head *head;
2838 struct bgp_pbr_interface *pbr_if;
2839 int cnt = 0;
2840
2841 if (!bgp_pbr_cfg)
2842 return 0;
2843 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2844
2845 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
2846 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
2847 cnt++;
2848 }
2849 return cnt;
2850}
2851
c16a0a62
PG
2852void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
2853 struct bgp_pbr_match *pbm,
2854 bool install)
2855{
2856 struct stream *s;
b5c40105 2857 int ret = 0;
4762c213 2858 int nb_interface;
c16a0a62
PG
2859
2860 if (pbm->install_iptable_in_progress)
2861 return;
f0936054 2862 if (BGP_DEBUG(zebra, ZEBRA))
88055124 2863 zlog_debug("%s: name %s type %d mark %d %d, ID %u",
f0936054 2864 __PRETTY_FUNCTION__, pbm->ipset_name,
88055124
PG
2865 pbm->type, pba->fwmark, install,
2866 pbm->unique2);
c16a0a62
PG
2867 s = zclient->obuf;
2868 stream_reset(s);
2869
2870 zclient_create_header(s,
2871 install ? ZEBRA_IPTABLE_ADD :
2872 ZEBRA_IPTABLE_DELETE,
2873 VRF_DEFAULT);
2874
2875 bgp_encode_pbr_iptable_match(s, pba, pbm);
4762c213
PG
2876 nb_interface = bgp_pbr_get_ifnumber(pba->bgp);
2877 stream_putl(s, nb_interface);
2878 if (nb_interface)
2879 bgp_encode_pbr_interface_list(pba->bgp, s);
c16a0a62 2880 stream_putw_at(s, 0, stream_get_endp(s));
b5c40105
PG
2881 ret = zclient_send_message(zclient);
2882 if (install) {
2883 if (ret)
2884 pba->refcnt++;
2885 else
2886 pbm->install_iptable_in_progress = true;
a6b07429 2887 }
c16a0a62 2888}
f7df1907
PG
2889
2890/* inject in table <table_id> a default route to:
2891 * - if nexthop IP is present : to this nexthop
2892 * - if vrf is different from local : to the matching VRF
2893 */
2894void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
2895 afi_t afi, uint32_t table_id, bool announce)
2896{
2897 struct zapi_nexthop *api_nh;
2898 struct zapi_route api;
2899 struct prefix p;
2900
2901 if (!nh || nh->type != NEXTHOP_TYPE_IPV4
2902 || nh->vrf_id == VRF_UNKNOWN)
2903 return;
2904 memset(&p, 0, sizeof(struct prefix));
2905 /* default route */
2906 if (afi != AFI_IP)
2907 return;
2908 p.family = AF_INET;
2909 memset(&api, 0, sizeof(api));
2910 api.vrf_id = bgp->vrf_id;
2911 api.type = ZEBRA_ROUTE_BGP;
2912 api.safi = SAFI_UNICAST;
2913 api.prefix = p;
2914 api.tableid = table_id;
2915 api.nexthop_num = 1;
2916 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
2917 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
2918 api_nh = &api.nexthops[0];
2919
2920 /* redirect IP */
2921 if (nh->gate.ipv4.s_addr) {
2922 char buff[PREFIX_STRLEN];
2923
2924 api_nh->vrf_id = nh->vrf_id;
2925 api_nh->gate.ipv4 = nh->gate.ipv4;
2926 api_nh->type = NEXTHOP_TYPE_IPV4;
2927
2928 inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN);
2929 if (BGP_DEBUG(zebra, ZEBRA))
d887503c
PG
2930 zlog_info("BGP: %s default route to %s table %d (redirect IP)",
2931 announce ? "adding" : "withdrawing",
f7df1907
PG
2932 buff, table_id);
2933 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
2934 : ZEBRA_ROUTE_DELETE,
2935 zclient, &api);
2936 } else if (nh->vrf_id != bgp->vrf_id) {
2937 struct vrf *vrf;
eb4244f8 2938 struct interface *ifp;
f7df1907 2939
eb4244f8 2940 vrf = vrf_lookup_by_id(nh->vrf_id);
f7df1907
PG
2941 if (!vrf)
2942 return;
eb4244f8
PG
2943 /* create default route with interface <VRF>
2944 * with nexthop-vrf <VRF>
f7df1907 2945 */
eb4244f8
PG
2946 ifp = if_lookup_by_name_all_vrf(vrf->name);
2947 if (!ifp)
2948 return;
2949 api_nh->vrf_id = nh->vrf_id;
2950 api_nh->type = NEXTHOP_TYPE_IFINDEX;
2951 api_nh->ifindex = ifp->ifindex;
2952 if (BGP_DEBUG(zebra, ZEBRA))
d887503c
PG
2953 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
2954 announce ? "adding" : "withdrawing",
eb4244f8
PG
2955 vrf->name, table_id);
2956 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
2957 : ZEBRA_ROUTE_DELETE,
2958 zclient, &api);
f7df1907
PG
2959 return;
2960 }
2961}