]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_mplsvpn.h
bgpd: fix the IGP metric for best path selection on VPN import
[mirror_frr.git] / bgpd / bgp_mplsvpn.h
CommitLineData
718e3744 1/* MPLS-VPN
896014f4
DL
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
8557760c 4 * This file is part of GxNU Zebra.
896014f4
DL
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
00d252cb 21#ifndef _QUAGGA_BGP_MPLSVPN_H
22#define _QUAGGA_BGP_MPLSVPN_H
23
ea7cd161 24#include "bgpd/bgp_attr.h"
4f280b15 25#include "bgpd/bgp_route.h"
8557760c 26#include "bgpd/bgp_rd.h"
ddb5b488 27#include "bgpd/bgp_zebra.h"
a486300b 28#include "bgpd/bgp_vty.h"
718e3744 29
d62a17ae 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)
65efcfce 35
00e6edb9 36#define BGP_VPNVX_HELP_STR BGP_AF_STR BGP_AF_STR
d6902373 37
d62a17ae 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"
96ade3ed 43
c4ab9fd9
RS
44#define BGP_PREFIX_SID_SRV6_MAX_FUNCTION_LENGTH 20
45
d62a17ae 46extern void bgp_mplsvpn_init(void);
47extern int bgp_nlri_parse_vpn(struct peer *, struct attr *, struct bgp_nlri *);
d7c0a89a 48extern uint32_t decode_label(mpls_label_t *);
9bedbb1e 49extern void encode_label(mpls_label_t, mpls_label_t *);
00d252cb 50
d62a17ae 51extern int argv_find_and_parse_vpnvx(struct cmd_token **argv, int argc,
52 int *index, afi_t *afi);
53extern int bgp_show_mpls_vpn(struct vty *vty, afi_t afi, struct prefix_rd *prd,
54 enum bgp_show_type type, void *output_arg,
9f049418 55 int tags, bool use_json);
d6902373 56
02212dee 57extern void vpn_leak_from_vrf_update(struct bgp *to_bgp, struct bgp *from_bgp,
40381db7 58 struct bgp_path_info *path_vrf);
ddb5b488 59
02212dee 60extern void vpn_leak_from_vrf_withdraw(struct bgp *to_bgp, struct bgp *from_bgp,
40381db7 61 struct bgp_path_info *path_vrf);
ddb5b488 62
02212dee
LS
63extern void vpn_leak_from_vrf_withdraw_all(struct bgp *to_bgp,
64 struct bgp *from_bgp, afi_t afi);
ddb5b488 65
02212dee
LS
66extern void vpn_leak_from_vrf_update_all(struct bgp *to_bgp,
67 struct bgp *from_bgp, afi_t afi);
ddb5b488 68
02212dee 69extern void vpn_leak_to_vrf_withdraw_all(struct bgp *to_bgp, afi_t afi);
ddb5b488 70
02212dee 71extern void vpn_leak_to_vrf_update_all(struct bgp *to_bgp, struct bgp *from_bgp,
ddb5b488
PZ
72 afi_t afi);
73
a486300b 74extern bool vpn_leak_to_vrf_update(struct bgp *from_bgp,
46dbf9d0
DA
75 struct bgp_path_info *path_vpn,
76 struct prefix_rd *prd);
ddb5b488 77
02212dee 78extern void vpn_leak_to_vrf_withdraw(struct bgp *from_bgp,
40381db7 79 struct bgp_path_info *path_vpn);
ddb5b488
PZ
80
81extern void vpn_leak_zebra_vrf_label_update(struct bgp *bgp, afi_t afi);
82extern void vpn_leak_zebra_vrf_label_withdraw(struct bgp *bgp, afi_t afi);
b72c9e14 83extern void vpn_leak_zebra_vrf_sid_update(struct bgp *bgp, afi_t afi);
527588aa
CS
84extern void vpn_leak_zebra_vrf_sid_update_per_af(struct bgp *bgp, afi_t afi);
85extern void vpn_leak_zebra_vrf_sid_update_per_vrf(struct bgp *bgp);
b72c9e14 86extern void vpn_leak_zebra_vrf_sid_withdraw(struct bgp *bgp, afi_t afi);
527588aa
CS
87extern void vpn_leak_zebra_vrf_sid_withdraw_per_af(struct bgp *bgp, afi_t afi);
88extern void vpn_leak_zebra_vrf_sid_withdraw_per_vrf(struct bgp *bgp);
e70e9f8e 89extern int vpn_leak_label_callback(mpls_label_t label, void *lblid, bool alloc);
b72c9e14 90extern void ensure_vrf_tovpn_sid(struct bgp *vpn, struct bgp *vrf, afi_t afi);
e606d8ec
CS
91extern void delete_vrf_tovpn_sid(struct bgp *vpn, struct bgp *vrf, afi_t afi);
92extern void delete_vrf_tovpn_sid_per_af(struct bgp *vpn, struct bgp *vrf,
93 afi_t afi);
94extern void delete_vrf_tovpn_sid_per_vrf(struct bgp *vpn, struct bgp *vrf);
527588aa
CS
95extern void ensure_vrf_tovpn_sid_per_af(struct bgp *vpn, struct bgp *vrf,
96 afi_t afi);
97extern void ensure_vrf_tovpn_sid_per_vrf(struct bgp *vpn, struct bgp *vrf);
a45cd34e
RS
98extern void transpose_sid(struct in6_addr *sid, uint32_t label, uint8_t offset,
99 uint8_t size);
1d4e8b0d 100extern void vrf_import_from_vrf(struct bgp *to_bgp, struct bgp *from_bgp,
44338987 101 afi_t afi, safi_t safi);
1d4e8b0d 102void vrf_unimport_from_vrf(struct bgp *to_bgp, struct bgp *from_bgp,
44338987 103 afi_t afi, safi_t safi);
ddb5b488 104
2fceba1f
PR
105static inline bool is_bgp_vrf_mplsvpn(struct bgp *bgp)
106{
107 afi_t afi;
108
109 if (bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
110 for (afi = 0; afi < AFI_MAX; ++afi) {
111 if (CHECK_FLAG(bgp->af_flags[afi][SAFI_UNICAST],
112 BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT)
113 || CHECK_FLAG(bgp->af_flags[afi][SAFI_UNICAST],
114 BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT))
115 return true;
116 }
117 return false;
118}
119
ddb5b488
PZ
120static inline int vpn_leak_to_vpn_active(struct bgp *bgp_vrf, afi_t afi,
121 const char **pmsg)
122{
d555f3e9
PZ
123 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF
124 && bgp_vrf->inst_type != BGP_INSTANCE_TYPE_DEFAULT) {
125
126 if (pmsg)
127 *pmsg = "source bgp instance neither vrf nor default";
128 return 0;
129 }
130
ddb5b488
PZ
131 /* Is vrf configured to export to vpn? */
132 if (!CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
12a844a5
DS
133 BGP_CONFIG_VRF_TO_MPLSVPN_EXPORT)
134 && !CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
135 BGP_CONFIG_VRF_TO_VRF_EXPORT)) {
ddb5b488
PZ
136 if (pmsg)
137 *pmsg = "export not set";
138 return 0;
139 }
140
141 /* Is there an RT list set? */
142 if (!bgp_vrf->vpn_policy[afi].rtlist[BGP_VPN_POLICY_DIR_TOVPN]) {
143 if (pmsg)
144 *pmsg = "rtlist tovpn not defined";
145 return 0;
146 }
147
148 /* Is there an RD set? */
149 if (!CHECK_FLAG(bgp_vrf->vpn_policy[afi].flags,
150 BGP_VPN_POLICY_TOVPN_RD_SET)) {
151 if (pmsg)
152 *pmsg = "rd not defined";
153 return 0;
154 }
e70e9f8e 155
d92a55df
PZ
156 /* Is a route-map specified, but not defined? */
157 if (bgp_vrf->vpn_policy[afi].rmap_name[BGP_VPN_POLICY_DIR_TOVPN] &&
158 !bgp_vrf->vpn_policy[afi].rmap[BGP_VPN_POLICY_DIR_TOVPN]) {
159 if (pmsg)
160 *pmsg = "route-map tovpn named but not defined";
161 return 0;
162 }
163
e70e9f8e
PZ
164 /* Is there an "auto" export label that isn't allocated yet? */
165 if (CHECK_FLAG(bgp_vrf->vpn_policy[afi].flags,
166 BGP_VPN_POLICY_TOVPN_LABEL_AUTO) &&
167 (bgp_vrf->vpn_policy[afi].tovpn_label == MPLS_LABEL_NONE)) {
168
169 if (pmsg)
170 *pmsg = "auto label not allocated";
171 return 0;
172 }
173
ddb5b488
PZ
174 return 1;
175}
176
177static inline int vpn_leak_from_vpn_active(struct bgp *bgp_vrf, afi_t afi,
b9c7bc5a 178 const char **pmsg)
ddb5b488 179{
ddb5b488 180 if (bgp_vrf->inst_type != BGP_INSTANCE_TYPE_VRF
d555f3e9 181 && bgp_vrf->inst_type != BGP_INSTANCE_TYPE_DEFAULT) {
ddb5b488
PZ
182
183 if (pmsg)
184 *pmsg = "destination bgp instance neither vrf nor default";
185 return 0;
186 }
187
e504cf3b
DS
188 if (bgp_vrf->vrf_id == VRF_UNKNOWN) {
189 if (pmsg)
190 *pmsg = "destination bgp instance vrf is VRF_UNKNOWN";
191 return 0;
192 }
193
b9c7bc5a
PZ
194 /* Is vrf configured to import from vpn? */
195 if (!CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
12a844a5
DS
196 BGP_CONFIG_MPLSVPN_TO_VRF_IMPORT)
197 && !CHECK_FLAG(bgp_vrf->af_flags[afi][SAFI_UNICAST],
198 BGP_CONFIG_VRF_TO_VRF_IMPORT)) {
ddb5b488 199 if (pmsg)
b9c7bc5a 200 *pmsg = "import not set";
ddb5b488
PZ
201 return 0;
202 }
d92a55df
PZ
203
204 /* Is there an RT list set? */
ddb5b488
PZ
205 if (!bgp_vrf->vpn_policy[afi].rtlist[BGP_VPN_POLICY_DIR_FROMVPN]) {
206 if (pmsg)
207 *pmsg = "rtlist fromvpn not defined";
208 return 0;
209 }
d92a55df
PZ
210
211 /* Is a route-map specified, but not defined? */
212 if (bgp_vrf->vpn_policy[afi].rmap_name[BGP_VPN_POLICY_DIR_FROMVPN] &&
213 !bgp_vrf->vpn_policy[afi].rmap[BGP_VPN_POLICY_DIR_FROMVPN]) {
214 if (pmsg)
215 *pmsg = "route-map fromvpn named but not defined";
216 return 0;
217 }
ddb5b488
PZ
218 return 1;
219}
220
9c2fd3fe 221static inline void vpn_leak_prechange(enum vpn_policy_direction direction,
ddb5b488
PZ
222 afi_t afi, struct bgp *bgp_vpn,
223 struct bgp *bgp_vrf)
224{
3bd70bf8
PZ
225 /* Detect when default bgp instance is not (yet) defined by config */
226 if (!bgp_vpn)
227 return;
228
d555f3e9
PZ
229 if ((direction == BGP_VPN_POLICY_DIR_FROMVPN) &&
230 vpn_leak_from_vpn_active(bgp_vrf, afi, NULL)) {
231
ddb5b488 232 vpn_leak_to_vrf_withdraw_all(bgp_vrf, afi);
d555f3e9
PZ
233 }
234 if ((direction == BGP_VPN_POLICY_DIR_TOVPN) &&
235 vpn_leak_to_vpn_active(bgp_vrf, afi, NULL)) {
236
ddb5b488 237 vpn_leak_from_vrf_withdraw_all(bgp_vpn, bgp_vrf, afi);
d555f3e9 238 }
ddb5b488
PZ
239}
240
9c2fd3fe 241static inline void vpn_leak_postchange(enum vpn_policy_direction direction,
ddb5b488
PZ
242 afi_t afi, struct bgp *bgp_vpn,
243 struct bgp *bgp_vrf)
244{
3bd70bf8
PZ
245 /* Detect when default bgp instance is not (yet) defined by config */
246 if (!bgp_vpn)
247 return;
248
a486300b
PG
249 if (direction == BGP_VPN_POLICY_DIR_FROMVPN) {
250 /* trigger a flush to re-sync with ADJ-RIB-in */
251 if (!CHECK_FLAG(bgp_vpn->af_flags[afi][SAFI_MPLS_VPN],
252 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL))
253 bgp_clear_soft_in(bgp_vpn, afi, SAFI_MPLS_VPN);
254 else
255 vpn_leak_to_vrf_update_all(bgp_vrf, bgp_vpn, afi);
256 }
ddb5b488
PZ
257 if (direction == BGP_VPN_POLICY_DIR_TOVPN) {
258
d555f3e9
PZ
259 if (bgp_vrf->vpn_policy[afi].tovpn_label !=
260 bgp_vrf->vpn_policy[afi]
ddb5b488
PZ
261 .tovpn_zebra_vrf_label_last_sent) {
262 vpn_leak_zebra_vrf_label_update(bgp_vrf, afi);
263 }
264
e606d8ec
CS
265 if (bgp_vrf->vpn_policy[afi].tovpn_sid_index == 0 &&
266 !CHECK_FLAG(bgp_vrf->vpn_policy[afi].flags,
267 BGP_VPN_POLICY_TOVPN_SID_AUTO) &&
268 bgp_vrf->tovpn_sid_index == 0 &&
269 !CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_TOVPN_SID_AUTO))
270 delete_vrf_tovpn_sid(bgp_vpn, bgp_vrf, afi);
271
527588aa 272 if (!bgp_vrf->vpn_policy[afi].tovpn_sid && !bgp_vrf->tovpn_sid)
b72c9e14
HS
273 ensure_vrf_tovpn_sid(bgp_vpn, bgp_vrf, afi);
274
527588aa
CS
275 if ((!bgp_vrf->vpn_policy[afi].tovpn_sid &&
276 bgp_vrf->vpn_policy[afi].tovpn_zebra_vrf_sid_last_sent) ||
277 (!bgp_vrf->tovpn_sid &&
278 bgp_vrf->tovpn_zebra_vrf_sid_last_sent))
d79ff732
HS
279 vpn_leak_zebra_vrf_sid_withdraw(bgp_vrf, afi);
280
527588aa
CS
281 if (bgp_vrf->vpn_policy[afi].tovpn_sid) {
282 if (sid_diff(bgp_vrf->vpn_policy[afi].tovpn_sid,
283 bgp_vrf->vpn_policy[afi]
284 .tovpn_zebra_vrf_sid_last_sent)) {
285 vpn_leak_zebra_vrf_sid_update(bgp_vrf, afi);
286 }
287 } else if (bgp_vrf->tovpn_sid) {
288 if (sid_diff(bgp_vrf->tovpn_sid,
289 bgp_vrf->tovpn_zebra_vrf_sid_last_sent)) {
290 vpn_leak_zebra_vrf_sid_update(bgp_vrf, afi);
291 }
b72c9e14
HS
292 }
293
ddb5b488
PZ
294 vpn_leak_from_vrf_update_all(bgp_vpn, bgp_vrf, afi);
295 }
ddb5b488
PZ
296}
297
12d6100c 298/* Flag if the route is injectable into VPN. This would be either a
299 * non-imported route or a non-VPN imported route.
300 */
301static inline bool is_route_injectable_into_vpn(struct bgp_path_info *pi)
302{
303 struct bgp_path_info *parent_pi;
304 struct bgp_table *table;
9bcb3eef 305 struct bgp_dest *dest;
12d6100c 306
307 if (pi->sub_type != BGP_ROUTE_IMPORTED ||
308 !pi->extra ||
309 !pi->extra->parent)
310 return true;
311
312 parent_pi = (struct bgp_path_info *)pi->extra->parent;
9bcb3eef
DS
313 dest = parent_pi->net;
314 if (!dest)
12d6100c 315 return true;
9bcb3eef 316 table = bgp_dest_table(dest);
12d6100c 317 if (table &&
318 (table->afi == AFI_IP || table->afi == AFI_IP6) &&
319 table->safi == SAFI_MPLS_VPN)
320 return false;
321 return true;
322}
323
9544ddb2 324/* Flag if the route path's family is VPN. */
325static inline bool is_pi_family_vpn(struct bgp_path_info *pi)
326{
327 return (is_pi_family_matching(pi, AFI_IP, SAFI_MPLS_VPN) ||
328 is_pi_family_matching(pi, AFI_IP6, SAFI_MPLS_VPN));
329}
330
ddb5b488
PZ
331extern void vpn_policy_routemap_event(const char *rmap_name);
332
301ad80a
PG
333extern vrf_id_t get_first_vrf_for_redirect_with_rt(struct ecommunity *eckey);
334
3bd70bf8 335extern void vpn_leak_postchange_all(void);
e65fe398
MS
336extern void vpn_handle_router_id_update(struct bgp *bgp, bool withdraw,
337 bool is_config);
ff8a8a7a 338extern void bgp_vpn_leak_unimport(struct bgp *from_bgp);
48381346 339extern void bgp_vpn_leak_export(struct bgp *from_bgp);
3bd70bf8 340
00d252cb 341#endif /* _QUAGGA_BGP_MPLSVPN_H */