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