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