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