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