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