]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #1371 from donaldsharp/bgp_exit
[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 ifindex = mpinfo->peer->ifp->ifindex;
1110 else if (mpinfo->peer->ifname)
1111 ifindex = ifname2ifindex(
1112 mpinfo->peer->ifname,
1113 bgp->vrf_id);
1114 else if (mpinfo->peer->nexthop.ifp)
1115 ifindex = mpinfo->peer->nexthop.ifp
1116 ->ifindex;
1117 }
1118 if (ifindex == 0)
1119 continue;
1120
1121 api_nh = &api.nexthops[valid_nh_count];
1122 api_nh->gate.ipv6 = *nexthop;
1123 api_nh->ifindex = ifindex;
1124 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1125 }
1126
1127 if (mpinfo->extra
1128 && bgp_is_valid_label(&mpinfo->extra->label)) {
1129 has_valid_label = 1;
1130 label = label_pton(&mpinfo->extra->label);
1131
1132 api_nh->label_num = 1;
1133 api_nh->labels[0] = label;
1134 }
1135 valid_nh_count++;
1136 }
1137
1138 if (has_valid_label)
1139 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1140
1141 if (info->sub_type != BGP_ROUTE_AGGREGATE)
1142 api.nexthop_num = valid_nh_count;
1143
1144 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1145 api.metric = metric;
1146
1147 if (tag) {
1148 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1149 api.tag = tag;
1150 }
1151
1152 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1153 if (distance) {
1154 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1155 api.distance = distance;
1156 }
1157
1158 if (bgp_debug_zebra(p)) {
1159 char prefix_buf[PREFIX_STRLEN];
1160 char nh_buf[INET6_ADDRSTRLEN];
1161 char label_buf[20];
1162 int i;
1163
1164 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1165 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1166 " count %d",
1167 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1168 prefix_buf, api.metric, api.tag, api.nexthop_num);
1169 for (i = 0; i < api.nexthop_num; i++) {
1170 api_nh = &api.nexthops[i];
1171
1172 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1173 nh_family = AF_INET;
1174 else
1175 nh_family = AF_INET6;
1176 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1177 sizeof(nh_buf));
1178
1179 label_buf[0] = '\0';
1180 if (has_valid_label)
1181 sprintf(label_buf, "label %u",
1182 api_nh->labels[0]);
1183 zlog_debug(" nhop [%d]: %s %s", i + 1, nh_buf,
1184 label_buf);
1185 }
1186 }
1187
1188 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1189 : ZEBRA_ROUTE_DELETE,
1190 zclient, &api);
1191 }
1192
1193 /* Announce all routes of a table to zebra */
1194 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1195 {
1196 struct bgp_node *rn;
1197 struct bgp_table *table;
1198 struct bgp_info *ri;
1199
1200 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1201 * know of this instance.
1202 */
1203 if (!bgp_install_info_to_zebra(bgp))
1204 return;
1205
1206 table = bgp->rib[afi][safi];
1207 if (!table)
1208 return;
1209
1210 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1211 for (ri = rn->info; ri; ri = ri->next)
1212 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
1213 && ri->type == ZEBRA_ROUTE_BGP
1214 && ri->sub_type == BGP_ROUTE_NORMAL)
1215 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1216 safi);
1217 }
1218
1219 void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info, safi_t safi)
1220 {
1221 struct zapi_route api;
1222 struct peer *peer;
1223
1224 peer = info->peer;
1225 assert(peer);
1226
1227 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1228 * know of this instance.
1229 */
1230 if (!bgp_install_info_to_zebra(peer->bgp))
1231 return;
1232
1233 memset(&api, 0, sizeof(api));
1234 api.vrf_id = peer->bgp->vrf_id;
1235 api.type = ZEBRA_ROUTE_BGP;
1236 api.safi = safi;
1237 api.prefix = *p;
1238
1239 if (peer->sort == BGP_PEER_IBGP) {
1240 SET_FLAG(api.flags, ZEBRA_FLAG_INTERNAL);
1241 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1242 }
1243
1244 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1245 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1246 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1247 SET_FLAG(api.flags, ZEBRA_FLAG_INTERNAL);
1248
1249 if (bgp_debug_zebra(p)) {
1250 char buf[PREFIX_STRLEN];
1251
1252 prefix2str(&api.prefix, buf, sizeof(buf));
1253 zlog_debug("Tx route delete VRF %u %s", peer->bgp->vrf_id, buf);
1254 }
1255
1256 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1257 }
1258
1259 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, u_char type,
1260 u_short instance)
1261 {
1262 struct list *red_list;
1263 struct listnode *node;
1264 struct bgp_redist *red;
1265
1266 red_list = bgp->redist[afi][type];
1267 if (!red_list)
1268 return (NULL);
1269
1270 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1271 if (red->instance == instance)
1272 return red;
1273
1274 return NULL;
1275 }
1276
1277 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, u_char type,
1278 u_short instance)
1279 {
1280 struct list *red_list;
1281 struct bgp_redist *red;
1282
1283 red = bgp_redist_lookup(bgp, afi, type, instance);
1284 if (red)
1285 return red;
1286
1287 if (!bgp->redist[afi][type])
1288 bgp->redist[afi][type] = list_new();
1289
1290 red_list = bgp->redist[afi][type];
1291 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1292 sizeof(struct bgp_redist));
1293 red->instance = instance;
1294
1295 listnode_add(red_list, red);
1296
1297 return red;
1298 }
1299
1300 static void bgp_redist_del(struct bgp *bgp, afi_t afi, u_char type,
1301 u_short instance)
1302 {
1303 struct bgp_redist *red;
1304
1305 red = bgp_redist_lookup(bgp, afi, type, instance);
1306
1307 if (red) {
1308 listnode_delete(bgp->redist[afi][type], red);
1309 XFREE(MTYPE_BGP_REDIST, red);
1310 if (!bgp->redist[afi][type]->count)
1311 list_delete_and_null(&bgp->redist[afi][type]);
1312 }
1313 }
1314
1315 /* Other routes redistribution into BGP. */
1316 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type, u_short instance)
1317 {
1318
1319 /* Return if already redistribute flag is set. */
1320 if (instance) {
1321 if (redist_check_instance(&zclient->mi_redist[afi][type],
1322 instance))
1323 return CMD_WARNING;
1324
1325 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1326 } else {
1327 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1328 return CMD_WARNING;
1329
1330 #if ENABLE_BGP_VNC
1331 if (bgp->vrf_id == VRF_DEFAULT
1332 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1333 vnc_export_bgp_enable(
1334 bgp, afi); /* only enables if mode bits cfg'd */
1335 }
1336 #endif
1337
1338 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1339 }
1340
1341 /*
1342 * Don't try to register if we're not connected to Zebra or Zebra
1343 * doesn't know of this instance.
1344 *
1345 * When we come up later well resend if needed.
1346 */
1347 if (!bgp_install_info_to_zebra(bgp))
1348 return CMD_SUCCESS;
1349
1350 if (BGP_DEBUG(zebra, ZEBRA))
1351 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1352 bgp->vrf_id, afi, zebra_route_string(type),
1353 instance);
1354
1355 /* Send distribute add message to zebra. */
1356 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1357 instance, bgp->vrf_id);
1358
1359 return CMD_SUCCESS;
1360 }
1361
1362 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1363 u_short instance)
1364 {
1365 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1366 * know of this instance.
1367 */
1368 if (!bgp_install_info_to_zebra(bgp))
1369 return -1;
1370
1371 if (BGP_DEBUG(zebra, ZEBRA))
1372 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1373 bgp->vrf_id, afi, zebra_route_string(type),
1374 instance);
1375
1376 /* Send distribute add message to zebra. */
1377 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1378 instance, bgp->vrf_id);
1379 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1380 instance, bgp->vrf_id);
1381
1382 return 0;
1383 }
1384
1385 /* Redistribute with route-map specification. */
1386 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
1387 {
1388 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1389 return 0;
1390
1391 if (red->rmap.name)
1392 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1393 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1394 red->rmap.map = route_map_lookup_by_name(name);
1395
1396 return 1;
1397 }
1398
1399 /* Redistribute with metric specification. */
1400 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1401 afi_t afi, int type, u_int32_t metric)
1402 {
1403 struct bgp_node *rn;
1404 struct bgp_info *ri;
1405
1406 if (red->redist_metric_flag && red->redist_metric == metric)
1407 return 0;
1408
1409 red->redist_metric_flag = 1;
1410 red->redist_metric = metric;
1411
1412 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1413 rn = bgp_route_next(rn)) {
1414 for (ri = rn->info; ri; ri = ri->next) {
1415 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1416 && ri->type == type
1417 && ri->instance == red->instance) {
1418 struct attr *old_attr;
1419 struct attr new_attr;
1420
1421 bgp_attr_dup(&new_attr, ri->attr);
1422 new_attr.med = red->redist_metric;
1423 old_attr = ri->attr;
1424 ri->attr = bgp_attr_intern(&new_attr);
1425 bgp_attr_unintern(&old_attr);
1426
1427 bgp_info_set_flag(rn, ri,
1428 BGP_INFO_ATTR_CHANGED);
1429 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1430 }
1431 }
1432 }
1433
1434 return 1;
1435 }
1436
1437 /* Unset redistribution. */
1438 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1439 u_short instance)
1440 {
1441 struct bgp_redist *red;
1442
1443 red = bgp_redist_lookup(bgp, afi, type, instance);
1444 if (!red)
1445 return CMD_SUCCESS;
1446
1447 /* Return if zebra connection is disabled. */
1448 if (instance) {
1449 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1450 instance))
1451 return CMD_WARNING;
1452 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1453 } else {
1454 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1455 return CMD_WARNING;
1456 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1457 }
1458
1459 #if ENABLE_BGP_VNC
1460 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1461 vnc_export_bgp_disable(bgp, afi);
1462 }
1463 #endif
1464
1465 if (bgp_install_info_to_zebra(bgp)) {
1466 /* Send distribute delete message to zebra. */
1467 if (BGP_DEBUG(zebra, ZEBRA))
1468 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1469 bgp->vrf_id, afi, zebra_route_string(type),
1470 instance);
1471 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1472 type, instance, bgp->vrf_id);
1473 }
1474
1475 /* Withdraw redistributed routes from current BGP's routing table. */
1476 bgp_redistribute_withdraw(bgp, afi, type, instance);
1477
1478 return CMD_SUCCESS;
1479 }
1480
1481 /* Unset redistribution. */
1482 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1483 u_short instance)
1484 {
1485 struct bgp_redist *red;
1486
1487 red = bgp_redist_lookup(bgp, afi, type, instance);
1488 if (!red)
1489 return CMD_SUCCESS;
1490
1491 bgp_redistribute_unreg(bgp, afi, type, instance);
1492
1493 /* Unset route-map. */
1494 if (red->rmap.name)
1495 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1496 red->rmap.name = NULL;
1497 red->rmap.map = NULL;
1498
1499 /* Unset metric. */
1500 red->redist_metric_flag = 0;
1501 red->redist_metric = 0;
1502
1503 bgp_redist_del(bgp, afi, type, instance);
1504
1505 return CMD_SUCCESS;
1506 }
1507
1508 /* Update redistribute vrf bitmap during triggers like
1509 restart networking or delete/add VRFs */
1510 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1511 {
1512 int i;
1513 afi_t afi;
1514
1515 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1516 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1517 if (vrf_bitmap_check(zclient->redist[afi][i],
1518 old_vrf_id)) {
1519 vrf_bitmap_unset(zclient->redist[afi][i],
1520 old_vrf_id);
1521 vrf_bitmap_set(zclient->redist[afi][i],
1522 bgp->vrf_id);
1523 }
1524 return;
1525 }
1526
1527 void bgp_zclient_reset(void)
1528 {
1529 zclient_reset(zclient);
1530 }
1531
1532 /* Register this instance with Zebra. Invoked upon connect (for
1533 * default instance) and when other VRFs are learnt (or created and
1534 * already learnt).
1535 */
1536 void bgp_zebra_instance_register(struct bgp *bgp)
1537 {
1538 /* Don't try to register if we're not connected to Zebra */
1539 if (!zclient || zclient->sock < 0)
1540 return;
1541
1542 if (BGP_DEBUG(zebra, ZEBRA))
1543 zlog_debug("Registering VRF %u", bgp->vrf_id);
1544
1545 /* Register for router-id, interfaces, redistributed routes. */
1546 zclient_send_reg_requests(zclient, bgp->vrf_id);
1547
1548 /* For default instance, register to learn about VNIs, if appropriate.
1549 */
1550 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
1551 && bgp->advertise_all_vni)
1552 bgp_zebra_advertise_all_vni(bgp, 1);
1553 }
1554
1555 /* Deregister this instance with Zebra. Invoked upon the instance
1556 * being deleted (default or VRF) and it is already registered.
1557 */
1558 void bgp_zebra_instance_deregister(struct bgp *bgp)
1559 {
1560 /* Don't try to deregister if we're not connected to Zebra */
1561 if (zclient->sock < 0)
1562 return;
1563
1564 if (BGP_DEBUG(zebra, ZEBRA))
1565 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
1566
1567 /* For default instance, unregister learning about VNIs, if appropriate.
1568 */
1569 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
1570 && bgp->advertise_all_vni)
1571 bgp_zebra_advertise_all_vni(bgp, 0);
1572
1573 /* Deregister for router-id, interfaces, redistributed routes. */
1574 zclient_send_dereg_requests(zclient, bgp->vrf_id);
1575 }
1576
1577 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
1578 {
1579 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
1580
1581 /* Don't try to initiate if we're not connected to Zebra */
1582 if (zclient->sock < 0)
1583 return;
1584
1585 if (BGP_DEBUG(zebra, ZEBRA))
1586 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1587 peer->host);
1588
1589 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1590 ra_interval);
1591 }
1592
1593 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
1594 {
1595 /* Don't try to terminate if we're not connected to Zebra */
1596 if (zclient->sock < 0)
1597 return;
1598
1599 if (BGP_DEBUG(zebra, ZEBRA))
1600 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1601 peer->host);
1602
1603 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
1604 }
1605
1606 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1607 {
1608 struct stream *s = NULL;
1609
1610 /* Check socket. */
1611 if (!zclient || zclient->sock < 0)
1612 return 0;
1613
1614 /* Don't try to register if Zebra doesn't know of this instance. */
1615 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1616 return 0;
1617
1618 s = zclient->obuf;
1619 stream_reset(s);
1620
1621 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1622 stream_putc(s, advertise);
1623 stream_put3(s, vni);
1624 stream_putw_at(s, 0, stream_get_endp(s));
1625
1626 return zclient_send_message(zclient);
1627 }
1628
1629 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
1630 {
1631 struct stream *s;
1632
1633 /* Check socket. */
1634 if (!zclient || zclient->sock < 0)
1635 return 0;
1636
1637 /* Don't try to register if Zebra doesn't know of this instance. */
1638 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1639 return 0;
1640
1641 s = zclient->obuf;
1642 stream_reset(s);
1643
1644 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1645 stream_putc(s, advertise);
1646 stream_putw_at(s, 0, stream_get_endp(s));
1647
1648 return zclient_send_message(zclient);
1649 }
1650
1651 /* BGP has established connection with Zebra. */
1652 static void bgp_zebra_connected(struct zclient *zclient)
1653 {
1654 struct bgp *bgp;
1655
1656 zclient_num_connects++; /* increment even if not responding */
1657
1658 /* At this point, we may or may not have BGP instances configured, but
1659 * we're only interested in the default VRF (others wouldn't have learnt
1660 * the VRF from Zebra yet.)
1661 */
1662 bgp = bgp_get_default();
1663 if (!bgp)
1664 return;
1665
1666 bgp_zebra_instance_register(bgp);
1667
1668 /* Send the client registration */
1669 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
1670
1671 /* TODO - What if we have peers and networks configured, do we have to
1672 * kick-start them?
1673 */
1674 }
1675
1676 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
1677 zebra_size_t length, vrf_id_t vrf_id)
1678 {
1679 struct stream *s;
1680 vni_t vni;
1681 struct bgp *bgp;
1682 struct in_addr vtep_ip;
1683
1684 s = zclient->ibuf;
1685 vni = stream_getl(s);
1686 if (command == ZEBRA_VNI_ADD)
1687 vtep_ip.s_addr = stream_get_ipv4(s);
1688 bgp = bgp_lookup_by_vrf_id(vrf_id);
1689 if (!bgp)
1690 return 0;
1691
1692 if (BGP_DEBUG(zebra, ZEBRA))
1693 zlog_debug("Rx VNI %s VRF %u VNI %u",
1694 (command == ZEBRA_VNI_ADD) ? "add" : "del", vrf_id,
1695 vni);
1696
1697 if (command == ZEBRA_VNI_ADD)
1698 return bgp_evpn_local_vni_add(
1699 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id);
1700 else
1701 return bgp_evpn_local_vni_del(bgp, vni);
1702 }
1703
1704 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
1705 zebra_size_t length, vrf_id_t vrf_id)
1706 {
1707 struct stream *s;
1708 vni_t vni;
1709 struct bgp *bgp;
1710 struct ethaddr mac;
1711 struct ipaddr ip;
1712 int ipa_len;
1713 char buf[ETHER_ADDR_STRLEN];
1714 char buf1[INET6_ADDRSTRLEN];
1715 u_char flags;
1716
1717 memset(&ip, 0, sizeof(ip));
1718 s = zclient->ibuf;
1719 vni = stream_getl(s);
1720 stream_get(&mac.octet, s, ETH_ALEN);
1721 ipa_len = stream_getl(s);
1722 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
1723 && ipa_len != IPV6_MAX_BYTELEN) {
1724 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1725 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1726 ipa_len);
1727 return -1;
1728 }
1729
1730 if (ipa_len) {
1731 ip.ipa_type =
1732 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
1733 stream_get(&ip.ip.addr, s, ipa_len);
1734 }
1735 flags = stream_getc(s);
1736
1737 bgp = bgp_lookup_by_vrf_id(vrf_id);
1738 if (!bgp)
1739 return 0;
1740
1741 if (BGP_DEBUG(zebra, ZEBRA))
1742 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1743 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1744 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
1745 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
1746
1747 if (command == ZEBRA_MACIP_ADD)
1748 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, flags);
1749 else
1750 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
1751 }
1752
1753 extern struct zebra_privs_t bgpd_privs;
1754
1755 void bgp_zebra_init(struct thread_master *master)
1756 {
1757 zclient_num_connects = 0;
1758
1759 /* Set default values. */
1760 zclient = zclient_new(master);
1761 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
1762 zclient->zebra_connected = bgp_zebra_connected;
1763 zclient->router_id_update = bgp_router_id_update;
1764 zclient->interface_add = bgp_interface_add;
1765 zclient->interface_delete = bgp_interface_delete;
1766 zclient->interface_address_add = bgp_interface_address_add;
1767 zclient->interface_address_delete = bgp_interface_address_delete;
1768 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
1769 zclient->interface_nbr_address_delete =
1770 bgp_interface_nbr_address_delete;
1771 zclient->interface_vrf_update = bgp_interface_vrf_update;
1772 zclient->redistribute_route_add = zebra_read_route;
1773 zclient->redistribute_route_del = zebra_read_route;
1774 zclient->interface_up = bgp_interface_up;
1775 zclient->interface_down = bgp_interface_down;
1776 zclient->nexthop_update = bgp_read_nexthop_update;
1777 zclient->import_check_update = bgp_read_import_check_update;
1778 zclient->fec_update = bgp_read_fec_update;
1779 zclient->local_vni_add = bgp_zebra_process_local_vni;
1780 zclient->local_vni_del = bgp_zebra_process_local_vni;
1781 zclient->local_macip_add = bgp_zebra_process_local_macip;
1782 zclient->local_macip_del = bgp_zebra_process_local_macip;
1783 }
1784
1785 void bgp_zebra_destroy(void)
1786 {
1787 if (zclient == NULL)
1788 return;
1789 zclient_stop(zclient);
1790 zclient_free(zclient);
1791 zclient = NULL;
1792 }
1793
1794 int bgp_zebra_num_connects(void)
1795 {
1796 return zclient_num_connects;
1797 }