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