3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _ZEBRA_PREFIX_H
23 #define _ZEBRA_PREFIX_H
26 #include <net/ethernet.h>
28 #include <netinet/if_ether.h>
30 #include "sockunion.h"
42 /* EVPN route types. */
44 BGP_EVPN_AD_ROUTE
= 1, /* Ethernet Auto-Discovery (A-D) route */
45 BGP_EVPN_MAC_IP_ROUTE
, /* MAC/IP Advertisement route */
46 BGP_EVPN_IMET_ROUTE
, /* Inclusive Multicast Ethernet Tag route */
47 BGP_EVPN_ES_ROUTE
, /* Ethernet Segment route */
48 BGP_EVPN_IP_PREFIX_ROUTE
, /* IP Prefix route */
49 } bgp_evpn_route_type
;
51 /* value of first byte of ESI */
52 #define ESI_TYPE_ARBITRARY 0 /* */
53 #define ESI_TYPE_LACP 1 /* <> */
54 #define ESI_TYPE_BRIDGE 2 /* <Root bridge Mac-6B>:<Root Br Priority-2B>:00 */
55 #define ESI_TYPE_MAC 3 /* <Syst Mac Add-6B>:<Local Discriminator Value-3B> */
56 #define ESI_TYPE_ROUTER 4 /* <RouterId-4B>:<Local Discriminator Value-4B> */
57 #define ESI_TYPE_AS 5 /* <AS-4B>:<Local Discriminator Value-4B> */
59 #define MAX_ESI {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
62 #define EVPN_ETH_TAG_BYTES 4
64 #define ESI_STR_LEN (3 * ESI_BYTES)
65 #define EVPN_DF_ALG_STR_LEN 24
67 /* Maximum number of VTEPs per-ES -
68 * XXX - temporary limit for allocating strings etc.
70 #define ES_VTEP_MAX_CNT 10
71 #define ES_VTEP_LIST_STR_SZ (ES_VTEP_MAX_CNT * 16)
73 #define ETHER_ADDR_STRLEN (3*ETH_ALEN)
75 * there isn't a portable ethernet address type. We define our
76 * own to simplify internal handling
79 uint8_t octet
[ETH_ALEN
];
80 } __attribute__((packed
));
83 /* length is the number of valuable bits of prefix structure
84 * 18 bytes is current length in structure, if address is ipv4
85 * 30 bytes is in case of ipv6
87 #define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
88 #define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
90 typedef struct esi_t_
{
91 uint8_t val
[ESI_BYTES
];
94 struct evpn_ead_addr
{
100 struct evpn_macip_addr
{
102 uint8_t ip_prefix_length
;
107 struct evpn_imet_addr
{
109 uint8_t ip_prefix_length
;
113 struct evpn_es_addr
{
115 uint8_t ip_prefix_length
;
119 struct evpn_prefix_addr
{
121 uint8_t ip_prefix_length
;
125 /* EVPN address (RFC 7432) */
129 struct evpn_ead_addr _ead_addr
;
130 struct evpn_macip_addr _macip_addr
;
131 struct evpn_imet_addr _imet_addr
;
132 struct evpn_es_addr _es_addr
;
133 struct evpn_prefix_addr _prefix_addr
;
135 #define ead_addr u._ead_addr
136 #define macip_addr u._macip_addr
137 #define imet_addr u._imet_addr
138 #define es_addr u._es_addr
139 #define prefix_addr u._prefix_addr
143 * A struct prefix contains an address family, a prefix length, and an
144 * address. This can represent either a 'network prefix' as defined
145 * by CIDR, where the 'host bits' of the prefix are 0
146 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
147 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
151 /* different OSes use different names */
152 #if defined(AF_PACKET)
153 #define AF_ETHERNET AF_PACKET
156 #define AF_ETHERNET AF_LINK
160 /* The 'family' in the prefix structure is internal to FRR and need not
161 * map to standard OS AF_ definitions except where needed for interacting
162 * with the kernel. However, AF_ definitions are currently in use and
163 * prevalent across the code. Define a new FRR-specific AF for EVPN to
164 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
165 * ensure it does not conflict with any OS AF_ definition.
167 #if !defined(AF_EVPN)
168 #define AF_EVPN (AF_MAX + 1)
171 #if !defined(AF_FLOWSPEC)
172 #define AF_FLOWSPEC (AF_MAX + 2)
175 struct flowspec_prefix
{
177 uint16_t prefixlen
; /* length in bytes */
181 /* FRR generic prefix structure. */
187 struct in_addr prefix4
;
188 struct in6_addr prefix6
;
191 struct in_addr adv_router
;
193 struct ethaddr prefix_eth
; /* AF_ETHERNET */
197 struct evpn_addr prefix_evpn
; /* AF_EVPN */
198 struct flowspec_prefix prefix_flowspec
; /* AF_FLOWSPEC */
199 } u
__attribute__((aligned(8)));
202 /* IPv4 prefix structure. */
206 struct in_addr prefix
__attribute__((aligned(8)));
209 /* IPv6 prefix structure. */
213 struct in6_addr prefix
__attribute__((aligned(8)));
219 struct in_addr id
__attribute__((aligned(8)));
220 struct in_addr adv_router
;
223 /* Prefix for routing distinguisher. */
227 uint8_t val
[8] __attribute__((aligned(8)));
230 /* Prefix for ethernet. */
234 struct ethaddr eth_addr
__attribute__((aligned(8))); /* AF_ETHERNET */
237 /* EVPN prefix structure. */
241 struct evpn_addr prefix
__attribute__((aligned(8)));
244 static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn
*evp
)
246 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
247 return IS_IPADDR_NONE(&(evp
)->prefix
.ead_addr
.ip
);
248 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
249 return IS_IPADDR_NONE(&(evp
)->prefix
.macip_addr
.ip
);
250 if (evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
251 return IS_IPADDR_NONE(&(evp
)->prefix
.imet_addr
.ip
);
252 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
)
253 return IS_IPADDR_NONE(&(evp
)->prefix
.es_addr
.ip
);
254 if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
255 return IS_IPADDR_NONE(&(evp
)->prefix
.prefix_addr
.ip
);
259 static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn
*evp
)
261 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
262 return IS_IPADDR_V4(&(evp
)->prefix
.ead_addr
.ip
);
263 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
264 return IS_IPADDR_V4(&(evp
)->prefix
.macip_addr
.ip
);
265 if (evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
266 return IS_IPADDR_V4(&(evp
)->prefix
.imet_addr
.ip
);
267 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
)
268 return IS_IPADDR_V4(&(evp
)->prefix
.es_addr
.ip
);
269 if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
270 return IS_IPADDR_V4(&(evp
)->prefix
.prefix_addr
.ip
);
274 static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn
*evp
)
276 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
)
277 return IS_IPADDR_V6(&(evp
)->prefix
.ead_addr
.ip
);
278 if (evp
->prefix
.route_type
== BGP_EVPN_MAC_IP_ROUTE
)
279 return IS_IPADDR_V6(&(evp
)->prefix
.macip_addr
.ip
);
280 if (evp
->prefix
.route_type
== BGP_EVPN_IMET_ROUTE
)
281 return IS_IPADDR_V6(&(evp
)->prefix
.imet_addr
.ip
);
282 if (evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
)
283 return IS_IPADDR_V6(&(evp
)->prefix
.es_addr
.ip
);
284 if (evp
->prefix
.route_type
== BGP_EVPN_IP_PREFIX_ROUTE
)
285 return IS_IPADDR_V6(&(evp
)->prefix
.prefix_addr
.ip
);
289 /* Prefix for a generic pointer */
293 uintptr_t prefix
__attribute__((aligned(8)));
296 /* Prefix for a Flowspec entry */
299 uint16_t prefixlen
; /* unused */
300 struct flowspec_prefix prefix
__attribute__((aligned(8)));
306 struct in_addr src
__attribute__((aligned(8)));
310 /* helper to get type safety/avoid casts on calls
311 * (w/o this, functions accepting all prefix types need casts on the caller
312 * side, which strips type safety since the cast will accept any pointer
316 #define prefixtype(uname, typename, fieldname) \
319 #define prefixtype(uname, typename, fieldname) \
320 typename *fieldname; \
321 uname(typename *x) { this->fieldname = x; }
325 prefixtype(prefixptr
, struct prefix
, p
)
326 prefixtype(prefixptr
, struct prefix_ipv4
, p4
)
327 prefixtype(prefixptr
, struct prefix_ipv6
, p6
)
328 prefixtype(prefixptr
, struct prefix_evpn
, evp
)
329 prefixtype(prefixptr
, struct prefix_fs
, fs
)
330 prefixtype(prefixptr
, struct prefix_rd
, rd
)
331 } __attribute__((transparent_union
));
333 union prefixconstptr
{
334 prefixtype(prefixconstptr
, const struct prefix
, p
)
335 prefixtype(prefixconstptr
, const struct prefix_ipv4
, p4
)
336 prefixtype(prefixconstptr
, const struct prefix_ipv6
, p6
)
337 prefixtype(prefixconstptr
, const struct prefix_evpn
, evp
)
338 prefixtype(prefixconstptr
, const struct prefix_fs
, fs
)
339 prefixtype(prefixconstptr
, const struct prefix_rd
, rd
)
340 } __attribute__((transparent_union
));
342 #ifndef INET_ADDRSTRLEN
343 #define INET_ADDRSTRLEN 16
344 #endif /* INET_ADDRSTRLEN */
346 #ifndef INET6_ADDRSTRLEN
347 /* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
348 #define INET6_ADDRSTRLEN 46
349 #endif /* INET6_ADDRSTRLEN */
352 #define INET6_BUFSIZ 53
353 #endif /* INET6_BUFSIZ */
355 /* Maximum string length of the result of prefix2str */
356 #define PREFIX_STRLEN 80
359 * Longest possible length of a (S,G) string is 36 bytes
360 * 123.123.123.123 = 15 * 2
362 * NULL Character at end = 1
363 * (123.123.123.123,123.123.123.123)
365 #define PREFIX_SG_STR_LEN 34
367 /* Max bit/byte length of IPv4 address. */
368 #define IPV4_MAX_BYTELEN 4
369 #define IPV4_MAX_BITLEN 32
370 #define IPV4_MAX_PREFIXLEN 32
371 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
373 static inline bool ipv4_addr_same(const struct in_addr
*a
,
374 const struct in_addr
*b
)
376 return (a
->s_addr
== b
->s_addr
);
378 #define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
380 static inline void ipv4_addr_copy(struct in_addr
*dst
,
381 const struct in_addr
*src
)
383 dst
->s_addr
= src
->s_addr
;
385 #define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
387 #define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
388 #define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
389 #define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
390 #define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
391 #define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
393 /* Max bit/byte length of IPv6 address. */
394 #define IPV6_MAX_BYTELEN 16
395 #define IPV6_MAX_BITLEN 128
396 #define IPV6_MAX_PREFIXLEN 128
397 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
398 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
399 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
401 /* Count prefix size from mask length */
402 #define PSIZE(a) (((a) + 7) / (8))
404 #define BSIZE(a) ((a) * (8))
406 /* Prefix's family member. */
407 #define PREFIX_FAMILY(p) ((p)->family)
409 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
411 #define s6_addr32 __u6_addr.__u6_addr32
415 extern int str2family(const char *);
416 extern int afi2family(afi_t
);
417 extern afi_t
family2afi(int);
418 extern const char *family2str(int family
);
419 extern const char *safi2str(safi_t safi
);
420 extern const char *afi2str(afi_t afi
);
423 * Check bit of the prefix.
429 * which bit to fetch from byte buffer, 0 indexed.
431 extern unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t bit_index
);
433 extern struct prefix
*prefix_new(void);
434 extern void prefix_free(struct prefix
**p
);
436 * Function to handle prefix_free being used as a del function.
438 extern void prefix_free_lists(void *arg
);
439 extern const char *prefix_family_str(const struct prefix
*);
440 extern int prefix_blen(const struct prefix
*);
441 extern int str2prefix(const char *, struct prefix
*);
443 #define PREFIX2STR_BUFFER PREFIX_STRLEN
445 extern void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
446 char *buf
, int buf_size
);
447 extern const char *prefix_sg2str(const struct prefix_sg
*sg
, char *str
);
448 extern const char *prefix2str(union prefixconstptr
, char *, int);
449 extern int evpn_type5_prefix_match(const struct prefix
*evpn_pfx
,
450 const struct prefix
*match_pfx
);
451 extern int prefix_match(const struct prefix
*, const struct prefix
*);
452 extern int prefix_match_network_statement(const struct prefix
*,
453 const struct prefix
*);
454 extern int prefix_same(union prefixconstptr
, union prefixconstptr
);
455 extern int prefix_cmp(union prefixconstptr
, union prefixconstptr
);
456 extern int prefix_common_bits(const struct prefix
*, const struct prefix
*);
457 extern void prefix_copy(union prefixptr
, union prefixconstptr
);
458 extern void apply_mask(struct prefix
*);
460 #ifdef __clang_analyzer__
461 /* clang-SA doesn't understand transparent unions, making it think that the
462 * target of prefix_copy is uninitialized. So just memset the target.
463 * cf. https://bugs.llvm.org/show_bug.cgi?id=42811
465 #define prefix_copy(a, b) ({ memset(a, 0, sizeof(*a)); prefix_copy(a, b); })
468 extern struct prefix
*sockunion2hostprefix(const union sockunion
*,
470 extern void prefix2sockunion(const struct prefix
*, union sockunion
*);
472 extern int str2prefix_eth(const char *, struct prefix_eth
*);
474 extern struct prefix_ipv4
*prefix_ipv4_new(void);
475 extern void prefix_ipv4_free(struct prefix_ipv4
**p
);
476 extern int str2prefix_ipv4(const char *, struct prefix_ipv4
*);
477 extern void apply_mask_ipv4(struct prefix_ipv4
*);
479 #define PREFIX_COPY(DST, SRC) \
480 *((struct prefix *)(DST)) = *((const struct prefix *)(SRC))
481 #define PREFIX_COPY_IPV4(DST, SRC) \
482 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC))
484 extern int prefix_ipv4_any(const struct prefix_ipv4
*);
485 extern void apply_classful_mask_ipv4(struct prefix_ipv4
*);
487 extern uint8_t ip_masklen(struct in_addr
);
488 extern void masklen2ip(const int, struct in_addr
*);
489 /* given the address of a host on a network and the network mask length,
490 * calculate the broadcast address for that network;
491 * special treatment for /31 according to RFC3021 section 3.3 */
492 extern in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
);
494 extern int netmask_str2prefix_str(const char *, const char *, char *, size_t);
496 extern struct prefix_ipv6
*prefix_ipv6_new(void);
497 extern void prefix_ipv6_free(struct prefix_ipv6
**p
);
498 extern int str2prefix_ipv6(const char *, struct prefix_ipv6
*);
499 extern void apply_mask_ipv6(struct prefix_ipv6
*);
501 #define PREFIX_COPY_IPV6(DST, SRC) \
502 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC))
504 extern int ip6_masklen(struct in6_addr
);
505 extern void masklen2ip6(const int, struct in6_addr
*);
507 extern const char *inet6_ntoa(struct in6_addr
);
509 extern int is_zero_mac(const struct ethaddr
*mac
);
510 extern bool is_mcast_mac(const struct ethaddr
*mac
);
511 extern bool is_bcast_mac(const struct ethaddr
*mac
);
512 extern int prefix_str2mac(const char *str
, struct ethaddr
*mac
);
513 extern char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
);
515 extern unsigned prefix_hash_key(const void *pp
);
517 extern int str_to_esi(const char *str
, esi_t
*esi
);
518 extern char *esi_to_str(const esi_t
*esi
, char *buf
, int size
);
519 extern char *evpn_es_df_alg2str(uint8_t df_alg
, char *buf
, int buf_len
);
520 extern void prefix_evpn_hexdump(const struct prefix_evpn
*p
);
522 static inline int ipv6_martian(struct in6_addr
*addr
)
524 struct in6_addr localhost_addr
;
526 inet_pton(AF_INET6
, "::1", &localhost_addr
);
528 if (IPV6_ADDR_SAME(&localhost_addr
, addr
))
534 extern int macstr2prefix_evpn(const char *str
, struct prefix_evpn
*p
);
536 /* NOTE: This routine expects the address argument in network byte order. */
537 static inline int ipv4_martian(struct in_addr
*addr
)
539 in_addr_t ip
= ntohl(addr
->s_addr
);
541 if (IPV4_NET0(ip
) || IPV4_NET127(ip
) || IPV4_CLASS_DE(ip
)) {
547 static inline int is_default_prefix(const struct prefix
*p
)
552 if ((p
->family
== AF_INET
) && (p
->u
.prefix4
.s_addr
== INADDR_ANY
)
553 && (p
->prefixlen
== 0))
556 if ((p
->family
== AF_INET6
) && (p
->prefixlen
== 0)
557 && (!memcmp(&p
->u
.prefix6
, &in6addr_any
, sizeof(struct in6_addr
))))
563 static inline int is_host_route(const struct prefix
*p
)
565 if (p
->family
== AF_INET
)
566 return (p
->prefixlen
== IPV4_MAX_BITLEN
);
567 else if (p
->family
== AF_INET6
)
568 return (p
->prefixlen
== IPV6_MAX_BITLEN
);
572 static inline int is_default_host_route(const struct prefix
*p
)
574 if (p
->family
== AF_INET
) {
575 return (p
->u
.prefix4
.s_addr
== INADDR_ANY
&&
576 p
->prefixlen
== IPV4_MAX_BITLEN
);
577 } else if (p
->family
== AF_INET6
) {
578 return ((!memcmp(&p
->u
.prefix6
, &in6addr_any
,
579 sizeof(struct in6_addr
))) &&
580 p
->prefixlen
== IPV6_MAX_BITLEN
);
585 #ifdef _FRR_ATTRIBUTE_PRINTFRR
586 #pragma FRR printfrr_ext "%pEA" (struct ethaddr *)
588 #pragma FRR printfrr_ext "%pI4" (struct in_addr *)
589 #pragma FRR printfrr_ext "%pI4" (in_addr_t *)
591 #pragma FRR printfrr_ext "%pI6" (struct in6_addr *)
593 #pragma FRR printfrr_ext "%pFX" (struct prefix *)
594 #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv4 *)
595 #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv6 *)
596 #pragma FRR printfrr_ext "%pFX" (struct prefix_eth *)
597 #pragma FRR printfrr_ext "%pFX" (struct prefix_evpn *)
598 #pragma FRR printfrr_ext "%pFX" (struct prefix_fs *)
600 #pragma FRR printfrr_ext "%pSG4" (struct prefix_sg *)
607 #endif /* _ZEBRA_PREFIX_H */