]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
be3c6eea3a2418a305de3a0807866b8dbc7c97e7
[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 ifp->ifindex = IFINDEX_DELETED;
273
274 if (BGP_DEBUG(zebra, ZEBRA))
275 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
276
277 bgp = bgp_lookup_by_vrf_id(vrf_id);
278 if (!bgp)
279 return 0;
280
281 bgp_update_interface_nbrs(bgp, ifp, NULL);
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,
625 inet_ntop(AF_INET, &p.prefix, buf[0],
626 sizeof(buf[0])),
627 p.prefixlen,
628 inet_ntop(AF_INET, &nexthop, buf[1],
629 sizeof(buf[1])),
630 api.metric, api.tag);
631 }
632
633 /*
634 * The ADD message is actually an UPDATE and there is no
635 * explicit DEL
636 * for a prior redistributed route, if any. So, perform an
637 * implicit
638 * DEL processing for the same redistributed route from any
639 * other
640 * source type.
641 */
642 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
643 if (i != api.type)
644 bgp_redistribute_delete(bgp,
645 (struct prefix *)&p, i,
646 api.instance);
647 }
648
649 /* Now perform the add/update. */
650 bgp_redistribute_add(bgp, (struct prefix *)&p, &nexthop, NULL,
651 ifindex, api.metric, api.type,
652 api.instance, api.tag);
653 } else if (command == ZEBRA_REDISTRIBUTE_IPV4_DEL) {
654 if (bgp_debug_zebra((struct prefix *)&p)) {
655 char buf[2][INET_ADDRSTRLEN];
656 zlog_debug(
657 "Rx IPv4 route delete VRF %u %s[%d] %s/%d "
658 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
659 vrf_id, zebra_route_string(api.type),
660 api.instance,
661 inet_ntop(AF_INET, &p.prefix, buf[0],
662 sizeof(buf[0])),
663 p.prefixlen,
664 inet_ntop(AF_INET, &nexthop, buf[1],
665 sizeof(buf[1])),
666 api.metric, api.tag);
667 }
668 bgp_redistribute_delete(bgp, (struct prefix *)&p, api.type,
669 api.instance);
670 }
671
672 return 0;
673 }
674
675 /* Zebra route add and delete treatment. */
676 static int zebra_read_ipv6(int command, struct zclient *zclient,
677 zebra_size_t length, vrf_id_t vrf_id)
678 {
679 struct stream *s;
680 struct zapi_ipv6 api;
681 struct in6_addr nexthop;
682 struct prefix_ipv6 p, src_p;
683 unsigned int ifindex;
684 int i;
685 struct bgp *bgp;
686
687 bgp = bgp_lookup_by_vrf_id(vrf_id);
688 if (!bgp)
689 return 0;
690
691 s = zclient->ibuf;
692 memset(&nexthop, 0, sizeof(struct in6_addr));
693
694 /* Type, flags, message. */
695 api.type = stream_getc(s);
696 api.instance = stream_getw(s);
697 api.flags = stream_getl(s);
698 api.message = stream_getc(s);
699
700 /* IPv6 prefix. */
701 memset(&p, 0, sizeof(struct prefix_ipv6));
702 p.family = AF_INET6;
703 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc(s));
704 stream_get(&p.prefix, s, PSIZE(p.prefixlen));
705
706 memset(&src_p, 0, sizeof(struct prefix_ipv6));
707 src_p.family = AF_INET6;
708 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
709 src_p.prefixlen = stream_getc(s);
710 stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen));
711 }
712
713 if (src_p.prefixlen)
714 /* we completely ignore srcdest routes for now. */
715 return 0;
716
717 /* Nexthop, ifindex, distance, metric. */
718 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
719 api.nexthop_num = stream_getc(s);
720 stream_get(&nexthop, s, 16);
721 }
722
723 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_IFINDEX)) {
724 api.ifindex_num = stream_getc(s);
725 ifindex = stream_getl(s); /* ifindex, unused */
726 } else {
727 ifindex = 0;
728 }
729
730 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
731 api.distance = stream_getc(s);
732 else
733 api.distance = 0;
734
735 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
736 api.metric = stream_getl(s);
737 else
738 api.metric = 0;
739
740 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
741 api.tag = stream_getl(s);
742 else
743 api.tag = 0;
744
745 /* Simply ignore link-local address. */
746 if (IN6_IS_ADDR_LINKLOCAL(&p.prefix))
747 return 0;
748
749 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD) {
750 if (bgp_debug_zebra((struct prefix *)&p)) {
751 char buf[2][INET6_ADDRSTRLEN];
752 zlog_debug(
753 "Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %" ROUTE_TAG_PRI,
754 vrf_id, zebra_route_string(api.type),
755 api.instance,
756 inet_ntop(AF_INET6, &p.prefix, buf[0],
757 sizeof(buf[0])),
758 p.prefixlen,
759 inet_ntop(AF_INET, &nexthop, buf[1],
760 sizeof(buf[1])),
761 api.metric, api.tag);
762 }
763
764 /*
765 * The ADD message is actually an UPDATE and there is no
766 * explicit DEL
767 * for a prior redistributed route, if any. So, perform an
768 * implicit
769 * DEL processing for the same redistributed route from any
770 * other
771 * source type.
772 */
773 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
774 if (i != api.type)
775 bgp_redistribute_delete(bgp,
776 (struct prefix *)&p, i,
777 api.instance);
778 }
779
780 bgp_redistribute_add(bgp, (struct prefix *)&p, NULL, &nexthop,
781 ifindex, api.metric, api.type,
782 api.instance, api.tag);
783 } else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL) {
784 if (bgp_debug_zebra((struct prefix *)&p)) {
785 char buf[2][INET6_ADDRSTRLEN];
786 zlog_debug(
787 "Rx IPv6 route delete VRF %u %s[%d] %s/%d "
788 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
789 vrf_id, zebra_route_string(api.type),
790 api.instance,
791 inet_ntop(AF_INET6, &p.prefix, buf[0],
792 sizeof(buf[0])),
793 p.prefixlen,
794 inet_ntop(AF_INET6, &nexthop, buf[1],
795 sizeof(buf[1])),
796 api.metric, api.tag);
797 }
798 bgp_redistribute_delete(bgp, (struct prefix *)&p, api.type,
799 api.instance);
800 }
801
802 return 0;
803 }
804
805 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
806 {
807 struct listnode *ifnode;
808 struct listnode *cnode;
809 struct interface *ifp;
810 struct connected *connected;
811 struct prefix_ipv4 p;
812 struct prefix *cp;
813
814 p.family = AF_INET;
815 p.prefix = *addr;
816 p.prefixlen = IPV4_MAX_BITLEN;
817
818 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), ifnode, ifp)) {
819 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
820 cp = connected->address;
821
822 if (cp->family == AF_INET)
823 if (prefix_match(cp, (struct prefix *)&p))
824 return ifp;
825 }
826 }
827 return NULL;
828 }
829
830 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
831 {
832 struct listnode *ifnode;
833 struct listnode *cnode;
834 struct interface *ifp;
835 struct connected *connected;
836 struct prefix *cp;
837
838 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), ifnode, ifp)) {
839 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
840 cp = connected->address;
841
842 if (cp->family == AF_INET)
843 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
844 return ifp;
845 }
846 }
847 return NULL;
848 }
849
850 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
851 vrf_id_t vrf_id)
852 {
853 struct listnode *ifnode;
854 struct listnode *cnode;
855 struct interface *ifp;
856 struct connected *connected;
857 struct prefix_ipv6 p;
858 struct prefix *cp;
859
860 p.family = AF_INET6;
861 p.prefix = *addr;
862 p.prefixlen = IPV6_MAX_BITLEN;
863
864 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), ifnode, ifp)) {
865 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
866 cp = connected->address;
867
868 if (cp->family == AF_INET6)
869 if (prefix_match(cp, (struct prefix *)&p)) {
870 if (IN6_IS_ADDR_LINKLOCAL(
871 &cp->u.prefix6)) {
872 if (ifindex == ifp->ifindex)
873 return ifp;
874 } else
875 return ifp;
876 }
877 }
878 }
879 return NULL;
880 }
881
882 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
883 ifindex_t ifindex, vrf_id_t vrf_id)
884 {
885 struct listnode *ifnode;
886 struct listnode *cnode;
887 struct interface *ifp;
888 struct connected *connected;
889 struct prefix *cp;
890
891 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), ifnode, ifp)) {
892 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
893 cp = connected->address;
894
895 if (cp->family == AF_INET6)
896 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
897 if (IN6_IS_ADDR_LINKLOCAL(
898 &cp->u.prefix6)) {
899 if (ifindex == ifp->ifindex)
900 return ifp;
901 } else
902 return ifp;
903 }
904 }
905 }
906 return NULL;
907 }
908
909 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
910 {
911 struct listnode *cnode;
912 struct connected *connected;
913 struct prefix *cp;
914
915 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
916 cp = connected->address;
917
918 if (cp->family == AF_INET6)
919 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
920 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
921 return 1;
922 }
923 }
924 return 0;
925 }
926
927 static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
928 {
929 struct listnode *cnode;
930 struct connected *connected;
931 struct prefix *cp;
932
933 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
934 cp = connected->address;
935
936 if (cp->family == AF_INET6)
937 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
938 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
939 return 1;
940 }
941 }
942 return 0;
943 }
944
945 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
946 {
947 struct listnode *cnode;
948 struct connected *connected;
949 struct prefix *cp;
950
951 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
952 cp = connected->address;
953 if ((cp->family == AF_INET)
954 && !ipv4_martian(&(cp->u.prefix4))) {
955 *addr = cp->u.prefix4;
956 return 1;
957 }
958 }
959 return 0;
960 }
961
962 int bgp_nexthop_set(union sockunion *local, union sockunion *remote,
963 struct bgp_nexthop *nexthop, struct peer *peer)
964 {
965 int ret = 0;
966 struct interface *ifp = NULL;
967
968 memset(nexthop, 0, sizeof(struct bgp_nexthop));
969
970 if (!local)
971 return -1;
972 if (!remote)
973 return -1;
974
975 if (local->sa.sa_family == AF_INET) {
976 nexthop->v4 = local->sin.sin_addr;
977 if (peer->update_if)
978 ifp = if_lookup_by_name(peer->update_if,
979 peer->bgp->vrf_id);
980 else
981 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
982 peer->bgp->vrf_id);
983 }
984 if (local->sa.sa_family == AF_INET6) {
985 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
986 if (peer->conf_if || peer->ifname)
987 ifp = if_lookup_by_name(peer->conf_if
988 ? peer->conf_if
989 : peer->ifname,
990 peer->bgp->vrf_id);
991 } else if (peer->update_if)
992 ifp = if_lookup_by_name(peer->update_if,
993 peer->bgp->vrf_id);
994 else
995 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
996 local->sin6.sin6_scope_id,
997 peer->bgp->vrf_id);
998 }
999
1000 if (!ifp)
1001 return -1;
1002
1003 nexthop->ifp = ifp;
1004
1005 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
1006 if (local->sa.sa_family == AF_INET) {
1007 /* IPv6 nexthop*/
1008 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
1009
1010 if (!ret) {
1011 /* There is no global nexthop. Use link-local address as
1012 * both the
1013 * global and link-local nexthop. In this scenario, the
1014 * expectation
1015 * for interop is that the network admin would use a
1016 * route-map to
1017 * specify the global IPv6 nexthop.
1018 */
1019 if_get_ipv6_local(ifp, &nexthop->v6_global);
1020 memcpy(&nexthop->v6_local, &nexthop->v6_global,
1021 IPV6_MAX_BYTELEN);
1022 } else
1023 if_get_ipv6_local(ifp, &nexthop->v6_local);
1024
1025 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
1026 peer->shared_network = 1;
1027 else
1028 peer->shared_network = 0;
1029 }
1030
1031 /* IPv6 connection, fetch and store IPv4 local address if any. */
1032 if (local->sa.sa_family == AF_INET6) {
1033 struct interface *direct = NULL;
1034
1035 /* IPv4 nexthop. */
1036 ret = if_get_ipv4_address(ifp, &nexthop->v4);
1037 if (!ret && peer->local_id.s_addr)
1038 nexthop->v4 = peer->local_id;
1039
1040 /* Global address*/
1041 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
1042 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
1043 IPV6_MAX_BYTELEN);
1044
1045 /* If directory connected set link-local address. */
1046 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
1047 remote->sin6.sin6_scope_id,
1048 peer->bgp->vrf_id);
1049 if (direct)
1050 if_get_ipv6_local(ifp, &nexthop->v6_local);
1051 } else
1052 /* Link-local address. */
1053 {
1054 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
1055
1056 /* If there is no global address. Set link-local
1057 address as
1058 global. I know this break RFC specification... */
1059 /* In this scenario, the expectation for interop is that
1060 * the
1061 * network admin would use a route-map to specify the
1062 * global
1063 * IPv6 nexthop.
1064 */
1065 if (!ret)
1066 memcpy(&nexthop->v6_global,
1067 &local->sin6.sin6_addr,
1068 IPV6_MAX_BYTELEN);
1069 /* Always set the link-local address */
1070 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
1071 IPV6_MAX_BYTELEN);
1072 }
1073
1074 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
1075 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
1076 remote->sin6.sin6_scope_id,
1077 peer->bgp->vrf_id))
1078 peer->shared_network = 1;
1079 else
1080 peer->shared_network = 0;
1081 }
1082
1083 /* KAME stack specific treatment. */
1084 #ifdef KAME
1085 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
1086 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
1087 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
1088 }
1089 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
1090 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
1091 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
1092 }
1093 #endif /* KAME */
1094
1095 /* If we have identified the local interface, there is no error for now.
1096 */
1097 return 0;
1098 }
1099
1100 static struct in6_addr *bgp_info_to_ipv6_nexthop(struct bgp_info *info)
1101 {
1102 struct in6_addr *nexthop = NULL;
1103
1104 /* Only global address nexthop exists. */
1105 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
1106 nexthop = &info->attr->mp_nexthop_global;
1107
1108 /* If both global and link-local address present. */
1109 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
1110 /* Check if route-map is set to prefer global over link-local */
1111 if (info->attr->mp_nexthop_prefer_global)
1112 nexthop = &info->attr->mp_nexthop_global;
1113 else {
1114 /* Workaround for Cisco's nexthop bug. */
1115 if (IN6_IS_ADDR_UNSPECIFIED(
1116 &info->attr->mp_nexthop_global)
1117 && info->peer->su_remote->sa.sa_family == AF_INET6)
1118 nexthop =
1119 &info->peer->su_remote->sin6.sin6_addr;
1120 else
1121 nexthop = &info->attr->mp_nexthop_local;
1122 }
1123 }
1124
1125 return nexthop;
1126 }
1127
1128 static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
1129 struct bgp_info *info)
1130 {
1131 if (route_map_apply(map, p, RMAP_BGP, info) != RMAP_DENYMATCH)
1132 return 1;
1133
1134 if (bgp_debug_zebra(p)) {
1135 if (p->family == AF_INET) {
1136 char buf[2][INET_ADDRSTRLEN];
1137 zlog_debug(
1138 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1139 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
1140 sizeof(buf[0])),
1141 p->prefixlen,
1142 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
1143 sizeof(buf[1])));
1144 }
1145 if (p->family == AF_INET6) {
1146 char buf[2][INET6_ADDRSTRLEN];
1147 zlog_debug(
1148 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1149 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
1150 sizeof(buf[0])),
1151 p->prefixlen,
1152 inet_ntop(AF_INET6,
1153 bgp_info_to_ipv6_nexthop(info),
1154 buf[1], sizeof(buf[1])));
1155 }
1156 }
1157 return 0;
1158 }
1159
1160 void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
1161 struct bgp_info *info, struct bgp *bgp, afi_t afi,
1162 safi_t safi)
1163 {
1164 u_int32_t flags;
1165 u_char distance;
1166 struct peer *peer;
1167 struct bgp_info *mpinfo;
1168 u_int32_t metric;
1169 struct bgp_info local_info;
1170 struct bgp_info *info_cp = &local_info;
1171 route_tag_t tag;
1172 mpls_label_t label;
1173
1174 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1175 * know of this instance.
1176 */
1177 if (!bgp_install_info_to_zebra(bgp))
1178 return;
1179
1180 if ((p->family == AF_INET
1181 && !vrf_bitmap_check(zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP],
1182 bgp->vrf_id))
1183 || (p->family == AF_INET6
1184 && !vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP],
1185 bgp->vrf_id)))
1186 return;
1187
1188 if (bgp->main_zebra_update_hold)
1189 return;
1190
1191 flags = 0;
1192 peer = info->peer;
1193
1194 tag = info->attr->tag;
1195
1196 /* When we create an aggregate route we must also install a Null0 route
1197 * in
1198 * the RIB */
1199 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1200 SET_FLAG(flags, ZEBRA_FLAG_BLACKHOLE);
1201
1202 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1203 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1204 SET_FLAG(flags, ZEBRA_FLAG_IBGP);
1205 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
1206 }
1207
1208 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1209 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1210 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1211
1212 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
1213
1214 if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(info->attr)) {
1215 struct zapi_ipv4 api;
1216 struct in_addr *nexthop;
1217 char buf[2][INET_ADDRSTRLEN];
1218 int valid_nh_count = 0;
1219 int has_valid_label = 0;
1220
1221 /* resize nexthop buffer size if necessary */
1222 stream_reset(bgp_nexthop_buf);
1223 nexthop = NULL;
1224
1225 stream_reset(bgp_label_buf);
1226
1227 if (bgp->table_map[afi][safi].name)
1228 BGP_INFO_ATTR_BUF_INIT();
1229
1230 /* Metric is currently based on the best-path only */
1231 metric = info->attr->med;
1232 for (mpinfo = info; mpinfo;
1233 mpinfo = bgp_info_mpath_next(mpinfo)) {
1234 nexthop = NULL;
1235
1236 if (bgp->table_map[afi][safi].name) {
1237 /* Copy info and attributes, so the route-map
1238 apply doesn't modify the
1239 BGP route info. */
1240 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1241 if (bgp_table_map_apply(
1242 bgp->table_map[afi][safi].map, p,
1243 info_cp)) {
1244 if (mpinfo == info) {
1245 /* Metric is currently based on
1246 * the best-path only */
1247 metric = info_cp->attr->med;
1248 tag = info_cp->attr->tag;
1249 }
1250 nexthop = &info_cp->attr->nexthop;
1251 }
1252 BGP_INFO_ATTR_BUF_FREE(info_cp);
1253 } else
1254 nexthop = &mpinfo->attr->nexthop;
1255
1256 if (nexthop == NULL)
1257 continue;
1258
1259 stream_put(bgp_nexthop_buf, &nexthop,
1260 sizeof(struct in_addr *));
1261 if (mpinfo->extra
1262 && bgp_is_valid_label(&mpinfo->extra->label)) {
1263 has_valid_label = 1;
1264 label = label_pton(&mpinfo->extra->label);
1265 stream_put(bgp_label_buf, &label,
1266 sizeof(mpls_label_t));
1267 }
1268 valid_nh_count++;
1269 }
1270
1271 api.vrf_id = bgp->vrf_id;
1272 api.flags = flags;
1273 api.type = ZEBRA_ROUTE_BGP;
1274 api.instance = 0;
1275 api.message = 0;
1276 api.safi = safi;
1277 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1278
1279 if (has_valid_label)
1280 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1281
1282 /* Note that this currently only applies to Null0 routes for
1283 * aggregates.
1284 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a
1285 * special
1286 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero
1287 * since we
1288 * do not want to also encode the 0.0.0.0 nexthop for the
1289 * aggregate route.
1290 */
1291 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1292 api.nexthop_num = 0;
1293 else
1294 api.nexthop_num = valid_nh_count;
1295
1296 api.nexthop = (struct in_addr **)STREAM_DATA(bgp_nexthop_buf);
1297 if (has_valid_label) {
1298 api.label_num = valid_nh_count;
1299 api.label = (unsigned int *)STREAM_DATA(bgp_label_buf);
1300 } else {
1301 api.label_num = 0;
1302 api.label = NULL;
1303 }
1304 api.ifindex_num = 0;
1305 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1306 api.metric = metric;
1307 api.tag = 0;
1308
1309 if (tag) {
1310 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1311 api.tag = tag;
1312 }
1313
1314 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1315 if (distance) {
1316 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1317 api.distance = distance;
1318 }
1319
1320 if (bgp_debug_zebra(p)) {
1321 int i;
1322 char label_buf[20];
1323 zlog_debug(
1324 "Tx IPv4 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI
1325 " count %d",
1326 (valid_nh_count ? "add" : "delete"),
1327 bgp->vrf_id,
1328 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
1329 sizeof(buf[0])),
1330 p->prefixlen, api.metric, api.tag,
1331 api.nexthop_num);
1332 for (i = 0; i < api.nexthop_num; i++) {
1333 label_buf[0] = '\0';
1334 if (has_valid_label)
1335 sprintf(label_buf, "label %u",
1336 api.label[i]);
1337 zlog_debug(" nhop [%d]: %s %s", i + 1,
1338 inet_ntop(AF_INET, api.nexthop[i],
1339 buf[1], sizeof(buf[1])),
1340 label_buf);
1341 }
1342 }
1343
1344 zapi_ipv4_route(valid_nh_count ? ZEBRA_IPV4_ROUTE_ADD
1345 : ZEBRA_IPV4_ROUTE_DELETE,
1346 zclient, (struct prefix_ipv4 *)p, &api);
1347 }
1348
1349 /* We have to think about a IPv6 link-local address curse. */
1350 if (p->family == AF_INET6
1351 || (p->family == AF_INET && BGP_ATTR_NEXTHOP_AFI_IP6(info->attr))) {
1352 ifindex_t ifindex;
1353 struct in6_addr *nexthop;
1354 struct zapi_ipv6 api;
1355 int valid_nh_count = 0;
1356 char buf[2][INET6_ADDRSTRLEN];
1357 int has_valid_label = 0;
1358
1359 stream_reset(bgp_nexthop_buf);
1360 stream_reset(bgp_ifindices_buf);
1361 stream_reset(bgp_label_buf);
1362
1363 ifindex = 0;
1364 nexthop = NULL;
1365
1366 if (bgp->table_map[afi][safi].name)
1367 BGP_INFO_ATTR_BUF_INIT();
1368
1369 metric = info->attr->med;
1370 for (mpinfo = info; mpinfo;
1371 mpinfo = bgp_info_mpath_next(mpinfo)) {
1372 ifindex = 0;
1373 nexthop = NULL;
1374
1375 if (bgp->table_map[afi][safi].name) {
1376 /* Copy info and attributes, so the route-map
1377 apply doesn't modify the
1378 BGP route info. */
1379 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1380 if (bgp_table_map_apply(
1381 bgp->table_map[afi][safi].map, p,
1382 info_cp)) {
1383 if (mpinfo == info) {
1384 metric = info_cp->attr->med;
1385 tag = info_cp->attr->tag;
1386 }
1387 nexthop = bgp_info_to_ipv6_nexthop(
1388 info_cp);
1389 }
1390 BGP_INFO_ATTR_BUF_FREE(info_cp);
1391 } else
1392 nexthop = bgp_info_to_ipv6_nexthop(mpinfo);
1393
1394 if (nexthop == NULL)
1395 continue;
1396
1397 if ((mpinfo == info)
1398 && mpinfo->attr->mp_nexthop_len
1399 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1400 if (mpinfo->peer->nexthop.ifp)
1401 ifindex = mpinfo->peer->nexthop.ifp
1402 ->ifindex;
1403
1404 if (!ifindex) {
1405 if (mpinfo->peer->conf_if
1406 || mpinfo->peer->ifname)
1407 ifindex = ifname2ifindex(
1408 mpinfo->peer->conf_if
1409 ? mpinfo->peer->conf_if
1410 : mpinfo->peer->ifname,
1411 bgp->vrf_id);
1412 else if (mpinfo->peer->nexthop.ifp)
1413 ifindex = mpinfo->peer->nexthop.ifp
1414 ->ifindex;
1415 }
1416 if (ifindex == 0)
1417 continue;
1418
1419 stream_put(bgp_nexthop_buf, &nexthop,
1420 sizeof(struct in6_addr *));
1421 stream_put(bgp_ifindices_buf, &ifindex,
1422 sizeof(unsigned int));
1423
1424 if (mpinfo->extra
1425 && bgp_is_valid_label(&mpinfo->extra->label)) {
1426 has_valid_label = 1;
1427 label = label_pton(&mpinfo->extra->label);
1428 stream_put(bgp_label_buf, &label,
1429 sizeof(mpls_label_t));
1430 }
1431 valid_nh_count++;
1432 }
1433
1434 /* Make Zebra API structure. */
1435 api.vrf_id = bgp->vrf_id;
1436 api.flags = flags;
1437 api.type = ZEBRA_ROUTE_BGP;
1438 api.instance = 0;
1439 api.message = 0;
1440 api.safi = safi;
1441 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1442
1443 if (has_valid_label)
1444 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1445
1446 /* Note that this currently only applies to Null0 routes for
1447 * aggregates.
1448 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a
1449 * special
1450 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero
1451 * since we
1452 * do not want to also encode the :: nexthop for the aggregate
1453 * route.
1454 */
1455 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1456 api.nexthop_num = 0;
1457 else
1458 api.nexthop_num = valid_nh_count;
1459
1460 api.nexthop = (struct in6_addr **)STREAM_DATA(bgp_nexthop_buf);
1461 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
1462 api.ifindex_num = valid_nh_count;
1463 api.ifindex = (ifindex_t *)STREAM_DATA(bgp_ifindices_buf);
1464 if (has_valid_label) {
1465 api.label_num = valid_nh_count;
1466 api.label = (unsigned int *)STREAM_DATA(bgp_label_buf);
1467 } else {
1468 api.label_num = 0;
1469 api.label = NULL;
1470 }
1471 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1472 api.metric = metric;
1473 api.tag = 0;
1474
1475 if (tag) {
1476 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1477 api.tag = tag;
1478 }
1479
1480 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1481 if (distance) {
1482 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1483 api.distance = distance;
1484 }
1485
1486 if (p->family == AF_INET) {
1487 if (bgp_debug_zebra(p)) {
1488 int i;
1489 char label_buf[20];
1490 zlog_debug(
1491 "Tx IPv4 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI,
1492 valid_nh_count ? "add" : "delete",
1493 bgp->vrf_id,
1494 inet_ntop(AF_INET, &p->u.prefix4,
1495 buf[0], sizeof(buf[0])),
1496 p->prefixlen, api.metric, api.tag);
1497 for (i = 0; i < api.nexthop_num; i++) {
1498 label_buf[0] = '\0';
1499 if (has_valid_label)
1500 sprintf(label_buf, "label %u",
1501 api.label[i]);
1502 zlog_debug(
1503 " nhop [%d]: %s if %s %s",
1504 i + 1,
1505 inet_ntop(AF_INET6,
1506 api.nexthop[i],
1507 buf[1],
1508 sizeof(buf[1])),
1509 ifindex2ifname(api.ifindex[i],
1510 bgp->vrf_id),
1511 label_buf);
1512 }
1513 }
1514
1515 if (valid_nh_count)
1516 zapi_ipv4_route_ipv6_nexthop(
1517 ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
1518 zclient, (struct prefix_ipv4 *)p,
1519 (struct zapi_ipv6 *)&api);
1520 else
1521 zapi_ipv4_route(ZEBRA_IPV4_ROUTE_DELETE,
1522 zclient,
1523 (struct prefix_ipv4 *)p,
1524 (struct zapi_ipv4 *)&api);
1525 } else {
1526 if (bgp_debug_zebra(p)) {
1527 int i;
1528 char label_buf[20];
1529 zlog_debug(
1530 "Tx IPv6 route %s VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI,
1531 valid_nh_count ? "add" : "delete",
1532 bgp->vrf_id,
1533 inet_ntop(AF_INET6, &p->u.prefix6,
1534 buf[0], sizeof(buf[0])),
1535 p->prefixlen, api.metric, api.tag);
1536 for (i = 0; i < api.nexthop_num; i++) {
1537 label_buf[0] = '\0';
1538 if (has_valid_label)
1539 sprintf(label_buf, "label %u",
1540 api.label[i]);
1541 zlog_debug(
1542 " nhop [%d]: %s if %s %s",
1543 i + 1,
1544 inet_ntop(AF_INET6,
1545 api.nexthop[i],
1546 buf[1],
1547 sizeof(buf[1])),
1548 ifindex2ifname(api.ifindex[i],
1549 bgp->vrf_id),
1550 label_buf);
1551 }
1552 }
1553
1554 zapi_ipv6_route(
1555 valid_nh_count ? ZEBRA_IPV6_ROUTE_ADD
1556 : ZEBRA_IPV6_ROUTE_DELETE,
1557 zclient, (struct prefix_ipv6 *)p, NULL, &api);
1558 }
1559 }
1560 }
1561
1562 /* Announce all routes of a table to zebra */
1563 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1564 {
1565 struct bgp_node *rn;
1566 struct bgp_table *table;
1567 struct bgp_info *ri;
1568
1569 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1570 * know of this instance.
1571 */
1572 if (!bgp_install_info_to_zebra(bgp))
1573 return;
1574
1575 table = bgp->rib[afi][safi];
1576 if (!table)
1577 return;
1578
1579 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1580 for (ri = rn->info; ri; ri = ri->next)
1581 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
1582 && ri->type == ZEBRA_ROUTE_BGP
1583 && ri->sub_type == BGP_ROUTE_NORMAL)
1584 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1585 safi);
1586 }
1587
1588 void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info, safi_t safi)
1589 {
1590 u_int32_t flags;
1591 struct peer *peer;
1592
1593 peer = info->peer;
1594 assert(peer);
1595
1596 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1597 * know of this instance.
1598 */
1599 if (!bgp_install_info_to_zebra(peer->bgp))
1600 return;
1601
1602 if ((p->family == AF_INET
1603 && !vrf_bitmap_check(zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP],
1604 peer->bgp->vrf_id))
1605 || (p->family == AF_INET6
1606 && !vrf_bitmap_check(zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP],
1607 peer->bgp->vrf_id)))
1608 return;
1609
1610 flags = 0;
1611
1612 if (peer->sort == BGP_PEER_IBGP) {
1613 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
1614 SET_FLAG(flags, ZEBRA_FLAG_IBGP);
1615 }
1616
1617 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1618 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1619 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1620 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
1621
1622 if (p->family == AF_INET) {
1623 struct zapi_ipv4 api;
1624
1625 api.vrf_id = peer->bgp->vrf_id;
1626 api.flags = flags;
1627
1628 api.type = ZEBRA_ROUTE_BGP;
1629 api.instance = 0;
1630 api.message = 0;
1631 api.safi = safi;
1632 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1633 api.nexthop_num = 0;
1634 api.nexthop = NULL;
1635 api.label_num = 0;
1636 api.label = NULL;
1637 api.ifindex_num = 0;
1638 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1639 api.metric = info->attr->med;
1640 api.tag = 0;
1641
1642 if (info->attr->tag != 0) {
1643 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1644 api.tag = info->attr->tag;
1645 }
1646
1647 if (bgp_debug_zebra(p)) {
1648 char buf[2][INET_ADDRSTRLEN];
1649 zlog_debug(
1650 "Tx IPv4 route delete VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI,
1651 peer->bgp->vrf_id,
1652 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
1653 sizeof(buf[0])),
1654 p->prefixlen, api.metric, api.tag);
1655 }
1656
1657 zapi_ipv4_route(ZEBRA_IPV4_ROUTE_DELETE, zclient,
1658 (struct prefix_ipv4 *)p, &api);
1659 }
1660 /* We have to think about a IPv6 link-local address curse. */
1661 if (p->family == AF_INET6) {
1662 struct zapi_ipv6 api;
1663
1664 api.vrf_id = peer->bgp->vrf_id;
1665 api.flags = flags;
1666 api.type = ZEBRA_ROUTE_BGP;
1667 api.instance = 0;
1668 api.message = 0;
1669 api.safi = safi;
1670 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1671 api.nexthop_num = 0;
1672 api.nexthop = NULL;
1673 api.ifindex_num = 0;
1674 api.label_num = 0;
1675 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1676 api.metric = info->attr->med;
1677 api.tag = 0;
1678
1679 if (info->attr->tag != 0) {
1680 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1681 api.tag = info->attr->tag;
1682 }
1683
1684 if (bgp_debug_zebra(p)) {
1685 char buf[2][INET6_ADDRSTRLEN];
1686 zlog_debug(
1687 "Tx IPv6 route delete VRF %u %s/%d metric %u tag %" ROUTE_TAG_PRI,
1688 peer->bgp->vrf_id,
1689 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
1690 sizeof(buf[0])),
1691 p->prefixlen, api.metric, api.tag);
1692 }
1693
1694 zapi_ipv6_route(ZEBRA_IPV6_ROUTE_DELETE, zclient,
1695 (struct prefix_ipv6 *)p, NULL, &api);
1696 }
1697 }
1698
1699 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, u_char type,
1700 u_short instance)
1701 {
1702 struct list *red_list;
1703 struct listnode *node;
1704 struct bgp_redist *red;
1705
1706 red_list = bgp->redist[afi][type];
1707 if (!red_list)
1708 return (NULL);
1709
1710 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1711 if (red->instance == instance)
1712 return red;
1713
1714 return NULL;
1715 }
1716
1717 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, u_char type,
1718 u_short instance)
1719 {
1720 struct list *red_list;
1721 struct bgp_redist *red;
1722
1723 red = bgp_redist_lookup(bgp, afi, type, instance);
1724 if (red)
1725 return red;
1726
1727 if (!bgp->redist[afi][type])
1728 bgp->redist[afi][type] = list_new();
1729
1730 red_list = bgp->redist[afi][type];
1731 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1732 sizeof(struct bgp_redist));
1733 red->instance = instance;
1734
1735 listnode_add(red_list, red);
1736
1737 return red;
1738 }
1739
1740 static void bgp_redist_del(struct bgp *bgp, afi_t afi, u_char type,
1741 u_short instance)
1742 {
1743 struct bgp_redist *red;
1744
1745 red = bgp_redist_lookup(bgp, afi, type, instance);
1746
1747 if (red) {
1748 listnode_delete(bgp->redist[afi][type], red);
1749 XFREE(MTYPE_BGP_REDIST, red);
1750 if (!bgp->redist[afi][type]->count) {
1751 list_free(bgp->redist[afi][type]);
1752 bgp->redist[afi][type] = NULL;
1753 }
1754 }
1755 }
1756
1757 /* Other routes redistribution into BGP. */
1758 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type, u_short instance)
1759 {
1760
1761 /* Return if already redistribute flag is set. */
1762 if (instance) {
1763 if (redist_check_instance(&zclient->mi_redist[afi][type],
1764 instance))
1765 return CMD_WARNING;
1766
1767 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1768 } else {
1769 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1770 return CMD_WARNING;
1771
1772 #if ENABLE_BGP_VNC
1773 if (bgp->vrf_id == VRF_DEFAULT
1774 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1775 vnc_export_bgp_enable(
1776 bgp, afi); /* only enables if mode bits cfg'd */
1777 }
1778 #endif
1779
1780 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1781 }
1782
1783 /* Don't try to register if we're not connected to Zebra or Zebra
1784 * doesn't
1785 * know of this instance.
1786 */
1787 if (!bgp_install_info_to_zebra(bgp))
1788 return CMD_WARNING_CONFIG_FAILED;
1789
1790 if (BGP_DEBUG(zebra, ZEBRA))
1791 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1792 bgp->vrf_id, afi, zebra_route_string(type),
1793 instance);
1794
1795 /* Send distribute add message to zebra. */
1796 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1797 instance, bgp->vrf_id);
1798
1799 return CMD_SUCCESS;
1800 }
1801
1802 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1803 u_short instance)
1804 {
1805 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1806 * know of this instance.
1807 */
1808 if (!bgp_install_info_to_zebra(bgp))
1809 return -1;
1810
1811 if (BGP_DEBUG(zebra, ZEBRA))
1812 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1813 bgp->vrf_id, afi, zebra_route_string(type),
1814 instance);
1815
1816 /* Send distribute add message to zebra. */
1817 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1818 instance, bgp->vrf_id);
1819 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1820 instance, bgp->vrf_id);
1821
1822 return 0;
1823 }
1824
1825 /* Redistribute with route-map specification. */
1826 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
1827 {
1828 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1829 return 0;
1830
1831 if (red->rmap.name)
1832 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1833 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1834 red->rmap.map = route_map_lookup_by_name(name);
1835
1836 return 1;
1837 }
1838
1839 /* Redistribute with metric specification. */
1840 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1841 afi_t afi, int type, u_int32_t metric)
1842 {
1843 struct bgp_node *rn;
1844 struct bgp_info *ri;
1845
1846 if (red->redist_metric_flag && red->redist_metric == metric)
1847 return 0;
1848
1849 red->redist_metric_flag = 1;
1850 red->redist_metric = metric;
1851
1852 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1853 rn = bgp_route_next(rn)) {
1854 for (ri = rn->info; ri; ri = ri->next) {
1855 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1856 && ri->type == type
1857 && ri->instance == red->instance) {
1858 struct attr *old_attr;
1859 struct attr new_attr;
1860
1861 bgp_attr_dup(&new_attr, ri->attr);
1862 new_attr.med = red->redist_metric;
1863 old_attr = ri->attr;
1864 ri->attr = bgp_attr_intern(&new_attr);
1865 bgp_attr_unintern(&old_attr);
1866
1867 bgp_info_set_flag(rn, ri,
1868 BGP_INFO_ATTR_CHANGED);
1869 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1870 }
1871 }
1872 }
1873
1874 return 1;
1875 }
1876
1877 /* Unset redistribution. */
1878 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1879 u_short instance)
1880 {
1881 struct bgp_redist *red;
1882
1883 red = bgp_redist_lookup(bgp, afi, type, instance);
1884 if (!red)
1885 return CMD_SUCCESS;
1886
1887 /* Return if zebra connection is disabled. */
1888 if (instance) {
1889 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1890 instance))
1891 return CMD_WARNING;
1892 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1893 } else {
1894 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1895 return CMD_WARNING;
1896 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1897 }
1898
1899 #if ENABLE_BGP_VNC
1900 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1901 vnc_export_bgp_disable(bgp, afi);
1902 }
1903 #endif
1904
1905 if (bgp_install_info_to_zebra(bgp)) {
1906 /* Send distribute delete message to zebra. */
1907 if (BGP_DEBUG(zebra, ZEBRA))
1908 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1909 bgp->vrf_id, afi, zebra_route_string(type),
1910 instance);
1911 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1912 type, instance, bgp->vrf_id);
1913 }
1914
1915 /* Withdraw redistributed routes from current BGP's routing table. */
1916 bgp_redistribute_withdraw(bgp, afi, type, instance);
1917
1918 return CMD_SUCCESS;
1919 }
1920
1921 /* Unset redistribution. */
1922 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1923 u_short instance)
1924 {
1925 struct bgp_redist *red;
1926
1927 red = bgp_redist_lookup(bgp, afi, type, instance);
1928 if (!red)
1929 return CMD_SUCCESS;
1930
1931 bgp_redistribute_unreg(bgp, afi, type, instance);
1932
1933 /* Unset route-map. */
1934 if (red->rmap.name)
1935 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1936 red->rmap.name = NULL;
1937 red->rmap.map = NULL;
1938
1939 /* Unset metric. */
1940 red->redist_metric_flag = 0;
1941 red->redist_metric = 0;
1942
1943 bgp_redist_del(bgp, afi, type, instance);
1944
1945 return CMD_SUCCESS;
1946 }
1947
1948 /* Update redistribute vrf bitmap during triggers like
1949 restart networking or delete/add VRFs */
1950 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1951 {
1952 int i;
1953 afi_t afi;
1954
1955 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1956 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1957 if (vrf_bitmap_check(zclient->redist[afi][i],
1958 old_vrf_id)) {
1959 vrf_bitmap_unset(zclient->redist[afi][i],
1960 old_vrf_id);
1961 vrf_bitmap_set(zclient->redist[afi][i],
1962 bgp->vrf_id);
1963 }
1964 return;
1965 }
1966
1967 void bgp_zclient_reset(void)
1968 {
1969 zclient_reset(zclient);
1970 }
1971
1972 /* Register this instance with Zebra. Invoked upon connect (for
1973 * default instance) and when other VRFs are learnt (or created and
1974 * already learnt).
1975 */
1976 void bgp_zebra_instance_register(struct bgp *bgp)
1977 {
1978 /* Don't try to register if we're not connected to Zebra */
1979 if (!zclient || zclient->sock < 0)
1980 return;
1981
1982 if (BGP_DEBUG(zebra, ZEBRA))
1983 zlog_debug("Registering VRF %u", bgp->vrf_id);
1984
1985 /* Register for router-id, interfaces, redistributed routes. */
1986 zclient_send_reg_requests(zclient, bgp->vrf_id);
1987
1988 /* For default instance, register to learn about VNIs, if appropriate.
1989 */
1990 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
1991 && bgp->advertise_all_vni)
1992 bgp_zebra_advertise_all_vni(bgp, 1);
1993 }
1994
1995 /* Deregister this instance with Zebra. Invoked upon the instance
1996 * being deleted (default or VRF) and it is already registered.
1997 */
1998 void bgp_zebra_instance_deregister(struct bgp *bgp)
1999 {
2000 /* Don't try to deregister if we're not connected to Zebra */
2001 if (zclient->sock < 0)
2002 return;
2003
2004 if (BGP_DEBUG(zebra, ZEBRA))
2005 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
2006
2007 /* For default instance, unregister learning about VNIs, if appropriate.
2008 */
2009 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
2010 && bgp->advertise_all_vni)
2011 bgp_zebra_advertise_all_vni(bgp, 0);
2012
2013 /* Deregister for router-id, interfaces, redistributed routes. */
2014 zclient_send_dereg_requests(zclient, bgp->vrf_id);
2015 }
2016
2017 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
2018 {
2019 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
2020
2021 /* Don't try to initiate if we're not connected to Zebra */
2022 if (zclient->sock < 0)
2023 return;
2024
2025 if (BGP_DEBUG(zebra, ZEBRA))
2026 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
2027 peer->host);
2028
2029 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
2030 ra_interval);
2031 }
2032
2033 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
2034 {
2035 /* Don't try to terminate if we're not connected to Zebra */
2036 if (zclient->sock < 0)
2037 return;
2038
2039 if (BGP_DEBUG(zebra, ZEBRA))
2040 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
2041 peer->host);
2042
2043 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
2044 }
2045
2046 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
2047 {
2048 struct stream *s;
2049
2050 /* Check socket. */
2051 if (!zclient || zclient->sock < 0)
2052 return 0;
2053
2054 /* Don't try to register if Zebra doesn't know of this instance. */
2055 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2056 return 0;
2057
2058 s = zclient->obuf;
2059 stream_reset(s);
2060
2061 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
2062 stream_putc(s, advertise);
2063 stream_putw_at(s, 0, stream_get_endp(s));
2064
2065 return zclient_send_message(zclient);
2066 }
2067
2068 /* BGP has established connection with Zebra. */
2069 static void bgp_zebra_connected(struct zclient *zclient)
2070 {
2071 struct bgp *bgp;
2072
2073 zclient_num_connects++; /* increment even if not responding */
2074
2075 /* At this point, we may or may not have BGP instances configured, but
2076 * we're only interested in the default VRF (others wouldn't have learnt
2077 * the VRF from Zebra yet.)
2078 */
2079 bgp = bgp_get_default();
2080 if (!bgp)
2081 return;
2082
2083 bgp_zebra_instance_register(bgp);
2084
2085 /* Send the client registration */
2086 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2087
2088 /* TODO - What if we have peers and networks configured, do we have to
2089 * kick-start them?
2090 */
2091 }
2092
2093 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
2094 zebra_size_t length, vrf_id_t vrf_id)
2095 {
2096 struct stream *s;
2097 vni_t vni;
2098 struct bgp *bgp;
2099 struct in_addr vtep_ip;
2100
2101 s = zclient->ibuf;
2102 vni = stream_getl(s);
2103 if (command == ZEBRA_VNI_ADD)
2104 vtep_ip.s_addr = stream_get_ipv4(s);
2105 bgp = bgp_lookup_by_vrf_id(vrf_id);
2106 if (!bgp)
2107 return 0;
2108
2109 if (BGP_DEBUG(zebra, ZEBRA))
2110 zlog_debug("Rx VNI %s VRF %u VNI %u",
2111 (command == ZEBRA_VNI_ADD) ? "add" : "del", vrf_id,
2112 vni);
2113
2114 if (command == ZEBRA_VNI_ADD)
2115 return bgp_evpn_local_vni_add(
2116 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id);
2117 else
2118 return bgp_evpn_local_vni_del(bgp, vni);
2119 }
2120
2121 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
2122 zebra_size_t length, vrf_id_t vrf_id)
2123 {
2124 struct stream *s;
2125 vni_t vni;
2126 struct bgp *bgp;
2127 struct ethaddr mac;
2128 struct ipaddr ip;
2129 int ipa_len;
2130 char buf[ETHER_ADDR_STRLEN];
2131 char buf1[INET6_ADDRSTRLEN];
2132 u_char sticky;
2133
2134 memset(&ip, 0, sizeof(ip));
2135 s = zclient->ibuf;
2136 vni = stream_getl(s);
2137 stream_get(&mac.octet, s, ETHER_ADDR_LEN);
2138 ipa_len = stream_getl(s);
2139 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2140 && ipa_len != IPV6_MAX_BYTELEN) {
2141 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
2142 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2143 ipa_len);
2144 return -1;
2145 }
2146
2147 if (ipa_len) {
2148 ip.ipa_type =
2149 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
2150 stream_get(&ip.ip.addr, s, ipa_len);
2151 }
2152 sticky = stream_getc(s);
2153
2154 bgp = bgp_lookup_by_vrf_id(vrf_id);
2155 if (!bgp)
2156 return 0;
2157
2158 if (BGP_DEBUG(zebra, ZEBRA))
2159 zlog_debug("%u:Recv MACIP %s %sMAC %s IP %s VNI %u", vrf_id,
2160 (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2161 sticky ? "sticky " : "",
2162 prefix_mac2str(&mac, buf, sizeof(buf)),
2163 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
2164
2165 if (command == ZEBRA_MACIP_ADD)
2166 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, sticky);
2167 else
2168 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
2169 }
2170
2171 void bgp_zebra_init(struct thread_master *master)
2172 {
2173 zclient_num_connects = 0;
2174
2175 /* Set default values. */
2176 zclient = zclient_new(master);
2177 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0);
2178 zclient->zebra_connected = bgp_zebra_connected;
2179 zclient->router_id_update = bgp_router_id_update;
2180 zclient->interface_add = bgp_interface_add;
2181 zclient->interface_delete = bgp_interface_delete;
2182 zclient->interface_address_add = bgp_interface_address_add;
2183 zclient->interface_address_delete = bgp_interface_address_delete;
2184 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2185 zclient->interface_nbr_address_delete =
2186 bgp_interface_nbr_address_delete;
2187 zclient->interface_vrf_update = bgp_interface_vrf_update;
2188 zclient->redistribute_route_ipv4_add = zebra_read_ipv4;
2189 zclient->redistribute_route_ipv4_del = zebra_read_ipv4;
2190 zclient->interface_up = bgp_interface_up;
2191 zclient->interface_down = bgp_interface_down;
2192 zclient->redistribute_route_ipv6_add = zebra_read_ipv6;
2193 zclient->redistribute_route_ipv6_del = zebra_read_ipv6;
2194 zclient->nexthop_update = bgp_read_nexthop_update;
2195 zclient->import_check_update = bgp_read_import_check_update;
2196 zclient->fec_update = bgp_read_fec_update;
2197 zclient->local_vni_add = bgp_zebra_process_local_vni;
2198 zclient->local_vni_del = bgp_zebra_process_local_vni;
2199 zclient->local_macip_add = bgp_zebra_process_local_macip;
2200 zclient->local_macip_del = bgp_zebra_process_local_macip;
2201
2202 bgp_nexthop_buf = stream_new(multipath_num * sizeof(struct in6_addr));
2203 bgp_ifindices_buf = stream_new(multipath_num * sizeof(unsigned int));
2204 bgp_label_buf = stream_new(multipath_num * sizeof(unsigned int));
2205 }
2206
2207 void bgp_zebra_destroy(void)
2208 {
2209
2210 if (bgp_nexthop_buf)
2211 stream_free(bgp_nexthop_buf);
2212 if (bgp_ifindices_buf)
2213 stream_free(bgp_ifindices_buf);
2214 if (bgp_label_buf)
2215 stream_free(bgp_label_buf);
2216
2217 if (zclient == NULL)
2218 return;
2219 zclient_stop(zclient);
2220 zclient_free(zclient);
2221 zclient = NULL;
2222 }
2223
2224 int bgp_zebra_num_connects(void)
2225 {
2226 return zclient_num_connects;
2227 }