]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge pull request #2111 from mjstapp/zmq_sockopt
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "zclient.h"
30 #include "routemap.h"
31 #include "thread.h"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35 #include "lib/bfd.h"
36 #include "filter.h"
37 #include "mpls.h"
38 #include "vxlan.h"
39
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_nexthop.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_mpath.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_label.h"
52 #if ENABLE_BGP_VNC
53 #include "bgpd/rfapi/rfapi_backend.h"
54 #include "bgpd/rfapi/vnc_export_bgp.h"
55 #endif
56 #include "bgpd/bgp_evpn.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_labelpool.h"
59
60 /* All information about zebra. */
61 struct zclient *zclient = NULL;
62
63 /* Can we install into zebra? */
64 static inline int bgp_install_info_to_zebra(struct bgp *bgp)
65 {
66 if (zclient->sock <= 0)
67 return 0;
68
69 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
70 return 0;
71
72 return 1;
73 }
74
75 int zclient_num_connects;
76
77 /* Router-id update message from zebra. */
78 static int bgp_router_id_update(int command, struct zclient *zclient,
79 zebra_size_t length, vrf_id_t vrf_id)
80 {
81 struct prefix router_id;
82
83 zebra_router_id_update_read(zclient->ibuf, &router_id);
84
85 if (BGP_DEBUG(zebra, ZEBRA)) {
86 char buf[PREFIX2STR_BUFFER];
87 prefix2str(&router_id, buf, sizeof(buf));
88 zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
89 }
90
91 bgp_router_id_zebra_bump(vrf_id, &router_id);
92 return 0;
93 }
94
95 /* Nexthop update message from zebra. */
96 static int bgp_read_nexthop_update(int command, struct zclient *zclient,
97 zebra_size_t length, vrf_id_t vrf_id)
98 {
99 bgp_parse_nexthop_update(command, vrf_id);
100 return 0;
101 }
102
103 static int bgp_read_import_check_update(int command, struct zclient *zclient,
104 zebra_size_t length, vrf_id_t vrf_id)
105 {
106 bgp_parse_nexthop_update(command, vrf_id);
107 return 0;
108 }
109
110 /* Set or clear interface on which unnumbered neighbor is configured. This
111 * would in turn cause BGP to initiate or turn off IPv6 RAs on this
112 * interface.
113 */
114 static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp,
115 struct interface *upd_ifp)
116 {
117 struct listnode *node, *nnode;
118 struct peer *peer;
119
120 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
121 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) {
122 if (upd_ifp) {
123 peer->ifp = upd_ifp;
124 bgp_zebra_initiate_radv(bgp, peer);
125 } else {
126 bgp_zebra_terminate_radv(bgp, peer);
127 peer->ifp = upd_ifp;
128 }
129 }
130 }
131 }
132
133 static int bgp_read_fec_update(int command, struct zclient *zclient,
134 zebra_size_t length)
135 {
136 bgp_parse_fec_update();
137 return 0;
138 }
139
140 static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp)
141 {
142 struct listnode *node, *nnode;
143 struct peer *peer;
144
145 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
146 if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)
147 && peer->status != Established) {
148 if (peer_active(peer))
149 BGP_EVENT_ADD(peer, BGP_Stop);
150 BGP_EVENT_ADD(peer, BGP_Start);
151 }
152 }
153 }
154
155 static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc)
156 {
157 struct listnode *node;
158 struct connected *connected;
159 struct interface *ifp;
160 struct prefix *p;
161
162 /* Kick-off the FSM for any relevant peers only if there is a
163 * valid local address on the interface.
164 */
165 ifp = ifc->ifp;
166 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
167 p = connected->address;
168 if (p->family == AF_INET6
169 && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
170 break;
171 }
172 if (!connected)
173 return;
174
175 bgp_start_interface_nbrs(bgp, ifp);
176 }
177
178 static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc,
179 int del)
180 {
181 struct listnode *node, *nnode;
182 struct peer *peer;
183 struct interface *ifp;
184
185 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
186 if (peer->conf_if
187 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) {
188 peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
189 BGP_EVENT_ADD(peer, BGP_Stop);
190 }
191 }
192 /* Free neighbor also, if we're asked to. */
193 if (del) {
194 ifp = ifc->ifp;
195 listnode_delete(ifp->nbr_connected, ifc);
196 nbr_connected_free(ifc);
197 }
198 }
199
200 /* Inteface addition message from zebra. */
201 static int bgp_interface_add(int command, struct zclient *zclient,
202 zebra_size_t length, vrf_id_t vrf_id)
203 {
204 struct interface *ifp;
205 struct bgp *bgp;
206
207 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
208 if (!ifp) // unexpected
209 return 0;
210
211 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
212 zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
213
214 bgp = bgp_lookup_by_vrf_id(vrf_id);
215 if (!bgp)
216 return 0;
217
218 bgp_update_interface_nbrs(bgp, ifp, ifp);
219 return 0;
220 }
221
222 static int bgp_interface_delete(int command, struct zclient *zclient,
223 zebra_size_t length, vrf_id_t vrf_id)
224 {
225 struct stream *s;
226 struct interface *ifp;
227 struct bgp *bgp;
228
229 bgp = bgp_lookup_by_vrf_id(vrf_id);
230 if (!bgp)
231 return 0;
232
233 s = zclient->ibuf;
234 ifp = zebra_interface_state_read(s, vrf_id);
235 if (!ifp) /* This may happen if we've just unregistered for a VRF. */
236 return 0;
237
238 if (BGP_DEBUG(zebra, ZEBRA))
239 zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
240
241 bgp_update_interface_nbrs(bgp, ifp, NULL);
242
243 if_set_index(ifp, IFINDEX_INTERNAL);
244 return 0;
245 }
246
247 static int bgp_interface_up(int command, struct zclient *zclient,
248 zebra_size_t length, vrf_id_t vrf_id)
249 {
250 struct stream *s;
251 struct interface *ifp;
252 struct connected *c;
253 struct nbr_connected *nc;
254 struct listnode *node, *nnode;
255 struct bgp *bgp;
256
257 bgp = bgp_lookup_by_vrf_id(vrf_id);
258 if (!bgp)
259 return 0;
260
261 s = zclient->ibuf;
262 ifp = zebra_interface_state_read(s, vrf_id);
263
264 if (!ifp)
265 return 0;
266
267 if (BGP_DEBUG(zebra, ZEBRA))
268 zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
269
270 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
271 bgp_connected_add(bgp, c);
272
273 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
274 bgp_nbr_connected_add(bgp, nc);
275
276 return 0;
277 }
278
279 static int bgp_interface_down(int command, struct zclient *zclient,
280 zebra_size_t length, vrf_id_t vrf_id)
281 {
282 struct stream *s;
283 struct interface *ifp;
284 struct connected *c;
285 struct nbr_connected *nc;
286 struct listnode *node, *nnode;
287 struct bgp *bgp;
288
289 bgp = bgp_lookup_by_vrf_id(vrf_id);
290 if (!bgp)
291 return 0;
292
293 s = zclient->ibuf;
294 ifp = zebra_interface_state_read(s, vrf_id);
295 if (!ifp)
296 return 0;
297
298 if (BGP_DEBUG(zebra, ZEBRA))
299 zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
300
301 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
302 bgp_connected_delete(bgp, c);
303
304 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
305 bgp_nbr_connected_delete(bgp, nc, 1);
306
307 /* Fast external-failover */
308 {
309 struct peer *peer;
310
311 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
312 return 0;
313
314 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
315 #if defined(HAVE_CUMULUS)
316 /* Take down directly connected EBGP peers as well as
317 * 1-hop BFD
318 * tracked (directly connected) IBGP peers.
319 */
320 if ((peer->ttl != 1) && (peer->gtsm_hops != 1)
321 && (!peer->bfd_info
322 || bgp_bfd_is_peer_multihop(peer)))
323 #else
324 /* Take down directly connected EBGP peers */
325 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
326 #endif
327 continue;
328
329 if (ifp == peer->nexthop.ifp) {
330 BGP_EVENT_ADD(peer, BGP_Stop);
331 peer->last_reset = PEER_DOWN_IF_DOWN;
332 }
333 }
334 }
335
336 return 0;
337 }
338
339 static int bgp_interface_address_add(int command, struct zclient *zclient,
340 zebra_size_t length, vrf_id_t vrf_id)
341 {
342 struct connected *ifc;
343 struct bgp *bgp;
344
345 bgp = bgp_lookup_by_vrf_id(vrf_id);
346 if (!bgp)
347 return 0;
348
349 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
350
351 if (ifc == NULL)
352 return 0;
353
354 if (bgp_debug_zebra(ifc->address)) {
355 char buf[PREFIX2STR_BUFFER];
356 prefix2str(ifc->address, buf, sizeof(buf));
357 zlog_debug("Rx Intf address add VRF %u IF %s addr %s", vrf_id,
358 ifc->ifp->name, buf);
359 }
360
361 if (if_is_operative(ifc->ifp)) {
362 bgp_connected_add(bgp, ifc);
363
364 /* If we have learnt of any neighbors on this interface,
365 * check to kick off any BGP interface-based neighbors,
366 * but only if this is a link-local address.
367 */
368 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)
369 && !list_isempty(ifc->ifp->nbr_connected))
370 bgp_start_interface_nbrs(bgp, ifc->ifp);
371 }
372
373 return 0;
374 }
375
376 static int bgp_interface_address_delete(int command, struct zclient *zclient,
377 zebra_size_t length, vrf_id_t vrf_id)
378 {
379 struct connected *ifc;
380 struct bgp *bgp;
381
382 bgp = bgp_lookup_by_vrf_id(vrf_id);
383 if (!bgp)
384 return 0;
385
386 ifc = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
387
388 if (ifc == NULL)
389 return 0;
390
391 if (bgp_debug_zebra(ifc->address)) {
392 char buf[PREFIX2STR_BUFFER];
393 prefix2str(ifc->address, buf, sizeof(buf));
394 zlog_debug("Rx Intf address del VRF %u IF %s addr %s", vrf_id,
395 ifc->ifp->name, buf);
396 }
397
398 if (if_is_operative(ifc->ifp)) {
399 bgp_connected_delete(bgp, ifc);
400 }
401
402 connected_free(ifc);
403
404 return 0;
405 }
406
407 static int bgp_interface_nbr_address_add(int command, struct zclient *zclient,
408 zebra_size_t length, vrf_id_t vrf_id)
409 {
410 struct nbr_connected *ifc = NULL;
411 struct bgp *bgp;
412
413 ifc = zebra_interface_nbr_address_read(command, zclient->ibuf, vrf_id);
414
415 if (ifc == NULL)
416 return 0;
417
418 if (bgp_debug_zebra(ifc->address)) {
419 char buf[PREFIX2STR_BUFFER];
420 prefix2str(ifc->address, buf, sizeof(buf));
421 zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s", vrf_id,
422 ifc->ifp->name, buf);
423 }
424
425 if (if_is_operative(ifc->ifp)) {
426 bgp = bgp_lookup_by_vrf_id(vrf_id);
427 if (bgp)
428 bgp_nbr_connected_add(bgp, ifc);
429 }
430
431 return 0;
432 }
433
434 static int bgp_interface_nbr_address_delete(int command,
435 struct zclient *zclient,
436 zebra_size_t length,
437 vrf_id_t vrf_id)
438 {
439 struct nbr_connected *ifc = NULL;
440 struct bgp *bgp;
441
442 ifc = zebra_interface_nbr_address_read(command, 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(int command, struct zclient *zclient,
467 zebra_size_t length, vrf_id_t vrf_id)
468 {
469 struct interface *ifp;
470 vrf_id_t new_vrf_id;
471 struct connected *c;
472 struct nbr_connected *nc;
473 struct listnode *node, *nnode;
474 struct bgp *bgp;
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 if (!bgp)
487 return 0;
488
489 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
490 bgp_connected_delete(bgp, c);
491
492 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
493 bgp_nbr_connected_delete(bgp, nc, 1);
494
495 /* Fast external-failover */
496 {
497 struct peer *peer;
498
499 if (CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
500 return 0;
501
502 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
503 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
504 continue;
505
506 if (ifp == peer->nexthop.ifp)
507 BGP_EVENT_ADD(peer, BGP_Stop);
508 }
509 }
510
511 if_update_to_new_vrf(ifp, new_vrf_id);
512
513 bgp = bgp_lookup_by_vrf_id(new_vrf_id);
514 if (!bgp)
515 return 0;
516
517 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c))
518 bgp_connected_add(bgp, c);
519
520 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc))
521 bgp_nbr_connected_add(bgp, nc);
522 return 0;
523 }
524
525 /* Zebra route add and delete treatment. */
526 static int zebra_read_route(int command, struct zclient *zclient,
527 zebra_size_t length, vrf_id_t vrf_id)
528 {
529 enum nexthop_types_t nhtype;
530 struct zapi_route api;
531 union g_addr nexthop;
532 ifindex_t ifindex;
533 int add, i;
534 struct bgp *bgp;
535
536 bgp = bgp_lookup_by_vrf_id(vrf_id);
537 if (!bgp)
538 return 0;
539
540 if (zapi_route_decode(zclient->ibuf, &api) < 0)
541 return -1;
542
543 /* we completely ignore srcdest routes for now. */
544 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
545 return 0;
546
547 /* ignore link-local address. */
548 if (api.prefix.family == AF_INET6
549 && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
550 return 0;
551
552 nexthop = api.nexthops[0].gate;
553 ifindex = api.nexthops[0].ifindex;
554 nhtype = api.nexthops[0].type;
555
556 add = (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
557 if (add) {
558 /*
559 * The ADD message is actually an UPDATE and there is no
560 * explicit DEL
561 * for a prior redistributed route, if any. So, perform an
562 * implicit
563 * DEL processing for the same redistributed route from any
564 * other
565 * source type.
566 */
567 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
568 if (i != api.type)
569 bgp_redistribute_delete(bgp, &api.prefix, i,
570 api.instance);
571 }
572
573 /* Now perform the add/update. */
574 bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex,
575 nhtype, api.metric, api.type, api.instance,
576 api.tag);
577 } else {
578 bgp_redistribute_delete(bgp, &api.prefix, api.type,
579 api.instance);
580 }
581
582 if (bgp_debug_zebra(&api.prefix)) {
583 char buf[2][PREFIX_STRLEN];
584
585 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
586 inet_ntop(api.prefix.family, &nexthop, buf[1], sizeof(buf[1]));
587 zlog_debug(
588 "Rx route %s VRF %u %s[%d] %s "
589 "nexthop %s metric %u tag %" ROUTE_TAG_PRI,
590 (add) ? "add" : "delete", vrf_id,
591 zebra_route_string(api.type), api.instance, buf[0],
592 buf[1], api.metric, api.tag);
593 }
594
595 return 0;
596 }
597
598 struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id)
599 {
600 struct vrf *vrf;
601 struct listnode *cnode;
602 struct interface *ifp;
603 struct connected *connected;
604 struct prefix_ipv4 p;
605 struct prefix *cp;
606
607 vrf = vrf_lookup_by_id(vrf_id);
608 if (!vrf)
609 return NULL;
610
611 p.family = AF_INET;
612 p.prefix = *addr;
613 p.prefixlen = IPV4_MAX_BITLEN;
614
615 FOR_ALL_INTERFACES (vrf, ifp) {
616 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
617 cp = connected->address;
618
619 if (cp->family == AF_INET)
620 if (prefix_match(cp, (struct prefix *)&p))
621 return ifp;
622 }
623 }
624 return NULL;
625 }
626
627 struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id)
628 {
629 struct vrf *vrf;
630 struct listnode *cnode;
631 struct interface *ifp;
632 struct connected *connected;
633 struct prefix *cp;
634
635 vrf = vrf_lookup_by_id(vrf_id);
636 if (!vrf)
637 return NULL;
638
639 FOR_ALL_INTERFACES (vrf, ifp) {
640 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
641 cp = connected->address;
642
643 if (cp->family == AF_INET)
644 if (IPV4_ADDR_SAME(&cp->u.prefix4, addr))
645 return ifp;
646 }
647 }
648 return NULL;
649 }
650
651 struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex,
652 vrf_id_t vrf_id)
653 {
654 struct vrf *vrf;
655 struct listnode *cnode;
656 struct interface *ifp;
657 struct connected *connected;
658 struct prefix_ipv6 p;
659 struct prefix *cp;
660
661 vrf = vrf_lookup_by_id(vrf_id);
662 if (!vrf)
663 return NULL;
664
665 p.family = AF_INET6;
666 p.prefix = *addr;
667 p.prefixlen = IPV6_MAX_BITLEN;
668
669 FOR_ALL_INTERFACES (vrf, ifp) {
670 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
671 cp = connected->address;
672
673 if (cp->family == AF_INET6)
674 if (prefix_match(cp, (struct prefix *)&p)) {
675 if (IN6_IS_ADDR_LINKLOCAL(
676 &cp->u.prefix6)) {
677 if (ifindex == ifp->ifindex)
678 return ifp;
679 } else
680 return ifp;
681 }
682 }
683 }
684 return NULL;
685 }
686
687 struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr,
688 ifindex_t ifindex, vrf_id_t vrf_id)
689 {
690 struct vrf *vrf;
691 struct listnode *cnode;
692 struct interface *ifp;
693 struct connected *connected;
694 struct prefix *cp;
695
696 vrf = vrf_lookup_by_id(vrf_id);
697 if (!vrf)
698 return NULL;
699
700 FOR_ALL_INTERFACES (vrf, ifp) {
701 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
702 cp = connected->address;
703
704 if (cp->family == AF_INET6)
705 if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) {
706 if (IN6_IS_ADDR_LINKLOCAL(
707 &cp->u.prefix6)) {
708 if (ifindex == ifp->ifindex)
709 return ifp;
710 } else
711 return ifp;
712 }
713 }
714 }
715 return NULL;
716 }
717
718 static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr)
719 {
720 struct listnode *cnode;
721 struct connected *connected;
722 struct prefix *cp;
723
724 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
725 cp = connected->address;
726
727 if (cp->family == AF_INET6)
728 if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
729 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
730 return 1;
731 }
732 }
733 return 0;
734 }
735
736 static int if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr)
737 {
738 struct listnode *cnode;
739 struct connected *connected;
740 struct prefix *cp;
741
742 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
743 cp = connected->address;
744
745 if (cp->family == AF_INET6)
746 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) {
747 memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
748 return 1;
749 }
750 }
751 return 0;
752 }
753
754 static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr)
755 {
756 struct listnode *cnode;
757 struct connected *connected;
758 struct prefix *cp;
759
760 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) {
761 cp = connected->address;
762 if ((cp->family == AF_INET)
763 && !ipv4_martian(&(cp->u.prefix4))) {
764 *addr = cp->u.prefix4;
765 return 1;
766 }
767 }
768 return 0;
769 }
770
771 int bgp_nexthop_set(union sockunion *local, union sockunion *remote,
772 struct bgp_nexthop *nexthop, struct peer *peer)
773 {
774 int ret = 0;
775 struct interface *ifp = NULL;
776
777 memset(nexthop, 0, sizeof(struct bgp_nexthop));
778
779 if (!local)
780 return -1;
781 if (!remote)
782 return -1;
783
784 if (local->sa.sa_family == AF_INET) {
785 nexthop->v4 = local->sin.sin_addr;
786 if (peer->update_if)
787 ifp = if_lookup_by_name(peer->update_if,
788 peer->bgp->vrf_id);
789 else
790 ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr,
791 peer->bgp->vrf_id);
792 }
793 if (local->sa.sa_family == AF_INET6) {
794 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
795 if (peer->conf_if || peer->ifname)
796 ifp = if_lookup_by_name(peer->conf_if
797 ? peer->conf_if
798 : peer->ifname,
799 peer->bgp->vrf_id);
800 } else if (peer->update_if)
801 ifp = if_lookup_by_name(peer->update_if,
802 peer->bgp->vrf_id);
803 else
804 ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
805 local->sin6.sin6_scope_id,
806 peer->bgp->vrf_id);
807 }
808
809 if (!ifp)
810 return -1;
811
812 nexthop->ifp = ifp;
813
814 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
815 if (local->sa.sa_family == AF_INET) {
816 /* IPv6 nexthop*/
817 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
818
819 if (!ret) {
820 /* There is no global nexthop. Use link-local address as
821 * both the
822 * global and link-local nexthop. In this scenario, the
823 * expectation
824 * for interop is that the network admin would use a
825 * route-map to
826 * specify the global IPv6 nexthop.
827 */
828 if_get_ipv6_local(ifp, &nexthop->v6_global);
829 memcpy(&nexthop->v6_local, &nexthop->v6_global,
830 IPV6_MAX_BYTELEN);
831 } else
832 if_get_ipv6_local(ifp, &nexthop->v6_local);
833
834 if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id))
835 peer->shared_network = 1;
836 else
837 peer->shared_network = 0;
838 }
839
840 /* IPv6 connection, fetch and store IPv4 local address if any. */
841 if (local->sa.sa_family == AF_INET6) {
842 struct interface *direct = NULL;
843
844 /* IPv4 nexthop. */
845 ret = if_get_ipv4_address(ifp, &nexthop->v4);
846 if (!ret && peer->local_id.s_addr)
847 nexthop->v4 = peer->local_id;
848
849 /* Global address*/
850 if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) {
851 memcpy(&nexthop->v6_global, &local->sin6.sin6_addr,
852 IPV6_MAX_BYTELEN);
853
854 /* If directory connected set link-local address. */
855 direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr,
856 remote->sin6.sin6_scope_id,
857 peer->bgp->vrf_id);
858 if (direct)
859 if_get_ipv6_local(ifp, &nexthop->v6_local);
860 } else
861 /* Link-local address. */
862 {
863 ret = if_get_ipv6_global(ifp, &nexthop->v6_global);
864
865 /* If there is no global address. Set link-local
866 address as
867 global. I know this break RFC specification... */
868 /* In this scenario, the expectation for interop is that
869 * the
870 * network admin would use a route-map to specify the
871 * global
872 * IPv6 nexthop.
873 */
874 if (!ret)
875 memcpy(&nexthop->v6_global,
876 &local->sin6.sin6_addr,
877 IPV6_MAX_BYTELEN);
878 /* Always set the link-local address */
879 memcpy(&nexthop->v6_local, &local->sin6.sin6_addr,
880 IPV6_MAX_BYTELEN);
881 }
882
883 if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)
884 || if_lookup_by_ipv6(&remote->sin6.sin6_addr,
885 remote->sin6.sin6_scope_id,
886 peer->bgp->vrf_id))
887 peer->shared_network = 1;
888 else
889 peer->shared_network = 0;
890 }
891
892 /* KAME stack specific treatment. */
893 #ifdef KAME
894 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global)
895 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) {
896 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0);
897 }
898 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local)
899 && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) {
900 SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0);
901 }
902 #endif /* KAME */
903
904 /* If we have identified the local interface, there is no error for now.
905 */
906 return 0;
907 }
908
909 static struct in6_addr *bgp_info_to_ipv6_nexthop(struct bgp_info *info)
910 {
911 struct in6_addr *nexthop = NULL;
912
913 /* Only global address nexthop exists. */
914 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
915 nexthop = &info->attr->mp_nexthop_global;
916
917 /* If both global and link-local address present. */
918 if (info->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
919 /* Check if route-map is set to prefer global over link-local */
920 if (info->attr->mp_nexthop_prefer_global)
921 nexthop = &info->attr->mp_nexthop_global;
922 else {
923 /* Workaround for Cisco's nexthop bug. */
924 if (IN6_IS_ADDR_UNSPECIFIED(
925 &info->attr->mp_nexthop_global)
926 && info->peer->su_remote->sa.sa_family == AF_INET6)
927 nexthop =
928 &info->peer->su_remote->sin6.sin6_addr;
929 else
930 nexthop = &info->attr->mp_nexthop_local;
931 }
932 }
933
934 return nexthop;
935 }
936
937 static int bgp_table_map_apply(struct route_map *map, struct prefix *p,
938 struct bgp_info *info)
939 {
940 route_map_result_t ret;
941
942 ret = route_map_apply(map, p, RMAP_BGP, info);
943 bgp_attr_flush(info->attr);
944
945 if (ret != RMAP_DENYMATCH)
946 return 1;
947
948 if (bgp_debug_zebra(p)) {
949 if (p->family == AF_INET) {
950 char buf[2][INET_ADDRSTRLEN];
951 zlog_debug(
952 "Zebra rmap deny: IPv4 route %s/%d nexthop %s",
953 inet_ntop(AF_INET, &p->u.prefix4, buf[0],
954 sizeof(buf[0])),
955 p->prefixlen,
956 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
957 sizeof(buf[1])));
958 }
959 if (p->family == AF_INET6) {
960 char buf[2][INET6_ADDRSTRLEN];
961 zlog_debug(
962 "Zebra rmap deny: IPv6 route %s/%d nexthop %s",
963 inet_ntop(AF_INET6, &p->u.prefix6, buf[0],
964 sizeof(buf[0])),
965 p->prefixlen,
966 inet_ntop(AF_INET6,
967 bgp_info_to_ipv6_nexthop(info),
968 buf[1], sizeof(buf[1])));
969 }
970 }
971 return 0;
972 }
973
974 static struct thread *bgp_tm_thread_connect;
975 static bool bgp_tm_status_connected;
976
977 static int bgp_zebra_tm_connect(struct thread *t)
978 {
979 struct zclient *zclient;
980 int delay = 10, ret = 0;
981
982 zclient = THREAD_ARG(t);
983 if (bgp_tm_status_connected && zclient->sock > 0)
984 delay = 60;
985 else {
986 bgp_tm_status_connected = false;
987 ret = tm_table_manager_connect(zclient);
988 }
989 if (ret < 0) {
990 zlog_warn("Error connecting to table manager!");
991 bgp_tm_status_connected = false;
992 } else {
993 if (!bgp_tm_status_connected)
994 zlog_debug("Connecting to table manager. Success");
995 bgp_tm_status_connected = true;
996 }
997 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
998 &bgp_tm_thread_connect);
999 return 0;
1000 }
1001
1002 void bgp_zebra_init_tm_connect(void)
1003 {
1004 int delay = 1;
1005
1006 /* if already set, do nothing
1007 */
1008 if (bgp_tm_thread_connect != NULL)
1009 return;
1010 bgp_tm_status_connected = false;
1011 thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay,
1012 &bgp_tm_thread_connect);
1013 }
1014
1015 int bgp_zebra_get_table_range(uint32_t chunk_size,
1016 uint32_t *start, uint32_t *end)
1017 {
1018 int ret;
1019
1020 if (!bgp_tm_status_connected)
1021 return -1;
1022 ret = tm_get_table_chunk(zclient, chunk_size, start, end);
1023 if (ret < 0) {
1024 zlog_err("BGP: Error getting table chunk %u", chunk_size);
1025 return -1;
1026 }
1027 zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]",
1028 chunk_size, *start, *end);
1029 return 0;
1030 }
1031
1032 void bgp_zebra_announce(struct bgp_node *rn, struct prefix *p,
1033 struct bgp_info *info, struct bgp *bgp, afi_t afi,
1034 safi_t safi)
1035 {
1036 struct zapi_route api;
1037 struct zapi_nexthop *api_nh;
1038 int nh_family;
1039 unsigned int valid_nh_count = 0;
1040 int has_valid_label = 0;
1041 uint8_t distance;
1042 struct peer *peer;
1043 struct bgp_info *mpinfo;
1044 uint32_t metric;
1045 struct attr local_attr;
1046 struct bgp_info local_info;
1047 struct bgp_info *mpinfo_cp = &local_info;
1048 route_tag_t tag;
1049 mpls_label_t label;
1050 int nh_othervrf = 0;
1051 char buf_prefix[PREFIX_STRLEN]; /* filled in if we are debugging */
1052
1053 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1054 * know of this instance.
1055 */
1056 if (!bgp_install_info_to_zebra(bgp))
1057 return;
1058
1059 if (bgp->main_zebra_update_hold)
1060 return;
1061
1062 if (bgp_debug_zebra(p))
1063 prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
1064
1065 /*
1066 * vrf leaking support (will have only one nexthop)
1067 */
1068 if (info->extra && info->extra->bgp_orig)
1069 nh_othervrf = 1;
1070
1071 /* Make Zebra API structure. */
1072 memset(&api, 0, sizeof(api));
1073 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1074 api.vrf_id = bgp->vrf_id;
1075 api.type = ZEBRA_ROUTE_BGP;
1076 api.safi = safi;
1077 api.prefix = *p;
1078 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
1079
1080 peer = info->peer;
1081
1082 if (info->type == ZEBRA_ROUTE_BGP
1083 && info->sub_type == BGP_ROUTE_IMPORTED) {
1084
1085 struct bgp_info *bi;
1086
1087 /*
1088 * Look at parent chain for peer sort
1089 */
1090 for (bi = info; bi->extra && bi->extra->parent;
1091 bi = bi->extra->parent) {
1092
1093 peer = ((struct bgp_info *)(bi->extra->parent))->peer;
1094 }
1095 }
1096
1097 tag = info->attr->tag;
1098
1099 /* When we create an aggregate route we must also install a Null0 route
1100 * in
1101 * the RIB */
1102 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1103 zapi_route_set_blackhole(&api, BLACKHOLE_NULL);
1104
1105 /* If the route's source is EVPN, flag as such. */
1106 if (is_route_parent_evpn(info))
1107 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1108
1109 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED
1110 || info->sub_type == BGP_ROUTE_AGGREGATE) {
1111 SET_FLAG(api.flags, ZEBRA_FLAG_IBGP);
1112 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1113 }
1114
1115 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1116 || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1117 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1118
1119 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
1120
1121 /* Metric is currently based on the best-path only */
1122 metric = info->attr->med;
1123 for (mpinfo = info; mpinfo; mpinfo = bgp_info_mpath_next(mpinfo)) {
1124 if (valid_nh_count >= multipath_num)
1125 break;
1126
1127 *mpinfo_cp = *mpinfo;
1128
1129 /* Get nexthop address-family */
1130 if (p->family == AF_INET
1131 && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))
1132 nh_family = AF_INET;
1133 else if (p->family == AF_INET6
1134 || (p->family == AF_INET
1135 && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)))
1136 nh_family = AF_INET6;
1137 else
1138 continue;
1139
1140 api_nh = &api.nexthops[valid_nh_count];
1141 api_nh->vrf_id = nh_othervrf ? info->extra->bgp_orig->vrf_id
1142 : bgp->vrf_id;
1143 if (nh_family == AF_INET) {
1144 struct in_addr *nexthop;
1145
1146 if (bgp_debug_zebra(&api.prefix)) {
1147 if (mpinfo->extra) {
1148 zlog_debug(
1149 "%s: p=%s, bgp_is_valid_label: %d",
1150 __func__, buf_prefix,
1151 bgp_is_valid_label(
1152 &mpinfo->extra
1153 ->label[0]));
1154 } else {
1155 zlog_debug(
1156 "%s: p=%s, extra is NULL, no label",
1157 __func__, buf_prefix);
1158 }
1159 }
1160
1161 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1162 /* Copy info and attributes, so the route-map
1163 apply doesn't modify the BGP route info. */
1164 local_attr = *mpinfo->attr;
1165 mpinfo_cp->attr = &local_attr;
1166 if (nh_othervrf) {
1167 /* allow route-map to modify */
1168 local_attr.nexthop =
1169 info->extra->nexthop_orig.u
1170 .prefix4;
1171 }
1172 }
1173
1174 if (bgp->table_map[afi][safi].name) {
1175 if (!bgp_table_map_apply(
1176 bgp->table_map[afi][safi].map, p,
1177 mpinfo_cp))
1178 continue;
1179
1180 /* metric/tag is only allowed to be
1181 * overridden on 1st nexthop */
1182 if (mpinfo == info) {
1183 metric = mpinfo_cp->attr->med;
1184 tag = mpinfo_cp->attr->tag;
1185 }
1186 }
1187
1188 nexthop = &mpinfo_cp->attr->nexthop;
1189 api_nh->gate.ipv4 = *nexthop;
1190
1191 /* EVPN type-2 routes are
1192 programmed as onlink on l3-vni SVI
1193 */
1194 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1195 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
1196 else
1197 api_nh->type = NEXTHOP_TYPE_IPV4;
1198 } else {
1199 ifindex_t ifindex;
1200 struct in6_addr *nexthop;
1201
1202 ifindex = 0;
1203
1204 if (bgp->table_map[afi][safi].name || nh_othervrf) {
1205 /* Copy info and attributes, so the route-map
1206 apply doesn't modify the BGP route info. */
1207 local_attr = *mpinfo->attr;
1208 mpinfo_cp->attr = &local_attr;
1209 if (nh_othervrf) {
1210 /* allow route-map to modify */
1211 local_attr.mp_nexthop_global =
1212 info->extra->nexthop_orig.u
1213 .prefix6;
1214 local_attr.mp_nexthop_len =
1215 BGP_ATTR_NHLEN_IPV6_GLOBAL;
1216 }
1217 }
1218
1219 if (bgp->table_map[afi][safi].name) {
1220 /* Copy info and attributes, so the route-map
1221 apply doesn't modify the BGP route info. */
1222 local_attr = *mpinfo->attr;
1223 mpinfo_cp->attr = &local_attr;
1224
1225 if (!bgp_table_map_apply(
1226 bgp->table_map[afi][safi].map, p,
1227 mpinfo_cp))
1228 continue;
1229
1230 /* metric/tag is only allowed to be
1231 * overridden on 1st nexthop */
1232 if (mpinfo == info) {
1233 metric = mpinfo_cp->attr->med;
1234 tag = mpinfo_cp->attr->tag;
1235 }
1236 }
1237 nexthop = bgp_info_to_ipv6_nexthop(mpinfo_cp);
1238
1239 if ((mpinfo == info)
1240 && mpinfo->attr->mp_nexthop_len
1241 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1242 if (mpinfo->peer->nexthop.ifp)
1243 ifindex = mpinfo->peer->nexthop.ifp
1244 ->ifindex;
1245
1246 if (!ifindex) {
1247 if (mpinfo->peer->conf_if)
1248 ifindex = mpinfo->peer->ifp->ifindex;
1249 else if (mpinfo->peer->ifname)
1250 ifindex = ifname2ifindex(
1251 mpinfo->peer->ifname,
1252 bgp->vrf_id);
1253 else if (mpinfo->peer->nexthop.ifp)
1254 ifindex = mpinfo->peer->nexthop.ifp
1255 ->ifindex;
1256 }
1257
1258 if (IN6_IS_ADDR_LINKLOCAL(nexthop)) {
1259 if (ifindex == 0)
1260 continue;
1261 } else
1262 ifindex = 0;
1263
1264 api_nh->gate.ipv6 = *nexthop;
1265 api_nh->ifindex = ifindex;
1266 api_nh->type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX
1267 : NEXTHOP_TYPE_IPV6;
1268 }
1269
1270 if (mpinfo->extra
1271 && bgp_is_valid_label(&mpinfo->extra->label[0])
1272 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1273 has_valid_label = 1;
1274 label = label_pton(&mpinfo->extra->label[0]);
1275
1276 api_nh->label_num = 1;
1277 api_nh->labels[0] = label;
1278 }
1279 valid_nh_count++;
1280 }
1281
1282 /* if this is a evpn route we don't have to include the label */
1283 if (has_valid_label && !(CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)))
1284 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
1285
1286 if (info->sub_type != BGP_ROUTE_AGGREGATE)
1287 api.nexthop_num = valid_nh_count;
1288
1289 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
1290 api.metric = metric;
1291
1292 if (tag) {
1293 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1294 api.tag = tag;
1295 }
1296
1297 distance = bgp_distance_apply(p, info, afi, safi, bgp);
1298 if (distance) {
1299 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
1300 api.distance = distance;
1301 }
1302
1303 if (bgp_debug_zebra(p)) {
1304 char prefix_buf[PREFIX_STRLEN];
1305 char nh_buf[INET6_ADDRSTRLEN];
1306 char label_buf[20];
1307 int i;
1308
1309 prefix2str(&api.prefix, prefix_buf, sizeof(prefix_buf));
1310 zlog_debug("Tx route %s VRF %u %s metric %u tag %" ROUTE_TAG_PRI
1311 " count %d",
1312 valid_nh_count ? "add" : "delete", bgp->vrf_id,
1313 prefix_buf, api.metric, api.tag, api.nexthop_num);
1314 for (i = 0; i < api.nexthop_num; i++) {
1315 api_nh = &api.nexthops[i];
1316
1317 if (api_nh->type == NEXTHOP_TYPE_IPV4)
1318 nh_family = AF_INET;
1319 else
1320 nh_family = AF_INET6;
1321 inet_ntop(nh_family, &api_nh->gate, nh_buf,
1322 sizeof(nh_buf));
1323
1324 label_buf[0] = '\0';
1325 if (has_valid_label
1326 && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE))
1327 sprintf(label_buf, "label %u",
1328 api_nh->labels[0]);
1329 zlog_debug(" nhop [%d]: %s %s", i + 1, nh_buf,
1330 label_buf);
1331 }
1332 }
1333
1334 if (bgp_debug_zebra(p)) {
1335 int recursion_flag = 0;
1336
1337 if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION))
1338 recursion_flag = 1;
1339
1340 zlog_debug("%s: %s: announcing to zebra (recursion %sset)",
1341 __func__, buf_prefix,
1342 (recursion_flag ? "" : "NOT "));
1343 }
1344 zclient_route_send(valid_nh_count ? ZEBRA_ROUTE_ADD
1345 : ZEBRA_ROUTE_DELETE,
1346 zclient, &api);
1347 }
1348
1349 /* Announce all routes of a table to zebra */
1350 void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi)
1351 {
1352 struct bgp_node *rn;
1353 struct bgp_table *table;
1354 struct bgp_info *ri;
1355
1356 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1357 * know of this instance.
1358 */
1359 if (!bgp_install_info_to_zebra(bgp))
1360 return;
1361
1362 table = bgp->rib[afi][safi];
1363 if (!table)
1364 return;
1365
1366 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
1367 for (ri = rn->info; ri; ri = ri->next)
1368 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) &&
1369
1370 (ri->type == ZEBRA_ROUTE_BGP
1371 && (ri->sub_type == BGP_ROUTE_NORMAL
1372 || ri->sub_type == BGP_ROUTE_IMPORTED)))
1373
1374 bgp_zebra_announce(rn, &rn->p, ri, bgp, afi,
1375 safi);
1376 }
1377
1378 void bgp_zebra_withdraw(struct prefix *p, struct bgp_info *info,
1379 struct bgp *bgp, safi_t safi)
1380 {
1381 struct zapi_route api;
1382
1383 /* Don't try to install if we're not connected to Zebra or Zebra doesn't
1384 * know of this instance.
1385 */
1386 if (!bgp_install_info_to_zebra(bgp))
1387 return;
1388
1389 memset(&api, 0, sizeof(api));
1390 memcpy(&api.rmac, &(info->attr->rmac), sizeof(struct ethaddr));
1391 api.vrf_id = bgp->vrf_id;
1392 api.type = ZEBRA_ROUTE_BGP;
1393 api.safi = safi;
1394 api.prefix = *p;
1395
1396 /* If the route's source is EVPN, flag as such. */
1397 if (is_route_parent_evpn(info))
1398 SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE);
1399
1400 if (bgp_debug_zebra(p)) {
1401 char buf[PREFIX_STRLEN];
1402
1403 prefix2str(&api.prefix, buf, sizeof(buf));
1404 zlog_debug("Tx route delete VRF %u %s", bgp->vrf_id, buf);
1405 }
1406
1407 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
1408 }
1409
1410 struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type,
1411 unsigned short instance)
1412 {
1413 struct list *red_list;
1414 struct listnode *node;
1415 struct bgp_redist *red;
1416
1417 red_list = bgp->redist[afi][type];
1418 if (!red_list)
1419 return (NULL);
1420
1421 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1422 if (red->instance == instance)
1423 return red;
1424
1425 return NULL;
1426 }
1427
1428 struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type,
1429 unsigned short instance)
1430 {
1431 struct list *red_list;
1432 struct bgp_redist *red;
1433
1434 red = bgp_redist_lookup(bgp, afi, type, instance);
1435 if (red)
1436 return red;
1437
1438 if (!bgp->redist[afi][type])
1439 bgp->redist[afi][type] = list_new();
1440
1441 red_list = bgp->redist[afi][type];
1442 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST,
1443 sizeof(struct bgp_redist));
1444 red->instance = instance;
1445
1446 listnode_add(red_list, red);
1447
1448 return red;
1449 }
1450
1451 static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type,
1452 unsigned short instance)
1453 {
1454 struct bgp_redist *red;
1455
1456 red = bgp_redist_lookup(bgp, afi, type, instance);
1457
1458 if (red) {
1459 listnode_delete(bgp->redist[afi][type], red);
1460 XFREE(MTYPE_BGP_REDIST, red);
1461 if (!bgp->redist[afi][type]->count)
1462 list_delete_and_null(&bgp->redist[afi][type]);
1463 }
1464 }
1465
1466 /* Other routes redistribution into BGP. */
1467 int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type,
1468 unsigned short instance)
1469 {
1470
1471 /* Return if already redistribute flag is set. */
1472 if (instance) {
1473 if (redist_check_instance(&zclient->mi_redist[afi][type],
1474 instance))
1475 return CMD_WARNING;
1476
1477 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1478 } else {
1479 if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1480 return CMD_WARNING;
1481
1482 #if ENABLE_BGP_VNC
1483 if (bgp->vrf_id == VRF_DEFAULT
1484 && type == ZEBRA_ROUTE_VNC_DIRECT) {
1485 vnc_export_bgp_enable(
1486 bgp, afi); /* only enables if mode bits cfg'd */
1487 }
1488 #endif
1489
1490 vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id);
1491 }
1492
1493 /*
1494 * Don't try to register if we're not connected to Zebra or Zebra
1495 * doesn't know of this instance.
1496 *
1497 * When we come up later well resend if needed.
1498 */
1499 if (!bgp_install_info_to_zebra(bgp))
1500 return CMD_SUCCESS;
1501
1502 if (BGP_DEBUG(zebra, ZEBRA))
1503 zlog_debug("Tx redistribute add VRF %u afi %d %s %d",
1504 bgp->vrf_id, afi, zebra_route_string(type),
1505 instance);
1506
1507 /* Send distribute add message to zebra. */
1508 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1509 instance, bgp->vrf_id);
1510
1511 return CMD_SUCCESS;
1512 }
1513
1514 int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type,
1515 unsigned short instance)
1516 {
1517 /* Don't try to send if we're not connected to Zebra or Zebra doesn't
1518 * know of this instance.
1519 */
1520 if (!bgp_install_info_to_zebra(bgp))
1521 return -1;
1522
1523 if (BGP_DEBUG(zebra, ZEBRA))
1524 zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d",
1525 bgp->vrf_id, afi, zebra_route_string(type),
1526 instance);
1527
1528 /* Send distribute add message to zebra. */
1529 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1530 instance, bgp->vrf_id);
1531 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1532 instance, bgp->vrf_id);
1533
1534 return 0;
1535 }
1536
1537 /* Redistribute with route-map specification. */
1538 int bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name)
1539 {
1540 if (red->rmap.name && (strcmp(red->rmap.name, name) == 0))
1541 return 0;
1542
1543 if (red->rmap.name)
1544 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1545 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1546 red->rmap.map = route_map_lookup_by_name(name);
1547
1548 return 1;
1549 }
1550
1551 /* Redistribute with metric specification. */
1552 int bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red,
1553 afi_t afi, int type, uint32_t metric)
1554 {
1555 struct bgp_node *rn;
1556 struct bgp_info *ri;
1557
1558 if (red->redist_metric_flag && red->redist_metric == metric)
1559 return 0;
1560
1561 red->redist_metric_flag = 1;
1562 red->redist_metric = metric;
1563
1564 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn;
1565 rn = bgp_route_next(rn)) {
1566 for (ri = rn->info; ri; ri = ri->next) {
1567 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE
1568 && ri->type == type
1569 && ri->instance == red->instance) {
1570 struct attr *old_attr;
1571 struct attr new_attr;
1572
1573 bgp_attr_dup(&new_attr, ri->attr);
1574 new_attr.med = red->redist_metric;
1575 old_attr = ri->attr;
1576 ri->attr = bgp_attr_intern(&new_attr);
1577 bgp_attr_unintern(&old_attr);
1578
1579 bgp_info_set_flag(rn, ri,
1580 BGP_INFO_ATTR_CHANGED);
1581 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1582 }
1583 }
1584 }
1585
1586 return 1;
1587 }
1588
1589 /* Unset redistribution. */
1590 int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type,
1591 unsigned short instance)
1592 {
1593 struct bgp_redist *red;
1594
1595 red = bgp_redist_lookup(bgp, afi, type, instance);
1596 if (!red)
1597 return CMD_SUCCESS;
1598
1599 /* Return if zebra connection is disabled. */
1600 if (instance) {
1601 if (!redist_check_instance(&zclient->mi_redist[afi][type],
1602 instance))
1603 return CMD_WARNING;
1604 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1605 } else {
1606 if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id))
1607 return CMD_WARNING;
1608 vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id);
1609 }
1610
1611
1612 if (bgp_install_info_to_zebra(bgp)) {
1613 /* Send distribute delete message to zebra. */
1614 if (BGP_DEBUG(zebra, ZEBRA))
1615 zlog_debug("Tx redistribute del VRF %u afi %d %s %d",
1616 bgp->vrf_id, afi, zebra_route_string(type),
1617 instance);
1618 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi,
1619 type, instance, bgp->vrf_id);
1620 }
1621
1622 /* Withdraw redistributed routes from current BGP's routing table. */
1623 bgp_redistribute_withdraw(bgp, afi, type, instance);
1624
1625 return CMD_SUCCESS;
1626 }
1627
1628 /* Unset redistribution. */
1629 int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type,
1630 unsigned short instance)
1631 {
1632 struct bgp_redist *red;
1633
1634 /*
1635 * vnc and vpn->vrf checks must be before red check because
1636 * they operate within bgpd irrespective of zebra connection
1637 * status. red lookup fails if there is no zebra connection.
1638 */
1639 #if ENABLE_BGP_VNC
1640 if (bgp->vrf_id == VRF_DEFAULT && type == ZEBRA_ROUTE_VNC_DIRECT) {
1641 vnc_export_bgp_disable(bgp, afi);
1642 }
1643 #endif
1644
1645 red = bgp_redist_lookup(bgp, afi, type, instance);
1646 if (!red)
1647 return CMD_SUCCESS;
1648
1649 bgp_redistribute_unreg(bgp, afi, type, instance);
1650
1651 /* Unset route-map. */
1652 if (red->rmap.name)
1653 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1654 red->rmap.name = NULL;
1655 red->rmap.map = NULL;
1656
1657 /* Unset metric. */
1658 red->redist_metric_flag = 0;
1659 red->redist_metric = 0;
1660
1661 bgp_redist_del(bgp, afi, type, instance);
1662
1663 return CMD_SUCCESS;
1664 }
1665
1666 /* Update redistribute vrf bitmap during triggers like
1667 restart networking or delete/add VRFs */
1668 void bgp_update_redist_vrf_bitmaps(struct bgp *bgp, vrf_id_t old_vrf_id)
1669 {
1670 int i;
1671 afi_t afi;
1672
1673 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1674 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1675 if ((old_vrf_id == VRF_UNKNOWN)
1676 || vrf_bitmap_check(zclient->redist[afi][i],
1677 old_vrf_id)) {
1678 vrf_bitmap_unset(zclient->redist[afi][i],
1679 old_vrf_id);
1680 vrf_bitmap_set(zclient->redist[afi][i],
1681 bgp->vrf_id);
1682 }
1683 return;
1684 }
1685
1686 void bgp_zclient_reset(void)
1687 {
1688 zclient_reset(zclient);
1689 }
1690
1691 /* Register this instance with Zebra. Invoked upon connect (for
1692 * default instance) and when other VRFs are learnt (or created and
1693 * already learnt).
1694 */
1695 void bgp_zebra_instance_register(struct bgp *bgp)
1696 {
1697 /* Don't try to register if we're not connected to Zebra */
1698 if (!zclient || zclient->sock < 0)
1699 return;
1700
1701 if (BGP_DEBUG(zebra, ZEBRA))
1702 zlog_debug("Registering VRF %u", bgp->vrf_id);
1703
1704 /* Register for router-id, interfaces, redistributed routes. */
1705 zclient_send_reg_requests(zclient, bgp->vrf_id);
1706
1707 /* For default instance, register to learn about VNIs, if appropriate.
1708 */
1709 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1710 bgp_zebra_advertise_all_vni(bgp, 1);
1711 }
1712
1713 /* Deregister this instance with Zebra. Invoked upon the instance
1714 * being deleted (default or VRF) and it is already registered.
1715 */
1716 void bgp_zebra_instance_deregister(struct bgp *bgp)
1717 {
1718 /* Don't try to deregister if we're not connected to Zebra */
1719 if (zclient->sock < 0)
1720 return;
1721
1722 if (BGP_DEBUG(zebra, ZEBRA))
1723 zlog_debug("Deregistering VRF %u", bgp->vrf_id);
1724
1725 /* For default instance, unregister learning about VNIs, if appropriate.
1726 */
1727 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT && is_evpn_enabled())
1728 bgp_zebra_advertise_all_vni(bgp, 0);
1729
1730 /* Deregister for router-id, interfaces, redistributed routes. */
1731 zclient_send_dereg_requests(zclient, bgp->vrf_id);
1732 }
1733
1734 void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer)
1735 {
1736 int ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL;
1737
1738 /* Don't try to initiate if we're not connected to Zebra */
1739 if (zclient->sock < 0)
1740 return;
1741
1742 if (BGP_DEBUG(zebra, ZEBRA))
1743 zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id,
1744 peer->host);
1745
1746 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 1,
1747 ra_interval);
1748 }
1749
1750 void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer)
1751 {
1752 /* Don't try to terminate if we're not connected to Zebra */
1753 if (zclient->sock < 0)
1754 return;
1755
1756 if (BGP_DEBUG(zebra, ZEBRA))
1757 zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id,
1758 peer->host);
1759
1760 zclient_send_interface_radv_req(zclient, bgp->vrf_id, peer->ifp, 0, 0);
1761 }
1762
1763 int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni)
1764 {
1765 struct stream *s = NULL;
1766
1767 /* Check socket. */
1768 if (!zclient || zclient->sock < 0)
1769 return 0;
1770
1771 /* Don't try to register if Zebra doesn't know of this instance. */
1772 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1773 return 0;
1774
1775 s = zclient->obuf;
1776 stream_reset(s);
1777
1778 zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id);
1779 stream_putc(s, advertise);
1780 stream_put3(s, vni);
1781 stream_putw_at(s, 0, stream_get_endp(s));
1782
1783 return zclient_send_message(zclient);
1784 }
1785
1786 int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni)
1787 {
1788 struct stream *s = NULL;
1789
1790 /* Check socket. */
1791 if (!zclient || zclient->sock < 0)
1792 return 0;
1793
1794 /* Don't try to register if Zebra doesn't know of this instance. */
1795 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1796 return 0;
1797
1798 s = zclient->obuf;
1799 stream_reset(s);
1800
1801 zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id);
1802 stream_putc(s, advertise);
1803 stream_put3(s, vni);
1804 stream_putw_at(s, 0, stream_get_endp(s));
1805
1806 return zclient_send_message(zclient);
1807 }
1808
1809 int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise)
1810 {
1811 struct stream *s;
1812
1813 /* Check socket. */
1814 if (!zclient || zclient->sock < 0)
1815 return 0;
1816
1817 /* Don't try to register if Zebra doesn't know of this instance. */
1818 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
1819 return 0;
1820
1821 s = zclient->obuf;
1822 stream_reset(s);
1823
1824 zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id);
1825 stream_putc(s, advertise);
1826 stream_putw_at(s, 0, stream_get_endp(s));
1827
1828 return zclient_send_message(zclient);
1829 }
1830
1831 /* BGP has established connection with Zebra. */
1832 static void bgp_zebra_connected(struct zclient *zclient)
1833 {
1834 struct bgp *bgp;
1835
1836 zclient_num_connects++; /* increment even if not responding */
1837
1838 /* At this point, we may or may not have BGP instances configured, but
1839 * we're only interested in the default VRF (others wouldn't have learnt
1840 * the VRF from Zebra yet.)
1841 */
1842 bgp = bgp_get_default();
1843 if (!bgp)
1844 return;
1845
1846 bgp_zebra_instance_register(bgp);
1847
1848 /* Send the client registration */
1849 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
1850
1851 /* tell label pool that zebra is connected */
1852 bgp_lp_event_zebra_up();
1853
1854 /* TODO - What if we have peers and networks configured, do we have to
1855 * kick-start them?
1856 */
1857 }
1858
1859 static int bgp_zebra_process_local_l3vni(int cmd, struct zclient *zclient,
1860 zebra_size_t length, vrf_id_t vrf_id)
1861 {
1862 int filter = 0;
1863 char buf[ETHER_ADDR_STRLEN];
1864 vni_t l3vni = 0;
1865 struct ethaddr rmac;
1866 struct in_addr originator_ip;
1867 struct stream *s;
1868
1869 memset(&rmac, 0, sizeof(struct ethaddr));
1870 memset(&originator_ip, 0, sizeof(struct in_addr));
1871 s = zclient->ibuf;
1872 l3vni = stream_getl(s);
1873 if (cmd == ZEBRA_L3VNI_ADD) {
1874 stream_get(&rmac, s, sizeof(struct ethaddr));
1875 originator_ip.s_addr = stream_get_ipv4(s);
1876 stream_get(&filter, s, sizeof(int));
1877 }
1878
1879 if (BGP_DEBUG(zebra, ZEBRA))
1880 zlog_debug("Rx L3-VNI %s VRF %s VNI %u RMAC %s filter %s",
1881 (cmd == ZEBRA_L3VNI_ADD) ? "add" : "del",
1882 vrf_id_to_name(vrf_id), l3vni,
1883 prefix_mac2str(&rmac, buf, sizeof(buf)),
1884 filter ? "prefix-routes-only" : "none");
1885
1886 if (cmd == ZEBRA_L3VNI_ADD)
1887 bgp_evpn_local_l3vni_add(l3vni, vrf_id, &rmac, originator_ip,
1888 filter);
1889 else
1890 bgp_evpn_local_l3vni_del(l3vni, vrf_id);
1891
1892 return 0;
1893 }
1894
1895 static int bgp_zebra_process_local_vni(int command, struct zclient *zclient,
1896 zebra_size_t length, vrf_id_t vrf_id)
1897 {
1898 struct stream *s;
1899 vni_t vni;
1900 struct bgp *bgp;
1901 struct in_addr vtep_ip = {INADDR_ANY};
1902 vrf_id_t tenant_vrf_id = VRF_DEFAULT;
1903
1904 s = zclient->ibuf;
1905 vni = stream_getl(s);
1906 if (command == ZEBRA_VNI_ADD) {
1907 vtep_ip.s_addr = stream_get_ipv4(s);
1908 stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t));
1909 }
1910
1911 bgp = bgp_lookup_by_vrf_id(vrf_id);
1912 if (!bgp)
1913 return 0;
1914
1915 if (BGP_DEBUG(zebra, ZEBRA))
1916 zlog_debug("Rx VNI %s VRF %s VNI %u tenant-vrf %s",
1917 (command == ZEBRA_VNI_ADD) ? "add" : "del",
1918 vrf_id_to_name(vrf_id), vni,
1919 vrf_id_to_name(tenant_vrf_id));
1920
1921 if (command == ZEBRA_VNI_ADD)
1922 return bgp_evpn_local_vni_add(
1923 bgp, vni, vtep_ip.s_addr ? vtep_ip : bgp->router_id,
1924 tenant_vrf_id);
1925 else
1926 return bgp_evpn_local_vni_del(bgp, vni);
1927 }
1928
1929 static int bgp_zebra_process_local_macip(int command, struct zclient *zclient,
1930 zebra_size_t length, vrf_id_t vrf_id)
1931 {
1932 struct stream *s;
1933 vni_t vni;
1934 struct bgp *bgp;
1935 struct ethaddr mac;
1936 struct ipaddr ip;
1937 int ipa_len;
1938 char buf[ETHER_ADDR_STRLEN];
1939 char buf1[INET6_ADDRSTRLEN];
1940 uint8_t flags;
1941
1942 memset(&ip, 0, sizeof(ip));
1943 s = zclient->ibuf;
1944 vni = stream_getl(s);
1945 stream_get(&mac.octet, s, ETH_ALEN);
1946 ipa_len = stream_getl(s);
1947 if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN
1948 && ipa_len != IPV6_MAX_BYTELEN) {
1949 zlog_err("%u:Recv MACIP %s with invalid IP addr length %d",
1950 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1951 ipa_len);
1952 return -1;
1953 }
1954
1955 if (ipa_len) {
1956 ip.ipa_type =
1957 (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6;
1958 stream_get(&ip.ip.addr, s, ipa_len);
1959 }
1960 flags = stream_getc(s);
1961
1962 bgp = bgp_lookup_by_vrf_id(vrf_id);
1963 if (!bgp)
1964 return 0;
1965
1966 if (BGP_DEBUG(zebra, ZEBRA))
1967 zlog_debug("%u:Recv MACIP %s flags 0x%x MAC %s IP %s VNI %u",
1968 vrf_id, (command == ZEBRA_MACIP_ADD) ? "Add" : "Del",
1969 flags, prefix_mac2str(&mac, buf, sizeof(buf)),
1970 ipaddr2str(&ip, buf1, sizeof(buf1)), vni);
1971
1972 if (command == ZEBRA_MACIP_ADD)
1973 return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, flags);
1974 else
1975 return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip);
1976 }
1977
1978 static void bgp_zebra_process_local_ip_prefix(int cmd, struct zclient *zclient,
1979 zebra_size_t length,
1980 vrf_id_t vrf_id)
1981 {
1982 struct stream *s = NULL;
1983 struct bgp *bgp_vrf = NULL;
1984 struct prefix p;
1985 char buf[PREFIX_STRLEN];
1986
1987 memset(&p, 0, sizeof(struct prefix));
1988 s = zclient->ibuf;
1989 stream_get(&p, s, sizeof(struct prefix));
1990
1991 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
1992 if (!bgp_vrf)
1993 return;
1994
1995 if (BGP_DEBUG(zebra, ZEBRA))
1996 zlog_debug("Recv prefix %s %s on vrf %s",
1997 prefix2str(&p, buf, sizeof(buf)),
1998 (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL",
1999 vrf_id_to_name(vrf_id));
2000
2001 if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) {
2002
2003 if (p.family == AF_INET)
2004 return bgp_evpn_advertise_type5_route(
2005 bgp_vrf, &p, NULL, AFI_IP, SAFI_UNICAST);
2006 else
2007 return bgp_evpn_advertise_type5_route(
2008 bgp_vrf, &p, NULL, AFI_IP6, SAFI_UNICAST);
2009
2010 } else {
2011 if (p.family == AF_INET)
2012 return bgp_evpn_withdraw_type5_route(
2013 bgp_vrf, &p, AFI_IP, SAFI_UNICAST);
2014 else
2015 return bgp_evpn_withdraw_type5_route(
2016 bgp_vrf, &p, AFI_IP6, SAFI_UNICAST);
2017 }
2018 }
2019
2020 static void bgp_zebra_process_label_chunk(
2021 int cmd,
2022 struct zclient *zclient,
2023 zebra_size_t length,
2024 vrf_id_t vrf_id)
2025 {
2026 struct stream *s = NULL;
2027 uint8_t response_keep;
2028 uint32_t first;
2029 uint32_t last;
2030
2031 s = zclient->ibuf;
2032 STREAM_GETC(s, response_keep);
2033 STREAM_GETL(s, first);
2034 STREAM_GETL(s, last);
2035
2036 if (first > last ||
2037 first < MPLS_LABEL_UNRESERVED_MIN ||
2038 last > MPLS_LABEL_UNRESERVED_MAX) {
2039
2040 zlog_err("%s: Invalid Label chunk: %u - %u",
2041 __func__, first, last);
2042 return;
2043 }
2044 if (BGP_DEBUG(zebra, ZEBRA)) {
2045 zlog_debug("Label Chunk assign: %u - %u (%u) ",
2046 first, last, response_keep);
2047 }
2048
2049 bgp_lp_event_chunk(response_keep, first, last);
2050
2051 stream_failure: /* for STREAM_GETX */
2052 return;
2053 }
2054
2055 extern struct zebra_privs_t bgpd_privs;
2056
2057 void bgp_zebra_init(struct thread_master *master)
2058 {
2059 zclient_num_connects = 0;
2060
2061 /* Set default values. */
2062 zclient = zclient_new_notify(master, &zclient_options_default);
2063 zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs);
2064 zclient->zebra_connected = bgp_zebra_connected;
2065 zclient->router_id_update = bgp_router_id_update;
2066 zclient->interface_add = bgp_interface_add;
2067 zclient->interface_delete = bgp_interface_delete;
2068 zclient->interface_address_add = bgp_interface_address_add;
2069 zclient->interface_address_delete = bgp_interface_address_delete;
2070 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
2071 zclient->interface_nbr_address_delete =
2072 bgp_interface_nbr_address_delete;
2073 zclient->interface_vrf_update = bgp_interface_vrf_update;
2074 zclient->redistribute_route_add = zebra_read_route;
2075 zclient->redistribute_route_del = zebra_read_route;
2076 zclient->interface_up = bgp_interface_up;
2077 zclient->interface_down = bgp_interface_down;
2078 zclient->nexthop_update = bgp_read_nexthop_update;
2079 zclient->import_check_update = bgp_read_import_check_update;
2080 zclient->fec_update = bgp_read_fec_update;
2081 zclient->local_vni_add = bgp_zebra_process_local_vni;
2082 zclient->local_vni_del = bgp_zebra_process_local_vni;
2083 zclient->local_macip_add = bgp_zebra_process_local_macip;
2084 zclient->local_macip_del = bgp_zebra_process_local_macip;
2085 zclient->local_l3vni_add = bgp_zebra_process_local_l3vni;
2086 zclient->local_l3vni_del = bgp_zebra_process_local_l3vni;
2087 zclient->local_ip_prefix_add = bgp_zebra_process_local_ip_prefix;
2088 zclient->local_ip_prefix_del = bgp_zebra_process_local_ip_prefix;
2089 zclient->label_chunk = bgp_zebra_process_label_chunk;
2090 }
2091
2092 void bgp_zebra_destroy(void)
2093 {
2094 if (zclient == NULL)
2095 return;
2096 zclient_stop(zclient);
2097 zclient_free(zclient);
2098 zclient = NULL;
2099 }
2100
2101 int bgp_zebra_num_connects(void)
2102 {
2103 return zclient_num_connects;
2104 }