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