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