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