removed keyblobtoid.c from libfreeswan
[strongswan.git] / src / libfreeswan / freeswan.h
1 #ifndef _FREESWAN_H
2 /*
3 * header file for FreeS/WAN library functions
4 * Copyright (C) 1998, 1999, 2000 Henry Spencer.
5 * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Library General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 * License for more details.
16 */
17 #define _FREESWAN_H /* seen it, no need to see it again */
18
19 # include <sys/types.h>
20 # include <stdio.h>
21 # include <netinet/in.h>
22
23 # define DEBUG_NO_STATIC static
24
25 #include <ipsec_param.h>
26 #include <utils.h>
27
28 /*
29 * We assume header files have IPv6 (i.e. kernel version >= 2.1.0)
30 */
31 #define NET_21
32
33 #ifndef IPPROTO_COMP
34 # define IPPROTO_COMP 108
35 #endif /* !IPPROTO_COMP */
36
37 #ifndef IPPROTO_INT
38 # define IPPROTO_INT 61
39 #endif /* !IPPROTO_INT */
40
41 #ifdef CONFIG_IPSEC_DEBUG
42 # define DEBUG_NO_STATIC
43 #else /* CONFIG_IPSEC_DEBUG */
44 # define DEBUG_NO_STATIC static
45 #endif /* CONFIG_IPSEC_DEBUG */
46
47 #define ESPINUDP_WITH_NON_IKE 1 /* draft-ietf-ipsec-nat-t-ike-00/01 */
48 #define ESPINUDP_WITH_NON_ESP 2 /* draft-ietf-ipsec-nat-t-ike-02 */
49
50 /*
51 * Basic data types for the address-handling functions.
52 * ip_address and ip_subnet are supposed to be opaque types; do not
53 * use their definitions directly, they are subject to change!
54 */
55
56 /* then the main types */
57 typedef struct {
58 union {
59 struct sockaddr_in v4;
60 struct sockaddr_in6 v6;
61 } u;
62 } ip_address;
63 typedef struct {
64 ip_address addr;
65 int maskbits;
66 } ip_subnet;
67
68 /* and the SA ID stuff */
69 typedef u_int32_t ipsec_spi_t;
70 typedef struct { /* to identify an SA, we need: */
71 ip_address dst; /* A. destination host */
72 ipsec_spi_t spi; /* B. 32-bit SPI, assigned by dest. host */
73 # define SPI_PASS 256 /* magic values... */
74 # define SPI_DROP 257 /* ...for use... */
75 # define SPI_REJECT 258 /* ...with SA_INT */
76 # define SPI_HOLD 259
77 # define SPI_TRAP 260
78 # define SPI_TRAPSUBNET 261
79 int proto; /* C. protocol */
80 # define SA_ESP 50 /* IPPROTO_ESP */
81 # define SA_AH 51 /* IPPROTO_AH */
82 # define SA_IPIP 4 /* IPPROTO_IPIP */
83 # define SA_COMP 108 /* IPPROTO_COMP */
84 # define SA_INT 61 /* IANA reserved for internal use */
85 } ip_said;
86 struct sa_id { /* old v4-only version */
87 struct in_addr dst;
88 ipsec_spi_t spi;
89 int proto;
90 };
91
92 /* misc */
93 struct prng { /* pseudo-random-number-generator guts */
94 unsigned char sbox[256];
95 int i, j;
96 unsigned long count;
97 };
98
99
100 /*
101 * definitions for user space, taken from freeswan/ipsec_sa.h
102 */
103 typedef uint32_t IPsecSAref_t;
104
105 #define IPSEC_SA_REF_TABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH)
106
107 #define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
108
109 #define IPsecSAref2NFmark(x) ((x) << (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
110 #define NFmark2IPsecSAref(x) ((x) >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
111
112 #define IPSEC_SAREF_NULL (~((IPsecSAref_t)0))
113
114 /* GCC magic for use in function definitions! */
115 #ifdef GCC_LINT
116 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
117 # define NEVER_RETURNS __attribute__ ((noreturn))
118 # define UNUSED __attribute__ ((unused))
119 # define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
120 #else
121 # define PRINTF_LIKE(n) /* ignore */
122 # define NEVER_RETURNS /* ignore */
123 # define UNUSED /* ignore */
124 # define BLANK_FORMAT ""
125 #endif
126
127
128
129
130
131 /*
132 * new IPv6-compatible functions
133 */
134
135 /* text conversions */
136 err_t ttoul(const char *src, size_t srclen, int format, unsigned long *dst);
137 size_t ultot(unsigned long src, int format, char *buf, size_t buflen);
138 #define ULTOT_BUF (22+1) /* holds 64 bits in octal */
139 err_t ttoaddr(const char *src, size_t srclen, int af, ip_address *dst);
140 err_t tnatoaddr(const char *src, size_t srclen, int af, ip_address *dst);
141 size_t addrtot(const ip_address *src, int format, char *buf, size_t buflen);
142 /* RFC 1886 old IPv6 reverse-lookup format is the bulkiest */
143 #define ADDRTOT_BUF (32*2 + 3 + 1 + 3 + 1 + 1)
144 err_t ttosubnet(const char *src, size_t srclen, int af, ip_subnet *dst);
145 size_t subnettot(const ip_subnet *src, int format, char *buf, size_t buflen);
146 #define SUBNETTOT_BUF (ADDRTOT_BUF + 1 + 3)
147 err_t ttosa(const char *src, size_t srclen, ip_said *dst);
148 size_t satot(const ip_said *src, int format, char *bufptr, size_t buflen);
149 #define SATOT_BUF (5 + ULTOA_BUF + 1 + ADDRTOT_BUF)
150 err_t ttodata(const char *src, size_t srclen, int base, char *buf,
151 size_t buflen, size_t *needed);
152 err_t ttodatav(const char *src, size_t srclen, int base,
153 char *buf, size_t buflen, size_t *needed,
154 char *errp, size_t errlen, unsigned int flags);
155 #define TTODATAV_BUF 40 /* ttodatav's largest non-literal message */
156 #define TTODATAV_IGNORESPACE (1<<1) /* ignore spaces in base64 encodings*/
157 #define TTODATAV_SPACECOUNTS 0 /* do not ignore spaces in base64 */
158
159 size_t datatot(const char *src, size_t srclen, int format, char *buf,
160 size_t buflen);
161 err_t ttoprotoport(char *src, size_t src_len, u_int8_t *proto, u_int16_t *port,
162 bool *has_port_wildcard);
163
164 /* initializations */
165 void initsaid(const ip_address *addr, ipsec_spi_t spi, int proto, ip_said *dst);
166 err_t loopbackaddr(int af, ip_address *dst);
167 err_t unspecaddr(int af, ip_address *dst);
168 err_t anyaddr(int af, ip_address *dst);
169 err_t initaddr(const unsigned char *src, size_t srclen, int af, ip_address *dst);
170 err_t initsubnet(const ip_address *addr, int maskbits, int clash, ip_subnet *dst);
171 err_t addrtosubnet(const ip_address *addr, ip_subnet *dst);
172
173 /* misc. conversions and related */
174 err_t rangetosubnet(const ip_address *from, const ip_address *to, ip_subnet *dst);
175 int addrtypeof(const ip_address *src);
176 int subnettypeof(const ip_subnet *src);
177 size_t addrlenof(const ip_address *src);
178 size_t addrbytesptr(const ip_address *src, const unsigned char **dst);
179 size_t addrbytesof(const ip_address *src, unsigned char *dst, size_t dstlen);
180 int masktocount(const ip_address *src);
181 void networkof(const ip_subnet *src, ip_address *dst);
182 void maskof(const ip_subnet *src, ip_address *dst);
183
184 /* tests */
185 int sameaddr(const ip_address *a, const ip_address *b);
186 int addrcmp(const ip_address *a, const ip_address *b);
187 int samesubnet(const ip_subnet *a, const ip_subnet *b);
188 int addrinsubnet(const ip_address *a, const ip_subnet *s);
189 int subnetinsubnet(const ip_subnet *a, const ip_subnet *b);
190 int subnetishost(const ip_subnet *s);
191 int samesaid(const ip_said *a, const ip_said *b);
192 int sameaddrtype(const ip_address *a, const ip_address *b);
193 int samesubnettype(const ip_subnet *a, const ip_subnet *b);
194 int isanyaddr(const ip_address *src);
195 int isunspecaddr(const ip_address *src);
196 int isloopbackaddr(const ip_address *src);
197
198 /* low-level grot */
199 int portof(const ip_address *src);
200 void setportof(int port, ip_address *dst);
201 struct sockaddr *sockaddrof(ip_address *src);
202 size_t sockaddrlenof(const ip_address *src);
203
204 /* odds and ends */
205 const char **ipsec_copyright_notice(void);
206
207 const char *dns_string_rr(int rr, char *buf, int bufsize);
208 const char *dns_string_datetime(time_t seconds,
209 char *buf,
210 int bufsize);
211
212
213 /*
214 * old functions, to be deleted eventually
215 */
216
217 /* unsigned long */
218 const char * /* NULL for success, else string literal */
219 atoul(
220 const char *src,
221 size_t srclen, /* 0 means strlen(src) */
222 int base, /* 0 means figure it out */
223 unsigned long *resultp
224 );
225 size_t /* space needed for full conversion */
226 ultoa(
227 unsigned long n,
228 int base,
229 char *dst,
230 size_t dstlen
231 );
232 #define ULTOA_BUF 21 /* just large enough for largest result, */
233 /* assuming 64-bit unsigned long! */
234
235 /* Internet addresses */
236 const char * /* NULL for success, else string literal */
237 atoaddr(
238 const char *src,
239 size_t srclen, /* 0 means strlen(src) */
240 struct in_addr *addr
241 );
242 size_t /* space needed for full conversion */
243 addrtoa(
244 struct in_addr addr,
245 int format, /* character; 0 means default */
246 char *dst,
247 size_t dstlen
248 );
249 #define ADDRTOA_BUF 16 /* just large enough for largest result */
250
251 /* subnets */
252 const char * /* NULL for success, else string literal */
253 atosubnet(
254 const char *src,
255 size_t srclen, /* 0 means strlen(src) */
256 struct in_addr *addr,
257 struct in_addr *mask
258 );
259 size_t /* space needed for full conversion */
260 subnettoa(
261 struct in_addr addr,
262 struct in_addr mask,
263 int format, /* character; 0 means default */
264 char *dst,
265 size_t dstlen
266 );
267 #define SUBNETTOA_BUF 32 /* large enough for worst case result */
268
269 /* ranges */
270 const char * /* NULL for success, else string literal */
271 atoasr(
272 const char *src,
273 size_t srclen, /* 0 means strlen(src) */
274 char *type, /* 'a', 's', 'r' */
275 struct in_addr *addrs /* two-element array */
276 );
277 size_t /* space needed for full conversion */
278 rangetoa(
279 struct in_addr *addrs, /* two-element array */
280 int format, /* character; 0 means default */
281 char *dst,
282 size_t dstlen
283 );
284 #define RANGETOA_BUF 34 /* large enough for worst case result */
285
286 /* generic data, e.g. keys */
287 const char * /* NULL for success, else string literal */
288 atobytes(
289 const char *src,
290 size_t srclen, /* 0 means strlen(src) */
291 char *dst,
292 size_t dstlen,
293 size_t *lenp /* NULL means don't bother telling me */
294 );
295 size_t /* 0 failure, else true size */
296 bytestoa(
297 const char *src,
298 size_t srclen,
299 int format, /* character; 0 means default */
300 char *dst,
301 size_t dstlen
302 );
303
304 /* old versions of generic-data functions; deprecated */
305 size_t /* 0 failure, else true size */
306 atodata(
307 const char *src,
308 size_t srclen, /* 0 means strlen(src) */
309 char *dst,
310 size_t dstlen
311 );
312 size_t /* 0 failure, else true size */
313 datatoa(
314 const char *src,
315 size_t srclen,
316 int format, /* character; 0 means default */
317 char *dst,
318 size_t dstlen
319 );
320
321 /* part extraction and special addresses */
322 struct in_addr
323 subnetof(
324 struct in_addr addr,
325 struct in_addr mask
326 );
327 struct in_addr
328 hostof(
329 struct in_addr addr,
330 struct in_addr mask
331 );
332 struct in_addr
333 broadcastof(
334 struct in_addr addr,
335 struct in_addr mask
336 );
337
338 /* mask handling */
339 int
340 goodmask(
341 struct in_addr mask
342 );
343 int
344 masktobits(
345 struct in_addr mask
346 );
347 struct in_addr
348 bitstomask(
349 int n
350 );
351
352 /*
353 * Debugging levels for pfkey_lib_debug
354 */
355 #define PF_KEY_DEBUG_PARSE_NONE 0
356 #define PF_KEY_DEBUG_PARSE_PROBLEM 1
357 #define PF_KEY_DEBUG_PARSE_STRUCT 2
358 #define PF_KEY_DEBUG_PARSE_FLOW 4
359 #define PF_KEY_DEBUG_PARSE_MAX 7
360
361 extern unsigned int pfkey_lib_debug; /* bits selecting what to report */
362
363 /*
364 * pluto and lwdnsq need to know the maximum size of the commands to,
365 * and replies from lwdnsq.
366 */
367
368 #define LWDNSQ_CMDBUF_LEN 1024
369 #define LWDNSQ_RESULT_LEN_MAX 4096
370
371 #endif /* _FREESWAN_H */