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