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