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