Home | History | Annotate | Line # | Download | only in netipsec
ipsec.c revision 1.116
      1 /*	$NetBSD: ipsec.c,v 1.116 2017/08/03 06:32:51 ozaki-r Exp $	*/
      2 /*	$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $	*/
      3 /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 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: ipsec.c,v 1.116 2017/08/03 06:32:51 ozaki-r Exp $");
     36 
     37 /*
     38  * IPsec controller part.
     39  */
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_inet.h"
     43 #include "opt_ipsec.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/domain.h>
     50 #include <sys/protosw.h>
     51 #include <sys/socket.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/errno.h>
     54 #include <sys/time.h>
     55 #include <sys/kernel.h>
     56 #include <sys/syslog.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/proc.h>
     59 #include <sys/kauth.h>
     60 #include <sys/cpu.h>
     61 #include <sys/kmem.h>
     62 #include <sys/pserialize.h>
     63 
     64 #include <net/if.h>
     65 #include <net/route.h>
     66 
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/ip_var.h>
     71 #include <netinet/in_var.h>
     72 #include <netinet/udp.h>
     73 #include <netinet/udp_var.h>
     74 #include <netinet/tcp.h>
     75 #include <netinet/udp.h>
     76 #include <netinet/ip_icmp.h>
     77 #include <netinet/ip_private.h>
     78 
     79 #include <netinet/ip6.h>
     80 #ifdef INET6
     81 #include <netinet6/ip6_var.h>
     82 #endif
     83 #include <netinet/in_pcb.h>
     84 #ifdef INET6
     85 #include <netinet6/in6_pcb.h>
     86 #include <netinet/icmp6.h>
     87 #endif
     88 
     89 #include <netipsec/ipsec.h>
     90 #include <netipsec/ipsec_var.h>
     91 #include <netipsec/ipsec_private.h>
     92 #ifdef INET6
     93 #include <netipsec/ipsec6.h>
     94 #endif
     95 #include <netipsec/ah_var.h>
     96 #include <netipsec/esp_var.h>
     97 #include <netipsec/ipcomp.h>		/*XXX*/
     98 #include <netipsec/ipcomp_var.h>
     99 
    100 #include <netipsec/key.h>
    101 #include <netipsec/keydb.h>
    102 #include <netipsec/key_debug.h>
    103 
    104 #include <netipsec/xform.h>
    105 
    106 #include <net/net_osdep.h>
    107 
    108 int ipsec_used = 0;
    109 int ipsec_enabled = 1;
    110 
    111 #ifdef IPSEC_DEBUG
    112 int ipsec_debug = 1;
    113 
    114 /*
    115  * When set to 1, IPsec will send packets with the same sequence number.
    116  * This allows to verify if the other side has proper replay attacks detection.
    117  */
    118 int ipsec_replay = 0;
    119 
    120 /*
    121  * When set 1, IPsec will send packets with corrupted HMAC.
    122  * This allows to verify if the other side properly detects modified packets.
    123  */
    124 int ipsec_integrity = 0;
    125 #else
    126 int ipsec_debug = 0;
    127 #endif
    128 
    129 percpu_t *ipsecstat_percpu;
    130 int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
    131 int ip4_ipsec_dfbit = 2;	/* DF bit on encap. 0: clear 1: set 2: copy */
    132 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
    133 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
    134 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
    135 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
    136 struct secpolicy ip4_def_policy;
    137 int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
    138 int ip4_esp_randpad = -1;
    139 
    140 u_int ipsec_spdgen = 1;		/* SPD generation # */
    141 
    142 static struct secpolicy *ipsec_checkpcbcache (struct mbuf *,
    143 	struct inpcbpolicy *, int);
    144 static int ipsec_fillpcbcache (struct inpcbpolicy *, struct mbuf *,
    145 	struct secpolicy *, int);
    146 static int ipsec_invalpcbcache (struct inpcbpolicy *, int);
    147 
    148 /*
    149  * Crypto support requirements:
    150  *
    151  *  1	require hardware support
    152  * -1	require software support
    153  *  0	take anything
    154  */
    155 int	crypto_support = 0;
    156 
    157 static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
    158     struct inpcb_hdr *, int *);
    159 
    160 #ifdef __FreeBSD__
    161 /* net.inet.ipsec */
    162 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
    163 	esp_randpad, CTLFLAG_RW,	&ip4_esp_randpad,	0, "");
    164 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
    165 	crypto_support,	CTLFLAG_RW,	&crypto_support,0, "");
    166 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
    167 	"Emulate replay attack");
    168 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
    169 	&ipsec_integrity, 0, "Emulate man-in-the-middle attack");
    170 #endif /* __FreeBSD__ */
    171 
    172 #ifdef INET6
    173 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
    174 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
    175 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
    176 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
    177 struct secpolicy ip6_def_policy;
    178 int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
    179 int ip6_esp_randpad = -1;
    180 
    181 
    182 #ifdef __FreeBSD__
    183 /* net.inet6.ipsec6 */
    184 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
    185 	esp_randpad, CTLFLAG_RW,	&ip6_esp_randpad,	0, "");
    186 #endif /* __FreeBSD__ */
    187 #endif /* INET6 */
    188 
    189 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *);
    190 #ifdef INET6
    191 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *);
    192 #endif
    193 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
    194 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
    195 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
    196 #ifdef INET6
    197 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
    198 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
    199 #endif
    200 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
    201 #if 0 /* unused */
    202 static struct secpolicy *ipsec_deepcopy_policy (const struct secpolicy *);
    203 #endif
    204 static int ipsec_set_policy (struct secpolicy **, int, const void *, size_t,
    205     kauth_cred_t);
    206 static int ipsec_get_policy (struct secpolicy *, struct mbuf **);
    207 static void ipsec_destroy_policy(struct secpolicy *);
    208 static void vshiftl (unsigned char *, int, int);
    209 static size_t ipsec_hdrsiz (const struct secpolicy *);
    210 
    211 /*
    212  * Try to validate and use cached policy on a PCB.
    213  */
    214 static struct secpolicy *
    215 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
    216 {
    217 	struct secpolicyindex spidx;
    218 	struct secpolicy *sp = NULL;
    219 	int s;
    220 
    221 	KASSERT(IPSEC_DIR_IS_VALID(dir));
    222 	KASSERT(pcbsp != NULL);
    223 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    224 	KASSERT(inph_locked(pcbsp->sp_inph));
    225 
    226 	/*
    227 	 * Checking the generation and sp->state and taking a reference to an SP
    228 	 * must be in a critical section of pserialize. See key_unlink_sp.
    229 	 */
    230 	s = pserialize_read_enter();
    231 	/* SPD table change invalidate all the caches. */
    232 	if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
    233 		ipsec_invalpcbcache(pcbsp, dir);
    234 		goto out;
    235 	}
    236 	sp = pcbsp->sp_cache[dir].cachesp;
    237 	if (sp == NULL)
    238 		goto out;
    239 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
    240 		sp = NULL;
    241 		ipsec_invalpcbcache(pcbsp, dir);
    242 		goto out;
    243 	}
    244 	if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
    245 		/* NB: assume ipsec_setspidx never sleep */
    246 		if (ipsec_setspidx(m, &spidx, 1) != 0) {
    247 			sp = NULL;
    248 			goto out;
    249 		}
    250 
    251 		/*
    252 		 * We have to make an exact match here since the cached rule
    253 		 * might have lower priority than a rule that would otherwise
    254 		 * have matched the packet.
    255 		 */
    256 		if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
    257 		    sizeof(spidx))) {
    258 			sp = NULL;
    259 			goto out;
    260 		}
    261 	} else {
    262 		/*
    263 		 * The pcb is connected, and the L4 code is sure that:
    264 		 * - outgoing side uses inp_[lf]addr
    265 		 * - incoming side looks up policy after inpcb lookup
    266 		 * and address pair is know to be stable.  We do not need
    267 		 * to generate spidx again, nor check the address match again.
    268 		 *
    269 		 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
    270 		 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
    271 		 */
    272 	}
    273 
    274 	sp->lastused = time_second;
    275 	KEY_SP_REF(sp);
    276 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    277 	    "DP cause refcnt++:%d SP:%p\n",
    278 	    key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
    279 out:
    280 	pserialize_read_exit(s);
    281 	return sp;
    282 }
    283 
    284 static int
    285 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
    286 	struct secpolicy *sp, int dir)
    287 {
    288 
    289 	KASSERT(IPSEC_DIR_IS_INOROUT(dir));
    290 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    291 	KASSERT(inph_locked(pcbsp->sp_inph));
    292 
    293 	pcbsp->sp_cache[dir].cachesp = NULL;
    294 	pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
    295 	if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, 1) != 0) {
    296 		return EINVAL;
    297 	}
    298 	pcbsp->sp_cache[dir].cachesp = sp;
    299 	if (pcbsp->sp_cache[dir].cachesp) {
    300 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    301 		    "DP cause refcnt++:%d SP:%p\n",
    302 		    key_sp_refcnt(pcbsp->sp_cache[dir].cachesp),
    303 		    pcbsp->sp_cache[dir].cachesp);
    304 
    305 		/*
    306 		 * If the PCB is connected, we can remember a hint to
    307 		 * possibly short-circuit IPsec processing in other places.
    308 		 */
    309 		if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
    310 			switch (pcbsp->sp_cache[dir].cachesp->policy) {
    311 			case IPSEC_POLICY_NONE:
    312 			case IPSEC_POLICY_BYPASS:
    313 				pcbsp->sp_cache[dir].cachehint =
    314 				    IPSEC_PCBHINT_NO;
    315 				break;
    316 			default:
    317 				pcbsp->sp_cache[dir].cachehint =
    318 				    IPSEC_PCBHINT_YES;
    319 			}
    320 		}
    321 	}
    322 	pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
    323 
    324 	return 0;
    325 }
    326 
    327 static int
    328 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
    329 {
    330 	int i;
    331 
    332 	KASSERT(inph_locked(pcbsp->sp_inph));
    333 
    334 	for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
    335 		if (dir != IPSEC_DIR_ANY && i != dir)
    336 			continue;
    337 		pcbsp->sp_cache[i].cachesp = NULL;
    338 		pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
    339 		pcbsp->sp_cache[i].cachegen = 0;
    340 		memset(&pcbsp->sp_cache[i].cacheidx, 0,
    341 		    sizeof(pcbsp->sp_cache[i].cacheidx));
    342 	}
    343 	return 0;
    344 }
    345 
    346 void
    347 ipsec_pcbconn(struct inpcbpolicy *pcbsp)
    348 {
    349 
    350 	KASSERT(inph_locked(pcbsp->sp_inph));
    351 
    352 	pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
    353 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    354 }
    355 
    356 void
    357 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
    358 {
    359 
    360 	KASSERT(inph_locked(pcbsp->sp_inph));
    361 
    362 	pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
    363 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    364 }
    365 
    366 void
    367 ipsec_invalpcbcacheall(void)
    368 {
    369 
    370 	if (ipsec_spdgen == UINT_MAX)
    371 		ipsec_spdgen = 1;
    372 	else
    373 		ipsec_spdgen++;
    374 }
    375 
    376 /*
    377  * Return a held reference to the default SP.
    378  */
    379 static struct secpolicy *
    380 key_get_default_sp(int af, const char *where, int tag)
    381 {
    382 	struct secpolicy *sp;
    383 
    384 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
    385 
    386 	switch(af) {
    387 	case AF_INET:
    388 		sp = &ip4_def_policy;
    389 		break;
    390 #ifdef INET6
    391 	case AF_INET6:
    392 		sp = &ip6_def_policy;
    393 		break;
    394 #endif
    395 	default:
    396 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    397 		    "unexpected protocol family %u\n", af);
    398 		return NULL;
    399 	}
    400 
    401 	if (sp->policy != IPSEC_POLICY_DISCARD &&
    402 		sp->policy != IPSEC_POLICY_NONE) {
    403 		IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
    404 		    sp->policy, IPSEC_POLICY_NONE);
    405 		sp->policy = IPSEC_POLICY_NONE;
    406 	}
    407 	KEY_SP_REF(sp);
    408 
    409 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
    410 	    sp, key_sp_refcnt(sp));
    411 	return sp;
    412 }
    413 #define	KEY_GET_DEFAULT_SP(af) \
    414 	key_get_default_sp((af), __func__, __LINE__)
    415 
    416 /*
    417  * For OUTBOUND packet having a socket. Searching SPD for packet,
    418  * and return a pointer to SP.
    419  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
    420  *		0	: bypass
    421  *		EACCES	: discard packet.
    422  *		ENOENT	: ipsec_acquire() in progress, maybe.
    423  *		others	: error occurred.
    424  *	others:	a pointer to SP
    425  *
    426  * NOTE: IPv6 mapped address concern is implemented here.
    427  */
    428 static struct secpolicy *
    429 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph,
    430     int *error)
    431 {
    432 	struct inpcbpolicy *pcbsp = NULL;
    433 	struct secpolicy *currsp = NULL;	/* policy on socket */
    434 	struct secpolicy *sp;
    435 	int af;
    436 
    437 	KASSERT(m != NULL);
    438 	KASSERT(inph != NULL);
    439 	KASSERT(error != NULL);
    440 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    441 
    442 	KASSERT(inph->inph_socket != NULL);
    443 	KASSERT(inph_locked(inph));
    444 
    445 	/* XXX FIXME inpcb/in6pcb  vs socket*/
    446 	af = inph->inph_af;
    447 	KASSERTMSG(af == AF_INET || af == AF_INET6,
    448 	    "unexpected protocol family %u", af);
    449 
    450 	KASSERT(inph->inph_sp != NULL);
    451 	/* If we have a cached entry, and if it is still valid, use it. */
    452 	IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
    453 	currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir);
    454 	if (currsp) {
    455 		*error = 0;
    456 		return currsp;
    457 	}
    458 	IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
    459 
    460 	switch (af) {
    461 	case AF_INET: {
    462 		struct inpcb *in4p = (struct inpcb *)inph;
    463 		/* set spidx in pcb */
    464 		*error = ipsec4_setspidx_inpcb(m, in4p);
    465 		pcbsp = in4p->inp_sp;
    466 		break;
    467 		}
    468 
    469 #if defined(INET6)
    470 	case AF_INET6: {
    471 		struct in6pcb *in6p = (struct in6pcb *)inph;
    472 		/* set spidx in pcb */
    473 		*error = ipsec6_setspidx_in6pcb(m, in6p);
    474 		pcbsp = in6p->in6p_sp;
    475 		break;
    476 		}
    477 #endif
    478 	default:
    479 		*error = EPFNOSUPPORT;
    480 		break;
    481 	}
    482 	if (*error)
    483 		return NULL;
    484 
    485 	KASSERT(pcbsp != NULL);
    486 	switch (dir) {
    487 	case IPSEC_DIR_INBOUND:
    488 		currsp = pcbsp->sp_in;
    489 		break;
    490 	case IPSEC_DIR_OUTBOUND:
    491 		currsp = pcbsp->sp_out;
    492 		break;
    493 	}
    494 	KASSERT(currsp != NULL);
    495 
    496 	if (pcbsp->priv) {			/* when privilieged socket */
    497 		switch (currsp->policy) {
    498 		case IPSEC_POLICY_BYPASS:
    499 		case IPSEC_POLICY_IPSEC:
    500 			KEY_SP_REF(currsp);
    501 			sp = currsp;
    502 			break;
    503 
    504 		case IPSEC_POLICY_ENTRUST:
    505 			/* look for a policy in SPD */
    506 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    507 			if (sp == NULL)		/* no SP found */
    508 				sp = KEY_GET_DEFAULT_SP(af);
    509 			break;
    510 
    511 		default:
    512 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
    513 			    currsp->policy);
    514 			*error = EINVAL;
    515 			return NULL;
    516 		}
    517 	} else {				/* unpriv, SPD has policy */
    518 		sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    519 		if (sp == NULL) {		/* no SP found */
    520 			switch (currsp->policy) {
    521 			case IPSEC_POLICY_BYPASS:
    522 				IPSECLOG(LOG_ERR, "Illegal policy for "
    523 				    "non-priviliged defined %d\n",
    524 				    currsp->policy);
    525 				*error = EINVAL;
    526 				return NULL;
    527 
    528 			case IPSEC_POLICY_ENTRUST:
    529 				sp = KEY_GET_DEFAULT_SP(af);
    530 				break;
    531 
    532 			case IPSEC_POLICY_IPSEC:
    533 				KEY_SP_REF(currsp);
    534 				sp = currsp;
    535 				break;
    536 
    537 			default:
    538 				IPSECLOG(LOG_ERR, "Invalid policy for "
    539 				    "PCB %d\n", currsp->policy);
    540 				*error = EINVAL;
    541 				return NULL;
    542 			}
    543 		}
    544 	}
    545 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
    546 	    currsp->policy);
    547 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    548 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
    549 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
    550 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
    551 	return sp;
    552 }
    553 
    554 /*
    555  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
    556  * and return a pointer to SP.
    557  * OUT:	positive: a pointer to the entry for security policy leaf matched.
    558  *	NULL:	no apropreate SP found, the following value is set to error.
    559  *		0	: bypass
    560  *		EACCES	: discard packet.
    561  *		ENOENT	: ipsec_acquire() in progress, maybe.
    562  *		others	: error occurred.
    563  */
    564 struct secpolicy *
    565 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
    566 {
    567 	struct secpolicyindex spidx;
    568 	struct secpolicy *sp;
    569 
    570 	KASSERT(m != NULL);
    571 	KASSERT(error != NULL);
    572 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    573 
    574 	sp = NULL;
    575 
    576 	/* Make an index to look for a policy. */
    577 	*error = ipsec_setspidx(m, &spidx, (flag & IP_FORWARDING) ? 0 : 1);
    578 	if (*error != 0) {
    579 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
    580 		memset(&spidx, 0, sizeof (spidx));
    581 		return NULL;
    582 	}
    583 
    584 	spidx.dir = dir;
    585 
    586 	if (key_havesp(dir)) {
    587 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
    588 	}
    589 
    590 	if (sp == NULL)			/* no SP found, use system default */
    591 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
    592 	KASSERT(sp != NULL);
    593 	return sp;
    594 }
    595 
    596 struct secpolicy *
    597 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
    598 		   struct inpcb *inp)
    599 {
    600 	struct secpolicy *sp;
    601 
    602 	*error = 0;
    603 
    604 	if (inp == NULL) {
    605 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
    606 	} else {
    607 		KASSERT(inp->inp_socket != NULL);
    608 		sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)inp, error);
    609 	}
    610 	if (sp == NULL) {
    611 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    612 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    613 		return NULL;
    614 	}
    615 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    616 	switch (sp->policy) {
    617 	case IPSEC_POLICY_ENTRUST:
    618 	default:
    619 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    620 		/* fall thru... */
    621 	case IPSEC_POLICY_DISCARD:
    622 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    623 		*error = -EINVAL;	/* packet is discarded by caller */
    624 		break;
    625 	case IPSEC_POLICY_BYPASS:
    626 	case IPSEC_POLICY_NONE:
    627 		KEY_SP_UNREF(&sp);
    628 		sp = NULL;		/* NB: force NULL result */
    629 		break;
    630 	case IPSEC_POLICY_IPSEC:
    631 		KASSERT(sp->req != NULL);
    632 		break;
    633 	}
    634 	if (*error != 0) {
    635 		KEY_SP_UNREF(&sp);
    636 		sp = NULL;
    637 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    638 	}
    639 	return sp;
    640 }
    641 
    642 int
    643 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
    644     u_long *mtu, bool *natt_frag, bool *done)
    645 {
    646 	struct secpolicy *sp = NULL;
    647 	int error, s;
    648 
    649 	/*
    650 	 * Check the security policy (SP) for the packet and, if required,
    651 	 * do IPsec-related processing.  There are two cases here; the first
    652 	 * time a packet is sent through it will be untagged and handled by
    653 	 * ipsec4_checkpolicy().  If the packet is resubmitted to ip_output
    654 	 * (e.g. after AH, ESP, etc. processing), there will be a tag to
    655 	 * bypass the lookup and related policy checking.
    656 	 */
    657 	if (ipsec_outdone(m)) {
    658 		return 0;
    659 	}
    660 	s = splsoftnet();
    661 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
    662 		splx(s);
    663 		return 0;
    664 	}
    665 	sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
    666 
    667 	/*
    668 	 * There are four return cases:
    669 	 *	sp != NULL			apply IPsec policy
    670 	 *	sp == NULL, error == 0		no IPsec handling needed
    671 	 *	sp == NULL, error == -EINVAL	discard packet w/o error
    672 	 *	sp == NULL, error != 0		discard packet, report error
    673 	 */
    674 	if (sp == NULL) {
    675 		splx(s);
    676 		if (error) {
    677 			/*
    678 			 * Hack: -EINVAL is used to signal that a packet
    679 			 * should be silently discarded.  This is typically
    680 			 * because we asked key management for an SA and
    681 			 * it was delayed (e.g. kicked up to IKE).
    682 			 */
    683 			if (error == -EINVAL)
    684 				error = 0;
    685 			m_freem(m);
    686 			*done = true;
    687 			return error;
    688 		}
    689 		/* No IPsec processing for this packet. */
    690 		return 0;
    691 	}
    692 
    693 	/*
    694 	 * Do delayed checksums now because we send before
    695 	 * this is done in the normal processing path.
    696 	 */
    697 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    698 		in_delayed_cksum(m);
    699 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    700 	}
    701 
    702     {
    703 	u_long _mtu = 0;
    704 
    705 	/* Note: callee frees mbuf */
    706 	error = ipsec4_process_packet(m, sp->req, &_mtu);
    707 
    708 	if (error == 0 && _mtu != 0) {
    709 		/*
    710 		 * NAT-T ESP fragmentation: do not do IPSec processing
    711 		 * now, we will do it on each fragmented packet.
    712 		 */
    713 		*mtu = _mtu;
    714 		*natt_frag = true;
    715 		KEY_SP_UNREF(&sp);
    716 		splx(s);
    717 		return 0;
    718 	}
    719     }
    720 	/*
    721 	 * Preserve KAME behaviour: ENOENT can be returned
    722 	 * when an SA acquire is in progress.  Don't propagate
    723 	 * this to user-level; it confuses applications.
    724 	 *
    725 	 * XXX this will go away when the SADB is redone.
    726 	 */
    727 	if (error == ENOENT)
    728 		error = 0;
    729 	KEY_SP_UNREF(&sp);
    730 	splx(s);
    731 	*done = true;
    732 	return error;
    733 }
    734 
    735 int
    736 ipsec4_input(struct mbuf *m, int flags)
    737 {
    738 	struct secpolicy *sp;
    739 	int error, s;
    740 
    741 	s = splsoftnet();
    742 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
    743 	if (sp == NULL) {
    744 		splx(s);
    745 		return EINVAL;
    746 	}
    747 
    748 	/*
    749 	 * Check security policy against packet attributes.
    750 	 */
    751 	error = ipsec_in_reject(sp, m);
    752 	KEY_SP_UNREF(&sp);
    753 	splx(s);
    754 	if (error) {
    755 		return error;
    756 	}
    757 
    758 	if (flags == 0) {
    759 		/* We are done. */
    760 		return 0;
    761 	}
    762 
    763 	/*
    764 	 * Peek at the outbound SP for this packet to determine if
    765 	 * it is a Fast Forward candidate.
    766 	 */
    767 	s = splsoftnet();
    768 	sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, NULL);
    769 	if (sp != NULL) {
    770 		m->m_flags &= ~M_CANFASTFWD;
    771 		KEY_SP_UNREF(&sp);
    772 	}
    773 	splx(s);
    774 	return 0;
    775 }
    776 
    777 int
    778 ipsec4_forward(struct mbuf *m, int *destmtu)
    779 {
    780 	/*
    781 	 * If the packet is routed over IPsec tunnel, tell the
    782 	 * originator the tunnel MTU.
    783 	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
    784 	 * XXX quickhack!!!
    785 	 */
    786 	struct secpolicy *sp;
    787 	size_t ipsechdr;
    788 	int error;
    789 
    790 	sp = ipsec4_getpolicybyaddr(m,
    791 	    IPSEC_DIR_OUTBOUND, IP_FORWARDING, &error);
    792 	if (sp == NULL) {
    793 		return EINVAL;
    794 	}
    795 
    796 	/* Count IPsec header size. */
    797 	ipsechdr = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
    798 
    799 	/*
    800 	 * Find the correct route for outer IPv4 header, compute tunnel MTU.
    801 	 */
    802 	if (sp->req) {
    803 		struct route *ro;
    804 		struct rtentry *rt;
    805 		struct secasvar *sav = NULL;
    806 
    807 		error = key_checkrequest(sp->req, &sav);
    808 		if (error != 0)
    809 			return error;
    810 		ro = &sav->sah->sa_route;
    811 		rt = rtcache_validate(ro);
    812 		if (rt && rt->rt_ifp) {
    813 			*destmtu = rt->rt_rmx.rmx_mtu ?
    814 			    rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
    815 			*destmtu -= ipsechdr;
    816 		}
    817 		rtcache_unref(rt, ro);
    818 		KEY_SA_UNREF(&sav);
    819 	}
    820 	KEY_SP_UNREF(&sp);
    821 	return 0;
    822 }
    823 
    824 #ifdef INET6
    825 struct secpolicy *
    826 ipsec6_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
    827 	 	   struct in6pcb *in6p)
    828 {
    829 	struct secpolicy *sp;
    830 
    831 	*error = 0;
    832 
    833 	if (in6p == NULL) {
    834 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
    835 	} else {
    836 		KASSERT(in6p->in6p_socket != NULL);
    837 		sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)in6p, error);
    838 	}
    839 	if (sp == NULL) {
    840 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    841 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    842 		return NULL;
    843 	}
    844 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    845 	switch (sp->policy) {
    846 	case IPSEC_POLICY_ENTRUST:
    847 	default:
    848 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    849 		/* fall thru... */
    850 	case IPSEC_POLICY_DISCARD:
    851 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    852 		*error = -EINVAL;   /* packet is discarded by caller */
    853 		break;
    854 	case IPSEC_POLICY_BYPASS:
    855 	case IPSEC_POLICY_NONE:
    856 		KEY_SP_UNREF(&sp);
    857 		sp = NULL;	  /* NB: force NULL result */
    858 		break;
    859 	case IPSEC_POLICY_IPSEC:
    860 		KASSERT(sp->req != NULL);
    861 		break;
    862 	}
    863 	if (*error != 0) {
    864 		KEY_SP_UNREF(&sp);
    865 		sp = NULL;
    866 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    867 	}
    868 	return sp;
    869 }
    870 #endif /* INET6 */
    871 
    872 static int
    873 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
    874 {
    875 	int error;
    876 
    877 	KASSERT(pcb != NULL);
    878 	KASSERT(pcb->inp_sp != NULL);
    879 	KASSERT(pcb->inp_sp->sp_out != NULL);
    880 	KASSERT(pcb->inp_sp->sp_in != NULL);
    881 
    882 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
    883 	if (error == 0) {
    884 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
    885 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
    886 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
    887 	} else {
    888 		memset(&pcb->inp_sp->sp_in->spidx, 0,
    889 		    sizeof(pcb->inp_sp->sp_in->spidx));
    890 		memset(&pcb->inp_sp->sp_out->spidx, 0,
    891 		    sizeof(pcb->inp_sp->sp_in->spidx));
    892 	}
    893 	return error;
    894 }
    895 
    896 #ifdef INET6
    897 static int
    898 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
    899 {
    900 	struct secpolicyindex *spidx;
    901 	int error;
    902 
    903 	KASSERT(pcb != NULL);
    904 	KASSERT(pcb->in6p_sp != NULL);
    905 	KASSERT(pcb->in6p_sp->sp_out != NULL);
    906 	KASSERT(pcb->in6p_sp->sp_in != NULL);
    907 
    908 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
    909 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
    910 
    911 	spidx = &pcb->in6p_sp->sp_in->spidx;
    912 	error = ipsec_setspidx(m, spidx, 1);
    913 	if (error)
    914 		goto bad;
    915 	spidx->dir = IPSEC_DIR_INBOUND;
    916 
    917 	spidx = &pcb->in6p_sp->sp_out->spidx;
    918 	error = ipsec_setspidx(m, spidx, 1);
    919 	if (error)
    920 		goto bad;
    921 	spidx->dir = IPSEC_DIR_OUTBOUND;
    922 
    923 	return 0;
    924 
    925 bad:
    926 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
    927 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
    928 	return error;
    929 }
    930 #endif
    931 
    932 /*
    933  * configure security policy index (src/dst/proto/sport/dport)
    934  * by looking at the content of mbuf.
    935  * the caller is responsible for error recovery (like clearing up spidx).
    936  */
    937 static int
    938 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    939 {
    940 	struct ip *ip = NULL;
    941 	struct ip ipbuf;
    942 	u_int v;
    943 	struct mbuf *n;
    944 	int len;
    945 	int error;
    946 
    947 	KASSERT(m != NULL);
    948 
    949 	/*
    950 	 * validate m->m_pkthdr.len.  we see incorrect length if we
    951 	 * mistakenly call this function with inconsistent mbuf chain
    952 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
    953 	 */
    954 	len = 0;
    955 	for (n = m; n; n = n->m_next)
    956 		len += n->m_len;
    957 	if (m->m_pkthdr.len != len) {
    958 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    959 		    "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
    960 		    len, m->m_pkthdr.len);
    961 		return EINVAL;
    962 	}
    963 
    964 	if (m->m_pkthdr.len < sizeof(struct ip)) {
    965 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    966 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
    967 		    m->m_pkthdr.len);
    968 		return EINVAL;
    969 	}
    970 
    971 	if (m->m_len >= sizeof(*ip))
    972 		ip = mtod(m, struct ip *);
    973 	else {
    974 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
    975 		ip = &ipbuf;
    976 	}
    977 	v = ip->ip_v;
    978 	switch (v) {
    979 	case 4:
    980 		error = ipsec4_setspidx_ipaddr(m, spidx);
    981 		if (error)
    982 			return error;
    983 		ipsec4_get_ulp(m, spidx, needport);
    984 		return 0;
    985 #ifdef INET6
    986 	case 6:
    987 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
    988 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    989 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
    990 			    "ignored.\n", m->m_pkthdr.len);
    991 			return EINVAL;
    992 		}
    993 		error = ipsec6_setspidx_ipaddr(m, spidx);
    994 		if (error)
    995 			return error;
    996 		ipsec6_get_ulp(m, spidx, needport);
    997 		return 0;
    998 #endif
    999 	default:
   1000 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1001 		    "unknown IP version %u, ignored.\n", v);
   1002 		return EINVAL;
   1003 	}
   1004 }
   1005 
   1006 static void
   1007 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
   1008 {
   1009 	u_int8_t nxt;
   1010 	int off;
   1011 
   1012 	/* sanity check */
   1013 	KASSERT(m != NULL);
   1014 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
   1015 
   1016 	/* NB: ip_input() flips it into host endian XXX need more checking */
   1017 	if (m->m_len >= sizeof(struct ip)) {
   1018 		struct ip *ip = mtod(m, struct ip *);
   1019 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
   1020 			goto done;
   1021 		off = ip->ip_hl << 2;
   1022 		nxt = ip->ip_p;
   1023 	} else {
   1024 		struct ip ih;
   1025 
   1026 		m_copydata(m, 0, sizeof (struct ip), &ih);
   1027 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
   1028 			goto done;
   1029 		off = ih.ip_hl << 2;
   1030 		nxt = ih.ip_p;
   1031 	}
   1032 
   1033 	while (off < m->m_pkthdr.len) {
   1034 		struct ip6_ext ip6e;
   1035 		struct tcphdr th;
   1036 		struct udphdr uh;
   1037 		struct icmp icmph;
   1038 
   1039 		switch (nxt) {
   1040 		case IPPROTO_TCP:
   1041 			spidx->ul_proto = nxt;
   1042 			if (!needport)
   1043 				goto done_proto;
   1044 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1045 				goto done;
   1046 			m_copydata(m, off, sizeof (th), &th);
   1047 			spidx->src.sin.sin_port = th.th_sport;
   1048 			spidx->dst.sin.sin_port = th.th_dport;
   1049 			return;
   1050 		case IPPROTO_UDP:
   1051 			spidx->ul_proto = nxt;
   1052 			if (!needport)
   1053 				goto done_proto;
   1054 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1055 				goto done;
   1056 			m_copydata(m, off, sizeof (uh), &uh);
   1057 			spidx->src.sin.sin_port = uh.uh_sport;
   1058 			spidx->dst.sin.sin_port = uh.uh_dport;
   1059 			return;
   1060 		case IPPROTO_AH:
   1061 			if (m->m_pkthdr.len > off + sizeof(ip6e))
   1062 				goto done;
   1063 			/* XXX sigh, this works but is totally bogus */
   1064 			m_copydata(m, off, sizeof(ip6e), &ip6e);
   1065 			off += (ip6e.ip6e_len + 2) << 2;
   1066 			nxt = ip6e.ip6e_nxt;
   1067 			break;
   1068 		case IPPROTO_ICMP:
   1069 			spidx->ul_proto = nxt;
   1070 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
   1071 				return;
   1072 			m_copydata(m, off, sizeof(icmph), &icmph);
   1073 			((struct sockaddr_in *)&spidx->src)->sin_port =
   1074 			    htons((uint16_t)icmph.icmp_type);
   1075 			((struct sockaddr_in *)&spidx->dst)->sin_port =
   1076 			    htons((uint16_t)icmph.icmp_code);
   1077 			return;
   1078 		default:
   1079 			/* XXX intermediate headers??? */
   1080 			spidx->ul_proto = nxt;
   1081 			goto done_proto;
   1082 		}
   1083 	}
   1084 done:
   1085 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1086 done_proto:
   1087 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
   1088 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
   1089 }
   1090 
   1091 /* assumes that m is sane */
   1092 static int
   1093 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1094 {
   1095 	static const struct sockaddr_in template = {
   1096 		sizeof (struct sockaddr_in),
   1097 		AF_INET,
   1098 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
   1099 	};
   1100 
   1101 	spidx->src.sin = template;
   1102 	spidx->dst.sin = template;
   1103 
   1104 	if (m->m_len < sizeof (struct ip)) {
   1105 		m_copydata(m, offsetof(struct ip, ip_src),
   1106 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
   1107 		m_copydata(m, offsetof(struct ip, ip_dst),
   1108 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
   1109 	} else {
   1110 		struct ip *ip = mtod(m, struct ip *);
   1111 		spidx->src.sin.sin_addr = ip->ip_src;
   1112 		spidx->dst.sin.sin_addr = ip->ip_dst;
   1113 	}
   1114 
   1115 	spidx->prefs = sizeof(struct in_addr) << 3;
   1116 	spidx->prefd = sizeof(struct in_addr) << 3;
   1117 
   1118 	return 0;
   1119 }
   1120 
   1121 #ifdef INET6
   1122 static void
   1123 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx,
   1124 	       int needport)
   1125 {
   1126 	int off, nxt;
   1127 	struct tcphdr th;
   1128 	struct udphdr uh;
   1129 	struct icmp6_hdr icmph;
   1130 
   1131 	KASSERT(m != NULL);
   1132 
   1133 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1134 		printf("%s:\n", __func__);
   1135 		kdebug_mbuf(m);
   1136 	}
   1137 
   1138 	/* set default */
   1139 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1140 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
   1141 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
   1142 
   1143 	nxt = -1;
   1144 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
   1145 	if (off < 0 || m->m_pkthdr.len < off)
   1146 		return;
   1147 
   1148 	switch (nxt) {
   1149 	case IPPROTO_TCP:
   1150 		spidx->ul_proto = nxt;
   1151 		if (!needport)
   1152 			break;
   1153 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1154 			break;
   1155 		m_copydata(m, off, sizeof(th), &th);
   1156 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
   1157 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
   1158 		break;
   1159 	case IPPROTO_UDP:
   1160 		spidx->ul_proto = nxt;
   1161 		if (!needport)
   1162 			break;
   1163 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1164 			break;
   1165 		m_copydata(m, off, sizeof(uh), &uh);
   1166 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
   1167 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
   1168 		break;
   1169 	case IPPROTO_ICMPV6:
   1170 		spidx->ul_proto = nxt;
   1171 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
   1172 			break;
   1173 		m_copydata(m, off, sizeof(icmph), &icmph);
   1174 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
   1175 		    htons((uint16_t)icmph.icmp6_type);
   1176 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
   1177 		    htons((uint16_t)icmph.icmp6_code);
   1178 		break;
   1179 	default:
   1180 		/* XXX intermediate headers??? */
   1181 		spidx->ul_proto = nxt;
   1182 		break;
   1183 	}
   1184 }
   1185 
   1186 /* assumes that m is sane */
   1187 static int
   1188 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1189 {
   1190 	struct ip6_hdr *ip6 = NULL;
   1191 	struct ip6_hdr ip6buf;
   1192 	struct sockaddr_in6 *sin6;
   1193 
   1194 	if (m->m_len >= sizeof(*ip6))
   1195 		ip6 = mtod(m, struct ip6_hdr *);
   1196 	else {
   1197 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
   1198 		ip6 = &ip6buf;
   1199 	}
   1200 
   1201 	sin6 = (struct sockaddr_in6 *)&spidx->src;
   1202 	memset(sin6, 0, sizeof(*sin6));
   1203 	sin6->sin6_family = AF_INET6;
   1204 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1205 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
   1206 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
   1207 		sin6->sin6_addr.s6_addr16[1] = 0;
   1208 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
   1209 	}
   1210 	spidx->prefs = sizeof(struct in6_addr) << 3;
   1211 
   1212 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
   1213 	memset(sin6, 0, sizeof(*sin6));
   1214 	sin6->sin6_family = AF_INET6;
   1215 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1216 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
   1217 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
   1218 		sin6->sin6_addr.s6_addr16[1] = 0;
   1219 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
   1220 	}
   1221 	spidx->prefd = sizeof(struct in6_addr) << 3;
   1222 
   1223 	return 0;
   1224 }
   1225 #endif
   1226 
   1227 static void
   1228 ipsec_delpcbpolicy(struct inpcbpolicy *p)
   1229 {
   1230 
   1231 	kmem_intr_free(p, sizeof(*p));
   1232 }
   1233 
   1234 /* initialize policy in PCB */
   1235 int
   1236 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
   1237 {
   1238 	struct inpcbpolicy *new;
   1239 
   1240 	KASSERT(so != NULL);
   1241 	KASSERT(policy != NULL);
   1242 
   1243 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
   1244 	if (new == NULL) {
   1245 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1246 		return ENOBUFS;
   1247 	}
   1248 
   1249 	if (IPSEC_PRIVILEGED_SO(so))
   1250 		new->priv = 1;
   1251 	else
   1252 		new->priv = 0;
   1253 
   1254 	/*
   1255 	 * These SPs are dummy. Never be used because the policy
   1256 	 * is ENTRUST. See ipsec_getpolicybysock.
   1257 	 */
   1258 	new->sp_in = kmem_intr_zalloc(sizeof(struct secpolicy), KM_NOSLEEP);
   1259 	if (new->sp_in == NULL) {
   1260 		ipsec_delpcbpolicy(new);
   1261 		return ENOBUFS;
   1262 	}
   1263 	new->sp_in->state = IPSEC_SPSTATE_ALIVE;
   1264 	new->sp_in->policy = IPSEC_POLICY_ENTRUST;
   1265 	new->sp_in->created = 0; /* Indicates dummy */
   1266 
   1267 	new->sp_out = kmem_intr_zalloc(sizeof(struct secpolicy), KM_NOSLEEP);
   1268 	if (new->sp_out == NULL) {
   1269 		kmem_intr_free(new->sp_in, sizeof(struct secpolicy));
   1270 		ipsec_delpcbpolicy(new);
   1271 		return ENOBUFS;
   1272 	}
   1273 	new->sp_out->state = IPSEC_SPSTATE_ALIVE;
   1274 	new->sp_out->policy = IPSEC_POLICY_ENTRUST;
   1275 	new->sp_out->created = 0; /* Indicates dummy */
   1276 
   1277 	*policy = new;
   1278 
   1279 	return 0;
   1280 }
   1281 
   1282 #if 0 /* unused */
   1283 /* copy old ipsec policy into new */
   1284 int
   1285 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
   1286 {
   1287 	struct secpolicy *sp;
   1288 
   1289 	sp = ipsec_deepcopy_policy(old->sp_in);
   1290 	if (sp) {
   1291 		KEY_SP_UNREF(&new->sp_in);
   1292 		new->sp_in = sp;
   1293 	} else
   1294 		return ENOBUFS;
   1295 
   1296 	sp = ipsec_deepcopy_policy(old->sp_out);
   1297 	if (sp) {
   1298 		KEY_SP_UNREF(&new->sp_out);
   1299 		new->sp_out = sp;
   1300 	} else
   1301 		return ENOBUFS;
   1302 
   1303 	new->priv = old->priv;
   1304 
   1305 	return 0;
   1306 }
   1307 
   1308 /* deep-copy a policy in PCB */
   1309 static struct secpolicy *
   1310 ipsec_deepcopy_policy(const struct secpolicy *src)
   1311 {
   1312 	struct ipsecrequest *newchain = NULL;
   1313 	const struct ipsecrequest *p;
   1314 	struct ipsecrequest **q;
   1315 	struct secpolicy *dst;
   1316 
   1317 	if (src == NULL)
   1318 		return NULL;
   1319 	dst = KEY_NEWSP();
   1320 	if (dst == NULL)
   1321 		return NULL;
   1322 
   1323 	/*
   1324 	 * deep-copy IPsec request chain.  This is required since struct
   1325 	 * ipsecrequest is not reference counted.
   1326 	 */
   1327 	q = &newchain;
   1328 	for (p = src->req; p; p = p->next) {
   1329 		*q = kmem_zalloc(sizeof(**q), KM_SLEEP);
   1330 		(*q)->next = NULL;
   1331 
   1332 		(*q)->saidx.proto = p->saidx.proto;
   1333 		(*q)->saidx.mode = p->saidx.mode;
   1334 		(*q)->level = p->level;
   1335 		(*q)->saidx.reqid = p->saidx.reqid;
   1336 
   1337 		memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
   1338 		memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
   1339 
   1340 		(*q)->sp = dst;
   1341 
   1342 		q = &((*q)->next);
   1343 	}
   1344 
   1345 	dst->req = newchain;
   1346 	dst->state = src->state;
   1347 	dst->policy = src->policy;
   1348 	/* do not touch the refcnt fields */
   1349 
   1350 	return dst;
   1351 }
   1352 #endif
   1353 
   1354 static void
   1355 ipsec_destroy_policy(struct secpolicy *sp)
   1356 {
   1357 
   1358 	if (sp->created == 0)
   1359 		/* It's dummy. We can simply free it */
   1360 		kmem_intr_free(sp, sizeof(*sp));
   1361 	else {
   1362 		/*
   1363 		 * We cannot destroy here because it can be called in
   1364 		 * softint. So mark the SP as DEAD and let the timer
   1365 		 * destroy it. See key_timehandler_spd.
   1366 		 */
   1367 		sp->state = IPSEC_SPSTATE_DEAD;
   1368 	}
   1369 }
   1370 
   1371 /* set policy and ipsec request if present. */
   1372 static int
   1373 ipsec_set_policy(
   1374 	struct secpolicy **policy,
   1375 	int optname,
   1376 	const void *request,
   1377 	size_t len,
   1378 	kauth_cred_t cred
   1379 )
   1380 {
   1381 	const struct sadb_x_policy *xpl;
   1382 	struct secpolicy *newsp = NULL, *oldsp;
   1383 	int error;
   1384 
   1385 	KASSERT(!cpu_softintr_p());
   1386 
   1387 	/* sanity check. */
   1388 	if (policy == NULL || *policy == NULL || request == NULL)
   1389 		return EINVAL;
   1390 	if (len < sizeof(*xpl))
   1391 		return EINVAL;
   1392 	xpl = (const struct sadb_x_policy *)request;
   1393 
   1394 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1395 		printf("%s: passed policy\n", __func__);
   1396 		kdebug_sadb_x_policy((const struct sadb_ext *)xpl);
   1397 	}
   1398 
   1399 	/* check policy type */
   1400 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
   1401 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
   1402 	 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
   1403 		return EINVAL;
   1404 
   1405 	/* check privileged socket */
   1406 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
   1407 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
   1408 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
   1409 		if (error)
   1410 			return (error);
   1411 	}
   1412 
   1413 	/* allocation new SP entry */
   1414 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
   1415 		return error;
   1416 
   1417 	key_init_sp(newsp);
   1418 	newsp->created = time_uptime;
   1419 	/* Insert the global list for SPs for sockets */
   1420 	key_socksplist_add(newsp);
   1421 
   1422 	/* clear old SP and set new SP */
   1423 	oldsp = *policy;
   1424 	*policy = newsp;
   1425 	ipsec_destroy_policy(oldsp);
   1426 
   1427 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1428 		printf("%s: new policy\n", __func__);
   1429 		kdebug_secpolicy(newsp);
   1430 	}
   1431 
   1432 	return 0;
   1433 }
   1434 
   1435 static int
   1436 ipsec_get_policy(struct secpolicy *policy, struct mbuf **mp)
   1437 {
   1438 
   1439 	/* sanity check. */
   1440 	if (policy == NULL || mp == NULL)
   1441 		return EINVAL;
   1442 
   1443 	*mp = key_sp2msg(policy);
   1444 	if (!*mp) {
   1445 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1446 		return ENOBUFS;
   1447 	}
   1448 
   1449 	(*mp)->m_type = MT_DATA;
   1450 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1451 		printf("%s:\n", __func__);
   1452 		kdebug_mbuf(*mp);
   1453 	}
   1454 
   1455 	return 0;
   1456 }
   1457 
   1458 int
   1459 ipsec4_set_policy(struct inpcb *inp, int optname, const void *request,
   1460 		  size_t len, kauth_cred_t cred)
   1461 {
   1462 	const struct sadb_x_policy *xpl;
   1463 	struct secpolicy **policy;
   1464 
   1465 	KASSERT(!cpu_softintr_p());
   1466 	KASSERT(inp_locked(inp));
   1467 
   1468 	/* sanity check. */
   1469 	if (inp == NULL || request == NULL)
   1470 		return EINVAL;
   1471 	if (len < sizeof(*xpl))
   1472 		return EINVAL;
   1473 	xpl = (const struct sadb_x_policy *)request;
   1474 
   1475 	KASSERT(inp->inp_sp != NULL);
   1476 
   1477 	/* select direction */
   1478 	switch (xpl->sadb_x_policy_dir) {
   1479 	case IPSEC_DIR_INBOUND:
   1480 		policy = &inp->inp_sp->sp_in;
   1481 		break;
   1482 	case IPSEC_DIR_OUTBOUND:
   1483 		policy = &inp->inp_sp->sp_out;
   1484 		break;
   1485 	default:
   1486 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1487 		    xpl->sadb_x_policy_dir);
   1488 		return EINVAL;
   1489 	}
   1490 
   1491 	return ipsec_set_policy(policy, optname, request, len, cred);
   1492 }
   1493 
   1494 int
   1495 ipsec4_get_policy(struct inpcb *inp, const void *request, size_t len,
   1496 		  struct mbuf **mp)
   1497 {
   1498 	const struct sadb_x_policy *xpl;
   1499 	struct secpolicy *policy;
   1500 
   1501 	/* sanity check. */
   1502 	if (inp == NULL || request == NULL || mp == NULL)
   1503 		return EINVAL;
   1504 	KASSERT(inp->inp_sp != NULL);
   1505 	if (len < sizeof(*xpl))
   1506 		return EINVAL;
   1507 	xpl = (const struct sadb_x_policy *)request;
   1508 
   1509 	/* select direction */
   1510 	switch (xpl->sadb_x_policy_dir) {
   1511 	case IPSEC_DIR_INBOUND:
   1512 		policy = inp->inp_sp->sp_in;
   1513 		break;
   1514 	case IPSEC_DIR_OUTBOUND:
   1515 		policy = inp->inp_sp->sp_out;
   1516 		break;
   1517 	default:
   1518 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1519 		    xpl->sadb_x_policy_dir);
   1520 		return EINVAL;
   1521 	}
   1522 
   1523 	return ipsec_get_policy(policy, mp);
   1524 }
   1525 
   1526 /* delete policy in PCB */
   1527 int
   1528 ipsec4_delete_pcbpolicy(struct inpcb *inp)
   1529 {
   1530 
   1531 	KASSERT(inp != NULL);
   1532 
   1533 	if (inp->inp_sp == NULL)
   1534 		return 0;
   1535 
   1536 	if (inp->inp_sp->sp_in != NULL)
   1537 		ipsec_destroy_policy(inp->inp_sp->sp_in);
   1538 
   1539 	if (inp->inp_sp->sp_out != NULL)
   1540 		ipsec_destroy_policy(inp->inp_sp->sp_out);
   1541 
   1542 	ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
   1543 
   1544 	ipsec_delpcbpolicy(inp->inp_sp);
   1545 	inp->inp_sp = NULL;
   1546 
   1547 	return 0;
   1548 }
   1549 
   1550 #ifdef INET6
   1551 int
   1552 ipsec6_set_policy(struct in6pcb *in6p, int optname, const void *request,
   1553 		  size_t len, kauth_cred_t cred)
   1554 {
   1555 	const struct sadb_x_policy *xpl;
   1556 	struct secpolicy **policy;
   1557 
   1558 	KASSERT(!cpu_softintr_p());
   1559 	KASSERT(in6p_locked(in6p));
   1560 
   1561 	/* sanity check. */
   1562 	if (in6p == NULL || request == NULL)
   1563 		return EINVAL;
   1564 	if (len < sizeof(*xpl))
   1565 		return EINVAL;
   1566 	xpl = (const struct sadb_x_policy *)request;
   1567 
   1568 	/* select direction */
   1569 	switch (xpl->sadb_x_policy_dir) {
   1570 	case IPSEC_DIR_INBOUND:
   1571 		policy = &in6p->in6p_sp->sp_in;
   1572 		break;
   1573 	case IPSEC_DIR_OUTBOUND:
   1574 		policy = &in6p->in6p_sp->sp_out;
   1575 		break;
   1576 	default:
   1577 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1578 		    xpl->sadb_x_policy_dir);
   1579 		return EINVAL;
   1580 	}
   1581 
   1582 	return ipsec_set_policy(policy, optname, request, len, cred);
   1583 }
   1584 
   1585 int
   1586 ipsec6_get_policy(struct in6pcb *in6p, const void *request, size_t len,
   1587 		  struct mbuf **mp)
   1588 {
   1589 	const struct sadb_x_policy *xpl;
   1590 	struct secpolicy *policy;
   1591 
   1592 	/* sanity check. */
   1593 	if (in6p == NULL || request == NULL || mp == NULL)
   1594 		return EINVAL;
   1595 	KASSERT(in6p->in6p_sp != NULL);
   1596 	if (len < sizeof(*xpl))
   1597 		return EINVAL;
   1598 	xpl = (const struct sadb_x_policy *)request;
   1599 
   1600 	/* select direction */
   1601 	switch (xpl->sadb_x_policy_dir) {
   1602 	case IPSEC_DIR_INBOUND:
   1603 		policy = in6p->in6p_sp->sp_in;
   1604 		break;
   1605 	case IPSEC_DIR_OUTBOUND:
   1606 		policy = in6p->in6p_sp->sp_out;
   1607 		break;
   1608 	default:
   1609 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1610 		    xpl->sadb_x_policy_dir);
   1611 		return EINVAL;
   1612 	}
   1613 
   1614 	return ipsec_get_policy(policy, mp);
   1615 }
   1616 
   1617 int
   1618 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
   1619 {
   1620 
   1621 	KASSERT(in6p != NULL);
   1622 
   1623 	if (in6p->in6p_sp == NULL)
   1624 		return 0;
   1625 
   1626 	if (in6p->in6p_sp->sp_in != NULL)
   1627 		ipsec_destroy_policy(in6p->in6p_sp->sp_in);
   1628 
   1629 	if (in6p->in6p_sp->sp_out != NULL)
   1630 		ipsec_destroy_policy(in6p->in6p_sp->sp_out);
   1631 
   1632 	ipsec_invalpcbcache(in6p->in6p_sp, IPSEC_DIR_ANY);
   1633 
   1634 	ipsec_delpcbpolicy(in6p->in6p_sp);
   1635 	in6p->in6p_sp = NULL;
   1636 
   1637 	return 0;
   1638 }
   1639 #endif
   1640 
   1641 /*
   1642  * return current level.
   1643  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
   1644  */
   1645 u_int
   1646 ipsec_get_reqlevel(const struct ipsecrequest *isr)
   1647 {
   1648 	u_int level = 0;
   1649 	u_int esp_trans_deflev, esp_net_deflev;
   1650 	u_int ah_trans_deflev, ah_net_deflev;
   1651 
   1652 	KASSERT(isr != NULL);
   1653 	KASSERT(isr->sp != NULL);
   1654 	KASSERTMSG(
   1655 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
   1656 	    "af family mismatch, src %u, dst %u",
   1657 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
   1658 
   1659 /* XXX note that we have ipseclog() expanded here - code sync issue */
   1660 #define IPSEC_CHECK_DEFAULT(lev) 					\
   1661     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
   1662     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
   1663 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
   1664 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
   1665 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
   1666     : (lev))
   1667 
   1668 	/* set default level */
   1669 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
   1670 #ifdef INET
   1671 	case AF_INET:
   1672 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
   1673 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
   1674 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
   1675 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
   1676 		break;
   1677 #endif
   1678 #ifdef INET6
   1679 	case AF_INET6:
   1680 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
   1681 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
   1682 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
   1683 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
   1684 		break;
   1685 #endif /* INET6 */
   1686 	default:
   1687 		panic("%s: unknown af %u", __func__,
   1688 		    isr->sp->spidx.src.sa.sa_family);
   1689 	}
   1690 
   1691 #undef IPSEC_CHECK_DEFAULT
   1692 
   1693 	/* set level */
   1694 	switch (isr->level) {
   1695 	case IPSEC_LEVEL_DEFAULT:
   1696 		switch (isr->saidx.proto) {
   1697 		case IPPROTO_ESP:
   1698 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1699 				level = esp_net_deflev;
   1700 			else
   1701 				level = esp_trans_deflev;
   1702 			break;
   1703 		case IPPROTO_AH:
   1704 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1705 				level = ah_net_deflev;
   1706 			else
   1707 				level = ah_trans_deflev;
   1708 			break;
   1709 		case IPPROTO_IPCOMP:
   1710 			/*
   1711 			 * we don't really care, as IPcomp document says that
   1712 			 * we shouldn't compress small packets
   1713 			 */
   1714 			level = IPSEC_LEVEL_USE;
   1715 			break;
   1716 		default:
   1717 			panic("%s: Illegal protocol defined %u", __func__,
   1718 			    isr->saidx.proto);
   1719 		}
   1720 		break;
   1721 
   1722 	case IPSEC_LEVEL_USE:
   1723 	case IPSEC_LEVEL_REQUIRE:
   1724 		level = isr->level;
   1725 		break;
   1726 	case IPSEC_LEVEL_UNIQUE:
   1727 		level = IPSEC_LEVEL_REQUIRE;
   1728 		break;
   1729 
   1730 	default:
   1731 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
   1732 	}
   1733 
   1734 	return level;
   1735 }
   1736 
   1737 /*
   1738  * Check security policy requirements against the actual
   1739  * packet contents.  Return one if the packet should be
   1740  * reject as "invalid"; otherwiser return zero to have the
   1741  * packet treated as "valid".
   1742  *
   1743  * OUT:
   1744  *	0: valid
   1745  *	1: invalid
   1746  */
   1747 int
   1748 ipsec_in_reject(const struct secpolicy *sp, const struct mbuf *m)
   1749 {
   1750 	struct ipsecrequest *isr;
   1751 
   1752 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1753 		printf("%s: using SP\n", __func__);
   1754 		kdebug_secpolicy(sp);
   1755 	}
   1756 
   1757 	/* check policy */
   1758 	switch (sp->policy) {
   1759 	case IPSEC_POLICY_DISCARD:
   1760 		return 1;
   1761 	case IPSEC_POLICY_BYPASS:
   1762 	case IPSEC_POLICY_NONE:
   1763 		return 0;
   1764 	}
   1765 
   1766 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1767 	    "invalid policy %u", sp->policy);
   1768 
   1769 	/* XXX should compare policy against ipsec header history */
   1770 
   1771 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1772 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
   1773 			continue;
   1774 		switch (isr->saidx.proto) {
   1775 		case IPPROTO_ESP:
   1776 			if ((m->m_flags & M_DECRYPTED) == 0) {
   1777 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1778 				    "ESP m_flags:%x\n", m->m_flags);
   1779 				return 1;
   1780 			}
   1781 			break;
   1782 		case IPPROTO_AH:
   1783 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
   1784 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1785 				    "AH m_flags:%x\n", m->m_flags);
   1786 				return 1;
   1787 			}
   1788 			break;
   1789 		case IPPROTO_IPCOMP:
   1790 			/*
   1791 			 * we don't really care, as IPcomp document
   1792 			 * says that we shouldn't compress small
   1793 			 * packets, IPComp policy should always be
   1794 			 * treated as being in "use" level.
   1795 			 */
   1796 			break;
   1797 		}
   1798 	}
   1799 	return 0;		/* valid */
   1800 }
   1801 
   1802 /*
   1803  * Check AH/ESP integrity.
   1804  * This function is called from tcp_input(), udp_input(),
   1805  * and {ah,esp}4_input for tunnel mode
   1806  */
   1807 int
   1808 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
   1809 {
   1810 	struct secpolicy *sp;
   1811 	int error;
   1812 	int result;
   1813 
   1814 	KASSERT(m != NULL);
   1815 
   1816 	/* get SP for this packet.
   1817 	 * When we are called from ip_forward(), we call
   1818 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
   1819 	 */
   1820 	if (inp == NULL)
   1821 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
   1822 	else
   1823 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1824 					   (struct inpcb_hdr *)inp, &error);
   1825 
   1826 	if (sp != NULL) {
   1827 		result = ipsec_in_reject(sp, m);
   1828 		if (result)
   1829 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1830 		KEY_SP_UNREF(&sp);
   1831 	} else {
   1832 		result = 0;	/* XXX should be panic ?
   1833 				 * -> No, there may be error. */
   1834 	}
   1835 	return result;
   1836 }
   1837 
   1838 
   1839 #ifdef INET6
   1840 /*
   1841  * Check AH/ESP integrity.
   1842  * This function is called from tcp6_input(), udp6_input(),
   1843  * and {ah,esp}6_input for tunnel mode
   1844  */
   1845 int
   1846 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
   1847 {
   1848 	struct secpolicy *sp = NULL;
   1849 	int error;
   1850 	int result;
   1851 
   1852 	KASSERT(m != NULL);
   1853 
   1854 	/* get SP for this packet.
   1855 	 * When we are called from ip_forward(), we call
   1856 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
   1857 	 */
   1858 	if (in6p == NULL)
   1859 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
   1860 	else
   1861 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1862 			(struct inpcb_hdr *)in6p,
   1863 			&error);
   1864 
   1865 	if (sp != NULL) {
   1866 		result = ipsec_in_reject(sp, m);
   1867 		if (result)
   1868 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1869 		KEY_SP_UNREF(&sp);
   1870 	} else {
   1871 		result = 0;
   1872 	}
   1873 	return result;
   1874 }
   1875 #endif
   1876 
   1877 /*
   1878  * compute the byte size to be occupied by IPsec header.
   1879  * in case it is tunneled, it includes the size of outer IP header.
   1880  * NOTE: SP passed is free in this function.
   1881  */
   1882 static size_t
   1883 ipsec_hdrsiz(const struct secpolicy *sp)
   1884 {
   1885 	struct ipsecrequest *isr;
   1886 	size_t siz;
   1887 
   1888 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1889 		printf("%s: using SP\n", __func__);
   1890 		kdebug_secpolicy(sp);
   1891 	}
   1892 
   1893 	switch (sp->policy) {
   1894 	case IPSEC_POLICY_DISCARD:
   1895 	case IPSEC_POLICY_BYPASS:
   1896 	case IPSEC_POLICY_NONE:
   1897 		return 0;
   1898 	}
   1899 
   1900 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1901 	    "invalid policy %u", sp->policy);
   1902 
   1903 	siz = 0;
   1904 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1905 		size_t clen = 0;
   1906 		struct secasvar *sav = NULL;
   1907 		int error;
   1908 
   1909 		switch (isr->saidx.proto) {
   1910 		case IPPROTO_ESP:
   1911 			error = key_checkrequest(isr, &sav);
   1912 			if (error == 0) {
   1913 				clen = esp_hdrsiz(sav);
   1914 				KEY_SA_UNREF(&sav);
   1915 			} else
   1916 				clen = esp_hdrsiz(NULL);
   1917 			break;
   1918 		case IPPROTO_AH:
   1919 			error = key_checkrequest(isr, &sav);
   1920 			if (error == 0) {
   1921 				clen = ah_hdrsiz(sav);
   1922 				KEY_SA_UNREF(&sav);
   1923 			} else
   1924 				clen = ah_hdrsiz(NULL);
   1925 			break;
   1926 		case IPPROTO_IPCOMP:
   1927 			clen = sizeof(struct ipcomp);
   1928 			break;
   1929 		}
   1930 
   1931 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
   1932 			switch (isr->saidx.dst.sa.sa_family) {
   1933 			case AF_INET:
   1934 				clen += sizeof(struct ip);
   1935 				break;
   1936 #ifdef INET6
   1937 			case AF_INET6:
   1938 				clen += sizeof(struct ip6_hdr);
   1939 				break;
   1940 #endif
   1941 			default:
   1942 				IPSECLOG(LOG_ERR, "unknown AF %d in "
   1943 				    "IPsec tunnel SA\n",
   1944 				    ((const struct sockaddr *)&isr->saidx.dst)
   1945 				    ->sa_family);
   1946 				break;
   1947 			}
   1948 		}
   1949 		siz += clen;
   1950 	}
   1951 
   1952 	return siz;
   1953 }
   1954 
   1955 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
   1956 size_t
   1957 ipsec4_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
   1958 {
   1959 	struct secpolicy *sp;
   1960 	int error;
   1961 	size_t size;
   1962 
   1963 	KASSERT(m != NULL);
   1964 	KASSERTMSG(inp == NULL || inp->inp_socket != NULL, "socket w/o inpcb");
   1965 
   1966 	/* get SP for this packet.
   1967 	 * When we are called from ip_forward(), we call
   1968 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
   1969 	 */
   1970 	if (inp == NULL)
   1971 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1972 	else
   1973 		sp = ipsec_getpolicybysock(m, dir,
   1974 					   (struct inpcb_hdr *)inp, &error);
   1975 
   1976 	if (sp != NULL) {
   1977 		size = ipsec_hdrsiz(sp);
   1978 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%lu.\n",
   1979 		    (unsigned long)size);
   1980 
   1981 		KEY_SP_UNREF(&sp);
   1982 	} else {
   1983 		size = 0;	/* XXX should be panic ? */
   1984 	}
   1985 	return size;
   1986 }
   1987 
   1988 #ifdef INET6
   1989 /* This function is called from ipsec6_hdrsize_tcp(),
   1990  * and maybe from ip6_forward.()
   1991  */
   1992 size_t
   1993 ipsec6_hdrsiz(struct mbuf *m, u_int dir, struct in6pcb *in6p)
   1994 {
   1995 	struct secpolicy *sp;
   1996 	int error;
   1997 	size_t size;
   1998 
   1999 	KASSERT(m != NULL);
   2000 	KASSERTMSG(in6p == NULL || in6p->in6p_socket != NULL,
   2001 	    "socket w/o inpcb");
   2002 
   2003 	/* get SP for this packet */
   2004 	/* XXX Is it right to call with IP_FORWARDING. */
   2005 	if (in6p == NULL)
   2006 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   2007 	else
   2008 		sp = ipsec_getpolicybysock(m, dir,
   2009 			(struct inpcb_hdr *)in6p,
   2010 			&error);
   2011 
   2012 	if (sp == NULL)
   2013 		return 0;
   2014 	size = ipsec_hdrsiz(sp);
   2015 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   2016 	KEY_SP_UNREF(&sp);
   2017 
   2018 	return size;
   2019 }
   2020 #endif /*INET6*/
   2021 
   2022 /*
   2023  * Check the variable replay window.
   2024  * ipsec_chkreplay() performs replay check before ICV verification.
   2025  * ipsec_updatereplay() updates replay bitmap.  This must be called after
   2026  * ICV verification (it also performs replay check, which is usually done
   2027  * beforehand).
   2028  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
   2029  *
   2030  * based on RFC 2401.
   2031  */
   2032 int
   2033 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
   2034 {
   2035 	const struct secreplay *replay;
   2036 	u_int32_t diff;
   2037 	int fr;
   2038 	u_int32_t wsizeb;	/* constant: bits of window size */
   2039 	int frlast;		/* constant: last frame */
   2040 
   2041 	IPSEC_SPLASSERT_SOFTNET(__func__);
   2042 
   2043 	KASSERT(sav != NULL);
   2044 	KASSERT(sav->replay != NULL);
   2045 
   2046 	replay = sav->replay;
   2047 
   2048 	if (replay->wsize == 0)
   2049 		return 1;	/* no need to check replay. */
   2050 
   2051 	/* constant */
   2052 	frlast = replay->wsize - 1;
   2053 	wsizeb = replay->wsize << 3;
   2054 
   2055 	/* sequence number of 0 is invalid */
   2056 	if (seq == 0)
   2057 		return 0;
   2058 
   2059 	/* first time is always okay */
   2060 	if (replay->count == 0)
   2061 		return 1;
   2062 
   2063 	if (seq > replay->lastseq) {
   2064 		/* larger sequences are okay */
   2065 		return 1;
   2066 	} else {
   2067 		/* seq is equal or less than lastseq. */
   2068 		diff = replay->lastseq - seq;
   2069 
   2070 		/* over range to check, i.e. too old or wrapped */
   2071 		if (diff >= wsizeb)
   2072 			return 0;
   2073 
   2074 		fr = frlast - diff / 8;
   2075 
   2076 		/* this packet already seen ? */
   2077 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   2078 			return 0;
   2079 
   2080 		/* out of order but good */
   2081 		return 1;
   2082 	}
   2083 }
   2084 
   2085 /*
   2086  * check replay counter whether to update or not.
   2087  * OUT:	0:	OK
   2088  *	1:	NG
   2089  */
   2090 int
   2091 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
   2092 {
   2093 	struct secreplay *replay;
   2094 	u_int32_t diff;
   2095 	int fr;
   2096 	u_int32_t wsizeb;	/* constant: bits of window size */
   2097 	int frlast;		/* constant: last frame */
   2098 	char buf[INET6_ADDRSTRLEN];
   2099 
   2100 	IPSEC_SPLASSERT_SOFTNET(__func__);
   2101 
   2102 	KASSERT(sav != NULL);
   2103 	KASSERT(sav->replay != NULL);
   2104 
   2105 	replay = sav->replay;
   2106 
   2107 	if (replay->wsize == 0)
   2108 		goto ok;	/* no need to check replay. */
   2109 
   2110 	/* constant */
   2111 	frlast = replay->wsize - 1;
   2112 	wsizeb = replay->wsize << 3;
   2113 
   2114 	/* sequence number of 0 is invalid */
   2115 	if (seq == 0)
   2116 		return 1;
   2117 
   2118 	/* first time */
   2119 	if (replay->count == 0) {
   2120 		replay->lastseq = seq;
   2121 		memset(replay->bitmap, 0, replay->wsize);
   2122 		(replay->bitmap)[frlast] = 1;
   2123 		goto ok;
   2124 	}
   2125 
   2126 	if (seq > replay->lastseq) {
   2127 		/* seq is larger than lastseq. */
   2128 		diff = seq - replay->lastseq;
   2129 
   2130 		/* new larger sequence number */
   2131 		if (diff < wsizeb) {
   2132 			/* In window */
   2133 			/* set bit for this packet */
   2134 			vshiftl(replay->bitmap, diff, replay->wsize);
   2135 			(replay->bitmap)[frlast] |= 1;
   2136 		} else {
   2137 			/* this packet has a "way larger" */
   2138 			memset(replay->bitmap, 0, replay->wsize);
   2139 			(replay->bitmap)[frlast] = 1;
   2140 		}
   2141 		replay->lastseq = seq;
   2142 
   2143 		/* larger is good */
   2144 	} else {
   2145 		/* seq is equal or less than lastseq. */
   2146 		diff = replay->lastseq - seq;
   2147 
   2148 		/* over range to check, i.e. too old or wrapped */
   2149 		if (diff >= wsizeb)
   2150 			return 1;
   2151 
   2152 		fr = frlast - diff / 8;
   2153 
   2154 		/* this packet already seen ? */
   2155 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   2156 			return 1;
   2157 
   2158 		/* mark as seen */
   2159 		(replay->bitmap)[fr] |= (1 << (diff % 8));
   2160 
   2161 		/* out of order but good */
   2162 	}
   2163 
   2164 ok:
   2165 	if (replay->count == ~0) {
   2166 
   2167 		/* set overflow flag */
   2168 		replay->overflow++;
   2169 
   2170 		/* don't increment, no more packets accepted */
   2171 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
   2172 			return 1;
   2173 
   2174 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
   2175 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
   2176 	}
   2177 
   2178 	replay->count++;
   2179 
   2180 	return 0;
   2181 }
   2182 
   2183 /*
   2184  * shift variable length bunffer to left.
   2185  * IN:	bitmap: pointer to the buffer
   2186  * 	nbit:	the number of to shift.
   2187  *	wsize:	buffer size (bytes).
   2188  */
   2189 static void
   2190 vshiftl(unsigned char *bitmap, int nbit, int wsize)
   2191 {
   2192 	int s, j, i;
   2193 	unsigned char over;
   2194 
   2195 	for (j = 0; j < nbit; j += 8) {
   2196 		s = (nbit - j < 8) ? (nbit - j): 8;
   2197 		bitmap[0] <<= s;
   2198 		for (i = 1; i < wsize; i++) {
   2199 			over = (bitmap[i] >> (8 - s));
   2200 			bitmap[i] <<= s;
   2201 			bitmap[i-1] |= over;
   2202 		}
   2203 	}
   2204 
   2205 	return;
   2206 }
   2207 
   2208 /* Return a printable string for the address. */
   2209 const char *
   2210 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
   2211 {
   2212 	switch (sa->sa.sa_family) {
   2213 #if INET
   2214 	case AF_INET:
   2215 		in_print(buf, size, &sa->sin.sin_addr);
   2216 		return buf;
   2217 #endif /* INET */
   2218 
   2219 #if INET6
   2220 	case AF_INET6:
   2221 		in6_print(buf, size, &sa->sin6.sin6_addr);
   2222 		return buf;
   2223 #endif /* INET6 */
   2224 
   2225 	default:
   2226 		return "(unknown address family)";
   2227 	}
   2228 }
   2229 
   2230 const char *
   2231 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
   2232 {
   2233 	const struct secasindex *saidx = &sav->sah->saidx;
   2234 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
   2235 
   2236 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
   2237 	    "af family mismatch, src %u, dst %u",
   2238 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
   2239 
   2240 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
   2241 	    (u_int32_t)ntohl(sav->spi),
   2242 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
   2243 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
   2244 
   2245 	return buf;
   2246 }
   2247 
   2248 void
   2249 ipsec_dumpmbuf(struct mbuf *m)
   2250 {
   2251 	int totlen;
   2252 	int i;
   2253 	u_char *p;
   2254 
   2255 	totlen = 0;
   2256 	printf("---\n");
   2257 	while (m) {
   2258 		p = mtod(m, u_char *);
   2259 		for (i = 0; i < m->m_len; i++) {
   2260 			printf("%02x ", p[i]);
   2261 			totlen++;
   2262 			if (totlen % 16 == 0)
   2263 				printf("\n");
   2264 		}
   2265 		m = m->m_next;
   2266 	}
   2267 	if (totlen % 16 != 0)
   2268 		printf("\n");
   2269 	printf("---\n");
   2270 }
   2271 
   2272 #ifdef INET6
   2273 struct secpolicy *
   2274 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p,
   2275 		    int flags, int *needipsecp, int *errorp)
   2276 {
   2277 	struct secpolicy *sp = NULL;
   2278 	int s;
   2279 	int error = 0;
   2280 	int needipsec = 0;
   2281 
   2282 	if (!ipsec_outdone(m)) {
   2283 		s = splsoftnet();
   2284 		if (in6p != NULL &&
   2285 		    ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
   2286 			splx(s);
   2287 			goto skippolicycheck;
   2288 		}
   2289 		sp = ipsec6_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,in6p);
   2290 
   2291 		/*
   2292 		 * There are four return cases:
   2293 		 *	sp != NULL			apply IPsec policy
   2294 		 *	sp == NULL, error == 0		no IPsec handling needed
   2295 		 *	sp == NULL, error == -EINVAL  discard packet w/o error
   2296 		 *	sp == NULL, error != 0		discard packet, report error
   2297 		 */
   2298 
   2299 		splx(s);
   2300 		if (sp == NULL) {
   2301 			/*
   2302 			 * Caller must check the error return to see if it needs to discard
   2303 			 * the packet.
   2304 			 */
   2305 			needipsec = 0;
   2306 		} else {
   2307 			needipsec = 1;
   2308 		}
   2309 	}
   2310 skippolicycheck:;
   2311 
   2312 	*errorp = error;
   2313 	*needipsecp = needipsec;
   2314 	return sp;
   2315 }
   2316 
   2317 int
   2318 ipsec6_input(struct mbuf *m)
   2319 {
   2320 	struct secpolicy *sp;
   2321 	int s, error;
   2322 
   2323 	s = splsoftnet();
   2324 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
   2325 	if (sp != NULL) {
   2326 		/*
   2327 		 * Check security policy against packet
   2328 		 * attributes.
   2329 		 */
   2330 		error = ipsec_in_reject(sp, m);
   2331 		KEY_SP_UNREF(&sp);
   2332 	} else {
   2333 		/* XXX error stat??? */
   2334 		error = EINVAL;
   2335 		IPSECLOG(LOG_DEBUG, "no SP, packet discarded\n");/*XXX*/
   2336 	}
   2337 	splx(s);
   2338 
   2339 	return error;
   2340 }
   2341 #endif /* INET6 */
   2342 
   2343 
   2344 
   2345 /* XXX this stuff doesn't belong here... */
   2346 
   2347 static	struct xformsw *xforms = NULL;
   2348 
   2349 /*
   2350  * Register a transform; typically at system startup.
   2351  */
   2352 void
   2353 xform_register(struct xformsw *xsp)
   2354 {
   2355 	xsp->xf_next = xforms;
   2356 	xforms = xsp;
   2357 }
   2358 
   2359 /*
   2360  * Initialize transform support in an sav.
   2361  */
   2362 int
   2363 xform_init(struct secasvar *sav, int xftype)
   2364 {
   2365 	struct xformsw *xsp;
   2366 
   2367 	if (sav->tdb_xform != NULL)	/* previously initialized */
   2368 		return 0;
   2369 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
   2370 		if (xsp->xf_type == xftype)
   2371 			return (*xsp->xf_init)(sav, xsp);
   2372 
   2373 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
   2374 	return EINVAL;
   2375 }
   2376 
   2377 void
   2378 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport) {
   2379 	struct m_tag *tag;
   2380 
   2381 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
   2382 		*sport = ((u_int16_t *)(tag + 1))[0];
   2383 		*dport = ((u_int16_t *)(tag + 1))[1];
   2384 	} else
   2385 		*sport = *dport = 0;
   2386 }
   2387 
   2388 /*
   2389  * XXXJRT This should be done as a protosw init call.
   2390  */
   2391 void
   2392 ipsec_attach(void)
   2393 {
   2394 
   2395 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
   2396 
   2397 	sysctl_net_inet_ipsec_setup(NULL);
   2398 #ifdef INET6
   2399 	sysctl_net_inet6_ipsec6_setup(NULL);
   2400 #endif
   2401 
   2402 	ah_attach();
   2403 	esp_attach();
   2404 	ipcomp_attach();
   2405 	ipe4_attach();
   2406 #ifdef TCP_SIGNATURE
   2407 	tcpsignature_attach();
   2408 #endif
   2409 }
   2410