]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_ecommunity.h
Merge pull request #9366 from idryzhov/fix-plist-yang
[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_DF_ELECTION 0x06
77 #define ECOMMUNITY_EVPN_SUBTYPE_DEF_GW 0x0d
78 #define ECOMMUNITY_EVPN_SUBTYPE_ND 0x08
79
80 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY 0x01
81
82 /* DF alg bits - only lower 5 bits are applicable */
83 #define ECOMMUNITY_EVPN_SUBTYPE_DF_ALG_BITS 0x1f
84
85 #define ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG 0x01
86 #define ECOMMUNITY_EVPN_SUBTYPE_ND_OVERRIDE_FLAG 0x02
87 #define ECOMMUNITY_EVPN_SUBTYPE_PROXY_FLAG 0x04
88
89 #define ECOMMUNITY_EVPN_SUBTYPE_ESI_SA_FLAG (1 << 0) /* single-active */
90
91 /* Low-order octet of the Extended Communities type field for OPAQUE types */
92 #define ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP 0x0c
93
94 /* Extended communities attribute string format. */
95 #define ECOMMUNITY_FORMAT_ROUTE_MAP 0
96 #define ECOMMUNITY_FORMAT_COMMUNITY_LIST 1
97 #define ECOMMUNITY_FORMAT_DISPLAY 2
98
99 /* Extended Communities value is eight octet long. */
100 #define ECOMMUNITY_SIZE 8
101 #define IPV6_ECOMMUNITY_SIZE 20
102
103 /* Extended Communities type flag. */
104 #define ECOMMUNITY_FLAG_NON_TRANSITIVE 0x40
105
106 /* Extended Community readable string length */
107 #define ECOMMUNITY_STRLEN 64
108
109 /* Extended Communities attribute. */
110 struct ecommunity {
111 /* Reference counter. */
112 unsigned long refcnt;
113
114 /* Size of Each Unit of Extended Communities attribute.
115 * to differentiate between IPv6 ext comm and ext comm
116 */
117 uint8_t unit_size;
118
119 /* Size of Extended Communities attribute. */
120 uint32_t size;
121
122 /* Extended Communities value. */
123 uint8_t *val;
124
125 /* Human readable format string. */
126 char *str;
127 };
128
129 struct ecommunity_as {
130 as_t as;
131 uint32_t val;
132 };
133
134 struct ecommunity_ip {
135 struct in_addr ip;
136 uint16_t val;
137 };
138
139 struct ecommunity_ip6 {
140 struct in6_addr ip;
141 uint16_t val;
142 };
143
144 /* Extended community value is eight octet. */
145 struct ecommunity_val {
146 char val[ECOMMUNITY_SIZE];
147 };
148
149 /* IPv6 Extended community value is eight octet. */
150 struct ecommunity_val_ipv6 {
151 char val[IPV6_ECOMMUNITY_SIZE];
152 };
153
154 enum ecommunity_lb_type {
155 EXPLICIT_BANDWIDTH,
156 CUMULATIVE_BANDWIDTH,
157 COMPUTED_BANDWIDTH
158 };
159
160 #define ecom_length_size(X, Y) ((X)->size * (Y))
161
162 /*
163 * Encode BGP Route Target AS:nn.
164 */
165 static inline void encode_route_target_as(as_t as, uint32_t val,
166 struct ecommunity_val *eval)
167 {
168 eval->val[0] = ECOMMUNITY_ENCODE_AS;
169 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
170 eval->val[2] = (as >> 8) & 0xff;
171 eval->val[3] = as & 0xff;
172 eval->val[4] = (val >> 24) & 0xff;
173 eval->val[5] = (val >> 16) & 0xff;
174 eval->val[6] = (val >> 8) & 0xff;
175 eval->val[7] = val & 0xff;
176 }
177
178 /*
179 * Encode BGP Route Target IP:nn.
180 */
181 static inline void encode_route_target_ip(struct in_addr ip, uint16_t val,
182 struct ecommunity_val *eval)
183 {
184 eval->val[0] = ECOMMUNITY_ENCODE_IP;
185 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
186 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
187 eval->val[6] = (val >> 8) & 0xff;
188 eval->val[7] = val & 0xff;
189 }
190
191 /*
192 * Encode BGP Route Target AS4:nn.
193 */
194 static inline void encode_route_target_as4(as_t as, uint16_t val,
195 struct ecommunity_val *eval)
196 {
197 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
198 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
199 eval->val[2] = (as >> 24) & 0xff;
200 eval->val[3] = (as >> 16) & 0xff;
201 eval->val[4] = (as >> 8) & 0xff;
202 eval->val[5] = as & 0xff;
203 eval->val[6] = (val >> 8) & 0xff;
204 eval->val[7] = val & 0xff;
205 }
206
207 /*
208 * Encode BGP Link Bandwidth extended community
209 * bandwidth (bw) is in bytes-per-sec
210 */
211 static inline void encode_lb_extcomm(as_t as, uint32_t bw, bool non_trans,
212 struct ecommunity_val *eval)
213 {
214 memset(eval, 0, sizeof(*eval));
215 eval->val[0] = ECOMMUNITY_ENCODE_AS;
216 if (non_trans)
217 eval->val[0] |= ECOMMUNITY_FLAG_NON_TRANSITIVE;
218 eval->val[1] = ECOMMUNITY_LINK_BANDWIDTH;
219 eval->val[2] = (as >> 8) & 0xff;
220 eval->val[3] = as & 0xff;
221 eval->val[4] = (bw >> 24) & 0xff;
222 eval->val[5] = (bw >> 16) & 0xff;
223 eval->val[6] = (bw >> 8) & 0xff;
224 eval->val[7] = bw & 0xff;
225 }
226
227 extern void ecommunity_init(void);
228 extern void ecommunity_finish(void);
229 extern void ecommunity_free(struct ecommunity **);
230 extern struct ecommunity *ecommunity_parse(uint8_t *, unsigned short);
231 extern struct ecommunity *ecommunity_parse_ipv6(uint8_t *pnt,
232 unsigned short length);
233 extern struct ecommunity *ecommunity_dup(struct ecommunity *);
234 extern struct ecommunity *ecommunity_merge(struct ecommunity *,
235 struct ecommunity *);
236 extern struct ecommunity *ecommunity_uniq_sort(struct ecommunity *);
237 extern struct ecommunity *ecommunity_intern(struct ecommunity *);
238 extern bool ecommunity_cmp(const void *arg1, const void *arg2);
239 extern void ecommunity_unintern(struct ecommunity **ecommunity);
240 extern unsigned int ecommunity_hash_make(const void *);
241 extern struct ecommunity *ecommunity_str2com(const char *, int, int);
242 extern struct ecommunity *ecommunity_str2com_ipv6(const char *str, int type,
243 int keyword_included);
244 extern char *ecommunity_ecom2str(struct ecommunity *, int, int);
245 extern void ecommunity_strfree(char **s);
246 extern bool ecommunity_match(const struct ecommunity *,
247 const struct ecommunity *);
248 extern char *ecommunity_str(struct ecommunity *);
249 extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *,
250 uint8_t, uint8_t);
251
252 extern bool ecommunity_add_val(struct ecommunity *ecom,
253 struct ecommunity_val *eval,
254 bool unique, bool overwrite);
255 extern bool ecommunity_add_val_ipv6(struct ecommunity *ecom,
256 struct ecommunity_val_ipv6 *eval,
257 bool unique, bool overwrite);
258
259 /* for vpn */
260 extern struct ecommunity *ecommunity_new(void);
261 extern bool ecommunity_strip(struct ecommunity *ecom, uint8_t type,
262 uint8_t subtype);
263 extern struct ecommunity *ecommunity_new(void);
264 extern bool ecommunity_del_val(struct ecommunity *ecom,
265 struct ecommunity_val *eval);
266 struct bgp_pbr_entry_action;
267 extern int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
268 struct bgp_pbr_entry_action *api,
269 afi_t afi);
270
271 extern void bgp_compute_aggregate_ecommunity(
272 struct bgp_aggregate *aggregate,
273 struct ecommunity *ecommunity);
274
275 extern void bgp_compute_aggregate_ecommunity_hash(
276 struct bgp_aggregate *aggregate,
277 struct ecommunity *ecommunity);
278 extern void bgp_compute_aggregate_ecommunity_val(
279 struct bgp_aggregate *aggregate);
280 extern void bgp_remove_ecommunity_from_aggregate(
281 struct bgp_aggregate *aggregate,
282 struct ecommunity *ecommunity);
283 extern void bgp_remove_ecomm_from_aggregate_hash(
284 struct bgp_aggregate *aggregate,
285 struct ecommunity *ecommunity);
286 extern void bgp_aggr_ecommunity_remove(void *arg);
287 extern const uint8_t *ecommunity_linkbw_present(struct ecommunity *ecom,
288 uint32_t *bw);
289 extern struct ecommunity *ecommunity_replace_linkbw(as_t as,
290 struct ecommunity *ecom, uint64_t cum_bw);
291
292 static inline void ecommunity_strip_rts(struct ecommunity *ecom)
293 {
294 uint8_t subtype = ECOMMUNITY_ROUTE_TARGET;
295
296 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS, subtype);
297 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_IP, subtype);
298 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS4, subtype);
299 }
300 #endif /* _QUAGGA_BGP_ECOMMUNITY_H */