]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipsec.h
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / netinet6 / ipsec.h
1 /* $FreeBSD: src/sys/netinet6/ipsec.h,v 1.4.2.2 2001/07/03 11:01:54 ume Exp $ */
2 /* $KAME: ipsec.h,v 1.44 2001/03/23 08:08:47 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * IPsec controller part.
35 */
36
37 #ifndef _NETINET6_IPSEC_H_
38 #define _NETINET6_IPSEC_H_
39 #include <sys/appleapiopts.h>
40
41 #include <net/pfkeyv2.h>
42 #include <netkey/keydb.h>
43
44 #ifdef KERNEL
45 #ifdef __APPLE_API_PRIVATE
46 /*
47 * Security Policy Index
48 * NOTE: Ensure to be same address family and upper layer protocol.
49 * NOTE: ul_proto, port number, uid, gid:
50 * ANY: reserved for waldcard.
51 * 0 to (~0 - 1): is one of the number of each value.
52 */
53 struct secpolicyindex {
54 u_int8_t dir; /* direction of packet flow, see blow */
55 struct sockaddr_storage src; /* IP src address for SP */
56 struct sockaddr_storage dst; /* IP dst address for SP */
57 u_int8_t prefs; /* prefix length in bits for src */
58 u_int8_t prefd; /* prefix length in bits for dst */
59 u_int16_t ul_proto; /* upper layer Protocol */
60 #ifdef notyet
61 uid_t uids;
62 uid_t uidd;
63 gid_t gids;
64 gid_t gidd;
65 #endif
66 };
67
68 /* Security Policy Data Base */
69 struct secpolicy {
70 LIST_ENTRY(secpolicy) chain;
71
72 int refcnt; /* reference count */
73 struct secpolicyindex spidx; /* selector */
74 u_int32_t id; /* It's unique number on the system. */
75 u_int state; /* 0: dead, others: alive */
76 #define IPSEC_SPSTATE_DEAD 0
77 #define IPSEC_SPSTATE_ALIVE 1
78
79 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */
80 struct ipsecrequest *req;
81 /* pointer to the ipsec request tree, */
82 /* if policy == IPSEC else this value == NULL.*/
83
84 /*
85 * lifetime handler.
86 * the policy can be used without limitiation if both lifetime and
87 * validtime are zero.
88 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
89 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
90 */
91 long created; /* time created the policy */
92 long lastused; /* updated every when kernel sends a packet */
93 long lifetime; /* duration of the lifetime of this policy */
94 long validtime; /* duration this policy is valid without use */
95 };
96
97 /* Request for IPsec */
98 struct ipsecrequest {
99 struct ipsecrequest *next;
100 /* pointer to next structure */
101 /* If NULL, it means the end of chain. */
102 struct secasindex saidx;/* hint for search proper SA */
103 /* if __ss_len == 0 then no address specified.*/
104 u_int level; /* IPsec level defined below. */
105
106 struct secasvar *sav; /* place holder of SA for use */
107 struct secpolicy *sp; /* back pointer to SP */
108 };
109
110 /* security policy in PCB */
111 struct inpcbpolicy {
112 struct secpolicy *sp_in;
113 struct secpolicy *sp_out;
114 int priv; /* privileged socket ? */
115 };
116
117 /* SP acquiring list table. */
118 struct secspacq {
119 LIST_ENTRY(secspacq) chain;
120
121 struct secpolicyindex spidx;
122
123 long created; /* for lifetime */
124 int count; /* for lifetime */
125 /* XXX: here is mbuf place holder to be sent ? */
126 };
127 #endif /* __APPLE_API_PRIVATE */
128 #endif /*KERNEL*/
129
130 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
131 #define IPSEC_PORT_ANY 0
132 #define IPSEC_ULPROTO_ANY 255
133 #define IPSEC_PROTO_ANY 255
134
135 /* mode of security protocol */
136 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
137 #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
138 #define IPSEC_MODE_TRANSPORT 1
139 #define IPSEC_MODE_TUNNEL 2
140
141 /*
142 * Direction of security policy.
143 * NOTE: Since INVALID is used just as flag.
144 * The other are used for loop counter too.
145 */
146 #define IPSEC_DIR_ANY 0
147 #define IPSEC_DIR_INBOUND 1
148 #define IPSEC_DIR_OUTBOUND 2
149 #define IPSEC_DIR_MAX 3
150 #define IPSEC_DIR_INVALID 4
151
152 /* Policy level */
153 /*
154 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
155 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
156 * DISCARD and NONE are allowed for system default.
157 */
158 #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
159 #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
160 #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
161 #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
162 #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
163
164 /* Security protocol level */
165 #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
166 #define IPSEC_LEVEL_USE 1 /* use SA if present. */
167 #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
168 #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
169
170 #define IPSEC_MANUAL_REQID_MAX 0x3fff
171 /*
172 * if security policy level == unique, this id
173 * indicate to a relative SA for use, else is
174 * zero.
175 * 1 - 0x3fff are reserved for manual keying.
176 * 0 are reserved for above reason. Others is
177 * for kernel use.
178 * Note that this id doesn't identify SA
179 * by only itself.
180 */
181 #define IPSEC_REPLAYWSIZE 32
182
183 #ifdef __APPLE_API_UNSTABLE
184 /* statistics for ipsec processing */
185 struct ipsecstat {
186 u_quad_t in_success; /* succeeded inbound process */
187 u_quad_t in_polvio;
188 /* security policy violation for inbound process */
189 u_quad_t in_nosa; /* inbound SA is unavailable */
190 u_quad_t in_inval; /* inbound processing failed due to EINVAL */
191 u_quad_t in_nomem; /* inbound processing failed due to ENOBUFS */
192 u_quad_t in_badspi; /* failed getting a SPI */
193 u_quad_t in_ahreplay; /* AH replay check failed */
194 u_quad_t in_espreplay; /* ESP replay check failed */
195 u_quad_t in_ahauthsucc; /* AH authentication success */
196 u_quad_t in_ahauthfail; /* AH authentication failure */
197 u_quad_t in_espauthsucc; /* ESP authentication success */
198 u_quad_t in_espauthfail; /* ESP authentication failure */
199 u_quad_t in_esphist[256];
200 u_quad_t in_ahhist[256];
201 u_quad_t in_comphist[256];
202 u_quad_t out_success; /* succeeded outbound process */
203 u_quad_t out_polvio;
204 /* security policy violation for outbound process */
205 u_quad_t out_nosa; /* outbound SA is unavailable */
206 u_quad_t out_inval; /* outbound process failed due to EINVAL */
207 u_quad_t out_nomem; /* inbound processing failed due to ENOBUFS */
208 u_quad_t out_noroute; /* there is no route */
209 u_quad_t out_esphist[256];
210 u_quad_t out_ahhist[256];
211 u_quad_t out_comphist[256];
212 };
213 #endif /* __APPLE_API_UNSTABLE */
214
215 /*
216 * Definitions for IPsec & Key sysctl operations.
217 */
218 /*
219 * Names for IPsec & Key sysctl objects
220 */
221 #define IPSECCTL_STATS 1 /* stats */
222 #define IPSECCTL_DEF_POLICY 2
223 #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
224 #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
225 #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
226 #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
227 #if 0 /*obsolete, do not reuse*/
228 #define IPSECCTL_INBOUND_CALL_IKE 7
229 #endif
230 #define IPSECCTL_AH_CLEARTOS 8
231 #define IPSECCTL_AH_OFFSETMASK 9
232 #define IPSECCTL_DFBIT 10
233 #define IPSECCTL_ECN 11
234 #define IPSECCTL_DEBUG 12
235 #define IPSECCTL_ESP_RANDPAD 13
236 #define IPSECCTL_MAXID 14
237
238 #define IPSECCTL_NAMES { \
239 { 0, 0 }, \
240 { 0, 0 }, \
241 { "def_policy", CTLTYPE_INT }, \
242 { "esp_trans_deflev", CTLTYPE_INT }, \
243 { "esp_net_deflev", CTLTYPE_INT }, \
244 { "ah_trans_deflev", CTLTYPE_INT }, \
245 { "ah_net_deflev", CTLTYPE_INT }, \
246 { 0, 0 }, \
247 { "ah_cleartos", CTLTYPE_INT }, \
248 { "ah_offsetmask", CTLTYPE_INT }, \
249 { "dfbit", CTLTYPE_INT }, \
250 { "ecn", CTLTYPE_INT }, \
251 { "debug", CTLTYPE_INT }, \
252 { "esp_randpad", CTLTYPE_INT }, \
253 }
254
255 #define IPSEC6CTL_NAMES { \
256 { 0, 0 }, \
257 { 0, 0 }, \
258 { "def_policy", CTLTYPE_INT }, \
259 { "esp_trans_deflev", CTLTYPE_INT }, \
260 { "esp_net_deflev", CTLTYPE_INT }, \
261 { "ah_trans_deflev", CTLTYPE_INT }, \
262 { "ah_net_deflev", CTLTYPE_INT }, \
263 { 0, 0 }, \
264 { 0, 0 }, \
265 { 0, 0 }, \
266 { 0, 0 }, \
267 { "ecn", CTLTYPE_INT }, \
268 { "debug", CTLTYPE_INT }, \
269 { "esp_randpad", CTLTYPE_INT }, \
270 }
271
272 #ifdef KERNEL
273 #ifdef __APPLE_API_PRIVATE
274 struct ipsec_output_state {
275 struct mbuf *m;
276 struct route *ro;
277 struct sockaddr *dst;
278 };
279
280 struct ipsec_history {
281 int ih_proto;
282 u_int32_t ih_spi;
283 };
284
285 extern int ipsec_debug;
286
287 extern struct ipsecstat ipsecstat;
288 extern struct secpolicy ip4_def_policy;
289 extern int ip4_esp_trans_deflev;
290 extern int ip4_esp_net_deflev;
291 extern int ip4_ah_trans_deflev;
292 extern int ip4_ah_net_deflev;
293 extern int ip4_ah_cleartos;
294 extern int ip4_ah_offsetmask;
295 extern int ip4_ipsec_dfbit;
296 extern int ip4_ipsec_ecn;
297 extern int ip4_esp_randpad;
298
299 #define ipseclog(x) do { if (ipsec_debug) log x; } while (0)
300
301 extern struct secpolicy *ipsec4_getpolicybysock
302 __P((struct mbuf *, u_int, struct socket *, int *));
303 extern struct secpolicy *ipsec4_getpolicybyaddr
304 __P((struct mbuf *, u_int, int, int *));
305
306 struct inpcb;
307 extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **));
308 extern int ipsec_copy_policy
309 __P((struct inpcbpolicy *, struct inpcbpolicy *));
310 extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *));
311
312 extern int ipsec4_set_policy __P((struct inpcb *inp, int optname,
313 caddr_t request, size_t len, int priv));
314 extern int ipsec4_get_policy __P((struct inpcb *inpcb, caddr_t request,
315 size_t len, struct mbuf **mp));
316 extern int ipsec4_delete_pcbpolicy __P((struct inpcb *));
317 extern int ipsec4_in_reject_so __P((struct mbuf *, struct socket *));
318 extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
319
320 struct secas;
321 struct tcpcb;
322 extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
323 extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
324
325 extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
326 extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
327
328 struct ip;
329 extern const char *ipsec4_logpacketstr __P((struct ip *, u_int32_t));
330 extern const char *ipsec_logsastr __P((struct secasvar *));
331
332 extern void ipsec_dumpmbuf __P((struct mbuf *));
333
334 extern int ipsec4_output __P((struct ipsec_output_state *, struct secpolicy *,
335 int));
336 extern int ipsec4_tunnel_validate __P((struct mbuf *, int, u_int,
337 struct secasvar *));
338 extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
339 extern void ipsec_delaux __P((struct mbuf *));
340 extern int ipsec_setsocket __P((struct mbuf *, struct socket *));
341 extern struct socket *ipsec_getsocket __P((struct mbuf *));
342 extern int ipsec_addhist __P((struct mbuf *, int, u_int32_t));
343 extern struct ipsec_history *ipsec_gethist __P((struct mbuf *, int *));
344 extern void ipsec_clearhist __P((struct mbuf *));
345 #endif /* __APPLE_API_PRIVATE */
346 #endif /*KERNEL*/
347
348 #ifndef KERNEL
349 extern caddr_t ipsec_set_policy __P((char *, int));
350 extern int ipsec_get_policylen __P((caddr_t));
351 extern char *ipsec_dump_policy __P((caddr_t, char *));
352
353 extern const char *ipsec_strerror __P((void));
354 #endif /*!KERNEL*/
355
356 #endif /*_NETINET6_IPSEC_H_*/