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