]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_ecommunity.h
*: Replace hash_cmp function return value to a bool
[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 /* High-order octet of the Extended Communities type field. */
25 #define ECOMMUNITY_ENCODE_AS 0x00
26 #define ECOMMUNITY_ENCODE_IP 0x01
27 #define ECOMMUNITY_ENCODE_AS4 0x02
28 #define ECOMMUNITY_ENCODE_OPAQUE 0x03
29 #define ECOMMUNITY_ENCODE_EVPN 0x06
30 #define ECOMMUNITY_ENCODE_TRANS_EXP 0x80 /* Flow Spec */
31 #define ECOMMUNITY_ENCODE_REDIRECT_IP_NH 0x08 /* Flow Spec */
32 /* RFC7674 */
33 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 0x81
34 #define ECOMMUNITY_EXTENDED_COMMUNITY_PART_3 0x82
35
36 /* Low-order octet of the Extended Communities type field. */
37 #define ECOMMUNITY_ROUTE_TARGET 0x02
38 #define ECOMMUNITY_SITE_ORIGIN 0x03
39 #define ECOMMUNITY_TRAFFIC_RATE 0x06 /* Flow Spec */
40 #define ECOMMUNITY_TRAFFIC_ACTION 0x07
41 #define ECOMMUNITY_REDIRECT_VRF 0x08
42 #define ECOMMUNITY_TRAFFIC_MARKING 0x09
43 #define ECOMMUNITY_REDIRECT_IP_NH 0x00
44
45 /* Low-order octet of the Extended Communities type field for EVPN types */
46 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY 0x00
47 #define ECOMMUNITY_EVPN_SUBTYPE_ESI_LABEL 0x01
48 #define ECOMMUNITY_EVPN_SUBTYPE_ES_IMPORT_RT 0x02
49 #define ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC 0x03
50 #define ECOMMUNITY_EVPN_SUBTYPE_DEF_GW 0x0d
51 #define ECOMMUNITY_EVPN_SUBTYPE_ND 0x08
52
53 #define ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY 0x01
54 #define ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG 0x01
55 #define ECOMMUNITY_EVPN_SUBTYPE_ND_OVERRIDE_FLAG 0x02
56
57 /* Low-order octet of the Extended Communities type field for OPAQUE types */
58 #define ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP 0x0c
59
60 /* Extended communities attribute string format. */
61 #define ECOMMUNITY_FORMAT_ROUTE_MAP 0
62 #define ECOMMUNITY_FORMAT_COMMUNITY_LIST 1
63 #define ECOMMUNITY_FORMAT_DISPLAY 2
64
65 /* Extended Communities value is eight octet long. */
66 #define ECOMMUNITY_SIZE 8
67
68 /* Extended Communities type flag. */
69 #define ECOMMUNITY_FLAG_NON_TRANSITIVE 0x40
70
71 /* Extended Communities attribute. */
72 struct ecommunity {
73 /* Reference counter. */
74 unsigned long refcnt;
75
76 /* Size of Extended Communities attribute. */
77 int size;
78
79 /* Extended Communities value. */
80 uint8_t *val;
81
82 /* Human readable format string. */
83 char *str;
84 };
85
86 struct ecommunity_as {
87 as_t as;
88 uint32_t val;
89 };
90
91 struct ecommunity_ip {
92 struct in_addr ip;
93 uint16_t val;
94 };
95
96 /* Extended community value is eight octet. */
97 struct ecommunity_val {
98 char val[ECOMMUNITY_SIZE];
99 };
100
101 #define ecom_length(X) ((X)->size * ECOMMUNITY_SIZE)
102
103 /*
104 * Encode BGP Route Target AS:nn.
105 */
106 static inline void encode_route_target_as(as_t as, uint32_t val,
107 struct ecommunity_val *eval)
108 {
109 eval->val[0] = ECOMMUNITY_ENCODE_AS;
110 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
111 eval->val[2] = (as >> 8) & 0xff;
112 eval->val[3] = as & 0xff;
113 eval->val[4] = (val >> 24) & 0xff;
114 eval->val[5] = (val >> 16) & 0xff;
115 eval->val[6] = (val >> 8) & 0xff;
116 eval->val[7] = val & 0xff;
117 }
118
119 /*
120 * Encode BGP Route Target IP:nn.
121 */
122 static inline void encode_route_target_ip(struct in_addr ip, uint16_t val,
123 struct ecommunity_val *eval)
124 {
125 eval->val[0] = ECOMMUNITY_ENCODE_IP;
126 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
127 memcpy(&eval->val[2], &ip, sizeof(struct in_addr));
128 eval->val[6] = (val >> 8) & 0xff;
129 eval->val[7] = val & 0xff;
130 }
131
132 /*
133 * Encode BGP Route Target AS4:nn.
134 */
135 static inline void encode_route_target_as4(as_t as, uint16_t val,
136 struct ecommunity_val *eval)
137 {
138 eval->val[0] = ECOMMUNITY_ENCODE_AS4;
139 eval->val[1] = ECOMMUNITY_ROUTE_TARGET;
140 eval->val[2] = (as >> 24) & 0xff;
141 eval->val[3] = (as >> 16) & 0xff;
142 eval->val[4] = (as >> 8) & 0xff;
143 eval->val[5] = as & 0xff;
144 eval->val[6] = (val >> 8) & 0xff;
145 eval->val[7] = val & 0xff;
146 }
147
148 extern void ecommunity_init(void);
149 extern void ecommunity_finish(void);
150 extern void ecommunity_free(struct ecommunity **);
151 extern struct ecommunity *ecommunity_parse(uint8_t *, unsigned short);
152 extern struct ecommunity *ecommunity_dup(struct ecommunity *);
153 extern struct ecommunity *ecommunity_merge(struct ecommunity *,
154 struct ecommunity *);
155 extern struct ecommunity *ecommunity_uniq_sort(struct ecommunity *);
156 extern struct ecommunity *ecommunity_intern(struct ecommunity *);
157 extern bool ecommunity_cmp(const void *arg1, const void *arg2);
158 extern void ecommunity_unintern(struct ecommunity **);
159 extern unsigned int ecommunity_hash_make(void *);
160 extern struct ecommunity *ecommunity_str2com(const char *, int, int);
161 extern char *ecommunity_ecom2str(struct ecommunity *, int, int);
162 extern void ecommunity_strfree(char **s);
163 extern int ecommunity_match(const struct ecommunity *,
164 const struct ecommunity *);
165 extern char *ecommunity_str(struct ecommunity *);
166 extern struct ecommunity_val *ecommunity_lookup(const struct ecommunity *,
167 uint8_t, uint8_t);
168 extern int ecommunity_add_val(struct ecommunity *ecom,
169 struct ecommunity_val *eval);
170
171 /* for vpn */
172 extern struct ecommunity *ecommunity_new(void);
173 extern int ecommunity_add_val(struct ecommunity *, struct ecommunity_val *);
174 extern int ecommunity_strip(struct ecommunity *ecom, uint8_t type,
175 uint8_t subtype);
176 extern struct ecommunity *ecommunity_new(void);
177 extern int ecommunity_del_val(struct ecommunity *ecom,
178 struct ecommunity_val *eval);
179 struct bgp_pbr_entry_action;
180 extern int ecommunity_fill_pbr_action(struct ecommunity_val *ecom_eval,
181 struct bgp_pbr_entry_action *api);
182
183 #endif /* _QUAGGA_BGP_ECOMMUNITY_H */