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