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