]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_ecommunity.h
Merge pull request #11842 from opensourcerouting/fix/topotests_platform_check
[mirror_frr.git] / bgpd / bgp_ecommunity.h
CommitLineData
718e3744 1/* BGP Extended Communities Attribute.
896014f4
DL
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 */
718e3744 20
00d252cb 21#ifndef _QUAGGA_BGP_ECOMMUNITY_H
22#define _QUAGGA_BGP_ECOMMUNITY_H
23
5b820d9e 24#include "bgpd/bgp_route.h"
f4bd90c5 25#include "bgpd/bgpd.h"
5b820d9e 26
7e3ebfd1 27/* Refer to rfc7153 for the IANA registry definitions. These are
28 * updated by other standards like rfc7674.
29 */
718e3744 30/* High-order octet of the Extended Communities type field. */
31#define ECOMMUNITY_ENCODE_AS 0x00
32#define ECOMMUNITY_ENCODE_IP 0x01
0b2aa3a0 33#define ECOMMUNITY_ENCODE_AS4 0x02
0014c301 34#define ECOMMUNITY_ENCODE_OPAQUE 0x03
e82202b7 35#define ECOMMUNITY_ENCODE_EVPN 0x06
b72220fc 36#define ECOMMUNITY_ENCODE_REDIRECT_IP_NH 0x08 /* Flow Spec */
7e3ebfd1 37/* Generic Transitive Experimental */
38#define ECOMMUNITY_ENCODE_TRANS_EXP 0x80
39
a8d72b61
PG
40/* RFC7674 */
41#define ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 0x81
42#define ECOMMUNITY_EXTENDED_COMMUNITY_PART_3 0x82
718e3744 43
7e3ebfd1 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
0014c301 50/* Low-order octet of the Extended Communities type field. */
7e3ebfd1 51/* Note: This really depends on the high-order octet. This means that
52 * multiple definitions for the same value are possible.
53 */
718e3744 54#define ECOMMUNITY_ROUTE_TARGET 0x02
55#define ECOMMUNITY_SITE_ORIGIN 0x03
650b0511 56#define ECOMMUNITY_LINK_BANDWIDTH 0x04
a8d72b61
PG
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
2551b26e
PG
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
9a659715
PG
66/* from draft-ietf-idr-flow-spec-v6-09
67 * 0x0b Flow-spec Redirect to IPv6
68 */
69#define ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6 0x0b
718e3744 70
128ea8ab 71/* Low-order octet of the Extended Communities type field for EVPN types */
1e27ef50
PG
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
74e2bd89 76#define ECOMMUNITY_EVPN_SUBTYPE_DF_ELECTION 0x06
1e27ef50 77#define ECOMMUNITY_EVPN_SUBTYPE_DEF_GW 0x0d
68e33151 78#define ECOMMUNITY_EVPN_SUBTYPE_ND 0x08
1e27ef50 79
128ea8ab 80#define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY 0x01
7904e9fd 81
74e2bd89
AK
82/* DF alg bits - only lower 5 bits are applicable */
83#define ECOMMUNITY_EVPN_SUBTYPE_DF_ALG_BITS 0x1f
84
7904e9fd
AK
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
128ea8ab 88
4248407b
AK
89#define ECOMMUNITY_EVPN_SUBTYPE_ESI_SA_FLAG (1 << 0) /* single-active */
90
0014c301
LB
91/* Low-order octet of the Extended Communities type field for OPAQUE types */
92#define ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP 0x0c
93
718e3744 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
c6423c31 101#define IPV6_ECOMMUNITY_SIZE 20
718e3744 102
4372df71 103/* Extended Communities type flag. */
7c40bf39 104#define ECOMMUNITY_FLAG_NON_TRANSITIVE 0x40
4372df71 105
f5e04c75
PR
106/* Extended Community readable string length */
107#define ECOMMUNITY_STRLEN 64
108
718e3744 109/* Extended Communities attribute. */
d62a17ae 110struct ecommunity {
111 /* Reference counter. */
112 unsigned long refcnt;
718e3744 113
9a659715
PG
114 /* Size of Each Unit of Extended Communities attribute.
115 * to differentiate between IPv6 ext comm and ext comm
116 */
c6423c31 117 uint8_t unit_size;
9a659715 118
d62a17ae 119 /* Size of Extended Communities attribute. */
f6e07e1b 120 uint32_t size;
718e3744 121
d62a17ae 122 /* Extended Communities value. */
d7c0a89a 123 uint8_t *val;
718e3744 124
d62a17ae 125 /* Human readable format string. */
126 char *str;
27aa23a4
DA
127
128 /* Disable IEEE floating-point encoding for extended community */
129 bool disable_ieee_floating;
718e3744 130};
131
5a0ccebf
QY
132struct ecommunity_as {
133 as_t as;
134 uint32_t val;
135};
136
137struct ecommunity_ip {
138 struct in_addr ip;
139 uint16_t val;
140};
141
9a659715
PG
142struct ecommunity_ip6 {
143 struct in6_addr ip;
144 uint16_t val;
145};
146
718e3744 147/* Extended community value is eight octet. */
d62a17ae 148struct ecommunity_val {
149 char val[ECOMMUNITY_SIZE];
718e3744 150};
151
9a659715
PG
152/* IPv6 Extended community value is eight octet. */
153struct ecommunity_val_ipv6 {
154 char val[IPV6_ECOMMUNITY_SIZE];
155};
156
157#define ecom_length_size(X, Y) ((X)->size * (Y))
158
c5900768 159/*
160 * Encode BGP Route Target AS:nn.
161 */
d7c0a89a 162static inline void encode_route_target_as(as_t as, uint32_t val,
d62a17ae 163 struct ecommunity_val *eval)
c5900768 164{
d62a17ae 165 eval->val[0] = ECOMMUNITY_ENCODE_AS;
166 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
167 eval->val[2] = (as >> 8) & 0xff;
168 eval->val[3] = as & 0xff;
169 eval->val[4] = (val >> 24) & 0xff;
170 eval->val[5] = (val >> 16) & 0xff;
171 eval->val[6] = (val >> 8) & 0xff;
172 eval->val[7] = val & 0xff;
c5900768 173}
174
175/*
176 * Encode BGP Route Target IP:nn.
177 */
d7c0a89a 178static inline void encode_route_target_ip(struct in_addr ip, uint16_t val,
d62a17ae 179 struct ecommunity_val *eval)
c5900768 180{
d62a17ae 181 eval->val[0] = ECOMMUNITY_ENCODE_IP;
182 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
183 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
184 eval->val[6] = (val >> 8) & 0xff;
185 eval->val[7] = val & 0xff;
c5900768 186}
187
188/*
189 * Encode BGP Route Target AS4:nn.
190 */
d7c0a89a 191static inline void encode_route_target_as4(as_t as, uint16_t val,
d62a17ae 192 struct ecommunity_val *eval)
c5900768 193{
d62a17ae 194 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
195 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
196 eval->val[2] = (as >> 24) & 0xff;
197 eval->val[3] = (as >> 16) & 0xff;
198 eval->val[4] = (as >> 8) & 0xff;
199 eval->val[5] = as & 0xff;
200 eval->val[6] = (val >> 8) & 0xff;
201 eval->val[7] = val & 0xff;
c5900768 202}
203
8bcaad3d
DA
204/* Helper function to convert uint32 to IEEE-754 Floating Point */
205static uint32_t uint32_to_ieee_float_uint32(uint32_t u)
206{
207 union {
208 float r;
209 uint32_t d;
210 } f = {.r = (float)u};
211
212 return f.d;
213}
214
ca9ac3ef 215/*
216 * Encode BGP Link Bandwidth extended community
217 * bandwidth (bw) is in bytes-per-sec
218 */
219static inline void encode_lb_extcomm(as_t as, uint32_t bw, bool non_trans,
27aa23a4
DA
220 struct ecommunity_val *eval,
221 bool disable_ieee_floating)
ca9ac3ef 222{
27aa23a4
DA
223 uint32_t bandwidth =
224 disable_ieee_floating ? bw : uint32_to_ieee_float_uint32(bw);
8bcaad3d 225
ca9ac3ef 226 memset(eval, 0, sizeof(*eval));
227 eval->val[0] = ECOMMUNITY_ENCODE_AS;
228 if (non_trans)
229 eval->val[0] |= ECOMMUNITY_FLAG_NON_TRANSITIVE;
230 eval->val[1] = ECOMMUNITY_LINK_BANDWIDTH;
231 eval->val[2] = (as >> 8) & 0xff;
232 eval->val[3] = as & 0xff;
8bcaad3d
DA
233 eval->val[4] = (bandwidth >> 24) & 0xff;
234 eval->val[5] = (bandwidth >> 16) & 0xff;
235 eval->val[6] = (bandwidth >> 8) & 0xff;
236 eval->val[7] = bandwidth & 0xff;
ca9ac3ef 237}
238
d62a17ae 239extern void ecommunity_init(void);
240extern void ecommunity_finish(void);
241extern void ecommunity_free(struct ecommunity **);
27aa23a4
DA
242extern struct ecommunity *ecommunity_parse(uint8_t *, unsigned short,
243 bool disable_ieee_floating);
9a659715 244extern struct ecommunity *ecommunity_parse_ipv6(uint8_t *pnt,
27aa23a4
DA
245 unsigned short length,
246 bool disable_ieee_floating);
d62a17ae 247extern struct ecommunity *ecommunity_dup(struct ecommunity *);
248extern struct ecommunity *ecommunity_merge(struct ecommunity *,
249 struct ecommunity *);
250extern struct ecommunity *ecommunity_uniq_sort(struct ecommunity *);
251extern struct ecommunity *ecommunity_intern(struct ecommunity *);
74df8d6d 252extern bool ecommunity_cmp(const void *arg1, const void *arg2);
df7d4670 253extern void ecommunity_unintern(struct ecommunity **ecommunity);
d8b87afe 254extern unsigned int ecommunity_hash_make(const void *);
d62a17ae 255extern struct ecommunity *ecommunity_str2com(const char *, int, int);
9a659715
PG
256extern struct ecommunity *ecommunity_str2com_ipv6(const char *str, int type,
257 int keyword_included);
d62a17ae 258extern char *ecommunity_ecom2str(struct ecommunity *, int, int);
c7ee6c35 259extern void ecommunity_strfree(char **s);
3dc339cd
DA
260extern bool ecommunity_match(const struct ecommunity *,
261 const struct ecommunity *);
d62a17ae 262extern char *ecommunity_str(struct ecommunity *);
263extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *,
264 uint8_t, uint8_t);
9a659715 265
3dc339cd 266extern bool ecommunity_add_val(struct ecommunity *ecom,
1207a5bc 267 struct ecommunity_val *eval,
268 bool unique, bool overwrite);
9a659715
PG
269extern bool ecommunity_add_val_ipv6(struct ecommunity *ecom,
270 struct ecommunity_val_ipv6 *eval,
271 bool unique, bool overwrite);
00d252cb 272
65efcfce 273/* for vpn */
d62a17ae 274extern struct ecommunity *ecommunity_new(void);
3dc339cd
DA
275extern bool ecommunity_strip(struct ecommunity *ecom, uint8_t type,
276 uint8_t subtype);
d62a17ae 277extern struct ecommunity *ecommunity_new(void);
3dc339cd
DA
278extern bool ecommunity_del_val(struct ecommunity *ecom,
279 struct ecommunity_val *eval);
dacf6ec1
PG
280struct bgp_pbr_entry_action;
281extern int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
f01e580f
PG
282 struct bgp_pbr_entry_action *api,
283 afi_t afi);
dacf6ec1 284
5b820d9e
NT
285extern void bgp_compute_aggregate_ecommunity(
286 struct bgp_aggregate *aggregate,
287 struct ecommunity *ecommunity);
4edd83f9 288
289extern void bgp_compute_aggregate_ecommunity_hash(
290 struct bgp_aggregate *aggregate,
291 struct ecommunity *ecommunity);
292extern void bgp_compute_aggregate_ecommunity_val(
293 struct bgp_aggregate *aggregate);
5b820d9e
NT
294extern void bgp_remove_ecommunity_from_aggregate(
295 struct bgp_aggregate *aggregate,
296 struct ecommunity *ecommunity);
4edd83f9 297extern void bgp_remove_ecomm_from_aggregate_hash(
298 struct bgp_aggregate *aggregate,
299 struct ecommunity *ecommunity);
5b820d9e 300extern void bgp_aggr_ecommunity_remove(void *arg);
d901dc13 301extern const uint8_t *ecommunity_linkbw_present(struct ecommunity *ecom,
302 uint32_t *bw);
7b651a32 303extern struct ecommunity *ecommunity_replace_linkbw(as_t as,
27aa23a4
DA
304 struct ecommunity *ecom,
305 uint64_t cum_bw,
306 bool disable_ieee_floating);
e8bfa90e 307
308static inline void ecommunity_strip_rts(struct ecommunity *ecom)
309{
310 uint8_t subtype = ECOMMUNITY_ROUTE_TARGET;
311
312 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS, subtype);
313 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_IP, subtype);
314 ecommunity_strip(ecom, ECOMMUNITY_ENCODE_AS4, subtype);
315}
00d252cb 316#endif /* _QUAGGA_BGP_ECOMMUNITY_H */