Home | History | Annotate | Line # | Download | only in netipsec
ipsec.c revision 1.149
      1 /* $NetBSD: ipsec.c,v 1.149 2018/02/28 11:29:14 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.149 2018/02/28 11:29:14 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 		struct inpcb *in4p = (struct inpcb *)inph;
    438 		/* set spidx in pcb */
    439 		*error = ipsec_setspidx_inpcb(m, in4p);
    440 		pcbsp = in4p->inp_sp;
    441 		break;
    442 		}
    443 #if defined(INET6)
    444 	case AF_INET6: {
    445 		struct in6pcb *in6p = (struct in6pcb *)inph;
    446 		/* set spidx in pcb */
    447 		*error = ipsec_setspidx_inpcb(m, in6p);
    448 		pcbsp = in6p->in6p_sp;
    449 		break;
    450 		}
    451 #endif
    452 	default:
    453 		*error = EPFNOSUPPORT;
    454 		break;
    455 	}
    456 	if (*error)
    457 		return NULL;
    458 
    459 	KASSERT(pcbsp != NULL);
    460 	switch (dir) {
    461 	case IPSEC_DIR_INBOUND:
    462 		currsp = pcbsp->sp_in;
    463 		break;
    464 	case IPSEC_DIR_OUTBOUND:
    465 		currsp = pcbsp->sp_out;
    466 		break;
    467 	}
    468 	KASSERT(currsp != NULL);
    469 
    470 	if (pcbsp->priv) {			/* when privilieged socket */
    471 		switch (currsp->policy) {
    472 		case IPSEC_POLICY_BYPASS:
    473 		case IPSEC_POLICY_IPSEC:
    474 			KEY_SP_REF(currsp);
    475 			sp = currsp;
    476 			break;
    477 
    478 		case IPSEC_POLICY_ENTRUST:
    479 			/* look for a policy in SPD */
    480 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    481 			if (sp == NULL)		/* no SP found */
    482 				sp = KEY_GET_DEFAULT_SP(af);
    483 			break;
    484 
    485 		default:
    486 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
    487 			    currsp->policy);
    488 			*error = EINVAL;
    489 			return NULL;
    490 		}
    491 	} else {				/* unpriv, SPD has policy */
    492 		sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    493 		if (sp == NULL) {		/* no SP found */
    494 			switch (currsp->policy) {
    495 			case IPSEC_POLICY_BYPASS:
    496 				IPSECLOG(LOG_ERR, "Illegal policy for "
    497 				    "non-priviliged defined %d\n",
    498 				    currsp->policy);
    499 				*error = EINVAL;
    500 				return NULL;
    501 
    502 			case IPSEC_POLICY_ENTRUST:
    503 				sp = KEY_GET_DEFAULT_SP(af);
    504 				break;
    505 
    506 			case IPSEC_POLICY_IPSEC:
    507 				KEY_SP_REF(currsp);
    508 				sp = currsp;
    509 				break;
    510 
    511 			default:
    512 				IPSECLOG(LOG_ERR, "Invalid policy for "
    513 				    "PCB %d\n", currsp->policy);
    514 				*error = EINVAL;
    515 				return NULL;
    516 			}
    517 		}
    518 	}
    519 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
    520 	    currsp->policy);
    521 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    522 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
    523 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
    524 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
    525 	return sp;
    526 }
    527 
    528 /*
    529  * For FORWARDING packet or OUTBOUND without a socket. Searching SPD for packet,
    530  * and return a pointer to SP.
    531  * OUT:	positive: a pointer to the entry for security policy leaf matched.
    532  *	NULL:	no apropreate SP found, the following value is set to error.
    533  *		0	: bypass
    534  *		EACCES	: discard packet.
    535  *		ENOENT	: ipsec_acquire() in progress, maybe.
    536  *		others	: error occurred.
    537  */
    538 static struct secpolicy *
    539 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
    540 {
    541 	struct secpolicyindex spidx;
    542 	struct secpolicy *sp;
    543 
    544 	KASSERT(m != NULL);
    545 	KASSERT(error != NULL);
    546 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    547 
    548 	sp = NULL;
    549 
    550 	/* Make an index to look for a policy. */
    551 	*error = ipsec_setspidx(m, &spidx, (flag & IP_FORWARDING) ? 0 : 1);
    552 	if (*error != 0) {
    553 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
    554 		memset(&spidx, 0, sizeof(spidx));
    555 		return NULL;
    556 	}
    557 
    558 	spidx.dir = dir;
    559 
    560 	if (key_havesp(dir)) {
    561 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
    562 	}
    563 
    564 	if (sp == NULL)			/* no SP found, use system default */
    565 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
    566 	KASSERT(sp != NULL);
    567 	return sp;
    568 }
    569 
    570 static struct secpolicy *
    571 ipsec_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
    572     void *inp)
    573 {
    574 	struct secpolicy *sp;
    575 
    576 	*error = 0;
    577 
    578 	if (inp == NULL) {
    579 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
    580 	} else {
    581 		struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
    582 		KASSERT(inph->inph_socket != NULL);
    583 		sp = ipsec_getpolicybysock(m, dir, inph, error);
    584 	}
    585 	if (sp == NULL) {
    586 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    587 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    588 		return NULL;
    589 	}
    590 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    591 
    592 	switch (sp->policy) {
    593 	case IPSEC_POLICY_ENTRUST:
    594 	default:
    595 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    596 		/* fall thru... */
    597 	case IPSEC_POLICY_DISCARD:
    598 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    599 		*error = -EINVAL;	/* packet is discarded by caller */
    600 		break;
    601 	case IPSEC_POLICY_BYPASS:
    602 	case IPSEC_POLICY_NONE:
    603 		KEY_SP_UNREF(&sp);
    604 		sp = NULL;		/* NB: force NULL result */
    605 		break;
    606 	case IPSEC_POLICY_IPSEC:
    607 		KASSERT(sp->req != NULL);
    608 		break;
    609 	}
    610 
    611 	if (*error != 0) {
    612 		KEY_SP_UNREF(&sp);
    613 		sp = NULL;
    614 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    615 	}
    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 	 * ipsec_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 = ipsec_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 	error = ipsec4_process_packet(m, sp->req, &_mtu);
    684 
    685 	if (error == 0 && _mtu != 0) {
    686 		/*
    687 		 * NAT-T ESP fragmentation: do not do IPSec processing
    688 		 * now, we will do it on each fragmented packet.
    689 		 */
    690 		*mtu = _mtu;
    691 		*natt_frag = true;
    692 		KEY_SP_UNREF(&sp);
    693 		splx(s);
    694 		return 0;
    695 	}
    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 	error = ipsec_in_reject(m, NULL);
    721 	splx(s);
    722 	if (error) {
    723 		return EINVAL;
    724 	}
    725 
    726 	if (flags == 0) {
    727 		/* We are done. */
    728 		return 0;
    729 	}
    730 
    731 	/*
    732 	 * Peek at the outbound SP for this packet to determine if
    733 	 * it is a Fast Forward candidate.
    734 	 */
    735 	s = splsoftnet();
    736 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, NULL);
    737 	if (sp != NULL) {
    738 		m->m_flags &= ~M_CANFASTFWD;
    739 		KEY_SP_UNREF(&sp);
    740 	}
    741 	splx(s);
    742 	return 0;
    743 }
    744 
    745 int
    746 ipsec4_forward(struct mbuf *m, int *destmtu)
    747 {
    748 	/*
    749 	 * If the packet is routed over IPsec tunnel, tell the
    750 	 * originator the tunnel MTU.
    751 	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
    752 	 * XXX quickhack!!!
    753 	 */
    754 	struct secpolicy *sp;
    755 	size_t ipsechdr;
    756 	int error;
    757 
    758 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
    759 	    &error);
    760 	if (sp == NULL) {
    761 		return EINVAL;
    762 	}
    763 
    764 	/* Count IPsec header size. */
    765 	ipsechdr = ipsec_sp_hdrsiz(sp, m);
    766 
    767 	/*
    768 	 * Find the correct route for outer IPv4 header, compute tunnel MTU.
    769 	 */
    770 	if (sp->req) {
    771 		struct secasvar *sav;
    772 
    773 		sav = ipsec_lookup_sa(sp->req, m);
    774 		if (sav != NULL) {
    775 			struct route *ro;
    776 			struct rtentry *rt;
    777 
    778 			ro = &sav->sah->sa_route;
    779 			rt = rtcache_validate(ro);
    780 			if (rt && rt->rt_ifp) {
    781 				*destmtu = rt->rt_rmx.rmx_mtu ?
    782 				    rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
    783 				*destmtu -= ipsechdr;
    784 			}
    785 			rtcache_unref(rt, ro);
    786 			KEY_SA_UNREF(&sav);
    787 		}
    788 	}
    789 	KEY_SP_UNREF(&sp);
    790 	return 0;
    791 }
    792 
    793 static int
    794 ipsec_setspidx_inpcb(struct mbuf *m, void *pcb)
    795 {
    796 	struct inpcb_hdr *inph = (struct inpcb_hdr *)pcb;
    797 	int error;
    798 
    799 	KASSERT(inph != NULL);
    800 	KASSERT(inph->inph_sp != NULL);
    801 	KASSERT(inph->inph_sp->sp_out != NULL);
    802 	KASSERT(inph->inph_sp->sp_in != NULL);
    803 
    804 	error = ipsec_setspidx(m, &inph->inph_sp->sp_in->spidx, 1);
    805 	if (error == 0) {
    806 		inph->inph_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
    807 		inph->inph_sp->sp_out->spidx = inph->inph_sp->sp_in->spidx;
    808 		inph->inph_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
    809 	} else {
    810 		memset(&inph->inph_sp->sp_in->spidx, 0,
    811 		    sizeof(inph->inph_sp->sp_in->spidx));
    812 		memset(&inph->inph_sp->sp_out->spidx, 0,
    813 		    sizeof(inph->inph_sp->sp_out->spidx));
    814 	}
    815 	return error;
    816 }
    817 
    818 /*
    819  * configure security policy index (src/dst/proto/sport/dport)
    820  * by looking at the content of mbuf.
    821  * the caller is responsible for error recovery (like clearing up spidx).
    822  */
    823 static int
    824 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    825 {
    826 	struct ip *ip = NULL;
    827 	struct ip ipbuf;
    828 	u_int v;
    829 	struct mbuf *n;
    830 	int len;
    831 	int error;
    832 
    833 	KASSERT(m != NULL);
    834 
    835 	/*
    836 	 * validate m->m_pkthdr.len.  we see incorrect length if we
    837 	 * mistakenly call this function with inconsistent mbuf chain
    838 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
    839 	 */
    840 	len = 0;
    841 	for (n = m; n; n = n->m_next)
    842 		len += n->m_len;
    843 	if (m->m_pkthdr.len != len) {
    844 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    845 		    "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
    846 		    len, m->m_pkthdr.len);
    847 		KASSERTMSG(0, "impossible");
    848 		return EINVAL;
    849 	}
    850 
    851 	if (m->m_pkthdr.len < sizeof(struct ip)) {
    852 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    853 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
    854 		    m->m_pkthdr.len);
    855 		return EINVAL;
    856 	}
    857 
    858 	if (m->m_len >= sizeof(*ip)) {
    859 		ip = mtod(m, struct ip *);
    860 	} else {
    861 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
    862 		ip = &ipbuf;
    863 	}
    864 	v = ip->ip_v;
    865 	switch (v) {
    866 	case 4:
    867 		error = ipsec4_setspidx_ipaddr(m, spidx);
    868 		if (error)
    869 			return error;
    870 		ipsec4_get_ulp(m, spidx, needport);
    871 		return 0;
    872 #ifdef INET6
    873 	case 6:
    874 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
    875 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    876 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
    877 			    "ignored.\n", m->m_pkthdr.len);
    878 			return EINVAL;
    879 		}
    880 		error = ipsec6_setspidx_ipaddr(m, spidx);
    881 		if (error)
    882 			return error;
    883 		ipsec6_get_ulp(m, spidx, needport);
    884 		return 0;
    885 #endif
    886 	default:
    887 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    888 		    "unknown IP version %u, ignored.\n", v);
    889 		return EINVAL;
    890 	}
    891 }
    892 
    893 static void
    894 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    895 {
    896 	u_int8_t nxt;
    897 	int off;
    898 
    899 	KASSERT(m != NULL);
    900 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
    901 
    902 	/* NB: ip_input() flips it into host endian XXX need more checking */
    903 	if (m->m_len >= sizeof(struct ip)) {
    904 		struct ip *ip = mtod(m, struct ip *);
    905 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
    906 			goto done;
    907 		off = ip->ip_hl << 2;
    908 		nxt = ip->ip_p;
    909 	} else {
    910 		struct ip ih;
    911 
    912 		m_copydata(m, 0, sizeof(struct ip), &ih);
    913 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
    914 			goto done;
    915 		off = ih.ip_hl << 2;
    916 		nxt = ih.ip_p;
    917 	}
    918 
    919 	while (off < m->m_pkthdr.len) {
    920 		struct ip6_ext ip6e;
    921 		struct tcphdr th;
    922 		struct udphdr uh;
    923 		struct icmp icmph;
    924 
    925 		switch (nxt) {
    926 		case IPPROTO_TCP:
    927 			spidx->ul_proto = nxt;
    928 			if (!needport)
    929 				goto done_proto;
    930 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
    931 				goto done;
    932 			m_copydata(m, off, sizeof(th), &th);
    933 			spidx->src.sin.sin_port = th.th_sport;
    934 			spidx->dst.sin.sin_port = th.th_dport;
    935 			return;
    936 		case IPPROTO_UDP:
    937 			spidx->ul_proto = nxt;
    938 			if (!needport)
    939 				goto done_proto;
    940 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
    941 				goto done;
    942 			m_copydata(m, off, sizeof(uh), &uh);
    943 			spidx->src.sin.sin_port = uh.uh_sport;
    944 			spidx->dst.sin.sin_port = uh.uh_dport;
    945 			return;
    946 		case IPPROTO_AH:
    947 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
    948 				goto done;
    949 			/* XXX sigh, this works but is totally bogus */
    950 			m_copydata(m, off, sizeof(ip6e), &ip6e);
    951 			off += (ip6e.ip6e_len + 2) << 2;
    952 			nxt = ip6e.ip6e_nxt;
    953 			break;
    954 		case IPPROTO_ICMP:
    955 			spidx->ul_proto = nxt;
    956 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
    957 				goto done;
    958 			m_copydata(m, off, sizeof(icmph), &icmph);
    959 			((struct sockaddr_in *)&spidx->src)->sin_port =
    960 			    htons((uint16_t)icmph.icmp_type);
    961 			((struct sockaddr_in *)&spidx->dst)->sin_port =
    962 			    htons((uint16_t)icmph.icmp_code);
    963 			return;
    964 		default:
    965 			/* XXX intermediate headers??? */
    966 			spidx->ul_proto = nxt;
    967 			goto done_proto;
    968 		}
    969 	}
    970 done:
    971 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
    972 done_proto:
    973 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
    974 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
    975 }
    976 
    977 static int
    978 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
    979 {
    980 	static const struct sockaddr_in template = {
    981 		sizeof(struct sockaddr_in),
    982 		AF_INET,
    983 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
    984 	};
    985 
    986 	spidx->src.sin = template;
    987 	spidx->dst.sin = template;
    988 
    989 	if (m->m_len < sizeof(struct ip)) {
    990 		m_copydata(m, offsetof(struct ip, ip_src),
    991 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
    992 		m_copydata(m, offsetof(struct ip, ip_dst),
    993 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
    994 	} else {
    995 		struct ip *ip = mtod(m, struct ip *);
    996 		spidx->src.sin.sin_addr = ip->ip_src;
    997 		spidx->dst.sin.sin_addr = ip->ip_dst;
    998 	}
    999 
   1000 	spidx->prefs = sizeof(struct in_addr) << 3;
   1001 	spidx->prefd = sizeof(struct in_addr) << 3;
   1002 
   1003 	return 0;
   1004 }
   1005 
   1006 #ifdef INET6
   1007 static void
   1008 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
   1009 {
   1010 	int off, nxt;
   1011 	struct tcphdr th;
   1012 	struct udphdr uh;
   1013 	struct icmp6_hdr icmph;
   1014 
   1015 	KASSERT(m != NULL);
   1016 
   1017 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1018 		kdebug_mbuf(__func__, m);
   1019 	}
   1020 
   1021 	/* set default */
   1022 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1023 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
   1024 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
   1025 
   1026 	nxt = -1;
   1027 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
   1028 	if (off < 0 || m->m_pkthdr.len < off)
   1029 		return;
   1030 
   1031 	switch (nxt) {
   1032 	case IPPROTO_TCP:
   1033 		spidx->ul_proto = nxt;
   1034 		if (!needport)
   1035 			break;
   1036 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1037 			break;
   1038 		m_copydata(m, off, sizeof(th), &th);
   1039 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
   1040 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
   1041 		break;
   1042 	case IPPROTO_UDP:
   1043 		spidx->ul_proto = nxt;
   1044 		if (!needport)
   1045 			break;
   1046 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1047 			break;
   1048 		m_copydata(m, off, sizeof(uh), &uh);
   1049 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
   1050 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
   1051 		break;
   1052 	case IPPROTO_ICMPV6:
   1053 		spidx->ul_proto = nxt;
   1054 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
   1055 			break;
   1056 		m_copydata(m, off, sizeof(icmph), &icmph);
   1057 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
   1058 		    htons((uint16_t)icmph.icmp6_type);
   1059 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
   1060 		    htons((uint16_t)icmph.icmp6_code);
   1061 		break;
   1062 	default:
   1063 		/* XXX intermediate headers??? */
   1064 		spidx->ul_proto = nxt;
   1065 		break;
   1066 	}
   1067 }
   1068 
   1069 static int
   1070 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1071 {
   1072 	struct ip6_hdr *ip6 = NULL;
   1073 	struct ip6_hdr ip6buf;
   1074 	struct sockaddr_in6 *sin6;
   1075 
   1076 	if (m->m_len >= sizeof(*ip6))
   1077 		ip6 = mtod(m, struct ip6_hdr *);
   1078 	else {
   1079 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
   1080 		ip6 = &ip6buf;
   1081 	}
   1082 
   1083 	sin6 = (struct sockaddr_in6 *)&spidx->src;
   1084 	memset(sin6, 0, sizeof(*sin6));
   1085 	sin6->sin6_family = AF_INET6;
   1086 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1087 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
   1088 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
   1089 		sin6->sin6_addr.s6_addr16[1] = 0;
   1090 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
   1091 	}
   1092 	spidx->prefs = sizeof(struct in6_addr) << 3;
   1093 
   1094 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
   1095 	memset(sin6, 0, sizeof(*sin6));
   1096 	sin6->sin6_family = AF_INET6;
   1097 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1098 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
   1099 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
   1100 		sin6->sin6_addr.s6_addr16[1] = 0;
   1101 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
   1102 	}
   1103 	spidx->prefd = sizeof(struct in6_addr) << 3;
   1104 
   1105 	return 0;
   1106 }
   1107 #endif
   1108 
   1109 static void
   1110 ipsec_delpcbpolicy(struct inpcbpolicy *p)
   1111 {
   1112 
   1113 	kmem_intr_free(p, sizeof(*p));
   1114 }
   1115 
   1116 /* initialize policy in PCB */
   1117 int
   1118 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
   1119 {
   1120 	struct inpcbpolicy *new;
   1121 
   1122 	KASSERT(so != NULL);
   1123 	KASSERT(policy != NULL);
   1124 
   1125 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
   1126 	if (new == NULL) {
   1127 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1128 		return ENOBUFS;
   1129 	}
   1130 
   1131 	if (IPSEC_PRIVILEGED_SO(so))
   1132 		new->priv = 1;
   1133 	else
   1134 		new->priv = 0;
   1135 
   1136 	/*
   1137 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
   1138 	 */
   1139 	new->sp_in = &ipsec_dummy_sp;
   1140 	new->sp_out = &ipsec_dummy_sp;
   1141 
   1142 	*policy = new;
   1143 
   1144 	return 0;
   1145 }
   1146 
   1147 #if 0 /* unused */
   1148 /* copy old ipsec policy into new */
   1149 int
   1150 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
   1151 {
   1152 	struct secpolicy *sp;
   1153 
   1154 	sp = ipsec_deepcopy_policy(old->sp_in);
   1155 	if (sp) {
   1156 		KEY_SP_UNREF(&new->sp_in);
   1157 		new->sp_in = sp;
   1158 	} else
   1159 		return ENOBUFS;
   1160 
   1161 	sp = ipsec_deepcopy_policy(old->sp_out);
   1162 	if (sp) {
   1163 		KEY_SP_UNREF(&new->sp_out);
   1164 		new->sp_out = sp;
   1165 	} else
   1166 		return ENOBUFS;
   1167 
   1168 	new->priv = old->priv;
   1169 
   1170 	return 0;
   1171 }
   1172 
   1173 /* deep-copy a policy in PCB */
   1174 static struct secpolicy *
   1175 ipsec_deepcopy_policy(const struct secpolicy *src)
   1176 {
   1177 	struct ipsecrequest *newchain = NULL;
   1178 	const struct ipsecrequest *p;
   1179 	struct ipsecrequest **q;
   1180 	struct secpolicy *dst;
   1181 
   1182 	if (src == NULL)
   1183 		return NULL;
   1184 	dst = KEY_NEWSP();
   1185 	if (dst == NULL)
   1186 		return NULL;
   1187 
   1188 	/*
   1189 	 * deep-copy IPsec request chain.  This is required since struct
   1190 	 * ipsecrequest is not reference counted.
   1191 	 */
   1192 	q = &newchain;
   1193 	for (p = src->req; p; p = p->next) {
   1194 		*q = kmem_zalloc(sizeof(**q), KM_SLEEP);
   1195 		(*q)->next = NULL;
   1196 
   1197 		(*q)->saidx.proto = p->saidx.proto;
   1198 		(*q)->saidx.mode = p->saidx.mode;
   1199 		(*q)->level = p->level;
   1200 		(*q)->saidx.reqid = p->saidx.reqid;
   1201 
   1202 		memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
   1203 		memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
   1204 
   1205 		(*q)->sp = dst;
   1206 
   1207 		q = &((*q)->next);
   1208 	}
   1209 
   1210 	dst->req = newchain;
   1211 	dst->state = src->state;
   1212 	dst->policy = src->policy;
   1213 	/* do not touch the refcnt fields */
   1214 
   1215 	return dst;
   1216 }
   1217 #endif
   1218 
   1219 static void
   1220 ipsec_destroy_policy(struct secpolicy *sp)
   1221 {
   1222 
   1223 	if (sp == &ipsec_dummy_sp)
   1224 		; /* It's dummy. No need to free it. */
   1225 	else {
   1226 		/*
   1227 		 * We cannot destroy here because it can be called in
   1228 		 * softint. So mark the SP as DEAD and let the timer
   1229 		 * destroy it. See key_timehandler_spd.
   1230 		 */
   1231 		sp->state = IPSEC_SPSTATE_DEAD;
   1232 	}
   1233 }
   1234 
   1235 int
   1236 ipsec_set_policy(void *inp, int optname, const void *request, size_t len,
   1237     kauth_cred_t cred)
   1238 {
   1239 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1240 	const struct sadb_x_policy *xpl;
   1241 	struct secpolicy *newsp, *oldsp;
   1242 	struct secpolicy **policy;
   1243 	int error;
   1244 
   1245 	KASSERT(!cpu_softintr_p());
   1246 	KASSERT(inph != NULL);
   1247 	KASSERT(inph_locked(inph));
   1248 	KASSERT(request != NULL);
   1249 
   1250 	if (len < sizeof(*xpl))
   1251 		return EINVAL;
   1252 	xpl = (const struct sadb_x_policy *)request;
   1253 
   1254 	KASSERT(inph->inph_sp != NULL);
   1255 
   1256 	/* select direction */
   1257 	switch (xpl->sadb_x_policy_dir) {
   1258 	case IPSEC_DIR_INBOUND:
   1259 		policy = &inph->inph_sp->sp_in;
   1260 		break;
   1261 	case IPSEC_DIR_OUTBOUND:
   1262 		policy = &inph->inph_sp->sp_out;
   1263 		break;
   1264 	default:
   1265 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1266 		    xpl->sadb_x_policy_dir);
   1267 		return EINVAL;
   1268 	}
   1269 
   1270 	/* sanity check. */
   1271 	if (policy == NULL || *policy == NULL)
   1272 		return EINVAL;
   1273 
   1274 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1275 		kdebug_sadb_xpolicy("set passed policy", request);
   1276 	}
   1277 
   1278 	/* check policy type */
   1279 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
   1280 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
   1281 	    xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
   1282 		return EINVAL;
   1283 
   1284 	/* check privileged socket */
   1285 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
   1286 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
   1287 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
   1288 		if (error)
   1289 			return error;
   1290 	}
   1291 
   1292 	/* allocation new SP entry */
   1293 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
   1294 		return error;
   1295 
   1296 	key_init_sp(newsp);
   1297 	newsp->created = time_uptime;
   1298 	/* Insert the global list for SPs for sockets */
   1299 	key_socksplist_add(newsp);
   1300 
   1301 	/* clear old SP and set new SP */
   1302 	oldsp = *policy;
   1303 	*policy = newsp;
   1304 	ipsec_destroy_policy(oldsp);
   1305 
   1306 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1307 		printf("%s: new policy\n", __func__);
   1308 		kdebug_secpolicy(newsp);
   1309 	}
   1310 
   1311 	return 0;
   1312 }
   1313 
   1314 int
   1315 ipsec_get_policy(void *inp, const void *request, size_t len,
   1316     struct mbuf **mp)
   1317 {
   1318 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1319 	const struct sadb_x_policy *xpl;
   1320 	struct secpolicy *policy;
   1321 
   1322 	/* sanity check. */
   1323 	if (inph == NULL || request == NULL || mp == NULL)
   1324 		return EINVAL;
   1325 	KASSERT(inph->inph_sp != NULL);
   1326 	if (len < sizeof(*xpl))
   1327 		return EINVAL;
   1328 	xpl = (const struct sadb_x_policy *)request;
   1329 
   1330 	/* select direction */
   1331 	switch (xpl->sadb_x_policy_dir) {
   1332 	case IPSEC_DIR_INBOUND:
   1333 		policy = inph->inph_sp->sp_in;
   1334 		break;
   1335 	case IPSEC_DIR_OUTBOUND:
   1336 		policy = inph->inph_sp->sp_out;
   1337 		break;
   1338 	default:
   1339 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1340 		    xpl->sadb_x_policy_dir);
   1341 		return EINVAL;
   1342 	}
   1343 
   1344 	if (policy == NULL)
   1345 		return EINVAL;
   1346 
   1347 	*mp = key_sp2msg(policy, M_NOWAIT);
   1348 	if (!*mp) {
   1349 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1350 		return ENOBUFS;
   1351 	}
   1352 
   1353 	(*mp)->m_type = MT_DATA;
   1354 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1355 		kdebug_mbuf(__func__, *mp);
   1356 	}
   1357 
   1358 	return 0;
   1359 }
   1360 
   1361 int
   1362 ipsec_delete_pcbpolicy(void *inp)
   1363 {
   1364 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1365 
   1366 	KASSERT(inph != NULL);
   1367 
   1368 	if (inph->inph_sp == NULL)
   1369 		return 0;
   1370 
   1371 	if (inph->inph_sp->sp_in != NULL)
   1372 		ipsec_destroy_policy(inph->inph_sp->sp_in);
   1373 
   1374 	if (inph->inph_sp->sp_out != NULL)
   1375 		ipsec_destroy_policy(inph->inph_sp->sp_out);
   1376 
   1377 	ipsec_invalpcbcache(inph->inph_sp, IPSEC_DIR_ANY);
   1378 
   1379 	ipsec_delpcbpolicy(inph->inph_sp);
   1380 	inph->inph_sp = NULL;
   1381 
   1382 	return 0;
   1383 }
   1384 
   1385 /*
   1386  * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
   1387  */
   1388 u_int
   1389 ipsec_get_reqlevel(const struct ipsecrequest *isr)
   1390 {
   1391 	u_int level = 0;
   1392 	u_int esp_trans_deflev, esp_net_deflev;
   1393 	u_int ah_trans_deflev, ah_net_deflev;
   1394 
   1395 	KASSERT(isr != NULL);
   1396 	KASSERT(isr->sp != NULL);
   1397 	KASSERTMSG(
   1398 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
   1399 	    "af family mismatch, src %u, dst %u",
   1400 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
   1401 
   1402 /* XXX note that we have ipseclog() expanded here - code sync issue */
   1403 #define IPSEC_CHECK_DEFAULT(lev)					\
   1404     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
   1405     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
   1406 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
   1407 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
   1408 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
   1409     : (lev))
   1410 
   1411 	/* set default level */
   1412 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
   1413 #ifdef INET
   1414 	case AF_INET:
   1415 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
   1416 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
   1417 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
   1418 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
   1419 		break;
   1420 #endif
   1421 #ifdef INET6
   1422 	case AF_INET6:
   1423 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
   1424 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
   1425 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
   1426 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
   1427 		break;
   1428 #endif /* INET6 */
   1429 	default:
   1430 		panic("%s: unknown af %u", __func__,
   1431 		    isr->sp->spidx.src.sa.sa_family);
   1432 	}
   1433 
   1434 #undef IPSEC_CHECK_DEFAULT
   1435 
   1436 	/* set level */
   1437 	switch (isr->level) {
   1438 	case IPSEC_LEVEL_DEFAULT:
   1439 		switch (isr->saidx.proto) {
   1440 		case IPPROTO_ESP:
   1441 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1442 				level = esp_net_deflev;
   1443 			else
   1444 				level = esp_trans_deflev;
   1445 			break;
   1446 		case IPPROTO_AH:
   1447 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1448 				level = ah_net_deflev;
   1449 			else
   1450 				level = ah_trans_deflev;
   1451 			break;
   1452 		case IPPROTO_IPCOMP:
   1453 			/*
   1454 			 * we don't really care, as IPcomp document says that
   1455 			 * we shouldn't compress small packets
   1456 			 */
   1457 			level = IPSEC_LEVEL_USE;
   1458 			break;
   1459 		default:
   1460 			panic("%s: Illegal protocol defined %u", __func__,
   1461 			    isr->saidx.proto);
   1462 		}
   1463 		break;
   1464 
   1465 	case IPSEC_LEVEL_USE:
   1466 	case IPSEC_LEVEL_REQUIRE:
   1467 		level = isr->level;
   1468 		break;
   1469 	case IPSEC_LEVEL_UNIQUE:
   1470 		level = IPSEC_LEVEL_REQUIRE;
   1471 		break;
   1472 
   1473 	default:
   1474 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
   1475 	}
   1476 
   1477 	return level;
   1478 }
   1479 
   1480 /*
   1481  * Check security policy requirements against the actual packet contents.
   1482  *
   1483  * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
   1484  * then kick it.
   1485  */
   1486 static int
   1487 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
   1488 {
   1489 	struct ipsecrequest *isr;
   1490 
   1491 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1492 		printf("%s: using SP\n", __func__);
   1493 		kdebug_secpolicy(sp);
   1494 	}
   1495 
   1496 	/* check policy */
   1497 	switch (sp->policy) {
   1498 	case IPSEC_POLICY_DISCARD:
   1499 		return 1;
   1500 	case IPSEC_POLICY_BYPASS:
   1501 	case IPSEC_POLICY_NONE:
   1502 		return 0;
   1503 	}
   1504 
   1505 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1506 	    "invalid policy %u", sp->policy);
   1507 
   1508 	/* XXX should compare policy against ipsec header history */
   1509 
   1510 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1511 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
   1512 			continue;
   1513 		switch (isr->saidx.proto) {
   1514 		case IPPROTO_ESP:
   1515 			if ((m->m_flags & M_DECRYPTED) == 0) {
   1516 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1517 				    "ESP m_flags:%x\n", m->m_flags);
   1518 				return 1;
   1519 			}
   1520 			break;
   1521 		case IPPROTO_AH:
   1522 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
   1523 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1524 				    "AH m_flags:%x\n", m->m_flags);
   1525 				return 1;
   1526 			}
   1527 			break;
   1528 		case IPPROTO_IPCOMP:
   1529 			/*
   1530 			 * We don't really care, as IPcomp document
   1531 			 * says that we shouldn't compress small
   1532 			 * packets, IPComp policy should always be
   1533 			 * treated as being in "use" level.
   1534 			 */
   1535 			break;
   1536 		}
   1537 	}
   1538 
   1539 	return 0;
   1540 }
   1541 
   1542 /*
   1543  * Check security policy requirements.
   1544  */
   1545 int
   1546 ipsec_in_reject(struct mbuf *m, void *inp)
   1547 {
   1548 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1549 	struct secpolicy *sp;
   1550 	int error;
   1551 	int result;
   1552 
   1553 	KASSERT(m != NULL);
   1554 
   1555 	if (inph == NULL)
   1556 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1557 		    IP_FORWARDING, &error);
   1558 	else
   1559 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1560 		    inph, &error);
   1561 
   1562 	if (sp != NULL) {
   1563 		result = ipsec_sp_reject(sp, m);
   1564 		if (result)
   1565 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1566 		KEY_SP_UNREF(&sp);
   1567 	} else {
   1568 		result = 0;
   1569 	}
   1570 	return result;
   1571 }
   1572 
   1573 /*
   1574  * Compute the byte size to be occupied by the IPsec header. If it is
   1575  * tunneled, it includes the size of outer IP header.
   1576  */
   1577 static size_t
   1578 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
   1579 {
   1580 	struct ipsecrequest *isr;
   1581 	size_t siz;
   1582 
   1583 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1584 		printf("%s: using SP\n", __func__);
   1585 		kdebug_secpolicy(sp);
   1586 	}
   1587 
   1588 	switch (sp->policy) {
   1589 	case IPSEC_POLICY_DISCARD:
   1590 	case IPSEC_POLICY_BYPASS:
   1591 	case IPSEC_POLICY_NONE:
   1592 		return 0;
   1593 	}
   1594 
   1595 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1596 	    "invalid policy %u", sp->policy);
   1597 
   1598 	siz = 0;
   1599 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1600 		size_t clen = 0;
   1601 		struct secasvar *sav;
   1602 
   1603 		switch (isr->saidx.proto) {
   1604 		case IPPROTO_ESP:
   1605 			sav = ipsec_lookup_sa(isr, m);
   1606 			if (sav != NULL) {
   1607 				clen = esp_hdrsiz(sav);
   1608 				KEY_SA_UNREF(&sav);
   1609 			} else
   1610 				clen = esp_hdrsiz(NULL);
   1611 			break;
   1612 		case IPPROTO_AH:
   1613 			sav = ipsec_lookup_sa(isr, m);
   1614 			if (sav != NULL) {
   1615 				clen = ah_hdrsiz(sav);
   1616 				KEY_SA_UNREF(&sav);
   1617 			} else
   1618 				clen = ah_hdrsiz(NULL);
   1619 			break;
   1620 		case IPPROTO_IPCOMP:
   1621 			clen = sizeof(struct ipcomp);
   1622 			break;
   1623 		}
   1624 
   1625 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
   1626 			switch (isr->saidx.dst.sa.sa_family) {
   1627 			case AF_INET:
   1628 				clen += sizeof(struct ip);
   1629 				break;
   1630 #ifdef INET6
   1631 			case AF_INET6:
   1632 				clen += sizeof(struct ip6_hdr);
   1633 				break;
   1634 #endif
   1635 			default:
   1636 				IPSECLOG(LOG_ERR, "unknown AF %d in "
   1637 				    "IPsec tunnel SA\n",
   1638 				    ((const struct sockaddr *)&isr->saidx.dst)
   1639 				    ->sa_family);
   1640 				break;
   1641 			}
   1642 		}
   1643 		siz += clen;
   1644 	}
   1645 
   1646 	return siz;
   1647 }
   1648 
   1649 size_t
   1650 ipsec_hdrsiz(struct mbuf *m, u_int dir, void *inp)
   1651 {
   1652 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1653 	struct secpolicy *sp;
   1654 	int error;
   1655 	size_t size;
   1656 
   1657 	KASSERT(m != NULL);
   1658 	KASSERTMSG(inph == NULL || inph->inph_socket != NULL,
   1659 	    "socket w/o inpcb");
   1660 
   1661 	if (inph == NULL)
   1662 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1663 	else
   1664 		sp = ipsec_getpolicybysock(m, dir, inph, &error);
   1665 
   1666 	if (sp != NULL) {
   1667 		size = ipsec_sp_hdrsiz(sp, m);
   1668 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   1669 		KEY_SP_UNREF(&sp);
   1670 	} else {
   1671 		size = 0;
   1672 	}
   1673 
   1674 	return size;
   1675 }
   1676 
   1677 /*
   1678  * Check the variable replay window.
   1679  * ipsec_chkreplay() performs replay check before ICV verification.
   1680  * ipsec_updatereplay() updates replay bitmap.  This must be called after
   1681  * ICV verification (it also performs replay check, which is usually done
   1682  * beforehand).
   1683  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
   1684  *
   1685  * based on RFC 2401.
   1686  */
   1687 int
   1688 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
   1689 {
   1690 	const struct secreplay *replay;
   1691 	u_int32_t diff;
   1692 	int fr;
   1693 	u_int32_t wsizeb;	/* constant: bits of window size */
   1694 	int frlast;		/* constant: last frame */
   1695 
   1696 	IPSEC_SPLASSERT_SOFTNET(__func__);
   1697 
   1698 	KASSERT(sav != NULL);
   1699 	KASSERT(sav->replay != NULL);
   1700 
   1701 	replay = sav->replay;
   1702 
   1703 	if (replay->wsize == 0)
   1704 		return 1;	/* no need to check replay. */
   1705 
   1706 	/* constant */
   1707 	frlast = replay->wsize - 1;
   1708 	wsizeb = replay->wsize << 3;
   1709 
   1710 	/* sequence number of 0 is invalid */
   1711 	if (seq == 0)
   1712 		return 0;
   1713 
   1714 	/* first time is always okay */
   1715 	if (replay->count == 0)
   1716 		return 1;
   1717 
   1718 	if (seq > replay->lastseq) {
   1719 		/* larger sequences are okay */
   1720 		return 1;
   1721 	} else {
   1722 		/* seq is equal or less than lastseq. */
   1723 		diff = replay->lastseq - seq;
   1724 
   1725 		/* over range to check, i.e. too old or wrapped */
   1726 		if (diff >= wsizeb)
   1727 			return 0;
   1728 
   1729 		fr = frlast - diff / 8;
   1730 
   1731 		/* this packet already seen ? */
   1732 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1733 			return 0;
   1734 
   1735 		/* out of order but good */
   1736 		return 1;
   1737 	}
   1738 }
   1739 
   1740 /*
   1741  * check replay counter whether to update or not.
   1742  * OUT:	0:	OK
   1743  *	1:	NG
   1744  */
   1745 int
   1746 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
   1747 {
   1748 	struct secreplay *replay;
   1749 	u_int32_t diff;
   1750 	int fr;
   1751 	u_int32_t wsizeb;	/* constant: bits of window size */
   1752 	int frlast;		/* constant: last frame */
   1753 
   1754 	IPSEC_SPLASSERT_SOFTNET(__func__);
   1755 
   1756 	KASSERT(sav != NULL);
   1757 	KASSERT(sav->replay != NULL);
   1758 
   1759 	replay = sav->replay;
   1760 
   1761 	if (replay->wsize == 0)
   1762 		goto ok;	/* no need to check replay. */
   1763 
   1764 	/* constant */
   1765 	frlast = replay->wsize - 1;
   1766 	wsizeb = replay->wsize << 3;
   1767 
   1768 	/* sequence number of 0 is invalid */
   1769 	if (seq == 0)
   1770 		return 1;
   1771 
   1772 	/* first time */
   1773 	if (replay->count == 0) {
   1774 		replay->lastseq = seq;
   1775 		memset(replay->bitmap, 0, replay->wsize);
   1776 		(replay->bitmap)[frlast] = 1;
   1777 		goto ok;
   1778 	}
   1779 
   1780 	if (seq > replay->lastseq) {
   1781 		/* seq is larger than lastseq. */
   1782 		diff = seq - replay->lastseq;
   1783 
   1784 		/* new larger sequence number */
   1785 		if (diff < wsizeb) {
   1786 			/* In window */
   1787 			/* set bit for this packet */
   1788 			vshiftl(replay->bitmap, diff, replay->wsize);
   1789 			(replay->bitmap)[frlast] |= 1;
   1790 		} else {
   1791 			/* this packet has a "way larger" */
   1792 			memset(replay->bitmap, 0, replay->wsize);
   1793 			(replay->bitmap)[frlast] = 1;
   1794 		}
   1795 		replay->lastseq = seq;
   1796 
   1797 		/* larger is good */
   1798 	} else {
   1799 		/* seq is equal or less than lastseq. */
   1800 		diff = replay->lastseq - seq;
   1801 
   1802 		/* over range to check, i.e. too old or wrapped */
   1803 		if (diff >= wsizeb)
   1804 			return 1;
   1805 
   1806 		fr = frlast - diff / 8;
   1807 
   1808 		/* this packet already seen ? */
   1809 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1810 			return 1;
   1811 
   1812 		/* mark as seen */
   1813 		(replay->bitmap)[fr] |= (1 << (diff % 8));
   1814 
   1815 		/* out of order but good */
   1816 	}
   1817 
   1818 ok:
   1819 	if (replay->count == ~0) {
   1820 		char buf[IPSEC_LOGSASTRLEN];
   1821 
   1822 		/* set overflow flag */
   1823 		replay->overflow++;
   1824 
   1825 		/* don't increment, no more packets accepted */
   1826 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
   1827 			return 1;
   1828 
   1829 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
   1830 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
   1831 	}
   1832 
   1833 	replay->count++;
   1834 
   1835 	return 0;
   1836 }
   1837 
   1838 /*
   1839  * shift variable length buffer to left.
   1840  * IN:	bitmap: pointer to the buffer
   1841  *	nbit:	the number of to shift.
   1842  *	wsize:	buffer size (bytes).
   1843  */
   1844 static void
   1845 vshiftl(unsigned char *bitmap, int nbit, int wsize)
   1846 {
   1847 	int s, j, i;
   1848 	unsigned char over;
   1849 
   1850 	for (j = 0; j < nbit; j += 8) {
   1851 		s = (nbit - j < 8) ? (nbit - j): 8;
   1852 		bitmap[0] <<= s;
   1853 		for (i = 1; i < wsize; i++) {
   1854 			over = (bitmap[i] >> (8 - s));
   1855 			bitmap[i] <<= s;
   1856 			bitmap[i-1] |= over;
   1857 		}
   1858 	}
   1859 
   1860 	return;
   1861 }
   1862 
   1863 /* Return a printable string for the address. */
   1864 const char *
   1865 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
   1866 {
   1867 	switch (sa->sa.sa_family) {
   1868 #if INET
   1869 	case AF_INET:
   1870 		in_print(buf, size, &sa->sin.sin_addr);
   1871 		return buf;
   1872 #endif
   1873 #if INET6
   1874 	case AF_INET6:
   1875 		in6_print(buf, size, &sa->sin6.sin6_addr);
   1876 		return buf;
   1877 #endif
   1878 	default:
   1879 		return "(unknown address family)";
   1880 	}
   1881 }
   1882 
   1883 const char *
   1884 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
   1885 {
   1886 	const struct secasindex *saidx = &sav->sah->saidx;
   1887 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
   1888 
   1889 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
   1890 	    "af family mismatch, src %u, dst %u",
   1891 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
   1892 
   1893 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
   1894 	    (u_int32_t)ntohl(sav->spi),
   1895 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
   1896 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
   1897 
   1898 	return buf;
   1899 }
   1900 
   1901 void
   1902 ipsec_dumpmbuf(struct mbuf *m)
   1903 {
   1904 	int totlen;
   1905 	int i;
   1906 	u_char *p;
   1907 
   1908 	totlen = 0;
   1909 	printf("---\n");
   1910 	while (m) {
   1911 		p = mtod(m, u_char *);
   1912 		for (i = 0; i < m->m_len; i++) {
   1913 			printf("%02x ", p[i]);
   1914 			totlen++;
   1915 			if (totlen % 16 == 0)
   1916 				printf("\n");
   1917 		}
   1918 		m = m->m_next;
   1919 	}
   1920 	if (totlen % 16 != 0)
   1921 		printf("\n");
   1922 	printf("---\n");
   1923 }
   1924 
   1925 #ifdef INET6
   1926 struct secpolicy *
   1927 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p, int flags,
   1928     int *needipsecp, int *errorp)
   1929 {
   1930 	struct secpolicy *sp = NULL;
   1931 	int s;
   1932 	int error = 0;
   1933 	int needipsec = 0;
   1934 
   1935 	if (!ipsec_outdone(m)) {
   1936 		s = splsoftnet();
   1937 		if (in6p != NULL &&
   1938 		    ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
   1939 			splx(s);
   1940 			goto skippolicycheck;
   1941 		}
   1942 		sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,
   1943 		    in6p);
   1944 
   1945 		/*
   1946 		 * There are four return cases:
   1947 		 *	sp != NULL			apply IPsec policy
   1948 		 *	sp == NULL, error == 0		no IPsec handling needed
   1949 		 *	sp == NULL, error == -EINVAL  discard packet w/o error
   1950 		 *	sp == NULL, error != 0		discard packet, report error
   1951 		 */
   1952 
   1953 		splx(s);
   1954 		if (sp == NULL) {
   1955 			/*
   1956 			 * Caller must check the error return to see if it needs to discard
   1957 			 * the packet.
   1958 			 */
   1959 			needipsec = 0;
   1960 		} else {
   1961 			needipsec = 1;
   1962 		}
   1963 	}
   1964 skippolicycheck:;
   1965 
   1966 	*errorp = error;
   1967 	*needipsecp = needipsec;
   1968 	return sp;
   1969 }
   1970 
   1971 int
   1972 ipsec6_input(struct mbuf *m)
   1973 {
   1974 	int s, error;
   1975 
   1976 	s = splsoftnet();
   1977 	error = ipsec_in_reject(m, NULL);
   1978 	splx(s);
   1979 	if (error) {
   1980 		return EINVAL;
   1981 	}
   1982 
   1983 	return 0;
   1984 }
   1985 #endif /* INET6 */
   1986 
   1987 /*
   1988  * -----------------------------------------------------------------------------
   1989  */
   1990 
   1991 /* XXX this stuff doesn't belong here... */
   1992 
   1993 static struct xformsw *xforms = NULL;
   1994 
   1995 /*
   1996  * Register a transform; typically at system startup.
   1997  */
   1998 void
   1999 xform_register(struct xformsw *xsp)
   2000 {
   2001 	xsp->xf_next = xforms;
   2002 	xforms = xsp;
   2003 }
   2004 
   2005 /*
   2006  * Initialize transform support in an sav.
   2007  */
   2008 int
   2009 xform_init(struct secasvar *sav, int xftype)
   2010 {
   2011 	struct xformsw *xsp;
   2012 
   2013 	if (sav->tdb_xform != NULL)	/* previously initialized */
   2014 		return 0;
   2015 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
   2016 		if (xsp->xf_type == xftype)
   2017 			return (*xsp->xf_init)(sav, xsp);
   2018 
   2019 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
   2020 	return EINVAL;
   2021 }
   2022 
   2023 void
   2024 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport)
   2025 {
   2026 	struct m_tag *tag;
   2027 
   2028 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
   2029 		*sport = ((u_int16_t *)(tag + 1))[0];
   2030 		*dport = ((u_int16_t *)(tag + 1))[1];
   2031 	} else
   2032 		*sport = *dport = 0;
   2033 }
   2034 
   2035 /*
   2036  * XXXJRT This should be done as a protosw init call.
   2037  */
   2038 void
   2039 ipsec_attach(void)
   2040 {
   2041 
   2042 	ipsec_output_init();
   2043 
   2044 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
   2045 
   2046 	sysctl_net_inet_ipsec_setup(NULL);
   2047 #ifdef INET6
   2048 	sysctl_net_inet6_ipsec6_setup(NULL);
   2049 #endif
   2050 
   2051 	ah_attach();
   2052 	esp_attach();
   2053 	ipcomp_attach();
   2054 	ipe4_attach();
   2055 #ifdef TCP_SIGNATURE
   2056 	tcpsignature_attach();
   2057 #endif
   2058 }
   2059