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