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