]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Prefix structure. | |
3 | * Copyright (C) 1998 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
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 | |
10 | * later version. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #ifndef _ZEBRA_PREFIX_H | |
24 | #define _ZEBRA_PREFIX_H | |
25 | ||
8cc4198f | 26 | #include "sockunion.h" |
27 | ||
9d24baaa | 28 | /* |
29 | * A struct prefix contains an address family, a prefix length, and an | |
30 | * address. This can represent either a 'network prefix' as defined | |
31 | * by CIDR, where the 'host bits' of the prefix are 0 | |
32 | * (e.g. AF_INET:10.0.0.0/8), or an address and netmask | |
33 | * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an | |
34 | * interface. | |
35 | */ | |
36 | ||
718e3744 | 37 | /* IPv4 and IPv6 unified prefix structure. */ |
38 | struct prefix | |
39 | { | |
40 | u_char family; | |
41 | u_char prefixlen; | |
42 | union | |
43 | { | |
44 | u_char prefix; | |
45 | struct in_addr prefix4; | |
46 | #ifdef HAVE_IPV6 | |
47 | struct in6_addr prefix6; | |
48 | #endif /* HAVE_IPV6 */ | |
49 | struct | |
50 | { | |
51 | struct in_addr id; | |
52 | struct in_addr adv_router; | |
53 | } lp; | |
54 | u_char val[8]; | |
b4b359a2 | 55 | uintptr_t ptr; |
718e3744 | 56 | } u __attribute__ ((aligned (8))); |
57 | }; | |
58 | ||
59 | /* IPv4 prefix structure. */ | |
60 | struct prefix_ipv4 | |
61 | { | |
62 | u_char family; | |
63 | u_char prefixlen; | |
64 | struct in_addr prefix __attribute__ ((aligned (8))); | |
65 | }; | |
66 | ||
67 | /* IPv6 prefix structure. */ | |
68 | #ifdef HAVE_IPV6 | |
69 | struct prefix_ipv6 | |
70 | { | |
71 | u_char family; | |
72 | u_char prefixlen; | |
73 | struct in6_addr prefix __attribute__ ((aligned (8))); | |
74 | }; | |
75 | #endif /* HAVE_IPV6 */ | |
76 | ||
77 | struct prefix_ls | |
78 | { | |
79 | u_char family; | |
80 | u_char prefixlen; | |
81 | struct in_addr id __attribute__ ((aligned (8))); | |
82 | struct in_addr adv_router; | |
83 | }; | |
84 | ||
85 | /* Prefix for routing distinguisher. */ | |
86 | struct prefix_rd | |
87 | { | |
88 | u_char family; | |
89 | u_char prefixlen; | |
90 | u_char val[8] __attribute__ ((aligned (8))); | |
91 | }; | |
92 | ||
b4b359a2 PM |
93 | /* Prefix for a generic pointer */ |
94 | struct prefix_ptr | |
95 | { | |
96 | u_char family; | |
97 | u_char prefixlen; | |
98 | uintptr_t prefix __attribute__ ((aligned (8))); | |
99 | }; | |
100 | ||
f7bf4153 DL |
101 | /* helper to get type safety/avoid casts on calls |
102 | * (w/o this, functions accepting all prefix types need casts on the caller | |
103 | * side, which strips type safety since the cast will accept any pointer | |
104 | * type.) | |
105 | */ | |
106 | union prefix46ptr | |
107 | { | |
108 | struct prefix *p; | |
109 | struct prefix_ipv4 *p4; | |
110 | struct prefix_ipv6 *p6; | |
111 | } __attribute__ ((transparent_union)); | |
112 | ||
113 | union prefix46constptr | |
114 | { | |
115 | const struct prefix *p; | |
116 | const struct prefix_ipv4 *p4; | |
117 | const struct prefix_ipv6 *p6; | |
118 | } __attribute__ ((transparent_union)); | |
119 | ||
718e3744 | 120 | #ifndef INET_ADDRSTRLEN |
121 | #define INET_ADDRSTRLEN 16 | |
122 | #endif /* INET_ADDRSTRLEN */ | |
123 | ||
124 | #ifndef INET6_ADDRSTRLEN | |
125 | #define INET6_ADDRSTRLEN 46 | |
126 | #endif /* INET6_ADDRSTRLEN */ | |
127 | ||
128 | #ifndef INET6_BUFSIZ | |
129 | #define INET6_BUFSIZ 51 | |
130 | #endif /* INET6_BUFSIZ */ | |
131 | ||
132 | /* Max bit/byte length of IPv4 address. */ | |
133 | #define IPV4_MAX_BYTELEN 4 | |
134 | #define IPV4_MAX_BITLEN 32 | |
135 | #define IPV4_MAX_PREFIXLEN 32 | |
136 | #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) | |
137 | #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) | |
138 | #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) | |
139 | ||
140 | #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) | |
141 | #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) | |
6dc686a2 | 142 | #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000) |
733cd9e5 | 143 | #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000) |
718e3744 | 144 | |
145 | /* Max bit/byte length of IPv6 address. */ | |
146 | #define IPV6_MAX_BYTELEN 16 | |
147 | #define IPV6_MAX_BITLEN 128 | |
148 | #define IPV6_MAX_PREFIXLEN 128 | |
149 | #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) | |
150 | #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) | |
151 | #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) | |
152 | ||
153 | /* Count prefix size from mask length */ | |
154 | #define PSIZE(a) (((a) + 7) / (8)) | |
155 | ||
156 | /* Prefix's family member. */ | |
157 | #define PREFIX_FAMILY(p) ((p)->family) | |
158 | ||
5c6ed111 DL |
159 | /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */ |
160 | #ifndef s6_addr32 | |
161 | #if defined(SUNOS_5) | |
162 | /* Some SunOS define s6_addr32 only to kernel */ | |
163 | #define s6_addr32 _S6_un._S6_u32 | |
164 | #else | |
165 | #define s6_addr32 __u6_addr.__u6_addr32 | |
166 | #endif /* SUNOS_5 */ | |
167 | #endif /*s6_addr32*/ | |
168 | ||
718e3744 | 169 | /* Prototypes. */ |
f3ccedaa | 170 | extern int str2family(const char *); |
4c9641ba ML |
171 | extern int afi2family (afi_t); |
172 | extern afi_t family2afi (int); | |
1ec23d90 | 173 | extern const char *safi2str(safi_t safi); |
8cc4198f | 174 | |
f63f06da PJ |
175 | /* Check bit of the prefix. */ |
176 | extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen); | |
177 | extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen); | |
178 | ||
8cc4198f | 179 | extern struct prefix *prefix_new (void); |
180 | extern void prefix_free (struct prefix *); | |
181 | extern const char *prefix_family_str (const struct prefix *); | |
182 | extern int prefix_blen (const struct prefix *); | |
183 | extern int str2prefix (const char *, struct prefix *); | |
4690c7d7 DS |
184 | |
185 | /* | |
186 | * 8 groups of 4 bytes of hexadecimal + 7 seperators is 39 | |
187 | * /128 = 4 bytes | |
188 | * Null = 1 byte | |
189 | * 39 + 4 + 1 = 44 bytes | |
190 | */ | |
191 | #define PREFIX2STR_BUFFER 44 | |
78104b9b | 192 | extern const char *prefix2str (union prefix46constptr, char *, int); |
8cc4198f | 193 | extern int prefix_match (const struct prefix *, const struct prefix *); |
194 | extern int prefix_same (const struct prefix *, const struct prefix *); | |
195 | extern int prefix_cmp (const struct prefix *, const struct prefix *); | |
17e52061 | 196 | extern int prefix_common_bits (const struct prefix *, const struct prefix *); |
8cc4198f | 197 | extern void prefix_copy (struct prefix *dest, const struct prefix *src); |
198 | extern void apply_mask (struct prefix *); | |
199 | ||
200 | extern struct prefix *sockunion2prefix (const union sockunion *dest, | |
201 | const union sockunion *mask); | |
40ee54a7 | 202 | extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p); |
17e52061 | 203 | extern void prefix2sockunion (const struct prefix *, union sockunion *); |
8cc4198f | 204 | |
205 | extern struct prefix_ipv4 *prefix_ipv4_new (void); | |
206 | extern void prefix_ipv4_free (struct prefix_ipv4 *); | |
207 | extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *); | |
208 | extern void apply_mask_ipv4 (struct prefix_ipv4 *); | |
209 | ||
e4529636 AS |
210 | #define PREFIX_COPY_IPV4(DST, SRC) \ |
211 | *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC)); | |
212 | ||
8cc4198f | 213 | extern int prefix_ipv4_any (const struct prefix_ipv4 *); |
214 | extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *); | |
215 | ||
216 | extern u_char ip_masklen (struct in_addr); | |
9663386f | 217 | extern void masklen2ip (const int, struct in_addr *); |
3fb9cd6e | 218 | /* returns the network portion of the host address */ |
8cc4198f | 219 | extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen); |
3fb9cd6e | 220 | /* given the address of a host on a network and the network mask length, |
221 | * calculate the broadcast address for that network; | |
222 | * special treatment for /31: returns the address of the other host | |
223 | * on the network by flipping the host bit */ | |
8cc4198f | 224 | extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen); |
3fb9cd6e | 225 | |
8cc4198f | 226 | extern int netmask_str2prefix_str (const char *, const char *, char *); |
718e3744 | 227 | |
228 | #ifdef HAVE_IPV6 | |
8cc4198f | 229 | extern struct prefix_ipv6 *prefix_ipv6_new (void); |
230 | extern void prefix_ipv6_free (struct prefix_ipv6 *); | |
231 | extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *); | |
232 | extern void apply_mask_ipv6 (struct prefix_ipv6 *); | |
718e3744 | 233 | |
e4529636 AS |
234 | #define PREFIX_COPY_IPV6(DST, SRC) \ |
235 | *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC)); | |
236 | ||
8cc4198f | 237 | extern int ip6_masklen (struct in6_addr); |
21f569e3 | 238 | extern void masklen2ip6 (const int, struct in6_addr *); |
b04c699e | 239 | |
8cc4198f | 240 | extern void str2in6_addr (const char *, struct in6_addr *); |
241 | extern const char *inet6_ntoa (struct in6_addr); | |
5920990f | 242 | |
d914d5ff DS |
243 | static inline int ipv6_martian (struct in6_addr *addr) |
244 | { | |
245 | struct in6_addr localhost_addr; | |
246 | ||
247 | inet_pton (AF_INET6, "::1", &localhost_addr); | |
248 | ||
249 | if (IPV6_ADDR_SAME(&localhost_addr, addr)) | |
250 | return 1; | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
b04c699e | 255 | #endif /* HAVE_IPV6 */ |
718e3744 | 256 | |
8cc4198f | 257 | extern int all_digit (const char *); |
718e3744 | 258 | |
d914d5ff | 259 | /* NOTE: This routine expects the address argument in network byte order. */ |
6ee06fa9 PM |
260 | static inline int ipv4_martian (struct in_addr *addr) |
261 | { | |
d914d5ff | 262 | in_addr_t ip = ntohl(addr->s_addr); |
6ee06fa9 PM |
263 | |
264 | if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) { | |
265 | return 1; | |
266 | } | |
267 | return 0; | |
268 | } | |
269 | ||
18ff3edd DS |
270 | static inline int |
271 | is_default_prefix (struct prefix *p) | |
272 | { | |
273 | if (!p) | |
274 | return 0; | |
275 | ||
276 | if (((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY)) | |
277 | || ((p->family == AF_INET6) && | |
278 | !memcmp(&p->u.prefix6, &in6addr_any, sizeof (struct in6_addr)))) | |
279 | return 1; | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
718e3744 | 284 | #endif /* _ZEBRA_PREFIX_H */ |