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