1/*
2 * Copyright (c) 2000-2011 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*	$KAME: pfkeyv2.h,v 1.10 2000/03/22 07:04:20 sakane Exp $	*/
29
30/*
31 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 *    may be used to endorse or promote products derived from this software
44 *    without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59/*
60 * This file has been derived rfc 2367,
61 * And added some flags of SADB_KEY_FLAGS_ as SADB_X_EXT_.
62 *	sakane@ydc.co.jp
63 */
64
65#ifndef _NET_PFKEYV2_H_
66#define _NET_PFKEYV2_H_
67#include <sys/appleapiopts.h>
68
69/*
70This file defines structures and symbols for the PF_KEY Version 2
71key management interface. It was written at the U.S. Naval Research
72Laboratory. This file is in the public domain. The authors ask that
73you leave this credit intact on any copies of this file.
74*/
75#ifndef __PFKEY_V2_H
76#define __PFKEY_V2_H 1
77
78#define PF_KEY_V2 2
79#define PFKEYV2_REVISION        199806L
80
81#define SADB_RESERVED    0
82#define SADB_GETSPI      1
83#define SADB_UPDATE      2
84#define SADB_ADD         3
85#define SADB_DELETE      4
86#define SADB_GET         5
87#define SADB_ACQUIRE     6
88#define SADB_REGISTER    7
89#define SADB_EXPIRE      8
90#define SADB_FLUSH       9
91#define SADB_DUMP        10
92#define SADB_X_PROMISC   11
93#define SADB_X_PCHANGE   12
94
95#define SADB_X_SPDUPDATE  13
96#define SADB_X_SPDADD     14
97#define SADB_X_SPDDELETE  15	/* by policy index */
98#define SADB_X_SPDGET     16
99#define SADB_X_SPDACQUIRE 17
100#define SADB_X_SPDDUMP    18
101#define SADB_X_SPDFLUSH   19
102#define SADB_X_SPDSETIDX  20
103#define SADB_X_SPDEXPIRE  21
104#define SADB_X_SPDDELETE2 22	/* by policy id */
105#define SADB_GETSASTAT    23
106#define SADB_MAX          23
107
108struct sadb_msg {
109  u_int8_t sadb_msg_version;
110  u_int8_t sadb_msg_type;
111  u_int8_t sadb_msg_errno;
112  u_int8_t sadb_msg_satype;
113  u_int16_t sadb_msg_len;
114  u_int16_t sadb_msg_reserved;
115  u_int32_t sadb_msg_seq;
116  u_int32_t sadb_msg_pid;
117};
118
119struct sadb_ext {
120  u_int16_t sadb_ext_len;
121  u_int16_t sadb_ext_type;
122};
123
124struct sadb_sa {
125  u_int16_t sadb_sa_len;
126  u_int16_t sadb_sa_exttype;
127  u_int32_t sadb_sa_spi;
128  u_int8_t sadb_sa_replay;
129  u_int8_t sadb_sa_state;
130  u_int8_t sadb_sa_auth;
131  u_int8_t sadb_sa_encrypt;
132  u_int32_t sadb_sa_flags;
133};
134
135#ifdef PRIVATE
136struct sadb_sa_2 {
137	struct sadb_sa	sa;
138	u_int16_t		sadb_sa_natt_port;
139	u_int16_t		sadb_reserved0;
140	u_int32_t		sadb_reserved1;
141};
142#endif /* PRIVATE */
143
144struct sadb_lifetime {
145  u_int16_t sadb_lifetime_len;
146  u_int16_t sadb_lifetime_exttype;
147  u_int32_t sadb_lifetime_allocations;
148  u_int64_t sadb_lifetime_bytes;
149  u_int64_t sadb_lifetime_addtime;
150  u_int64_t sadb_lifetime_usetime;
151};
152
153struct sadb_address {
154  u_int16_t sadb_address_len;
155  u_int16_t sadb_address_exttype;
156  u_int8_t sadb_address_proto;
157  u_int8_t sadb_address_prefixlen;
158  u_int16_t sadb_address_reserved;
159};
160
161struct sadb_key {
162  u_int16_t sadb_key_len;
163  u_int16_t sadb_key_exttype;
164  u_int16_t sadb_key_bits;
165  u_int16_t sadb_key_reserved;
166};
167
168struct sadb_ident {
169  u_int16_t sadb_ident_len;
170  u_int16_t sadb_ident_exttype;
171  u_int16_t sadb_ident_type;
172  u_int16_t sadb_ident_reserved;
173  u_int64_t sadb_ident_id;
174};
175
176struct sadb_sens {
177  u_int16_t sadb_sens_len;
178  u_int16_t sadb_sens_exttype;
179  u_int32_t sadb_sens_dpd;
180  u_int8_t sadb_sens_sens_level;
181  u_int8_t sadb_sens_sens_len;
182  u_int8_t sadb_sens_integ_level;
183  u_int8_t sadb_sens_integ_len;
184  u_int32_t sadb_sens_reserved;
185};
186
187struct sadb_prop {
188  u_int16_t sadb_prop_len;
189  u_int16_t sadb_prop_exttype;
190  u_int8_t sadb_prop_replay;
191  u_int8_t sadb_prop_reserved[3];
192};
193
194struct sadb_comb {
195  u_int8_t sadb_comb_auth;
196  u_int8_t sadb_comb_encrypt;
197  u_int16_t sadb_comb_flags;
198  u_int16_t sadb_comb_auth_minbits;
199  u_int16_t sadb_comb_auth_maxbits;
200  u_int16_t sadb_comb_encrypt_minbits;
201  u_int16_t sadb_comb_encrypt_maxbits;
202  u_int32_t sadb_comb_reserved;
203  u_int32_t sadb_comb_soft_allocations;
204  u_int32_t sadb_comb_hard_allocations;
205  u_int64_t sadb_comb_soft_bytes;
206  u_int64_t sadb_comb_hard_bytes;
207  u_int64_t sadb_comb_soft_addtime;
208  u_int64_t sadb_comb_hard_addtime;
209  u_int64_t sadb_comb_soft_usetime;
210  u_int64_t sadb_comb_hard_usetime;
211};
212
213struct sadb_supported {
214  u_int16_t sadb_supported_len;
215  u_int16_t sadb_supported_exttype;
216  u_int32_t sadb_supported_reserved;
217};
218
219struct sadb_alg {
220  u_int8_t sadb_alg_id;
221  u_int8_t sadb_alg_ivlen;
222  u_int16_t sadb_alg_minbits;
223  u_int16_t sadb_alg_maxbits;
224  u_int16_t sadb_alg_reserved;
225};
226
227struct sadb_spirange {
228  u_int16_t sadb_spirange_len;
229  u_int16_t sadb_spirange_exttype;
230  u_int32_t sadb_spirange_min;
231  u_int32_t sadb_spirange_max;
232  u_int32_t sadb_spirange_reserved;
233};
234
235struct sadb_x_kmprivate {
236  u_int16_t sadb_x_kmprivate_len;
237  u_int16_t sadb_x_kmprivate_exttype;
238  u_int32_t sadb_x_kmprivate_reserved;
239};
240
241/*
242 * XXX Additional SA Extension.
243 * mode: tunnel or transport
244 * reqid: to make SA unique nevertheless the address pair of SA are same.
245 *        Mainly it's for VPN.
246 */
247struct sadb_x_sa2 {
248  u_int16_t sadb_x_sa2_len;
249  u_int16_t sadb_x_sa2_exttype;
250  u_int8_t sadb_x_sa2_mode;
251  u_int8_t sadb_x_sa2_reserved1;
252  u_int16_t sadb_x_sa2_reserved2;
253  u_int32_t sadb_x_sa2_sequence;
254  u_int32_t sadb_x_sa2_reqid;
255};
256
257/* XXX Policy Extension */
258/* sizeof(struct sadb_x_policy) == 16 */
259struct sadb_x_policy {
260  u_int16_t sadb_x_policy_len;
261  u_int16_t sadb_x_policy_exttype;
262  u_int16_t sadb_x_policy_type;		/* See policy type of ipsec.h */
263  u_int8_t sadb_x_policy_dir;		/* direction, see ipsec.h */
264  u_int8_t sadb_x_policy_reserved;
265  u_int32_t sadb_x_policy_id;
266  u_int32_t sadb_x_policy_reserved2;
267};
268/*
269 * When policy_type == IPSEC, it is followed by some of
270 * the ipsec policy request.
271 * [total length of ipsec policy requests]
272 *	= (sadb_x_policy_len * sizeof(uint64_t) - sizeof(struct sadb_x_policy))
273 */
274
275/* XXX IPsec Policy Request Extension */
276/*
277 * This structure is aligned 8 bytes.
278 */
279struct sadb_x_ipsecrequest {
280  u_int16_t sadb_x_ipsecrequest_len;	/* structure length aligned to 8 bytes.
281					 * This value is true length of bytes.
282					 * Not in units of 64 bits. */
283  u_int16_t sadb_x_ipsecrequest_proto;	/* See ipsec.h */
284  u_int8_t sadb_x_ipsecrequest_mode;	/* See IPSEC_MODE_XX in ipsec.h. */
285  u_int8_t sadb_x_ipsecrequest_level;	/* See IPSEC_LEVEL_XX in ipsec.h */
286  u_int16_t sadb_x_ipsecrequest_reqid;	/* See ipsec.h */
287
288  /*
289   * followed by source IP address of SA, and immediately followed by
290   * destination IP address of SA.  These encoded into two of sockaddr
291   * structure without any padding.  Must set each sa_len exactly.
292   * Each of length of the sockaddr structure are not aligned to 64bits,
293   * but sum of x_request and addresses is aligned to 64bits.
294   */
295};
296
297struct sadb_session_id {
298        u_int16_t            sadb_session_id_len;
299        u_int16_t            sadb_session_id_exttype;
300       /* [0] is an arbitrary handle that means something only for requester
301        * [1] is a global session id for lookups in the kernel and racoon.
302        */
303        u_int64_t            sadb_session_id_v[2];
304} __attribute__ ((aligned(8)));
305
306struct sastat {
307	u_int32_t            spi;		/* SPI Value, network byte order */
308	u_int32_t            created;	        /* for lifetime */
309	struct sadb_lifetime lft_c;	        /* CURRENT lifetime. */
310}; // no need to align
311
312struct sadb_sastat {
313        u_int16_t            sadb_sastat_len;
314        u_int16_t            sadb_sastat_exttype;
315        u_int32_t            sadb_sastat_dir;
316        u_int32_t            sadb_sastat_reserved;
317        u_int32_t            sadb_sastat_list_len;
318        /* list of struct sastat comes after */
319} __attribute__ ((aligned(8)));
320
321#define SADB_EXT_RESERVED             0
322#define SADB_EXT_SA                   1
323#define SADB_EXT_LIFETIME_CURRENT     2
324#define SADB_EXT_LIFETIME_HARD        3
325#define SADB_EXT_LIFETIME_SOFT        4
326#define SADB_EXT_ADDRESS_SRC          5
327#define SADB_EXT_ADDRESS_DST          6
328#define SADB_EXT_ADDRESS_PROXY        7
329#define SADB_EXT_KEY_AUTH             8
330#define SADB_EXT_KEY_ENCRYPT          9
331#define SADB_EXT_IDENTITY_SRC         10
332#define SADB_EXT_IDENTITY_DST         11
333#define SADB_EXT_SENSITIVITY          12
334#define SADB_EXT_PROPOSAL             13
335#define SADB_EXT_SUPPORTED_AUTH       14
336#define SADB_EXT_SUPPORTED_ENCRYPT    15
337#define SADB_EXT_SPIRANGE             16
338#define SADB_X_EXT_KMPRIVATE          17
339#define SADB_X_EXT_POLICY             18
340#define SADB_X_EXT_SA2                19
341#define SADB_EXT_SESSION_ID           20
342#define SADB_EXT_SASTAT               21
343#define SADB_EXT_MAX                  21
344
345#define SADB_SATYPE_UNSPEC	0
346#define SADB_SATYPE_AH		2
347#define SADB_SATYPE_ESP		3
348#define SADB_SATYPE_RSVP	5
349#define SADB_SATYPE_OSPFV2	6
350#define SADB_SATYPE_RIPV2	7
351#define SADB_SATYPE_MIP		8
352#define SADB_X_SATYPE_IPCOMP	9
353#define SADB_X_SATYPE_POLICY	10
354#define SADB_SATYPE_MAX		11
355
356#define SADB_SASTATE_LARVAL   0
357#define SADB_SASTATE_MATURE   1
358#define SADB_SASTATE_DYING    2
359#define SADB_SASTATE_DEAD     3
360#define SADB_SASTATE_MAX      3
361
362#define SADB_SAFLAGS_PFS      1
363
364/* RFC2367 numbers - meets RFC2407 */
365#define SADB_AALG_NONE		0
366#define SADB_AALG_MD5HMAC	1	/*2*/
367#define SADB_AALG_SHA1HMAC	2	/*3*/
368#define SADB_AALG_MAX		8
369/* private allocations - based on RFC2407/IANA assignment */
370#define SADB_X_AALG_SHA2_256	6	/*5*/
371#define SADB_X_AALG_SHA2_384	7	/*6*/
372#define SADB_X_AALG_SHA2_512	8	/*7*/
373/* private allocations should use 249-255 (RFC2407) */
374#define SADB_X_AALG_MD5		3	/*249*/	/* Keyed MD5 */
375#define SADB_X_AALG_SHA		4	/*250*/	/* Keyed SHA */
376#define SADB_X_AALG_NULL	5	/*251*/	/* null authentication */
377
378/* RFC2367 numbers - meets RFC2407 */
379#define SADB_EALG_NONE		0
380#define SADB_EALG_DESCBC	1	/*2*/
381#define SADB_EALG_3DESCBC	2	/*3*/
382#define SADB_EALG_NULL		3	/*11*/
383#define SADB_EALG_MAX		12
384/* private allocations - based on RFC2407/IANA assignment */
385#define SADB_X_EALG_CAST128CBC	5	/*6*/
386#define SADB_X_EALG_BLOWFISHCBC	4	/*7*/
387#define SADB_X_EALG_RIJNDAELCBC	12
388#define SADB_X_EALG_AESCBC      12
389#define SADB_X_EALG_AES		12
390/* private allocations should use 249-255 (RFC2407) */
391
392#if 1	/*nonstandard */
393#define SADB_X_CALG_NONE	0
394#define SADB_X_CALG_OUI		1
395#define SADB_X_CALG_DEFLATE	2
396#define SADB_X_CALG_LZS		3
397#define SADB_X_CALG_MAX		4
398#endif
399
400#define SADB_IDENTTYPE_RESERVED   0
401#define SADB_IDENTTYPE_PREFIX     1
402#define SADB_IDENTTYPE_FQDN       2
403#define SADB_IDENTTYPE_USERFQDN   3
404#define SADB_X_IDENTTYPE_ADDR     4
405#define SADB_IDENTTYPE_MAX        4
406
407/* `flags' in sadb_sa structure holds followings */
408#define SADB_X_EXT_NONE		0x0000	/* i.e. new format. */
409#define SADB_X_EXT_OLD		0x0001	/* old format. */
410#ifdef PRIVATE
411#define SADB_X_EXT_NATT				0x0002	/* Use UDP encapsulation to traverse NAT */
412#define SADB_X_EXT_NATT_KEEPALIVE	0x0004	/* Local node is behind NAT, send keepalives */
413											/* Should only be set for outbound SAs */
414#define SADB_X_EXT_NATT_MULTIPLEUSERS 0x0008	/* For use on VPN server - support multiple users 	*/
415
416#endif /* PRIVATE */
417
418#define SADB_X_EXT_IV4B		0x0010	/* IV length of 4 bytes in use */
419#define SADB_X_EXT_DERIV	0x0020	/* DES derived */
420#define SADB_X_EXT_CYCSEQ	0x0040	/* allowing to cyclic sequence. */
421
422	/* three of followings are exclusive flags each them */
423#define SADB_X_EXT_PSEQ		0x0000	/* sequencial padding for ESP */
424#define SADB_X_EXT_PRAND	0x0100	/* random padding for ESP */
425#define SADB_X_EXT_PZERO	0x0200	/* zero padding for ESP */
426#define SADB_X_EXT_PMASK	0x0300	/* mask for padding flag */
427
428#ifdef PRIVATE
429#define SADB_X_EXT_NATT_DETECTED_PEER 0x1000
430#define SADB_X_EXT_ESP_KEEPALIVE      0x2000
431#define SADB_X_EXT_PUNT_RX_KEEPALIVE  0x4000
432#endif /* PRIVATE */
433
434#if 1
435#define SADB_X_EXT_RAWCPI	0x0080	/* use well known CPI (IPComp) */
436#endif
437
438#define SADB_KEY_FLAGS_MAX	0x0fff
439
440/* SPI size for PF_KEYv2 */
441#define PFKEY_SPI_SIZE	sizeof(u_int32_t)
442
443/* Identifier for menber of lifetime structure */
444#define SADB_X_LIFETIME_ALLOCATIONS	0
445#define SADB_X_LIFETIME_BYTES		1
446#define SADB_X_LIFETIME_ADDTIME		2
447#define SADB_X_LIFETIME_USETIME		3
448
449/* The rate for SOFT lifetime against HARD one. */
450#define PFKEY_SOFT_LIFETIME_RATE	80
451
452/* Utilities */
453#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
454#define	PFKEY_EXTLEN(msg) \
455	PFKEY_UNUNIT64(((struct sadb_ext *)(msg))->sadb_ext_len)
456#define PFKEY_ADDR_PREFIX(ext) \
457	(((struct sadb_address *)(ext))->sadb_address_prefixlen)
458#define PFKEY_ADDR_PROTO(ext) \
459	(((struct sadb_address *)(ext))->sadb_address_proto)
460#define PFKEY_ADDR_SADDR(ext) \
461	((struct sockaddr *)((caddr_t)(ext) + sizeof(struct sadb_address)))
462
463/* in 64bits */
464#define	PFKEY_UNUNIT64(a)	((a) << 3)
465#define	PFKEY_UNIT64(a)		((a) >> 3)
466
467#endif /* __PFKEY_V2_H */
468
469#endif /* _NET_PFKEYV2_H_ */
470