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