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