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