]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_ecommunity.h
Merge pull request #8044 from SaiGomathiN/nsg
[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 #define ecom_length_size(X, Y) ((X)->size * (Y))
155
156 /*
157 * Encode BGP Route Target AS:nn.
158 */
159 static inline void encode_route_target_as(as_t as, uint32_t val,
160 struct ecommunity_val *eval)
161 {
162 eval->val[0] = ECOMMUNITY_ENCODE_AS;
163 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
164 eval->val[2] = (as >> 8) & 0xff;
165 eval->val[3] = as & 0xff;
166 eval->val[4] = (val >> 24) & 0xff;
167 eval->val[5] = (val >> 16) & 0xff;
168 eval->val[6] = (val >> 8) & 0xff;
169 eval->val[7] = val & 0xff;
170 }
171
172 /*
173 * Encode BGP Route Target IP:nn.
174 */
175 static inline void encode_route_target_ip(struct in_addr ip, uint16_t val,
176 struct ecommunity_val *eval)
177 {
178 eval->val[0] = ECOMMUNITY_ENCODE_IP;
179 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
180 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
181 eval->val[6] = (val >> 8) & 0xff;
182 eval->val[7] = val & 0xff;
183 }
184
185 /*
186 * Encode BGP Route Target AS4:nn.
187 */
188 static inline void encode_route_target_as4(as_t as, uint16_t val,
189 struct ecommunity_val *eval)
190 {
191 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
192 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
193 eval->val[2] = (as >> 24) & 0xff;
194 eval->val[3] = (as >> 16) & 0xff;
195 eval->val[4] = (as >> 8) & 0xff;
196 eval->val[5] = as & 0xff;
197 eval->val[6] = (val >> 8) & 0xff;
198 eval->val[7] = val & 0xff;
199 }
200
201 /*
202 * Encode BGP Link Bandwidth extended community
203 * bandwidth (bw) is in bytes-per-sec
204 */
205 static inline void encode_lb_extcomm(as_t as, uint32_t bw, bool non_trans,
206 struct ecommunity_val *eval)
207 {
208 memset(eval, 0, sizeof(*eval));
209 eval->val[0] = ECOMMUNITY_ENCODE_AS;
210 if (non_trans)
211 eval->val[0] |= ECOMMUNITY_FLAG_NON_TRANSITIVE;
212 eval->val[1] = ECOMMUNITY_LINK_BANDWIDTH;
213 eval->val[2] = (as >> 8) & 0xff;
214 eval->val[3] = as & 0xff;
215 eval->val[4] = (bw >> 24) & 0xff;
216 eval->val[5] = (bw >> 16) & 0xff;
217 eval->val[6] = (bw >> 8) & 0xff;
218 eval->val[7] = bw & 0xff;
219 }
220
221 extern void ecommunity_init(void);
222 extern void ecommunity_finish(void);
223 extern void ecommunity_free(struct ecommunity **);
224 extern struct ecommunity *ecommunity_parse(uint8_t *, unsigned short);
225 extern struct ecommunity *ecommunity_parse_ipv6(uint8_t *pnt,
226 unsigned short length);
227 extern struct ecommunity *ecommunity_dup(struct ecommunity *);
228 extern struct ecommunity *ecommunity_merge(struct ecommunity *,
229 struct ecommunity *);
230 extern struct ecommunity *ecommunity_uniq_sort(struct ecommunity *);
231 extern struct ecommunity *ecommunity_intern(struct ecommunity *);
232 extern bool ecommunity_cmp(const void *arg1, const void *arg2);
233 extern void ecommunity_unintern(struct ecommunity **ecommunity);
234 extern unsigned int ecommunity_hash_make(const void *);
235 extern struct ecommunity *ecommunity_str2com(const char *, int, int);
236 extern struct ecommunity *ecommunity_str2com_ipv6(const char *str, int type,
237 int keyword_included);
238 extern char *ecommunity_ecom2str(struct ecommunity *, int, int);
239 extern void ecommunity_strfree(char **s);
240 extern bool ecommunity_match(const struct ecommunity *,
241 const struct ecommunity *);
242 extern char *ecommunity_str(struct ecommunity *);
243 extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *,
244 uint8_t, uint8_t);
245
246 extern bool ecommunity_add_val(struct ecommunity *ecom,
247 struct ecommunity_val *eval,
248 bool unique, bool overwrite);
249 extern bool ecommunity_add_val_ipv6(struct ecommunity *ecom,
250 struct ecommunity_val_ipv6 *eval,
251 bool unique, bool overwrite);
252
253 /* for vpn */
254 extern struct ecommunity *ecommunity_new(void);
255 extern bool ecommunity_strip(struct ecommunity *ecom, uint8_t type,
256 uint8_t subtype);
257 extern struct ecommunity *ecommunity_new(void);
258 extern bool ecommunity_del_val(struct ecommunity *ecom,
259 struct ecommunity_val *eval);
260 struct bgp_pbr_entry_action;
261 extern int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
262 struct bgp_pbr_entry_action *api,
263 afi_t afi);
264
265 extern void bgp_compute_aggregate_ecommunity(
266 struct bgp_aggregate *aggregate,
267 struct ecommunity *ecommunity);
268
269 extern void bgp_compute_aggregate_ecommunity_hash(
270 struct bgp_aggregate *aggregate,
271 struct ecommunity *ecommunity);
272 extern void bgp_compute_aggregate_ecommunity_val(
273 struct bgp_aggregate *aggregate);
274 extern void bgp_remove_ecommunity_from_aggregate(
275 struct bgp_aggregate *aggregate,
276 struct ecommunity *ecommunity);
277 extern void bgp_remove_ecomm_from_aggregate_hash(
278 struct bgp_aggregate *aggregate,
279 struct ecommunity *ecommunity);
280 extern void bgp_aggr_ecommunity_remove(void *arg);
281 extern const uint8_t *ecommunity_linkbw_present(struct ecommunity *ecom,
282 uint32_t *bw);
283 extern struct ecommunity *ecommunity_replace_linkbw(as_t as,
284 struct ecommunity *ecom, uint64_t cum_bw);
285
286 static inline void ecommunity_strip_rts(struct ecommunity *ecom)
287 {
288 uint8_t subtype = ECOMMUNITY_ROUTE_TARGET;
289
290 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS, subtype);
291 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_IP, subtype);
292 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS4, subtype);
293 }
294 #endif /* _QUAGGA_BGP_ECOMMUNITY_H */