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