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