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