]>
git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.h
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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #ifndef _ZEBRA_PREFIX_H
24 #define _ZEBRA_PREFIX_H
27 # include <sys/ethernet.h>
30 # include <net/ethernet.h>
32 # include <netinet/if_ether.h>
35 #include "sockunion.h"
37 #ifndef ETHER_ADDR_LEN
38 #define ETHER_ADDR_LEN ETHERADDRL
42 * there isn't a portable ethernet address type. We define our
43 * own to simplify internal handling
46 u_char octet
[ETHER_ADDR_LEN
];
51 * A struct prefix contains an address family, a prefix length, and an
52 * address. This can represent either a 'network prefix' as defined
53 * by CIDR, where the 'host bits' of the prefix are 0
54 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
55 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
59 /* different OSes use different names */
60 #if defined(AF_PACKET)
61 #define AF_ETHERNET AF_PACKET
64 #define AF_ETHERNET AF_LINK
68 /* IPv4 and IPv6 unified prefix structure. */
76 struct in_addr prefix4
;
77 struct in6_addr prefix6
;
81 struct in_addr adv_router
;
83 struct ethaddr prefix_eth
; /* AF_ETHERNET */
86 } u
__attribute__ ((aligned (8)));
89 /* IPv4 prefix structure. */
94 struct in_addr prefix
__attribute__ ((aligned (8)));
97 /* IPv6 prefix structure. */
102 struct in6_addr prefix
__attribute__ ((aligned (8)));
109 struct in_addr id
__attribute__ ((aligned (8)));
110 struct in_addr adv_router
;
113 /* Prefix for routing distinguisher. */
118 u_char val
[8] __attribute__ ((aligned (8)));
121 /* Prefix for ethernet. */
126 struct ethaddr eth_addr
__attribute__ ((aligned (8))); /* AF_ETHERNET */
129 /* Prefix for a generic pointer */
134 uintptr_t prefix
__attribute__ ((aligned (8)));
137 /* helper to get type safety/avoid casts on calls
138 * (w/o this, functions accepting all prefix types need casts on the caller
139 * side, which strips type safety since the cast will accept any pointer
145 struct prefix_ipv4
*p4
;
146 struct prefix_ipv6
*p6
;
147 } __attribute__ ((transparent_union
));
149 union prefix46constptr
151 const struct prefix
*p
;
152 const struct prefix_ipv4
*p4
;
153 const struct prefix_ipv6
*p6
;
154 } __attribute__ ((transparent_union
));
156 #ifndef INET_ADDRSTRLEN
157 #define INET_ADDRSTRLEN 16
158 #endif /* INET_ADDRSTRLEN */
160 #ifndef INET6_ADDRSTRLEN
161 #define INET6_ADDRSTRLEN 46
162 #endif /* INET6_ADDRSTRLEN */
165 #define INET6_BUFSIZ 51
166 #endif /* INET6_BUFSIZ */
168 /* Maximum prefix string length (IPv6) */
169 #define PREFIX_STRLEN 51
171 /* Max bit/byte length of IPv4 address. */
172 #define IPV4_MAX_BYTELEN 4
173 #define IPV4_MAX_BITLEN 32
174 #define IPV4_MAX_PREFIXLEN 32
175 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
176 #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0)
177 #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN)
179 #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
180 #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
181 #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
182 #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
184 /* Max bit/byte length of IPv6 address. */
185 #define IPV6_MAX_BYTELEN 16
186 #define IPV6_MAX_BITLEN 128
187 #define IPV6_MAX_PREFIXLEN 128
188 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
189 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
190 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
192 /* Count prefix size from mask length */
193 #define PSIZE(a) (((a) + 7) / (8))
195 /* Prefix's family member. */
196 #define PREFIX_FAMILY(p) ((p)->family)
198 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
201 /* Some SunOS define s6_addr32 only to kernel */
202 #define s6_addr32 _S6_un._S6_u32
204 #define s6_addr32 __u6_addr.__u6_addr32
209 extern int str2family(const char *);
210 extern int afi2family (afi_t
);
211 extern afi_t
family2afi (int);
212 extern const char *safi2str(safi_t safi
);
213 extern const char *afi2str(afi_t afi
);
215 /* Check bit of the prefix. */
216 extern unsigned int prefix_bit (const u_char
*prefix
, const u_char prefixlen
);
217 extern unsigned int prefix6_bit (const struct in6_addr
*prefix
, const u_char prefixlen
);
219 extern struct prefix
*prefix_new (void);
220 extern void prefix_free (struct prefix
*);
221 extern const char *prefix_family_str (const struct prefix
*);
222 extern int prefix_blen (const struct prefix
*);
223 extern int str2prefix (const char *, struct prefix
*);
226 * 8 groups of 4 bytes of hexadecimal + 7 seperators is 39
229 * 39 + 4 + 1 = 44 bytes
231 #define PREFIX2STR_BUFFER 44
232 extern const char *prefix2str (union prefix46constptr
, char *, int);
233 extern int prefix_match (const struct prefix
*, const struct prefix
*);
234 extern int prefix_same (const struct prefix
*, const struct prefix
*);
235 extern int prefix_cmp (const struct prefix
*, const struct prefix
*);
236 extern int prefix_common_bits (const struct prefix
*, const struct prefix
*);
237 extern void prefix_copy (struct prefix
*dest
, const struct prefix
*src
);
238 extern void apply_mask (struct prefix
*);
240 extern struct prefix
*sockunion2prefix (const union sockunion
*dest
,
241 const union sockunion
*mask
);
242 extern struct prefix
*sockunion2hostprefix (const union sockunion
*, struct prefix
*p
);
243 extern void prefix2sockunion (const struct prefix
*, union sockunion
*);
245 extern int str2prefix_eth (const char *, struct prefix_eth
*);
247 extern struct prefix_ipv4
*prefix_ipv4_new (void);
248 extern void prefix_ipv4_free (struct prefix_ipv4
*);
249 extern int str2prefix_ipv4 (const char *, struct prefix_ipv4
*);
250 extern void apply_mask_ipv4 (struct prefix_ipv4
*);
252 #define PREFIX_COPY_IPV4(DST, SRC) \
253 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
255 extern int prefix_ipv4_any (const struct prefix_ipv4
*);
256 extern void apply_classful_mask_ipv4 (struct prefix_ipv4
*);
258 extern u_char
ip_masklen (struct in_addr
);
259 extern void masklen2ip (const int, struct in_addr
*);
260 /* returns the network portion of the host address */
261 extern in_addr_t
ipv4_network_addr (in_addr_t hostaddr
, int masklen
);
262 /* given the address of a host on a network and the network mask length,
263 * calculate the broadcast address for that network;
264 * special treatment for /31: returns the address of the other host
265 * on the network by flipping the host bit */
266 extern in_addr_t
ipv4_broadcast_addr (in_addr_t hostaddr
, int masklen
);
268 extern int netmask_str2prefix_str (const char *, const char *, char *);
270 extern struct prefix_ipv6
*prefix_ipv6_new (void);
271 extern void prefix_ipv6_free (struct prefix_ipv6
*);
272 extern int str2prefix_ipv6 (const char *, struct prefix_ipv6
*);
273 extern void apply_mask_ipv6 (struct prefix_ipv6
*);
275 #define PREFIX_COPY_IPV6(DST, SRC) \
276 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
278 extern int ip6_masklen (struct in6_addr
);
279 extern void masklen2ip6 (const int, struct in6_addr
*);
281 extern void str2in6_addr (const char *, struct in6_addr
*);
282 extern const char *inet6_ntoa (struct in6_addr
);
284 static inline int ipv6_martian (struct in6_addr
*addr
)
286 struct in6_addr localhost_addr
;
288 inet_pton (AF_INET6
, "::1", &localhost_addr
);
290 if (IPV6_ADDR_SAME(&localhost_addr
, addr
))
296 extern int all_digit (const char *);
298 /* NOTE: This routine expects the address argument in network byte order. */
299 static inline int ipv4_martian (struct in_addr
*addr
)
301 in_addr_t ip
= ntohl(addr
->s_addr
);
303 if (IPV4_NET0(ip
) || IPV4_NET127(ip
) || IPV4_CLASS_DE(ip
)) {
310 is_default_prefix (struct prefix
*p
)
315 if (((p
->family
== AF_INET
) && (p
->u
.prefix4
.s_addr
== INADDR_ANY
))
316 || ((p
->family
== AF_INET6
) &&
317 !memcmp(&p
->u
.prefix6
, &in6addr_any
, sizeof (struct in6_addr
))))
323 #endif /* _ZEBRA_PREFIX_H */