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