1 /* Ethernet-VPN Attribute handling file
2 * Copyright (C) 2016 6WIND
4 * This file is part of FRRouting.
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
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.
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
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_attr.h"
32 #include "bgpd/bgp_route.h"
33 #include "bgpd/bgp_attr_evpn.h"
34 #include "bgpd/bgp_ecommunity.h"
35 #include "bgpd/bgp_evpn.h"
36 #include "bgpd/bgp_evpn_private.h"
38 void bgp_add_routermac_ecom(struct attr
*attr
, struct ethaddr
*routermac
)
40 struct ecommunity_val routermac_ecom
;
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
;
45 memcpy(&routermac_ecom
.val
[2], routermac
->octet
, ETH_ALEN
);
46 if (!attr
->ecommunity
)
47 attr
->ecommunity
= ecommunity_new();
48 ecommunity_add_val(attr
->ecommunity
, &routermac_ecom
);
49 ecommunity_str(attr
->ecommunity
);
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
57 int str2esi(const char *str
, struct eth_segment_id
*id
)
59 unsigned int a
[ESI_LEN
];
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)
67 /* error in incoming str length */
70 /* valid mac address */
73 for (i
= 0; i
< ESI_LEN
; ++i
)
74 id
->val
[i
] = a
[i
] & 0xff;
78 char *esi2str(struct eth_segment_id
*id
)
87 ptr
= (char *)XMALLOC(MTYPE_TMP
,
88 (ESI_LEN
* 2 + ESI_LEN
- 1 + 1) * sizeof(char));
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],
98 char *ecom_mac2str(char *ecom_mac
)
105 return prefix_mac2str((struct ethaddr
*)en
, NULL
, 0);
108 /* Fetch router-mac from extended community */
109 void bgp_attr_rmac(struct attr
*attr
, struct ethaddr
*rmac
)
112 struct ecommunity
*ecom
;
114 ecom
= attr
->ecommunity
;
115 if (!ecom
|| !ecom
->size
)
118 /* If there is a router mac extended community, set RMAC in attr */
119 for (i
= 0; i
< ecom
->size
; i
++) {
122 uint8_t sub_type
= 0;
124 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
128 if (!(type
== ECOMMUNITY_ENCODE_EVPN
129 && sub_type
== ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
))
132 memcpy(rmac
, pnt
, ETH_ALEN
);
137 * return true if attr contains default gw extended community
139 uint8_t bgp_attr_default_gw(struct attr
*attr
)
141 struct ecommunity
*ecom
;
144 ecom
= attr
->ecommunity
;
145 if (!ecom
|| !ecom
->size
)
148 /* If there is a default gw extendd community return true otherwise
150 for (i
= 0; i
< ecom
->size
; i
++) {
152 uint8_t type
, sub_type
;
154 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
158 if ((type
== ECOMMUNITY_ENCODE_OPAQUE
159 && sub_type
== ECOMMUNITY_EVPN_SUBTYPE_DEF_GW
))
167 * Fetch and return the sequence number from MAC Mobility extended
168 * community, if present, else 0.
170 uint32_t bgp_attr_mac_mobility_seqnum(struct attr
*attr
, uint8_t *sticky
)
172 struct ecommunity
*ecom
;
176 ecom
= attr
->ecommunity
;
177 if (!ecom
|| !ecom
->size
)
180 /* If there is a MAC Mobility extended community, return its
182 * TODO: RFC is silent on handling of multiple MAC mobility extended
183 * communities for the same route. We will bail out upon the first
186 for (i
= 0; i
< ecom
->size
; i
++) {
188 uint8_t type
, sub_type
;
191 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
194 if (!(type
== ECOMMUNITY_ENCODE_EVPN
195 && sub_type
== ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY
))
199 if (flags
& ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY
)
205 pnt
= ptr_get_be32(pnt
, &seq_num
);
206 (void)pnt
; /* consume value */
214 * return true if attr contains router flag extended community
216 void bgp_attr_evpn_na_flag(struct attr
*attr
, uint8_t *router_flag
)
218 struct ecommunity
*ecom
;
222 ecom
= attr
->ecommunity
;
223 if (!ecom
|| !ecom
->size
)
226 /* If there is a evpn na extendd community set router_flag */
227 for (i
= 0; i
< ecom
->size
; i
++) {
229 uint8_t type
, sub_type
;
231 pnt
= (ecom
->val
+ (i
* ECOMMUNITY_SIZE
));
235 if (type
== ECOMMUNITY_ENCODE_EVPN
&&
236 sub_type
== ECOMMUNITY_EVPN_SUBTYPE_ND
) {
238 if (val
& ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG
) {
246 /* dst prefix must be AF_INET or AF_INET6 prefix, to forge EVPN prefix */
247 extern int bgp_build_evpn_prefix(int evpn_type
, uint32_t eth_tag
,
250 struct evpn_addr
*p_evpn_p
;
252 struct prefix
*src
= &p2
;
254 if (!dst
|| dst
->family
== 0)
256 /* store initial prefix in src */
257 prefix_copy(src
, dst
);
258 memset(dst
, 0, sizeof(struct prefix
));
259 p_evpn_p
= &(dst
->u
.prefix_evpn
);
260 dst
->family
= AF_EVPN
;
261 p_evpn_p
->route_type
= evpn_type
;
262 if (evpn_type
== BGP_EVPN_IP_PREFIX_ROUTE
) {
263 p_evpn_p
->prefix_addr
.eth_tag
= eth_tag
;
264 p_evpn_p
->prefix_addr
.ip_prefix_length
= p2
.prefixlen
;
265 if (src
->family
== AF_INET
) {
266 SET_IPADDR_V4(&p_evpn_p
->prefix_addr
.ip
);
267 memcpy(&p_evpn_p
->prefix_addr
.ip
.ipaddr_v4
,
269 sizeof(struct in_addr
));
270 dst
->prefixlen
= (uint8_t)PREFIX_LEN_ROUTE_TYPE_5_IPV4
;
272 SET_IPADDR_V6(&p_evpn_p
->prefix_addr
.ip
);
273 memcpy(&p_evpn_p
->prefix_addr
.ip
.ipaddr_v6
,
275 sizeof(struct in6_addr
));
276 dst
->prefixlen
= (uint8_t)PREFIX_LEN_ROUTE_TYPE_5_IPV6
;