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