1105197Ssam/*	$FreeBSD$	*/
2105197Ssam/*	$KAME: keydb.h,v 1.14 2000/08/02 17:58:26 sakane Exp $	*/
3105197Ssam
4139823Simp/*-
5105197Ssam * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6105197Ssam * All rights reserved.
7105197Ssam *
8105197Ssam * Redistribution and use in source and binary forms, with or without
9105197Ssam * modification, are permitted provided that the following conditions
10105197Ssam * are met:
11105197Ssam * 1. Redistributions of source code must retain the above copyright
12105197Ssam *    notice, this list of conditions and the following disclaimer.
13105197Ssam * 2. Redistributions in binary form must reproduce the above copyright
14105197Ssam *    notice, this list of conditions and the following disclaimer in the
15105197Ssam *    documentation and/or other materials provided with the distribution.
16105197Ssam * 3. Neither the name of the project nor the names of its contributors
17105197Ssam *    may be used to endorse or promote products derived from this software
18105197Ssam *    without specific prior written permission.
19105197Ssam *
20105197Ssam * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21105197Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22105197Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23105197Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24105197Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25105197Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26105197Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27105197Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28105197Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29105197Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30105197Ssam * SUCH DAMAGE.
31105197Ssam */
32105197Ssam
33105197Ssam#ifndef _NETIPSEC_KEYDB_H_
34105197Ssam#define _NETIPSEC_KEYDB_H_
35105197Ssam
36105197Ssam#ifdef _KERNEL
37105197Ssam
38105197Ssam#include <netipsec/key_var.h>
39105197Ssam
40135615Smlaier#ifndef _SOCKADDR_UNION_DEFINED
41135615Smlaier#define	_SOCKADDR_UNION_DEFINED
42105197Ssam/*
43105197Ssam * The union of all possible address formats we handle.
44105197Ssam */
45105197Ssamunion sockaddr_union {
46105197Ssam	struct sockaddr		sa;
47105197Ssam	struct sockaddr_in	sin;
48105197Ssam	struct sockaddr_in6	sin6;
49105197Ssam};
50135615Smlaier#endif /* _SOCKADDR_UNION_DEFINED */
51105197Ssam
52105197Ssam/* Security Assocciation Index */
53105197Ssam/* NOTE: Ensure to be same address family */
54105197Ssamstruct secasindex {
55204074Spjd	union sockaddr_union src;	/* source address for SA */
56105197Ssam	union sockaddr_union dst;	/* destination address for SA */
57105197Ssam	u_int16_t proto;		/* IPPROTO_ESP or IPPROTO_AH */
58105197Ssam	u_int8_t mode;			/* mode of protocol, see ipsec.h */
59105197Ssam	u_int32_t reqid;		/* reqid id who owned this SA */
60105197Ssam					/* see IPSEC_MANUAL_REQID_MAX. */
61105197Ssam};
62105197Ssam
63157123Sgnn/*
64157123Sgnn * In order to split out the keydb implementation from that of the
65157123Sgnn * PF_KEY sockets we need to define a few structures that while they
66157123Sgnn * may seem common are likely to diverge over time.
67157123Sgnn */
68157123Sgnn
69157123Sgnn/* sadb_identity */
70157123Sgnnstruct secident {
71157123Sgnn	u_int16_t type;
72157123Sgnn	u_int64_t id;
73157123Sgnn};
74157123Sgnn
75157123Sgnn/* sadb_key */
76157123Sgnnstruct seckey {
77157123Sgnn	u_int16_t bits;
78157123Sgnn	char *key_data;
79157123Sgnn};
80157123Sgnn
81157123Sgnnstruct seclifetime {
82157123Sgnn	u_int32_t allocations;
83157123Sgnn	u_int64_t bytes;
84157123Sgnn	u_int64_t addtime;
85157123Sgnn	u_int64_t usetime;
86157123Sgnn};
87157123Sgnn
88214250Sbzunion sa_route_union {
89214250Sbz	struct route		sa_route;
90214250Sbz	struct route		sin_route;	/* Duplicate for consistency. */
91214250Sbz	struct route_in6	sin6_route;
92214250Sbz};
93214250Sbz
94105197Ssam/* Security Association Data Base */
95105197Ssamstruct secashead {
96105197Ssam	LIST_ENTRY(secashead) chain;
97105197Ssam
98105197Ssam	struct secasindex saidx;
99105197Ssam
100157123Sgnn	struct secident *idents;	/* source identity */
101157123Sgnn	struct secident *identd;	/* destination identity */
102105197Ssam					/* XXX I don't know how to use them. */
103105197Ssam
104105197Ssam	u_int8_t state;			/* MATURE or DEAD. */
105105197Ssam	LIST_HEAD(_satree, secasvar) savtree[SADB_SASTATE_MAX+1];
106105197Ssam					/* SA chain */
107105197Ssam					/* The first of this list is newer SA */
108105197Ssam
109214250Sbz	union sa_route_union route_cache;
110105197Ssam};
111105197Ssam
112105197Ssamstruct xformsw;
113105197Ssamstruct enc_xform;
114105197Ssamstruct auth_hash;
115105197Ssamstruct comp_algo;
116105197Ssam
117105197Ssam/* Security Association */
118105197Ssamstruct secasvar {
119105197Ssam	LIST_ENTRY(secasvar) chain;
120119643Ssam	struct mtx lock;		/* update/access lock */
121105197Ssam
122105197Ssam	u_int refcnt;			/* reference count */
123105197Ssam	u_int8_t state;			/* Status of this Association */
124105197Ssam
125105197Ssam	u_int8_t alg_auth;		/* Authentication Algorithm Identifier*/
126105197Ssam	u_int8_t alg_enc;		/* Cipher Algorithm Identifier */
127105197Ssam	u_int8_t alg_comp;		/* Compression Algorithm Identifier */
128105197Ssam	u_int32_t spi;			/* SPI Value, network byte order */
129105197Ssam	u_int32_t flags;		/* holder for SADB_KEY_FLAGS */
130105197Ssam
131157123Sgnn	struct seckey *key_auth;	/* Key for Authentication */
132157123Sgnn	struct seckey *key_enc;	        /* Key for Encryption */
133105197Ssam	caddr_t iv;			/* Initilization Vector */
134105197Ssam	u_int ivlen;			/* length of IV */
135105197Ssam	void *sched;			/* intermediate encryption key */
136105197Ssam	size_t schedlen;
137105197Ssam
138105197Ssam	struct secreplay *replay;	/* replay prevention */
139120585Ssam	time_t created;			/* for lifetime */
140105197Ssam
141157123Sgnn	struct seclifetime *lft_c;	/* CURRENT lifetime, it's constant. */
142157123Sgnn	struct seclifetime *lft_h;	/* HARD lifetime */
143157123Sgnn	struct seclifetime *lft_s;	/* SOFT lifetime */
144105197Ssam
145105197Ssam	u_int32_t seq;			/* sequence number */
146105197Ssam	pid_t pid;			/* message's pid */
147105197Ssam
148105197Ssam	struct secashead *sah;		/* back pointer to the secashead */
149105197Ssam
150105197Ssam	/*
151105197Ssam	 * NB: Fields with a tdb_ prefix are part of the "glue" used
152105197Ssam	 *     to interface to the OpenBSD crypto support.  This was done
153105197Ssam	 *     to distinguish this code from the mainline KAME code.
154105197Ssam	 */
155105197Ssam	struct xformsw *tdb_xform;	/* transform */
156105197Ssam	struct enc_xform *tdb_encalgxform;	/* encoding algorithm */
157105197Ssam	struct auth_hash *tdb_authalgxform;	/* authentication algorithm */
158105197Ssam	struct comp_algo *tdb_compalgxform;	/* compression algorithm */
159105197Ssam	u_int64_t tdb_cryptoid;		/* crypto session id */
160194062Svanhu
161194062Svanhu	/*
162194062Svanhu	 * NAT-Traversal.
163194062Svanhu	 */
164194062Svanhu	u_int16_t natt_type;		/* IKE/ESP-marker in output. */
165194062Svanhu	u_int16_t natt_esp_frag_len;	/* MTU for payload fragmentation. */
166105197Ssam};
167105197Ssam
168120585Ssam#define	SECASVAR_LOCK_INIT(_sav) \
169120585Ssam	mtx_init(&(_sav)->lock, "ipsec association", NULL, MTX_DEF)
170120585Ssam#define	SECASVAR_LOCK(_sav)		mtx_lock(&(_sav)->lock)
171120585Ssam#define	SECASVAR_UNLOCK(_sav)		mtx_unlock(&(_sav)->lock)
172120585Ssam#define	SECASVAR_LOCK_DESTROY(_sav)	mtx_destroy(&(_sav)->lock)
173120585Ssam#define	SECASVAR_LOCK_ASSERT(_sav)	mtx_assert(&(_sav)->lock, MA_OWNED)
174120585Ssam
175105197Ssam/* replay prevention */
176105197Ssamstruct secreplay {
177105197Ssam	u_int32_t count;
178105197Ssam	u_int wsize;		/* window size, i.g. 4 bytes */
179105197Ssam	u_int32_t seq;		/* used by sender */
180105197Ssam	u_int32_t lastseq;	/* used by receiver */
181105197Ssam	caddr_t bitmap;		/* used by receiver */
182105197Ssam	int overflow;		/* overflow flag */
183105197Ssam};
184105197Ssam
185105197Ssam/* socket table due to send PF_KEY messages. */
186105197Ssamstruct secreg {
187105197Ssam	LIST_ENTRY(secreg) chain;
188105197Ssam
189105197Ssam	struct socket *so;
190105197Ssam};
191105197Ssam
192105197Ssam/* acquiring list table. */
193105197Ssamstruct secacq {
194105197Ssam	LIST_ENTRY(secacq) chain;
195105197Ssam
196105197Ssam	struct secasindex saidx;
197105197Ssam
198105197Ssam	u_int32_t seq;		/* sequence number */
199120585Ssam	time_t created;		/* for lifetime */
200105197Ssam	int count;		/* for lifetime */
201105197Ssam};
202105197Ssam
203105197Ssam/* Sensitivity Level Specification */
204105197Ssam/* nothing */
205105197Ssam
206105197Ssam#define SADB_KILL_INTERVAL	600	/* six seconds */
207105197Ssam
208105197Ssam/* secpolicy */
209105197Ssamextern struct secpolicy *keydb_newsecpolicy __P((void));
210105197Ssamextern void keydb_delsecpolicy __P((struct secpolicy *));
211105197Ssam/* secashead */
212105197Ssamextern struct secashead *keydb_newsecashead __P((void));
213105197Ssamextern void keydb_delsecashead __P((struct secashead *));
214105197Ssam/* secasvar */
215105197Ssamextern struct secasvar *keydb_newsecasvar __P((void));
216105197Ssamextern void keydb_refsecasvar __P((struct secasvar *));
217105197Ssamextern void keydb_freesecasvar __P((struct secasvar *));
218105197Ssam/* secreplay */
219105197Ssamextern struct secreplay *keydb_newsecreplay __P((size_t));
220105197Ssamextern void keydb_delsecreplay __P((struct secreplay *));
221105197Ssam/* secreg */
222105197Ssamextern struct secreg *keydb_newsecreg __P((void));
223105197Ssamextern void keydb_delsecreg __P((struct secreg *));
224105197Ssam
225105197Ssam#endif /* _KERNEL */
226105197Ssam
227105197Ssam#endif /* _NETIPSEC_KEYDB_H_ */
228