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"
43 #define ESI_STR_LEN (3 * ESI_BYTES)
45 #define ETHER_ADDR_STRLEN (3*ETH_ALEN)
47 * there isn't a portable ethernet address type. We define our
48 * own to simplify internal handling
51 uint8_t octet
[ETH_ALEN
];
52 } __attribute__((packed
));
55 /* length is the number of valuable bits of prefix structure
56 * 18 bytes is current length in structure, if address is ipv4
57 * 30 bytes is in case of ipv6
59 #define PREFIX_LEN_ROUTE_TYPE_5_IPV4 (18*8)
60 #define PREFIX_LEN_ROUTE_TYPE_5_IPV6 (30*8)
62 typedef struct esi_t_
{
66 struct evpn_ead_addr
{
71 struct evpn_macip_addr
{
73 uint8_t ip_prefix_length
;
78 struct evpn_imet_addr
{
80 uint8_t ip_prefix_length
;
86 uint8_t ip_prefix_length
;
90 struct evpn_prefix_addr
{
92 uint8_t ip_prefix_length
;
96 /* EVPN address (RFC 7432) */
100 struct evpn_ead_addr _ead_addr
;
101 struct evpn_macip_addr _macip_addr
;
102 struct evpn_imet_addr _imet_addr
;
103 struct evpn_es_addr _es_addr
;
104 struct evpn_prefix_addr _prefix_addr
;
106 #define ead_addr u._ead_addr
107 #define macip_addr u._macip_addr
108 #define imet_addr u._imet_addr
109 #define es_addr u._es_addr
110 #define prefix_addr u._prefix_addr
114 * A struct prefix contains an address family, a prefix length, and an
115 * address. This can represent either a 'network prefix' as defined
116 * by CIDR, where the 'host bits' of the prefix are 0
117 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
118 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
122 /* different OSes use different names */
123 #if defined(AF_PACKET)
124 #define AF_ETHERNET AF_PACKET
127 #define AF_ETHERNET AF_LINK
131 /* The 'family' in the prefix structure is internal to FRR and need not
132 * map to standard OS AF_ definitions except where needed for interacting
133 * with the kernel. However, AF_ definitions are currently in use and
134 * prevalent across the code. Define a new FRR-specific AF for EVPN to
135 * distinguish between 'ethernet' (MAC-only) and 'evpn' prefixes and
136 * ensure it does not conflict with any OS AF_ definition.
138 #if !defined(AF_EVPN)
139 #define AF_EVPN (AF_MAX + 1)
142 #if !defined(AF_FLOWSPEC)
143 #define AF_FLOWSPEC (AF_MAX + 2)
146 struct flowspec_prefix
{
147 uint16_t prefixlen
; /* length in bytes */
151 /* FRR generic prefix structure. */
157 struct in_addr prefix4
;
158 struct in6_addr prefix6
;
161 struct in_addr adv_router
;
163 struct ethaddr prefix_eth
; /* AF_ETHERNET */
166 struct evpn_addr prefix_evpn
; /* AF_EVPN */
167 struct flowspec_prefix prefix_flowspec
; /* AF_FLOWSPEC */
168 } u
__attribute__((aligned(8)));
171 /* IPv4 prefix structure. */
175 struct in_addr prefix
__attribute__((aligned(8)));
178 /* IPv6 prefix structure. */
182 struct in6_addr prefix
__attribute__((aligned(8)));
188 struct in_addr id
__attribute__((aligned(8)));
189 struct in_addr adv_router
;
192 /* Prefix for routing distinguisher. */
196 uint8_t val
[8] __attribute__((aligned(8)));
199 /* Prefix for ethernet. */
203 struct ethaddr eth_addr
__attribute__((aligned(8))); /* AF_ETHERNET */
206 /* EVPN prefix structure. */
210 struct evpn_addr prefix
__attribute__((aligned(8)));
213 static inline int is_evpn_prefix_ipaddr_none(const struct prefix_evpn
*evp
)
215 if (evp
->prefix
.route_type
== 2)
216 return IS_IPADDR_NONE(&(evp
)->prefix
.macip_addr
.ip
);
217 if (evp
->prefix
.route_type
== 3)
218 return IS_IPADDR_NONE(&(evp
)->prefix
.imet_addr
.ip
);
219 if (evp
->prefix
.route_type
== 4)
220 return IS_IPADDR_NONE(&(evp
)->prefix
.es_addr
.ip
);
221 if (evp
->prefix
.route_type
== 5)
222 return IS_IPADDR_NONE(&(evp
)->prefix
.prefix_addr
.ip
);
226 static inline int is_evpn_prefix_ipaddr_v4(const struct prefix_evpn
*evp
)
228 if (evp
->prefix
.route_type
== 2)
229 return IS_IPADDR_V4(&(evp
)->prefix
.macip_addr
.ip
);
230 if (evp
->prefix
.route_type
== 3)
231 return IS_IPADDR_V4(&(evp
)->prefix
.imet_addr
.ip
);
232 if (evp
->prefix
.route_type
== 4)
233 return IS_IPADDR_V4(&(evp
)->prefix
.es_addr
.ip
);
234 if (evp
->prefix
.route_type
== 5)
235 return IS_IPADDR_V4(&(evp
)->prefix
.prefix_addr
.ip
);
239 static inline int is_evpn_prefix_ipaddr_v6(const struct prefix_evpn
*evp
)
241 if (evp
->prefix
.route_type
== 2)
242 return IS_IPADDR_V6(&(evp
)->prefix
.macip_addr
.ip
);
243 if (evp
->prefix
.route_type
== 3)
244 return IS_IPADDR_V6(&(evp
)->prefix
.imet_addr
.ip
);
245 if (evp
->prefix
.route_type
== 4)
246 return IS_IPADDR_V6(&(evp
)->prefix
.es_addr
.ip
);
247 if (evp
->prefix
.route_type
== 5)
248 return IS_IPADDR_V6(&(evp
)->prefix
.prefix_addr
.ip
);
252 /* Prefix for a generic pointer */
256 uintptr_t prefix
__attribute__((aligned(8)));
259 /* Prefix for a Flowspec entry */
262 uint8_t prefixlen
; /* unused */
263 struct flowspec_prefix prefix
__attribute__((aligned(8)));
269 struct in_addr src
__attribute__((aligned(8)));
273 /* helper to get type safety/avoid casts on calls
274 * (w/o this, functions accepting all prefix types need casts on the caller
275 * side, which strips type safety since the cast will accept any pointer
280 struct prefix_ipv4
*p4
;
281 struct prefix_ipv6
*p6
;
282 struct prefix_evpn
*evp
;
283 const struct prefix_fs
*fs
;
284 } __attribute__((transparent_union
));
286 union prefixconstptr
{
287 const struct prefix
*p
;
288 const struct prefix_ipv4
*p4
;
289 const struct prefix_ipv6
*p6
;
290 const struct prefix_evpn
*evp
;
291 const struct prefix_fs
*fs
;
292 } __attribute__((transparent_union
));
294 #ifndef INET_ADDRSTRLEN
295 #define INET_ADDRSTRLEN 16
296 #endif /* INET_ADDRSTRLEN */
298 #ifndef INET6_ADDRSTRLEN
299 #define INET6_ADDRSTRLEN 46
300 #endif /* INET6_ADDRSTRLEN */
303 #define INET6_BUFSIZ 51
304 #endif /* INET6_BUFSIZ */
306 /* Maximum prefix string length (IPv6) */
307 #define PREFIX_STRLEN 51
309 /* Max bit/byte length of IPv4 address. */
310 #define IPV4_MAX_BYTELEN 4
311 #define IPV4_MAX_BITLEN 32
312 #define IPV4_MAX_PREFIXLEN 32
313 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
315 static inline bool ipv4_addr_same(const struct in_addr
*a
,
316 const struct in_addr
*b
)
318 return (a
->s_addr
== b
->s_addr
);
320 #define IPV4_ADDR_SAME(A,B) ipv4_addr_same((A), (B))
322 static inline void ipv4_addr_copy(struct in_addr
*dst
,
323 const struct in_addr
*src
)
325 dst
->s_addr
= src
->s_addr
;
327 #define IPV4_ADDR_COPY(D,S) ipv4_addr_copy((D), (S))
329 #define IPV4_NET0(a) ((((uint32_t)(a)) & 0xff000000) == 0x00000000)
330 #define IPV4_NET127(a) ((((uint32_t)(a)) & 0xff000000) == 0x7f000000)
331 #define IPV4_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffff0000) == 0xa9fe0000)
332 #define IPV4_CLASS_DE(a) ((((uint32_t)(a)) & 0xe0000000) == 0xe0000000)
333 #define IPV4_MC_LINKLOCAL(a) ((((uint32_t)(a)) & 0xffffff00) == 0xe0000000)
335 /* Max bit/byte length of IPv6 address. */
336 #define IPV6_MAX_BYTELEN 16
337 #define IPV6_MAX_BITLEN 128
338 #define IPV6_MAX_PREFIXLEN 128
339 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
340 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
341 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
343 /* Count prefix size from mask length */
344 #define PSIZE(a) (((a) + 7) / (8))
346 #define BSIZE(a) ((a) * (8))
348 /* Prefix's family member. */
349 #define PREFIX_FAMILY(p) ((p)->family)
351 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
354 /* Some SunOS define s6_addr32 only to kernel */
355 #define s6_addr32 _S6_un._S6_u32
357 #define s6_addr32 __u6_addr.__u6_addr32
362 extern int str2family(const char *);
363 extern int afi2family(afi_t
);
364 extern afi_t
family2afi(int);
365 extern const char *family2str(int family
);
366 extern const char *safi2str(safi_t safi
);
367 extern const char *afi2str(afi_t afi
);
369 /* Check bit of the prefix. */
370 extern unsigned int prefix_bit(const uint8_t *prefix
, const uint8_t prefixlen
);
371 extern unsigned int prefix6_bit(const struct in6_addr
*prefix
,
372 const uint8_t prefixlen
);
374 extern struct prefix
*prefix_new(void);
375 extern void prefix_free(struct prefix
*);
376 extern const char *prefix_family_str(const struct prefix
*);
377 extern int prefix_blen(const struct prefix
*);
378 extern int str2prefix(const char *, struct prefix
*);
380 #define PREFIX2STR_BUFFER PREFIX_STRLEN
382 extern const char *prefix2str(union prefixconstptr
, char *, int);
383 extern int prefix_match(const struct prefix
*, const struct prefix
*);
384 extern int prefix_match_network_statement(const struct prefix
*,
385 const struct prefix
*);
386 extern int prefix_same(const struct prefix
*, const struct prefix
*);
387 extern int prefix_cmp(const struct prefix
*, const struct prefix
*);
388 extern int prefix_common_bits(const struct prefix
*, const struct prefix
*);
389 extern void prefix_copy(struct prefix
*dest
, const struct prefix
*src
);
390 extern void apply_mask(struct prefix
*);
392 extern struct prefix
*sockunion2prefix(const union sockunion
*dest
,
393 const union sockunion
*mask
);
394 extern struct prefix
*sockunion2hostprefix(const union sockunion
*,
396 extern void prefix2sockunion(const struct prefix
*, union sockunion
*);
398 extern int str2prefix_eth(const char *, struct prefix_eth
*);
400 extern struct prefix_ipv4
*prefix_ipv4_new(void);
401 extern void prefix_ipv4_free(struct prefix_ipv4
*);
402 extern int str2prefix_ipv4(const char *, struct prefix_ipv4
*);
403 extern void apply_mask_ipv4(struct prefix_ipv4
*);
405 #define PREFIX_COPY(DST, SRC) \
406 *((struct prefix *)(DST)) = *((const struct prefix *)(SRC))
407 #define PREFIX_COPY_IPV4(DST, SRC) \
408 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
410 extern int prefix_ipv4_any(const struct prefix_ipv4
*);
411 extern void apply_classful_mask_ipv4(struct prefix_ipv4
*);
413 extern uint8_t ip_masklen(struct in_addr
);
414 extern void masklen2ip(const int, struct in_addr
*);
415 /* returns the network portion of the host address */
416 extern in_addr_t
ipv4_network_addr(in_addr_t hostaddr
, int masklen
);
417 /* given the address of a host on a network and the network mask length,
418 * calculate the broadcast address for that network;
419 * special treatment for /31: returns the address of the other host
420 * on the network by flipping the host bit */
421 extern in_addr_t
ipv4_broadcast_addr(in_addr_t hostaddr
, int masklen
);
423 extern int netmask_str2prefix_str(const char *, const char *, char *);
425 extern struct prefix_ipv6
*prefix_ipv6_new(void);
426 extern void prefix_ipv6_free(struct prefix_ipv6
*);
427 extern int str2prefix_ipv6(const char *, struct prefix_ipv6
*);
428 extern void apply_mask_ipv6(struct prefix_ipv6
*);
430 #define PREFIX_COPY_IPV6(DST, SRC) \
431 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
433 extern int ip6_masklen(struct in6_addr
);
434 extern void masklen2ip6(const int, struct in6_addr
*);
436 extern const char *inet6_ntoa(struct in6_addr
);
438 extern int is_zero_mac(struct ethaddr
*mac
);
439 extern int prefix_str2mac(const char *str
, struct ethaddr
*mac
);
440 extern char *prefix_mac2str(const struct ethaddr
*mac
, char *buf
, int size
);
442 extern unsigned prefix_hash_key(void *pp
);
444 extern int str_to_esi(const char *str
, esi_t
*esi
);
445 extern char *esi_to_str(const esi_t
*esi
, char *buf
, int size
);
446 extern void prefix_hexdump(const struct prefix
*p
);
447 extern void prefix_evpn_hexdump(const struct prefix_evpn
*p
);
449 static inline int ipv6_martian(struct in6_addr
*addr
)
451 struct in6_addr localhost_addr
;
453 inet_pton(AF_INET6
, "::1", &localhost_addr
);
455 if (IPV6_ADDR_SAME(&localhost_addr
, addr
))
461 extern int macstr2prefix_evpn(const char *str
, struct prefix_evpn
*p
);
463 /* NOTE: This routine expects the address argument in network byte order. */
464 static inline int ipv4_martian(struct in_addr
*addr
)
466 in_addr_t ip
= ntohl(addr
->s_addr
);
468 if (IPV4_NET0(ip
) || IPV4_NET127(ip
) || IPV4_CLASS_DE(ip
)) {
474 static inline int is_default_prefix(const struct prefix
*p
)
479 if ((p
->family
== AF_INET
) && (p
->u
.prefix4
.s_addr
== INADDR_ANY
)
480 && (p
->prefixlen
== 0))
483 if ((p
->family
== AF_INET6
) && (p
->prefixlen
== 0)
484 && (!memcmp(&p
->u
.prefix6
, &in6addr_any
, sizeof(struct in6_addr
))))
490 static inline int is_host_route(struct prefix
*p
)
492 if (p
->family
== AF_INET
)
493 return (p
->prefixlen
== IPV4_MAX_BITLEN
);
494 else if (p
->family
== AF_INET6
)
495 return (p
->prefixlen
== IPV6_MAX_BITLEN
);
498 #endif /* _ZEBRA_PREFIX_H */