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