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