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