]>
Commit | Line | Data |
---|---|---|
212f5cbc | 1 | /* Ethernet-VPN Attribute handling file |
896014f4 DL |
2 | * Copyright (C) 2016 6WIND |
3 | * | |
4 | * This file is part of FRRouting. | |
5 | * | |
6 | * FRRouting 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 | * FRRouting 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 | */ | |
212f5cbc PG |
20 | |
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
7ef5a232 | 24 | #include "filter.h" |
212f5cbc PG |
25 | #include "prefix.h" |
26 | #include "log.h" | |
27 | #include "memory.h" | |
28 | #include "stream.h" | |
29 | ||
dfa42ea3 PG |
30 | #include "bgpd/bgpd.h" |
31 | #include "bgpd/bgp_attr.h" | |
32 | #include "bgpd/bgp_route.h" | |
212f5cbc | 33 | #include "bgpd/bgp_attr_evpn.h" |
dfa42ea3 | 34 | #include "bgpd/bgp_ecommunity.h" |
3da6fcd5 | 35 | #include "bgpd/bgp_evpn.h" |
128ea8ab | 36 | #include "bgpd/bgp_evpn_private.h" |
dfa42ea3 | 37 | |
31689a53 | 38 | void bgp_add_routermac_ecom(struct attr *attr, struct ethaddr *routermac) |
dfa42ea3 | 39 | { |
d62a17ae | 40 | struct ecommunity_val routermac_ecom; |
41 | ||
42 | memset(&routermac_ecom, 0, sizeof(struct ecommunity_val)); | |
43 | routermac_ecom.val[0] = ECOMMUNITY_ENCODE_EVPN; | |
44 | routermac_ecom.val[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC; | |
28328ea9 | 45 | memcpy(&routermac_ecom.val[2], routermac->octet, ETH_ALEN); |
d62a17ae | 46 | if (!attr->ecommunity) |
47 | attr->ecommunity = ecommunity_new(); | |
48 | ecommunity_add_val(attr->ecommunity, &routermac_ecom); | |
49 | ecommunity_str(attr->ecommunity); | |
dfa42ea3 | 50 | } |
212f5cbc | 51 | |
212f5cbc PG |
52 | /* converts to an esi |
53 | * returns 1 on success, 0 otherwise | |
54 | * format accepted: AA:BB:CC:DD:EE:FF:GG:HH:II:JJ | |
55 | * if id is null, check only is done | |
56 | */ | |
4d0e6ece | 57 | int str2esi(const char *str, struct eth_segment_id *id) |
212f5cbc | 58 | { |
d62a17ae | 59 | unsigned int a[ESI_LEN]; |
60 | int i; | |
61 | ||
62 | if (!str) | |
63 | return 0; | |
64 | if (sscanf(str, "%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x", a + 0, a + 1, | |
65 | a + 2, a + 3, a + 4, a + 5, a + 6, a + 7, a + 8, a + 9) | |
66 | != ESI_LEN) { | |
67 | /* error in incoming str length */ | |
68 | return 0; | |
69 | } | |
70 | /* valid mac address */ | |
71 | if (!id) | |
72 | return 1; | |
73 | for (i = 0; i < ESI_LEN; ++i) | |
74 | id->val[i] = a[i] & 0xff; | |
75 | return 1; | |
212f5cbc PG |
76 | } |
77 | ||
4d0e6ece | 78 | char *esi2str(struct eth_segment_id *id) |
212f5cbc | 79 | { |
d62a17ae | 80 | char *ptr; |
d7c0a89a | 81 | uint8_t *val; |
212f5cbc | 82 | |
d62a17ae | 83 | if (!id) |
84 | return NULL; | |
212f5cbc | 85 | |
d62a17ae | 86 | val = id->val; |
9f5dc319 QY |
87 | ptr = XMALLOC(MTYPE_TMP, |
88 | (ESI_LEN * 2 + ESI_LEN - 1 + 1) * sizeof(char)); | |
212f5cbc | 89 | |
d62a17ae | 90 | snprintf(ptr, (ESI_LEN * 2 + ESI_LEN - 1 + 1), |
91 | "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", val[0], | |
92 | val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], | |
93 | val[9]); | |
212f5cbc | 94 | |
d62a17ae | 95 | return ptr; |
212f5cbc PG |
96 | } |
97 | ||
212f5cbc PG |
98 | char *ecom_mac2str(char *ecom_mac) |
99 | { | |
d62a17ae | 100 | char *en; |
212f5cbc | 101 | |
d62a17ae | 102 | en = ecom_mac; |
103 | en += 2; | |
128ea8ab | 104 | |
d62a17ae | 105 | return prefix_mac2str((struct ethaddr *)en, NULL, 0); |
128ea8ab | 106 | } |
107 | ||
bc59a672 | 108 | /* Fetch router-mac from extended community */ |
eee353c5 | 109 | bool bgp_attr_rmac(struct attr *attr, struct ethaddr *rmac) |
bc59a672 MK |
110 | { |
111 | int i = 0; | |
112 | struct ecommunity *ecom; | |
113 | ||
114 | ecom = attr->ecommunity; | |
115 | if (!ecom || !ecom->size) | |
eee353c5 | 116 | return false; |
bc59a672 MK |
117 | |
118 | /* If there is a router mac extended community, set RMAC in attr */ | |
119 | for (i = 0; i < ecom->size; i++) { | |
d7c0a89a QY |
120 | uint8_t *pnt = NULL; |
121 | uint8_t type = 0; | |
122 | uint8_t sub_type = 0; | |
bc59a672 MK |
123 | |
124 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
125 | type = *pnt++; | |
126 | sub_type = *pnt++; | |
127 | ||
996c9314 LB |
128 | if (!(type == ECOMMUNITY_ENCODE_EVPN |
129 | && sub_type == ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC)) | |
bc59a672 MK |
130 | continue; |
131 | ||
132 | memcpy(rmac, pnt, ETH_ALEN); | |
eee353c5 | 133 | return true; |
bc59a672 | 134 | } |
eee353c5 | 135 | return false; |
bc59a672 MK |
136 | } |
137 | ||
ead40654 MK |
138 | /* |
139 | * return true if attr contains default gw extended community | |
140 | */ | |
141 | uint8_t bgp_attr_default_gw(struct attr *attr) | |
142 | { | |
996c9314 LB |
143 | struct ecommunity *ecom; |
144 | int i; | |
ead40654 MK |
145 | |
146 | ecom = attr->ecommunity; | |
147 | if (!ecom || !ecom->size) | |
148 | return 0; | |
149 | ||
150 | /* If there is a default gw extendd community return true otherwise | |
151 | * return 0 */ | |
152 | for (i = 0; i < ecom->size; i++) { | |
d7c0a89a QY |
153 | uint8_t *pnt; |
154 | uint8_t type, sub_type; | |
ead40654 MK |
155 | |
156 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
157 | type = *pnt++; | |
158 | sub_type = *pnt++; | |
159 | ||
160 | if ((type == ECOMMUNITY_ENCODE_OPAQUE | |
996c9314 | 161 | && sub_type == ECOMMUNITY_EVPN_SUBTYPE_DEF_GW)) |
ead40654 MK |
162 | return 1; |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
128ea8ab | 168 | /* |
169 | * Fetch and return the sequence number from MAC Mobility extended | |
170 | * community, if present, else 0. | |
171 | */ | |
d7c0a89a | 172 | uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr, uint8_t *sticky) |
128ea8ab | 173 | { |
d62a17ae | 174 | struct ecommunity *ecom; |
175 | int i; | |
d7c0a89a | 176 | uint8_t flags = 0; |
d62a17ae | 177 | |
178 | ecom = attr->ecommunity; | |
179 | if (!ecom || !ecom->size) | |
180 | return 0; | |
181 | ||
182 | /* If there is a MAC Mobility extended community, return its | |
183 | * sequence number. | |
184 | * TODO: RFC is silent on handling of multiple MAC mobility extended | |
185 | * communities for the same route. We will bail out upon the first | |
186 | * one. | |
187 | */ | |
188 | for (i = 0; i < ecom->size; i++) { | |
d7c0a89a QY |
189 | uint8_t *pnt; |
190 | uint8_t type, sub_type; | |
191 | uint32_t seq_num; | |
d62a17ae | 192 | |
193 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
194 | type = *pnt++; | |
195 | sub_type = *pnt++; | |
196 | if (!(type == ECOMMUNITY_ENCODE_EVPN | |
197 | && sub_type == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY)) | |
198 | continue; | |
199 | flags = *pnt++; | |
200 | ||
201 | if (flags & ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY) | |
202 | *sticky = 1; | |
203 | else | |
204 | *sticky = 0; | |
205 | ||
206 | pnt++; | |
937652c6 DL |
207 | pnt = ptr_get_be32(pnt, &seq_num); |
208 | (void)pnt; /* consume value */ | |
d62a17ae | 209 | return seq_num; |
210 | } | |
211 | ||
212 | return 0; | |
212f5cbc | 213 | } |
3da6fcd5 | 214 | |
68e33151 CS |
215 | /* |
216 | * return true if attr contains router flag extended community | |
217 | */ | |
218 | void bgp_attr_evpn_na_flag(struct attr *attr, uint8_t *router_flag) | |
219 | { | |
220 | struct ecommunity *ecom; | |
221 | int i; | |
222 | uint8_t val; | |
223 | ||
224 | ecom = attr->ecommunity; | |
225 | if (!ecom || !ecom->size) | |
226 | return; | |
227 | ||
228 | /* If there is a evpn na extendd community set router_flag */ | |
229 | for (i = 0; i < ecom->size; i++) { | |
230 | uint8_t *pnt; | |
231 | uint8_t type, sub_type; | |
232 | ||
233 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
234 | type = *pnt++; | |
235 | sub_type = *pnt++; | |
236 | ||
237 | if (type == ECOMMUNITY_ENCODE_EVPN && | |
238 | sub_type == ECOMMUNITY_EVPN_SUBTYPE_ND) { | |
239 | val = *pnt++; | |
240 | if (val & ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG) { | |
241 | *router_flag = 1; | |
242 | break; | |
243 | } | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
3da6fcd5 | 248 | /* dst prefix must be AF_INET or AF_INET6 prefix, to forge EVPN prefix */ |
d62a17ae | 249 | extern int bgp_build_evpn_prefix(int evpn_type, uint32_t eth_tag, |
250 | struct prefix *dst) | |
3da6fcd5 | 251 | { |
d62a17ae | 252 | struct evpn_addr *p_evpn_p; |
253 | struct prefix p2; | |
254 | struct prefix *src = &p2; | |
255 | ||
256 | if (!dst || dst->family == 0) | |
257 | return -1; | |
258 | /* store initial prefix in src */ | |
259 | prefix_copy(src, dst); | |
260 | memset(dst, 0, sizeof(struct prefix)); | |
261 | p_evpn_p = &(dst->u.prefix_evpn); | |
b03b8898 | 262 | dst->family = AF_EVPN; |
d62a17ae | 263 | p_evpn_p->route_type = evpn_type; |
264 | if (evpn_type == BGP_EVPN_IP_PREFIX_ROUTE) { | |
3714a385 | 265 | p_evpn_p->prefix_addr.eth_tag = eth_tag; |
266 | p_evpn_p->prefix_addr.ip_prefix_length = p2.prefixlen; | |
d62a17ae | 267 | if (src->family == AF_INET) { |
3714a385 | 268 | SET_IPADDR_V4(&p_evpn_p->prefix_addr.ip); |
269 | memcpy(&p_evpn_p->prefix_addr.ip.ipaddr_v4, | |
270 | &src->u.prefix4, | |
d62a17ae | 271 | sizeof(struct in_addr)); |
61be6e94 | 272 | dst->prefixlen = (uint16_t)PREFIX_LEN_ROUTE_TYPE_5_IPV4; |
d62a17ae | 273 | } else { |
3714a385 | 274 | SET_IPADDR_V6(&p_evpn_p->prefix_addr.ip); |
275 | memcpy(&p_evpn_p->prefix_addr.ip.ipaddr_v6, | |
276 | &src->u.prefix6, | |
d62a17ae | 277 | sizeof(struct in6_addr)); |
61be6e94 | 278 | dst->prefixlen = (uint16_t)PREFIX_LEN_ROUTE_TYPE_5_IPV6; |
d62a17ae | 279 | } |
280 | } else | |
281 | return -1; | |
282 | return 0; | |
3da6fcd5 | 283 | } |