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 <sys/ethernet.h>
29 #include <net/ethernet.h>
31 #include <netinet/if_ether.h>
34 #include "sockunion.h"
47 #define ESI_STR_LEN (3 * ESI_BYTES)
49 #define ETHER_ADDR_STRLEN (3*ETH_ALEN)
51 * there isn't a portable ethernet address type. We define our
52 * own to simplify internal handling
55 uint8_t octet
[ETH_ALEN
];
56 } __attribute__((packed
));
59 /* length is the number of valuable bits of prefix structure
60 * 18 bytes is current length in structure, if address is ipv4
61 * 30 bytes is in case of ipv6
63 #define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
64 #define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
66 typedef struct esi_t_
{
70 struct evpn_ead_addr
{
75 struct evpn_macip_addr
{
77 uint8_t ip_prefix_length
;
82 struct evpn_imet_addr
{
84 uint8_t ip_prefix_length
;
90 uint8_t ip_prefix_length
;
94 struct evpn_prefix_addr
{
96 uint8_t ip_prefix_length
;
100 /* EVPN address (RFC 7432) */
104 struct evpn_ead_addr _ead_addr
;
105 struct evpn_macip_addr _macip_addr
;
106 struct evpn_imet_addr _imet_addr
;
107 struct evpn_es_addr _es_addr
;
108 struct evpn_prefix_addr _prefix_addr
;
110 #define ead_addr u._ead_addr
111 #define macip_addr u._macip_addr
112 #define imet_addr u._imet_addr
113 #define es_addr u._es_addr
114 #define prefix_addr u._prefix_addr
118 * A struct prefix contains an address family, a prefix length, and an
119 * address. This can represent either a 'network prefix' as defined
120 * by CIDR, where the 'host bits' of the prefix are 0
121 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
122 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
126 /* different OSes use different names */
127 #if defined(AF_PACKET)
128 #define AF_ETHERNET AF_PACKET
131 #define AF_ETHERNET AF_LINK
135 /* The 'family' in the prefix structure is internal to FRR and need not
136 * map to standard OS AF_ definitions except where needed for interacting
137 * with the kernel. However, AF_ definitions are currently in use and
138 * prevalent across the code. Define a new FRR-specific AF for EVPN to
139 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
140 * ensure it does not conflict with any OS AF_ definition.
142 #if !defined(AF_EVPN)
143 #define AF_EVPN (AF_MAX + 1)
146 #if !defined(AF_FLOWSPEC)
147 #define AF_FLOWSPEC (AF_MAX + 2)
150 struct flowspec_prefix
{
151 uint16_t prefixlen
; /* length in bytes */
155 /* FRR generic prefix structure. */
161 struct in_addr prefix4
;
162 struct in6_addr prefix6
;
165 struct in_addr adv_router
;
167 struct ethaddr prefix_eth
; /* AF_ETHERNET */
171 struct evpn_addr prefix_evpn
; /* AF_EVPN */
172 struct flowspec_prefix prefix_flowspec
; /* AF_FLOWSPEC */
173 } u
__attribute__((aligned(8)));
176 /* IPv4 prefix structure. */
180 struct in_addr prefix
__attribute__((aligned(8)));
183 /* IPv6 prefix structure. */
187 struct in6_addr prefix
__attribute__((aligned(8)));
193 struct in_addr id
__attribute__((aligned(8)));
194 struct in_addr adv_router
;
197 /* Prefix for routing distinguisher. */
201 uint8_t val
[8] __attribute__((aligned(8)));
204 /* Prefix for ethernet. */
208 struct ethaddr eth_addr
__attribute__((aligned(8))); /* AF_ETHERNET */
211 /* EVPN prefix structure. */
215 struct evpn_addr prefix
__attribute__((aligned(8)));
218 static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn
*evp
)
220 if (evp
->prefix
.route_type
== 2)
221 return IS_IPADDR_NONE(&(evp
)->prefix
.macip_addr
.ip
);
222 if (evp
->prefix
.route_type
== 3)
223 return IS_IPADDR_NONE(&(evp
)->prefix
.imet_addr
.ip
);
224 if (evp
->prefix
.route_type
== 4)
225 return IS_IPADDR_NONE(&(evp
)->prefix
.es_addr
.ip
);
226 if (evp
->prefix
.route_type
== 5)
227 return IS_IPADDR_NONE(&(evp
)->prefix
.prefix_addr
.ip
);
231 static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn
*evp
)
233 if (evp
->prefix
.route_type
== 2)
234 return IS_IPADDR_V4(&(evp
)->prefix
.macip_addr
.ip
);
235 if (evp
->prefix
.route_type
== 3)
236 return IS_IPADDR_V4(&(evp
)->prefix
.imet_addr
.ip
);
237 if (evp
->prefix
.route_type
== 4)
238 return IS_IPADDR_V4(&(evp
)->prefix
.es_addr
.ip
);
239 if (evp
->prefix
.route_type
== 5)
240 return IS_IPADDR_V4(&(evp
)->prefix
.prefix_addr
.ip
);
244 static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn
*evp
)
246 if (evp
->prefix
.route_type
== 2)
247 return IS_IPADDR_V6(&(evp
)->prefix
.macip_addr
.ip
);
248 if (evp
->prefix
.route_type
== 3)
249 return IS_IPADDR_V6(&(evp
)->prefix
.imet_addr
.ip
);
250 if (evp
->prefix
.route_type
== 4)
251 return IS_IPADDR_V6(&(evp
)->prefix
.es_addr
.ip
);
252 if (evp
->prefix
.route_type
== 5)
253 return IS_IPADDR_V6(&(evp
)->prefix
.prefix_addr
.ip
);
257 /* Prefix for a generic pointer */
261 uintptr_t prefix
__attribute__((aligned(8)));
264 /* Prefix for a Flowspec entry */
267 uint16_t prefixlen
; /* unused */
268 struct flowspec_prefix prefix
__attribute__((aligned(8)));
274 struct in_addr src
__attribute__((aligned(8)));
278 /* helper to get type safety/avoid casts on calls
279 * (w/o this, functions accepting all prefix types need casts on the caller
280 * side, which strips type safety since the cast will accept any pointer
284 #define prefixtype(uname, typename, fieldname) \
287 #define prefixtype(uname, typename, fieldname) \
288 typename *fieldname; \
289 uname(typename *x) { this->fieldname = x; }
293 prefixtype(prefixptr
, struct prefix
, p
)
294 prefixtype(prefixptr
, struct prefix_ipv4
, p4
)
295 prefixtype(prefixptr
, struct prefix_ipv6
, p6
)
296 prefixtype(prefixptr
, struct prefix_evpn
, evp
)
297 prefixtype(prefixptr
, struct prefix_fs
, fs
)
298 } __attribute__((transparent_union
));
300 union prefixconstptr
{
301 prefixtype(prefixconstptr
, const struct prefix
, p
)
302 prefixtype(prefixconstptr
, const struct prefix_ipv4
, p4
)
303 prefixtype(prefixconstptr
, const struct prefix_ipv6
, p6
)
304 prefixtype(prefixconstptr
, const struct prefix_evpn
, evp
)
305 prefixtype(prefixconstptr
, const struct prefix_fs
, fs
)
306 } __attribute__((transparent_union
));
308 #ifndef INET_ADDRSTRLEN
309 #define INET_ADDRSTRLEN 16
310 #endif /* INET_ADDRSTRLEN */
312 #ifndef INET6_ADDRSTRLEN
313 /* dead:beef:dead:beef:dead:beef:dead:beef + \0 */
314 #define INET6_ADDRSTRLEN 46
315 #endif /* INET6_ADDRSTRLEN */
318 #define INET6_BUFSIZ 53
319 #endif /* INET6_BUFSIZ */
321 /* Maximum string length of the result of prefix2str */
322 #define PREFIX_STRLEN 80
325 * Longest possible length of a (S,G) string is 36 bytes
326 * 123.123.123.123 = 15 * 2
328 * NULL Character at end = 1
329 * (123.123.123.123,123.123.123.123)
331 #define PREFIX_SG_STR_LEN 34
333 /* Max bit/byte length of IPv4 address. */
334 #define IPV4_MAX_BYTELEN 4
335 #define IPV4_MAX_BITLEN 32
336 #define IPV4_MAX_PREFIXLEN 32
337 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
339 static inline bool ipv4_addr_same(const struct in_addr
*a
,
340 const struct in_addr
*b
)
342 return (a
->s_addr
== b
->s_addr
);
344 #define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
346 static inline void ipv4_addr_copy(struct in_addr
*dst
,
347 const struct in_addr
*src
)
349 dst
->s_addr
= src
->s_addr
;
351 #define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
353 #define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
354 #define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
355 #define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
356 #define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
357 #define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
359 /* Max bit/byte length of IPv6 address. */
360 #define IPV6_MAX_BYTELEN 16
361 #define IPV6_MAX_BITLEN 128
362 #define IPV6_MAX_PREFIXLEN 128
363 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
364 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
365 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
367 /* Count prefix size from mask length */
368 #define PSIZE(a) (((a) + 7) / (8))
370 #define BSIZE(a) ((a) * (8))
372 /* Prefix's family member. */
373 #define PREFIX_FAMILY(p) ((p)->family)
375 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
378 /* Some SunOS define s6_addr32 only to kernel */
379 #define s6_addr32 _S6_un._S6_u32
381 #define s6_addr32 __u6_addr.__u6_addr32
386 extern int str2family(const char *);
387 extern int afi2family(afi_t
);
388 extern afi_t
family2afi(int);
389 extern const char *family2str(int family
);
390 extern const char *safi2str(safi_t safi
);
391 extern const char *afi2str(afi_t afi
);
393 /* Check bit of the prefix. */
394 extern unsigned int prefix_bit(const uint8_t *prefix
, const uint16_t prefixlen
);
396 extern struct prefix
*prefix_new(void);
397 extern void prefix_free(struct prefix
**p
);
399 * Function to handle prefix_free being used as a del function.
401 extern void prefix_free_lists(void *arg
);
402 extern const char *prefix_family_str(const struct prefix
*);
403 extern int prefix_blen(const struct prefix
*);
404 extern int str2prefix(const char *, struct prefix
*);
406 #define PREFIX2STR_BUFFER PREFIX_STRLEN
408 extern void prefix_mcast_inet4_dump(const char *onfail
, struct in_addr addr
,
409 char *buf
, int buf_size
);
410 extern const char *prefix_sg2str(const struct prefix_sg
*sg
, char *str
);
411 extern const char *prefix2str(union prefixconstptr
, char *, int);
412 extern int evpn_type5_prefix_match(const struct prefix
*evpn_pfx
,
413 const struct prefix
*match_pfx
);
414 extern int prefix_match(const struct prefix
*, const struct prefix
*);
415 extern int prefix_match_network_statement(const struct prefix
*,
416 const struct prefix
*);
417 extern int prefix_same(union prefixconstptr
, union prefixconstptr
);
418 extern int prefix_cmp(union prefixconstptr
, union prefixconstptr
);
419 extern int prefix_common_bits(const struct prefix
*, const struct prefix
*);
420 extern void prefix_copy(union prefixptr
, union prefixconstptr
);
421 extern void apply_mask(struct prefix
*);
423 #ifdef __clang_analyzer__
424 /* clang-SA doesn't understand transparent unions, making it think that the
425 * target of prefix_copy is uninitialized. So just memset the target.
426 * cf. https://bugs.llvm.org/show_bug.cgi?id=42811
428 #define prefix_copy(a, b) ({ memset(a, 0, sizeof(*a)); prefix_copy(a, b); })
431 extern struct prefix
*sockunion2hostprefix(const union sockunion
*,
433 extern void prefix2sockunion(const struct prefix
*, union sockunion
*);
435 extern int str2prefix_eth(const char *, struct prefix_eth
*);
437 extern struct prefix_ipv4
*prefix_ipv4_new(void);
438 extern void prefix_ipv4_free(struct prefix_ipv4
**p
);
439 extern int str2prefix_ipv4(const char *, struct prefix_ipv4
*);
440 extern void apply_mask_ipv4(struct prefix_ipv4
*);
442 #define PREFIX_COPY(DST, SRC) \
443 *((struct prefix *)(DST)) = *((const struct prefix *)(SRC))
444 #define PREFIX_COPY_IPV4(DST, SRC) \
445 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
447 extern int prefix_ipv4_any(const struct prefix_ipv4
*);
448 extern void apply_classful_mask_ipv4(struct prefix_ipv4
*);
450 extern uint8_t ip_masklen(struct in_addr
);
451 extern void masklen2ip(const int, struct in_addr
*);
452 /* given the address of a host on a network and the network mask length,
453 * calculate the broadcast address for that network;
454 * special treatment for /31 according to RFC3021 section 3.3 */
455 extern in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
);
457 extern int netmask_str2prefix_str(const char *, const char *, char *);
459 extern struct prefix_ipv6
*prefix_ipv6_new(void);
460 extern void prefix_ipv6_free(struct prefix_ipv6
**p
);
461 extern int str2prefix_ipv6(const char *, struct prefix_ipv6
*);
462 extern void apply_mask_ipv6(struct prefix_ipv6
*);
464 #define PREFIX_COPY_IPV6(DST, SRC) \
465 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
467 extern int ip6_masklen(struct in6_addr
);
468 extern void masklen2ip6(const int, struct in6_addr
*);
470 extern const char *inet6_ntoa(struct in6_addr
);
472 extern int is_zero_mac(const struct ethaddr
*mac
);
473 extern bool is_mcast_mac(const struct ethaddr
*mac
);
474 extern bool is_bcast_mac(const struct ethaddr
*mac
);
475 extern int prefix_str2mac(const char *str
, struct ethaddr
*mac
);
476 extern char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
);
478 extern unsigned prefix_hash_key(const void *pp
);
480 extern int str_to_esi(const char *str
, esi_t
*esi
);
481 extern char *esi_to_str(const esi_t
*esi
, char *buf
, int size
);
482 extern void prefix_evpn_hexdump(const struct prefix_evpn
*p
);
484 static inline int ipv6_martian(struct in6_addr
*addr
)
486 struct in6_addr localhost_addr
;
488 inet_pton(AF_INET6
, "::1", &localhost_addr
);
490 if (IPV6_ADDR_SAME(&localhost_addr
, addr
))
496 extern int macstr2prefix_evpn(const char *str
, struct prefix_evpn
*p
);
498 /* NOTE: This routine expects the address argument in network byte order. */
499 static inline int ipv4_martian(struct in_addr
*addr
)
501 in_addr_t ip
= ntohl(addr
->s_addr
);
503 if (IPV4_NET0(ip
) || IPV4_NET127(ip
) || IPV4_CLASS_DE(ip
)) {
509 static inline int is_default_prefix(const struct prefix
*p
)
514 if ((p
->family
== AF_INET
) && (p
->u
.prefix4
.s_addr
== INADDR_ANY
)
515 && (p
->prefixlen
== 0))
518 if ((p
->family
== AF_INET6
) && (p
->prefixlen
== 0)
519 && (!memcmp(&p
->u
.prefix6
, &in6addr_any
, sizeof(struct in6_addr
))))
525 static inline int is_host_route(const struct prefix
*p
)
527 if (p
->family
== AF_INET
)
528 return (p
->prefixlen
== IPV4_MAX_BITLEN
);
529 else if (p
->family
== AF_INET6
)
530 return (p
->prefixlen
== IPV6_MAX_BITLEN
);
534 static inline int is_default_host_route(const struct prefix
*p
)
536 if (p
->family
== AF_INET
) {
537 return (p
->u
.prefix4
.s_addr
== INADDR_ANY
&&
538 p
->prefixlen
== IPV4_MAX_BITLEN
);
539 } else if (p
->family
== AF_INET6
) {
540 return ((!memcmp(&p
->u
.prefix6
, &in6addr_any
,
541 sizeof(struct in6_addr
))) &&
542 p
->prefixlen
== IPV6_MAX_BITLEN
);
547 #ifdef _FRR_ATTRIBUTE_PRINTFRR
548 #pragma FRR printfrr_ext "%pI4" (struct in_addr *)
549 #pragma FRR printfrr_ext "%pI4" (in_addr_t *)
551 #pragma FRR printfrr_ext "%pI6" (struct in6_addr *)
553 #pragma FRR printfrr_ext "%pFX" (struct prefix *)
554 #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv4 *)
555 #pragma FRR printfrr_ext "%pFX" (struct prefix_ipv6 *)
556 #pragma FRR printfrr_ext "%pFX" (struct prefix_eth *)
557 #pragma FRR printfrr_ext "%pFX" (struct prefix_evpn *)
558 #pragma FRR printfrr_ext "%pFX" (struct prefix_fs *)
560 #pragma FRR printfrr_ext "%pSG4" (struct prefix_sg *)
567 #endif /* _ZEBRA_PREFIX_H */