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