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