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