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