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