xref: /freebsd/sys/netipsec/key.c (revision c5fda9bac0325eb8c5b447717862d279006f318f)
1 /*	$FreeBSD$	*/
2 /*	$KAME: key.c,v 1.191 2001/06/27 10:46:49 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 /*
36  * This code is referd to RFC 2367
37  */
38 
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipsec.h"
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/fnv_hash.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/mbuf.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/malloc.h>
54 #include <sys/rmlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/errno.h>
59 #include <sys/proc.h>
60 #include <sys/queue.h>
61 #include <sys/refcount.h>
62 #include <sys/syslog.h>
63 
64 #include <vm/uma.h>
65 
66 #include <net/if.h>
67 #include <net/if_var.h>
68 #include <net/vnet.h>
69 #include <net/raw_cb.h>
70 
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_var.h>
75 #include <netinet/udp.h>
76 
77 #ifdef INET6
78 #include <netinet/ip6.h>
79 #include <netinet6/in6_var.h>
80 #include <netinet6/ip6_var.h>
81 #endif /* INET6 */
82 
83 #include <net/pfkeyv2.h>
84 #include <netipsec/keydb.h>
85 #include <netipsec/key.h>
86 #include <netipsec/keysock.h>
87 #include <netipsec/key_debug.h>
88 
89 #include <netipsec/ipsec.h>
90 #ifdef INET6
91 #include <netipsec/ipsec6.h>
92 #endif
93 
94 #include <netipsec/xform.h>
95 #include <machine/in_cksum.h>
96 #include <machine/stdarg.h>
97 
98 /* randomness */
99 #include <sys/random.h>
100 
101 #define FULLMASK	0xff
102 #define	_BITS(bytes)	((bytes) << 3)
103 
104 /*
105  * Note on SA reference counting:
106  * - SAs that are not in DEAD state will have (total external reference + 1)
107  *   following value in reference count field.  they cannot be freed and are
108  *   referenced from SA header.
109  * - SAs that are in DEAD state will have (total external reference)
110  *   in reference count field.  they are ready to be freed.  reference from
111  *   SA header will be removed in key_delsav(), when the reference count
112  *   field hits 0 (= no external reference other than from SA header.
113  */
114 
115 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
116 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
117 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
118 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff;	/* XXX */
119 static VNET_DEFINE(u_int32_t, policy_id) = 0;
120 /*interval to initialize randseed,1(m)*/
121 static VNET_DEFINE(u_int, key_int_random) = 60;
122 /* interval to expire acquiring, 30(s)*/
123 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
124 /* counter for blocking SADB_ACQUIRE.*/
125 static VNET_DEFINE(int, key_blockacq_count) = 10;
126 /* lifetime for blocking SADB_ACQUIRE.*/
127 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
128 /* preferred old sa rather than new sa.*/
129 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
130 #define	V_key_spi_trycnt	VNET(key_spi_trycnt)
131 #define	V_key_spi_minval	VNET(key_spi_minval)
132 #define	V_key_spi_maxval	VNET(key_spi_maxval)
133 #define	V_policy_id		VNET(policy_id)
134 #define	V_key_int_random	VNET(key_int_random)
135 #define	V_key_larval_lifetime	VNET(key_larval_lifetime)
136 #define	V_key_blockacq_count	VNET(key_blockacq_count)
137 #define	V_key_blockacq_lifetime	VNET(key_blockacq_lifetime)
138 #define	V_key_preferred_oldsa	VNET(key_preferred_oldsa)
139 
140 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
141 #define	V_acq_seq		VNET(acq_seq)
142 
143 static VNET_DEFINE(uint32_t, sp_genid) = 0;
144 #define	V_sp_genid		VNET(sp_genid)
145 
146 /* SPD */
147 TAILQ_HEAD(secpolicy_queue, secpolicy);
148 LIST_HEAD(secpolicy_list, secpolicy);
149 static VNET_DEFINE(struct secpolicy_queue, sptree[IPSEC_DIR_MAX]);
150 static VNET_DEFINE(struct secpolicy_queue, sptree_ifnet[IPSEC_DIR_MAX]);
151 static struct rmlock sptree_lock;
152 #define	V_sptree		VNET(sptree)
153 #define	V_sptree_ifnet		VNET(sptree_ifnet)
154 #define	SPTREE_LOCK_INIT()      rm_init(&sptree_lock, "sptree")
155 #define	SPTREE_LOCK_DESTROY()   rm_destroy(&sptree_lock)
156 #define	SPTREE_RLOCK_TRACKER    struct rm_priotracker sptree_tracker
157 #define	SPTREE_RLOCK()          rm_rlock(&sptree_lock, &sptree_tracker)
158 #define	SPTREE_RUNLOCK()        rm_runlock(&sptree_lock, &sptree_tracker)
159 #define	SPTREE_RLOCK_ASSERT()   rm_assert(&sptree_lock, RA_RLOCKED)
160 #define	SPTREE_WLOCK()          rm_wlock(&sptree_lock)
161 #define	SPTREE_WUNLOCK()        rm_wunlock(&sptree_lock)
162 #define	SPTREE_WLOCK_ASSERT()   rm_assert(&sptree_lock, RA_WLOCKED)
163 #define	SPTREE_UNLOCK_ASSERT()  rm_assert(&sptree_lock, RA_UNLOCKED)
164 
165 /* Hash table for lookup SP using unique id */
166 static VNET_DEFINE(struct secpolicy_list *, sphashtbl);
167 static VNET_DEFINE(u_long, sphash_mask);
168 #define	V_sphashtbl		VNET(sphashtbl)
169 #define	V_sphash_mask		VNET(sphash_mask)
170 
171 #define	SPHASH_NHASH_LOG2	7
172 #define	SPHASH_NHASH		(1 << SPHASH_NHASH_LOG2)
173 #define	SPHASH_HASHVAL(id)	(key_u32hash(id) & V_sphash_mask)
174 #define	SPHASH_HASH(id)		&V_sphashtbl[SPHASH_HASHVAL(id)]
175 
176 /* SPD cache */
177 struct spdcache_entry {
178    struct secpolicyindex spidx;	/* secpolicyindex */
179    struct secpolicy *sp;	/* cached policy to be used */
180 
181    LIST_ENTRY(spdcache_entry) chain;
182 };
183 LIST_HEAD(spdcache_entry_list, spdcache_entry);
184 
185 #define	SPDCACHE_MAX_ENTRIES_PER_HASH	8
186 
187 static VNET_DEFINE(u_int, key_spdcache_maxentries) = 0;
188 #define	V_key_spdcache_maxentries	VNET(key_spdcache_maxentries)
189 static VNET_DEFINE(u_int, key_spdcache_threshold) = 32;
190 #define	V_key_spdcache_threshold	VNET(key_spdcache_threshold)
191 static VNET_DEFINE(unsigned long, spd_size) = 0;
192 #define	V_spd_size		VNET(spd_size)
193 
194 #define SPDCACHE_ENABLED()	(V_key_spdcache_maxentries != 0)
195 #define SPDCACHE_ACTIVE() \
196 	(SPDCACHE_ENABLED() && V_spd_size >= V_key_spdcache_threshold)
197 
198 static VNET_DEFINE(struct spdcache_entry_list *, spdcachehashtbl);
199 static VNET_DEFINE(u_long, spdcachehash_mask);
200 #define	V_spdcachehashtbl	VNET(spdcachehashtbl)
201 #define	V_spdcachehash_mask	VNET(spdcachehash_mask)
202 
203 #define	SPDCACHE_HASHVAL(idx) \
204 	(key_addrprotohash(&(idx)->src, &(idx)->dst, &(idx)->ul_proto) &  \
205 	    V_spdcachehash_mask)
206 
207 /* Each cache line is protected by a mutex */
208 static VNET_DEFINE(struct mtx *, spdcache_lock);
209 #define	V_spdcache_lock		VNET(spdcache_lock)
210 
211 #define	SPDCACHE_LOCK_INIT(a) \
212 	mtx_init(&V_spdcache_lock[a], "spdcache", \
213 	    "fast ipsec SPD cache", MTX_DEF|MTX_DUPOK)
214 #define	SPDCACHE_LOCK_DESTROY(a)	mtx_destroy(&V_spdcache_lock[a])
215 #define	SPDCACHE_LOCK(a)		mtx_lock(&V_spdcache_lock[a]);
216 #define	SPDCACHE_UNLOCK(a)		mtx_unlock(&V_spdcache_lock[a]);
217 
218 /* SAD */
219 TAILQ_HEAD(secashead_queue, secashead);
220 LIST_HEAD(secashead_list, secashead);
221 static VNET_DEFINE(struct secashead_queue, sahtree);
222 static struct rmlock sahtree_lock;
223 #define	V_sahtree		VNET(sahtree)
224 #define	SAHTREE_LOCK_INIT()	rm_init(&sahtree_lock, "sahtree")
225 #define	SAHTREE_LOCK_DESTROY()	rm_destroy(&sahtree_lock)
226 #define	SAHTREE_RLOCK_TRACKER	struct rm_priotracker sahtree_tracker
227 #define	SAHTREE_RLOCK()		rm_rlock(&sahtree_lock, &sahtree_tracker)
228 #define	SAHTREE_RUNLOCK()	rm_runlock(&sahtree_lock, &sahtree_tracker)
229 #define	SAHTREE_RLOCK_ASSERT()	rm_assert(&sahtree_lock, RA_RLOCKED)
230 #define	SAHTREE_WLOCK()		rm_wlock(&sahtree_lock)
231 #define	SAHTREE_WUNLOCK()	rm_wunlock(&sahtree_lock)
232 #define	SAHTREE_WLOCK_ASSERT()	rm_assert(&sahtree_lock, RA_WLOCKED)
233 #define	SAHTREE_UNLOCK_ASSERT()	rm_assert(&sahtree_lock, RA_UNLOCKED)
234 
235 /* Hash table for lookup in SAD using SA addresses */
236 static VNET_DEFINE(struct secashead_list *, sahaddrhashtbl);
237 static VNET_DEFINE(u_long, sahaddrhash_mask);
238 #define	V_sahaddrhashtbl	VNET(sahaddrhashtbl)
239 #define	V_sahaddrhash_mask	VNET(sahaddrhash_mask)
240 
241 #define	SAHHASH_NHASH_LOG2	7
242 #define	SAHHASH_NHASH		(1 << SAHHASH_NHASH_LOG2)
243 #define	SAHADDRHASH_HASHVAL(idx)	\
244 	(key_addrprotohash(&(idx)->src, &(idx)->dst, &(idx)->proto) & \
245 	    V_sahaddrhash_mask)
246 #define	SAHADDRHASH_HASH(saidx)		\
247     &V_sahaddrhashtbl[SAHADDRHASH_HASHVAL(saidx)]
248 
249 /* Hash table for lookup in SAD using SPI */
250 LIST_HEAD(secasvar_list, secasvar);
251 static VNET_DEFINE(struct secasvar_list *, savhashtbl);
252 static VNET_DEFINE(u_long, savhash_mask);
253 #define	V_savhashtbl		VNET(savhashtbl)
254 #define	V_savhash_mask		VNET(savhash_mask)
255 #define	SAVHASH_NHASH_LOG2	7
256 #define	SAVHASH_NHASH		(1 << SAVHASH_NHASH_LOG2)
257 #define	SAVHASH_HASHVAL(spi)	(key_u32hash(spi) & V_savhash_mask)
258 #define	SAVHASH_HASH(spi)	&V_savhashtbl[SAVHASH_HASHVAL(spi)]
259 
260 static uint32_t
261 key_addrprotohash(const union sockaddr_union *src,
262     const union sockaddr_union *dst, const uint8_t *proto)
263 {
264 	uint32_t hval;
265 
266 	hval = fnv_32_buf(proto, sizeof(*proto),
267 	    FNV1_32_INIT);
268 	switch (dst->sa.sa_family) {
269 #ifdef INET
270 	case AF_INET:
271 		hval = fnv_32_buf(&src->sin.sin_addr,
272 		    sizeof(in_addr_t), hval);
273 		hval = fnv_32_buf(&dst->sin.sin_addr,
274 		    sizeof(in_addr_t), hval);
275 		break;
276 #endif
277 #ifdef INET6
278 	case AF_INET6:
279 		hval = fnv_32_buf(&src->sin6.sin6_addr,
280 		    sizeof(struct in6_addr), hval);
281 		hval = fnv_32_buf(&dst->sin6.sin6_addr,
282 		    sizeof(struct in6_addr), hval);
283 		break;
284 #endif
285 	default:
286 		hval = 0;
287 		ipseclog((LOG_DEBUG, "%s: unknown address family %d",
288 		    __func__, dst->sa.sa_family));
289 	}
290 	return (hval);
291 }
292 
293 static uint32_t
294 key_u32hash(uint32_t val)
295 {
296 
297 	return (fnv_32_buf(&val, sizeof(val), FNV1_32_INIT));
298 }
299 
300 							/* registed list */
301 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
302 #define	V_regtree		VNET(regtree)
303 static struct mtx regtree_lock;
304 #define	REGTREE_LOCK_INIT() \
305 	mtx_init(&regtree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
306 #define	REGTREE_LOCK_DESTROY()	mtx_destroy(&regtree_lock)
307 #define	REGTREE_LOCK()		mtx_lock(&regtree_lock)
308 #define	REGTREE_UNLOCK()	mtx_unlock(&regtree_lock)
309 #define	REGTREE_LOCK_ASSERT()	mtx_assert(&regtree_lock, MA_OWNED)
310 
311 /* Acquiring list */
312 LIST_HEAD(secacq_list, secacq);
313 static VNET_DEFINE(struct secacq_list, acqtree);
314 #define	V_acqtree		VNET(acqtree)
315 static struct mtx acq_lock;
316 #define	ACQ_LOCK_INIT() \
317     mtx_init(&acq_lock, "acqtree", "ipsec SA acquiring list", MTX_DEF)
318 #define	ACQ_LOCK_DESTROY()	mtx_destroy(&acq_lock)
319 #define	ACQ_LOCK()		mtx_lock(&acq_lock)
320 #define	ACQ_UNLOCK()		mtx_unlock(&acq_lock)
321 #define	ACQ_LOCK_ASSERT()	mtx_assert(&acq_lock, MA_OWNED)
322 
323 /* Hash table for lookup in ACQ list using SA addresses */
324 static VNET_DEFINE(struct secacq_list *, acqaddrhashtbl);
325 static VNET_DEFINE(u_long, acqaddrhash_mask);
326 #define	V_acqaddrhashtbl	VNET(acqaddrhashtbl)
327 #define	V_acqaddrhash_mask	VNET(acqaddrhash_mask)
328 
329 /* Hash table for lookup in ACQ list using SEQ number */
330 static VNET_DEFINE(struct secacq_list *, acqseqhashtbl);
331 static VNET_DEFINE(u_long, acqseqhash_mask);
332 #define	V_acqseqhashtbl		VNET(acqseqhashtbl)
333 #define	V_acqseqhash_mask	VNET(acqseqhash_mask)
334 
335 #define	ACQHASH_NHASH_LOG2	7
336 #define	ACQHASH_NHASH		(1 << ACQHASH_NHASH_LOG2)
337 #define	ACQADDRHASH_HASHVAL(idx)	\
338 	(key_addrprotohash(&(idx)->src, &(idx)->dst, &(idx)->proto) & \
339 	    V_acqaddrhash_mask)
340 #define	ACQSEQHASH_HASHVAL(seq)		\
341     (key_u32hash(seq) & V_acqseqhash_mask)
342 #define	ACQADDRHASH_HASH(saidx)	\
343     &V_acqaddrhashtbl[ACQADDRHASH_HASHVAL(saidx)]
344 #define	ACQSEQHASH_HASH(seq)	\
345     &V_acqseqhashtbl[ACQSEQHASH_HASHVAL(seq)]
346 							/* SP acquiring list */
347 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
348 #define	V_spacqtree		VNET(spacqtree)
349 static struct mtx spacq_lock;
350 #define	SPACQ_LOCK_INIT() \
351 	mtx_init(&spacq_lock, "spacqtree", \
352 		"fast ipsec security policy acquire list", MTX_DEF)
353 #define	SPACQ_LOCK_DESTROY()	mtx_destroy(&spacq_lock)
354 #define	SPACQ_LOCK()		mtx_lock(&spacq_lock)
355 #define	SPACQ_UNLOCK()		mtx_unlock(&spacq_lock)
356 #define	SPACQ_LOCK_ASSERT()	mtx_assert(&spacq_lock, MA_OWNED)
357 
358 static const int minsize[] = {
359 	sizeof(struct sadb_msg),	/* SADB_EXT_RESERVED */
360 	sizeof(struct sadb_sa),		/* SADB_EXT_SA */
361 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_CURRENT */
362 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_HARD */
363 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_SOFT */
364 	sizeof(struct sadb_address),	/* SADB_EXT_ADDRESS_SRC */
365 	sizeof(struct sadb_address),	/* SADB_EXT_ADDRESS_DST */
366 	sizeof(struct sadb_address),	/* SADB_EXT_ADDRESS_PROXY */
367 	sizeof(struct sadb_key),	/* SADB_EXT_KEY_AUTH */
368 	sizeof(struct sadb_key),	/* SADB_EXT_KEY_ENCRYPT */
369 	sizeof(struct sadb_ident),	/* SADB_EXT_IDENTITY_SRC */
370 	sizeof(struct sadb_ident),	/* SADB_EXT_IDENTITY_DST */
371 	sizeof(struct sadb_sens),	/* SADB_EXT_SENSITIVITY */
372 	sizeof(struct sadb_prop),	/* SADB_EXT_PROPOSAL */
373 	sizeof(struct sadb_supported),	/* SADB_EXT_SUPPORTED_AUTH */
374 	sizeof(struct sadb_supported),	/* SADB_EXT_SUPPORTED_ENCRYPT */
375 	sizeof(struct sadb_spirange),	/* SADB_EXT_SPIRANGE */
376 	0,				/* SADB_X_EXT_KMPRIVATE */
377 	sizeof(struct sadb_x_policy),	/* SADB_X_EXT_POLICY */
378 	sizeof(struct sadb_x_sa2),	/* SADB_X_SA2 */
379 	sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
380 	sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
381 	sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
382 	sizeof(struct sadb_address),	/* SADB_X_EXT_NAT_T_OAI */
383 	sizeof(struct sadb_address),	/* SADB_X_EXT_NAT_T_OAR */
384 	sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
385 	sizeof(struct sadb_x_sa_replay), /* SADB_X_EXT_SA_REPLAY */
386 	sizeof(struct sadb_address),	/* SADB_X_EXT_NEW_ADDRESS_SRC */
387 	sizeof(struct sadb_address),	/* SADB_X_EXT_NEW_ADDRESS_DST */
388 };
389 _Static_assert(sizeof(minsize)/sizeof(int) == SADB_EXT_MAX + 1, "minsize size mismatch");
390 
391 static const int maxsize[] = {
392 	sizeof(struct sadb_msg),	/* SADB_EXT_RESERVED */
393 	sizeof(struct sadb_sa),		/* SADB_EXT_SA */
394 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_CURRENT */
395 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_HARD */
396 	sizeof(struct sadb_lifetime),	/* SADB_EXT_LIFETIME_SOFT */
397 	0,				/* SADB_EXT_ADDRESS_SRC */
398 	0,				/* SADB_EXT_ADDRESS_DST */
399 	0,				/* SADB_EXT_ADDRESS_PROXY */
400 	0,				/* SADB_EXT_KEY_AUTH */
401 	0,				/* SADB_EXT_KEY_ENCRYPT */
402 	0,				/* SADB_EXT_IDENTITY_SRC */
403 	0,				/* SADB_EXT_IDENTITY_DST */
404 	0,				/* SADB_EXT_SENSITIVITY */
405 	0,				/* SADB_EXT_PROPOSAL */
406 	0,				/* SADB_EXT_SUPPORTED_AUTH */
407 	0,				/* SADB_EXT_SUPPORTED_ENCRYPT */
408 	sizeof(struct sadb_spirange),	/* SADB_EXT_SPIRANGE */
409 	0,				/* SADB_X_EXT_KMPRIVATE */
410 	0,				/* SADB_X_EXT_POLICY */
411 	sizeof(struct sadb_x_sa2),	/* SADB_X_SA2 */
412 	sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
413 	sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
414 	sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
415 	0,				/* SADB_X_EXT_NAT_T_OAI */
416 	0,				/* SADB_X_EXT_NAT_T_OAR */
417 	sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
418 	sizeof(struct sadb_x_sa_replay), /* SADB_X_EXT_SA_REPLAY */
419 	0,				/* SADB_X_EXT_NEW_ADDRESS_SRC */
420 	0,				/* SADB_X_EXT_NEW_ADDRESS_DST */
421 };
422 _Static_assert(sizeof(maxsize)/sizeof(int) == SADB_EXT_MAX + 1, "minsize size mismatch");
423 
424 /*
425  * Internal values for SA flags:
426  * SADB_X_EXT_F_CLONED means that SA was cloned by key_updateaddresses,
427  *	thus we will not free the most of SA content in key_delsav().
428  */
429 #define	SADB_X_EXT_F_CLONED	0x80000000
430 
431 #define	SADB_CHECKLEN(_mhp, _ext)			\
432     ((_mhp)->extlen[(_ext)] < minsize[(_ext)] || (maxsize[(_ext)] != 0 && \
433 	((_mhp)->extlen[(_ext)] > maxsize[(_ext)])))
434 #define	SADB_CHECKHDR(_mhp, _ext)	((_mhp)->ext[(_ext)] == NULL)
435 
436 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
437 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
438 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
439 
440 #define	V_ipsec_esp_keymin	VNET(ipsec_esp_keymin)
441 #define	V_ipsec_esp_auth	VNET(ipsec_esp_auth)
442 #define	V_ipsec_ah_keymin	VNET(ipsec_ah_keymin)
443 
444 #ifdef IPSEC_DEBUG
445 VNET_DEFINE(int, ipsec_debug) = 1;
446 #else
447 VNET_DEFINE(int, ipsec_debug) = 0;
448 #endif
449 
450 #ifdef INET
451 SYSCTL_DECL(_net_inet_ipsec);
452 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
453     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
454     "Enable IPsec debugging output when set.");
455 #endif
456 #ifdef INET6
457 SYSCTL_DECL(_net_inet6_ipsec6);
458 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug,
459     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
460     "Enable IPsec debugging output when set.");
461 #endif
462 
463 SYSCTL_DECL(_net_key);
464 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL,	debug,
465 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
466 
467 /* max count of trial for the decision of spi value */
468 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
469 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
470 
471 /* minimum spi value to allocate automatically. */
472 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval,
473 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
474 
475 /* maximun spi value to allocate automatically. */
476 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval,
477 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
478 
479 /* interval to initialize randseed */
480 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random,
481 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
482 
483 /* lifetime for larval SA */
484 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime,
485 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
486 
487 /* counter for blocking to send SADB_ACQUIRE to IKEd */
488 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count,
489 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
490 
491 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
492 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime,
493 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
494 
495 /* ESP auth */
496 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
497 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
498 
499 /* minimum ESP key length */
500 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin,
501 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
502 
503 /* minimum AH key length */
504 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
505 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
506 
507 /* perfered old SA rather than new SA */
508 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa,
509 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
510 
511 static SYSCTL_NODE(_net_key, OID_AUTO, spdcache, CTLFLAG_RW, 0, "SPD cache");
512 
513 SYSCTL_UINT(_net_key_spdcache, OID_AUTO, maxentries,
514 	CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(key_spdcache_maxentries), 0,
515 	"Maximum number of entries in the SPD cache"
516 	" (power of 2, 0 to disable)");
517 
518 SYSCTL_UINT(_net_key_spdcache, OID_AUTO, threshold,
519 	CTLFLAG_VNET | CTLFLAG_RDTUN, &VNET_NAME(key_spdcache_threshold), 0,
520 	"Number of SPs that make the SPD cache active");
521 
522 #define __LIST_CHAINED(elm) \
523 	(!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
524 
525 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
526 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
527 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
528 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
529 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
530 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
531 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
532 MALLOC_DEFINE(M_IPSEC_SPDCACHE, "ipsec-spdcache", "ipsec SPD cache");
533 
534 static VNET_DEFINE(uma_zone_t, key_lft_zone);
535 #define	V_key_lft_zone		VNET(key_lft_zone)
536 
537 static LIST_HEAD(xforms_list, xformsw) xforms = LIST_HEAD_INITIALIZER();
538 static struct mtx xforms_lock;
539 #define	XFORMS_LOCK_INIT()	\
540     mtx_init(&xforms_lock, "xforms_list", "IPsec transforms list", MTX_DEF)
541 #define	XFORMS_LOCK_DESTROY()	mtx_destroy(&xforms_lock)
542 #define	XFORMS_LOCK()		mtx_lock(&xforms_lock)
543 #define	XFORMS_UNLOCK()		mtx_unlock(&xforms_lock)
544 
545 /*
546  * set parameters into secpolicyindex buffer.
547  * Must allocate secpolicyindex buffer passed to this function.
548  */
549 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
550 do { \
551 	bzero((idx), sizeof(struct secpolicyindex));                         \
552 	(idx)->dir = (_dir);                                                 \
553 	(idx)->prefs = (ps);                                                 \
554 	(idx)->prefd = (pd);                                                 \
555 	(idx)->ul_proto = (ulp);                                             \
556 	bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len);     \
557 	bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len);     \
558 } while (0)
559 
560 /*
561  * set parameters into secasindex buffer.
562  * Must allocate secasindex buffer before calling this function.
563  */
564 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
565 do { \
566 	bzero((idx), sizeof(struct secasindex));                             \
567 	(idx)->proto = (p);                                                  \
568 	(idx)->mode = (m);                                                   \
569 	(idx)->reqid = (r);                                                  \
570 	bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len);     \
571 	bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len);     \
572 	key_porttosaddr(&(idx)->src.sa, 0);				     \
573 	key_porttosaddr(&(idx)->dst.sa, 0);				     \
574 } while (0)
575 
576 /* key statistics */
577 struct _keystat {
578 	u_long getspi_count; /* the avarage of count to try to get new SPI */
579 } keystat;
580 
581 struct sadb_msghdr {
582 	struct sadb_msg *msg;
583 	struct sadb_ext *ext[SADB_EXT_MAX + 1];
584 	int extoff[SADB_EXT_MAX + 1];
585 	int extlen[SADB_EXT_MAX + 1];
586 };
587 
588 static struct supported_ealgs {
589 	int sadb_alg;
590 	const struct enc_xform *xform;
591 } supported_ealgs[] = {
592 	{ SADB_EALG_DESCBC,		&enc_xform_des },
593 	{ SADB_EALG_3DESCBC,		&enc_xform_3des },
594 	{ SADB_X_EALG_AES,		&enc_xform_rijndael128 },
595 	{ SADB_X_EALG_BLOWFISHCBC,	&enc_xform_blf },
596 	{ SADB_X_EALG_CAST128CBC,	&enc_xform_cast5 },
597 	{ SADB_EALG_NULL,		&enc_xform_null },
598 	{ SADB_X_EALG_CAMELLIACBC,	&enc_xform_camellia },
599 	{ SADB_X_EALG_AESCTR,		&enc_xform_aes_icm },
600 	{ SADB_X_EALG_AESGCM16,		&enc_xform_aes_nist_gcm },
601 	{ SADB_X_EALG_AESGMAC,		&enc_xform_aes_nist_gmac },
602 };
603 
604 static struct supported_aalgs {
605 	int sadb_alg;
606 	const struct auth_hash *xform;
607 } supported_aalgs[] = {
608 	{ SADB_X_AALG_NULL,		&auth_hash_null },
609 	{ SADB_AALG_MD5HMAC,		&auth_hash_hmac_md5 },
610 	{ SADB_AALG_SHA1HMAC,		&auth_hash_hmac_sha1 },
611 	{ SADB_X_AALG_RIPEMD160HMAC,	&auth_hash_hmac_ripemd_160 },
612 	{ SADB_X_AALG_MD5,		&auth_hash_key_md5 },
613 	{ SADB_X_AALG_SHA,		&auth_hash_key_sha1 },
614 	{ SADB_X_AALG_SHA2_256,		&auth_hash_hmac_sha2_256 },
615 	{ SADB_X_AALG_SHA2_384,		&auth_hash_hmac_sha2_384 },
616 	{ SADB_X_AALG_SHA2_512,		&auth_hash_hmac_sha2_512 },
617 	{ SADB_X_AALG_AES128GMAC,	&auth_hash_nist_gmac_aes_128 },
618 	{ SADB_X_AALG_AES192GMAC,	&auth_hash_nist_gmac_aes_192 },
619 	{ SADB_X_AALG_AES256GMAC,	&auth_hash_nist_gmac_aes_256 },
620 };
621 
622 static struct supported_calgs {
623 	int sadb_alg;
624 	const struct comp_algo *xform;
625 } supported_calgs[] = {
626 	{ SADB_X_CALG_DEFLATE,		&comp_algo_deflate },
627 };
628 
629 #ifndef IPSEC_DEBUG2
630 static struct callout key_timer;
631 #endif
632 
633 static void key_unlink(struct secpolicy *);
634 static struct secpolicy *key_do_allocsp(struct secpolicyindex *spidx, u_int dir);
635 static struct secpolicy *key_getsp(struct secpolicyindex *);
636 static struct secpolicy *key_getspbyid(u_int32_t);
637 static struct mbuf *key_gather_mbuf(struct mbuf *,
638 	const struct sadb_msghdr *, int, int, ...);
639 static int key_spdadd(struct socket *, struct mbuf *,
640 	const struct sadb_msghdr *);
641 static uint32_t key_getnewspid(void);
642 static int key_spddelete(struct socket *, struct mbuf *,
643 	const struct sadb_msghdr *);
644 static int key_spddelete2(struct socket *, struct mbuf *,
645 	const struct sadb_msghdr *);
646 static int key_spdget(struct socket *, struct mbuf *,
647 	const struct sadb_msghdr *);
648 static int key_spdflush(struct socket *, struct mbuf *,
649 	const struct sadb_msghdr *);
650 static int key_spddump(struct socket *, struct mbuf *,
651 	const struct sadb_msghdr *);
652 static struct mbuf *key_setdumpsp(struct secpolicy *,
653 	u_int8_t, u_int32_t, u_int32_t);
654 static struct mbuf *key_sp2mbuf(struct secpolicy *);
655 static size_t key_getspreqmsglen(struct secpolicy *);
656 static int key_spdexpire(struct secpolicy *);
657 static struct secashead *key_newsah(struct secasindex *);
658 static void key_freesah(struct secashead **);
659 static void key_delsah(struct secashead *);
660 static struct secasvar *key_newsav(const struct sadb_msghdr *,
661     struct secasindex *, uint32_t, int *);
662 static void key_delsav(struct secasvar *);
663 static void key_unlinksav(struct secasvar *);
664 static struct secashead *key_getsah(struct secasindex *);
665 static int key_checkspidup(uint32_t);
666 static struct secasvar *key_getsavbyspi(uint32_t);
667 static int key_setnatt(struct secasvar *, const struct sadb_msghdr *);
668 static int key_setsaval(struct secasvar *, const struct sadb_msghdr *);
669 static int key_updatelifetimes(struct secasvar *, const struct sadb_msghdr *);
670 static int key_updateaddresses(struct socket *, struct mbuf *,
671     const struct sadb_msghdr *, struct secasvar *, struct secasindex *);
672 
673 static struct mbuf *key_setdumpsa(struct secasvar *, u_int8_t,
674 	u_int8_t, u_int32_t, u_int32_t);
675 static struct mbuf *key_setsadbmsg(u_int8_t, u_int16_t, u_int8_t,
676 	u_int32_t, pid_t, u_int16_t);
677 static struct mbuf *key_setsadbsa(struct secasvar *);
678 static struct mbuf *key_setsadbaddr(u_int16_t,
679 	const struct sockaddr *, u_int8_t, u_int16_t);
680 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
681 static struct mbuf *key_setsadbxtype(u_int16_t);
682 static struct mbuf *key_setsadbxsa2(u_int8_t, u_int32_t, u_int32_t);
683 static struct mbuf *key_setsadbxsareplay(u_int32_t);
684 static struct mbuf *key_setsadbxpolicy(u_int16_t, u_int8_t,
685 	u_int32_t, u_int32_t);
686 static struct seckey *key_dup_keymsg(const struct sadb_key *, size_t,
687     struct malloc_type *);
688 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
689     struct malloc_type *);
690 
691 /* flags for key_cmpsaidx() */
692 #define CMP_HEAD	1	/* protocol, addresses. */
693 #define CMP_MODE_REQID	2	/* additionally HEAD, reqid, mode. */
694 #define CMP_REQID	3	/* additionally HEAD, reaid. */
695 #define CMP_EXACTLY	4	/* all elements. */
696 static int key_cmpsaidx(const struct secasindex *,
697     const struct secasindex *, int);
698 static int key_cmpspidx_exactly(struct secpolicyindex *,
699     struct secpolicyindex *);
700 static int key_cmpspidx_withmask(struct secpolicyindex *,
701     struct secpolicyindex *);
702 static int key_bbcmp(const void *, const void *, u_int);
703 static uint8_t key_satype2proto(uint8_t);
704 static uint8_t key_proto2satype(uint8_t);
705 
706 static int key_getspi(struct socket *, struct mbuf *,
707 	const struct sadb_msghdr *);
708 static uint32_t key_do_getnewspi(struct sadb_spirange *, struct secasindex *);
709 static int key_update(struct socket *, struct mbuf *,
710 	const struct sadb_msghdr *);
711 static int key_add(struct socket *, struct mbuf *,
712 	const struct sadb_msghdr *);
713 static int key_setident(struct secashead *, const struct sadb_msghdr *);
714 static struct mbuf *key_getmsgbuf_x1(struct mbuf *,
715 	const struct sadb_msghdr *);
716 static int key_delete(struct socket *, struct mbuf *,
717 	const struct sadb_msghdr *);
718 static int key_delete_all(struct socket *, struct mbuf *,
719 	const struct sadb_msghdr *, struct secasindex *);
720 static void key_delete_xform(const struct xformsw *);
721 static int key_get(struct socket *, struct mbuf *,
722 	const struct sadb_msghdr *);
723 
724 static void key_getcomb_setlifetime(struct sadb_comb *);
725 static struct mbuf *key_getcomb_ealg(void);
726 static struct mbuf *key_getcomb_ah(void);
727 static struct mbuf *key_getcomb_ipcomp(void);
728 static struct mbuf *key_getprop(const struct secasindex *);
729 
730 static int key_acquire(const struct secasindex *, struct secpolicy *);
731 static uint32_t key_newacq(const struct secasindex *, int *);
732 static uint32_t key_getacq(const struct secasindex *, int *);
733 static int key_acqdone(const struct secasindex *, uint32_t);
734 static int key_acqreset(uint32_t);
735 static struct secspacq *key_newspacq(struct secpolicyindex *);
736 static struct secspacq *key_getspacq(struct secpolicyindex *);
737 static int key_acquire2(struct socket *, struct mbuf *,
738 	const struct sadb_msghdr *);
739 static int key_register(struct socket *, struct mbuf *,
740 	const struct sadb_msghdr *);
741 static int key_expire(struct secasvar *, int);
742 static int key_flush(struct socket *, struct mbuf *,
743 	const struct sadb_msghdr *);
744 static int key_dump(struct socket *, struct mbuf *,
745 	const struct sadb_msghdr *);
746 static int key_promisc(struct socket *, struct mbuf *,
747 	const struct sadb_msghdr *);
748 static int key_senderror(struct socket *, struct mbuf *, int);
749 static int key_validate_ext(const struct sadb_ext *, int);
750 static int key_align(struct mbuf *, struct sadb_msghdr *);
751 static struct mbuf *key_setlifetime(struct seclifetime *, uint16_t);
752 static struct mbuf *key_setkey(struct seckey *, uint16_t);
753 static int xform_init(struct secasvar *, u_short);
754 
755 static void spdcache_init(void);
756 static void spdcache_clear(void);
757 static struct spdcache_entry *spdcache_entry_alloc(
758 	const struct secpolicyindex *spidx,
759 	struct secpolicy *policy);
760 static void spdcache_entry_free(struct spdcache_entry *entry);
761 #ifdef VIMAGE
762 static void spdcache_destroy(void);
763 #endif
764 
765 #define	DBG_IPSEC_INITREF(t, p)	do {				\
766 	refcount_init(&(p)->refcnt, 1);				\
767 	KEYDBG(KEY_STAMP,					\
768 	    printf("%s: Initialize refcnt %s(%p) = %u\n",	\
769 	    __func__, #t, (p), (p)->refcnt));			\
770 } while (0)
771 #define	DBG_IPSEC_ADDREF(t, p)	do {				\
772 	refcount_acquire(&(p)->refcnt);				\
773 	KEYDBG(KEY_STAMP,					\
774 	    printf("%s: Acquire refcnt %s(%p) -> %u\n",		\
775 	    __func__, #t, (p), (p)->refcnt));			\
776 } while (0)
777 #define	DBG_IPSEC_DELREF(t, p)	do {				\
778 	KEYDBG(KEY_STAMP,					\
779 	    printf("%s: Release refcnt %s(%p) -> %u\n",		\
780 	    __func__, #t, (p), (p)->refcnt - 1));		\
781 	refcount_release(&(p)->refcnt);				\
782 } while (0)
783 
784 #define	IPSEC_INITREF(t, p)	refcount_init(&(p)->refcnt, 1)
785 #define	IPSEC_ADDREF(t, p)	refcount_acquire(&(p)->refcnt)
786 #define	IPSEC_DELREF(t, p)	refcount_release(&(p)->refcnt)
787 
788 #define	SP_INITREF(p)	IPSEC_INITREF(SP, p)
789 #define	SP_ADDREF(p)	IPSEC_ADDREF(SP, p)
790 #define	SP_DELREF(p)	IPSEC_DELREF(SP, p)
791 
792 #define	SAH_INITREF(p)	IPSEC_INITREF(SAH, p)
793 #define	SAH_ADDREF(p)	IPSEC_ADDREF(SAH, p)
794 #define	SAH_DELREF(p)	IPSEC_DELREF(SAH, p)
795 
796 #define	SAV_INITREF(p)	IPSEC_INITREF(SAV, p)
797 #define	SAV_ADDREF(p)	IPSEC_ADDREF(SAV, p)
798 #define	SAV_DELREF(p)	IPSEC_DELREF(SAV, p)
799 
800 /*
801  * Update the refcnt while holding the SPTREE lock.
802  */
803 void
804 key_addref(struct secpolicy *sp)
805 {
806 
807 	SP_ADDREF(sp);
808 }
809 
810 /*
811  * Return 0 when there are known to be no SP's for the specified
812  * direction.  Otherwise return 1.  This is used by IPsec code
813  * to optimize performance.
814  */
815 int
816 key_havesp(u_int dir)
817 {
818 
819 	return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
820 		TAILQ_FIRST(&V_sptree[dir]) != NULL : 1);
821 }
822 
823 /* %%% IPsec policy management */
824 /*
825  * Return current SPDB generation.
826  */
827 uint32_t
828 key_getspgen(void)
829 {
830 
831 	return (V_sp_genid);
832 }
833 
834 void
835 key_bumpspgen(void)
836 {
837 
838 	V_sp_genid++;
839 }
840 
841 static int
842 key_checksockaddrs(struct sockaddr *src, struct sockaddr *dst)
843 {
844 
845 	/* family match */
846 	if (src->sa_family != dst->sa_family)
847 		return (EINVAL);
848 	/* sa_len match */
849 	if (src->sa_len != dst->sa_len)
850 		return (EINVAL);
851 	switch (src->sa_family) {
852 #ifdef INET
853 	case AF_INET:
854 		if (src->sa_len != sizeof(struct sockaddr_in))
855 			return (EINVAL);
856 		break;
857 #endif
858 #ifdef INET6
859 	case AF_INET6:
860 		if (src->sa_len != sizeof(struct sockaddr_in6))
861 			return (EINVAL);
862 		break;
863 #endif
864 	default:
865 		return (EAFNOSUPPORT);
866 	}
867 	return (0);
868 }
869 
870 struct secpolicy *
871 key_do_allocsp(struct secpolicyindex *spidx, u_int dir)
872 {
873 	SPTREE_RLOCK_TRACKER;
874 	struct secpolicy *sp;
875 
876 	IPSEC_ASSERT(spidx != NULL, ("null spidx"));
877 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
878 		("invalid direction %u", dir));
879 
880 	SPTREE_RLOCK();
881 	TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
882 		if (key_cmpspidx_withmask(&sp->spidx, spidx)) {
883 			SP_ADDREF(sp);
884 			break;
885 		}
886 	}
887 	SPTREE_RUNLOCK();
888 	return (sp);
889 }
890 
891 
892 /*
893  * allocating a SP for OUTBOUND or INBOUND packet.
894  * Must call key_freesp() later.
895  * OUT:	NULL:	not found
896  *	others:	found and return the pointer.
897  */
898 struct secpolicy *
899 key_allocsp(struct secpolicyindex *spidx, u_int dir)
900 {
901 	struct spdcache_entry *entry, *lastentry, *tmpentry;
902 	struct secpolicy *sp;
903 	uint32_t hashv;
904 	int nb_entries;
905 
906 	if (!SPDCACHE_ACTIVE()) {
907 		sp = key_do_allocsp(spidx, dir);
908 		goto out;
909 	}
910 
911 	hashv = SPDCACHE_HASHVAL(spidx);
912 	SPDCACHE_LOCK(hashv);
913 	nb_entries = 0;
914 	LIST_FOREACH_SAFE(entry, &V_spdcachehashtbl[hashv], chain, tmpentry) {
915 		/* Removed outdated entries */
916 		if (entry->sp != NULL &&
917 		    entry->sp->state == IPSEC_SPSTATE_DEAD) {
918 			LIST_REMOVE(entry, chain);
919 			spdcache_entry_free(entry);
920 			continue;
921 		}
922 
923 		nb_entries++;
924 		if (!key_cmpspidx_exactly(&entry->spidx, spidx)) {
925 			lastentry = entry;
926 			continue;
927 		}
928 
929 		sp = entry->sp;
930 		if (entry->sp != NULL)
931 			SP_ADDREF(sp);
932 
933 		/* IPSECSTAT_INC(ips_spdcache_hits); */
934 
935 		SPDCACHE_UNLOCK(hashv);
936 		goto out;
937 	}
938 
939 	/* IPSECSTAT_INC(ips_spdcache_misses); */
940 
941 	sp = key_do_allocsp(spidx, dir);
942 	entry = spdcache_entry_alloc(spidx, sp);
943 	if (entry != NULL) {
944 		if (nb_entries >= SPDCACHE_MAX_ENTRIES_PER_HASH) {
945 			LIST_REMOVE(lastentry, chain);
946 			spdcache_entry_free(lastentry);
947 		}
948 
949 		LIST_INSERT_HEAD(&V_spdcachehashtbl[hashv], entry, chain);
950 	}
951 
952 	SPDCACHE_UNLOCK(hashv);
953 
954 out:
955 	if (sp != NULL) {	/* found a SPD entry */
956 		sp->lastused = time_second;
957 		KEYDBG(IPSEC_STAMP,
958 		    printf("%s: return SP(%p)\n", __func__, sp));
959 		KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
960 	} else {
961 		KEYDBG(IPSEC_DATA,
962 		    printf("%s: lookup failed for ", __func__);
963 		    kdebug_secpolicyindex(spidx, NULL));
964 	}
965 	return (sp);
966 }
967 
968 /*
969  * Allocating an SA entry for an *INBOUND* or *OUTBOUND* TCP packet, signed
970  * or should be signed by MD5 signature.
971  * We don't use key_allocsa() for such lookups, because we don't know SPI.
972  * Unlike ESP and AH protocols, SPI isn't transmitted in the TCP header with
973  * signed packet. We use SADB only as storage for password.
974  * OUT:	positive:	corresponding SA for given saidx found.
975  *	NULL:		SA not found
976  */
977 struct secasvar *
978 key_allocsa_tcpmd5(struct secasindex *saidx)
979 {
980 	SAHTREE_RLOCK_TRACKER;
981 	struct secashead *sah;
982 	struct secasvar *sav;
983 
984 	IPSEC_ASSERT(saidx->proto == IPPROTO_TCP,
985 	    ("unexpected security protocol %u", saidx->proto));
986 	IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TCPMD5,
987 	    ("unexpected mode %u", saidx->mode));
988 
989 	SAHTREE_RLOCK();
990 	LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
991 		KEYDBG(IPSEC_DUMP,
992 		    printf("%s: checking SAH\n", __func__);
993 		    kdebug_secash(sah, "  "));
994 		if (sah->saidx.proto != IPPROTO_TCP)
995 			continue;
996 		if (!key_sockaddrcmp(&saidx->dst.sa, &sah->saidx.dst.sa, 0) &&
997 		    !key_sockaddrcmp(&saidx->src.sa, &sah->saidx.src.sa, 0))
998 			break;
999 	}
1000 	if (sah != NULL) {
1001 		if (V_key_preferred_oldsa)
1002 			sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
1003 		else
1004 			sav = TAILQ_FIRST(&sah->savtree_alive);
1005 		if (sav != NULL)
1006 			SAV_ADDREF(sav);
1007 	} else
1008 		sav = NULL;
1009 	SAHTREE_RUNLOCK();
1010 
1011 	if (sav != NULL) {
1012 		KEYDBG(IPSEC_STAMP,
1013 		    printf("%s: return SA(%p)\n", __func__, sav));
1014 		KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1015 	} else {
1016 		KEYDBG(IPSEC_STAMP,
1017 		    printf("%s: SA not found\n", __func__));
1018 		KEYDBG(IPSEC_DATA, kdebug_secasindex(saidx, NULL));
1019 	}
1020 	return (sav);
1021 }
1022 
1023 /*
1024  * Allocating an SA entry for an *OUTBOUND* packet.
1025  * OUT:	positive:	corresponding SA for given saidx found.
1026  *	NULL:		SA not found, but will be acquired, check *error
1027  *			for acquiring status.
1028  */
1029 struct secasvar *
1030 key_allocsa_policy(struct secpolicy *sp, const struct secasindex *saidx,
1031     int *error)
1032 {
1033 	SAHTREE_RLOCK_TRACKER;
1034 	struct secashead *sah;
1035 	struct secasvar *sav;
1036 
1037 	IPSEC_ASSERT(saidx != NULL, ("null saidx"));
1038 	IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
1039 		saidx->mode == IPSEC_MODE_TUNNEL,
1040 		("unexpected policy %u", saidx->mode));
1041 
1042 	/*
1043 	 * We check new SA in the IPsec request because a different
1044 	 * SA may be involved each time this request is checked, either
1045 	 * because new SAs are being configured, or this request is
1046 	 * associated with an unconnected datagram socket, or this request
1047 	 * is associated with a system default policy.
1048 	 */
1049 	SAHTREE_RLOCK();
1050 	LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
1051 		KEYDBG(IPSEC_DUMP,
1052 		    printf("%s: checking SAH\n", __func__);
1053 		    kdebug_secash(sah, "  "));
1054 		if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID))
1055 			break;
1056 
1057 	}
1058 	if (sah != NULL) {
1059 		/*
1060 		 * Allocate the oldest SA available according to
1061 		 * draft-jenkins-ipsec-rekeying-03.
1062 		 */
1063 		if (V_key_preferred_oldsa)
1064 			sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
1065 		else
1066 			sav = TAILQ_FIRST(&sah->savtree_alive);
1067 		if (sav != NULL)
1068 			SAV_ADDREF(sav);
1069 	} else
1070 		sav = NULL;
1071 	SAHTREE_RUNLOCK();
1072 
1073 	if (sav != NULL) {
1074 		*error = 0;
1075 		KEYDBG(IPSEC_STAMP,
1076 		    printf("%s: chosen SA(%p) for SP(%p)\n", __func__,
1077 			sav, sp));
1078 		KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1079 		return (sav); /* return referenced SA */
1080 	}
1081 
1082 	/* there is no SA */
1083 	*error = key_acquire(saidx, sp);
1084 	if ((*error) != 0)
1085 		ipseclog((LOG_DEBUG,
1086 		    "%s: error %d returned from key_acquire()\n",
1087 			__func__, *error));
1088 	KEYDBG(IPSEC_STAMP,
1089 	    printf("%s: acquire SA for SP(%p), error %d\n",
1090 		__func__, sp, *error));
1091 	KEYDBG(IPSEC_DATA, kdebug_secasindex(saidx, NULL));
1092 	return (NULL);
1093 }
1094 
1095 /*
1096  * allocating a usable SA entry for a *INBOUND* packet.
1097  * Must call key_freesav() later.
1098  * OUT: positive:	pointer to a usable sav (i.e. MATURE or DYING state).
1099  *	NULL:		not found, or error occurred.
1100  *
1101  * According to RFC 2401 SA is uniquely identified by a triple SPI,
1102  * destination address, and security protocol. But according to RFC 4301,
1103  * SPI by itself suffices to specify an SA.
1104  *
1105  * Note that, however, we do need to keep source address in IPsec SA.
1106  * IKE specification and PF_KEY specification do assume that we
1107  * keep source address in IPsec SA.  We see a tricky situation here.
1108  */
1109 struct secasvar *
1110 key_allocsa(union sockaddr_union *dst, uint8_t proto, uint32_t spi)
1111 {
1112 	SAHTREE_RLOCK_TRACKER;
1113 	struct secasvar *sav;
1114 
1115 	IPSEC_ASSERT(proto == IPPROTO_ESP || proto == IPPROTO_AH ||
1116 	    proto == IPPROTO_IPCOMP, ("unexpected security protocol %u",
1117 	    proto));
1118 
1119 	SAHTREE_RLOCK();
1120 	LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
1121 		if (sav->spi == spi)
1122 			break;
1123 	}
1124 	/*
1125 	 * We use single SPI namespace for all protocols, so it is
1126 	 * impossible to have SPI duplicates in the SAVHASH.
1127 	 */
1128 	if (sav != NULL) {
1129 		if (sav->state != SADB_SASTATE_LARVAL &&
1130 		    sav->sah->saidx.proto == proto &&
1131 		    key_sockaddrcmp(&dst->sa,
1132 			&sav->sah->saidx.dst.sa, 0) == 0)
1133 			SAV_ADDREF(sav);
1134 		else
1135 			sav = NULL;
1136 	}
1137 	SAHTREE_RUNLOCK();
1138 
1139 	if (sav == NULL) {
1140 		KEYDBG(IPSEC_STAMP,
1141 		    char buf[IPSEC_ADDRSTRLEN];
1142 		    printf("%s: SA not found for spi %u proto %u dst %s\n",
1143 			__func__, ntohl(spi), proto, ipsec_address(dst, buf,
1144 			sizeof(buf))));
1145 	} else {
1146 		KEYDBG(IPSEC_STAMP,
1147 		    printf("%s: return SA(%p)\n", __func__, sav));
1148 		KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1149 	}
1150 	return (sav);
1151 }
1152 
1153 struct secasvar *
1154 key_allocsa_tunnel(union sockaddr_union *src, union sockaddr_union *dst,
1155     uint8_t proto)
1156 {
1157 	SAHTREE_RLOCK_TRACKER;
1158 	struct secasindex saidx;
1159 	struct secashead *sah;
1160 	struct secasvar *sav;
1161 
1162 	IPSEC_ASSERT(src != NULL, ("null src address"));
1163 	IPSEC_ASSERT(dst != NULL, ("null dst address"));
1164 
1165 	KEY_SETSECASIDX(proto, IPSEC_MODE_TUNNEL, 0, &src->sa,
1166 	    &dst->sa, &saidx);
1167 
1168 	sav = NULL;
1169 	SAHTREE_RLOCK();
1170 	LIST_FOREACH(sah, SAHADDRHASH_HASH(&saidx), addrhash) {
1171 		if (IPSEC_MODE_TUNNEL != sah->saidx.mode)
1172 			continue;
1173 		if (proto != sah->saidx.proto)
1174 			continue;
1175 		if (key_sockaddrcmp(&src->sa, &sah->saidx.src.sa, 0) != 0)
1176 			continue;
1177 		if (key_sockaddrcmp(&dst->sa, &sah->saidx.dst.sa, 0) != 0)
1178 			continue;
1179 		/* XXXAE: is key_preferred_oldsa reasonably?*/
1180 		if (V_key_preferred_oldsa)
1181 			sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
1182 		else
1183 			sav = TAILQ_FIRST(&sah->savtree_alive);
1184 		if (sav != NULL) {
1185 			SAV_ADDREF(sav);
1186 			break;
1187 		}
1188 	}
1189 	SAHTREE_RUNLOCK();
1190 	KEYDBG(IPSEC_STAMP,
1191 	    printf("%s: return SA(%p)\n", __func__, sav));
1192 	if (sav != NULL)
1193 		KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1194 	return (sav);
1195 }
1196 
1197 /*
1198  * Must be called after calling key_allocsp().
1199  */
1200 void
1201 key_freesp(struct secpolicy **spp)
1202 {
1203 	struct secpolicy *sp = *spp;
1204 
1205 	IPSEC_ASSERT(sp != NULL, ("null sp"));
1206 	if (SP_DELREF(sp) == 0)
1207 		return;
1208 
1209 	KEYDBG(IPSEC_STAMP,
1210 	    printf("%s: last reference to SP(%p)\n", __func__, sp));
1211 	KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1212 
1213 	*spp = NULL;
1214 	while (sp->tcount > 0)
1215 		ipsec_delisr(sp->req[--sp->tcount]);
1216 	free(sp, M_IPSEC_SP);
1217 }
1218 
1219 static void
1220 key_unlink(struct secpolicy *sp)
1221 {
1222 
1223 	IPSEC_ASSERT(sp->spidx.dir == IPSEC_DIR_INBOUND ||
1224 	    sp->spidx.dir == IPSEC_DIR_OUTBOUND,
1225 	    ("invalid direction %u", sp->spidx.dir));
1226 	SPTREE_UNLOCK_ASSERT();
1227 
1228 	KEYDBG(KEY_STAMP,
1229 	    printf("%s: SP(%p)\n", __func__, sp));
1230 	SPTREE_WLOCK();
1231 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
1232 		/* SP is already unlinked */
1233 		SPTREE_WUNLOCK();
1234 		return;
1235 	}
1236 	sp->state = IPSEC_SPSTATE_DEAD;
1237 	TAILQ_REMOVE(&V_sptree[sp->spidx.dir], sp, chain);
1238 	V_spd_size--;
1239 	LIST_REMOVE(sp, idhash);
1240 	V_sp_genid++;
1241 	SPTREE_WUNLOCK();
1242 	if (SPDCACHE_ENABLED())
1243 		spdcache_clear();
1244 	key_freesp(&sp);
1245 }
1246 
1247 /*
1248  * insert a secpolicy into the SP database. Lower priorities first
1249  */
1250 static void
1251 key_insertsp(struct secpolicy *newsp)
1252 {
1253 	struct secpolicy *sp;
1254 
1255 	SPTREE_WLOCK_ASSERT();
1256 	TAILQ_FOREACH(sp, &V_sptree[newsp->spidx.dir], chain) {
1257 		if (newsp->priority < sp->priority) {
1258 			TAILQ_INSERT_BEFORE(sp, newsp, chain);
1259 			goto done;
1260 		}
1261 	}
1262 	TAILQ_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, chain);
1263 done:
1264 	LIST_INSERT_HEAD(SPHASH_HASH(newsp->id), newsp, idhash);
1265 	newsp->state = IPSEC_SPSTATE_ALIVE;
1266 	V_spd_size++;
1267 	V_sp_genid++;
1268 }
1269 
1270 /*
1271  * Insert a bunch of VTI secpolicies into the SPDB.
1272  * We keep VTI policies in the separate list due to following reasons:
1273  * 1) they should be immutable to user's or some deamon's attempts to
1274  *    delete. The only way delete such policies - destroy or unconfigure
1275  *    corresponding virtual inteface.
1276  * 2) such policies have traffic selector that matches all traffic per
1277  *    address family.
1278  * Since all VTI policies have the same priority, we don't care about
1279  * policies order.
1280  */
1281 int
1282 key_register_ifnet(struct secpolicy **spp, u_int count)
1283 {
1284 	struct mbuf *m;
1285 	u_int i;
1286 
1287 	SPTREE_WLOCK();
1288 	/*
1289 	 * First of try to acquire id for each SP.
1290 	 */
1291 	for (i = 0; i < count; i++) {
1292 		IPSEC_ASSERT(spp[i]->spidx.dir == IPSEC_DIR_INBOUND ||
1293 		    spp[i]->spidx.dir == IPSEC_DIR_OUTBOUND,
1294 		    ("invalid direction %u", spp[i]->spidx.dir));
1295 
1296 		if ((spp[i]->id = key_getnewspid()) == 0) {
1297 			SPTREE_WUNLOCK();
1298 			return (EAGAIN);
1299 		}
1300 	}
1301 	for (i = 0; i < count; i++) {
1302 		TAILQ_INSERT_TAIL(&V_sptree_ifnet[spp[i]->spidx.dir],
1303 		    spp[i], chain);
1304 		/*
1305 		 * NOTE: despite the fact that we keep VTI SP in the
1306 		 * separate list, SPHASH contains policies from both
1307 		 * sources. Thus SADB_X_SPDGET will correctly return
1308 		 * SP by id, because it uses SPHASH for lookups.
1309 		 */
1310 		LIST_INSERT_HEAD(SPHASH_HASH(spp[i]->id), spp[i], idhash);
1311 		spp[i]->state = IPSEC_SPSTATE_IFNET;
1312 	}
1313 	SPTREE_WUNLOCK();
1314 	/*
1315 	 * Notify user processes about new SP.
1316 	 */
1317 	for (i = 0; i < count; i++) {
1318 		m = key_setdumpsp(spp[i], SADB_X_SPDADD, 0, 0);
1319 		if (m != NULL)
1320 			key_sendup_mbuf(NULL, m, KEY_SENDUP_ALL);
1321 	}
1322 	return (0);
1323 }
1324 
1325 void
1326 key_unregister_ifnet(struct secpolicy **spp, u_int count)
1327 {
1328 	struct mbuf *m;
1329 	u_int i;
1330 
1331 	SPTREE_WLOCK();
1332 	for (i = 0; i < count; i++) {
1333 		IPSEC_ASSERT(spp[i]->spidx.dir == IPSEC_DIR_INBOUND ||
1334 		    spp[i]->spidx.dir == IPSEC_DIR_OUTBOUND,
1335 		    ("invalid direction %u", spp[i]->spidx.dir));
1336 
1337 		if (spp[i]->state != IPSEC_SPSTATE_IFNET)
1338 			continue;
1339 		spp[i]->state = IPSEC_SPSTATE_DEAD;
1340 		TAILQ_REMOVE(&V_sptree_ifnet[spp[i]->spidx.dir],
1341 		    spp[i], chain);
1342 		V_spd_size--;
1343 		LIST_REMOVE(spp[i], idhash);
1344 	}
1345 	SPTREE_WUNLOCK();
1346 	if (SPDCACHE_ENABLED())
1347 		spdcache_clear();
1348 
1349 	for (i = 0; i < count; i++) {
1350 		m = key_setdumpsp(spp[i], SADB_X_SPDDELETE, 0, 0);
1351 		if (m != NULL)
1352 			key_sendup_mbuf(NULL, m, KEY_SENDUP_ALL);
1353 	}
1354 }
1355 
1356 /*
1357  * Must be called after calling key_allocsa().
1358  * This function is called by key_freesp() to free some SA allocated
1359  * for a policy.
1360  */
1361 void
1362 key_freesav(struct secasvar **psav)
1363 {
1364 	struct secasvar *sav = *psav;
1365 
1366 	IPSEC_ASSERT(sav != NULL, ("null sav"));
1367 	if (SAV_DELREF(sav) == 0)
1368 		return;
1369 
1370 	KEYDBG(IPSEC_STAMP,
1371 	    printf("%s: last reference to SA(%p)\n", __func__, sav));
1372 
1373 	*psav = NULL;
1374 	key_delsav(sav);
1375 }
1376 
1377 /*
1378  * Unlink SA from SAH and SPI hash under SAHTREE_WLOCK.
1379  * Expect that SA has extra reference due to lookup.
1380  * Release this references, also release SAH reference after unlink.
1381  */
1382 static void
1383 key_unlinksav(struct secasvar *sav)
1384 {
1385 	struct secashead *sah;
1386 
1387 	KEYDBG(KEY_STAMP,
1388 	    printf("%s: SA(%p)\n", __func__, sav));
1389 
1390 	SAHTREE_UNLOCK_ASSERT();
1391 	SAHTREE_WLOCK();
1392 	if (sav->state == SADB_SASTATE_DEAD) {
1393 		/* SA is already unlinked */
1394 		SAHTREE_WUNLOCK();
1395 		return;
1396 	}
1397 	/* Unlink from SAH */
1398 	if (sav->state == SADB_SASTATE_LARVAL)
1399 		TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
1400 	else
1401 		TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
1402 	/* Unlink from SPI hash */
1403 	LIST_REMOVE(sav, spihash);
1404 	sav->state = SADB_SASTATE_DEAD;
1405 	sah = sav->sah;
1406 	SAHTREE_WUNLOCK();
1407 	key_freesav(&sav);
1408 	/* Since we are unlinked, release reference to SAH */
1409 	key_freesah(&sah);
1410 }
1411 
1412 /* %%% SPD management */
1413 /*
1414  * search SPD
1415  * OUT:	NULL	: not found
1416  *	others	: found, pointer to a SP.
1417  */
1418 static struct secpolicy *
1419 key_getsp(struct secpolicyindex *spidx)
1420 {
1421 	SPTREE_RLOCK_TRACKER;
1422 	struct secpolicy *sp;
1423 
1424 	IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1425 
1426 	SPTREE_RLOCK();
1427 	TAILQ_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1428 		if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1429 			SP_ADDREF(sp);
1430 			break;
1431 		}
1432 	}
1433 	SPTREE_RUNLOCK();
1434 
1435 	return sp;
1436 }
1437 
1438 /*
1439  * get SP by index.
1440  * OUT:	NULL	: not found
1441  *	others	: found, pointer to referenced SP.
1442  */
1443 static struct secpolicy *
1444 key_getspbyid(uint32_t id)
1445 {
1446 	SPTREE_RLOCK_TRACKER;
1447 	struct secpolicy *sp;
1448 
1449 	SPTREE_RLOCK();
1450 	LIST_FOREACH(sp, SPHASH_HASH(id), idhash) {
1451 		if (sp->id == id) {
1452 			SP_ADDREF(sp);
1453 			break;
1454 		}
1455 	}
1456 	SPTREE_RUNLOCK();
1457 	return (sp);
1458 }
1459 
1460 struct secpolicy *
1461 key_newsp(void)
1462 {
1463 	struct secpolicy *sp;
1464 
1465 	sp = malloc(sizeof(*sp), M_IPSEC_SP, M_NOWAIT | M_ZERO);
1466 	if (sp != NULL)
1467 		SP_INITREF(sp);
1468 	return (sp);
1469 }
1470 
1471 struct ipsecrequest *
1472 ipsec_newisr(void)
1473 {
1474 
1475 	return (malloc(sizeof(struct ipsecrequest), M_IPSEC_SR,
1476 	    M_NOWAIT | M_ZERO));
1477 }
1478 
1479 void
1480 ipsec_delisr(struct ipsecrequest *p)
1481 {
1482 
1483 	free(p, M_IPSEC_SR);
1484 }
1485 
1486 /*
1487  * create secpolicy structure from sadb_x_policy structure.
1488  * NOTE: `state', `secpolicyindex' and 'id' in secpolicy structure
1489  * are not set, so must be set properly later.
1490  */
1491 struct secpolicy *
1492 key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
1493 {
1494 	struct secpolicy *newsp;
1495 
1496 	IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1497 	IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1498 
1499 	if (len != PFKEY_EXTLEN(xpl0)) {
1500 		ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1501 		*error = EINVAL;
1502 		return NULL;
1503 	}
1504 
1505 	if ((newsp = key_newsp()) == NULL) {
1506 		*error = ENOBUFS;
1507 		return NULL;
1508 	}
1509 
1510 	newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1511 	newsp->policy = xpl0->sadb_x_policy_type;
1512 	newsp->priority = xpl0->sadb_x_policy_priority;
1513 	newsp->tcount = 0;
1514 
1515 	/* check policy */
1516 	switch (xpl0->sadb_x_policy_type) {
1517 	case IPSEC_POLICY_DISCARD:
1518 	case IPSEC_POLICY_NONE:
1519 	case IPSEC_POLICY_ENTRUST:
1520 	case IPSEC_POLICY_BYPASS:
1521 		break;
1522 
1523 	case IPSEC_POLICY_IPSEC:
1524 	    {
1525 		struct sadb_x_ipsecrequest *xisr;
1526 		struct ipsecrequest *isr;
1527 		int tlen;
1528 
1529 		/* validity check */
1530 		if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1531 			ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1532 				__func__));
1533 			key_freesp(&newsp);
1534 			*error = EINVAL;
1535 			return NULL;
1536 		}
1537 
1538 		tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1539 		xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1540 
1541 		while (tlen > 0) {
1542 			/* length check */
1543 			if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr) ||
1544 			    xisr->sadb_x_ipsecrequest_len > tlen) {
1545 				ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1546 					"length.\n", __func__));
1547 				key_freesp(&newsp);
1548 				*error = EINVAL;
1549 				return NULL;
1550 			}
1551 
1552 			if (newsp->tcount >= IPSEC_MAXREQ) {
1553 				ipseclog((LOG_DEBUG,
1554 				    "%s: too many ipsecrequests.\n",
1555 				    __func__));
1556 				key_freesp(&newsp);
1557 				*error = EINVAL;
1558 				return (NULL);
1559 			}
1560 
1561 			/* allocate request buffer */
1562 			/* NB: data structure is zero'd */
1563 			isr = ipsec_newisr();
1564 			if (isr == NULL) {
1565 				ipseclog((LOG_DEBUG,
1566 				    "%s: No more memory.\n", __func__));
1567 				key_freesp(&newsp);
1568 				*error = ENOBUFS;
1569 				return NULL;
1570 			}
1571 
1572 			newsp->req[newsp->tcount++] = isr;
1573 
1574 			/* set values */
1575 			switch (xisr->sadb_x_ipsecrequest_proto) {
1576 			case IPPROTO_ESP:
1577 			case IPPROTO_AH:
1578 			case IPPROTO_IPCOMP:
1579 				break;
1580 			default:
1581 				ipseclog((LOG_DEBUG,
1582 				    "%s: invalid proto type=%u\n", __func__,
1583 				    xisr->sadb_x_ipsecrequest_proto));
1584 				key_freesp(&newsp);
1585 				*error = EPROTONOSUPPORT;
1586 				return NULL;
1587 			}
1588 			isr->saidx.proto =
1589 			    (uint8_t)xisr->sadb_x_ipsecrequest_proto;
1590 
1591 			switch (xisr->sadb_x_ipsecrequest_mode) {
1592 			case IPSEC_MODE_TRANSPORT:
1593 			case IPSEC_MODE_TUNNEL:
1594 				break;
1595 			case IPSEC_MODE_ANY:
1596 			default:
1597 				ipseclog((LOG_DEBUG,
1598 				    "%s: invalid mode=%u\n", __func__,
1599 				    xisr->sadb_x_ipsecrequest_mode));
1600 				key_freesp(&newsp);
1601 				*error = EINVAL;
1602 				return NULL;
1603 			}
1604 			isr->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1605 
1606 			switch (xisr->sadb_x_ipsecrequest_level) {
1607 			case IPSEC_LEVEL_DEFAULT:
1608 			case IPSEC_LEVEL_USE:
1609 			case IPSEC_LEVEL_REQUIRE:
1610 				break;
1611 			case IPSEC_LEVEL_UNIQUE:
1612 				/* validity check */
1613 				/*
1614 				 * If range violation of reqid, kernel will
1615 				 * update it, don't refuse it.
1616 				 */
1617 				if (xisr->sadb_x_ipsecrequest_reqid
1618 						> IPSEC_MANUAL_REQID_MAX) {
1619 					ipseclog((LOG_DEBUG,
1620 					    "%s: reqid=%d range "
1621 					    "violation, updated by kernel.\n",
1622 					    __func__,
1623 					    xisr->sadb_x_ipsecrequest_reqid));
1624 					xisr->sadb_x_ipsecrequest_reqid = 0;
1625 				}
1626 
1627 				/* allocate new reqid id if reqid is zero. */
1628 				if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1629 					u_int32_t reqid;
1630 					if ((reqid = key_newreqid()) == 0) {
1631 						key_freesp(&newsp);
1632 						*error = ENOBUFS;
1633 						return NULL;
1634 					}
1635 					isr->saidx.reqid = reqid;
1636 					xisr->sadb_x_ipsecrequest_reqid = reqid;
1637 				} else {
1638 				/* set it for manual keying. */
1639 					isr->saidx.reqid =
1640 					    xisr->sadb_x_ipsecrequest_reqid;
1641 				}
1642 				break;
1643 
1644 			default:
1645 				ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1646 					__func__,
1647 					xisr->sadb_x_ipsecrequest_level));
1648 				key_freesp(&newsp);
1649 				*error = EINVAL;
1650 				return NULL;
1651 			}
1652 			isr->level = xisr->sadb_x_ipsecrequest_level;
1653 
1654 			/* set IP addresses if there */
1655 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1656 				struct sockaddr *paddr;
1657 
1658 				len = tlen - sizeof(*xisr);
1659 				paddr = (struct sockaddr *)(xisr + 1);
1660 				/* validity check */
1661 				if (len < sizeof(struct sockaddr) ||
1662 				    len < 2 * paddr->sa_len ||
1663 				    paddr->sa_len > sizeof(isr->saidx.src)) {
1664 					ipseclog((LOG_DEBUG, "%s: invalid "
1665 						"request address length.\n",
1666 						__func__));
1667 					key_freesp(&newsp);
1668 					*error = EINVAL;
1669 					return NULL;
1670 				}
1671 				/*
1672 				 * Request length should be enough to keep
1673 				 * source and destination addresses.
1674 				 */
1675 				if (xisr->sadb_x_ipsecrequest_len <
1676 				    sizeof(*xisr) + 2 * paddr->sa_len) {
1677 					ipseclog((LOG_DEBUG, "%s: invalid "
1678 					    "ipsecrequest length.\n",
1679 					    __func__));
1680 					key_freesp(&newsp);
1681 					*error = EINVAL;
1682 					return (NULL);
1683 				}
1684 				bcopy(paddr, &isr->saidx.src, paddr->sa_len);
1685 				paddr = (struct sockaddr *)((caddr_t)paddr +
1686 				    paddr->sa_len);
1687 
1688 				/* validity check */
1689 				if (paddr->sa_len !=
1690 				    isr->saidx.src.sa.sa_len) {
1691 					ipseclog((LOG_DEBUG, "%s: invalid "
1692 						"request address length.\n",
1693 						__func__));
1694 					key_freesp(&newsp);
1695 					*error = EINVAL;
1696 					return NULL;
1697 				}
1698 				/* AF family should match */
1699 				if (paddr->sa_family !=
1700 				    isr->saidx.src.sa.sa_family) {
1701 					ipseclog((LOG_DEBUG, "%s: address "
1702 					    "family doesn't match.\n",
1703 						__func__));
1704 					key_freesp(&newsp);
1705 					*error = EINVAL;
1706 					return (NULL);
1707 				}
1708 				bcopy(paddr, &isr->saidx.dst, paddr->sa_len);
1709 			} else {
1710 				/*
1711 				 * Addresses for TUNNEL mode requests are
1712 				 * mandatory.
1713 				 */
1714 				if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1715 					ipseclog((LOG_DEBUG, "%s: missing "
1716 					    "request addresses.\n", __func__));
1717 					key_freesp(&newsp);
1718 					*error = EINVAL;
1719 					return (NULL);
1720 				}
1721 			}
1722 			tlen -= xisr->sadb_x_ipsecrequest_len;
1723 
1724 			/* validity check */
1725 			if (tlen < 0) {
1726 				ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1727 					__func__));
1728 				key_freesp(&newsp);
1729 				*error = EINVAL;
1730 				return NULL;
1731 			}
1732 
1733 			xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1734 			                 + xisr->sadb_x_ipsecrequest_len);
1735 		}
1736 		/* XXXAE: LARVAL SP */
1737 		if (newsp->tcount < 1) {
1738 			ipseclog((LOG_DEBUG, "%s: valid IPSEC transforms "
1739 			    "not found.\n", __func__));
1740 			key_freesp(&newsp);
1741 			*error = EINVAL;
1742 			return (NULL);
1743 		}
1744 	    }
1745 		break;
1746 	default:
1747 		ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1748 		key_freesp(&newsp);
1749 		*error = EINVAL;
1750 		return NULL;
1751 	}
1752 
1753 	*error = 0;
1754 	return (newsp);
1755 }
1756 
1757 uint32_t
1758 key_newreqid(void)
1759 {
1760 	static uint32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1761 
1762 	if (auto_reqid == ~0)
1763 		auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1764 	else
1765 		auto_reqid++;
1766 
1767 	/* XXX should be unique check */
1768 	return (auto_reqid);
1769 }
1770 
1771 /*
1772  * copy secpolicy struct to sadb_x_policy structure indicated.
1773  */
1774 static struct mbuf *
1775 key_sp2mbuf(struct secpolicy *sp)
1776 {
1777 	struct mbuf *m;
1778 	size_t tlen;
1779 
1780 	tlen = key_getspreqmsglen(sp);
1781 	m = m_get2(tlen, M_NOWAIT, MT_DATA, 0);
1782 	if (m == NULL)
1783 		return (NULL);
1784 	m_align(m, tlen);
1785 	m->m_len = tlen;
1786 	if (key_sp2msg(sp, m->m_data, &tlen) != 0) {
1787 		m_freem(m);
1788 		return (NULL);
1789 	}
1790 	return (m);
1791 }
1792 
1793 int
1794 key_sp2msg(struct secpolicy *sp, void *request, size_t *len)
1795 {
1796 	struct sadb_x_ipsecrequest *xisr;
1797 	struct sadb_x_policy *xpl;
1798 	struct ipsecrequest *isr;
1799 	size_t xlen, ilen;
1800 	caddr_t p;
1801 	int error, i;
1802 
1803 	IPSEC_ASSERT(sp != NULL, ("null policy"));
1804 
1805 	xlen = sizeof(*xpl);
1806 	if (*len < xlen)
1807 		return (EINVAL);
1808 
1809 	error = 0;
1810 	bzero(request, *len);
1811 	xpl = (struct sadb_x_policy *)request;
1812 	xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1813 	xpl->sadb_x_policy_type = sp->policy;
1814 	xpl->sadb_x_policy_dir = sp->spidx.dir;
1815 	xpl->sadb_x_policy_id = sp->id;
1816 	xpl->sadb_x_policy_priority = sp->priority;
1817 	switch (sp->state) {
1818 	case IPSEC_SPSTATE_IFNET:
1819 		xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_IFNET;
1820 		break;
1821 	case IPSEC_SPSTATE_PCB:
1822 		xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_PCB;
1823 		break;
1824 	default:
1825 		xpl->sadb_x_policy_scope = IPSEC_POLICYSCOPE_GLOBAL;
1826 	}
1827 
1828 	/* if is the policy for ipsec ? */
1829 	if (sp->policy == IPSEC_POLICY_IPSEC) {
1830 		p = (caddr_t)xpl + sizeof(*xpl);
1831 		for (i = 0; i < sp->tcount; i++) {
1832 			isr = sp->req[i];
1833 			ilen = PFKEY_ALIGN8(sizeof(*xisr) +
1834 			    isr->saidx.src.sa.sa_len +
1835 			    isr->saidx.dst.sa.sa_len);
1836 			xlen += ilen;
1837 			if (xlen > *len) {
1838 				error = ENOBUFS;
1839 				/* Calculate needed size */
1840 				continue;
1841 			}
1842 			xisr = (struct sadb_x_ipsecrequest *)p;
1843 			xisr->sadb_x_ipsecrequest_len = ilen;
1844 			xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1845 			xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1846 			xisr->sadb_x_ipsecrequest_level = isr->level;
1847 			xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1848 
1849 			p += sizeof(*xisr);
1850 			bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1851 			p += isr->saidx.src.sa.sa_len;
1852 			bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1853 			p += isr->saidx.dst.sa.sa_len;
1854 		}
1855 	}
1856 	xpl->sadb_x_policy_len = PFKEY_UNIT64(xlen);
1857 	if (error == 0)
1858 		*len = xlen;
1859 	else
1860 		*len = sizeof(*xpl);
1861 	return (error);
1862 }
1863 
1864 /* m will not be freed nor modified */
1865 static struct mbuf *
1866 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1867     int ndeep, int nitem, ...)
1868 {
1869 	va_list ap;
1870 	int idx;
1871 	int i;
1872 	struct mbuf *result = NULL, *n;
1873 	int len;
1874 
1875 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
1876 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1877 
1878 	va_start(ap, nitem);
1879 	for (i = 0; i < nitem; i++) {
1880 		idx = va_arg(ap, int);
1881 		if (idx < 0 || idx > SADB_EXT_MAX)
1882 			goto fail;
1883 		/* don't attempt to pull empty extension */
1884 		if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1885 			continue;
1886 		if (idx != SADB_EXT_RESERVED  &&
1887 		    (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1888 			continue;
1889 
1890 		if (idx == SADB_EXT_RESERVED) {
1891 			len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1892 
1893 			IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1894 
1895 			MGETHDR(n, M_NOWAIT, MT_DATA);
1896 			if (!n)
1897 				goto fail;
1898 			n->m_len = len;
1899 			n->m_next = NULL;
1900 			m_copydata(m, 0, sizeof(struct sadb_msg),
1901 			    mtod(n, caddr_t));
1902 		} else if (i < ndeep) {
1903 			len = mhp->extlen[idx];
1904 			n = m_get2(len, M_NOWAIT, MT_DATA, 0);
1905 			if (n == NULL)
1906 				goto fail;
1907 			m_align(n, len);
1908 			n->m_len = len;
1909 			m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1910 			    mtod(n, caddr_t));
1911 		} else {
1912 			n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1913 			    M_NOWAIT);
1914 		}
1915 		if (n == NULL)
1916 			goto fail;
1917 
1918 		if (result)
1919 			m_cat(result, n);
1920 		else
1921 			result = n;
1922 	}
1923 	va_end(ap);
1924 
1925 	if ((result->m_flags & M_PKTHDR) != 0) {
1926 		result->m_pkthdr.len = 0;
1927 		for (n = result; n; n = n->m_next)
1928 			result->m_pkthdr.len += n->m_len;
1929 	}
1930 
1931 	return result;
1932 
1933 fail:
1934 	m_freem(result);
1935 	va_end(ap);
1936 	return NULL;
1937 }
1938 
1939 /*
1940  * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1941  * add an entry to SP database, when received
1942  *   <base, address(SD), (lifetime(H),) policy>
1943  * from the user(?).
1944  * Adding to SP database,
1945  * and send
1946  *   <base, address(SD), (lifetime(H),) policy>
1947  * to the socket which was send.
1948  *
1949  * SPDADD set a unique policy entry.
1950  * SPDSETIDX like SPDADD without a part of policy requests.
1951  * SPDUPDATE replace a unique policy entry.
1952  *
1953  * XXXAE: serialize this in PF_KEY to avoid races.
1954  * m will always be freed.
1955  */
1956 static int
1957 key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1958 {
1959 	struct secpolicyindex spidx;
1960 	struct sadb_address *src0, *dst0;
1961 	struct sadb_x_policy *xpl0, *xpl;
1962 	struct sadb_lifetime *lft = NULL;
1963 	struct secpolicy *newsp;
1964 	int error;
1965 
1966 	IPSEC_ASSERT(so != NULL, ("null socket"));
1967 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
1968 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1969 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1970 
1971 	if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
1972 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
1973 	    SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY)) {
1974 		ipseclog((LOG_DEBUG,
1975 		    "%s: invalid message: missing required header.\n",
1976 		    __func__));
1977 		return key_senderror(so, m, EINVAL);
1978 	}
1979 	if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
1980 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
1981 	    SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
1982 		ipseclog((LOG_DEBUG,
1983 		    "%s: invalid message: wrong header size.\n", __func__));
1984 		return key_senderror(so, m, EINVAL);
1985 	}
1986 	if (!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD)) {
1987 		if (SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_HARD)) {
1988 			ipseclog((LOG_DEBUG,
1989 			    "%s: invalid message: wrong header size.\n",
1990 			    __func__));
1991 			return key_senderror(so, m, EINVAL);
1992 		}
1993 		lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1994 	}
1995 
1996 	src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1997 	dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1998 	xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1999 
2000 	/* check the direciton */
2001 	switch (xpl0->sadb_x_policy_dir) {
2002 	case IPSEC_DIR_INBOUND:
2003 	case IPSEC_DIR_OUTBOUND:
2004 		break;
2005 	default:
2006 		ipseclog((LOG_DEBUG, "%s: invalid SP direction.\n", __func__));
2007 		return key_senderror(so, m, EINVAL);
2008 	}
2009 	/* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2010 	if (xpl0->sadb_x_policy_type != IPSEC_POLICY_DISCARD &&
2011 	    xpl0->sadb_x_policy_type != IPSEC_POLICY_NONE &&
2012 	    xpl0->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
2013 		ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
2014 		return key_senderror(so, m, EINVAL);
2015 	}
2016 
2017 	/* policy requests are mandatory when action is ipsec. */
2018 	if (xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2019 	    mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
2020 		ipseclog((LOG_DEBUG,
2021 		    "%s: policy requests required.\n", __func__));
2022 		return key_senderror(so, m, EINVAL);
2023 	}
2024 
2025 	error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
2026 	    (struct sockaddr *)(dst0 + 1));
2027 	if (error != 0 ||
2028 	    src0->sadb_address_proto != dst0->sadb_address_proto) {
2029 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
2030 		return key_senderror(so, m, error);
2031 	}
2032 	/* make secindex */
2033 	KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2034 	                src0 + 1,
2035 	                dst0 + 1,
2036 	                src0->sadb_address_prefixlen,
2037 	                dst0->sadb_address_prefixlen,
2038 	                src0->sadb_address_proto,
2039 	                &spidx);
2040 	/* Checking there is SP already or not. */
2041 	newsp = key_getsp(&spidx);
2042 	if (newsp != NULL) {
2043 		if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
2044 			KEYDBG(KEY_STAMP,
2045 			    printf("%s: unlink SP(%p) for SPDUPDATE\n",
2046 				__func__, newsp));
2047 			KEYDBG(KEY_DATA, kdebug_secpolicy(newsp));
2048 			key_unlink(newsp);
2049 			key_freesp(&newsp);
2050 		} else {
2051 			key_freesp(&newsp);
2052 			ipseclog((LOG_DEBUG, "%s: a SP entry exists already.",
2053 			    __func__));
2054 			return (key_senderror(so, m, EEXIST));
2055 		}
2056 	}
2057 
2058 	/* allocate new SP entry */
2059 	if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
2060 		return key_senderror(so, m, error);
2061 	}
2062 
2063 	newsp->lastused = newsp->created = time_second;
2064 	newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
2065 	newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
2066 	bcopy(&spidx, &newsp->spidx, sizeof(spidx));
2067 
2068 	/* XXXAE: there is race between key_getsp() and key_insertsp() */
2069 	SPTREE_WLOCK();
2070 	if ((newsp->id = key_getnewspid()) == 0) {
2071 		SPTREE_WUNLOCK();
2072 		key_freesp(&newsp);
2073 		return key_senderror(so, m, ENOBUFS);
2074 	}
2075 	key_insertsp(newsp);
2076 	SPTREE_WUNLOCK();
2077 	if (SPDCACHE_ENABLED())
2078 		spdcache_clear();
2079 
2080 	KEYDBG(KEY_STAMP,
2081 	    printf("%s: SP(%p)\n", __func__, newsp));
2082 	KEYDBG(KEY_DATA, kdebug_secpolicy(newsp));
2083 
2084     {
2085 	struct mbuf *n, *mpolicy;
2086 	struct sadb_msg *newmsg;
2087 	int off;
2088 
2089 	/* create new sadb_msg to reply. */
2090 	if (lft) {
2091 		n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
2092 		    SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
2093 		    SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2094 	} else {
2095 		n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
2096 		    SADB_X_EXT_POLICY,
2097 		    SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2098 	}
2099 	if (!n)
2100 		return key_senderror(so, m, ENOBUFS);
2101 
2102 	if (n->m_len < sizeof(*newmsg)) {
2103 		n = m_pullup(n, sizeof(*newmsg));
2104 		if (!n)
2105 			return key_senderror(so, m, ENOBUFS);
2106 	}
2107 	newmsg = mtod(n, struct sadb_msg *);
2108 	newmsg->sadb_msg_errno = 0;
2109 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2110 
2111 	off = 0;
2112 	mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2113 	    sizeof(*xpl), &off);
2114 	if (mpolicy == NULL) {
2115 		/* n is already freed */
2116 		return key_senderror(so, m, ENOBUFS);
2117 	}
2118 	xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2119 	if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2120 		m_freem(n);
2121 		return key_senderror(so, m, EINVAL);
2122 	}
2123 	xpl->sadb_x_policy_id = newsp->id;
2124 
2125 	m_freem(m);
2126 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2127     }
2128 }
2129 
2130 /*
2131  * get new policy id.
2132  * OUT:
2133  *	0:	failure.
2134  *	others: success.
2135  */
2136 static uint32_t
2137 key_getnewspid(void)
2138 {
2139 	struct secpolicy *sp;
2140 	uint32_t newid = 0;
2141 	int count = V_key_spi_trycnt;	/* XXX */
2142 
2143 	SPTREE_WLOCK_ASSERT();
2144 	while (count--) {
2145 		if (V_policy_id == ~0) /* overflowed */
2146 			newid = V_policy_id = 1;
2147 		else
2148 			newid = ++V_policy_id;
2149 		LIST_FOREACH(sp, SPHASH_HASH(newid), idhash) {
2150 			if (sp->id == newid)
2151 				break;
2152 		}
2153 		if (sp == NULL)
2154 			break;
2155 	}
2156 	if (count == 0 || newid == 0) {
2157 		ipseclog((LOG_DEBUG, "%s: failed to allocate policy id.\n",
2158 		    __func__));
2159 		return (0);
2160 	}
2161 	return (newid);
2162 }
2163 
2164 /*
2165  * SADB_SPDDELETE processing
2166  * receive
2167  *   <base, address(SD), policy(*)>
2168  * from the user(?), and set SADB_SASTATE_DEAD,
2169  * and send,
2170  *   <base, address(SD), policy(*)>
2171  * to the ikmpd.
2172  * policy(*) including direction of policy.
2173  *
2174  * m will always be freed.
2175  */
2176 static int
2177 key_spddelete(struct socket *so, struct mbuf *m,
2178     const struct sadb_msghdr *mhp)
2179 {
2180 	struct secpolicyindex spidx;
2181 	struct sadb_address *src0, *dst0;
2182 	struct sadb_x_policy *xpl0;
2183 	struct secpolicy *sp;
2184 
2185 	IPSEC_ASSERT(so != NULL, ("null so"));
2186 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
2187 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2188 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2189 
2190 	if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
2191 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
2192 	    SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY)) {
2193 		ipseclog((LOG_DEBUG,
2194 		    "%s: invalid message: missing required header.\n",
2195 		    __func__));
2196 		return key_senderror(so, m, EINVAL);
2197 	}
2198 	if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
2199 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
2200 	    SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
2201 		ipseclog((LOG_DEBUG,
2202 		    "%s: invalid message: wrong header size.\n", __func__));
2203 		return key_senderror(so, m, EINVAL);
2204 	}
2205 
2206 	src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2207 	dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2208 	xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2209 
2210 	/* check the direciton */
2211 	switch (xpl0->sadb_x_policy_dir) {
2212 	case IPSEC_DIR_INBOUND:
2213 	case IPSEC_DIR_OUTBOUND:
2214 		break;
2215 	default:
2216 		ipseclog((LOG_DEBUG, "%s: invalid SP direction.\n", __func__));
2217 		return key_senderror(so, m, EINVAL);
2218 	}
2219 	/* Only DISCARD, NONE and IPSEC are allowed */
2220 	if (xpl0->sadb_x_policy_type != IPSEC_POLICY_DISCARD &&
2221 	    xpl0->sadb_x_policy_type != IPSEC_POLICY_NONE &&
2222 	    xpl0->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
2223 		ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
2224 		return key_senderror(so, m, EINVAL);
2225 	}
2226 	if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
2227 	    (struct sockaddr *)(dst0 + 1)) != 0 ||
2228 	    src0->sadb_address_proto != dst0->sadb_address_proto) {
2229 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
2230 		return key_senderror(so, m, EINVAL);
2231 	}
2232 	/* make secindex */
2233 	KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2234 	                src0 + 1,
2235 	                dst0 + 1,
2236 	                src0->sadb_address_prefixlen,
2237 	                dst0->sadb_address_prefixlen,
2238 	                src0->sadb_address_proto,
2239 	                &spidx);
2240 
2241 	/* Is there SP in SPD ? */
2242 	if ((sp = key_getsp(&spidx)) == NULL) {
2243 		ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2244 		return key_senderror(so, m, EINVAL);
2245 	}
2246 
2247 	/* save policy id to buffer to be returned. */
2248 	xpl0->sadb_x_policy_id = sp->id;
2249 
2250 	KEYDBG(KEY_STAMP,
2251 	    printf("%s: SP(%p)\n", __func__, sp));
2252 	KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
2253 	key_unlink(sp);
2254 	key_freesp(&sp);
2255 
2256     {
2257 	struct mbuf *n;
2258 	struct sadb_msg *newmsg;
2259 
2260 	/* create new sadb_msg to reply. */
2261 	n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2262 	    SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2263 	if (!n)
2264 		return key_senderror(so, m, ENOBUFS);
2265 
2266 	newmsg = mtod(n, struct sadb_msg *);
2267 	newmsg->sadb_msg_errno = 0;
2268 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2269 
2270 	m_freem(m);
2271 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2272     }
2273 }
2274 
2275 /*
2276  * SADB_SPDDELETE2 processing
2277  * receive
2278  *   <base, policy(*)>
2279  * from the user(?), and set SADB_SASTATE_DEAD,
2280  * and send,
2281  *   <base, policy(*)>
2282  * to the ikmpd.
2283  * policy(*) including direction of policy.
2284  *
2285  * m will always be freed.
2286  */
2287 static int
2288 key_spddelete2(struct socket *so, struct mbuf *m,
2289     const struct sadb_msghdr *mhp)
2290 {
2291 	struct secpolicy *sp;
2292 	uint32_t id;
2293 
2294 	IPSEC_ASSERT(so != NULL, ("null socket"));
2295 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
2296 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2297 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2298 
2299 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY) ||
2300 	    SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
2301 		ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2302 		    __func__));
2303 		return key_senderror(so, m, EINVAL);
2304 	}
2305 
2306 	id = ((struct sadb_x_policy *)
2307 	    mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2308 
2309 	/* Is there SP in SPD ? */
2310 	if ((sp = key_getspbyid(id)) == NULL) {
2311 		ipseclog((LOG_DEBUG, "%s: no SP found for id %u.\n",
2312 		    __func__, id));
2313 		return key_senderror(so, m, EINVAL);
2314 	}
2315 
2316 	KEYDBG(KEY_STAMP,
2317 	    printf("%s: SP(%p)\n", __func__, sp));
2318 	KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
2319 	key_unlink(sp);
2320 	if (sp->state != IPSEC_SPSTATE_DEAD) {
2321 		ipseclog((LOG_DEBUG, "%s: failed to delete SP with id %u.\n",
2322 		    __func__, id));
2323 		key_freesp(&sp);
2324 		return (key_senderror(so, m, EACCES));
2325 	}
2326 	key_freesp(&sp);
2327 
2328     {
2329 	struct mbuf *n, *nn;
2330 	struct sadb_msg *newmsg;
2331 	int off, len;
2332 
2333 	/* create new sadb_msg to reply. */
2334 	len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2335 
2336 	MGETHDR(n, M_NOWAIT, MT_DATA);
2337 	if (n && len > MHLEN) {
2338 		if (!(MCLGET(n, M_NOWAIT))) {
2339 			m_freem(n);
2340 			n = NULL;
2341 		}
2342 	}
2343 	if (!n)
2344 		return key_senderror(so, m, ENOBUFS);
2345 
2346 	n->m_len = len;
2347 	n->m_next = NULL;
2348 	off = 0;
2349 
2350 	m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2351 	off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2352 
2353 	IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2354 		off, len));
2355 
2356 	n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2357 	    mhp->extlen[SADB_X_EXT_POLICY], M_NOWAIT);
2358 	if (!n->m_next) {
2359 		m_freem(n);
2360 		return key_senderror(so, m, ENOBUFS);
2361 	}
2362 
2363 	n->m_pkthdr.len = 0;
2364 	for (nn = n; nn; nn = nn->m_next)
2365 		n->m_pkthdr.len += nn->m_len;
2366 
2367 	newmsg = mtod(n, struct sadb_msg *);
2368 	newmsg->sadb_msg_errno = 0;
2369 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2370 
2371 	m_freem(m);
2372 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2373     }
2374 }
2375 
2376 /*
2377  * SADB_X_SPDGET processing
2378  * receive
2379  *   <base, policy(*)>
2380  * from the user(?),
2381  * and send,
2382  *   <base, address(SD), policy>
2383  * to the ikmpd.
2384  * policy(*) including direction of policy.
2385  *
2386  * m will always be freed.
2387  */
2388 static int
2389 key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2390 {
2391 	struct secpolicy *sp;
2392 	struct mbuf *n;
2393 	uint32_t id;
2394 
2395 	IPSEC_ASSERT(so != NULL, ("null socket"));
2396 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
2397 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2398 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2399 
2400 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_POLICY) ||
2401 	    SADB_CHECKLEN(mhp, SADB_X_EXT_POLICY)) {
2402 		ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2403 		    __func__));
2404 		return key_senderror(so, m, EINVAL);
2405 	}
2406 
2407 	id = ((struct sadb_x_policy *)
2408 	    mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2409 
2410 	/* Is there SP in SPD ? */
2411 	if ((sp = key_getspbyid(id)) == NULL) {
2412 		ipseclog((LOG_DEBUG, "%s: no SP found for id %u.\n",
2413 		    __func__, id));
2414 		return key_senderror(so, m, ENOENT);
2415 	}
2416 
2417 	n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
2418 	    mhp->msg->sadb_msg_pid);
2419 	key_freesp(&sp);
2420 	if (n != NULL) {
2421 		m_freem(m);
2422 		return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2423 	} else
2424 		return key_senderror(so, m, ENOBUFS);
2425 }
2426 
2427 /*
2428  * SADB_X_SPDACQUIRE processing.
2429  * Acquire policy and SA(s) for a *OUTBOUND* packet.
2430  * send
2431  *   <base, policy(*)>
2432  * to KMD, and expect to receive
2433  *   <base> with SADB_X_SPDACQUIRE if error occurred,
2434  * or
2435  *   <base, policy>
2436  * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2437  * policy(*) is without policy requests.
2438  *
2439  *    0     : succeed
2440  *    others: error number
2441  */
2442 int
2443 key_spdacquire(struct secpolicy *sp)
2444 {
2445 	struct mbuf *result = NULL, *m;
2446 	struct secspacq *newspacq;
2447 
2448 	IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2449 	IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2450 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2451 		("policy not IPSEC %u", sp->policy));
2452 
2453 	/* Get an entry to check whether sent message or not. */
2454 	newspacq = key_getspacq(&sp->spidx);
2455 	if (newspacq != NULL) {
2456 		if (V_key_blockacq_count < newspacq->count) {
2457 			/* reset counter and do send message. */
2458 			newspacq->count = 0;
2459 		} else {
2460 			/* increment counter and do nothing. */
2461 			newspacq->count++;
2462 			SPACQ_UNLOCK();
2463 			return (0);
2464 		}
2465 		SPACQ_UNLOCK();
2466 	} else {
2467 		/* make new entry for blocking to send SADB_ACQUIRE. */
2468 		newspacq = key_newspacq(&sp->spidx);
2469 		if (newspacq == NULL)
2470 			return ENOBUFS;
2471 	}
2472 
2473 	/* create new sadb_msg to reply. */
2474 	m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2475 	if (!m)
2476 		return ENOBUFS;
2477 
2478 	result = m;
2479 
2480 	result->m_pkthdr.len = 0;
2481 	for (m = result; m; m = m->m_next)
2482 		result->m_pkthdr.len += m->m_len;
2483 
2484 	mtod(result, struct sadb_msg *)->sadb_msg_len =
2485 	    PFKEY_UNIT64(result->m_pkthdr.len);
2486 
2487 	return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2488 }
2489 
2490 /*
2491  * SADB_SPDFLUSH processing
2492  * receive
2493  *   <base>
2494  * from the user, and free all entries in secpctree.
2495  * and send,
2496  *   <base>
2497  * to the user.
2498  * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2499  *
2500  * m will always be freed.
2501  */
2502 static int
2503 key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2504 {
2505 	struct secpolicy_queue drainq;
2506 	struct sadb_msg *newmsg;
2507 	struct secpolicy *sp, *nextsp;
2508 	u_int dir;
2509 
2510 	IPSEC_ASSERT(so != NULL, ("null socket"));
2511 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
2512 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2513 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2514 
2515 	if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2516 		return key_senderror(so, m, EINVAL);
2517 
2518 	TAILQ_INIT(&drainq);
2519 	SPTREE_WLOCK();
2520 	for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2521 		TAILQ_CONCAT(&drainq, &V_sptree[dir], chain);
2522 	}
2523 	/*
2524 	 * We need to set state to DEAD for each policy to be sure,
2525 	 * that another thread won't try to unlink it.
2526 	 * Also remove SP from sphash.
2527 	 */
2528 	TAILQ_FOREACH(sp, &drainq, chain) {
2529 		sp->state = IPSEC_SPSTATE_DEAD;
2530 		LIST_REMOVE(sp, idhash);
2531 	}
2532 	V_sp_genid++;
2533 	V_spd_size = 0;
2534 	SPTREE_WUNLOCK();
2535 	if (SPDCACHE_ENABLED())
2536 		spdcache_clear();
2537 	sp = TAILQ_FIRST(&drainq);
2538 	while (sp != NULL) {
2539 		nextsp = TAILQ_NEXT(sp, chain);
2540 		key_freesp(&sp);
2541 		sp = nextsp;
2542 	}
2543 
2544 	if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2545 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2546 		return key_senderror(so, m, ENOBUFS);
2547 	}
2548 
2549 	if (m->m_next)
2550 		m_freem(m->m_next);
2551 	m->m_next = NULL;
2552 	m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2553 	newmsg = mtod(m, struct sadb_msg *);
2554 	newmsg->sadb_msg_errno = 0;
2555 	newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2556 
2557 	return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2558 }
2559 
2560 static uint8_t
2561 key_satype2scopemask(uint8_t satype)
2562 {
2563 
2564 	if (satype == IPSEC_POLICYSCOPE_ANY)
2565 		return (0xff);
2566 	return (satype);
2567 }
2568 /*
2569  * SADB_SPDDUMP processing
2570  * receive
2571  *   <base>
2572  * from the user, and dump all SP leaves and send,
2573  *   <base> .....
2574  * to the ikmpd.
2575  *
2576  * NOTE:
2577  *   sadb_msg_satype is considered as mask of policy scopes.
2578  *   m will always be freed.
2579  */
2580 static int
2581 key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2582 {
2583 	SPTREE_RLOCK_TRACKER;
2584 	struct secpolicy *sp;
2585 	struct mbuf *n;
2586 	int cnt;
2587 	u_int dir, scope;
2588 
2589 	IPSEC_ASSERT(so != NULL, ("null socket"));
2590 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
2591 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2592 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2593 
2594 	/* search SPD entry and get buffer size. */
2595 	cnt = 0;
2596 	scope = key_satype2scopemask(mhp->msg->sadb_msg_satype);
2597 	SPTREE_RLOCK();
2598 	for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2599 		if (scope & IPSEC_POLICYSCOPE_GLOBAL) {
2600 			TAILQ_FOREACH(sp, &V_sptree[dir], chain)
2601 				cnt++;
2602 		}
2603 		if (scope & IPSEC_POLICYSCOPE_IFNET) {
2604 			TAILQ_FOREACH(sp, &V_sptree_ifnet[dir], chain)
2605 				cnt++;
2606 		}
2607 	}
2608 
2609 	if (cnt == 0) {
2610 		SPTREE_RUNLOCK();
2611 		return key_senderror(so, m, ENOENT);
2612 	}
2613 
2614 	for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2615 		if (scope & IPSEC_POLICYSCOPE_GLOBAL) {
2616 			TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
2617 				--cnt;
2618 				n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2619 				    mhp->msg->sadb_msg_pid);
2620 
2621 				if (n != NULL)
2622 					key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2623 			}
2624 		}
2625 		if (scope & IPSEC_POLICYSCOPE_IFNET) {
2626 			TAILQ_FOREACH(sp, &V_sptree_ifnet[dir], chain) {
2627 				--cnt;
2628 				n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2629 				    mhp->msg->sadb_msg_pid);
2630 
2631 				if (n != NULL)
2632 					key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2633 			}
2634 		}
2635 	}
2636 
2637 	SPTREE_RUNLOCK();
2638 	m_freem(m);
2639 	return (0);
2640 }
2641 
2642 static struct mbuf *
2643 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
2644     u_int32_t pid)
2645 {
2646 	struct mbuf *result = NULL, *m;
2647 	struct seclifetime lt;
2648 
2649 	m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2650 	if (!m)
2651 		goto fail;
2652 	result = m;
2653 
2654 	m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2655 	    &sp->spidx.src.sa, sp->spidx.prefs,
2656 	    sp->spidx.ul_proto);
2657 	if (!m)
2658 		goto fail;
2659 	m_cat(result, m);
2660 
2661 	m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2662 	    &sp->spidx.dst.sa, sp->spidx.prefd,
2663 	    sp->spidx.ul_proto);
2664 	if (!m)
2665 		goto fail;
2666 	m_cat(result, m);
2667 
2668 	m = key_sp2mbuf(sp);
2669 	if (!m)
2670 		goto fail;
2671 	m_cat(result, m);
2672 
2673 	if(sp->lifetime){
2674 		lt.addtime=sp->created;
2675 		lt.usetime= sp->lastused;
2676 		m = key_setlifetime(&lt, SADB_EXT_LIFETIME_CURRENT);
2677 		if (!m)
2678 			goto fail;
2679 		m_cat(result, m);
2680 
2681 		lt.addtime=sp->lifetime;
2682 		lt.usetime= sp->validtime;
2683 		m = key_setlifetime(&lt, SADB_EXT_LIFETIME_HARD);
2684 		if (!m)
2685 			goto fail;
2686 		m_cat(result, m);
2687 	}
2688 
2689 	if ((result->m_flags & M_PKTHDR) == 0)
2690 		goto fail;
2691 
2692 	if (result->m_len < sizeof(struct sadb_msg)) {
2693 		result = m_pullup(result, sizeof(struct sadb_msg));
2694 		if (result == NULL)
2695 			goto fail;
2696 	}
2697 
2698 	result->m_pkthdr.len = 0;
2699 	for (m = result; m; m = m->m_next)
2700 		result->m_pkthdr.len += m->m_len;
2701 
2702 	mtod(result, struct sadb_msg *)->sadb_msg_len =
2703 	    PFKEY_UNIT64(result->m_pkthdr.len);
2704 
2705 	return result;
2706 
2707 fail:
2708 	m_freem(result);
2709 	return NULL;
2710 }
2711 /*
2712  * get PFKEY message length for security policy and request.
2713  */
2714 static size_t
2715 key_getspreqmsglen(struct secpolicy *sp)
2716 {
2717 	size_t tlen, len;
2718 	int i;
2719 
2720 	tlen = sizeof(struct sadb_x_policy);
2721 	/* if is the policy for ipsec ? */
2722 	if (sp->policy != IPSEC_POLICY_IPSEC)
2723 		return (tlen);
2724 
2725 	/* get length of ipsec requests */
2726 	for (i = 0; i < sp->tcount; i++) {
2727 		len = sizeof(struct sadb_x_ipsecrequest)
2728 			+ sp->req[i]->saidx.src.sa.sa_len
2729 			+ sp->req[i]->saidx.dst.sa.sa_len;
2730 
2731 		tlen += PFKEY_ALIGN8(len);
2732 	}
2733 	return (tlen);
2734 }
2735 
2736 /*
2737  * SADB_SPDEXPIRE processing
2738  * send
2739  *   <base, address(SD), lifetime(CH), policy>
2740  * to KMD by PF_KEY.
2741  *
2742  * OUT:	0	: succeed
2743  *	others	: error number
2744  */
2745 static int
2746 key_spdexpire(struct secpolicy *sp)
2747 {
2748 	struct sadb_lifetime *lt;
2749 	struct mbuf *result = NULL, *m;
2750 	int len, error = -1;
2751 
2752 	IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2753 
2754 	KEYDBG(KEY_STAMP,
2755 	    printf("%s: SP(%p)\n", __func__, sp));
2756 	KEYDBG(KEY_DATA, kdebug_secpolicy(sp));
2757 
2758 	/* set msg header */
2759 	m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2760 	if (!m) {
2761 		error = ENOBUFS;
2762 		goto fail;
2763 	}
2764 	result = m;
2765 
2766 	/* create lifetime extension (current and hard) */
2767 	len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2768 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
2769 	if (m == NULL) {
2770 		error = ENOBUFS;
2771 		goto fail;
2772 	}
2773 	m_align(m, len);
2774 	m->m_len = len;
2775 	bzero(mtod(m, caddr_t), len);
2776 	lt = mtod(m, struct sadb_lifetime *);
2777 	lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2778 	lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2779 	lt->sadb_lifetime_allocations = 0;
2780 	lt->sadb_lifetime_bytes = 0;
2781 	lt->sadb_lifetime_addtime = sp->created;
2782 	lt->sadb_lifetime_usetime = sp->lastused;
2783 	lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2784 	lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2785 	lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2786 	lt->sadb_lifetime_allocations = 0;
2787 	lt->sadb_lifetime_bytes = 0;
2788 	lt->sadb_lifetime_addtime = sp->lifetime;
2789 	lt->sadb_lifetime_usetime = sp->validtime;
2790 	m_cat(result, m);
2791 
2792 	/* set sadb_address for source */
2793 	m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2794 	    &sp->spidx.src.sa,
2795 	    sp->spidx.prefs, sp->spidx.ul_proto);
2796 	if (!m) {
2797 		error = ENOBUFS;
2798 		goto fail;
2799 	}
2800 	m_cat(result, m);
2801 
2802 	/* set sadb_address for destination */
2803 	m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2804 	    &sp->spidx.dst.sa,
2805 	    sp->spidx.prefd, sp->spidx.ul_proto);
2806 	if (!m) {
2807 		error = ENOBUFS;
2808 		goto fail;
2809 	}
2810 	m_cat(result, m);
2811 
2812 	/* set secpolicy */
2813 	m = key_sp2mbuf(sp);
2814 	if (!m) {
2815 		error = ENOBUFS;
2816 		goto fail;
2817 	}
2818 	m_cat(result, m);
2819 
2820 	if ((result->m_flags & M_PKTHDR) == 0) {
2821 		error = EINVAL;
2822 		goto fail;
2823 	}
2824 
2825 	if (result->m_len < sizeof(struct sadb_msg)) {
2826 		result = m_pullup(result, sizeof(struct sadb_msg));
2827 		if (result == NULL) {
2828 			error = ENOBUFS;
2829 			goto fail;
2830 		}
2831 	}
2832 
2833 	result->m_pkthdr.len = 0;
2834 	for (m = result; m; m = m->m_next)
2835 		result->m_pkthdr.len += m->m_len;
2836 
2837 	mtod(result, struct sadb_msg *)->sadb_msg_len =
2838 	    PFKEY_UNIT64(result->m_pkthdr.len);
2839 
2840 	return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2841 
2842  fail:
2843 	if (result)
2844 		m_freem(result);
2845 	return error;
2846 }
2847 
2848 /* %%% SAD management */
2849 /*
2850  * allocating and initialize new SA head.
2851  * OUT:	NULL	: failure due to the lack of memory.
2852  *	others	: pointer to new SA head.
2853  */
2854 static struct secashead *
2855 key_newsah(struct secasindex *saidx)
2856 {
2857 	struct secashead *sah;
2858 
2859 	sah = malloc(sizeof(struct secashead), M_IPSEC_SAH,
2860 	    M_NOWAIT | M_ZERO);
2861 	if (sah == NULL) {
2862 		PFKEYSTAT_INC(in_nomem);
2863 		return (NULL);
2864 	}
2865 	TAILQ_INIT(&sah->savtree_larval);
2866 	TAILQ_INIT(&sah->savtree_alive);
2867 	sah->saidx = *saidx;
2868 	sah->state = SADB_SASTATE_DEAD;
2869 	SAH_INITREF(sah);
2870 
2871 	KEYDBG(KEY_STAMP,
2872 	    printf("%s: SAH(%p)\n", __func__, sah));
2873 	KEYDBG(KEY_DATA, kdebug_secash(sah, NULL));
2874 	return (sah);
2875 }
2876 
2877 static void
2878 key_freesah(struct secashead **psah)
2879 {
2880 	struct secashead *sah = *psah;
2881 
2882 	if (SAH_DELREF(sah) == 0)
2883 		return;
2884 
2885 	KEYDBG(KEY_STAMP,
2886 	    printf("%s: last reference to SAH(%p)\n", __func__, sah));
2887 	KEYDBG(KEY_DATA, kdebug_secash(sah, NULL));
2888 
2889 	*psah = NULL;
2890 	key_delsah(sah);
2891 }
2892 
2893 static void
2894 key_delsah(struct secashead *sah)
2895 {
2896 	IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2897 	IPSEC_ASSERT(sah->state == SADB_SASTATE_DEAD,
2898 	    ("Attempt to free non DEAD SAH %p", sah));
2899 	IPSEC_ASSERT(TAILQ_EMPTY(&sah->savtree_larval),
2900 	    ("Attempt to free SAH %p with LARVAL SA", sah));
2901 	IPSEC_ASSERT(TAILQ_EMPTY(&sah->savtree_alive),
2902 	    ("Attempt to free SAH %p with ALIVE SA", sah));
2903 
2904 	free(sah, M_IPSEC_SAH);
2905 }
2906 
2907 /*
2908  * allocating a new SA for key_add() and key_getspi() call,
2909  * and copy the values of mhp into new buffer.
2910  * When SAD message type is SADB_GETSPI set SA state to LARVAL.
2911  * For SADB_ADD create and initialize SA with MATURE state.
2912  * OUT:	NULL	: fail
2913  *	others	: pointer to new secasvar.
2914  */
2915 static struct secasvar *
2916 key_newsav(const struct sadb_msghdr *mhp, struct secasindex *saidx,
2917     uint32_t spi, int *errp)
2918 {
2919 	struct secashead *sah;
2920 	struct secasvar *sav;
2921 	int isnew;
2922 
2923 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2924 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2925 	IPSEC_ASSERT(mhp->msg->sadb_msg_type == SADB_GETSPI ||
2926 	    mhp->msg->sadb_msg_type == SADB_ADD, ("wrong message type"));
2927 
2928 	sav = NULL;
2929 	sah = NULL;
2930 	/* check SPI value */
2931 	switch (saidx->proto) {
2932 	case IPPROTO_ESP:
2933 	case IPPROTO_AH:
2934 		/*
2935 		 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
2936 		 * 1-255 reserved by IANA for future use,
2937 		 * 0 for implementation specific, local use.
2938 		 */
2939 		if (ntohl(spi) <= 255) {
2940 			ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
2941 			    __func__, ntohl(spi)));
2942 			*errp = EINVAL;
2943 			goto done;
2944 		}
2945 		break;
2946 	}
2947 
2948 	sav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT | M_ZERO);
2949 	if (sav == NULL) {
2950 		*errp = ENOBUFS;
2951 		goto done;
2952 	}
2953 	sav->lock = malloc(sizeof(struct mtx), M_IPSEC_MISC,
2954 	    M_NOWAIT | M_ZERO);
2955 	if (sav->lock == NULL) {
2956 		*errp = ENOBUFS;
2957 		goto done;
2958 	}
2959 	mtx_init(sav->lock, "ipsec association", NULL, MTX_DEF);
2960 	sav->lft_c = uma_zalloc_pcpu(V_key_lft_zone, M_NOWAIT);
2961 	if (sav->lft_c == NULL) {
2962 		*errp = ENOBUFS;
2963 		goto done;
2964 	}
2965 	counter_u64_zero(sav->lft_c_allocations);
2966 	counter_u64_zero(sav->lft_c_bytes);
2967 
2968 	sav->spi = spi;
2969 	sav->seq = mhp->msg->sadb_msg_seq;
2970 	sav->state = SADB_SASTATE_LARVAL;
2971 	sav->pid = (pid_t)mhp->msg->sadb_msg_pid;
2972 	SAV_INITREF(sav);
2973 again:
2974 	sah = key_getsah(saidx);
2975 	if (sah == NULL) {
2976 		/* create a new SA index */
2977 		sah = key_newsah(saidx);
2978 		if (sah == NULL) {
2979 			ipseclog((LOG_DEBUG,
2980 			    "%s: No more memory.\n", __func__));
2981 			*errp = ENOBUFS;
2982 			goto done;
2983 		}
2984 		isnew = 1;
2985 	} else
2986 		isnew = 0;
2987 
2988 	sav->sah = sah;
2989 	if (mhp->msg->sadb_msg_type == SADB_GETSPI) {
2990 		sav->created = time_second;
2991 	} else if (sav->state == SADB_SASTATE_LARVAL) {
2992 		/*
2993 		 * Do not call key_setsaval() second time in case
2994 		 * of `goto again`. We will have MATURE state.
2995 		 */
2996 		*errp = key_setsaval(sav, mhp);
2997 		if (*errp != 0)
2998 			goto done;
2999 		sav->state = SADB_SASTATE_MATURE;
3000 	}
3001 
3002 	SAHTREE_WLOCK();
3003 	/*
3004 	 * Check that existing SAH wasn't unlinked.
3005 	 * Since we didn't hold the SAHTREE lock, it is possible,
3006 	 * that callout handler or key_flush() or key_delete() could
3007 	 * unlink this SAH.
3008 	 */
3009 	if (isnew == 0 && sah->state == SADB_SASTATE_DEAD) {
3010 		SAHTREE_WUNLOCK();
3011 		key_freesah(&sah);	/* reference from key_getsah() */
3012 		goto again;
3013 	}
3014 	if (isnew != 0) {
3015 		/*
3016 		 * Add new SAH into SADB.
3017 		 *
3018 		 * XXXAE: we can serialize key_add and key_getspi calls, so
3019 		 * several threads will not fight in the race.
3020 		 * Otherwise we should check under SAHTREE lock, that this
3021 		 * SAH would not added twice.
3022 		 */
3023 		TAILQ_INSERT_HEAD(&V_sahtree, sah, chain);
3024 		/* Add new SAH into hash by addresses */
3025 		LIST_INSERT_HEAD(SAHADDRHASH_HASH(saidx), sah, addrhash);
3026 		/* Now we are linked in the chain */
3027 		sah->state = SADB_SASTATE_MATURE;
3028 		/*
3029 		 * SAV references this new SAH.
3030 		 * In case of existing SAH we reuse reference
3031 		 * from key_getsah().
3032 		 */
3033 		SAH_ADDREF(sah);
3034 	}
3035 	/* Link SAV with SAH */
3036 	if (sav->state == SADB_SASTATE_MATURE)
3037 		TAILQ_INSERT_HEAD(&sah->savtree_alive, sav, chain);
3038 	else
3039 		TAILQ_INSERT_HEAD(&sah->savtree_larval, sav, chain);
3040 	/* Add SAV into SPI hash */
3041 	LIST_INSERT_HEAD(SAVHASH_HASH(sav->spi), sav, spihash);
3042 	SAHTREE_WUNLOCK();
3043 	*errp = 0;	/* success */
3044 done:
3045 	if (*errp != 0) {
3046 		if (sav != NULL) {
3047 			if (sav->lock != NULL) {
3048 				mtx_destroy(sav->lock);
3049 				free(sav->lock, M_IPSEC_MISC);
3050 			}
3051 			if (sav->lft_c != NULL)
3052 				uma_zfree_pcpu(V_key_lft_zone, sav->lft_c);
3053 			free(sav, M_IPSEC_SA), sav = NULL;
3054 		}
3055 		if (sah != NULL)
3056 			key_freesah(&sah);
3057 		if (*errp == ENOBUFS) {
3058 			ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3059 			    __func__));
3060 			PFKEYSTAT_INC(in_nomem);
3061 		}
3062 	}
3063 	return (sav);
3064 }
3065 
3066 /*
3067  * free() SA variable entry.
3068  */
3069 static void
3070 key_cleansav(struct secasvar *sav)
3071 {
3072 
3073 	if (sav->natt != NULL) {
3074 		free(sav->natt, M_IPSEC_MISC);
3075 		sav->natt = NULL;
3076 	}
3077 	if (sav->flags & SADB_X_EXT_F_CLONED)
3078 		return;
3079 	/*
3080 	 * Cleanup xform state.  Note that zeroize'ing causes the
3081 	 * keys to be cleared; otherwise we must do it ourself.
3082 	 */
3083 	if (sav->tdb_xform != NULL) {
3084 		sav->tdb_xform->xf_zeroize(sav);
3085 		sav->tdb_xform = NULL;
3086 	} else {
3087 		if (sav->key_auth != NULL)
3088 			bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
3089 		if (sav->key_enc != NULL)
3090 			bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
3091 	}
3092 	if (sav->key_auth != NULL) {
3093 		if (sav->key_auth->key_data != NULL)
3094 			free(sav->key_auth->key_data, M_IPSEC_MISC);
3095 		free(sav->key_auth, M_IPSEC_MISC);
3096 		sav->key_auth = NULL;
3097 	}
3098 	if (sav->key_enc != NULL) {
3099 		if (sav->key_enc->key_data != NULL)
3100 			free(sav->key_enc->key_data, M_IPSEC_MISC);
3101 		free(sav->key_enc, M_IPSEC_MISC);
3102 		sav->key_enc = NULL;
3103 	}
3104 	if (sav->replay != NULL) {
3105 		if (sav->replay->bitmap != NULL)
3106 			free(sav->replay->bitmap, M_IPSEC_MISC);
3107 		free(sav->replay, M_IPSEC_MISC);
3108 		sav->replay = NULL;
3109 	}
3110 	if (sav->lft_h != NULL) {
3111 		free(sav->lft_h, M_IPSEC_MISC);
3112 		sav->lft_h = NULL;
3113 	}
3114 	if (sav->lft_s != NULL) {
3115 		free(sav->lft_s, M_IPSEC_MISC);
3116 		sav->lft_s = NULL;
3117 	}
3118 }
3119 
3120 /*
3121  * free() SA variable entry.
3122  */
3123 static void
3124 key_delsav(struct secasvar *sav)
3125 {
3126 	IPSEC_ASSERT(sav != NULL, ("null sav"));
3127 	IPSEC_ASSERT(sav->state == SADB_SASTATE_DEAD,
3128 	    ("attempt to free non DEAD SA %p", sav));
3129 	IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0",
3130 	    sav->refcnt));
3131 
3132 	/*
3133 	 * SA must be unlinked from the chain and hashtbl.
3134 	 * If SA was cloned, we leave all fields untouched,
3135 	 * except NAT-T config.
3136 	 */
3137 	key_cleansav(sav);
3138 	if ((sav->flags & SADB_X_EXT_F_CLONED) == 0) {
3139 		mtx_destroy(sav->lock);
3140 		free(sav->lock, M_IPSEC_MISC);
3141 		uma_zfree(V_key_lft_zone, sav->lft_c);
3142 	}
3143 	free(sav, M_IPSEC_SA);
3144 }
3145 
3146 /*
3147  * search SAH.
3148  * OUT:
3149  *	NULL	: not found
3150  *	others	: found, referenced pointer to a SAH.
3151  */
3152 static struct secashead *
3153 key_getsah(struct secasindex *saidx)
3154 {
3155 	SAHTREE_RLOCK_TRACKER;
3156 	struct secashead *sah;
3157 
3158 	SAHTREE_RLOCK();
3159 	LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
3160 	    if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID) != 0) {
3161 		    SAH_ADDREF(sah);
3162 		    break;
3163 	    }
3164 	}
3165 	SAHTREE_RUNLOCK();
3166 	return (sah);
3167 }
3168 
3169 /*
3170  * Check not to be duplicated SPI.
3171  * OUT:
3172  *	0	: not found
3173  *	1	: found SA with given SPI.
3174  */
3175 static int
3176 key_checkspidup(uint32_t spi)
3177 {
3178 	SAHTREE_RLOCK_TRACKER;
3179 	struct secasvar *sav;
3180 
3181 	/* Assume SPI is in network byte order */
3182 	SAHTREE_RLOCK();
3183 	LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
3184 		if (sav->spi == spi)
3185 			break;
3186 	}
3187 	SAHTREE_RUNLOCK();
3188 	return (sav != NULL);
3189 }
3190 
3191 /*
3192  * Search SA by SPI.
3193  * OUT:
3194  *	NULL	: not found
3195  *	others	: found, referenced pointer to a SA.
3196  */
3197 static struct secasvar *
3198 key_getsavbyspi(uint32_t spi)
3199 {
3200 	SAHTREE_RLOCK_TRACKER;
3201 	struct secasvar *sav;
3202 
3203 	/* Assume SPI is in network byte order */
3204 	SAHTREE_RLOCK();
3205 	LIST_FOREACH(sav, SAVHASH_HASH(spi), spihash) {
3206 		if (sav->spi != spi)
3207 			continue;
3208 		SAV_ADDREF(sav);
3209 		break;
3210 	}
3211 	SAHTREE_RUNLOCK();
3212 	return (sav);
3213 }
3214 
3215 static int
3216 key_updatelifetimes(struct secasvar *sav, const struct sadb_msghdr *mhp)
3217 {
3218 	struct seclifetime *lft_h, *lft_s, *tmp;
3219 
3220 	/* Lifetime extension is optional, check that it is present. */
3221 	if (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
3222 	    SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) {
3223 		/*
3224 		 * In case of SADB_UPDATE we may need to change
3225 		 * existing lifetimes.
3226 		 */
3227 		if (sav->state == SADB_SASTATE_MATURE) {
3228 			lft_h = lft_s = NULL;
3229 			goto reset;
3230 		}
3231 		return (0);
3232 	}
3233 	/* Both HARD and SOFT extensions must present */
3234 	if ((SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
3235 	    !SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
3236 	    (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
3237 	    !SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
3238 		ipseclog((LOG_DEBUG,
3239 		    "%s: invalid message: missing required header.\n",
3240 		    __func__));
3241 		return (EINVAL);
3242 	}
3243 	if (SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_HARD) ||
3244 	    SADB_CHECKLEN(mhp, SADB_EXT_LIFETIME_SOFT)) {
3245 		ipseclog((LOG_DEBUG,
3246 		    "%s: invalid message: wrong header size.\n", __func__));
3247 		return (EINVAL);
3248 	}
3249 	lft_h = key_dup_lifemsg((const struct sadb_lifetime *)
3250 	    mhp->ext[SADB_EXT_LIFETIME_HARD], M_IPSEC_MISC);
3251 	if (lft_h == NULL) {
3252 		PFKEYSTAT_INC(in_nomem);
3253 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3254 		return (ENOBUFS);
3255 	}
3256 	lft_s = key_dup_lifemsg((const struct sadb_lifetime *)
3257 	    mhp->ext[SADB_EXT_LIFETIME_SOFT], M_IPSEC_MISC);
3258 	if (lft_s == NULL) {
3259 		PFKEYSTAT_INC(in_nomem);
3260 		free(lft_h, M_IPSEC_MISC);
3261 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3262 		return (ENOBUFS);
3263 	}
3264 reset:
3265 	if (sav->state != SADB_SASTATE_LARVAL) {
3266 		/*
3267 		 * key_update() holds reference to this SA,
3268 		 * so it won't be deleted in meanwhile.
3269 		 */
3270 		SECASVAR_LOCK(sav);
3271 		tmp = sav->lft_h;
3272 		sav->lft_h = lft_h;
3273 		lft_h = tmp;
3274 
3275 		tmp = sav->lft_s;
3276 		sav->lft_s = lft_s;
3277 		lft_s = tmp;
3278 		SECASVAR_UNLOCK(sav);
3279 		if (lft_h != NULL)
3280 			free(lft_h, M_IPSEC_MISC);
3281 		if (lft_s != NULL)
3282 			free(lft_s, M_IPSEC_MISC);
3283 		return (0);
3284 	}
3285 	/* We can update lifetime without holding a lock */
3286 	IPSEC_ASSERT(sav->lft_h == NULL, ("lft_h is already initialized\n"));
3287 	IPSEC_ASSERT(sav->lft_s == NULL, ("lft_s is already initialized\n"));
3288 	sav->lft_h = lft_h;
3289 	sav->lft_s = lft_s;
3290 	return (0);
3291 }
3292 
3293 /*
3294  * copy SA values from PF_KEY message except *SPI, SEQ, PID and TYPE*.
3295  * You must update these if need. Expects only LARVAL SAs.
3296  * OUT:	0:	success.
3297  *	!0:	failure.
3298  */
3299 static int
3300 key_setsaval(struct secasvar *sav, const struct sadb_msghdr *mhp)
3301 {
3302 	const struct sadb_sa *sa0;
3303 	const struct sadb_key *key0;
3304 	uint32_t replay;
3305 	size_t len;
3306 	int error;
3307 
3308 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3309 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3310 	IPSEC_ASSERT(sav->state == SADB_SASTATE_LARVAL,
3311 	    ("Attempt to update non LARVAL SA"));
3312 
3313 	/* XXX rewrite */
3314 	error = key_setident(sav->sah, mhp);
3315 	if (error != 0)
3316 		goto fail;
3317 
3318 	/* SA */
3319 	if (!SADB_CHECKHDR(mhp, SADB_EXT_SA)) {
3320 		if (SADB_CHECKLEN(mhp, SADB_EXT_SA)) {
3321 			error = EINVAL;
3322 			goto fail;
3323 		}
3324 		sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3325 		sav->alg_auth = sa0->sadb_sa_auth;
3326 		sav->alg_enc = sa0->sadb_sa_encrypt;
3327 		sav->flags = sa0->sadb_sa_flags;
3328 		if ((sav->flags & SADB_KEY_FLAGS_MAX) != sav->flags) {
3329 			ipseclog((LOG_DEBUG,
3330 			    "%s: invalid sa_flags 0x%08x.\n", __func__,
3331 			    sav->flags));
3332 			error = EINVAL;
3333 			goto fail;
3334 		}
3335 
3336 		/* Optional replay window */
3337 		replay = 0;
3338 		if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0)
3339 			replay = sa0->sadb_sa_replay;
3340 		if (!SADB_CHECKHDR(mhp, SADB_X_EXT_SA_REPLAY)) {
3341 			if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA_REPLAY)) {
3342 				error = EINVAL;
3343 				goto fail;
3344 			}
3345 			replay = ((const struct sadb_x_sa_replay *)
3346 			    mhp->ext[SADB_X_EXT_SA_REPLAY])->sadb_x_sa_replay_replay;
3347 
3348 			if (replay > UINT32_MAX - 32) {
3349 				ipseclog((LOG_DEBUG,
3350 				    "%s: replay window too big.\n", __func__));
3351 				error = EINVAL;
3352 				goto fail;
3353 			}
3354 
3355 			replay = (replay + 7) >> 3;
3356 		}
3357 
3358 		sav->replay = malloc(sizeof(struct secreplay), M_IPSEC_MISC,
3359 		    M_NOWAIT | M_ZERO);
3360 		if (sav->replay == NULL) {
3361 			PFKEYSTAT_INC(in_nomem);
3362 			ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3363 			    __func__));
3364 			error = ENOBUFS;
3365 			goto fail;
3366 		}
3367 
3368 		if (replay != 0) {
3369 			/* number of 32b blocks to be allocated */
3370 			uint32_t bitmap_size;
3371 
3372 			/* RFC 6479:
3373 			 * - the allocated replay window size must be
3374 			 *   a power of two.
3375 			 * - use an extra 32b block as a redundant window.
3376 			 */
3377 			bitmap_size = 1;
3378 			while (replay + 4 > bitmap_size)
3379 				bitmap_size <<= 1;
3380 			bitmap_size = bitmap_size / 4;
3381 
3382 			sav->replay->bitmap = malloc(
3383 			    bitmap_size * sizeof(uint32_t), M_IPSEC_MISC,
3384 			    M_NOWAIT | M_ZERO);
3385 			if (sav->replay->bitmap == NULL) {
3386 				PFKEYSTAT_INC(in_nomem);
3387 				ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3388 					__func__));
3389 				error = ENOBUFS;
3390 				goto fail;
3391 			}
3392 			sav->replay->bitmap_size = bitmap_size;
3393 			sav->replay->wsize = replay;
3394 		}
3395 	}
3396 
3397 	/* Authentication keys */
3398 	if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH)) {
3399 		if (SADB_CHECKLEN(mhp, SADB_EXT_KEY_AUTH)) {
3400 			error = EINVAL;
3401 			goto fail;
3402 		}
3403 		error = 0;
3404 		key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3405 		len = mhp->extlen[SADB_EXT_KEY_AUTH];
3406 		switch (mhp->msg->sadb_msg_satype) {
3407 		case SADB_SATYPE_AH:
3408 		case SADB_SATYPE_ESP:
3409 		case SADB_X_SATYPE_TCPSIGNATURE:
3410 			if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3411 			    sav->alg_auth != SADB_X_AALG_NULL)
3412 				error = EINVAL;
3413 			break;
3414 		case SADB_X_SATYPE_IPCOMP:
3415 		default:
3416 			error = EINVAL;
3417 			break;
3418 		}
3419 		if (error) {
3420 			ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3421 				__func__));
3422 			goto fail;
3423 		}
3424 
3425 		sav->key_auth = key_dup_keymsg(key0, len, M_IPSEC_MISC);
3426 		if (sav->key_auth == NULL ) {
3427 			ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3428 				  __func__));
3429 			PFKEYSTAT_INC(in_nomem);
3430 			error = ENOBUFS;
3431 			goto fail;
3432 		}
3433 	}
3434 
3435 	/* Encryption key */
3436 	if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT)) {
3437 		if (SADB_CHECKLEN(mhp, SADB_EXT_KEY_ENCRYPT)) {
3438 			error = EINVAL;
3439 			goto fail;
3440 		}
3441 		error = 0;
3442 		key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3443 		len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3444 		switch (mhp->msg->sadb_msg_satype) {
3445 		case SADB_SATYPE_ESP:
3446 			if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3447 			    sav->alg_enc != SADB_EALG_NULL) {
3448 				error = EINVAL;
3449 				break;
3450 			}
3451 			sav->key_enc = key_dup_keymsg(key0, len, M_IPSEC_MISC);
3452 			if (sav->key_enc == NULL) {
3453 				ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3454 					__func__));
3455 				PFKEYSTAT_INC(in_nomem);
3456 				error = ENOBUFS;
3457 				goto fail;
3458 			}
3459 			break;
3460 		case SADB_X_SATYPE_IPCOMP:
3461 			if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3462 				error = EINVAL;
3463 			sav->key_enc = NULL;	/*just in case*/
3464 			break;
3465 		case SADB_SATYPE_AH:
3466 		case SADB_X_SATYPE_TCPSIGNATURE:
3467 		default:
3468 			error = EINVAL;
3469 			break;
3470 		}
3471 		if (error) {
3472 			ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3473 				__func__));
3474 			goto fail;
3475 		}
3476 	}
3477 
3478 	/* set iv */
3479 	sav->ivlen = 0;
3480 	switch (mhp->msg->sadb_msg_satype) {
3481 	case SADB_SATYPE_AH:
3482 		if (sav->flags & SADB_X_EXT_DERIV) {
3483 			ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3484 			    "given to AH SA.\n", __func__));
3485 			error = EINVAL;
3486 			goto fail;
3487 		}
3488 		if (sav->alg_enc != SADB_EALG_NONE) {
3489 			ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3490 			    "mismated.\n", __func__));
3491 			error = EINVAL;
3492 			goto fail;
3493 		}
3494 		error = xform_init(sav, XF_AH);
3495 		break;
3496 	case SADB_SATYPE_ESP:
3497 		if ((sav->flags & (SADB_X_EXT_OLD | SADB_X_EXT_DERIV)) ==
3498 		    (SADB_X_EXT_OLD | SADB_X_EXT_DERIV)) {
3499 			ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3500 			    "given to old-esp.\n", __func__));
3501 			error = EINVAL;
3502 			goto fail;
3503 		}
3504 		error = xform_init(sav, XF_ESP);
3505 		break;
3506 	case SADB_X_SATYPE_IPCOMP:
3507 		if (sav->alg_auth != SADB_AALG_NONE) {
3508 			ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3509 			    "mismated.\n", __func__));
3510 			error = EINVAL;
3511 			goto fail;
3512 		}
3513 		if ((sav->flags & SADB_X_EXT_RAWCPI) == 0 &&
3514 		    ntohl(sav->spi) >= 0x10000) {
3515 			ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3516 			    __func__));
3517 			error = EINVAL;
3518 			goto fail;
3519 		}
3520 		error = xform_init(sav, XF_IPCOMP);
3521 		break;
3522 	case SADB_X_SATYPE_TCPSIGNATURE:
3523 		if (sav->alg_enc != SADB_EALG_NONE) {
3524 			ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3525 			    "mismated.\n", __func__));
3526 			error = EINVAL;
3527 			goto fail;
3528 		}
3529 		error = xform_init(sav, XF_TCPSIGNATURE);
3530 		break;
3531 	default:
3532 		ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3533 		error = EPROTONOSUPPORT;
3534 		goto fail;
3535 	}
3536 	if (error) {
3537 		ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3538 		    __func__, mhp->msg->sadb_msg_satype));
3539 		goto fail;
3540 	}
3541 
3542 	/* Handle NAT-T headers */
3543 	error = key_setnatt(sav, mhp);
3544 	if (error != 0)
3545 		goto fail;
3546 
3547 	/* Initialize lifetime for CURRENT */
3548 	sav->firstused = 0;
3549 	sav->created = time_second;
3550 
3551 	/* lifetimes for HARD and SOFT */
3552 	error = key_updatelifetimes(sav, mhp);
3553 	if (error == 0)
3554 		return (0);
3555 fail:
3556 	key_cleansav(sav);
3557 	return (error);
3558 }
3559 
3560 /*
3561  * subroutine for SADB_GET and SADB_DUMP.
3562  */
3563 static struct mbuf *
3564 key_setdumpsa(struct secasvar *sav, uint8_t type, uint8_t satype,
3565     uint32_t seq, uint32_t pid)
3566 {
3567 	struct seclifetime lft_c;
3568 	struct mbuf *result = NULL, *tres = NULL, *m;
3569 	int i, dumporder[] = {
3570 		SADB_EXT_SA, SADB_X_EXT_SA2, SADB_X_EXT_SA_REPLAY,
3571 		SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3572 		SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3573 		SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY,
3574 		SADB_EXT_KEY_AUTH, SADB_EXT_KEY_ENCRYPT,
3575 		SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST,
3576 		SADB_EXT_SENSITIVITY,
3577 		SADB_X_EXT_NAT_T_TYPE,
3578 		SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3579 		SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3580 		SADB_X_EXT_NAT_T_FRAG,
3581 	};
3582 	uint32_t replay_count;
3583 
3584 	m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3585 	if (m == NULL)
3586 		goto fail;
3587 	result = m;
3588 
3589 	for (i = nitems(dumporder) - 1; i >= 0; i--) {
3590 		m = NULL;
3591 		switch (dumporder[i]) {
3592 		case SADB_EXT_SA:
3593 			m = key_setsadbsa(sav);
3594 			if (!m)
3595 				goto fail;
3596 			break;
3597 
3598 		case SADB_X_EXT_SA2:
3599 			SECASVAR_LOCK(sav);
3600 			replay_count = sav->replay ? sav->replay->count : 0;
3601 			SECASVAR_UNLOCK(sav);
3602 			m = key_setsadbxsa2(sav->sah->saidx.mode, replay_count,
3603 					sav->sah->saidx.reqid);
3604 			if (!m)
3605 				goto fail;
3606 			break;
3607 
3608 		case SADB_X_EXT_SA_REPLAY:
3609 			if (sav->replay == NULL ||
3610 			    sav->replay->wsize <= UINT8_MAX)
3611 				continue;
3612 
3613 			m = key_setsadbxsareplay(sav->replay->wsize);
3614 			if (!m)
3615 				goto fail;
3616 			break;
3617 
3618 		case SADB_EXT_ADDRESS_SRC:
3619 			m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3620 			    &sav->sah->saidx.src.sa,
3621 			    FULLMASK, IPSEC_ULPROTO_ANY);
3622 			if (!m)
3623 				goto fail;
3624 			break;
3625 
3626 		case SADB_EXT_ADDRESS_DST:
3627 			m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3628 			    &sav->sah->saidx.dst.sa,
3629 			    FULLMASK, IPSEC_ULPROTO_ANY);
3630 			if (!m)
3631 				goto fail;
3632 			break;
3633 
3634 		case SADB_EXT_KEY_AUTH:
3635 			if (!sav->key_auth)
3636 				continue;
3637 			m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3638 			if (!m)
3639 				goto fail;
3640 			break;
3641 
3642 		case SADB_EXT_KEY_ENCRYPT:
3643 			if (!sav->key_enc)
3644 				continue;
3645 			m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3646 			if (!m)
3647 				goto fail;
3648 			break;
3649 
3650 		case SADB_EXT_LIFETIME_CURRENT:
3651 			lft_c.addtime = sav->created;
3652 			lft_c.allocations = (uint32_t)counter_u64_fetch(
3653 			    sav->lft_c_allocations);
3654 			lft_c.bytes = counter_u64_fetch(sav->lft_c_bytes);
3655 			lft_c.usetime = sav->firstused;
3656 			m = key_setlifetime(&lft_c, SADB_EXT_LIFETIME_CURRENT);
3657 			if (!m)
3658 				goto fail;
3659 			break;
3660 
3661 		case SADB_EXT_LIFETIME_HARD:
3662 			if (!sav->lft_h)
3663 				continue;
3664 			m = key_setlifetime(sav->lft_h,
3665 					    SADB_EXT_LIFETIME_HARD);
3666 			if (!m)
3667 				goto fail;
3668 			break;
3669 
3670 		case SADB_EXT_LIFETIME_SOFT:
3671 			if (!sav->lft_s)
3672 				continue;
3673 			m = key_setlifetime(sav->lft_s,
3674 					    SADB_EXT_LIFETIME_SOFT);
3675 
3676 			if (!m)
3677 				goto fail;
3678 			break;
3679 
3680 		case SADB_X_EXT_NAT_T_TYPE:
3681 			if (sav->natt == NULL)
3682 				continue;
3683 			m = key_setsadbxtype(UDP_ENCAP_ESPINUDP);
3684 			if (!m)
3685 				goto fail;
3686 			break;
3687 
3688 		case SADB_X_EXT_NAT_T_DPORT:
3689 			if (sav->natt == NULL)
3690 				continue;
3691 			m = key_setsadbxport(sav->natt->dport,
3692 			    SADB_X_EXT_NAT_T_DPORT);
3693 			if (!m)
3694 				goto fail;
3695 			break;
3696 
3697 		case SADB_X_EXT_NAT_T_SPORT:
3698 			if (sav->natt == NULL)
3699 				continue;
3700 			m = key_setsadbxport(sav->natt->sport,
3701 			    SADB_X_EXT_NAT_T_SPORT);
3702 			if (!m)
3703 				goto fail;
3704 			break;
3705 
3706 		case SADB_X_EXT_NAT_T_OAI:
3707 			if (sav->natt == NULL ||
3708 			    (sav->natt->flags & IPSEC_NATT_F_OAI) == 0)
3709 				continue;
3710 			m = key_setsadbaddr(SADB_X_EXT_NAT_T_OAI,
3711 			    &sav->natt->oai.sa, FULLMASK, IPSEC_ULPROTO_ANY);
3712 			if (!m)
3713 				goto fail;
3714 			break;
3715 		case SADB_X_EXT_NAT_T_OAR:
3716 			if (sav->natt == NULL ||
3717 			    (sav->natt->flags & IPSEC_NATT_F_OAR) == 0)
3718 				continue;
3719 			m = key_setsadbaddr(SADB_X_EXT_NAT_T_OAR,
3720 			    &sav->natt->oar.sa, FULLMASK, IPSEC_ULPROTO_ANY);
3721 			if (!m)
3722 				goto fail;
3723 			break;
3724 		case SADB_X_EXT_NAT_T_FRAG:
3725 			/* We do not (yet) support those. */
3726 			continue;
3727 
3728 		case SADB_EXT_ADDRESS_PROXY:
3729 		case SADB_EXT_IDENTITY_SRC:
3730 		case SADB_EXT_IDENTITY_DST:
3731 			/* XXX: should we brought from SPD ? */
3732 		case SADB_EXT_SENSITIVITY:
3733 		default:
3734 			continue;
3735 		}
3736 
3737 		if (!m)
3738 			goto fail;
3739 		if (tres)
3740 			m_cat(m, tres);
3741 		tres = m;
3742 	}
3743 
3744 	m_cat(result, tres);
3745 	tres = NULL;
3746 	if (result->m_len < sizeof(struct sadb_msg)) {
3747 		result = m_pullup(result, sizeof(struct sadb_msg));
3748 		if (result == NULL)
3749 			goto fail;
3750 	}
3751 
3752 	result->m_pkthdr.len = 0;
3753 	for (m = result; m; m = m->m_next)
3754 		result->m_pkthdr.len += m->m_len;
3755 
3756 	mtod(result, struct sadb_msg *)->sadb_msg_len =
3757 	    PFKEY_UNIT64(result->m_pkthdr.len);
3758 
3759 	return result;
3760 
3761 fail:
3762 	m_freem(result);
3763 	m_freem(tres);
3764 	return NULL;
3765 }
3766 
3767 /*
3768  * set data into sadb_msg.
3769  */
3770 static struct mbuf *
3771 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3772     pid_t pid, u_int16_t reserved)
3773 {
3774 	struct mbuf *m;
3775 	struct sadb_msg *p;
3776 	int len;
3777 
3778 	len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3779 	if (len > MCLBYTES)
3780 		return NULL;
3781 	MGETHDR(m, M_NOWAIT, MT_DATA);
3782 	if (m && len > MHLEN) {
3783 		if (!(MCLGET(m, M_NOWAIT))) {
3784 			m_freem(m);
3785 			m = NULL;
3786 		}
3787 	}
3788 	if (!m)
3789 		return NULL;
3790 	m->m_pkthdr.len = m->m_len = len;
3791 	m->m_next = NULL;
3792 
3793 	p = mtod(m, struct sadb_msg *);
3794 
3795 	bzero(p, len);
3796 	p->sadb_msg_version = PF_KEY_V2;
3797 	p->sadb_msg_type = type;
3798 	p->sadb_msg_errno = 0;
3799 	p->sadb_msg_satype = satype;
3800 	p->sadb_msg_len = PFKEY_UNIT64(tlen);
3801 	p->sadb_msg_reserved = reserved;
3802 	p->sadb_msg_seq = seq;
3803 	p->sadb_msg_pid = (u_int32_t)pid;
3804 
3805 	return m;
3806 }
3807 
3808 /*
3809  * copy secasvar data into sadb_address.
3810  */
3811 static struct mbuf *
3812 key_setsadbsa(struct secasvar *sav)
3813 {
3814 	struct mbuf *m;
3815 	struct sadb_sa *p;
3816 	int len;
3817 
3818 	len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3819 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3820 	if (m == NULL)
3821 		return (NULL);
3822 	m_align(m, len);
3823 	m->m_len = len;
3824 	p = mtod(m, struct sadb_sa *);
3825 	bzero(p, len);
3826 	p->sadb_sa_len = PFKEY_UNIT64(len);
3827 	p->sadb_sa_exttype = SADB_EXT_SA;
3828 	p->sadb_sa_spi = sav->spi;
3829 	p->sadb_sa_replay = sav->replay ?
3830 	    (sav->replay->wsize > UINT8_MAX ? UINT8_MAX :
3831 		sav->replay->wsize): 0;
3832 	p->sadb_sa_state = sav->state;
3833 	p->sadb_sa_auth = sav->alg_auth;
3834 	p->sadb_sa_encrypt = sav->alg_enc;
3835 	p->sadb_sa_flags = sav->flags & SADB_KEY_FLAGS_MAX;
3836 	return (m);
3837 }
3838 
3839 /*
3840  * set data into sadb_address.
3841  */
3842 static struct mbuf *
3843 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr,
3844     u_int8_t prefixlen, u_int16_t ul_proto)
3845 {
3846 	struct mbuf *m;
3847 	struct sadb_address *p;
3848 	size_t len;
3849 
3850 	len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3851 	    PFKEY_ALIGN8(saddr->sa_len);
3852 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3853 	if (m == NULL)
3854 		return (NULL);
3855 	m_align(m, len);
3856 	m->m_len = len;
3857 	p = mtod(m, struct sadb_address *);
3858 
3859 	bzero(p, len);
3860 	p->sadb_address_len = PFKEY_UNIT64(len);
3861 	p->sadb_address_exttype = exttype;
3862 	p->sadb_address_proto = ul_proto;
3863 	if (prefixlen == FULLMASK) {
3864 		switch (saddr->sa_family) {
3865 		case AF_INET:
3866 			prefixlen = sizeof(struct in_addr) << 3;
3867 			break;
3868 		case AF_INET6:
3869 			prefixlen = sizeof(struct in6_addr) << 3;
3870 			break;
3871 		default:
3872 			; /*XXX*/
3873 		}
3874 	}
3875 	p->sadb_address_prefixlen = prefixlen;
3876 	p->sadb_address_reserved = 0;
3877 
3878 	bcopy(saddr,
3879 	    mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3880 	    saddr->sa_len);
3881 
3882 	return m;
3883 }
3884 
3885 /*
3886  * set data into sadb_x_sa2.
3887  */
3888 static struct mbuf *
3889 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3890 {
3891 	struct mbuf *m;
3892 	struct sadb_x_sa2 *p;
3893 	size_t len;
3894 
3895 	len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3896 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3897 	if (m == NULL)
3898 		return (NULL);
3899 	m_align(m, len);
3900 	m->m_len = len;
3901 	p = mtod(m, struct sadb_x_sa2 *);
3902 
3903 	bzero(p, len);
3904 	p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3905 	p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3906 	p->sadb_x_sa2_mode = mode;
3907 	p->sadb_x_sa2_reserved1 = 0;
3908 	p->sadb_x_sa2_reserved2 = 0;
3909 	p->sadb_x_sa2_sequence = seq;
3910 	p->sadb_x_sa2_reqid = reqid;
3911 
3912 	return m;
3913 }
3914 
3915 /*
3916  * Set data into sadb_x_sa_replay.
3917  */
3918 static struct mbuf *
3919 key_setsadbxsareplay(u_int32_t replay)
3920 {
3921 	struct mbuf *m;
3922 	struct sadb_x_sa_replay *p;
3923 	size_t len;
3924 
3925 	len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa_replay));
3926 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3927 	if (m == NULL)
3928 		return (NULL);
3929 	m_align(m, len);
3930 	m->m_len = len;
3931 	p = mtod(m, struct sadb_x_sa_replay *);
3932 
3933 	bzero(p, len);
3934 	p->sadb_x_sa_replay_len = PFKEY_UNIT64(len);
3935 	p->sadb_x_sa_replay_exttype = SADB_X_EXT_SA_REPLAY;
3936 	p->sadb_x_sa_replay_replay = (replay << 3);
3937 
3938 	return m;
3939 }
3940 
3941 /*
3942  * Set a type in sadb_x_nat_t_type.
3943  */
3944 static struct mbuf *
3945 key_setsadbxtype(u_int16_t type)
3946 {
3947 	struct mbuf *m;
3948 	size_t len;
3949 	struct sadb_x_nat_t_type *p;
3950 
3951 	len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3952 
3953 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3954 	if (m == NULL)
3955 		return (NULL);
3956 	m_align(m, len);
3957 	m->m_len = len;
3958 	p = mtod(m, struct sadb_x_nat_t_type *);
3959 
3960 	bzero(p, len);
3961 	p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3962 	p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3963 	p->sadb_x_nat_t_type_type = type;
3964 
3965 	return (m);
3966 }
3967 /*
3968  * Set a port in sadb_x_nat_t_port.
3969  * In contrast to default RFC 2367 behaviour, port is in network byte order.
3970  */
3971 static struct mbuf *
3972 key_setsadbxport(u_int16_t port, u_int16_t type)
3973 {
3974 	struct mbuf *m;
3975 	size_t len;
3976 	struct sadb_x_nat_t_port *p;
3977 
3978 	len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3979 
3980 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3981 	if (m == NULL)
3982 		return (NULL);
3983 	m_align(m, len);
3984 	m->m_len = len;
3985 	p = mtod(m, struct sadb_x_nat_t_port *);
3986 
3987 	bzero(p, len);
3988 	p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3989 	p->sadb_x_nat_t_port_exttype = type;
3990 	p->sadb_x_nat_t_port_port = port;
3991 
3992 	return (m);
3993 }
3994 
3995 /*
3996  * Get port from sockaddr. Port is in network byte order.
3997  */
3998 uint16_t
3999 key_portfromsaddr(struct sockaddr *sa)
4000 {
4001 
4002 	switch (sa->sa_family) {
4003 #ifdef INET
4004 	case AF_INET:
4005 		return ((struct sockaddr_in *)sa)->sin_port;
4006 #endif
4007 #ifdef INET6
4008 	case AF_INET6:
4009 		return ((struct sockaddr_in6 *)sa)->sin6_port;
4010 #endif
4011 	}
4012 	return (0);
4013 }
4014 
4015 /*
4016  * Set port in struct sockaddr. Port is in network byte order.
4017  */
4018 void
4019 key_porttosaddr(struct sockaddr *sa, uint16_t port)
4020 {
4021 
4022 	switch (sa->sa_family) {
4023 #ifdef INET
4024 	case AF_INET:
4025 		((struct sockaddr_in *)sa)->sin_port = port;
4026 		break;
4027 #endif
4028 #ifdef INET6
4029 	case AF_INET6:
4030 		((struct sockaddr_in6 *)sa)->sin6_port = port;
4031 		break;
4032 #endif
4033 	default:
4034 		ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
4035 			__func__, sa->sa_family));
4036 		break;
4037 	}
4038 }
4039 
4040 /*
4041  * set data into sadb_x_policy
4042  */
4043 static struct mbuf *
4044 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id, u_int32_t priority)
4045 {
4046 	struct mbuf *m;
4047 	struct sadb_x_policy *p;
4048 	size_t len;
4049 
4050 	len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
4051 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
4052 	if (m == NULL)
4053 		return (NULL);
4054 	m_align(m, len);
4055 	m->m_len = len;
4056 	p = mtod(m, struct sadb_x_policy *);
4057 
4058 	bzero(p, len);
4059 	p->sadb_x_policy_len = PFKEY_UNIT64(len);
4060 	p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
4061 	p->sadb_x_policy_type = type;
4062 	p->sadb_x_policy_dir = dir;
4063 	p->sadb_x_policy_id = id;
4064 	p->sadb_x_policy_priority = priority;
4065 
4066 	return m;
4067 }
4068 
4069 /* %%% utilities */
4070 /* Take a key message (sadb_key) from the socket and turn it into one
4071  * of the kernel's key structures (seckey).
4072  *
4073  * IN: pointer to the src
4074  * OUT: NULL no more memory
4075  */
4076 struct seckey *
4077 key_dup_keymsg(const struct sadb_key *src, size_t len,
4078     struct malloc_type *type)
4079 {
4080 	struct seckey *dst;
4081 
4082 	dst = malloc(sizeof(*dst), type, M_NOWAIT);
4083 	if (dst != NULL) {
4084 		dst->bits = src->sadb_key_bits;
4085 		dst->key_data = malloc(len, type, M_NOWAIT);
4086 		if (dst->key_data != NULL) {
4087 			bcopy((const char *)(src + 1), dst->key_data, len);
4088 		} else {
4089 			ipseclog((LOG_DEBUG, "%s: No more memory.\n",
4090 			    __func__));
4091 			free(dst, type);
4092 			dst = NULL;
4093 		}
4094 	} else {
4095 		ipseclog((LOG_DEBUG, "%s: No more memory.\n",
4096 		    __func__));
4097 
4098 	}
4099 	return (dst);
4100 }
4101 
4102 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
4103  * turn it into one of the kernel's lifetime structures (seclifetime).
4104  *
4105  * IN: pointer to the destination, source and malloc type
4106  * OUT: NULL, no more memory
4107  */
4108 
4109 static struct seclifetime *
4110 key_dup_lifemsg(const struct sadb_lifetime *src, struct malloc_type *type)
4111 {
4112 	struct seclifetime *dst;
4113 
4114 	dst = malloc(sizeof(*dst), type, M_NOWAIT);
4115 	if (dst == NULL) {
4116 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4117 		return (NULL);
4118 	}
4119 	dst->allocations = src->sadb_lifetime_allocations;
4120 	dst->bytes = src->sadb_lifetime_bytes;
4121 	dst->addtime = src->sadb_lifetime_addtime;
4122 	dst->usetime = src->sadb_lifetime_usetime;
4123 	return (dst);
4124 }
4125 
4126 /*
4127  * compare two secasindex structure.
4128  * flag can specify to compare 2 saidxes.
4129  * compare two secasindex structure without both mode and reqid.
4130  * don't compare port.
4131  * IN:
4132  *      saidx0: source, it can be in SAD.
4133  *      saidx1: object.
4134  * OUT:
4135  *      1 : equal
4136  *      0 : not equal
4137  */
4138 static int
4139 key_cmpsaidx(const struct secasindex *saidx0, const struct secasindex *saidx1,
4140     int flag)
4141 {
4142 
4143 	/* sanity */
4144 	if (saidx0 == NULL && saidx1 == NULL)
4145 		return 1;
4146 
4147 	if (saidx0 == NULL || saidx1 == NULL)
4148 		return 0;
4149 
4150 	if (saidx0->proto != saidx1->proto)
4151 		return 0;
4152 
4153 	if (flag == CMP_EXACTLY) {
4154 		if (saidx0->mode != saidx1->mode)
4155 			return 0;
4156 		if (saidx0->reqid != saidx1->reqid)
4157 			return 0;
4158 		if (bcmp(&saidx0->src, &saidx1->src,
4159 		    saidx0->src.sa.sa_len) != 0 ||
4160 		    bcmp(&saidx0->dst, &saidx1->dst,
4161 		    saidx0->dst.sa.sa_len) != 0)
4162 			return 0;
4163 	} else {
4164 
4165 		/* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4166 		if (flag == CMP_MODE_REQID || flag == CMP_REQID) {
4167 			/*
4168 			 * If reqid of SPD is non-zero, unique SA is required.
4169 			 * The result must be of same reqid in this case.
4170 			 */
4171 			if (saidx1->reqid != 0 &&
4172 			    saidx0->reqid != saidx1->reqid)
4173 				return 0;
4174 		}
4175 
4176 		if (flag == CMP_MODE_REQID) {
4177 			if (saidx0->mode != IPSEC_MODE_ANY
4178 			 && saidx0->mode != saidx1->mode)
4179 				return 0;
4180 		}
4181 
4182 		if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0)
4183 			return 0;
4184 		if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0)
4185 			return 0;
4186 	}
4187 
4188 	return 1;
4189 }
4190 
4191 /*
4192  * compare two secindex structure exactly.
4193  * IN:
4194  *	spidx0: source, it is often in SPD.
4195  *	spidx1: object, it is often from PFKEY message.
4196  * OUT:
4197  *	1 : equal
4198  *	0 : not equal
4199  */
4200 static int
4201 key_cmpspidx_exactly(struct secpolicyindex *spidx0,
4202     struct secpolicyindex *spidx1)
4203 {
4204 	/* sanity */
4205 	if (spidx0 == NULL && spidx1 == NULL)
4206 		return 1;
4207 
4208 	if (spidx0 == NULL || spidx1 == NULL)
4209 		return 0;
4210 
4211 	if (spidx0->prefs != spidx1->prefs
4212 	 || spidx0->prefd != spidx1->prefd
4213 	 || spidx0->ul_proto != spidx1->ul_proto
4214 	 || spidx0->dir != spidx1->dir)
4215 		return 0;
4216 
4217 	return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4218 	       key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4219 }
4220 
4221 /*
4222  * compare two secindex structure with mask.
4223  * IN:
4224  *	spidx0: source, it is often in SPD.
4225  *	spidx1: object, it is often from IP header.
4226  * OUT:
4227  *	1 : equal
4228  *	0 : not equal
4229  */
4230 static int
4231 key_cmpspidx_withmask(struct secpolicyindex *spidx0,
4232     struct secpolicyindex *spidx1)
4233 {
4234 	/* sanity */
4235 	if (spidx0 == NULL && spidx1 == NULL)
4236 		return 1;
4237 
4238 	if (spidx0 == NULL || spidx1 == NULL)
4239 		return 0;
4240 
4241 	if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4242 	    spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4243 	    spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4244 	    spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4245 		return 0;
4246 
4247 	/* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4248 	if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4249 	 && spidx0->ul_proto != spidx1->ul_proto)
4250 		return 0;
4251 
4252 	switch (spidx0->src.sa.sa_family) {
4253 	case AF_INET:
4254 		if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4255 		 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4256 			return 0;
4257 		if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4258 		    &spidx1->src.sin.sin_addr, spidx0->prefs))
4259 			return 0;
4260 		break;
4261 	case AF_INET6:
4262 		if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4263 		 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4264 			return 0;
4265 		/*
4266 		 * scope_id check. if sin6_scope_id is 0, we regard it
4267 		 * as a wildcard scope, which matches any scope zone ID.
4268 		 */
4269 		if (spidx0->src.sin6.sin6_scope_id &&
4270 		    spidx1->src.sin6.sin6_scope_id &&
4271 		    spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4272 			return 0;
4273 		if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4274 		    &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4275 			return 0;
4276 		break;
4277 	default:
4278 		/* XXX */
4279 		if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4280 			return 0;
4281 		break;
4282 	}
4283 
4284 	switch (spidx0->dst.sa.sa_family) {
4285 	case AF_INET:
4286 		if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4287 		 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4288 			return 0;
4289 		if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4290 		    &spidx1->dst.sin.sin_addr, spidx0->prefd))
4291 			return 0;
4292 		break;
4293 	case AF_INET6:
4294 		if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4295 		 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4296 			return 0;
4297 		/*
4298 		 * scope_id check. if sin6_scope_id is 0, we regard it
4299 		 * as a wildcard scope, which matches any scope zone ID.
4300 		 */
4301 		if (spidx0->dst.sin6.sin6_scope_id &&
4302 		    spidx1->dst.sin6.sin6_scope_id &&
4303 		    spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4304 			return 0;
4305 		if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4306 		    &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4307 			return 0;
4308 		break;
4309 	default:
4310 		/* XXX */
4311 		if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4312 			return 0;
4313 		break;
4314 	}
4315 
4316 	/* XXX Do we check other field ?  e.g. flowinfo */
4317 
4318 	return 1;
4319 }
4320 
4321 #ifdef satosin
4322 #undef satosin
4323 #endif
4324 #define satosin(s) ((const struct sockaddr_in *)s)
4325 #ifdef satosin6
4326 #undef satosin6
4327 #endif
4328 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4329 /* returns 0 on match */
4330 int
4331 key_sockaddrcmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
4332     int port)
4333 {
4334 	if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4335 		return 1;
4336 
4337 	switch (sa1->sa_family) {
4338 #ifdef INET
4339 	case AF_INET:
4340 		if (sa1->sa_len != sizeof(struct sockaddr_in))
4341 			return 1;
4342 		if (satosin(sa1)->sin_addr.s_addr !=
4343 		    satosin(sa2)->sin_addr.s_addr) {
4344 			return 1;
4345 		}
4346 		if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4347 			return 1;
4348 		break;
4349 #endif
4350 #ifdef INET6
4351 	case AF_INET6:
4352 		if (sa1->sa_len != sizeof(struct sockaddr_in6))
4353 			return 1;	/*EINVAL*/
4354 		if (satosin6(sa1)->sin6_scope_id !=
4355 		    satosin6(sa2)->sin6_scope_id) {
4356 			return 1;
4357 		}
4358 		if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4359 		    &satosin6(sa2)->sin6_addr)) {
4360 			return 1;
4361 		}
4362 		if (port &&
4363 		    satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4364 			return 1;
4365 		}
4366 		break;
4367 #endif
4368 	default:
4369 		if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4370 			return 1;
4371 		break;
4372 	}
4373 
4374 	return 0;
4375 }
4376 
4377 /* returns 0 on match */
4378 int
4379 key_sockaddrcmp_withmask(const struct sockaddr *sa1,
4380     const struct sockaddr *sa2, size_t mask)
4381 {
4382 	if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4383 		return (1);
4384 
4385 	switch (sa1->sa_family) {
4386 #ifdef INET
4387 	case AF_INET:
4388 		return (!key_bbcmp(&satosin(sa1)->sin_addr,
4389 		    &satosin(sa2)->sin_addr, mask));
4390 #endif
4391 #ifdef INET6
4392 	case AF_INET6:
4393 		if (satosin6(sa1)->sin6_scope_id !=
4394 		    satosin6(sa2)->sin6_scope_id)
4395 			return (1);
4396 		return (!key_bbcmp(&satosin6(sa1)->sin6_addr,
4397 		    &satosin6(sa2)->sin6_addr, mask));
4398 #endif
4399 	}
4400 	return (1);
4401 }
4402 #undef satosin
4403 #undef satosin6
4404 
4405 /*
4406  * compare two buffers with mask.
4407  * IN:
4408  *	addr1: source
4409  *	addr2: object
4410  *	bits:  Number of bits to compare
4411  * OUT:
4412  *	1 : equal
4413  *	0 : not equal
4414  */
4415 static int
4416 key_bbcmp(const void *a1, const void *a2, u_int bits)
4417 {
4418 	const unsigned char *p1 = a1;
4419 	const unsigned char *p2 = a2;
4420 
4421 	/* XXX: This could be considerably faster if we compare a word
4422 	 * at a time, but it is complicated on LSB Endian machines */
4423 
4424 	/* Handle null pointers */
4425 	if (p1 == NULL || p2 == NULL)
4426 		return (p1 == p2);
4427 
4428 	while (bits >= 8) {
4429 		if (*p1++ != *p2++)
4430 			return 0;
4431 		bits -= 8;
4432 	}
4433 
4434 	if (bits > 0) {
4435 		u_int8_t mask = ~((1<<(8-bits))-1);
4436 		if ((*p1 & mask) != (*p2 & mask))
4437 			return 0;
4438 	}
4439 	return 1;	/* Match! */
4440 }
4441 
4442 static void
4443 key_flush_spd(time_t now)
4444 {
4445 	SPTREE_RLOCK_TRACKER;
4446 	struct secpolicy_list drainq;
4447 	struct secpolicy *sp, *nextsp;
4448 	u_int dir;
4449 
4450 	LIST_INIT(&drainq);
4451 	SPTREE_RLOCK();
4452 	for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4453 		TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
4454 			if (sp->lifetime == 0 && sp->validtime == 0)
4455 				continue;
4456 			if ((sp->lifetime &&
4457 			    now - sp->created > sp->lifetime) ||
4458 			    (sp->validtime &&
4459 			    now - sp->lastused > sp->validtime)) {
4460 				/* Hold extra reference to send SPDEXPIRE */
4461 				SP_ADDREF(sp);
4462 				LIST_INSERT_HEAD(&drainq, sp, drainq);
4463 			}
4464 		}
4465 	}
4466 	SPTREE_RUNLOCK();
4467 	if (LIST_EMPTY(&drainq))
4468 		return;
4469 
4470 	SPTREE_WLOCK();
4471 	sp = LIST_FIRST(&drainq);
4472 	while (sp != NULL) {
4473 		nextsp = LIST_NEXT(sp, drainq);
4474 		/* Check that SP is still linked */
4475 		if (sp->state != IPSEC_SPSTATE_ALIVE) {
4476 			LIST_REMOVE(sp, drainq);
4477 			key_freesp(&sp); /* release extra reference */
4478 			sp = nextsp;
4479 			continue;
4480 		}
4481 		TAILQ_REMOVE(&V_sptree[sp->spidx.dir], sp, chain);
4482 		V_spd_size--;
4483 		LIST_REMOVE(sp, idhash);
4484 		sp->state = IPSEC_SPSTATE_DEAD;
4485 		sp = nextsp;
4486 	}
4487 	V_sp_genid++;
4488 	SPTREE_WUNLOCK();
4489 	if (SPDCACHE_ENABLED())
4490 		spdcache_clear();
4491 
4492 	sp = LIST_FIRST(&drainq);
4493 	while (sp != NULL) {
4494 		nextsp = LIST_NEXT(sp, drainq);
4495 		key_spdexpire(sp);
4496 		key_freesp(&sp); /* release extra reference */
4497 		key_freesp(&sp); /* release last reference */
4498 		sp = nextsp;
4499 	}
4500 }
4501 
4502 static void
4503 key_flush_sad(time_t now)
4504 {
4505 	SAHTREE_RLOCK_TRACKER;
4506 	struct secashead_list emptyq;
4507 	struct secasvar_list drainq, hexpireq, sexpireq, freeq;
4508 	struct secashead *sah, *nextsah;
4509 	struct secasvar *sav, *nextsav;
4510 
4511 	LIST_INIT(&drainq);
4512 	LIST_INIT(&hexpireq);
4513 	LIST_INIT(&sexpireq);
4514 	LIST_INIT(&emptyq);
4515 
4516 	SAHTREE_RLOCK();
4517 	TAILQ_FOREACH(sah, &V_sahtree, chain) {
4518 		/* Check for empty SAH */
4519 		if (TAILQ_EMPTY(&sah->savtree_larval) &&
4520 		    TAILQ_EMPTY(&sah->savtree_alive)) {
4521 			SAH_ADDREF(sah);
4522 			LIST_INSERT_HEAD(&emptyq, sah, drainq);
4523 			continue;
4524 		}
4525 		/* Add all stale LARVAL SAs into drainq */
4526 		TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
4527 			if (now - sav->created < V_key_larval_lifetime)
4528 				continue;
4529 			SAV_ADDREF(sav);
4530 			LIST_INSERT_HEAD(&drainq, sav, drainq);
4531 		}
4532 		TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
4533 			/* lifetimes aren't specified */
4534 			if (sav->lft_h == NULL)
4535 				continue;
4536 			SECASVAR_LOCK(sav);
4537 			/*
4538 			 * Check again with lock held, because it may
4539 			 * be updated by SADB_UPDATE.
4540 			 */
4541 			if (sav->lft_h == NULL) {
4542 				SECASVAR_UNLOCK(sav);
4543 				continue;
4544 			}
4545 			/*
4546 			 * RFC 2367:
4547 			 * HARD lifetimes MUST take precedence over SOFT
4548 			 * lifetimes, meaning if the HARD and SOFT lifetimes
4549 			 * are the same, the HARD lifetime will appear on the
4550 			 * EXPIRE message.
4551 			 */
4552 			/* check HARD lifetime */
4553 			if ((sav->lft_h->addtime != 0 &&
4554 			    now - sav->created > sav->lft_h->addtime) ||
4555 			    (sav->lft_h->usetime != 0 && sav->firstused &&
4556 			    now - sav->firstused > sav->lft_h->usetime) ||
4557 			    (sav->lft_h->bytes != 0 && counter_u64_fetch(
4558 			        sav->lft_c_bytes) > sav->lft_h->bytes)) {
4559 				SECASVAR_UNLOCK(sav);
4560 				SAV_ADDREF(sav);
4561 				LIST_INSERT_HEAD(&hexpireq, sav, drainq);
4562 				continue;
4563 			}
4564 			/* check SOFT lifetime (only for MATURE SAs) */
4565 			if (sav->state == SADB_SASTATE_MATURE && (
4566 			    (sav->lft_s->addtime != 0 &&
4567 			    now - sav->created > sav->lft_s->addtime) ||
4568 			    (sav->lft_s->usetime != 0 && sav->firstused &&
4569 			    now - sav->firstused > sav->lft_s->usetime) ||
4570 			    (sav->lft_s->bytes != 0 && counter_u64_fetch(
4571 				sav->lft_c_bytes) > sav->lft_s->bytes))) {
4572 				SECASVAR_UNLOCK(sav);
4573 				SAV_ADDREF(sav);
4574 				LIST_INSERT_HEAD(&sexpireq, sav, drainq);
4575 				continue;
4576 			}
4577 			SECASVAR_UNLOCK(sav);
4578 		}
4579 	}
4580 	SAHTREE_RUNLOCK();
4581 
4582 	if (LIST_EMPTY(&emptyq) && LIST_EMPTY(&drainq) &&
4583 	    LIST_EMPTY(&hexpireq) && LIST_EMPTY(&sexpireq))
4584 		return;
4585 
4586 	LIST_INIT(&freeq);
4587 	SAHTREE_WLOCK();
4588 	/* Unlink stale LARVAL SAs */
4589 	sav = LIST_FIRST(&drainq);
4590 	while (sav != NULL) {
4591 		nextsav = LIST_NEXT(sav, drainq);
4592 		/* Check that SA is still LARVAL */
4593 		if (sav->state != SADB_SASTATE_LARVAL) {
4594 			LIST_REMOVE(sav, drainq);
4595 			LIST_INSERT_HEAD(&freeq, sav, drainq);
4596 			sav = nextsav;
4597 			continue;
4598 		}
4599 		TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
4600 		LIST_REMOVE(sav, spihash);
4601 		sav->state = SADB_SASTATE_DEAD;
4602 		sav = nextsav;
4603 	}
4604 	/* Unlink all SAs with expired HARD lifetime */
4605 	sav = LIST_FIRST(&hexpireq);
4606 	while (sav != NULL) {
4607 		nextsav = LIST_NEXT(sav, drainq);
4608 		/* Check that SA is not unlinked */
4609 		if (sav->state == SADB_SASTATE_DEAD) {
4610 			LIST_REMOVE(sav, drainq);
4611 			LIST_INSERT_HEAD(&freeq, sav, drainq);
4612 			sav = nextsav;
4613 			continue;
4614 		}
4615 		TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
4616 		LIST_REMOVE(sav, spihash);
4617 		sav->state = SADB_SASTATE_DEAD;
4618 		sav = nextsav;
4619 	}
4620 	/* Mark all SAs with expired SOFT lifetime as DYING */
4621 	sav = LIST_FIRST(&sexpireq);
4622 	while (sav != NULL) {
4623 		nextsav = LIST_NEXT(sav, drainq);
4624 		/* Check that SA is not unlinked */
4625 		if (sav->state == SADB_SASTATE_DEAD) {
4626 			LIST_REMOVE(sav, drainq);
4627 			LIST_INSERT_HEAD(&freeq, sav, drainq);
4628 			sav = nextsav;
4629 			continue;
4630 		}
4631 		/*
4632 		 * NOTE: this doesn't change SA order in the chain.
4633 		 */
4634 		sav->state = SADB_SASTATE_DYING;
4635 		sav = nextsav;
4636 	}
4637 	/* Unlink empty SAHs */
4638 	sah = LIST_FIRST(&emptyq);
4639 	while (sah != NULL) {
4640 		nextsah = LIST_NEXT(sah, drainq);
4641 		/* Check that SAH is still empty and not unlinked */
4642 		if (sah->state == SADB_SASTATE_DEAD ||
4643 		    !TAILQ_EMPTY(&sah->savtree_larval) ||
4644 		    !TAILQ_EMPTY(&sah->savtree_alive)) {
4645 			LIST_REMOVE(sah, drainq);
4646 			key_freesah(&sah); /* release extra reference */
4647 			sah = nextsah;
4648 			continue;
4649 		}
4650 		TAILQ_REMOVE(&V_sahtree, sah, chain);
4651 		LIST_REMOVE(sah, addrhash);
4652 		sah->state = SADB_SASTATE_DEAD;
4653 		sah = nextsah;
4654 	}
4655 	SAHTREE_WUNLOCK();
4656 
4657 	/* Send SPDEXPIRE messages */
4658 	sav = LIST_FIRST(&hexpireq);
4659 	while (sav != NULL) {
4660 		nextsav = LIST_NEXT(sav, drainq);
4661 		key_expire(sav, 1);
4662 		key_freesah(&sav->sah); /* release reference from SAV */
4663 		key_freesav(&sav); /* release extra reference */
4664 		key_freesav(&sav); /* release last reference */
4665 		sav = nextsav;
4666 	}
4667 	sav = LIST_FIRST(&sexpireq);
4668 	while (sav != NULL) {
4669 		nextsav = LIST_NEXT(sav, drainq);
4670 		key_expire(sav, 0);
4671 		key_freesav(&sav); /* release extra reference */
4672 		sav = nextsav;
4673 	}
4674 	/* Free stale LARVAL SAs */
4675 	sav = LIST_FIRST(&drainq);
4676 	while (sav != NULL) {
4677 		nextsav = LIST_NEXT(sav, drainq);
4678 		key_freesah(&sav->sah); /* release reference from SAV */
4679 		key_freesav(&sav); /* release extra reference */
4680 		key_freesav(&sav); /* release last reference */
4681 		sav = nextsav;
4682 	}
4683 	/* Free SAs that were unlinked/changed by someone else */
4684 	sav = LIST_FIRST(&freeq);
4685 	while (sav != NULL) {
4686 		nextsav = LIST_NEXT(sav, drainq);
4687 		key_freesav(&sav); /* release extra reference */
4688 		sav = nextsav;
4689 	}
4690 	/* Free empty SAH */
4691 	sah = LIST_FIRST(&emptyq);
4692 	while (sah != NULL) {
4693 		nextsah = LIST_NEXT(sah, drainq);
4694 		key_freesah(&sah); /* release extra reference */
4695 		key_freesah(&sah); /* release last reference */
4696 		sah = nextsah;
4697 	}
4698 }
4699 
4700 static void
4701 key_flush_acq(time_t now)
4702 {
4703 	struct secacq *acq, *nextacq;
4704 
4705 	/* ACQ tree */
4706 	ACQ_LOCK();
4707 	acq = LIST_FIRST(&V_acqtree);
4708 	while (acq != NULL) {
4709 		nextacq = LIST_NEXT(acq, chain);
4710 		if (now - acq->created > V_key_blockacq_lifetime) {
4711 			LIST_REMOVE(acq, chain);
4712 			LIST_REMOVE(acq, addrhash);
4713 			LIST_REMOVE(acq, seqhash);
4714 			free(acq, M_IPSEC_SAQ);
4715 		}
4716 		acq = nextacq;
4717 	}
4718 	ACQ_UNLOCK();
4719 }
4720 
4721 static void
4722 key_flush_spacq(time_t now)
4723 {
4724 	struct secspacq *acq, *nextacq;
4725 
4726 	/* SP ACQ tree */
4727 	SPACQ_LOCK();
4728 	for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4729 		nextacq = LIST_NEXT(acq, chain);
4730 		if (now - acq->created > V_key_blockacq_lifetime
4731 		 && __LIST_CHAINED(acq)) {
4732 			LIST_REMOVE(acq, chain);
4733 			free(acq, M_IPSEC_SAQ);
4734 		}
4735 	}
4736 	SPACQ_UNLOCK();
4737 }
4738 
4739 /*
4740  * time handler.
4741  * scanning SPD and SAD to check status for each entries,
4742  * and do to remove or to expire.
4743  * XXX: year 2038 problem may remain.
4744  */
4745 static void
4746 key_timehandler(void *arg)
4747 {
4748 	VNET_ITERATOR_DECL(vnet_iter);
4749 	time_t now = time_second;
4750 
4751 	VNET_LIST_RLOCK_NOSLEEP();
4752 	VNET_FOREACH(vnet_iter) {
4753 		CURVNET_SET(vnet_iter);
4754 		key_flush_spd(now);
4755 		key_flush_sad(now);
4756 		key_flush_acq(now);
4757 		key_flush_spacq(now);
4758 		CURVNET_RESTORE();
4759 	}
4760 	VNET_LIST_RUNLOCK_NOSLEEP();
4761 
4762 #ifndef IPSEC_DEBUG2
4763 	/* do exchange to tick time !! */
4764 	callout_schedule(&key_timer, hz);
4765 #endif /* IPSEC_DEBUG2 */
4766 }
4767 
4768 u_long
4769 key_random()
4770 {
4771 	u_long value;
4772 
4773 	key_randomfill(&value, sizeof(value));
4774 	return value;
4775 }
4776 
4777 void
4778 key_randomfill(void *p, size_t l)
4779 {
4780 	size_t n;
4781 	u_long v;
4782 	static int warn = 1;
4783 
4784 	n = 0;
4785 	n = (size_t)read_random(p, (u_int)l);
4786 	/* last resort */
4787 	while (n < l) {
4788 		v = random();
4789 		bcopy(&v, (u_int8_t *)p + n,
4790 		    l - n < sizeof(v) ? l - n : sizeof(v));
4791 		n += sizeof(v);
4792 
4793 		if (warn) {
4794 			printf("WARNING: pseudo-random number generator "
4795 			    "used for IPsec processing\n");
4796 			warn = 0;
4797 		}
4798 	}
4799 }
4800 
4801 /*
4802  * map SADB_SATYPE_* to IPPROTO_*.
4803  * if satype == SADB_SATYPE then satype is mapped to ~0.
4804  * OUT:
4805  *	0: invalid satype.
4806  */
4807 static uint8_t
4808 key_satype2proto(uint8_t satype)
4809 {
4810 	switch (satype) {
4811 	case SADB_SATYPE_UNSPEC:
4812 		return IPSEC_PROTO_ANY;
4813 	case SADB_SATYPE_AH:
4814 		return IPPROTO_AH;
4815 	case SADB_SATYPE_ESP:
4816 		return IPPROTO_ESP;
4817 	case SADB_X_SATYPE_IPCOMP:
4818 		return IPPROTO_IPCOMP;
4819 	case SADB_X_SATYPE_TCPSIGNATURE:
4820 		return IPPROTO_TCP;
4821 	default:
4822 		return 0;
4823 	}
4824 	/* NOTREACHED */
4825 }
4826 
4827 /*
4828  * map IPPROTO_* to SADB_SATYPE_*
4829  * OUT:
4830  *	0: invalid protocol type.
4831  */
4832 static uint8_t
4833 key_proto2satype(uint8_t proto)
4834 {
4835 	switch (proto) {
4836 	case IPPROTO_AH:
4837 		return SADB_SATYPE_AH;
4838 	case IPPROTO_ESP:
4839 		return SADB_SATYPE_ESP;
4840 	case IPPROTO_IPCOMP:
4841 		return SADB_X_SATYPE_IPCOMP;
4842 	case IPPROTO_TCP:
4843 		return SADB_X_SATYPE_TCPSIGNATURE;
4844 	default:
4845 		return 0;
4846 	}
4847 	/* NOTREACHED */
4848 }
4849 
4850 /* %%% PF_KEY */
4851 /*
4852  * SADB_GETSPI processing is to receive
4853  *	<base, (SA2), src address, dst address, (SPI range)>
4854  * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4855  * tree with the status of LARVAL, and send
4856  *	<base, SA(*), address(SD)>
4857  * to the IKMPd.
4858  *
4859  * IN:	mhp: pointer to the pointer to each header.
4860  * OUT:	NULL if fail.
4861  *	other if success, return pointer to the message to send.
4862  */
4863 static int
4864 key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4865 {
4866 	struct secasindex saidx;
4867 	struct sadb_address *src0, *dst0;
4868 	struct secasvar *sav;
4869 	uint32_t reqid, spi;
4870 	int error;
4871 	uint8_t mode, proto;
4872 
4873 	IPSEC_ASSERT(so != NULL, ("null socket"));
4874 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
4875 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4876 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4877 
4878 	if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
4879 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST)
4880 #ifdef PFKEY_STRICT_CHECKS
4881 	    || SADB_CHECKHDR(mhp, SADB_EXT_SPIRANGE)
4882 #endif
4883 	    ) {
4884 		ipseclog((LOG_DEBUG,
4885 		    "%s: invalid message: missing required header.\n",
4886 		    __func__));
4887 		error = EINVAL;
4888 		goto fail;
4889 	}
4890 	if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
4891 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)
4892 #ifdef PFKEY_STRICT_CHECKS
4893 	    || SADB_CHECKLEN(mhp, SADB_EXT_SPIRANGE)
4894 #endif
4895 	    ) {
4896 		ipseclog((LOG_DEBUG,
4897 		    "%s: invalid message: wrong header size.\n", __func__));
4898 		error = EINVAL;
4899 		goto fail;
4900 	}
4901 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
4902 		mode = IPSEC_MODE_ANY;
4903 		reqid = 0;
4904 	} else {
4905 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
4906 			ipseclog((LOG_DEBUG,
4907 			    "%s: invalid message: wrong header size.\n",
4908 			    __func__));
4909 			error = EINVAL;
4910 			goto fail;
4911 		}
4912 		mode = ((struct sadb_x_sa2 *)
4913 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4914 		reqid = ((struct sadb_x_sa2 *)
4915 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4916 	}
4917 
4918 	src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4919 	dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4920 
4921 	/* map satype to proto */
4922 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4923 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4924 			__func__));
4925 		error = EINVAL;
4926 		goto fail;
4927 	}
4928 	error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
4929 	    (struct sockaddr *)(dst0 + 1));
4930 	if (error != 0) {
4931 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
4932 		error = EINVAL;
4933 		goto fail;
4934 	}
4935 	KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4936 
4937 	/* SPI allocation */
4938 	spi = key_do_getnewspi(
4939 	    (struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE], &saidx);
4940 	if (spi == 0) {
4941 		/*
4942 		 * Requested SPI or SPI range is not available or
4943 		 * already used.
4944 		 */
4945 		error = EEXIST;
4946 		goto fail;
4947 	}
4948 	sav = key_newsav(mhp, &saidx, spi, &error);
4949 	if (sav == NULL)
4950 		goto fail;
4951 
4952 	if (sav->seq != 0) {
4953 		/*
4954 		 * RFC2367:
4955 		 * If the SADB_GETSPI message is in response to a
4956 		 * kernel-generated SADB_ACQUIRE, the sadb_msg_seq
4957 		 * MUST be the same as the SADB_ACQUIRE message.
4958 		 *
4959 		 * XXXAE: However it doesn't definethe behaviour how to
4960 		 * check this and what to do if it doesn't match.
4961 		 * Also what we should do if it matches?
4962 		 *
4963 		 * We can compare saidx used in SADB_ACQUIRE with saidx
4964 		 * used in SADB_GETSPI, but this probably can break
4965 		 * existing software. For now just warn if it doesn't match.
4966 		 *
4967 		 * XXXAE: anyway it looks useless.
4968 		 */
4969 		key_acqdone(&saidx, sav->seq);
4970 	}
4971 	KEYDBG(KEY_STAMP,
4972 	    printf("%s: SA(%p)\n", __func__, sav));
4973 	KEYDBG(KEY_DATA, kdebug_secasv(sav));
4974 
4975     {
4976 	struct mbuf *n, *nn;
4977 	struct sadb_sa *m_sa;
4978 	struct sadb_msg *newmsg;
4979 	int off, len;
4980 
4981 	/* create new sadb_msg to reply. */
4982 	len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4983 	    PFKEY_ALIGN8(sizeof(struct sadb_sa));
4984 
4985 	MGETHDR(n, M_NOWAIT, MT_DATA);
4986 	if (len > MHLEN) {
4987 		if (!(MCLGET(n, M_NOWAIT))) {
4988 			m_freem(n);
4989 			n = NULL;
4990 		}
4991 	}
4992 	if (!n) {
4993 		error = ENOBUFS;
4994 		goto fail;
4995 	}
4996 
4997 	n->m_len = len;
4998 	n->m_next = NULL;
4999 	off = 0;
5000 
5001 	m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
5002 	off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
5003 
5004 	m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
5005 	m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
5006 	m_sa->sadb_sa_exttype = SADB_EXT_SA;
5007 	m_sa->sadb_sa_spi = spi; /* SPI is already in network byte order */
5008 	off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
5009 
5010 	IPSEC_ASSERT(off == len,
5011 		("length inconsistency (off %u len %u)", off, len));
5012 
5013 	n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
5014 	    SADB_EXT_ADDRESS_DST);
5015 	if (!n->m_next) {
5016 		m_freem(n);
5017 		error = ENOBUFS;
5018 		goto fail;
5019 	}
5020 
5021 	if (n->m_len < sizeof(struct sadb_msg)) {
5022 		n = m_pullup(n, sizeof(struct sadb_msg));
5023 		if (n == NULL)
5024 			return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
5025 	}
5026 
5027 	n->m_pkthdr.len = 0;
5028 	for (nn = n; nn; nn = nn->m_next)
5029 		n->m_pkthdr.len += nn->m_len;
5030 
5031 	newmsg = mtod(n, struct sadb_msg *);
5032 	newmsg->sadb_msg_seq = sav->seq;
5033 	newmsg->sadb_msg_errno = 0;
5034 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5035 
5036 	m_freem(m);
5037 	return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5038     }
5039 
5040 fail:
5041 	return (key_senderror(so, m, error));
5042 }
5043 
5044 /*
5045  * allocating new SPI
5046  * called by key_getspi().
5047  * OUT:
5048  *	0:	failure.
5049  *	others: success, SPI in network byte order.
5050  */
5051 static uint32_t
5052 key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
5053 {
5054 	uint32_t min, max, newspi, t;
5055 	int count = V_key_spi_trycnt;
5056 
5057 	/* set spi range to allocate */
5058 	if (spirange != NULL) {
5059 		min = spirange->sadb_spirange_min;
5060 		max = spirange->sadb_spirange_max;
5061 	} else {
5062 		min = V_key_spi_minval;
5063 		max = V_key_spi_maxval;
5064 	}
5065 	/* IPCOMP needs 2-byte SPI */
5066 	if (saidx->proto == IPPROTO_IPCOMP) {
5067 		if (min >= 0x10000)
5068 			min = 0xffff;
5069 		if (max >= 0x10000)
5070 			max = 0xffff;
5071 		if (min > max) {
5072 			t = min; min = max; max = t;
5073 		}
5074 	}
5075 
5076 	if (min == max) {
5077 		if (!key_checkspidup(htonl(min))) {
5078 			ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
5079 			    __func__, min));
5080 			return 0;
5081 		}
5082 
5083 		count--; /* taking one cost. */
5084 		newspi = min;
5085 	} else {
5086 
5087 		/* init SPI */
5088 		newspi = 0;
5089 
5090 		/* when requesting to allocate spi ranged */
5091 		while (count--) {
5092 			/* generate pseudo-random SPI value ranged. */
5093 			newspi = min + (key_random() % (max - min + 1));
5094 			if (!key_checkspidup(htonl(newspi)))
5095 				break;
5096 		}
5097 
5098 		if (count == 0 || newspi == 0) {
5099 			ipseclog((LOG_DEBUG,
5100 			    "%s: failed to allocate SPI.\n", __func__));
5101 			return 0;
5102 		}
5103 	}
5104 
5105 	/* statistics */
5106 	keystat.getspi_count =
5107 	    (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
5108 
5109 	return (htonl(newspi));
5110 }
5111 
5112 /*
5113  * Find TCP-MD5 SA with corresponding secasindex.
5114  * If not found, return NULL and fill SPI with usable value if needed.
5115  */
5116 static struct secasvar *
5117 key_getsav_tcpmd5(struct secasindex *saidx, uint32_t *spi)
5118 {
5119 	SAHTREE_RLOCK_TRACKER;
5120 	struct secashead *sah;
5121 	struct secasvar *sav;
5122 
5123 	IPSEC_ASSERT(saidx->proto == IPPROTO_TCP, ("wrong proto"));
5124 	SAHTREE_RLOCK();
5125 	LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
5126 		if (sah->saidx.proto != IPPROTO_TCP)
5127 			continue;
5128 		if (!key_sockaddrcmp(&saidx->dst.sa, &sah->saidx.dst.sa, 0) &&
5129 		    !key_sockaddrcmp(&saidx->src.sa, &sah->saidx.src.sa, 0))
5130 			break;
5131 	}
5132 	if (sah != NULL) {
5133 		if (V_key_preferred_oldsa)
5134 			sav = TAILQ_LAST(&sah->savtree_alive, secasvar_queue);
5135 		else
5136 			sav = TAILQ_FIRST(&sah->savtree_alive);
5137 		if (sav != NULL) {
5138 			SAV_ADDREF(sav);
5139 			SAHTREE_RUNLOCK();
5140 			return (sav);
5141 		}
5142 	}
5143 	if (spi == NULL) {
5144 		/* No SPI required */
5145 		SAHTREE_RUNLOCK();
5146 		return (NULL);
5147 	}
5148 	/* Check that SPI is unique */
5149 	LIST_FOREACH(sav, SAVHASH_HASH(*spi), spihash) {
5150 		if (sav->spi == *spi)
5151 			break;
5152 	}
5153 	if (sav == NULL) {
5154 		SAHTREE_RUNLOCK();
5155 		/* SPI is already unique */
5156 		return (NULL);
5157 	}
5158 	SAHTREE_RUNLOCK();
5159 	/* XXX: not optimal */
5160 	*spi = key_do_getnewspi(NULL, saidx);
5161 	return (NULL);
5162 }
5163 
5164 static int
5165 key_updateaddresses(struct socket *so, struct mbuf *m,
5166     const struct sadb_msghdr *mhp, struct secasvar *sav,
5167     struct secasindex *saidx)
5168 {
5169 	struct sockaddr *newaddr;
5170 	struct secashead *sah;
5171 	struct secasvar *newsav, *tmp;
5172 	struct mbuf *n;
5173 	int error, isnew;
5174 
5175 	/* Check that we need to change SAH */
5176 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC)) {
5177 		newaddr = (struct sockaddr *)(
5178 		    ((struct sadb_address *)
5179 		    mhp->ext[SADB_X_EXT_NEW_ADDRESS_SRC]) + 1);
5180 		bcopy(newaddr, &saidx->src, newaddr->sa_len);
5181 		key_porttosaddr(&saidx->src.sa, 0);
5182 	}
5183 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST)) {
5184 		newaddr = (struct sockaddr *)(
5185 		    ((struct sadb_address *)
5186 		    mhp->ext[SADB_X_EXT_NEW_ADDRESS_DST]) + 1);
5187 		bcopy(newaddr, &saidx->dst, newaddr->sa_len);
5188 		key_porttosaddr(&saidx->dst.sa, 0);
5189 	}
5190 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC) ||
5191 	    !SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST)) {
5192 		error = key_checksockaddrs(&saidx->src.sa, &saidx->dst.sa);
5193 		if (error != 0) {
5194 			ipseclog((LOG_DEBUG, "%s: invalid new sockaddr.\n",
5195 			    __func__));
5196 			return (error);
5197 		}
5198 
5199 		sah = key_getsah(saidx);
5200 		if (sah == NULL) {
5201 			/* create a new SA index */
5202 			sah = key_newsah(saidx);
5203 			if (sah == NULL) {
5204 				ipseclog((LOG_DEBUG,
5205 				    "%s: No more memory.\n", __func__));
5206 				return (ENOBUFS);
5207 			}
5208 			isnew = 2; /* SAH is new */
5209 		} else
5210 			isnew = 1; /* existing SAH is referenced */
5211 	} else {
5212 		/*
5213 		 * src and dst addresses are still the same.
5214 		 * Do we want to change NAT-T config?
5215 		 */
5216 		if (sav->sah->saidx.proto != IPPROTO_ESP ||
5217 		    SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) ||
5218 		    SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_SPORT) ||
5219 		    SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_DPORT)) {
5220 			ipseclog((LOG_DEBUG,
5221 			    "%s: invalid message: missing required header.\n",
5222 			    __func__));
5223 			return (EINVAL);
5224 		}
5225 		/* We hold reference to SA, thus SAH will be referenced too. */
5226 		sah = sav->sah;
5227 		isnew = 0;
5228 	}
5229 
5230 	newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA,
5231 	    M_NOWAIT | M_ZERO);
5232 	if (newsav == NULL) {
5233 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5234 		error = ENOBUFS;
5235 		goto fail;
5236 	}
5237 
5238 	/* Clone SA's content into newsav */
5239 	SAV_INITREF(newsav);
5240 	bcopy(sav, newsav, offsetof(struct secasvar, chain));
5241 	/*
5242 	 * We create new NAT-T config if it is needed.
5243 	 * Old NAT-T config will be freed by key_cleansav() when
5244 	 * last reference to SA will be released.
5245 	 */
5246 	newsav->natt = NULL;
5247 	newsav->sah = sah;
5248 	newsav->state = SADB_SASTATE_MATURE;
5249 	error = key_setnatt(newsav, mhp);
5250 	if (error != 0)
5251 		goto fail;
5252 
5253 	SAHTREE_WLOCK();
5254 	/* Check that SA is still alive */
5255 	if (sav->state == SADB_SASTATE_DEAD) {
5256 		/* SA was unlinked */
5257 		SAHTREE_WUNLOCK();
5258 		error = ESRCH;
5259 		goto fail;
5260 	}
5261 
5262 	/* Unlink SA from SAH and SPI hash */
5263 	IPSEC_ASSERT((sav->flags & SADB_X_EXT_F_CLONED) == 0,
5264 	    ("SA is already cloned"));
5265 	IPSEC_ASSERT(sav->state == SADB_SASTATE_MATURE ||
5266 	    sav->state == SADB_SASTATE_DYING,
5267 	    ("Wrong SA state %u\n", sav->state));
5268 	TAILQ_REMOVE(&sav->sah->savtree_alive, sav, chain);
5269 	LIST_REMOVE(sav, spihash);
5270 	sav->state = SADB_SASTATE_DEAD;
5271 
5272 	/*
5273 	 * Link new SA with SAH. Keep SAs ordered by
5274 	 * create time (newer are first).
5275 	 */
5276 	TAILQ_FOREACH(tmp, &sah->savtree_alive, chain) {
5277 		if (newsav->created > tmp->created) {
5278 			TAILQ_INSERT_BEFORE(tmp, newsav, chain);
5279 			break;
5280 		}
5281 	}
5282 	if (tmp == NULL)
5283 		TAILQ_INSERT_TAIL(&sah->savtree_alive, newsav, chain);
5284 
5285 	/* Add new SA into SPI hash. */
5286 	LIST_INSERT_HEAD(SAVHASH_HASH(newsav->spi), newsav, spihash);
5287 
5288 	/* Add new SAH into SADB. */
5289 	if (isnew == 2) {
5290 		TAILQ_INSERT_HEAD(&V_sahtree, sah, chain);
5291 		LIST_INSERT_HEAD(SAHADDRHASH_HASH(saidx), sah, addrhash);
5292 		sah->state = SADB_SASTATE_MATURE;
5293 		SAH_ADDREF(sah); /* newsav references new SAH */
5294 	}
5295 	/*
5296 	 * isnew == 1 -> @sah was referenced by key_getsah().
5297 	 * isnew == 0 -> we use the same @sah, that was used by @sav,
5298 	 *	and we use its reference for @newsav.
5299 	 */
5300 	SECASVAR_LOCK(sav);
5301 	/* XXX: replace cntr with pointer? */
5302 	newsav->cntr = sav->cntr;
5303 	sav->flags |= SADB_X_EXT_F_CLONED;
5304 	SECASVAR_UNLOCK(sav);
5305 
5306 	SAHTREE_WUNLOCK();
5307 
5308 	KEYDBG(KEY_STAMP,
5309 	    printf("%s: SA(%p) cloned into SA(%p)\n",
5310 	    __func__, sav, newsav));
5311 	KEYDBG(KEY_DATA, kdebug_secasv(newsav));
5312 
5313 	key_freesav(&sav); /* release last reference */
5314 
5315 	/* set msg buf from mhp */
5316 	n = key_getmsgbuf_x1(m, mhp);
5317 	if (n == NULL) {
5318 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5319 		return (ENOBUFS);
5320 	}
5321 	m_freem(m);
5322 	key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5323 	return (0);
5324 fail:
5325 	if (isnew != 0)
5326 		key_freesah(&sah);
5327 	if (newsav != NULL) {
5328 		if (newsav->natt != NULL)
5329 			free(newsav->natt, M_IPSEC_MISC);
5330 		free(newsav, M_IPSEC_SA);
5331 	}
5332 	return (error);
5333 }
5334 
5335 /*
5336  * SADB_UPDATE processing
5337  * receive
5338  *   <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5339  *       key(AE), (identity(SD),) (sensitivity)>
5340  * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5341  * and send
5342  *   <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5343  *       (identity(SD),) (sensitivity)>
5344  * to the ikmpd.
5345  *
5346  * m will always be freed.
5347  */
5348 static int
5349 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5350 {
5351 	struct secasindex saidx;
5352 	struct sadb_address *src0, *dst0;
5353 	struct sadb_sa *sa0;
5354 	struct secasvar *sav;
5355 	uint32_t reqid;
5356 	int error;
5357 	uint8_t mode, proto;
5358 
5359 	IPSEC_ASSERT(so != NULL, ("null socket"));
5360 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
5361 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5362 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5363 
5364 	/* map satype to proto */
5365 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5366 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5367 		    __func__));
5368 		return key_senderror(so, m, EINVAL);
5369 	}
5370 
5371 	if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
5372 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
5373 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
5374 	    (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
5375 		!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
5376 	    (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
5377 		!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
5378 		ipseclog((LOG_DEBUG,
5379 		    "%s: invalid message: missing required header.\n",
5380 		    __func__));
5381 		return key_senderror(so, m, EINVAL);
5382 	}
5383 	if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
5384 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
5385 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
5386 		ipseclog((LOG_DEBUG,
5387 		    "%s: invalid message: wrong header size.\n", __func__));
5388 		return key_senderror(so, m, EINVAL);
5389 	}
5390 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
5391 		mode = IPSEC_MODE_ANY;
5392 		reqid = 0;
5393 	} else {
5394 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
5395 			ipseclog((LOG_DEBUG,
5396 			    "%s: invalid message: wrong header size.\n",
5397 			    __func__));
5398 			return key_senderror(so, m, EINVAL);
5399 		}
5400 		mode = ((struct sadb_x_sa2 *)
5401 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5402 		reqid = ((struct sadb_x_sa2 *)
5403 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5404 	}
5405 
5406 	sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5407 	src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5408 	dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5409 
5410 	/*
5411 	 * Only SADB_SASTATE_MATURE SAs may be submitted in an
5412 	 * SADB_UPDATE message.
5413 	 */
5414 	if (sa0->sadb_sa_state != SADB_SASTATE_MATURE) {
5415 		ipseclog((LOG_DEBUG, "%s: invalid state.\n", __func__));
5416 #ifdef PFKEY_STRICT_CHECKS
5417 		return key_senderror(so, m, EINVAL);
5418 #endif
5419 	}
5420 	error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
5421 	    (struct sockaddr *)(dst0 + 1));
5422 	if (error != 0) {
5423 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
5424 		return key_senderror(so, m, error);
5425 	}
5426 	KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5427 	sav = key_getsavbyspi(sa0->sadb_sa_spi);
5428 	if (sav == NULL) {
5429 		ipseclog((LOG_DEBUG, "%s: no SA found for SPI %u\n",
5430 		    __func__, ntohl(sa0->sadb_sa_spi)));
5431 		return key_senderror(so, m, EINVAL);
5432 	}
5433 	/*
5434 	 * Check that SADB_UPDATE issued by the same process that did
5435 	 * SADB_GETSPI or SADB_ADD.
5436 	 */
5437 	if (sav->pid != mhp->msg->sadb_msg_pid) {
5438 		ipseclog((LOG_DEBUG,
5439 		    "%s: pid mismatched (SPI %u, pid %u vs. %u)\n", __func__,
5440 		    ntohl(sav->spi), sav->pid, mhp->msg->sadb_msg_pid));
5441 		key_freesav(&sav);
5442 		return key_senderror(so, m, EINVAL);
5443 	}
5444 	/* saidx should match with SA. */
5445 	if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_MODE_REQID) == 0) {
5446 		ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u",
5447 		    __func__, ntohl(sav->spi)));
5448 		key_freesav(&sav);
5449 		return key_senderror(so, m, ESRCH);
5450 	}
5451 
5452 	if (sav->state == SADB_SASTATE_LARVAL) {
5453 		if ((mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5454 		    SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT)) ||
5455 		    (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5456 		    SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH))) {
5457 			ipseclog((LOG_DEBUG,
5458 			    "%s: invalid message: missing required header.\n",
5459 			    __func__));
5460 			key_freesav(&sav);
5461 			return key_senderror(so, m, EINVAL);
5462 		}
5463 		/*
5464 		 * We can set any values except src, dst and SPI.
5465 		 */
5466 		error = key_setsaval(sav, mhp);
5467 		if (error != 0) {
5468 			key_freesav(&sav);
5469 			return (key_senderror(so, m, error));
5470 		}
5471 		/* Change SA state to MATURE */
5472 		SAHTREE_WLOCK();
5473 		if (sav->state != SADB_SASTATE_LARVAL) {
5474 			/* SA was deleted or another thread made it MATURE. */
5475 			SAHTREE_WUNLOCK();
5476 			key_freesav(&sav);
5477 			return (key_senderror(so, m, ESRCH));
5478 		}
5479 		/*
5480 		 * NOTE: we keep SAs in savtree_alive ordered by created
5481 		 * time. When SA's state changed from LARVAL to MATURE,
5482 		 * we update its created time in key_setsaval() and move
5483 		 * it into head of savtree_alive.
5484 		 */
5485 		TAILQ_REMOVE(&sav->sah->savtree_larval, sav, chain);
5486 		TAILQ_INSERT_HEAD(&sav->sah->savtree_alive, sav, chain);
5487 		sav->state = SADB_SASTATE_MATURE;
5488 		SAHTREE_WUNLOCK();
5489 	} else {
5490 		/*
5491 		 * For DYING and MATURE SA we can change only state
5492 		 * and lifetimes. Report EINVAL if something else attempted
5493 		 * to change.
5494 		 */
5495 		if (!SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT) ||
5496 		    !SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH)) {
5497 			key_freesav(&sav);
5498 			return (key_senderror(so, m, EINVAL));
5499 		}
5500 		error = key_updatelifetimes(sav, mhp);
5501 		if (error != 0) {
5502 			key_freesav(&sav);
5503 			return (key_senderror(so, m, error));
5504 		}
5505 		/*
5506 		 * This is FreeBSD extension to RFC2367.
5507 		 * IKEd can specify SADB_X_EXT_NEW_ADDRESS_SRC and/or
5508 		 * SADB_X_EXT_NEW_ADDRESS_DST when it wants to change
5509 		 * SA addresses (for example to implement MOBIKE protocol
5510 		 * as described in RFC4555). Also we allow to change
5511 		 * NAT-T config.
5512 		 */
5513 		if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_SRC) ||
5514 		    !SADB_CHECKHDR(mhp, SADB_X_EXT_NEW_ADDRESS_DST) ||
5515 		    !SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) ||
5516 		    sav->natt != NULL) {
5517 			error = key_updateaddresses(so, m, mhp, sav, &saidx);
5518 			key_freesav(&sav);
5519 			if (error != 0)
5520 				return (key_senderror(so, m, error));
5521 			return (0);
5522 		}
5523 		/* Check that SA is still alive */
5524 		SAHTREE_WLOCK();
5525 		if (sav->state == SADB_SASTATE_DEAD) {
5526 			/* SA was unlinked */
5527 			SAHTREE_WUNLOCK();
5528 			key_freesav(&sav);
5529 			return (key_senderror(so, m, ESRCH));
5530 		}
5531 		/*
5532 		 * NOTE: there is possible state moving from DYING to MATURE,
5533 		 * but this doesn't change created time, so we won't reorder
5534 		 * this SA.
5535 		 */
5536 		sav->state = SADB_SASTATE_MATURE;
5537 		SAHTREE_WUNLOCK();
5538 	}
5539 	KEYDBG(KEY_STAMP,
5540 	    printf("%s: SA(%p)\n", __func__, sav));
5541 	KEYDBG(KEY_DATA, kdebug_secasv(sav));
5542 	key_freesav(&sav);
5543 
5544     {
5545 	struct mbuf *n;
5546 
5547 	/* set msg buf from mhp */
5548 	n = key_getmsgbuf_x1(m, mhp);
5549 	if (n == NULL) {
5550 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5551 		return key_senderror(so, m, ENOBUFS);
5552 	}
5553 
5554 	m_freem(m);
5555 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5556     }
5557 }
5558 
5559 /*
5560  * SADB_ADD processing
5561  * add an entry to SA database, when received
5562  *   <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5563  *       key(AE), (identity(SD),) (sensitivity)>
5564  * from the ikmpd,
5565  * and send
5566  *   <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5567  *       (identity(SD),) (sensitivity)>
5568  * to the ikmpd.
5569  *
5570  * IGNORE identity and sensitivity messages.
5571  *
5572  * m will always be freed.
5573  */
5574 static int
5575 key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5576 {
5577 	struct secasindex saidx;
5578 	struct sadb_address *src0, *dst0;
5579 	struct sadb_sa *sa0;
5580 	struct secasvar *sav;
5581 	uint32_t reqid, spi;
5582 	uint8_t mode, proto;
5583 	int error;
5584 
5585 	IPSEC_ASSERT(so != NULL, ("null socket"));
5586 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
5587 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5588 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5589 
5590 	/* map satype to proto */
5591 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5592 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5593 		    __func__));
5594 		return key_senderror(so, m, EINVAL);
5595 	}
5596 
5597 	if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
5598 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
5599 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
5600 	    (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP && (
5601 		SADB_CHECKHDR(mhp, SADB_EXT_KEY_ENCRYPT) ||
5602 		SADB_CHECKLEN(mhp, SADB_EXT_KEY_ENCRYPT))) ||
5603 	    (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH && (
5604 		SADB_CHECKHDR(mhp, SADB_EXT_KEY_AUTH) ||
5605 		SADB_CHECKLEN(mhp, SADB_EXT_KEY_AUTH))) ||
5606 	    (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD) &&
5607 		!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT)) ||
5608 	    (SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_SOFT) &&
5609 		!SADB_CHECKHDR(mhp, SADB_EXT_LIFETIME_HARD))) {
5610 		ipseclog((LOG_DEBUG,
5611 		    "%s: invalid message: missing required header.\n",
5612 		    __func__));
5613 		return key_senderror(so, m, EINVAL);
5614 	}
5615 	if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
5616 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
5617 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
5618 		ipseclog((LOG_DEBUG,
5619 		    "%s: invalid message: wrong header size.\n", __func__));
5620 		return key_senderror(so, m, EINVAL);
5621 	}
5622 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
5623 		mode = IPSEC_MODE_ANY;
5624 		reqid = 0;
5625 	} else {
5626 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
5627 			ipseclog((LOG_DEBUG,
5628 			    "%s: invalid message: wrong header size.\n",
5629 			    __func__));
5630 			return key_senderror(so, m, EINVAL);
5631 		}
5632 		mode = ((struct sadb_x_sa2 *)
5633 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5634 		reqid = ((struct sadb_x_sa2 *)
5635 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5636 	}
5637 
5638 	sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5639 	src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5640 	dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5641 
5642 	/*
5643 	 * Only SADB_SASTATE_MATURE SAs may be submitted in an
5644 	 * SADB_ADD message.
5645 	 */
5646 	if (sa0->sadb_sa_state != SADB_SASTATE_MATURE) {
5647 		ipseclog((LOG_DEBUG, "%s: invalid state.\n", __func__));
5648 #ifdef PFKEY_STRICT_CHECKS
5649 		return key_senderror(so, m, EINVAL);
5650 #endif
5651 	}
5652 	error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
5653 	    (struct sockaddr *)(dst0 + 1));
5654 	if (error != 0) {
5655 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
5656 		return key_senderror(so, m, error);
5657 	}
5658 	KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5659 	spi = sa0->sadb_sa_spi;
5660 	/*
5661 	 * For TCP-MD5 SAs we don't use SPI. Check the uniqueness using
5662 	 * secasindex.
5663 	 * XXXAE: IPComp seems also doesn't use SPI.
5664 	 */
5665 	if (proto == IPPROTO_TCP) {
5666 		sav = key_getsav_tcpmd5(&saidx, &spi);
5667 		if (sav == NULL && spi == 0) {
5668 			/* Failed to allocate SPI */
5669 			ipseclog((LOG_DEBUG, "%s: SA already exists.\n",
5670 			    __func__));
5671 			return key_senderror(so, m, EEXIST);
5672 		}
5673 		/* XXX: SPI that we report back can have another value */
5674 	} else {
5675 		/* We can create new SA only if SPI is different. */
5676 		sav = key_getsavbyspi(spi);
5677 	}
5678 	if (sav != NULL) {
5679 		key_freesav(&sav);
5680 		ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5681 		return key_senderror(so, m, EEXIST);
5682 	}
5683 
5684 	sav = key_newsav(mhp, &saidx, spi, &error);
5685 	if (sav == NULL)
5686 		return key_senderror(so, m, error);
5687 	KEYDBG(KEY_STAMP,
5688 	    printf("%s: return SA(%p)\n", __func__, sav));
5689 	KEYDBG(KEY_DATA, kdebug_secasv(sav));
5690 	/*
5691 	 * If SADB_ADD was in response to SADB_ACQUIRE, we need to schedule
5692 	 * ACQ for deletion.
5693 	 */
5694 	if (sav->seq != 0)
5695 		key_acqdone(&saidx, sav->seq);
5696 
5697     {
5698 	/*
5699 	 * Don't call key_freesav() on error here, as we would like to
5700 	 * keep the SA in the database.
5701 	 */
5702 	struct mbuf *n;
5703 
5704 	/* set msg buf from mhp */
5705 	n = key_getmsgbuf_x1(m, mhp);
5706 	if (n == NULL) {
5707 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5708 		return key_senderror(so, m, ENOBUFS);
5709 	}
5710 
5711 	m_freem(m);
5712 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5713     }
5714 }
5715 
5716 /*
5717  * NAT-T support.
5718  * IKEd may request the use ESP in UDP encapsulation when it detects the
5719  * presence of NAT. It uses NAT-T extension headers for such SAs to specify
5720  * parameters needed for encapsulation and decapsulation. These PF_KEY
5721  * extension headers are not standardized, so this comment addresses our
5722  * implementation.
5723  * SADB_X_EXT_NAT_T_TYPE specifies type of encapsulation, we support only
5724  * UDP_ENCAP_ESPINUDP as described in RFC3948.
5725  * SADB_X_EXT_NAT_T_SPORT/DPORT specifies source and destination ports for
5726  * UDP header. We use these ports in UDP encapsulation procedure, also we
5727  * can check them in UDP decapsulation procedure.
5728  * SADB_X_EXT_NAT_T_OA[IR] specifies original address of initiator or
5729  * responder. These addresses can be used for transport mode to adjust
5730  * checksum after decapsulation and decryption. Since original IP addresses
5731  * used by peer usually different (we detected presence of NAT), TCP/UDP
5732  * pseudo header checksum and IP header checksum was calculated using original
5733  * addresses. After decapsulation and decryption we need to adjust checksum
5734  * to have correct datagram.
5735  *
5736  * We expect presence of NAT-T extension headers only in SADB_ADD and
5737  * SADB_UPDATE messages. We report NAT-T extension headers in replies
5738  * to SADB_ADD, SADB_UPDATE, SADB_GET, and SADB_DUMP messages.
5739  */
5740 static int
5741 key_setnatt(struct secasvar *sav, const struct sadb_msghdr *mhp)
5742 {
5743 	struct sadb_x_nat_t_port *port;
5744 	struct sadb_x_nat_t_type *type;
5745 	struct sadb_address *oai, *oar;
5746 	struct sockaddr *sa;
5747 	uint32_t addr;
5748 	uint16_t cksum;
5749 
5750 	IPSEC_ASSERT(sav->natt == NULL, ("natt is already initialized"));
5751 	/*
5752 	 * Ignore NAT-T headers if sproto isn't ESP.
5753 	 */
5754 	if (sav->sah->saidx.proto != IPPROTO_ESP)
5755 		return (0);
5756 
5757 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_TYPE) &&
5758 	    !SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_SPORT) &&
5759 	    !SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_DPORT)) {
5760 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_TYPE) ||
5761 		    SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_SPORT) ||
5762 		    SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_DPORT)) {
5763 			ipseclog((LOG_DEBUG,
5764 			    "%s: invalid message: wrong header size.\n",
5765 			    __func__));
5766 			return (EINVAL);
5767 		}
5768 	} else
5769 		return (0);
5770 
5771 	type = (struct sadb_x_nat_t_type *)mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5772 	if (type->sadb_x_nat_t_type_type != UDP_ENCAP_ESPINUDP) {
5773 		ipseclog((LOG_DEBUG, "%s: unsupported NAT-T type %u.\n",
5774 		    __func__, type->sadb_x_nat_t_type_type));
5775 		return (EINVAL);
5776 	}
5777 	/*
5778 	 * Allocate storage for NAT-T config.
5779 	 * On error it will be released by key_cleansav().
5780 	 */
5781 	sav->natt = malloc(sizeof(struct secnatt), M_IPSEC_MISC,
5782 	    M_NOWAIT | M_ZERO);
5783 	if (sav->natt == NULL) {
5784 		PFKEYSTAT_INC(in_nomem);
5785 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5786 		return (ENOBUFS);
5787 	}
5788 	port = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5789 	if (port->sadb_x_nat_t_port_port == 0) {
5790 		ipseclog((LOG_DEBUG, "%s: invalid NAT-T sport specified.\n",
5791 		    __func__));
5792 		return (EINVAL);
5793 	}
5794 	sav->natt->sport = port->sadb_x_nat_t_port_port;
5795 	port = (struct sadb_x_nat_t_port *)mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5796 	if (port->sadb_x_nat_t_port_port == 0) {
5797 		ipseclog((LOG_DEBUG, "%s: invalid NAT-T dport specified.\n",
5798 		    __func__));
5799 		return (EINVAL);
5800 	}
5801 	sav->natt->dport = port->sadb_x_nat_t_port_port;
5802 
5803 	/*
5804 	 * SADB_X_EXT_NAT_T_OAI and SADB_X_EXT_NAT_T_OAR are optional
5805 	 * and needed only for transport mode IPsec.
5806 	 * Usually NAT translates only one address, but it is possible,
5807 	 * that both addresses could be translated.
5808 	 * NOTE: Value of SADB_X_EXT_NAT_T_OAI is equal to SADB_X_EXT_NAT_T_OA.
5809 	 */
5810 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_OAI)) {
5811 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_OAI)) {
5812 			ipseclog((LOG_DEBUG,
5813 			    "%s: invalid message: wrong header size.\n",
5814 			    __func__));
5815 			return (EINVAL);
5816 		}
5817 		oai = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5818 	} else
5819 		oai = NULL;
5820 	if (!SADB_CHECKHDR(mhp, SADB_X_EXT_NAT_T_OAR)) {
5821 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_NAT_T_OAR)) {
5822 			ipseclog((LOG_DEBUG,
5823 			    "%s: invalid message: wrong header size.\n",
5824 			    __func__));
5825 			return (EINVAL);
5826 		}
5827 		oar = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5828 	} else
5829 		oar = NULL;
5830 
5831 	/* Initialize addresses only for transport mode */
5832 	if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL) {
5833 		cksum = 0;
5834 		if (oai != NULL) {
5835 			/* Currently we support only AF_INET */
5836 			sa = (struct sockaddr *)(oai + 1);
5837 			if (sa->sa_family != AF_INET ||
5838 			    sa->sa_len != sizeof(struct sockaddr_in)) {
5839 				ipseclog((LOG_DEBUG,
5840 				    "%s: wrong NAT-OAi header.\n",
5841 				    __func__));
5842 				return (EINVAL);
5843 			}
5844 			/* Ignore address if it the same */
5845 			if (((struct sockaddr_in *)sa)->sin_addr.s_addr !=
5846 			    sav->sah->saidx.src.sin.sin_addr.s_addr) {
5847 				bcopy(sa, &sav->natt->oai.sa, sa->sa_len);
5848 				sav->natt->flags |= IPSEC_NATT_F_OAI;
5849 				/* Calculate checksum delta */
5850 				addr = sav->sah->saidx.src.sin.sin_addr.s_addr;
5851 				cksum = in_addword(cksum, ~addr >> 16);
5852 				cksum = in_addword(cksum, ~addr & 0xffff);
5853 				addr = sav->natt->oai.sin.sin_addr.s_addr;
5854 				cksum = in_addword(cksum, addr >> 16);
5855 				cksum = in_addword(cksum, addr & 0xffff);
5856 			}
5857 		}
5858 		if (oar != NULL) {
5859 			/* Currently we support only AF_INET */
5860 			sa = (struct sockaddr *)(oar + 1);
5861 			if (sa->sa_family != AF_INET ||
5862 			    sa->sa_len != sizeof(struct sockaddr_in)) {
5863 				ipseclog((LOG_DEBUG,
5864 				    "%s: wrong NAT-OAr header.\n",
5865 				    __func__));
5866 				return (EINVAL);
5867 			}
5868 			/* Ignore address if it the same */
5869 			if (((struct sockaddr_in *)sa)->sin_addr.s_addr !=
5870 			    sav->sah->saidx.dst.sin.sin_addr.s_addr) {
5871 				bcopy(sa, &sav->natt->oar.sa, sa->sa_len);
5872 				sav->natt->flags |= IPSEC_NATT_F_OAR;
5873 				/* Calculate checksum delta */
5874 				addr = sav->sah->saidx.dst.sin.sin_addr.s_addr;
5875 				cksum = in_addword(cksum, ~addr >> 16);
5876 				cksum = in_addword(cksum, ~addr & 0xffff);
5877 				addr = sav->natt->oar.sin.sin_addr.s_addr;
5878 				cksum = in_addword(cksum, addr >> 16);
5879 				cksum = in_addword(cksum, addr & 0xffff);
5880 			}
5881 		}
5882 		sav->natt->cksum = cksum;
5883 	}
5884 	return (0);
5885 }
5886 
5887 static int
5888 key_setident(struct secashead *sah, const struct sadb_msghdr *mhp)
5889 {
5890 	const struct sadb_ident *idsrc, *iddst;
5891 
5892 	IPSEC_ASSERT(sah != NULL, ("null secashead"));
5893 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5894 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5895 
5896 	/* don't make buffer if not there */
5897 	if (SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_SRC) &&
5898 	    SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_DST)) {
5899 		sah->idents = NULL;
5900 		sah->identd = NULL;
5901 		return (0);
5902 	}
5903 
5904 	if (SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_SRC) ||
5905 	    SADB_CHECKHDR(mhp, SADB_EXT_IDENTITY_DST)) {
5906 		ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5907 		return (EINVAL);
5908 	}
5909 
5910 	idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5911 	iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5912 
5913 	/* validity check */
5914 	if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5915 		ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5916 		return EINVAL;
5917 	}
5918 
5919 	switch (idsrc->sadb_ident_type) {
5920 	case SADB_IDENTTYPE_PREFIX:
5921 	case SADB_IDENTTYPE_FQDN:
5922 	case SADB_IDENTTYPE_USERFQDN:
5923 	default:
5924 		/* XXX do nothing */
5925 		sah->idents = NULL;
5926 		sah->identd = NULL;
5927 	 	return 0;
5928 	}
5929 
5930 	/* make structure */
5931 	sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5932 	if (sah->idents == NULL) {
5933 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5934 		return ENOBUFS;
5935 	}
5936 	sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5937 	if (sah->identd == NULL) {
5938 		free(sah->idents, M_IPSEC_MISC);
5939 		sah->idents = NULL;
5940 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5941 		return ENOBUFS;
5942 	}
5943 	sah->idents->type = idsrc->sadb_ident_type;
5944 	sah->idents->id = idsrc->sadb_ident_id;
5945 
5946 	sah->identd->type = iddst->sadb_ident_type;
5947 	sah->identd->id = iddst->sadb_ident_id;
5948 
5949 	return 0;
5950 }
5951 
5952 /*
5953  * m will not be freed on return.
5954  * it is caller's responsibility to free the result.
5955  *
5956  * Called from SADB_ADD and SADB_UPDATE. Reply will contain headers
5957  * from the request in defined order.
5958  */
5959 static struct mbuf *
5960 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
5961 {
5962 	struct mbuf *n;
5963 
5964 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
5965 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5966 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5967 
5968 	/* create new sadb_msg to reply. */
5969 	n = key_gather_mbuf(m, mhp, 1, 16, SADB_EXT_RESERVED,
5970 	    SADB_EXT_SA, SADB_X_EXT_SA2,
5971 	    SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5972 	    SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5973 	    SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST,
5974 	    SADB_X_EXT_NAT_T_TYPE, SADB_X_EXT_NAT_T_SPORT,
5975 	    SADB_X_EXT_NAT_T_DPORT, SADB_X_EXT_NAT_T_OAI,
5976 	    SADB_X_EXT_NAT_T_OAR, SADB_X_EXT_NEW_ADDRESS_SRC,
5977 	    SADB_X_EXT_NEW_ADDRESS_DST);
5978 	if (!n)
5979 		return NULL;
5980 
5981 	if (n->m_len < sizeof(struct sadb_msg)) {
5982 		n = m_pullup(n, sizeof(struct sadb_msg));
5983 		if (n == NULL)
5984 			return NULL;
5985 	}
5986 	mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5987 	mtod(n, struct sadb_msg *)->sadb_msg_len =
5988 	    PFKEY_UNIT64(n->m_pkthdr.len);
5989 
5990 	return n;
5991 }
5992 
5993 /*
5994  * SADB_DELETE processing
5995  * receive
5996  *   <base, SA(*), address(SD)>
5997  * from the ikmpd, and set SADB_SASTATE_DEAD,
5998  * and send,
5999  *   <base, SA(*), address(SD)>
6000  * to the ikmpd.
6001  *
6002  * m will always be freed.
6003  */
6004 static int
6005 key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6006 {
6007 	struct secasindex saidx;
6008 	struct sadb_address *src0, *dst0;
6009 	struct secasvar *sav;
6010 	struct sadb_sa *sa0;
6011 	uint8_t proto;
6012 
6013 	IPSEC_ASSERT(so != NULL, ("null socket"));
6014 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
6015 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6016 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6017 
6018 	/* map satype to proto */
6019 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6020 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6021 		    __func__));
6022 		return key_senderror(so, m, EINVAL);
6023 	}
6024 
6025 	if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
6026 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
6027 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
6028 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
6029 		ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6030 		    __func__));
6031 		return key_senderror(so, m, EINVAL);
6032 	}
6033 
6034 	src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
6035 	dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
6036 
6037 	if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
6038 	    (struct sockaddr *)(dst0 + 1)) != 0) {
6039 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
6040 		return (key_senderror(so, m, EINVAL));
6041 	}
6042 	KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6043 	if (SADB_CHECKHDR(mhp, SADB_EXT_SA)) {
6044 		/*
6045 		 * Caller wants us to delete all non-LARVAL SAs
6046 		 * that match the src/dst.  This is used during
6047 		 * IKE INITIAL-CONTACT.
6048 		 * XXXAE: this looks like some extension to RFC2367.
6049 		 */
6050 		ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
6051 		return (key_delete_all(so, m, mhp, &saidx));
6052 	}
6053 	if (SADB_CHECKLEN(mhp, SADB_EXT_SA)) {
6054 		ipseclog((LOG_DEBUG,
6055 		    "%s: invalid message: wrong header size.\n", __func__));
6056 		return (key_senderror(so, m, EINVAL));
6057 	}
6058 	sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
6059 	if (proto == IPPROTO_TCP)
6060 		sav = key_getsav_tcpmd5(&saidx, NULL);
6061 	else
6062 		sav = key_getsavbyspi(sa0->sadb_sa_spi);
6063 	if (sav == NULL) {
6064 		ipseclog((LOG_DEBUG, "%s: no SA found for SPI %u.\n",
6065 		    __func__, ntohl(sa0->sadb_sa_spi)));
6066 		return (key_senderror(so, m, ESRCH));
6067 	}
6068 	if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_HEAD) == 0) {
6069 		ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u.\n",
6070 		    __func__, ntohl(sav->spi)));
6071 		key_freesav(&sav);
6072 		return (key_senderror(so, m, ESRCH));
6073 	}
6074 	KEYDBG(KEY_STAMP,
6075 	    printf("%s: SA(%p)\n", __func__, sav));
6076 	KEYDBG(KEY_DATA, kdebug_secasv(sav));
6077 	key_unlinksav(sav);
6078 	key_freesav(&sav);
6079 
6080     {
6081 	struct mbuf *n;
6082 	struct sadb_msg *newmsg;
6083 
6084 	/* create new sadb_msg to reply. */
6085 	n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
6086 	    SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
6087 	if (!n)
6088 		return key_senderror(so, m, ENOBUFS);
6089 
6090 	if (n->m_len < sizeof(struct sadb_msg)) {
6091 		n = m_pullup(n, sizeof(struct sadb_msg));
6092 		if (n == NULL)
6093 			return key_senderror(so, m, ENOBUFS);
6094 	}
6095 	newmsg = mtod(n, struct sadb_msg *);
6096 	newmsg->sadb_msg_errno = 0;
6097 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
6098 
6099 	m_freem(m);
6100 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
6101     }
6102 }
6103 
6104 /*
6105  * delete all SAs for src/dst.  Called from key_delete().
6106  */
6107 static int
6108 key_delete_all(struct socket *so, struct mbuf *m,
6109     const struct sadb_msghdr *mhp, struct secasindex *saidx)
6110 {
6111 	struct secasvar_queue drainq;
6112 	struct secashead *sah;
6113 	struct secasvar *sav, *nextsav;
6114 
6115 	TAILQ_INIT(&drainq);
6116 	SAHTREE_WLOCK();
6117 	LIST_FOREACH(sah, SAHADDRHASH_HASH(saidx), addrhash) {
6118 		if (key_cmpsaidx(&sah->saidx, saidx, CMP_HEAD) == 0)
6119 			continue;
6120 		/* Move all ALIVE SAs into drainq */
6121 		TAILQ_CONCAT(&drainq, &sah->savtree_alive, chain);
6122 	}
6123 	/* Unlink all queued SAs from SPI hash */
6124 	TAILQ_FOREACH(sav, &drainq, chain) {
6125 		sav->state = SADB_SASTATE_DEAD;
6126 		LIST_REMOVE(sav, spihash);
6127 	}
6128 	SAHTREE_WUNLOCK();
6129 	/* Now we can release reference for all SAs in drainq */
6130 	sav = TAILQ_FIRST(&drainq);
6131 	while (sav != NULL) {
6132 		KEYDBG(KEY_STAMP,
6133 		    printf("%s: SA(%p)\n", __func__, sav));
6134 		KEYDBG(KEY_DATA, kdebug_secasv(sav));
6135 		nextsav = TAILQ_NEXT(sav, chain);
6136 		key_freesah(&sav->sah); /* release reference from SAV */
6137 		key_freesav(&sav); /* release last reference */
6138 		sav = nextsav;
6139 	}
6140 
6141     {
6142 	struct mbuf *n;
6143 	struct sadb_msg *newmsg;
6144 
6145 	/* create new sadb_msg to reply. */
6146 	n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
6147 	    SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
6148 	if (!n)
6149 		return key_senderror(so, m, ENOBUFS);
6150 
6151 	if (n->m_len < sizeof(struct sadb_msg)) {
6152 		n = m_pullup(n, sizeof(struct sadb_msg));
6153 		if (n == NULL)
6154 			return key_senderror(so, m, ENOBUFS);
6155 	}
6156 	newmsg = mtod(n, struct sadb_msg *);
6157 	newmsg->sadb_msg_errno = 0;
6158 	newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
6159 
6160 	m_freem(m);
6161 	return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
6162     }
6163 }
6164 
6165 /*
6166  * Delete all alive SAs for corresponding xform.
6167  * Larval SAs have not initialized tdb_xform, so it is safe to leave them
6168  * here when xform disappears.
6169  */
6170 static void
6171 key_delete_xform(const struct xformsw *xsp)
6172 {
6173 	struct secasvar_queue drainq;
6174 	struct secashead *sah;
6175 	struct secasvar *sav, *nextsav;
6176 
6177 	TAILQ_INIT(&drainq);
6178 	SAHTREE_WLOCK();
6179 	TAILQ_FOREACH(sah, &V_sahtree, chain) {
6180 		sav = TAILQ_FIRST(&sah->savtree_alive);
6181 		if (sav == NULL)
6182 			continue;
6183 		if (sav->tdb_xform != xsp)
6184 			continue;
6185 		/*
6186 		 * It is supposed that all SAs in the chain are related to
6187 		 * one xform.
6188 		 */
6189 		TAILQ_CONCAT(&drainq, &sah->savtree_alive, chain);
6190 	}
6191 	/* Unlink all queued SAs from SPI hash */
6192 	TAILQ_FOREACH(sav, &drainq, chain) {
6193 		sav->state = SADB_SASTATE_DEAD;
6194 		LIST_REMOVE(sav, spihash);
6195 	}
6196 	SAHTREE_WUNLOCK();
6197 
6198 	/* Now we can release reference for all SAs in drainq */
6199 	sav = TAILQ_FIRST(&drainq);
6200 	while (sav != NULL) {
6201 		KEYDBG(KEY_STAMP,
6202 		    printf("%s: SA(%p)\n", __func__, sav));
6203 		KEYDBG(KEY_DATA, kdebug_secasv(sav));
6204 		nextsav = TAILQ_NEXT(sav, chain);
6205 		key_freesah(&sav->sah); /* release reference from SAV */
6206 		key_freesav(&sav); /* release last reference */
6207 		sav = nextsav;
6208 	}
6209 }
6210 
6211 /*
6212  * SADB_GET processing
6213  * receive
6214  *   <base, SA(*), address(SD)>
6215  * from the ikmpd, and get a SP and a SA to respond,
6216  * and send,
6217  *   <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
6218  *       (identity(SD),) (sensitivity)>
6219  * to the ikmpd.
6220  *
6221  * m will always be freed.
6222  */
6223 static int
6224 key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6225 {
6226 	struct secasindex saidx;
6227 	struct sadb_address *src0, *dst0;
6228 	struct sadb_sa *sa0;
6229 	struct secasvar *sav;
6230 	uint8_t proto;
6231 
6232 	IPSEC_ASSERT(so != NULL, ("null socket"));
6233 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
6234 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6235 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6236 
6237 	/* map satype to proto */
6238 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6239 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6240 			__func__));
6241 		return key_senderror(so, m, EINVAL);
6242 	}
6243 
6244 	if (SADB_CHECKHDR(mhp, SADB_EXT_SA) ||
6245 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
6246 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST)) {
6247 		ipseclog((LOG_DEBUG,
6248 		    "%s: invalid message: missing required header.\n",
6249 		    __func__));
6250 		return key_senderror(so, m, EINVAL);
6251 	}
6252 	if (SADB_CHECKLEN(mhp, SADB_EXT_SA) ||
6253 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
6254 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST)) {
6255 		ipseclog((LOG_DEBUG,
6256 		    "%s: invalid message: wrong header size.\n", __func__));
6257 		return key_senderror(so, m, EINVAL);
6258 	}
6259 
6260 	sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
6261 	src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6262 	dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6263 
6264 	if (key_checksockaddrs((struct sockaddr *)(src0 + 1),
6265 	    (struct sockaddr *)(dst0 + 1)) != 0) {
6266 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
6267 		return key_senderror(so, m, EINVAL);
6268 	}
6269 	KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6270 
6271 	if (proto == IPPROTO_TCP)
6272 		sav = key_getsav_tcpmd5(&saidx, NULL);
6273 	else
6274 		sav = key_getsavbyspi(sa0->sadb_sa_spi);
6275 	if (sav == NULL) {
6276 		ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
6277 		return key_senderror(so, m, ESRCH);
6278 	}
6279 	if (key_cmpsaidx(&sav->sah->saidx, &saidx, CMP_HEAD) == 0) {
6280 		ipseclog((LOG_DEBUG, "%s: saidx mismatched for SPI %u.\n",
6281 		    __func__, ntohl(sa0->sadb_sa_spi)));
6282 		key_freesav(&sav);
6283 		return (key_senderror(so, m, ESRCH));
6284 	}
6285 
6286     {
6287 	struct mbuf *n;
6288 	uint8_t satype;
6289 
6290 	/* map proto to satype */
6291 	if ((satype = key_proto2satype(sav->sah->saidx.proto)) == 0) {
6292 		ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
6293 		    __func__));
6294 		key_freesav(&sav);
6295 		return key_senderror(so, m, EINVAL);
6296 	}
6297 
6298 	/* create new sadb_msg to reply. */
6299 	n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
6300 	    mhp->msg->sadb_msg_pid);
6301 
6302 	key_freesav(&sav);
6303 	if (!n)
6304 		return key_senderror(so, m, ENOBUFS);
6305 
6306 	m_freem(m);
6307 	return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6308     }
6309 }
6310 
6311 /* XXX make it sysctl-configurable? */
6312 static void
6313 key_getcomb_setlifetime(struct sadb_comb *comb)
6314 {
6315 
6316 	comb->sadb_comb_soft_allocations = 1;
6317 	comb->sadb_comb_hard_allocations = 1;
6318 	comb->sadb_comb_soft_bytes = 0;
6319 	comb->sadb_comb_hard_bytes = 0;
6320 	comb->sadb_comb_hard_addtime = 86400;	/* 1 day */
6321 	comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
6322 	comb->sadb_comb_soft_usetime = 28800;	/* 8 hours */
6323 	comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6324 }
6325 
6326 /*
6327  * XXX reorder combinations by preference
6328  * XXX no idea if the user wants ESP authentication or not
6329  */
6330 static struct mbuf *
6331 key_getcomb_ealg(void)
6332 {
6333 	struct sadb_comb *comb;
6334 	const struct enc_xform *algo;
6335 	struct mbuf *result = NULL, *m, *n;
6336 	int encmin;
6337 	int i, off, o;
6338 	int totlen;
6339 	const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6340 
6341 	m = NULL;
6342 	for (i = 1; i <= SADB_EALG_MAX; i++) {
6343 		algo = enc_algorithm_lookup(i);
6344 		if (algo == NULL)
6345 			continue;
6346 
6347 		/* discard algorithms with key size smaller than system min */
6348 		if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6349 			continue;
6350 		if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6351 			encmin = V_ipsec_esp_keymin;
6352 		else
6353 			encmin = _BITS(algo->minkey);
6354 
6355 		if (V_ipsec_esp_auth)
6356 			m = key_getcomb_ah();
6357 		else {
6358 			IPSEC_ASSERT(l <= MLEN,
6359 				("l=%u > MLEN=%lu", l, (u_long) MLEN));
6360 			MGET(m, M_NOWAIT, MT_DATA);
6361 			if (m) {
6362 				M_ALIGN(m, l);
6363 				m->m_len = l;
6364 				m->m_next = NULL;
6365 				bzero(mtod(m, caddr_t), m->m_len);
6366 			}
6367 		}
6368 		if (!m)
6369 			goto fail;
6370 
6371 		totlen = 0;
6372 		for (n = m; n; n = n->m_next)
6373 			totlen += n->m_len;
6374 		IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6375 
6376 		for (off = 0; off < totlen; off += l) {
6377 			n = m_pulldown(m, off, l, &o);
6378 			if (!n) {
6379 				/* m is already freed */
6380 				goto fail;
6381 			}
6382 			comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6383 			bzero(comb, sizeof(*comb));
6384 			key_getcomb_setlifetime(comb);
6385 			comb->sadb_comb_encrypt = i;
6386 			comb->sadb_comb_encrypt_minbits = encmin;
6387 			comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6388 		}
6389 
6390 		if (!result)
6391 			result = m;
6392 		else
6393 			m_cat(result, m);
6394 	}
6395 
6396 	return result;
6397 
6398  fail:
6399 	if (result)
6400 		m_freem(result);
6401 	return NULL;
6402 }
6403 
6404 static void
6405 key_getsizes_ah(const struct auth_hash *ah, int alg, u_int16_t* min,
6406     u_int16_t* max)
6407 {
6408 
6409 	*min = *max = ah->hashsize;
6410 	if (ah->keysize == 0) {
6411 		/*
6412 		 * Transform takes arbitrary key size but algorithm
6413 		 * key size is restricted.  Enforce this here.
6414 		 */
6415 		switch (alg) {
6416 		case SADB_X_AALG_MD5:	*min = *max = 16; break;
6417 		case SADB_X_AALG_SHA:	*min = *max = 20; break;
6418 		case SADB_X_AALG_NULL:	*min = 1; *max = 256; break;
6419 		case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
6420 		case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
6421 		case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
6422 		default:
6423 			DPRINTF(("%s: unknown AH algorithm %u\n",
6424 				__func__, alg));
6425 			break;
6426 		}
6427 	}
6428 }
6429 
6430 /*
6431  * XXX reorder combinations by preference
6432  */
6433 static struct mbuf *
6434 key_getcomb_ah()
6435 {
6436 	const struct auth_hash *algo;
6437 	struct sadb_comb *comb;
6438 	struct mbuf *m;
6439 	u_int16_t minkeysize, maxkeysize;
6440 	int i;
6441 	const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6442 
6443 	m = NULL;
6444 	for (i = 1; i <= SADB_AALG_MAX; i++) {
6445 #if 1
6446 		/* we prefer HMAC algorithms, not old algorithms */
6447 		if (i != SADB_AALG_SHA1HMAC &&
6448 		    i != SADB_AALG_MD5HMAC  &&
6449 		    i != SADB_X_AALG_SHA2_256 &&
6450 		    i != SADB_X_AALG_SHA2_384 &&
6451 		    i != SADB_X_AALG_SHA2_512)
6452 			continue;
6453 #endif
6454 		algo = auth_algorithm_lookup(i);
6455 		if (!algo)
6456 			continue;
6457 		key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6458 		/* discard algorithms with key size smaller than system min */
6459 		if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6460 			continue;
6461 
6462 		if (!m) {
6463 			IPSEC_ASSERT(l <= MLEN,
6464 				("l=%u > MLEN=%lu", l, (u_long) MLEN));
6465 			MGET(m, M_NOWAIT, MT_DATA);
6466 			if (m) {
6467 				M_ALIGN(m, l);
6468 				m->m_len = l;
6469 				m->m_next = NULL;
6470 			}
6471 		} else
6472 			M_PREPEND(m, l, M_NOWAIT);
6473 		if (!m)
6474 			return NULL;
6475 
6476 		comb = mtod(m, struct sadb_comb *);
6477 		bzero(comb, sizeof(*comb));
6478 		key_getcomb_setlifetime(comb);
6479 		comb->sadb_comb_auth = i;
6480 		comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6481 		comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6482 	}
6483 
6484 	return m;
6485 }
6486 
6487 /*
6488  * not really an official behavior.  discussed in pf_key@inner.net in Sep2000.
6489  * XXX reorder combinations by preference
6490  */
6491 static struct mbuf *
6492 key_getcomb_ipcomp()
6493 {
6494 	const struct comp_algo *algo;
6495 	struct sadb_comb *comb;
6496 	struct mbuf *m;
6497 	int i;
6498 	const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6499 
6500 	m = NULL;
6501 	for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6502 		algo = comp_algorithm_lookup(i);
6503 		if (!algo)
6504 			continue;
6505 
6506 		if (!m) {
6507 			IPSEC_ASSERT(l <= MLEN,
6508 				("l=%u > MLEN=%lu", l, (u_long) MLEN));
6509 			MGET(m, M_NOWAIT, MT_DATA);
6510 			if (m) {
6511 				M_ALIGN(m, l);
6512 				m->m_len = l;
6513 				m->m_next = NULL;
6514 			}
6515 		} else
6516 			M_PREPEND(m, l, M_NOWAIT);
6517 		if (!m)
6518 			return NULL;
6519 
6520 		comb = mtod(m, struct sadb_comb *);
6521 		bzero(comb, sizeof(*comb));
6522 		key_getcomb_setlifetime(comb);
6523 		comb->sadb_comb_encrypt = i;
6524 		/* what should we set into sadb_comb_*_{min,max}bits? */
6525 	}
6526 
6527 	return m;
6528 }
6529 
6530 /*
6531  * XXX no way to pass mode (transport/tunnel) to userland
6532  * XXX replay checking?
6533  * XXX sysctl interface to ipsec_{ah,esp}_keymin
6534  */
6535 static struct mbuf *
6536 key_getprop(const struct secasindex *saidx)
6537 {
6538 	struct sadb_prop *prop;
6539 	struct mbuf *m, *n;
6540 	const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6541 	int totlen;
6542 
6543 	switch (saidx->proto)  {
6544 	case IPPROTO_ESP:
6545 		m = key_getcomb_ealg();
6546 		break;
6547 	case IPPROTO_AH:
6548 		m = key_getcomb_ah();
6549 		break;
6550 	case IPPROTO_IPCOMP:
6551 		m = key_getcomb_ipcomp();
6552 		break;
6553 	default:
6554 		return NULL;
6555 	}
6556 
6557 	if (!m)
6558 		return NULL;
6559 	M_PREPEND(m, l, M_NOWAIT);
6560 	if (!m)
6561 		return NULL;
6562 
6563 	totlen = 0;
6564 	for (n = m; n; n = n->m_next)
6565 		totlen += n->m_len;
6566 
6567 	prop = mtod(m, struct sadb_prop *);
6568 	bzero(prop, sizeof(*prop));
6569 	prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6570 	prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6571 	prop->sadb_prop_replay = 32;	/* XXX */
6572 
6573 	return m;
6574 }
6575 
6576 /*
6577  * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6578  * send
6579  *   <base, SA, address(SD), (address(P)), x_policy,
6580  *       (identity(SD),) (sensitivity,) proposal>
6581  * to KMD, and expect to receive
6582  *   <base> with SADB_ACQUIRE if error occurred,
6583  * or
6584  *   <base, src address, dst address, (SPI range)> with SADB_GETSPI
6585  * from KMD by PF_KEY.
6586  *
6587  * XXX x_policy is outside of RFC2367 (KAME extension).
6588  * XXX sensitivity is not supported.
6589  * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6590  * see comment for key_getcomb_ipcomp().
6591  *
6592  * OUT:
6593  *    0     : succeed
6594  *    others: error number
6595  */
6596 static int
6597 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6598 {
6599 	union sockaddr_union addr;
6600 	struct mbuf *result, *m;
6601 	uint32_t seq;
6602 	int error;
6603 	uint16_t ul_proto;
6604 	uint8_t mask, satype;
6605 
6606 	IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6607 	satype = key_proto2satype(saidx->proto);
6608 	IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6609 
6610 	error = -1;
6611 	result = NULL;
6612 	ul_proto = IPSEC_ULPROTO_ANY;
6613 
6614 	/* Get seq number to check whether sending message or not. */
6615 	seq = key_getacq(saidx, &error);
6616 	if (seq == 0)
6617 		return (error);
6618 
6619 	m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6620 	if (!m) {
6621 		error = ENOBUFS;
6622 		goto fail;
6623 	}
6624 	result = m;
6625 
6626 	/*
6627 	 * set sadb_address for saidx's.
6628 	 *
6629 	 * Note that if sp is supplied, then we're being called from
6630 	 * key_allocsa_policy() and should supply port and protocol
6631 	 * information.
6632 	 * XXXAE: why only TCP and UDP? ICMP and SCTP looks applicable too.
6633 	 * XXXAE: probably we can handle this in the ipsec[46]_allocsa().
6634 	 * XXXAE: it looks like we should save this info in the ACQ entry.
6635 	 */
6636 	if (sp != NULL && (sp->spidx.ul_proto == IPPROTO_TCP ||
6637 	    sp->spidx.ul_proto == IPPROTO_UDP))
6638 		ul_proto = sp->spidx.ul_proto;
6639 
6640 	addr = saidx->src;
6641 	mask = FULLMASK;
6642 	if (ul_proto != IPSEC_ULPROTO_ANY) {
6643 		switch (sp->spidx.src.sa.sa_family) {
6644 		case AF_INET:
6645 			if (sp->spidx.src.sin.sin_port != IPSEC_PORT_ANY) {
6646 				addr.sin.sin_port = sp->spidx.src.sin.sin_port;
6647 				mask = sp->spidx.prefs;
6648 			}
6649 			break;
6650 		case AF_INET6:
6651 			if (sp->spidx.src.sin6.sin6_port != IPSEC_PORT_ANY) {
6652 				addr.sin6.sin6_port =
6653 				    sp->spidx.src.sin6.sin6_port;
6654 				mask = sp->spidx.prefs;
6655 			}
6656 			break;
6657 		default:
6658 			break;
6659 		}
6660 	}
6661 	m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &addr.sa, mask, ul_proto);
6662 	if (!m) {
6663 		error = ENOBUFS;
6664 		goto fail;
6665 	}
6666 	m_cat(result, m);
6667 
6668 	addr = saidx->dst;
6669 	mask = FULLMASK;
6670 	if (ul_proto != IPSEC_ULPROTO_ANY) {
6671 		switch (sp->spidx.dst.sa.sa_family) {
6672 		case AF_INET:
6673 			if (sp->spidx.dst.sin.sin_port != IPSEC_PORT_ANY) {
6674 				addr.sin.sin_port = sp->spidx.dst.sin.sin_port;
6675 				mask = sp->spidx.prefd;
6676 			}
6677 			break;
6678 		case AF_INET6:
6679 			if (sp->spidx.dst.sin6.sin6_port != IPSEC_PORT_ANY) {
6680 				addr.sin6.sin6_port =
6681 				    sp->spidx.dst.sin6.sin6_port;
6682 				mask = sp->spidx.prefd;
6683 			}
6684 			break;
6685 		default:
6686 			break;
6687 		}
6688 	}
6689 	m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &addr.sa, mask, ul_proto);
6690 	if (!m) {
6691 		error = ENOBUFS;
6692 		goto fail;
6693 	}
6694 	m_cat(result, m);
6695 
6696 	/* XXX proxy address (optional) */
6697 
6698 	/* set sadb_x_policy */
6699 	if (sp != NULL) {
6700 		m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id,
6701 		    sp->priority);
6702 		if (!m) {
6703 			error = ENOBUFS;
6704 			goto fail;
6705 		}
6706 		m_cat(result, m);
6707 	}
6708 
6709 	/* XXX identity (optional) */
6710 #if 0
6711 	if (idexttype && fqdn) {
6712 		/* create identity extension (FQDN) */
6713 		struct sadb_ident *id;
6714 		int fqdnlen;
6715 
6716 		fqdnlen = strlen(fqdn) + 1;	/* +1 for terminating-NUL */
6717 		id = (struct sadb_ident *)p;
6718 		bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6719 		id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6720 		id->sadb_ident_exttype = idexttype;
6721 		id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6722 		bcopy(fqdn, id + 1, fqdnlen);
6723 		p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6724 	}
6725 
6726 	if (idexttype) {
6727 		/* create identity extension (USERFQDN) */
6728 		struct sadb_ident *id;
6729 		int userfqdnlen;
6730 
6731 		if (userfqdn) {
6732 			/* +1 for terminating-NUL */
6733 			userfqdnlen = strlen(userfqdn) + 1;
6734 		} else
6735 			userfqdnlen = 0;
6736 		id = (struct sadb_ident *)p;
6737 		bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6738 		id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6739 		id->sadb_ident_exttype = idexttype;
6740 		id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6741 		/* XXX is it correct? */
6742 		if (curproc && curproc->p_cred)
6743 			id->sadb_ident_id = curproc->p_cred->p_ruid;
6744 		if (userfqdn && userfqdnlen)
6745 			bcopy(userfqdn, id + 1, userfqdnlen);
6746 		p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6747 	}
6748 #endif
6749 
6750 	/* XXX sensitivity (optional) */
6751 
6752 	/* create proposal/combination extension */
6753 	m = key_getprop(saidx);
6754 #if 0
6755 	/*
6756 	 * spec conformant: always attach proposal/combination extension,
6757 	 * the problem is that we have no way to attach it for ipcomp,
6758 	 * due to the way sadb_comb is declared in RFC2367.
6759 	 */
6760 	if (!m) {
6761 		error = ENOBUFS;
6762 		goto fail;
6763 	}
6764 	m_cat(result, m);
6765 #else
6766 	/*
6767 	 * outside of spec; make proposal/combination extension optional.
6768 	 */
6769 	if (m)
6770 		m_cat(result, m);
6771 #endif
6772 
6773 	if ((result->m_flags & M_PKTHDR) == 0) {
6774 		error = EINVAL;
6775 		goto fail;
6776 	}
6777 
6778 	if (result->m_len < sizeof(struct sadb_msg)) {
6779 		result = m_pullup(result, sizeof(struct sadb_msg));
6780 		if (result == NULL) {
6781 			error = ENOBUFS;
6782 			goto fail;
6783 		}
6784 	}
6785 
6786 	result->m_pkthdr.len = 0;
6787 	for (m = result; m; m = m->m_next)
6788 		result->m_pkthdr.len += m->m_len;
6789 
6790 	mtod(result, struct sadb_msg *)->sadb_msg_len =
6791 	    PFKEY_UNIT64(result->m_pkthdr.len);
6792 
6793 	KEYDBG(KEY_STAMP,
6794 	    printf("%s: SP(%p)\n", __func__, sp));
6795 	KEYDBG(KEY_DATA, kdebug_secasindex(saidx, NULL));
6796 
6797 	return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6798 
6799  fail:
6800 	if (result)
6801 		m_freem(result);
6802 	return error;
6803 }
6804 
6805 static uint32_t
6806 key_newacq(const struct secasindex *saidx, int *perror)
6807 {
6808 	struct secacq *acq;
6809 	uint32_t seq;
6810 
6811 	acq = malloc(sizeof(*acq), M_IPSEC_SAQ, M_NOWAIT | M_ZERO);
6812 	if (acq == NULL) {
6813 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6814 		*perror = ENOBUFS;
6815 		return (0);
6816 	}
6817 
6818 	/* copy secindex */
6819 	bcopy(saidx, &acq->saidx, sizeof(acq->saidx));
6820 	acq->created = time_second;
6821 	acq->count = 0;
6822 
6823 	/* add to acqtree */
6824 	ACQ_LOCK();
6825 	seq = acq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6826 	LIST_INSERT_HEAD(&V_acqtree, acq, chain);
6827 	LIST_INSERT_HEAD(ACQADDRHASH_HASH(saidx), acq, addrhash);
6828 	LIST_INSERT_HEAD(ACQSEQHASH_HASH(seq), acq, seqhash);
6829 	ACQ_UNLOCK();
6830 	*perror = 0;
6831 	return (seq);
6832 }
6833 
6834 static uint32_t
6835 key_getacq(const struct secasindex *saidx, int *perror)
6836 {
6837 	struct secacq *acq;
6838 	uint32_t seq;
6839 
6840 	ACQ_LOCK();
6841 	LIST_FOREACH(acq, ACQADDRHASH_HASH(saidx), addrhash) {
6842 		if (key_cmpsaidx(&acq->saidx, saidx, CMP_EXACTLY)) {
6843 			if (acq->count > V_key_blockacq_count) {
6844 				/*
6845 				 * Reset counter and send message.
6846 				 * Also reset created time to keep ACQ for
6847 				 * this saidx.
6848 				 */
6849 				acq->created = time_second;
6850 				acq->count = 0;
6851 				seq = acq->seq;
6852 			} else {
6853 				/*
6854 				 * Increment counter and do nothing.
6855 				 * We send SADB_ACQUIRE message only
6856 				 * for each V_key_blockacq_count packet.
6857 				 */
6858 				acq->count++;
6859 				seq = 0;
6860 			}
6861 			break;
6862 		}
6863 	}
6864 	ACQ_UNLOCK();
6865 	if (acq != NULL) {
6866 		*perror = 0;
6867 		return (seq);
6868 	}
6869 	/* allocate new  entry */
6870 	return (key_newacq(saidx, perror));
6871 }
6872 
6873 static int
6874 key_acqreset(uint32_t seq)
6875 {
6876 	struct secacq *acq;
6877 
6878 	ACQ_LOCK();
6879 	LIST_FOREACH(acq, ACQSEQHASH_HASH(seq), seqhash) {
6880 		if (acq->seq == seq) {
6881 			acq->count = 0;
6882 			acq->created = time_second;
6883 			break;
6884 		}
6885 	}
6886 	ACQ_UNLOCK();
6887 	if (acq == NULL)
6888 		return (ESRCH);
6889 	return (0);
6890 }
6891 /*
6892  * Mark ACQ entry as stale to remove it in key_flush_acq().
6893  * Called after successful SADB_GETSPI message.
6894  */
6895 static int
6896 key_acqdone(const struct secasindex *saidx, uint32_t seq)
6897 {
6898 	struct secacq *acq;
6899 
6900 	ACQ_LOCK();
6901 	LIST_FOREACH(acq, ACQSEQHASH_HASH(seq), seqhash) {
6902 		if (acq->seq == seq)
6903 			break;
6904 	}
6905 	if (acq != NULL) {
6906 		if (key_cmpsaidx(&acq->saidx, saidx, CMP_EXACTLY) == 0) {
6907 			ipseclog((LOG_DEBUG,
6908 			    "%s: Mismatched saidx for ACQ %u", __func__, seq));
6909 			acq = NULL;
6910 		} else {
6911 			acq->created = 0;
6912 		}
6913 	} else {
6914 		ipseclog((LOG_DEBUG,
6915 		    "%s: ACQ %u is not found.", __func__, seq));
6916 	}
6917 	ACQ_UNLOCK();
6918 	if (acq == NULL)
6919 		return (ESRCH);
6920 	return (0);
6921 }
6922 
6923 static struct secspacq *
6924 key_newspacq(struct secpolicyindex *spidx)
6925 {
6926 	struct secspacq *acq;
6927 
6928 	/* get new entry */
6929 	acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6930 	if (acq == NULL) {
6931 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6932 		return NULL;
6933 	}
6934 
6935 	/* copy secindex */
6936 	bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6937 	acq->created = time_second;
6938 	acq->count = 0;
6939 
6940 	/* add to spacqtree */
6941 	SPACQ_LOCK();
6942 	LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6943 	SPACQ_UNLOCK();
6944 
6945 	return acq;
6946 }
6947 
6948 static struct secspacq *
6949 key_getspacq(struct secpolicyindex *spidx)
6950 {
6951 	struct secspacq *acq;
6952 
6953 	SPACQ_LOCK();
6954 	LIST_FOREACH(acq, &V_spacqtree, chain) {
6955 		if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6956 			/* NB: return holding spacq_lock */
6957 			return acq;
6958 		}
6959 	}
6960 	SPACQ_UNLOCK();
6961 
6962 	return NULL;
6963 }
6964 
6965 /*
6966  * SADB_ACQUIRE processing,
6967  * in first situation, is receiving
6968  *   <base>
6969  * from the ikmpd, and clear sequence of its secasvar entry.
6970  *
6971  * In second situation, is receiving
6972  *   <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6973  * from a user land process, and return
6974  *   <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6975  * to the socket.
6976  *
6977  * m will always be freed.
6978  */
6979 static int
6980 key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6981 {
6982 	SAHTREE_RLOCK_TRACKER;
6983 	struct sadb_address *src0, *dst0;
6984 	struct secasindex saidx;
6985 	struct secashead *sah;
6986 	uint32_t reqid;
6987 	int error;
6988 	uint8_t mode, proto;
6989 
6990 	IPSEC_ASSERT(so != NULL, ("null socket"));
6991 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
6992 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6993 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6994 
6995 	/*
6996 	 * Error message from KMd.
6997 	 * We assume that if error was occurred in IKEd, the length of PFKEY
6998 	 * message is equal to the size of sadb_msg structure.
6999 	 * We do not raise error even if error occurred in this function.
7000 	 */
7001 	if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
7002 		/* check sequence number */
7003 		if (mhp->msg->sadb_msg_seq == 0 ||
7004 		    mhp->msg->sadb_msg_errno == 0) {
7005 			ipseclog((LOG_DEBUG, "%s: must specify sequence "
7006 				"number and errno.\n", __func__));
7007 		} else {
7008 			/*
7009 			 * IKEd reported that error occurred.
7010 			 * XXXAE: what it expects from the kernel?
7011 			 * Probably we should send SADB_ACQUIRE again?
7012 			 * If so, reset ACQ's state.
7013 			 * XXXAE: it looks useless.
7014 			 */
7015 			key_acqreset(mhp->msg->sadb_msg_seq);
7016 		}
7017 		m_freem(m);
7018 		return (0);
7019 	}
7020 
7021 	/*
7022 	 * This message is from user land.
7023 	 */
7024 
7025 	/* map satype to proto */
7026 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7027 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7028 		    __func__));
7029 		return key_senderror(so, m, EINVAL);
7030 	}
7031 
7032 	if (SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_SRC) ||
7033 	    SADB_CHECKHDR(mhp, SADB_EXT_ADDRESS_DST) ||
7034 	    SADB_CHECKHDR(mhp, SADB_EXT_PROPOSAL)) {
7035 		ipseclog((LOG_DEBUG,
7036 		    "%s: invalid message: missing required header.\n",
7037 		    __func__));
7038 		return key_senderror(so, m, EINVAL);
7039 	}
7040 	if (SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_SRC) ||
7041 	    SADB_CHECKLEN(mhp, SADB_EXT_ADDRESS_DST) ||
7042 	    SADB_CHECKLEN(mhp, SADB_EXT_PROPOSAL)) {
7043 		ipseclog((LOG_DEBUG,
7044 		    "%s: invalid message: wrong header size.\n", __func__));
7045 		return key_senderror(so, m, EINVAL);
7046 	}
7047 
7048 	if (SADB_CHECKHDR(mhp, SADB_X_EXT_SA2)) {
7049 		mode = IPSEC_MODE_ANY;
7050 		reqid = 0;
7051 	} else {
7052 		if (SADB_CHECKLEN(mhp, SADB_X_EXT_SA2)) {
7053 			ipseclog((LOG_DEBUG,
7054 			    "%s: invalid message: wrong header size.\n",
7055 			    __func__));
7056 			return key_senderror(so, m, EINVAL);
7057 		}
7058 		mode = ((struct sadb_x_sa2 *)
7059 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
7060 		reqid = ((struct sadb_x_sa2 *)
7061 		    mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
7062 	}
7063 
7064 	src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
7065 	dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
7066 
7067 	error = key_checksockaddrs((struct sockaddr *)(src0 + 1),
7068 	    (struct sockaddr *)(dst0 + 1));
7069 	if (error != 0) {
7070 		ipseclog((LOG_DEBUG, "%s: invalid sockaddr.\n", __func__));
7071 		return key_senderror(so, m, EINVAL);
7072 	}
7073 	KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
7074 
7075 	/* get a SA index */
7076 	SAHTREE_RLOCK();
7077 	LIST_FOREACH(sah, SAHADDRHASH_HASH(&saidx), addrhash) {
7078 		if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
7079 			break;
7080 	}
7081 	SAHTREE_RUNLOCK();
7082 	if (sah != NULL) {
7083 		ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
7084 		return key_senderror(so, m, EEXIST);
7085 	}
7086 
7087 	error = key_acquire(&saidx, NULL);
7088 	if (error != 0) {
7089 		ipseclog((LOG_DEBUG,
7090 		    "%s: error %d returned from key_acquire()\n",
7091 			__func__, error));
7092 		return key_senderror(so, m, error);
7093 	}
7094 	m_freem(m);
7095 	return (0);
7096 }
7097 
7098 /*
7099  * SADB_REGISTER processing.
7100  * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
7101  * receive
7102  *   <base>
7103  * from the ikmpd, and register a socket to send PF_KEY messages,
7104  * and send
7105  *   <base, supported>
7106  * to KMD by PF_KEY.
7107  * If socket is detached, must free from regnode.
7108  *
7109  * m will always be freed.
7110  */
7111 static int
7112 key_register(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7113 {
7114 	struct secreg *reg, *newreg = NULL;
7115 
7116 	IPSEC_ASSERT(so != NULL, ("null socket"));
7117 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
7118 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7119 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7120 
7121 	/* check for invalid register message */
7122 	if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
7123 		return key_senderror(so, m, EINVAL);
7124 
7125 	/* When SATYPE_UNSPEC is specified, only return sabd_supported. */
7126 	if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
7127 		goto setmsg;
7128 
7129 	/* check whether existing or not */
7130 	REGTREE_LOCK();
7131 	LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
7132 		if (reg->so == so) {
7133 			REGTREE_UNLOCK();
7134 			ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
7135 				__func__));
7136 			return key_senderror(so, m, EEXIST);
7137 		}
7138 	}
7139 
7140 	/* create regnode */
7141 	newreg =  malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
7142 	if (newreg == NULL) {
7143 		REGTREE_UNLOCK();
7144 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7145 		return key_senderror(so, m, ENOBUFS);
7146 	}
7147 
7148 	newreg->so = so;
7149 	((struct keycb *)sotorawcb(so))->kp_registered++;
7150 
7151 	/* add regnode to regtree. */
7152 	LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
7153 	REGTREE_UNLOCK();
7154 
7155   setmsg:
7156     {
7157 	struct mbuf *n;
7158 	struct sadb_msg *newmsg;
7159 	struct sadb_supported *sup;
7160 	u_int len, alen, elen;
7161 	int off;
7162 	int i;
7163 	struct sadb_alg *alg;
7164 
7165 	/* create new sadb_msg to reply. */
7166 	alen = 0;
7167 	for (i = 1; i <= SADB_AALG_MAX; i++) {
7168 		if (auth_algorithm_lookup(i))
7169 			alen += sizeof(struct sadb_alg);
7170 	}
7171 	if (alen)
7172 		alen += sizeof(struct sadb_supported);
7173 	elen = 0;
7174 	for (i = 1; i <= SADB_EALG_MAX; i++) {
7175 		if (enc_algorithm_lookup(i))
7176 			elen += sizeof(struct sadb_alg);
7177 	}
7178 	if (elen)
7179 		elen += sizeof(struct sadb_supported);
7180 
7181 	len = sizeof(struct sadb_msg) + alen + elen;
7182 
7183 	if (len > MCLBYTES)
7184 		return key_senderror(so, m, ENOBUFS);
7185 
7186 	MGETHDR(n, M_NOWAIT, MT_DATA);
7187 	if (len > MHLEN) {
7188 		if (!(MCLGET(n, M_NOWAIT))) {
7189 			m_freem(n);
7190 			n = NULL;
7191 		}
7192 	}
7193 	if (!n)
7194 		return key_senderror(so, m, ENOBUFS);
7195 
7196 	n->m_pkthdr.len = n->m_len = len;
7197 	n->m_next = NULL;
7198 	off = 0;
7199 
7200 	m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
7201 	newmsg = mtod(n, struct sadb_msg *);
7202 	newmsg->sadb_msg_errno = 0;
7203 	newmsg->sadb_msg_len = PFKEY_UNIT64(len);
7204 	off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
7205 
7206 	/* for authentication algorithm */
7207 	if (alen) {
7208 		sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
7209 		sup->sadb_supported_len = PFKEY_UNIT64(alen);
7210 		sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
7211 		off += PFKEY_ALIGN8(sizeof(*sup));
7212 
7213 		for (i = 1; i <= SADB_AALG_MAX; i++) {
7214 			const struct auth_hash *aalgo;
7215 			u_int16_t minkeysize, maxkeysize;
7216 
7217 			aalgo = auth_algorithm_lookup(i);
7218 			if (!aalgo)
7219 				continue;
7220 			alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
7221 			alg->sadb_alg_id = i;
7222 			alg->sadb_alg_ivlen = 0;
7223 			key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
7224 			alg->sadb_alg_minbits = _BITS(minkeysize);
7225 			alg->sadb_alg_maxbits = _BITS(maxkeysize);
7226 			off += PFKEY_ALIGN8(sizeof(*alg));
7227 		}
7228 	}
7229 
7230 	/* for encryption algorithm */
7231 	if (elen) {
7232 		sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
7233 		sup->sadb_supported_len = PFKEY_UNIT64(elen);
7234 		sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
7235 		off += PFKEY_ALIGN8(sizeof(*sup));
7236 
7237 		for (i = 1; i <= SADB_EALG_MAX; i++) {
7238 			const struct enc_xform *ealgo;
7239 
7240 			ealgo = enc_algorithm_lookup(i);
7241 			if (!ealgo)
7242 				continue;
7243 			alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
7244 			alg->sadb_alg_id = i;
7245 			alg->sadb_alg_ivlen = ealgo->ivsize;
7246 			alg->sadb_alg_minbits = _BITS(ealgo->minkey);
7247 			alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
7248 			off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
7249 		}
7250 	}
7251 
7252 	IPSEC_ASSERT(off == len,
7253 		("length assumption failed (off %u len %u)", off, len));
7254 
7255 	m_freem(m);
7256 	return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
7257     }
7258 }
7259 
7260 /*
7261  * free secreg entry registered.
7262  * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
7263  */
7264 void
7265 key_freereg(struct socket *so)
7266 {
7267 	struct secreg *reg;
7268 	int i;
7269 
7270 	IPSEC_ASSERT(so != NULL, ("NULL so"));
7271 
7272 	/*
7273 	 * check whether existing or not.
7274 	 * check all type of SA, because there is a potential that
7275 	 * one socket is registered to multiple type of SA.
7276 	 */
7277 	REGTREE_LOCK();
7278 	for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7279 		LIST_FOREACH(reg, &V_regtree[i], chain) {
7280 			if (reg->so == so && __LIST_CHAINED(reg)) {
7281 				LIST_REMOVE(reg, chain);
7282 				free(reg, M_IPSEC_SAR);
7283 				break;
7284 			}
7285 		}
7286 	}
7287 	REGTREE_UNLOCK();
7288 }
7289 
7290 /*
7291  * SADB_EXPIRE processing
7292  * send
7293  *   <base, SA, SA2, lifetime(C and one of HS), address(SD)>
7294  * to KMD by PF_KEY.
7295  * NOTE: We send only soft lifetime extension.
7296  *
7297  * OUT:	0	: succeed
7298  *	others	: error number
7299  */
7300 static int
7301 key_expire(struct secasvar *sav, int hard)
7302 {
7303 	struct mbuf *result = NULL, *m;
7304 	struct sadb_lifetime *lt;
7305 	uint32_t replay_count;
7306 	int error, len;
7307 	uint8_t satype;
7308 
7309 	IPSEC_ASSERT (sav != NULL, ("null sav"));
7310 	IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
7311 
7312 	KEYDBG(KEY_STAMP,
7313 	    printf("%s: SA(%p) expired %s lifetime\n", __func__,
7314 		sav, hard ? "hard": "soft"));
7315 	KEYDBG(KEY_DATA, kdebug_secasv(sav));
7316 	/* set msg header */
7317 	satype = key_proto2satype(sav->sah->saidx.proto);
7318 	IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
7319 	m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
7320 	if (!m) {
7321 		error = ENOBUFS;
7322 		goto fail;
7323 	}
7324 	result = m;
7325 
7326 	/* create SA extension */
7327 	m = key_setsadbsa(sav);
7328 	if (!m) {
7329 		error = ENOBUFS;
7330 		goto fail;
7331 	}
7332 	m_cat(result, m);
7333 
7334 	/* create SA extension */
7335 	SECASVAR_LOCK(sav);
7336 	replay_count = sav->replay ? sav->replay->count : 0;
7337 	SECASVAR_UNLOCK(sav);
7338 
7339 	m = key_setsadbxsa2(sav->sah->saidx.mode, replay_count,
7340 			sav->sah->saidx.reqid);
7341 	if (!m) {
7342 		error = ENOBUFS;
7343 		goto fail;
7344 	}
7345 	m_cat(result, m);
7346 
7347 	if (sav->replay && sav->replay->wsize > UINT8_MAX) {
7348 		m = key_setsadbxsareplay(sav->replay->wsize);
7349 		if (!m) {
7350 			error = ENOBUFS;
7351 			goto fail;
7352 		}
7353 		m_cat(result, m);
7354 	}
7355 
7356 	/* create lifetime extension (current and soft) */
7357 	len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
7358 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7359 	if (m == NULL) {
7360 		error = ENOBUFS;
7361 		goto fail;
7362 	}
7363 	m_align(m, len);
7364 	m->m_len = len;
7365 	bzero(mtod(m, caddr_t), len);
7366 	lt = mtod(m, struct sadb_lifetime *);
7367 	lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
7368 	lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
7369 	lt->sadb_lifetime_allocations =
7370 	    (uint32_t)counter_u64_fetch(sav->lft_c_allocations);
7371 	lt->sadb_lifetime_bytes =
7372 	    counter_u64_fetch(sav->lft_c_bytes);
7373 	lt->sadb_lifetime_addtime = sav->created;
7374 	lt->sadb_lifetime_usetime = sav->firstused;
7375 	lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
7376 	lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
7377 	if (hard) {
7378 		lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
7379 		lt->sadb_lifetime_allocations = sav->lft_h->allocations;
7380 		lt->sadb_lifetime_bytes = sav->lft_h->bytes;
7381 		lt->sadb_lifetime_addtime = sav->lft_h->addtime;
7382 		lt->sadb_lifetime_usetime = sav->lft_h->usetime;
7383 	} else {
7384 		lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
7385 		lt->sadb_lifetime_allocations = sav->lft_s->allocations;
7386 		lt->sadb_lifetime_bytes = sav->lft_s->bytes;
7387 		lt->sadb_lifetime_addtime = sav->lft_s->addtime;
7388 		lt->sadb_lifetime_usetime = sav->lft_s->usetime;
7389 	}
7390 	m_cat(result, m);
7391 
7392 	/* set sadb_address for source */
7393 	m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
7394 	    &sav->sah->saidx.src.sa,
7395 	    FULLMASK, IPSEC_ULPROTO_ANY);
7396 	if (!m) {
7397 		error = ENOBUFS;
7398 		goto fail;
7399 	}
7400 	m_cat(result, m);
7401 
7402 	/* set sadb_address for destination */
7403 	m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
7404 	    &sav->sah->saidx.dst.sa,
7405 	    FULLMASK, IPSEC_ULPROTO_ANY);
7406 	if (!m) {
7407 		error = ENOBUFS;
7408 		goto fail;
7409 	}
7410 	m_cat(result, m);
7411 
7412 	/*
7413 	 * XXX-BZ Handle NAT-T extensions here.
7414 	 * XXXAE: it doesn't seem quite useful. IKEs should not depend on
7415 	 * this information, we report only significant SA fields.
7416 	 */
7417 
7418 	if ((result->m_flags & M_PKTHDR) == 0) {
7419 		error = EINVAL;
7420 		goto fail;
7421 	}
7422 
7423 	if (result->m_len < sizeof(struct sadb_msg)) {
7424 		result = m_pullup(result, sizeof(struct sadb_msg));
7425 		if (result == NULL) {
7426 			error = ENOBUFS;
7427 			goto fail;
7428 		}
7429 	}
7430 
7431 	result->m_pkthdr.len = 0;
7432 	for (m = result; m; m = m->m_next)
7433 		result->m_pkthdr.len += m->m_len;
7434 
7435 	mtod(result, struct sadb_msg *)->sadb_msg_len =
7436 	    PFKEY_UNIT64(result->m_pkthdr.len);
7437 
7438 	return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7439 
7440  fail:
7441 	if (result)
7442 		m_freem(result);
7443 	return error;
7444 }
7445 
7446 static void
7447 key_freesah_flushed(struct secashead_queue *flushq)
7448 {
7449 	struct secashead *sah, *nextsah;
7450 	struct secasvar *sav, *nextsav;
7451 
7452 	sah = TAILQ_FIRST(flushq);
7453 	while (sah != NULL) {
7454 		sav = TAILQ_FIRST(&sah->savtree_larval);
7455 		while (sav != NULL) {
7456 			nextsav = TAILQ_NEXT(sav, chain);
7457 			TAILQ_REMOVE(&sah->savtree_larval, sav, chain);
7458 			key_freesav(&sav); /* release last reference */
7459 			key_freesah(&sah); /* release reference from SAV */
7460 			sav = nextsav;
7461 		}
7462 		sav = TAILQ_FIRST(&sah->savtree_alive);
7463 		while (sav != NULL) {
7464 			nextsav = TAILQ_NEXT(sav, chain);
7465 			TAILQ_REMOVE(&sah->savtree_alive, sav, chain);
7466 			key_freesav(&sav); /* release last reference */
7467 			key_freesah(&sah); /* release reference from SAV */
7468 			sav = nextsav;
7469 		}
7470 		nextsah = TAILQ_NEXT(sah, chain);
7471 		key_freesah(&sah);	/* release last reference */
7472 		sah = nextsah;
7473 	}
7474 }
7475 
7476 /*
7477  * SADB_FLUSH processing
7478  * receive
7479  *   <base>
7480  * from the ikmpd, and free all entries in secastree.
7481  * and send,
7482  *   <base>
7483  * to the ikmpd.
7484  * NOTE: to do is only marking SADB_SASTATE_DEAD.
7485  *
7486  * m will always be freed.
7487  */
7488 static int
7489 key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7490 {
7491 	struct secashead_queue flushq;
7492 	struct sadb_msg *newmsg;
7493 	struct secashead *sah, *nextsah;
7494 	struct secasvar *sav;
7495 	uint8_t proto;
7496 	int i;
7497 
7498 	IPSEC_ASSERT(so != NULL, ("null socket"));
7499 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7500 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7501 
7502 	/* map satype to proto */
7503 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7504 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7505 			__func__));
7506 		return key_senderror(so, m, EINVAL);
7507 	}
7508 	KEYDBG(KEY_STAMP,
7509 	    printf("%s: proto %u\n", __func__, proto));
7510 
7511 	TAILQ_INIT(&flushq);
7512 	if (proto == IPSEC_PROTO_ANY) {
7513 		/* no SATYPE specified, i.e. flushing all SA. */
7514 		SAHTREE_WLOCK();
7515 		/* Move all SAHs into flushq */
7516 		TAILQ_CONCAT(&flushq, &V_sahtree, chain);
7517 		/* Flush all buckets in SPI hash */
7518 		for (i = 0; i < V_savhash_mask + 1; i++)
7519 			LIST_INIT(&V_savhashtbl[i]);
7520 		/* Flush all buckets in SAHADDRHASH */
7521 		for (i = 0; i < V_sahaddrhash_mask + 1; i++)
7522 			LIST_INIT(&V_sahaddrhashtbl[i]);
7523 		/* Mark all SAHs as unlinked */
7524 		TAILQ_FOREACH(sah, &flushq, chain) {
7525 			sah->state = SADB_SASTATE_DEAD;
7526 			/*
7527 			 * Callout handler makes its job using
7528 			 * RLOCK and drain queues. In case, when this
7529 			 * function will be called just before it
7530 			 * acquires WLOCK, we need to mark SAs as
7531 			 * unlinked to prevent second unlink.
7532 			 */
7533 			TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
7534 				sav->state = SADB_SASTATE_DEAD;
7535 			}
7536 			TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
7537 				sav->state = SADB_SASTATE_DEAD;
7538 			}
7539 		}
7540 		SAHTREE_WUNLOCK();
7541 	} else {
7542 		SAHTREE_WLOCK();
7543 		sah = TAILQ_FIRST(&V_sahtree);
7544 		while (sah != NULL) {
7545 			IPSEC_ASSERT(sah->state != SADB_SASTATE_DEAD,
7546 			    ("DEAD SAH %p in SADB_FLUSH", sah));
7547 			nextsah = TAILQ_NEXT(sah, chain);
7548 			if (sah->saidx.proto != proto) {
7549 				sah = nextsah;
7550 				continue;
7551 			}
7552 			sah->state = SADB_SASTATE_DEAD;
7553 			TAILQ_REMOVE(&V_sahtree, sah, chain);
7554 			LIST_REMOVE(sah, addrhash);
7555 			/* Unlink all SAs from SPI hash */
7556 			TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
7557 				LIST_REMOVE(sav, spihash);
7558 				sav->state = SADB_SASTATE_DEAD;
7559 			}
7560 			TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
7561 				LIST_REMOVE(sav, spihash);
7562 				sav->state = SADB_SASTATE_DEAD;
7563 			}
7564 			/* Add SAH into flushq */
7565 			TAILQ_INSERT_HEAD(&flushq, sah, chain);
7566 			sah = nextsah;
7567 		}
7568 		SAHTREE_WUNLOCK();
7569 	}
7570 
7571 	key_freesah_flushed(&flushq);
7572 	/* Free all queued SAs and SAHs */
7573 	if (m->m_len < sizeof(struct sadb_msg) ||
7574 	    sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7575 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7576 		return key_senderror(so, m, ENOBUFS);
7577 	}
7578 
7579 	if (m->m_next)
7580 		m_freem(m->m_next);
7581 	m->m_next = NULL;
7582 	m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7583 	newmsg = mtod(m, struct sadb_msg *);
7584 	newmsg->sadb_msg_errno = 0;
7585 	newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7586 
7587 	return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7588 }
7589 
7590 /*
7591  * SADB_DUMP processing
7592  * dump all entries including status of DEAD in SAD.
7593  * receive
7594  *   <base>
7595  * from the ikmpd, and dump all secasvar leaves
7596  * and send,
7597  *   <base> .....
7598  * to the ikmpd.
7599  *
7600  * m will always be freed.
7601  */
7602 static int
7603 key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7604 {
7605 	SAHTREE_RLOCK_TRACKER;
7606 	struct secashead *sah;
7607 	struct secasvar *sav;
7608 	struct mbuf *n;
7609 	uint32_t cnt;
7610 	uint8_t proto, satype;
7611 
7612 	IPSEC_ASSERT(so != NULL, ("null socket"));
7613 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
7614 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7615 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7616 
7617 	/* map satype to proto */
7618 	if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7619 		ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7620 		    __func__));
7621 		return key_senderror(so, m, EINVAL);
7622 	}
7623 
7624 	/* count sav entries to be sent to the userland. */
7625 	cnt = 0;
7626 	SAHTREE_RLOCK();
7627 	TAILQ_FOREACH(sah, &V_sahtree, chain) {
7628 		if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
7629 		    proto != sah->saidx.proto)
7630 			continue;
7631 
7632 		TAILQ_FOREACH(sav, &sah->savtree_larval, chain)
7633 			cnt++;
7634 		TAILQ_FOREACH(sav, &sah->savtree_alive, chain)
7635 			cnt++;
7636 	}
7637 
7638 	if (cnt == 0) {
7639 		SAHTREE_RUNLOCK();
7640 		return key_senderror(so, m, ENOENT);
7641 	}
7642 
7643 	/* send this to the userland, one at a time. */
7644 	TAILQ_FOREACH(sah, &V_sahtree, chain) {
7645 		if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
7646 		    proto != sah->saidx.proto)
7647 			continue;
7648 
7649 		/* map proto to satype */
7650 		if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7651 			SAHTREE_RUNLOCK();
7652 			ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7653 			    "SAD.\n", __func__));
7654 			return key_senderror(so, m, EINVAL);
7655 		}
7656 		TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
7657 			n = key_setdumpsa(sav, SADB_DUMP, satype,
7658 			    --cnt, mhp->msg->sadb_msg_pid);
7659 			if (n == NULL) {
7660 				SAHTREE_RUNLOCK();
7661 				return key_senderror(so, m, ENOBUFS);
7662 			}
7663 			key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7664 		}
7665 		TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
7666 			n = key_setdumpsa(sav, SADB_DUMP, satype,
7667 			    --cnt, mhp->msg->sadb_msg_pid);
7668 			if (n == NULL) {
7669 				SAHTREE_RUNLOCK();
7670 				return key_senderror(so, m, ENOBUFS);
7671 			}
7672 			key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7673 		}
7674 	}
7675 	SAHTREE_RUNLOCK();
7676 	m_freem(m);
7677 	return (0);
7678 }
7679 /*
7680  * SADB_X_PROMISC processing
7681  *
7682  * m will always be freed.
7683  */
7684 static int
7685 key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7686 {
7687 	int olen;
7688 
7689 	IPSEC_ASSERT(so != NULL, ("null socket"));
7690 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
7691 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7692 	IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7693 
7694 	olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7695 
7696 	if (olen < sizeof(struct sadb_msg)) {
7697 #if 1
7698 		return key_senderror(so, m, EINVAL);
7699 #else
7700 		m_freem(m);
7701 		return 0;
7702 #endif
7703 	} else if (olen == sizeof(struct sadb_msg)) {
7704 		/* enable/disable promisc mode */
7705 		struct keycb *kp;
7706 
7707 		if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7708 			return key_senderror(so, m, EINVAL);
7709 		mhp->msg->sadb_msg_errno = 0;
7710 		switch (mhp->msg->sadb_msg_satype) {
7711 		case 0:
7712 		case 1:
7713 			kp->kp_promisc = mhp->msg->sadb_msg_satype;
7714 			break;
7715 		default:
7716 			return key_senderror(so, m, EINVAL);
7717 		}
7718 
7719 		/* send the original message back to everyone */
7720 		mhp->msg->sadb_msg_errno = 0;
7721 		return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7722 	} else {
7723 		/* send packet as is */
7724 
7725 		m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7726 
7727 		/* TODO: if sadb_msg_seq is specified, send to specific pid */
7728 		return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7729 	}
7730 }
7731 
7732 static int (*key_typesw[])(struct socket *, struct mbuf *,
7733 		const struct sadb_msghdr *) = {
7734 	NULL,		/* SADB_RESERVED */
7735 	key_getspi,	/* SADB_GETSPI */
7736 	key_update,	/* SADB_UPDATE */
7737 	key_add,	/* SADB_ADD */
7738 	key_delete,	/* SADB_DELETE */
7739 	key_get,	/* SADB_GET */
7740 	key_acquire2,	/* SADB_ACQUIRE */
7741 	key_register,	/* SADB_REGISTER */
7742 	NULL,		/* SADB_EXPIRE */
7743 	key_flush,	/* SADB_FLUSH */
7744 	key_dump,	/* SADB_DUMP */
7745 	key_promisc,	/* SADB_X_PROMISC */
7746 	NULL,		/* SADB_X_PCHANGE */
7747 	key_spdadd,	/* SADB_X_SPDUPDATE */
7748 	key_spdadd,	/* SADB_X_SPDADD */
7749 	key_spddelete,	/* SADB_X_SPDDELETE */
7750 	key_spdget,	/* SADB_X_SPDGET */
7751 	NULL,		/* SADB_X_SPDACQUIRE */
7752 	key_spddump,	/* SADB_X_SPDDUMP */
7753 	key_spdflush,	/* SADB_X_SPDFLUSH */
7754 	key_spdadd,	/* SADB_X_SPDSETIDX */
7755 	NULL,		/* SADB_X_SPDEXPIRE */
7756 	key_spddelete2,	/* SADB_X_SPDDELETE2 */
7757 };
7758 
7759 /*
7760  * parse sadb_msg buffer to process PFKEYv2,
7761  * and create a data to response if needed.
7762  * I think to be dealed with mbuf directly.
7763  * IN:
7764  *     msgp  : pointer to pointer to a received buffer pulluped.
7765  *             This is rewrited to response.
7766  *     so    : pointer to socket.
7767  * OUT:
7768  *    length for buffer to send to user process.
7769  */
7770 int
7771 key_parse(struct mbuf *m, struct socket *so)
7772 {
7773 	struct sadb_msg *msg;
7774 	struct sadb_msghdr mh;
7775 	u_int orglen;
7776 	int error;
7777 	int target;
7778 
7779 	IPSEC_ASSERT(so != NULL, ("null socket"));
7780 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
7781 
7782 	if (m->m_len < sizeof(struct sadb_msg)) {
7783 		m = m_pullup(m, sizeof(struct sadb_msg));
7784 		if (!m)
7785 			return ENOBUFS;
7786 	}
7787 	msg = mtod(m, struct sadb_msg *);
7788 	orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7789 	target = KEY_SENDUP_ONE;
7790 
7791 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len != orglen) {
7792 		ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7793 		PFKEYSTAT_INC(out_invlen);
7794 		error = EINVAL;
7795 		goto senderror;
7796 	}
7797 
7798 	if (msg->sadb_msg_version != PF_KEY_V2) {
7799 		ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7800 		    __func__, msg->sadb_msg_version));
7801 		PFKEYSTAT_INC(out_invver);
7802 		error = EINVAL;
7803 		goto senderror;
7804 	}
7805 
7806 	if (msg->sadb_msg_type > SADB_MAX) {
7807 		ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7808 		    __func__, msg->sadb_msg_type));
7809 		PFKEYSTAT_INC(out_invmsgtype);
7810 		error = EINVAL;
7811 		goto senderror;
7812 	}
7813 
7814 	/* for old-fashioned code - should be nuked */
7815 	if (m->m_pkthdr.len > MCLBYTES) {
7816 		m_freem(m);
7817 		return ENOBUFS;
7818 	}
7819 	if (m->m_next) {
7820 		struct mbuf *n;
7821 
7822 		MGETHDR(n, M_NOWAIT, MT_DATA);
7823 		if (n && m->m_pkthdr.len > MHLEN) {
7824 			if (!(MCLGET(n, M_NOWAIT))) {
7825 				m_free(n);
7826 				n = NULL;
7827 			}
7828 		}
7829 		if (!n) {
7830 			m_freem(m);
7831 			return ENOBUFS;
7832 		}
7833 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7834 		n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7835 		n->m_next = NULL;
7836 		m_freem(m);
7837 		m = n;
7838 	}
7839 
7840 	/* align the mbuf chain so that extensions are in contiguous region. */
7841 	error = key_align(m, &mh);
7842 	if (error)
7843 		return error;
7844 
7845 	msg = mh.msg;
7846 
7847 	/* We use satype as scope mask for spddump */
7848 	if (msg->sadb_msg_type == SADB_X_SPDDUMP) {
7849 		switch (msg->sadb_msg_satype) {
7850 		case IPSEC_POLICYSCOPE_ANY:
7851 		case IPSEC_POLICYSCOPE_GLOBAL:
7852 		case IPSEC_POLICYSCOPE_IFNET:
7853 		case IPSEC_POLICYSCOPE_PCB:
7854 			break;
7855 		default:
7856 			ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7857 			    __func__, msg->sadb_msg_type));
7858 			PFKEYSTAT_INC(out_invsatype);
7859 			error = EINVAL;
7860 			goto senderror;
7861 		}
7862 	} else {
7863 		switch (msg->sadb_msg_satype) { /* check SA type */
7864 		case SADB_SATYPE_UNSPEC:
7865 			switch (msg->sadb_msg_type) {
7866 			case SADB_GETSPI:
7867 			case SADB_UPDATE:
7868 			case SADB_ADD:
7869 			case SADB_DELETE:
7870 			case SADB_GET:
7871 			case SADB_ACQUIRE:
7872 			case SADB_EXPIRE:
7873 				ipseclog((LOG_DEBUG, "%s: must specify satype "
7874 				    "when msg type=%u.\n", __func__,
7875 				    msg->sadb_msg_type));
7876 				PFKEYSTAT_INC(out_invsatype);
7877 				error = EINVAL;
7878 				goto senderror;
7879 			}
7880 			break;
7881 		case SADB_SATYPE_AH:
7882 		case SADB_SATYPE_ESP:
7883 		case SADB_X_SATYPE_IPCOMP:
7884 		case SADB_X_SATYPE_TCPSIGNATURE:
7885 			switch (msg->sadb_msg_type) {
7886 			case SADB_X_SPDADD:
7887 			case SADB_X_SPDDELETE:
7888 			case SADB_X_SPDGET:
7889 			case SADB_X_SPDFLUSH:
7890 			case SADB_X_SPDSETIDX:
7891 			case SADB_X_SPDUPDATE:
7892 			case SADB_X_SPDDELETE2:
7893 				ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7894 				    __func__, msg->sadb_msg_type));
7895 				PFKEYSTAT_INC(out_invsatype);
7896 				error = EINVAL;
7897 				goto senderror;
7898 			}
7899 			break;
7900 		case SADB_SATYPE_RSVP:
7901 		case SADB_SATYPE_OSPFV2:
7902 		case SADB_SATYPE_RIPV2:
7903 		case SADB_SATYPE_MIP:
7904 			ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7905 			    __func__, msg->sadb_msg_satype));
7906 			PFKEYSTAT_INC(out_invsatype);
7907 			error = EOPNOTSUPP;
7908 			goto senderror;
7909 		case 1:	/* XXX: What does it do? */
7910 			if (msg->sadb_msg_type == SADB_X_PROMISC)
7911 				break;
7912 			/*FALLTHROUGH*/
7913 		default:
7914 			ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7915 			    __func__, msg->sadb_msg_satype));
7916 			PFKEYSTAT_INC(out_invsatype);
7917 			error = EINVAL;
7918 			goto senderror;
7919 		}
7920 	}
7921 
7922 	/* check field of upper layer protocol and address family */
7923 	if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7924 	 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7925 		struct sadb_address *src0, *dst0;
7926 		u_int plen;
7927 
7928 		src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7929 		dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7930 
7931 		/* check upper layer protocol */
7932 		if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7933 			ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7934 				"mismatched.\n", __func__));
7935 			PFKEYSTAT_INC(out_invaddr);
7936 			error = EINVAL;
7937 			goto senderror;
7938 		}
7939 
7940 		/* check family */
7941 		if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7942 		    PFKEY_ADDR_SADDR(dst0)->sa_family) {
7943 			ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7944 				__func__));
7945 			PFKEYSTAT_INC(out_invaddr);
7946 			error = EINVAL;
7947 			goto senderror;
7948 		}
7949 		if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7950 		    PFKEY_ADDR_SADDR(dst0)->sa_len) {
7951 			ipseclog((LOG_DEBUG, "%s: address struct size "
7952 				"mismatched.\n", __func__));
7953 			PFKEYSTAT_INC(out_invaddr);
7954 			error = EINVAL;
7955 			goto senderror;
7956 		}
7957 
7958 		switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7959 		case AF_INET:
7960 			if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7961 			    sizeof(struct sockaddr_in)) {
7962 				PFKEYSTAT_INC(out_invaddr);
7963 				error = EINVAL;
7964 				goto senderror;
7965 			}
7966 			break;
7967 		case AF_INET6:
7968 			if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7969 			    sizeof(struct sockaddr_in6)) {
7970 				PFKEYSTAT_INC(out_invaddr);
7971 				error = EINVAL;
7972 				goto senderror;
7973 			}
7974 			break;
7975 		default:
7976 			ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7977 				__func__));
7978 			PFKEYSTAT_INC(out_invaddr);
7979 			error = EAFNOSUPPORT;
7980 			goto senderror;
7981 		}
7982 
7983 		switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7984 		case AF_INET:
7985 			plen = sizeof(struct in_addr) << 3;
7986 			break;
7987 		case AF_INET6:
7988 			plen = sizeof(struct in6_addr) << 3;
7989 			break;
7990 		default:
7991 			plen = 0;	/*fool gcc*/
7992 			break;
7993 		}
7994 
7995 		/* check max prefix length */
7996 		if (src0->sadb_address_prefixlen > plen ||
7997 		    dst0->sadb_address_prefixlen > plen) {
7998 			ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7999 				__func__));
8000 			PFKEYSTAT_INC(out_invaddr);
8001 			error = EINVAL;
8002 			goto senderror;
8003 		}
8004 
8005 		/*
8006 		 * prefixlen == 0 is valid because there can be a case when
8007 		 * all addresses are matched.
8008 		 */
8009 	}
8010 
8011 	if (msg->sadb_msg_type >= nitems(key_typesw) ||
8012 	    key_typesw[msg->sadb_msg_type] == NULL) {
8013 		PFKEYSTAT_INC(out_invmsgtype);
8014 		error = EINVAL;
8015 		goto senderror;
8016 	}
8017 
8018 	return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
8019 
8020 senderror:
8021 	msg->sadb_msg_errno = error;
8022 	return key_sendup_mbuf(so, m, target);
8023 }
8024 
8025 static int
8026 key_senderror(struct socket *so, struct mbuf *m, int code)
8027 {
8028 	struct sadb_msg *msg;
8029 
8030 	IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
8031 		("mbuf too small, len %u", m->m_len));
8032 
8033 	msg = mtod(m, struct sadb_msg *);
8034 	msg->sadb_msg_errno = code;
8035 	return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
8036 }
8037 
8038 /*
8039  * set the pointer to each header into message buffer.
8040  * m will be freed on error.
8041  * XXX larger-than-MCLBYTES extension?
8042  */
8043 static int
8044 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
8045 {
8046 	struct mbuf *n;
8047 	struct sadb_ext *ext;
8048 	size_t off, end;
8049 	int extlen;
8050 	int toff;
8051 
8052 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
8053 	IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
8054 	IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
8055 		("mbuf too small, len %u", m->m_len));
8056 
8057 	/* initialize */
8058 	bzero(mhp, sizeof(*mhp));
8059 
8060 	mhp->msg = mtod(m, struct sadb_msg *);
8061 	mhp->ext[0] = (struct sadb_ext *)mhp->msg;	/*XXX backward compat */
8062 
8063 	end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
8064 	extlen = end;	/*just in case extlen is not updated*/
8065 	for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
8066 		n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
8067 		if (!n) {
8068 			/* m is already freed */
8069 			return ENOBUFS;
8070 		}
8071 		ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
8072 
8073 		/* set pointer */
8074 		switch (ext->sadb_ext_type) {
8075 		case SADB_EXT_SA:
8076 		case SADB_EXT_ADDRESS_SRC:
8077 		case SADB_EXT_ADDRESS_DST:
8078 		case SADB_EXT_ADDRESS_PROXY:
8079 		case SADB_EXT_LIFETIME_CURRENT:
8080 		case SADB_EXT_LIFETIME_HARD:
8081 		case SADB_EXT_LIFETIME_SOFT:
8082 		case SADB_EXT_KEY_AUTH:
8083 		case SADB_EXT_KEY_ENCRYPT:
8084 		case SADB_EXT_IDENTITY_SRC:
8085 		case SADB_EXT_IDENTITY_DST:
8086 		case SADB_EXT_SENSITIVITY:
8087 		case SADB_EXT_PROPOSAL:
8088 		case SADB_EXT_SUPPORTED_AUTH:
8089 		case SADB_EXT_SUPPORTED_ENCRYPT:
8090 		case SADB_EXT_SPIRANGE:
8091 		case SADB_X_EXT_POLICY:
8092 		case SADB_X_EXT_SA2:
8093 		case SADB_X_EXT_NAT_T_TYPE:
8094 		case SADB_X_EXT_NAT_T_SPORT:
8095 		case SADB_X_EXT_NAT_T_DPORT:
8096 		case SADB_X_EXT_NAT_T_OAI:
8097 		case SADB_X_EXT_NAT_T_OAR:
8098 		case SADB_X_EXT_NAT_T_FRAG:
8099 		case SADB_X_EXT_SA_REPLAY:
8100 		case SADB_X_EXT_NEW_ADDRESS_SRC:
8101 		case SADB_X_EXT_NEW_ADDRESS_DST:
8102 			/* duplicate check */
8103 			/*
8104 			 * XXX Are there duplication payloads of either
8105 			 * KEY_AUTH or KEY_ENCRYPT ?
8106 			 */
8107 			if (mhp->ext[ext->sadb_ext_type] != NULL) {
8108 				ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
8109 					"%u\n", __func__, ext->sadb_ext_type));
8110 				m_freem(m);
8111 				PFKEYSTAT_INC(out_dupext);
8112 				return EINVAL;
8113 			}
8114 			break;
8115 		default:
8116 			ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
8117 				__func__, ext->sadb_ext_type));
8118 			m_freem(m);
8119 			PFKEYSTAT_INC(out_invexttype);
8120 			return EINVAL;
8121 		}
8122 
8123 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
8124 
8125 		if (key_validate_ext(ext, extlen)) {
8126 			m_freem(m);
8127 			PFKEYSTAT_INC(out_invlen);
8128 			return EINVAL;
8129 		}
8130 
8131 		n = m_pulldown(m, off, extlen, &toff);
8132 		if (!n) {
8133 			/* m is already freed */
8134 			return ENOBUFS;
8135 		}
8136 		ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
8137 
8138 		mhp->ext[ext->sadb_ext_type] = ext;
8139 		mhp->extoff[ext->sadb_ext_type] = off;
8140 		mhp->extlen[ext->sadb_ext_type] = extlen;
8141 	}
8142 
8143 	if (off != end) {
8144 		m_freem(m);
8145 		PFKEYSTAT_INC(out_invlen);
8146 		return EINVAL;
8147 	}
8148 
8149 	return 0;
8150 }
8151 
8152 static int
8153 key_validate_ext(const struct sadb_ext *ext, int len)
8154 {
8155 	const struct sockaddr *sa;
8156 	enum { NONE, ADDR } checktype = NONE;
8157 	int baselen = 0;
8158 	const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
8159 
8160 	if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
8161 		return EINVAL;
8162 
8163 	/* if it does not match minimum/maximum length, bail */
8164 	if (ext->sadb_ext_type >= nitems(minsize) ||
8165 	    ext->sadb_ext_type >= nitems(maxsize))
8166 		return EINVAL;
8167 	if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
8168 		return EINVAL;
8169 	if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
8170 		return EINVAL;
8171 
8172 	/* more checks based on sadb_ext_type XXX need more */
8173 	switch (ext->sadb_ext_type) {
8174 	case SADB_EXT_ADDRESS_SRC:
8175 	case SADB_EXT_ADDRESS_DST:
8176 	case SADB_EXT_ADDRESS_PROXY:
8177 	case SADB_X_EXT_NAT_T_OAI:
8178 	case SADB_X_EXT_NAT_T_OAR:
8179 	case SADB_X_EXT_NEW_ADDRESS_SRC:
8180 	case SADB_X_EXT_NEW_ADDRESS_DST:
8181 		baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
8182 		checktype = ADDR;
8183 		break;
8184 	case SADB_EXT_IDENTITY_SRC:
8185 	case SADB_EXT_IDENTITY_DST:
8186 		if (((const struct sadb_ident *)ext)->sadb_ident_type ==
8187 		    SADB_X_IDENTTYPE_ADDR) {
8188 			baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
8189 			checktype = ADDR;
8190 		} else
8191 			checktype = NONE;
8192 		break;
8193 	default:
8194 		checktype = NONE;
8195 		break;
8196 	}
8197 
8198 	switch (checktype) {
8199 	case NONE:
8200 		break;
8201 	case ADDR:
8202 		sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
8203 		if (len < baselen + sal)
8204 			return EINVAL;
8205 		if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
8206 			return EINVAL;
8207 		break;
8208 	}
8209 
8210 	return 0;
8211 }
8212 
8213 void
8214 spdcache_init(void)
8215 {
8216 	int i;
8217 
8218 	TUNABLE_INT_FETCH("net.key.spdcache.maxentries",
8219 	    &V_key_spdcache_maxentries);
8220 	TUNABLE_INT_FETCH("net.key.spdcache.threshold",
8221 	    &V_key_spdcache_threshold);
8222 
8223 	if (V_key_spdcache_maxentries) {
8224 		V_key_spdcache_maxentries = MAX(V_key_spdcache_maxentries,
8225 		    SPDCACHE_MAX_ENTRIES_PER_HASH);
8226 		V_spdcachehashtbl = hashinit(V_key_spdcache_maxentries /
8227 		    SPDCACHE_MAX_ENTRIES_PER_HASH,
8228 		    M_IPSEC_SPDCACHE, &V_spdcachehash_mask);
8229 		V_key_spdcache_maxentries = (V_spdcachehash_mask + 1)
8230 		    * SPDCACHE_MAX_ENTRIES_PER_HASH;
8231 
8232 		V_spdcache_lock = malloc(sizeof(struct mtx) *
8233 		    (V_spdcachehash_mask + 1),
8234 		    M_IPSEC_SPDCACHE, M_WAITOK|M_ZERO);
8235 
8236 		for (i = 0; i < V_spdcachehash_mask + 1; ++i)
8237 			SPDCACHE_LOCK_INIT(i);
8238 	}
8239 }
8240 
8241 struct spdcache_entry *
8242 spdcache_entry_alloc(const struct secpolicyindex *spidx, struct secpolicy *sp)
8243 {
8244 	struct spdcache_entry *entry;
8245 
8246 	entry = malloc(sizeof(struct spdcache_entry),
8247 		    M_IPSEC_SPDCACHE, M_NOWAIT|M_ZERO);
8248 	if (entry == NULL)
8249 		return NULL;
8250 
8251 	if (sp != NULL)
8252 		SP_ADDREF(sp);
8253 
8254 	entry->spidx = *spidx;
8255 	entry->sp = sp;
8256 
8257 	return (entry);
8258 }
8259 
8260 void
8261 spdcache_entry_free(struct spdcache_entry *entry)
8262 {
8263 
8264 	if (entry->sp != NULL)
8265 		key_freesp(&entry->sp);
8266 	free(entry, M_IPSEC_SPDCACHE);
8267 }
8268 
8269 void
8270 spdcache_clear(void)
8271 {
8272 	struct spdcache_entry *entry;
8273 	int i;
8274 
8275 	for (i = 0; i < V_spdcachehash_mask + 1; ++i) {
8276 		SPDCACHE_LOCK(i);
8277 		while (!LIST_EMPTY(&V_spdcachehashtbl[i])) {
8278 			entry = LIST_FIRST(&V_spdcachehashtbl[i]);
8279 			LIST_REMOVE(entry, chain);
8280 			spdcache_entry_free(entry);
8281 		}
8282 		SPDCACHE_UNLOCK(i);
8283 	}
8284 }
8285 
8286 #ifdef VIMAGE
8287 void
8288 spdcache_destroy(void)
8289 {
8290 	int i;
8291 
8292 	if (SPDCACHE_ENABLED()) {
8293 		spdcache_clear();
8294 		hashdestroy(V_spdcachehashtbl, M_IPSEC_SPDCACHE, V_spdcachehash_mask);
8295 
8296 		for (i = 0; i < V_spdcachehash_mask + 1; ++i)
8297 			SPDCACHE_LOCK_DESTROY(i);
8298 
8299 		free(V_spdcache_lock, M_IPSEC_SPDCACHE);
8300 	}
8301 }
8302 #endif
8303 void
8304 key_init(void)
8305 {
8306 	int i;
8307 
8308 	for (i = 0; i < IPSEC_DIR_MAX; i++) {
8309 		TAILQ_INIT(&V_sptree[i]);
8310 		TAILQ_INIT(&V_sptree_ifnet[i]);
8311 	}
8312 
8313 	V_key_lft_zone = uma_zcreate("IPsec SA lft_c",
8314 	    sizeof(uint64_t) * 2, NULL, NULL, NULL, NULL,
8315 	    UMA_ALIGN_PTR, UMA_ZONE_PCPU);
8316 
8317 	TAILQ_INIT(&V_sahtree);
8318 	V_sphashtbl = hashinit(SPHASH_NHASH, M_IPSEC_SP, &V_sphash_mask);
8319 	V_savhashtbl = hashinit(SAVHASH_NHASH, M_IPSEC_SA, &V_savhash_mask);
8320 	V_sahaddrhashtbl = hashinit(SAHHASH_NHASH, M_IPSEC_SAH,
8321 	    &V_sahaddrhash_mask);
8322 	V_acqaddrhashtbl = hashinit(ACQHASH_NHASH, M_IPSEC_SAQ,
8323 	    &V_acqaddrhash_mask);
8324 	V_acqseqhashtbl = hashinit(ACQHASH_NHASH, M_IPSEC_SAQ,
8325 	    &V_acqseqhash_mask);
8326 
8327 	spdcache_init();
8328 
8329 	for (i = 0; i <= SADB_SATYPE_MAX; i++)
8330 		LIST_INIT(&V_regtree[i]);
8331 
8332 	LIST_INIT(&V_acqtree);
8333 	LIST_INIT(&V_spacqtree);
8334 
8335 	if (!IS_DEFAULT_VNET(curvnet))
8336 		return;
8337 
8338 	XFORMS_LOCK_INIT();
8339 	SPTREE_LOCK_INIT();
8340 	REGTREE_LOCK_INIT();
8341 	SAHTREE_LOCK_INIT();
8342 	ACQ_LOCK_INIT();
8343 	SPACQ_LOCK_INIT();
8344 
8345 #ifndef IPSEC_DEBUG2
8346 	callout_init(&key_timer, 1);
8347 	callout_reset(&key_timer, hz, key_timehandler, NULL);
8348 #endif /*IPSEC_DEBUG2*/
8349 
8350 	/* initialize key statistics */
8351 	keystat.getspi_count = 1;
8352 
8353 	if (bootverbose)
8354 		printf("IPsec: Initialized Security Association Processing.\n");
8355 }
8356 
8357 #ifdef VIMAGE
8358 void
8359 key_destroy(void)
8360 {
8361 	struct secashead_queue sahdrainq;
8362 	struct secpolicy_queue drainq;
8363 	struct secpolicy *sp, *nextsp;
8364 	struct secacq *acq, *nextacq;
8365 	struct secspacq *spacq, *nextspacq;
8366 	struct secashead *sah;
8367 	struct secasvar *sav;
8368 	struct secreg *reg;
8369 	int i;
8370 
8371 	/*
8372 	 * XXX: can we just call free() for each object without
8373 	 * walking through safe way with releasing references?
8374 	 */
8375 	TAILQ_INIT(&drainq);
8376 	SPTREE_WLOCK();
8377 	for (i = 0; i < IPSEC_DIR_MAX; i++) {
8378 		TAILQ_CONCAT(&drainq, &V_sptree[i], chain);
8379 		TAILQ_CONCAT(&drainq, &V_sptree_ifnet[i], chain);
8380 	}
8381 	for (i = 0; i < V_sphash_mask + 1; i++)
8382 		LIST_INIT(&V_sphashtbl[i]);
8383 	SPTREE_WUNLOCK();
8384 	spdcache_destroy();
8385 
8386 	sp = TAILQ_FIRST(&drainq);
8387 	while (sp != NULL) {
8388 		nextsp = TAILQ_NEXT(sp, chain);
8389 		key_freesp(&sp);
8390 		sp = nextsp;
8391 	}
8392 
8393 	TAILQ_INIT(&sahdrainq);
8394 	SAHTREE_WLOCK();
8395 	TAILQ_CONCAT(&sahdrainq, &V_sahtree, chain);
8396 	for (i = 0; i < V_savhash_mask + 1; i++)
8397 		LIST_INIT(&V_savhashtbl[i]);
8398 	for (i = 0; i < V_sahaddrhash_mask + 1; i++)
8399 		LIST_INIT(&V_sahaddrhashtbl[i]);
8400 	TAILQ_FOREACH(sah, &sahdrainq, chain) {
8401 		sah->state = SADB_SASTATE_DEAD;
8402 		TAILQ_FOREACH(sav, &sah->savtree_larval, chain) {
8403 			sav->state = SADB_SASTATE_DEAD;
8404 		}
8405 		TAILQ_FOREACH(sav, &sah->savtree_alive, chain) {
8406 			sav->state = SADB_SASTATE_DEAD;
8407 		}
8408 	}
8409 	SAHTREE_WUNLOCK();
8410 
8411 	key_freesah_flushed(&sahdrainq);
8412 	hashdestroy(V_sphashtbl, M_IPSEC_SP, V_sphash_mask);
8413 	hashdestroy(V_savhashtbl, M_IPSEC_SA, V_savhash_mask);
8414 	hashdestroy(V_sahaddrhashtbl, M_IPSEC_SAH, V_sahaddrhash_mask);
8415 
8416 	REGTREE_LOCK();
8417 	for (i = 0; i <= SADB_SATYPE_MAX; i++) {
8418 		LIST_FOREACH(reg, &V_regtree[i], chain) {
8419 			if (__LIST_CHAINED(reg)) {
8420 				LIST_REMOVE(reg, chain);
8421 				free(reg, M_IPSEC_SAR);
8422 				break;
8423 			}
8424 		}
8425 	}
8426 	REGTREE_UNLOCK();
8427 
8428 	ACQ_LOCK();
8429 	acq = LIST_FIRST(&V_acqtree);
8430 	while (acq != NULL) {
8431 		nextacq = LIST_NEXT(acq, chain);
8432 		LIST_REMOVE(acq, chain);
8433 		free(acq, M_IPSEC_SAQ);
8434 		acq = nextacq;
8435 	}
8436 	for (i = 0; i < V_acqaddrhash_mask + 1; i++)
8437 		LIST_INIT(&V_acqaddrhashtbl[i]);
8438 	for (i = 0; i < V_acqseqhash_mask + 1; i++)
8439 		LIST_INIT(&V_acqseqhashtbl[i]);
8440 	ACQ_UNLOCK();
8441 
8442 	SPACQ_LOCK();
8443 	for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
8444 	    spacq = nextspacq) {
8445 		nextspacq = LIST_NEXT(spacq, chain);
8446 		if (__LIST_CHAINED(spacq)) {
8447 			LIST_REMOVE(spacq, chain);
8448 			free(spacq, M_IPSEC_SAQ);
8449 		}
8450 	}
8451 	SPACQ_UNLOCK();
8452 	hashdestroy(V_acqaddrhashtbl, M_IPSEC_SAQ, V_acqaddrhash_mask);
8453 	hashdestroy(V_acqseqhashtbl, M_IPSEC_SAQ, V_acqseqhash_mask);
8454 	uma_zdestroy(V_key_lft_zone);
8455 
8456 	if (!IS_DEFAULT_VNET(curvnet))
8457 		return;
8458 #ifndef IPSEC_DEBUG2
8459 	callout_drain(&key_timer);
8460 #endif
8461 	XFORMS_LOCK_DESTROY();
8462 	SPTREE_LOCK_DESTROY();
8463 	REGTREE_LOCK_DESTROY();
8464 	SAHTREE_LOCK_DESTROY();
8465 	ACQ_LOCK_DESTROY();
8466 	SPACQ_LOCK_DESTROY();
8467 }
8468 #endif
8469 
8470 /* record data transfer on SA, and update timestamps */
8471 void
8472 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
8473 {
8474 	IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
8475 	IPSEC_ASSERT(m != NULL, ("Null mbuf"));
8476 
8477 	/*
8478 	 * XXX Currently, there is a difference of bytes size
8479 	 * between inbound and outbound processing.
8480 	 */
8481 	counter_u64_add(sav->lft_c_bytes, m->m_pkthdr.len);
8482 
8483 	/*
8484 	 * We use the number of packets as the unit of
8485 	 * allocations.  We increment the variable
8486 	 * whenever {esp,ah}_{in,out}put is called.
8487 	 */
8488 	counter_u64_add(sav->lft_c_allocations, 1);
8489 
8490 	/*
8491 	 * NOTE: We record CURRENT usetime by using wall clock,
8492 	 * in seconds.  HARD and SOFT lifetime are measured by the time
8493 	 * difference (again in seconds) from usetime.
8494 	 *
8495 	 *	usetime
8496 	 *	v     expire   expire
8497 	 * -----+-----+--------+---> t
8498 	 *	<--------------> HARD
8499 	 *	<-----> SOFT
8500 	 */
8501 	if (sav->firstused == 0)
8502 		sav->firstused = time_second;
8503 }
8504 
8505 /*
8506  * Take one of the kernel's security keys and convert it into a PF_KEY
8507  * structure within an mbuf, suitable for sending up to a waiting
8508  * application in user land.
8509  *
8510  * IN:
8511  *    src: A pointer to a kernel security key.
8512  *    exttype: Which type of key this is. Refer to the PF_KEY data structures.
8513  * OUT:
8514  *    a valid mbuf or NULL indicating an error
8515  *
8516  */
8517 
8518 static struct mbuf *
8519 key_setkey(struct seckey *src, uint16_t exttype)
8520 {
8521 	struct mbuf *m;
8522 	struct sadb_key *p;
8523 	int len;
8524 
8525 	if (src == NULL)
8526 		return NULL;
8527 
8528 	len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8529 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
8530 	if (m == NULL)
8531 		return NULL;
8532 	m_align(m, len);
8533 	m->m_len = len;
8534 	p = mtod(m, struct sadb_key *);
8535 	bzero(p, len);
8536 	p->sadb_key_len = PFKEY_UNIT64(len);
8537 	p->sadb_key_exttype = exttype;
8538 	p->sadb_key_bits = src->bits;
8539 	bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8540 
8541 	return m;
8542 }
8543 
8544 /*
8545  * Take one of the kernel's lifetime data structures and convert it
8546  * into a PF_KEY structure within an mbuf, suitable for sending up to
8547  * a waiting application in user land.
8548  *
8549  * IN:
8550  *    src: A pointer to a kernel lifetime structure.
8551  *    exttype: Which type of lifetime this is. Refer to the PF_KEY
8552  *             data structures for more information.
8553  * OUT:
8554  *    a valid mbuf or NULL indicating an error
8555  *
8556  */
8557 
8558 static struct mbuf *
8559 key_setlifetime(struct seclifetime *src, uint16_t exttype)
8560 {
8561 	struct mbuf *m = NULL;
8562 	struct sadb_lifetime *p;
8563 	int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8564 
8565 	if (src == NULL)
8566 		return NULL;
8567 
8568 	m = m_get2(len, M_NOWAIT, MT_DATA, 0);
8569 	if (m == NULL)
8570 		return m;
8571 	m_align(m, len);
8572 	m->m_len = len;
8573 	p = mtod(m, struct sadb_lifetime *);
8574 
8575 	bzero(p, len);
8576 	p->sadb_lifetime_len = PFKEY_UNIT64(len);
8577 	p->sadb_lifetime_exttype = exttype;
8578 	p->sadb_lifetime_allocations = src->allocations;
8579 	p->sadb_lifetime_bytes = src->bytes;
8580 	p->sadb_lifetime_addtime = src->addtime;
8581 	p->sadb_lifetime_usetime = src->usetime;
8582 
8583 	return m;
8584 
8585 }
8586 
8587 const struct enc_xform *
8588 enc_algorithm_lookup(int alg)
8589 {
8590 	int i;
8591 
8592 	for (i = 0; i < nitems(supported_ealgs); i++)
8593 		if (alg == supported_ealgs[i].sadb_alg)
8594 			return (supported_ealgs[i].xform);
8595 	return (NULL);
8596 }
8597 
8598 const struct auth_hash *
8599 auth_algorithm_lookup(int alg)
8600 {
8601 	int i;
8602 
8603 	for (i = 0; i < nitems(supported_aalgs); i++)
8604 		if (alg == supported_aalgs[i].sadb_alg)
8605 			return (supported_aalgs[i].xform);
8606 	return (NULL);
8607 }
8608 
8609 const struct comp_algo *
8610 comp_algorithm_lookup(int alg)
8611 {
8612 	int i;
8613 
8614 	for (i = 0; i < nitems(supported_calgs); i++)
8615 		if (alg == supported_calgs[i].sadb_alg)
8616 			return (supported_calgs[i].xform);
8617 	return (NULL);
8618 }
8619 
8620 /*
8621  * Register a transform.
8622  */
8623 static int
8624 xform_register(struct xformsw* xsp)
8625 {
8626 	struct xformsw *entry;
8627 
8628 	XFORMS_LOCK();
8629 	LIST_FOREACH(entry, &xforms, chain) {
8630 		if (entry->xf_type == xsp->xf_type) {
8631 			XFORMS_UNLOCK();
8632 			return (EEXIST);
8633 		}
8634 	}
8635 	LIST_INSERT_HEAD(&xforms, xsp, chain);
8636 	XFORMS_UNLOCK();
8637 	return (0);
8638 }
8639 
8640 void
8641 xform_attach(void *data)
8642 {
8643 	struct xformsw *xsp = (struct xformsw *)data;
8644 
8645 	if (xform_register(xsp) != 0)
8646 		printf("%s: failed to register %s xform\n", __func__,
8647 		    xsp->xf_name);
8648 }
8649 
8650 void
8651 xform_detach(void *data)
8652 {
8653 	struct xformsw *xsp = (struct xformsw *)data;
8654 
8655 	XFORMS_LOCK();
8656 	LIST_REMOVE(xsp, chain);
8657 	XFORMS_UNLOCK();
8658 
8659 	/* Delete all SAs related to this xform. */
8660 	key_delete_xform(xsp);
8661 }
8662 
8663 /*
8664  * Initialize transform support in an sav.
8665  */
8666 static int
8667 xform_init(struct secasvar *sav, u_short xftype)
8668 {
8669 	struct xformsw *entry;
8670 	int ret;
8671 
8672 	IPSEC_ASSERT(sav->tdb_xform == NULL,
8673 	    ("tdb_xform is already initialized"));
8674 
8675 	ret = EINVAL;
8676 	XFORMS_LOCK();
8677 	LIST_FOREACH(entry, &xforms, chain) {
8678 	    if (entry->xf_type == xftype) {
8679 		    ret = (*entry->xf_init)(sav, entry);
8680 		    break;
8681 	    }
8682 	}
8683 	XFORMS_UNLOCK();
8684 	return (ret);
8685 }
8686 
8687