Home | History | Annotate | Line # | Download | only in netipsec
ipsec_input.c revision 1.43
      1 /*	$NetBSD: ipsec_input.c,v 1.43 2017/05/19 04:34:09 ozaki-r 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.43 2017/05/19 04:34:09 ozaki-r 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 		IPSECLOG(LOG_DEBUG, "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 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
    203 		m_freem(m);
    204 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
    205 		    IPCOMP_STAT_NOPF);
    206 		return EPFNOSUPPORT;
    207 	}
    208 
    209 	s = splsoftnet();
    210 
    211 	/* NB: only pass dst since key_allocsa follows RFC2401 */
    212 	sav = KEY_ALLOCSA(&dst_address, sproto, spi, sport, dport);
    213 	if (sav == NULL) {
    214 		IPSECLOG(LOG_DEBUG,
    215 		    "no key association found for SA %s/%08lx/%u/%u\n",
    216 		    ipsec_address(&dst_address, buf, sizeof(buf)),
    217 		    (u_long) ntohl(spi), sproto, ntohs(dport));
    218 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
    219 		    IPCOMP_STAT_NOTDB);
    220 		splx(s);
    221 		m_freem(m);
    222 		return ENOENT;
    223 	}
    224 
    225 	if (sav->tdb_xform == NULL) {
    226 		IPSECLOG(LOG_DEBUG,
    227 		    "attempted to use uninitialized SA %s/%08lx/%u\n",
    228 		    ipsec_address(&dst_address, buf, sizeof(buf)),
    229 		    (u_long) ntohl(spi), sproto);
    230 		IPSEC_ISTAT(sproto, ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
    231 		    IPCOMP_STAT_NOXFORM);
    232 		KEY_FREESAV(&sav);
    233 		splx(s);
    234 		m_freem(m);
    235 		return ENXIO;
    236 	}
    237 
    238 	/*
    239 	 * Call appropriate transform and return -- callback takes care of
    240 	 * everything else.
    241 	 */
    242 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
    243 	KEY_FREESAV(&sav);
    244 	splx(s);
    245 	return error;
    246 }
    247 
    248 #ifdef INET
    249 /*
    250  * Common input handler for IPv4 AH, ESP, and IPCOMP.
    251  */
    252 void
    253 ipsec4_common_input(struct mbuf *m, ...)
    254 {
    255 	va_list ap;
    256 	int off, nxt;
    257 
    258 	va_start(ap, m);
    259 	off = va_arg(ap, int);
    260 	nxt = va_arg(ap, int);
    261 	va_end(ap);
    262 
    263 	(void) ipsec_common_input(m, off, offsetof(struct ip, ip_p),
    264 				  AF_INET, nxt);
    265 }
    266 
    267 /*
    268  * IPsec input callback for INET protocols.
    269  * This routine is called as the transform callback.
    270  * Takes care of filtering and other sanity checks on
    271  * the processed packet.
    272  */
    273 int
    274 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
    275     int skip, int protoff, struct m_tag *mt)
    276 {
    277 	int prot, af __diagused, sproto;
    278 	struct ip *ip;
    279 	struct m_tag *mtag;
    280 	struct tdb_ident *tdbi;
    281 	struct secasindex *saidx;
    282 	int error;
    283 
    284 	IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
    285 
    286 	KASSERT(m != NULL);
    287 	KASSERT(sav != NULL);
    288 	KASSERT(sav->sah != NULL);
    289 	saidx = &sav->sah->saidx;
    290 	af = saidx->dst.sa.sa_family;
    291 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
    292 	sproto = saidx->proto;
    293 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    294 	    sproto == IPPROTO_IPCOMP,
    295 	    "unexpected security protocol %u", sproto);
    296 
    297 	/* Sanity check */
    298 	if (m == NULL) {
    299 		IPSECLOG(LOG_DEBUG, "null mbuf");
    300 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    301 		    IPCOMP_STAT_BADKCR);
    302 		KEY_FREESAV(&sav);
    303 		return EINVAL;
    304 	}
    305 
    306 	/* Fix IPv4 header */
    307 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
    308 		char buf[IPSEC_ADDRSTRLEN];
    309 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
    310 		    ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
    311 		    (u_long) ntohl(sav->spi));
    312 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    313 		    IPCOMP_STAT_HDROPS);
    314 		error = ENOBUFS;
    315 		goto bad;
    316 	}
    317 
    318 	ip = mtod(m, struct ip *);
    319 	ip->ip_len = htons(m->m_pkthdr.len);
    320 	prot = ip->ip_p;
    321 
    322 	/* IP-in-IP encapsulation */
    323 	if (prot == IPPROTO_IPIP) {
    324 		struct ip ipn;
    325 
    326 		/* ipn will now contain the inner IPv4 header */
    327 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
    328 
    329 #ifdef notyet
    330 		/* XXX PROXY address isn't recorded in SAH */
    331 		/*
    332 		 * Check that the inner source address is the same as
    333 		 * the proxy address, if available.
    334 		 */
    335 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    336 		    saidx->proxy.sin.sin_addr.s_addr !=
    337 		    INADDR_ANY &&
    338 		    ipn.ip_src.s_addr !=
    339 		    saidx->proxy.sin.sin_addr.s_addr) ||
    340 		    (saidx->proxy.sa.sa_family != AF_INET &&
    341 			saidx->proxy.sa.sa_family != 0)) {
    342 
    343 			char ipbuf[INET_ADDRSTRLEN];
    344 			IPSECLOG(LOG_DEBUG,
    345 			    "inner source address %s doesn't correspond to "
    346 			    "expected proxy source %s, SA %s/%08lx\n",
    347 			    IN_PRINT(ipbuf, ipn.ip_src),
    348 			    ipsp_address(saidx->proxy),
    349 			    ipsp_address(saidx->dst),
    350 			    (u_long) ntohl(sav->spi));
    351 
    352 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    353 			    AH_STAT_PDROPS,
    354 			    IPCOMP_STAT_PDROPS);
    355 			error = EACCES;
    356 			goto bad;
    357 		}
    358 #endif /*XXX*/
    359 	}
    360 #if INET6
    361 	/* IPv6-in-IP encapsulation. */
    362 	if (prot == IPPROTO_IPV6) {
    363 		struct ip6_hdr ip6n;
    364 
    365 		/* ip6n will now contain the inner IPv6 header. */
    366 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
    367 
    368 #ifdef notyet
    369 		/*
    370 		 * Check that the inner source address is the same as
    371 		 * the proxy address, if available.
    372 		 */
    373 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    374 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    375 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    376 			&saidx->proxy.sin6.sin6_addr)) ||
    377 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    378 			saidx->proxy.sa.sa_family != 0)) {
    379 
    380 			char ip6buf[INET6_ADDRSTRLEN];
    381 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    382 			IPSECLOG(LOG_DEBUG,
    383 			    "inner source address %s doesn't correspond to "
    384 			    "expected proxy source %s, SA %s/%08lx\n",
    385 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    386 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    387 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    388 			    (u_long) ntohl(sav->spi));
    389 
    390 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    391 			    AH_STAT_PDROPS,
    392 			    IPCOMP_STAT_PDROPS);
    393 			error = EACCES;
    394 			goto bad;
    395 		}
    396 #endif /*XXX*/
    397 	}
    398 #endif /* INET6 */
    399 
    400 	/*
    401 	 * Record what we've done to the packet (under what SA it was
    402 	 * processed). If we've been passed an mtag, it means the packet
    403 	 * was already processed by an ethernet/crypto combo card and
    404 	 * thus has a tag attached with all the right information, but
    405 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
    406 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
    407 	 */
    408 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
    409 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
    410 		    sizeof(struct tdb_ident), M_NOWAIT);
    411 		if (mtag == NULL) {
    412 			IPSECLOG(LOG_DEBUG, "failed to get tag\n");
    413 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
    414 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
    415 			error = ENOMEM;
    416 			goto bad;
    417 		}
    418 
    419 		tdbi = (struct tdb_ident *)(mtag + 1);
    420 		memcpy(&tdbi->dst, &saidx->dst, saidx->dst.sa.sa_len);
    421 		tdbi->proto = sproto;
    422 		tdbi->spi = sav->spi;
    423 
    424 		m_tag_prepend(m, mtag);
    425 	} else {
    426 		if (mt != NULL)
    427 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
    428 			/* XXX do we need to mark m_flags??? */
    429 	}
    430 
    431 	key_sa_recordxfer(sav, m);		/* record data transfer */
    432 
    433 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
    434 				ipsec4_in_reject(m, NULL)) {
    435 		error = EINVAL;
    436 		goto bad;
    437 	}
    438 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
    439 	return 0;
    440 bad:
    441 	m_freem(m);
    442 	return error;
    443 }
    444 #endif /* INET */
    445 
    446 #ifdef INET6
    447 /* IPv6 AH wrapper. */
    448 int
    449 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
    450 {
    451 	int l = 0;
    452 	int protoff, nxt;
    453 	struct ip6_ext ip6e;
    454 
    455 	if (*offp < sizeof(struct ip6_hdr)) {
    456 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
    457 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    458 			    IPCOMP_STAT_HDROPS);
    459 		m_freem(*mp);
    460 		return IPPROTO_DONE;
    461 	} else if (*offp == sizeof(struct ip6_hdr)) {
    462 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
    463 	} else {
    464 		/* Chase down the header chain... */
    465 		protoff = sizeof(struct ip6_hdr);
    466 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
    467 
    468 		do {
    469 			protoff += l;
    470 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
    471 
    472 			if (nxt == IPPROTO_AH)
    473 				l = (ip6e.ip6e_len + 2) << 2;
    474 			else
    475 				l = (ip6e.ip6e_len + 1) << 3;
    476 			KASSERT(l > 0);
    477 
    478 			nxt = ip6e.ip6e_nxt;
    479 		} while (protoff + l < *offp);
    480 
    481 		/* Malformed packet check */
    482 		if (protoff + l != *offp) {
    483 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
    484 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
    485 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
    486 				    AH_STAT_HDROPS,
    487 				    IPCOMP_STAT_HDROPS);
    488 			m_freem(*mp);
    489 			*mp = NULL;
    490 			return IPPROTO_DONE;
    491 		}
    492 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
    493 	}
    494 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
    495 	return IPPROTO_DONE;
    496 }
    497 
    498 extern	const struct ip6protosw inet6sw[];
    499 extern	u_char ip6_protox[];
    500 
    501 /*
    502  * IPsec input callback, called by the transform callback. Takes care of
    503  * filtering and other sanity checks on the processed packet.
    504  */
    505 int
    506 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
    507     struct m_tag *mt)
    508 {
    509 	int af __diagused, sproto;
    510 	struct ip6_hdr *ip6;
    511 	struct m_tag *mtag;
    512 	struct tdb_ident *tdbi;
    513 	struct secasindex *saidx;
    514 	int nxt;
    515 	u_int8_t prot, nxt8;
    516 	int error, nest;
    517 
    518 	KASSERT(m != NULL);
    519 	KASSERT(sav != NULL);
    520 	KASSERT(sav->sah != NULL);
    521 	saidx = &sav->sah->saidx;
    522 	af = saidx->dst.sa.sa_family;
    523 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
    524 	sproto = saidx->proto;
    525 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    526 	    sproto == IPPROTO_IPCOMP,
    527 	    "unexpected security protocol %u", sproto);
    528 
    529 	/* Sanity check */
    530 	if (m == NULL) {
    531 		IPSECLOG(LOG_DEBUG, "null mbuf");
    532 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    533 		    IPCOMP_STAT_BADKCR);
    534 		error = EINVAL;
    535 		goto bad;
    536 	}
    537 
    538 	/* Fix IPv6 header */
    539 	if (m->m_len < sizeof(struct ip6_hdr) &&
    540 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    541 
    542 		char buf[IPSEC_ADDRSTRLEN];
    543 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
    544 		    ipsec_address(&sav->sah->saidx.dst,
    545 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
    546 
    547 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    548 		    IPCOMP_STAT_HDROPS);
    549 		error = EACCES;
    550 		goto bad;
    551 	}
    552 
    553 	ip6 = mtod(m, struct ip6_hdr *);
    554 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    555 
    556 	/* Save protocol */
    557 	m_copydata(m, protoff, 1, &prot);
    558 
    559 #ifdef INET
    560 	/* IP-in-IP encapsulation */
    561 	if (prot == IPPROTO_IPIP) {
    562 		struct ip ipn;
    563 
    564 		/* ipn will now contain the inner IPv4 header */
    565 		m_copydata(m, skip, sizeof(struct ip), &ipn);
    566 
    567 #ifdef notyet
    568 		/*
    569 		 * Check that the inner source address is the same as
    570 		 * the proxy address, if available.
    571 		 */
    572 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    573 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
    574 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
    575 		    (saidx->proxy.sa.sa_family != AF_INET &&
    576 			saidx->proxy.sa.sa_family != 0)) {
    577 
    578 			char ipbuf[INET_ADDRSTRLEN];
    579 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    580 			IPSECLOG(LOG_DEBUG,
    581 			    "inner source address %s doesn't correspond to "
    582 			    "expected proxy source %s, SA %s/%08lx\n",
    583 			    IN_PRINT(ipbuf, ipn.ip_src),
    584 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    585 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    586 			    (u_long) ntohl(sav->spi));
    587 
    588 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    589 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    590 			error = EACCES;
    591 			goto bad;
    592 		}
    593 #endif /*XXX*/
    594 	}
    595 #endif /* INET */
    596 
    597 	/* IPv6-in-IP encapsulation */
    598 	if (prot == IPPROTO_IPV6) {
    599 		struct ip6_hdr ip6n;
    600 
    601 		/* ip6n will now contain the inner IPv6 header. */
    602 		m_copydata(m, skip, sizeof(struct ip6_hdr), &ip6n);
    603 
    604 #ifdef notyet
    605 		/*
    606 		 * Check that the inner source address is the same as
    607 		 * the proxy address, if available.
    608 		 */
    609 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    610 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    611 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    612 			&saidx->proxy.sin6.sin6_addr)) ||
    613 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    614 			saidx->proxy.sa.sa_family != 0)) {
    615 
    616 			char ip6buf[INET6_ADDRSTRLEN];
    617 			char pbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
    618 			IPSECLOG(LOG_DEBUG,
    619 			    "inner source address %s doesn't correspond to "
    620 			    "expected proxy source %s, SA %s/%08lx\n",
    621 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    622 			    ipsec_address(&saidx->proxy, pbuf, sizeof(pbuf)),
    623 			    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)),
    624 			    (u_long) ntohl(sav->spi));
    625 
    626 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    627 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    628 			error = EACCES;
    629 			goto bad;
    630 		}
    631 #endif /*XXX*/
    632 	}
    633 
    634 	/*
    635 	 * Record what we've done to the packet (under what SA it was
    636 	 * processed). If we've been passed an mtag, it means the packet
    637 	 * was already processed by an ethernet/crypto combo card and
    638 	 * thus has a tag attached with all the right information, but
    639 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
    640 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
    641 	 */
    642 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
    643 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
    644 		    sizeof(struct tdb_ident), M_NOWAIT);
    645 		if (mtag == NULL) {
    646 			IPSECLOG(LOG_DEBUG, "failed to get tag\n");
    647 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
    648 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
    649 			error = ENOMEM;
    650 			goto bad;
    651 		}
    652 
    653 		tdbi = (struct tdb_ident *)(mtag + 1);
    654 		memcpy(&tdbi->dst, &saidx->dst, sizeof(union sockaddr_union));
    655 		tdbi->proto = sproto;
    656 		tdbi->spi = sav->spi;
    657 
    658 		m_tag_prepend(m, mtag);
    659 	} else {
    660 		if (mt != NULL)
    661 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
    662 			/* XXX do we need to mark m_flags??? */
    663 	}
    664 
    665 	key_sa_recordxfer(sav, m);
    666 
    667 	/* Retrieve new protocol */
    668 	m_copydata(m, protoff, sizeof(u_int8_t), &nxt8);
    669 
    670 	/*
    671 	 * See the end of ip6_input for this logic.
    672 	 * IPPROTO_IPV[46] case will be processed just like other ones
    673 	 */
    674 	nest = 0;
    675 	nxt = nxt8;
    676 	while (nxt != IPPROTO_DONE) {
    677 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    678 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    679 			error = EINVAL;
    680 			goto bad;
    681 		}
    682 
    683 		/*
    684 		 * Protection against faulty packet - there should be
    685 		 * more sanity checks in header chain processing.
    686 		 */
    687 		if (m->m_pkthdr.len < skip) {
    688 			IP6_STATINC(IP6_STAT_TOOSHORT);
    689 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
    690 				       ifs6_in_truncated);
    691 			error = EINVAL;
    692 			goto bad;
    693 		}
    694 		/*
    695 		 * Enforce IPsec policy checking if we are seeing last header.
    696 		 * note that we do not visit this with protocols with pcb layer
    697 		 * code - like udp/tcp/raw ip.
    698 		 */
    699 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    700 		    ipsec6_in_reject(m, NULL)) {
    701 			error = EINVAL;
    702 			goto bad;
    703 		}
    704 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
    705 	}
    706 	return 0;
    707 bad:
    708 	if (m)
    709 		m_freem(m);
    710 	return error;
    711 }
    712 #endif /* INET6 */
    713