]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #1993 from vivek-cumulus/evpn-fixes
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "zclient.h"
30 #include "routemap.h"
31 #include "thread.h"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35 #include "lib/bfd.h"
36 #include "filter.h"
37 #include "mpls.h"
38 #include "vxlan.h"
39
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
52 #if ENABLE_BGP_VNC
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
55 #endif
56 #include "bgpd/bgp_evpn.h"
57 #include "bgpd/bgp_mplsvpn.h"
58
59 /* All information about zebra. */
60 struct zclient *zclient = NULL;
61
62 /* Can we install into zebra? */
63 static inline int bgp_install_info_to_zebra(struct bgp *bgp)
64 {
65 if (zclient->sock <= 0)
66 return 0;
67
68 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
69 return 0;
70
71 return 1;
72 }
73
74 int zclient_num_connects;
75
76 /* Router-id update message from zebra. */
77 static int bgp_router_id_update(int command, struct zclient *zclient,
78 zebra_size_t length, vrf_id_t vrf_id)
79 {
80 struct prefix router_id;
81
82 zebra_router_id_update_read(zclient->ibuf, &router_id);
83
84 if (BGP_DEBUG(zebra, ZEBRA)) {
85 char buf[PREFIX2STR_BUFFER];
86 prefix2str(&router_id, buf, sizeof(buf));
87 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
88 }
89
90 bgp_router_id_zebra_bump(vrf_id, &router_id);
91 return 0;
92 }
93
94 /* Nexthop update message from zebra. */
95 static int bgp_read_nexthop_update(int command, struct zclient *zclient,
96 zebra_size_t length, vrf_id_t vrf_id)
97 {
98 bgp_parse_nexthop_update(command, vrf_id);
99 return 0;
100 }
101
102 static int bgp_read_import_check_update(int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
104 {
105 bgp_parse_nexthop_update(command, vrf_id);
106 return 0;
107 }
108
109 /* Set or clear interface on which unnumbered neighbor is configured. This
110 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
111 * interface.
112 */
113 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
114 struct interface *upd_ifp)
115 {
116 struct listnode *node, *nnode;
117 struct peer *peer;
118
119 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
120 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
121 if (upd_ifp) {
122 peer->ifp = upd_ifp;
123 bgp_zebra_initiate_radv(bgp, peer);
124 } else {
125 bgp_zebra_terminate_radv(bgp, peer);
126 peer->ifp = upd_ifp;
127 }
128 }
129 }
130 }
131
132 static int bgp_read_fec_update(int command, struct zclient *zclient,
133 zebra_size_t length)
134 {
135 bgp_parse_fec_update();
136 return 0;
137 }
138
139 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
140 {
141 struct listnode *node, *nnode;
142 struct peer *peer;
143
144 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
145 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
146 && peer->status != Established) {
147 if (peer_active(peer))
148 BGP_EVENT_ADD(peer, BGP_Stop);
149 BGP_EVENT_ADD(peer, BGP_Start);
150 }
151 }
152 }
153
154 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
155 {
156 struct listnode *node;
157 struct connected *connected;
158 struct interface *ifp;
159 struct prefix *p;
160
161 /* Kick-off the FSM for any relevant peers only if there is a
162 * valid local address on the interface.
163 */
164 ifp = ifc->ifp;
165 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
166 p = connected->address;
167 if (p->family == AF_INET6
168 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
169 break;
170 }
171 if (!connected)
172 return;
173
174 bgp_start_interface_nbrs(bgp, ifp);
175 }
176
177 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
178 int del)
179 {
180 struct listnode *node, *nnode;
181 struct peer *peer;
182 struct interface *ifp;
183
184 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
185 if (peer->conf_if
186 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
187 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
188 BGP_EVENT_ADD(peer, BGP_Stop);
189 }
190 }
191 /* Free neighbor also, if we're asked to. */
192 if (del) {
193 ifp = ifc->ifp;
194 listnode_delete(ifp->nbr_connected, ifc);
195 nbr_connected_free(ifc);
196 }
197 }
198
199 /* Inteface addition message from zebra. */
200 static int bgp_interface_add(int command, struct zclient *zclient,
201 zebra_size_t length, vrf_id_t vrf_id)
202 {
203 struct interface *ifp;
204 struct bgp *bgp;
205
206 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
207 if (!ifp) // unexpected
208 return 0;
209
210 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
211 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
212
213 bgp = bgp_lookup_by_vrf_id(vrf_id);
214 if (!bgp)
215 return 0;
216
217 bgp_update_interface_nbrs(bgp, ifp, ifp);
218 return 0;
219 }
220
221 static int bgp_interface_delete(int command, struct zclient *zclient,
222 zebra_size_t length, vrf_id_t vrf_id)
223 {
224 struct stream *s;
225 struct interface *ifp;
226 struct bgp *bgp;
227
228 bgp = bgp_lookup_by_vrf_id(vrf_id);
229 if (!bgp)
230 return 0;
231
232 s = zclient->ibuf;
233 ifp = zebra_interface_state_read(s, vrf_id);
234 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
235 return 0;
236
237 if (BGP_DEBUG(zebra, ZEBRA))
238 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
239
240 bgp_update_interface_nbrs(bgp, ifp, NULL);
241
242 if_set_index(ifp, IFINDEX_INTERNAL);
243 return 0;
244 }
245
246 static int bgp_interface_up(int command, struct zclient *zclient,
247 zebra_size_t length, vrf_id_t vrf_id)
248 {
249 struct stream *s;
250 struct interface *ifp;
251 struct connected *c;
252 struct nbr_connected *nc;
253 struct listnode *node, *nnode;
254 struct bgp *bgp;
255
256 bgp = bgp_lookup_by_vrf_id(vrf_id);
257 if (!bgp)
258 return 0;
259
260 s = zclient->ibuf;
261 ifp = zebra_interface_state_read(s, vrf_id);
262
263 if (!ifp)
264 return 0;
265
266 if (BGP_DEBUG(zebra, ZEBRA))
267 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
268
269 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
270 bgp_connected_add(bgp, c);
271
272 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
273 bgp_nbr_connected_add(bgp, nc);
274
275 return 0;
276 }
277
278 static int bgp_interface_down(int command, struct zclient *zclient,
279 zebra_size_t length, vrf_id_t vrf_id)
280 {
281 struct stream *s;
282 struct interface *ifp;
283 struct connected *c;
284 struct nbr_connected *nc;
285 struct listnode *node, *nnode;
286 struct bgp *bgp;
287
288 bgp = bgp_lookup_by_vrf_id(vrf_id);
289 if (!bgp)
290 return 0;
291
292 s = zclient->ibuf;
293 ifp = zebra_interface_state_read(s, vrf_id);
294 if (!ifp)
295 return 0;
296
297 if (BGP_DEBUG(zebra, ZEBRA))
298 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
299
300 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
301 bgp_connected_delete(bgp, c);
302
303 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
304 bgp_nbr_connected_delete(bgp, nc, 1);
305
306 /* Fast external-failover */
307 {
308 struct peer *peer;
309
310 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
311 return 0;
312
313 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
314 #if defined(HAVE_CUMULUS)
315 /* Take down directly connected EBGP peers as well as
316 * 1-hop BFD
317 * tracked (directly connected) IBGP peers.
318 */
319 if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
320 && (!peer->bfd_info
321 || bgp_bfd_is_peer_multihop(peer)))
322 #else
323 /* Take down directly connected EBGP peers */
324 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
325 #endif
326 continue;
327
328 if (ifp == peer->nexthop.ifp) {
329 BGP_EVENT_ADD(peer, BGP_Stop);
330 peer->last_reset = PEER_DOWN_IF_DOWN;
331 }
332 }
333 }
334
335 return 0;
336 }
337
338 static int bgp_interface_address_add(int command, struct zclient *zclient,
339 zebra_size_t length, vrf_id_t vrf_id)
340 {
341 struct connected *ifc;
342 struct bgp *bgp;
343
344 bgp = bgp_lookup_by_vrf_id(vrf_id);
345 if (!bgp)
346 return 0;
347
348 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
349
350 if (ifc == NULL)
351 return 0;
352
353 if (bgp_debug_zebra(ifc->address)) {
354 char buf[PREFIX2STR_BUFFER];
355 prefix2str(ifc->address, buf, sizeof(buf));
356 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
357 ifc->ifp->name, buf);
358 }
359
360 if (if_is_operative(ifc->ifp)) {
361 bgp_connected_add(bgp, ifc);
362
363 /* If we have learnt of any neighbors on this interface,
364 * check to kick off any BGP interface-based neighbors,
365 * but only if this is a link-local address.
366 */
367 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
368 && !list_isempty(ifc->ifp->nbr_connected))
369 bgp_start_interface_nbrs(bgp, ifc->ifp);
370 }
371
372 return 0;
373 }
374
375 static int bgp_interface_address_delete(int command, struct zclient *zclient,
376 zebra_size_t length, vrf_id_t vrf_id)
377 {
378 struct connected *ifc;
379 struct bgp *bgp;
380
381 bgp = bgp_lookup_by_vrf_id(vrf_id);
382 if (!bgp)
383 return 0;
384
385 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
386
387 if (ifc == NULL)
388 return 0;
389
390 if (bgp_debug_zebra(ifc->address)) {
391 char buf[PREFIX2STR_BUFFER];
392 prefix2str(ifc->address, buf, sizeof(buf));
393 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
394 ifc->ifp->name, buf);
395 }
396
397 if (if_is_operative(ifc->ifp)) {
398 bgp_connected_delete(bgp, ifc);
399 }
400
401 connected_free(ifc);
402
403 return 0;
404 }
405
406 static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
407 zebra_size_t length, vrf_id_t vrf_id)
408 {
409 struct nbr_connected *ifc = NULL;
410 struct bgp *bgp;
411
412 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
413
414 if (ifc == NULL)
415 return 0;
416
417 if (bgp_debug_zebra(ifc->address)) {
418 char buf[PREFIX2STR_BUFFER];
419 prefix2str(ifc->address, buf, sizeof(buf));
420 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
421 ifc->ifp->name, buf);
422 }
423
424 if (if_is_operative(ifc->ifp)) {
425 bgp = bgp_lookup_by_vrf_id(vrf_id);
426 if (bgp)
427 bgp_nbr_connected_add(bgp, ifc);
428 }
429
430 return 0;
431 }
432
433 static int bgp_interface_nbr_address_delete(int command,
434 struct zclient *zclient,
435 zebra_size_t length,
436 vrf_id_t vrf_id)
437 {
438 struct nbr_connected *ifc = NULL;
439 struct bgp *bgp;
440
441 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
442
443 if (ifc == NULL)
444 return 0;
445
446 if (bgp_debug_zebra(ifc->address)) {
447 char buf[PREFIX2STR_BUFFER];
448 prefix2str(ifc->address, buf, sizeof(buf));
449 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id,
450 ifc->ifp->name, buf);
451 }
452
453 if (if_is_operative(ifc->ifp)) {
454 bgp = bgp_lookup_by_vrf_id(vrf_id);
455 if (bgp)
456 bgp_nbr_connected_delete(bgp, ifc, 0);
457 }
458
459 nbr_connected_free(ifc);
460
461 return 0;
462 }
463
464 /* VRF update for an interface. */
465 static int bgp_interface_vrf_update(int command, struct zclient *zclient,
466 zebra_size_t length, vrf_id_t vrf_id)
467 {
468 struct interface *ifp;
469 vrf_id_t new_vrf_id;
470 struct connected *c;
471 struct nbr_connected *nc;
472 struct listnode *node, *nnode;
473 struct bgp *bgp;
474
475 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
476 &new_vrf_id);
477 if (!ifp)
478 return 0;
479
480 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
481 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
482 ifp->name, new_vrf_id);
483
484 bgp = bgp_lookup_by_vrf_id(vrf_id);
485 if (!bgp)
486 return 0;
487
488 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
489 bgp_connected_delete(bgp, c);
490
491 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
492 bgp_nbr_connected_delete(bgp, nc, 1);
493
494 /* Fast external-failover */
495 {
496 struct peer *peer;
497
498 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
499 return 0;
500
501 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
502 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
503 continue;
504
505 if (ifp == peer->nexthop.ifp)
506 BGP_EVENT_ADD(peer, BGP_Stop);
507 }
508 }
509
510 if_update_to_new_vrf(ifp, new_vrf_id);
511
512 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
513 if (!bgp)
514 return 0;
515
516 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
517 bgp_connected_add(bgp, c);
518
519 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
520 bgp_nbr_connected_add(bgp, nc);
521 return 0;
522 }
523
524 /* Zebra route add and delete treatment. */
525 static int zebra_read_route(int command, struct zclient *zclient,
526 zebra_size_t length, vrf_id_t vrf_id)
527 {
528 enum nexthop_types_t nhtype;
529 struct zapi_route api;
530 union g_addr nexthop;
531 ifindex_t ifindex;
532 int add, i;
533 struct bgp *bgp;
534
535 bgp = bgp_lookup_by_vrf_id(vrf_id);
536 if (!bgp)
537 return 0;
538
539 if (zapi_route_decode(zclient->ibuf, &api) < 0)
540 return -1;
541
542 /* we completely ignore srcdest routes for now. */
543 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
544 return 0;
545
546 /* ignore link-local address. */
547 if (api.prefix.family == AF_INET6
548 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
549 return 0;
550
551 nexthop = api.nexthops[0].gate;
552 ifindex = api.nexthops[0].ifindex;
553 nhtype = api.nexthops[0].type;
554
555 add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
556 if (add) {
557 /*
558 * The ADD message is actually an UPDATE and there is no
559 * explicit DEL
560 * for a prior redistributed route, if any. So, perform an
561 * implicit
562 * DEL processing for the same redistributed route from any
563 * other
564 * source type.
565 */
566 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
567 if (i != api.type)
568 bgp_redistribute_delete(bgp, &api.prefix, i,
569 api.instance);
570 }
571
572 /* Now perform the add/update. */
573 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
574 nhtype, api.metric, api.type, api.instance,
575 api.tag);
576 } else {
577 bgp_redistribute_delete(bgp, &api.prefix, api.type,
578 api.instance);
579 }
580
581 if (bgp_debug_zebra(&api.prefix)) {
582 char buf[2][PREFIX_STRLEN];
583
584 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
585 inet_ntop(api.prefix.family, &nexthop, buf[1], sizeof(buf[1]));
586 zlog_debug(
587 "Rx route %s VRF %u %s[%d] %s "
588 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
589 (add) ? "add" : "delete", vrf_id,
590 zebra_route_string(api.type), api.instance, buf[0],
591 buf[1], api.metric, api.tag);
592 }
593
594 return 0;
595 }
596
597 struct interface *if_lookup_by_ipv4(struct in_addr *addr, 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_ipv4 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_INET;
611 p.prefix = *addr;
612 p.prefixlen = IPV4_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_INET)
619 if (prefix_match(cp, (struct prefix *)&p))
620 return ifp;
621 }
622 }
623 return NULL;
624 }
625
626 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
627 {
628 struct vrf *vrf;
629 struct listnode *cnode;
630 struct interface *ifp;
631 struct connected *connected;
632 struct prefix *cp;
633
634 vrf = vrf_lookup_by_id(vrf_id);
635 if (!vrf)
636 return NULL;
637
638 FOR_ALL_INTERFACES (vrf, ifp) {
639 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
640 cp = connected->address;
641
642 if (cp->family == AF_INET)
643 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
644 return ifp;
645 }
646 }
647 return NULL;
648 }
649
650 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
651 vrf_id_t vrf_id)
652 {
653 struct vrf *vrf;
654 struct listnode *cnode;
655 struct interface *ifp;
656 struct connected *connected;
657 struct prefix_ipv6 p;
658 struct prefix *cp;
659
660 vrf = vrf_lookup_by_id(vrf_id);
661 if (!vrf)
662 return NULL;
663
664 p.family = AF_INET6;
665 p.prefix = *addr;
666 p.prefixlen = IPV6_MAX_BITLEN;
667
668 FOR_ALL_INTERFACES (vrf, ifp) {
669 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
670 cp = connected->address;
671
672 if (cp->family == AF_INET6)
673 if (prefix_match(cp, (struct prefix *)&p)) {
674 if (IN6_IS_ADDR_LINKLOCAL(
675 &cp->u.prefix6)) {
676 if (ifindex == ifp->ifindex)
677 return ifp;
678 } else
679 return ifp;
680 }
681 }
682 }
683 return NULL;
684 }
685
686 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
687 ifindex_t ifindex, vrf_id_t vrf_id)
688 {
689 struct vrf *vrf;
690 struct listnode *cnode;
691 struct interface *ifp;
692 struct connected *connected;
693 struct prefix *cp;
694
695 vrf = vrf_lookup_by_id(vrf_id);
696 if (!vrf)
697 return NULL;
698
699 FOR_ALL_INTERFACES (vrf, ifp) {
700 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
701 cp = connected->address;
702
703 if (cp->family == AF_INET6)
704 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
705 if (IN6_IS_ADDR_LINKLOCAL(
706 &cp->u.prefix6)) {
707 if (ifindex == ifp->ifindex)
708 return ifp;
709 } else
710 return ifp;
711 }
712 }
713 }
714 return NULL;
715 }
716
717 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
718 {
719 struct listnode *cnode;
720 struct connected *connected;
721 struct prefix *cp;
722
723 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
724 cp = connected->address;
725
726 if (cp->family == AF_INET6)
727 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
728 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
729 return 1;
730 }
731 }
732 return 0;
733 }
734
735 static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
736 {
737 struct listnode *cnode;
738 struct connected *connected;
739 struct prefix *cp;
740
741 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
742 cp = connected->address;
743
744 if (cp->family == AF_INET6)
745 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
746 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
747 return 1;
748 }
749 }
750 return 0;
751 }
752
753 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
754 {
755 struct listnode *cnode;
756 struct connected *connected;
757 struct prefix *cp;
758
759 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
760 cp = connected->address;
761 if ((cp->family == AF_INET)
762 && !ipv4_martian(&(cp->u.prefix4))) {
763 *addr = cp->u.prefix4;
764 return 1;
765 }
766 }
767 return 0;
768 }
769
770 int bgp_nexthop_set(union sockunion *local, union sockunion *remote,
771 struct bgp_nexthop *nexthop, struct peer *peer)
772 {
773 int ret = 0;
774 struct interface *ifp = NULL;
775
776 memset(nexthop, 0, sizeof(struct bgp_nexthop));
777
778 if (!local)
779 return -1;
780 if (!remote)
781 return -1;
782
783 if (local->sa.sa_family == AF_INET) {
784 nexthop->v4 = local->sin.sin_addr;
785 if (peer->update_if)
786 ifp = if_lookup_by_name(peer->update_if,
787 peer->bgp->vrf_id);
788 else
789 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
790 peer->bgp->vrf_id);
791 }
792 if (local->sa.sa_family == AF_INET6) {
793 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
794 if (peer->conf_if || peer->ifname)
795 ifp = if_lookup_by_name(peer->conf_if
796 ? peer->conf_if
797 : peer->ifname,
798 peer->bgp->vrf_id);
799 } else if (peer->update_if)
800 ifp = if_lookup_by_name(peer->update_if,
801 peer->bgp->vrf_id);
802 else
803 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
804 local->sin6.sin6_scope_id,
805 peer->bgp->vrf_id);
806 }
807
808 if (!ifp)
809 return -1;
810
811 nexthop->ifp = ifp;
812
813 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
814 if (local->sa.sa_family == AF_INET) {
815 /* IPv6 nexthop*/
816 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
817
818 if (!ret) {
819 /* There is no global nexthop. Use link-local address as
820 * both the
821 * global and link-local nexthop. In this scenario, the
822 * expectation
823 * for interop is that the network admin would use a
824 * route-map to
825 * specify the global IPv6 nexthop.
826 */
827 if_get_ipv6_local(ifp, &nexthop->v6_global);
828 memcpy(&nexthop->v6_local, &nexthop->v6_global,
829 IPV6_MAX_BYTELEN);
830 } else
831 if_get_ipv6_local(ifp, &nexthop->v6_local);
832
833 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
834 peer->shared_network = 1;
835 else
836 peer->shared_network = 0;
837 }
838
839 /* IPv6 connection, fetch and store IPv4 local address if any. */
840 if (local->sa.sa_family == AF_INET6) {
841 struct interface *direct = NULL;
842
843 /* IPv4 nexthop. */
844 ret = if_get_ipv4_address(ifp, &nexthop->v4);
845 if (!ret && peer->local_id.s_addr)
846 nexthop->v4 = peer->local_id;
847
848 /* Global address*/
849 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
850 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
851 IPV6_MAX_BYTELEN);
852
853 /* If directory connected set link-local address. */
854 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
855 remote->sin6.sin6_scope_id,
856 peer->bgp->vrf_id);
857 if (direct)
858 if_get_ipv6_local(ifp, &nexthop->v6_local);
859 } else
860 /* Link-local address. */
861 {
862 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
863
864 /* If there is no global address. Set link-local
865 address as
866 global. I know this break RFC specification... */
867 /* In this scenario, the expectation for interop is that
868 * the
869 * network admin would use a route-map to specify the
870 * global
871 * IPv6 nexthop.
872 */
873 if (!ret)
874 memcpy(&nexthop->v6_global,
875 &local->sin6.sin6_addr,
876 IPV6_MAX_BYTELEN);
877 /* Always set the link-local address */
878 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
879 IPV6_MAX_BYTELEN);
880 }
881
882 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
883 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
884 remote->sin6.sin6_scope_id,
885 peer->bgp->vrf_id))
886 peer->shared_network = 1;
887 else
888 peer->shared_network = 0;
889 }
890
891 /* KAME stack specific treatment. */
892 #ifdef KAME
893 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
894 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
895 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
896 }
897 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
898 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
899 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
900 }
901 #endif /* KAME */
902
903 /* If we have identified the local interface, there is no error for now.
904 */
905 return 0;
906 }
907
908 static struct in6_addr *bgp_info_to_ipv6_nexthop(struct bgp_info *info)
909 {
910 struct in6_addr *nexthop = NULL;
911
912 /* Only global address nexthop exists. */
913 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
914 nexthop = &info->attr->mp_nexthop_global;
915
916 /* If both global and link-local address present. */
917 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
918 /* Check if route-map is set to prefer global over link-local */
919 if (info->attr->mp_nexthop_prefer_global)
920 nexthop = &info->attr->mp_nexthop_global;
921 else {
922 /* Workaround for Cisco's nexthop bug. */
923 if (IN6_IS_ADDR_UNSPECIFIED(
924 &info->attr->mp_nexthop_global)
925 && info->peer->su_remote->sa.sa_family == AF_INET6)
926 nexthop =
927 &info->peer->su_remote->sin6.sin6_addr;
928 else
929 nexthop = &info->attr->mp_nexthop_local;
930 }
931 }
932
933 return nexthop;
934 }
935
936 static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
937 struct bgp_info *info)
938 {
939 route_map_result_t ret;
940
941 ret = route_map_apply(map, p, RMAP_BGP, info);
942 bgp_attr_flush(info->attr);
943
944 if (ret != RMAP_DENYMATCH)
945 return 1;
946
947 if (bgp_debug_zebra(p)) {
948 if (p->family == AF_INET) {
949 char buf[2][INET_ADDRSTRLEN];
950 zlog_debug(
951 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
952 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
953 sizeof(buf[0])),
954 p->prefixlen,
955 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
956 sizeof(buf[1])));
957 }
958 if (p->family == AF_INET6) {
959 char buf[2][INET6_ADDRSTRLEN];
960 zlog_debug(
961 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
962 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
963 sizeof(buf[0])),
964 p->prefixlen,
965 inet_ntop(AF_INET6,
966 bgp_info_to_ipv6_nexthop(info),
967 buf[1], sizeof(buf[1])));
968 }
969 }
970 return 0;
971 }
972
973 static struct thread *bgp_tm_thread_connect;
974 static bool bgp_tm_status_connected;
975
976 static int bgp_zebra_tm_connect(struct thread *t)
977 {
978 struct zclient *zclient;
979 int delay = 10, ret = 0;
980
981 zclient = THREAD_ARG(t);
982 if (bgp_tm_status_connected && zclient->sock > 0)
983 delay = 60;
984 else {
985 bgp_tm_status_connected = false;
986 ret = tm_table_manager_connect(zclient);
987 }
988 if (ret < 0) {
989 zlog_warn("Error connecting to table manager!");
990 bgp_tm_status_connected = false;
991 } else {
992 if (!bgp_tm_status_connected)
993 zlog_debug("Connecting to table manager. Success");
994 bgp_tm_status_connected = true;
995 }
996 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
997 &bgp_tm_thread_connect);
998 return 0;
999 }
1000
1001 void bgp_zebra_init_tm_connect(void)
1002 {
1003 int delay = 1;
1004
1005 /* if already set, do nothing
1006 */
1007 if (bgp_tm_thread_connect != NULL)
1008 return;
1009 bgp_tm_status_connected = false;
1010 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1011 &bgp_tm_thread_connect);
1012 }
1013
1014 int bgp_zebra_get_table_range(uint32_t chunk_size,
1015 uint32_t *start, uint32_t *end)
1016 {
1017 int ret;
1018
1019 if (!bgp_tm_status_connected)
1020 return -1;
1021 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1022 if (ret < 0) {
1023 zlog_err("BGP: Error getting table chunk %u", chunk_size);
1024 return -1;
1025 }
1026 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1027 chunk_size, *start, *end);
1028 return 0;
1029 }
1030
1031 void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
1032 struct bgp_info *info, struct bgp *bgp, afi_t afi,
1033 safi_t safi)
1034 {
1035 struct zapi_route api;
1036 struct zapi_nexthop *api_nh;
1037 int nh_family;
1038 unsigned int valid_nh_count = 0;
1039 int has_valid_label = 0;
1040 uint8_t distance;
1041 struct peer *peer;
1042 struct bgp_info *mpinfo;
1043 uint32_t metric;
1044 struct attr local_attr;
1045 struct bgp_info local_info;
1046 struct bgp_info *mpinfo_cp = &local_info;
1047 route_tag_t tag;
1048 mpls_label_t label;
1049 int nh_othervrf = 0;
1050
1051 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1052 * know of this instance.
1053 */
1054 if (!bgp_install_info_to_zebra(bgp))
1055 return;
1056
1057 if (bgp->main_zebra_update_hold)
1058 return;
1059
1060 /*
1061 * vrf leaking support (will have only one nexthop)
1062 */
1063 if (info->extra && info->extra->bgp_orig)
1064 nh_othervrf = 1;
1065
1066 /* Make Zebra API structure. */
1067 memset(&api, 0, sizeof(api));
1068 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1069 api.vrf_id = bgp->vrf_id;
1070 api.type = ZEBRA_ROUTE_BGP;
1071 api.safi = safi;
1072 api.prefix = *p;
1073 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1074
1075 peer = info->peer;
1076
1077 if (info->type == ZEBRA_ROUTE_BGP
1078 && info->sub_type == BGP_ROUTE_IMPORTED) {
1079
1080 struct bgp_info *bi;
1081
1082 /*
1083 * Look at parent chain for peer sort
1084 */
1085 for (bi = info; bi->extra && bi->extra->parent;
1086 bi = bi->extra->parent) {
1087
1088 peer = ((struct bgp_info *)(bi->extra->parent))->peer;
1089 }
1090 }
1091
1092 tag = info->attr->tag;
1093
1094 /* When we create an aggregate route we must also install a Null0 route
1095 * in
1096 * the RIB */
1097 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1098 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1099
1100 /* If the route's source is EVPN, flag as such. */
1101 if (is_route_parent_evpn(info))
1102 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1103
1104 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1105 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1106 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1107 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1108 }
1109
1110 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1111 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1112 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1113
1114 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1115
1116 /* Metric is currently based on the best-path only */
1117 metric = info->attr->med;
1118 for (mpinfo = info; mpinfo; mpinfo = bgp_info_mpath_next(mpinfo)) {
1119 if (valid_nh_count >= multipath_num)
1120 break;
1121
1122 *mpinfo_cp = *mpinfo;
1123
1124 /* Get nexthop address-family */
1125 if (p->family == AF_INET
1126 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1127 nh_family = AF_INET;
1128 else if (p->family == AF_INET6
1129 || (p->family == AF_INET
1130 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1131 nh_family = AF_INET6;
1132 else
1133 continue;
1134
1135 api_nh = &api.nexthops[valid_nh_count];
1136 api_nh->vrf_id = nh_othervrf ? info->extra->bgp_orig->vrf_id
1137 : bgp->vrf_id;
1138 if (nh_family == AF_INET) {
1139 struct in_addr *nexthop;
1140
1141 if (bgp_debug_zebra(&api.prefix)) {
1142 char buf_prefix[PREFIX_STRLEN];
1143 prefix2str(&api.prefix, buf_prefix,
1144 sizeof(buf_prefix));
1145 if (mpinfo->extra) {
1146 zlog_debug(
1147 "%s: p=%s, bgp_is_valid_label: %d",
1148 __func__, buf_prefix,
1149 bgp_is_valid_label(
1150 &mpinfo->extra
1151 ->label[0]));
1152 } else {
1153 zlog_debug(
1154 "%s: p=%s, extra is NULL, no label",
1155 __func__, buf_prefix);
1156 }
1157 }
1158
1159 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1160 /* Copy info and attributes, so the route-map
1161 apply doesn't modify the BGP route info. */
1162 local_attr = *mpinfo->attr;
1163 mpinfo_cp->attr = &local_attr;
1164 if (nh_othervrf) {
1165 /* allow route-map to modify */
1166 local_attr.nexthop =
1167 info->extra->nexthop_orig.u
1168 .prefix4;
1169 }
1170 }
1171
1172 if (bgp->table_map[afi][safi].name) {
1173 if (!bgp_table_map_apply(
1174 bgp->table_map[afi][safi].map, p,
1175 mpinfo_cp))
1176 continue;
1177
1178 /* metric/tag is only allowed to be
1179 * overridden on 1st nexthop */
1180 if (mpinfo == info) {
1181 metric = mpinfo_cp->attr->med;
1182 tag = mpinfo_cp->attr->tag;
1183 }
1184 }
1185
1186 nexthop = &mpinfo_cp->attr->nexthop;
1187 api_nh->gate.ipv4 = *nexthop;
1188
1189 /* EVPN type-2 routes are
1190 programmed as onlink on l3-vni SVI
1191 */
1192 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1193 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1194 else
1195 api_nh->type = NEXTHOP_TYPE_IPV4;
1196 } else {
1197 ifindex_t ifindex;
1198 struct in6_addr *nexthop;
1199
1200 ifindex = 0;
1201
1202 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1203 /* Copy info and attributes, so the route-map
1204 apply doesn't modify the BGP route info. */
1205 local_attr = *mpinfo->attr;
1206 mpinfo_cp->attr = &local_attr;
1207 if (nh_othervrf) {
1208 /* allow route-map to modify */
1209 local_attr.mp_nexthop_global =
1210 info->extra->nexthop_orig.u
1211 .prefix6;
1212 local_attr.mp_nexthop_len =
1213 BGP_ATTR_NHLEN_IPV6_GLOBAL;
1214 }
1215 }
1216
1217 if (bgp->table_map[afi][safi].name) {
1218 /* Copy info and attributes, so the route-map
1219 apply doesn't modify the BGP route info. */
1220 local_attr = *mpinfo->attr;
1221 mpinfo_cp->attr = &local_attr;
1222
1223 if (!bgp_table_map_apply(
1224 bgp->table_map[afi][safi].map, p,
1225 mpinfo_cp))
1226 continue;
1227
1228 /* metric/tag is only allowed to be
1229 * overridden on 1st nexthop */
1230 if (mpinfo == info) {
1231 metric = mpinfo_cp->attr->med;
1232 tag = mpinfo_cp->attr->tag;
1233 }
1234 }
1235 nexthop = bgp_info_to_ipv6_nexthop(mpinfo_cp);
1236
1237 if ((mpinfo == info)
1238 && mpinfo->attr->mp_nexthop_len
1239 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1240 if (mpinfo->peer->nexthop.ifp)
1241 ifindex = mpinfo->peer->nexthop.ifp
1242 ->ifindex;
1243
1244 if (!ifindex) {
1245 if (mpinfo->peer->conf_if)
1246 ifindex = mpinfo->peer->ifp->ifindex;
1247 else if (mpinfo->peer->ifname)
1248 ifindex = ifname2ifindex(
1249 mpinfo->peer->ifname,
1250 bgp->vrf_id);
1251 else if (mpinfo->peer->nexthop.ifp)
1252 ifindex = mpinfo->peer->nexthop.ifp
1253 ->ifindex;
1254 }
1255
1256 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1257 if (ifindex == 0)
1258 continue;
1259 } else
1260 ifindex = 0;
1261
1262 api_nh->gate.ipv6 = *nexthop;
1263 api_nh->ifindex = ifindex;
1264 api_nh->type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1265 : NEXTHOP_TYPE_IPV6;
1266 }
1267
1268 if (mpinfo->extra
1269 && bgp_is_valid_label(&mpinfo->extra->label[0])
1270 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1271 has_valid_label = 1;
1272 label = label_pton(&mpinfo->extra->label[0]);
1273
1274 api_nh->label_num = 1;
1275 api_nh->labels[0] = label;
1276 }
1277 valid_nh_count++;
1278 }
1279
1280 /* if this is a evpn route we don't have to include the label */
1281 if (has_valid_label && !(CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)))
1282 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1283
1284 if (info->sub_type != BGP_ROUTE_AGGREGATE)
1285 api.nexthop_num = valid_nh_count;
1286
1287 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1288 api.metric = metric;
1289
1290 if (tag) {
1291 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1292 api.tag = tag;
1293 }
1294
1295 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1296 if (distance) {
1297 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1298 api.distance = distance;
1299 }
1300
1301 if (bgp_debug_zebra(p)) {
1302 char prefix_buf[PREFIX_STRLEN];
1303 char nh_buf[INET6_ADDRSTRLEN];
1304 char label_buf[20];
1305 int i;
1306
1307 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1308 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1309 " count %d",
1310 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1311 prefix_buf, api.metric, api.tag, api.nexthop_num);
1312 for (i = 0; i < api.nexthop_num; i++) {
1313 api_nh = &api.nexthops[i];
1314
1315 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1316 nh_family = AF_INET;
1317 else
1318 nh_family = AF_INET6;
1319 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1320 sizeof(nh_buf));
1321
1322 label_buf[0] = '\0';
1323 if (has_valid_label
1324 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1325 sprintf(label_buf, "label %u",
1326 api_nh->labels[0]);
1327 zlog_debug(" nhop [%d]: %s %s", i + 1, nh_buf,
1328 label_buf);
1329 }
1330 }
1331
1332 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1333 : ZEBRA_ROUTE_DELETE,
1334 zclient, &api);
1335 }
1336
1337 /* Announce all routes of a table to zebra */
1338 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1339 {
1340 struct bgp_node *rn;
1341 struct bgp_table *table;
1342 struct bgp_info *ri;
1343
1344 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1345 * know of this instance.
1346 */
1347 if (!bgp_install_info_to_zebra(bgp))
1348 return;
1349
1350 table = bgp->rib[afi][safi];
1351 if (!table)
1352 return;
1353
1354 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1355 for (ri = rn->info; ri; ri = ri->next)
1356 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) &&
1357
1358 (ri->type == ZEBRA_ROUTE_BGP
1359 && (ri->sub_type == BGP_ROUTE_NORMAL
1360 || ri->sub_type == BGP_ROUTE_IMPORTED)))
1361
1362 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1363 safi);
1364 }
1365
1366 void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info,
1367 struct bgp *bgp, safi_t safi)
1368 {
1369 struct zapi_route api;
1370 struct peer *peer;
1371
1372 peer = info->peer;
1373 assert(peer);
1374
1375 if (info->type == ZEBRA_ROUTE_BGP
1376 && info->sub_type == BGP_ROUTE_IMPORTED) {
1377
1378 struct bgp_info *bi;
1379
1380 /*
1381 * Look at parent chain for peer sort
1382 */
1383 for (bi = info; bi->extra && bi->extra->parent;
1384 bi = bi->extra->parent) {
1385
1386 peer = ((struct bgp_info *)(bi->extra->parent))->peer;
1387 }
1388 }
1389
1390 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1391 * know of this instance.
1392 */
1393 if (!bgp_install_info_to_zebra(bgp))
1394 return;
1395
1396 memset(&api, 0, sizeof(api));
1397 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1398 api.vrf_id = bgp->vrf_id;
1399 api.type = ZEBRA_ROUTE_BGP;
1400 api.safi = safi;
1401 api.prefix = *p;
1402
1403 /* If the route's source is EVPN, flag as such. */
1404 if (is_route_parent_evpn(info))
1405 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1406
1407 if (peer->sort == BGP_PEER_IBGP) {
1408 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1409 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1410 }
1411
1412 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1413 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1414 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1415 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1416
1417 if (bgp_debug_zebra(p)) {
1418 char buf[PREFIX_STRLEN];
1419
1420 prefix2str(&api.prefix, buf, sizeof(buf));
1421 zlog_debug("Tx route delete VRF %u %s", bgp->vrf_id, buf);
1422 }
1423
1424 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1425 }
1426
1427 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1428 unsigned short instance)
1429 {
1430 struct list *red_list;
1431 struct listnode *node;
1432 struct bgp_redist *red;
1433
1434 red_list = bgp->redist[afi][type];
1435 if (!red_list)
1436 return (NULL);
1437
1438 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1439 if (red->instance == instance)
1440 return red;
1441
1442 return NULL;
1443 }
1444
1445 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1446 unsigned short instance)
1447 {
1448 struct list *red_list;
1449 struct bgp_redist *red;
1450
1451 red = bgp_redist_lookup(bgp, afi, type, instance);
1452 if (red)
1453 return red;
1454
1455 if (!bgp->redist[afi][type])
1456 bgp->redist[afi][type] = list_new();
1457
1458 red_list = bgp->redist[afi][type];
1459 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1460 sizeof(struct bgp_redist));
1461 red->instance = instance;
1462
1463 listnode_add(red_list, red);
1464
1465 return red;
1466 }
1467
1468 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1469 unsigned short instance)
1470 {
1471 struct bgp_redist *red;
1472
1473 red = bgp_redist_lookup(bgp, afi, type, instance);
1474
1475 if (red) {
1476 listnode_delete(bgp->redist[afi][type], red);
1477 XFREE(MTYPE_BGP_REDIST, red);
1478 if (!bgp->redist[afi][type]->count)
1479 list_delete_and_null(&bgp->redist[afi][type]);
1480 }
1481 }
1482
1483 /* Other routes redistribution into BGP. */
1484 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1485 unsigned short instance)
1486 {
1487
1488 /* Return if already redistribute flag is set. */
1489 if (instance) {
1490 if (redist_check_instance(&zclient->mi_redist[afi][type],
1491 instance))
1492 return CMD_WARNING;
1493
1494 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1495 } else {
1496 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1497 return CMD_WARNING;
1498
1499 #if ENABLE_BGP_VNC
1500 if (bgp->vrf_id == VRF_DEFAULT
1501 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1502 vnc_export_bgp_enable(
1503 bgp, afi); /* only enables if mode bits cfg'd */
1504 }
1505 #endif
1506
1507 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1508 }
1509
1510 /*
1511 * Don't try to register if we're not connected to Zebra or Zebra
1512 * doesn't know of this instance.
1513 *
1514 * When we come up later well resend if needed.
1515 */
1516 if (!bgp_install_info_to_zebra(bgp))
1517 return CMD_SUCCESS;
1518
1519 if (BGP_DEBUG(zebra, ZEBRA))
1520 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1521 bgp->vrf_id, afi, zebra_route_string(type),
1522 instance);
1523
1524 /* Send distribute add message to zebra. */
1525 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1526 instance, bgp->vrf_id);
1527
1528 return CMD_SUCCESS;
1529 }
1530
1531 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1532 unsigned short instance)
1533 {
1534 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1535 * know of this instance.
1536 */
1537 if (!bgp_install_info_to_zebra(bgp))
1538 return -1;
1539
1540 if (BGP_DEBUG(zebra, ZEBRA))
1541 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1542 bgp->vrf_id, afi, zebra_route_string(type),
1543 instance);
1544
1545 /* Send distribute add message to zebra. */
1546 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1547 instance, bgp->vrf_id);
1548 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1549 instance, bgp->vrf_id);
1550
1551 return 0;
1552 }
1553
1554 /* Redistribute with route-map specification. */
1555 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
1556 {
1557 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1558 return 0;
1559
1560 if (red->rmap.name)
1561 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1562 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1563 red->rmap.map = route_map_lookup_by_name(name);
1564
1565 return 1;
1566 }
1567
1568 /* Redistribute with metric specification. */
1569 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1570 afi_t afi, int type, uint32_t metric)
1571 {
1572 struct bgp_node *rn;
1573 struct bgp_info *ri;
1574
1575 if (red->redist_metric_flag && red->redist_metric == metric)
1576 return 0;
1577
1578 red->redist_metric_flag = 1;
1579 red->redist_metric = metric;
1580
1581 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1582 rn = bgp_route_next(rn)) {
1583 for (ri = rn->info; ri; ri = ri->next) {
1584 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1585 && ri->type == type
1586 && ri->instance == red->instance) {
1587 struct attr *old_attr;
1588 struct attr new_attr;
1589
1590 bgp_attr_dup(&new_attr, ri->attr);
1591 new_attr.med = red->redist_metric;
1592 old_attr = ri->attr;
1593 ri->attr = bgp_attr_intern(&new_attr);
1594 bgp_attr_unintern(&old_attr);
1595
1596 bgp_info_set_flag(rn, ri,
1597 BGP_INFO_ATTR_CHANGED);
1598 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1599 }
1600 }
1601 }
1602
1603 return 1;
1604 }
1605
1606 /* Unset redistribution. */
1607 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1608 unsigned short instance)
1609 {
1610 struct bgp_redist *red;
1611
1612 red = bgp_redist_lookup(bgp, afi, type, instance);
1613 if (!red)
1614 return CMD_SUCCESS;
1615
1616 /* Return if zebra connection is disabled. */
1617 if (instance) {
1618 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1619 instance))
1620 return CMD_WARNING;
1621 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1622 } else {
1623 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1624 return CMD_WARNING;
1625 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1626 }
1627
1628
1629 if (bgp_install_info_to_zebra(bgp)) {
1630 /* Send distribute delete message to zebra. */
1631 if (BGP_DEBUG(zebra, ZEBRA))
1632 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1633 bgp->vrf_id, afi, zebra_route_string(type),
1634 instance);
1635 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1636 type, instance, bgp->vrf_id);
1637 }
1638
1639 /* Withdraw redistributed routes from current BGP's routing table. */
1640 bgp_redistribute_withdraw(bgp, afi, type, instance);
1641
1642 return CMD_SUCCESS;
1643 }
1644
1645 /* Unset redistribution. */
1646 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1647 unsigned short instance)
1648 {
1649 struct bgp_redist *red;
1650
1651 /*
1652 * vnc and vpn->vrf checks must be before red check because
1653 * they operate within bgpd irrespective of zebra connection
1654 * status. red lookup fails if there is no zebra connection.
1655 */
1656 #if ENABLE_BGP_VNC
1657 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1658 vnc_export_bgp_disable(bgp, afi);
1659 }
1660 #endif
1661
1662 red = bgp_redist_lookup(bgp, afi, type, instance);
1663 if (!red)
1664 return CMD_SUCCESS;
1665
1666 bgp_redistribute_unreg(bgp, afi, type, instance);
1667
1668 /* Unset route-map. */
1669 if (red->rmap.name)
1670 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1671 red->rmap.name = NULL;
1672 red->rmap.map = NULL;
1673
1674 /* Unset metric. */
1675 red->redist_metric_flag = 0;
1676 red->redist_metric = 0;
1677
1678 bgp_redist_del(bgp, afi, type, instance);
1679
1680 return CMD_SUCCESS;
1681 }
1682
1683 /* Update redistribute vrf bitmap during triggers like
1684 restart networking or delete/add VRFs */
1685 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1686 {
1687 int i;
1688 afi_t afi;
1689
1690 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1691 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1692 if ((old_vrf_id == VRF_UNKNOWN)
1693 || vrf_bitmap_check(zclient->redist[afi][i],
1694 old_vrf_id)) {
1695 vrf_bitmap_unset(zclient->redist[afi][i],
1696 old_vrf_id);
1697 vrf_bitmap_set(zclient->redist[afi][i],
1698 bgp->vrf_id);
1699 }
1700 return;
1701 }
1702
1703 void bgp_zclient_reset(void)
1704 {
1705 zclient_reset(zclient);
1706 }
1707
1708 /* Register this instance with Zebra. Invoked upon connect (for
1709 * default instance) and when other VRFs are learnt (or created and
1710 * already learnt).
1711 */
1712 void bgp_zebra_instance_register(struct bgp *bgp)
1713 {
1714 /* Don't try to register if we're not connected to Zebra */
1715 if (!zclient || zclient->sock < 0)
1716 return;
1717
1718 if (BGP_DEBUG(zebra, ZEBRA))
1719 zlog_debug("Registering VRF %u", bgp->vrf_id);
1720
1721 /* Register for router-id, interfaces, redistributed routes. */
1722 zclient_send_reg_requests(zclient, bgp->vrf_id);
1723
1724 /* For default instance, register to learn about VNIs, if appropriate.
1725 */
1726 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1727 bgp_zebra_advertise_all_vni(bgp, 1);
1728 }
1729
1730 /* Deregister this instance with Zebra. Invoked upon the instance
1731 * being deleted (default or VRF) and it is already registered.
1732 */
1733 void bgp_zebra_instance_deregister(struct bgp *bgp)
1734 {
1735 /* Don't try to deregister if we're not connected to Zebra */
1736 if (zclient->sock < 0)
1737 return;
1738
1739 if (BGP_DEBUG(zebra, ZEBRA))
1740 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
1741
1742 /* For default instance, unregister learning about VNIs, if appropriate.
1743 */
1744 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1745 bgp_zebra_advertise_all_vni(bgp, 0);
1746
1747 /* Deregister for router-id, interfaces, redistributed routes. */
1748 zclient_send_dereg_requests(zclient, bgp->vrf_id);
1749 }
1750
1751 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
1752 {
1753 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
1754
1755 /* Don't try to initiate if we're not connected to Zebra */
1756 if (zclient->sock < 0)
1757 return;
1758
1759 if (BGP_DEBUG(zebra, ZEBRA))
1760 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1761 peer->host);
1762
1763 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1764 ra_interval);
1765 }
1766
1767 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
1768 {
1769 /* Don't try to terminate if we're not connected to Zebra */
1770 if (zclient->sock < 0)
1771 return;
1772
1773 if (BGP_DEBUG(zebra, ZEBRA))
1774 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1775 peer->host);
1776
1777 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
1778 }
1779
1780 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1781 {
1782 struct stream *s = NULL;
1783
1784 /* Check socket. */
1785 if (!zclient || zclient->sock < 0)
1786 return 0;
1787
1788 /* Don't try to register if Zebra doesn't know of this instance. */
1789 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1790 return 0;
1791
1792 s = zclient->obuf;
1793 stream_reset(s);
1794
1795 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1796 stream_putc(s, advertise);
1797 stream_put3(s, vni);
1798 stream_putw_at(s, 0, stream_get_endp(s));
1799
1800 return zclient_send_message(zclient);
1801 }
1802
1803 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1804 {
1805 struct stream *s = NULL;
1806
1807 /* Check socket. */
1808 if (!zclient || zclient->sock < 0)
1809 return 0;
1810
1811 /* Don't try to register if Zebra doesn't know of this instance. */
1812 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1813 return 0;
1814
1815 s = zclient->obuf;
1816 stream_reset(s);
1817
1818 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1819 stream_putc(s, advertise);
1820 stream_put3(s, vni);
1821 stream_putw_at(s, 0, stream_get_endp(s));
1822
1823 return zclient_send_message(zclient);
1824 }
1825
1826 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
1827 {
1828 struct stream *s;
1829
1830 /* Check socket. */
1831 if (!zclient || zclient->sock < 0)
1832 return 0;
1833
1834 /* Don't try to register if Zebra doesn't know of this instance. */
1835 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1836 return 0;
1837
1838 s = zclient->obuf;
1839 stream_reset(s);
1840
1841 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1842 stream_putc(s, advertise);
1843 stream_putw_at(s, 0, stream_get_endp(s));
1844
1845 return zclient_send_message(zclient);
1846 }
1847
1848 /* BGP has established connection with Zebra. */
1849 static void bgp_zebra_connected(struct zclient *zclient)
1850 {
1851 struct bgp *bgp;
1852
1853 zclient_num_connects++; /* increment even if not responding */
1854
1855 /* At this point, we may or may not have BGP instances configured, but
1856 * we're only interested in the default VRF (others wouldn't have learnt
1857 * the VRF from Zebra yet.)
1858 */
1859 bgp = bgp_get_default();
1860 if (!bgp)
1861 return;
1862
1863 bgp_zebra_instance_register(bgp);
1864
1865 /* Send the client registration */
1866 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
1867
1868 /* TODO - What if we have peers and networks configured, do we have to
1869 * kick-start them?
1870 */
1871 }
1872
1873 static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
1874 zebra_size_t length, vrf_id_t vrf_id)
1875 {
1876 int filter = 0;
1877 char buf[ETHER_ADDR_STRLEN];
1878 vni_t l3vni = 0;
1879 struct ethaddr rmac;
1880 struct in_addr originator_ip;
1881 struct stream *s;
1882
1883 memset(&rmac, 0, sizeof(struct ethaddr));
1884 memset(&originator_ip, 0, sizeof(struct in_addr));
1885 s = zclient->ibuf;
1886 l3vni = stream_getl(s);
1887 if (cmd == ZEBRA_L3VNI_ADD) {
1888 stream_get(&rmac, s, sizeof(struct ethaddr));
1889 originator_ip.s_addr = stream_get_ipv4(s);
1890 stream_get(&filter, s, sizeof(int));
1891 }
1892
1893 if (BGP_DEBUG(zebra, ZEBRA))
1894 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1895 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
1896 vrf_id_to_name(vrf_id), l3vni,
1897 prefix_mac2str(&rmac, buf, sizeof(buf)),
1898 filter ? "prefix-routes-only" : "none");
1899
1900 if (cmd == ZEBRA_L3VNI_ADD)
1901 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
1902 filter);
1903 else
1904 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
1905
1906 return 0;
1907 }
1908
1909 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
1910 zebra_size_t length, vrf_id_t vrf_id)
1911 {
1912 struct stream *s;
1913 vni_t vni;
1914 struct bgp *bgp;
1915 struct in_addr vtep_ip = {INADDR_ANY};
1916 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
1917
1918 s = zclient->ibuf;
1919 vni = stream_getl(s);
1920 if (command == ZEBRA_VNI_ADD) {
1921 vtep_ip.s_addr = stream_get_ipv4(s);
1922 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
1923 }
1924
1925 bgp = bgp_lookup_by_vrf_id(vrf_id);
1926 if (!bgp)
1927 return 0;
1928
1929 if (BGP_DEBUG(zebra, ZEBRA))
1930 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1931 (command == ZEBRA_VNI_ADD) ? "add" : "del",
1932 vrf_id_to_name(vrf_id), vni,
1933 vrf_id_to_name(tenant_vrf_id));
1934
1935 if (command == ZEBRA_VNI_ADD)
1936 return bgp_evpn_local_vni_add(
1937 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
1938 tenant_vrf_id);
1939 else
1940 return bgp_evpn_local_vni_del(bgp, vni);
1941 }
1942
1943 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
1944 zebra_size_t length, vrf_id_t vrf_id)
1945 {
1946 struct stream *s;
1947 vni_t vni;
1948 struct bgp *bgp;
1949 struct ethaddr mac;
1950 struct ipaddr ip;
1951 int ipa_len;
1952 char buf[ETHER_ADDR_STRLEN];
1953 char buf1[INET6_ADDRSTRLEN];
1954 uint8_t flags;
1955
1956 memset(&ip, 0, sizeof(ip));
1957 s = zclient->ibuf;
1958 vni = stream_getl(s);
1959 stream_get(&mac.octet, s, ETH_ALEN);
1960 ipa_len = stream_getl(s);
1961 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
1962 && ipa_len != IPV6_MAX_BYTELEN) {
1963 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1964 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1965 ipa_len);
1966 return -1;
1967 }
1968
1969 if (ipa_len) {
1970 ip.ipa_type =
1971 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
1972 stream_get(&ip.ip.addr, s, ipa_len);
1973 }
1974 flags = stream_getc(s);
1975
1976 bgp = bgp_lookup_by_vrf_id(vrf_id);
1977 if (!bgp)
1978 return 0;
1979
1980 if (BGP_DEBUG(zebra, ZEBRA))
1981 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1982 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1983 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
1984 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
1985
1986 if (command == ZEBRA_MACIP_ADD)
1987 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, flags);
1988 else
1989 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
1990 }
1991
1992 static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
1993 zebra_size_t length,
1994 vrf_id_t vrf_id)
1995 {
1996 struct stream *s = NULL;
1997 struct bgp *bgp_vrf = NULL;
1998 struct prefix p;
1999 char buf[PREFIX_STRLEN];
2000
2001 memset(&p, 0, sizeof(struct prefix));
2002 s = zclient->ibuf;
2003 stream_get(&p, s, sizeof(struct prefix));
2004
2005 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
2006 if (!bgp_vrf)
2007 return;
2008
2009 if (BGP_DEBUG(zebra, ZEBRA))
2010 zlog_debug("Recv prefix %s %s on vrf %s",
2011 prefix2str(&p, buf, sizeof(buf)),
2012 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
2013 vrf_id_to_name(vrf_id));
2014
2015 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
2016
2017 if (p.family == AF_INET)
2018 return bgp_evpn_advertise_type5_route(
2019 bgp_vrf, &p, NULL, AFI_IP, SAFI_UNICAST);
2020 else
2021 return bgp_evpn_advertise_type5_route(
2022 bgp_vrf, &p, NULL, AFI_IP6, SAFI_UNICAST);
2023
2024 } else {
2025 if (p.family == AF_INET)
2026 return bgp_evpn_withdraw_type5_route(
2027 bgp_vrf, &p, AFI_IP, SAFI_UNICAST);
2028 else
2029 return bgp_evpn_withdraw_type5_route(
2030 bgp_vrf, &p, AFI_IP6, SAFI_UNICAST);
2031 }
2032 }
2033
2034 extern struct zebra_privs_t bgpd_privs;
2035
2036 void bgp_zebra_init(struct thread_master *master)
2037 {
2038 zclient_num_connects = 0;
2039
2040 /* Set default values. */
2041 zclient = zclient_new_notify(master, &zclient_options_default);
2042 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
2043 zclient->zebra_connected = bgp_zebra_connected;
2044 zclient->router_id_update = bgp_router_id_update;
2045 zclient->interface_add = bgp_interface_add;
2046 zclient->interface_delete = bgp_interface_delete;
2047 zclient->interface_address_add = bgp_interface_address_add;
2048 zclient->interface_address_delete = bgp_interface_address_delete;
2049 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2050 zclient->interface_nbr_address_delete =
2051 bgp_interface_nbr_address_delete;
2052 zclient->interface_vrf_update = bgp_interface_vrf_update;
2053 zclient->redistribute_route_add = zebra_read_route;
2054 zclient->redistribute_route_del = zebra_read_route;
2055 zclient->interface_up = bgp_interface_up;
2056 zclient->interface_down = bgp_interface_down;
2057 zclient->nexthop_update = bgp_read_nexthop_update;
2058 zclient->import_check_update = bgp_read_import_check_update;
2059 zclient->fec_update = bgp_read_fec_update;
2060 zclient->local_vni_add = bgp_zebra_process_local_vni;
2061 zclient->local_vni_del = bgp_zebra_process_local_vni;
2062 zclient->local_macip_add = bgp_zebra_process_local_macip;
2063 zclient->local_macip_del = bgp_zebra_process_local_macip;
2064 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2065 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
2066 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2067 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
2068 }
2069
2070 void bgp_zebra_destroy(void)
2071 {
2072 if (zclient == NULL)
2073 return;
2074 zclient_stop(zclient);
2075 zclient_free(zclient);
2076 zclient = NULL;
2077 }
2078
2079 int bgp_zebra_num_connects(void)
2080 {
2081 return zclient_num_connects;
2082 }