Home | History | Annotate | Line # | Download | only in netipsec
ipsec.c revision 1.135
      1 /* $NetBSD: ipsec.c,v 1.135 2018/02/26 06:17:01 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.135 2018/02/26 06:17:01 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 int ipsec_sp_reject(const struct secpolicy *, const struct mbuf *);
    193 static void vshiftl(unsigned char *, int, int);
    194 static size_t ipsec_sp_hdrsiz(const struct secpolicy *, const struct mbuf *);
    195 
    196 /*
    197  * Try to validate and use cached policy on a PCB.
    198  */
    199 static struct secpolicy *
    200 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
    201 {
    202 	struct secpolicyindex spidx;
    203 	struct secpolicy *sp = NULL;
    204 	int s;
    205 
    206 	KASSERT(IPSEC_DIR_IS_VALID(dir));
    207 	KASSERT(pcbsp != NULL);
    208 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    209 	KASSERT(inph_locked(pcbsp->sp_inph));
    210 
    211 	/*
    212 	 * Checking the generation and sp->state and taking a reference to an SP
    213 	 * must be in a critical section of pserialize. See key_unlink_sp.
    214 	 */
    215 	s = pserialize_read_enter();
    216 	/* SPD table change invalidate all the caches. */
    217 	if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
    218 		ipsec_invalpcbcache(pcbsp, dir);
    219 		goto out;
    220 	}
    221 	sp = pcbsp->sp_cache[dir].cachesp;
    222 	if (sp == NULL)
    223 		goto out;
    224 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
    225 		sp = NULL;
    226 		ipsec_invalpcbcache(pcbsp, dir);
    227 		goto out;
    228 	}
    229 	if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
    230 		/* NB: assume ipsec_setspidx never sleep */
    231 		if (ipsec_setspidx(m, &spidx, 1) != 0) {
    232 			sp = NULL;
    233 			goto out;
    234 		}
    235 
    236 		/*
    237 		 * We have to make an exact match here since the cached rule
    238 		 * might have lower priority than a rule that would otherwise
    239 		 * have matched the packet.
    240 		 */
    241 		if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
    242 		    sizeof(spidx))) {
    243 			sp = NULL;
    244 			goto out;
    245 		}
    246 	} else {
    247 		/*
    248 		 * The pcb is connected, and the L4 code is sure that:
    249 		 * - outgoing side uses inp_[lf]addr
    250 		 * - incoming side looks up policy after inpcb lookup
    251 		 * and address pair is know to be stable.  We do not need
    252 		 * to generate spidx again, nor check the address match again.
    253 		 *
    254 		 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
    255 		 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
    256 		 */
    257 	}
    258 
    259 	sp->lastused = time_second;
    260 	KEY_SP_REF(sp);
    261 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    262 	    "DP cause refcnt++:%d SP:%p\n",
    263 	    key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
    264 out:
    265 	pserialize_read_exit(s);
    266 	return sp;
    267 }
    268 
    269 static int
    270 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
    271     struct secpolicy *sp, int dir)
    272 {
    273 
    274 	KASSERT(IPSEC_DIR_IS_INOROUT(dir));
    275 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    276 	KASSERT(inph_locked(pcbsp->sp_inph));
    277 
    278 	pcbsp->sp_cache[dir].cachesp = NULL;
    279 	pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
    280 	if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, 1) != 0) {
    281 		return EINVAL;
    282 	}
    283 	pcbsp->sp_cache[dir].cachesp = sp;
    284 	if (pcbsp->sp_cache[dir].cachesp) {
    285 		/*
    286 		 * If the PCB is connected, we can remember a hint to
    287 		 * possibly short-circuit IPsec processing in other places.
    288 		 */
    289 		if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
    290 			switch (pcbsp->sp_cache[dir].cachesp->policy) {
    291 			case IPSEC_POLICY_NONE:
    292 			case IPSEC_POLICY_BYPASS:
    293 				pcbsp->sp_cache[dir].cachehint =
    294 				    IPSEC_PCBHINT_NO;
    295 				break;
    296 			default:
    297 				pcbsp->sp_cache[dir].cachehint =
    298 				    IPSEC_PCBHINT_YES;
    299 			}
    300 		}
    301 	}
    302 	pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
    303 
    304 	return 0;
    305 }
    306 
    307 static int
    308 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
    309 {
    310 	int i;
    311 
    312 	KASSERT(inph_locked(pcbsp->sp_inph));
    313 
    314 	for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
    315 		if (dir != IPSEC_DIR_ANY && i != dir)
    316 			continue;
    317 		pcbsp->sp_cache[i].cachesp = NULL;
    318 		pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
    319 		pcbsp->sp_cache[i].cachegen = 0;
    320 		memset(&pcbsp->sp_cache[i].cacheidx, 0,
    321 		    sizeof(pcbsp->sp_cache[i].cacheidx));
    322 	}
    323 	return 0;
    324 }
    325 
    326 void
    327 ipsec_pcbconn(struct inpcbpolicy *pcbsp)
    328 {
    329 
    330 	KASSERT(inph_locked(pcbsp->sp_inph));
    331 
    332 	pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
    333 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    334 }
    335 
    336 void
    337 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
    338 {
    339 
    340 	KASSERT(inph_locked(pcbsp->sp_inph));
    341 
    342 	pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
    343 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    344 }
    345 
    346 void
    347 ipsec_invalpcbcacheall(void)
    348 {
    349 
    350 	if (ipsec_spdgen == UINT_MAX)
    351 		ipsec_spdgen = 1;
    352 	else
    353 		ipsec_spdgen++;
    354 }
    355 
    356 /*
    357  * Return a held reference to the default SP.
    358  */
    359 static struct secpolicy *
    360 key_get_default_sp(int af, const char *where, int tag)
    361 {
    362 	struct secpolicy *sp;
    363 
    364 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
    365 
    366 	switch(af) {
    367 	case AF_INET:
    368 		sp = &ip4_def_policy;
    369 		break;
    370 #ifdef INET6
    371 	case AF_INET6:
    372 		sp = &ip6_def_policy;
    373 		break;
    374 #endif
    375 	default:
    376 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    377 		    "unexpected protocol family %u\n", af);
    378 		return NULL;
    379 	}
    380 
    381 	if (sp->policy != IPSEC_POLICY_DISCARD &&
    382 	    sp->policy != IPSEC_POLICY_NONE) {
    383 		IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
    384 		    sp->policy, IPSEC_POLICY_NONE);
    385 		sp->policy = IPSEC_POLICY_NONE;
    386 	}
    387 	KEY_SP_REF(sp);
    388 
    389 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
    390 	    sp, key_sp_refcnt(sp));
    391 	return sp;
    392 }
    393 
    394 #define	KEY_GET_DEFAULT_SP(af) \
    395 	key_get_default_sp((af), __func__, __LINE__)
    396 
    397 /*
    398  * For OUTBOUND packet having a socket. Searching SPD for packet,
    399  * and return a pointer to SP.
    400  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
    401  *		0	: bypass
    402  *		EACCES	: discard packet.
    403  *		ENOENT	: ipsec_acquire() in progress, maybe.
    404  *		others	: error occurred.
    405  *	others:	a pointer to SP
    406  *
    407  * NOTE: IPv6 mapped address concern is implemented here.
    408  */
    409 static struct secpolicy *
    410 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph,
    411     int *error)
    412 {
    413 	struct inpcbpolicy *pcbsp = NULL;
    414 	struct secpolicy *currsp = NULL;	/* policy on socket */
    415 	struct secpolicy *sp;
    416 	int af;
    417 
    418 	KASSERT(m != NULL);
    419 	KASSERT(inph != NULL);
    420 	KASSERT(error != NULL);
    421 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    422 
    423 	KASSERT(inph->inph_socket != NULL);
    424 	KASSERT(inph_locked(inph));
    425 
    426 	/* XXX FIXME inpcb/in6pcb  vs socket*/
    427 	af = inph->inph_af;
    428 	KASSERTMSG(af == AF_INET || af == AF_INET6,
    429 	    "unexpected protocol family %u", af);
    430 
    431 	KASSERT(inph->inph_sp != NULL);
    432 	/* If we have a cached entry, and if it is still valid, use it. */
    433 	IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
    434 	currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir);
    435 	if (currsp) {
    436 		*error = 0;
    437 		return currsp;
    438 	}
    439 	IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
    440 
    441 	switch (af) {
    442 	case AF_INET: {
    443 		struct inpcb *in4p = (struct inpcb *)inph;
    444 		/* set spidx in pcb */
    445 		*error = ipsec4_setspidx_inpcb(m, in4p);
    446 		pcbsp = in4p->inp_sp;
    447 		break;
    448 		}
    449 #if defined(INET6)
    450 	case AF_INET6: {
    451 		struct in6pcb *in6p = (struct in6pcb *)inph;
    452 		/* set spidx in pcb */
    453 		*error = ipsec6_setspidx_in6pcb(m, in6p);
    454 		pcbsp = in6p->in6p_sp;
    455 		break;
    456 		}
    457 #endif
    458 	default:
    459 		*error = EPFNOSUPPORT;
    460 		break;
    461 	}
    462 	if (*error)
    463 		return NULL;
    464 
    465 	KASSERT(pcbsp != NULL);
    466 	switch (dir) {
    467 	case IPSEC_DIR_INBOUND:
    468 		currsp = pcbsp->sp_in;
    469 		break;
    470 	case IPSEC_DIR_OUTBOUND:
    471 		currsp = pcbsp->sp_out;
    472 		break;
    473 	}
    474 	KASSERT(currsp != NULL);
    475 
    476 	if (pcbsp->priv) {			/* when privilieged socket */
    477 		switch (currsp->policy) {
    478 		case IPSEC_POLICY_BYPASS:
    479 		case IPSEC_POLICY_IPSEC:
    480 			KEY_SP_REF(currsp);
    481 			sp = currsp;
    482 			break;
    483 
    484 		case IPSEC_POLICY_ENTRUST:
    485 			/* look for a policy in SPD */
    486 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    487 			if (sp == NULL)		/* no SP found */
    488 				sp = KEY_GET_DEFAULT_SP(af);
    489 			break;
    490 
    491 		default:
    492 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
    493 			    currsp->policy);
    494 			*error = EINVAL;
    495 			return NULL;
    496 		}
    497 	} else {				/* unpriv, SPD has policy */
    498 		sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    499 		if (sp == NULL) {		/* no SP found */
    500 			switch (currsp->policy) {
    501 			case IPSEC_POLICY_BYPASS:
    502 				IPSECLOG(LOG_ERR, "Illegal policy for "
    503 				    "non-priviliged defined %d\n",
    504 				    currsp->policy);
    505 				*error = EINVAL;
    506 				return NULL;
    507 
    508 			case IPSEC_POLICY_ENTRUST:
    509 				sp = KEY_GET_DEFAULT_SP(af);
    510 				break;
    511 
    512 			case IPSEC_POLICY_IPSEC:
    513 				KEY_SP_REF(currsp);
    514 				sp = currsp;
    515 				break;
    516 
    517 			default:
    518 				IPSECLOG(LOG_ERR, "Invalid policy for "
    519 				    "PCB %d\n", currsp->policy);
    520 				*error = EINVAL;
    521 				return NULL;
    522 			}
    523 		}
    524 	}
    525 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
    526 	    currsp->policy);
    527 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    528 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
    529 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
    530 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
    531 	return sp;
    532 }
    533 
    534 /*
    535  * For FORWARDING packet or OUTBOUND without a socket. Searching SPD for packet,
    536  * and return a pointer to SP.
    537  * OUT:	positive: a pointer to the entry for security policy leaf matched.
    538  *	NULL:	no apropreate SP found, the following value is set to error.
    539  *		0	: bypass
    540  *		EACCES	: discard packet.
    541  *		ENOENT	: ipsec_acquire() in progress, maybe.
    542  *		others	: error occurred.
    543  */
    544 static struct secpolicy *
    545 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
    546 {
    547 	struct secpolicyindex spidx;
    548 	struct secpolicy *sp;
    549 
    550 	KASSERT(m != NULL);
    551 	KASSERT(error != NULL);
    552 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    553 
    554 	sp = NULL;
    555 
    556 	/* Make an index to look for a policy. */
    557 	*error = ipsec_setspidx(m, &spidx, (flag & IP_FORWARDING) ? 0 : 1);
    558 	if (*error != 0) {
    559 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
    560 		memset(&spidx, 0, sizeof(spidx));
    561 		return NULL;
    562 	}
    563 
    564 	spidx.dir = dir;
    565 
    566 	if (key_havesp(dir)) {
    567 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
    568 	}
    569 
    570 	if (sp == NULL)			/* no SP found, use system default */
    571 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
    572 	KASSERT(sp != NULL);
    573 	return sp;
    574 }
    575 
    576 static struct secpolicy *
    577 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
    578     struct inpcb *inp)
    579 {
    580 	struct secpolicy *sp;
    581 
    582 	*error = 0;
    583 
    584 	if (inp == NULL) {
    585 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
    586 	} else {
    587 		struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
    588 		KASSERT(inph->inph_socket != NULL);
    589 		sp = ipsec_getpolicybysock(m, dir, inph, error);
    590 	}
    591 	if (sp == NULL) {
    592 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    593 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    594 		return NULL;
    595 	}
    596 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    597 
    598 	switch (sp->policy) {
    599 	case IPSEC_POLICY_ENTRUST:
    600 	default:
    601 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    602 		/* fall thru... */
    603 	case IPSEC_POLICY_DISCARD:
    604 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    605 		*error = -EINVAL;	/* packet is discarded by caller */
    606 		break;
    607 	case IPSEC_POLICY_BYPASS:
    608 	case IPSEC_POLICY_NONE:
    609 		KEY_SP_UNREF(&sp);
    610 		sp = NULL;		/* NB: force NULL result */
    611 		break;
    612 	case IPSEC_POLICY_IPSEC:
    613 		KASSERT(sp->req != NULL);
    614 		break;
    615 	}
    616 
    617 	if (*error != 0) {
    618 		KEY_SP_UNREF(&sp);
    619 		sp = NULL;
    620 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    621 	}
    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 	error = ipsec4_process_packet(m, sp->req, &_mtu);
    690 
    691 	if (error == 0 && _mtu != 0) {
    692 		/*
    693 		 * NAT-T ESP fragmentation: do not do IPSec processing
    694 		 * now, we will do it on each fragmented packet.
    695 		 */
    696 		*mtu = _mtu;
    697 		*natt_frag = true;
    698 		KEY_SP_UNREF(&sp);
    699 		splx(s);
    700 		return 0;
    701 	}
    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_sp_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 = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
    775 	    &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 		struct inpcb_hdr *inph = (struct inpcb_hdr *)in6p;
    822 		KASSERT(inph->inph_socket != NULL);
    823 		sp = ipsec_getpolicybysock(m, dir, inph, error);
    824 	}
    825 	if (sp == NULL) {
    826 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    827 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    828 		return NULL;
    829 	}
    830 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    831 	switch (sp->policy) {
    832 	case IPSEC_POLICY_ENTRUST:
    833 	default:
    834 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    835 		/* fall thru... */
    836 	case IPSEC_POLICY_DISCARD:
    837 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    838 		*error = -EINVAL;   /* packet is discarded by caller */
    839 		break;
    840 	case IPSEC_POLICY_BYPASS:
    841 	case IPSEC_POLICY_NONE:
    842 		KEY_SP_UNREF(&sp);
    843 		sp = NULL;		/* NB: force NULL result */
    844 		break;
    845 	case IPSEC_POLICY_IPSEC:
    846 		KASSERT(sp->req != NULL);
    847 		break;
    848 	}
    849 	if (*error != 0) {
    850 		KEY_SP_UNREF(&sp);
    851 		sp = NULL;
    852 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    853 	}
    854 	return sp;
    855 }
    856 #endif /* INET6 */
    857 
    858 static int
    859 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
    860 {
    861 	int error;
    862 
    863 	KASSERT(pcb != NULL);
    864 	KASSERT(pcb->inp_sp != NULL);
    865 	KASSERT(pcb->inp_sp->sp_out != NULL);
    866 	KASSERT(pcb->inp_sp->sp_in != NULL);
    867 
    868 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
    869 	if (error == 0) {
    870 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
    871 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
    872 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
    873 	} else {
    874 		memset(&pcb->inp_sp->sp_in->spidx, 0,
    875 		    sizeof(pcb->inp_sp->sp_in->spidx));
    876 		memset(&pcb->inp_sp->sp_out->spidx, 0,
    877 		    sizeof(pcb->inp_sp->sp_in->spidx));
    878 	}
    879 	return error;
    880 }
    881 
    882 #ifdef INET6
    883 static int
    884 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
    885 {
    886 	struct secpolicyindex *spidx;
    887 	int error;
    888 
    889 	KASSERT(pcb != NULL);
    890 	KASSERT(pcb->in6p_sp != NULL);
    891 	KASSERT(pcb->in6p_sp->sp_out != NULL);
    892 	KASSERT(pcb->in6p_sp->sp_in != NULL);
    893 
    894 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
    895 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
    896 
    897 	spidx = &pcb->in6p_sp->sp_in->spidx;
    898 	error = ipsec_setspidx(m, spidx, 1);
    899 	if (error)
    900 		goto bad;
    901 	spidx->dir = IPSEC_DIR_INBOUND;
    902 
    903 	spidx = &pcb->in6p_sp->sp_out->spidx;
    904 	error = ipsec_setspidx(m, spidx, 1);
    905 	if (error)
    906 		goto bad;
    907 	spidx->dir = IPSEC_DIR_OUTBOUND;
    908 
    909 	return 0;
    910 
    911 bad:
    912 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
    913 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
    914 	return error;
    915 }
    916 #endif
    917 
    918 /*
    919  * configure security policy index (src/dst/proto/sport/dport)
    920  * by looking at the content of mbuf.
    921  * the caller is responsible for error recovery (like clearing up spidx).
    922  */
    923 static int
    924 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    925 {
    926 	struct ip *ip = NULL;
    927 	struct ip ipbuf;
    928 	u_int v;
    929 	struct mbuf *n;
    930 	int len;
    931 	int error;
    932 
    933 	KASSERT(m != NULL);
    934 
    935 	/*
    936 	 * validate m->m_pkthdr.len.  we see incorrect length if we
    937 	 * mistakenly call this function with inconsistent mbuf chain
    938 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
    939 	 */
    940 	len = 0;
    941 	for (n = m; n; n = n->m_next)
    942 		len += n->m_len;
    943 	if (m->m_pkthdr.len != len) {
    944 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    945 		    "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
    946 		    len, m->m_pkthdr.len);
    947 		KASSERTMSG(0, "impossible");
    948 		return EINVAL;
    949 	}
    950 
    951 	if (m->m_pkthdr.len < sizeof(struct ip)) {
    952 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    953 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
    954 		    m->m_pkthdr.len);
    955 		return EINVAL;
    956 	}
    957 
    958 	if (m->m_len >= sizeof(*ip)) {
    959 		ip = mtod(m, struct ip *);
    960 	} else {
    961 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
    962 		ip = &ipbuf;
    963 	}
    964 	v = ip->ip_v;
    965 	switch (v) {
    966 	case 4:
    967 		error = ipsec4_setspidx_ipaddr(m, spidx);
    968 		if (error)
    969 			return error;
    970 		ipsec4_get_ulp(m, spidx, needport);
    971 		return 0;
    972 #ifdef INET6
    973 	case 6:
    974 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
    975 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    976 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
    977 			    "ignored.\n", m->m_pkthdr.len);
    978 			return EINVAL;
    979 		}
    980 		error = ipsec6_setspidx_ipaddr(m, spidx);
    981 		if (error)
    982 			return error;
    983 		ipsec6_get_ulp(m, spidx, needport);
    984 		return 0;
    985 #endif
    986 	default:
    987 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    988 		    "unknown IP version %u, ignored.\n", v);
    989 		return EINVAL;
    990 	}
    991 }
    992 
    993 static void
    994 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    995 {
    996 	u_int8_t nxt;
    997 	int off;
    998 
    999 	KASSERT(m != NULL);
   1000 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
   1001 
   1002 	/* NB: ip_input() flips it into host endian XXX need more checking */
   1003 	if (m->m_len >= sizeof(struct ip)) {
   1004 		struct ip *ip = mtod(m, struct ip *);
   1005 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
   1006 			goto done;
   1007 		off = ip->ip_hl << 2;
   1008 		nxt = ip->ip_p;
   1009 	} else {
   1010 		struct ip ih;
   1011 
   1012 		m_copydata(m, 0, sizeof(struct ip), &ih);
   1013 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
   1014 			goto done;
   1015 		off = ih.ip_hl << 2;
   1016 		nxt = ih.ip_p;
   1017 	}
   1018 
   1019 	while (off < m->m_pkthdr.len) {
   1020 		struct ip6_ext ip6e;
   1021 		struct tcphdr th;
   1022 		struct udphdr uh;
   1023 		struct icmp icmph;
   1024 
   1025 		switch (nxt) {
   1026 		case IPPROTO_TCP:
   1027 			spidx->ul_proto = nxt;
   1028 			if (!needport)
   1029 				goto done_proto;
   1030 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1031 				goto done;
   1032 			m_copydata(m, off, sizeof(th), &th);
   1033 			spidx->src.sin.sin_port = th.th_sport;
   1034 			spidx->dst.sin.sin_port = th.th_dport;
   1035 			return;
   1036 		case IPPROTO_UDP:
   1037 			spidx->ul_proto = nxt;
   1038 			if (!needport)
   1039 				goto done_proto;
   1040 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1041 				goto done;
   1042 			m_copydata(m, off, sizeof(uh), &uh);
   1043 			spidx->src.sin.sin_port = uh.uh_sport;
   1044 			spidx->dst.sin.sin_port = uh.uh_dport;
   1045 			return;
   1046 		case IPPROTO_AH:
   1047 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
   1048 				goto done;
   1049 			/* XXX sigh, this works but is totally bogus */
   1050 			m_copydata(m, off, sizeof(ip6e), &ip6e);
   1051 			off += (ip6e.ip6e_len + 2) << 2;
   1052 			nxt = ip6e.ip6e_nxt;
   1053 			break;
   1054 		case IPPROTO_ICMP:
   1055 			spidx->ul_proto = nxt;
   1056 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
   1057 				goto done;
   1058 			m_copydata(m, off, sizeof(icmph), &icmph);
   1059 			((struct sockaddr_in *)&spidx->src)->sin_port =
   1060 			    htons((uint16_t)icmph.icmp_type);
   1061 			((struct sockaddr_in *)&spidx->dst)->sin_port =
   1062 			    htons((uint16_t)icmph.icmp_code);
   1063 			return;
   1064 		default:
   1065 			/* XXX intermediate headers??? */
   1066 			spidx->ul_proto = nxt;
   1067 			goto done_proto;
   1068 		}
   1069 	}
   1070 done:
   1071 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1072 done_proto:
   1073 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
   1074 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
   1075 }
   1076 
   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, int needport)
   1109 {
   1110 	int off, nxt;
   1111 	struct tcphdr th;
   1112 	struct udphdr uh;
   1113 	struct icmp6_hdr icmph;
   1114 
   1115 	KASSERT(m != NULL);
   1116 
   1117 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1118 		kdebug_mbuf(__func__, m);
   1119 	}
   1120 
   1121 	/* set default */
   1122 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1123 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
   1124 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
   1125 
   1126 	nxt = -1;
   1127 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
   1128 	if (off < 0 || m->m_pkthdr.len < off)
   1129 		return;
   1130 
   1131 	switch (nxt) {
   1132 	case IPPROTO_TCP:
   1133 		spidx->ul_proto = nxt;
   1134 		if (!needport)
   1135 			break;
   1136 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1137 			break;
   1138 		m_copydata(m, off, sizeof(th), &th);
   1139 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
   1140 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
   1141 		break;
   1142 	case IPPROTO_UDP:
   1143 		spidx->ul_proto = nxt;
   1144 		if (!needport)
   1145 			break;
   1146 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1147 			break;
   1148 		m_copydata(m, off, sizeof(uh), &uh);
   1149 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
   1150 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
   1151 		break;
   1152 	case IPPROTO_ICMPV6:
   1153 		spidx->ul_proto = nxt;
   1154 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
   1155 			break;
   1156 		m_copydata(m, off, sizeof(icmph), &icmph);
   1157 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
   1158 		    htons((uint16_t)icmph.icmp6_type);
   1159 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
   1160 		    htons((uint16_t)icmph.icmp6_code);
   1161 		break;
   1162 	default:
   1163 		/* XXX intermediate headers??? */
   1164 		spidx->ul_proto = nxt;
   1165 		break;
   1166 	}
   1167 }
   1168 
   1169 static int
   1170 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1171 {
   1172 	struct ip6_hdr *ip6 = NULL;
   1173 	struct ip6_hdr ip6buf;
   1174 	struct sockaddr_in6 *sin6;
   1175 
   1176 	if (m->m_len >= sizeof(*ip6))
   1177 		ip6 = mtod(m, struct ip6_hdr *);
   1178 	else {
   1179 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
   1180 		ip6 = &ip6buf;
   1181 	}
   1182 
   1183 	sin6 = (struct sockaddr_in6 *)&spidx->src;
   1184 	memset(sin6, 0, sizeof(*sin6));
   1185 	sin6->sin6_family = AF_INET6;
   1186 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1187 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
   1188 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
   1189 		sin6->sin6_addr.s6_addr16[1] = 0;
   1190 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
   1191 	}
   1192 	spidx->prefs = sizeof(struct in6_addr) << 3;
   1193 
   1194 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
   1195 	memset(sin6, 0, sizeof(*sin6));
   1196 	sin6->sin6_family = AF_INET6;
   1197 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1198 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
   1199 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
   1200 		sin6->sin6_addr.s6_addr16[1] = 0;
   1201 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
   1202 	}
   1203 	spidx->prefd = sizeof(struct in6_addr) << 3;
   1204 
   1205 	return 0;
   1206 }
   1207 #endif
   1208 
   1209 static void
   1210 ipsec_delpcbpolicy(struct inpcbpolicy *p)
   1211 {
   1212 
   1213 	kmem_intr_free(p, sizeof(*p));
   1214 }
   1215 
   1216 /* initialize policy in PCB */
   1217 int
   1218 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
   1219 {
   1220 	struct inpcbpolicy *new;
   1221 
   1222 	KASSERT(so != NULL);
   1223 	KASSERT(policy != NULL);
   1224 
   1225 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
   1226 	if (new == NULL) {
   1227 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1228 		return ENOBUFS;
   1229 	}
   1230 
   1231 	if (IPSEC_PRIVILEGED_SO(so))
   1232 		new->priv = 1;
   1233 	else
   1234 		new->priv = 0;
   1235 
   1236 	/*
   1237 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
   1238 	 */
   1239 	new->sp_in = &ipsec_dummy_sp;
   1240 	new->sp_out = &ipsec_dummy_sp;
   1241 
   1242 	*policy = new;
   1243 
   1244 	return 0;
   1245 }
   1246 
   1247 #if 0 /* unused */
   1248 /* copy old ipsec policy into new */
   1249 int
   1250 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
   1251 {
   1252 	struct secpolicy *sp;
   1253 
   1254 	sp = ipsec_deepcopy_policy(old->sp_in);
   1255 	if (sp) {
   1256 		KEY_SP_UNREF(&new->sp_in);
   1257 		new->sp_in = sp;
   1258 	} else
   1259 		return ENOBUFS;
   1260 
   1261 	sp = ipsec_deepcopy_policy(old->sp_out);
   1262 	if (sp) {
   1263 		KEY_SP_UNREF(&new->sp_out);
   1264 		new->sp_out = sp;
   1265 	} else
   1266 		return ENOBUFS;
   1267 
   1268 	new->priv = old->priv;
   1269 
   1270 	return 0;
   1271 }
   1272 
   1273 /* deep-copy a policy in PCB */
   1274 static struct secpolicy *
   1275 ipsec_deepcopy_policy(const struct secpolicy *src)
   1276 {
   1277 	struct ipsecrequest *newchain = NULL;
   1278 	const struct ipsecrequest *p;
   1279 	struct ipsecrequest **q;
   1280 	struct secpolicy *dst;
   1281 
   1282 	if (src == NULL)
   1283 		return NULL;
   1284 	dst = KEY_NEWSP();
   1285 	if (dst == NULL)
   1286 		return NULL;
   1287 
   1288 	/*
   1289 	 * deep-copy IPsec request chain.  This is required since struct
   1290 	 * ipsecrequest is not reference counted.
   1291 	 */
   1292 	q = &newchain;
   1293 	for (p = src->req; p; p = p->next) {
   1294 		*q = kmem_zalloc(sizeof(**q), KM_SLEEP);
   1295 		(*q)->next = NULL;
   1296 
   1297 		(*q)->saidx.proto = p->saidx.proto;
   1298 		(*q)->saidx.mode = p->saidx.mode;
   1299 		(*q)->level = p->level;
   1300 		(*q)->saidx.reqid = p->saidx.reqid;
   1301 
   1302 		memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
   1303 		memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
   1304 
   1305 		(*q)->sp = dst;
   1306 
   1307 		q = &((*q)->next);
   1308 	}
   1309 
   1310 	dst->req = newchain;
   1311 	dst->state = src->state;
   1312 	dst->policy = src->policy;
   1313 	/* do not touch the refcnt fields */
   1314 
   1315 	return dst;
   1316 }
   1317 #endif
   1318 
   1319 static void
   1320 ipsec_destroy_policy(struct secpolicy *sp)
   1321 {
   1322 
   1323 	if (sp == &ipsec_dummy_sp)
   1324 		; /* It's dummy. No need to free it. */
   1325 	else {
   1326 		/*
   1327 		 * We cannot destroy here because it can be called in
   1328 		 * softint. So mark the SP as DEAD and let the timer
   1329 		 * destroy it. See key_timehandler_spd.
   1330 		 */
   1331 		sp->state = IPSEC_SPSTATE_DEAD;
   1332 	}
   1333 }
   1334 
   1335 /* set policy and ipsec request if present. */
   1336 static int
   1337 ipsec_set_policy(struct secpolicy **policy, int optname, const void *request,
   1338     size_t len, kauth_cred_t cred)
   1339 {
   1340 	const struct sadb_x_policy *xpl;
   1341 	struct secpolicy *newsp = NULL, *oldsp;
   1342 	int error;
   1343 
   1344 	KASSERT(!cpu_softintr_p());
   1345 
   1346 	/* sanity check. */
   1347 	if (policy == NULL || *policy == NULL || request == NULL)
   1348 		return EINVAL;
   1349 	if (len < sizeof(*xpl))
   1350 		return EINVAL;
   1351 	xpl = (const struct sadb_x_policy *)request;
   1352 
   1353 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1354 		kdebug_sadb_xpolicy("set passed policy", request);
   1355 	}
   1356 
   1357 	/* check policy type */
   1358 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
   1359 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
   1360 	    xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
   1361 		return EINVAL;
   1362 
   1363 	/* check privileged socket */
   1364 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
   1365 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
   1366 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
   1367 		if (error)
   1368 			return error;
   1369 	}
   1370 
   1371 	/* allocation new SP entry */
   1372 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
   1373 		return error;
   1374 
   1375 	key_init_sp(newsp);
   1376 	newsp->created = time_uptime;
   1377 	/* Insert the global list for SPs for sockets */
   1378 	key_socksplist_add(newsp);
   1379 
   1380 	/* clear old SP and set new SP */
   1381 	oldsp = *policy;
   1382 	*policy = newsp;
   1383 	ipsec_destroy_policy(oldsp);
   1384 
   1385 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1386 		printf("%s: new policy\n", __func__);
   1387 		kdebug_secpolicy(newsp);
   1388 	}
   1389 
   1390 	return 0;
   1391 }
   1392 
   1393 static int
   1394 ipsec_get_policy(struct secpolicy *policy, struct mbuf **mp)
   1395 {
   1396 
   1397 	/* sanity check. */
   1398 	if (policy == NULL || mp == NULL)
   1399 		return EINVAL;
   1400 
   1401 	*mp = key_sp2msg(policy, M_NOWAIT);
   1402 	if (!*mp) {
   1403 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1404 		return ENOBUFS;
   1405 	}
   1406 
   1407 	(*mp)->m_type = MT_DATA;
   1408 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1409 		kdebug_mbuf(__func__, *mp);
   1410 	}
   1411 
   1412 	return 0;
   1413 }
   1414 
   1415 int
   1416 ipsec4_set_policy(struct inpcb *inp, int optname, const void *request,
   1417     size_t len, kauth_cred_t cred)
   1418 {
   1419 	const struct sadb_x_policy *xpl;
   1420 	struct secpolicy **policy;
   1421 
   1422 	KASSERT(!cpu_softintr_p());
   1423 	KASSERT(inp != NULL);
   1424 	KASSERT(inp_locked(inp));
   1425 	KASSERT(request != NULL);
   1426 
   1427 	if (len < sizeof(*xpl))
   1428 		return EINVAL;
   1429 	xpl = (const struct sadb_x_policy *)request;
   1430 
   1431 	KASSERT(inp->inp_sp != NULL);
   1432 
   1433 	/* select direction */
   1434 	switch (xpl->sadb_x_policy_dir) {
   1435 	case IPSEC_DIR_INBOUND:
   1436 		policy = &inp->inp_sp->sp_in;
   1437 		break;
   1438 	case IPSEC_DIR_OUTBOUND:
   1439 		policy = &inp->inp_sp->sp_out;
   1440 		break;
   1441 	default:
   1442 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1443 		    xpl->sadb_x_policy_dir);
   1444 		return EINVAL;
   1445 	}
   1446 
   1447 	return ipsec_set_policy(policy, optname, request, len, cred);
   1448 }
   1449 
   1450 int
   1451 ipsec4_get_policy(struct inpcb *inp, const void *request, size_t len,
   1452     struct mbuf **mp)
   1453 {
   1454 	const struct sadb_x_policy *xpl;
   1455 	struct secpolicy *policy;
   1456 
   1457 	/* sanity check. */
   1458 	if (inp == NULL || request == NULL || mp == NULL)
   1459 		return EINVAL;
   1460 	KASSERT(inp->inp_sp != NULL);
   1461 	if (len < sizeof(*xpl))
   1462 		return EINVAL;
   1463 	xpl = (const struct sadb_x_policy *)request;
   1464 
   1465 	/* select direction */
   1466 	switch (xpl->sadb_x_policy_dir) {
   1467 	case IPSEC_DIR_INBOUND:
   1468 		policy = inp->inp_sp->sp_in;
   1469 		break;
   1470 	case IPSEC_DIR_OUTBOUND:
   1471 		policy = inp->inp_sp->sp_out;
   1472 		break;
   1473 	default:
   1474 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1475 		    xpl->sadb_x_policy_dir);
   1476 		return EINVAL;
   1477 	}
   1478 
   1479 	return ipsec_get_policy(policy, mp);
   1480 }
   1481 
   1482 int
   1483 ipsec4_delete_pcbpolicy(struct inpcb *inp)
   1484 {
   1485 
   1486 	KASSERT(inp != NULL);
   1487 
   1488 	if (inp->inp_sp == NULL)
   1489 		return 0;
   1490 
   1491 	if (inp->inp_sp->sp_in != NULL)
   1492 		ipsec_destroy_policy(inp->inp_sp->sp_in);
   1493 
   1494 	if (inp->inp_sp->sp_out != NULL)
   1495 		ipsec_destroy_policy(inp->inp_sp->sp_out);
   1496 
   1497 	ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
   1498 
   1499 	ipsec_delpcbpolicy(inp->inp_sp);
   1500 	inp->inp_sp = NULL;
   1501 
   1502 	return 0;
   1503 }
   1504 
   1505 #ifdef INET6
   1506 int
   1507 ipsec6_set_policy(struct in6pcb *in6p, int optname, const void *request,
   1508     size_t len, kauth_cred_t cred)
   1509 {
   1510 	const struct sadb_x_policy *xpl;
   1511 	struct secpolicy **policy;
   1512 
   1513 	KASSERT(!cpu_softintr_p());
   1514 	KASSERT(in6p_locked(in6p));
   1515 
   1516 	/* sanity check. */
   1517 	if (in6p == NULL || request == NULL)
   1518 		return EINVAL;
   1519 	if (len < sizeof(*xpl))
   1520 		return EINVAL;
   1521 	xpl = (const struct sadb_x_policy *)request;
   1522 
   1523 	/* select direction */
   1524 	switch (xpl->sadb_x_policy_dir) {
   1525 	case IPSEC_DIR_INBOUND:
   1526 		policy = &in6p->in6p_sp->sp_in;
   1527 		break;
   1528 	case IPSEC_DIR_OUTBOUND:
   1529 		policy = &in6p->in6p_sp->sp_out;
   1530 		break;
   1531 	default:
   1532 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1533 		    xpl->sadb_x_policy_dir);
   1534 		return EINVAL;
   1535 	}
   1536 
   1537 	return ipsec_set_policy(policy, optname, request, len, cred);
   1538 }
   1539 
   1540 int
   1541 ipsec6_get_policy(struct in6pcb *in6p, const void *request, size_t len,
   1542     struct mbuf **mp)
   1543 {
   1544 	const struct sadb_x_policy *xpl;
   1545 	struct secpolicy *policy;
   1546 
   1547 	/* sanity check. */
   1548 	if (in6p == NULL || request == NULL || mp == NULL)
   1549 		return EINVAL;
   1550 	KASSERT(in6p->in6p_sp != NULL);
   1551 	if (len < sizeof(*xpl))
   1552 		return EINVAL;
   1553 	xpl = (const struct sadb_x_policy *)request;
   1554 
   1555 	/* select direction */
   1556 	switch (xpl->sadb_x_policy_dir) {
   1557 	case IPSEC_DIR_INBOUND:
   1558 		policy = in6p->in6p_sp->sp_in;
   1559 		break;
   1560 	case IPSEC_DIR_OUTBOUND:
   1561 		policy = in6p->in6p_sp->sp_out;
   1562 		break;
   1563 	default:
   1564 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1565 		    xpl->sadb_x_policy_dir);
   1566 		return EINVAL;
   1567 	}
   1568 
   1569 	return ipsec_get_policy(policy, mp);
   1570 }
   1571 
   1572 int
   1573 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
   1574 {
   1575 
   1576 	KASSERT(in6p != NULL);
   1577 
   1578 	if (in6p->in6p_sp == NULL)
   1579 		return 0;
   1580 
   1581 	if (in6p->in6p_sp->sp_in != NULL)
   1582 		ipsec_destroy_policy(in6p->in6p_sp->sp_in);
   1583 
   1584 	if (in6p->in6p_sp->sp_out != NULL)
   1585 		ipsec_destroy_policy(in6p->in6p_sp->sp_out);
   1586 
   1587 	ipsec_invalpcbcache(in6p->in6p_sp, IPSEC_DIR_ANY);
   1588 
   1589 	ipsec_delpcbpolicy(in6p->in6p_sp);
   1590 	in6p->in6p_sp = NULL;
   1591 
   1592 	return 0;
   1593 }
   1594 #endif
   1595 
   1596 /*
   1597  * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
   1598  */
   1599 u_int
   1600 ipsec_get_reqlevel(const struct ipsecrequest *isr)
   1601 {
   1602 	u_int level = 0;
   1603 	u_int esp_trans_deflev, esp_net_deflev;
   1604 	u_int ah_trans_deflev, ah_net_deflev;
   1605 
   1606 	KASSERT(isr != NULL);
   1607 	KASSERT(isr->sp != NULL);
   1608 	KASSERTMSG(
   1609 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
   1610 	    "af family mismatch, src %u, dst %u",
   1611 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
   1612 
   1613 /* XXX note that we have ipseclog() expanded here - code sync issue */
   1614 #define IPSEC_CHECK_DEFAULT(lev)					\
   1615     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
   1616     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
   1617 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
   1618 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
   1619 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
   1620     : (lev))
   1621 
   1622 	/* set default level */
   1623 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
   1624 #ifdef INET
   1625 	case AF_INET:
   1626 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
   1627 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
   1628 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
   1629 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
   1630 		break;
   1631 #endif
   1632 #ifdef INET6
   1633 	case AF_INET6:
   1634 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
   1635 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
   1636 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
   1637 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
   1638 		break;
   1639 #endif /* INET6 */
   1640 	default:
   1641 		panic("%s: unknown af %u", __func__,
   1642 		    isr->sp->spidx.src.sa.sa_family);
   1643 	}
   1644 
   1645 #undef IPSEC_CHECK_DEFAULT
   1646 
   1647 	/* set level */
   1648 	switch (isr->level) {
   1649 	case IPSEC_LEVEL_DEFAULT:
   1650 		switch (isr->saidx.proto) {
   1651 		case IPPROTO_ESP:
   1652 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1653 				level = esp_net_deflev;
   1654 			else
   1655 				level = esp_trans_deflev;
   1656 			break;
   1657 		case IPPROTO_AH:
   1658 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1659 				level = ah_net_deflev;
   1660 			else
   1661 				level = ah_trans_deflev;
   1662 			break;
   1663 		case IPPROTO_IPCOMP:
   1664 			/*
   1665 			 * we don't really care, as IPcomp document says that
   1666 			 * we shouldn't compress small packets
   1667 			 */
   1668 			level = IPSEC_LEVEL_USE;
   1669 			break;
   1670 		default:
   1671 			panic("%s: Illegal protocol defined %u", __func__,
   1672 			    isr->saidx.proto);
   1673 		}
   1674 		break;
   1675 
   1676 	case IPSEC_LEVEL_USE:
   1677 	case IPSEC_LEVEL_REQUIRE:
   1678 		level = isr->level;
   1679 		break;
   1680 	case IPSEC_LEVEL_UNIQUE:
   1681 		level = IPSEC_LEVEL_REQUIRE;
   1682 		break;
   1683 
   1684 	default:
   1685 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
   1686 	}
   1687 
   1688 	return level;
   1689 }
   1690 
   1691 /*
   1692  * Check security policy requirements against the actual packet contents.
   1693  *
   1694  * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
   1695  * then kick it.
   1696  */
   1697 int
   1698 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
   1699 {
   1700 	struct ipsecrequest *isr;
   1701 
   1702 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1703 		printf("%s: using SP\n", __func__);
   1704 		kdebug_secpolicy(sp);
   1705 	}
   1706 
   1707 	/* check policy */
   1708 	switch (sp->policy) {
   1709 	case IPSEC_POLICY_DISCARD:
   1710 		return 1;
   1711 	case IPSEC_POLICY_BYPASS:
   1712 	case IPSEC_POLICY_NONE:
   1713 		return 0;
   1714 	}
   1715 
   1716 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1717 	    "invalid policy %u", sp->policy);
   1718 
   1719 	/* XXX should compare policy against ipsec header history */
   1720 
   1721 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1722 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
   1723 			continue;
   1724 		switch (isr->saidx.proto) {
   1725 		case IPPROTO_ESP:
   1726 			if ((m->m_flags & M_DECRYPTED) == 0) {
   1727 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1728 				    "ESP m_flags:%x\n", m->m_flags);
   1729 				return 1;
   1730 			}
   1731 			break;
   1732 		case IPPROTO_AH:
   1733 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
   1734 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1735 				    "AH m_flags:%x\n", m->m_flags);
   1736 				return 1;
   1737 			}
   1738 			break;
   1739 		case IPPROTO_IPCOMP:
   1740 			/*
   1741 			 * We don't really care, as IPcomp document
   1742 			 * says that we shouldn't compress small
   1743 			 * packets, IPComp policy should always be
   1744 			 * treated as being in "use" level.
   1745 			 */
   1746 			break;
   1747 		}
   1748 	}
   1749 
   1750 	return 0;
   1751 }
   1752 
   1753 /*
   1754  * Check AH/ESP integrity.
   1755  * This function is called from tcp_input(), udp_input(),
   1756  * and {ah,esp}4_input for tunnel mode
   1757  */
   1758 int
   1759 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
   1760 {
   1761 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1762 	struct secpolicy *sp;
   1763 	int error;
   1764 	int result;
   1765 
   1766 	KASSERT(m != NULL);
   1767 
   1768 	if (inph == NULL)
   1769 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1770 		    IP_FORWARDING, &error);
   1771 	else
   1772 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1773 		    inph, &error);
   1774 
   1775 	if (sp != NULL) {
   1776 		result = ipsec_sp_reject(sp, m);
   1777 		if (result)
   1778 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1779 		KEY_SP_UNREF(&sp);
   1780 	} else {
   1781 		result = 0;
   1782 	}
   1783 	return result;
   1784 }
   1785 
   1786 #ifdef INET6
   1787 /*
   1788  * Check AH/ESP integrity.
   1789  * This function is called from tcp6_input(), udp6_input(),
   1790  * and {ah,esp}6_input for tunnel mode
   1791  */
   1792 int
   1793 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
   1794 {
   1795 	struct inpcb_hdr *inph = (struct inpcb_hdr *)in6p;
   1796 	struct secpolicy *sp;
   1797 	int error;
   1798 	int result;
   1799 
   1800 	KASSERT(m != NULL);
   1801 
   1802 	if (inph == NULL)
   1803 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1804 		    IP_FORWARDING, &error);
   1805 	else
   1806 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1807 		    inph, &error);
   1808 
   1809 	if (sp != NULL) {
   1810 		result = ipsec_sp_reject(sp, m);
   1811 		if (result)
   1812 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1813 		KEY_SP_UNREF(&sp);
   1814 	} else {
   1815 		result = 0;
   1816 	}
   1817 	return result;
   1818 }
   1819 #endif
   1820 
   1821 /*
   1822  * Compute the byte size to be occupied by the IPsec header. If it is
   1823  * tunneled, it includes the size of outer IP header.
   1824  */
   1825 static size_t
   1826 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
   1827 {
   1828 	struct ipsecrequest *isr;
   1829 	size_t siz;
   1830 
   1831 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1832 		printf("%s: using SP\n", __func__);
   1833 		kdebug_secpolicy(sp);
   1834 	}
   1835 
   1836 	switch (sp->policy) {
   1837 	case IPSEC_POLICY_DISCARD:
   1838 	case IPSEC_POLICY_BYPASS:
   1839 	case IPSEC_POLICY_NONE:
   1840 		return 0;
   1841 	}
   1842 
   1843 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1844 	    "invalid policy %u", sp->policy);
   1845 
   1846 	siz = 0;
   1847 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1848 		size_t clen = 0;
   1849 		struct secasvar *sav;
   1850 
   1851 		switch (isr->saidx.proto) {
   1852 		case IPPROTO_ESP:
   1853 			sav = ipsec_lookup_sa(isr, m);
   1854 			if (sav != NULL) {
   1855 				clen = esp_hdrsiz(sav);
   1856 				KEY_SA_UNREF(&sav);
   1857 			} else
   1858 				clen = esp_hdrsiz(NULL);
   1859 			break;
   1860 		case IPPROTO_AH:
   1861 			sav = ipsec_lookup_sa(isr, m);
   1862 			if (sav != NULL) {
   1863 				clen = ah_hdrsiz(sav);
   1864 				KEY_SA_UNREF(&sav);
   1865 			} else
   1866 				clen = ah_hdrsiz(NULL);
   1867 			break;
   1868 		case IPPROTO_IPCOMP:
   1869 			clen = sizeof(struct ipcomp);
   1870 			break;
   1871 		}
   1872 
   1873 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
   1874 			switch (isr->saidx.dst.sa.sa_family) {
   1875 			case AF_INET:
   1876 				clen += sizeof(struct ip);
   1877 				break;
   1878 #ifdef INET6
   1879 			case AF_INET6:
   1880 				clen += sizeof(struct ip6_hdr);
   1881 				break;
   1882 #endif
   1883 			default:
   1884 				IPSECLOG(LOG_ERR, "unknown AF %d in "
   1885 				    "IPsec tunnel SA\n",
   1886 				    ((const struct sockaddr *)&isr->saidx.dst)
   1887 				    ->sa_family);
   1888 				break;
   1889 			}
   1890 		}
   1891 		siz += clen;
   1892 	}
   1893 
   1894 	return siz;
   1895 }
   1896 
   1897 size_t
   1898 ipsec4_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
   1899 {
   1900 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
   1901 	struct secpolicy *sp;
   1902 	int error;
   1903 	size_t size;
   1904 
   1905 	KASSERT(m != NULL);
   1906 	KASSERTMSG(inph == NULL || inph->inph_socket != NULL,
   1907 	    "socket w/o inpcb");
   1908 
   1909 	if (inph == NULL)
   1910 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1911 	else
   1912 		sp = ipsec_getpolicybysock(m, dir, inph, &error);
   1913 
   1914 	if (sp != NULL) {
   1915 		size = ipsec_sp_hdrsiz(sp, m);
   1916 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   1917 		KEY_SP_UNREF(&sp);
   1918 	} else {
   1919 		size = 0;
   1920 	}
   1921 
   1922 	return size;
   1923 }
   1924 
   1925 #ifdef INET6
   1926 size_t
   1927 ipsec6_hdrsiz(struct mbuf *m, u_int dir, struct in6pcb *in6p)
   1928 {
   1929 	struct inpcb_hdr *inph = (struct inpcb_hdr *)in6p;
   1930 	struct secpolicy *sp;
   1931 	int error;
   1932 	size_t size;
   1933 
   1934 	KASSERT(m != NULL);
   1935 	KASSERTMSG(inph == NULL || inph->inph_socket != NULL,
   1936 	    "socket w/o inpcb");
   1937 
   1938 	if (inph == NULL)
   1939 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1940 	else
   1941 		sp = ipsec_getpolicybysock(m, dir, inph, &error);
   1942 
   1943 	if (sp != NULL) {
   1944 		size = ipsec_sp_hdrsiz(sp, m);
   1945 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   1946 		KEY_SP_UNREF(&sp);
   1947 	} else {
   1948 		size = 0;
   1949 	}
   1950 
   1951 	return size;
   1952 }
   1953 #endif
   1954 
   1955 /*
   1956  * Check the variable replay window.
   1957  * ipsec_chkreplay() performs replay check before ICV verification.
   1958  * ipsec_updatereplay() updates replay bitmap.  This must be called after
   1959  * ICV verification (it also performs replay check, which is usually done
   1960  * beforehand).
   1961  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
   1962  *
   1963  * based on RFC 2401.
   1964  */
   1965 int
   1966 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
   1967 {
   1968 	const struct secreplay *replay;
   1969 	u_int32_t diff;
   1970 	int fr;
   1971 	u_int32_t wsizeb;	/* constant: bits of window size */
   1972 	int frlast;		/* constant: last frame */
   1973 
   1974 	IPSEC_SPLASSERT_SOFTNET(__func__);
   1975 
   1976 	KASSERT(sav != NULL);
   1977 	KASSERT(sav->replay != NULL);
   1978 
   1979 	replay = sav->replay;
   1980 
   1981 	if (replay->wsize == 0)
   1982 		return 1;	/* no need to check replay. */
   1983 
   1984 	/* constant */
   1985 	frlast = replay->wsize - 1;
   1986 	wsizeb = replay->wsize << 3;
   1987 
   1988 	/* sequence number of 0 is invalid */
   1989 	if (seq == 0)
   1990 		return 0;
   1991 
   1992 	/* first time is always okay */
   1993 	if (replay->count == 0)
   1994 		return 1;
   1995 
   1996 	if (seq > replay->lastseq) {
   1997 		/* larger sequences are okay */
   1998 		return 1;
   1999 	} else {
   2000 		/* seq is equal or less than lastseq. */
   2001 		diff = replay->lastseq - seq;
   2002 
   2003 		/* over range to check, i.e. too old or wrapped */
   2004 		if (diff >= wsizeb)
   2005 			return 0;
   2006 
   2007 		fr = frlast - diff / 8;
   2008 
   2009 		/* this packet already seen ? */
   2010 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   2011 			return 0;
   2012 
   2013 		/* out of order but good */
   2014 		return 1;
   2015 	}
   2016 }
   2017 
   2018 /*
   2019  * check replay counter whether to update or not.
   2020  * OUT:	0:	OK
   2021  *	1:	NG
   2022  */
   2023 int
   2024 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
   2025 {
   2026 	struct secreplay *replay;
   2027 	u_int32_t diff;
   2028 	int fr;
   2029 	u_int32_t wsizeb;	/* constant: bits of window size */
   2030 	int frlast;		/* constant: last frame */
   2031 
   2032 	IPSEC_SPLASSERT_SOFTNET(__func__);
   2033 
   2034 	KASSERT(sav != NULL);
   2035 	KASSERT(sav->replay != NULL);
   2036 
   2037 	replay = sav->replay;
   2038 
   2039 	if (replay->wsize == 0)
   2040 		goto ok;	/* no need to check replay. */
   2041 
   2042 	/* constant */
   2043 	frlast = replay->wsize - 1;
   2044 	wsizeb = replay->wsize << 3;
   2045 
   2046 	/* sequence number of 0 is invalid */
   2047 	if (seq == 0)
   2048 		return 1;
   2049 
   2050 	/* first time */
   2051 	if (replay->count == 0) {
   2052 		replay->lastseq = seq;
   2053 		memset(replay->bitmap, 0, replay->wsize);
   2054 		(replay->bitmap)[frlast] = 1;
   2055 		goto ok;
   2056 	}
   2057 
   2058 	if (seq > replay->lastseq) {
   2059 		/* seq is larger than lastseq. */
   2060 		diff = seq - replay->lastseq;
   2061 
   2062 		/* new larger sequence number */
   2063 		if (diff < wsizeb) {
   2064 			/* In window */
   2065 			/* set bit for this packet */
   2066 			vshiftl(replay->bitmap, diff, replay->wsize);
   2067 			(replay->bitmap)[frlast] |= 1;
   2068 		} else {
   2069 			/* this packet has a "way larger" */
   2070 			memset(replay->bitmap, 0, replay->wsize);
   2071 			(replay->bitmap)[frlast] = 1;
   2072 		}
   2073 		replay->lastseq = seq;
   2074 
   2075 		/* larger is good */
   2076 	} else {
   2077 		/* seq is equal or less than lastseq. */
   2078 		diff = replay->lastseq - seq;
   2079 
   2080 		/* over range to check, i.e. too old or wrapped */
   2081 		if (diff >= wsizeb)
   2082 			return 1;
   2083 
   2084 		fr = frlast - diff / 8;
   2085 
   2086 		/* this packet already seen ? */
   2087 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   2088 			return 1;
   2089 
   2090 		/* mark as seen */
   2091 		(replay->bitmap)[fr] |= (1 << (diff % 8));
   2092 
   2093 		/* out of order but good */
   2094 	}
   2095 
   2096 ok:
   2097 	if (replay->count == ~0) {
   2098 		char buf[IPSEC_LOGSASTRLEN];
   2099 
   2100 		/* set overflow flag */
   2101 		replay->overflow++;
   2102 
   2103 		/* don't increment, no more packets accepted */
   2104 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
   2105 			return 1;
   2106 
   2107 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
   2108 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
   2109 	}
   2110 
   2111 	replay->count++;
   2112 
   2113 	return 0;
   2114 }
   2115 
   2116 /*
   2117  * shift variable length buffer to left.
   2118  * IN:	bitmap: pointer to the buffer
   2119  *	nbit:	the number of to shift.
   2120  *	wsize:	buffer size (bytes).
   2121  */
   2122 static void
   2123 vshiftl(unsigned char *bitmap, int nbit, int wsize)
   2124 {
   2125 	int s, j, i;
   2126 	unsigned char over;
   2127 
   2128 	for (j = 0; j < nbit; j += 8) {
   2129 		s = (nbit - j < 8) ? (nbit - j): 8;
   2130 		bitmap[0] <<= s;
   2131 		for (i = 1; i < wsize; i++) {
   2132 			over = (bitmap[i] >> (8 - s));
   2133 			bitmap[i] <<= s;
   2134 			bitmap[i-1] |= over;
   2135 		}
   2136 	}
   2137 
   2138 	return;
   2139 }
   2140 
   2141 /* Return a printable string for the address. */
   2142 const char *
   2143 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
   2144 {
   2145 	switch (sa->sa.sa_family) {
   2146 #if INET
   2147 	case AF_INET:
   2148 		in_print(buf, size, &sa->sin.sin_addr);
   2149 		return buf;
   2150 #endif
   2151 #if INET6
   2152 	case AF_INET6:
   2153 		in6_print(buf, size, &sa->sin6.sin6_addr);
   2154 		return buf;
   2155 #endif
   2156 	default:
   2157 		return "(unknown address family)";
   2158 	}
   2159 }
   2160 
   2161 const char *
   2162 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
   2163 {
   2164 	const struct secasindex *saidx = &sav->sah->saidx;
   2165 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
   2166 
   2167 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
   2168 	    "af family mismatch, src %u, dst %u",
   2169 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
   2170 
   2171 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
   2172 	    (u_int32_t)ntohl(sav->spi),
   2173 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
   2174 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
   2175 
   2176 	return buf;
   2177 }
   2178 
   2179 void
   2180 ipsec_dumpmbuf(struct mbuf *m)
   2181 {
   2182 	int totlen;
   2183 	int i;
   2184 	u_char *p;
   2185 
   2186 	totlen = 0;
   2187 	printf("---\n");
   2188 	while (m) {
   2189 		p = mtod(m, u_char *);
   2190 		for (i = 0; i < m->m_len; i++) {
   2191 			printf("%02x ", p[i]);
   2192 			totlen++;
   2193 			if (totlen % 16 == 0)
   2194 				printf("\n");
   2195 		}
   2196 		m = m->m_next;
   2197 	}
   2198 	if (totlen % 16 != 0)
   2199 		printf("\n");
   2200 	printf("---\n");
   2201 }
   2202 
   2203 #ifdef INET6
   2204 struct secpolicy *
   2205 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p, int flags,
   2206     int *needipsecp, int *errorp)
   2207 {
   2208 	struct secpolicy *sp = NULL;
   2209 	int s;
   2210 	int error = 0;
   2211 	int needipsec = 0;
   2212 
   2213 	if (!ipsec_outdone(m)) {
   2214 		s = splsoftnet();
   2215 		if (in6p != NULL &&
   2216 		    ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
   2217 			splx(s);
   2218 			goto skippolicycheck;
   2219 		}
   2220 		sp = ipsec6_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,
   2221 		    in6p);
   2222 
   2223 		/*
   2224 		 * There are four return cases:
   2225 		 *	sp != NULL			apply IPsec policy
   2226 		 *	sp == NULL, error == 0		no IPsec handling needed
   2227 		 *	sp == NULL, error == -EINVAL  discard packet w/o error
   2228 		 *	sp == NULL, error != 0		discard packet, report error
   2229 		 */
   2230 
   2231 		splx(s);
   2232 		if (sp == NULL) {
   2233 			/*
   2234 			 * Caller must check the error return to see if it needs to discard
   2235 			 * the packet.
   2236 			 */
   2237 			needipsec = 0;
   2238 		} else {
   2239 			needipsec = 1;
   2240 		}
   2241 	}
   2242 skippolicycheck:;
   2243 
   2244 	*errorp = error;
   2245 	*needipsecp = needipsec;
   2246 	return sp;
   2247 }
   2248 
   2249 int
   2250 ipsec6_input(struct mbuf *m)
   2251 {
   2252 	struct secpolicy *sp;
   2253 	int s, error;
   2254 
   2255 	s = splsoftnet();
   2256 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
   2257 	if (sp != NULL) {
   2258 		/*
   2259 		 * Check security policy against packet
   2260 		 * attributes.
   2261 		 */
   2262 		error = ipsec_sp_reject(sp, m);
   2263 		KEY_SP_UNREF(&sp);
   2264 	} else {
   2265 		/* XXX error stat??? */
   2266 		error = EINVAL;
   2267 		IPSECLOG(LOG_DEBUG, "no SP, packet discarded\n");/*XXX*/
   2268 	}
   2269 	splx(s);
   2270 
   2271 	return error;
   2272 }
   2273 #endif /* INET6 */
   2274 
   2275 /*
   2276  * -----------------------------------------------------------------------------
   2277  */
   2278 
   2279 /* XXX this stuff doesn't belong here... */
   2280 
   2281 static struct xformsw *xforms = NULL;
   2282 
   2283 /*
   2284  * Register a transform; typically at system startup.
   2285  */
   2286 void
   2287 xform_register(struct xformsw *xsp)
   2288 {
   2289 	xsp->xf_next = xforms;
   2290 	xforms = xsp;
   2291 }
   2292 
   2293 /*
   2294  * Initialize transform support in an sav.
   2295  */
   2296 int
   2297 xform_init(struct secasvar *sav, int xftype)
   2298 {
   2299 	struct xformsw *xsp;
   2300 
   2301 	if (sav->tdb_xform != NULL)	/* previously initialized */
   2302 		return 0;
   2303 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
   2304 		if (xsp->xf_type == xftype)
   2305 			return (*xsp->xf_init)(sav, xsp);
   2306 
   2307 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
   2308 	return EINVAL;
   2309 }
   2310 
   2311 void
   2312 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport)
   2313 {
   2314 	struct m_tag *tag;
   2315 
   2316 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
   2317 		*sport = ((u_int16_t *)(tag + 1))[0];
   2318 		*dport = ((u_int16_t *)(tag + 1))[1];
   2319 	} else
   2320 		*sport = *dport = 0;
   2321 }
   2322 
   2323 /*
   2324  * XXXJRT This should be done as a protosw init call.
   2325  */
   2326 void
   2327 ipsec_attach(void)
   2328 {
   2329 
   2330 	ipsec_output_init();
   2331 
   2332 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
   2333 
   2334 	sysctl_net_inet_ipsec_setup(NULL);
   2335 #ifdef INET6
   2336 	sysctl_net_inet6_ipsec6_setup(NULL);
   2337 #endif
   2338 
   2339 	ah_attach();
   2340 	esp_attach();
   2341 	ipcomp_attach();
   2342 	ipe4_attach();
   2343 #ifdef TCP_SIGNATURE
   2344 	tcpsignature_attach();
   2345 #endif
   2346 }
   2347