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