]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_mplsvpn.h
Merge pull request #12140 from opensourcerouting/fix/watchfrr_dont_givup
[mirror_frr.git] / bgpd / bgp_mplsvpn.h
1 /* MPLS-VPN
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GxNU 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 #ifndef _QUAGGA_BGP_MPLSVPN_H
22 #define _QUAGGA_BGP_MPLSVPN_H
23
24 #include "bgpd/bgp_attr.h"
25 #include "bgpd/bgp_route.h"
26 #include "bgpd/bgp_rd.h"
27 #include "bgpd/bgp_zebra.h"
28 #include "bgpd/bgp_vty.h"
29
30 #define MPLS_LABEL_IS_SPECIAL(label) ((label) <= MPLS_LABEL_EXTENSION)
31 #define MPLS_LABEL_IS_NULL(label) \
32 ((label) == MPLS_LABEL_IPV4_EXPLICIT_NULL \
33 || (label) == MPLS_LABEL_IPV6_EXPLICIT_NULL \
34 || (label) == MPLS_LABEL_IMPLICIT_NULL)
35
36 #define BGP_VPNVX_HELP_STR BGP_AF_STR BGP_AF_STR
37
38 #define V4_HEADER \
39 " Network Next Hop Metric LocPrf Weight Path\n"
40 #define V4_HEADER_TAG " Network Next Hop In tag/Out tag\n"
41 #define V4_HEADER_OVERLAY \
42 " Network Next Hop EthTag Overlay Index RouterMac\n"
43
44 #define BGP_PREFIX_SID_SRV6_MAX_FUNCTION_LENGTH 20
45
46 extern void bgp_mplsvpn_init(void);
47 extern int bgp_nlri_parse_vpn(struct peer *, struct attr *, struct bgp_nlri *);
48 extern uint32_t decode_label(mpls_label_t *);
49 extern void encode_label(mpls_label_t, mpls_label_t *);
50
51 extern int argv_find_and_parse_vpnvx(struct cmd_token **argv, int argc,
52 int *index, afi_t *afi);
53 extern int bgp_show_mpls_vpn(struct vty *vty, afi_t afi, struct prefix_rd *prd,
54 enum bgp_show_type type, void *output_arg,
55 int tags, bool use_json);
56
57 extern void vpn_leak_from_vrf_update(struct bgp *to_bgp, struct bgp *from_bgp,
58 struct bgp_path_info *path_vrf);
59
60 extern void vpn_leak_from_vrf_withdraw(struct bgp *to_bgp, struct bgp *from_bgp,
61 struct bgp_path_info *path_vrf);
62
63 extern void vpn_leak_from_vrf_withdraw_all(struct bgp *to_bgp,
64 struct bgp *from_bgp, afi_t afi);
65
66 extern void vpn_leak_from_vrf_update_all(struct bgp *to_bgp,
67 struct bgp *from_bgp, afi_t afi);
68
69 extern void vpn_leak_to_vrf_withdraw_all(struct bgp *to_bgp, afi_t afi);
70
71 extern void vpn_leak_to_vrf_update_all(struct bgp *to_bgp, struct bgp *from_bgp,
72 afi_t afi);
73
74 extern bool vpn_leak_to_vrf_update(struct bgp *from_bgp,
75 struct bgp_path_info *path_vpn,
76 struct prefix_rd *prd);
77
78 extern void vpn_leak_to_vrf_withdraw(struct bgp *from_bgp,
79 struct bgp_path_info *path_vpn);
80
81 extern void vpn_leak_zebra_vrf_label_update(struct bgp *bgp, afi_t afi);
82 extern void vpn_leak_zebra_vrf_label_withdraw(struct bgp *bgp, afi_t afi);
83 extern void vpn_leak_zebra_vrf_sid_update(struct bgp *bgp, afi_t afi);
84 extern void vpn_leak_zebra_vrf_sid_withdraw(struct bgp *bgp, afi_t afi);
85 extern int vpn_leak_label_callback(mpls_label_t label, void *lblid, bool alloc);
86 extern void ensure_vrf_tovpn_sid(struct bgp *vpn, struct bgp *vrf, afi_t afi);
87 extern void transpose_sid(struct in6_addr *sid, uint32_t label, uint8_t offset,
88 uint8_t size);
89 extern void vrf_import_from_vrf(struct bgp *to_bgp, struct bgp *from_bgp,
90 afi_t afi, safi_t safi);
91 void vrf_unimport_from_vrf(struct bgp *to_bgp, struct bgp *from_bgp,
92 afi_t afi, safi_t safi);
93
94 static inline bool is_bgp_vrf_mplsvpn(struct bgp *bgp)
95 {
96 afi_t afi;
97
98 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
99 for (afi = 0; afi < AFI_MAX; ++afi) {
100 if (CHECK_FLAG(bgp->af_flags[afi][SAFI_UNICAST],
101 BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT)
102 || CHECK_FLAG(bgp->af_flags[afi][SAFI_UNICAST],
103 BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT))
104 return true;
105 }
106 return false;
107 }
108
109 static inline int vpn_leak_to_vpn_active(struct bgp *bgp_vrf, afi_t afi,
110 const char **pmsg)
111 {
112 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF
113 && bgp_vrf->inst_type != BGP_INSTANCE_TYPE_DEFAULT) {
114
115 if (pmsg)
116 *pmsg = "source bgp instance neither vrf nor default";
117 return 0;
118 }
119
120 /* Is vrf configured to export to vpn? */
121 if (!CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
122 BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT)
123 && !CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
124 BGP_CONFIG_VRF_TO_VRF_EXPORT)) {
125 if (pmsg)
126 *pmsg = "export not set";
127 return 0;
128 }
129
130 /* Is there an RT list set? */
131 if (!bgp_vrf->vpn_policy[afi].rtlist[BGP_VPN_POLICY_DIR_TOVPN]) {
132 if (pmsg)
133 *pmsg = "rtlist tovpn not defined";
134 return 0;
135 }
136
137 /* Is there an RD set? */
138 if (!CHECK_FLAG(bgp_vrf->vpn_policy[afi].flags,
139 BGP_VPN_POLICY_TOVPN_RD_SET)) {
140 if (pmsg)
141 *pmsg = "rd not defined";
142 return 0;
143 }
144
145 /* Is a route-map specified, but not defined? */
146 if (bgp_vrf->vpn_policy[afi].rmap_name[BGP_VPN_POLICY_DIR_TOVPN] &&
147 !bgp_vrf->vpn_policy[afi].rmap[BGP_VPN_POLICY_DIR_TOVPN]) {
148 if (pmsg)
149 *pmsg = "route-map tovpn named but not defined";
150 return 0;
151 }
152
153 /* Is there an "auto" export label that isn't allocated yet? */
154 if (CHECK_FLAG(bgp_vrf->vpn_policy[afi].flags,
155 BGP_VPN_POLICY_TOVPN_LABEL_AUTO) &&
156 (bgp_vrf->vpn_policy[afi].tovpn_label == MPLS_LABEL_NONE)) {
157
158 if (pmsg)
159 *pmsg = "auto label not allocated";
160 return 0;
161 }
162
163 return 1;
164 }
165
166 static inline int vpn_leak_from_vpn_active(struct bgp *bgp_vrf, afi_t afi,
167 const char **pmsg)
168 {
169 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF
170 && bgp_vrf->inst_type != BGP_INSTANCE_TYPE_DEFAULT) {
171
172 if (pmsg)
173 *pmsg = "destination bgp instance neither vrf nor default";
174 return 0;
175 }
176
177 if (bgp_vrf->vrf_id == VRF_UNKNOWN) {
178 if (pmsg)
179 *pmsg = "destination bgp instance vrf is VRF_UNKNOWN";
180 return 0;
181 }
182
183 /* Is vrf configured to import from vpn? */
184 if (!CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
185 BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT)
186 && !CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
187 BGP_CONFIG_VRF_TO_VRF_IMPORT)) {
188 if (pmsg)
189 *pmsg = "import not set";
190 return 0;
191 }
192
193 /* Is there an RT list set? */
194 if (!bgp_vrf->vpn_policy[afi].rtlist[BGP_VPN_POLICY_DIR_FROMVPN]) {
195 if (pmsg)
196 *pmsg = "rtlist fromvpn not defined";
197 return 0;
198 }
199
200 /* Is a route-map specified, but not defined? */
201 if (bgp_vrf->vpn_policy[afi].rmap_name[BGP_VPN_POLICY_DIR_FROMVPN] &&
202 !bgp_vrf->vpn_policy[afi].rmap[BGP_VPN_POLICY_DIR_FROMVPN]) {
203 if (pmsg)
204 *pmsg = "route-map fromvpn named but not defined";
205 return 0;
206 }
207 return 1;
208 }
209
210 static inline void vpn_leak_prechange(enum vpn_policy_direction direction,
211 afi_t afi, struct bgp *bgp_vpn,
212 struct bgp *bgp_vrf)
213 {
214 /* Detect when default bgp instance is not (yet) defined by config */
215 if (!bgp_vpn)
216 return;
217
218 if ((direction == BGP_VPN_POLICY_DIR_FROMVPN) &&
219 vpn_leak_from_vpn_active(bgp_vrf, afi, NULL)) {
220
221 vpn_leak_to_vrf_withdraw_all(bgp_vrf, afi);
222 }
223 if ((direction == BGP_VPN_POLICY_DIR_TOVPN) &&
224 vpn_leak_to_vpn_active(bgp_vrf, afi, NULL)) {
225
226 vpn_leak_from_vrf_withdraw_all(bgp_vpn, bgp_vrf, afi);
227 }
228 }
229
230 static inline void vpn_leak_postchange(enum vpn_policy_direction direction,
231 afi_t afi, struct bgp *bgp_vpn,
232 struct bgp *bgp_vrf)
233 {
234 /* Detect when default bgp instance is not (yet) defined by config */
235 if (!bgp_vpn)
236 return;
237
238 if (direction == BGP_VPN_POLICY_DIR_FROMVPN) {
239 /* trigger a flush to re-sync with ADJ-RIB-in */
240 if (!CHECK_FLAG(bgp_vpn->af_flags[afi][SAFI_MPLS_VPN],
241 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL))
242 bgp_clear_soft_in(bgp_vpn, afi, SAFI_MPLS_VPN);
243 else
244 vpn_leak_to_vrf_update_all(bgp_vrf, bgp_vpn, afi);
245 }
246 if (direction == BGP_VPN_POLICY_DIR_TOVPN) {
247
248 if (bgp_vrf->vpn_policy[afi].tovpn_label !=
249 bgp_vrf->vpn_policy[afi]
250 .tovpn_zebra_vrf_label_last_sent) {
251 vpn_leak_zebra_vrf_label_update(bgp_vrf, afi);
252 }
253
254 if (!bgp_vrf->vpn_policy[afi].tovpn_sid)
255 ensure_vrf_tovpn_sid(bgp_vpn, bgp_vrf, afi);
256
257 if (!bgp_vrf->vpn_policy[afi].tovpn_sid
258 && bgp_vrf->vpn_policy[afi].tovpn_zebra_vrf_sid_last_sent)
259 vpn_leak_zebra_vrf_sid_withdraw(bgp_vrf, afi);
260
261 if (sid_diff(bgp_vrf->vpn_policy[afi].tovpn_sid,
262 bgp_vrf->vpn_policy[afi]
263 .tovpn_zebra_vrf_sid_last_sent)) {
264 vpn_leak_zebra_vrf_sid_update(bgp_vrf, afi);
265 }
266
267 vpn_leak_from_vrf_update_all(bgp_vpn, bgp_vrf, afi);
268 }
269 }
270
271 /* Flag if the route is injectable into VPN. This would be either a
272 * non-imported route or a non-VPN imported route.
273 */
274 static inline bool is_route_injectable_into_vpn(struct bgp_path_info *pi)
275 {
276 struct bgp_path_info *parent_pi;
277 struct bgp_table *table;
278 struct bgp_dest *dest;
279
280 if (pi->sub_type != BGP_ROUTE_IMPORTED ||
281 !pi->extra ||
282 !pi->extra->parent)
283 return true;
284
285 parent_pi = (struct bgp_path_info *)pi->extra->parent;
286 dest = parent_pi->net;
287 if (!dest)
288 return true;
289 table = bgp_dest_table(dest);
290 if (table &&
291 (table->afi == AFI_IP || table->afi == AFI_IP6) &&
292 table->safi == SAFI_MPLS_VPN)
293 return false;
294 return true;
295 }
296
297 /* Flag if the route path's family is VPN. */
298 static inline bool is_pi_family_vpn(struct bgp_path_info *pi)
299 {
300 return (is_pi_family_matching(pi, AFI_IP, SAFI_MPLS_VPN) ||
301 is_pi_family_matching(pi, AFI_IP6, SAFI_MPLS_VPN));
302 }
303
304 extern void vpn_policy_routemap_event(const char *rmap_name);
305
306 extern vrf_id_t get_first_vrf_for_redirect_with_rt(struct ecommunity *eckey);
307
308 extern void vpn_leak_postchange_all(void);
309 extern void vpn_handle_router_id_update(struct bgp *bgp, bool withdraw,
310 bool is_config);
311 extern void bgp_vpn_leak_unimport(struct bgp *from_bgp);
312 extern void bgp_vpn_leak_export(struct bgp *from_bgp);
313
314 #endif /* _QUAGGA_BGP_MPLSVPN_H */