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