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