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