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