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