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