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