]>
Commit | Line | Data |
---|---|---|
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 "lib/route_opaque.h" | |
37 | #include "filter.h" | |
38 | #include "mpls.h" | |
39 | #include "vxlan.h" | |
40 | #include "pbr.h" | |
41 | ||
42 | #include "bgpd/bgpd.h" | |
43 | #include "bgpd/bgp_route.h" | |
44 | #include "bgpd/bgp_attr.h" | |
45 | #include "bgpd/bgp_aspath.h" | |
46 | #include "bgpd/bgp_nexthop.h" | |
47 | #include "bgpd/bgp_zebra.h" | |
48 | #include "bgpd/bgp_fsm.h" | |
49 | #include "bgpd/bgp_debug.h" | |
50 | #include "bgpd/bgp_errors.h" | |
51 | #include "bgpd/bgp_mpath.h" | |
52 | #include "bgpd/bgp_nexthop.h" | |
53 | #include "bgpd/bgp_nht.h" | |
54 | #include "bgpd/bgp_bfd.h" | |
55 | #include "bgpd/bgp_label.h" | |
56 | #ifdef ENABLE_BGP_VNC | |
57 | #include "bgpd/rfapi/rfapi_backend.h" | |
58 | #include "bgpd/rfapi/vnc_export_bgp.h" | |
59 | #endif | |
60 | #include "bgpd/bgp_evpn.h" | |
61 | #include "bgpd/bgp_mplsvpn.h" | |
62 | #include "bgpd/bgp_labelpool.h" | |
63 | #include "bgpd/bgp_pbr.h" | |
64 | #include "bgpd/bgp_evpn_private.h" | |
65 | #include "bgpd/bgp_evpn_mh.h" | |
66 | #include "bgpd/bgp_mac.h" | |
67 | #include "bgpd/bgp_trace.h" | |
68 | #include "bgpd/bgp_community.h" | |
69 | #include "bgpd/bgp_lcommunity.h" | |
70 | ||
71 | /* All information about zebra. */ | |
72 | struct zclient *zclient = NULL; | |
73 | ||
74 | /* hook to indicate vrf status change for SNMP */ | |
75 | DEFINE_HOOK(bgp_vrf_status_changed, (struct bgp *bgp, struct interface *ifp), | |
76 | (bgp, ifp)); | |
77 | ||
78 | /* Can we install into zebra? */ | |
79 | static inline bool bgp_install_info_to_zebra(struct bgp *bgp) | |
80 | { | |
81 | if (zclient->sock <= 0) | |
82 | return false; | |
83 | ||
84 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) { | |
85 | zlog_debug( | |
86 | "%s: No zebra instance to talk to, not installing information", | |
87 | __func__); | |
88 | return false; | |
89 | } | |
90 | ||
91 | return true; | |
92 | } | |
93 | ||
94 | int zclient_num_connects; | |
95 | ||
96 | /* Router-id update message from zebra. */ | |
97 | static int bgp_router_id_update(ZAPI_CALLBACK_ARGS) | |
98 | { | |
99 | struct prefix router_id; | |
100 | ||
101 | zebra_router_id_update_read(zclient->ibuf, &router_id); | |
102 | ||
103 | if (BGP_DEBUG(zebra, ZEBRA)) | |
104 | zlog_debug("Rx Router Id update VRF %u Id %pFX", vrf_id, | |
105 | &router_id); | |
106 | ||
107 | bgp_router_id_zebra_bump(vrf_id, &router_id); | |
108 | return 0; | |
109 | } | |
110 | ||
111 | /* Nexthop update message from zebra. */ | |
112 | static int bgp_read_nexthop_update(ZAPI_CALLBACK_ARGS) | |
113 | { | |
114 | bgp_parse_nexthop_update(cmd, vrf_id); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | /* Set or clear interface on which unnumbered neighbor is configured. This | |
119 | * would in turn cause BGP to initiate or turn off IPv6 RAs on this | |
120 | * interface. | |
121 | */ | |
122 | static void bgp_update_interface_nbrs(struct bgp *bgp, struct interface *ifp, | |
123 | struct interface *upd_ifp) | |
124 | { | |
125 | struct listnode *node, *nnode; | |
126 | struct peer *peer; | |
127 | ||
128 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
129 | if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0)) { | |
130 | if (upd_ifp) { | |
131 | peer->ifp = upd_ifp; | |
132 | bgp_zebra_initiate_radv(bgp, peer); | |
133 | } else { | |
134 | bgp_zebra_terminate_radv(bgp, peer); | |
135 | peer->ifp = upd_ifp; | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | static int bgp_read_fec_update(ZAPI_CALLBACK_ARGS) | |
142 | { | |
143 | bgp_parse_fec_update(); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static void bgp_start_interface_nbrs(struct bgp *bgp, struct interface *ifp) | |
148 | { | |
149 | struct listnode *node, *nnode; | |
150 | struct peer *peer; | |
151 | ||
152 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
153 | if (peer->conf_if && (strcmp(peer->conf_if, ifp->name) == 0) | |
154 | && !peer_established(peer)) { | |
155 | if (peer_active(peer)) | |
156 | BGP_EVENT_ADD(peer, BGP_Stop); | |
157 | BGP_EVENT_ADD(peer, BGP_Start); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | static void bgp_nbr_connected_add(struct bgp *bgp, struct nbr_connected *ifc) | |
163 | { | |
164 | struct listnode *node; | |
165 | struct connected *connected; | |
166 | struct interface *ifp; | |
167 | struct prefix *p; | |
168 | ||
169 | /* Kick-off the FSM for any relevant peers only if there is a | |
170 | * valid local address on the interface. | |
171 | */ | |
172 | ifp = ifc->ifp; | |
173 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) { | |
174 | p = connected->address; | |
175 | if (p->family == AF_INET6 | |
176 | && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) | |
177 | break; | |
178 | } | |
179 | if (!connected) | |
180 | return; | |
181 | ||
182 | bgp_start_interface_nbrs(bgp, ifp); | |
183 | } | |
184 | ||
185 | static void bgp_nbr_connected_delete(struct bgp *bgp, struct nbr_connected *ifc, | |
186 | int del) | |
187 | { | |
188 | struct listnode *node, *nnode; | |
189 | struct peer *peer; | |
190 | struct interface *ifp; | |
191 | ||
192 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
193 | if (peer->conf_if | |
194 | && (strcmp(peer->conf_if, ifc->ifp->name) == 0)) { | |
195 | peer->last_reset = PEER_DOWN_NBR_ADDR_DEL; | |
196 | BGP_EVENT_ADD(peer, BGP_Stop); | |
197 | } | |
198 | } | |
199 | /* Free neighbor also, if we're asked to. */ | |
200 | if (del) { | |
201 | ifp = ifc->ifp; | |
202 | listnode_delete(ifp->nbr_connected, ifc); | |
203 | nbr_connected_free(ifc); | |
204 | } | |
205 | } | |
206 | ||
207 | static int bgp_ifp_destroy(struct interface *ifp) | |
208 | { | |
209 | struct bgp *bgp; | |
210 | ||
211 | bgp = ifp->vrf->info; | |
212 | ||
213 | if (BGP_DEBUG(zebra, ZEBRA)) | |
214 | zlog_debug("Rx Intf del VRF %u IF %s", ifp->vrf->vrf_id, | |
215 | ifp->name); | |
216 | ||
217 | if (bgp) { | |
218 | bgp_update_interface_nbrs(bgp, ifp, NULL); | |
219 | hook_call(bgp_vrf_status_changed, bgp, ifp); | |
220 | } | |
221 | ||
222 | bgp_mac_del_mac_entry(ifp); | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | static int bgp_ifp_up(struct interface *ifp) | |
228 | { | |
229 | struct connected *c; | |
230 | struct nbr_connected *nc; | |
231 | struct listnode *node, *nnode; | |
232 | struct bgp *bgp; | |
233 | ||
234 | bgp = ifp->vrf->info; | |
235 | ||
236 | bgp_mac_add_mac_entry(ifp); | |
237 | ||
238 | if (BGP_DEBUG(zebra, ZEBRA)) | |
239 | zlog_debug("Rx Intf up VRF %u IF %s", ifp->vrf->vrf_id, | |
240 | ifp->name); | |
241 | ||
242 | if (!bgp) | |
243 | return 0; | |
244 | ||
245 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c)) | |
246 | bgp_connected_add(bgp, c); | |
247 | ||
248 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc)) | |
249 | bgp_nbr_connected_add(bgp, nc); | |
250 | ||
251 | hook_call(bgp_vrf_status_changed, bgp, ifp); | |
252 | bgp_nht_ifp_up(ifp); | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | static int bgp_ifp_down(struct interface *ifp) | |
258 | { | |
259 | struct connected *c; | |
260 | struct nbr_connected *nc; | |
261 | struct listnode *node, *nnode; | |
262 | struct bgp *bgp; | |
263 | struct peer *peer; | |
264 | ||
265 | bgp = ifp->vrf->info; | |
266 | ||
267 | bgp_mac_del_mac_entry(ifp); | |
268 | ||
269 | if (BGP_DEBUG(zebra, ZEBRA)) | |
270 | zlog_debug("Rx Intf down VRF %u IF %s", ifp->vrf->vrf_id, | |
271 | ifp->name); | |
272 | ||
273 | if (!bgp) | |
274 | return 0; | |
275 | ||
276 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c)) | |
277 | bgp_connected_delete(bgp, c); | |
278 | ||
279 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc)) | |
280 | bgp_nbr_connected_delete(bgp, nc, 1); | |
281 | ||
282 | /* Fast external-failover */ | |
283 | if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) { | |
284 | ||
285 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
286 | /* Take down directly connected peers. */ | |
287 | if ((peer->ttl != BGP_DEFAULT_TTL) | |
288 | && (peer->gtsm_hops != BGP_GTSM_HOPS_CONNECTED)) | |
289 | continue; | |
290 | ||
291 | if (ifp == peer->nexthop.ifp) { | |
292 | BGP_EVENT_ADD(peer, BGP_Stop); | |
293 | peer->last_reset = PEER_DOWN_IF_DOWN; | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
298 | hook_call(bgp_vrf_status_changed, bgp, ifp); | |
299 | bgp_nht_ifp_down(ifp); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS) | |
305 | { | |
306 | struct connected *ifc; | |
307 | struct bgp *bgp; | |
308 | ||
309 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
310 | ||
311 | ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); | |
312 | ||
313 | if (ifc == NULL) | |
314 | return 0; | |
315 | ||
316 | if (bgp_debug_zebra(ifc->address)) | |
317 | zlog_debug("Rx Intf address add VRF %u IF %s addr %pFX", vrf_id, | |
318 | ifc->ifp->name, ifc->address); | |
319 | ||
320 | if (!bgp) | |
321 | return 0; | |
322 | ||
323 | if (if_is_operative(ifc->ifp)) { | |
324 | bgp_connected_add(bgp, ifc); | |
325 | ||
326 | /* If we have learnt of any neighbors on this interface, | |
327 | * check to kick off any BGP interface-based neighbors, | |
328 | * but only if this is a link-local address. | |
329 | */ | |
330 | if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) | |
331 | && !list_isempty(ifc->ifp->nbr_connected)) | |
332 | bgp_start_interface_nbrs(bgp, ifc->ifp); | |
333 | } | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS) | |
339 | { | |
340 | struct listnode *node, *nnode; | |
341 | struct connected *ifc; | |
342 | struct peer *peer; | |
343 | struct bgp *bgp; | |
344 | struct prefix *addr; | |
345 | ||
346 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
347 | ||
348 | ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); | |
349 | ||
350 | if (ifc == NULL) | |
351 | return 0; | |
352 | ||
353 | if (bgp_debug_zebra(ifc->address)) | |
354 | zlog_debug("Rx Intf address del VRF %u IF %s addr %pFX", vrf_id, | |
355 | ifc->ifp->name, ifc->address); | |
356 | ||
357 | if (bgp && if_is_operative(ifc->ifp)) { | |
358 | bgp_connected_delete(bgp, ifc); | |
359 | } | |
360 | ||
361 | addr = ifc->address; | |
362 | ||
363 | if (bgp) { | |
364 | /* | |
365 | * When we are using the v6 global as part of the peering | |
366 | * nexthops and we are removing it, then we need to | |
367 | * clear the peer data saved for that nexthop and | |
368 | * cause a re-announcement of the route. Since | |
369 | * we do not want the peering to bounce. | |
370 | */ | |
371 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
372 | afi_t afi; | |
373 | safi_t safi; | |
374 | ||
375 | if (addr->family == AF_INET) | |
376 | continue; | |
377 | ||
378 | if (!IN6_IS_ADDR_LINKLOCAL(&addr->u.prefix6) | |
379 | && memcmp(&peer->nexthop.v6_global, | |
380 | &addr->u.prefix6, 16) | |
381 | == 0) { | |
382 | memset(&peer->nexthop.v6_global, 0, 16); | |
383 | FOREACH_AFI_SAFI (afi, safi) | |
384 | bgp_announce_route(peer, afi, safi, | |
385 | true); | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
390 | connected_free(&ifc); | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | static int bgp_interface_nbr_address_add(ZAPI_CALLBACK_ARGS) | |
396 | { | |
397 | struct nbr_connected *ifc = NULL; | |
398 | struct bgp *bgp; | |
399 | ||
400 | ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id); | |
401 | ||
402 | if (ifc == NULL) | |
403 | return 0; | |
404 | ||
405 | if (bgp_debug_zebra(ifc->address)) | |
406 | zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %pFX", | |
407 | vrf_id, ifc->ifp->name, ifc->address); | |
408 | ||
409 | if (if_is_operative(ifc->ifp)) { | |
410 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
411 | if (bgp) | |
412 | bgp_nbr_connected_add(bgp, ifc); | |
413 | } | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | static int bgp_interface_nbr_address_delete(ZAPI_CALLBACK_ARGS) | |
419 | { | |
420 | struct nbr_connected *ifc = NULL; | |
421 | struct bgp *bgp; | |
422 | ||
423 | ifc = zebra_interface_nbr_address_read(cmd, zclient->ibuf, vrf_id); | |
424 | ||
425 | if (ifc == NULL) | |
426 | return 0; | |
427 | ||
428 | if (bgp_debug_zebra(ifc->address)) | |
429 | zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %pFX", | |
430 | vrf_id, ifc->ifp->name, ifc->address); | |
431 | ||
432 | if (if_is_operative(ifc->ifp)) { | |
433 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
434 | if (bgp) | |
435 | bgp_nbr_connected_delete(bgp, ifc, 0); | |
436 | } | |
437 | ||
438 | nbr_connected_free(ifc); | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | /* VRF update for an interface. */ | |
444 | static int bgp_interface_vrf_update(ZAPI_CALLBACK_ARGS) | |
445 | { | |
446 | struct interface *ifp; | |
447 | vrf_id_t new_vrf_id; | |
448 | struct connected *c; | |
449 | struct nbr_connected *nc; | |
450 | struct listnode *node, *nnode; | |
451 | struct bgp *bgp; | |
452 | struct peer *peer; | |
453 | ||
454 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, | |
455 | &new_vrf_id); | |
456 | if (!ifp) | |
457 | return 0; | |
458 | ||
459 | if (BGP_DEBUG(zebra, ZEBRA) && ifp) | |
460 | zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u", vrf_id, | |
461 | ifp->name, new_vrf_id); | |
462 | ||
463 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
464 | ||
465 | if (bgp) { | |
466 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c)) | |
467 | bgp_connected_delete(bgp, c); | |
468 | ||
469 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc)) | |
470 | bgp_nbr_connected_delete(bgp, nc, 1); | |
471 | ||
472 | /* Fast external-failover */ | |
473 | if (!CHECK_FLAG(bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) { | |
474 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
475 | if ((peer->ttl != BGP_DEFAULT_TTL) | |
476 | && (peer->gtsm_hops | |
477 | != BGP_GTSM_HOPS_CONNECTED)) | |
478 | continue; | |
479 | ||
480 | if (ifp == peer->nexthop.ifp) | |
481 | BGP_EVENT_ADD(peer, BGP_Stop); | |
482 | } | |
483 | } | |
484 | } | |
485 | ||
486 | if_update_to_new_vrf(ifp, new_vrf_id); | |
487 | ||
488 | bgp = bgp_lookup_by_vrf_id(new_vrf_id); | |
489 | if (!bgp) | |
490 | return 0; | |
491 | ||
492 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, c)) | |
493 | bgp_connected_add(bgp, c); | |
494 | ||
495 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode, nc)) | |
496 | bgp_nbr_connected_add(bgp, nc); | |
497 | ||
498 | hook_call(bgp_vrf_status_changed, bgp, ifp); | |
499 | return 0; | |
500 | } | |
501 | ||
502 | /* Zebra route add and delete treatment. */ | |
503 | static int zebra_read_route(ZAPI_CALLBACK_ARGS) | |
504 | { | |
505 | enum nexthop_types_t nhtype; | |
506 | enum blackhole_type bhtype = BLACKHOLE_UNSPEC; | |
507 | struct zapi_route api; | |
508 | union g_addr nexthop = {}; | |
509 | ifindex_t ifindex; | |
510 | int add, i; | |
511 | struct bgp *bgp; | |
512 | ||
513 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
514 | if (!bgp) | |
515 | return 0; | |
516 | ||
517 | if (zapi_route_decode(zclient->ibuf, &api) < 0) | |
518 | return -1; | |
519 | ||
520 | /* we completely ignore srcdest routes for now. */ | |
521 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
522 | return 0; | |
523 | ||
524 | /* ignore link-local address. */ | |
525 | if (api.prefix.family == AF_INET6 | |
526 | && IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6)) | |
527 | return 0; | |
528 | ||
529 | ifindex = api.nexthops[0].ifindex; | |
530 | nhtype = api.nexthops[0].type; | |
531 | ||
532 | /* api_nh structure has union of gate and bh_type */ | |
533 | if (nhtype == NEXTHOP_TYPE_BLACKHOLE) { | |
534 | /* bh_type is only applicable if NEXTHOP_TYPE_BLACKHOLE*/ | |
535 | bhtype = api.nexthops[0].bh_type; | |
536 | } else | |
537 | nexthop = api.nexthops[0].gate; | |
538 | ||
539 | add = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD); | |
540 | if (add) { | |
541 | /* | |
542 | * The ADD message is actually an UPDATE and there is no | |
543 | * explicit DEL | |
544 | * for a prior redistributed route, if any. So, perform an | |
545 | * implicit | |
546 | * DEL processing for the same redistributed route from any | |
547 | * other | |
548 | * source type. | |
549 | */ | |
550 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
551 | if (i != api.type) | |
552 | bgp_redistribute_delete(bgp, &api.prefix, i, | |
553 | api.instance); | |
554 | } | |
555 | ||
556 | /* Now perform the add/update. */ | |
557 | bgp_redistribute_add(bgp, &api.prefix, &nexthop, ifindex, | |
558 | nhtype, bhtype, api.distance, api.metric, | |
559 | api.type, api.instance, api.tag); | |
560 | } else { | |
561 | bgp_redistribute_delete(bgp, &api.prefix, api.type, | |
562 | api.instance); | |
563 | } | |
564 | ||
565 | if (bgp_debug_zebra(&api.prefix)) { | |
566 | char buf[PREFIX_STRLEN]; | |
567 | ||
568 | if (add) { | |
569 | inet_ntop(api.prefix.family, &nexthop, buf, | |
570 | sizeof(buf)); | |
571 | zlog_debug( | |
572 | "Rx route ADD VRF %u %s[%d] %pFX nexthop %s (type %d if %u) metric %u distance %u tag %" ROUTE_TAG_PRI, | |
573 | vrf_id, zebra_route_string(api.type), | |
574 | api.instance, &api.prefix, buf, nhtype, ifindex, | |
575 | api.metric, api.distance, api.tag); | |
576 | } else { | |
577 | zlog_debug("Rx route DEL VRF %u %s[%d] %pFX", vrf_id, | |
578 | zebra_route_string(api.type), api.instance, | |
579 | &api.prefix); | |
580 | } | |
581 | } | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
586 | struct interface *if_lookup_by_ipv4(struct in_addr *addr, vrf_id_t vrf_id) | |
587 | { | |
588 | struct vrf *vrf; | |
589 | struct listnode *cnode; | |
590 | struct interface *ifp; | |
591 | struct connected *connected; | |
592 | struct prefix_ipv4 p; | |
593 | struct prefix *cp; | |
594 | ||
595 | vrf = vrf_lookup_by_id(vrf_id); | |
596 | if (!vrf) | |
597 | return NULL; | |
598 | ||
599 | p.family = AF_INET; | |
600 | p.prefix = *addr; | |
601 | p.prefixlen = IPV4_MAX_BITLEN; | |
602 | ||
603 | FOR_ALL_INTERFACES (vrf, ifp) { | |
604 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
605 | cp = connected->address; | |
606 | ||
607 | if (cp->family == AF_INET) | |
608 | if (prefix_match(cp, (struct prefix *)&p)) | |
609 | return ifp; | |
610 | } | |
611 | } | |
612 | return NULL; | |
613 | } | |
614 | ||
615 | struct interface *if_lookup_by_ipv4_exact(struct in_addr *addr, vrf_id_t vrf_id) | |
616 | { | |
617 | struct vrf *vrf; | |
618 | struct listnode *cnode; | |
619 | struct interface *ifp; | |
620 | struct connected *connected; | |
621 | struct prefix *cp; | |
622 | ||
623 | vrf = vrf_lookup_by_id(vrf_id); | |
624 | if (!vrf) | |
625 | return NULL; | |
626 | ||
627 | FOR_ALL_INTERFACES (vrf, ifp) { | |
628 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
629 | cp = connected->address; | |
630 | ||
631 | if (cp->family == AF_INET) | |
632 | if (IPV4_ADDR_SAME(&cp->u.prefix4, addr)) | |
633 | return ifp; | |
634 | } | |
635 | } | |
636 | return NULL; | |
637 | } | |
638 | ||
639 | struct interface *if_lookup_by_ipv6(struct in6_addr *addr, ifindex_t ifindex, | |
640 | vrf_id_t vrf_id) | |
641 | { | |
642 | struct vrf *vrf; | |
643 | struct listnode *cnode; | |
644 | struct interface *ifp; | |
645 | struct connected *connected; | |
646 | struct prefix_ipv6 p; | |
647 | struct prefix *cp; | |
648 | ||
649 | vrf = vrf_lookup_by_id(vrf_id); | |
650 | if (!vrf) | |
651 | return NULL; | |
652 | ||
653 | p.family = AF_INET6; | |
654 | p.prefix = *addr; | |
655 | p.prefixlen = IPV6_MAX_BITLEN; | |
656 | ||
657 | FOR_ALL_INTERFACES (vrf, ifp) { | |
658 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
659 | cp = connected->address; | |
660 | ||
661 | if (cp->family == AF_INET6) | |
662 | if (prefix_match(cp, (struct prefix *)&p)) { | |
663 | if (IN6_IS_ADDR_LINKLOCAL( | |
664 | &cp->u.prefix6)) { | |
665 | if (ifindex == ifp->ifindex) | |
666 | return ifp; | |
667 | } else | |
668 | return ifp; | |
669 | } | |
670 | } | |
671 | } | |
672 | return NULL; | |
673 | } | |
674 | ||
675 | struct interface *if_lookup_by_ipv6_exact(struct in6_addr *addr, | |
676 | ifindex_t ifindex, vrf_id_t vrf_id) | |
677 | { | |
678 | struct vrf *vrf; | |
679 | struct listnode *cnode; | |
680 | struct interface *ifp; | |
681 | struct connected *connected; | |
682 | struct prefix *cp; | |
683 | ||
684 | vrf = vrf_lookup_by_id(vrf_id); | |
685 | if (!vrf) | |
686 | return NULL; | |
687 | ||
688 | FOR_ALL_INTERFACES (vrf, ifp) { | |
689 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
690 | cp = connected->address; | |
691 | ||
692 | if (cp->family == AF_INET6) | |
693 | if (IPV6_ADDR_SAME(&cp->u.prefix6, addr)) { | |
694 | if (IN6_IS_ADDR_LINKLOCAL( | |
695 | &cp->u.prefix6)) { | |
696 | if (ifindex == ifp->ifindex) | |
697 | return ifp; | |
698 | } else | |
699 | return ifp; | |
700 | } | |
701 | } | |
702 | } | |
703 | return NULL; | |
704 | } | |
705 | ||
706 | static int if_get_ipv6_global(struct interface *ifp, struct in6_addr *addr) | |
707 | { | |
708 | struct listnode *cnode; | |
709 | struct connected *connected; | |
710 | struct prefix *cp; | |
711 | ||
712 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
713 | cp = connected->address; | |
714 | ||
715 | if (cp->family == AF_INET6) | |
716 | if (!IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) { | |
717 | memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); | |
718 | return 1; | |
719 | } | |
720 | } | |
721 | return 0; | |
722 | } | |
723 | ||
724 | static bool if_get_ipv6_local(struct interface *ifp, struct in6_addr *addr) | |
725 | { | |
726 | struct listnode *cnode; | |
727 | struct connected *connected; | |
728 | struct prefix *cp; | |
729 | ||
730 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
731 | cp = connected->address; | |
732 | ||
733 | if (cp->family == AF_INET6) | |
734 | if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6)) { | |
735 | memcpy(addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); | |
736 | return true; | |
737 | } | |
738 | } | |
739 | return false; | |
740 | } | |
741 | ||
742 | static int if_get_ipv4_address(struct interface *ifp, struct in_addr *addr) | |
743 | { | |
744 | struct listnode *cnode; | |
745 | struct connected *connected; | |
746 | struct prefix *cp; | |
747 | ||
748 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { | |
749 | cp = connected->address; | |
750 | if ((cp->family == AF_INET) | |
751 | && !ipv4_martian(&(cp->u.prefix4))) { | |
752 | *addr = cp->u.prefix4; | |
753 | return 1; | |
754 | } | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
759 | ||
760 | bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote, | |
761 | struct bgp_nexthop *nexthop, struct peer *peer) | |
762 | { | |
763 | int ret = 0; | |
764 | struct interface *ifp = NULL; | |
765 | bool v6_ll_avail = true; | |
766 | ||
767 | memset(nexthop, 0, sizeof(struct bgp_nexthop)); | |
768 | ||
769 | if (!local) | |
770 | return false; | |
771 | if (!remote) | |
772 | return false; | |
773 | ||
774 | if (local->sa.sa_family == AF_INET) { | |
775 | nexthop->v4 = local->sin.sin_addr; | |
776 | if (peer->update_if) | |
777 | ifp = if_lookup_by_name(peer->update_if, | |
778 | peer->bgp->vrf_id); | |
779 | else | |
780 | ifp = if_lookup_by_ipv4_exact(&local->sin.sin_addr, | |
781 | peer->bgp->vrf_id); | |
782 | } | |
783 | if (local->sa.sa_family == AF_INET6) { | |
784 | memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, IPV6_MAX_BYTELEN); | |
785 | if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) { | |
786 | if (peer->conf_if || peer->ifname) | |
787 | ifp = if_lookup_by_name(peer->conf_if | |
788 | ? peer->conf_if | |
789 | : peer->ifname, | |
790 | peer->bgp->vrf_id); | |
791 | else if (peer->update_if) | |
792 | ifp = if_lookup_by_name(peer->update_if, | |
793 | peer->bgp->vrf_id); | |
794 | } else 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_ipv6_exact(&local->sin6.sin6_addr, | |
799 | local->sin6.sin6_scope_id, | |
800 | peer->bgp->vrf_id); | |
801 | } | |
802 | ||
803 | if (!ifp) { | |
804 | /* | |
805 | * BGP views do not currently get proper data | |
806 | * from zebra( when attached ) to be able to | |
807 | * properly resolve nexthops, so give this | |
808 | * instance type a pass. | |
809 | */ | |
810 | if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW) | |
811 | return true; | |
812 | /* | |
813 | * If we have no interface data but we have established | |
814 | * some connection w/ zebra than something has gone | |
815 | * terribly terribly wrong here, so say this failed | |
816 | * If we do not any zebra connection then not | |
817 | * having a ifp pointer is ok. | |
818 | */ | |
819 | return zclient_num_connects ? false : true; | |
820 | } | |
821 | ||
822 | nexthop->ifp = ifp; | |
823 | ||
824 | /* IPv4 connection, fetch and store IPv6 local address(es) if any. */ | |
825 | if (local->sa.sa_family == AF_INET) { | |
826 | /* IPv6 nexthop*/ | |
827 | ret = if_get_ipv6_global(ifp, &nexthop->v6_global); | |
828 | ||
829 | if (!ret) { | |
830 | /* There is no global nexthop. Use link-local address as | |
831 | * both the | |
832 | * global and link-local nexthop. In this scenario, the | |
833 | * expectation | |
834 | * for interop is that the network admin would use a | |
835 | * route-map to | |
836 | * specify the global IPv6 nexthop. | |
837 | */ | |
838 | v6_ll_avail = | |
839 | if_get_ipv6_local(ifp, &nexthop->v6_global); | |
840 | memcpy(&nexthop->v6_local, &nexthop->v6_global, | |
841 | IPV6_MAX_BYTELEN); | |
842 | } else | |
843 | v6_ll_avail = | |
844 | if_get_ipv6_local(ifp, &nexthop->v6_local); | |
845 | ||
846 | /* | |
847 | * If we are a v4 connection and we are not doing unnumbered | |
848 | * not having a v6 LL address is ok | |
849 | */ | |
850 | if (!v6_ll_avail && !peer->conf_if) | |
851 | v6_ll_avail = true; | |
852 | if (if_lookup_by_ipv4(&remote->sin.sin_addr, peer->bgp->vrf_id)) | |
853 | peer->shared_network = 1; | |
854 | else | |
855 | peer->shared_network = 0; | |
856 | } | |
857 | ||
858 | /* IPv6 connection, fetch and store IPv4 local address if any. */ | |
859 | if (local->sa.sa_family == AF_INET6) { | |
860 | struct interface *direct = NULL; | |
861 | ||
862 | /* IPv4 nexthop. */ | |
863 | ret = if_get_ipv4_address(ifp, &nexthop->v4); | |
864 | if (!ret && peer->local_id.s_addr != INADDR_ANY) | |
865 | nexthop->v4 = peer->local_id; | |
866 | ||
867 | /* Global address*/ | |
868 | if (!IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr)) { | |
869 | memcpy(&nexthop->v6_global, &local->sin6.sin6_addr, | |
870 | IPV6_MAX_BYTELEN); | |
871 | ||
872 | /* If directory connected set link-local address. */ | |
873 | direct = if_lookup_by_ipv6(&remote->sin6.sin6_addr, | |
874 | remote->sin6.sin6_scope_id, | |
875 | peer->bgp->vrf_id); | |
876 | if (direct) | |
877 | v6_ll_avail = if_get_ipv6_local( | |
878 | ifp, &nexthop->v6_local); | |
879 | /* | |
880 | * It's fine to not have a v6 LL when using | |
881 | * update-source loopback/vrf | |
882 | */ | |
883 | if (!v6_ll_avail && if_is_loopback(ifp)) | |
884 | v6_ll_avail = true; | |
885 | else { | |
886 | flog_warn( | |
887 | EC_BGP_NO_LL_ADDRESS_AVAILABLE, | |
888 | "Interface: %s does not have a v6 LL address associated with it, waiting until one is created for it", | |
889 | ifp->name); | |
890 | } | |
891 | } else | |
892 | /* Link-local address. */ | |
893 | { | |
894 | ret = if_get_ipv6_global(ifp, &nexthop->v6_global); | |
895 | ||
896 | /* If there is no global address. Set link-local | |
897 | address as | |
898 | global. I know this break RFC specification... */ | |
899 | /* In this scenario, the expectation for interop is that | |
900 | * the | |
901 | * network admin would use a route-map to specify the | |
902 | * global | |
903 | * IPv6 nexthop. | |
904 | */ | |
905 | if (!ret) | |
906 | memcpy(&nexthop->v6_global, | |
907 | &local->sin6.sin6_addr, | |
908 | IPV6_MAX_BYTELEN); | |
909 | /* Always set the link-local address */ | |
910 | memcpy(&nexthop->v6_local, &local->sin6.sin6_addr, | |
911 | IPV6_MAX_BYTELEN); | |
912 | } | |
913 | ||
914 | if (IN6_IS_ADDR_LINKLOCAL(&local->sin6.sin6_addr) | |
915 | || if_lookup_by_ipv6(&remote->sin6.sin6_addr, | |
916 | remote->sin6.sin6_scope_id, | |
917 | peer->bgp->vrf_id)) | |
918 | peer->shared_network = 1; | |
919 | else | |
920 | peer->shared_network = 0; | |
921 | } | |
922 | ||
923 | /* KAME stack specific treatment. */ | |
924 | #ifdef KAME | |
925 | if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_global) | |
926 | && IN6_LINKLOCAL_IFINDEX(nexthop->v6_global)) { | |
927 | SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_global, 0); | |
928 | } | |
929 | if (IN6_IS_ADDR_LINKLOCAL(&nexthop->v6_local) | |
930 | && IN6_LINKLOCAL_IFINDEX(nexthop->v6_local)) { | |
931 | SET_IN6_LINKLOCAL_IFINDEX(nexthop->v6_local, 0); | |
932 | } | |
933 | #endif /* KAME */ | |
934 | ||
935 | /* If we have identified the local interface, there is no error for now. | |
936 | */ | |
937 | return v6_ll_avail; | |
938 | } | |
939 | ||
940 | static struct in6_addr * | |
941 | bgp_path_info_to_ipv6_nexthop(struct bgp_path_info *path, ifindex_t *ifindex) | |
942 | { | |
943 | struct in6_addr *nexthop = NULL; | |
944 | ||
945 | /* Only global address nexthop exists. */ | |
946 | if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL | |
947 | || path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV6_GLOBAL) { | |
948 | nexthop = &path->attr->mp_nexthop_global; | |
949 | if (IN6_IS_ADDR_LINKLOCAL(nexthop)) | |
950 | *ifindex = path->attr->nh_ifindex; | |
951 | } | |
952 | ||
953 | /* If both global and link-local address present. */ | |
954 | if (path->attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL | |
955 | || path->attr->mp_nexthop_len | |
956 | == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) { | |
957 | /* Check if route-map is set to prefer global over link-local */ | |
958 | if (path->attr->mp_nexthop_prefer_global) { | |
959 | nexthop = &path->attr->mp_nexthop_global; | |
960 | if (IN6_IS_ADDR_LINKLOCAL(nexthop)) | |
961 | *ifindex = path->attr->nh_ifindex; | |
962 | } else { | |
963 | /* Workaround for Cisco's nexthop bug. */ | |
964 | if (IN6_IS_ADDR_UNSPECIFIED( | |
965 | &path->attr->mp_nexthop_global) | |
966 | && path->peer->su_remote | |
967 | && path->peer->su_remote->sa.sa_family | |
968 | == AF_INET6) { | |
969 | nexthop = | |
970 | &path->peer->su_remote->sin6.sin6_addr; | |
971 | if (IN6_IS_ADDR_LINKLOCAL(nexthop)) | |
972 | *ifindex = path->peer->nexthop.ifp | |
973 | ->ifindex; | |
974 | } else { | |
975 | nexthop = &path->attr->mp_nexthop_local; | |
976 | if (IN6_IS_ADDR_LINKLOCAL(nexthop)) | |
977 | *ifindex = path->attr->nh_lla_ifindex; | |
978 | } | |
979 | } | |
980 | } | |
981 | ||
982 | return nexthop; | |
983 | } | |
984 | ||
985 | static bool bgp_table_map_apply(struct route_map *map, const struct prefix *p, | |
986 | struct bgp_path_info *path) | |
987 | { | |
988 | route_map_result_t ret; | |
989 | ||
990 | ret = route_map_apply(map, p, path); | |
991 | bgp_attr_flush(path->attr); | |
992 | ||
993 | if (ret != RMAP_DENYMATCH) | |
994 | return true; | |
995 | ||
996 | if (bgp_debug_zebra(p)) { | |
997 | if (p->family == AF_INET) { | |
998 | zlog_debug( | |
999 | "Zebra rmap deny: IPv4 route %pFX nexthop %pI4", | |
1000 | p, &path->attr->nexthop); | |
1001 | } | |
1002 | if (p->family == AF_INET6) { | |
1003 | char buf[2][INET6_ADDRSTRLEN]; | |
1004 | ifindex_t ifindex; | |
1005 | struct in6_addr *nexthop; | |
1006 | ||
1007 | nexthop = bgp_path_info_to_ipv6_nexthop(path, &ifindex); | |
1008 | zlog_debug( | |
1009 | "Zebra rmap deny: IPv6 route %pFX nexthop %s", | |
1010 | p, | |
1011 | nexthop ? inet_ntop(AF_INET6, nexthop, buf[1], | |
1012 | sizeof(buf[1])) | |
1013 | : inet_ntop(AF_INET, | |
1014 | &path->attr->nexthop, | |
1015 | buf[1], sizeof(buf[1]))); | |
1016 | } | |
1017 | } | |
1018 | return false; | |
1019 | } | |
1020 | ||
1021 | static struct thread *bgp_tm_thread_connect; | |
1022 | static bool bgp_tm_status_connected; | |
1023 | static bool bgp_tm_chunk_obtained; | |
1024 | #define BGP_FLOWSPEC_TABLE_CHUNK 100000 | |
1025 | static uint32_t bgp_tm_min, bgp_tm_max, bgp_tm_chunk_size; | |
1026 | struct bgp *bgp_tm_bgp; | |
1027 | ||
1028 | static void bgp_zebra_tm_connect(struct thread *t) | |
1029 | { | |
1030 | struct zclient *zclient; | |
1031 | int delay = 10, ret = 0; | |
1032 | ||
1033 | zclient = THREAD_ARG(t); | |
1034 | if (bgp_tm_status_connected && zclient->sock > 0) | |
1035 | delay = 60; | |
1036 | else { | |
1037 | bgp_tm_status_connected = false; | |
1038 | ret = tm_table_manager_connect(zclient); | |
1039 | } | |
1040 | if (ret < 0) { | |
1041 | zlog_info("Error connecting to table manager!"); | |
1042 | bgp_tm_status_connected = false; | |
1043 | } else { | |
1044 | if (!bgp_tm_status_connected) | |
1045 | zlog_debug("Connecting to table manager. Success"); | |
1046 | bgp_tm_status_connected = true; | |
1047 | if (!bgp_tm_chunk_obtained) { | |
1048 | if (bgp_zebra_get_table_range(bgp_tm_chunk_size, | |
1049 | &bgp_tm_min, | |
1050 | &bgp_tm_max) >= 0) { | |
1051 | bgp_tm_chunk_obtained = true; | |
1052 | /* parse non installed entries */ | |
1053 | bgp_zebra_announce_table(bgp_tm_bgp, AFI_IP, SAFI_FLOWSPEC); | |
1054 | } | |
1055 | } | |
1056 | } | |
1057 | thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay, | |
1058 | &bgp_tm_thread_connect); | |
1059 | } | |
1060 | ||
1061 | bool bgp_zebra_tm_chunk_obtained(void) | |
1062 | { | |
1063 | return bgp_tm_chunk_obtained; | |
1064 | } | |
1065 | ||
1066 | uint32_t bgp_zebra_tm_get_id(void) | |
1067 | { | |
1068 | static int table_id; | |
1069 | ||
1070 | if (!bgp_tm_chunk_obtained) | |
1071 | return ++table_id; | |
1072 | return bgp_tm_min++; | |
1073 | } | |
1074 | ||
1075 | void bgp_zebra_init_tm_connect(struct bgp *bgp) | |
1076 | { | |
1077 | int delay = 1; | |
1078 | ||
1079 | /* if already set, do nothing | |
1080 | */ | |
1081 | if (bgp_tm_thread_connect != NULL) | |
1082 | return; | |
1083 | bgp_tm_status_connected = false; | |
1084 | bgp_tm_chunk_obtained = false; | |
1085 | bgp_tm_min = bgp_tm_max = 0; | |
1086 | bgp_tm_chunk_size = BGP_FLOWSPEC_TABLE_CHUNK; | |
1087 | bgp_tm_bgp = bgp; | |
1088 | thread_add_timer(bm->master, bgp_zebra_tm_connect, zclient, delay, | |
1089 | &bgp_tm_thread_connect); | |
1090 | } | |
1091 | ||
1092 | int bgp_zebra_get_table_range(uint32_t chunk_size, | |
1093 | uint32_t *start, uint32_t *end) | |
1094 | { | |
1095 | int ret; | |
1096 | ||
1097 | if (!bgp_tm_status_connected) | |
1098 | return -1; | |
1099 | ret = tm_get_table_chunk(zclient, chunk_size, start, end); | |
1100 | if (ret < 0) { | |
1101 | flog_err(EC_BGP_TABLE_CHUNK, | |
1102 | "BGP: Error getting table chunk %u", chunk_size); | |
1103 | return -1; | |
1104 | } | |
1105 | zlog_info("BGP: Table Manager returns range from chunk %u is [%u %u]", | |
1106 | chunk_size, *start, *end); | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | static bool update_ipv4nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp, | |
1111 | struct in_addr *nexthop, | |
1112 | struct attr *attr, bool is_evpn, | |
1113 | struct zapi_nexthop *api_nh) | |
1114 | { | |
1115 | api_nh->gate.ipv4 = *nexthop; | |
1116 | api_nh->vrf_id = nh_bgp->vrf_id; | |
1117 | ||
1118 | /* Need to set fields appropriately for EVPN routes imported into | |
1119 | * a VRF (which are programmed as onlink on l3-vni SVI) as well as | |
1120 | * connected routes leaked into a VRF. | |
1121 | */ | |
1122 | if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) { | |
1123 | api_nh->type = attr->nh_type; | |
1124 | api_nh->bh_type = attr->bh_type; | |
1125 | } else if (is_evpn) { | |
1126 | /* | |
1127 | * If the nexthop is EVPN overlay index gateway IP, | |
1128 | * treat the nexthop as NEXTHOP_TYPE_IPV4 | |
1129 | * Else, mark the nexthop as onlink. | |
1130 | */ | |
1131 | if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) | |
1132 | api_nh->type = NEXTHOP_TYPE_IPV4; | |
1133 | else { | |
1134 | api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1135 | SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK); | |
1136 | api_nh->ifindex = nh_bgp->l3vni_svi_ifindex; | |
1137 | } | |
1138 | } else if (nh_othervrf && api_nh->gate.ipv4.s_addr == INADDR_ANY) { | |
1139 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
1140 | api_nh->ifindex = attr->nh_ifindex; | |
1141 | } else | |
1142 | api_nh->type = NEXTHOP_TYPE_IPV4; | |
1143 | ||
1144 | return true; | |
1145 | } | |
1146 | ||
1147 | static bool update_ipv6nh_for_route_install(int nh_othervrf, struct bgp *nh_bgp, | |
1148 | struct in6_addr *nexthop, | |
1149 | ifindex_t ifindex, | |
1150 | struct bgp_path_info *pi, | |
1151 | struct bgp_path_info *best_pi, | |
1152 | bool is_evpn, | |
1153 | struct zapi_nexthop *api_nh) | |
1154 | { | |
1155 | struct attr *attr; | |
1156 | ||
1157 | attr = pi->attr; | |
1158 | api_nh->vrf_id = nh_bgp->vrf_id; | |
1159 | ||
1160 | if (attr->nh_type == NEXTHOP_TYPE_BLACKHOLE) { | |
1161 | api_nh->type = attr->nh_type; | |
1162 | api_nh->bh_type = attr->bh_type; | |
1163 | } else if (is_evpn) { | |
1164 | /* | |
1165 | * If the nexthop is EVPN overlay index gateway IP, | |
1166 | * treat the nexthop as NEXTHOP_TYPE_IPV4 | |
1167 | * Else, mark the nexthop as onlink. | |
1168 | */ | |
1169 | if (attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) | |
1170 | api_nh->type = NEXTHOP_TYPE_IPV6; | |
1171 | else { | |
1172 | api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1173 | SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK); | |
1174 | api_nh->ifindex = nh_bgp->l3vni_svi_ifindex; | |
1175 | } | |
1176 | } else if (nh_othervrf) { | |
1177 | if (IN6_IS_ADDR_UNSPECIFIED(nexthop)) { | |
1178 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
1179 | api_nh->ifindex = attr->nh_ifindex; | |
1180 | } else if (IN6_IS_ADDR_LINKLOCAL(nexthop)) { | |
1181 | if (ifindex == 0) | |
1182 | return false; | |
1183 | api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1184 | api_nh->ifindex = ifindex; | |
1185 | } else { | |
1186 | api_nh->type = NEXTHOP_TYPE_IPV6; | |
1187 | api_nh->ifindex = 0; | |
1188 | } | |
1189 | } else { | |
1190 | if (IN6_IS_ADDR_LINKLOCAL(nexthop)) { | |
1191 | if (pi == best_pi | |
1192 | && attr->mp_nexthop_len | |
1193 | == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) | |
1194 | if (pi->peer->nexthop.ifp) | |
1195 | ifindex = | |
1196 | pi->peer->nexthop.ifp->ifindex; | |
1197 | if (!ifindex) { | |
1198 | if (pi->peer->conf_if) | |
1199 | ifindex = pi->peer->ifp->ifindex; | |
1200 | else if (pi->peer->ifname) | |
1201 | ifindex = ifname2ifindex( | |
1202 | pi->peer->ifname, | |
1203 | pi->peer->bgp->vrf_id); | |
1204 | else if (pi->peer->nexthop.ifp) | |
1205 | ifindex = | |
1206 | pi->peer->nexthop.ifp->ifindex; | |
1207 | } | |
1208 | ||
1209 | if (ifindex == 0) | |
1210 | return false; | |
1211 | api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1212 | api_nh->ifindex = ifindex; | |
1213 | } else { | |
1214 | api_nh->type = NEXTHOP_TYPE_IPV6; | |
1215 | api_nh->ifindex = 0; | |
1216 | } | |
1217 | } | |
1218 | /* api_nh structure has union of gate and bh_type */ | |
1219 | if (nexthop && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) | |
1220 | api_nh->gate.ipv6 = *nexthop; | |
1221 | ||
1222 | return true; | |
1223 | } | |
1224 | ||
1225 | static bool bgp_zebra_use_nhop_weighted(struct bgp *bgp, struct attr *attr, | |
1226 | uint64_t tot_bw, uint32_t *nh_weight) | |
1227 | { | |
1228 | uint32_t bw; | |
1229 | uint64_t tmp; | |
1230 | ||
1231 | bw = attr->link_bw; | |
1232 | /* zero link-bandwidth and link-bandwidth not present are treated | |
1233 | * as the same situation. | |
1234 | */ | |
1235 | if (!bw) { | |
1236 | /* the only situations should be if we're either told | |
1237 | * to skip or use default weight. | |
1238 | */ | |
1239 | if (bgp->lb_handling == BGP_LINK_BW_SKIP_MISSING) | |
1240 | return false; | |
1241 | *nh_weight = BGP_ZEBRA_DEFAULT_NHOP_WEIGHT; | |
1242 | } else { | |
1243 | tmp = (uint64_t)bw * 100; | |
1244 | *nh_weight = ((uint32_t)(tmp / tot_bw)); | |
1245 | } | |
1246 | ||
1247 | return true; | |
1248 | } | |
1249 | ||
1250 | void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p, | |
1251 | struct bgp_path_info *info, struct bgp *bgp, afi_t afi, | |
1252 | safi_t safi) | |
1253 | { | |
1254 | struct zapi_route api = { 0 }; | |
1255 | struct zapi_nexthop *api_nh; | |
1256 | int nh_family; | |
1257 | unsigned int valid_nh_count = 0; | |
1258 | bool allow_recursion = false; | |
1259 | uint8_t distance; | |
1260 | struct peer *peer; | |
1261 | struct bgp_path_info *mpinfo; | |
1262 | struct bgp *bgp_orig; | |
1263 | uint32_t metric; | |
1264 | struct attr local_attr; | |
1265 | struct bgp_path_info local_info; | |
1266 | struct bgp_path_info *mpinfo_cp = &local_info; | |
1267 | route_tag_t tag; | |
1268 | mpls_label_t label; | |
1269 | struct bgp_sid_info *sid_info; | |
1270 | int nh_othervrf = 0; | |
1271 | bool is_evpn; | |
1272 | bool nh_updated = false; | |
1273 | bool do_wt_ecmp; | |
1274 | uint64_t cum_bw = 0; | |
1275 | uint32_t nhg_id = 0; | |
1276 | bool is_add; | |
1277 | ||
1278 | /* Don't try to install if we're not connected to Zebra or Zebra doesn't | |
1279 | * know of this instance. | |
1280 | */ | |
1281 | if (!bgp_install_info_to_zebra(bgp)) | |
1282 | return; | |
1283 | ||
1284 | if (bgp->main_zebra_update_hold) | |
1285 | return; | |
1286 | ||
1287 | if (safi == SAFI_FLOWSPEC) { | |
1288 | bgp_pbr_update_entry(bgp, bgp_dest_get_prefix(dest), info, afi, | |
1289 | safi, true); | |
1290 | return; | |
1291 | } | |
1292 | ||
1293 | /* | |
1294 | * vrf leaking support (will have only one nexthop) | |
1295 | */ | |
1296 | if (info->extra && info->extra->bgp_orig) | |
1297 | nh_othervrf = 1; | |
1298 | ||
1299 | /* Make Zebra API structure. */ | |
1300 | api.vrf_id = bgp->vrf_id; | |
1301 | api.type = ZEBRA_ROUTE_BGP; | |
1302 | api.safi = safi; | |
1303 | api.prefix = *p; | |
1304 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
1305 | ||
1306 | peer = info->peer; | |
1307 | ||
1308 | if (info->type == ZEBRA_ROUTE_BGP | |
1309 | && info->sub_type == BGP_ROUTE_IMPORTED) { | |
1310 | ||
1311 | /* Obtain peer from parent */ | |
1312 | if (info->extra && info->extra->parent) | |
1313 | peer = ((struct bgp_path_info *)(info->extra->parent)) | |
1314 | ->peer; | |
1315 | } | |
1316 | ||
1317 | tag = info->attr->tag; | |
1318 | ||
1319 | /* If the route's source is EVPN, flag as such. */ | |
1320 | is_evpn = is_route_parent_evpn(info); | |
1321 | if (is_evpn) | |
1322 | SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE); | |
1323 | ||
1324 | if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED | |
1325 | || info->sub_type == BGP_ROUTE_AGGREGATE) { | |
1326 | SET_FLAG(api.flags, ZEBRA_FLAG_IBGP); | |
1327 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); | |
1328 | } | |
1329 | ||
1330 | if ((peer->sort == BGP_PEER_EBGP && peer->ttl != BGP_DEFAULT_TTL) | |
1331 | || CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK) | |
1332 | || CHECK_FLAG(bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK)) | |
1333 | ||
1334 | allow_recursion = true; | |
1335 | ||
1336 | if (info->attr->rmap_table_id) { | |
1337 | SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID); | |
1338 | api.tableid = info->attr->rmap_table_id; | |
1339 | } | |
1340 | ||
1341 | if (CHECK_FLAG(info->attr->flag, ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR))) | |
1342 | SET_FLAG(api.message, ZAPI_MESSAGE_SRTE); | |
1343 | ||
1344 | /* Metric is currently based on the best-path only */ | |
1345 | metric = info->attr->med; | |
1346 | ||
1347 | /* Determine if we're doing weighted ECMP or not */ | |
1348 | do_wt_ecmp = bgp_path_info_mpath_chkwtd(bgp, info); | |
1349 | if (do_wt_ecmp) | |
1350 | cum_bw = bgp_path_info_mpath_cumbw(info); | |
1351 | ||
1352 | /* EVPN MAC-IP routes are installed with a L3 NHG id */ | |
1353 | if (bgp_evpn_path_es_use_nhg(bgp, info, &nhg_id)) { | |
1354 | mpinfo = NULL; | |
1355 | api.nhgid = nhg_id; | |
1356 | if (nhg_id) | |
1357 | SET_FLAG(api.message, ZAPI_MESSAGE_NHG); | |
1358 | } else { | |
1359 | mpinfo = info; | |
1360 | } | |
1361 | ||
1362 | for (; mpinfo; mpinfo = bgp_path_info_mpath_next(mpinfo)) { | |
1363 | uint32_t nh_weight; | |
1364 | ||
1365 | if (valid_nh_count >= multipath_num) | |
1366 | break; | |
1367 | ||
1368 | *mpinfo_cp = *mpinfo; | |
1369 | nh_weight = 0; | |
1370 | ||
1371 | /* Get nexthop address-family */ | |
1372 | if (p->family == AF_INET | |
1373 | && !BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr)) | |
1374 | nh_family = AF_INET; | |
1375 | else if (p->family == AF_INET6 | |
1376 | || (p->family == AF_INET | |
1377 | && BGP_ATTR_NEXTHOP_AFI_IP6(mpinfo_cp->attr))) | |
1378 | nh_family = AF_INET6; | |
1379 | else | |
1380 | continue; | |
1381 | ||
1382 | /* If processing for weighted ECMP, determine the next hop's | |
1383 | * weight. Based on user setting, we may skip the next hop | |
1384 | * in some situations. | |
1385 | */ | |
1386 | if (do_wt_ecmp) { | |
1387 | if (!bgp_zebra_use_nhop_weighted(bgp, mpinfo->attr, | |
1388 | cum_bw, &nh_weight)) | |
1389 | continue; | |
1390 | } | |
1391 | api_nh = &api.nexthops[valid_nh_count]; | |
1392 | ||
1393 | if (CHECK_FLAG(info->attr->flag, | |
1394 | ATTR_FLAG_BIT(BGP_ATTR_SRTE_COLOR))) | |
1395 | api_nh->srte_color = info->attr->srte_color; | |
1396 | ||
1397 | if (bgp_debug_zebra(&api.prefix)) { | |
1398 | if (mpinfo->extra) { | |
1399 | zlog_debug("%s: p=%pFX, bgp_is_valid_label: %d", | |
1400 | __func__, p, | |
1401 | bgp_is_valid_label( | |
1402 | &mpinfo->extra->label[0])); | |
1403 | } else { | |
1404 | zlog_debug( | |
1405 | "%s: p=%pFX, extra is NULL, no label", | |
1406 | __func__, p); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | if (bgp->table_map[afi][safi].name) { | |
1411 | /* Copy info and attributes, so the route-map | |
1412 | apply doesn't modify the BGP route info. */ | |
1413 | local_attr = *mpinfo->attr; | |
1414 | mpinfo_cp->attr = &local_attr; | |
1415 | if (!bgp_table_map_apply(bgp->table_map[afi][safi].map, | |
1416 | p, mpinfo_cp)) | |
1417 | continue; | |
1418 | ||
1419 | /* metric/tag is only allowed to be | |
1420 | * overridden on 1st nexthop */ | |
1421 | if (mpinfo == info) { | |
1422 | metric = mpinfo_cp->attr->med; | |
1423 | tag = mpinfo_cp->attr->tag; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | BGP_ORIGINAL_UPDATE(bgp_orig, mpinfo, bgp); | |
1428 | ||
1429 | if (nh_family == AF_INET) { | |
1430 | nh_updated = update_ipv4nh_for_route_install( | |
1431 | nh_othervrf, bgp_orig, | |
1432 | &mpinfo_cp->attr->nexthop, mpinfo_cp->attr, | |
1433 | is_evpn, api_nh); | |
1434 | } else { | |
1435 | ifindex_t ifindex = IFINDEX_INTERNAL; | |
1436 | struct in6_addr *nexthop; | |
1437 | ||
1438 | nexthop = bgp_path_info_to_ipv6_nexthop(mpinfo_cp, | |
1439 | &ifindex); | |
1440 | ||
1441 | if (!nexthop) | |
1442 | nh_updated = update_ipv4nh_for_route_install( | |
1443 | nh_othervrf, bgp_orig, | |
1444 | &mpinfo_cp->attr->nexthop, | |
1445 | mpinfo_cp->attr, is_evpn, api_nh); | |
1446 | else | |
1447 | nh_updated = update_ipv6nh_for_route_install( | |
1448 | nh_othervrf, bgp_orig, nexthop, ifindex, | |
1449 | mpinfo, info, is_evpn, api_nh); | |
1450 | } | |
1451 | ||
1452 | /* Did we get proper nexthop info to update zebra? */ | |
1453 | if (!nh_updated) | |
1454 | continue; | |
1455 | ||
1456 | /* Allow recursion if it is a multipath group with both | |
1457 | * eBGP and iBGP paths. | |
1458 | */ | |
1459 | if (!allow_recursion | |
1460 | && CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX) | |
1461 | && (mpinfo->peer->sort == BGP_PEER_IBGP | |
1462 | || mpinfo->peer->sort == BGP_PEER_CONFED)) | |
1463 | allow_recursion = true; | |
1464 | ||
1465 | if (mpinfo->extra | |
1466 | && bgp_is_valid_label(&mpinfo->extra->label[0]) | |
1467 | && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) { | |
1468 | label = label_pton(&mpinfo->extra->label[0]); | |
1469 | ||
1470 | SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL); | |
1471 | ||
1472 | api_nh->label_num = 1; | |
1473 | api_nh->labels[0] = label; | |
1474 | } | |
1475 | ||
1476 | if (is_evpn | |
1477 | && mpinfo->attr->evpn_overlay.type | |
1478 | != OVERLAY_INDEX_GATEWAY_IP) | |
1479 | memcpy(&api_nh->rmac, &(mpinfo->attr->rmac), | |
1480 | sizeof(struct ethaddr)); | |
1481 | ||
1482 | api_nh->weight = nh_weight; | |
1483 | ||
1484 | if (mpinfo->extra && !sid_zero(&mpinfo->extra->sid[0].sid) | |
1485 | && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) { | |
1486 | sid_info = &mpinfo->extra->sid[0]; | |
1487 | ||
1488 | memcpy(&api_nh->seg6_segs, &sid_info->sid, | |
1489 | sizeof(api_nh->seg6_segs)); | |
1490 | ||
1491 | if (sid_info->transposition_len != 0) { | |
1492 | if (!bgp_is_valid_label( | |
1493 | &mpinfo->extra->label[0])) | |
1494 | continue; | |
1495 | ||
1496 | label = label_pton(&mpinfo->extra->label[0]); | |
1497 | transpose_sid(&api_nh->seg6_segs, label, | |
1498 | sid_info->transposition_offset, | |
1499 | sid_info->transposition_len); | |
1500 | } | |
1501 | ||
1502 | SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6); | |
1503 | } | |
1504 | ||
1505 | valid_nh_count++; | |
1506 | } | |
1507 | ||
1508 | is_add = (valid_nh_count || nhg_id) ? true : false; | |
1509 | ||
1510 | if (is_add && CHECK_FLAG(bm->flags, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA)) { | |
1511 | struct bgp_zebra_opaque bzo = {}; | |
1512 | const char *reason = | |
1513 | bgp_path_selection_reason2str(dest->reason); | |
1514 | ||
1515 | strlcpy(bzo.aspath, info->attr->aspath->str, | |
1516 | sizeof(bzo.aspath)); | |
1517 | ||
1518 | if (info->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) | |
1519 | strlcpy(bzo.community, | |
1520 | bgp_attr_get_community(info->attr)->str, | |
1521 | sizeof(bzo.community)); | |
1522 | ||
1523 | if (info->attr->flag | |
1524 | & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) | |
1525 | strlcpy(bzo.lcommunity, | |
1526 | bgp_attr_get_lcommunity(info->attr)->str, | |
1527 | sizeof(bzo.lcommunity)); | |
1528 | ||
1529 | strlcpy(bzo.selection_reason, reason, | |
1530 | sizeof(bzo.selection_reason)); | |
1531 | ||
1532 | SET_FLAG(api.message, ZAPI_MESSAGE_OPAQUE); | |
1533 | api.opaque.length = MIN(sizeof(struct bgp_zebra_opaque), | |
1534 | ZAPI_MESSAGE_OPAQUE_LENGTH); | |
1535 | memcpy(api.opaque.data, &bzo, api.opaque.length); | |
1536 | } | |
1537 | ||
1538 | if (allow_recursion) | |
1539 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); | |
1540 | ||
1541 | /* | |
1542 | * When we create an aggregate route we must also | |
1543 | * install a Null0 route in the RIB, so overwrite | |
1544 | * what was written into api with a blackhole route | |
1545 | */ | |
1546 | if (info->sub_type == BGP_ROUTE_AGGREGATE) | |
1547 | zapi_route_set_blackhole(&api, BLACKHOLE_NULL); | |
1548 | else | |
1549 | api.nexthop_num = valid_nh_count; | |
1550 | ||
1551 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); | |
1552 | api.metric = metric; | |
1553 | ||
1554 | if (tag) { | |
1555 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
1556 | api.tag = tag; | |
1557 | } | |
1558 | ||
1559 | distance = bgp_distance_apply(p, info, afi, safi, bgp); | |
1560 | if (distance) { | |
1561 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
1562 | api.distance = distance; | |
1563 | } | |
1564 | ||
1565 | if (bgp_debug_zebra(p)) { | |
1566 | char nh_buf[INET6_ADDRSTRLEN]; | |
1567 | char eth_buf[ETHER_ADDR_STRLEN + 7] = {'\0'}; | |
1568 | char buf1[ETHER_ADDR_STRLEN]; | |
1569 | char label_buf[20]; | |
1570 | char sid_buf[20]; | |
1571 | char segs_buf[256]; | |
1572 | int i; | |
1573 | ||
1574 | zlog_debug( | |
1575 | "Tx route %s VRF %u %pFX metric %u tag %" ROUTE_TAG_PRI | |
1576 | " count %d nhg %d", | |
1577 | valid_nh_count ? "add" : "delete", bgp->vrf_id, | |
1578 | &api.prefix, api.metric, api.tag, api.nexthop_num, | |
1579 | nhg_id); | |
1580 | for (i = 0; i < api.nexthop_num; i++) { | |
1581 | api_nh = &api.nexthops[i]; | |
1582 | ||
1583 | switch (api_nh->type) { | |
1584 | case NEXTHOP_TYPE_IFINDEX: | |
1585 | nh_buf[0] = '\0'; | |
1586 | break; | |
1587 | case NEXTHOP_TYPE_IPV4: | |
1588 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1589 | nh_family = AF_INET; | |
1590 | inet_ntop(nh_family, &api_nh->gate, nh_buf, | |
1591 | sizeof(nh_buf)); | |
1592 | break; | |
1593 | case NEXTHOP_TYPE_IPV6: | |
1594 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1595 | nh_family = AF_INET6; | |
1596 | inet_ntop(nh_family, &api_nh->gate, nh_buf, | |
1597 | sizeof(nh_buf)); | |
1598 | break; | |
1599 | case NEXTHOP_TYPE_BLACKHOLE: | |
1600 | strlcpy(nh_buf, "blackhole", sizeof(nh_buf)); | |
1601 | break; | |
1602 | default: | |
1603 | /* Note: add new nexthop case */ | |
1604 | assert(0); | |
1605 | break; | |
1606 | } | |
1607 | ||
1608 | label_buf[0] = '\0'; | |
1609 | eth_buf[0] = '\0'; | |
1610 | segs_buf[0] = '\0'; | |
1611 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL) | |
1612 | && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) | |
1613 | snprintf(label_buf, sizeof(label_buf), | |
1614 | "label %u", api_nh->labels[0]); | |
1615 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6) | |
1616 | && !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) { | |
1617 | inet_ntop(AF_INET6, &api_nh->seg6_segs, | |
1618 | sid_buf, sizeof(sid_buf)); | |
1619 | snprintf(segs_buf, sizeof(segs_buf), "segs %s", | |
1620 | sid_buf); | |
1621 | } | |
1622 | if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE) | |
1623 | && !is_zero_mac(&api_nh->rmac)) | |
1624 | snprintf(eth_buf, sizeof(eth_buf), " RMAC %s", | |
1625 | prefix_mac2str(&api_nh->rmac, | |
1626 | buf1, sizeof(buf1))); | |
1627 | zlog_debug(" nhop [%d]: %s if %u VRF %u wt %u %s %s %s", | |
1628 | i + 1, nh_buf, api_nh->ifindex, | |
1629 | api_nh->vrf_id, api_nh->weight, | |
1630 | label_buf, segs_buf, eth_buf); | |
1631 | } | |
1632 | ||
1633 | int recursion_flag = 0; | |
1634 | ||
1635 | if (CHECK_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION)) | |
1636 | recursion_flag = 1; | |
1637 | ||
1638 | zlog_debug("%s: %pFX: announcing to zebra (recursion %sset)", | |
1639 | __func__, p, (recursion_flag ? "" : "NOT ")); | |
1640 | } | |
1641 | zclient_route_send(is_add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, | |
1642 | zclient, &api); | |
1643 | } | |
1644 | ||
1645 | /* Announce all routes of a table to zebra */ | |
1646 | void bgp_zebra_announce_table(struct bgp *bgp, afi_t afi, safi_t safi) | |
1647 | { | |
1648 | struct bgp_dest *dest; | |
1649 | struct bgp_table *table; | |
1650 | struct bgp_path_info *pi; | |
1651 | ||
1652 | /* Don't try to install if we're not connected to Zebra or Zebra doesn't | |
1653 | * know of this instance. | |
1654 | */ | |
1655 | if (!bgp_install_info_to_zebra(bgp)) | |
1656 | return; | |
1657 | ||
1658 | table = bgp->rib[afi][safi]; | |
1659 | if (!table) | |
1660 | return; | |
1661 | ||
1662 | for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) | |
1663 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) | |
1664 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) && | |
1665 | ||
1666 | (pi->type == ZEBRA_ROUTE_BGP | |
1667 | && (pi->sub_type == BGP_ROUTE_NORMAL | |
1668 | || pi->sub_type == BGP_ROUTE_IMPORTED))) | |
1669 | ||
1670 | bgp_zebra_announce(dest, | |
1671 | bgp_dest_get_prefix(dest), | |
1672 | pi, bgp, afi, safi); | |
1673 | } | |
1674 | ||
1675 | /* Announce routes of any bgp subtype of a table to zebra */ | |
1676 | void bgp_zebra_announce_table_all_subtypes(struct bgp *bgp, afi_t afi, | |
1677 | safi_t safi) | |
1678 | { | |
1679 | struct bgp_dest *dest; | |
1680 | struct bgp_table *table; | |
1681 | struct bgp_path_info *pi; | |
1682 | ||
1683 | if (!bgp_install_info_to_zebra(bgp)) | |
1684 | return; | |
1685 | ||
1686 | table = bgp->rib[afi][safi]; | |
1687 | if (!table) | |
1688 | return; | |
1689 | ||
1690 | for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) | |
1691 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) | |
1692 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) && | |
1693 | pi->type == ZEBRA_ROUTE_BGP) | |
1694 | bgp_zebra_announce(dest, | |
1695 | bgp_dest_get_prefix(dest), | |
1696 | pi, bgp, afi, safi); | |
1697 | } | |
1698 | ||
1699 | void bgp_zebra_withdraw(const struct prefix *p, struct bgp_path_info *info, | |
1700 | struct bgp *bgp, safi_t safi) | |
1701 | { | |
1702 | struct zapi_route api; | |
1703 | struct peer *peer; | |
1704 | ||
1705 | /* Don't try to install if we're not connected to Zebra or Zebra doesn't | |
1706 | * know of this instance. | |
1707 | */ | |
1708 | if (!bgp_install_info_to_zebra(bgp)) | |
1709 | return; | |
1710 | ||
1711 | if (safi == SAFI_FLOWSPEC) { | |
1712 | peer = info->peer; | |
1713 | bgp_pbr_update_entry(peer->bgp, p, info, AFI_IP, safi, false); | |
1714 | return; | |
1715 | } | |
1716 | ||
1717 | memset(&api, 0, sizeof(api)); | |
1718 | api.vrf_id = bgp->vrf_id; | |
1719 | api.type = ZEBRA_ROUTE_BGP; | |
1720 | api.safi = safi; | |
1721 | api.prefix = *p; | |
1722 | ||
1723 | if (info->attr->rmap_table_id) { | |
1724 | SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID); | |
1725 | api.tableid = info->attr->rmap_table_id; | |
1726 | } | |
1727 | ||
1728 | /* If the route's source is EVPN, flag as such. */ | |
1729 | if (is_route_parent_evpn(info)) | |
1730 | SET_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE); | |
1731 | ||
1732 | if (bgp_debug_zebra(p)) | |
1733 | zlog_debug("Tx route delete VRF %u %pFX", bgp->vrf_id, | |
1734 | &api.prefix); | |
1735 | ||
1736 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
1737 | } | |
1738 | ||
1739 | /* Withdraw all entries in a BGP instances RIB table from Zebra */ | |
1740 | void bgp_zebra_withdraw_table_all_subtypes(struct bgp *bgp, afi_t afi, safi_t safi) | |
1741 | { | |
1742 | struct bgp_dest *dest; | |
1743 | struct bgp_table *table; | |
1744 | struct bgp_path_info *pi; | |
1745 | ||
1746 | if (!bgp_install_info_to_zebra(bgp)) | |
1747 | return; | |
1748 | ||
1749 | table = bgp->rib[afi][safi]; | |
1750 | if (!table) | |
1751 | return; | |
1752 | ||
1753 | for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) { | |
1754 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) { | |
1755 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED) | |
1756 | && (pi->type == ZEBRA_ROUTE_BGP)) | |
1757 | bgp_zebra_withdraw(bgp_dest_get_prefix(dest), | |
1758 | pi, bgp, safi); | |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | struct bgp_redist *bgp_redist_lookup(struct bgp *bgp, afi_t afi, uint8_t type, | |
1764 | unsigned short instance) | |
1765 | { | |
1766 | struct list *red_list; | |
1767 | struct listnode *node; | |
1768 | struct bgp_redist *red; | |
1769 | ||
1770 | red_list = bgp->redist[afi][type]; | |
1771 | if (!red_list) | |
1772 | return (NULL); | |
1773 | ||
1774 | for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) | |
1775 | if (red->instance == instance) | |
1776 | return red; | |
1777 | ||
1778 | return NULL; | |
1779 | } | |
1780 | ||
1781 | struct bgp_redist *bgp_redist_add(struct bgp *bgp, afi_t afi, uint8_t type, | |
1782 | unsigned short instance) | |
1783 | { | |
1784 | struct list *red_list; | |
1785 | struct bgp_redist *red; | |
1786 | ||
1787 | red = bgp_redist_lookup(bgp, afi, type, instance); | |
1788 | if (red) | |
1789 | return red; | |
1790 | ||
1791 | if (!bgp->redist[afi][type]) | |
1792 | bgp->redist[afi][type] = list_new(); | |
1793 | ||
1794 | red_list = bgp->redist[afi][type]; | |
1795 | red = XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist)); | |
1796 | red->instance = instance; | |
1797 | ||
1798 | listnode_add(red_list, red); | |
1799 | ||
1800 | return red; | |
1801 | } | |
1802 | ||
1803 | static void bgp_redist_del(struct bgp *bgp, afi_t afi, uint8_t type, | |
1804 | unsigned short instance) | |
1805 | { | |
1806 | struct bgp_redist *red; | |
1807 | ||
1808 | red = bgp_redist_lookup(bgp, afi, type, instance); | |
1809 | ||
1810 | if (red) { | |
1811 | listnode_delete(bgp->redist[afi][type], red); | |
1812 | XFREE(MTYPE_BGP_REDIST, red); | |
1813 | if (!bgp->redist[afi][type]->count) | |
1814 | list_delete(&bgp->redist[afi][type]); | |
1815 | } | |
1816 | } | |
1817 | ||
1818 | /* Other routes redistribution into BGP. */ | |
1819 | int bgp_redistribute_set(struct bgp *bgp, afi_t afi, int type, | |
1820 | unsigned short instance, bool changed) | |
1821 | { | |
1822 | /* If redistribute options are changed call | |
1823 | * bgp_redistribute_unreg() to reset the option and withdraw | |
1824 | * the routes | |
1825 | */ | |
1826 | if (changed) | |
1827 | bgp_redistribute_unreg(bgp, afi, type, instance); | |
1828 | ||
1829 | /* Return if already redistribute flag is set. */ | |
1830 | if (instance) { | |
1831 | if (redist_check_instance(&zclient->mi_redist[afi][type], | |
1832 | instance)) | |
1833 | return CMD_WARNING; | |
1834 | ||
1835 | redist_add_instance(&zclient->mi_redist[afi][type], instance); | |
1836 | } else { | |
1837 | if (vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id)) | |
1838 | return CMD_WARNING; | |
1839 | ||
1840 | #ifdef ENABLE_BGP_VNC | |
1841 | if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) { | |
1842 | vnc_export_bgp_enable( | |
1843 | bgp, afi); /* only enables if mode bits cfg'd */ | |
1844 | } | |
1845 | #endif | |
1846 | ||
1847 | vrf_bitmap_set(zclient->redist[afi][type], bgp->vrf_id); | |
1848 | } | |
1849 | ||
1850 | /* | |
1851 | * Don't try to register if we're not connected to Zebra or Zebra | |
1852 | * doesn't know of this instance. | |
1853 | * | |
1854 | * When we come up later well resend if needed. | |
1855 | */ | |
1856 | if (!bgp_install_info_to_zebra(bgp)) | |
1857 | return CMD_SUCCESS; | |
1858 | ||
1859 | if (BGP_DEBUG(zebra, ZEBRA)) | |
1860 | zlog_debug("Tx redistribute add VRF %u afi %d %s %d", | |
1861 | bgp->vrf_id, afi, zebra_route_string(type), | |
1862 | instance); | |
1863 | ||
1864 | /* Send distribute add message to zebra. */ | |
1865 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type, | |
1866 | instance, bgp->vrf_id); | |
1867 | ||
1868 | return CMD_SUCCESS; | |
1869 | } | |
1870 | ||
1871 | int bgp_redistribute_resend(struct bgp *bgp, afi_t afi, int type, | |
1872 | unsigned short instance) | |
1873 | { | |
1874 | /* Don't try to send if we're not connected to Zebra or Zebra doesn't | |
1875 | * know of this instance. | |
1876 | */ | |
1877 | if (!bgp_install_info_to_zebra(bgp)) | |
1878 | return -1; | |
1879 | ||
1880 | if (BGP_DEBUG(zebra, ZEBRA)) | |
1881 | zlog_debug("Tx redistribute del/add VRF %u afi %d %s %d", | |
1882 | bgp->vrf_id, afi, zebra_route_string(type), | |
1883 | instance); | |
1884 | ||
1885 | /* Send distribute add message to zebra. */ | |
1886 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, | |
1887 | instance, bgp->vrf_id); | |
1888 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type, | |
1889 | instance, bgp->vrf_id); | |
1890 | ||
1891 | return 0; | |
1892 | } | |
1893 | ||
1894 | /* Redistribute with route-map specification. */ | |
1895 | bool bgp_redistribute_rmap_set(struct bgp_redist *red, const char *name, | |
1896 | struct route_map *route_map) | |
1897 | { | |
1898 | if (red->rmap.name && (strcmp(red->rmap.name, name) == 0)) | |
1899 | return false; | |
1900 | ||
1901 | XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name); | |
1902 | /* Decrement the count for existing routemap and | |
1903 | * increment the count for new route map. | |
1904 | */ | |
1905 | route_map_counter_decrement(red->rmap.map); | |
1906 | red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name); | |
1907 | red->rmap.map = route_map; | |
1908 | route_map_counter_increment(red->rmap.map); | |
1909 | ||
1910 | return true; | |
1911 | } | |
1912 | ||
1913 | /* Redistribute with metric specification. */ | |
1914 | bool bgp_redistribute_metric_set(struct bgp *bgp, struct bgp_redist *red, | |
1915 | afi_t afi, int type, uint32_t metric) | |
1916 | { | |
1917 | struct bgp_dest *dest; | |
1918 | struct bgp_path_info *pi; | |
1919 | ||
1920 | if (red->redist_metric_flag && red->redist_metric == metric) | |
1921 | return false; | |
1922 | ||
1923 | red->redist_metric_flag = 1; | |
1924 | red->redist_metric = metric; | |
1925 | ||
1926 | for (dest = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); dest; | |
1927 | dest = bgp_route_next(dest)) { | |
1928 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) { | |
1929 | if (pi->sub_type == BGP_ROUTE_REDISTRIBUTE | |
1930 | && pi->type == type | |
1931 | && pi->instance == red->instance) { | |
1932 | struct attr *old_attr; | |
1933 | struct attr new_attr; | |
1934 | ||
1935 | new_attr = *pi->attr; | |
1936 | new_attr.med = red->redist_metric; | |
1937 | old_attr = pi->attr; | |
1938 | pi->attr = bgp_attr_intern(&new_attr); | |
1939 | bgp_attr_unintern(&old_attr); | |
1940 | ||
1941 | bgp_path_info_set_flag(dest, pi, | |
1942 | BGP_PATH_ATTR_CHANGED); | |
1943 | bgp_process(bgp, dest, afi, SAFI_UNICAST); | |
1944 | } | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | return true; | |
1949 | } | |
1950 | ||
1951 | /* Unset redistribution. */ | |
1952 | int bgp_redistribute_unreg(struct bgp *bgp, afi_t afi, int type, | |
1953 | unsigned short instance) | |
1954 | { | |
1955 | struct bgp_redist *red; | |
1956 | ||
1957 | red = bgp_redist_lookup(bgp, afi, type, instance); | |
1958 | if (!red) | |
1959 | return CMD_SUCCESS; | |
1960 | ||
1961 | /* Return if zebra connection is disabled. */ | |
1962 | if (instance) { | |
1963 | if (!redist_check_instance(&zclient->mi_redist[afi][type], | |
1964 | instance)) | |
1965 | return CMD_WARNING; | |
1966 | redist_del_instance(&zclient->mi_redist[afi][type], instance); | |
1967 | } else { | |
1968 | if (!vrf_bitmap_check(zclient->redist[afi][type], bgp->vrf_id)) | |
1969 | return CMD_WARNING; | |
1970 | vrf_bitmap_unset(zclient->redist[afi][type], bgp->vrf_id); | |
1971 | } | |
1972 | ||
1973 | if (bgp_install_info_to_zebra(bgp)) { | |
1974 | /* Send distribute delete message to zebra. */ | |
1975 | if (BGP_DEBUG(zebra, ZEBRA)) | |
1976 | zlog_debug("Tx redistribute del VRF %u afi %d %s %d", | |
1977 | bgp->vrf_id, afi, zebra_route_string(type), | |
1978 | instance); | |
1979 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, | |
1980 | type, instance, bgp->vrf_id); | |
1981 | } | |
1982 | ||
1983 | /* Withdraw redistributed routes from current BGP's routing table. */ | |
1984 | bgp_redistribute_withdraw(bgp, afi, type, instance); | |
1985 | ||
1986 | return CMD_SUCCESS; | |
1987 | } | |
1988 | ||
1989 | /* Unset redistribution. */ | |
1990 | int bgp_redistribute_unset(struct bgp *bgp, afi_t afi, int type, | |
1991 | unsigned short instance) | |
1992 | { | |
1993 | struct bgp_redist *red; | |
1994 | ||
1995 | /* | |
1996 | * vnc and vpn->vrf checks must be before red check because | |
1997 | * they operate within bgpd irrespective of zebra connection | |
1998 | * status. red lookup fails if there is no zebra connection. | |
1999 | */ | |
2000 | #ifdef ENABLE_BGP_VNC | |
2001 | if (EVPN_ENABLED(bgp) && type == ZEBRA_ROUTE_VNC_DIRECT) { | |
2002 | vnc_export_bgp_disable(bgp, afi); | |
2003 | } | |
2004 | #endif | |
2005 | ||
2006 | red = bgp_redist_lookup(bgp, afi, type, instance); | |
2007 | if (!red) | |
2008 | return CMD_SUCCESS; | |
2009 | ||
2010 | bgp_redistribute_unreg(bgp, afi, type, instance); | |
2011 | ||
2012 | /* Unset route-map. */ | |
2013 | XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name); | |
2014 | route_map_counter_decrement(red->rmap.map); | |
2015 | red->rmap.map = NULL; | |
2016 | ||
2017 | /* Unset metric. */ | |
2018 | red->redist_metric_flag = 0; | |
2019 | red->redist_metric = 0; | |
2020 | ||
2021 | bgp_redist_del(bgp, afi, type, instance); | |
2022 | ||
2023 | return CMD_SUCCESS; | |
2024 | } | |
2025 | ||
2026 | void bgp_redistribute_redo(struct bgp *bgp) | |
2027 | { | |
2028 | afi_t afi; | |
2029 | int i; | |
2030 | struct list *red_list; | |
2031 | struct listnode *node; | |
2032 | struct bgp_redist *red; | |
2033 | ||
2034 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
2035 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
2036 | ||
2037 | red_list = bgp->redist[afi][i]; | |
2038 | if (!red_list) | |
2039 | continue; | |
2040 | ||
2041 | for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) { | |
2042 | bgp_redistribute_resend(bgp, afi, i, | |
2043 | red->instance); | |
2044 | } | |
2045 | } | |
2046 | } | |
2047 | } | |
2048 | ||
2049 | void bgp_zclient_reset(void) | |
2050 | { | |
2051 | zclient_reset(zclient); | |
2052 | } | |
2053 | ||
2054 | /* Register this instance with Zebra. Invoked upon connect (for | |
2055 | * default instance) and when other VRFs are learnt (or created and | |
2056 | * already learnt). | |
2057 | */ | |
2058 | void bgp_zebra_instance_register(struct bgp *bgp) | |
2059 | { | |
2060 | /* Don't try to register if we're not connected to Zebra */ | |
2061 | if (!zclient || zclient->sock < 0) | |
2062 | return; | |
2063 | ||
2064 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2065 | zlog_debug("Registering VRF %u", bgp->vrf_id); | |
2066 | ||
2067 | /* Register for router-id, interfaces, redistributed routes. */ | |
2068 | zclient_send_reg_requests(zclient, bgp->vrf_id); | |
2069 | ||
2070 | /* For EVPN instance, register to learn about VNIs, if appropriate. */ | |
2071 | if (bgp->advertise_all_vni) | |
2072 | bgp_zebra_advertise_all_vni(bgp, 1); | |
2073 | ||
2074 | bgp_nht_register_nexthops(bgp); | |
2075 | } | |
2076 | ||
2077 | /* Deregister this instance with Zebra. Invoked upon the instance | |
2078 | * being deleted (default or VRF) and it is already registered. | |
2079 | */ | |
2080 | void bgp_zebra_instance_deregister(struct bgp *bgp) | |
2081 | { | |
2082 | /* Don't try to deregister if we're not connected to Zebra */ | |
2083 | if (zclient->sock < 0) | |
2084 | return; | |
2085 | ||
2086 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2087 | zlog_debug("Deregistering VRF %u", bgp->vrf_id); | |
2088 | ||
2089 | /* For EVPN instance, unregister learning about VNIs, if appropriate. */ | |
2090 | if (bgp->advertise_all_vni) | |
2091 | bgp_zebra_advertise_all_vni(bgp, 0); | |
2092 | ||
2093 | /* Deregister for router-id, interfaces, redistributed routes. */ | |
2094 | zclient_send_dereg_requests(zclient, bgp->vrf_id); | |
2095 | } | |
2096 | ||
2097 | void bgp_zebra_initiate_radv(struct bgp *bgp, struct peer *peer) | |
2098 | { | |
2099 | uint32_t ra_interval = BGP_UNNUM_DEFAULT_RA_INTERVAL; | |
2100 | ||
2101 | /* Don't try to initiate if we're not connected to Zebra */ | |
2102 | if (zclient->sock < 0) | |
2103 | return; | |
2104 | ||
2105 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2106 | zlog_debug("%u: Initiating RA for peer %s", bgp->vrf_id, | |
2107 | peer->host); | |
2108 | ||
2109 | /* | |
2110 | * If unnumbered peer (peer->ifp) call thru zapi to start RAs. | |
2111 | * If we don't have an ifp pointer, call function to find the | |
2112 | * ifps for a numbered enhe peer to turn RAs on. | |
2113 | */ | |
2114 | peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id, | |
2115 | peer->ifp, 1, ra_interval) | |
2116 | : bgp_nht_reg_enhe_cap_intfs(peer); | |
2117 | } | |
2118 | ||
2119 | void bgp_zebra_terminate_radv(struct bgp *bgp, struct peer *peer) | |
2120 | { | |
2121 | /* Don't try to terminate if we're not connected to Zebra */ | |
2122 | if (zclient->sock < 0) | |
2123 | return; | |
2124 | ||
2125 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2126 | zlog_debug("%u: Terminating RA for peer %s", bgp->vrf_id, | |
2127 | peer->host); | |
2128 | ||
2129 | /* | |
2130 | * If unnumbered peer (peer->ifp) call thru zapi to stop RAs. | |
2131 | * If we don't have an ifp pointer, call function to find the | |
2132 | * ifps for a numbered enhe peer to turn RAs off. | |
2133 | */ | |
2134 | peer->ifp ? zclient_send_interface_radv_req(zclient, bgp->vrf_id, | |
2135 | peer->ifp, 0, 0) | |
2136 | : bgp_nht_dereg_enhe_cap_intfs(peer); | |
2137 | } | |
2138 | ||
2139 | int bgp_zebra_advertise_subnet(struct bgp *bgp, int advertise, vni_t vni) | |
2140 | { | |
2141 | struct stream *s = NULL; | |
2142 | ||
2143 | /* Check socket. */ | |
2144 | if (!zclient || zclient->sock < 0) | |
2145 | return 0; | |
2146 | ||
2147 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2148 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) { | |
2149 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2150 | zlog_debug( | |
2151 | "%s: No zebra instance to talk to, cannot advertise subnet", | |
2152 | __func__); | |
2153 | return 0; | |
2154 | } | |
2155 | ||
2156 | s = zclient->obuf; | |
2157 | stream_reset(s); | |
2158 | ||
2159 | zclient_create_header(s, ZEBRA_ADVERTISE_SUBNET, bgp->vrf_id); | |
2160 | stream_putc(s, advertise); | |
2161 | stream_put3(s, vni); | |
2162 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2163 | ||
2164 | return zclient_send_message(zclient); | |
2165 | } | |
2166 | ||
2167 | int bgp_zebra_advertise_svi_macip(struct bgp *bgp, int advertise, vni_t vni) | |
2168 | { | |
2169 | struct stream *s = NULL; | |
2170 | ||
2171 | /* Check socket. */ | |
2172 | if (!zclient || zclient->sock < 0) | |
2173 | return 0; | |
2174 | ||
2175 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2176 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) | |
2177 | return 0; | |
2178 | ||
2179 | s = zclient->obuf; | |
2180 | stream_reset(s); | |
2181 | ||
2182 | zclient_create_header(s, ZEBRA_ADVERTISE_SVI_MACIP, bgp->vrf_id); | |
2183 | stream_putc(s, advertise); | |
2184 | stream_putl(s, vni); | |
2185 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2186 | ||
2187 | return zclient_send_message(zclient); | |
2188 | } | |
2189 | ||
2190 | int bgp_zebra_advertise_gw_macip(struct bgp *bgp, int advertise, vni_t vni) | |
2191 | { | |
2192 | struct stream *s = NULL; | |
2193 | ||
2194 | /* Check socket. */ | |
2195 | if (!zclient || zclient->sock < 0) | |
2196 | return 0; | |
2197 | ||
2198 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2199 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) { | |
2200 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2201 | zlog_debug( | |
2202 | "%s: No zebra instance to talk to, not installing gw_macip", | |
2203 | __func__); | |
2204 | return 0; | |
2205 | } | |
2206 | ||
2207 | s = zclient->obuf; | |
2208 | stream_reset(s); | |
2209 | ||
2210 | zclient_create_header(s, ZEBRA_ADVERTISE_DEFAULT_GW, bgp->vrf_id); | |
2211 | stream_putc(s, advertise); | |
2212 | stream_putl(s, vni); | |
2213 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2214 | ||
2215 | return zclient_send_message(zclient); | |
2216 | } | |
2217 | ||
2218 | int bgp_zebra_vxlan_flood_control(struct bgp *bgp, | |
2219 | enum vxlan_flood_control flood_ctrl) | |
2220 | { | |
2221 | struct stream *s; | |
2222 | ||
2223 | /* Check socket. */ | |
2224 | if (!zclient || zclient->sock < 0) | |
2225 | return 0; | |
2226 | ||
2227 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2228 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) { | |
2229 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2230 | zlog_debug( | |
2231 | "%s: No zebra instance to talk to, not installing all vni", | |
2232 | __func__); | |
2233 | return 0; | |
2234 | } | |
2235 | ||
2236 | s = zclient->obuf; | |
2237 | stream_reset(s); | |
2238 | ||
2239 | zclient_create_header(s, ZEBRA_VXLAN_FLOOD_CONTROL, bgp->vrf_id); | |
2240 | stream_putc(s, flood_ctrl); | |
2241 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2242 | ||
2243 | return zclient_send_message(zclient); | |
2244 | } | |
2245 | ||
2246 | int bgp_zebra_advertise_all_vni(struct bgp *bgp, int advertise) | |
2247 | { | |
2248 | struct stream *s; | |
2249 | ||
2250 | /* Check socket. */ | |
2251 | if (!zclient || zclient->sock < 0) | |
2252 | return 0; | |
2253 | ||
2254 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2255 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) | |
2256 | return 0; | |
2257 | ||
2258 | s = zclient->obuf; | |
2259 | stream_reset(s); | |
2260 | ||
2261 | zclient_create_header(s, ZEBRA_ADVERTISE_ALL_VNI, bgp->vrf_id); | |
2262 | stream_putc(s, advertise); | |
2263 | /* Also inform current BUM handling setting. This is really | |
2264 | * relevant only when 'advertise' is set. | |
2265 | */ | |
2266 | stream_putc(s, bgp->vxlan_flood_ctrl); | |
2267 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2268 | ||
2269 | return zclient_send_message(zclient); | |
2270 | } | |
2271 | ||
2272 | int bgp_zebra_dup_addr_detection(struct bgp *bgp) | |
2273 | { | |
2274 | struct stream *s; | |
2275 | ||
2276 | /* Check socket. */ | |
2277 | if (!zclient || zclient->sock < 0) | |
2278 | return 0; | |
2279 | ||
2280 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
2281 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) | |
2282 | return 0; | |
2283 | ||
2284 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2285 | zlog_debug("dup addr detect %s max_moves %u time %u freeze %s freeze_time %u", | |
2286 | bgp->evpn_info->dup_addr_detect ? | |
2287 | "enable" : "disable", | |
2288 | bgp->evpn_info->dad_max_moves, | |
2289 | bgp->evpn_info->dad_time, | |
2290 | bgp->evpn_info->dad_freeze ? | |
2291 | "enable" : "disable", | |
2292 | bgp->evpn_info->dad_freeze_time); | |
2293 | ||
2294 | s = zclient->obuf; | |
2295 | stream_reset(s); | |
2296 | zclient_create_header(s, ZEBRA_DUPLICATE_ADDR_DETECTION, | |
2297 | bgp->vrf_id); | |
2298 | stream_putl(s, bgp->evpn_info->dup_addr_detect); | |
2299 | stream_putl(s, bgp->evpn_info->dad_time); | |
2300 | stream_putl(s, bgp->evpn_info->dad_max_moves); | |
2301 | stream_putl(s, bgp->evpn_info->dad_freeze); | |
2302 | stream_putl(s, bgp->evpn_info->dad_freeze_time); | |
2303 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2304 | ||
2305 | return zclient_send_message(zclient); | |
2306 | } | |
2307 | ||
2308 | static int rule_notify_owner(ZAPI_CALLBACK_ARGS) | |
2309 | { | |
2310 | uint32_t seqno, priority, unique; | |
2311 | enum zapi_rule_notify_owner note; | |
2312 | struct bgp_pbr_action *bgp_pbra; | |
2313 | struct bgp_pbr_rule *bgp_pbr = NULL; | |
2314 | char ifname[INTERFACE_NAMSIZ + 1]; | |
2315 | ||
2316 | if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique, | |
2317 | ifname, ¬e)) | |
2318 | return -1; | |
2319 | ||
2320 | bgp_pbra = bgp_pbr_action_rule_lookup(vrf_id, unique); | |
2321 | if (!bgp_pbra) { | |
2322 | /* look in bgp pbr rule */ | |
2323 | bgp_pbr = bgp_pbr_rule_lookup(vrf_id, unique); | |
2324 | if (!bgp_pbr && note != ZAPI_RULE_REMOVED) { | |
2325 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2326 | zlog_debug("%s: Fail to look BGP rule (%u)", | |
2327 | __func__, unique); | |
2328 | return 0; | |
2329 | } | |
2330 | } | |
2331 | ||
2332 | switch (note) { | |
2333 | case ZAPI_RULE_FAIL_INSTALL: | |
2334 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2335 | zlog_debug("%s: Received RULE_FAIL_INSTALL", __func__); | |
2336 | if (bgp_pbra) { | |
2337 | bgp_pbra->installed = false; | |
2338 | bgp_pbra->install_in_progress = false; | |
2339 | } else { | |
2340 | bgp_pbr->installed = false; | |
2341 | bgp_pbr->install_in_progress = false; | |
2342 | } | |
2343 | break; | |
2344 | case ZAPI_RULE_INSTALLED: | |
2345 | if (bgp_pbra) { | |
2346 | bgp_pbra->installed = true; | |
2347 | bgp_pbra->install_in_progress = false; | |
2348 | } else { | |
2349 | struct bgp_path_info *path; | |
2350 | struct bgp_path_info_extra *extra; | |
2351 | ||
2352 | bgp_pbr->installed = true; | |
2353 | bgp_pbr->install_in_progress = false; | |
2354 | bgp_pbr->action->refcnt++; | |
2355 | /* link bgp_info to bgp_pbr */ | |
2356 | path = (struct bgp_path_info *)bgp_pbr->path; | |
2357 | extra = bgp_path_info_extra_get(path); | |
2358 | listnode_add_force(&extra->bgp_fs_iprule, | |
2359 | bgp_pbr); | |
2360 | } | |
2361 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2362 | zlog_debug("%s: Received RULE_INSTALLED", __func__); | |
2363 | break; | |
2364 | case ZAPI_RULE_FAIL_REMOVE: | |
2365 | case ZAPI_RULE_REMOVED: | |
2366 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2367 | zlog_debug("%s: Received RULE REMOVED", __func__); | |
2368 | break; | |
2369 | } | |
2370 | ||
2371 | return 0; | |
2372 | } | |
2373 | ||
2374 | static int ipset_notify_owner(ZAPI_CALLBACK_ARGS) | |
2375 | { | |
2376 | uint32_t unique; | |
2377 | enum zapi_ipset_notify_owner note; | |
2378 | struct bgp_pbr_match *bgp_pbim; | |
2379 | ||
2380 | if (!zapi_ipset_notify_decode(zclient->ibuf, | |
2381 | &unique, | |
2382 | ¬e)) | |
2383 | return -1; | |
2384 | ||
2385 | bgp_pbim = bgp_pbr_match_ipset_lookup(vrf_id, unique); | |
2386 | if (!bgp_pbim) { | |
2387 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2388 | zlog_debug("%s: Fail to look BGP match ( %u, ID %u)", | |
2389 | __func__, note, unique); | |
2390 | return 0; | |
2391 | } | |
2392 | ||
2393 | switch (note) { | |
2394 | case ZAPI_IPSET_FAIL_INSTALL: | |
2395 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2396 | zlog_debug("%s: Received IPSET_FAIL_INSTALL", __func__); | |
2397 | bgp_pbim->installed = false; | |
2398 | bgp_pbim->install_in_progress = false; | |
2399 | break; | |
2400 | case ZAPI_IPSET_INSTALLED: | |
2401 | bgp_pbim->installed = true; | |
2402 | bgp_pbim->install_in_progress = false; | |
2403 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2404 | zlog_debug("%s: Received IPSET_INSTALLED", __func__); | |
2405 | break; | |
2406 | case ZAPI_IPSET_FAIL_REMOVE: | |
2407 | case ZAPI_IPSET_REMOVED: | |
2408 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2409 | zlog_debug("%s: Received IPSET REMOVED", __func__); | |
2410 | break; | |
2411 | } | |
2412 | ||
2413 | return 0; | |
2414 | } | |
2415 | ||
2416 | static int ipset_entry_notify_owner(ZAPI_CALLBACK_ARGS) | |
2417 | { | |
2418 | uint32_t unique; | |
2419 | char ipset_name[ZEBRA_IPSET_NAME_SIZE]; | |
2420 | enum zapi_ipset_entry_notify_owner note; | |
2421 | struct bgp_pbr_match_entry *bgp_pbime; | |
2422 | ||
2423 | if (!zapi_ipset_entry_notify_decode( | |
2424 | zclient->ibuf, | |
2425 | &unique, | |
2426 | ipset_name, | |
2427 | ¬e)) | |
2428 | return -1; | |
2429 | bgp_pbime = bgp_pbr_match_ipset_entry_lookup(vrf_id, | |
2430 | ipset_name, | |
2431 | unique); | |
2432 | if (!bgp_pbime) { | |
2433 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2434 | zlog_debug( | |
2435 | "%s: Fail to look BGP match entry (%u, ID %u)", | |
2436 | __func__, note, unique); | |
2437 | return 0; | |
2438 | } | |
2439 | ||
2440 | switch (note) { | |
2441 | case ZAPI_IPSET_ENTRY_FAIL_INSTALL: | |
2442 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2443 | zlog_debug("%s: Received IPSET_ENTRY_FAIL_INSTALL", | |
2444 | __func__); | |
2445 | bgp_pbime->installed = false; | |
2446 | bgp_pbime->install_in_progress = false; | |
2447 | break; | |
2448 | case ZAPI_IPSET_ENTRY_INSTALLED: | |
2449 | { | |
2450 | struct bgp_path_info *path; | |
2451 | struct bgp_path_info_extra *extra; | |
2452 | ||
2453 | bgp_pbime->installed = true; | |
2454 | bgp_pbime->install_in_progress = false; | |
2455 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2456 | zlog_debug("%s: Received IPSET_ENTRY_INSTALLED", | |
2457 | __func__); | |
2458 | /* link bgp_path_info to bpme */ | |
2459 | path = (struct bgp_path_info *)bgp_pbime->path; | |
2460 | extra = bgp_path_info_extra_get(path); | |
2461 | listnode_add_force(&extra->bgp_fs_pbr, bgp_pbime); | |
2462 | } | |
2463 | break; | |
2464 | case ZAPI_IPSET_ENTRY_FAIL_REMOVE: | |
2465 | case ZAPI_IPSET_ENTRY_REMOVED: | |
2466 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2467 | zlog_debug("%s: Received IPSET_ENTRY_REMOVED", | |
2468 | __func__); | |
2469 | break; | |
2470 | } | |
2471 | return 0; | |
2472 | } | |
2473 | ||
2474 | static int iptable_notify_owner(ZAPI_CALLBACK_ARGS) | |
2475 | { | |
2476 | uint32_t unique; | |
2477 | enum zapi_iptable_notify_owner note; | |
2478 | struct bgp_pbr_match *bgpm; | |
2479 | ||
2480 | if (!zapi_iptable_notify_decode( | |
2481 | zclient->ibuf, | |
2482 | &unique, | |
2483 | ¬e)) | |
2484 | return -1; | |
2485 | bgpm = bgp_pbr_match_iptable_lookup(vrf_id, unique); | |
2486 | if (!bgpm) { | |
2487 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2488 | zlog_debug("%s: Fail to look BGP iptable (%u %u)", | |
2489 | __func__, note, unique); | |
2490 | return 0; | |
2491 | } | |
2492 | switch (note) { | |
2493 | case ZAPI_IPTABLE_FAIL_INSTALL: | |
2494 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2495 | zlog_debug("%s: Received IPTABLE_FAIL_INSTALL", | |
2496 | __func__); | |
2497 | bgpm->installed_in_iptable = false; | |
2498 | bgpm->install_iptable_in_progress = false; | |
2499 | break; | |
2500 | case ZAPI_IPTABLE_INSTALLED: | |
2501 | bgpm->installed_in_iptable = true; | |
2502 | bgpm->install_iptable_in_progress = false; | |
2503 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2504 | zlog_debug("%s: Received IPTABLE_INSTALLED", __func__); | |
2505 | bgpm->action->refcnt++; | |
2506 | break; | |
2507 | case ZAPI_IPTABLE_FAIL_REMOVE: | |
2508 | case ZAPI_IPTABLE_REMOVED: | |
2509 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2510 | zlog_debug("%s: Received IPTABLE REMOVED", __func__); | |
2511 | break; | |
2512 | } | |
2513 | return 0; | |
2514 | } | |
2515 | ||
2516 | /* Process route notification messages from RIB */ | |
2517 | static int bgp_zebra_route_notify_owner(int command, struct zclient *zclient, | |
2518 | zebra_size_t length, vrf_id_t vrf_id) | |
2519 | { | |
2520 | struct prefix p; | |
2521 | enum zapi_route_notify_owner note; | |
2522 | uint32_t table_id; | |
2523 | afi_t afi; | |
2524 | safi_t safi; | |
2525 | struct bgp_dest *dest; | |
2526 | struct bgp *bgp; | |
2527 | struct bgp_path_info *pi, *new_select; | |
2528 | ||
2529 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, ¬e, | |
2530 | &afi, &safi)) { | |
2531 | zlog_err("%s : error in msg decode", __func__); | |
2532 | return -1; | |
2533 | } | |
2534 | ||
2535 | /* Get the bgp instance */ | |
2536 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
2537 | if (!bgp) { | |
2538 | flog_err(EC_BGP_INVALID_BGP_INSTANCE, | |
2539 | "%s : bgp instance not found vrf %d", __func__, | |
2540 | vrf_id); | |
2541 | return -1; | |
2542 | } | |
2543 | ||
2544 | /* Find the bgp route node */ | |
2545 | dest = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, &p, | |
2546 | &bgp->vrf_prd); | |
2547 | if (!dest) | |
2548 | return -1; | |
2549 | ||
2550 | switch (note) { | |
2551 | case ZAPI_ROUTE_INSTALLED: | |
2552 | new_select = NULL; | |
2553 | /* Clear the flags so that route can be processed */ | |
2554 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING); | |
2555 | SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED); | |
2556 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2557 | zlog_debug("route %pRN : INSTALLED", dest); | |
2558 | /* Find the best route */ | |
2559 | for (pi = dest->info; pi; pi = pi->next) { | |
2560 | /* Process aggregate route */ | |
2561 | bgp_aggregate_increment(bgp, &p, pi, afi, safi); | |
2562 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) | |
2563 | new_select = pi; | |
2564 | } | |
2565 | /* Advertise the route */ | |
2566 | if (new_select) | |
2567 | group_announce_route(bgp, afi, safi, dest, new_select); | |
2568 | else { | |
2569 | flog_err(EC_BGP_INVALID_ROUTE, | |
2570 | "selected route %pRN not found", dest); | |
2571 | ||
2572 | bgp_dest_unlock_node(dest); | |
2573 | return -1; | |
2574 | } | |
2575 | break; | |
2576 | case ZAPI_ROUTE_REMOVED: | |
2577 | /* Route deleted from dataplane, reset the installed flag | |
2578 | * so that route can be reinstalled when client sends | |
2579 | * route add later | |
2580 | */ | |
2581 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED); | |
2582 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2583 | zlog_debug("route %pRN: Removed from Fib", dest); | |
2584 | break; | |
2585 | case ZAPI_ROUTE_FAIL_INSTALL: | |
2586 | new_select = NULL; | |
2587 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2588 | zlog_debug("route: %pRN Failed to Install into Fib", | |
2589 | dest); | |
2590 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING); | |
2591 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED); | |
2592 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) { | |
2593 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) | |
2594 | new_select = pi; | |
2595 | } | |
2596 | if (new_select) | |
2597 | group_announce_route(bgp, afi, safi, dest, new_select); | |
2598 | /* Error will be logged by zebra module */ | |
2599 | break; | |
2600 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
2601 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2602 | zlog_debug("route: %pRN removed due to better admin won", | |
2603 | dest); | |
2604 | new_select = NULL; | |
2605 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING); | |
2606 | UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED); | |
2607 | for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) { | |
2608 | bgp_aggregate_decrement(bgp, &p, pi, afi, safi); | |
2609 | if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) | |
2610 | new_select = pi; | |
2611 | } | |
2612 | if (new_select) | |
2613 | group_announce_route(bgp, afi, safi, dest, new_select); | |
2614 | /* No action required */ | |
2615 | break; | |
2616 | case ZAPI_ROUTE_REMOVE_FAIL: | |
2617 | zlog_warn("%s: Route %pRN failure to remove", | |
2618 | __func__, dest); | |
2619 | break; | |
2620 | } | |
2621 | ||
2622 | bgp_dest_unlock_node(dest); | |
2623 | return 0; | |
2624 | } | |
2625 | ||
2626 | /* this function is used to forge ip rule, | |
2627 | * - either for iptable/ipset using fwmark id | |
2628 | * - or for sample ip rule cmd | |
2629 | */ | |
2630 | static void bgp_encode_pbr_rule_action(struct stream *s, | |
2631 | struct bgp_pbr_action *pbra, | |
2632 | struct bgp_pbr_rule *pbr) | |
2633 | { | |
2634 | struct prefix pfx; | |
2635 | uint8_t fam = AF_INET; | |
2636 | char ifname[INTERFACE_NAMSIZ]; | |
2637 | ||
2638 | if (pbra->nh.type == NEXTHOP_TYPE_IPV6) | |
2639 | fam = AF_INET6; | |
2640 | stream_putl(s, 0); /* seqno unused */ | |
2641 | if (pbr) | |
2642 | stream_putl(s, pbr->priority); | |
2643 | else | |
2644 | stream_putl(s, 0); | |
2645 | /* ruleno unused - priority change | |
2646 | * ruleno permits distinguishing various FS PBR entries | |
2647 | * - FS PBR entries based on ipset/iptables | |
2648 | * - FS PBR entries based on iprule | |
2649 | * the latter may contain default routing information injected by FS | |
2650 | */ | |
2651 | if (pbr) | |
2652 | stream_putl(s, pbr->unique); | |
2653 | else | |
2654 | stream_putl(s, pbra->unique); | |
2655 | stream_putc(s, 0); /* ip protocol being used */ | |
2656 | if (pbr && pbr->flags & MATCH_IP_SRC_SET) | |
2657 | memcpy(&pfx, &(pbr->src), sizeof(struct prefix)); | |
2658 | else { | |
2659 | memset(&pfx, 0, sizeof(pfx)); | |
2660 | pfx.family = fam; | |
2661 | } | |
2662 | stream_putc(s, pfx.family); | |
2663 | stream_putc(s, pfx.prefixlen); | |
2664 | stream_put(s, &pfx.u.prefix, prefix_blen(&pfx)); | |
2665 | ||
2666 | stream_putw(s, 0); /* src port */ | |
2667 | ||
2668 | if (pbr && pbr->flags & MATCH_IP_DST_SET) | |
2669 | memcpy(&pfx, &(pbr->dst), sizeof(struct prefix)); | |
2670 | else { | |
2671 | memset(&pfx, 0, sizeof(pfx)); | |
2672 | pfx.family = fam; | |
2673 | } | |
2674 | stream_putc(s, pfx.family); | |
2675 | stream_putc(s, pfx.prefixlen); | |
2676 | stream_put(s, &pfx.u.prefix, prefix_blen(&pfx)); | |
2677 | ||
2678 | stream_putw(s, 0); /* dst port */ | |
2679 | stream_putc(s, 0); /* dsfield */ | |
2680 | /* if pbr present, fwmark is not used */ | |
2681 | if (pbr) | |
2682 | stream_putl(s, 0); | |
2683 | else | |
2684 | stream_putl(s, pbra->fwmark); /* fwmark */ | |
2685 | ||
2686 | stream_putl(s, pbra->table_id); | |
2687 | ||
2688 | memset(ifname, 0, sizeof(ifname)); | |
2689 | stream_put(s, ifname, INTERFACE_NAMSIZ); /* ifname unused */ | |
2690 | } | |
2691 | ||
2692 | static void bgp_encode_pbr_ipset_match(struct stream *s, | |
2693 | struct bgp_pbr_match *pbim) | |
2694 | { | |
2695 | stream_putl(s, pbim->unique); | |
2696 | stream_putl(s, pbim->type); | |
2697 | stream_putc(s, pbim->family); | |
2698 | stream_put(s, pbim->ipset_name, | |
2699 | ZEBRA_IPSET_NAME_SIZE); | |
2700 | } | |
2701 | ||
2702 | static void bgp_encode_pbr_ipset_entry_match(struct stream *s, | |
2703 | struct bgp_pbr_match_entry *pbime) | |
2704 | { | |
2705 | stream_putl(s, pbime->unique); | |
2706 | /* check that back pointer is not null */ | |
2707 | stream_put(s, pbime->backpointer->ipset_name, | |
2708 | ZEBRA_IPSET_NAME_SIZE); | |
2709 | ||
2710 | stream_putc(s, pbime->src.family); | |
2711 | stream_putc(s, pbime->src.prefixlen); | |
2712 | stream_put(s, &pbime->src.u.prefix, prefix_blen(&pbime->src)); | |
2713 | ||
2714 | stream_putc(s, pbime->dst.family); | |
2715 | stream_putc(s, pbime->dst.prefixlen); | |
2716 | stream_put(s, &pbime->dst.u.prefix, prefix_blen(&pbime->dst)); | |
2717 | ||
2718 | stream_putw(s, pbime->src_port_min); | |
2719 | stream_putw(s, pbime->src_port_max); | |
2720 | stream_putw(s, pbime->dst_port_min); | |
2721 | stream_putw(s, pbime->dst_port_max); | |
2722 | stream_putc(s, pbime->proto); | |
2723 | } | |
2724 | ||
2725 | static void bgp_encode_pbr_iptable_match(struct stream *s, | |
2726 | struct bgp_pbr_action *bpa, | |
2727 | struct bgp_pbr_match *pbm) | |
2728 | { | |
2729 | stream_putl(s, pbm->unique2); | |
2730 | ||
2731 | stream_putl(s, pbm->type); | |
2732 | ||
2733 | stream_putl(s, pbm->flags); | |
2734 | ||
2735 | /* TODO: correlate with what is contained | |
2736 | * into bgp_pbr_action. | |
2737 | * currently only forward supported | |
2738 | */ | |
2739 | if (bpa->nh.type == NEXTHOP_TYPE_BLACKHOLE) | |
2740 | stream_putl(s, ZEBRA_IPTABLES_DROP); | |
2741 | else | |
2742 | stream_putl(s, ZEBRA_IPTABLES_FORWARD); | |
2743 | stream_putl(s, bpa->fwmark); | |
2744 | stream_put(s, pbm->ipset_name, | |
2745 | ZEBRA_IPSET_NAME_SIZE); | |
2746 | stream_putc(s, pbm->family); | |
2747 | stream_putw(s, pbm->pkt_len_min); | |
2748 | stream_putw(s, pbm->pkt_len_max); | |
2749 | stream_putw(s, pbm->tcp_flags); | |
2750 | stream_putw(s, pbm->tcp_mask_flags); | |
2751 | stream_putc(s, pbm->dscp_value); | |
2752 | stream_putc(s, pbm->fragment); | |
2753 | stream_putc(s, pbm->protocol); | |
2754 | stream_putw(s, pbm->flow_label); | |
2755 | } | |
2756 | ||
2757 | /* BGP has established connection with Zebra. */ | |
2758 | static void bgp_zebra_connected(struct zclient *zclient) | |
2759 | { | |
2760 | struct bgp *bgp; | |
2761 | ||
2762 | zclient_num_connects++; /* increment even if not responding */ | |
2763 | ||
2764 | /* Send the client registration */ | |
2765 | bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT); | |
2766 | ||
2767 | /* At this point, we may or may not have BGP instances configured, but | |
2768 | * we're only interested in the default VRF (others wouldn't have learnt | |
2769 | * the VRF from Zebra yet.) | |
2770 | */ | |
2771 | bgp = bgp_get_default(); | |
2772 | if (!bgp) | |
2773 | return; | |
2774 | ||
2775 | bgp_zebra_instance_register(bgp); | |
2776 | ||
2777 | /* tell label pool that zebra is connected */ | |
2778 | bgp_lp_event_zebra_up(); | |
2779 | ||
2780 | /* TODO - What if we have peers and networks configured, do we have to | |
2781 | * kick-start them? | |
2782 | */ | |
2783 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp, bgp->peer); | |
2784 | } | |
2785 | ||
2786 | static int bgp_zebra_process_local_es_add(ZAPI_CALLBACK_ARGS) | |
2787 | { | |
2788 | esi_t esi; | |
2789 | struct bgp *bgp = NULL; | |
2790 | struct stream *s = NULL; | |
2791 | char buf[ESI_STR_LEN]; | |
2792 | struct in_addr originator_ip; | |
2793 | uint8_t active; | |
2794 | uint8_t bypass; | |
2795 | uint16_t df_pref; | |
2796 | ||
2797 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
2798 | if (!bgp) | |
2799 | return 0; | |
2800 | ||
2801 | s = zclient->ibuf; | |
2802 | stream_get(&esi, s, sizeof(esi_t)); | |
2803 | originator_ip.s_addr = stream_get_ipv4(s); | |
2804 | active = stream_getc(s); | |
2805 | df_pref = stream_getw(s); | |
2806 | bypass = stream_getc(s); | |
2807 | ||
2808 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2809 | zlog_debug( | |
2810 | "Rx add ESI %s originator-ip %pI4 active %u df_pref %u %s", | |
2811 | esi_to_str(&esi, buf, sizeof(buf)), &originator_ip, | |
2812 | active, df_pref, bypass ? "bypass" : ""); | |
2813 | ||
2814 | frrtrace(5, frr_bgp, evpn_mh_local_es_add_zrecv, &esi, originator_ip, | |
2815 | active, bypass, df_pref); | |
2816 | ||
2817 | bgp_evpn_local_es_add(bgp, &esi, originator_ip, active, df_pref, | |
2818 | !!bypass); | |
2819 | ||
2820 | return 0; | |
2821 | } | |
2822 | ||
2823 | static int bgp_zebra_process_local_es_del(ZAPI_CALLBACK_ARGS) | |
2824 | { | |
2825 | esi_t esi; | |
2826 | struct bgp *bgp = NULL; | |
2827 | struct stream *s = NULL; | |
2828 | char buf[ESI_STR_LEN]; | |
2829 | ||
2830 | memset(&esi, 0, sizeof(esi_t)); | |
2831 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
2832 | if (!bgp) | |
2833 | return 0; | |
2834 | ||
2835 | s = zclient->ibuf; | |
2836 | stream_get(&esi, s, sizeof(esi_t)); | |
2837 | ||
2838 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2839 | zlog_debug("Rx del ESI %s", | |
2840 | esi_to_str(&esi, buf, sizeof(buf))); | |
2841 | ||
2842 | frrtrace(1, frr_bgp, evpn_mh_local_es_del_zrecv, &esi); | |
2843 | ||
2844 | bgp_evpn_local_es_del(bgp, &esi); | |
2845 | ||
2846 | return 0; | |
2847 | } | |
2848 | ||
2849 | static int bgp_zebra_process_local_es_evi(ZAPI_CALLBACK_ARGS) | |
2850 | { | |
2851 | esi_t esi; | |
2852 | vni_t vni; | |
2853 | struct bgp *bgp; | |
2854 | struct stream *s; | |
2855 | char buf[ESI_STR_LEN]; | |
2856 | ||
2857 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
2858 | if (!bgp) | |
2859 | return 0; | |
2860 | ||
2861 | s = zclient->ibuf; | |
2862 | stream_get(&esi, s, sizeof(esi_t)); | |
2863 | vni = stream_getl(s); | |
2864 | ||
2865 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2866 | zlog_debug("Rx %s ESI %s VNI %u", | |
2867 | (cmd == ZEBRA_VNI_ADD) ? "add" : "del", | |
2868 | esi_to_str(&esi, buf, sizeof(buf)), vni); | |
2869 | ||
2870 | if (cmd == ZEBRA_LOCAL_ES_EVI_ADD) { | |
2871 | frrtrace(2, frr_bgp, evpn_mh_local_es_evi_add_zrecv, &esi, vni); | |
2872 | ||
2873 | bgp_evpn_local_es_evi_add(bgp, &esi, vni); | |
2874 | } else { | |
2875 | frrtrace(2, frr_bgp, evpn_mh_local_es_evi_del_zrecv, &esi, vni); | |
2876 | ||
2877 | bgp_evpn_local_es_evi_del(bgp, &esi, vni); | |
2878 | } | |
2879 | ||
2880 | return 0; | |
2881 | } | |
2882 | ||
2883 | static int bgp_zebra_process_local_l3vni(ZAPI_CALLBACK_ARGS) | |
2884 | { | |
2885 | int filter = 0; | |
2886 | vni_t l3vni = 0; | |
2887 | struct ethaddr svi_rmac, vrr_rmac = {.octet = {0} }; | |
2888 | struct in_addr originator_ip; | |
2889 | struct stream *s; | |
2890 | ifindex_t svi_ifindex; | |
2891 | bool is_anycast_mac = false; | |
2892 | ||
2893 | memset(&svi_rmac, 0, sizeof(struct ethaddr)); | |
2894 | memset(&originator_ip, 0, sizeof(struct in_addr)); | |
2895 | s = zclient->ibuf; | |
2896 | l3vni = stream_getl(s); | |
2897 | if (cmd == ZEBRA_L3VNI_ADD) { | |
2898 | stream_get(&svi_rmac, s, sizeof(struct ethaddr)); | |
2899 | originator_ip.s_addr = stream_get_ipv4(s); | |
2900 | stream_get(&filter, s, sizeof(int)); | |
2901 | svi_ifindex = stream_getl(s); | |
2902 | stream_get(&vrr_rmac, s, sizeof(struct ethaddr)); | |
2903 | is_anycast_mac = stream_getl(s); | |
2904 | ||
2905 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2906 | zlog_debug( | |
2907 | "Rx L3-VNI ADD VRF %s VNI %u RMAC svi-mac %pEA vrr-mac %pEA filter %s svi-if %u", | |
2908 | vrf_id_to_name(vrf_id), l3vni, &svi_rmac, | |
2909 | &vrr_rmac, | |
2910 | filter ? "prefix-routes-only" : "none", | |
2911 | svi_ifindex); | |
2912 | ||
2913 | frrtrace(8, frr_bgp, evpn_local_l3vni_add_zrecv, l3vni, vrf_id, | |
2914 | &svi_rmac, &vrr_rmac, filter, originator_ip, | |
2915 | svi_ifindex, is_anycast_mac); | |
2916 | ||
2917 | bgp_evpn_local_l3vni_add(l3vni, vrf_id, &svi_rmac, &vrr_rmac, | |
2918 | originator_ip, filter, svi_ifindex, | |
2919 | is_anycast_mac); | |
2920 | } else { | |
2921 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2922 | zlog_debug("Rx L3-VNI DEL VRF %s VNI %u", | |
2923 | vrf_id_to_name(vrf_id), l3vni); | |
2924 | ||
2925 | frrtrace(2, frr_bgp, evpn_local_l3vni_del_zrecv, l3vni, vrf_id); | |
2926 | ||
2927 | bgp_evpn_local_l3vni_del(l3vni, vrf_id); | |
2928 | } | |
2929 | ||
2930 | return 0; | |
2931 | } | |
2932 | ||
2933 | static int bgp_zebra_process_local_vni(ZAPI_CALLBACK_ARGS) | |
2934 | { | |
2935 | struct stream *s; | |
2936 | vni_t vni; | |
2937 | struct bgp *bgp; | |
2938 | struct in_addr vtep_ip = {INADDR_ANY}; | |
2939 | vrf_id_t tenant_vrf_id = VRF_DEFAULT; | |
2940 | struct in_addr mcast_grp = {INADDR_ANY}; | |
2941 | ifindex_t svi_ifindex = 0; | |
2942 | ||
2943 | s = zclient->ibuf; | |
2944 | vni = stream_getl(s); | |
2945 | if (cmd == ZEBRA_VNI_ADD) { | |
2946 | vtep_ip.s_addr = stream_get_ipv4(s); | |
2947 | stream_get(&tenant_vrf_id, s, sizeof(vrf_id_t)); | |
2948 | mcast_grp.s_addr = stream_get_ipv4(s); | |
2949 | stream_get(&svi_ifindex, s, sizeof(ifindex_t)); | |
2950 | } | |
2951 | ||
2952 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
2953 | if (!bgp) | |
2954 | return 0; | |
2955 | ||
2956 | if (BGP_DEBUG(zebra, ZEBRA)) | |
2957 | zlog_debug( | |
2958 | "Rx VNI %s VRF %s VNI %u tenant-vrf %s SVI ifindex %u", | |
2959 | (cmd == ZEBRA_VNI_ADD) ? "add" : "del", | |
2960 | vrf_id_to_name(vrf_id), vni, | |
2961 | vrf_id_to_name(tenant_vrf_id), svi_ifindex); | |
2962 | ||
2963 | if (cmd == ZEBRA_VNI_ADD) { | |
2964 | frrtrace(4, frr_bgp, evpn_local_vni_add_zrecv, vni, vtep_ip, | |
2965 | tenant_vrf_id, mcast_grp); | |
2966 | ||
2967 | return bgp_evpn_local_vni_add( | |
2968 | bgp, vni, | |
2969 | vtep_ip.s_addr != INADDR_ANY ? vtep_ip : bgp->router_id, | |
2970 | tenant_vrf_id, mcast_grp, svi_ifindex); | |
2971 | } else { | |
2972 | frrtrace(1, frr_bgp, evpn_local_vni_del_zrecv, vni); | |
2973 | ||
2974 | return bgp_evpn_local_vni_del(bgp, vni); | |
2975 | } | |
2976 | } | |
2977 | ||
2978 | static int bgp_zebra_process_local_macip(ZAPI_CALLBACK_ARGS) | |
2979 | { | |
2980 | struct stream *s; | |
2981 | vni_t vni; | |
2982 | struct bgp *bgp; | |
2983 | struct ethaddr mac; | |
2984 | struct ipaddr ip; | |
2985 | int ipa_len; | |
2986 | uint8_t flags = 0; | |
2987 | uint32_t seqnum = 0; | |
2988 | int state = 0; | |
2989 | char buf2[ESI_STR_LEN]; | |
2990 | esi_t esi; | |
2991 | ||
2992 | memset(&ip, 0, sizeof(ip)); | |
2993 | s = zclient->ibuf; | |
2994 | vni = stream_getl(s); | |
2995 | stream_get(&mac.octet, s, ETH_ALEN); | |
2996 | ipa_len = stream_getl(s); | |
2997 | if (ipa_len != 0 && ipa_len != IPV4_MAX_BYTELEN | |
2998 | && ipa_len != IPV6_MAX_BYTELEN) { | |
2999 | flog_err(EC_BGP_MACIP_LEN, | |
3000 | "%u:Recv MACIP %s with invalid IP addr length %d", | |
3001 | vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", | |
3002 | ipa_len); | |
3003 | return -1; | |
3004 | } | |
3005 | ||
3006 | if (ipa_len) { | |
3007 | ip.ipa_type = | |
3008 | (ipa_len == IPV4_MAX_BYTELEN) ? IPADDR_V4 : IPADDR_V6; | |
3009 | stream_get(&ip.ip.addr, s, ipa_len); | |
3010 | } | |
3011 | if (cmd == ZEBRA_MACIP_ADD) { | |
3012 | flags = stream_getc(s); | |
3013 | seqnum = stream_getl(s); | |
3014 | stream_get(&esi, s, sizeof(esi_t)); | |
3015 | } else { | |
3016 | state = stream_getl(s); | |
3017 | memset(&esi, 0, sizeof(esi_t)); | |
3018 | } | |
3019 | ||
3020 | bgp = bgp_lookup_by_vrf_id(vrf_id); | |
3021 | if (!bgp) | |
3022 | return 0; | |
3023 | ||
3024 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3025 | zlog_debug( | |
3026 | "%u:Recv MACIP %s f 0x%x MAC %pEA IP %pIA VNI %u seq %u state %d ESI %s", | |
3027 | vrf_id, (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags, | |
3028 | &mac, &ip, vni, seqnum, state, | |
3029 | esi_to_str(&esi, buf2, sizeof(buf2))); | |
3030 | ||
3031 | if (cmd == ZEBRA_MACIP_ADD) { | |
3032 | frrtrace(6, frr_bgp, evpn_local_macip_add_zrecv, vni, &mac, &ip, | |
3033 | flags, seqnum, &esi); | |
3034 | ||
3035 | return bgp_evpn_local_macip_add(bgp, vni, &mac, &ip, | |
3036 | flags, seqnum, &esi); | |
3037 | } else { | |
3038 | frrtrace(4, frr_bgp, evpn_local_macip_del_zrecv, vni, &mac, &ip, | |
3039 | state); | |
3040 | ||
3041 | return bgp_evpn_local_macip_del(bgp, vni, &mac, &ip, state); | |
3042 | } | |
3043 | } | |
3044 | ||
3045 | static int bgp_zebra_process_local_ip_prefix(ZAPI_CALLBACK_ARGS) | |
3046 | { | |
3047 | struct stream *s = NULL; | |
3048 | struct bgp *bgp_vrf = NULL; | |
3049 | struct prefix p; | |
3050 | ||
3051 | memset(&p, 0, sizeof(struct prefix)); | |
3052 | s = zclient->ibuf; | |
3053 | stream_get(&p, s, sizeof(struct prefix)); | |
3054 | ||
3055 | bgp_vrf = bgp_lookup_by_vrf_id(vrf_id); | |
3056 | if (!bgp_vrf) | |
3057 | return 0; | |
3058 | ||
3059 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3060 | zlog_debug("Recv prefix %pFX %s on vrf %s", &p, | |
3061 | (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) ? "ADD" : "DEL", | |
3062 | vrf_id_to_name(vrf_id)); | |
3063 | ||
3064 | if (cmd == ZEBRA_IP_PREFIX_ROUTE_ADD) { | |
3065 | ||
3066 | if (p.family == AF_INET) | |
3067 | bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL, | |
3068 | AFI_IP, SAFI_UNICAST); | |
3069 | else | |
3070 | bgp_evpn_advertise_type5_route(bgp_vrf, &p, NULL, | |
3071 | AFI_IP6, SAFI_UNICAST); | |
3072 | ||
3073 | } else { | |
3074 | if (p.family == AF_INET) | |
3075 | bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP, | |
3076 | SAFI_UNICAST); | |
3077 | else | |
3078 | bgp_evpn_withdraw_type5_route(bgp_vrf, &p, AFI_IP6, | |
3079 | SAFI_UNICAST); | |
3080 | } | |
3081 | return 0; | |
3082 | } | |
3083 | ||
3084 | static int bgp_zebra_process_label_chunk(ZAPI_CALLBACK_ARGS) | |
3085 | { | |
3086 | struct stream *s = NULL; | |
3087 | uint8_t response_keep; | |
3088 | uint32_t first; | |
3089 | uint32_t last; | |
3090 | uint8_t proto; | |
3091 | unsigned short instance; | |
3092 | ||
3093 | s = zclient->ibuf; | |
3094 | STREAM_GETC(s, proto); | |
3095 | STREAM_GETW(s, instance); | |
3096 | STREAM_GETC(s, response_keep); | |
3097 | STREAM_GETL(s, first); | |
3098 | STREAM_GETL(s, last); | |
3099 | ||
3100 | if (zclient->redist_default != proto) { | |
3101 | flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong proto %u", | |
3102 | proto); | |
3103 | return 0; | |
3104 | } | |
3105 | if (zclient->instance != instance) { | |
3106 | flog_err(EC_BGP_LM_ERROR, "Got LM msg with wrong instance %u", | |
3107 | proto); | |
3108 | return 0; | |
3109 | } | |
3110 | ||
3111 | if (first > last || | |
3112 | first < MPLS_LABEL_UNRESERVED_MIN || | |
3113 | last > MPLS_LABEL_UNRESERVED_MAX) { | |
3114 | ||
3115 | flog_err(EC_BGP_LM_ERROR, "%s: Invalid Label chunk: %u - %u", | |
3116 | __func__, first, last); | |
3117 | return 0; | |
3118 | } | |
3119 | if (BGP_DEBUG(zebra, ZEBRA)) { | |
3120 | zlog_debug("Label Chunk assign: %u - %u (%u) ", | |
3121 | first, last, response_keep); | |
3122 | } | |
3123 | ||
3124 | bgp_lp_event_chunk(response_keep, first, last); | |
3125 | ||
3126 | return 0; | |
3127 | ||
3128 | stream_failure: /* for STREAM_GETX */ | |
3129 | return -1; | |
3130 | } | |
3131 | ||
3132 | extern struct zebra_privs_t bgpd_privs; | |
3133 | ||
3134 | static int bgp_ifp_create(struct interface *ifp) | |
3135 | { | |
3136 | struct bgp *bgp; | |
3137 | ||
3138 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3139 | zlog_debug("Rx Intf add VRF %u IF %s", ifp->vrf->vrf_id, | |
3140 | ifp->name); | |
3141 | ||
3142 | bgp = ifp->vrf->info; | |
3143 | if (!bgp) | |
3144 | return 0; | |
3145 | ||
3146 | bgp_mac_add_mac_entry(ifp); | |
3147 | ||
3148 | bgp_update_interface_nbrs(bgp, ifp, ifp); | |
3149 | hook_call(bgp_vrf_status_changed, bgp, ifp); | |
3150 | return 0; | |
3151 | } | |
3152 | ||
3153 | static int bgp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS) | |
3154 | { | |
3155 | struct stream *s = NULL; | |
3156 | struct bgp *bgp = bgp_get_default(); | |
3157 | struct listnode *node; | |
3158 | struct prefix_ipv6 *c; | |
3159 | struct srv6_locator_chunk s6c = {}; | |
3160 | struct prefix_ipv6 *chunk = NULL; | |
3161 | ||
3162 | s = zclient->ibuf; | |
3163 | zapi_srv6_locator_chunk_decode(s, &s6c); | |
3164 | ||
3165 | if (strcmp(bgp->srv6_locator_name, s6c.locator_name) != 0) { | |
3166 | zlog_err("%s: Locator name unmatch %s:%s", __func__, | |
3167 | bgp->srv6_locator_name, s6c.locator_name); | |
3168 | return 0; | |
3169 | } | |
3170 | ||
3171 | for (ALL_LIST_ELEMENTS_RO(bgp->srv6_locator_chunks, node, c)) { | |
3172 | if (!prefix_cmp(c, &s6c.prefix)) | |
3173 | return 0; | |
3174 | } | |
3175 | ||
3176 | chunk = prefix_ipv6_new(); | |
3177 | *chunk = s6c.prefix; | |
3178 | listnode_add(bgp->srv6_locator_chunks, chunk); | |
3179 | vpn_leak_postchange_all(); | |
3180 | return 0; | |
3181 | } | |
3182 | ||
3183 | static int bgp_zebra_process_srv6_locator_add(ZAPI_CALLBACK_ARGS) | |
3184 | { | |
3185 | struct srv6_locator loc = {}; | |
3186 | struct bgp *bgp = bgp_get_default(); | |
3187 | const char *loc_name = bgp->srv6_locator_name; | |
3188 | ||
3189 | if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0) | |
3190 | return -1; | |
3191 | ||
3192 | if (!bgp || !bgp->srv6_enabled) | |
3193 | return 0; | |
3194 | ||
3195 | if (bgp_zebra_srv6_manager_get_locator_chunk(loc_name) < 0) | |
3196 | return -1; | |
3197 | ||
3198 | return 0; | |
3199 | } | |
3200 | ||
3201 | static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS) | |
3202 | { | |
3203 | struct srv6_locator loc = {}; | |
3204 | struct bgp *bgp = bgp_get_default(); | |
3205 | struct listnode *node, *nnode; | |
3206 | struct prefix_ipv6 *chunk; | |
3207 | struct bgp_srv6_function *func; | |
3208 | struct bgp *bgp_vrf; | |
3209 | struct in6_addr *tovpn_sid; | |
3210 | struct prefix_ipv6 tmp_prefi; | |
3211 | ||
3212 | if (zapi_srv6_locator_decode(zclient->ibuf, &loc) < 0) | |
3213 | return -1; | |
3214 | ||
3215 | // refresh chunks | |
3216 | for (ALL_LIST_ELEMENTS(bgp->srv6_locator_chunks, node, nnode, chunk)) | |
3217 | if (prefix_match((struct prefix *)&loc.prefix, | |
3218 | (struct prefix *)chunk)) | |
3219 | listnode_delete(bgp->srv6_locator_chunks, chunk); | |
3220 | ||
3221 | // refresh functions | |
3222 | for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) { | |
3223 | tmp_prefi.family = AF_INET6; | |
3224 | tmp_prefi.prefixlen = 128; | |
3225 | tmp_prefi.prefix = func->sid; | |
3226 | if (prefix_match((struct prefix *)&loc.prefix, | |
3227 | (struct prefix *)&tmp_prefi)) | |
3228 | listnode_delete(bgp->srv6_functions, func); | |
3229 | } | |
3230 | ||
3231 | // refresh tovpn_sid | |
3232 | for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) { | |
3233 | if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF) | |
3234 | continue; | |
3235 | ||
3236 | // refresh vpnv4 tovpn_sid | |
3237 | tovpn_sid = bgp_vrf->vpn_policy[AFI_IP].tovpn_sid; | |
3238 | if (tovpn_sid) { | |
3239 | tmp_prefi.family = AF_INET6; | |
3240 | tmp_prefi.prefixlen = 128; | |
3241 | tmp_prefi.prefix = *tovpn_sid; | |
3242 | if (prefix_match((struct prefix *)&loc.prefix, | |
3243 | (struct prefix *)&tmp_prefi)) | |
3244 | XFREE(MTYPE_BGP_SRV6_SID, | |
3245 | bgp_vrf->vpn_policy[AFI_IP].tovpn_sid); | |
3246 | } | |
3247 | ||
3248 | // refresh vpnv6 tovpn_sid | |
3249 | tovpn_sid = bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid; | |
3250 | if (tovpn_sid) { | |
3251 | tmp_prefi.family = AF_INET6; | |
3252 | tmp_prefi.prefixlen = 128; | |
3253 | tmp_prefi.prefix = *tovpn_sid; | |
3254 | if (prefix_match((struct prefix *)&loc.prefix, | |
3255 | (struct prefix *)&tmp_prefi)) | |
3256 | XFREE(MTYPE_BGP_SRV6_SID, | |
3257 | bgp_vrf->vpn_policy[AFI_IP6].tovpn_sid); | |
3258 | } | |
3259 | } | |
3260 | ||
3261 | vpn_leak_postchange_all(); | |
3262 | return 0; | |
3263 | } | |
3264 | ||
3265 | static zclient_handler *const bgp_handlers[] = { | |
3266 | [ZEBRA_ROUTER_ID_UPDATE] = bgp_router_id_update, | |
3267 | [ZEBRA_INTERFACE_ADDRESS_ADD] = bgp_interface_address_add, | |
3268 | [ZEBRA_INTERFACE_ADDRESS_DELETE] = bgp_interface_address_delete, | |
3269 | [ZEBRA_INTERFACE_NBR_ADDRESS_ADD] = bgp_interface_nbr_address_add, | |
3270 | [ZEBRA_INTERFACE_NBR_ADDRESS_DELETE] = bgp_interface_nbr_address_delete, | |
3271 | [ZEBRA_INTERFACE_VRF_UPDATE] = bgp_interface_vrf_update, | |
3272 | [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = zebra_read_route, | |
3273 | [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = zebra_read_route, | |
3274 | [ZEBRA_NEXTHOP_UPDATE] = bgp_read_nexthop_update, | |
3275 | [ZEBRA_FEC_UPDATE] = bgp_read_fec_update, | |
3276 | [ZEBRA_LOCAL_ES_ADD] = bgp_zebra_process_local_es_add, | |
3277 | [ZEBRA_LOCAL_ES_DEL] = bgp_zebra_process_local_es_del, | |
3278 | [ZEBRA_VNI_ADD] = bgp_zebra_process_local_vni, | |
3279 | [ZEBRA_LOCAL_ES_EVI_ADD] = bgp_zebra_process_local_es_evi, | |
3280 | [ZEBRA_LOCAL_ES_EVI_DEL] = bgp_zebra_process_local_es_evi, | |
3281 | [ZEBRA_VNI_DEL] = bgp_zebra_process_local_vni, | |
3282 | [ZEBRA_MACIP_ADD] = bgp_zebra_process_local_macip, | |
3283 | [ZEBRA_MACIP_DEL] = bgp_zebra_process_local_macip, | |
3284 | [ZEBRA_L3VNI_ADD] = bgp_zebra_process_local_l3vni, | |
3285 | [ZEBRA_L3VNI_DEL] = bgp_zebra_process_local_l3vni, | |
3286 | [ZEBRA_IP_PREFIX_ROUTE_ADD] = bgp_zebra_process_local_ip_prefix, | |
3287 | [ZEBRA_IP_PREFIX_ROUTE_DEL] = bgp_zebra_process_local_ip_prefix, | |
3288 | [ZEBRA_GET_LABEL_CHUNK] = bgp_zebra_process_label_chunk, | |
3289 | [ZEBRA_RULE_NOTIFY_OWNER] = rule_notify_owner, | |
3290 | [ZEBRA_IPSET_NOTIFY_OWNER] = ipset_notify_owner, | |
3291 | [ZEBRA_IPSET_ENTRY_NOTIFY_OWNER] = ipset_entry_notify_owner, | |
3292 | [ZEBRA_IPTABLE_NOTIFY_OWNER] = iptable_notify_owner, | |
3293 | [ZEBRA_ROUTE_NOTIFY_OWNER] = bgp_zebra_route_notify_owner, | |
3294 | [ZEBRA_SRV6_LOCATOR_ADD] = bgp_zebra_process_srv6_locator_add, | |
3295 | [ZEBRA_SRV6_LOCATOR_DELETE] = bgp_zebra_process_srv6_locator_delete, | |
3296 | [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] = | |
3297 | bgp_zebra_process_srv6_locator_chunk, | |
3298 | }; | |
3299 | ||
3300 | void bgp_zebra_init(struct thread_master *master, unsigned short instance) | |
3301 | { | |
3302 | zclient_num_connects = 0; | |
3303 | ||
3304 | if_zapi_callbacks(bgp_ifp_create, bgp_ifp_up, | |
3305 | bgp_ifp_down, bgp_ifp_destroy); | |
3306 | ||
3307 | /* Set default values. */ | |
3308 | zclient = zclient_new(master, &zclient_options_default, bgp_handlers, | |
3309 | array_size(bgp_handlers)); | |
3310 | zclient_init(zclient, ZEBRA_ROUTE_BGP, 0, &bgpd_privs); | |
3311 | zclient->zebra_connected = bgp_zebra_connected; | |
3312 | zclient->instance = instance; | |
3313 | } | |
3314 | ||
3315 | void bgp_zebra_destroy(void) | |
3316 | { | |
3317 | if (zclient == NULL) | |
3318 | return; | |
3319 | zclient_stop(zclient); | |
3320 | zclient_free(zclient); | |
3321 | zclient = NULL; | |
3322 | } | |
3323 | ||
3324 | int bgp_zebra_num_connects(void) | |
3325 | { | |
3326 | return zclient_num_connects; | |
3327 | } | |
3328 | ||
3329 | void bgp_send_pbr_rule_action(struct bgp_pbr_action *pbra, | |
3330 | struct bgp_pbr_rule *pbr, | |
3331 | bool install) | |
3332 | { | |
3333 | struct stream *s; | |
3334 | ||
3335 | if (pbra->install_in_progress && !pbr) | |
3336 | return; | |
3337 | if (pbr && pbr->install_in_progress) | |
3338 | return; | |
3339 | if (BGP_DEBUG(zebra, ZEBRA)) { | |
3340 | if (pbr) | |
3341 | zlog_debug("%s: table %d (ip rule) %d", __func__, | |
3342 | pbra->table_id, install); | |
3343 | else | |
3344 | zlog_debug("%s: table %d fwmark %d %d", __func__, | |
3345 | pbra->table_id, pbra->fwmark, install); | |
3346 | } | |
3347 | s = zclient->obuf; | |
3348 | stream_reset(s); | |
3349 | ||
3350 | zclient_create_header(s, | |
3351 | install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE, | |
3352 | VRF_DEFAULT); | |
3353 | stream_putl(s, 1); /* send one pbr action */ | |
3354 | ||
3355 | bgp_encode_pbr_rule_action(s, pbra, pbr); | |
3356 | ||
3357 | stream_putw_at(s, 0, stream_get_endp(s)); | |
3358 | if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) | |
3359 | && install) { | |
3360 | if (!pbr) | |
3361 | pbra->install_in_progress = true; | |
3362 | else | |
3363 | pbr->install_in_progress = true; | |
3364 | } | |
3365 | } | |
3366 | ||
3367 | void bgp_send_pbr_ipset_match(struct bgp_pbr_match *pbrim, bool install) | |
3368 | { | |
3369 | struct stream *s; | |
3370 | ||
3371 | if (pbrim->install_in_progress) | |
3372 | return; | |
3373 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3374 | zlog_debug("%s: name %s type %d %d, ID %u", __func__, | |
3375 | pbrim->ipset_name, pbrim->type, install, | |
3376 | pbrim->unique); | |
3377 | s = zclient->obuf; | |
3378 | stream_reset(s); | |
3379 | ||
3380 | zclient_create_header(s, | |
3381 | install ? ZEBRA_IPSET_CREATE : | |
3382 | ZEBRA_IPSET_DESTROY, | |
3383 | VRF_DEFAULT); | |
3384 | ||
3385 | stream_putl(s, 1); /* send one pbr action */ | |
3386 | ||
3387 | bgp_encode_pbr_ipset_match(s, pbrim); | |
3388 | ||
3389 | stream_putw_at(s, 0, stream_get_endp(s)); | |
3390 | if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install) | |
3391 | pbrim->install_in_progress = true; | |
3392 | } | |
3393 | ||
3394 | void bgp_send_pbr_ipset_entry_match(struct bgp_pbr_match_entry *pbrime, | |
3395 | bool install) | |
3396 | { | |
3397 | struct stream *s; | |
3398 | ||
3399 | if (pbrime->install_in_progress) | |
3400 | return; | |
3401 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3402 | zlog_debug("%s: name %s %d %d, ID %u", __func__, | |
3403 | pbrime->backpointer->ipset_name, pbrime->unique, | |
3404 | install, pbrime->unique); | |
3405 | s = zclient->obuf; | |
3406 | stream_reset(s); | |
3407 | ||
3408 | zclient_create_header(s, | |
3409 | install ? ZEBRA_IPSET_ENTRY_ADD : | |
3410 | ZEBRA_IPSET_ENTRY_DELETE, | |
3411 | VRF_DEFAULT); | |
3412 | ||
3413 | stream_putl(s, 1); /* send one pbr action */ | |
3414 | ||
3415 | bgp_encode_pbr_ipset_entry_match(s, pbrime); | |
3416 | ||
3417 | stream_putw_at(s, 0, stream_get_endp(s)); | |
3418 | if ((zclient_send_message(zclient) != ZCLIENT_SEND_FAILURE) && install) | |
3419 | pbrime->install_in_progress = true; | |
3420 | } | |
3421 | ||
3422 | static void bgp_encode_pbr_interface_list(struct bgp *bgp, struct stream *s, | |
3423 | uint8_t family) | |
3424 | { | |
3425 | struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg; | |
3426 | struct bgp_pbr_interface_head *head; | |
3427 | struct bgp_pbr_interface *pbr_if; | |
3428 | struct interface *ifp; | |
3429 | ||
3430 | if (!bgp_pbr_cfg) | |
3431 | return; | |
3432 | if (family == AF_INET) | |
3433 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv4); | |
3434 | else | |
3435 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv6); | |
3436 | RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) { | |
3437 | ifp = if_lookup_by_name(pbr_if->name, bgp->vrf_id); | |
3438 | if (ifp) | |
3439 | stream_putl(s, ifp->ifindex); | |
3440 | } | |
3441 | } | |
3442 | ||
3443 | static int bgp_pbr_get_ifnumber(struct bgp *bgp, uint8_t family) | |
3444 | { | |
3445 | struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg; | |
3446 | struct bgp_pbr_interface_head *head; | |
3447 | struct bgp_pbr_interface *pbr_if; | |
3448 | int cnt = 0; | |
3449 | ||
3450 | if (!bgp_pbr_cfg) | |
3451 | return 0; | |
3452 | if (family == AF_INET) | |
3453 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv4); | |
3454 | else | |
3455 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv6); | |
3456 | RB_FOREACH (pbr_if, bgp_pbr_interface_head, head) { | |
3457 | if (if_lookup_by_name(pbr_if->name, bgp->vrf_id)) | |
3458 | cnt++; | |
3459 | } | |
3460 | return cnt; | |
3461 | } | |
3462 | ||
3463 | void bgp_send_pbr_iptable(struct bgp_pbr_action *pba, | |
3464 | struct bgp_pbr_match *pbm, | |
3465 | bool install) | |
3466 | { | |
3467 | struct stream *s; | |
3468 | int ret = 0; | |
3469 | int nb_interface; | |
3470 | ||
3471 | if (pbm->install_iptable_in_progress) | |
3472 | return; | |
3473 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3474 | zlog_debug("%s: name %s type %d mark %d %d, ID %u", __func__, | |
3475 | pbm->ipset_name, pbm->type, pba->fwmark, install, | |
3476 | pbm->unique2); | |
3477 | s = zclient->obuf; | |
3478 | stream_reset(s); | |
3479 | ||
3480 | zclient_create_header(s, | |
3481 | install ? ZEBRA_IPTABLE_ADD : | |
3482 | ZEBRA_IPTABLE_DELETE, | |
3483 | VRF_DEFAULT); | |
3484 | ||
3485 | bgp_encode_pbr_iptable_match(s, pba, pbm); | |
3486 | nb_interface = bgp_pbr_get_ifnumber(pba->bgp, pbm->family); | |
3487 | stream_putl(s, nb_interface); | |
3488 | if (nb_interface) | |
3489 | bgp_encode_pbr_interface_list(pba->bgp, s, pbm->family); | |
3490 | stream_putw_at(s, 0, stream_get_endp(s)); | |
3491 | ret = zclient_send_message(zclient); | |
3492 | if (install) { | |
3493 | if (ret != ZCLIENT_SEND_FAILURE) | |
3494 | pba->refcnt++; | |
3495 | else | |
3496 | pbm->install_iptable_in_progress = true; | |
3497 | } | |
3498 | } | |
3499 | ||
3500 | /* inject in table <table_id> a default route to: | |
3501 | * - if nexthop IP is present : to this nexthop | |
3502 | * - if vrf is different from local : to the matching VRF | |
3503 | */ | |
3504 | void bgp_zebra_announce_default(struct bgp *bgp, struct nexthop *nh, | |
3505 | afi_t afi, uint32_t table_id, bool announce) | |
3506 | { | |
3507 | struct zapi_nexthop *api_nh; | |
3508 | struct zapi_route api; | |
3509 | struct prefix p; | |
3510 | ||
3511 | if (!nh || (nh->type != NEXTHOP_TYPE_IPV4 | |
3512 | && nh->type != NEXTHOP_TYPE_IPV6) | |
3513 | || nh->vrf_id == VRF_UNKNOWN) | |
3514 | return; | |
3515 | ||
3516 | /* in vrf-lite, no default route has to be announced | |
3517 | * the table id of vrf is directly used to divert traffic | |
3518 | */ | |
3519 | if (!vrf_is_backend_netns() && bgp->vrf_id != nh->vrf_id) | |
3520 | return; | |
3521 | ||
3522 | memset(&p, 0, sizeof(struct prefix)); | |
3523 | if (afi != AFI_IP && afi != AFI_IP6) | |
3524 | return; | |
3525 | p.family = afi2family(afi); | |
3526 | memset(&api, 0, sizeof(api)); | |
3527 | api.vrf_id = bgp->vrf_id; | |
3528 | api.type = ZEBRA_ROUTE_BGP; | |
3529 | api.safi = SAFI_UNICAST; | |
3530 | api.prefix = p; | |
3531 | api.tableid = table_id; | |
3532 | api.nexthop_num = 1; | |
3533 | SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID); | |
3534 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
3535 | api_nh = &api.nexthops[0]; | |
3536 | ||
3537 | api.distance = ZEBRA_EBGP_DISTANCE_DEFAULT; | |
3538 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
3539 | ||
3540 | /* redirect IP */ | |
3541 | if (afi == AFI_IP && nh->gate.ipv4.s_addr != INADDR_ANY) { | |
3542 | char buff[PREFIX_STRLEN]; | |
3543 | ||
3544 | api_nh->vrf_id = nh->vrf_id; | |
3545 | api_nh->gate.ipv4 = nh->gate.ipv4; | |
3546 | api_nh->type = NEXTHOP_TYPE_IPV4; | |
3547 | ||
3548 | inet_ntop(AF_INET, &(nh->gate.ipv4), buff, INET_ADDRSTRLEN); | |
3549 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3550 | zlog_debug("BGP: %s default route to %s table %d (redirect IP)", | |
3551 | announce ? "adding" : "withdrawing", | |
3552 | buff, table_id); | |
3553 | zclient_route_send(announce ? ZEBRA_ROUTE_ADD | |
3554 | : ZEBRA_ROUTE_DELETE, | |
3555 | zclient, &api); | |
3556 | } else if (afi == AFI_IP6 && | |
3557 | memcmp(&nh->gate.ipv6, | |
3558 | &in6addr_any, sizeof(struct in6_addr))) { | |
3559 | char buff[PREFIX_STRLEN]; | |
3560 | ||
3561 | api_nh->vrf_id = nh->vrf_id; | |
3562 | memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, | |
3563 | sizeof(struct in6_addr)); | |
3564 | api_nh->type = NEXTHOP_TYPE_IPV6; | |
3565 | ||
3566 | inet_ntop(AF_INET6, &(nh->gate.ipv6), buff, INET_ADDRSTRLEN); | |
3567 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3568 | zlog_debug("BGP: %s default route to %s table %d (redirect IP)", | |
3569 | announce ? "adding" : "withdrawing", | |
3570 | buff, table_id); | |
3571 | zclient_route_send(announce ? ZEBRA_ROUTE_ADD | |
3572 | : ZEBRA_ROUTE_DELETE, | |
3573 | zclient, &api); | |
3574 | } else if (nh->vrf_id != bgp->vrf_id) { | |
3575 | struct vrf *vrf; | |
3576 | struct interface *ifp; | |
3577 | ||
3578 | vrf = vrf_lookup_by_id(nh->vrf_id); | |
3579 | if (!vrf) | |
3580 | return; | |
3581 | /* create default route with interface <VRF> | |
3582 | * with nexthop-vrf <VRF> | |
3583 | */ | |
3584 | ifp = if_lookup_by_name_vrf(vrf->name, vrf); | |
3585 | if (!ifp) | |
3586 | return; | |
3587 | api_nh->vrf_id = nh->vrf_id; | |
3588 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
3589 | api_nh->ifindex = ifp->ifindex; | |
3590 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3591 | zlog_info("BGP: %s default route to %s table %d (redirect VRF)", | |
3592 | announce ? "adding" : "withdrawing", | |
3593 | vrf->name, table_id); | |
3594 | zclient_route_send(announce ? ZEBRA_ROUTE_ADD | |
3595 | : ZEBRA_ROUTE_DELETE, | |
3596 | zclient, &api); | |
3597 | return; | |
3598 | } | |
3599 | } | |
3600 | ||
3601 | /* Send capabilities to RIB */ | |
3602 | int bgp_zebra_send_capabilities(struct bgp *bgp, bool disable) | |
3603 | { | |
3604 | struct zapi_cap api; | |
3605 | int ret = BGP_GR_SUCCESS; | |
3606 | ||
3607 | if (zclient == NULL) { | |
3608 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3609 | zlog_debug("zclient invalid"); | |
3610 | return BGP_GR_FAILURE; | |
3611 | } | |
3612 | ||
3613 | /* Check if the client is connected */ | |
3614 | if ((zclient->sock < 0) || (zclient->t_connect)) { | |
3615 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3616 | zlog_debug("client not connected"); | |
3617 | return BGP_GR_FAILURE; | |
3618 | } | |
3619 | ||
3620 | /* Check if capability is already sent. If the flag force is set | |
3621 | * send the capability since this can be initial bgp configuration | |
3622 | */ | |
3623 | memset(&api, 0, sizeof(struct zapi_cap)); | |
3624 | if (disable) { | |
3625 | api.cap = ZEBRA_CLIENT_GR_DISABLE; | |
3626 | api.vrf_id = bgp->vrf_id; | |
3627 | } else { | |
3628 | api.cap = ZEBRA_CLIENT_GR_CAPABILITIES; | |
3629 | api.stale_removal_time = bgp->rib_stale_time; | |
3630 | api.vrf_id = bgp->vrf_id; | |
3631 | } | |
3632 | ||
3633 | if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api) | |
3634 | == ZCLIENT_SEND_FAILURE) { | |
3635 | zlog_err("error sending capability"); | |
3636 | ret = BGP_GR_FAILURE; | |
3637 | } else { | |
3638 | if (disable) | |
3639 | bgp->present_zebra_gr_state = ZEBRA_GR_DISABLE; | |
3640 | else | |
3641 | bgp->present_zebra_gr_state = ZEBRA_GR_ENABLE; | |
3642 | ||
3643 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3644 | zlog_debug("send capabilty success"); | |
3645 | ret = BGP_GR_SUCCESS; | |
3646 | } | |
3647 | return ret; | |
3648 | } | |
3649 | ||
3650 | /* Send route update pesding or completed status to RIB for the | |
3651 | * specific AFI, SAFI | |
3652 | */ | |
3653 | int bgp_zebra_update(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type) | |
3654 | { | |
3655 | struct zapi_cap api = {0}; | |
3656 | ||
3657 | if (zclient == NULL) { | |
3658 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3659 | zlog_debug("zclient == NULL, invalid"); | |
3660 | return BGP_GR_FAILURE; | |
3661 | } | |
3662 | ||
3663 | /* Check if the client is connected */ | |
3664 | if ((zclient->sock < 0) || (zclient->t_connect)) { | |
3665 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3666 | zlog_debug("client not connected"); | |
3667 | return BGP_GR_FAILURE; | |
3668 | } | |
3669 | ||
3670 | api.afi = afi; | |
3671 | api.safi = safi; | |
3672 | api.vrf_id = vrf_id; | |
3673 | api.cap = type; | |
3674 | ||
3675 | if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api) | |
3676 | == ZCLIENT_SEND_FAILURE) { | |
3677 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3678 | zlog_debug("error sending capability"); | |
3679 | return BGP_GR_FAILURE; | |
3680 | } | |
3681 | return BGP_GR_SUCCESS; | |
3682 | } | |
3683 | ||
3684 | ||
3685 | /* Send RIB stale timer update */ | |
3686 | int bgp_zebra_stale_timer_update(struct bgp *bgp) | |
3687 | { | |
3688 | struct zapi_cap api; | |
3689 | ||
3690 | if (zclient == NULL) { | |
3691 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3692 | zlog_debug("zclient invalid"); | |
3693 | return BGP_GR_FAILURE; | |
3694 | } | |
3695 | ||
3696 | /* Check if the client is connected */ | |
3697 | if ((zclient->sock < 0) || (zclient->t_connect)) { | |
3698 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3699 | zlog_debug("client not connected"); | |
3700 | return BGP_GR_FAILURE; | |
3701 | } | |
3702 | ||
3703 | memset(&api, 0, sizeof(struct zapi_cap)); | |
3704 | api.cap = ZEBRA_CLIENT_RIB_STALE_TIME; | |
3705 | api.stale_removal_time = bgp->rib_stale_time; | |
3706 | api.vrf_id = bgp->vrf_id; | |
3707 | if (zclient_capabilities_send(ZEBRA_CLIENT_CAPABILITIES, zclient, &api) | |
3708 | == ZCLIENT_SEND_FAILURE) { | |
3709 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3710 | zlog_debug("error sending capability"); | |
3711 | return BGP_GR_FAILURE; | |
3712 | } | |
3713 | if (BGP_DEBUG(zebra, ZEBRA)) | |
3714 | zlog_debug("send capabilty success"); | |
3715 | return BGP_GR_SUCCESS; | |
3716 | } | |
3717 | ||
3718 | int bgp_zebra_srv6_manager_get_locator_chunk(const char *name) | |
3719 | { | |
3720 | return srv6_manager_get_locator_chunk(zclient, name); | |
3721 | } | |
3722 | ||
3723 | int bgp_zebra_srv6_manager_release_locator_chunk(const char *name) | |
3724 | { | |
3725 | return srv6_manager_release_locator_chunk(zclient, name); | |
3726 | } |