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