]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
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 */
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"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35 #include "lib/bfd.h"
36 #include "lib/route_opaque.h"
37 #include "filter.h"
38 #include "mpls.h"
39 #include "vxlan.h"
40 #include "pbr.h"
41
42 #include "bgpd/bgpd.h"
43 #include "bgpd/bgp_route.h"
44 #include "bgpd/bgp_attr.h"
45 #include "bgpd/bgp_aspath.h"
46 #include "bgpd/bgp_nexthop.h"
47 #include "bgpd/bgp_zebra.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_debug.h"
50 #include "bgpd/bgp_errors.h"
51 #include "bgpd/bgp_mpath.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_nht.h"
54 #include "bgpd/bgp_bfd.h"
55 #include "bgpd/bgp_label.h"
56 #ifdef ENABLE_BGP_VNC
57 #include "bgpd/rfapi/rfapi_backend.h"
58 #include "bgpd/rfapi/vnc_export_bgp.h"
59 #endif
60 #include "bgpd/bgp_evpn.h"
61 #include "bgpd/bgp_mplsvpn.h"
62 #include "bgpd/bgp_labelpool.h"
63 #include "bgpd/bgp_pbr.h"
64 #include "bgpd/bgp_evpn_private.h"
65 #include "bgpd/bgp_evpn_mh.h"
66 #include "bgpd/bgp_mac.h"
67 #include "bgpd/bgp_trace.h"
68 #include "bgpd/bgp_community.h"
69 #include "bgpd/bgp_lcommunity.h"
70
71 /* All information about zebra. */
72 struct zclient *zclient = NULL;
73
74 /* hook to indicate vrf status change for SNMP */
75 DEFINE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp),
76 (bgp, ifp));
77
78 DEFINE_MTYPE_STATIC(BGPD, BGP_IF_INFO, "BGP interface context");
79
80 /* Can we install into zebra? */
81 static inline bool bgp_install_info_to_zebra(struct bgp *bgp)
82 {
83 if (zclient->sock <= 0)
84 return false;
85
86 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
87 zlog_debug(
88 "%s: No zebra instance to talk to, not installing information",
89 __func__);
90 return false;
91 }
92
93 return true;
94 }
95
96 int zclient_num_connects;
97
98 /* Router-id update message from zebra. */
99 static int bgp_router_id_update(ZAPI_CALLBACK_ARGS)
100 {
101 struct prefix router_id;
102
103 zebra_router_id_update_read(zclient->ibuf, &router_id);
104
105 if (BGP_DEBUG(zebra, ZEBRA))
106 zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id,
107 &router_id);
108
109 bgp_router_id_zebra_bump(vrf_id, &router_id);
110 return 0;
111 }
112
113 /* Nexthop update message from zebra. */
114 static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS)
115 {
116 bgp_parse_nexthop_update(cmd, vrf_id);
117 return 0;
118 }
119
120 /* Set or clear interface on which unnumbered neighbor is configured. This
121 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
122 * interface.
123 */
124 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
125 struct interface *upd_ifp)
126 {
127 struct listnode *node, *nnode;
128 struct peer *peer;
129
130 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
131 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
132 if (upd_ifp) {
133 peer->ifp = upd_ifp;
134 bgp_zebra_initiate_radv(bgp, peer);
135 } else {
136 bgp_zebra_terminate_radv(bgp, peer);
137 peer->ifp = upd_ifp;
138 }
139 }
140 }
141 }
142
143 static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS)
144 {
145 bgp_parse_fec_update();
146 return 0;
147 }
148
149 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
150 {
151 struct listnode *node, *nnode;
152 struct peer *peer;
153
154 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
155 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
156 && !peer_established(peer)) {
157 if (peer_active(peer))
158 BGP_EVENT_ADD(peer, BGP_Stop);
159 BGP_EVENT_ADD(peer, BGP_Start);
160 }
161 }
162 }
163
164 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
165 {
166 struct listnode *node;
167 struct connected *connected;
168 struct interface *ifp;
169 struct prefix *p;
170
171 /* Kick-off the FSM for any relevant peers only if there is a
172 * valid local address on the interface.
173 */
174 ifp = ifc->ifp;
175 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
176 p = connected->address;
177 if (p->family == AF_INET6
178 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
179 break;
180 }
181 if (!connected)
182 return;
183
184 bgp_start_interface_nbrs(bgp, ifp);
185 }
186
187 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
188 int del)
189 {
190 struct listnode *node, *nnode;
191 struct peer *peer;
192 struct interface *ifp;
193
194 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
195 if (peer->conf_if
196 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
197 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
198 BGP_EVENT_ADD(peer, BGP_Stop);
199 }
200 }
201 /* Free neighbor also, if we're asked to. */
202 if (del) {
203 ifp = ifc->ifp;
204 listnode_delete(ifp->nbr_connected, ifc);
205 nbr_connected_free(ifc);
206 }
207 }
208
209 static int bgp_ifp_destroy(struct interface *ifp)
210 {
211 struct bgp *bgp;
212
213 bgp = ifp->vrf->info;
214
215 if (BGP_DEBUG(zebra, ZEBRA))
216 zlog_debug("Rx Intf del VRF %u IF %s", ifp->vrf->vrf_id,
217 ifp->name);
218
219 if (bgp) {
220 bgp_update_interface_nbrs(bgp, ifp, NULL);
221 hook_call(bgp_vrf_status_changed, bgp, ifp);
222 }
223
224 bgp_mac_del_mac_entry(ifp);
225
226 return 0;
227 }
228
229 static int bgp_ifp_up(struct interface *ifp)
230 {
231 struct connected *c;
232 struct nbr_connected *nc;
233 struct listnode *node, *nnode;
234 struct bgp *bgp;
235
236 bgp = ifp->vrf->info;
237
238 bgp_mac_add_mac_entry(ifp);
239
240 if (BGP_DEBUG(zebra, ZEBRA))
241 zlog_debug("Rx Intf up VRF %u IF %s", ifp->vrf->vrf_id,
242 ifp->name);
243
244 if (!bgp)
245 return 0;
246
247 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
248 bgp_connected_add(bgp, c);
249
250 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
251 bgp_nbr_connected_add(bgp, nc);
252
253 hook_call(bgp_vrf_status_changed, bgp, ifp);
254 bgp_nht_ifp_up(ifp);
255
256 return 0;
257 }
258
259 static int bgp_ifp_down(struct interface *ifp)
260 {
261 struct connected *c;
262 struct nbr_connected *nc;
263 struct listnode *node, *nnode;
264 struct bgp *bgp;
265 struct peer *peer;
266
267 bgp = ifp->vrf->info;
268
269 bgp_mac_del_mac_entry(ifp);
270
271 if (BGP_DEBUG(zebra, ZEBRA))
272 zlog_debug("Rx Intf down VRF %u IF %s", ifp->vrf->vrf_id,
273 ifp->name);
274
275 if (!bgp)
276 return 0;
277
278 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
279 bgp_connected_delete(bgp, c);
280
281 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
282 bgp_nbr_connected_delete(bgp, nc, 1);
283
284 /* Fast external-failover */
285 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
286
287 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
288 /* Take down directly connected peers. */
289 if ((peer->ttl != BGP_DEFAULT_TTL)
290 && (peer->gtsm_hops != BGP_GTSM_HOPS_CONNECTED))
291 continue;
292
293 if (ifp == peer->nexthop.ifp) {
294 BGP_EVENT_ADD(peer, BGP_Stop);
295 peer->last_reset = PEER_DOWN_IF_DOWN;
296 }
297 }
298 }
299
300 hook_call(bgp_vrf_status_changed, bgp, ifp);
301 bgp_nht_ifp_down(ifp);
302
303 return 0;
304 }
305
306 static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS)
307 {
308 struct connected *ifc;
309 struct bgp *bgp;
310 struct peer *peer;
311 struct prefix *addr;
312 struct listnode *node, *nnode;
313 afi_t afi;
314 safi_t safi;
315
316 bgp = bgp_lookup_by_vrf_id(vrf_id);
317
318 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
319
320 if (ifc == NULL)
321 return 0;
322
323 if (bgp_debug_zebra(ifc->address))
324 zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id,
325 ifc->ifp->name, ifc->address);
326
327 if (!bgp)
328 return 0;
329
330 if (if_is_operative(ifc->ifp)) {
331 bgp_connected_add(bgp, ifc);
332
333 /* If we have learnt of any neighbors on this interface,
334 * check to kick off any BGP interface-based neighbors,
335 * but only if this is a link-local address.
336 */
337 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
338 && !list_isempty(ifc->ifp->nbr_connected))
339 bgp_start_interface_nbrs(bgp, ifc->ifp);
340 else {
341 addr = ifc->address;
342
343 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
344 if (addr->family == AF_INET)
345 continue;
346
347 /*
348 * If the Peer's interface name matches the
349 * interface name for which BGP received the
350 * update and if the received interface address
351 * is a globalV6 and if the peer is currently
352 * using a v4-mapped-v6 addr or a link local
353 * address, then copy the Rxed global v6 addr
354 * into peer's v6_global and send updates out
355 * with new nexthop addr.
356 */
357 if ((peer->conf_if &&
358 (strcmp(peer->conf_if, ifc->ifp->name) ==
359 0)) &&
360 !IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6) &&
361 ((IS_MAPPED_IPV6(
362 &peer->nexthop.v6_global)) ||
363 IN6_IS_ADDR_LINKLOCAL(
364 &peer->nexthop.v6_global))) {
365
366 if (bgp_debug_zebra(ifc->address)) {
367 zlog_debug(
368 "Update peer %pBP's current intf addr %pI6 and send updates",
369 peer,
370 &peer->nexthop
371 .v6_global);
372 }
373 memcpy(&peer->nexthop.v6_global,
374 &addr->u.prefix6,
375 IPV6_MAX_BYTELEN);
376 FOREACH_AFI_SAFI (afi, safi)
377 bgp_announce_route(peer, afi,
378 safi, true);
379 }
380 }
381 }
382 }
383
384 return 0;
385 }
386
387 static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS)
388 {
389 struct listnode *node, *nnode;
390 struct connected *ifc;
391 struct peer *peer;
392 struct bgp *bgp;
393 struct prefix *addr;
394
395 bgp = bgp_lookup_by_vrf_id(vrf_id);
396
397 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
398
399 if (ifc == NULL)
400 return 0;
401
402 if (bgp_debug_zebra(ifc->address))
403 zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id,
404 ifc->ifp->name, ifc->address);
405
406 if (bgp && if_is_operative(ifc->ifp)) {
407 bgp_connected_delete(bgp, ifc);
408 }
409
410 addr = ifc->address;
411
412 if (bgp) {
413 /*
414 * When we are using the v6 global as part of the peering
415 * nexthops and we are removing it, then we need to
416 * clear the peer data saved for that nexthop and
417 * cause a re-announcement of the route. Since
418 * we do not want the peering to bounce.
419 */
420 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
421 afi_t afi;
422 safi_t safi;
423
424 if (addr->family == AF_INET)
425 continue;
426
427 if (!IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6)
428 && memcmp(&peer->nexthop.v6_global,
429 &addr->u.prefix6, 16)
430 == 0) {
431 memset(&peer->nexthop.v6_global, 0, 16);
432 FOREACH_AFI_SAFI (afi, safi)
433 bgp_announce_route(peer, afi, safi,
434 true);
435 }
436 }
437 }
438
439 connected_free(&ifc);
440
441 return 0;
442 }
443
444 static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS)
445 {
446 struct nbr_connected *ifc = NULL;
447 struct bgp *bgp;
448
449 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
450
451 if (ifc == NULL)
452 return 0;
453
454 if (bgp_debug_zebra(ifc->address))
455 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX",
456 vrf_id, ifc->ifp->name, ifc->address);
457
458 if (if_is_operative(ifc->ifp)) {
459 bgp = bgp_lookup_by_vrf_id(vrf_id);
460 if (bgp)
461 bgp_nbr_connected_add(bgp, ifc);
462 }
463
464 return 0;
465 }
466
467 static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS)
468 {
469 struct nbr_connected *ifc = NULL;
470 struct bgp *bgp;
471
472 ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id);
473
474 if (ifc == NULL)
475 return 0;
476
477 if (bgp_debug_zebra(ifc->address))
478 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX",
479 vrf_id, ifc->ifp->name, ifc->address);
480
481 if (if_is_operative(ifc->ifp)) {
482 bgp = bgp_lookup_by_vrf_id(vrf_id);
483 if (bgp)
484 bgp_nbr_connected_delete(bgp, ifc, 0);
485 }
486
487 nbr_connected_free(ifc);
488
489 return 0;
490 }
491
492 /* VRF update for an interface. */
493 static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS)
494 {
495 struct interface *ifp;
496 vrf_id_t new_vrf_id;
497 struct connected *c;
498 struct nbr_connected *nc;
499 struct listnode *node, *nnode;
500 struct bgp *bgp;
501 struct peer *peer;
502
503 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
504 &new_vrf_id);
505 if (!ifp)
506 return 0;
507
508 if (BGP_DEBUG(zebra, ZEBRA))
509 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
510 ifp->name, new_vrf_id);
511
512 bgp = bgp_lookup_by_vrf_id(vrf_id);
513
514 if (bgp) {
515 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
516 bgp_connected_delete(bgp, c);
517
518 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
519 bgp_nbr_connected_delete(bgp, nc, 1);
520
521 /* Fast external-failover */
522 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
523 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
524 if ((peer->ttl != BGP_DEFAULT_TTL)
525 && (peer->gtsm_hops
526 != BGP_GTSM_HOPS_CONNECTED))
527 continue;
528
529 if (ifp == peer->nexthop.ifp)
530 BGP_EVENT_ADD(peer, BGP_Stop);
531 }
532 }
533 }
534
535 if_update_to_new_vrf(ifp, new_vrf_id);
536
537 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
538 if (!bgp)
539 return 0;
540
541 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
542 bgp_connected_add(bgp, c);
543
544 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
545 bgp_nbr_connected_add(bgp, nc);
546
547 hook_call(bgp_vrf_status_changed, bgp, ifp);
548 return 0;
549 }
550
551 /* Zebra route add and delete treatment. */
552 static int zebra_read_route(ZAPI_CALLBACK_ARGS)
553 {
554 enum nexthop_types_t nhtype;
555 enum blackhole_type bhtype = BLACKHOLE_UNSPEC;
556 struct zapi_route api;
557 union g_addr nexthop = {};
558 ifindex_t ifindex;
559 int add, i;
560 struct bgp *bgp;
561
562 bgp = bgp_lookup_by_vrf_id(vrf_id);
563 if (!bgp)
564 return 0;
565
566 if (zapi_route_decode(zclient->ibuf, &api) < 0)
567 return -1;
568
569 /* we completely ignore srcdest routes for now. */
570 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
571 return 0;
572
573 /* ignore link-local address. */
574 if (api.prefix.family == AF_INET6
575 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
576 return 0;
577
578 ifindex = api.nexthops[0].ifindex;
579 nhtype = api.nexthops[0].type;
580
581 /* api_nh structure has union of gate and bh_type */
582 if (nhtype == NEXTHOP_TYPE_BLACKHOLE) {
583 /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/
584 bhtype = api.nexthops[0].bh_type;
585 } else
586 nexthop = api.nexthops[0].gate;
587
588 add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
589 if (add) {
590 /*
591 * The ADD message is actually an UPDATE and there is no
592 * explicit DEL
593 * for a prior redistributed route, if any. So, perform an
594 * implicit
595 * DEL processing for the same redistributed route from any
596 * other
597 * source type.
598 */
599 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
600 if (i != api.type)
601 bgp_redistribute_delete(bgp, &api.prefix, i,
602 api.instance);
603 }
604
605 /* Now perform the add/update. */
606 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
607 nhtype, bhtype, api.distance, api.metric,
608 api.type, api.instance, api.tag);
609 } else {
610 bgp_redistribute_delete(bgp, &api.prefix, api.type,
611 api.instance);
612 }
613
614 if (bgp_debug_zebra(&api.prefix)) {
615 char buf[PREFIX_STRLEN];
616
617 if (add) {
618 inet_ntop(api.prefix.family, &nexthop, buf,
619 sizeof(buf));
620 zlog_debug(
621 "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI,
622 vrf_id, zebra_route_string(api.type),
623 api.instance, &api.prefix, buf, nhtype, ifindex,
624 api.metric, api.distance, api.tag);
625 } else {
626 zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id,
627 zebra_route_string(api.type), api.instance,
628 &api.prefix);
629 }
630 }
631
632 return 0;
633 }
634
635 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
636 {
637 struct vrf *vrf;
638 struct listnode *cnode;
639 struct interface *ifp;
640 struct connected *connected;
641 struct prefix_ipv4 p;
642 struct prefix *cp;
643
644 vrf = vrf_lookup_by_id(vrf_id);
645 if (!vrf)
646 return NULL;
647
648 p.family = AF_INET;
649 p.prefix = *addr;
650 p.prefixlen = IPV4_MAX_BITLEN;
651
652 FOR_ALL_INTERFACES (vrf, ifp) {
653 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
654 cp = connected->address;
655
656 if (cp->family == AF_INET)
657 if (prefix_match(cp, (struct prefix *)&p))
658 return ifp;
659 }
660 }
661 return NULL;
662 }
663
664 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
665 {
666 struct vrf *vrf;
667 struct listnode *cnode;
668 struct interface *ifp;
669 struct connected *connected;
670 struct prefix *cp;
671
672 vrf = vrf_lookup_by_id(vrf_id);
673 if (!vrf)
674 return NULL;
675
676 FOR_ALL_INTERFACES (vrf, ifp) {
677 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
678 cp = connected->address;
679
680 if (cp->family == AF_INET)
681 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
682 return ifp;
683 }
684 }
685 return NULL;
686 }
687
688 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
689 vrf_id_t vrf_id)
690 {
691 struct vrf *vrf;
692 struct listnode *cnode;
693 struct interface *ifp;
694 struct connected *connected;
695 struct prefix_ipv6 p;
696 struct prefix *cp;
697
698 vrf = vrf_lookup_by_id(vrf_id);
699 if (!vrf)
700 return NULL;
701
702 p.family = AF_INET6;
703 p.prefix = *addr;
704 p.prefixlen = IPV6_MAX_BITLEN;
705
706 FOR_ALL_INTERFACES (vrf, ifp) {
707 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
708 cp = connected->address;
709
710 if (cp->family == AF_INET6)
711 if (prefix_match(cp, (struct prefix *)&p)) {
712 if (IN6_IS_ADDR_LINKLOCAL(
713 &cp->u.prefix6)) {
714 if (ifindex == ifp->ifindex)
715 return ifp;
716 } else
717 return ifp;
718 }
719 }
720 }
721 return NULL;
722 }
723
724 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
725 ifindex_t ifindex, vrf_id_t vrf_id)
726 {
727 struct vrf *vrf;
728 struct listnode *cnode;
729 struct interface *ifp;
730 struct connected *connected;
731 struct prefix *cp;
732
733 vrf = vrf_lookup_by_id(vrf_id);
734 if (!vrf)
735 return NULL;
736
737 FOR_ALL_INTERFACES (vrf, ifp) {
738 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
739 cp = connected->address;
740
741 if (cp->family == AF_INET6)
742 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
743 if (IN6_IS_ADDR_LINKLOCAL(
744 &cp->u.prefix6)) {
745 if (ifindex == ifp->ifindex)
746 return ifp;
747 } else
748 return ifp;
749 }
750 }
751 }
752 return NULL;
753 }
754
755 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
756 {
757 struct listnode *cnode;
758 struct connected *connected;
759 struct prefix *cp;
760
761 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
762 cp = connected->address;
763
764 if (cp->family == AF_INET6)
765 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
766 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
767 return 1;
768 }
769 }
770 return 0;
771 }
772
773 static bool if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
774 {
775 struct listnode *cnode;
776 struct connected *connected;
777 struct prefix *cp;
778
779 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
780 cp = connected->address;
781
782 if (cp->family == AF_INET6)
783 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
784 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
785 return true;
786 }
787 }
788 return false;
789 }
790
791 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
792 {
793 struct listnode *cnode;
794 struct connected *connected;
795 struct prefix *cp;
796
797 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
798 cp = connected->address;
799 if ((cp->family == AF_INET)
800 && !ipv4_martian(&(cp->u.prefix4))) {
801 *addr = cp->u.prefix4;
802 return 1;
803 }
804 }
805 return 0;
806 }
807
808
809 bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
810 struct bgp_nexthop *nexthop, struct peer *peer)
811 {
812 int ret = 0;
813 struct interface *ifp = NULL;
814 bool v6_ll_avail = true;
815
816 memset(nexthop, 0, sizeof(struct bgp_nexthop));
817
818 if (!local)
819 return false;
820 if (!remote)
821 return false;
822
823 if (local->sa.sa_family == AF_INET) {
824 nexthop->v4 = local->sin.sin_addr;
825 if (peer->update_if)
826 ifp = if_lookup_by_name(peer->update_if,
827 peer->bgp->vrf_id);
828 else
829 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
830 peer->bgp->vrf_id);
831 }
832 if (local->sa.sa_family == AF_INET6) {
833 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, IPV6_MAX_BYTELEN);
834 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
835 if (peer->conf_if || peer->ifname)
836 ifp = if_lookup_by_name(peer->conf_if
837 ? peer->conf_if
838 : peer->ifname,
839 peer->bgp->vrf_id);
840 else if (peer->update_if)
841 ifp = if_lookup_by_name(peer->update_if,
842 peer->bgp->vrf_id);
843 } else if (peer->update_if)
844 ifp = if_lookup_by_name(peer->update_if,
845 peer->bgp->vrf_id);
846 else
847 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
848 local->sin6.sin6_scope_id,
849 peer->bgp->vrf_id);
850 }
851
852 if (!ifp) {
853 /*
854 * BGP views do not currently get proper data
855 * from zebra( when attached ) to be able to
856 * properly resolve nexthops, so give this
857 * instance type a pass.
858 */
859 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
860 return true;
861 /*
862 * If we have no interface data but we have established
863 * some connection w/ zebra than something has gone
864 * terribly terribly wrong here, so say this failed
865 * If we do not any zebra connection then not
866 * having a ifp pointer is ok.
867 */
868 return zclient_num_connects ? false : true;
869 }
870
871 nexthop->ifp = ifp;
872
873 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
874 if (local->sa.sa_family == AF_INET) {
875 /* IPv6 nexthop*/
876 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
877
878 if (!ret) {
879 /* There is no global nexthop. Use link-local address as
880 * both the
881 * global and link-local nexthop. In this scenario, the
882 * expectation
883 * for interop is that the network admin would use a
884 * route-map to
885 * specify the global IPv6 nexthop.
886 */
887 v6_ll_avail =
888 if_get_ipv6_local(ifp, &nexthop->v6_global);
889 memcpy(&nexthop->v6_local, &nexthop->v6_global,
890 IPV6_MAX_BYTELEN);
891 } else
892 v6_ll_avail =
893 if_get_ipv6_local(ifp, &nexthop->v6_local);
894
895 /*
896 * If we are a v4 connection and we are not doing unnumbered
897 * not having a v6 LL address is ok
898 */
899 if (!v6_ll_avail && !peer->conf_if)
900 v6_ll_avail = true;
901 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
902 peer->shared_network = 1;
903 else
904 peer->shared_network = 0;
905 }
906
907 /* IPv6 connection, fetch and store IPv4 local address if any. */
908 if (local->sa.sa_family == AF_INET6) {
909 struct interface *direct = NULL;
910
911 /* IPv4 nexthop. */
912 ret = if_get_ipv4_address(ifp, &nexthop->v4);
913 if (!ret && peer->local_id.s_addr != INADDR_ANY)
914 nexthop->v4 = peer->local_id;
915
916 /* Global address*/
917 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
918 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
919 IPV6_MAX_BYTELEN);
920
921 /* If directly connected set link-local address. */
922 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
923 remote->sin6.sin6_scope_id,
924 peer->bgp->vrf_id);
925 if (direct)
926 v6_ll_avail = if_get_ipv6_local(
927 ifp, &nexthop->v6_local);
928 /*
929 * It's fine to not have a v6 LL when using
930 * update-source loopback/vrf
931 */
932 if (!v6_ll_avail && if_is_loopback(ifp))
933 v6_ll_avail = true;
934 else if (!v6_ll_avail) {
935 flog_warn(
936 EC_BGP_NO_LL_ADDRESS_AVAILABLE,
937 "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it",
938 ifp->name);
939 }
940 } else
941 /* Link-local address. */
942 {
943 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
944
945 /* If there is no global address. Set link-local
946 address as
947 global. I know this break RFC specification... */
948 /* In this scenario, the expectation for interop is that
949 * the
950 * network admin would use a route-map to specify the
951 * global
952 * IPv6 nexthop.
953 */
954 if (!ret)
955 memcpy(&nexthop->v6_global,
956 &local->sin6.sin6_addr,
957 IPV6_MAX_BYTELEN);
958 /* Always set the link-local address */
959 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
960 IPV6_MAX_BYTELEN);
961 }
962
963 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
964 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
965 remote->sin6.sin6_scope_id,
966 peer->bgp->vrf_id))
967 peer->shared_network = 1;
968 else
969 peer->shared_network = 0;
970 }
971
972 /* KAME stack specific treatment. */
973 #ifdef KAME
974 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
975 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
976 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
977 }
978 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
979 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
980 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
981 }
982 #endif /* KAME */
983
984 /* If we have identified the local interface, there is no error for now.
985 */
986 return v6_ll_avail;
987 }
988
989 static struct in6_addr *
990 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
991 {
992 struct in6_addr *nexthop = NULL;
993
994 /* Only global address nexthop exists. */
995 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
996 || path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) {
997 nexthop = &path->attr->mp_nexthop_global;
998 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
999 *ifindex = path->attr->nh_ifindex;
1000 }
1001
1002 /* If both global and link-local address present. */
1003 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
1004 || path->attr->mp_nexthop_len
1005 == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
1006 /* Check if route-map is set to prefer global over link-local */
1007 if (path->attr->mp_nexthop_prefer_global) {
1008 nexthop = &path->attr->mp_nexthop_global;
1009 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1010 *ifindex = path->attr->nh_ifindex;
1011 } else {
1012 /* Workaround for Cisco's nexthop bug. */
1013 if (IN6_IS_ADDR_UNSPECIFIED(
1014 &path->attr->mp_nexthop_global)
1015 && path->peer->su_remote
1016 && path->peer->su_remote->sa.sa_family
1017 == AF_INET6) {
1018 nexthop =
1019 &path->peer->su_remote->sin6.sin6_addr;
1020 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1021 *ifindex = path->peer->nexthop.ifp
1022 ->ifindex;
1023 } else {
1024 nexthop = &path->attr->mp_nexthop_local;
1025 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
1026 *ifindex = path->attr->nh_lla_ifindex;
1027 }
1028 }
1029 }
1030
1031 return nexthop;
1032 }
1033
1034 static bool bgp_table_map_apply(struct route_map *map, const struct prefix *p,
1035 struct bgp_path_info *path)
1036 {
1037 route_map_result_t ret;
1038
1039 ret = route_map_apply(map, p, path);
1040 bgp_attr_flush(path->attr);
1041
1042 if (ret != RMAP_DENYMATCH)
1043 return true;
1044
1045 if (bgp_debug_zebra(p)) {
1046 if (p->family == AF_INET) {
1047 zlog_debug(
1048 "Zebra rmap deny: IPv4 route %pFX nexthop %pI4",
1049 p, &path->attr->nexthop);
1050 }
1051 if (p->family == AF_INET6) {
1052 ifindex_t ifindex;
1053 struct in6_addr *nexthop;
1054
1055 nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex);
1056 zlog_debug(
1057 "Zebra rmap deny: IPv6 route %pFX nexthop %pI6",
1058 p, nexthop);
1059 }
1060 }
1061 return false;
1062 }
1063
1064 static struct thread *bgp_tm_thread_connect;
1065 static bool bgp_tm_status_connected;
1066 static bool bgp_tm_chunk_obtained;
1067 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1068 static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size;
1069 struct bgp *bgp_tm_bgp;
1070
1071 static void bgp_zebra_tm_connect(struct thread *t)
1072 {
1073 struct zclient *zclient;
1074 int delay = 10, ret = 0;
1075
1076 zclient = THREAD_ARG(t);
1077 if (bgp_tm_status_connected && zclient->sock > 0)
1078 delay = 60;
1079 else {
1080 bgp_tm_status_connected = false;
1081 ret = tm_table_manager_connect(zclient);
1082 }
1083 if (ret < 0) {
1084 zlog_info("Error connecting to table manager!");
1085 bgp_tm_status_connected = false;
1086 } else {
1087 if (!bgp_tm_status_connected)
1088 zlog_debug("Connecting to table manager. Success");
1089 bgp_tm_status_connected = true;
1090 if (!bgp_tm_chunk_obtained) {
1091 if (bgp_zebra_get_table_range(bgp_tm_chunk_size,
1092 &bgp_tm_min,
1093 &bgp_tm_max) >= 0) {
1094 bgp_tm_chunk_obtained = true;
1095 /* parse non installed entries */
1096 bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC);
1097 }
1098 }
1099 }
1100 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1101 &bgp_tm_thread_connect);
1102 }
1103
1104 bool bgp_zebra_tm_chunk_obtained(void)
1105 {
1106 return bgp_tm_chunk_obtained;
1107 }
1108
1109 uint32_t bgp_zebra_tm_get_id(void)
1110 {
1111 static int table_id;
1112
1113 if (!bgp_tm_chunk_obtained)
1114 return ++table_id;
1115 return bgp_tm_min++;
1116 }
1117
1118 void bgp_zebra_init_tm_connect(struct bgp *bgp)
1119 {
1120 int delay = 1;
1121
1122 /* if already set, do nothing
1123 */
1124 if (bgp_tm_thread_connect != NULL)
1125 return;
1126 bgp_tm_status_connected = false;
1127 bgp_tm_chunk_obtained = false;
1128 bgp_tm_min = bgp_tm_max = 0;
1129 bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
1130 bgp_tm_bgp = bgp;
1131 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1132 &bgp_tm_thread_connect);
1133 }
1134
1135 int bgp_zebra_get_table_range(uint32_t chunk_size,
1136 uint32_t *start, uint32_t *end)
1137 {
1138 int ret;
1139
1140 if (!bgp_tm_status_connected)
1141 return -1;
1142 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1143 if (ret < 0) {
1144 flog_err(EC_BGP_TABLE_CHUNK,
1145 "BGP: Error getting table chunk %u", chunk_size);
1146 return -1;
1147 }
1148 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1149 chunk_size, *start, *end);
1150 return 0;
1151 }
1152
1153 static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1154 struct in_addr *nexthop,
1155 struct attr *attr, bool is_evpn,
1156 struct zapi_nexthop *api_nh)
1157 {
1158 api_nh->gate.ipv4 = *nexthop;
1159 api_nh->vrf_id = nh_bgp->vrf_id;
1160
1161 /* Need to set fields appropriately for EVPN routes imported into
1162 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1163 * connected routes leaked into a VRF.
1164 */
1165 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1166 api_nh->type = attr->nh_type;
1167 api_nh->bh_type = attr->bh_type;
1168 } else if (is_evpn) {
1169 /*
1170 * If the nexthop is EVPN overlay index gateway IP,
1171 * treat the nexthop as NEXTHOP_TYPE_IPV4
1172 * Else, mark the nexthop as onlink.
1173 */
1174 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1175 api_nh->type = NEXTHOP_TYPE_IPV4;
1176 else {
1177 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1178 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1179 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1180 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1181 }
1182 } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) {
1183 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1184 api_nh->ifindex = attr->nh_ifindex;
1185 } else
1186 api_nh->type = NEXTHOP_TYPE_IPV4;
1187
1188 return true;
1189 }
1190
1191 static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp,
1192 struct in6_addr *nexthop,
1193 ifindex_t ifindex,
1194 struct bgp_path_info *pi,
1195 struct bgp_path_info *best_pi,
1196 bool is_evpn,
1197 struct zapi_nexthop *api_nh)
1198 {
1199 struct attr *attr;
1200
1201 attr = pi->attr;
1202 api_nh->vrf_id = nh_bgp->vrf_id;
1203
1204 if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) {
1205 api_nh->type = attr->nh_type;
1206 api_nh->bh_type = attr->bh_type;
1207 } else if (is_evpn) {
1208 /*
1209 * If the nexthop is EVPN overlay index gateway IP,
1210 * treat the nexthop as NEXTHOP_TYPE_IPV4
1211 * Else, mark the nexthop as onlink.
1212 */
1213 if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP)
1214 api_nh->type = NEXTHOP_TYPE_IPV6;
1215 else {
1216 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1217 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1218 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK);
1219 api_nh->ifindex = nh_bgp->l3vni_svi_ifindex;
1220 }
1221 } else if (nh_othervrf) {
1222 if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1223 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1224 api_nh->ifindex = attr->nh_ifindex;
1225 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1226 if (ifindex == 0)
1227 return false;
1228 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1229 api_nh->ifindex = ifindex;
1230 } else {
1231 api_nh->type = NEXTHOP_TYPE_IPV6;
1232 api_nh->ifindex = 0;
1233 }
1234 } else {
1235 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1236 if (pi == best_pi
1237 && attr->mp_nexthop_len
1238 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1239 if (pi->peer->nexthop.ifp)
1240 ifindex =
1241 pi->peer->nexthop.ifp->ifindex;
1242 if (!ifindex) {
1243 if (pi->peer->conf_if)
1244 ifindex = pi->peer->ifp->ifindex;
1245 else if (pi->peer->ifname)
1246 ifindex = ifname2ifindex(
1247 pi->peer->ifname,
1248 pi->peer->bgp->vrf_id);
1249 else if (pi->peer->nexthop.ifp)
1250 ifindex =
1251 pi->peer->nexthop.ifp->ifindex;
1252 }
1253
1254 if (ifindex == 0)
1255 return false;
1256 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1257 api_nh->ifindex = ifindex;
1258 } else {
1259 api_nh->type = NEXTHOP_TYPE_IPV6;
1260 api_nh->ifindex = 0;
1261 }
1262 }
1263 /* api_nh structure has union of gate and bh_type */
1264 if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE)
1265 api_nh->gate.ipv6 = *nexthop;
1266
1267 return true;
1268 }
1269
1270 static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr,
1271 uint64_t tot_bw, uint32_t *nh_weight)
1272 {
1273 uint32_t bw;
1274 uint64_t tmp;
1275
1276 bw = attr->link_bw;
1277 /* zero link-bandwidth and link-bandwidth not present are treated
1278 * as the same situation.
1279 */
1280 if (!bw) {
1281 /* the only situations should be if we're either told
1282 * to skip or use default weight.
1283 */
1284 if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING)
1285 return false;
1286 *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT;
1287 } else {
1288 tmp = (uint64_t)bw * 100;
1289 *nh_weight = ((uint32_t)(tmp / tot_bw));
1290 }
1291
1292 return true;
1293 }
1294
1295 void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
1296 struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
1297 safi_t safi)
1298 {
1299 struct zapi_route api = { 0 };
1300 struct zapi_nexthop *api_nh;
1301 int nh_family;
1302 unsigned int valid_nh_count = 0;
1303 bool allow_recursion = false;
1304 uint8_t distance;
1305 struct peer *peer;
1306 struct bgp_path_info *mpinfo;
1307 struct bgp *bgp_orig;
1308 uint32_t metric;
1309 struct attr local_attr;
1310 struct bgp_path_info local_info;
1311 struct bgp_path_info *mpinfo_cp = &local_info;
1312 route_tag_t tag;
1313 struct bgp_sid_info *sid_info;
1314 mpls_label_t *labels;
1315 uint32_t num_labels = 0;
1316 mpls_label_t nh_label;
1317 int nh_othervrf = 0;
1318 bool nh_updated = false;
1319 bool do_wt_ecmp;
1320 uint64_t cum_bw = 0;
1321 uint32_t nhg_id = 0;
1322 bool is_add;
1323 uint32_t ttl = 0;
1324 uint32_t bos = 0;
1325 uint32_t exp = 0;
1326
1327 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1328 * know of this instance.
1329 */
1330 if (!bgp_install_info_to_zebra(bgp))
1331 return;
1332
1333 if (bgp->main_zebra_update_hold)
1334 return;
1335
1336 if (safi == SAFI_FLOWSPEC) {
1337 bgp_pbr_update_entry(bgp, bgp_dest_get_prefix(dest), info, afi,
1338 safi, true);
1339 return;
1340 }
1341
1342 /*
1343 * vrf leaking support (will have only one nexthop)
1344 */
1345 if (info->extra && info->extra->bgp_orig)
1346 nh_othervrf = 1;
1347
1348 /* Make Zebra API structure. */
1349 api.vrf_id = bgp->vrf_id;
1350 api.type = ZEBRA_ROUTE_BGP;
1351 api.safi = safi;
1352 api.prefix = *p;
1353 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1354
1355 peer = info->peer;
1356
1357 if (info->type == ZEBRA_ROUTE_BGP
1358 && info->sub_type == BGP_ROUTE_IMPORTED) {
1359
1360 /* Obtain peer from parent */
1361 if (info->extra && info->extra->parent)
1362 peer = ((struct bgp_path_info *)(info->extra->parent))
1363 ->peer;
1364 }
1365
1366 tag = info->attr->tag;
1367
1368 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1369 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1370 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1371 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1372 }
1373
1374 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != BGP_DEFAULT_TTL)
1375 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1376 || CHECK_FLAG(bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1377
1378 allow_recursion = true;
1379
1380 if (info->attr->rmap_table_id) {
1381 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1382 api.tableid = info->attr->rmap_table_id;
1383 }
1384
1385 if (CHECK_FLAG(info->attr->flag, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1386 SET_FLAG(api.message, ZAPI_MESSAGE_SRTE);
1387
1388 /* Metric is currently based on the best-path only */
1389 metric = info->attr->med;
1390
1391 /* Determine if we're doing weighted ECMP or not */
1392 do_wt_ecmp = bgp_path_info_mpath_chkwtd(bgp, info);
1393 if (do_wt_ecmp)
1394 cum_bw = bgp_path_info_mpath_cumbw(info);
1395
1396 /* EVPN MAC-IP routes are installed with a L3 NHG id */
1397 if (bgp_evpn_path_es_use_nhg(bgp, info, &nhg_id)) {
1398 mpinfo = NULL;
1399 api.nhgid = nhg_id;
1400 if (nhg_id)
1401 SET_FLAG(api.message, ZAPI_MESSAGE_NHG);
1402 } else {
1403 mpinfo = info;
1404 }
1405
1406 for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
1407 labels = NULL;
1408 num_labels = 0;
1409 uint32_t nh_weight;
1410 bool is_evpn;
1411 bool is_parent_evpn;
1412
1413 if (valid_nh_count >= multipath_num)
1414 break;
1415
1416 *mpinfo_cp = *mpinfo;
1417 nh_weight = 0;
1418
1419 /* Get nexthop address-family */
1420 if (p->family == AF_INET &&
1421 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp->attr))
1422 nh_family = AF_INET;
1423 else if (p->family == AF_INET6 ||
1424 (p->family == AF_INET &&
1425 BGP_ATTR_MP_NEXTHOP_LEN_IP6(mpinfo_cp->attr)))
1426 nh_family = AF_INET6;
1427 else
1428 continue;
1429
1430 /* If processing for weighted ECMP, determine the next hop's
1431 * weight. Based on user setting, we may skip the next hop
1432 * in some situations.
1433 */
1434 if (do_wt_ecmp) {
1435 if (!bgp_zebra_use_nhop_weighted(bgp, mpinfo->attr,
1436 cum_bw, &nh_weight))
1437 continue;
1438 }
1439 api_nh = &api.nexthops[valid_nh_count];
1440
1441 if (CHECK_FLAG(info->attr->flag,
1442 ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR)))
1443 api_nh->srte_color = info->attr->srte_color;
1444
1445 if (bgp_debug_zebra(&api.prefix)) {
1446 if (mpinfo->extra) {
1447 zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d",
1448 __func__, p,
1449 bgp_is_valid_label(
1450 &mpinfo->extra->label[0]));
1451 } else {
1452 zlog_debug(
1453 "%s: p=%pFX, extra is NULL, no label",
1454 __func__, p);
1455 }
1456 }
1457
1458 if (bgp->table_map[afi][safi].name) {
1459 /* Copy info and attributes, so the route-map
1460 apply doesn't modify the BGP route info. */
1461 local_attr = *mpinfo->attr;
1462 mpinfo_cp->attr = &local_attr;
1463 if (!bgp_table_map_apply(bgp->table_map[afi][safi].map,
1464 p, mpinfo_cp))
1465 continue;
1466
1467 /* metric/tag is only allowed to be
1468 * overridden on 1st nexthop */
1469 if (mpinfo == info) {
1470 metric = mpinfo_cp->attr->med;
1471 tag = mpinfo_cp->attr->tag;
1472 }
1473 }
1474
1475 BGP_ORIGINAL_UPDATE(bgp_orig, mpinfo, bgp);
1476
1477 is_parent_evpn = is_route_parent_evpn(mpinfo);
1478
1479 if (nh_family == AF_INET) {
1480 nh_updated = update_ipv4nh_for_route_install(
1481 nh_othervrf, bgp_orig,
1482 &mpinfo_cp->attr->nexthop, mpinfo_cp->attr,
1483 is_parent_evpn, api_nh);
1484 } else {
1485 ifindex_t ifindex = IFINDEX_INTERNAL;
1486 struct in6_addr *nexthop;
1487
1488 nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp,
1489 &ifindex);
1490
1491 if (!nexthop)
1492 nh_updated = update_ipv4nh_for_route_install(
1493 nh_othervrf, bgp_orig,
1494 &mpinfo_cp->attr->nexthop,
1495 mpinfo_cp->attr, is_parent_evpn,
1496 api_nh);
1497 else
1498 nh_updated = update_ipv6nh_for_route_install(
1499 nh_othervrf, bgp_orig, nexthop, ifindex,
1500 mpinfo, info, is_parent_evpn, api_nh);
1501 }
1502
1503 is_evpn = !!CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN);
1504
1505 /* Did we get proper nexthop info to update zebra? */
1506 if (!nh_updated)
1507 continue;
1508
1509 /* Allow recursion if it is a multipath group with both
1510 * eBGP and iBGP paths.
1511 */
1512 if (!allow_recursion
1513 && CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX)
1514 && (mpinfo->peer->sort == BGP_PEER_IBGP
1515 || mpinfo->peer->sort == BGP_PEER_CONFED))
1516 allow_recursion = true;
1517
1518 if (mpinfo->extra) {
1519 labels = mpinfo->extra->label;
1520 num_labels = mpinfo->extra->num_labels;
1521 }
1522
1523 if (labels && (num_labels > 0) &&
1524 (is_evpn || bgp_is_valid_label(&labels[0]))) {
1525 enum lsp_types_t nh_label_type = ZEBRA_LSP_NONE;
1526
1527 if (is_evpn) {
1528 nh_label = *bgp_evpn_path_info_labels_get_l3vni(
1529 labels, num_labels);
1530 nh_label_type = ZEBRA_LSP_EVPN;
1531 } else {
1532 mpls_lse_decode(labels[0], &nh_label, &ttl,
1533 &exp, &bos);
1534 }
1535
1536 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
1537 api_nh->label_num = 1;
1538 api_nh->label_type = nh_label_type;
1539 api_nh->labels[0] = nh_label;
1540 }
1541
1542 if (is_evpn
1543 && mpinfo->attr->evpn_overlay.type
1544 != OVERLAY_INDEX_GATEWAY_IP)
1545 memcpy(&api_nh->rmac, &(mpinfo->attr->rmac),
1546 sizeof(struct ethaddr));
1547
1548 api_nh->weight = nh_weight;
1549
1550 if (mpinfo->extra && !is_evpn &&
1551 bgp_is_valid_label(&labels[0]) &&
1552 !sid_zero(&mpinfo->extra->sid[0].sid)) {
1553 sid_info = &mpinfo->extra->sid[0];
1554
1555 memcpy(&api_nh->seg6_segs, &sid_info->sid,
1556 sizeof(api_nh->seg6_segs));
1557
1558 if (sid_info->transposition_len != 0) {
1559 mpls_lse_decode(labels[0], &nh_label, &ttl,
1560 &exp, &bos);
1561
1562 if (nh_label < MPLS_LABEL_UNRESERVED_MIN) {
1563 if (bgp_debug_zebra(&api.prefix))
1564 zlog_debug(
1565 "skip invalid SRv6 routes: transposition scheme is used, but label is too small");
1566 continue;
1567 }
1568
1569 transpose_sid(&api_nh->seg6_segs, nh_label,
1570 sid_info->transposition_offset,
1571 sid_info->transposition_len);
1572 }
1573
1574 SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6);
1575 }
1576
1577 valid_nh_count++;
1578 }
1579
1580 is_add = (valid_nh_count || nhg_id) ? true : false;
1581
1582 if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) {
1583 struct bgp_zebra_opaque bzo = {};
1584 const char *reason =
1585 bgp_path_selection_reason2str(dest->reason);
1586
1587 strlcpy(bzo.aspath, info->attr->aspath->str,
1588 sizeof(bzo.aspath));
1589
1590 if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
1591 strlcpy(bzo.community,
1592 bgp_attr_get_community(info->attr)->str,
1593 sizeof(bzo.community));
1594
1595 if (info->attr->flag
1596 & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))
1597 strlcpy(bzo.lcommunity,
1598 bgp_attr_get_lcommunity(info->attr)->str,
1599 sizeof(bzo.lcommunity));
1600
1601 strlcpy(bzo.selection_reason, reason,
1602 sizeof(bzo.selection_reason));
1603
1604 SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE);
1605 api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque),
1606 ZAPI_MESSAGE_OPAQUE_LENGTH);
1607 memcpy(api.opaque.data, &bzo, api.opaque.length);
1608 }
1609
1610 if (allow_recursion)
1611 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1612
1613 /*
1614 * When we create an aggregate route we must also
1615 * install a Null0 route in the RIB, so overwrite
1616 * what was written into api with a blackhole route
1617 */
1618 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1619 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1620 else
1621 api.nexthop_num = valid_nh_count;
1622
1623 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1624 api.metric = metric;
1625
1626 if (tag) {
1627 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1628 api.tag = tag;
1629 }
1630
1631 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1632 if (distance) {
1633 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1634 api.distance = distance;
1635 }
1636
1637 if (bgp_debug_zebra(p)) {
1638 char nh_buf[INET6_ADDRSTRLEN];
1639 char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'};
1640 char buf1[ETHER_ADDR_STRLEN];
1641 char label_buf[20];
1642 char sid_buf[20];
1643 char segs_buf[256];
1644 int i;
1645
1646 zlog_debug(
1647 "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI
1648 " count %d nhg %d",
1649 is_add ? "add" : "delete", bgp->vrf_id, &api.prefix,
1650 api.metric, api.tag, api.nexthop_num, nhg_id);
1651 for (i = 0; i < api.nexthop_num; i++) {
1652 api_nh = &api.nexthops[i];
1653
1654 switch (api_nh->type) {
1655 case NEXTHOP_TYPE_IFINDEX:
1656 nh_buf[0] = '\0';
1657 break;
1658 case NEXTHOP_TYPE_IPV4:
1659 case NEXTHOP_TYPE_IPV4_IFINDEX:
1660 nh_family = AF_INET;
1661 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1662 sizeof(nh_buf));
1663 break;
1664 case NEXTHOP_TYPE_IPV6:
1665 case NEXTHOP_TYPE_IPV6_IFINDEX:
1666 nh_family = AF_INET6;
1667 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1668 sizeof(nh_buf));
1669 break;
1670 case NEXTHOP_TYPE_BLACKHOLE:
1671 strlcpy(nh_buf, "blackhole", sizeof(nh_buf));
1672 break;
1673 default:
1674 /* Note: add new nexthop case */
1675 assert(0);
1676 break;
1677 }
1678
1679 label_buf[0] = '\0';
1680 eth_buf[0] = '\0';
1681 segs_buf[0] = '\0';
1682 if (CHECK_FLAG(api_nh->flags,
1683 ZAPI_NEXTHOP_FLAG_LABEL) &&
1684 !CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN))
1685 snprintf(label_buf, sizeof(label_buf),
1686 "label %u", api_nh->labels[0]);
1687 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6) &&
1688 !CHECK_FLAG(api_nh->flags,
1689 ZAPI_NEXTHOP_FLAG_EVPN)) {
1690 inet_ntop(AF_INET6, &api_nh->seg6_segs,
1691 sid_buf, sizeof(sid_buf));
1692 snprintf(segs_buf, sizeof(segs_buf), "segs %s",
1693 sid_buf);
1694 }
1695 if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN) &&
1696 !is_zero_mac(&api_nh->rmac))
1697 snprintf(eth_buf, sizeof(eth_buf), " RMAC %s",
1698 prefix_mac2str(&api_nh->rmac,
1699 buf1, sizeof(buf1)));
1700 zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s",
1701 i + 1, nh_buf, api_nh->ifindex,
1702 api_nh->vrf_id, api_nh->weight,
1703 label_buf, segs_buf, eth_buf);
1704 }
1705
1706 int recursion_flag = 0;
1707
1708 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1709 recursion_flag = 1;
1710
1711 zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)",
1712 __func__, p, (recursion_flag ? "" : "NOT "));
1713 }
1714 zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
1715 zclient, &api);
1716 }
1717
1718 /* Announce all routes of a table to zebra */
1719 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1720 {
1721 struct bgp_dest *dest;
1722 struct bgp_table *table;
1723 struct bgp_path_info *pi;
1724
1725 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1726 * know of this instance.
1727 */
1728 if (!bgp_install_info_to_zebra(bgp))
1729 return;
1730
1731 table = bgp->rib[afi][safi];
1732 if (!table)
1733 return;
1734
1735 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1736 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1737 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1738
1739 (pi->type == ZEBRA_ROUTE_BGP
1740 && (pi->sub_type == BGP_ROUTE_NORMAL
1741 || pi->sub_type == BGP_ROUTE_IMPORTED)))
1742
1743 bgp_zebra_announce(dest,
1744 bgp_dest_get_prefix(dest),
1745 pi, bgp, afi, safi);
1746 }
1747
1748 /* Announce routes of any bgp subtype of a table to zebra */
1749 void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi,
1750 safi_t safi)
1751 {
1752 struct bgp_dest *dest;
1753 struct bgp_table *table;
1754 struct bgp_path_info *pi;
1755
1756 if (!bgp_install_info_to_zebra(bgp))
1757 return;
1758
1759 table = bgp->rib[afi][safi];
1760 if (!table)
1761 return;
1762
1763 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
1764 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
1765 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1766 pi->type == ZEBRA_ROUTE_BGP)
1767 bgp_zebra_announce(dest,
1768 bgp_dest_get_prefix(dest),
1769 pi, bgp, afi, safi);
1770 }
1771
1772 void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info,
1773 struct bgp *bgp, safi_t safi)
1774 {
1775 struct zapi_route api;
1776 struct peer *peer;
1777
1778 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1779 * know of this instance.
1780 */
1781 if (!bgp_install_info_to_zebra(bgp))
1782 return;
1783
1784 if (safi == SAFI_FLOWSPEC) {
1785 peer = info->peer;
1786 bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1787 return;
1788 }
1789
1790 memset(&api, 0, sizeof(api));
1791 api.vrf_id = bgp->vrf_id;
1792 api.type = ZEBRA_ROUTE_BGP;
1793 api.safi = safi;
1794 api.prefix = *p;
1795
1796 if (info->attr->rmap_table_id) {
1797 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
1798 api.tableid = info->attr->rmap_table_id;
1799 }
1800
1801 if (bgp_debug_zebra(p))
1802 zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id,
1803 &api.prefix);
1804
1805 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1806 }
1807
1808 /* Withdraw all entries in a BGP instances RIB table from Zebra */
1809 void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi)
1810 {
1811 struct bgp_dest *dest;
1812 struct bgp_table *table;
1813 struct bgp_path_info *pi;
1814
1815 if (!bgp_install_info_to_zebra(bgp))
1816 return;
1817
1818 table = bgp->rib[afi][safi];
1819 if (!table)
1820 return;
1821
1822 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
1823 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1824 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
1825 && (pi->type == ZEBRA_ROUTE_BGP))
1826 bgp_zebra_withdraw(bgp_dest_get_prefix(dest),
1827 pi, bgp, safi);
1828 }
1829 }
1830 }
1831
1832 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1833 unsigned short instance)
1834 {
1835 struct list *red_list;
1836 struct listnode *node;
1837 struct bgp_redist *red;
1838
1839 red_list = bgp->redist[afi][type];
1840 if (!red_list)
1841 return (NULL);
1842
1843 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1844 if (red->instance == instance)
1845 return red;
1846
1847 return NULL;
1848 }
1849
1850 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1851 unsigned short instance)
1852 {
1853 struct list *red_list;
1854 struct bgp_redist *red;
1855
1856 red = bgp_redist_lookup(bgp, afi, type, instance);
1857 if (red)
1858 return red;
1859
1860 if (!bgp->redist[afi][type])
1861 bgp->redist[afi][type] = list_new();
1862
1863 red_list = bgp->redist[afi][type];
1864 red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1865 red->instance = instance;
1866
1867 listnode_add(red_list, red);
1868
1869 return red;
1870 }
1871
1872 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1873 unsigned short instance)
1874 {
1875 struct bgp_redist *red;
1876
1877 red = bgp_redist_lookup(bgp, afi, type, instance);
1878
1879 if (red) {
1880 listnode_delete(bgp->redist[afi][type], red);
1881 XFREE(MTYPE_BGP_REDIST, red);
1882 if (!bgp->redist[afi][type]->count)
1883 list_delete(&bgp->redist[afi][type]);
1884 }
1885 }
1886
1887 /* Other routes redistribution into BGP. */
1888 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1889 unsigned short instance, bool changed)
1890 {
1891 /* If redistribute options are changed call
1892 * bgp_redistribute_unreg() to reset the option and withdraw
1893 * the routes
1894 */
1895 if (changed)
1896 bgp_redistribute_unreg(bgp, afi, type, instance);
1897
1898 /* Return if already redistribute flag is set. */
1899 if (instance) {
1900 if (redist_check_instance(&zclient->mi_redist[afi][type],
1901 instance))
1902 return CMD_WARNING;
1903
1904 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1905 } else {
1906 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1907 return CMD_WARNING;
1908
1909 #ifdef ENABLE_BGP_VNC
1910 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
1911 vnc_export_bgp_enable(
1912 bgp, afi); /* only enables if mode bits cfg'd */
1913 }
1914 #endif
1915
1916 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1917 }
1918
1919 /*
1920 * Don't try to register if we're not connected to Zebra or Zebra
1921 * doesn't know of this instance.
1922 *
1923 * When we come up later well resend if needed.
1924 */
1925 if (!bgp_install_info_to_zebra(bgp))
1926 return CMD_SUCCESS;
1927
1928 if (BGP_DEBUG(zebra, ZEBRA))
1929 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1930 bgp->vrf_id, afi, zebra_route_string(type),
1931 instance);
1932
1933 /* Send distribute add message to zebra. */
1934 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1935 instance, bgp->vrf_id);
1936
1937 return CMD_SUCCESS;
1938 }
1939
1940 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1941 unsigned short instance)
1942 {
1943 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1944 * know of this instance.
1945 */
1946 if (!bgp_install_info_to_zebra(bgp))
1947 return -1;
1948
1949 if (BGP_DEBUG(zebra, ZEBRA))
1950 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1951 bgp->vrf_id, afi, zebra_route_string(type),
1952 instance);
1953
1954 /* Send distribute add message to zebra. */
1955 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1956 instance, bgp->vrf_id);
1957 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1958 instance, bgp->vrf_id);
1959
1960 return 0;
1961 }
1962
1963 /* Redistribute with route-map specification. */
1964 bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1965 struct route_map *route_map)
1966 {
1967 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1968 return false;
1969
1970 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1971 /* Decrement the count for existing routemap and
1972 * increment the count for new route map.
1973 */
1974 route_map_counter_decrement(red->rmap.map);
1975 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1976 red->rmap.map = route_map;
1977 route_map_counter_increment(red->rmap.map);
1978
1979 return true;
1980 }
1981
1982 /* Redistribute with metric specification. */
1983 bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1984 afi_t afi, int type, uint32_t metric)
1985 {
1986 struct bgp_dest *dest;
1987 struct bgp_path_info *pi;
1988
1989 if (red->redist_metric_flag && red->redist_metric == metric)
1990 return false;
1991
1992 red->redist_metric_flag = 1;
1993 red->redist_metric = metric;
1994
1995 for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest;
1996 dest = bgp_route_next(dest)) {
1997 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
1998 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1999 && pi->type == type
2000 && pi->instance == red->instance) {
2001 struct attr *old_attr;
2002 struct attr new_attr;
2003
2004 new_attr = *pi->attr;
2005 new_attr.med = red->redist_metric;
2006 old_attr = pi->attr;
2007 pi->attr = bgp_attr_intern(&new_attr);
2008 bgp_attr_unintern(&old_attr);
2009
2010 bgp_path_info_set_flag(dest, pi,
2011 BGP_PATH_ATTR_CHANGED);
2012 bgp_process(bgp, dest, afi, SAFI_UNICAST);
2013 }
2014 }
2015 }
2016
2017 return true;
2018 }
2019
2020 /* Unset redistribution. */
2021 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
2022 unsigned short instance)
2023 {
2024 struct bgp_redist *red;
2025
2026 red = bgp_redist_lookup(bgp, afi, type, instance);
2027 if (!red)
2028 return CMD_SUCCESS;
2029
2030 /* Return if zebra connection is disabled. */
2031 if (instance) {
2032 if (!redist_check_instance(&zclient->mi_redist[afi][type],
2033 instance))
2034 return CMD_WARNING;
2035 redist_del_instance(&zclient->mi_redist[afi][type], instance);
2036 } else {
2037 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
2038 return CMD_WARNING;
2039 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
2040 }
2041
2042 if (bgp_install_info_to_zebra(bgp)) {
2043 /* Send distribute delete message to zebra. */
2044 if (BGP_DEBUG(zebra, ZEBRA))
2045 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
2046 bgp->vrf_id, afi, zebra_route_string(type),
2047 instance);
2048 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
2049 type, instance, bgp->vrf_id);
2050 }
2051
2052 /* Withdraw redistributed routes from current BGP's routing table. */
2053 bgp_redistribute_withdraw(bgp, afi, type, instance);
2054
2055 return CMD_SUCCESS;
2056 }
2057
2058 /* Unset redistribution. */
2059 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
2060 unsigned short instance)
2061 {
2062 struct bgp_redist *red;
2063
2064 /*
2065 * vnc and vpn->vrf checks must be before red check because
2066 * they operate within bgpd irrespective of zebra connection
2067 * status. red lookup fails if there is no zebra connection.
2068 */
2069 #ifdef ENABLE_BGP_VNC
2070 if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) {
2071 vnc_export_bgp_disable(bgp, afi);
2072 }
2073 #endif
2074
2075 red = bgp_redist_lookup(bgp, afi, type, instance);
2076 if (!red)
2077 return CMD_SUCCESS;
2078
2079 bgp_redistribute_unreg(bgp, afi, type, instance);
2080
2081 /* Unset route-map. */
2082 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
2083 route_map_counter_decrement(red->rmap.map);
2084 red->rmap.map = NULL;
2085
2086 /* Unset metric. */
2087 red->redist_metric_flag = 0;
2088 red->redist_metric = 0;
2089
2090 bgp_redist_del(bgp, afi, type, instance);
2091
2092 return CMD_SUCCESS;
2093 }
2094
2095 void bgp_redistribute_redo(struct bgp *bgp)
2096 {
2097 afi_t afi;
2098 int i;
2099 struct list *red_list;
2100 struct listnode *node;
2101 struct bgp_redist *red;
2102
2103 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
2104 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
2105
2106 red_list = bgp->redist[afi][i];
2107 if (!red_list)
2108 continue;
2109
2110 for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) {
2111 bgp_redistribute_resend(bgp, afi, i,
2112 red->instance);
2113 }
2114 }
2115 }
2116 }
2117
2118 void bgp_zclient_reset(void)
2119 {
2120 zclient_reset(zclient);
2121 }
2122
2123 /* Register this instance with Zebra. Invoked upon connect (for
2124 * default instance) and when other VRFs are learnt (or created and
2125 * already learnt).
2126 */
2127 void bgp_zebra_instance_register(struct bgp *bgp)
2128 {
2129 /* Don't try to register if we're not connected to Zebra */
2130 if (!zclient || zclient->sock < 0)
2131 return;
2132
2133 if (BGP_DEBUG(zebra, ZEBRA))
2134 zlog_debug("Registering VRF %u", bgp->vrf_id);
2135
2136 /* Register for router-id, interfaces, redistributed routes. */
2137 zclient_send_reg_requests(zclient, bgp->vrf_id);
2138
2139 /* For EVPN instance, register to learn about VNIs, if appropriate. */
2140 if (bgp->advertise_all_vni)
2141 bgp_zebra_advertise_all_vni(bgp, 1);
2142
2143 bgp_nht_register_nexthops(bgp);
2144 }
2145
2146 /* Deregister this instance with Zebra. Invoked upon the instance
2147 * being deleted (default or VRF) and it is already registered.
2148 */
2149 void bgp_zebra_instance_deregister(struct bgp *bgp)
2150 {
2151 /* Don't try to deregister if we're not connected to Zebra */
2152 if (zclient->sock < 0)
2153 return;
2154
2155 if (BGP_DEBUG(zebra, ZEBRA))
2156 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2157
2158 /* For EVPN instance, unregister learning about VNIs, if appropriate. */
2159 if (bgp->advertise_all_vni)
2160 bgp_zebra_advertise_all_vni(bgp, 0);
2161
2162 /* Deregister for router-id, interfaces, redistributed routes. */
2163 zclient_send_dereg_requests(zclient, bgp->vrf_id);
2164 }
2165
2166 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
2167 {
2168 uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2169
2170 /* Don't try to initiate if we're not connected to Zebra */
2171 if (zclient->sock < 0)
2172 return;
2173
2174 if (BGP_DEBUG(zebra, ZEBRA))
2175 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2176 peer->host);
2177
2178 /*
2179 * If unnumbered peer (peer->ifp) call thru zapi to start RAs.
2180 * If we don't have an ifp pointer, call function to find the
2181 * ifps for a numbered enhe peer to turn RAs on.
2182 */
2183 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2184 peer->ifp, 1, ra_interval)
2185 : bgp_nht_reg_enhe_cap_intfs(peer);
2186 }
2187
2188 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
2189 {
2190 /* Don't try to terminate if we're not connected to Zebra */
2191 if (zclient->sock < 0)
2192 return;
2193
2194 if (BGP_DEBUG(zebra, ZEBRA))
2195 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2196 peer->host);
2197
2198 /*
2199 * If unnumbered peer (peer->ifp) call thru zapi to stop RAs.
2200 * If we don't have an ifp pointer, call function to find the
2201 * ifps for a numbered enhe peer to turn RAs off.
2202 */
2203 peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id,
2204 peer->ifp, 0, 0)
2205 : bgp_nht_dereg_enhe_cap_intfs(peer);
2206 }
2207
2208 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
2209 {
2210 struct stream *s = NULL;
2211
2212 /* Check socket. */
2213 if (!zclient || zclient->sock < 0)
2214 return 0;
2215
2216 /* Don't try to register if Zebra doesn't know of this instance. */
2217 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2218 if (BGP_DEBUG(zebra, ZEBRA))
2219 zlog_debug(
2220 "%s: No zebra instance to talk to, cannot advertise subnet",
2221 __func__);
2222 return 0;
2223 }
2224
2225 s = zclient->obuf;
2226 stream_reset(s);
2227
2228 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
2229 stream_putc(s, advertise);
2230 stream_put3(s, vni);
2231 stream_putw_at(s, 0, stream_get_endp(s));
2232
2233 return zclient_send_message(zclient);
2234 }
2235
2236 int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni)
2237 {
2238 struct stream *s = NULL;
2239
2240 /* Check socket. */
2241 if (!zclient || zclient->sock < 0)
2242 return 0;
2243
2244 /* Don't try to register if Zebra doesn't know of this instance. */
2245 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2246 return 0;
2247
2248 s = zclient->obuf;
2249 stream_reset(s);
2250
2251 zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id);
2252 stream_putc(s, advertise);
2253 stream_putl(s, vni);
2254 stream_putw_at(s, 0, stream_get_endp(s));
2255
2256 return zclient_send_message(zclient);
2257 }
2258
2259 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
2260 {
2261 struct stream *s = NULL;
2262
2263 /* Check socket. */
2264 if (!zclient || zclient->sock < 0)
2265 return 0;
2266
2267 /* Don't try to register if Zebra doesn't know of this instance. */
2268 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2269 if (BGP_DEBUG(zebra, ZEBRA))
2270 zlog_debug(
2271 "%s: No zebra instance to talk to, not installing gw_macip",
2272 __func__);
2273 return 0;
2274 }
2275
2276 s = zclient->obuf;
2277 stream_reset(s);
2278
2279 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
2280 stream_putc(s, advertise);
2281 stream_putl(s, vni);
2282 stream_putw_at(s, 0, stream_get_endp(s));
2283
2284 return zclient_send_message(zclient);
2285 }
2286
2287 int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
2288 enum vxlan_flood_control flood_ctrl)
2289 {
2290 struct stream *s;
2291
2292 /* Check socket. */
2293 if (!zclient || zclient->sock < 0)
2294 return 0;
2295
2296 /* Don't try to register if Zebra doesn't know of this instance. */
2297 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
2298 if (BGP_DEBUG(zebra, ZEBRA))
2299 zlog_debug(
2300 "%s: No zebra instance to talk to, not installing all vni",
2301 __func__);
2302 return 0;
2303 }
2304
2305 s = zclient->obuf;
2306 stream_reset(s);
2307
2308 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
2309 stream_putc(s, flood_ctrl);
2310 stream_putw_at(s, 0, stream_get_endp(s));
2311
2312 return zclient_send_message(zclient);
2313 }
2314
2315 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
2316 {
2317 struct stream *s;
2318
2319 /* Check socket. */
2320 if (!zclient || zclient->sock < 0)
2321 return 0;
2322
2323 /* Don't try to register if Zebra doesn't know of this instance. */
2324 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2325 return 0;
2326
2327 s = zclient->obuf;
2328 stream_reset(s);
2329
2330 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2331 stream_putc(s, advertise);
2332 /* Also inform current BUM handling setting. This is really
2333 * relevant only when 'advertise' is set.
2334 */
2335 stream_putc(s, bgp->vxlan_flood_ctrl);
2336 stream_putw_at(s, 0, stream_get_endp(s));
2337
2338 return zclient_send_message(zclient);
2339 }
2340
2341 int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2342 {
2343 struct stream *s;
2344
2345 /* Check socket. */
2346 if (!zclient || zclient->sock < 0)
2347 return 0;
2348
2349 /* Don't try to register if Zebra doesn't know of this instance. */
2350 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2351 return 0;
2352
2353 if (BGP_DEBUG(zebra, ZEBRA))
2354 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2355 bgp->evpn_info->dup_addr_detect ?
2356 "enable" : "disable",
2357 bgp->evpn_info->dad_max_moves,
2358 bgp->evpn_info->dad_time,
2359 bgp->evpn_info->dad_freeze ?
2360 "enable" : "disable",
2361 bgp->evpn_info->dad_freeze_time);
2362
2363 s = zclient->obuf;
2364 stream_reset(s);
2365 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2366 bgp->vrf_id);
2367 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2368 stream_putl(s, bgp->evpn_info->dad_time);
2369 stream_putl(s, bgp->evpn_info->dad_max_moves);
2370 stream_putl(s, bgp->evpn_info->dad_freeze);
2371 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2372 stream_putw_at(s, 0, stream_get_endp(s));
2373
2374 return zclient_send_message(zclient);
2375 }
2376
2377 static int rule_notify_owner(ZAPI_CALLBACK_ARGS)
2378 {
2379 uint32_t seqno, priority, unique;
2380 enum zapi_rule_notify_owner note;
2381 struct bgp_pbr_action *bgp_pbra;
2382 struct bgp_pbr_rule *bgp_pbr = NULL;
2383 char ifname[INTERFACE_NAMSIZ + 1];
2384
2385 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2386 ifname, &note))
2387 return -1;
2388
2389 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
2390 if (!bgp_pbra) {
2391 /* look in bgp pbr rule */
2392 bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique);
2393 if (!bgp_pbr && note != ZAPI_RULE_REMOVED) {
2394 if (BGP_DEBUG(zebra, ZEBRA))
2395 zlog_debug("%s: Fail to look BGP rule (%u)",
2396 __func__, unique);
2397 return 0;
2398 }
2399 }
2400
2401 switch (note) {
2402 case ZAPI_RULE_FAIL_INSTALL:
2403 if (BGP_DEBUG(zebra, ZEBRA))
2404 zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__);
2405 if (bgp_pbra) {
2406 bgp_pbra->installed = false;
2407 bgp_pbra->install_in_progress = false;
2408 } else {
2409 bgp_pbr->installed = false;
2410 bgp_pbr->install_in_progress = false;
2411 }
2412 break;
2413 case ZAPI_RULE_INSTALLED:
2414 if (bgp_pbra) {
2415 bgp_pbra->installed = true;
2416 bgp_pbra->install_in_progress = false;
2417 } else {
2418 struct bgp_path_info *path;
2419 struct bgp_path_info_extra *extra;
2420
2421 bgp_pbr->installed = true;
2422 bgp_pbr->install_in_progress = false;
2423 bgp_pbr->action->refcnt++;
2424 /* link bgp_info to bgp_pbr */
2425 path = (struct bgp_path_info *)bgp_pbr->path;
2426 extra = bgp_path_info_extra_get(path);
2427 listnode_add_force(&extra->bgp_fs_iprule,
2428 bgp_pbr);
2429 }
2430 if (BGP_DEBUG(zebra, ZEBRA))
2431 zlog_debug("%s: Received RULE_INSTALLED", __func__);
2432 break;
2433 case ZAPI_RULE_FAIL_REMOVE:
2434 case ZAPI_RULE_REMOVED:
2435 if (BGP_DEBUG(zebra, ZEBRA))
2436 zlog_debug("%s: Received RULE REMOVED", __func__);
2437 break;
2438 }
2439
2440 return 0;
2441 }
2442
2443 static int ipset_notify_owner(ZAPI_CALLBACK_ARGS)
2444 {
2445 uint32_t unique;
2446 enum zapi_ipset_notify_owner note;
2447 struct bgp_pbr_match *bgp_pbim;
2448
2449 if (!zapi_ipset_notify_decode(zclient->ibuf,
2450 &unique,
2451 &note))
2452 return -1;
2453
2454 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2455 if (!bgp_pbim) {
2456 if (BGP_DEBUG(zebra, ZEBRA))
2457 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2458 __func__, note, unique);
2459 return 0;
2460 }
2461
2462 switch (note) {
2463 case ZAPI_IPSET_FAIL_INSTALL:
2464 if (BGP_DEBUG(zebra, ZEBRA))
2465 zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__);
2466 bgp_pbim->installed = false;
2467 bgp_pbim->install_in_progress = false;
2468 break;
2469 case ZAPI_IPSET_INSTALLED:
2470 bgp_pbim->installed = true;
2471 bgp_pbim->install_in_progress = false;
2472 if (BGP_DEBUG(zebra, ZEBRA))
2473 zlog_debug("%s: Received IPSET_INSTALLED", __func__);
2474 break;
2475 case ZAPI_IPSET_FAIL_REMOVE:
2476 case ZAPI_IPSET_REMOVED:
2477 if (BGP_DEBUG(zebra, ZEBRA))
2478 zlog_debug("%s: Received IPSET REMOVED", __func__);
2479 break;
2480 }
2481
2482 return 0;
2483 }
2484
2485 static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS)
2486 {
2487 uint32_t unique;
2488 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2489 enum zapi_ipset_entry_notify_owner note;
2490 struct bgp_pbr_match_entry *bgp_pbime;
2491
2492 if (!zapi_ipset_entry_notify_decode(
2493 zclient->ibuf,
2494 &unique,
2495 ipset_name,
2496 &note))
2497 return -1;
2498 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2499 ipset_name,
2500 unique);
2501 if (!bgp_pbime) {
2502 if (BGP_DEBUG(zebra, ZEBRA))
2503 zlog_debug(
2504 "%s: Fail to look BGP match entry (%u, ID %u)",
2505 __func__, note, unique);
2506 return 0;
2507 }
2508
2509 switch (note) {
2510 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2511 if (BGP_DEBUG(zebra, ZEBRA))
2512 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2513 __func__);
2514 bgp_pbime->installed = false;
2515 bgp_pbime->install_in_progress = false;
2516 break;
2517 case ZAPI_IPSET_ENTRY_INSTALLED:
2518 {
2519 struct bgp_path_info *path;
2520 struct bgp_path_info_extra *extra;
2521
2522 bgp_pbime->installed = true;
2523 bgp_pbime->install_in_progress = false;
2524 if (BGP_DEBUG(zebra, ZEBRA))
2525 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2526 __func__);
2527 /* link bgp_path_info to bpme */
2528 path = (struct bgp_path_info *)bgp_pbime->path;
2529 extra = bgp_path_info_extra_get(path);
2530 listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime);
2531 }
2532 break;
2533 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
2534 case ZAPI_IPSET_ENTRY_REMOVED:
2535 if (BGP_DEBUG(zebra, ZEBRA))
2536 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2537 __func__);
2538 break;
2539 }
2540 return 0;
2541 }
2542
2543 static int iptable_notify_owner(ZAPI_CALLBACK_ARGS)
2544 {
2545 uint32_t unique;
2546 enum zapi_iptable_notify_owner note;
2547 struct bgp_pbr_match *bgpm;
2548
2549 if (!zapi_iptable_notify_decode(
2550 zclient->ibuf,
2551 &unique,
2552 &note))
2553 return -1;
2554 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2555 if (!bgpm) {
2556 if (BGP_DEBUG(zebra, ZEBRA))
2557 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2558 __func__, note, unique);
2559 return 0;
2560 }
2561 switch (note) {
2562 case ZAPI_IPTABLE_FAIL_INSTALL:
2563 if (BGP_DEBUG(zebra, ZEBRA))
2564 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2565 __func__);
2566 bgpm->installed_in_iptable = false;
2567 bgpm->install_iptable_in_progress = false;
2568 break;
2569 case ZAPI_IPTABLE_INSTALLED:
2570 bgpm->installed_in_iptable = true;
2571 bgpm->install_iptable_in_progress = false;
2572 if (BGP_DEBUG(zebra, ZEBRA))
2573 zlog_debug("%s: Received IPTABLE_INSTALLED", __func__);
2574 bgpm->action->refcnt++;
2575 break;
2576 case ZAPI_IPTABLE_FAIL_REMOVE:
2577 case ZAPI_IPTABLE_REMOVED:
2578 if (BGP_DEBUG(zebra, ZEBRA))
2579 zlog_debug("%s: Received IPTABLE REMOVED", __func__);
2580 break;
2581 }
2582 return 0;
2583 }
2584
2585 /* Process route notification messages from RIB */
2586 static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient,
2587 zebra_size_t length, vrf_id_t vrf_id)
2588 {
2589 struct prefix p;
2590 enum zapi_route_notify_owner note;
2591 uint32_t table_id;
2592 afi_t afi;
2593 safi_t safi;
2594 struct bgp_dest *dest;
2595 struct bgp *bgp;
2596 struct bgp_path_info *pi, *new_select;
2597
2598 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, &note,
2599 &afi, &safi)) {
2600 zlog_err("%s : error in msg decode", __func__);
2601 return -1;
2602 }
2603
2604 /* Get the bgp instance */
2605 bgp = bgp_lookup_by_vrf_id(vrf_id);
2606 if (!bgp) {
2607 flog_err(EC_BGP_INVALID_BGP_INSTANCE,
2608 "%s : bgp instance not found vrf %d", __func__,
2609 vrf_id);
2610 return -1;
2611 }
2612
2613 /* Find the bgp route node */
2614 dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p,
2615 &bgp->vrf_prd);
2616 if (!dest)
2617 return -1;
2618
2619 switch (note) {
2620 case ZAPI_ROUTE_INSTALLED:
2621 new_select = NULL;
2622 /* Clear the flags so that route can be processed */
2623 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2624 SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2625 if (BGP_DEBUG(zebra, ZEBRA))
2626 zlog_debug("route %pRN : INSTALLED", dest);
2627 /* Find the best route */
2628 for (pi = dest->info; pi; pi = pi->next) {
2629 /* Process aggregate route */
2630 bgp_aggregate_increment(bgp, &p, pi, afi, safi);
2631 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2632 new_select = pi;
2633 }
2634 /* Advertise the route */
2635 if (new_select)
2636 group_announce_route(bgp, afi, safi, dest, new_select);
2637 else {
2638 flog_err(EC_BGP_INVALID_ROUTE,
2639 "selected route %pRN not found", dest);
2640
2641 bgp_dest_unlock_node(dest);
2642 return -1;
2643 }
2644 break;
2645 case ZAPI_ROUTE_REMOVED:
2646 /* Route deleted from dataplane, reset the installed flag
2647 * so that route can be reinstalled when client sends
2648 * route add later
2649 */
2650 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2651 if (BGP_DEBUG(zebra, ZEBRA))
2652 zlog_debug("route %pRN: Removed from Fib", dest);
2653 break;
2654 case ZAPI_ROUTE_FAIL_INSTALL:
2655 new_select = NULL;
2656 if (BGP_DEBUG(zebra, ZEBRA))
2657 zlog_debug("route: %pRN Failed to Install into Fib",
2658 dest);
2659 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2660 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2661 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2662 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2663 new_select = pi;
2664 }
2665 if (new_select)
2666 group_announce_route(bgp, afi, safi, dest, new_select);
2667 /* Error will be logged by zebra module */
2668 break;
2669 case ZAPI_ROUTE_BETTER_ADMIN_WON:
2670 if (BGP_DEBUG(zebra, ZEBRA))
2671 zlog_debug("route: %pRN removed due to better admin won",
2672 dest);
2673 new_select = NULL;
2674 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
2675 UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED);
2676 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2677 bgp_aggregate_decrement(bgp, &p, pi, afi, safi);
2678 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2679 new_select = pi;
2680 }
2681 if (new_select)
2682 group_announce_route(bgp, afi, safi, dest, new_select);
2683 /* No action required */
2684 break;
2685 case ZAPI_ROUTE_REMOVE_FAIL:
2686 zlog_warn("%s: Route %pRN failure to remove",
2687 __func__, dest);
2688 break;
2689 }
2690
2691 bgp_dest_unlock_node(dest);
2692 return 0;
2693 }
2694
2695 /* this function is used to forge ip rule,
2696 * - either for iptable/ipset using fwmark id
2697 * - or for sample ip rule cmd
2698 */
2699 static void bgp_encode_pbr_rule_action(struct stream *s,
2700 struct bgp_pbr_action *pbra,
2701 struct bgp_pbr_rule *pbr)
2702 {
2703 struct prefix pfx;
2704 uint8_t fam = AF_INET;
2705 char ifname[INTERFACE_NAMSIZ];
2706
2707 if (pbra->nh.type == NEXTHOP_TYPE_IPV6)
2708 fam = AF_INET6;
2709 stream_putl(s, 0); /* seqno unused */
2710 if (pbr)
2711 stream_putl(s, pbr->priority);
2712 else
2713 stream_putl(s, 0);
2714 /* ruleno unused - priority change
2715 * ruleno permits distinguishing various FS PBR entries
2716 * - FS PBR entries based on ipset/iptables
2717 * - FS PBR entries based on iprule
2718 * the latter may contain default routing information injected by FS
2719 */
2720 if (pbr)
2721 stream_putl(s, pbr->unique);
2722 else
2723 stream_putl(s, pbra->unique);
2724 stream_putc(s, 0); /* ip protocol being used */
2725 if (pbr && pbr->flags & MATCH_IP_SRC_SET)
2726 memcpy(&pfx, &(pbr->src), sizeof(struct prefix));
2727 else {
2728 memset(&pfx, 0, sizeof(pfx));
2729 pfx.family = fam;
2730 }
2731 stream_putc(s, pfx.family);
2732 stream_putc(s, pfx.prefixlen);
2733 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2734
2735 stream_putw(s, 0); /* src port */
2736
2737 if (pbr && pbr->flags & MATCH_IP_DST_SET)
2738 memcpy(&pfx, &(pbr->dst), sizeof(struct prefix));
2739 else {
2740 memset(&pfx, 0, sizeof(pfx));
2741 pfx.family = fam;
2742 }
2743 stream_putc(s, pfx.family);
2744 stream_putc(s, pfx.prefixlen);
2745 stream_put(s, &pfx.u.prefix, prefix_blen(&pfx));
2746
2747 stream_putw(s, 0); /* dst port */
2748 stream_putc(s, 0); /* dsfield */
2749 /* if pbr present, fwmark is not used */
2750 if (pbr)
2751 stream_putl(s, 0);
2752 else
2753 stream_putl(s, pbra->fwmark); /* fwmark */
2754
2755 stream_putl(s, 0); /* queue id */
2756 stream_putw(s, 0); /* vlan_id */
2757 stream_putw(s, 0); /* vlan_flags */
2758 stream_putw(s, 0); /* pcp */
2759
2760 stream_putl(s, pbra->table_id);
2761
2762 memset(ifname, 0, sizeof(ifname));
2763 stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */
2764 }
2765
2766 static void bgp_encode_pbr_ipset_match(struct stream *s,
2767 struct bgp_pbr_match *pbim)
2768 {
2769 stream_putl(s, pbim->unique);
2770 stream_putl(s, pbim->type);
2771 stream_putc(s, pbim->family);
2772 stream_put(s, pbim->ipset_name,
2773 ZEBRA_IPSET_NAME_SIZE);
2774 }
2775
2776 static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2777 struct bgp_pbr_match_entry *pbime)
2778 {
2779 stream_putl(s, pbime->unique);
2780 /* check that back pointer is not null */
2781 stream_put(s, pbime->backpointer->ipset_name,
2782 ZEBRA_IPSET_NAME_SIZE);
2783
2784 stream_putc(s, pbime->src.family);
2785 stream_putc(s, pbime->src.prefixlen);
2786 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2787
2788 stream_putc(s, pbime->dst.family);
2789 stream_putc(s, pbime->dst.prefixlen);
2790 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
2791
2792 stream_putw(s, pbime->src_port_min);
2793 stream_putw(s, pbime->src_port_max);
2794 stream_putw(s, pbime->dst_port_min);
2795 stream_putw(s, pbime->dst_port_max);
2796 stream_putc(s, pbime->proto);
2797 }
2798
2799 static void bgp_encode_pbr_iptable_match(struct stream *s,
2800 struct bgp_pbr_action *bpa,
2801 struct bgp_pbr_match *pbm)
2802 {
2803 stream_putl(s, pbm->unique2);
2804
2805 stream_putl(s, pbm->type);
2806
2807 stream_putl(s, pbm->flags);
2808
2809 /* TODO: correlate with what is contained
2810 * into bgp_pbr_action.
2811 * currently only forward supported
2812 */
2813 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2814 stream_putl(s, ZEBRA_IPTABLES_DROP);
2815 else
2816 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2817 stream_putl(s, bpa->fwmark);
2818 stream_put(s, pbm->ipset_name,
2819 ZEBRA_IPSET_NAME_SIZE);
2820 stream_putc(s, pbm->family);
2821 stream_putw(s, pbm->pkt_len_min);
2822 stream_putw(s, pbm->pkt_len_max);
2823 stream_putw(s, pbm->tcp_flags);
2824 stream_putw(s, pbm->tcp_mask_flags);
2825 stream_putc(s, pbm->dscp_value);
2826 stream_putc(s, pbm->fragment);
2827 stream_putc(s, pbm->protocol);
2828 stream_putw(s, pbm->flow_label);
2829 }
2830
2831 /* BGP has established connection with Zebra. */
2832 static void bgp_zebra_connected(struct zclient *zclient)
2833 {
2834 struct bgp *bgp;
2835
2836 zclient_num_connects++; /* increment even if not responding */
2837
2838 /* Send the client registration */
2839 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
2840
2841 /* At this point, we may or may not have BGP instances configured, but
2842 * we're only interested in the default VRF (others wouldn't have learnt
2843 * the VRF from Zebra yet.)
2844 */
2845 bgp = bgp_get_default();
2846 if (!bgp)
2847 return;
2848
2849 bgp_zebra_instance_register(bgp);
2850
2851 /* tell label pool that zebra is connected */
2852 bgp_lp_event_zebra_up();
2853
2854 /* TODO - What if we have peers and networks configured, do we have to
2855 * kick-start them?
2856 */
2857 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer);
2858 }
2859
2860 static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS)
2861 {
2862 esi_t esi;
2863 struct bgp *bgp = NULL;
2864 struct stream *s = NULL;
2865 char buf[ESI_STR_LEN];
2866 struct in_addr originator_ip;
2867 uint8_t active;
2868 uint8_t bypass;
2869 uint16_t df_pref;
2870
2871 bgp = bgp_lookup_by_vrf_id(vrf_id);
2872 if (!bgp)
2873 return 0;
2874
2875 s = zclient->ibuf;
2876 stream_get(&esi, s, sizeof(esi_t));
2877 originator_ip.s_addr = stream_get_ipv4(s);
2878 active = stream_getc(s);
2879 df_pref = stream_getw(s);
2880 bypass = stream_getc(s);
2881
2882 if (BGP_DEBUG(zebra, ZEBRA))
2883 zlog_debug(
2884 "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s",
2885 esi_to_str(&esi, buf, sizeof(buf)), &originator_ip,
2886 active, df_pref, bypass ? "bypass" : "");
2887
2888 frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip,
2889 active, bypass, df_pref);
2890
2891 bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref,
2892 !!bypass);
2893
2894 return 0;
2895 }
2896
2897 static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS)
2898 {
2899 esi_t esi;
2900 struct bgp *bgp = NULL;
2901 struct stream *s = NULL;
2902 char buf[ESI_STR_LEN];
2903
2904 memset(&esi, 0, sizeof(esi_t));
2905 bgp = bgp_lookup_by_vrf_id(vrf_id);
2906 if (!bgp)
2907 return 0;
2908
2909 s = zclient->ibuf;
2910 stream_get(&esi, s, sizeof(esi_t));
2911
2912 if (BGP_DEBUG(zebra, ZEBRA))
2913 zlog_debug("Rx del ESI %s",
2914 esi_to_str(&esi, buf, sizeof(buf)));
2915
2916 frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi);
2917
2918 bgp_evpn_local_es_del(bgp, &esi);
2919
2920 return 0;
2921 }
2922
2923 static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS)
2924 {
2925 esi_t esi;
2926 vni_t vni;
2927 struct bgp *bgp;
2928 struct stream *s;
2929 char buf[ESI_STR_LEN];
2930
2931 bgp = bgp_lookup_by_vrf_id(vrf_id);
2932 if (!bgp)
2933 return 0;
2934
2935 s = zclient->ibuf;
2936 stream_get(&esi, s, sizeof(esi_t));
2937 vni = stream_getl(s);
2938
2939 if (BGP_DEBUG(zebra, ZEBRA))
2940 zlog_debug("Rx %s ESI %s VNI %u",
2941 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
2942 esi_to_str(&esi, buf, sizeof(buf)), vni);
2943
2944 if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) {
2945 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni);
2946
2947 bgp_evpn_local_es_evi_add(bgp, &esi, vni);
2948 } else {
2949 frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni);
2950
2951 bgp_evpn_local_es_evi_del(bgp, &esi, vni);
2952 }
2953
2954 return 0;
2955 }
2956
2957 static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS)
2958 {
2959 int filter = 0;
2960 vni_t l3vni = 0;
2961 struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} };
2962 struct in_addr originator_ip;
2963 struct stream *s;
2964 ifindex_t svi_ifindex;
2965 bool is_anycast_mac = false;
2966
2967 memset(&svi_rmac, 0, sizeof(svi_rmac));
2968 memset(&originator_ip, 0, sizeof(originator_ip));
2969 s = zclient->ibuf;
2970 l3vni = stream_getl(s);
2971 if (cmd == ZEBRA_L3VNI_ADD) {
2972 stream_get(&svi_rmac, s, sizeof(struct ethaddr));
2973 originator_ip.s_addr = stream_get_ipv4(s);
2974 stream_get(&filter, s, sizeof(int));
2975 svi_ifindex = stream_getl(s);
2976 stream_get(&vrr_rmac, s, sizeof(struct ethaddr));
2977 is_anycast_mac = stream_getl(s);
2978
2979 if (BGP_DEBUG(zebra, ZEBRA))
2980 zlog_debug(
2981 "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u",
2982 vrf_id_to_name(vrf_id), l3vni, &svi_rmac,
2983 &vrr_rmac,
2984 filter ? "prefix-routes-only" : "none",
2985 svi_ifindex);
2986
2987 frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id,
2988 &svi_rmac, &vrr_rmac, filter, originator_ip,
2989 svi_ifindex, is_anycast_mac);
2990
2991 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac,
2992 originator_ip, filter, svi_ifindex,
2993 is_anycast_mac);
2994 } else {
2995 if (BGP_DEBUG(zebra, ZEBRA))
2996 zlog_debug("Rx L3-VNI DEL VRF %s VNI %u",
2997 vrf_id_to_name(vrf_id), l3vni);
2998
2999 frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id);
3000
3001 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
3002 }
3003
3004 return 0;
3005 }
3006
3007 static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS)
3008 {
3009 struct stream *s;
3010 vni_t vni;
3011 struct bgp *bgp;
3012 struct in_addr vtep_ip = {INADDR_ANY};
3013 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
3014 struct in_addr mcast_grp = {INADDR_ANY};
3015 ifindex_t svi_ifindex = 0;
3016
3017 s = zclient->ibuf;
3018 vni = stream_getl(s);
3019 if (cmd == ZEBRA_VNI_ADD) {
3020 vtep_ip.s_addr = stream_get_ipv4(s);
3021 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
3022 mcast_grp.s_addr = stream_get_ipv4(s);
3023 stream_get(&svi_ifindex, s, sizeof(ifindex_t));
3024 }
3025
3026 bgp = bgp_lookup_by_vrf_id(vrf_id);
3027 if (!bgp)
3028 return 0;
3029
3030 if (BGP_DEBUG(zebra, ZEBRA))
3031 zlog_debug(
3032 "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u",
3033 (cmd == ZEBRA_VNI_ADD) ? "add" : "del",
3034 vrf_id_to_name(vrf_id), vni,
3035 vrf_id_to_name(tenant_vrf_id), svi_ifindex);
3036
3037 if (cmd == ZEBRA_VNI_ADD) {
3038 frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip,
3039 tenant_vrf_id, mcast_grp);
3040
3041 return bgp_evpn_local_vni_add(
3042 bgp, vni,
3043 vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id,
3044 tenant_vrf_id, mcast_grp, svi_ifindex);
3045 } else {
3046 frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni);
3047
3048 return bgp_evpn_local_vni_del(bgp, vni);
3049 }
3050 }
3051
3052 static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS)
3053 {
3054 struct stream *s;
3055 vni_t vni;
3056 struct bgp *bgp;
3057 struct ethaddr mac;
3058 struct ipaddr ip;
3059 int ipa_len;
3060 uint8_t flags = 0;
3061 uint32_t seqnum = 0;
3062 int state = 0;
3063 char buf2[ESI_STR_LEN];
3064 esi_t esi;
3065
3066 memset(&ip, 0, sizeof(ip));
3067 s = zclient->ibuf;
3068 vni = stream_getl(s);
3069 stream_get(&mac.octet, s, ETH_ALEN);
3070 ipa_len = stream_getl(s);
3071 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
3072 && ipa_len != IPV6_MAX_BYTELEN) {
3073 flog_err(EC_BGP_MACIP_LEN,
3074 "%u:Recv MACIP %s with invalid IP addr length %d",
3075 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del",
3076 ipa_len);
3077 return -1;
3078 }
3079
3080 if (ipa_len) {
3081 ip.ipa_type =
3082 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
3083 stream_get(&ip.ip.addr, s, ipa_len);
3084 }
3085 if (cmd == ZEBRA_MACIP_ADD) {
3086 flags = stream_getc(s);
3087 seqnum = stream_getl(s);
3088 stream_get(&esi, s, sizeof(esi_t));
3089 } else {
3090 state = stream_getl(s);
3091 memset(&esi, 0, sizeof(esi_t));
3092 }
3093
3094 bgp = bgp_lookup_by_vrf_id(vrf_id);
3095 if (!bgp)
3096 return 0;
3097
3098 if (BGP_DEBUG(zebra, ZEBRA))
3099 zlog_debug(
3100 "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s",
3101 vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags,
3102 &mac, &ip, vni, seqnum, state,
3103 esi_to_str(&esi, buf2, sizeof(buf2)));
3104
3105 if (cmd == ZEBRA_MACIP_ADD) {
3106 frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip,
3107 flags, seqnum, &esi);
3108
3109 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
3110 flags, seqnum, &esi);
3111 } else {
3112 frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip,
3113 state);
3114
3115 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state);
3116 }
3117 }
3118
3119 static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS)
3120 {
3121 struct stream *s = NULL;
3122 struct bgp *bgp_vrf = NULL;
3123 struct prefix p;
3124
3125 memset(&p, 0, sizeof(p));
3126 s = zclient->ibuf;
3127 stream_get(&p, s, sizeof(struct prefix));
3128
3129 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3130 if (!bgp_vrf)
3131 return 0;
3132
3133 if (BGP_DEBUG(zebra, ZEBRA))
3134 zlog_debug("Recv prefix %pFX %s on vrf %s", &p,
3135 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
3136 vrf_id_to_name(vrf_id));
3137
3138 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
3139
3140 if (p.family == AF_INET)
3141 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3142 AFI_IP, SAFI_UNICAST);
3143 else
3144 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
3145 AFI_IP6, SAFI_UNICAST);
3146
3147 } else {
3148 if (p.family == AF_INET)
3149 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
3150 SAFI_UNICAST);
3151 else
3152 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
3153 SAFI_UNICAST);
3154 }
3155 return 0;
3156 }
3157
3158 static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS)
3159 {
3160 struct stream *s = NULL;
3161 uint8_t response_keep;
3162 uint32_t first;
3163 uint32_t last;
3164 uint8_t proto;
3165 unsigned short instance;
3166
3167 s = zclient->ibuf;
3168 STREAM_GETC(s, proto);
3169 STREAM_GETW(s, instance);
3170 STREAM_GETC(s, response_keep);
3171 STREAM_GETL(s, first);
3172 STREAM_GETL(s, last);
3173
3174 if (zclient->redist_default != proto) {
3175 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
3176 proto);
3177 return 0;
3178 }
3179 if (zclient->instance != instance) {
3180 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
3181 proto);
3182 return 0;
3183 }
3184
3185 if (first > last ||
3186 first < MPLS_LABEL_UNRESERVED_MIN ||
3187 last > MPLS_LABEL_UNRESERVED_MAX) {
3188
3189 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
3190 __func__, first, last);
3191 return 0;
3192 }
3193 if (BGP_DEBUG(zebra, ZEBRA)) {
3194 zlog_debug("Label Chunk assign: %u - %u (%u) ",
3195 first, last, response_keep);
3196 }
3197
3198 bgp_lp_event_chunk(response_keep, first, last);
3199
3200 return 0;
3201
3202 stream_failure: /* for STREAM_GETX */
3203 return -1;
3204 }
3205
3206 extern struct zebra_privs_t bgpd_privs;
3207
3208 static int bgp_ifp_create(struct interface *ifp)
3209 {
3210 struct bgp *bgp;
3211
3212 if (BGP_DEBUG(zebra, ZEBRA))
3213 zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id,
3214 ifp->name);
3215
3216 bgp = ifp->vrf->info;
3217 if (!bgp)
3218 return 0;
3219
3220 bgp_mac_add_mac_entry(ifp);
3221
3222 bgp_update_interface_nbrs(bgp, ifp, ifp);
3223 hook_call(bgp_vrf_status_changed, bgp, ifp);
3224 return 0;
3225 }
3226
3227 static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS)
3228 {
3229 struct stream *s = NULL;
3230 struct bgp *bgp = bgp_get_default();
3231 struct listnode *node;
3232 struct srv6_locator_chunk *c;
3233 struct srv6_locator_chunk *chunk = srv6_locator_chunk_alloc();
3234
3235 s = zclient->ibuf;
3236 zapi_srv6_locator_chunk_decode(s, chunk);
3237
3238 if (strcmp(bgp->srv6_locator_name, chunk->locator_name) != 0) {
3239 zlog_err("%s: Locator name unmatch %s:%s", __func__,
3240 bgp->srv6_locator_name, chunk->locator_name);
3241 srv6_locator_chunk_free(&chunk);
3242 return 0;
3243 }
3244
3245 for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) {
3246 if (!prefix_cmp(&c->prefix, &chunk->prefix)) {
3247 srv6_locator_chunk_free(&chunk);
3248 return 0;
3249 }
3250 }
3251
3252 listnode_add(bgp->srv6_locator_chunks, chunk);
3253 vpn_leak_postchange_all();
3254 return 0;
3255 }
3256
3257 static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS)
3258 {
3259 struct srv6_locator loc = {};
3260 struct bgp *bgp = bgp_get_default();
3261 const char *loc_name = bgp->srv6_locator_name;
3262
3263 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3264 return -1;
3265
3266 if (!bgp || !bgp->srv6_enabled)
3267 return 0;
3268
3269 if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0)
3270 return -1;
3271
3272 return 0;
3273 }
3274
3275 static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
3276 {
3277 struct srv6_locator loc = {};
3278 struct bgp *bgp = bgp_get_default();
3279 struct listnode *node, *nnode;
3280 struct srv6_locator_chunk *chunk, *tovpn_sid_locator;
3281 struct bgp_srv6_function *func;
3282 struct bgp *bgp_vrf;
3283 struct in6_addr *tovpn_sid;
3284 struct prefix_ipv6 tmp_prefi;
3285
3286 if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0)
3287 return -1;
3288
3289 // refresh chunks
3290 for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk))
3291 if (prefix_match((struct prefix *)&loc.prefix,
3292 (struct prefix *)&chunk->prefix)) {
3293 listnode_delete(bgp->srv6_locator_chunks, chunk);
3294 srv6_locator_chunk_free(&chunk);
3295 }
3296
3297 // refresh functions
3298 for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
3299 tmp_prefi.family = AF_INET6;
3300 tmp_prefi.prefixlen = 128;
3301 tmp_prefi.prefix = func->sid;
3302 if (prefix_match((struct prefix *)&loc.prefix,
3303 (struct prefix *)&tmp_prefi)) {
3304 listnode_delete(bgp->srv6_functions, func);
3305 XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
3306 }
3307 }
3308
3309 // refresh tovpn_sid
3310 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3311 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3312 continue;
3313
3314 // refresh vpnv4 tovpn_sid
3315 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid;
3316 if (tovpn_sid) {
3317 tmp_prefi.family = AF_INET6;
3318 tmp_prefi.prefixlen = 128;
3319 tmp_prefi.prefix = *tovpn_sid;
3320 if (prefix_match((struct prefix *)&loc.prefix,
3321 (struct prefix *)&tmp_prefi))
3322 XFREE(MTYPE_BGP_SRV6_SID,
3323 bgp_vrf->vpn_policy[AFI_IP].tovpn_sid);
3324 }
3325
3326 // refresh vpnv6 tovpn_sid
3327 tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid;
3328 if (tovpn_sid) {
3329 tmp_prefi.family = AF_INET6;
3330 tmp_prefi.prefixlen = 128;
3331 tmp_prefi.prefix = *tovpn_sid;
3332 if (prefix_match((struct prefix *)&loc.prefix,
3333 (struct prefix *)&tmp_prefi))
3334 XFREE(MTYPE_BGP_SRV6_SID,
3335 bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid);
3336 }
3337
3338 /* refresh per-vrf tovpn_sid */
3339 tovpn_sid = bgp_vrf->tovpn_sid;
3340 if (tovpn_sid) {
3341 tmp_prefi.family = AF_INET6;
3342 tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3343 tmp_prefi.prefix = *tovpn_sid;
3344 if (prefix_match((struct prefix *)&loc.prefix,
3345 (struct prefix *)&tmp_prefi))
3346 XFREE(MTYPE_BGP_SRV6_SID, bgp_vrf->tovpn_sid);
3347 }
3348 }
3349
3350 vpn_leak_postchange_all();
3351
3352 /* refresh tovpn_sid_locator */
3353 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
3354 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF)
3355 continue;
3356
3357 /* refresh vpnv4 tovpn_sid_locator */
3358 tovpn_sid_locator =
3359 bgp_vrf->vpn_policy[AFI_IP].tovpn_sid_locator;
3360 if (tovpn_sid_locator) {
3361 tmp_prefi.family = AF_INET6;
3362 tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3363 tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3364 if (prefix_match((struct prefix *)&loc.prefix,
3365 (struct prefix *)&tmp_prefi))
3366 srv6_locator_chunk_free(
3367 &bgp_vrf->vpn_policy[AFI_IP]
3368 .tovpn_sid_locator);
3369 }
3370
3371 /* refresh vpnv6 tovpn_sid_locator */
3372 tovpn_sid_locator =
3373 bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid_locator;
3374 if (tovpn_sid_locator) {
3375 tmp_prefi.family = AF_INET6;
3376 tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3377 tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3378 if (prefix_match((struct prefix *)&loc.prefix,
3379 (struct prefix *)&tmp_prefi))
3380 srv6_locator_chunk_free(
3381 &bgp_vrf->vpn_policy[AFI_IP6]
3382 .tovpn_sid_locator);
3383 }
3384
3385 /* refresh per-vrf tovpn_sid_locator */
3386 tovpn_sid_locator = bgp_vrf->tovpn_sid_locator;
3387 if (tovpn_sid_locator) {
3388 tmp_prefi.family = AF_INET6;
3389 tmp_prefi.prefixlen = IPV6_MAX_BITLEN;
3390 tmp_prefi.prefix = tovpn_sid_locator->prefix.prefix;
3391 if (prefix_match((struct prefix *)&loc.prefix,
3392 (struct prefix *)&tmp_prefi))
3393 srv6_locator_chunk_free(
3394 &bgp_vrf->tovpn_sid_locator);
3395 }
3396 }
3397
3398 return 0;
3399 }
3400
3401 static zclient_handler *const bgp_handlers[] = {
3402 [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update,
3403 [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add,
3404 [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete,
3405 [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add,
3406 [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete,
3407 [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update,
3408 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route,
3409 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route,
3410 [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update,
3411 [ZEBRA_FEC_UPDATE] = bgp_read_fec_update,
3412 [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add,
3413 [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del,
3414 [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni,
3415 [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi,
3416 [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi,
3417 [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni,
3418 [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip,
3419 [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip,
3420 [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni,
3421 [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni,
3422 [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix,
3423 [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix,
3424 [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk,
3425 [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner,
3426 [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner,
3427 [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner,
3428 [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner,
3429 [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner,
3430 [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add,
3431 [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete,
3432 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] =
3433 bgp_zebra_process_srv6_locator_chunk,
3434 };
3435
3436 static int bgp_if_new_hook(struct interface *ifp)
3437 {
3438 struct bgp_interface *iifp;
3439
3440 if (ifp->info)
3441 return 0;
3442 iifp = XCALLOC(MTYPE_BGP_IF_INFO, sizeof(struct bgp_interface));
3443 ifp->info = iifp;
3444
3445 return 0;
3446 }
3447
3448 static int bgp_if_delete_hook(struct interface *ifp)
3449 {
3450 XFREE(MTYPE_BGP_IF_INFO, ifp->info);
3451 return 0;
3452 }
3453
3454 void bgp_if_init(void)
3455 {
3456 /* Initialize Zebra interface data structure. */
3457 hook_register_prio(if_add, 0, bgp_if_new_hook);
3458 hook_register_prio(if_del, 0, bgp_if_delete_hook);
3459 }
3460
3461 void bgp_zebra_init(struct thread_master *master, unsigned short instance)
3462 {
3463 zclient_num_connects = 0;
3464
3465 if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up,
3466 bgp_ifp_down, bgp_ifp_destroy);
3467
3468 /* Set default values. */
3469 zclient = zclient_new(master, &zclient_options_default, bgp_handlers,
3470 array_size(bgp_handlers));
3471 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
3472 zclient->zebra_connected = bgp_zebra_connected;
3473 zclient->instance = instance;
3474 }
3475
3476 void bgp_zebra_destroy(void)
3477 {
3478 if (zclient == NULL)
3479 return;
3480 zclient_stop(zclient);
3481 zclient_free(zclient);
3482 zclient = NULL;
3483 }
3484
3485 int bgp_zebra_num_connects(void)
3486 {
3487 return zclient_num_connects;
3488 }
3489
3490 void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra,
3491 struct bgp_pbr_rule *pbr,
3492 bool install)
3493 {
3494 struct stream *s;
3495
3496 if (pbra->install_in_progress && !pbr)
3497 return;
3498 if (pbr && pbr->install_in_progress)
3499 return;
3500 if (BGP_DEBUG(zebra, ZEBRA)) {
3501 if (pbr)
3502 zlog_debug("%s: table %d (ip rule) %d", __func__,
3503 pbra->table_id, install);
3504 else
3505 zlog_debug("%s: table %d fwmark %d %d", __func__,
3506 pbra->table_id, pbra->fwmark, install);
3507 }
3508 s = zclient->obuf;
3509 stream_reset(s);
3510
3511 zclient_create_header(s,
3512 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
3513 VRF_DEFAULT);
3514 stream_putl(s, 1); /* send one pbr action */
3515
3516 bgp_encode_pbr_rule_action(s, pbra, pbr);
3517
3518 stream_putw_at(s, 0, stream_get_endp(s));
3519 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE)
3520 && install) {
3521 if (!pbr)
3522 pbra->install_in_progress = true;
3523 else
3524 pbr->install_in_progress = true;
3525 }
3526 }
3527
3528 void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
3529 {
3530 struct stream *s;
3531
3532 if (pbrim->install_in_progress)
3533 return;
3534 if (BGP_DEBUG(zebra, ZEBRA))
3535 zlog_debug("%s: name %s type %d %d, ID %u", __func__,
3536 pbrim->ipset_name, pbrim->type, install,
3537 pbrim->unique);
3538 s = zclient->obuf;
3539 stream_reset(s);
3540
3541 zclient_create_header(s,
3542 install ? ZEBRA_IPSET_CREATE :
3543 ZEBRA_IPSET_DESTROY,
3544 VRF_DEFAULT);
3545
3546 stream_putl(s, 1); /* send one pbr action */
3547
3548 bgp_encode_pbr_ipset_match(s, pbrim);
3549
3550 stream_putw_at(s, 0, stream_get_endp(s));
3551 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3552 pbrim->install_in_progress = true;
3553 }
3554
3555 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
3556 bool install)
3557 {
3558 struct stream *s;
3559
3560 if (pbrime->install_in_progress)
3561 return;
3562 if (BGP_DEBUG(zebra, ZEBRA))
3563 zlog_debug("%s: name %s %d %d, ID %u", __func__,
3564 pbrime->backpointer->ipset_name, pbrime->unique,
3565 install, pbrime->unique);
3566 s = zclient->obuf;
3567 stream_reset(s);
3568
3569 zclient_create_header(s,
3570 install ? ZEBRA_IPSET_ENTRY_ADD :
3571 ZEBRA_IPSET_ENTRY_DELETE,
3572 VRF_DEFAULT);
3573
3574 stream_putl(s, 1); /* send one pbr action */
3575
3576 bgp_encode_pbr_ipset_entry_match(s, pbrime);
3577
3578 stream_putw_at(s, 0, stream_get_endp(s));
3579 if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install)
3580 pbrime->install_in_progress = true;
3581 }
3582
3583 static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s,
3584 uint8_t family)
3585 {
3586 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3587 struct bgp_pbr_interface_head *head;
3588 struct bgp_pbr_interface *pbr_if;
3589 struct interface *ifp;
3590
3591 if (!bgp_pbr_cfg)
3592 return;
3593 if (family == AF_INET)
3594 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3595 else
3596 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3597 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3598 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
3599 if (ifp)
3600 stream_putl(s, ifp->ifindex);
3601 }
3602 }
3603
3604 static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family)
3605 {
3606 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
3607 struct bgp_pbr_interface_head *head;
3608 struct bgp_pbr_interface *pbr_if;
3609 int cnt = 0;
3610
3611 if (!bgp_pbr_cfg)
3612 return 0;
3613 if (family == AF_INET)
3614 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
3615 else
3616 head = &(bgp_pbr_cfg->ifaces_by_name_ipv6);
3617 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
3618 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
3619 cnt++;
3620 }
3621 return cnt;
3622 }
3623
3624 void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
3625 struct bgp_pbr_match *pbm,
3626 bool install)
3627 {
3628 struct stream *s;
3629 int ret = 0;
3630 int nb_interface;
3631
3632 if (pbm->install_iptable_in_progress)
3633 return;
3634 if (BGP_DEBUG(zebra, ZEBRA))
3635 zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__,
3636 pbm->ipset_name, pbm->type, pba->fwmark, install,
3637 pbm->unique2);
3638 s = zclient->obuf;
3639 stream_reset(s);
3640
3641 zclient_create_header(s,
3642 install ? ZEBRA_IPTABLE_ADD :
3643 ZEBRA_IPTABLE_DELETE,
3644 VRF_DEFAULT);
3645
3646 bgp_encode_pbr_iptable_match(s, pba, pbm);
3647 nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family);
3648 stream_putl(s, nb_interface);
3649 if (nb_interface)
3650 bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family);
3651 stream_putw_at(s, 0, stream_get_endp(s));
3652 ret = zclient_send_message(zclient);
3653 if (install) {
3654 if (ret != ZCLIENT_SEND_FAILURE)
3655 pba->refcnt++;
3656 else
3657 pbm->install_iptable_in_progress = true;
3658 }
3659 }
3660
3661 /* inject in table <table_id> a default route to:
3662 * - if nexthop IP is present : to this nexthop
3663 * - if vrf is different from local : to the matching VRF
3664 */
3665 void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
3666 afi_t afi, uint32_t table_id, bool announce)
3667 {
3668 struct zapi_nexthop *api_nh;
3669 struct zapi_route api;
3670 struct prefix p;
3671
3672 if (!nh || (nh->type != NEXTHOP_TYPE_IPV4
3673 && nh->type != NEXTHOP_TYPE_IPV6)
3674 || nh->vrf_id == VRF_UNKNOWN)
3675 return;
3676
3677 /* in vrf-lite, no default route has to be announced
3678 * the table id of vrf is directly used to divert traffic
3679 */
3680 if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id)
3681 return;
3682
3683 memset(&p, 0, sizeof(p));
3684 if (afi != AFI_IP && afi != AFI_IP6)
3685 return;
3686 p.family = afi2family(afi);
3687 memset(&api, 0, sizeof(api));
3688 api.vrf_id = bgp->vrf_id;
3689 api.type = ZEBRA_ROUTE_BGP;
3690 api.safi = SAFI_UNICAST;
3691 api.prefix = p;
3692 api.tableid = table_id;
3693 api.nexthop_num = 1;
3694 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
3695 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
3696 api_nh = &api.nexthops[0];
3697
3698 api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT;
3699 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
3700
3701 /* redirect IP */
3702 if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) {
3703 api_nh->vrf_id = nh->vrf_id;
3704 api_nh->gate.ipv4 = nh->gate.ipv4;
3705 api_nh->type = NEXTHOP_TYPE_IPV4;
3706
3707 if (BGP_DEBUG(zebra, ZEBRA))
3708 zlog_debug(
3709 "BGP: %s default route to %pI4 table %d (redirect IP)",
3710 announce ? "adding" : "withdrawing",
3711 &nh->gate.ipv4, table_id);
3712
3713 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3714 : ZEBRA_ROUTE_DELETE,
3715 zclient, &api);
3716 } else if (afi == AFI_IP6 &&
3717 memcmp(&nh->gate.ipv6,
3718 &in6addr_any, sizeof(struct in6_addr))) {
3719 api_nh->vrf_id = nh->vrf_id;
3720 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6,
3721 sizeof(struct in6_addr));
3722 api_nh->type = NEXTHOP_TYPE_IPV6;
3723
3724 if (BGP_DEBUG(zebra, ZEBRA))
3725 zlog_debug(
3726 "BGP: %s default route to %pI6 table %d (redirect IP)",
3727 announce ? "adding" : "withdrawing",
3728 &nh->gate.ipv6, table_id);
3729
3730 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3731 : ZEBRA_ROUTE_DELETE,
3732 zclient, &api);
3733 } else if (nh->vrf_id != bgp->vrf_id) {
3734 struct vrf *vrf;
3735 struct interface *ifp;
3736
3737 vrf = vrf_lookup_by_id(nh->vrf_id);
3738 if (!vrf)
3739 return;
3740 /* create default route with interface <VRF>
3741 * with nexthop-vrf <VRF>
3742 */
3743 ifp = if_lookup_by_name_vrf(vrf->name, vrf);
3744 if (!ifp)
3745 return;
3746 api_nh->vrf_id = nh->vrf_id;
3747 api_nh->type = NEXTHOP_TYPE_IFINDEX;
3748 api_nh->ifindex = ifp->ifindex;
3749 if (BGP_DEBUG(zebra, ZEBRA))
3750 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
3751 announce ? "adding" : "withdrawing",
3752 vrf->name, table_id);
3753 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
3754 : ZEBRA_ROUTE_DELETE,
3755 zclient, &api);
3756 return;
3757 }
3758 }
3759
3760 /* Send capabilities to RIB */
3761 int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable)
3762 {
3763 struct zapi_cap api;
3764 int ret = BGP_GR_SUCCESS;
3765
3766 if (zclient == NULL) {
3767 if (BGP_DEBUG(zebra, ZEBRA))
3768 zlog_debug("zclient invalid");
3769 return BGP_GR_FAILURE;
3770 }
3771
3772 /* Check if the client is connected */
3773 if ((zclient->sock < 0) || (zclient->t_connect)) {
3774 if (BGP_DEBUG(zebra, ZEBRA))
3775 zlog_debug("client not connected");
3776 return BGP_GR_FAILURE;
3777 }
3778
3779 /* Check if capability is already sent. If the flag force is set
3780 * send the capability since this can be initial bgp configuration
3781 */
3782 memset(&api, 0, sizeof(api));
3783 if (disable) {
3784 api.cap = ZEBRA_CLIENT_GR_DISABLE;
3785 api.vrf_id = bgp->vrf_id;
3786 } else {
3787 api.cap = ZEBRA_CLIENT_GR_CAPABILITIES;
3788 api.stale_removal_time = bgp->rib_stale_time;
3789 api.vrf_id = bgp->vrf_id;
3790 }
3791
3792 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3793 == ZCLIENT_SEND_FAILURE) {
3794 zlog_err("error sending capability");
3795 ret = BGP_GR_FAILURE;
3796 } else {
3797 if (disable)
3798 bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE;
3799 else
3800 bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE;
3801
3802 if (BGP_DEBUG(zebra, ZEBRA))
3803 zlog_debug("send capabilty success");
3804 ret = BGP_GR_SUCCESS;
3805 }
3806 return ret;
3807 }
3808
3809 /* Send route update pesding or completed status to RIB for the
3810 * specific AFI, SAFI
3811 */
3812 int bgp_zebra_update(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type)
3813 {
3814 struct zapi_cap api = {0};
3815
3816 if (zclient == NULL) {
3817 if (BGP_DEBUG(zebra, ZEBRA))
3818 zlog_debug("zclient == NULL, invalid");
3819 return BGP_GR_FAILURE;
3820 }
3821
3822 /* Check if the client is connected */
3823 if ((zclient->sock < 0) || (zclient->t_connect)) {
3824 if (BGP_DEBUG(zebra, ZEBRA))
3825 zlog_debug("client not connected");
3826 return BGP_GR_FAILURE;
3827 }
3828
3829 api.afi = afi;
3830 api.safi = safi;
3831 api.vrf_id = vrf_id;
3832 api.cap = type;
3833
3834 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3835 == ZCLIENT_SEND_FAILURE) {
3836 if (BGP_DEBUG(zebra, ZEBRA))
3837 zlog_debug("error sending capability");
3838 return BGP_GR_FAILURE;
3839 }
3840 return BGP_GR_SUCCESS;
3841 }
3842
3843
3844 /* Send RIB stale timer update */
3845 int bgp_zebra_stale_timer_update(struct bgp *bgp)
3846 {
3847 struct zapi_cap api;
3848
3849 if (zclient == NULL) {
3850 if (BGP_DEBUG(zebra, ZEBRA))
3851 zlog_debug("zclient invalid");
3852 return BGP_GR_FAILURE;
3853 }
3854
3855 /* Check if the client is connected */
3856 if ((zclient->sock < 0) || (zclient->t_connect)) {
3857 if (BGP_DEBUG(zebra, ZEBRA))
3858 zlog_debug("client not connected");
3859 return BGP_GR_FAILURE;
3860 }
3861
3862 memset(&api, 0, sizeof(api));
3863 api.cap = ZEBRA_CLIENT_RIB_STALE_TIME;
3864 api.stale_removal_time = bgp->rib_stale_time;
3865 api.vrf_id = bgp->vrf_id;
3866 if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api)
3867 == ZCLIENT_SEND_FAILURE) {
3868 if (BGP_DEBUG(zebra, ZEBRA))
3869 zlog_debug("error sending capability");
3870 return BGP_GR_FAILURE;
3871 }
3872 if (BGP_DEBUG(zebra, ZEBRA))
3873 zlog_debug("send capabilty success");
3874 return BGP_GR_SUCCESS;
3875 }
3876
3877 int bgp_zebra_srv6_manager_get_locator_chunk(const char *name)
3878 {
3879 return srv6_manager_get_locator_chunk(zclient, name);
3880 }
3881
3882 int bgp_zebra_srv6_manager_release_locator_chunk(const char *name)
3883 {
3884 return srv6_manager_release_locator_chunk(zclient, name);
3885 }