xref: /freebsd/sys/net/pfkeyv2.h (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*	$FreeBSD$	*/
2 /*	$KAME: pfkeyv2.h,v 1.37 2003/09/06 05:15:43 itojun Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * This file has been derived rfc 2367,
37  * And added some flags of SADB_KEY_FLAGS_ as SADB_X_EXT_.
38  *	sakane@ydc.co.jp
39  */
40 
41 #ifndef _NET_PFKEYV2_H_
42 #define _NET_PFKEYV2_H_
43 
44 /*
45 This file defines structures and symbols for the PF_KEY Version 2
46 key management interface. It was written at the U.S. Naval Research
47 Laboratory. This file is in the public domain. The authors ask that
48 you leave this credit intact on any copies of this file.
49 */
50 #ifndef __PFKEY_V2_H
51 #define __PFKEY_V2_H 1
52 
53 #define PF_KEY_V2 2
54 #define PFKEYV2_REVISION        199806L
55 
56 #define SADB_RESERVED    0
57 #define SADB_GETSPI      1
58 #define SADB_UPDATE      2
59 #define SADB_ADD         3
60 #define SADB_DELETE      4
61 #define SADB_GET         5
62 #define SADB_ACQUIRE     6
63 #define SADB_REGISTER    7
64 #define SADB_EXPIRE      8
65 #define SADB_FLUSH       9
66 #define SADB_DUMP        10
67 #define SADB_X_PROMISC   11
68 #define SADB_X_PCHANGE   12
69 
70 #define SADB_X_SPDUPDATE  13
71 #define SADB_X_SPDADD     14
72 #define SADB_X_SPDDELETE  15	/* by policy index */
73 #define SADB_X_SPDGET     16
74 #define SADB_X_SPDACQUIRE 17
75 #define SADB_X_SPDDUMP    18
76 #define SADB_X_SPDFLUSH   19
77 #define SADB_X_SPDSETIDX  20
78 #define SADB_X_SPDEXPIRE  21
79 #define SADB_X_SPDDELETE2 22	/* by policy id */
80 #define SADB_MAX          22
81 
82 struct sadb_msg {
83   u_int8_t sadb_msg_version;
84   u_int8_t sadb_msg_type;
85   u_int8_t sadb_msg_errno;
86   u_int8_t sadb_msg_satype;
87   u_int16_t sadb_msg_len;
88   u_int16_t sadb_msg_reserved;
89   u_int32_t sadb_msg_seq;
90   u_int32_t sadb_msg_pid;
91 };
92 
93 struct sadb_ext {
94   u_int16_t sadb_ext_len;
95   u_int16_t sadb_ext_type;
96 };
97 
98 struct sadb_sa {
99   u_int16_t sadb_sa_len;
100   u_int16_t sadb_sa_exttype;
101   u_int32_t sadb_sa_spi;
102   u_int8_t sadb_sa_replay;
103   u_int8_t sadb_sa_state;
104   u_int8_t sadb_sa_auth;
105   u_int8_t sadb_sa_encrypt;
106   u_int32_t sadb_sa_flags;
107 };
108 
109 struct sadb_lifetime {
110   u_int16_t sadb_lifetime_len;
111   u_int16_t sadb_lifetime_exttype;
112   u_int32_t sadb_lifetime_allocations;
113   u_int64_t sadb_lifetime_bytes;
114   u_int64_t sadb_lifetime_addtime;
115   u_int64_t sadb_lifetime_usetime;
116 };
117 
118 struct sadb_address {
119   u_int16_t sadb_address_len;
120   u_int16_t sadb_address_exttype;
121   u_int8_t sadb_address_proto;
122   u_int8_t sadb_address_prefixlen;
123   u_int16_t sadb_address_reserved;
124 };
125 
126 struct sadb_key {
127   u_int16_t sadb_key_len;
128   u_int16_t sadb_key_exttype;
129   u_int16_t sadb_key_bits;
130   u_int16_t sadb_key_reserved;
131 };
132 
133 struct sadb_ident {
134   u_int16_t sadb_ident_len;
135   u_int16_t sadb_ident_exttype;
136   u_int16_t sadb_ident_type;
137   u_int16_t sadb_ident_reserved;
138   u_int64_t sadb_ident_id;
139 };
140 
141 struct sadb_sens {
142   u_int16_t sadb_sens_len;
143   u_int16_t sadb_sens_exttype;
144   u_int32_t sadb_sens_dpd;
145   u_int8_t sadb_sens_sens_level;
146   u_int8_t sadb_sens_sens_len;
147   u_int8_t sadb_sens_integ_level;
148   u_int8_t sadb_sens_integ_len;
149   u_int32_t sadb_sens_reserved;
150 };
151 
152 struct sadb_prop {
153   u_int16_t sadb_prop_len;
154   u_int16_t sadb_prop_exttype;
155   u_int8_t sadb_prop_replay;
156   u_int8_t sadb_prop_reserved[3];
157 };
158 
159 struct sadb_comb {
160   u_int8_t sadb_comb_auth;
161   u_int8_t sadb_comb_encrypt;
162   u_int16_t sadb_comb_flags;
163   u_int16_t sadb_comb_auth_minbits;
164   u_int16_t sadb_comb_auth_maxbits;
165   u_int16_t sadb_comb_encrypt_minbits;
166   u_int16_t sadb_comb_encrypt_maxbits;
167   u_int32_t sadb_comb_reserved;
168   u_int32_t sadb_comb_soft_allocations;
169   u_int32_t sadb_comb_hard_allocations;
170   u_int64_t sadb_comb_soft_bytes;
171   u_int64_t sadb_comb_hard_bytes;
172   u_int64_t sadb_comb_soft_addtime;
173   u_int64_t sadb_comb_hard_addtime;
174   u_int64_t sadb_comb_soft_usetime;
175   u_int64_t sadb_comb_hard_usetime;
176 };
177 
178 struct sadb_supported {
179   u_int16_t sadb_supported_len;
180   u_int16_t sadb_supported_exttype;
181   u_int32_t sadb_supported_reserved;
182 };
183 
184 struct sadb_alg {
185   u_int8_t sadb_alg_id;
186   u_int8_t sadb_alg_ivlen;
187   u_int16_t sadb_alg_minbits;
188   u_int16_t sadb_alg_maxbits;
189   u_int16_t sadb_alg_reserved;
190 };
191 
192 struct sadb_spirange {
193   u_int16_t sadb_spirange_len;
194   u_int16_t sadb_spirange_exttype;
195   u_int32_t sadb_spirange_min;
196   u_int32_t sadb_spirange_max;
197   u_int32_t sadb_spirange_reserved;
198 };
199 
200 struct sadb_x_kmprivate {
201   u_int16_t sadb_x_kmprivate_len;
202   u_int16_t sadb_x_kmprivate_exttype;
203   u_int32_t sadb_x_kmprivate_reserved;
204 };
205 
206 /*
207  * XXX Additional SA Extension.
208  * mode: tunnel or transport
209  * reqid: to make SA unique nevertheless the address pair of SA are same.
210  *        Mainly it's for VPN.
211  */
212 struct sadb_x_sa2 {
213   u_int16_t sadb_x_sa2_len;
214   u_int16_t sadb_x_sa2_exttype;
215   u_int8_t sadb_x_sa2_mode;
216   u_int8_t sadb_x_sa2_reserved1;
217   u_int16_t sadb_x_sa2_reserved2;
218   u_int32_t sadb_x_sa2_sequence;	/* lowermost 32bit of sequence number */
219   u_int32_t sadb_x_sa2_reqid;
220 };
221 
222 /* XXX Policy Extension */
223 struct sadb_x_policy {
224   u_int16_t sadb_x_policy_len;
225   u_int16_t sadb_x_policy_exttype;
226   u_int16_t sadb_x_policy_type;		/* See policy type of ipsec.h */
227   u_int8_t sadb_x_policy_dir;		/* direction, see ipsec.h */
228   u_int8_t sadb_x_policy_scope;		/* scope, see ipsec.h */
229   u_int32_t sadb_x_policy_id;
230   u_int32_t sadb_x_policy_priority;
231 #define	sadb_x_policy_reserved	sadb_x_policy_scope
232 /* Policy with ifnet scope uses priority field to store ifindex */
233 #define	sadb_x_policy_ifindex	sadb_x_policy_priority
234 };
235 _Static_assert(sizeof(struct sadb_x_policy) == 16, "struct size mismatch");
236 
237 /*
238  * When policy_type == IPSEC, it is followed by some of
239  * the ipsec policy request.
240  * [total length of ipsec policy requests]
241  *	= (sadb_x_policy_len * sizeof(uint64_t) - sizeof(struct sadb_x_policy))
242  */
243 
244 /* XXX IPsec Policy Request Extension */
245 /*
246  * This structure is aligned 8 bytes.
247  */
248 struct sadb_x_ipsecrequest {
249   u_int16_t sadb_x_ipsecrequest_len;	/* structure length in 64 bits. */
250   u_int16_t sadb_x_ipsecrequest_proto;	/* See ipsec.h */
251   u_int8_t sadb_x_ipsecrequest_mode;	/* See IPSEC_MODE_XX in ipsec.h. */
252   u_int8_t sadb_x_ipsecrequest_level;	/* See IPSEC_LEVEL_XX in ipsec.h */
253   u_int16_t sadb_x_ipsecrequest_reqid;	/* See ipsec.h */
254 
255   /*
256    * followed by source IP address of SA, and immediately followed by
257    * destination IP address of SA.  These encoded into two of sockaddr
258    * structure without any padding.  Must set each sa_len exactly.
259    * Each of length of the sockaddr structure are not aligned to 64bits,
260    * but sum of x_request and addresses is aligned to 64bits.
261    */
262 };
263 
264 /* NAT-Traversal type, see RFC 3948 (and drafts). */
265 struct sadb_x_nat_t_type {
266   u_int16_t sadb_x_nat_t_type_len;
267   u_int16_t sadb_x_nat_t_type_exttype;
268   u_int8_t sadb_x_nat_t_type_type;
269   u_int8_t sadb_x_nat_t_type_reserved[3];
270 };
271 _Static_assert(sizeof(struct sadb_x_nat_t_type) == 8, "struct size mismatch");
272 
273 /* NAT-Traversal source or destination port. */
274 struct sadb_x_nat_t_port {
275   u_int16_t sadb_x_nat_t_port_len;
276   u_int16_t sadb_x_nat_t_port_exttype;
277   u_int16_t sadb_x_nat_t_port_port;
278   u_int16_t sadb_x_nat_t_port_reserved;
279 };
280 _Static_assert(sizeof(struct sadb_x_nat_t_port) == 8, "struct size mismatch");
281 
282 /* ESP fragmentation size. */
283 struct sadb_x_nat_t_frag {
284   u_int16_t sadb_x_nat_t_frag_len;
285   u_int16_t sadb_x_nat_t_frag_exttype;
286   u_int16_t sadb_x_nat_t_frag_fraglen;
287   u_int16_t sadb_x_nat_t_frag_reserved;
288 };
289 _Static_assert(sizeof(struct sadb_x_nat_t_frag) == 8, "struct size mismatch");
290 
291 /* Additional large replay window support
292  */
293 struct sadb_x_sa_replay {
294   u_int16_t sadb_x_sa_replay_len;
295   u_int16_t sadb_x_sa_replay_exttype;
296   u_int32_t sadb_x_sa_replay_replay;	/* in packets */
297 };
298 _Static_assert(sizeof(struct sadb_x_sa_replay) == 8, "struct size mismatch");
299 
300 #define SADB_EXT_RESERVED             0
301 #define SADB_EXT_SA                   1
302 #define SADB_EXT_LIFETIME_CURRENT     2
303 #define SADB_EXT_LIFETIME_HARD        3
304 #define SADB_EXT_LIFETIME_SOFT        4
305 #define SADB_EXT_ADDRESS_SRC          5
306 #define SADB_EXT_ADDRESS_DST          6
307 #define SADB_EXT_ADDRESS_PROXY        7
308 #define SADB_EXT_KEY_AUTH             8
309 #define SADB_EXT_KEY_ENCRYPT          9
310 #define SADB_EXT_IDENTITY_SRC         10
311 #define SADB_EXT_IDENTITY_DST         11
312 #define SADB_EXT_SENSITIVITY          12
313 #define SADB_EXT_PROPOSAL             13
314 #define SADB_EXT_SUPPORTED_AUTH       14
315 #define SADB_EXT_SUPPORTED_ENCRYPT    15
316 #define SADB_EXT_SPIRANGE             16
317 #define SADB_X_EXT_KMPRIVATE          17
318 #define SADB_X_EXT_POLICY             18
319 #define SADB_X_EXT_SA2                19
320 #define SADB_X_EXT_NAT_T_TYPE         20
321 #define SADB_X_EXT_NAT_T_SPORT        21
322 #define SADB_X_EXT_NAT_T_DPORT        22
323 #define SADB_X_EXT_NAT_T_OA           23	/* Deprecated. */
324 #define SADB_X_EXT_NAT_T_OAI          23	/* Peer's NAT_OA for src of SA. */
325 #define SADB_X_EXT_NAT_T_OAR          24	/* Peer's NAT_OA for dst of SA. */
326 #define SADB_X_EXT_NAT_T_FRAG         25	/* Manual MTU override. */
327 #define SADB_X_EXT_SA_REPLAY          26	/* Replay window override. */
328 #define	SADB_X_EXT_NEW_ADDRESS_SRC    27
329 #define	SADB_X_EXT_NEW_ADDRESS_DST    28
330 #define	SADB_EXT_MAX                  28
331 
332 #define SADB_SATYPE_UNSPEC	0
333 #define SADB_SATYPE_AH		2
334 #define SADB_SATYPE_ESP		3
335 #define SADB_SATYPE_RSVP	5
336 #define SADB_SATYPE_OSPFV2	6
337 #define SADB_SATYPE_RIPV2	7
338 #define SADB_SATYPE_MIP		8
339 #define SADB_X_SATYPE_IPCOMP	9
340 /*#define SADB_X_SATYPE_POLICY	10	obsolete, do not reuse */
341 #define SADB_X_SATYPE_TCPSIGNATURE	11
342 #define SADB_SATYPE_MAX		12
343 
344 #define SADB_SASTATE_LARVAL   0
345 #define SADB_SASTATE_MATURE   1
346 #define SADB_SASTATE_DYING    2
347 #define SADB_SASTATE_DEAD     3
348 #define SADB_SASTATE_MAX      3
349 
350 #define SADB_SAFLAGS_PFS      1
351 /* SADB_X_SAFLAGS_ESN was defined in sys/net/pfkeyv2.h in OpenBSD sources */
352 #define SADB_X_SAFLAGS_ESN    0x400
353 
354 /*
355  * Though some of these numbers (both _AALG and _EALG) appear to be
356  * IKEv2 numbers and others original IKE numbers, they have no meaning.
357  * These are constants that the various IKE daemons use to tell the kernel
358  * what cipher to use.
359  *
360  * Do not use these constants directly to decide which Transformation ID
361  * to send.  You are responsible for mapping them yourself.
362  */
363 #define SADB_AALG_NONE		0
364 #define SADB_AALG_MD5HMAC	2
365 #define SADB_AALG_SHA1HMAC	3
366 #define SADB_AALG_MAX		252
367 #define SADB_X_AALG_SHA2_256	5
368 #define SADB_X_AALG_SHA2_384	6
369 #define SADB_X_AALG_SHA2_512	7
370 #define SADB_X_AALG_RIPEMD160HMAC	8
371 #define SADB_X_AALG_AES_XCBC_MAC	9	/* RFC3566 */
372 #define SADB_X_AALG_AES128GMAC	11		/* RFC4543 + Errata1821 */
373 #define SADB_X_AALG_AES192GMAC	12
374 #define SADB_X_AALG_AES256GMAC	13
375 #define SADB_X_AALG_MD5		249	/* Keyed MD5 */
376 #define SADB_X_AALG_SHA		250	/* Keyed SHA */
377 #define SADB_X_AALG_NULL	251	/* null authentication */
378 #define SADB_X_AALG_TCP_MD5	252	/* Keyed TCP-MD5 (RFC2385) */
379 
380 #define SADB_EALG_NONE		0
381 #define SADB_EALG_DESCBC	2
382 #define SADB_EALG_3DESCBC	3
383 #define SADB_X_EALG_CAST128CBC	6
384 #define SADB_X_EALG_BLOWFISHCBC	7
385 #define SADB_EALG_NULL		11
386 #define SADB_X_EALG_RIJNDAELCBC	12
387 #define SADB_X_EALG_AES		12
388 #define	SADB_X_EALG_AESCBC	12
389 #define SADB_X_EALG_AESCTR	13
390 #define SADB_X_EALG_AESGCM8	18	/* RFC4106 */
391 #define SADB_X_EALG_AESGCM12	19
392 #define SADB_X_EALG_AESGCM16	20
393 #define SADB_X_EALG_CAMELLIACBC	22
394 #define SADB_X_EALG_AESGMAC	23	/* RFC4543 + Errata1821 */
395 #define SADB_EALG_MAX		23	/* !!! keep updated !!! */
396 
397 /* private allocations - based on RFC2407/IANA assignment */
398 #define SADB_X_CALG_NONE	0
399 #define SADB_X_CALG_OUI		1
400 #define SADB_X_CALG_DEFLATE	2
401 #define SADB_X_CALG_LZS		3
402 #define SADB_X_CALG_MAX		4
403 
404 #define SADB_IDENTTYPE_RESERVED   0
405 #define SADB_IDENTTYPE_PREFIX     1
406 #define SADB_IDENTTYPE_FQDN       2
407 #define SADB_IDENTTYPE_USERFQDN   3
408 #define SADB_X_IDENTTYPE_ADDR     4
409 #define SADB_IDENTTYPE_MAX        4
410 
411 /* `flags' in sadb_sa structure holds followings */
412 #define SADB_X_EXT_NONE		0x0000	/* i.e. new format. */
413 #define SADB_X_EXT_OLD		0x0001	/* old format. */
414 
415 #define SADB_X_EXT_IV4B		0x0010	/* IV length of 4 bytes in use */
416 #define SADB_X_EXT_DERIV	0x0020	/* DES derived */
417 #define SADB_X_EXT_CYCSEQ	0x0040	/* allowing to cyclic sequence. */
418 
419 	/* three of followings are exclusive flags each them */
420 #define SADB_X_EXT_PSEQ		0x0000	/* sequencial padding for ESP */
421 #define SADB_X_EXT_PRAND	0x0100	/* random padding for ESP */
422 #define SADB_X_EXT_PZERO	0x0200	/* zero padding for ESP */
423 #define SADB_X_EXT_PMASK	0x0300	/* mask for padding flag */
424 
425 #if 1
426 #define SADB_X_EXT_RAWCPI	0x0080	/* use well known CPI (IPComp) */
427 #endif
428 
429 #define SADB_KEY_FLAGS_MAX	0x0fff
430 
431 /* SPI size for PF_KEYv2 */
432 #define PFKEY_SPI_SIZE	sizeof(u_int32_t)
433 
434 /* Identifier for menber of lifetime structure */
435 #define SADB_X_LIFETIME_ALLOCATIONS	0
436 #define SADB_X_LIFETIME_BYTES		1
437 #define SADB_X_LIFETIME_ADDTIME		2
438 #define SADB_X_LIFETIME_USETIME		3
439 
440 /* The rate for SOFT lifetime against HARD one. */
441 #define PFKEY_SOFT_LIFETIME_RATE	80
442 
443 /* Utilities */
444 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
445 #define	PFKEY_EXTLEN(msg) \
446 	PFKEY_UNUNIT64(((struct sadb_ext *)(msg))->sadb_ext_len)
447 #define PFKEY_ADDR_PREFIX(ext) \
448 	(((struct sadb_address *)(ext))->sadb_address_prefixlen)
449 #define PFKEY_ADDR_PROTO(ext) \
450 	(((struct sadb_address *)(ext))->sadb_address_proto)
451 #define PFKEY_ADDR_SADDR(ext) \
452 	((struct sockaddr *)((caddr_t)(ext) + sizeof(struct sadb_address)))
453 
454 /* in 64bits */
455 #define	PFKEY_UNUNIT64(a)	((a) << 3)
456 #define	PFKEY_UNIT64(a)		((a) >> 3)
457 
458 #endif /* __PFKEY_V2_H */
459 
460 #endif /* _NET_PFKEYV2_H_ */
461