Home | History | Annotate | Line # | Download | only in netipsec
ipsec.c revision 1.174
      1 /* $NetBSD: ipsec.c,v 1.174 2022/10/28 05:18:39 ozaki-r 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.174 2022/10/28 05:18:39 ozaki-r Exp $");
     36 
     37 /*
     38  * IPsec controller part.
     39  */
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_inet.h"
     43 #include "opt_ipsec.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/domain.h>
     50 #include <sys/protosw.h>
     51 #include <sys/socket.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/errno.h>
     54 #include <sys/time.h>
     55 #include <sys/kernel.h>
     56 #include <sys/syslog.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/proc.h>
     59 #include <sys/kauth.h>
     60 #include <sys/cpu.h>
     61 #include <sys/kmem.h>
     62 #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 *, 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 *, struct inpcb *);
    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(inp_locked(pcbsp->sp_inp));
    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(inp_locked(pcbsp->sp_inp));
    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(inp_locked(pcbsp->sp_inp));
    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(inp_locked(pcbsp->sp_inp));
    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(inp_locked(pcbsp->sp_inp));
    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 *inp,
    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(inp != NULL);
    412 	KASSERT(error != NULL);
    413 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    414 
    415 	KASSERT(inp->inp_socket != NULL);
    416 	KASSERT(inp_locked(inp));
    417 
    418 	/* XXX FIXME inpcb vs socket*/
    419 	af = inp->inp_af;
    420 	KASSERTMSG(af == AF_INET || af == AF_INET6,
    421 	    "unexpected protocol family %u", af);
    422 
    423 	KASSERT(inp->inp_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, inp->inp_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, inp);
    439 		pcbsp = inp->inp_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     struct inpcb *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 		KASSERT(inp->inp_socket != NULL);
    579 		sp = ipsec_getpolicybysock(m, dir, inp, error);
    580 	}
    581 	if (sp == NULL) {
    582 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    583 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    584 		return NULL;
    585 	}
    586 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    587 
    588 	switch (sp->policy) {
    589 	case IPSEC_POLICY_ENTRUST:
    590 	default:
    591 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    592 		/* fall thru... */
    593 	case IPSEC_POLICY_DISCARD:
    594 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    595 		*error = -EINVAL;	/* packet is discarded by caller */
    596 		break;
    597 	case IPSEC_POLICY_BYPASS:
    598 	case IPSEC_POLICY_NONE:
    599 		KEY_SP_UNREF(&sp);
    600 		sp = NULL;		/* NB: force NULL result */
    601 		break;
    602 	case IPSEC_POLICY_IPSEC:
    603 		KASSERT(sp->req != NULL);
    604 		break;
    605 	}
    606 
    607 	if (*error != 0) {
    608 		KEY_SP_UNREF(&sp);
    609 		sp = NULL;
    610 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    611 	}
    612 
    613 	return sp;
    614 }
    615 
    616 int
    617 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
    618     u_long *mtu, bool *natt_frag, bool *done, bool *count_drop)
    619 {
    620 	struct secpolicy *sp = NULL;
    621 	u_long _mtu = 0;
    622 	int error, s;
    623 
    624 	/*
    625 	 * Check the security policy (SP) for the packet and, if required,
    626 	 * do IPsec-related processing.  There are two cases here; the first
    627 	 * time a packet is sent through it will be untagged and handled by
    628 	 * ipsec_checkpolicy().  If the packet is resubmitted to ip_output
    629 	 * (e.g. after AH, ESP, etc. processing), there will be a tag to
    630 	 * bypass the lookup and related policy checking.
    631 	 */
    632 	if (ipsec_outdone(m)) {
    633 		return 0;
    634 	}
    635 	s = splsoftnet();
    636 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
    637 		splx(s);
    638 		return 0;
    639 	}
    640 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
    641 
    642 	/*
    643 	 * There are four return cases:
    644 	 *	sp != NULL                    apply IPsec policy
    645 	 *	sp == NULL, error == 0        no IPsec handling needed
    646 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
    647 	 *	sp == NULL, error != 0        discard packet, report error
    648 	 */
    649 	if (sp == NULL) {
    650 		splx(s);
    651 		if (error) {
    652 			/*
    653 			 * Hack: -EINVAL is used to signal that a packet
    654 			 * should be silently discarded.  This is typically
    655 			 * because we asked key management for an SA and
    656 			 * it was delayed (e.g. kicked up to IKE).
    657 			 */
    658 			if (error == -EINVAL)
    659 				error = 0;
    660 			m_freem(m);
    661 			*done = true;
    662 			*count_drop = 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_checkpolicy(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, struct inpcb *inp)
    791 {
    792 	int error;
    793 
    794 	KASSERT(inp != NULL);
    795 	KASSERT(inp->inp_sp != NULL);
    796 	KASSERT(inp->inp_sp->sp_out != NULL);
    797 	KASSERT(inp->inp_sp->sp_in != NULL);
    798 
    799 	error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx,
    800 	    IPSEC_DIR_INBOUND, 1);
    801 	if (error == 0) {
    802 		inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
    803 		inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
    804 	} else {
    805 		memset(&inp->inp_sp->sp_in->spidx, 0,
    806 		    sizeof(inp->inp_sp->sp_in->spidx));
    807 		memset(&inp->inp_sp->sp_out->spidx, 0,
    808 		    sizeof(inp->inp_sp->sp_out->spidx));
    809 	}
    810 	return error;
    811 }
    812 
    813 /*
    814  * configure security policy index (src/dst/proto/sport/dport)
    815  * by looking at the content of mbuf.
    816  * the caller is responsible for error recovery (like clearing up spidx).
    817  */
    818 static int
    819 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int dir,
    820     int needport)
    821 {
    822 	struct ip *ip = NULL;
    823 	struct ip ipbuf;
    824 	u_int v;
    825 	int error;
    826 
    827 	KASSERT(m != NULL);
    828 	M_VERIFY_PACKET(m);
    829 
    830 	if (m->m_pkthdr.len < sizeof(struct ip)) {
    831 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    832 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
    833 		    m->m_pkthdr.len);
    834 		return EINVAL;
    835 	}
    836 
    837 	memset(spidx, 0, sizeof(*spidx));
    838 	spidx->dir = dir;
    839 
    840 	if (m->m_len >= sizeof(*ip)) {
    841 		ip = mtod(m, struct ip *);
    842 	} else {
    843 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
    844 		ip = &ipbuf;
    845 	}
    846 	v = ip->ip_v;
    847 	switch (v) {
    848 	case 4:
    849 		error = ipsec4_setspidx_ipaddr(m, spidx);
    850 		if (error)
    851 			return error;
    852 		ipsec4_get_ulp(m, spidx, needport);
    853 		return 0;
    854 #ifdef INET6
    855 	case 6:
    856 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
    857 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    858 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
    859 			    "ignored.\n", m->m_pkthdr.len);
    860 			return EINVAL;
    861 		}
    862 		error = ipsec6_setspidx_ipaddr(m, spidx);
    863 		if (error)
    864 			return error;
    865 		ipsec6_get_ulp(m, spidx, needport);
    866 		return 0;
    867 #endif
    868 	default:
    869 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    870 		    "unknown IP version %u, ignored.\n", v);
    871 		return EINVAL;
    872 	}
    873 }
    874 
    875 static void
    876 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    877 {
    878 	u_int8_t nxt;
    879 	int off;
    880 
    881 	KASSERT(m != NULL);
    882 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
    883 
    884 	/* NB: ip_input() flips it into host endian XXX need more checking */
    885 	if (m->m_len >= sizeof(struct ip)) {
    886 		struct ip *ip = mtod(m, struct ip *);
    887 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
    888 			goto done;
    889 		off = ip->ip_hl << 2;
    890 		nxt = ip->ip_p;
    891 	} else {
    892 		struct ip ih;
    893 
    894 		m_copydata(m, 0, sizeof(struct ip), &ih);
    895 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
    896 			goto done;
    897 		off = ih.ip_hl << 2;
    898 		nxt = ih.ip_p;
    899 	}
    900 
    901 	while (off < m->m_pkthdr.len) {
    902 		struct ip6_ext ip6e;
    903 		struct tcphdr th;
    904 		struct udphdr uh;
    905 		struct icmp icmph;
    906 
    907 		switch (nxt) {
    908 		case IPPROTO_TCP:
    909 			spidx->ul_proto = nxt;
    910 			if (!needport)
    911 				goto done_proto;
    912 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
    913 				goto done;
    914 			m_copydata(m, off, sizeof(th), &th);
    915 			spidx->src.sin.sin_port = th.th_sport;
    916 			spidx->dst.sin.sin_port = th.th_dport;
    917 			return;
    918 		case IPPROTO_UDP:
    919 			spidx->ul_proto = nxt;
    920 			if (!needport)
    921 				goto done_proto;
    922 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
    923 				goto done;
    924 			m_copydata(m, off, sizeof(uh), &uh);
    925 			spidx->src.sin.sin_port = uh.uh_sport;
    926 			spidx->dst.sin.sin_port = uh.uh_dport;
    927 			return;
    928 		case IPPROTO_AH:
    929 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
    930 				goto done;
    931 			/* XXX sigh, this works but is totally bogus */
    932 			m_copydata(m, off, sizeof(ip6e), &ip6e);
    933 			off += (ip6e.ip6e_len + 2) << 2;
    934 			nxt = ip6e.ip6e_nxt;
    935 			break;
    936 		case IPPROTO_ICMP:
    937 			spidx->ul_proto = nxt;
    938 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
    939 				goto done;
    940 			m_copydata(m, off, sizeof(icmph), &icmph);
    941 			((struct sockaddr_in *)&spidx->src)->sin_port =
    942 			    htons((uint16_t)icmph.icmp_type);
    943 			((struct sockaddr_in *)&spidx->dst)->sin_port =
    944 			    htons((uint16_t)icmph.icmp_code);
    945 			return;
    946 		default:
    947 			/* XXX intermediate headers??? */
    948 			spidx->ul_proto = nxt;
    949 			goto done_proto;
    950 		}
    951 	}
    952 done:
    953 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
    954 done_proto:
    955 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
    956 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
    957 }
    958 
    959 static int
    960 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
    961 {
    962 	static const struct sockaddr_in template = {
    963 		sizeof(struct sockaddr_in),
    964 		AF_INET,
    965 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
    966 	};
    967 
    968 	spidx->src.sin = template;
    969 	spidx->dst.sin = template;
    970 
    971 	if (m->m_len < sizeof(struct ip)) {
    972 		m_copydata(m, offsetof(struct ip, ip_src),
    973 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
    974 		m_copydata(m, offsetof(struct ip, ip_dst),
    975 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
    976 	} else {
    977 		struct ip *ip = mtod(m, struct ip *);
    978 		spidx->src.sin.sin_addr = ip->ip_src;
    979 		spidx->dst.sin.sin_addr = ip->ip_dst;
    980 	}
    981 
    982 	spidx->prefs = sizeof(struct in_addr) << 3;
    983 	spidx->prefd = sizeof(struct in_addr) << 3;
    984 
    985 	return 0;
    986 }
    987 
    988 #ifdef INET6
    989 static void
    990 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    991 {
    992 	int off, nxt;
    993 	struct tcphdr th;
    994 	struct udphdr uh;
    995 	struct icmp6_hdr icmph;
    996 
    997 	KASSERT(m != NULL);
    998 
    999 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1000 		kdebug_mbuf(__func__, m);
   1001 	}
   1002 
   1003 	/* set default */
   1004 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1005 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
   1006 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
   1007 
   1008 	nxt = -1;
   1009 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
   1010 	if (off < 0 || m->m_pkthdr.len < off)
   1011 		return;
   1012 
   1013 	switch (nxt) {
   1014 	case IPPROTO_TCP:
   1015 		spidx->ul_proto = nxt;
   1016 		if (!needport)
   1017 			break;
   1018 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1019 			break;
   1020 		m_copydata(m, off, sizeof(th), &th);
   1021 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
   1022 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
   1023 		break;
   1024 	case IPPROTO_UDP:
   1025 		spidx->ul_proto = nxt;
   1026 		if (!needport)
   1027 			break;
   1028 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1029 			break;
   1030 		m_copydata(m, off, sizeof(uh), &uh);
   1031 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
   1032 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
   1033 		break;
   1034 	case IPPROTO_ICMPV6:
   1035 		spidx->ul_proto = nxt;
   1036 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
   1037 			break;
   1038 		m_copydata(m, off, sizeof(icmph), &icmph);
   1039 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
   1040 		    htons((uint16_t)icmph.icmp6_type);
   1041 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
   1042 		    htons((uint16_t)icmph.icmp6_code);
   1043 		break;
   1044 	default:
   1045 		/* XXX intermediate headers??? */
   1046 		spidx->ul_proto = nxt;
   1047 		break;
   1048 	}
   1049 }
   1050 
   1051 static int
   1052 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1053 {
   1054 	struct ip6_hdr *ip6 = NULL;
   1055 	struct ip6_hdr ip6buf;
   1056 	struct sockaddr_in6 *sin6;
   1057 
   1058 	if (m->m_len >= sizeof(*ip6)) {
   1059 		ip6 = mtod(m, struct ip6_hdr *);
   1060 	} else {
   1061 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
   1062 		ip6 = &ip6buf;
   1063 	}
   1064 
   1065 	sin6 = (struct sockaddr_in6 *)&spidx->src;
   1066 	memset(sin6, 0, sizeof(*sin6));
   1067 	sin6->sin6_family = AF_INET6;
   1068 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1069 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
   1070 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
   1071 		sin6->sin6_addr.s6_addr16[1] = 0;
   1072 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
   1073 	}
   1074 	spidx->prefs = sizeof(struct in6_addr) << 3;
   1075 
   1076 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
   1077 	memset(sin6, 0, sizeof(*sin6));
   1078 	sin6->sin6_family = AF_INET6;
   1079 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1080 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
   1081 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
   1082 		sin6->sin6_addr.s6_addr16[1] = 0;
   1083 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
   1084 	}
   1085 	spidx->prefd = sizeof(struct in6_addr) << 3;
   1086 
   1087 	return 0;
   1088 }
   1089 #endif
   1090 
   1091 static void
   1092 ipsec_delpcbpolicy(struct inpcbpolicy *p)
   1093 {
   1094 
   1095 	kmem_intr_free(p, sizeof(*p));
   1096 }
   1097 
   1098 int
   1099 ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **policy)
   1100 {
   1101 	struct inpcbpolicy *new;
   1102 
   1103 	KASSERT(so != NULL);
   1104 	KASSERT(policy != NULL);
   1105 
   1106 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
   1107 	if (new == NULL) {
   1108 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1109 		return ENOBUFS;
   1110 	}
   1111 
   1112 	if (IPSEC_PRIVILEGED_SO(so))
   1113 		new->priv = 1;
   1114 	else
   1115 		new->priv = 0;
   1116 
   1117 	/*
   1118 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
   1119 	 */
   1120 	new->sp_in = &ipsec_dummy_sp;
   1121 	new->sp_out = &ipsec_dummy_sp;
   1122 
   1123 	*policy = new;
   1124 
   1125 	return 0;
   1126 }
   1127 
   1128 static void
   1129 ipsec_destroy_policy(struct secpolicy *sp)
   1130 {
   1131 
   1132 	if (sp == &ipsec_dummy_sp) {
   1133 		; /* It's dummy. No need to free it. */
   1134 	} else {
   1135 		/*
   1136 		 * We cannot destroy here because it can be called in
   1137 		 * softint. So mark the SP as DEAD and let the timer
   1138 		 * destroy it. See key_timehandler_spd.
   1139 		 */
   1140 		sp->state = IPSEC_SPSTATE_DEAD;
   1141 	}
   1142 }
   1143 
   1144 int
   1145 ipsec_set_policy(struct inpcb *inp, const void *request, size_t len,
   1146     kauth_cred_t cred)
   1147 {
   1148 	const struct sadb_x_policy *xpl;
   1149 	struct secpolicy *newsp, *oldsp;
   1150 	struct secpolicy **policy;
   1151 	int error;
   1152 
   1153 	KASSERT(!cpu_softintr_p());
   1154 	KASSERT(inp != NULL);
   1155 	KASSERT(inp_locked(inp));
   1156 	KASSERT(request != NULL);
   1157 
   1158 	if (len < sizeof(*xpl))
   1159 		return EINVAL;
   1160 	xpl = (const struct sadb_x_policy *)request;
   1161 
   1162 	KASSERT(inp->inp_sp != NULL);
   1163 
   1164 	/* select direction */
   1165 	switch (xpl->sadb_x_policy_dir) {
   1166 	case IPSEC_DIR_INBOUND:
   1167 		policy = &inp->inp_sp->sp_in;
   1168 		break;
   1169 	case IPSEC_DIR_OUTBOUND:
   1170 		policy = &inp->inp_sp->sp_out;
   1171 		break;
   1172 	default:
   1173 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1174 		    xpl->sadb_x_policy_dir);
   1175 		return EINVAL;
   1176 	}
   1177 
   1178 	/* sanity check. */
   1179 	if (policy == NULL || *policy == NULL)
   1180 		return EINVAL;
   1181 
   1182 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1183 		kdebug_sadb_xpolicy("set passed policy", request);
   1184 	}
   1185 
   1186 	/* check policy type */
   1187 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
   1188 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
   1189 	    xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
   1190 		return EINVAL;
   1191 
   1192 	/* check privileged socket */
   1193 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
   1194 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
   1195 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
   1196 		if (error)
   1197 			return error;
   1198 	}
   1199 
   1200 	/* allocation new SP entry */
   1201 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
   1202 		return error;
   1203 
   1204 	key_init_sp(newsp);
   1205 	newsp->created = time_uptime;
   1206 	/* Insert the global list for SPs for sockets */
   1207 	key_socksplist_add(newsp);
   1208 
   1209 	/* clear old SP and set new SP */
   1210 	oldsp = *policy;
   1211 	*policy = newsp;
   1212 	ipsec_destroy_policy(oldsp);
   1213 
   1214 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1215 		printf("%s: new policy\n", __func__);
   1216 		kdebug_secpolicy(newsp);
   1217 	}
   1218 
   1219 	return 0;
   1220 }
   1221 
   1222 int
   1223 ipsec_get_policy(struct inpcb *inp, const void *request, size_t len,
   1224     struct mbuf **mp)
   1225 {
   1226 	const struct sadb_x_policy *xpl;
   1227 	struct secpolicy *policy;
   1228 
   1229 	/* sanity check. */
   1230 	if (inp == NULL || request == NULL || mp == NULL)
   1231 		return EINVAL;
   1232 	KASSERT(inp->inp_sp != NULL);
   1233 	if (len < sizeof(*xpl))
   1234 		return EINVAL;
   1235 	xpl = (const struct sadb_x_policy *)request;
   1236 
   1237 	/* select direction */
   1238 	switch (xpl->sadb_x_policy_dir) {
   1239 	case IPSEC_DIR_INBOUND:
   1240 		policy = inp->inp_sp->sp_in;
   1241 		break;
   1242 	case IPSEC_DIR_OUTBOUND:
   1243 		policy = inp->inp_sp->sp_out;
   1244 		break;
   1245 	default:
   1246 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1247 		    xpl->sadb_x_policy_dir);
   1248 		return EINVAL;
   1249 	}
   1250 
   1251 	if (policy == NULL)
   1252 		return EINVAL;
   1253 
   1254 	*mp = key_sp2msg(policy, M_NOWAIT);
   1255 	if (!*mp) {
   1256 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1257 		return ENOBUFS;
   1258 	}
   1259 
   1260 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1261 		kdebug_mbuf(__func__, *mp);
   1262 	}
   1263 
   1264 	return 0;
   1265 }
   1266 
   1267 int
   1268 ipsec_delete_pcbpolicy(struct inpcb *inp)
   1269 {
   1270 
   1271 	KASSERT(inp != NULL);
   1272 
   1273 	if (inp->inp_sp == NULL)
   1274 		return 0;
   1275 
   1276 	if (inp->inp_sp->sp_in != NULL)
   1277 		ipsec_destroy_policy(inp->inp_sp->sp_in);
   1278 
   1279 	if (inp->inp_sp->sp_out != NULL)
   1280 		ipsec_destroy_policy(inp->inp_sp->sp_out);
   1281 
   1282 	ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
   1283 
   1284 	ipsec_delpcbpolicy(inp->inp_sp);
   1285 	inp->inp_sp = NULL;
   1286 
   1287 	return 0;
   1288 }
   1289 
   1290 /*
   1291  * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
   1292  */
   1293 u_int
   1294 ipsec_get_reqlevel(const struct ipsecrequest *isr)
   1295 {
   1296 	u_int level = 0;
   1297 	u_int esp_trans_deflev, esp_net_deflev;
   1298 	u_int ah_trans_deflev, ah_net_deflev;
   1299 
   1300 	KASSERT(isr != NULL);
   1301 	KASSERT(isr->sp != NULL);
   1302 	KASSERTMSG(
   1303 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
   1304 	    "af family mismatch, src %u, dst %u",
   1305 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
   1306 
   1307 /* XXX note that we have ipseclog() expanded here - code sync issue */
   1308 #define IPSEC_CHECK_DEFAULT(lev)					\
   1309     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
   1310     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
   1311 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
   1312 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
   1313 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
   1314     : (lev))
   1315 
   1316 	/* set default level */
   1317 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
   1318 #ifdef INET
   1319 	case AF_INET:
   1320 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
   1321 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
   1322 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
   1323 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
   1324 		break;
   1325 #endif
   1326 #ifdef INET6
   1327 	case AF_INET6:
   1328 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
   1329 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
   1330 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
   1331 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
   1332 		break;
   1333 #endif
   1334 	default:
   1335 		panic("%s: unknown af %u", __func__,
   1336 		    isr->sp->spidx.src.sa.sa_family);
   1337 	}
   1338 
   1339 #undef IPSEC_CHECK_DEFAULT
   1340 
   1341 	/* set level */
   1342 	switch (isr->level) {
   1343 	case IPSEC_LEVEL_DEFAULT:
   1344 		switch (isr->saidx.proto) {
   1345 		case IPPROTO_ESP:
   1346 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1347 				level = esp_net_deflev;
   1348 			else
   1349 				level = esp_trans_deflev;
   1350 			break;
   1351 		case IPPROTO_AH:
   1352 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1353 				level = ah_net_deflev;
   1354 			else
   1355 				level = ah_trans_deflev;
   1356 			break;
   1357 		case IPPROTO_IPCOMP:
   1358 			/*
   1359 			 * we don't really care, as IPcomp document says that
   1360 			 * we shouldn't compress small packets
   1361 			 */
   1362 			level = IPSEC_LEVEL_USE;
   1363 			break;
   1364 		default:
   1365 			panic("%s: Illegal protocol defined %u", __func__,
   1366 			    isr->saidx.proto);
   1367 		}
   1368 		break;
   1369 
   1370 	case IPSEC_LEVEL_USE:
   1371 	case IPSEC_LEVEL_REQUIRE:
   1372 		level = isr->level;
   1373 		break;
   1374 	case IPSEC_LEVEL_UNIQUE:
   1375 		level = IPSEC_LEVEL_REQUIRE;
   1376 		break;
   1377 
   1378 	default:
   1379 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
   1380 	}
   1381 
   1382 	return level;
   1383 }
   1384 
   1385 /*
   1386  * Check security policy requirements against the actual packet contents.
   1387  *
   1388  * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
   1389  * then kick it.
   1390  */
   1391 static int
   1392 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
   1393 {
   1394 	struct ipsecrequest *isr;
   1395 
   1396 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1397 		printf("%s: using SP\n", __func__);
   1398 		kdebug_secpolicy(sp);
   1399 	}
   1400 
   1401 	/* check policy */
   1402 	switch (sp->policy) {
   1403 	case IPSEC_POLICY_DISCARD:
   1404 		return 1;
   1405 	case IPSEC_POLICY_BYPASS:
   1406 	case IPSEC_POLICY_NONE:
   1407 		return 0;
   1408 	}
   1409 
   1410 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1411 	    "invalid policy %u", sp->policy);
   1412 
   1413 	/* XXX should compare policy against ipsec header history */
   1414 
   1415 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1416 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
   1417 			continue;
   1418 		switch (isr->saidx.proto) {
   1419 		case IPPROTO_ESP:
   1420 			if ((m->m_flags & M_DECRYPTED) == 0) {
   1421 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1422 				    "ESP m_flags:%x\n", m->m_flags);
   1423 				return 1;
   1424 			}
   1425 			break;
   1426 		case IPPROTO_AH:
   1427 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
   1428 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1429 				    "AH m_flags:%x\n", m->m_flags);
   1430 				return 1;
   1431 			}
   1432 			break;
   1433 		case IPPROTO_IPCOMP:
   1434 			/*
   1435 			 * We don't really care, as IPcomp document
   1436 			 * says that we shouldn't compress small
   1437 			 * packets, IPComp policy should always be
   1438 			 * treated as being in "use" level.
   1439 			 */
   1440 			break;
   1441 		}
   1442 	}
   1443 
   1444 	return 0;
   1445 }
   1446 
   1447 /*
   1448  * Check security policy requirements.
   1449  */
   1450 int
   1451 ipsec_in_reject(struct mbuf *m, struct inpcb *inp)
   1452 {
   1453 	struct secpolicy *sp;
   1454 	int error;
   1455 	int result;
   1456 
   1457 	KASSERT(m != NULL);
   1458 
   1459 	if (inp == NULL)
   1460 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1461 		    IP_FORWARDING, &error);
   1462 	else
   1463 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1464 		    inp, &error);
   1465 
   1466 	if (sp != NULL) {
   1467 		result = ipsec_sp_reject(sp, m);
   1468 		if (result)
   1469 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1470 		KEY_SP_UNREF(&sp);
   1471 	} else {
   1472 		result = 0;
   1473 	}
   1474 	return result;
   1475 }
   1476 
   1477 /*
   1478  * Compute the byte size to be occupied by the IPsec header. If it is
   1479  * tunneled, it includes the size of outer IP header.
   1480  */
   1481 static size_t
   1482 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
   1483 {
   1484 	struct ipsecrequest *isr;
   1485 	size_t siz;
   1486 
   1487 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1488 		printf("%s: using SP\n", __func__);
   1489 		kdebug_secpolicy(sp);
   1490 	}
   1491 
   1492 	switch (sp->policy) {
   1493 	case IPSEC_POLICY_DISCARD:
   1494 	case IPSEC_POLICY_BYPASS:
   1495 	case IPSEC_POLICY_NONE:
   1496 		return 0;
   1497 	}
   1498 
   1499 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1500 	    "invalid policy %u", sp->policy);
   1501 
   1502 	siz = 0;
   1503 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1504 		size_t clen = 0;
   1505 		struct secasvar *sav;
   1506 
   1507 		switch (isr->saidx.proto) {
   1508 		case IPPROTO_ESP:
   1509 			sav = ipsec_lookup_sa(isr, m);
   1510 			if (sav != NULL) {
   1511 				clen = esp_hdrsiz(sav);
   1512 				KEY_SA_UNREF(&sav);
   1513 			} else
   1514 				clen = esp_hdrsiz(NULL);
   1515 			break;
   1516 		case IPPROTO_AH:
   1517 			sav = ipsec_lookup_sa(isr, m);
   1518 			if (sav != NULL) {
   1519 				clen = ah_hdrsiz(sav);
   1520 				KEY_SA_UNREF(&sav);
   1521 			} else
   1522 				clen = ah_hdrsiz(NULL);
   1523 			break;
   1524 		case IPPROTO_IPCOMP:
   1525 			clen = sizeof(struct ipcomp);
   1526 			break;
   1527 		}
   1528 
   1529 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
   1530 			switch (isr->saidx.dst.sa.sa_family) {
   1531 			case AF_INET:
   1532 				clen += sizeof(struct ip);
   1533 				break;
   1534 #ifdef INET6
   1535 			case AF_INET6:
   1536 				clen += sizeof(struct ip6_hdr);
   1537 				break;
   1538 #endif
   1539 			default:
   1540 				IPSECLOG(LOG_ERR, "unknown AF %d in "
   1541 				    "IPsec tunnel SA\n",
   1542 				    ((const struct sockaddr *)&isr->saidx.dst)
   1543 				    ->sa_family);
   1544 				break;
   1545 			}
   1546 		}
   1547 		siz += clen;
   1548 	}
   1549 
   1550 	return siz;
   1551 }
   1552 
   1553 size_t
   1554 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
   1555 {
   1556 	struct secpolicy *sp;
   1557 	int error;
   1558 	size_t size;
   1559 
   1560 	KASSERT(m != NULL);
   1561 	KASSERTMSG(inp == NULL || inp->inp_socket != NULL,
   1562 	    "socket w/o inpcb");
   1563 
   1564 	if (inp == NULL)
   1565 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1566 	else
   1567 		sp = ipsec_getpolicybysock(m, dir, inp, &error);
   1568 
   1569 	if (sp != NULL) {
   1570 		size = ipsec_sp_hdrsiz(sp, m);
   1571 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   1572 		KEY_SP_UNREF(&sp);
   1573 	} else {
   1574 		size = 0;
   1575 	}
   1576 
   1577 	return size;
   1578 }
   1579 
   1580 /*
   1581  * Check the variable replay window.
   1582  * ipsec_chkreplay() performs replay check before ICV verification.
   1583  * ipsec_updatereplay() updates replay bitmap.  This must be called after
   1584  * ICV verification (it also performs replay check, which is usually done
   1585  * beforehand).
   1586  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
   1587  *
   1588  * based on RFC 2401.
   1589  */
   1590 int
   1591 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
   1592 {
   1593 	const struct secreplay *replay;
   1594 	u_int32_t diff;
   1595 	int fr;
   1596 	u_int32_t wsizeb;	/* constant: bits of window size */
   1597 	int frlast;		/* constant: last frame */
   1598 
   1599 	KASSERT(sav != NULL);
   1600 	KASSERT(sav->replay != NULL);
   1601 
   1602 	replay = sav->replay;
   1603 
   1604 	if (replay->wsize == 0)
   1605 		return 1;	/* no need to check replay. */
   1606 
   1607 	/* constant */
   1608 	frlast = replay->wsize - 1;
   1609 	wsizeb = replay->wsize << 3;
   1610 
   1611 	/* sequence number of 0 is invalid */
   1612 	if (seq == 0)
   1613 		return 0;
   1614 
   1615 	/* first time is always okay */
   1616 	if (replay->count == 0)
   1617 		return 1;
   1618 
   1619 	if (seq > replay->lastseq) {
   1620 		/* larger sequences are okay */
   1621 		return 1;
   1622 	} else {
   1623 		/* seq is equal or less than lastseq. */
   1624 		diff = replay->lastseq - seq;
   1625 
   1626 		/* over range to check, i.e. too old or wrapped */
   1627 		if (diff >= wsizeb)
   1628 			return 0;
   1629 
   1630 		fr = frlast - diff / 8;
   1631 
   1632 		/* this packet already seen ? */
   1633 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1634 			return 0;
   1635 
   1636 		/* out of order but good */
   1637 		return 1;
   1638 	}
   1639 }
   1640 
   1641 /*
   1642  * check replay counter whether to update or not.
   1643  * OUT:	0:	OK
   1644  *	1:	NG
   1645  */
   1646 int
   1647 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
   1648 {
   1649 	struct secreplay *replay;
   1650 	u_int32_t diff;
   1651 	int fr;
   1652 	u_int32_t wsizeb;	/* constant: bits of window size */
   1653 	int frlast;		/* constant: last frame */
   1654 
   1655 	KASSERT(sav != NULL);
   1656 	KASSERT(sav->replay != NULL);
   1657 
   1658 	replay = sav->replay;
   1659 
   1660 	if (replay->wsize == 0)
   1661 		goto ok;	/* no need to check replay. */
   1662 
   1663 	/* constant */
   1664 	frlast = replay->wsize - 1;
   1665 	wsizeb = replay->wsize << 3;
   1666 
   1667 	/* sequence number of 0 is invalid */
   1668 	if (seq == 0)
   1669 		return 1;
   1670 
   1671 	/* first time */
   1672 	if (replay->count == 0) {
   1673 		replay->lastseq = seq;
   1674 		memset(replay->bitmap, 0, replay->wsize);
   1675 		(replay->bitmap)[frlast] = 1;
   1676 		goto ok;
   1677 	}
   1678 
   1679 	if (seq > replay->lastseq) {
   1680 		/* seq is larger than lastseq. */
   1681 		diff = seq - replay->lastseq;
   1682 
   1683 		/* new larger sequence number */
   1684 		if (diff < wsizeb) {
   1685 			/* In window */
   1686 			/* set bit for this packet */
   1687 			vshiftl(replay->bitmap, diff, replay->wsize);
   1688 			(replay->bitmap)[frlast] |= 1;
   1689 		} else {
   1690 			/* this packet has a "way larger" */
   1691 			memset(replay->bitmap, 0, replay->wsize);
   1692 			(replay->bitmap)[frlast] = 1;
   1693 		}
   1694 		replay->lastseq = seq;
   1695 
   1696 		/* larger is good */
   1697 	} else {
   1698 		/* seq is equal or less than lastseq. */
   1699 		diff = replay->lastseq - seq;
   1700 
   1701 		/* over range to check, i.e. too old or wrapped */
   1702 		if (diff >= wsizeb)
   1703 			return 1;
   1704 
   1705 		fr = frlast - diff / 8;
   1706 
   1707 		/* this packet already seen ? */
   1708 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1709 			return 1;
   1710 
   1711 		/* mark as seen */
   1712 		(replay->bitmap)[fr] |= (1 << (diff % 8));
   1713 
   1714 		/* out of order but good */
   1715 	}
   1716 
   1717 ok:
   1718 	if (replay->count == ~0) {
   1719 		char buf[IPSEC_LOGSASTRLEN];
   1720 
   1721 		/* set overflow flag */
   1722 		replay->overflow++;
   1723 
   1724 		/* don't increment, no more packets accepted */
   1725 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
   1726 			return 1;
   1727 
   1728 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
   1729 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
   1730 	}
   1731 
   1732 	replay->count++;
   1733 
   1734 	return 0;
   1735 }
   1736 
   1737 /*
   1738  * shift variable length buffer to left.
   1739  * IN:	bitmap: pointer to the buffer
   1740  *	nbit:	the number of to shift.
   1741  *	wsize:	buffer size (bytes).
   1742  */
   1743 static void
   1744 vshiftl(unsigned char *bitmap, int nbit, int wsize)
   1745 {
   1746 	int s, j, i;
   1747 	unsigned char over;
   1748 
   1749 	for (j = 0; j < nbit; j += 8) {
   1750 		s = (nbit - j < 8) ? (nbit - j): 8;
   1751 		bitmap[0] <<= s;
   1752 		for (i = 1; i < wsize; i++) {
   1753 			over = (bitmap[i] >> (8 - s));
   1754 			bitmap[i] <<= s;
   1755 			bitmap[i-1] |= over;
   1756 		}
   1757 	}
   1758 
   1759 	return;
   1760 }
   1761 
   1762 /* Return a printable string for the address. */
   1763 const char *
   1764 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
   1765 {
   1766 	switch (sa->sa.sa_family) {
   1767 	case AF_INET:
   1768 		in_print(buf, size, &sa->sin.sin_addr);
   1769 		return buf;
   1770 #if INET6
   1771 	case AF_INET6:
   1772 		in6_print(buf, size, &sa->sin6.sin6_addr);
   1773 		return buf;
   1774 #endif
   1775 	default:
   1776 		return "(unknown address family)";
   1777 	}
   1778 }
   1779 
   1780 const char *
   1781 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
   1782 {
   1783 	const struct secasindex *saidx = &sav->sah->saidx;
   1784 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
   1785 
   1786 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
   1787 	    "af family mismatch, src %u, dst %u",
   1788 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
   1789 
   1790 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
   1791 	    (u_int32_t)ntohl(sav->spi),
   1792 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
   1793 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
   1794 
   1795 	return buf;
   1796 }
   1797 
   1798 #ifdef INET6
   1799 struct secpolicy *
   1800 ipsec6_check_policy(struct mbuf *m, struct inpcb *inp, int flags,
   1801     int *needipsecp, int *errorp)
   1802 {
   1803 	struct secpolicy *sp = NULL;
   1804 	int s;
   1805 	int error = 0;
   1806 	int needipsec = 0;
   1807 
   1808 	if (ipsec_outdone(m)) {
   1809 		goto skippolicycheck;
   1810 	}
   1811 	s = splsoftnet();
   1812 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
   1813 		splx(s);
   1814 		goto skippolicycheck;
   1815 	}
   1816 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
   1817 	splx(s);
   1818 
   1819 	/*
   1820 	 * There are four return cases:
   1821 	 *	sp != NULL                    apply IPsec policy
   1822 	 *	sp == NULL, error == 0        no IPsec handling needed
   1823 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
   1824 	 *	sp == NULL, error != 0        discard packet, report error
   1825 	 */
   1826 	if (sp == NULL) {
   1827 		needipsec = 0;
   1828 	} else {
   1829 		needipsec = 1;
   1830 	}
   1831 
   1832 skippolicycheck:
   1833 	*errorp = error;
   1834 	*needipsecp = needipsec;
   1835 	return sp;
   1836 }
   1837 
   1838 /*
   1839  * calculate UDP checksum for UDP encapsulated ESP for IPv6.
   1840  *
   1841  * RFC2460(Internet Protocol, Version 6 Specification) says:
   1842  *
   1843  *   IPv6 receivers MUST discard UDP packets with a zero checksum.
   1844  *
   1845  * There is more relaxed specification RFC6935(IPv6 and UDP Checksums for
   1846  * Tunneled Packets). The document allows zero checksum. It's too
   1847  * late to publish, there are a lot of interoperability problems...
   1848  */
   1849 void
   1850 ipsec6_udp_cksum(struct mbuf *m)
   1851 {
   1852 	struct ip6_hdr *ip6;
   1853 	uint16_t plen, uh_sum;
   1854 	int off;
   1855 
   1856 	/* must called after m_pullup() */
   1857 	KASSERT(m->m_len >= sizeof(struct ip6_hdr));
   1858 
   1859 	ip6 = mtod(m, struct ip6_hdr *);
   1860 	KASSERT(ip6->ip6_nxt == IPPROTO_UDP);
   1861 
   1862 	/* ip6->ip6_plen can not be updated before ip6_output() */
   1863 	plen = m->m_pkthdr.len - sizeof(*ip6);
   1864 	KASSERT(plen >= sizeof(struct udphdr));
   1865 
   1866 	uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(*ip6), plen);
   1867 	if (uh_sum == 0)
   1868 		uh_sum = 0xffff;
   1869 
   1870 	off = sizeof(*ip6) + offsetof(struct udphdr, uh_sum);
   1871 	m_copyback(m, off, sizeof(uh_sum), (void *)&uh_sum);
   1872 }
   1873 #endif /* INET6 */
   1874 
   1875 /*
   1876  * -----------------------------------------------------------------------------
   1877  */
   1878 
   1879 /* XXX this stuff doesn't belong here... */
   1880 
   1881 static struct xformsw *xforms = NULL;
   1882 
   1883 /*
   1884  * Register a transform; typically at system startup.
   1885  */
   1886 void
   1887 xform_register(struct xformsw *xsp)
   1888 {
   1889 	xsp->xf_next = xforms;
   1890 	xforms = xsp;
   1891 }
   1892 
   1893 /*
   1894  * Initialize transform support in an sav.
   1895  */
   1896 int
   1897 xform_init(struct secasvar *sav, int xftype)
   1898 {
   1899 	struct xformsw *xsp;
   1900 
   1901 	if (sav->tdb_xform != NULL)	/* previously initialized */
   1902 		return 0;
   1903 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
   1904 		if (xsp->xf_type == xftype)
   1905 			return (*xsp->xf_init)(sav, xsp);
   1906 
   1907 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
   1908 	return EINVAL;
   1909 }
   1910 
   1911 /*
   1912  * XXXJRT This should be done as a protosw init call.
   1913  */
   1914 void
   1915 ipsec_attach(void)
   1916 {
   1917 
   1918 	ipsec_output_init();
   1919 
   1920 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
   1921 
   1922 	sysctl_net_inet_ipsec_setup(NULL);
   1923 #ifdef INET6
   1924 	sysctl_net_inet6_ipsec6_setup(NULL);
   1925 #endif
   1926 
   1927 	ah_attach();
   1928 	esp_attach();
   1929 	ipcomp_attach();
   1930 	ipe4_attach();
   1931 #ifdef TCP_SIGNATURE
   1932 	tcpsignature_attach();
   1933 #endif
   1934 }
   1935