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