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