]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.h
Merge pull request #149 from pguibert6WIND/frr_6wind_stringify
[mirror_frr.git] / lib / prefix.h
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
26 #ifdef SUNOS_5
27 # include <sys/ethernet.h>
28 #else
29 # ifdef GNU_LINUX
30 # include <net/ethernet.h>
31 # else
32 # include <netinet/if_ether.h>
33 # endif
34 #endif
35 #include "sockunion.h"
36
37 #ifndef ETHER_ADDR_LEN
38 #ifdef ETHERADDRL
39 #define ETHER_ADDR_LEN ETHERADDRL
40 #else
41 #define ETHER_ADDR_LEN 6
42 #endif
43 #endif
44
45 #define ETHER_ADDR_STRLEN (3*ETHER_ADDR_LEN)
46 /*
47 * there isn't a portable ethernet address type. We define our
48 * own to simplify internal handling
49 */
50 struct ethaddr {
51 u_char octet[ETHER_ADDR_LEN];
52 } __packed;
53
54
55 /*
56 * A struct prefix contains an address family, a prefix length, and an
57 * address. This can represent either a 'network prefix' as defined
58 * by CIDR, where the 'host bits' of the prefix are 0
59 * (e.g. AF_INET:10.0.0.0/8), or an address and netmask
60 * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an
61 * interface.
62 */
63
64 /* different OSes use different names */
65 #if defined(AF_PACKET)
66 #define AF_ETHERNET AF_PACKET
67 #else
68 #if defined(AF_LINK)
69 #define AF_ETHERNET AF_LINK
70 #endif
71 #endif
72
73 /* IPv4 and IPv6 unified prefix structure. */
74 struct prefix
75 {
76 u_char family;
77 u_char prefixlen;
78 union
79 {
80 u_char prefix;
81 struct in_addr prefix4;
82 struct in6_addr prefix6;
83 struct
84 {
85 struct in_addr id;
86 struct in_addr adv_router;
87 } lp;
88 struct ethaddr prefix_eth; /* AF_ETHERNET */
89 u_char val[8];
90 uintptr_t ptr;
91 } u __attribute__ ((aligned (8)));
92 };
93
94 /* IPv4 prefix structure. */
95 struct prefix_ipv4
96 {
97 u_char family;
98 u_char prefixlen;
99 struct in_addr prefix __attribute__ ((aligned (8)));
100 };
101
102 /* IPv6 prefix structure. */
103 struct prefix_ipv6
104 {
105 u_char family;
106 u_char prefixlen;
107 struct in6_addr prefix __attribute__ ((aligned (8)));
108 };
109
110 struct prefix_ls
111 {
112 u_char family;
113 u_char prefixlen;
114 struct in_addr id __attribute__ ((aligned (8)));
115 struct in_addr adv_router;
116 };
117
118 /* Prefix for routing distinguisher. */
119 struct prefix_rd
120 {
121 u_char family;
122 u_char prefixlen;
123 u_char val[8] __attribute__ ((aligned (8)));
124 };
125
126 /* Prefix for ethernet. */
127 struct prefix_eth
128 {
129 u_char family;
130 u_char prefixlen;
131 struct ethaddr eth_addr __attribute__ ((aligned (8))); /* AF_ETHERNET */
132 };
133
134 /* Prefix for a generic pointer */
135 struct prefix_ptr
136 {
137 u_char family;
138 u_char prefixlen;
139 uintptr_t prefix __attribute__ ((aligned (8)));
140 };
141
142 struct prefix_sg
143 {
144 u_char family;
145 u_char prefixlen;
146 struct in_addr src __attribute ((aligned (8)));
147 struct in_addr grp;
148 };
149
150 /* helper to get type safety/avoid casts on calls
151 * (w/o this, functions accepting all prefix types need casts on the caller
152 * side, which strips type safety since the cast will accept any pointer
153 * type.)
154 */
155 union prefix46ptr
156 {
157 struct prefix *p;
158 struct prefix_ipv4 *p4;
159 struct prefix_ipv6 *p6;
160 } __attribute__ ((transparent_union));
161
162 union prefix46constptr
163 {
164 const struct prefix *p;
165 const struct prefix_ipv4 *p4;
166 const struct prefix_ipv6 *p6;
167 } __attribute__ ((transparent_union));
168
169 #ifndef INET_ADDRSTRLEN
170 #define INET_ADDRSTRLEN 16
171 #endif /* INET_ADDRSTRLEN */
172
173 #ifndef INET6_ADDRSTRLEN
174 #define INET6_ADDRSTRLEN 46
175 #endif /* INET6_ADDRSTRLEN */
176
177 #ifndef INET6_BUFSIZ
178 #define INET6_BUFSIZ 51
179 #endif /* INET6_BUFSIZ */
180
181 /* Maximum prefix string length (IPv6) */
182 #define PREFIX_STRLEN 51
183
184 /* Max bit/byte length of IPv4 address. */
185 #define IPV4_MAX_BYTELEN 4
186 #define IPV4_MAX_BITLEN 32
187 #define IPV4_MAX_PREFIXLEN 32
188 #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN)
189 #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0)
190 #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN)
191
192 #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000)
193 #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000)
194 #define IPV4_LINKLOCAL(a) ((((u_int32_t) (a)) & 0xffff0000) == 0xa9fe0000)
195 #define IPV4_CLASS_DE(a) ((((u_int32_t) (a)) & 0xe0000000) == 0xe0000000)
196
197 /* Max bit/byte length of IPv6 address. */
198 #define IPV6_MAX_BYTELEN 16
199 #define IPV6_MAX_BITLEN 128
200 #define IPV6_MAX_PREFIXLEN 128
201 #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN)
202 #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0)
203 #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN)
204
205 /* Count prefix size from mask length */
206 #define PSIZE(a) (((a) + 7) / (8))
207
208 /* Prefix's family member. */
209 #define PREFIX_FAMILY(p) ((p)->family)
210
211 /* glibc defines s6_addr32 to __in6_u.__u6_addr32 if __USE_{MISC || GNU} */
212 #ifndef s6_addr32
213 #if defined(SUNOS_5)
214 /* Some SunOS define s6_addr32 only to kernel */
215 #define s6_addr32 _S6_un._S6_u32
216 #else
217 #define s6_addr32 __u6_addr.__u6_addr32
218 #endif /* SUNOS_5 */
219 #endif /*s6_addr32*/
220
221 /* Prototypes. */
222 extern int str2family(const char *);
223 extern int afi2family (afi_t);
224 extern afi_t family2afi (int);
225 extern const char *safi2str(safi_t safi);
226 extern const char *afi2str(afi_t afi);
227
228 /* Check bit of the prefix. */
229 extern unsigned int prefix_bit (const u_char *prefix, const u_char prefixlen);
230 extern unsigned int prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen);
231
232 extern struct prefix *prefix_new (void);
233 extern void prefix_free (struct prefix *);
234 extern const char *prefix_family_str (const struct prefix *);
235 extern int prefix_blen (const struct prefix *);
236 extern int str2prefix (const char *, struct prefix *);
237
238 #define PREFIX2STR_BUFFER PREFIX_STRLEN
239
240 extern const char *prefix2str (union prefix46constptr, char *, int);
241 extern int prefix_match (const struct prefix *, const struct prefix *);
242 extern int prefix_same (const struct prefix *, const struct prefix *);
243 extern int prefix_cmp (const struct prefix *, const struct prefix *);
244 extern int prefix_common_bits (const struct prefix *, const struct prefix *);
245 extern void prefix_copy (struct prefix *dest, const struct prefix *src);
246 extern void apply_mask (struct prefix *);
247
248 extern struct prefix *sockunion2prefix (const union sockunion *dest,
249 const union sockunion *mask);
250 extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p);
251 extern void prefix2sockunion (const struct prefix *, union sockunion *);
252
253 extern int str2prefix_eth (const char *, struct prefix_eth *);
254
255 extern struct prefix_ipv4 *prefix_ipv4_new (void);
256 extern void prefix_ipv4_free (struct prefix_ipv4 *);
257 extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
258 extern void apply_mask_ipv4 (struct prefix_ipv4 *);
259
260 #define PREFIX_COPY_IPV4(DST, SRC) \
261 *((struct prefix_ipv4 *)(DST)) = *((const struct prefix_ipv4 *)(SRC));
262
263 extern int prefix_ipv4_any (const struct prefix_ipv4 *);
264 extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
265
266 extern u_char ip_masklen (struct in_addr);
267 extern void masklen2ip (const int, struct in_addr *);
268 /* returns the network portion of the host address */
269 extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen);
270 /* given the address of a host on a network and the network mask length,
271 * calculate the broadcast address for that network;
272 * special treatment for /31: returns the address of the other host
273 * on the network by flipping the host bit */
274 extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen);
275
276 extern int netmask_str2prefix_str (const char *, const char *, char *);
277
278 extern struct prefix_ipv6 *prefix_ipv6_new (void);
279 extern void prefix_ipv6_free (struct prefix_ipv6 *);
280 extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
281 extern void apply_mask_ipv6 (struct prefix_ipv6 *);
282
283 #define PREFIX_COPY_IPV6(DST, SRC) \
284 *((struct prefix_ipv6 *)(DST)) = *((const struct prefix_ipv6 *)(SRC));
285
286 extern int ip6_masklen (struct in6_addr);
287 extern void masklen2ip6 (const int, struct in6_addr *);
288
289 extern const char *inet6_ntoa (struct in6_addr);
290
291 extern int prefix_str2mac(const char *str, struct ethaddr *mac);
292 extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
293
294 static inline int ipv6_martian (struct in6_addr *addr)
295 {
296 struct in6_addr localhost_addr;
297
298 inet_pton (AF_INET6, "::1", &localhost_addr);
299
300 if (IPV6_ADDR_SAME(&localhost_addr, addr))
301 return 1;
302
303 return 0;
304 }
305
306 extern int all_digit (const char *);
307
308 /* NOTE: This routine expects the address argument in network byte order. */
309 static inline int ipv4_martian (struct in_addr *addr)
310 {
311 in_addr_t ip = ntohl(addr->s_addr);
312
313 if (IPV4_NET0(ip) || IPV4_NET127(ip) || IPV4_CLASS_DE(ip)) {
314 return 1;
315 }
316 return 0;
317 }
318
319 static inline int
320 is_default_prefix (struct prefix *p)
321 {
322 if (!p)
323 return 0;
324
325 if (((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY))
326 || ((p->family == AF_INET6) &&
327 !memcmp(&p->u.prefix6, &in6addr_any, sizeof (struct in6_addr))))
328 return 1;
329
330 return 0;
331 }
332
333 #endif /* _ZEBRA_PREFIX_H */