]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_ecommunity.h
Merge pull request #5895 from patrasar/2404618
[mirror_frr.git] / bgpd / bgp_ecommunity.h
1 /* BGP Extended Communities Attribute.
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
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 #ifndef _QUAGGA_BGP_ECOMMUNITY_H
22 #define _QUAGGA_BGP_ECOMMUNITY_H
23
24 #include "bgpd/bgp_route.h"
25 #include "bgpd/bgpd.h"
26
27 /* Refer to rfc7153 for the IANA registry definitions. These are
28 * updated by other standards like rfc7674.
29 */
30 /* High-order octet of the Extended Communities type field. */
31 #define ECOMMUNITY_ENCODE_AS 0x00
32 #define ECOMMUNITY_ENCODE_IP 0x01
33 #define ECOMMUNITY_ENCODE_AS4 0x02
34 #define ECOMMUNITY_ENCODE_OPAQUE 0x03
35 #define ECOMMUNITY_ENCODE_EVPN 0x06
36 #define ECOMMUNITY_ENCODE_REDIRECT_IP_NH 0x08 /* Flow Spec */
37 /* Generic Transitive Experimental */
38 #define ECOMMUNITY_ENCODE_TRANS_EXP 0x80
39
40 /* RFC7674 */
41 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 0x81
42 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_3 0x82
43
44 /* Non-transitive extended community types. */
45 #define ECOMMUNITY_ENCODE_AS_NON_TRANS 0x40
46 #define ECOMMUNITY_ENCODE_IP_NON_TRANS 0x41
47 #define ECOMMUNITY_ENCODE_AS4_NON_TRANS 0x42
48 #define ECOMMUNITY_ENCODE_OPAQUE_NON_TRANS 0x43
49
50 /* Low-order octet of the Extended Communities type field. */
51 /* Note: This really depends on the high-order octet. This means that
52 * multiple definitions for the same value are possible.
53 */
54 #define ECOMMUNITY_ROUTE_TARGET 0x02
55 #define ECOMMUNITY_SITE_ORIGIN 0x03
56 #define ECOMMUNITY_LINK_BANDWIDTH 0x04
57 #define ECOMMUNITY_TRAFFIC_RATE 0x06 /* Flow Spec */
58 #define ECOMMUNITY_TRAFFIC_ACTION 0x07
59 #define ECOMMUNITY_REDIRECT_VRF 0x08
60 #define ECOMMUNITY_TRAFFIC_MARKING 0x09
61 #define ECOMMUNITY_REDIRECT_IP_NH 0x00
62 /* from IANA: bgp-extended-communities/bgp-extended-communities.xhtml
63 * 0x0c Flow-spec Redirect to IPv4 - draft-ietf-idr-flowspec-redirect
64 */
65 #define ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4 0x0c
66 /* from draft-ietf-idr-flow-spec-v6-09
67 * 0x0b Flow-spec Redirect to IPv6
68 */
69 #define ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6 0x0b
70
71 /* Low-order octet of the Extended Communities type field for EVPN types */
72 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY 0x00
73 #define ECOMMUNITY_EVPN_SUBTYPE_ESI_LABEL 0x01
74 #define ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT 0x02
75 #define ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC 0x03
76 #define ECOMMUNITY_EVPN_SUBTYPE_DEF_GW 0x0d
77 #define ECOMMUNITY_EVPN_SUBTYPE_ND 0x08
78
79 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY 0x01
80
81 #define ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG 0x01
82 #define ECOMMUNITY_EVPN_SUBTYPE_ND_OVERRIDE_FLAG 0x02
83 #define ECOMMUNITY_EVPN_SUBTYPE_PROXY_FLAG 0x04
84
85 #define ECOMMUNITY_EVPN_SUBTYPE_ESI_SA_FLAG (1 << 0) /* single-active */
86
87 /* Low-order octet of the Extended Communities type field for OPAQUE types */
88 #define ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP 0x0c
89
90 /* Extended communities attribute string format. */
91 #define ECOMMUNITY_FORMAT_ROUTE_MAP 0
92 #define ECOMMUNITY_FORMAT_COMMUNITY_LIST 1
93 #define ECOMMUNITY_FORMAT_DISPLAY 2
94
95 /* Extended Communities value is eight octet long. */
96 #define ECOMMUNITY_SIZE 8
97 #define IPV6_ECOMMUNITY_SIZE 20
98
99 /* Extended Communities type flag. */
100 #define ECOMMUNITY_FLAG_NON_TRANSITIVE 0x40
101
102 /* Extended Communities attribute. */
103 struct ecommunity {
104 /* Reference counter. */
105 unsigned long refcnt;
106
107 /* Size of Each Unit of Extended Communities attribute.
108 * to differentiate between IPv6 ext comm and ext comm
109 */
110 uint8_t unit_size;
111
112 /* Size of Extended Communities attribute. */
113 int size;
114
115 /* Extended Communities value. */
116 uint8_t *val;
117
118 /* Human readable format string. */
119 char *str;
120 };
121
122 struct ecommunity_as {
123 as_t as;
124 uint32_t val;
125 };
126
127 struct ecommunity_ip {
128 struct in_addr ip;
129 uint16_t val;
130 };
131
132 struct ecommunity_ip6 {
133 struct in6_addr ip;
134 uint16_t val;
135 };
136
137 /* Extended community value is eight octet. */
138 struct ecommunity_val {
139 char val[ECOMMUNITY_SIZE];
140 };
141
142 /* IPv6 Extended community value is eight octet. */
143 struct ecommunity_val_ipv6 {
144 char val[IPV6_ECOMMUNITY_SIZE];
145 };
146
147 #define ecom_length_size(X, Y) ((X)->size * (Y))
148
149 /*
150 * Encode BGP Route Target AS:nn.
151 */
152 static inline void encode_route_target_as(as_t as, uint32_t val,
153 struct ecommunity_val *eval)
154 {
155 eval->val[0] = ECOMMUNITY_ENCODE_AS;
156 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
157 eval->val[2] = (as >> 8) & 0xff;
158 eval->val[3] = as & 0xff;
159 eval->val[4] = (val >> 24) & 0xff;
160 eval->val[5] = (val >> 16) & 0xff;
161 eval->val[6] = (val >> 8) & 0xff;
162 eval->val[7] = val & 0xff;
163 }
164
165 /*
166 * Encode BGP Route Target IP:nn.
167 */
168 static inline void encode_route_target_ip(struct in_addr ip, uint16_t val,
169 struct ecommunity_val *eval)
170 {
171 eval->val[0] = ECOMMUNITY_ENCODE_IP;
172 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
173 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
174 eval->val[6] = (val >> 8) & 0xff;
175 eval->val[7] = val & 0xff;
176 }
177
178 /*
179 * Encode BGP Route Target AS4:nn.
180 */
181 static inline void encode_route_target_as4(as_t as, uint16_t val,
182 struct ecommunity_val *eval)
183 {
184 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
185 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
186 eval->val[2] = (as >> 24) & 0xff;
187 eval->val[3] = (as >> 16) & 0xff;
188 eval->val[4] = (as >> 8) & 0xff;
189 eval->val[5] = as & 0xff;
190 eval->val[6] = (val >> 8) & 0xff;
191 eval->val[7] = val & 0xff;
192 }
193
194 /*
195 * Encode BGP Link Bandwidth extended community
196 * bandwidth (bw) is in bytes-per-sec
197 */
198 static inline void encode_lb_extcomm(as_t as, uint32_t bw, bool non_trans,
199 struct ecommunity_val *eval)
200 {
201 memset(eval, 0, sizeof(*eval));
202 eval->val[0] = ECOMMUNITY_ENCODE_AS;
203 if (non_trans)
204 eval->val[0] |= ECOMMUNITY_FLAG_NON_TRANSITIVE;
205 eval->val[1] = ECOMMUNITY_LINK_BANDWIDTH;
206 eval->val[2] = (as >> 8) & 0xff;
207 eval->val[3] = as & 0xff;
208 eval->val[4] = (bw >> 24) & 0xff;
209 eval->val[5] = (bw >> 16) & 0xff;
210 eval->val[6] = (bw >> 8) & 0xff;
211 eval->val[7] = bw & 0xff;
212 }
213
214 extern void ecommunity_init(void);
215 extern void ecommunity_finish(void);
216 extern void ecommunity_free(struct ecommunity **);
217 extern struct ecommunity *ecommunity_parse(uint8_t *, unsigned short);
218 extern struct ecommunity *ecommunity_parse_ipv6(uint8_t *pnt,
219 unsigned short length);
220 extern struct ecommunity *ecommunity_dup(struct ecommunity *);
221 extern struct ecommunity *ecommunity_merge(struct ecommunity *,
222 struct ecommunity *);
223 extern struct ecommunity *ecommunity_uniq_sort(struct ecommunity *);
224 extern struct ecommunity *ecommunity_intern(struct ecommunity *);
225 extern bool ecommunity_cmp(const void *arg1, const void *arg2);
226 extern void ecommunity_unintern(struct ecommunity **);
227 extern unsigned int ecommunity_hash_make(const void *);
228 extern struct ecommunity *ecommunity_str2com(const char *, int, int);
229 extern struct ecommunity *ecommunity_str2com_ipv6(const char *str, int type,
230 int keyword_included);
231 extern char *ecommunity_ecom2str(struct ecommunity *, int, int);
232 extern void ecommunity_strfree(char **s);
233 extern bool ecommunity_match(const struct ecommunity *,
234 const struct ecommunity *);
235 extern char *ecommunity_str(struct ecommunity *);
236 extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *,
237 uint8_t, uint8_t);
238
239 extern bool ecommunity_add_val(struct ecommunity *ecom,
240 struct ecommunity_val *eval,
241 bool unique, bool overwrite);
242 extern bool ecommunity_add_val_ipv6(struct ecommunity *ecom,
243 struct ecommunity_val_ipv6 *eval,
244 bool unique, bool overwrite);
245
246 /* for vpn */
247 extern struct ecommunity *ecommunity_new(void);
248 extern bool ecommunity_strip(struct ecommunity *ecom, uint8_t type,
249 uint8_t subtype);
250 extern struct ecommunity *ecommunity_new(void);
251 extern bool ecommunity_del_val(struct ecommunity *ecom,
252 struct ecommunity_val *eval);
253 struct bgp_pbr_entry_action;
254 extern int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
255 struct bgp_pbr_entry_action *api,
256 afi_t afi);
257
258 extern void bgp_compute_aggregate_ecommunity(
259 struct bgp_aggregate *aggregate,
260 struct ecommunity *ecommunity);
261
262 extern void bgp_compute_aggregate_ecommunity_hash(
263 struct bgp_aggregate *aggregate,
264 struct ecommunity *ecommunity);
265 extern void bgp_compute_aggregate_ecommunity_val(
266 struct bgp_aggregate *aggregate);
267 extern void bgp_remove_ecommunity_from_aggregate(
268 struct bgp_aggregate *aggregate,
269 struct ecommunity *ecommunity);
270 extern void bgp_remove_ecomm_from_aggregate_hash(
271 struct bgp_aggregate *aggregate,
272 struct ecommunity *ecommunity);
273 extern void bgp_aggr_ecommunity_remove(void *arg);
274 extern const uint8_t *ecommunity_linkbw_present(struct ecommunity *ecom,
275 uint32_t *bw);
276 extern struct ecommunity *ecommunity_replace_linkbw(as_t as,
277 struct ecommunity *ecom, uint64_t cum_bw);
278
279 static inline void ecommunity_strip_rts(struct ecommunity *ecom)
280 {
281 uint8_t subtype = ECOMMUNITY_ROUTE_TARGET;
282
283 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS, subtype);
284 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_IP, subtype);
285 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS4, subtype);
286 }
287 #endif /* _QUAGGA_BGP_ECOMMUNITY_H */