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