Home | History | Annotate | Line # | Download | only in netipsec
ipsec_input.c revision 1.57
      1 /*	$NetBSD: ipsec_input.c,v 1.57 2018/02/21 16:08:55 maxv 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.57 2018/02/21 16:08:55 maxv 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 #include <netinet/udp.h>
     72 #include <netinet/tcp.h>
     73 
     74 #include <netinet/ip6.h>
     75 #ifdef INET6
     76 #include <netinet6/ip6_var.h>
     77 #include <netinet6/ip6_private.h>
     78 #include <netinet6/scope6_var.h>
     79 #endif
     80 #include <netinet/in_pcb.h>
     81 #ifdef INET6
     82 #include <netinet/icmp6.h>
     83 #endif
     84 
     85 #include <netipsec/ipsec.h>
     86 #include <netipsec/ipsec_private.h>
     87 #ifdef INET6
     88 #include <netipsec/ipsec6.h>
     89 #endif
     90 #include <netipsec/ah_var.h>
     91 #include <netipsec/esp.h>
     92 #include <netipsec/esp_var.h>
     93 #include <netipsec/ipcomp_var.h>
     94 
     95 #include <netipsec/key.h>
     96 #include <netipsec/keydb.h>
     97 
     98 #include <netipsec/xform.h>
     99 #include <netinet6/ip6protosw.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  * fixup TCP/UDP checksum
    118  *
    119  * XXX: if we have NAT-OA payload from IKE server,
    120  *      we must do the differential update of checksum.
    121  *
    122  * XXX: NAT-OAi/NAT-OAr drived from IKE initiator/responder.
    123  *      how to know the IKE side from kernel?
    124  */
    125 static struct mbuf *
    126 ipsec4_fixup_checksum(struct mbuf *m)
    127 {
    128 	struct ip *ip;
    129 	struct tcphdr *th;
    130 	struct udphdr *uh;
    131 	int poff, off;
    132 	int plen;
    133 
    134 	if (m->m_len < sizeof(*ip)) {
    135 		m = m_pullup(m, sizeof(*ip));
    136 		if (m == NULL)
    137 			return NULL;
    138 	}
    139 	ip = mtod(m, struct ip *);
    140 	poff = ip->ip_hl << 2;
    141 	plen = ntohs(ip->ip_len) - poff;
    142 
    143 	switch (ip->ip_p) {
    144 	case IPPROTO_TCP:
    145 		IP6_EXTHDR_GET(th, struct tcphdr *, m, poff, sizeof(*th));
    146 		if (th == NULL)
    147 			return NULL;
    148 		off = th->th_off << 2;
    149 		if (off < sizeof(*th) || off > plen) {
    150 			m_freem(m);
    151 			return NULL;
    152 		}
    153 		th->th_sum = 0;
    154 		th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen);
    155 		break;
    156 	case IPPROTO_UDP:
    157 		IP6_EXTHDR_GET(uh, struct udphdr *, m, poff, sizeof(*uh));
    158 		if (uh == NULL)
    159 			return NULL;
    160 		off = sizeof(*uh);
    161 		if (off > plen) {
    162 			m_freem(m);
    163 			return NULL;
    164 		}
    165 		uh->uh_sum = 0;
    166 		uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen);
    167 		break;
    168 	default:
    169 		/* no checksum */
    170 		return m;
    171 	}
    172 
    173 	return m;
    174 }
    175 
    176 /*
    177  * ipsec_common_input gets called when an IPsec-protected packet
    178  * is received by IPv4 or IPv6.  It's job is to find the right SA
    179  # and call the appropriate transform.  The transform callback
    180  * takes care of further processing (like ingress filtering).
    181  */
    182 static int
    183 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
    184 {
    185 	char buf[IPSEC_ADDRSTRLEN];
    186 	union sockaddr_union dst_address;
    187 	struct secasvar *sav;
    188 	u_int32_t spi;
    189 	u_int16_t sport;
    190 	u_int16_t dport;
    191 	int s, error;
    192 
    193 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
    194 		IPCOMP_STAT_INPUT);
    195 
    196 	KASSERT(m != NULL);
    197 
    198 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
    199 	    (sproto == IPPROTO_AH && !ah_enable) ||
    200 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    201 		m_freem(m);
    202 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
    203 		    IPCOMP_STAT_PDROPS);
    204 		return EOPNOTSUPP;
    205 	}
    206 
    207 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
    208 		m_freem(m);
    209 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    210 		    IPCOMP_STAT_HDROPS);
    211 		IPSECLOG(LOG_DEBUG, "packet too small\n");
    212 		return EINVAL;
    213 	}
    214 
    215 	/* Retrieve the SPI from the relevant IPsec header */
    216 	if (sproto == IPPROTO_ESP)
    217 		m_copydata(m, skip, sizeof(u_int32_t), &spi);
    218 	else if (sproto == IPPROTO_AH)
    219 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
    220 	else if (sproto == IPPROTO_IPCOMP) {
    221 		u_int16_t cpi;
    222 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
    223 		spi = ntohl(htons(cpi));
    224 	} else {
    225 		panic("ipsec_common_input called with bad protocol number :"
    226 		      "%d\n", sproto);
    227 	}
    228 
    229 
    230 	/* find the source port for NAT-T */
    231 	nat_t_ports_get(m, &dport, &sport);
    232 
    233 	/*
    234 	 * Find the SA and (indirectly) call the appropriate
    235 	 * kernel crypto routine. The resulting mbuf chain is a valid
    236 	 * IP packet ready to go through input processing.
    237 	 */
    238 	memset(&dst_address, 0, sizeof (dst_address));
    239 	dst_address.sa.sa_family = af;
    240 	switch (af) {
    241 #ifdef INET
    242 	case AF_INET:
    243 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
    244 		m_copydata(m, offsetof(struct ip, ip_dst),
    245 		    sizeof(struct in_addr),
    246 		    &dst_address.sin.sin_addr);
    247 		break;
    248 #endif /* INET */
    249 #ifdef INET6
    250 	case AF_INET6:
    251 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
    252 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
    253 		    sizeof(struct in6_addr),
    254 		    &dst_address.sin6.sin6_addr);
    255 		if (sa6_recoverscope(&dst_address.sin6)) {
    256 			m_freem(m);
    257 			return EINVAL;
    258 		}
    259 		break;
    260 #endif /* INET6 */
    261 	default:
    262 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
    263 		m_freem(m);
    264 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
    265 		    IPCOMP_STAT_NOPF);
    266 		return EPFNOSUPPORT;
    267 	}
    268 
    269 	s = splsoftnet();
    270 
    271 	/* NB: only pass dst since key_lookup_sa follows RFC2401 */
    272 	sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
    273 	if (sav == NULL) {
    274 		IPSECLOG(LOG_DEBUG,
    275 		    "no key association found for SA %s/%08lx/%u/%u\n",
    276 		    ipsec_address(&dst_address, buf, sizeof(buf)),
    277 		    (u_long) ntohl(spi), sproto, ntohs(dport));
    278 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
    279 		    IPCOMP_STAT_NOTDB);
    280 		splx(s);
    281 		m_freem(m);
    282 		return ENOENT;
    283 	}
    284 
    285 	KASSERT(sav->tdb_xform != NULL);
    286 
    287 	/*
    288 	 * Call appropriate transform and return -- callback takes care of
    289 	 * everything else.
    290 	 */
    291 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
    292 	KEY_SA_UNREF(&sav);
    293 	splx(s);
    294 	return error;
    295 }
    296 
    297 #ifdef INET
    298 /*
    299  * Common input handler for IPv4 AH, ESP, and IPCOMP.
    300  */
    301 void
    302 ipsec4_common_input(struct mbuf *m, ...)
    303 {
    304 	va_list ap;
    305 	int off, nxt;
    306 
    307 	va_start(ap, m);
    308 	off = va_arg(ap, int);
    309 	nxt = va_arg(ap, int);
    310 	va_end(ap);
    311 
    312 	(void) ipsec_common_input(m, off, offsetof(struct ip, ip_p),
    313 				  AF_INET, nxt);
    314 }
    315 
    316 /*
    317  * IPsec input callback for INET protocols.
    318  * This routine is called as the transform callback.
    319  * Takes care of filtering and other sanity checks on
    320  * the processed packet.
    321  */
    322 int
    323 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
    324     int skip, int protoff)
    325 {
    326 	int prot, af __diagused, sproto;
    327 	struct ip *ip;
    328 	struct secasindex *saidx;
    329 	int error;
    330 
    331 	IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
    332 
    333 	KASSERT(m != NULL);
    334 	KASSERT(sav != NULL);
    335 	saidx = &sav->sah->saidx;
    336 	af = saidx->dst.sa.sa_family;
    337 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
    338 	sproto = saidx->proto;
    339 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    340 	    sproto == IPPROTO_IPCOMP,
    341 	    "unexpected security protocol %u", sproto);
    342 
    343 	/* Sanity check */
    344 	if (m == NULL) {
    345 		IPSECLOG(LOG_DEBUG, "null mbuf");
    346 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    347 		    IPCOMP_STAT_BADKCR);
    348 		return EINVAL;
    349 	}
    350 
    351 	/* Fix IPv4 header */
    352 	if (skip != 0) {
    353 		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
    354 			char buf[IPSEC_ADDRSTRLEN];
    355 cantpull:
    356 			IPSECLOG(LOG_DEBUG,
    357 			    "processing failed for SA %s/%08lx\n",
    358 			    ipsec_address(&sav->sah->saidx.dst, buf,
    359 			    sizeof(buf)), (u_long) ntohl(sav->spi));
    360 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    361 			    IPCOMP_STAT_HDROPS);
    362 			error = ENOBUFS;
    363 			goto bad;
    364 		}
    365 
    366 		ip = mtod(m, struct ip *);
    367 		ip->ip_len = htons(m->m_pkthdr.len);
    368 		ip->ip_sum = 0;
    369 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    370 	} else {
    371 		/* XXX this branch is never taken */
    372 		ip = mtod(m, struct ip *);
    373 	}
    374 
    375 	/*
    376 	 * Update TCP/UDP checksum
    377 	 * XXX: should only do it in NAT-T case
    378 	 * XXX: should do it incrementally, see FreeBSD code.
    379 	 */
    380 	m = ipsec4_fixup_checksum(m);
    381 	if (m == NULL)
    382 		goto cantpull;
    383 	ip = mtod(m, struct ip *);
    384 
    385 	prot = ip->ip_p;
    386 
    387 #ifdef notyet
    388 	/* IP-in-IP encapsulation */
    389 	if (prot == IPPROTO_IPIP) {
    390 		struct ip ipn;
    391 
    392 		/* ipn will now contain the inner IPv4 header */
    393 		/* XXX: check m_pkthdr.len */
    394 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
    395 
    396 		/* XXX PROXY address isn't recorded in SAH */
    397 		/*
    398 		 * Check that the inner source address is the same as
    399 		 * the proxy address, if available.
    400 		 */
    401 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    402 		    saidx->proxy.sin.sin_addr.s_addr !=
    403 		    INADDR_ANY &&
    404 		    ipn.ip_src.s_addr !=
    405 		    saidx->proxy.sin.sin_addr.s_addr) ||
    406 		    (saidx->proxy.sa.sa_family != AF_INET &&
    407 			saidx->proxy.sa.sa_family != 0)) {
    408 
    409 			char ipbuf[INET_ADDRSTRLEN];
    410 			IPSECLOG(LOG_DEBUG,
    411 			    "inner source address %s doesn't correspond to "
    412 			    "expected proxy source %s, SA %s/%08lx\n",
    413 			    IN_PRINT(ipbuf, ipn.ip_src),
    414 			    ipsp_address(saidx->proxy),
    415 			    ipsp_address(saidx->dst),
    416 			    (u_long) ntohl(sav->spi));
    417 
    418 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    419 			    AH_STAT_PDROPS,
    420 			    IPCOMP_STAT_PDROPS);
    421 			error = EACCES;
    422 			goto bad;
    423 		}
    424 	}
    425 #if INET6
    426 	/* IPv6-in-IP encapsulation. */
    427 	if (prot == IPPROTO_IPV6) {
    428 		struct ip6_hdr ip6n;
    429 
    430 		/* ip6n will now contain the inner IPv6 header. */
    431 		/* XXX: check m_pkthdr.len */
    432 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
    433 
    434 		/*
    435 		 * Check that the inner source address is the same as
    436 		 * the proxy address, if available.
    437 		 */
    438 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    439 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    440 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    441 			&saidx->proxy.sin6.sin6_addr)) ||
    442 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    443 			saidx->proxy.sa.sa_family != 0)) {
    444 
    445 			char ip6buf[INET6_ADDRSTRLEN];
    446 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    447 			IPSECLOG(LOG_DEBUG,
    448 			    "inner source address %s doesn't correspond to "
    449 			    "expected proxy source %s, SA %s/%08lx\n",
    450 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    451 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    452 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    453 			    (u_long) ntohl(sav->spi));
    454 
    455 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    456 			    AH_STAT_PDROPS,
    457 			    IPCOMP_STAT_PDROPS);
    458 			error = EACCES;
    459 			goto bad;
    460 		}
    461 	}
    462 #endif /* INET6 */
    463 #endif /* notyet */
    464 
    465 	key_sa_recordxfer(sav, m);		/* record data transfer */
    466 
    467 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
    468 				ipsec4_in_reject(m, NULL)) {
    469 		error = EINVAL;
    470 		goto bad;
    471 	}
    472 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
    473 	return 0;
    474 bad:
    475 	m_freem(m);
    476 	return error;
    477 }
    478 #endif /* INET */
    479 
    480 #ifdef INET6
    481 /* IPv6 AH wrapper. */
    482 int
    483 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
    484 {
    485 	int l = 0;
    486 	int protoff, nxt;
    487 	struct ip6_ext ip6e;
    488 
    489 	if (*offp < sizeof(struct ip6_hdr)) {
    490 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
    491 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    492 			    IPCOMP_STAT_HDROPS);
    493 		m_freem(*mp);
    494 		return IPPROTO_DONE;
    495 	} else if (*offp == sizeof(struct ip6_hdr)) {
    496 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
    497 	} else {
    498 		/* Chase down the header chain... */
    499 		protoff = sizeof(struct ip6_hdr);
    500 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
    501 
    502 		do {
    503 			protoff += l;
    504 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
    505 
    506 			if (nxt == IPPROTO_AH)
    507 				l = (ip6e.ip6e_len + 2) << 2;
    508 			else if (nxt == IPPROTO_FRAGMENT)
    509 				l = sizeof(struct ip6_frag);
    510 			else
    511 				l = (ip6e.ip6e_len + 1) << 3;
    512 			KASSERT(l > 0);
    513 
    514 			nxt = ip6e.ip6e_nxt;
    515 		} while (protoff + l < *offp);
    516 
    517 		/* Malformed packet check */
    518 		if (protoff + l != *offp) {
    519 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
    520 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
    521 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
    522 				    AH_STAT_HDROPS,
    523 				    IPCOMP_STAT_HDROPS);
    524 			m_freem(*mp);
    525 			*mp = NULL;
    526 			return IPPROTO_DONE;
    527 		}
    528 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
    529 	}
    530 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
    531 	return IPPROTO_DONE;
    532 }
    533 
    534 extern	const struct ip6protosw inet6sw[];
    535 extern	u_char ip6_protox[];
    536 
    537 /*
    538  * IPsec input callback, called by the transform callback. Takes care of
    539  * filtering and other sanity checks on the processed packet.
    540  */
    541 int
    542 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
    543     int protoff)
    544 {
    545 	int af __diagused, sproto;
    546 	struct ip6_hdr *ip6;
    547 	struct secasindex *saidx;
    548 	int nxt;
    549 	u_int8_t prot, nxt8;
    550 	int error, nest;
    551 
    552 	KASSERT(m != NULL);
    553 	KASSERT(sav != NULL);
    554 	saidx = &sav->sah->saidx;
    555 	af = saidx->dst.sa.sa_family;
    556 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
    557 	sproto = saidx->proto;
    558 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    559 	    sproto == IPPROTO_IPCOMP,
    560 	    "unexpected security protocol %u", sproto);
    561 
    562 	/* Sanity check */
    563 	if (m == NULL) {
    564 		IPSECLOG(LOG_DEBUG, "null mbuf");
    565 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    566 		    IPCOMP_STAT_BADKCR);
    567 		error = EINVAL;
    568 		goto bad;
    569 	}
    570 
    571 	/* Fix IPv6 header */
    572 	if (m->m_len < sizeof(struct ip6_hdr) &&
    573 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    574 
    575 		char buf[IPSEC_ADDRSTRLEN];
    576 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
    577 		    ipsec_address(&sav->sah->saidx.dst,
    578 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
    579 
    580 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    581 		    IPCOMP_STAT_HDROPS);
    582 		error = EACCES;
    583 		goto bad;
    584 	}
    585 
    586 	ip6 = mtod(m, struct ip6_hdr *);
    587 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    588 
    589 	/* Save protocol */
    590 	m_copydata(m, protoff, 1, &prot);
    591 
    592 #ifdef INET
    593 	/* IP-in-IP encapsulation */
    594 	if (prot == IPPROTO_IPIP) {
    595 		struct ip ipn;
    596 
    597 		/* ipn will now contain the inner IPv4 header */
    598 		m_copydata(m, skip, sizeof(struct ip), &ipn);
    599 
    600 #ifdef notyet
    601 		/*
    602 		 * Check that the inner source address is the same as
    603 		 * the proxy address, if available.
    604 		 */
    605 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    606 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
    607 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
    608 		    (saidx->proxy.sa.sa_family != AF_INET &&
    609 			saidx->proxy.sa.sa_family != 0)) {
    610 
    611 			char ipbuf[INET_ADDRSTRLEN];
    612 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    613 			IPSECLOG(LOG_DEBUG,
    614 			    "inner source address %s doesn't correspond to "
    615 			    "expected proxy source %s, SA %s/%08lx\n",
    616 			    IN_PRINT(ipbuf, ipn.ip_src),
    617 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    618 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    619 			    (u_long) ntohl(sav->spi));
    620 
    621 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    622 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    623 			error = EACCES;
    624 			goto bad;
    625 		}
    626 #endif /*XXX*/
    627 	}
    628 #endif /* INET */
    629 
    630 	/* IPv6-in-IP encapsulation */
    631 	if (prot == IPPROTO_IPV6) {
    632 		struct ip6_hdr ip6n;
    633 
    634 		/* ip6n will now contain the inner IPv6 header. */
    635 		m_copydata(m, skip, sizeof(struct ip6_hdr), &ip6n);
    636 
    637 #ifdef notyet
    638 		/*
    639 		 * Check that the inner source address is the same as
    640 		 * the proxy address, if available.
    641 		 */
    642 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    643 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    644 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    645 			&saidx->proxy.sin6.sin6_addr)) ||
    646 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    647 			saidx->proxy.sa.sa_family != 0)) {
    648 
    649 			char ip6buf[INET6_ADDRSTRLEN];
    650 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    651 			IPSECLOG(LOG_DEBUG,
    652 			    "inner source address %s doesn't correspond to "
    653 			    "expected proxy source %s, SA %s/%08lx\n",
    654 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    655 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    656 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    657 			    (u_long) ntohl(sav->spi));
    658 
    659 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    660 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    661 			error = EACCES;
    662 			goto bad;
    663 		}
    664 #endif /*XXX*/
    665 	}
    666 
    667 	key_sa_recordxfer(sav, m);
    668 
    669 	/* Retrieve new protocol */
    670 	m_copydata(m, protoff, sizeof(u_int8_t), &nxt8);
    671 
    672 	/*
    673 	 * See the end of ip6_input for this logic.
    674 	 * IPPROTO_IPV[46] case will be processed just like other ones
    675 	 */
    676 	nest = 0;
    677 	nxt = nxt8;
    678 	while (nxt != IPPROTO_DONE) {
    679 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    680 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    681 			error = EINVAL;
    682 			goto bad;
    683 		}
    684 
    685 		/*
    686 		 * Protection against faulty packet - there should be
    687 		 * more sanity checks in header chain processing.
    688 		 */
    689 		if (m->m_pkthdr.len < skip) {
    690 			IP6_STATINC(IP6_STAT_TOOSHORT);
    691 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
    692 				       ifs6_in_truncated);
    693 			error = EINVAL;
    694 			goto bad;
    695 		}
    696 		/*
    697 		 * Enforce IPsec policy checking if we are seeing last header.
    698 		 * note that we do not visit this with protocols with pcb layer
    699 		 * code - like udp/tcp/raw ip.
    700 		 */
    701 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    702 		    ipsec6_in_reject(m, NULL)) {
    703 			error = EINVAL;
    704 			goto bad;
    705 		}
    706 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
    707 	}
    708 	return 0;
    709 bad:
    710 	if (m)
    711 		m_freem(m);
    712 	return error;
    713 }
    714 #endif /* INET6 */
    715