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