1 /* BGP Extended Communities Attribute.
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
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
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.
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
21 #ifndef _QUAGGA_BGP_ECOMMUNITY_H
22 #define _QUAGGA_BGP_ECOMMUNITY_H
24 #include "bgpd/bgp_route.h"
25 #include "bgpd/bgpd.h"
27 /* Refer to rfc7153 for the IANA registry definitions. These are
28 * updated by other standards like rfc7674.
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
41 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 0x81
42 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_3 0x82
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
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.
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
65 #define ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4 0x0c
66 /* from draft-ietf-idr-flow-spec-v6-09
67 * 0x0b Flow-spec Redirect to IPv6
69 #define ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6 0x0b
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
79 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY 0x01
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
85 #define ECOMMUNITY_EVPN_SUBTYPE_ESI_SA_FLAG (1 << 0) /* single-active */
87 /* Low-order octet of the Extended Communities type field for OPAQUE types */
88 #define ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP 0x0c
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
95 /* Extended Communities value is eight octet long. */
96 #define ECOMMUNITY_SIZE 8
97 #define IPV6_ECOMMUNITY_SIZE 20
99 /* Extended Communities type flag. */
100 #define ECOMMUNITY_FLAG_NON_TRANSITIVE 0x40
102 /* Extended Communities attribute. */
104 /* Reference counter. */
105 unsigned long refcnt
;
107 /* Size of Each Unit of Extended Communities attribute.
108 * to differentiate between IPv6 ext comm and ext comm
112 /* Size of Extended Communities attribute. */
115 /* Extended Communities value. */
118 /* Human readable format string. */
122 struct ecommunity_as
{
127 struct ecommunity_ip
{
132 struct ecommunity_ip6
{
137 /* Extended community value is eight octet. */
138 struct ecommunity_val
{
139 char val
[ECOMMUNITY_SIZE
];
142 /* IPv6 Extended community value is eight octet. */
143 struct ecommunity_val_ipv6
{
144 char val
[IPV6_ECOMMUNITY_SIZE
];
147 #define ecom_length_size(X, Y) ((X)->size * (Y))
150 * Encode BGP Route Target AS:nn.
152 static inline void encode_route_target_as(as_t as
, uint32_t val
,
153 struct ecommunity_val
*eval
)
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;
166 * Encode BGP Route Target IP:nn.
168 static inline void encode_route_target_ip(struct in_addr ip
, uint16_t val
,
169 struct ecommunity_val
*eval
)
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;
179 * Encode BGP Route Target AS4:nn.
181 static inline void encode_route_target_as4(as_t as
, uint16_t val
,
182 struct ecommunity_val
*eval
)
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;
195 * Encode BGP Link Bandwidth extended community
196 * bandwidth (bw) is in bytes-per-sec
198 static inline void encode_lb_extcomm(as_t as
, uint32_t bw
, bool non_trans
,
199 struct ecommunity_val
*eval
)
201 memset(eval
, 0, sizeof(*eval
));
202 eval
->val
[0] = ECOMMUNITY_ENCODE_AS
;
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;
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
*,
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
);
247 extern struct ecommunity
*ecommunity_new(void);
248 extern bool ecommunity_strip(struct ecommunity
*ecom
, uint8_t type
,
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
,
258 extern void bgp_compute_aggregate_ecommunity(
259 struct bgp_aggregate
*aggregate
,
260 struct ecommunity
*ecommunity
);
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
,
276 extern struct ecommunity
*ecommunity_replace_linkbw(as_t as
,
277 struct ecommunity
*ecom
, uint64_t cum_bw
);
279 static inline void ecommunity_strip_rts(struct ecommunity
*ecom
)
281 uint8_t subtype
= ECOMMUNITY_ROUTE_TARGET
;
283 ecommunity_strip(ecom
, ECOMMUNITY_ENCODE_AS
, subtype
);
284 ecommunity_strip(ecom
, ECOMMUNITY_ENCODE_IP
, subtype
);
285 ecommunity_strip(ecom
, ECOMMUNITY_ENCODE_AS4
, subtype
);
287 #endif /* _QUAGGA_BGP_ECOMMUNITY_H */