]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ipsec.h
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / netinet6 / ipsec.h
CommitLineData
9bccf70c
A
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 $ */
1c79356b
A
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_
9bccf70c 39#include <sys/appleapiopts.h>
1c79356b
A
40
41#include <net/pfkeyv2.h>
91447636 42#ifdef KERNEL_PRIVATE
1c79356b
A
43#include <netkey/keydb.h>
44
2d21ac55
A
45/* lock for IPSec stats */
46lck_grp_t *sadb_stat_mutex_grp;
47lck_grp_attr_t *sadb_stat_mutex_grp_attr;
48lck_attr_t *sadb_stat_mutex_attr;
49lck_mtx_t *sadb_stat_mutex;
50
51
52#define IPSEC_STAT_INCREMENT(x) \
53 {lck_mtx_lock(sadb_stat_mutex); (x)++; lck_mtx_unlock(sadb_stat_mutex);}
54
55
1c79356b
A
56/*
57 * Security Policy Index
55e303ae
A
58 * Ensure that both address families in the "src" and "dst" are same.
59 * When the value of the ul_proto is ICMPv6, the port field in "src"
60 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
1c79356b
A
61 */
62struct secpolicyindex {
63 u_int8_t dir; /* direction of packet flow, see blow */
64 struct sockaddr_storage src; /* IP src address for SP */
65 struct sockaddr_storage dst; /* IP dst address for SP */
66 u_int8_t prefs; /* prefix length in bits for src */
67 u_int8_t prefd; /* prefix length in bits for dst */
68 u_int16_t ul_proto; /* upper layer Protocol */
69#ifdef notyet
70 uid_t uids;
71 uid_t uidd;
72 gid_t gids;
73 gid_t gidd;
74#endif
75};
76
77/* Security Policy Data Base */
78struct secpolicy {
79 LIST_ENTRY(secpolicy) chain;
80
81 int refcnt; /* reference count */
82 struct secpolicyindex spidx; /* selector */
83 u_int32_t id; /* It's unique number on the system. */
84 u_int state; /* 0: dead, others: alive */
85#define IPSEC_SPSTATE_DEAD 0
86#define IPSEC_SPSTATE_ALIVE 1
87
88 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */
89 struct ipsecrequest *req;
90 /* pointer to the ipsec request tree, */
91 /* if policy == IPSEC else this value == NULL.*/
9bccf70c
A
92
93 /*
94 * lifetime handler.
95 * the policy can be used without limitiation if both lifetime and
96 * validtime are zero.
97 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
98 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
99 */
100 long created; /* time created the policy */
101 long lastused; /* updated every when kernel sends a packet */
102 long lifetime; /* duration of the lifetime of this policy */
103 long validtime; /* duration this policy is valid without use */
1c79356b
A
104};
105
106/* Request for IPsec */
107struct ipsecrequest {
108 struct ipsecrequest *next;
109 /* pointer to next structure */
110 /* If NULL, it means the end of chain. */
111 struct secasindex saidx;/* hint for search proper SA */
112 /* if __ss_len == 0 then no address specified.*/
113 u_int level; /* IPsec level defined below. */
114
1c79356b
A
115 struct secpolicy *sp; /* back pointer to SP */
116};
117
118/* security policy in PCB */
119struct inpcbpolicy {
120 struct secpolicy *sp_in;
121 struct secpolicy *sp_out;
122 int priv; /* privileged socket ? */
123};
124
125/* SP acquiring list table. */
126struct secspacq {
127 LIST_ENTRY(secspacq) chain;
128
129 struct secpolicyindex spidx;
130
9bccf70c 131 long created; /* for lifetime */
1c79356b
A
132 int count; /* for lifetime */
133 /* XXX: here is mbuf place holder to be sent ? */
134};
91447636 135#endif /* KERNEL_PRIVATE */
1c79356b
A
136
137/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
138#define IPSEC_PORT_ANY 0
139#define IPSEC_ULPROTO_ANY 255
140#define IPSEC_PROTO_ANY 255
141
142/* mode of security protocol */
143/* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
144#define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
145#define IPSEC_MODE_TRANSPORT 1
146#define IPSEC_MODE_TUNNEL 2
147
148/*
149 * Direction of security policy.
150 * NOTE: Since INVALID is used just as flag.
151 * The other are used for loop counter too.
152 */
153#define IPSEC_DIR_ANY 0
154#define IPSEC_DIR_INBOUND 1
155#define IPSEC_DIR_OUTBOUND 2
156#define IPSEC_DIR_MAX 3
157#define IPSEC_DIR_INVALID 4
158
159/* Policy level */
160/*
9bccf70c
A
161 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
162 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
163 * DISCARD and NONE are allowed for system default.
1c79356b
A
164 */
165#define IPSEC_POLICY_DISCARD 0 /* discarding packet */
166#define IPSEC_POLICY_NONE 1 /* through IPsec engine */
167#define IPSEC_POLICY_IPSEC 2 /* do IPsec */
168#define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
169#define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
2d21ac55 170#define IPSEC_POLICY_GENERATE 5 /* same as discard - IKE daemon can override with generated policy */
1c79356b
A
171
172/* Security protocol level */
173#define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
174#define IPSEC_LEVEL_USE 1 /* use SA if present. */
175#define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
176#define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
177
178#define IPSEC_MANUAL_REQID_MAX 0x3fff
179 /*
180 * if security policy level == unique, this id
181 * indicate to a relative SA for use, else is
182 * zero.
183 * 1 - 0x3fff are reserved for manual keying.
184 * 0 are reserved for above reason. Others is
185 * for kernel use.
186 * Note that this id doesn't identify SA
187 * by only itself.
188 */
189#define IPSEC_REPLAYWSIZE 32
190
191/* statistics for ipsec processing */
192struct ipsecstat {
193 u_quad_t in_success; /* succeeded inbound process */
194 u_quad_t in_polvio;
195 /* security policy violation for inbound process */
196 u_quad_t in_nosa; /* inbound SA is unavailable */
197 u_quad_t in_inval; /* inbound processing failed due to EINVAL */
198 u_quad_t in_nomem; /* inbound processing failed due to ENOBUFS */
199 u_quad_t in_badspi; /* failed getting a SPI */
200 u_quad_t in_ahreplay; /* AH replay check failed */
201 u_quad_t in_espreplay; /* ESP replay check failed */
202 u_quad_t in_ahauthsucc; /* AH authentication success */
203 u_quad_t in_ahauthfail; /* AH authentication failure */
204 u_quad_t in_espauthsucc; /* ESP authentication success */
205 u_quad_t in_espauthfail; /* ESP authentication failure */
206 u_quad_t in_esphist[256];
207 u_quad_t in_ahhist[256];
208 u_quad_t in_comphist[256];
209 u_quad_t out_success; /* succeeded outbound process */
210 u_quad_t out_polvio;
211 /* security policy violation for outbound process */
212 u_quad_t out_nosa; /* outbound SA is unavailable */
213 u_quad_t out_inval; /* outbound process failed due to EINVAL */
214 u_quad_t out_nomem; /* inbound processing failed due to ENOBUFS */
215 u_quad_t out_noroute; /* there is no route */
216 u_quad_t out_esphist[256];
217 u_quad_t out_ahhist[256];
218 u_quad_t out_comphist[256];
219};
220
91447636 221#ifdef KERNEL_PRIVATE
1c79356b
A
222/*
223 * Definitions for IPsec & Key sysctl operations.
224 */
225/*
226 * Names for IPsec & Key sysctl objects
227 */
228#define IPSECCTL_STATS 1 /* stats */
229#define IPSECCTL_DEF_POLICY 2
230#define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
231#define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
232#define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
233#define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
55e303ae 234#if 0 /* obsolete, do not reuse */
1c79356b 235#define IPSECCTL_INBOUND_CALL_IKE 7
9bccf70c 236#endif
1c79356b
A
237#define IPSECCTL_AH_CLEARTOS 8
238#define IPSECCTL_AH_OFFSETMASK 9
239#define IPSECCTL_DFBIT 10
240#define IPSECCTL_ECN 11
241#define IPSECCTL_DEBUG 12
9bccf70c
A
242#define IPSECCTL_ESP_RANDPAD 13
243#define IPSECCTL_MAXID 14
1c79356b
A
244
245#define IPSECCTL_NAMES { \
246 { 0, 0 }, \
247 { 0, 0 }, \
248 { "def_policy", CTLTYPE_INT }, \
249 { "esp_trans_deflev", CTLTYPE_INT }, \
250 { "esp_net_deflev", CTLTYPE_INT }, \
251 { "ah_trans_deflev", CTLTYPE_INT }, \
252 { "ah_net_deflev", CTLTYPE_INT }, \
9bccf70c 253 { 0, 0 }, \
1c79356b
A
254 { "ah_cleartos", CTLTYPE_INT }, \
255 { "ah_offsetmask", CTLTYPE_INT }, \
256 { "dfbit", CTLTYPE_INT }, \
257 { "ecn", CTLTYPE_INT }, \
258 { "debug", CTLTYPE_INT }, \
9bccf70c 259 { "esp_randpad", CTLTYPE_INT }, \
1c79356b
A
260}
261
262#define IPSEC6CTL_NAMES { \
263 { 0, 0 }, \
264 { 0, 0 }, \
265 { "def_policy", CTLTYPE_INT }, \
266 { "esp_trans_deflev", CTLTYPE_INT }, \
267 { "esp_net_deflev", CTLTYPE_INT }, \
268 { "ah_trans_deflev", CTLTYPE_INT }, \
269 { "ah_net_deflev", CTLTYPE_INT }, \
9bccf70c 270 { 0, 0 }, \
1c79356b
A
271 { 0, 0 }, \
272 { 0, 0 }, \
273 { 0, 0 }, \
274 { "ecn", CTLTYPE_INT }, \
275 { "debug", CTLTYPE_INT }, \
9bccf70c 276 { "esp_randpad", CTLTYPE_INT }, \
1c79356b
A
277}
278
9bccf70c 279#ifdef KERNEL
1c79356b
A
280struct ipsec_output_state {
281 struct mbuf *m;
282 struct route *ro;
283 struct sockaddr *dst;
284};
285
9bccf70c
A
286struct ipsec_history {
287 int ih_proto;
288 u_int32_t ih_spi;
289};
290
1c79356b
A
291extern int ipsec_debug;
292
1c79356b
A
293extern struct ipsecstat ipsecstat;
294extern struct secpolicy ip4_def_policy;
295extern int ip4_esp_trans_deflev;
296extern int ip4_esp_net_deflev;
297extern int ip4_ah_trans_deflev;
298extern int ip4_ah_net_deflev;
1c79356b
A
299extern int ip4_ah_cleartos;
300extern int ip4_ah_offsetmask;
301extern int ip4_ipsec_dfbit;
302extern int ip4_ipsec_ecn;
9bccf70c 303extern int ip4_esp_randpad;
1c79356b
A
304
305#define ipseclog(x) do { if (ipsec_debug) log x; } while (0)
306
91447636
A
307extern struct secpolicy *ipsec4_getpolicybysock(struct mbuf *, u_int,
308 struct socket *, int *);
309extern struct secpolicy *ipsec4_getpolicybyaddr(struct mbuf *, u_int, int,
310 int *);
1c79356b 311
1c79356b 312struct inpcb;
91447636
A
313extern int ipsec_init_policy(struct socket *so, struct inpcbpolicy **);
314extern int ipsec_copy_policy(struct inpcbpolicy *, struct inpcbpolicy *);
315extern u_int ipsec_get_reqlevel(struct ipsecrequest *);
316
317extern int ipsec4_set_policy(struct inpcb *inp, int optname,
318 caddr_t request, size_t len, int priv);
319extern int ipsec4_get_policy(struct inpcb *inpcb, caddr_t request,
320 size_t len, struct mbuf **mp);
321extern int ipsec4_delete_pcbpolicy(struct inpcb *);
322extern int ipsec4_in_reject_so(struct mbuf *, struct socket *);
323extern int ipsec4_in_reject(struct mbuf *, struct inpcb *);
1c79356b 324
1c79356b
A
325struct secas;
326struct tcpcb;
91447636
A
327extern int ipsec_chkreplay(u_int32_t, struct secasvar *);
328extern int ipsec_updatereplay(u_int32_t, struct secasvar *);
1c79356b 329
91447636
A
330extern size_t ipsec4_hdrsiz(struct mbuf *, u_int, struct inpcb *);
331extern size_t ipsec_hdrsiz_tcp(struct tcpcb *);
2d21ac55 332extern size_t ipsec_hdrsiz(struct secpolicy *);
1c79356b
A
333
334struct ip;
91447636
A
335extern const char *ipsec4_logpacketstr(struct ip *, u_int32_t);
336extern const char *ipsec_logsastr(struct secasvar *);
337
338extern void ipsec_dumpmbuf(struct mbuf *);
339
340extern int ipsec4_output(struct ipsec_output_state *, struct secpolicy *, int);
2d21ac55 341extern int ipsec4_tunnel_validate(struct mbuf *, int, u_int, struct secasvar *, sa_family_t *);
91447636
A
342extern struct mbuf *ipsec_copypkt(struct mbuf *);
343extern void ipsec_delaux(struct mbuf *);
344extern int ipsec_setsocket(struct mbuf *, struct socket *);
345extern struct socket *ipsec_getsocket(struct mbuf *);
346extern int ipsec_addhist(struct mbuf *, int, u_int32_t);
347extern struct ipsec_history *ipsec_gethist(struct mbuf *, int *);
348extern void ipsec_clearhist(struct mbuf *);
349#endif KERNEL
350#endif KERNEL_PRIVATE
1c79356b
A
351
352#ifndef KERNEL
91447636
A
353extern caddr_t ipsec_set_policy(char *, int);
354extern int ipsec_get_policylen(caddr_t);
355extern char *ipsec_dump_policy(caddr_t, char *);
1c79356b 356
91447636
A
357extern const char *ipsec_strerror(void);
358#endif KERNEL
1c79356b 359
0c530ab8 360#endif /* _NETINET6_IPSEC_H_ */