Home | History | Annotate | Line # | Download | only in netipsec
ipsec_input.c revision 1.42
      1 /*	$NetBSD: ipsec_input.c,v 1.42 2017/05/11 05:55:14 ryo Exp $	*/
      2 /*	$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $	*/
      3 /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
      4 
      5 /*
      6  * The authors of this code are John Ioannidis (ji (at) tla.org),
      7  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8  * Niels Provos (provos (at) physnet.uni-hamburg.de).
      9  *
     10  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
     11  * in November 1995.
     12  *
     13  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
     14  * by Angelos D. Keromytis.
     15  *
     16  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
     17  * and Niels Provos.
     18  *
     19  * Additional features in 1999 by Angelos D. Keromytis.
     20  *
     21  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
     22  * Angelos D. Keromytis and Niels Provos.
     23  * Copyright (c) 2001, Angelos D. Keromytis.
     24  *
     25  * Permission to use, copy, and modify this software with or without fee
     26  * is hereby granted, provided that this entire notice is included in
     27  * all copies of any software which is or includes a copy or
     28  * modification of this software.
     29  * You may use this code under the GNU public license if you so wish. Please
     30  * contribute changes back to the authors under this freer than GPL license
     31  * so that we may further the use of strong encryption without limitations to
     32  * all.
     33  *
     34  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     35  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     36  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     37  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     38  * PURPOSE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.42 2017/05/11 05:55:14 ryo Exp $");
     43 
     44 /*
     45  * IPsec input processing.
     46  */
     47 
     48 #if defined(_KERNEL_OPT)
     49 #include "opt_inet.h"
     50 #endif
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/malloc.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/domain.h>
     57 #include <sys/protosw.h>
     58 #include <sys/socket.h>
     59 #include <sys/errno.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <net/if.h>
     63 #include <net/route.h>
     64 
     65 #include <netinet/in.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/ip.h>
     68 #include <netinet/ip_var.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/in_proto.h>
     71 
     72 #include <netinet/ip6.h>
     73 #ifdef INET6
     74 #include <netinet6/ip6_var.h>
     75 #include <netinet6/ip6_private.h>
     76 #include <netinet6/scope6_var.h>
     77 #endif
     78 #include <netinet/in_pcb.h>
     79 #ifdef INET6
     80 #include <netinet/icmp6.h>
     81 #endif
     82 
     83 #include <netipsec/ipsec.h>
     84 #include <netipsec/ipsec_private.h>
     85 #ifdef INET6
     86 #include <netipsec/ipsec6.h>
     87 #endif
     88 #include <netipsec/ah_var.h>
     89 #include <netipsec/esp.h>
     90 #include <netipsec/esp_var.h>
     91 #include <netipsec/ipcomp_var.h>
     92 
     93 #include <netipsec/key.h>
     94 #include <netipsec/keydb.h>
     95 
     96 #include <netipsec/xform.h>
     97 #include <netinet6/ip6protosw.h>
     98 
     99 #include <net/net_osdep.h>
    100 
    101 #define	IPSEC_ISTAT(p, x, y, z)						\
    102 do {									\
    103 	switch (p) {							\
    104 	case IPPROTO_ESP:						\
    105 		ESP_STATINC(x);						\
    106 		break;							\
    107 	case IPPROTO_AH:						\
    108 		AH_STATINC(y);						\
    109 		break;							\
    110 	default:							\
    111 		IPCOMP_STATINC(z);					\
    112 		break;							\
    113 	}								\
    114 } while (/*CONSTCOND*/0)
    115 
    116 /*
    117  * ipsec_common_input gets called when an IPsec-protected packet
    118  * is received by IPv4 or IPv6.  It's job is to find the right SA
    119  # and call the appropriate transform.  The transform callback
    120  * takes care of further processing (like ingress filtering).
    121  */
    122 static int
    123 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
    124 {
    125 	char buf[IPSEC_ADDRSTRLEN];
    126 	union sockaddr_union dst_address;
    127 	struct secasvar *sav;
    128 	u_int32_t spi;
    129 	u_int16_t sport;
    130 	u_int16_t dport;
    131 	int s, error;
    132 
    133 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
    134 		IPCOMP_STAT_INPUT);
    135 
    136 	KASSERT(m != NULL);
    137 
    138 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
    139 	    (sproto == IPPROTO_AH && !ah_enable) ||
    140 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    141 		m_freem(m);
    142 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
    143 		    IPCOMP_STAT_PDROPS);
    144 		return EOPNOTSUPP;
    145 	}
    146 
    147 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
    148 		m_freem(m);
    149 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    150 		    IPCOMP_STAT_HDROPS);
    151 		DPRINTF(("ipsec_common_input: packet too small\n"));
    152 		return EINVAL;
    153 	}
    154 
    155 	/* Retrieve the SPI from the relevant IPsec header */
    156 	if (sproto == IPPROTO_ESP)
    157 		m_copydata(m, skip, sizeof(u_int32_t), &spi);
    158 	else if (sproto == IPPROTO_AH)
    159 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
    160 	else if (sproto == IPPROTO_IPCOMP) {
    161 		u_int16_t cpi;
    162 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
    163 		spi = ntohl(htons(cpi));
    164 	} else {
    165 		panic("ipsec_common_input called with bad protocol number :"
    166 		      "%d\n", sproto);
    167 	}
    168 
    169 
    170 	/* find the source port for NAT-T */
    171 	nat_t_ports_get(m, &dport, &sport);
    172 
    173 	/*
    174 	 * Find the SA and (indirectly) call the appropriate
    175 	 * kernel crypto routine. The resulting mbuf chain is a valid
    176 	 * IP packet ready to go through input processing.
    177 	 */
    178 	memset(&dst_address, 0, sizeof (dst_address));
    179 	dst_address.sa.sa_family = af;
    180 	switch (af) {
    181 #ifdef INET
    182 	case AF_INET:
    183 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
    184 		m_copydata(m, offsetof(struct ip, ip_dst),
    185 		    sizeof(struct in_addr),
    186 		    &dst_address.sin.sin_addr);
    187 		break;
    188 #endif /* INET */
    189 #ifdef INET6
    190 	case AF_INET6:
    191 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
    192 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
    193 		    sizeof(struct in6_addr),
    194 		    &dst_address.sin6.sin6_addr);
    195 		if (sa6_recoverscope(&dst_address.sin6)) {
    196 			m_freem(m);
    197 			return EINVAL;
    198 		}
    199 		break;
    200 #endif /* INET6 */
    201 	default:
    202 		DPRINTF(("ipsec_common_input: unsupported protocol "
    203 			"family %u\n", af));
    204 		m_freem(m);
    205 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
    206 		    IPCOMP_STAT_NOPF);
    207 		return EPFNOSUPPORT;
    208 	}
    209 
    210 	s = splsoftnet();
    211 
    212 	/* NB: only pass dst since key_allocsa follows RFC2401 */
    213 	sav = KEY_ALLOCSA(&dst_address, sproto, spi, sport, dport);
    214 	if (sav == NULL) {
    215 		DPRINTF(("ipsec_common_input: no key association found for"
    216 			  " SA %s/%08lx/%u/%u\n",
    217 			  ipsec_address(&dst_address, buf, sizeof(buf)),
    218 			  (u_long) ntohl(spi), sproto, ntohs(dport)));
    219 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
    220 		    IPCOMP_STAT_NOTDB);
    221 		splx(s);
    222 		m_freem(m);
    223 		return ENOENT;
    224 	}
    225 
    226 	if (sav->tdb_xform == NULL) {
    227 		DPRINTF(("ipsec_common_input: attempted to use uninitialized"
    228 			 " SA %s/%08lx/%u\n",
    229 			 ipsec_address(&dst_address, buf, sizeof(buf)),
    230 			 (u_long) ntohl(spi), sproto));
    231 		IPSEC_ISTAT(sproto, ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
    232 		    IPCOMP_STAT_NOXFORM);
    233 		KEY_FREESAV(&sav);
    234 		splx(s);
    235 		m_freem(m);
    236 		return ENXIO;
    237 	}
    238 
    239 	/*
    240 	 * Call appropriate transform and return -- callback takes care of
    241 	 * everything else.
    242 	 */
    243 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
    244 	KEY_FREESAV(&sav);
    245 	splx(s);
    246 	return error;
    247 }
    248 
    249 #ifdef INET
    250 /*
    251  * Common input handler for IPv4 AH, ESP, and IPCOMP.
    252  */
    253 void
    254 ipsec4_common_input(struct mbuf *m, ...)
    255 {
    256 	va_list ap;
    257 	int off, nxt;
    258 
    259 	va_start(ap, m);
    260 	off = va_arg(ap, int);
    261 	nxt = va_arg(ap, int);
    262 	va_end(ap);
    263 
    264 	(void) ipsec_common_input(m, off, offsetof(struct ip, ip_p),
    265 				  AF_INET, nxt);
    266 }
    267 
    268 /*
    269  * IPsec input callback for INET protocols.
    270  * This routine is called as the transform callback.
    271  * Takes care of filtering and other sanity checks on
    272  * the processed packet.
    273  */
    274 int
    275 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
    276     int skip, int protoff, struct m_tag *mt)
    277 {
    278 	int prot, af __diagused, sproto;
    279 	struct ip *ip;
    280 	struct m_tag *mtag;
    281 	struct tdb_ident *tdbi;
    282 	struct secasindex *saidx;
    283 	int error;
    284 
    285 	IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
    286 
    287 	KASSERT(m != NULL);
    288 	KASSERT(sav != NULL);
    289 	KASSERT(sav->sah != NULL);
    290 	saidx = &sav->sah->saidx;
    291 	af = saidx->dst.sa.sa_family;
    292 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
    293 	sproto = saidx->proto;
    294 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    295 	    sproto == IPPROTO_IPCOMP,
    296 	    "unexpected security protocol %u", sproto);
    297 
    298 	/* Sanity check */
    299 	if (m == NULL) {
    300 		DPRINTF(("ipsec4_common_input_cb: null mbuf"));
    301 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    302 		    IPCOMP_STAT_BADKCR);
    303 		KEY_FREESAV(&sav);
    304 		return EINVAL;
    305 	}
    306 
    307 	/* Fix IPv4 header */
    308 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
    309 		char buf[IPSEC_ADDRSTRLEN];
    310 		DPRINTF(("ipsec4_common_input_cb: processing failed "
    311 		    "for SA %s/%08lx\n",
    312 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    313 		    (u_long) ntohl(sav->spi)));
    314 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    315 		    IPCOMP_STAT_HDROPS);
    316 		error = ENOBUFS;
    317 		goto bad;
    318 	}
    319 
    320 	ip = mtod(m, struct ip *);
    321 	ip->ip_len = htons(m->m_pkthdr.len);
    322 	prot = ip->ip_p;
    323 
    324 	/* IP-in-IP encapsulation */
    325 	if (prot == IPPROTO_IPIP) {
    326 		struct ip ipn;
    327 
    328 		/* ipn will now contain the inner IPv4 header */
    329 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
    330 
    331 #ifdef notyet
    332 		/* XXX PROXY address isn't recorded in SAH */
    333 		/*
    334 		 * Check that the inner source address is the same as
    335 		 * the proxy address, if available.
    336 		 */
    337 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    338 		    saidx->proxy.sin.sin_addr.s_addr !=
    339 		    INADDR_ANY &&
    340 		    ipn.ip_src.s_addr !=
    341 		    saidx->proxy.sin.sin_addr.s_addr) ||
    342 		    (saidx->proxy.sa.sa_family != AF_INET &&
    343 			saidx->proxy.sa.sa_family != 0)) {
    344 
    345 			char ipbuf[INET_ADDRSTRLEN];
    346 			DPRINTF(("ipsec4_common_input_cb: inner "
    347 			    "source address %s doesn't correspond to "
    348 			    "expected proxy source %s, SA %s/%08lx\n",
    349 			    IN_PRINT(ipbuf, ipn.ip_src),
    350 			    ipsp_address(saidx->proxy),
    351 			    ipsp_address(saidx->dst),
    352 			    (u_long) ntohl(sav->spi)));
    353 
    354 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    355 			    AH_STAT_PDROPS,
    356 			    IPCOMP_STAT_PDROPS);
    357 			error = EACCES;
    358 			goto bad;
    359 		}
    360 #endif /*XXX*/
    361 	}
    362 #if INET6
    363 	/* IPv6-in-IP encapsulation. */
    364 	if (prot == IPPROTO_IPV6) {
    365 		struct ip6_hdr ip6n;
    366 
    367 		/* ip6n will now contain the inner IPv6 header. */
    368 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
    369 
    370 #ifdef notyet
    371 		/*
    372 		 * Check that the inner source address is the same as
    373 		 * the proxy address, if available.
    374 		 */
    375 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    376 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    377 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    378 			&saidx->proxy.sin6.sin6_addr)) ||
    379 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    380 			saidx->proxy.sa.sa_family != 0)) {
    381 
    382 			char ip6buf[INET6_ADDRSTRLEN];
    383 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    384 			DPRINTF(("ipsec4_common_input_cb: inner "
    385 			    "source address %s doesn't correspond to "
    386 			    "expected proxy source %s, SA %s/%08lx\n",
    387 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    388 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    389 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    390 			    (u_long) ntohl(sav->spi)));
    391 
    392 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    393 			    AH_STAT_PDROPS,
    394 			    IPCOMP_STAT_PDROPS);
    395 			error = EACCES;
    396 			goto bad;
    397 		}
    398 #endif /*XXX*/
    399 	}
    400 #endif /* INET6 */
    401 
    402 	/*
    403 	 * Record what we've done to the packet (under what SA it was
    404 	 * processed). If we've been passed an mtag, it means the packet
    405 	 * was already processed by an ethernet/crypto combo card and
    406 	 * thus has a tag attached with all the right information, but
    407 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
    408 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
    409 	 */
    410 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
    411 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
    412 		    sizeof(struct tdb_ident), M_NOWAIT);
    413 		if (mtag == NULL) {
    414 			DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
    415 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
    416 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
    417 			error = ENOMEM;
    418 			goto bad;
    419 		}
    420 
    421 		tdbi = (struct tdb_ident *)(mtag + 1);
    422 		memcpy(&tdbi->dst, &saidx->dst, saidx->dst.sa.sa_len);
    423 		tdbi->proto = sproto;
    424 		tdbi->spi = sav->spi;
    425 
    426 		m_tag_prepend(m, mtag);
    427 	} else {
    428 		if (mt != NULL)
    429 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
    430 			/* XXX do we need to mark m_flags??? */
    431 	}
    432 
    433 	key_sa_recordxfer(sav, m);		/* record data transfer */
    434 
    435 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
    436 				ipsec4_in_reject(m, NULL)) {
    437 		error = EINVAL;
    438 		goto bad;
    439 	}
    440 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
    441 	return 0;
    442 bad:
    443 	m_freem(m);
    444 	return error;
    445 }
    446 #endif /* INET */
    447 
    448 #ifdef INET6
    449 /* IPv6 AH wrapper. */
    450 int
    451 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
    452 {
    453 	int l = 0;
    454 	int protoff, nxt;
    455 	struct ip6_ext ip6e;
    456 
    457 	if (*offp < sizeof(struct ip6_hdr)) {
    458 		DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp));
    459 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    460 			    IPCOMP_STAT_HDROPS);
    461 		m_freem(*mp);
    462 		return IPPROTO_DONE;
    463 	} else if (*offp == sizeof(struct ip6_hdr)) {
    464 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
    465 	} else {
    466 		/* Chase down the header chain... */
    467 		protoff = sizeof(struct ip6_hdr);
    468 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
    469 
    470 		do {
    471 			protoff += l;
    472 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
    473 
    474 			if (nxt == IPPROTO_AH)
    475 				l = (ip6e.ip6e_len + 2) << 2;
    476 			else
    477 				l = (ip6e.ip6e_len + 1) << 3;
    478 			KASSERT(l > 0);
    479 
    480 			nxt = ip6e.ip6e_nxt;
    481 		} while (protoff + l < *offp);
    482 
    483 		/* Malformed packet check */
    484 		if (protoff + l != *offp) {
    485 			DPRINTF(("ipsec6_common_input: bad packet header chain, "
    486 				"protoff %u, l %u, off %u\n", protoff, l, *offp));
    487 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
    488 				    AH_STAT_HDROPS,
    489 				    IPCOMP_STAT_HDROPS);
    490 			m_freem(*mp);
    491 			*mp = NULL;
    492 			return IPPROTO_DONE;
    493 		}
    494 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
    495 	}
    496 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
    497 	return IPPROTO_DONE;
    498 }
    499 
    500 extern	const struct ip6protosw inet6sw[];
    501 extern	u_char ip6_protox[];
    502 
    503 /*
    504  * IPsec input callback, called by the transform callback. Takes care of
    505  * filtering and other sanity checks on the processed packet.
    506  */
    507 int
    508 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
    509     struct m_tag *mt)
    510 {
    511 	int af __diagused, sproto;
    512 	struct ip6_hdr *ip6;
    513 	struct m_tag *mtag;
    514 	struct tdb_ident *tdbi;
    515 	struct secasindex *saidx;
    516 	int nxt;
    517 	u_int8_t prot, nxt8;
    518 	int error, nest;
    519 
    520 	KASSERT(m != NULL);
    521 	KASSERT(sav != NULL);
    522 	KASSERT(sav->sah != NULL);
    523 	saidx = &sav->sah->saidx;
    524 	af = saidx->dst.sa.sa_family;
    525 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
    526 	sproto = saidx->proto;
    527 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    528 	    sproto == IPPROTO_IPCOMP,
    529 	    "unexpected security protocol %u", sproto);
    530 
    531 	/* Sanity check */
    532 	if (m == NULL) {
    533 		DPRINTF(("ipsec6_common_input_cb: null mbuf"));
    534 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    535 		    IPCOMP_STAT_BADKCR);
    536 		error = EINVAL;
    537 		goto bad;
    538 	}
    539 
    540 	/* Fix IPv6 header */
    541 	if (m->m_len < sizeof(struct ip6_hdr) &&
    542 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    543 
    544 		char buf[IPSEC_ADDRSTRLEN];
    545 		DPRINTF(("ipsec6_common_input_cb: processing failed "
    546 		    "for SA %s/%08lx\n", ipsec_address(&sav->sah->saidx.dst,
    547 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
    548 
    549 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    550 		    IPCOMP_STAT_HDROPS);
    551 		error = EACCES;
    552 		goto bad;
    553 	}
    554 
    555 	ip6 = mtod(m, struct ip6_hdr *);
    556 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    557 
    558 	/* Save protocol */
    559 	m_copydata(m, protoff, 1, &prot);
    560 
    561 #ifdef INET
    562 	/* IP-in-IP encapsulation */
    563 	if (prot == IPPROTO_IPIP) {
    564 		struct ip ipn;
    565 
    566 		/* ipn will now contain the inner IPv4 header */
    567 		m_copydata(m, skip, sizeof(struct ip), &ipn);
    568 
    569 #ifdef notyet
    570 		/*
    571 		 * Check that the inner source address is the same as
    572 		 * the proxy address, if available.
    573 		 */
    574 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    575 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
    576 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
    577 		    (saidx->proxy.sa.sa_family != AF_INET &&
    578 			saidx->proxy.sa.sa_family != 0)) {
    579 
    580 			char ipbuf[INET_ADDRSTRLEN];
    581 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    582 			DPRINTF(("ipsec6_common_input_cb: inner "
    583 			    "source address %s doesn't correspond to "
    584 			    "expected proxy source %s, SA %s/%08lx\n",
    585 			    IN_PRINT(ipbuf, ipn.ip_src),
    586 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    587 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    588 			    (u_long) ntohl(sav->spi)));
    589 
    590 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    591 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    592 			error = EACCES;
    593 			goto bad;
    594 		}
    595 #endif /*XXX*/
    596 	}
    597 #endif /* INET */
    598 
    599 	/* IPv6-in-IP encapsulation */
    600 	if (prot == IPPROTO_IPV6) {
    601 		struct ip6_hdr ip6n;
    602 
    603 		/* ip6n will now contain the inner IPv6 header. */
    604 		m_copydata(m, skip, sizeof(struct ip6_hdr), &ip6n);
    605 
    606 #ifdef notyet
    607 		/*
    608 		 * Check that the inner source address is the same as
    609 		 * the proxy address, if available.
    610 		 */
    611 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    612 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    613 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    614 			&saidx->proxy.sin6.sin6_addr)) ||
    615 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    616 			saidx->proxy.sa.sa_family != 0)) {
    617 
    618 			char ip6buf[INET6_ADDRSTRLEN];
    619 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    620 			DPRINTF(("ipsec6_common_input_cb: inner "
    621 			    "source address %s doesn't correspond to "
    622 			    "expected proxy source %s, SA %s/%08lx\n",
    623 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    624 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    625 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    626 			    (u_long) ntohl(sav->spi)));
    627 
    628 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    629 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    630 			error = EACCES;
    631 			goto bad;
    632 		}
    633 #endif /*XXX*/
    634 	}
    635 
    636 	/*
    637 	 * Record what we've done to the packet (under what SA it was
    638 	 * processed). If we've been passed an mtag, it means the packet
    639 	 * was already processed by an ethernet/crypto combo card and
    640 	 * thus has a tag attached with all the right information, but
    641 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
    642 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
    643 	 */
    644 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
    645 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
    646 		    sizeof(struct tdb_ident), M_NOWAIT);
    647 		if (mtag == NULL) {
    648 			DPRINTF(("ipsec_common_input_cb: failed to "
    649 			    "get tag\n"));
    650 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
    651 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
    652 			error = ENOMEM;
    653 			goto bad;
    654 		}
    655 
    656 		tdbi = (struct tdb_ident *)(mtag + 1);
    657 		memcpy(&tdbi->dst, &saidx->dst, sizeof(union sockaddr_union));
    658 		tdbi->proto = sproto;
    659 		tdbi->spi = sav->spi;
    660 
    661 		m_tag_prepend(m, mtag);
    662 	} else {
    663 		if (mt != NULL)
    664 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
    665 			/* XXX do we need to mark m_flags??? */
    666 	}
    667 
    668 	key_sa_recordxfer(sav, m);
    669 
    670 	/* Retrieve new protocol */
    671 	m_copydata(m, protoff, sizeof(u_int8_t), &nxt8);
    672 
    673 	/*
    674 	 * See the end of ip6_input for this logic.
    675 	 * IPPROTO_IPV[46] case will be processed just like other ones
    676 	 */
    677 	nest = 0;
    678 	nxt = nxt8;
    679 	while (nxt != IPPROTO_DONE) {
    680 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    681 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    682 			error = EINVAL;
    683 			goto bad;
    684 		}
    685 
    686 		/*
    687 		 * Protection against faulty packet - there should be
    688 		 * more sanity checks in header chain processing.
    689 		 */
    690 		if (m->m_pkthdr.len < skip) {
    691 			IP6_STATINC(IP6_STAT_TOOSHORT);
    692 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
    693 				       ifs6_in_truncated);
    694 			error = EINVAL;
    695 			goto bad;
    696 		}
    697 		/*
    698 		 * Enforce IPsec policy checking if we are seeing last header.
    699 		 * note that we do not visit this with protocols with pcb layer
    700 		 * code - like udp/tcp/raw ip.
    701 		 */
    702 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    703 		    ipsec6_in_reject(m, NULL)) {
    704 			error = EINVAL;
    705 			goto bad;
    706 		}
    707 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
    708 	}
    709 	return 0;
    710 bad:
    711 	if (m)
    712 		m_freem(m);
    713 	return error;
    714 }
    715 #endif /* INET6 */
    716