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