1/*	$FreeBSD$	*/
2/*	$KAME: keydb.h,v 1.14 2000/08/02 17:58:26 sakane 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#ifndef _NETIPSEC_KEYDB_H_
36#define _NETIPSEC_KEYDB_H_
37
38#ifdef _KERNEL
39#include <sys/counter.h>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42
43#include <netipsec/key_var.h>
44#include <opencrypto/_cryptodev.h>
45
46#ifndef _SOCKADDR_UNION_DEFINED
47#define	_SOCKADDR_UNION_DEFINED
48/*
49 * The union of all possible address formats we handle.
50 */
51union sockaddr_union {
52	struct sockaddr		sa;
53	struct sockaddr_in	sin;
54	struct sockaddr_in6	sin6;
55};
56#endif /* _SOCKADDR_UNION_DEFINED */
57
58/* Security Assocciation Index */
59/* NOTE: Ensure to be same address family */
60struct secasindex {
61	union sockaddr_union src;	/* source address for SA */
62	union sockaddr_union dst;	/* destination address for SA */
63	uint8_t proto;			/* IPPROTO_ESP or IPPROTO_AH */
64	uint8_t mode;			/* mode of protocol, see ipsec.h */
65	uint32_t reqid;			/* reqid id who owned this SA */
66					/* see IPSEC_MANUAL_REQID_MAX. */
67};
68
69/*
70 * In order to split out the keydb implementation from that of the
71 * PF_KEY sockets we need to define a few structures that while they
72 * may seem common are likely to diverge over time.
73 */
74
75/* sadb_identity */
76struct secident {
77	u_int16_t type;
78	u_int64_t id;
79};
80
81/* sadb_key */
82struct seckey {
83	u_int16_t bits;
84	char *key_data;
85};
86
87struct seclifetime {
88	u_int32_t allocations;
89	u_int64_t bytes;
90	u_int64_t addtime;
91	u_int64_t usetime;
92};
93
94struct secnatt {
95	union sockaddr_union oai;	/* original addresses of initiator */
96	union sockaddr_union oar;	/* original address of responder */
97	uint16_t sport;			/* source port */
98	uint16_t dport;			/* destination port */
99	uint16_t cksum;			/* checksum delta */
100	uint16_t flags;
101#define	IPSEC_NATT_F_OAI	0x0001
102#define	IPSEC_NATT_F_OAR	0x0002
103};
104
105/* Security Association Data Base */
106TAILQ_HEAD(secasvar_queue, secasvar);
107struct secashead {
108	TAILQ_ENTRY(secashead) chain;
109	LIST_ENTRY(secashead) addrhash;	/* hash by sproto+src+dst addresses */
110	LIST_ENTRY(secashead) drainq;	/* used ONLY by flush callout */
111
112	struct secasindex saidx;
113
114	struct secident *idents;	/* source identity */
115	struct secident *identd;	/* destination identity */
116					/* XXX I don't know how to use them. */
117
118	volatile u_int refcnt;		/* reference count */
119	uint8_t state;			/* MATURE or DEAD. */
120	struct secasvar_queue savtree_alive;	/* MATURE and DYING SA */
121	struct secasvar_queue savtree_larval;	/* LARVAL SA */
122};
123
124struct xformsw;
125struct enc_xform;
126struct auth_hash;
127struct comp_algo;
128
129/*
130 * Security Association
131 *
132 * For INBOUND packets we do SA lookup using SPI, thus only SPIHASH is used.
133 * For OUTBOUND packets there may be several SA suitable for packet.
134 * We use key_preferred_oldsa variable to choose better SA. First of we do
135 * lookup for suitable SAH using packet's saidx. Then we use SAH's savtree
136 * to search better candidate. The newer SA (by created time) are placed
137 * in the beginning of the savtree list. There is no preference between
138 * DYING and MATURE.
139 *
140 * NB: Fields with a tdb_ prefix are part of the "glue" used
141 *     to interface to the OpenBSD crypto support.  This was done
142 *     to distinguish this code from the mainline KAME code.
143 * NB: Fields are sorted on the basis of the frequency of changes, i.e.
144 *     constants and unchangeable fields are going first.
145 * NB: if you want to change this structure, check that this will not break
146 *     key_updateaddresses().
147 */
148struct secasvar {
149	uint32_t spi;			/* SPI Value, network byte order */
150	uint32_t flags;			/* holder for SADB_KEY_FLAGS */
151	uint32_t seq;			/* sequence number */
152	pid_t pid;			/* message's pid */
153	u_int ivlen;			/* length of IV */
154
155	struct secashead *sah;		/* back pointer to the secashead */
156	struct seckey *key_auth;	/* Key for Authentication */
157	struct seckey *key_enc;	        /* Key for Encryption */
158	struct secreplay *replay;	/* replay prevention */
159	struct secnatt *natt;		/* NAT-T config */
160	struct mtx *lock;		/* update/access lock */
161
162	const struct xformsw *tdb_xform;	/* transform */
163	const struct enc_xform *tdb_encalgxform;/* encoding algorithm */
164	const struct auth_hash *tdb_authalgxform;/* authentication algorithm */
165	const struct comp_algo *tdb_compalgxform;/* compression algorithm */
166	crypto_session_t tdb_cryptoid;		/* crypto session */
167
168	uint8_t alg_auth;		/* Authentication Algorithm Identifier*/
169	uint8_t alg_enc;		/* Cipher Algorithm Identifier */
170	uint8_t alg_comp;		/* Compression Algorithm Identifier */
171	uint8_t state;			/* Status of this SA (pfkeyv2.h) */
172
173	counter_u64_t lft_c;		/* CURRENT lifetime */
174#define	lft_c_allocations	lft_c
175#define	lft_c_bytes		lft_c + 1
176	struct seclifetime *lft_h;	/* HARD lifetime */
177	struct seclifetime *lft_s;	/* SOFT lifetime */
178
179	uint64_t created;		/* time when SA was created */
180	uint64_t firstused;		/* time when SA was first used */
181
182	TAILQ_ENTRY(secasvar) chain;
183	LIST_ENTRY(secasvar) spihash;
184	LIST_ENTRY(secasvar) drainq;	/* used ONLY by flush callout */
185
186	uint64_t cntr;			/* counter for GCM and CTR */
187	volatile u_int refcnt;		/* reference count */
188};
189
190#define	SECASVAR_LOCK(_sav)		mtx_lock((_sav)->lock)
191#define	SECASVAR_UNLOCK(_sav)		mtx_unlock((_sav)->lock)
192#define	SECASVAR_LOCK_ASSERT(_sav)	mtx_assert((_sav)->lock, MA_OWNED)
193#define	SAV_ISGCM(_sav)							\
194			((_sav)->alg_enc == SADB_X_EALG_AESGCM8 ||	\
195			(_sav)->alg_enc == SADB_X_EALG_AESGCM12 ||	\
196			(_sav)->alg_enc == SADB_X_EALG_AESGCM16)
197#define	SAV_ISCTR(_sav) ((_sav)->alg_enc == SADB_X_EALG_AESCTR)
198#define SAV_ISCTRORGCM(_sav)	(SAV_ISCTR((_sav)) || SAV_ISGCM((_sav)))
199
200/* Replay prevention, protected by SECASVAR_LOCK:
201 *  (m) locked by mtx
202 *  (c) read only except during creation / free
203 */
204struct secreplay {
205	u_int32_t count;	/* (m) */
206	u_int wsize;		/* (c) window size, i.g. 4 bytes */
207	u_int32_t seq;		/* (m) used by sender */
208	u_int32_t lastseq;	/* (m) used by receiver */
209	u_int32_t *bitmap;	/* (m) used by receiver */
210	u_int bitmap_size;	/* (c) size of the bitmap array */
211	int overflow;		/* (m) overflow flag */
212};
213
214/* socket table due to send PF_KEY messages. */
215struct secreg {
216	LIST_ENTRY(secreg) chain;
217
218	struct socket *so;
219};
220
221/* acquiring list table. */
222struct secacq {
223	LIST_ENTRY(secacq) chain;
224	LIST_ENTRY(secacq) addrhash;
225	LIST_ENTRY(secacq) seqhash;
226
227	struct secasindex saidx;
228	uint32_t seq;		/* sequence number */
229	time_t created;		/* for lifetime */
230	int count;		/* for lifetime */
231};
232
233#endif /* _KERNEL */
234
235#endif /* _NETIPSEC_KEYDB_H_ */
236