]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #3370 from pguibert6WIND/default_vrf_initialization
[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_errors.h"
48 #include "bgpd/bgp_mpath.h"
49 #include "bgpd/bgp_nexthop.h"
50 #include "bgpd/bgp_nht.h"
51 #include "bgpd/bgp_bfd.h"
52 #include "bgpd/bgp_label.h"
53 #if ENABLE_BGP_VNC
54 #include "bgpd/rfapi/rfapi_backend.h"
55 #include "bgpd/rfapi/vnc_export_bgp.h"
56 #endif
57 #include "bgpd/bgp_evpn.h"
58 #include "bgpd/bgp_mplsvpn.h"
59 #include "bgpd/bgp_labelpool.h"
60 #include "bgpd/bgp_pbr.h"
61 #include "bgpd/bgp_evpn_private.h"
62
63 /* All information about zebra. */
64 struct zclient *zclient = NULL;
65
66 /* Can we install into zebra? */
67 static inline int bgp_install_info_to_zebra(struct bgp *bgp)
68 {
69 if (zclient->sock <= 0)
70 return 0;
71
72 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
73 zlog_debug("%s: No zebra instance to talk to, not installing information",
74 __PRETTY_FUNCTION__);
75 return 0;
76 }
77
78 return 1;
79 }
80
81 int zclient_num_connects;
82
83 /* Router-id update message from zebra. */
84 static int bgp_router_id_update(int command, struct zclient *zclient,
85 zebra_size_t length, vrf_id_t vrf_id)
86 {
87 struct prefix router_id;
88
89 zebra_router_id_update_read(zclient->ibuf, &router_id);
90
91 if (BGP_DEBUG(zebra, ZEBRA)) {
92 char buf[PREFIX2STR_BUFFER];
93 prefix2str(&router_id, buf, sizeof(buf));
94 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
95 }
96
97 bgp_router_id_zebra_bump(vrf_id, &router_id);
98 return 0;
99 }
100
101 /* Nexthop update message from zebra. */
102 static int bgp_read_nexthop_update(int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
104 {
105 bgp_parse_nexthop_update(command, vrf_id);
106 return 0;
107 }
108
109 static int bgp_read_import_check_update(int command, struct zclient *zclient,
110 zebra_size_t length, vrf_id_t vrf_id)
111 {
112 bgp_parse_nexthop_update(command, vrf_id);
113 return 0;
114 }
115
116 /* Set or clear interface on which unnumbered neighbor is configured. This
117 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
118 * interface.
119 */
120 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
121 struct interface *upd_ifp)
122 {
123 struct listnode *node, *nnode;
124 struct peer *peer;
125
126 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
127 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
128 if (upd_ifp) {
129 peer->ifp = upd_ifp;
130 bgp_zebra_initiate_radv(bgp, peer);
131 } else {
132 bgp_zebra_terminate_radv(bgp, peer);
133 peer->ifp = upd_ifp;
134 }
135 }
136 }
137 }
138
139 static int bgp_read_fec_update(int command, struct zclient *zclient,
140 zebra_size_t length)
141 {
142 bgp_parse_fec_update();
143 return 0;
144 }
145
146 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
147 {
148 struct listnode *node, *nnode;
149 struct peer *peer;
150
151 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
152 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
153 && peer->status != Established) {
154 if (peer_active(peer))
155 BGP_EVENT_ADD(peer, BGP_Stop);
156 BGP_EVENT_ADD(peer, BGP_Start);
157 }
158 }
159 }
160
161 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
162 {
163 struct listnode *node;
164 struct connected *connected;
165 struct interface *ifp;
166 struct prefix *p;
167
168 /* Kick-off the FSM for any relevant peers only if there is a
169 * valid local address on the interface.
170 */
171 ifp = ifc->ifp;
172 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
173 p = connected->address;
174 if (p->family == AF_INET6
175 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
176 break;
177 }
178 if (!connected)
179 return;
180
181 bgp_start_interface_nbrs(bgp, ifp);
182 }
183
184 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
185 int del)
186 {
187 struct listnode *node, *nnode;
188 struct peer *peer;
189 struct interface *ifp;
190
191 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
192 if (peer->conf_if
193 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
194 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
195 BGP_EVENT_ADD(peer, BGP_Stop);
196 }
197 }
198 /* Free neighbor also, if we're asked to. */
199 if (del) {
200 ifp = ifc->ifp;
201 listnode_delete(ifp->nbr_connected, ifc);
202 nbr_connected_free(ifc);
203 }
204 }
205
206 /* Inteface addition message from zebra. */
207 static int bgp_interface_add(int command, struct zclient *zclient,
208 zebra_size_t length, vrf_id_t vrf_id)
209 {
210 struct interface *ifp;
211 struct bgp *bgp;
212
213 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
214 if (!ifp) // unexpected
215 return 0;
216
217 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
218 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
219
220 bgp = bgp_lookup_by_vrf_id(vrf_id);
221 if (!bgp)
222 return 0;
223
224 bgp_update_interface_nbrs(bgp, ifp, ifp);
225 return 0;
226 }
227
228 static int bgp_interface_delete(int command, struct zclient *zclient,
229 zebra_size_t length, vrf_id_t vrf_id)
230 {
231 struct stream *s;
232 struct interface *ifp;
233 struct bgp *bgp;
234
235 bgp = bgp_lookup_by_vrf_id(vrf_id);
236
237 s = zclient->ibuf;
238 ifp = zebra_interface_state_read(s, vrf_id);
239 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
240 return 0;
241
242 if (BGP_DEBUG(zebra, ZEBRA))
243 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
244
245 if (bgp)
246 bgp_update_interface_nbrs(bgp, ifp, NULL);
247
248 if_set_index(ifp, IFINDEX_INTERNAL);
249 return 0;
250 }
251
252 static int bgp_interface_up(int command, struct zclient *zclient,
253 zebra_size_t length, vrf_id_t vrf_id)
254 {
255 struct stream *s;
256 struct interface *ifp;
257 struct connected *c;
258 struct nbr_connected *nc;
259 struct listnode *node, *nnode;
260 struct bgp *bgp;
261
262 bgp = bgp_lookup_by_vrf_id(vrf_id);
263
264 s = zclient->ibuf;
265 ifp = zebra_interface_state_read(s, vrf_id);
266
267 if (!ifp)
268 return 0;
269
270 if (BGP_DEBUG(zebra, ZEBRA))
271 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
272
273 if (!bgp)
274 return 0;
275
276 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
277 bgp_connected_add(bgp, c);
278
279 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
280 bgp_nbr_connected_add(bgp, nc);
281
282 return 0;
283 }
284
285 static int bgp_interface_down(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 struct peer *peer;
295
296 bgp = bgp_lookup_by_vrf_id(vrf_id);
297
298 s = zclient->ibuf;
299 ifp = zebra_interface_state_read(s, vrf_id);
300 if (!ifp)
301 return 0;
302
303 if (BGP_DEBUG(zebra, ZEBRA))
304 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
305
306 if (!bgp)
307 return 0;
308
309 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
310 bgp_connected_delete(bgp, c);
311
312 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
313 bgp_nbr_connected_delete(bgp, nc, 1);
314
315 /* Fast external-failover */
316 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
317
318 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
319 #if defined(HAVE_CUMULUS)
320 /* Take down directly connected EBGP peers as well as
321 * 1-hop BFD
322 * tracked (directly connected) IBGP peers.
323 */
324 if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
325 && (!peer->bfd_info
326 || bgp_bfd_is_peer_multihop(peer)))
327 #else
328 /* Take down directly connected EBGP peers */
329 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
330 #endif
331 continue;
332
333 if (ifp == peer->nexthop.ifp) {
334 BGP_EVENT_ADD(peer, BGP_Stop);
335 peer->last_reset = PEER_DOWN_IF_DOWN;
336 }
337 }
338 }
339
340 return 0;
341 }
342
343 static int bgp_interface_address_add(int command, struct zclient *zclient,
344 zebra_size_t length, vrf_id_t vrf_id)
345 {
346 struct connected *ifc;
347 struct bgp *bgp;
348
349 bgp = bgp_lookup_by_vrf_id(vrf_id);
350
351 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
352
353 if (ifc == NULL)
354 return 0;
355
356 if (bgp_debug_zebra(ifc->address)) {
357 char buf[PREFIX2STR_BUFFER];
358 prefix2str(ifc->address, buf, sizeof(buf));
359 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
360 ifc->ifp->name, buf);
361 }
362
363 if (!bgp)
364 return 0;
365
366 if (if_is_operative(ifc->ifp)) {
367 bgp_connected_add(bgp, ifc);
368
369 /* If we have learnt of any neighbors on this interface,
370 * check to kick off any BGP interface-based neighbors,
371 * but only if this is a link-local address.
372 */
373 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
374 && !list_isempty(ifc->ifp->nbr_connected))
375 bgp_start_interface_nbrs(bgp, ifc->ifp);
376 }
377
378 return 0;
379 }
380
381 static int bgp_interface_address_delete(int command, struct zclient *zclient,
382 zebra_size_t length, vrf_id_t vrf_id)
383 {
384 struct connected *ifc;
385 struct bgp *bgp;
386
387 bgp = bgp_lookup_by_vrf_id(vrf_id);
388
389 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
390
391 if (ifc == NULL)
392 return 0;
393
394 if (bgp_debug_zebra(ifc->address)) {
395 char buf[PREFIX2STR_BUFFER];
396 prefix2str(ifc->address, buf, sizeof(buf));
397 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
398 ifc->ifp->name, buf);
399 }
400
401 if (bgp && if_is_operative(ifc->ifp)) {
402 bgp_connected_delete(bgp, ifc);
403 }
404
405 connected_free(ifc);
406
407 return 0;
408 }
409
410 static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
411 zebra_size_t length, vrf_id_t vrf_id)
412 {
413 struct nbr_connected *ifc = NULL;
414 struct bgp *bgp;
415
416 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
417
418 if (ifc == NULL)
419 return 0;
420
421 if (bgp_debug_zebra(ifc->address)) {
422 char buf[PREFIX2STR_BUFFER];
423 prefix2str(ifc->address, buf, sizeof(buf));
424 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
425 ifc->ifp->name, buf);
426 }
427
428 if (if_is_operative(ifc->ifp)) {
429 bgp = bgp_lookup_by_vrf_id(vrf_id);
430 if (bgp)
431 bgp_nbr_connected_add(bgp, ifc);
432 }
433
434 return 0;
435 }
436
437 static int bgp_interface_nbr_address_delete(int command,
438 struct zclient *zclient,
439 zebra_size_t length,
440 vrf_id_t vrf_id)
441 {
442 struct nbr_connected *ifc = NULL;
443 struct bgp *bgp;
444
445 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
446
447 if (ifc == NULL)
448 return 0;
449
450 if (bgp_debug_zebra(ifc->address)) {
451 char buf[PREFIX2STR_BUFFER];
452 prefix2str(ifc->address, buf, sizeof(buf));
453 zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s", vrf_id,
454 ifc->ifp->name, buf);
455 }
456
457 if (if_is_operative(ifc->ifp)) {
458 bgp = bgp_lookup_by_vrf_id(vrf_id);
459 if (bgp)
460 bgp_nbr_connected_delete(bgp, ifc, 0);
461 }
462
463 nbr_connected_free(ifc);
464
465 return 0;
466 }
467
468 /* VRF update for an interface. */
469 static int bgp_interface_vrf_update(int command, struct zclient *zclient,
470 zebra_size_t length, vrf_id_t vrf_id)
471 {
472 struct interface *ifp;
473 vrf_id_t new_vrf_id;
474 struct connected *c;
475 struct nbr_connected *nc;
476 struct listnode *node, *nnode;
477 struct bgp *bgp;
478 struct peer *peer;
479
480 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
481 &new_vrf_id);
482 if (!ifp)
483 return 0;
484
485 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
486 zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id,
487 ifp->name, new_vrf_id);
488
489 bgp = bgp_lookup_by_vrf_id(vrf_id);
490
491 if (bgp) {
492 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
493 bgp_connected_delete(bgp, c);
494
495 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
496 bgp_nbr_connected_delete(bgp, nc, 1);
497
498 /* Fast external-failover */
499 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) {
500 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
501 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
502 continue;
503
504 if (ifp == peer->nexthop.ifp)
505 BGP_EVENT_ADD(peer, BGP_Stop);
506 }
507 }
508 }
509
510 if_update_to_new_vrf(ifp, new_vrf_id);
511
512 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
513 if (!bgp)
514 return 0;
515
516 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
517 bgp_connected_add(bgp, c);
518
519 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
520 bgp_nbr_connected_add(bgp, nc);
521 return 0;
522 }
523
524 /* Zebra route add and delete treatment. */
525 static int zebra_read_route(int command, struct zclient *zclient,
526 zebra_size_t length, vrf_id_t vrf_id)
527 {
528 enum nexthop_types_t nhtype;
529 struct zapi_route api;
530 union g_addr nexthop;
531 ifindex_t ifindex;
532 int add, i;
533 struct bgp *bgp;
534
535 bgp = bgp_lookup_by_vrf_id(vrf_id);
536 if (!bgp)
537 return 0;
538
539 if (zapi_route_decode(zclient->ibuf, &api) < 0)
540 return -1;
541
542 /* we completely ignore srcdest routes for now. */
543 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
544 return 0;
545
546 /* ignore link-local address. */
547 if (api.prefix.family == AF_INET6
548 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
549 return 0;
550
551 nexthop = api.nexthops[0].gate;
552 ifindex = api.nexthops[0].ifindex;
553 nhtype = api.nexthops[0].type;
554
555 add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
556 if (add) {
557 /*
558 * The ADD message is actually an UPDATE and there is no
559 * explicit DEL
560 * for a prior redistributed route, if any. So, perform an
561 * implicit
562 * DEL processing for the same redistributed route from any
563 * other
564 * source type.
565 */
566 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
567 if (i != api.type)
568 bgp_redistribute_delete(bgp, &api.prefix, i,
569 api.instance);
570 }
571
572 /* Now perform the add/update. */
573 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
574 nhtype, api.metric, api.type, api.instance,
575 api.tag);
576 } else {
577 bgp_redistribute_delete(bgp, &api.prefix, api.type,
578 api.instance);
579 }
580
581 if (bgp_debug_zebra(&api.prefix)) {
582 char buf[2][PREFIX_STRLEN];
583
584 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
585 if (add) {
586 inet_ntop(api.prefix.family, &nexthop, buf[1],
587 sizeof(buf[1]));
588 zlog_debug(
589 "Rx route ADD VRF %u %s[%d] %s nexthop %s (type %d if %u) metric %u tag %" ROUTE_TAG_PRI,
590 vrf_id, zebra_route_string(api.type),
591 api.instance, buf[0], buf[1], nhtype,
592 ifindex, api.metric, api.tag);
593 } else {
594 zlog_debug(
595 "Rx route DEL VRF %u %s[%d] %s",
596 vrf_id, zebra_route_string(api.type),
597 api.instance, buf[0]);
598 }
599 }
600
601 return 0;
602 }
603
604 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
605 {
606 struct vrf *vrf;
607 struct listnode *cnode;
608 struct interface *ifp;
609 struct connected *connected;
610 struct prefix_ipv4 p;
611 struct prefix *cp;
612
613 vrf = vrf_lookup_by_id(vrf_id);
614 if (!vrf)
615 return NULL;
616
617 p.family = AF_INET;
618 p.prefix = *addr;
619 p.prefixlen = IPV4_MAX_BITLEN;
620
621 FOR_ALL_INTERFACES (vrf, ifp) {
622 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
623 cp = connected->address;
624
625 if (cp->family == AF_INET)
626 if (prefix_match(cp, (struct prefix *)&p))
627 return ifp;
628 }
629 }
630 return NULL;
631 }
632
633 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
634 {
635 struct vrf *vrf;
636 struct listnode *cnode;
637 struct interface *ifp;
638 struct connected *connected;
639 struct prefix *cp;
640
641 vrf = vrf_lookup_by_id(vrf_id);
642 if (!vrf)
643 return NULL;
644
645 FOR_ALL_INTERFACES (vrf, ifp) {
646 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
647 cp = connected->address;
648
649 if (cp->family == AF_INET)
650 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
651 return ifp;
652 }
653 }
654 return NULL;
655 }
656
657 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
658 vrf_id_t vrf_id)
659 {
660 struct vrf *vrf;
661 struct listnode *cnode;
662 struct interface *ifp;
663 struct connected *connected;
664 struct prefix_ipv6 p;
665 struct prefix *cp;
666
667 vrf = vrf_lookup_by_id(vrf_id);
668 if (!vrf)
669 return NULL;
670
671 p.family = AF_INET6;
672 p.prefix = *addr;
673 p.prefixlen = IPV6_MAX_BITLEN;
674
675 FOR_ALL_INTERFACES (vrf, ifp) {
676 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
677 cp = connected->address;
678
679 if (cp->family == AF_INET6)
680 if (prefix_match(cp, (struct prefix *)&p)) {
681 if (IN6_IS_ADDR_LINKLOCAL(
682 &cp->u.prefix6)) {
683 if (ifindex == ifp->ifindex)
684 return ifp;
685 } else
686 return ifp;
687 }
688 }
689 }
690 return NULL;
691 }
692
693 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
694 ifindex_t ifindex, vrf_id_t vrf_id)
695 {
696 struct vrf *vrf;
697 struct listnode *cnode;
698 struct interface *ifp;
699 struct connected *connected;
700 struct prefix *cp;
701
702 vrf = vrf_lookup_by_id(vrf_id);
703 if (!vrf)
704 return NULL;
705
706 FOR_ALL_INTERFACES (vrf, ifp) {
707 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
708 cp = connected->address;
709
710 if (cp->family == AF_INET6)
711 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
712 if (IN6_IS_ADDR_LINKLOCAL(
713 &cp->u.prefix6)) {
714 if (ifindex == ifp->ifindex)
715 return ifp;
716 } else
717 return ifp;
718 }
719 }
720 }
721 return NULL;
722 }
723
724 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
725 {
726 struct listnode *cnode;
727 struct connected *connected;
728 struct prefix *cp;
729
730 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
731 cp = connected->address;
732
733 if (cp->family == AF_INET6)
734 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
735 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
736 return 1;
737 }
738 }
739 return 0;
740 }
741
742 static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
743 {
744 struct listnode *cnode;
745 struct connected *connected;
746 struct prefix *cp;
747
748 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
749 cp = connected->address;
750
751 if (cp->family == AF_INET6)
752 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
753 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
754 return 1;
755 }
756 }
757 return 0;
758 }
759
760 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
761 {
762 struct listnode *cnode;
763 struct connected *connected;
764 struct prefix *cp;
765
766 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
767 cp = connected->address;
768 if ((cp->family == AF_INET)
769 && !ipv4_martian(&(cp->u.prefix4))) {
770 *addr = cp->u.prefix4;
771 return 1;
772 }
773 }
774 return 0;
775 }
776
777
778 bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
779 struct bgp_nexthop *nexthop, struct peer *peer)
780 {
781 int ret = 0;
782 struct interface *ifp = NULL;
783
784 memset(nexthop, 0, sizeof(struct bgp_nexthop));
785
786 if (!local)
787 return false;
788 if (!remote)
789 return false;
790
791 if (local->sa.sa_family == AF_INET) {
792 nexthop->v4 = local->sin.sin_addr;
793 if (peer->update_if)
794 ifp = if_lookup_by_name(peer->update_if,
795 peer->bgp->vrf_id);
796 else
797 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
798 peer->bgp->vrf_id);
799 }
800 if (local->sa.sa_family == AF_INET6) {
801 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
802 if (peer->conf_if || peer->ifname)
803 ifp = if_lookup_by_name(peer->conf_if
804 ? peer->conf_if
805 : peer->ifname,
806 peer->bgp->vrf_id);
807 } else if (peer->update_if)
808 ifp = if_lookup_by_name(peer->update_if,
809 peer->bgp->vrf_id);
810 else
811 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
812 local->sin6.sin6_scope_id,
813 peer->bgp->vrf_id);
814 }
815
816 if (!ifp) {
817 /*
818 * BGP views do not currently get proper data
819 * from zebra( when attached ) to be able to
820 * properly resolve nexthops, so give this
821 * instance type a pass.
822 */
823 if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW)
824 return true;
825 /*
826 * If we have no interface data but we have established
827 * some connection w/ zebra than something has gone
828 * terribly terribly wrong here, so say this failed
829 * If we do not any zebra connection then not
830 * having a ifp pointer is ok.
831 */
832 return zclient_num_connects ? false : true;
833 }
834
835 nexthop->ifp = ifp;
836
837 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
838 if (local->sa.sa_family == AF_INET) {
839 /* IPv6 nexthop*/
840 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
841
842 if (!ret) {
843 /* There is no global nexthop. Use link-local address as
844 * both the
845 * global and link-local nexthop. In this scenario, the
846 * expectation
847 * for interop is that the network admin would use a
848 * route-map to
849 * specify the global IPv6 nexthop.
850 */
851 if_get_ipv6_local(ifp, &nexthop->v6_global);
852 memcpy(&nexthop->v6_local, &nexthop->v6_global,
853 IPV6_MAX_BYTELEN);
854 } else
855 if_get_ipv6_local(ifp, &nexthop->v6_local);
856
857 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
858 peer->shared_network = 1;
859 else
860 peer->shared_network = 0;
861 }
862
863 /* IPv6 connection, fetch and store IPv4 local address if any. */
864 if (local->sa.sa_family == AF_INET6) {
865 struct interface *direct = NULL;
866
867 /* IPv4 nexthop. */
868 ret = if_get_ipv4_address(ifp, &nexthop->v4);
869 if (!ret && peer->local_id.s_addr)
870 nexthop->v4 = peer->local_id;
871
872 /* Global address*/
873 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
874 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
875 IPV6_MAX_BYTELEN);
876
877 /* If directory connected set link-local address. */
878 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
879 remote->sin6.sin6_scope_id,
880 peer->bgp->vrf_id);
881 if (direct)
882 if_get_ipv6_local(ifp, &nexthop->v6_local);
883 } else
884 /* Link-local address. */
885 {
886 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
887
888 /* If there is no global address. Set link-local
889 address as
890 global. I know this break RFC specification... */
891 /* In this scenario, the expectation for interop is that
892 * the
893 * network admin would use a route-map to specify the
894 * global
895 * IPv6 nexthop.
896 */
897 if (!ret)
898 memcpy(&nexthop->v6_global,
899 &local->sin6.sin6_addr,
900 IPV6_MAX_BYTELEN);
901 /* Always set the link-local address */
902 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
903 IPV6_MAX_BYTELEN);
904 }
905
906 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
907 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
908 remote->sin6.sin6_scope_id,
909 peer->bgp->vrf_id))
910 peer->shared_network = 1;
911 else
912 peer->shared_network = 0;
913 }
914
915 /* KAME stack specific treatment. */
916 #ifdef KAME
917 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
918 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
919 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
920 }
921 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
922 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
923 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
924 }
925 #endif /* KAME */
926
927 /* If we have identified the local interface, there is no error for now.
928 */
929 return true;
930 }
931
932 static struct in6_addr *
933 bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex)
934 {
935 struct in6_addr *nexthop = NULL;
936
937 /* Only global address nexthop exists. */
938 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
939 nexthop = &path->attr->mp_nexthop_global;
940 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
941 *ifindex = path->attr->nh_ifindex;
942 }
943
944 /* If both global and link-local address present. */
945 if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
946 /* Check if route-map is set to prefer global over link-local */
947 if (path->attr->mp_nexthop_prefer_global) {
948 nexthop = &path->attr->mp_nexthop_global;
949 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
950 *ifindex = path->attr->nh_ifindex;
951 } else {
952 /* Workaround for Cisco's nexthop bug. */
953 if (IN6_IS_ADDR_UNSPECIFIED(
954 &path->attr->mp_nexthop_global)
955 && path->peer->su_remote->sa.sa_family
956 == AF_INET6) {
957 nexthop =
958 &path->peer->su_remote->sin6.sin6_addr;
959 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
960 *ifindex = path->peer->nexthop.ifp
961 ->ifindex;
962 } else {
963 nexthop = &path->attr->mp_nexthop_local;
964 if (IN6_IS_ADDR_LINKLOCAL(nexthop))
965 *ifindex = path->attr->nh_lla_ifindex;
966 }
967 }
968 }
969
970 return nexthop;
971 }
972
973 static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
974 struct bgp_path_info *path)
975 {
976 route_map_result_t ret;
977
978 ret = route_map_apply(map, p, RMAP_BGP, path);
979 bgp_attr_flush(path->attr);
980
981 if (ret != RMAP_DENYMATCH)
982 return 1;
983
984 if (bgp_debug_zebra(p)) {
985 if (p->family == AF_INET) {
986 char buf[2][INET_ADDRSTRLEN];
987 zlog_debug(
988 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
989 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
990 sizeof(buf[0])),
991 p->prefixlen,
992 inet_ntop(AF_INET, &path->attr->nexthop, buf[1],
993 sizeof(buf[1])));
994 }
995 if (p->family == AF_INET6) {
996 char buf[2][INET6_ADDRSTRLEN];
997 ifindex_t ifindex;
998 struct in6_addr *nexthop;
999
1000 nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex);
1001 zlog_debug(
1002 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1003 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
1004 sizeof(buf[0])),
1005 p->prefixlen,
1006 inet_ntop(AF_INET6, nexthop,
1007 buf[1], sizeof(buf[1])));
1008 }
1009 }
1010 return 0;
1011 }
1012
1013 static struct thread *bgp_tm_thread_connect;
1014 static bool bgp_tm_status_connected;
1015 static bool bgp_tm_chunk_obtained;
1016 #define BGP_FLOWSPEC_TABLE_CHUNK 100000
1017 static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size;
1018 struct bgp *bgp_tm_bgp;
1019
1020 static int bgp_zebra_tm_connect(struct thread *t)
1021 {
1022 struct zclient *zclient;
1023 int delay = 10, ret = 0;
1024
1025 zclient = THREAD_ARG(t);
1026 if (bgp_tm_status_connected && zclient->sock > 0)
1027 delay = 60;
1028 else {
1029 bgp_tm_status_connected = false;
1030 ret = tm_table_manager_connect(zclient);
1031 }
1032 if (ret < 0) {
1033 zlog_info("Error connecting to table manager!");
1034 bgp_tm_status_connected = false;
1035 } else {
1036 if (!bgp_tm_status_connected)
1037 zlog_debug("Connecting to table manager. Success");
1038 bgp_tm_status_connected = true;
1039 if (!bgp_tm_chunk_obtained) {
1040 if (bgp_zebra_get_table_range(bgp_tm_chunk_size,
1041 &bgp_tm_min,
1042 &bgp_tm_max) >= 0) {
1043 bgp_tm_chunk_obtained = true;
1044 /* parse non installed entries */
1045 bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC);
1046 }
1047 }
1048 }
1049 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1050 &bgp_tm_thread_connect);
1051 return 0;
1052 }
1053
1054 bool bgp_zebra_tm_chunk_obtained(void)
1055 {
1056 return bgp_tm_chunk_obtained;
1057 }
1058
1059 uint32_t bgp_zebra_tm_get_id(void)
1060 {
1061 static int table_id;
1062
1063 if (!bgp_tm_chunk_obtained)
1064 return ++table_id;
1065 return bgp_tm_min++;
1066 }
1067
1068 void bgp_zebra_init_tm_connect(struct bgp *bgp)
1069 {
1070 int delay = 1;
1071
1072 /* if already set, do nothing
1073 */
1074 if (bgp_tm_thread_connect != NULL)
1075 return;
1076 bgp_tm_status_connected = false;
1077 bgp_tm_chunk_obtained = false;
1078 bgp_tm_min = bgp_tm_max = 0;
1079 bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK;
1080 bgp_tm_bgp = bgp;
1081 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1082 &bgp_tm_thread_connect);
1083 }
1084
1085 int bgp_zebra_get_table_range(uint32_t chunk_size,
1086 uint32_t *start, uint32_t *end)
1087 {
1088 int ret;
1089
1090 if (!bgp_tm_status_connected)
1091 return -1;
1092 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1093 if (ret < 0) {
1094 flog_err(EC_BGP_TABLE_CHUNK,
1095 "BGP: Error getting table chunk %u", chunk_size);
1096 return -1;
1097 }
1098 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1099 chunk_size, *start, *end);
1100 return 0;
1101 }
1102
1103 static int update_ipv4nh_for_route_install(int nh_othervrf,
1104 struct in_addr *nexthop,
1105 struct attr *attr,
1106 bool is_evpn,
1107 struct zapi_nexthop *api_nh)
1108 {
1109 api_nh->gate.ipv4 = *nexthop;
1110
1111 /* Need to set fields appropriately for EVPN routes imported into
1112 * a VRF (which are programmed as onlink on l3-vni SVI) as well as
1113 * connected routes leaked into a VRF.
1114 */
1115 if (is_evpn)
1116 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1117 else if (nh_othervrf &&
1118 api_nh->gate.ipv4.s_addr == INADDR_ANY) {
1119 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1120 api_nh->ifindex = attr->nh_ifindex;
1121 } else
1122 api_nh->type = NEXTHOP_TYPE_IPV4;
1123
1124 return 1;
1125 }
1126
1127 static int
1128 update_ipv6nh_for_route_install(int nh_othervrf, struct in6_addr *nexthop,
1129 ifindex_t ifindex, struct bgp_path_info *pi,
1130 struct bgp_path_info *best_pi, bool is_evpn,
1131 struct zapi_nexthop *api_nh)
1132 {
1133 struct attr *attr;
1134
1135 attr = pi->attr;
1136
1137 if (is_evpn)
1138 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1139 else if (nh_othervrf) {
1140 if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) {
1141 api_nh->type = NEXTHOP_TYPE_IFINDEX;
1142 api_nh->ifindex = attr->nh_ifindex;
1143 } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1144 if (ifindex == 0)
1145 return 0;
1146 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1147 api_nh->ifindex = ifindex;
1148 } else {
1149 api_nh->type = NEXTHOP_TYPE_IPV6;
1150 api_nh->ifindex = 0;
1151 }
1152 } else {
1153 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1154 if (pi == best_pi
1155 && attr->mp_nexthop_len
1156 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1157 if (pi->peer->nexthop.ifp)
1158 ifindex =
1159 pi->peer->nexthop.ifp->ifindex;
1160 if (!ifindex) {
1161 if (pi->peer->conf_if)
1162 ifindex = pi->peer->ifp->ifindex;
1163 else if (pi->peer->ifname)
1164 ifindex = ifname2ifindex(
1165 pi->peer->ifname,
1166 pi->peer->bgp->vrf_id);
1167 else if (pi->peer->nexthop.ifp)
1168 ifindex =
1169 pi->peer->nexthop.ifp->ifindex;
1170 }
1171
1172 if (ifindex == 0)
1173 return 0;
1174 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
1175 api_nh->ifindex = ifindex;
1176 } else {
1177 api_nh->type = NEXTHOP_TYPE_IPV6;
1178 api_nh->ifindex = 0;
1179 }
1180 }
1181 api_nh->gate.ipv6 = *nexthop;
1182
1183 return 1;
1184 }
1185
1186 void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
1187 struct bgp_path_info *info, struct bgp *bgp, afi_t afi,
1188 safi_t safi)
1189 {
1190 struct zapi_route api;
1191 struct zapi_nexthop *api_nh;
1192 int nh_family;
1193 unsigned int valid_nh_count = 0;
1194 int has_valid_label = 0;
1195 uint8_t distance;
1196 struct peer *peer;
1197 struct bgp_path_info *mpinfo;
1198 uint32_t metric;
1199 struct attr local_attr;
1200 struct bgp_path_info local_info;
1201 struct bgp_path_info *mpinfo_cp = &local_info;
1202 route_tag_t tag;
1203 mpls_label_t label;
1204 int nh_othervrf = 0;
1205 char buf_prefix[PREFIX_STRLEN]; /* filled in if we are debugging */
1206 bool is_evpn;
1207 int nh_updated;
1208
1209 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1210 * know of this instance.
1211 */
1212 if (!bgp_install_info_to_zebra(bgp))
1213 return;
1214
1215 if (bgp->main_zebra_update_hold)
1216 return;
1217
1218 if (bgp_debug_zebra(p))
1219 prefix2str(p, buf_prefix, sizeof(buf_prefix));
1220
1221 if (safi == SAFI_FLOWSPEC) {
1222 bgp_pbr_update_entry(bgp, &rn->p, info, afi, safi, true);
1223 return;
1224 }
1225
1226 /*
1227 * vrf leaking support (will have only one nexthop)
1228 */
1229 if (info->extra && info->extra->bgp_orig)
1230 nh_othervrf = 1;
1231
1232 /* Make Zebra API structure. */
1233 memset(&api, 0, sizeof(api));
1234 api.vrf_id = bgp->vrf_id;
1235 api.type = ZEBRA_ROUTE_BGP;
1236 api.safi = safi;
1237 api.prefix = *p;
1238 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1239
1240 peer = info->peer;
1241
1242 if (info->type == ZEBRA_ROUTE_BGP
1243 && info->sub_type == BGP_ROUTE_IMPORTED) {
1244
1245 /* Obtain peer from parent */
1246 if (info->extra && info->extra->parent)
1247 peer = ((struct bgp_path_info *)(info->extra->parent))
1248 ->peer;
1249 }
1250
1251 tag = info->attr->tag;
1252
1253 /* If the route's source is EVPN, flag as such. */
1254 is_evpn = is_route_parent_evpn(info);
1255 if (is_evpn)
1256 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1257
1258 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1259 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1260 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1261 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1262 }
1263
1264 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1265 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1266 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1267
1268 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1269
1270 /* Metric is currently based on the best-path only */
1271 metric = info->attr->med;
1272 for (mpinfo = info; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) {
1273 if (valid_nh_count >= multipath_num)
1274 break;
1275
1276 *mpinfo_cp = *mpinfo;
1277
1278 /* Get nexthop address-family */
1279 if (p->family == AF_INET
1280 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1281 nh_family = AF_INET;
1282 else if (p->family == AF_INET6
1283 || (p->family == AF_INET
1284 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1285 nh_family = AF_INET6;
1286 else
1287 continue;
1288
1289 api_nh = &api.nexthops[valid_nh_count];
1290 api_nh->vrf_id = nh_othervrf ? info->extra->bgp_orig->vrf_id
1291 : bgp->vrf_id;
1292 if (nh_family == AF_INET) {
1293 if (bgp_debug_zebra(&api.prefix)) {
1294 if (mpinfo->extra) {
1295 zlog_debug(
1296 "%s: p=%s, bgp_is_valid_label: %d",
1297 __func__, buf_prefix,
1298 bgp_is_valid_label(
1299 &mpinfo->extra
1300 ->label[0]));
1301 } else {
1302 zlog_debug(
1303 "%s: p=%s, extra is NULL, no label",
1304 __func__, buf_prefix);
1305 }
1306 }
1307
1308 if (bgp->table_map[afi][safi].name) {
1309 /* Copy info and attributes, so the route-map
1310 apply doesn't modify the BGP route info. */
1311 local_attr = *mpinfo->attr;
1312 mpinfo_cp->attr = &local_attr;
1313 }
1314
1315 if (bgp->table_map[afi][safi].name) {
1316 if (!bgp_table_map_apply(
1317 bgp->table_map[afi][safi].map, p,
1318 mpinfo_cp))
1319 continue;
1320
1321 /* metric/tag is only allowed to be
1322 * overridden on 1st nexthop */
1323 if (mpinfo == info) {
1324 metric = mpinfo_cp->attr->med;
1325 tag = mpinfo_cp->attr->tag;
1326 }
1327 }
1328
1329 nh_updated = update_ipv4nh_for_route_install(
1330 nh_othervrf,
1331 &mpinfo_cp->attr->nexthop,
1332 mpinfo_cp->attr, is_evpn, api_nh);
1333 } else {
1334 ifindex_t ifindex = IFINDEX_INTERNAL;
1335 struct in6_addr *nexthop;
1336
1337 if (bgp->table_map[afi][safi].name) {
1338 /* Copy info and attributes, so the route-map
1339 apply doesn't modify the BGP route info. */
1340 local_attr = *mpinfo->attr;
1341 mpinfo_cp->attr = &local_attr;
1342 }
1343
1344 if (bgp->table_map[afi][safi].name) {
1345 /* Copy info and attributes, so the route-map
1346 apply doesn't modify the BGP route info. */
1347 local_attr = *mpinfo->attr;
1348 mpinfo_cp->attr = &local_attr;
1349
1350 if (!bgp_table_map_apply(
1351 bgp->table_map[afi][safi].map, p,
1352 mpinfo_cp))
1353 continue;
1354
1355 /* metric/tag is only allowed to be
1356 * overridden on 1st nexthop */
1357 if (mpinfo == info) {
1358 metric = mpinfo_cp->attr->med;
1359 tag = mpinfo_cp->attr->tag;
1360 }
1361 }
1362 nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp,
1363 &ifindex);
1364 nh_updated = update_ipv6nh_for_route_install(
1365 nh_othervrf, nexthop, ifindex,
1366 mpinfo, info, is_evpn, api_nh);
1367 }
1368
1369 /* Did we get proper nexthop info to update zebra? */
1370 if (!nh_updated)
1371 continue;
1372
1373 if (mpinfo->extra
1374 && bgp_is_valid_label(&mpinfo->extra->label[0])
1375 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1376 has_valid_label = 1;
1377 label = label_pton(&mpinfo->extra->label[0]);
1378
1379 api_nh->label_num = 1;
1380 api_nh->labels[0] = label;
1381 }
1382 memcpy(&api_nh->rmac, &(mpinfo->attr->rmac),
1383 sizeof(struct ethaddr));
1384 valid_nh_count++;
1385 }
1386
1387
1388 /* if this is a evpn route we don't have to include the label */
1389 if (has_valid_label && !(CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)))
1390 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1391
1392 /*
1393 * When we create an aggregate route we must also
1394 * install a Null0 route in the RIB, so overwrite
1395 * what was written into api with a blackhole route
1396 */
1397 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1398 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1399 else
1400 api.nexthop_num = valid_nh_count;
1401
1402 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1403 api.metric = metric;
1404
1405 if (tag) {
1406 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1407 api.tag = tag;
1408 }
1409
1410 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1411 if (distance) {
1412 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1413 api.distance = distance;
1414 }
1415
1416 if (bgp_debug_zebra(p)) {
1417 char prefix_buf[PREFIX_STRLEN];
1418 char nh_buf[INET6_ADDRSTRLEN];
1419 char label_buf[20];
1420 int i;
1421
1422 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1423 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1424 " count %d",
1425 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1426 prefix_buf, api.metric, api.tag, api.nexthop_num);
1427 for (i = 0; i < api.nexthop_num; i++) {
1428 api_nh = &api.nexthops[i];
1429
1430 if (api_nh->type == NEXTHOP_TYPE_IFINDEX)
1431 nh_buf[0] = '\0';
1432 else {
1433 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1434 nh_family = AF_INET;
1435 else
1436 nh_family = AF_INET6;
1437 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1438 sizeof(nh_buf));
1439 }
1440
1441 label_buf[0] = '\0';
1442 if (has_valid_label
1443 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1444 sprintf(label_buf, "label %u",
1445 api_nh->labels[0]);
1446 zlog_debug(" nhop [%d]: %s if %u VRF %u %s",
1447 i + 1, nh_buf, api_nh->ifindex,
1448 api_nh->vrf_id, label_buf);
1449 }
1450 }
1451
1452 if (bgp_debug_zebra(p)) {
1453 int recursion_flag = 0;
1454
1455 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1456 recursion_flag = 1;
1457
1458 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1459 __func__, buf_prefix,
1460 (recursion_flag ? "" : "NOT "));
1461 }
1462 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1463 : ZEBRA_ROUTE_DELETE,
1464 zclient, &api);
1465 }
1466
1467 /* Announce all routes of a table to zebra */
1468 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1469 {
1470 struct bgp_node *rn;
1471 struct bgp_table *table;
1472 struct bgp_path_info *pi;
1473
1474 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1475 * know of this instance.
1476 */
1477 if (!bgp_install_info_to_zebra(bgp))
1478 return;
1479
1480 table = bgp->rib[afi][safi];
1481 if (!table)
1482 return;
1483
1484 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1485 for (pi = rn->info; pi; pi = pi->next)
1486 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) &&
1487
1488 (pi->type == ZEBRA_ROUTE_BGP
1489 && (pi->sub_type == BGP_ROUTE_NORMAL
1490 || pi->sub_type == BGP_ROUTE_IMPORTED)))
1491
1492 bgp_zebra_announce(rn, &rn->p, pi, bgp, afi,
1493 safi);
1494 }
1495
1496 void bgp_zebra_withdraw(struct prefix *p, struct bgp_path_info *info,
1497 struct bgp *bgp, safi_t safi)
1498 {
1499 struct zapi_route api;
1500 struct peer *peer;
1501
1502 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1503 * know of this instance.
1504 */
1505 if (!bgp_install_info_to_zebra(bgp))
1506 return;
1507
1508 if (safi == SAFI_FLOWSPEC) {
1509 peer = info->peer;
1510 bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false);
1511 return;
1512 }
1513
1514 memset(&api, 0, sizeof(api));
1515 api.vrf_id = bgp->vrf_id;
1516 api.type = ZEBRA_ROUTE_BGP;
1517 api.safi = safi;
1518 api.prefix = *p;
1519
1520 /* If the route's source is EVPN, flag as such. */
1521 if (is_route_parent_evpn(info))
1522 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1523
1524 if (bgp_debug_zebra(p)) {
1525 char buf[PREFIX_STRLEN];
1526
1527 prefix2str(&api.prefix, buf, sizeof(buf));
1528 zlog_debug("Tx route delete VRF %u %s", bgp->vrf_id, buf);
1529 }
1530
1531 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1532 }
1533
1534 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1535 unsigned short instance)
1536 {
1537 struct list *red_list;
1538 struct listnode *node;
1539 struct bgp_redist *red;
1540
1541 red_list = bgp->redist[afi][type];
1542 if (!red_list)
1543 return (NULL);
1544
1545 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1546 if (red->instance == instance)
1547 return red;
1548
1549 return NULL;
1550 }
1551
1552 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1553 unsigned short instance)
1554 {
1555 struct list *red_list;
1556 struct bgp_redist *red;
1557
1558 red = bgp_redist_lookup(bgp, afi, type, instance);
1559 if (red)
1560 return red;
1561
1562 if (!bgp->redist[afi][type])
1563 bgp->redist[afi][type] = list_new();
1564
1565 red_list = bgp->redist[afi][type];
1566 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1567 sizeof(struct bgp_redist));
1568 red->instance = instance;
1569
1570 listnode_add(red_list, red);
1571
1572 return red;
1573 }
1574
1575 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1576 unsigned short instance)
1577 {
1578 struct bgp_redist *red;
1579
1580 red = bgp_redist_lookup(bgp, afi, type, instance);
1581
1582 if (red) {
1583 listnode_delete(bgp->redist[afi][type], red);
1584 XFREE(MTYPE_BGP_REDIST, red);
1585 if (!bgp->redist[afi][type]->count)
1586 list_delete(&bgp->redist[afi][type]);
1587 }
1588 }
1589
1590 /* Other routes redistribution into BGP. */
1591 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1592 unsigned short instance, bool changed)
1593 {
1594 /* If redistribute options are changed call
1595 * bgp_redistribute_unreg() to reset the option and withdraw
1596 * the routes
1597 */
1598 if (changed)
1599 bgp_redistribute_unreg(bgp, afi, type, instance);
1600
1601 /* Return if already redistribute flag is set. */
1602 if (instance) {
1603 if (redist_check_instance(&zclient->mi_redist[afi][type],
1604 instance))
1605 return CMD_WARNING;
1606
1607 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1608 } else {
1609 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1610 return CMD_WARNING;
1611
1612 #if ENABLE_BGP_VNC
1613 if (bgp->vrf_id == VRF_DEFAULT
1614 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1615 vnc_export_bgp_enable(
1616 bgp, afi); /* only enables if mode bits cfg'd */
1617 }
1618 #endif
1619
1620 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1621 }
1622
1623 /*
1624 * Don't try to register if we're not connected to Zebra or Zebra
1625 * doesn't know of this instance.
1626 *
1627 * When we come up later well resend if needed.
1628 */
1629 if (!bgp_install_info_to_zebra(bgp))
1630 return CMD_SUCCESS;
1631
1632 if (BGP_DEBUG(zebra, ZEBRA))
1633 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1634 bgp->vrf_id, afi, zebra_route_string(type),
1635 instance);
1636
1637 /* Send distribute add message to zebra. */
1638 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1639 instance, bgp->vrf_id);
1640
1641 return CMD_SUCCESS;
1642 }
1643
1644 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1645 unsigned short instance)
1646 {
1647 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1648 * know of this instance.
1649 */
1650 if (!bgp_install_info_to_zebra(bgp))
1651 return -1;
1652
1653 if (BGP_DEBUG(zebra, ZEBRA))
1654 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1655 bgp->vrf_id, afi, zebra_route_string(type),
1656 instance);
1657
1658 /* Send distribute add message to zebra. */
1659 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1660 instance, bgp->vrf_id);
1661 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1662 instance, bgp->vrf_id);
1663
1664 return 0;
1665 }
1666
1667 /* Redistribute with route-map specification. */
1668 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name,
1669 struct route_map *route_map)
1670 {
1671 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1672 return 0;
1673
1674 if (red->rmap.name)
1675 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1676 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1677 red->rmap.map = route_map;
1678
1679 return 1;
1680 }
1681
1682 /* Redistribute with metric specification. */
1683 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1684 afi_t afi, int type, uint32_t metric)
1685 {
1686 struct bgp_node *rn;
1687 struct bgp_path_info *pi;
1688
1689 if (red->redist_metric_flag && red->redist_metric == metric)
1690 return 0;
1691
1692 red->redist_metric_flag = 1;
1693 red->redist_metric = metric;
1694
1695 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1696 rn = bgp_route_next(rn)) {
1697 for (pi = rn->info; pi; pi = pi->next) {
1698 if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE
1699 && pi->type == type
1700 && pi->instance == red->instance) {
1701 struct attr *old_attr;
1702 struct attr new_attr;
1703
1704 bgp_attr_dup(&new_attr, pi->attr);
1705 new_attr.med = red->redist_metric;
1706 old_attr = pi->attr;
1707 pi->attr = bgp_attr_intern(&new_attr);
1708 bgp_attr_unintern(&old_attr);
1709
1710 bgp_path_info_set_flag(rn, pi,
1711 BGP_PATH_ATTR_CHANGED);
1712 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1713 }
1714 }
1715 }
1716
1717 return 1;
1718 }
1719
1720 /* Unset redistribution. */
1721 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1722 unsigned short instance)
1723 {
1724 struct bgp_redist *red;
1725
1726 red = bgp_redist_lookup(bgp, afi, type, instance);
1727 if (!red)
1728 return CMD_SUCCESS;
1729
1730 /* Return if zebra connection is disabled. */
1731 if (instance) {
1732 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1733 instance))
1734 return CMD_WARNING;
1735 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1736 } else {
1737 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1738 return CMD_WARNING;
1739 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1740 }
1741
1742
1743 if (bgp_install_info_to_zebra(bgp)) {
1744 /* Send distribute delete message to zebra. */
1745 if (BGP_DEBUG(zebra, ZEBRA))
1746 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1747 bgp->vrf_id, afi, zebra_route_string(type),
1748 instance);
1749 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1750 type, instance, bgp->vrf_id);
1751 }
1752
1753 /* Withdraw redistributed routes from current BGP's routing table. */
1754 bgp_redistribute_withdraw(bgp, afi, type, instance);
1755
1756 return CMD_SUCCESS;
1757 }
1758
1759 /* Unset redistribution. */
1760 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1761 unsigned short instance)
1762 {
1763 struct bgp_redist *red;
1764
1765 /*
1766 * vnc and vpn->vrf checks must be before red check because
1767 * they operate within bgpd irrespective of zebra connection
1768 * status. red lookup fails if there is no zebra connection.
1769 */
1770 #if ENABLE_BGP_VNC
1771 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1772 vnc_export_bgp_disable(bgp, afi);
1773 }
1774 #endif
1775
1776 red = bgp_redist_lookup(bgp, afi, type, instance);
1777 if (!red)
1778 return CMD_SUCCESS;
1779
1780 bgp_redistribute_unreg(bgp, afi, type, instance);
1781
1782 /* Unset route-map. */
1783 if (red->rmap.name)
1784 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1785 red->rmap.name = NULL;
1786 red->rmap.map = NULL;
1787
1788 /* Unset metric. */
1789 red->redist_metric_flag = 0;
1790 red->redist_metric = 0;
1791
1792 bgp_redist_del(bgp, afi, type, instance);
1793
1794 return CMD_SUCCESS;
1795 }
1796
1797 /* Update redistribute vrf bitmap during triggers like
1798 restart networking or delete/add VRFs */
1799 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1800 {
1801 int i;
1802 afi_t afi;
1803
1804 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1805 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1806 if ((old_vrf_id == VRF_UNKNOWN)
1807 || vrf_bitmap_check(zclient->redist[afi][i],
1808 old_vrf_id)) {
1809 vrf_bitmap_unset(zclient->redist[afi][i],
1810 old_vrf_id);
1811 vrf_bitmap_set(zclient->redist[afi][i],
1812 bgp->vrf_id);
1813 }
1814 return;
1815 }
1816
1817 void bgp_zclient_reset(void)
1818 {
1819 zclient_reset(zclient);
1820 }
1821
1822 /* Register this instance with Zebra. Invoked upon connect (for
1823 * default instance) and when other VRFs are learnt (or created and
1824 * already learnt).
1825 */
1826 void bgp_zebra_instance_register(struct bgp *bgp)
1827 {
1828 /* Don't try to register if we're not connected to Zebra */
1829 if (!zclient || zclient->sock < 0)
1830 return;
1831
1832 if (BGP_DEBUG(zebra, ZEBRA))
1833 zlog_debug("Registering VRF %u", bgp->vrf_id);
1834
1835 /* Register for router-id, interfaces, redistributed routes. */
1836 zclient_send_reg_requests(zclient, bgp->vrf_id);
1837
1838 /* For default instance, register to learn about VNIs, if appropriate.
1839 */
1840 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1841 bgp_zebra_advertise_all_vni(bgp, 1);
1842
1843 bgp_nht_register_nexthops(bgp);
1844 }
1845
1846 /* Deregister this instance with Zebra. Invoked upon the instance
1847 * being deleted (default or VRF) and it is already registered.
1848 */
1849 void bgp_zebra_instance_deregister(struct bgp *bgp)
1850 {
1851 /* Don't try to deregister if we're not connected to Zebra */
1852 if (zclient->sock < 0)
1853 return;
1854
1855 if (BGP_DEBUG(zebra, ZEBRA))
1856 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
1857
1858 /* For default instance, unregister learning about VNIs, if appropriate.
1859 */
1860 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1861 bgp_zebra_advertise_all_vni(bgp, 0);
1862
1863 /* Deregister for router-id, interfaces, redistributed routes. */
1864 zclient_send_dereg_requests(zclient, bgp->vrf_id);
1865 }
1866
1867 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
1868 {
1869 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
1870
1871 /* Don't try to initiate if we're not connected to Zebra */
1872 if (zclient->sock < 0)
1873 return;
1874
1875 if (BGP_DEBUG(zebra, ZEBRA))
1876 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1877 peer->host);
1878
1879 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1880 ra_interval);
1881 }
1882
1883 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
1884 {
1885 /* Don't try to terminate if we're not connected to Zebra */
1886 if (zclient->sock < 0)
1887 return;
1888
1889 if (BGP_DEBUG(zebra, ZEBRA))
1890 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1891 peer->host);
1892
1893 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
1894 }
1895
1896 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1897 {
1898 struct stream *s = NULL;
1899
1900 /* Check socket. */
1901 if (!zclient || zclient->sock < 0)
1902 return 0;
1903
1904 /* Don't try to register if Zebra doesn't know of this instance. */
1905 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1906 if (BGP_DEBUG(zebra, ZEBRA))
1907 zlog_debug("%s: No zebra instance to talk to, cannot advertise subnet",
1908 __PRETTY_FUNCTION__);
1909 return 0;
1910 }
1911
1912 s = zclient->obuf;
1913 stream_reset(s);
1914
1915 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1916 stream_putc(s, advertise);
1917 stream_put3(s, vni);
1918 stream_putw_at(s, 0, stream_get_endp(s));
1919
1920 return zclient_send_message(zclient);
1921 }
1922
1923 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1924 {
1925 struct stream *s = NULL;
1926
1927 /* Check socket. */
1928 if (!zclient || zclient->sock < 0)
1929 return 0;
1930
1931 /* Don't try to register if Zebra doesn't know of this instance. */
1932 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1933 if (BGP_DEBUG(zebra, ZEBRA))
1934 zlog_debug("%s: No zebra instance to talk to, not installing gw_macip",
1935 __PRETTY_FUNCTION__);
1936 return 0;
1937 }
1938
1939 s = zclient->obuf;
1940 stream_reset(s);
1941
1942 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1943 stream_putc(s, advertise);
1944 stream_putl(s, vni);
1945 stream_putw_at(s, 0, stream_get_endp(s));
1946
1947 return zclient_send_message(zclient);
1948 }
1949
1950 int bgp_zebra_vxlan_flood_control(struct bgp *bgp,
1951 enum vxlan_flood_control flood_ctrl)
1952 {
1953 struct stream *s;
1954
1955 /* Check socket. */
1956 if (!zclient || zclient->sock < 0)
1957 return 0;
1958
1959 /* Don't try to register if Zebra doesn't know of this instance. */
1960 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1961 if (BGP_DEBUG(zebra, ZEBRA))
1962 zlog_debug("%s: No zebra instance to talk to, not installing all vni",
1963 __PRETTY_FUNCTION__);
1964 return 0;
1965 }
1966
1967 s = zclient->obuf;
1968 stream_reset(s);
1969
1970 zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id);
1971 stream_putc(s, flood_ctrl);
1972 stream_putw_at(s, 0, stream_get_endp(s));
1973
1974 return zclient_send_message(zclient);
1975 }
1976
1977 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
1978 {
1979 struct stream *s;
1980
1981 /* Check socket. */
1982 if (!zclient || zclient->sock < 0)
1983 return 0;
1984
1985 /* Don't try to register if Zebra doesn't know of this instance. */
1986 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1987 return 0;
1988
1989 s = zclient->obuf;
1990 stream_reset(s);
1991
1992 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1993 stream_putc(s, advertise);
1994 /* Also inform current BUM handling setting. This is really
1995 * relevant only when 'advertise' is set.
1996 */
1997 stream_putc(s, bgp->vxlan_flood_ctrl);
1998 stream_putw_at(s, 0, stream_get_endp(s));
1999
2000 return zclient_send_message(zclient);
2001 }
2002
2003 int bgp_zebra_dup_addr_detection(struct bgp *bgp)
2004 {
2005 struct stream *s;
2006
2007 /* Check socket. */
2008 if (!zclient || zclient->sock < 0)
2009 return 0;
2010
2011 /* Don't try to register if Zebra doesn't know of this instance. */
2012 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
2013 return 0;
2014
2015 if (BGP_DEBUG(zebra, ZEBRA))
2016 zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u",
2017 bgp->evpn_info->dup_addr_detect ?
2018 "enable" : "disable",
2019 bgp->evpn_info->dad_max_moves,
2020 bgp->evpn_info->dad_time,
2021 bgp->evpn_info->dad_freeze ?
2022 "enable" : "disable",
2023 bgp->evpn_info->dad_freeze_time);
2024
2025 s = zclient->obuf;
2026 stream_reset(s);
2027 zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION,
2028 bgp->vrf_id);
2029 stream_putl(s, bgp->evpn_info->dup_addr_detect);
2030 stream_putl(s, bgp->evpn_info->dad_time);
2031 stream_putl(s, bgp->evpn_info->dad_max_moves);
2032 stream_putl(s, bgp->evpn_info->dad_freeze);
2033 stream_putl(s, bgp->evpn_info->dad_freeze_time);
2034 stream_putw_at(s, 0, stream_get_endp(s));
2035
2036 return zclient_send_message(zclient);
2037 }
2038
2039 static int rule_notify_owner(int command, struct zclient *zclient,
2040 zebra_size_t length, vrf_id_t vrf_id)
2041 {
2042 uint32_t seqno, priority, unique;
2043 enum zapi_rule_notify_owner note;
2044 struct bgp_pbr_action *bgp_pbra;
2045 ifindex_t ifi;
2046
2047 if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique,
2048 &ifi, &note))
2049 return -1;
2050
2051 bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique);
2052 if (!bgp_pbra) {
2053 if (BGP_DEBUG(zebra, ZEBRA))
2054 zlog_debug("%s: Fail to look BGP rule (%u)",
2055 __PRETTY_FUNCTION__, unique);
2056 return 0;
2057 }
2058
2059 switch (note) {
2060 case ZAPI_RULE_FAIL_INSTALL:
2061 if (BGP_DEBUG(zebra, ZEBRA))
2062 zlog_debug("%s: Received RULE_FAIL_INSTALL",
2063 __PRETTY_FUNCTION__);
2064 bgp_pbra->installed = false;
2065 bgp_pbra->install_in_progress = false;
2066 break;
2067 case ZAPI_RULE_INSTALLED:
2068 bgp_pbra->installed = true;
2069 bgp_pbra->install_in_progress = false;
2070 if (BGP_DEBUG(zebra, ZEBRA))
2071 zlog_debug("%s: Received RULE_INSTALLED",
2072 __PRETTY_FUNCTION__);
2073 break;
2074 case ZAPI_RULE_FAIL_REMOVE:
2075 case ZAPI_RULE_REMOVED:
2076 if (BGP_DEBUG(zebra, ZEBRA))
2077 zlog_debug("%s: Received RULE REMOVED",
2078 __PRETTY_FUNCTION__);
2079 break;
2080 }
2081
2082 return 0;
2083 }
2084
2085 static int ipset_notify_owner(int command, struct zclient *zclient,
2086 zebra_size_t length, vrf_id_t vrf_id)
2087 {
2088 uint32_t unique;
2089 enum zapi_ipset_notify_owner note;
2090 struct bgp_pbr_match *bgp_pbim;
2091
2092 if (!zapi_ipset_notify_decode(zclient->ibuf,
2093 &unique,
2094 &note))
2095 return -1;
2096
2097 bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique);
2098 if (!bgp_pbim) {
2099 if (BGP_DEBUG(zebra, ZEBRA))
2100 zlog_debug("%s: Fail to look BGP match ( %u, ID %u)",
2101 __PRETTY_FUNCTION__, note, unique);
2102 return 0;
2103 }
2104
2105 switch (note) {
2106 case ZAPI_IPSET_FAIL_INSTALL:
2107 if (BGP_DEBUG(zebra, ZEBRA))
2108 zlog_debug("%s: Received IPSET_FAIL_INSTALL",
2109 __PRETTY_FUNCTION__);
2110 bgp_pbim->installed = false;
2111 bgp_pbim->install_in_progress = false;
2112 break;
2113 case ZAPI_IPSET_INSTALLED:
2114 bgp_pbim->installed = true;
2115 bgp_pbim->install_in_progress = false;
2116 if (BGP_DEBUG(zebra, ZEBRA))
2117 zlog_debug("%s: Received IPSET_INSTALLED",
2118 __PRETTY_FUNCTION__);
2119 break;
2120 case ZAPI_IPSET_FAIL_REMOVE:
2121 case ZAPI_IPSET_REMOVED:
2122 if (BGP_DEBUG(zebra, ZEBRA))
2123 zlog_debug("%s: Received IPSET REMOVED",
2124 __PRETTY_FUNCTION__);
2125 break;
2126 }
2127
2128 return 0;
2129 }
2130
2131 static int ipset_entry_notify_owner(int command, struct zclient *zclient,
2132 zebra_size_t length, vrf_id_t vrf_id)
2133 {
2134 uint32_t unique;
2135 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
2136 enum zapi_ipset_entry_notify_owner note;
2137 struct bgp_pbr_match_entry *bgp_pbime;
2138
2139 if (!zapi_ipset_entry_notify_decode(
2140 zclient->ibuf,
2141 &unique,
2142 ipset_name,
2143 &note))
2144 return -1;
2145 bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id,
2146 ipset_name,
2147 unique);
2148 if (!bgp_pbime) {
2149 if (BGP_DEBUG(zebra, ZEBRA))
2150 zlog_debug("%s: Fail to look BGP match entry (%u, ID %u)",
2151 __PRETTY_FUNCTION__, note, unique);
2152 return 0;
2153 }
2154
2155 switch (note) {
2156 case ZAPI_IPSET_ENTRY_FAIL_INSTALL:
2157 if (BGP_DEBUG(zebra, ZEBRA))
2158 zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL",
2159 __PRETTY_FUNCTION__);
2160 bgp_pbime->installed = false;
2161 bgp_pbime->install_in_progress = false;
2162 break;
2163 case ZAPI_IPSET_ENTRY_INSTALLED:
2164 {
2165 struct bgp_path_info *path;
2166 struct bgp_path_info_extra *extra;
2167
2168 bgp_pbime->installed = true;
2169 bgp_pbime->install_in_progress = false;
2170 if (BGP_DEBUG(zebra, ZEBRA))
2171 zlog_debug("%s: Received IPSET_ENTRY_INSTALLED",
2172 __PRETTY_FUNCTION__);
2173 /* link bgp_path_info to bpme */
2174 path = (struct bgp_path_info *)bgp_pbime->path;
2175 extra = bgp_path_info_extra_get(path);
2176 if (extra->bgp_fs_pbr == NULL)
2177 extra->bgp_fs_pbr = list_new();
2178 listnode_add(extra->bgp_fs_pbr, bgp_pbime);
2179 }
2180 break;
2181 case ZAPI_IPSET_ENTRY_FAIL_REMOVE:
2182 case ZAPI_IPSET_ENTRY_REMOVED:
2183 if (BGP_DEBUG(zebra, ZEBRA))
2184 zlog_debug("%s: Received IPSET_ENTRY_REMOVED",
2185 __PRETTY_FUNCTION__);
2186 break;
2187 }
2188 return 0;
2189 }
2190
2191 static int iptable_notify_owner(int command, struct zclient *zclient,
2192 zebra_size_t length, vrf_id_t vrf_id)
2193 {
2194 uint32_t unique;
2195 enum zapi_iptable_notify_owner note;
2196 struct bgp_pbr_match *bgpm;
2197
2198 if (!zapi_iptable_notify_decode(
2199 zclient->ibuf,
2200 &unique,
2201 &note))
2202 return -1;
2203 bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique);
2204 if (!bgpm) {
2205 if (BGP_DEBUG(zebra, ZEBRA))
2206 zlog_debug("%s: Fail to look BGP iptable (%u %u)",
2207 __PRETTY_FUNCTION__, note, unique);
2208 return 0;
2209 }
2210 switch (note) {
2211 case ZAPI_IPTABLE_FAIL_INSTALL:
2212 if (BGP_DEBUG(zebra, ZEBRA))
2213 zlog_debug("%s: Received IPTABLE_FAIL_INSTALL",
2214 __PRETTY_FUNCTION__);
2215 bgpm->installed_in_iptable = false;
2216 bgpm->install_iptable_in_progress = false;
2217 break;
2218 case ZAPI_IPTABLE_INSTALLED:
2219 bgpm->installed_in_iptable = true;
2220 bgpm->install_iptable_in_progress = false;
2221 if (BGP_DEBUG(zebra, ZEBRA))
2222 zlog_debug("%s: Received IPTABLE_INSTALLED",
2223 __PRETTY_FUNCTION__);
2224 bgpm->action->refcnt++;
2225 break;
2226 case ZAPI_IPTABLE_FAIL_REMOVE:
2227 case ZAPI_IPTABLE_REMOVED:
2228 if (BGP_DEBUG(zebra, ZEBRA))
2229 zlog_debug("%s: Received IPTABLE REMOVED",
2230 __PRETTY_FUNCTION__);
2231 break;
2232 }
2233 return 0;
2234 }
2235
2236 static void bgp_encode_pbr_rule_action(struct stream *s,
2237 struct bgp_pbr_action *pbra)
2238 {
2239 struct prefix any;
2240
2241 stream_putl(s, 0); /* seqno unused */
2242 stream_putl(s, 0); /* ruleno unused */
2243
2244 stream_putl(s, pbra->unique);
2245
2246 memset(&any, 0, sizeof(any));
2247 any.family = AF_INET;
2248 stream_putc(s, any.family);
2249 stream_putc(s, any.prefixlen);
2250 stream_put(s, &any.u.prefix, prefix_blen(&any));
2251
2252 stream_putw(s, 0); /* src port */
2253
2254 stream_putc(s, any.family);
2255 stream_putc(s, any.prefixlen);
2256 stream_put(s, &any.u.prefix, prefix_blen(&any));
2257
2258 stream_putw(s, 0); /* dst port */
2259
2260 stream_putl(s, pbra->fwmark); /* fwmark */
2261
2262 stream_putl(s, pbra->table_id);
2263
2264 stream_putl(s, 0); /* ifindex unused */
2265 }
2266
2267 static void bgp_encode_pbr_ipset_match(struct stream *s,
2268 struct bgp_pbr_match *pbim)
2269 {
2270 stream_putl(s, pbim->unique);
2271 stream_putl(s, pbim->type);
2272
2273 stream_put(s, pbim->ipset_name,
2274 ZEBRA_IPSET_NAME_SIZE);
2275 }
2276
2277 static void bgp_encode_pbr_ipset_entry_match(struct stream *s,
2278 struct bgp_pbr_match_entry *pbime)
2279 {
2280 stream_putl(s, pbime->unique);
2281 /* check that back pointer is not null */
2282 stream_put(s, pbime->backpointer->ipset_name,
2283 ZEBRA_IPSET_NAME_SIZE);
2284
2285 stream_putc(s, pbime->src.family);
2286 stream_putc(s, pbime->src.prefixlen);
2287 stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src));
2288
2289 stream_putc(s, pbime->dst.family);
2290 stream_putc(s, pbime->dst.prefixlen);
2291 stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst));
2292
2293 stream_putw(s, pbime->src_port_min);
2294 stream_putw(s, pbime->src_port_max);
2295 stream_putw(s, pbime->dst_port_min);
2296 stream_putw(s, pbime->dst_port_max);
2297 stream_putc(s, pbime->proto);
2298 }
2299
2300 static void bgp_encode_pbr_iptable_match(struct stream *s,
2301 struct bgp_pbr_action *bpa,
2302 struct bgp_pbr_match *pbm)
2303 {
2304 stream_putl(s, pbm->unique2);
2305
2306 stream_putl(s, pbm->type);
2307
2308 stream_putl(s, pbm->flags);
2309
2310 /* TODO: correlate with what is contained
2311 * into bgp_pbr_action.
2312 * currently only forward supported
2313 */
2314 if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE)
2315 stream_putl(s, ZEBRA_IPTABLES_DROP);
2316 else
2317 stream_putl(s, ZEBRA_IPTABLES_FORWARD);
2318 stream_putl(s, bpa->fwmark);
2319 stream_put(s, pbm->ipset_name,
2320 ZEBRA_IPSET_NAME_SIZE);
2321 stream_putw(s, pbm->pkt_len_min);
2322 stream_putw(s, pbm->pkt_len_max);
2323 stream_putw(s, pbm->tcp_flags);
2324 stream_putw(s, pbm->tcp_mask_flags);
2325 stream_putc(s, pbm->dscp_value);
2326 stream_putc(s, pbm->fragment);
2327 }
2328
2329 /* BGP has established connection with Zebra. */
2330 static void bgp_zebra_connected(struct zclient *zclient)
2331 {
2332 struct bgp *bgp;
2333
2334 zclient_num_connects++; /* increment even if not responding */
2335
2336 /* At this point, we may or may not have BGP instances configured, but
2337 * we're only interested in the default VRF (others wouldn't have learnt
2338 * the VRF from Zebra yet.)
2339 */
2340 bgp = bgp_get_default();
2341 if (!bgp)
2342 return;
2343
2344 bgp_zebra_instance_register(bgp);
2345
2346 /* Send the client registration */
2347 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
2348
2349 /* tell label pool that zebra is connected */
2350 bgp_lp_event_zebra_up();
2351
2352 /* TODO - What if we have peers and networks configured, do we have to
2353 * kick-start them?
2354 */
2355 }
2356
2357 static int bgp_zebra_process_local_es(int cmd, struct zclient *zclient,
2358 zebra_size_t length, vrf_id_t vrf_id)
2359 {
2360 esi_t esi;
2361 struct bgp *bgp = NULL;
2362 struct stream *s = NULL;
2363 char buf[ESI_STR_LEN];
2364 char buf1[INET6_ADDRSTRLEN];
2365 struct ipaddr originator_ip;
2366
2367 memset(&esi, 0, sizeof(esi_t));
2368 memset(&originator_ip, 0, sizeof(struct ipaddr));
2369
2370 bgp = bgp_lookup_by_vrf_id(vrf_id);
2371 if (!bgp)
2372 return 0;
2373
2374 s = zclient->ibuf;
2375 stream_get(&esi, s, sizeof(esi_t));
2376 stream_get(&originator_ip, s, sizeof(struct ipaddr));
2377
2378 if (BGP_DEBUG(zebra, ZEBRA))
2379 zlog_debug("Rx %s ESI %s originator-ip %s",
2380 (cmd == ZEBRA_LOCAL_ES_ADD) ? "add" : "del",
2381 esi_to_str(&esi, buf, sizeof(buf)),
2382 ipaddr2str(&originator_ip, buf1, sizeof(buf1)));
2383
2384 if (cmd == ZEBRA_LOCAL_ES_ADD)
2385 bgp_evpn_local_es_add(bgp, &esi, &originator_ip);
2386 else
2387 bgp_evpn_local_es_del(bgp, &esi, &originator_ip);
2388 return 0;
2389 }
2390
2391 static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
2392 zebra_size_t length, vrf_id_t vrf_id)
2393 {
2394 int filter = 0;
2395 char buf[ETHER_ADDR_STRLEN];
2396 vni_t l3vni = 0;
2397 struct ethaddr rmac;
2398 struct in_addr originator_ip;
2399 struct stream *s;
2400
2401 memset(&rmac, 0, sizeof(struct ethaddr));
2402 memset(&originator_ip, 0, sizeof(struct in_addr));
2403 s = zclient->ibuf;
2404 l3vni = stream_getl(s);
2405 if (cmd == ZEBRA_L3VNI_ADD) {
2406 stream_get(&rmac, s, sizeof(struct ethaddr));
2407 originator_ip.s_addr = stream_get_ipv4(s);
2408 stream_get(&filter, s, sizeof(int));
2409 }
2410
2411 if (BGP_DEBUG(zebra, ZEBRA))
2412 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
2413 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
2414 vrf_id_to_name(vrf_id), l3vni,
2415 prefix_mac2str(&rmac, buf, sizeof(buf)),
2416 filter ? "prefix-routes-only" : "none");
2417
2418 if (cmd == ZEBRA_L3VNI_ADD)
2419 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
2420 filter);
2421 else
2422 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
2423
2424 return 0;
2425 }
2426
2427 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
2428 zebra_size_t length, vrf_id_t vrf_id)
2429 {
2430 struct stream *s;
2431 vni_t vni;
2432 struct bgp *bgp;
2433 struct in_addr vtep_ip = {INADDR_ANY};
2434 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
2435
2436 s = zclient->ibuf;
2437 vni = stream_getl(s);
2438 if (command == ZEBRA_VNI_ADD) {
2439 vtep_ip.s_addr = stream_get_ipv4(s);
2440 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
2441 }
2442
2443 bgp = bgp_lookup_by_vrf_id(vrf_id);
2444 if (!bgp)
2445 return 0;
2446
2447 if (BGP_DEBUG(zebra, ZEBRA))
2448 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
2449 (command == ZEBRA_VNI_ADD) ? "add" : "del",
2450 vrf_id_to_name(vrf_id), vni,
2451 vrf_id_to_name(tenant_vrf_id));
2452
2453 if (command == ZEBRA_VNI_ADD)
2454 return bgp_evpn_local_vni_add(
2455 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
2456 tenant_vrf_id);
2457 else
2458 return bgp_evpn_local_vni_del(bgp, vni);
2459 }
2460
2461 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
2462 zebra_size_t length, vrf_id_t vrf_id)
2463 {
2464 struct stream *s;
2465 vni_t vni;
2466 struct bgp *bgp;
2467 struct ethaddr mac;
2468 struct ipaddr ip;
2469 int ipa_len;
2470 char buf[ETHER_ADDR_STRLEN];
2471 char buf1[INET6_ADDRSTRLEN];
2472 uint8_t flags = 0;
2473 uint32_t seqnum = 0;
2474
2475 memset(&ip, 0, sizeof(ip));
2476 s = zclient->ibuf;
2477 vni = stream_getl(s);
2478 stream_get(&mac.octet, s, ETH_ALEN);
2479 ipa_len = stream_getl(s);
2480 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
2481 && ipa_len != IPV6_MAX_BYTELEN) {
2482 flog_err(EC_BGP_MACIP_LEN,
2483 "%u:Recv MACIP %s with invalid IP addr length %d",
2484 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2485 ipa_len);
2486 return -1;
2487 }
2488
2489 if (ipa_len) {
2490 ip.ipa_type =
2491 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
2492 stream_get(&ip.ip.addr, s, ipa_len);
2493 }
2494 if (command == ZEBRA_MACIP_ADD) {
2495 flags = stream_getc(s);
2496 seqnum = stream_getl(s);
2497 }
2498
2499 bgp = bgp_lookup_by_vrf_id(vrf_id);
2500 if (!bgp)
2501 return 0;
2502
2503 if (BGP_DEBUG(zebra, ZEBRA))
2504 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u seq %u",
2505 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
2506 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
2507 ipaddr2str(&ip, buf1, sizeof(buf1)), vni, seqnum);
2508
2509 if (command == ZEBRA_MACIP_ADD)
2510 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip,
2511 flags, seqnum);
2512 else
2513 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
2514 }
2515
2516 static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
2517 zebra_size_t length,
2518 vrf_id_t vrf_id)
2519 {
2520 struct stream *s = NULL;
2521 struct bgp *bgp_vrf = NULL;
2522 struct prefix p;
2523 char buf[PREFIX_STRLEN];
2524
2525 memset(&p, 0, sizeof(struct prefix));
2526 s = zclient->ibuf;
2527 stream_get(&p, s, sizeof(struct prefix));
2528
2529 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
2530 if (!bgp_vrf)
2531 return;
2532
2533 if (BGP_DEBUG(zebra, ZEBRA))
2534 zlog_debug("Recv prefix %s %s on vrf %s",
2535 prefix2str(&p, buf, sizeof(buf)),
2536 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
2537 vrf_id_to_name(vrf_id));
2538
2539 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
2540
2541 if (p.family == AF_INET)
2542 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
2543 AFI_IP, SAFI_UNICAST);
2544 else
2545 bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL,
2546 AFI_IP6, SAFI_UNICAST);
2547
2548 } else {
2549 if (p.family == AF_INET)
2550 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP,
2551 SAFI_UNICAST);
2552 else
2553 bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6,
2554 SAFI_UNICAST);
2555 }
2556 }
2557
2558 static void bgp_zebra_process_label_chunk(
2559 int cmd,
2560 struct zclient *zclient,
2561 zebra_size_t length,
2562 vrf_id_t vrf_id)
2563 {
2564 struct stream *s = NULL;
2565 uint8_t response_keep;
2566 uint32_t first;
2567 uint32_t last;
2568 uint8_t proto;
2569 unsigned short instance;
2570
2571 s = zclient->ibuf;
2572 STREAM_GETC(s, proto);
2573 STREAM_GETW(s, instance);
2574 STREAM_GETC(s, response_keep);
2575 STREAM_GETL(s, first);
2576 STREAM_GETL(s, last);
2577
2578 if (zclient->redist_default != proto) {
2579 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u",
2580 proto);
2581 return;
2582 }
2583 if (zclient->instance != instance) {
2584 flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u",
2585 proto);
2586 return;
2587 }
2588
2589 if (first > last ||
2590 first < MPLS_LABEL_UNRESERVED_MIN ||
2591 last > MPLS_LABEL_UNRESERVED_MAX) {
2592
2593 flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u",
2594 __func__, first, last);
2595 return;
2596 }
2597 if (BGP_DEBUG(zebra, ZEBRA)) {
2598 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2599 first, last, response_keep);
2600 }
2601
2602 bgp_lp_event_chunk(response_keep, first, last);
2603
2604 stream_failure: /* for STREAM_GETX */
2605 return;
2606 }
2607
2608 extern struct zebra_privs_t bgpd_privs;
2609
2610 void bgp_zebra_init(struct thread_master *master, unsigned short instance)
2611 {
2612 zclient_num_connects = 0;
2613
2614 /* Set default values. */
2615 zclient = zclient_new(master, &zclient_options_default);
2616 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
2617 zclient->zebra_connected = bgp_zebra_connected;
2618 zclient->router_id_update = bgp_router_id_update;
2619 zclient->interface_add = bgp_interface_add;
2620 zclient->interface_delete = bgp_interface_delete;
2621 zclient->interface_address_add = bgp_interface_address_add;
2622 zclient->interface_address_delete = bgp_interface_address_delete;
2623 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2624 zclient->interface_nbr_address_delete =
2625 bgp_interface_nbr_address_delete;
2626 zclient->interface_vrf_update = bgp_interface_vrf_update;
2627 zclient->redistribute_route_add = zebra_read_route;
2628 zclient->redistribute_route_del = zebra_read_route;
2629 zclient->interface_up = bgp_interface_up;
2630 zclient->interface_down = bgp_interface_down;
2631 zclient->nexthop_update = bgp_read_nexthop_update;
2632 zclient->import_check_update = bgp_read_import_check_update;
2633 zclient->fec_update = bgp_read_fec_update;
2634 zclient->local_es_add = bgp_zebra_process_local_es;
2635 zclient->local_es_del = bgp_zebra_process_local_es;
2636 zclient->local_vni_add = bgp_zebra_process_local_vni;
2637 zclient->local_vni_del = bgp_zebra_process_local_vni;
2638 zclient->local_macip_add = bgp_zebra_process_local_macip;
2639 zclient->local_macip_del = bgp_zebra_process_local_macip;
2640 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2641 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
2642 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2643 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
2644 zclient->label_chunk = bgp_zebra_process_label_chunk;
2645 zclient->rule_notify_owner = rule_notify_owner;
2646 zclient->ipset_notify_owner = ipset_notify_owner;
2647 zclient->ipset_entry_notify_owner = ipset_entry_notify_owner;
2648 zclient->iptable_notify_owner = iptable_notify_owner;
2649 zclient->instance = instance;
2650 }
2651
2652 void bgp_zebra_destroy(void)
2653 {
2654 if (zclient == NULL)
2655 return;
2656 zclient_stop(zclient);
2657 zclient_free(zclient);
2658 zclient = NULL;
2659 }
2660
2661 int bgp_zebra_num_connects(void)
2662 {
2663 return zclient_num_connects;
2664 }
2665
2666 void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra, bool install)
2667 {
2668 struct stream *s;
2669
2670 if (pbra->install_in_progress)
2671 return;
2672 if (BGP_DEBUG(zebra, ZEBRA))
2673 zlog_debug("%s: table %d fwmark %d %d",
2674 __PRETTY_FUNCTION__,
2675 pbra->table_id, pbra->fwmark, install);
2676 s = zclient->obuf;
2677 stream_reset(s);
2678
2679 zclient_create_header(s,
2680 install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE,
2681 VRF_DEFAULT);
2682 stream_putl(s, 1); /* send one pbr action */
2683
2684 bgp_encode_pbr_rule_action(s, pbra);
2685
2686 stream_putw_at(s, 0, stream_get_endp(s));
2687 if (!zclient_send_message(zclient) && install)
2688 pbra->install_in_progress = true;
2689 }
2690
2691 void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install)
2692 {
2693 struct stream *s;
2694
2695 if (pbrim->install_in_progress)
2696 return;
2697 if (BGP_DEBUG(zebra, ZEBRA))
2698 zlog_debug("%s: name %s type %d %d, ID %u",
2699 __PRETTY_FUNCTION__,
2700 pbrim->ipset_name, pbrim->type,
2701 install, pbrim->unique);
2702 s = zclient->obuf;
2703 stream_reset(s);
2704
2705 zclient_create_header(s,
2706 install ? ZEBRA_IPSET_CREATE :
2707 ZEBRA_IPSET_DESTROY,
2708 VRF_DEFAULT);
2709
2710 stream_putl(s, 1); /* send one pbr action */
2711
2712 bgp_encode_pbr_ipset_match(s, pbrim);
2713
2714 stream_putw_at(s, 0, stream_get_endp(s));
2715 if (!zclient_send_message(zclient) && install)
2716 pbrim->install_in_progress = true;
2717 }
2718
2719 void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime,
2720 bool install)
2721 {
2722 struct stream *s;
2723
2724 if (pbrime->install_in_progress)
2725 return;
2726 if (BGP_DEBUG(zebra, ZEBRA))
2727 zlog_debug("%s: name %s %d %d, ID %u", __PRETTY_FUNCTION__,
2728 pbrime->backpointer->ipset_name,
2729 pbrime->unique, install, pbrime->unique);
2730 s = zclient->obuf;
2731 stream_reset(s);
2732
2733 zclient_create_header(s,
2734 install ? ZEBRA_IPSET_ENTRY_ADD :
2735 ZEBRA_IPSET_ENTRY_DELETE,
2736 VRF_DEFAULT);
2737
2738 stream_putl(s, 1); /* send one pbr action */
2739
2740 bgp_encode_pbr_ipset_entry_match(s, pbrime);
2741
2742 stream_putw_at(s, 0, stream_get_endp(s));
2743 if (!zclient_send_message(zclient) && install)
2744 pbrime->install_in_progress = true;
2745 }
2746
2747 static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s)
2748 {
2749 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2750 struct bgp_pbr_interface_head *head;
2751 struct bgp_pbr_interface *pbr_if;
2752 struct interface *ifp;
2753
2754 if (!bgp_pbr_cfg)
2755 return;
2756 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2757
2758 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
2759 ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id);
2760 if (ifp)
2761 stream_putl(s, ifp->ifindex);
2762 }
2763 }
2764
2765 static int bgp_pbr_get_ifnumber(struct bgp *bgp)
2766 {
2767 struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg;
2768 struct bgp_pbr_interface_head *head;
2769 struct bgp_pbr_interface *pbr_if;
2770 int cnt = 0;
2771
2772 if (!bgp_pbr_cfg)
2773 return 0;
2774 head = &(bgp_pbr_cfg->ifaces_by_name_ipv4);
2775
2776 RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) {
2777 if (if_lookup_by_name(pbr_if->name, bgp->vrf_id))
2778 cnt++;
2779 }
2780 return cnt;
2781 }
2782
2783 void bgp_send_pbr_iptable(struct bgp_pbr_action *pba,
2784 struct bgp_pbr_match *pbm,
2785 bool install)
2786 {
2787 struct stream *s;
2788 int ret = 0;
2789 int nb_interface;
2790
2791 if (pbm->install_iptable_in_progress)
2792 return;
2793 if (BGP_DEBUG(zebra, ZEBRA))
2794 zlog_debug("%s: name %s type %d mark %d %d, ID %u",
2795 __PRETTY_FUNCTION__, pbm->ipset_name,
2796 pbm->type, pba->fwmark, install,
2797 pbm->unique2);
2798 s = zclient->obuf;
2799 stream_reset(s);
2800
2801 zclient_create_header(s,
2802 install ? ZEBRA_IPTABLE_ADD :
2803 ZEBRA_IPTABLE_DELETE,
2804 VRF_DEFAULT);
2805
2806 bgp_encode_pbr_iptable_match(s, pba, pbm);
2807 nb_interface = bgp_pbr_get_ifnumber(pba->bgp);
2808 stream_putl(s, nb_interface);
2809 if (nb_interface)
2810 bgp_encode_pbr_interface_list(pba->bgp, s);
2811 stream_putw_at(s, 0, stream_get_endp(s));
2812 ret = zclient_send_message(zclient);
2813 if (install) {
2814 if (ret)
2815 pba->refcnt++;
2816 else
2817 pbm->install_iptable_in_progress = true;
2818 }
2819 }
2820
2821 /* inject in table <table_id> a default route to:
2822 * - if nexthop IP is present : to this nexthop
2823 * - if vrf is different from local : to the matching VRF
2824 */
2825 void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh,
2826 afi_t afi, uint32_t table_id, bool announce)
2827 {
2828 struct zapi_nexthop *api_nh;
2829 struct zapi_route api;
2830 struct prefix p;
2831
2832 if (!nh || nh->type != NEXTHOP_TYPE_IPV4
2833 || nh->vrf_id == VRF_UNKNOWN)
2834 return;
2835 memset(&p, 0, sizeof(struct prefix));
2836 /* default route */
2837 if (afi != AFI_IP)
2838 return;
2839 p.family = AF_INET;
2840 memset(&api, 0, sizeof(api));
2841 api.vrf_id = bgp->vrf_id;
2842 api.type = ZEBRA_ROUTE_BGP;
2843 api.safi = SAFI_UNICAST;
2844 api.prefix = p;
2845 api.tableid = table_id;
2846 api.nexthop_num = 1;
2847 SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID);
2848 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
2849 api_nh = &api.nexthops[0];
2850
2851 /* redirect IP */
2852 if (nh->gate.ipv4.s_addr) {
2853 char buff[PREFIX_STRLEN];
2854
2855 api_nh->vrf_id = nh->vrf_id;
2856 api_nh->gate.ipv4 = nh->gate.ipv4;
2857 api_nh->type = NEXTHOP_TYPE_IPV4;
2858
2859 inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN);
2860 if (BGP_DEBUG(zebra, ZEBRA))
2861 zlog_info("BGP: %s default route to %s table %d (redirect IP)",
2862 announce ? "adding" : "withdrawing",
2863 buff, table_id);
2864 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
2865 : ZEBRA_ROUTE_DELETE,
2866 zclient, &api);
2867 } else if (nh->vrf_id != bgp->vrf_id) {
2868 struct vrf *vrf;
2869 struct interface *ifp;
2870
2871 vrf = vrf_lookup_by_id(nh->vrf_id);
2872 if (!vrf)
2873 return;
2874 /* create default route with interface <VRF>
2875 * with nexthop-vrf <VRF>
2876 */
2877 ifp = if_lookup_by_name_all_vrf(vrf->name);
2878 if (!ifp)
2879 return;
2880 api_nh->vrf_id = nh->vrf_id;
2881 api_nh->type = NEXTHOP_TYPE_IFINDEX;
2882 api_nh->ifindex = ifp->ifindex;
2883 if (BGP_DEBUG(zebra, ZEBRA))
2884 zlog_info("BGP: %s default route to %s table %d (redirect VRF)",
2885 announce ? "adding" : "withdrawing",
2886 vrf->name, table_id);
2887 zclient_route_send(announce ? ZEBRA_ROUTE_ADD
2888 : ZEBRA_ROUTE_DELETE,
2889 zclient, &api);
2890 return;
2891 }
2892 }