Home | History | Annotate | Line # | Download | only in netipsec
ipsec_input.c revision 1.37
      1 /*	$NetBSD: ipsec_input.c,v 1.37 2017/01/16 07:33:36 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.37 2017/01/16 07:33:36 ryo Exp $");
     43 
     44 /*
     45  * IPsec input processing.
     46  */
     47 
     48 #include "opt_inet.h"
     49 #ifdef __FreeBSD__
     50 #include "opt_inet6.h"
     51 #endif
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/domain.h>
     58 #include <sys/protosw.h>
     59 #include <sys/socket.h>
     60 #include <sys/errno.h>
     61 #include <sys/syslog.h>
     62 
     63 #include <net/if.h>
     64 #include <net/route.h>
     65 
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/ip_var.h>
     70 #include <netinet/in_var.h>
     71 #include <netinet/in_proto.h>
     72 
     73 #include <netinet/ip6.h>
     74 #ifdef INET6
     75 #include <netinet6/ip6_var.h>
     76 #include <netinet6/ip6_private.h>
     77 #include <netinet6/scope6_var.h>
     78 #endif
     79 #include <netinet/in_pcb.h>
     80 #ifdef INET6
     81 #include <netinet/icmp6.h>
     82 #endif
     83 
     84 #include <netipsec/ipsec.h>
     85 #include <netipsec/ipsec_private.h>
     86 #ifdef INET6
     87 #include <netipsec/ipsec6.h>
     88 #endif
     89 #include <netipsec/ah_var.h>
     90 #include <netipsec/esp.h>
     91 #include <netipsec/esp_var.h>
     92 #include <netipsec/ipcomp_var.h>
     93 
     94 #include <netipsec/key.h>
     95 #include <netipsec/keydb.h>
     96 
     97 #include <netipsec/xform.h>
     98 #include <netinet6/ip6protosw.h>
     99 
    100 #include <netipsec/ipsec_osdep.h>
    101 
    102 #include <net/net_osdep.h>
    103 
    104 #define	IPSEC_ISTAT(p, x, y, z)						\
    105 do {									\
    106 	switch (p) {							\
    107 	case IPPROTO_ESP:						\
    108 		ESP_STATINC(x);						\
    109 		break;							\
    110 	case IPPROTO_AH:						\
    111 		AH_STATINC(y);						\
    112 		break;							\
    113 	default:							\
    114 		IPCOMP_STATINC(z);					\
    115 		break;							\
    116 	}								\
    117 } while (/*CONSTCOND*/0)
    118 
    119 /*
    120  * ipsec_common_input gets called when an IPsec-protected packet
    121  * is received by IPv4 or IPv6.  It's job is to find the right SA
    122  # and call the appropriate transform.  The transform callback
    123  * takes care of further processing (like ingress filtering).
    124  */
    125 static int
    126 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
    127 {
    128 	union sockaddr_union dst_address;
    129 	struct secasvar *sav;
    130 	u_int32_t spi;
    131 	u_int16_t sport;
    132 	u_int16_t dport;
    133 	int s, error;
    134 
    135 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
    136 		IPCOMP_STAT_INPUT);
    137 
    138 	IPSEC_ASSERT(m != NULL, ("ipsec_common_input: null packet"));
    139 
    140 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
    141 	    (sproto == IPPROTO_AH && !ah_enable) ||
    142 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    143 		m_freem(m);
    144 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
    145 		    IPCOMP_STAT_PDROPS);
    146 		return EOPNOTSUPP;
    147 	}
    148 
    149 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
    150 		m_freem(m);
    151 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    152 		    IPCOMP_STAT_HDROPS);
    153 		DPRINTF(("ipsec_common_input: packet too small\n"));
    154 		return EINVAL;
    155 	}
    156 
    157 	/* Retrieve the SPI from the relevant IPsec header */
    158 	if (sproto == IPPROTO_ESP)
    159 		m_copydata(m, skip, sizeof(u_int32_t), &spi);
    160 	else if (sproto == IPPROTO_AH)
    161 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
    162 	else if (sproto == IPPROTO_IPCOMP) {
    163 		u_int16_t cpi;
    164 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
    165 		spi = ntohl(htons(cpi));
    166 	} else {
    167 		panic("ipsec_common_input called with bad protocol number :"
    168 		      "%d\n", sproto);
    169 	}
    170 
    171 
    172 	/* find the source port for NAT-T */
    173 	nat_t_ports_get(m, &dport, &sport);
    174 
    175 	/*
    176 	 * Find the SA and (indirectly) call the appropriate
    177 	 * kernel crypto routine. The resulting mbuf chain is a valid
    178 	 * IP packet ready to go through input processing.
    179 	 */
    180 	memset(&dst_address, 0, sizeof (dst_address));
    181 	dst_address.sa.sa_family = af;
    182 	switch (af) {
    183 #ifdef INET
    184 	case AF_INET:
    185 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
    186 		m_copydata(m, offsetof(struct ip, ip_dst),
    187 		    sizeof(struct in_addr),
    188 		    &dst_address.sin.sin_addr);
    189 		break;
    190 #endif /* INET */
    191 #ifdef INET6
    192 	case AF_INET6:
    193 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
    194 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
    195 		    sizeof(struct in6_addr),
    196 		    &dst_address.sin6.sin6_addr);
    197 		if (sa6_recoverscope(&dst_address.sin6)) {
    198 			m_freem(m);
    199 			return EINVAL;
    200 		}
    201 		break;
    202 #endif /* INET6 */
    203 	default:
    204 		DPRINTF(("ipsec_common_input: unsupported protocol "
    205 			"family %u\n", af));
    206 		m_freem(m);
    207 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
    208 		    IPCOMP_STAT_NOPF);
    209 		return EPFNOSUPPORT;
    210 	}
    211 
    212 	s = splsoftnet();
    213 
    214 	/* NB: only pass dst since key_allocsa follows RFC2401 */
    215 	sav = KEY_ALLOCSA(&dst_address, sproto, spi, sport, dport);
    216 	if (sav == NULL) {
    217 		DPRINTF(("ipsec_common_input: no key association found for"
    218 			  " SA %s/%08lx/%u/%u\n",
    219 			  ipsec_address(&dst_address),
    220 			  (u_long) ntohl(spi), sproto, ntohs(dport)));
    221 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
    222 		    IPCOMP_STAT_NOTDB);
    223 		splx(s);
    224 		m_freem(m);
    225 		return ENOENT;
    226 	}
    227 
    228 	if (sav->tdb_xform == NULL) {
    229 		DPRINTF(("ipsec_common_input: attempted to use uninitialized"
    230 			 " SA %s/%08lx/%u\n",
    231 			 ipsec_address(&dst_address),
    232 			 (u_long) ntohl(spi), sproto));
    233 		IPSEC_ISTAT(sproto, ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
    234 		    IPCOMP_STAT_NOXFORM);
    235 		KEY_FREESAV(&sav);
    236 		splx(s);
    237 		m_freem(m);
    238 		return ENXIO;
    239 	}
    240 
    241 	/*
    242 	 * Call appropriate transform and return -- callback takes care of
    243 	 * everything else.
    244 	 */
    245 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
    246 	KEY_FREESAV(&sav);
    247 	splx(s);
    248 	return error;
    249 }
    250 
    251 #ifdef INET
    252 /*
    253  * Common input handler for IPv4 AH, ESP, and IPCOMP.
    254  */
    255 void
    256 ipsec4_common_input(struct mbuf *m, ...)
    257 {
    258 	va_list ap;
    259 	int off, nxt;
    260 
    261 	va_start(ap, m);
    262 	off = va_arg(ap, int);
    263 	nxt = va_arg(ap, int);
    264 	va_end(ap);
    265 
    266 	(void) ipsec_common_input(m, off, offsetof(struct ip, ip_p),
    267 				  AF_INET, nxt);
    268 }
    269 
    270 /*
    271  * IPsec input callback for INET protocols.
    272  * This routine is called as the transform callback.
    273  * Takes care of filtering and other sanity checks on
    274  * the processed packet.
    275  */
    276 int
    277 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
    278     int skip, int protoff, struct m_tag *mt)
    279 {
    280 	int prot, af __diagused, sproto;
    281 	struct ip *ip;
    282 	struct m_tag *mtag;
    283 	struct tdb_ident *tdbi;
    284 	struct secasindex *saidx;
    285 	int error;
    286 
    287 	IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
    288 
    289 	IPSEC_ASSERT(m != NULL, ("ipsec4_common_input_cb: null mbuf"));
    290 	IPSEC_ASSERT(sav != NULL, ("ipsec4_common_input_cb: null SA"));
    291 	IPSEC_ASSERT(sav->sah != NULL, ("ipsec4_common_input_cb: null SAH"));
    292 	saidx = &sav->sah->saidx;
    293 	af = saidx->dst.sa.sa_family;
    294 	IPSEC_ASSERT(af == AF_INET, ("ipsec4_common_input_cb: unexpected af %u",af));
    295 	sproto = saidx->proto;
    296 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    297 		sproto == IPPROTO_IPCOMP,
    298 		("ipsec4_common_input_cb: unexpected security protocol %u",
    299 		sproto));
    300 
    301 	/* Sanity check */
    302 	if (m == NULL) {
    303 		DPRINTF(("ipsec4_common_input_cb: null mbuf"));
    304 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    305 		    IPCOMP_STAT_BADKCR);
    306 		KEY_FREESAV(&sav);
    307 		return EINVAL;
    308 	}
    309 
    310 	/* Fix IPv4 header */
    311 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
    312 		DPRINTF(("ipsec4_common_input_cb: processing failed "
    313 		    "for SA %s/%08lx\n",
    314 		    ipsec_address(&sav->sah->saidx.dst),
    315 		    (u_long) ntohl(sav->spi)));
    316 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    317 		    IPCOMP_STAT_HDROPS);
    318 		error = ENOBUFS;
    319 		goto bad;
    320 	}
    321 
    322 	ip = mtod(m, struct ip *);
    323 	ip->ip_len = htons(m->m_pkthdr.len);
    324 	prot = ip->ip_p;
    325 
    326 	/* IP-in-IP encapsulation */
    327 	if (prot == IPPROTO_IPIP) {
    328 		struct ip ipn;
    329 
    330 		/* ipn will now contain the inner IPv4 header */
    331 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
    332 
    333 #ifdef notyet
    334 		/* XXX PROXY address isn't recorded in SAH */
    335 		/*
    336 		 * Check that the inner source address is the same as
    337 		 * the proxy address, if available.
    338 		 */
    339 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    340 		    saidx->proxy.sin.sin_addr.s_addr !=
    341 		    INADDR_ANY &&
    342 		    ipn.ip_src.s_addr !=
    343 		    saidx->proxy.sin.sin_addr.s_addr) ||
    344 		    (saidx->proxy.sa.sa_family != AF_INET &&
    345 			saidx->proxy.sa.sa_family != 0)) {
    346 
    347 			DPRINTF(("ipsec4_common_input_cb: inner "
    348 			    "source address %s doesn't correspond to "
    349 			    "expected proxy source %s, SA %s/%08lx\n",
    350 			    inet_ntoa4(ipn.ip_src),
    351 			    ipsp_address(saidx->proxy),
    352 			    ipsp_address(saidx->dst),
    353 			    (u_long) ntohl(sav->spi)));
    354 
    355 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    356 			    AH_STAT_PDROPS,
    357 			    IPCOMP_STAT_PDROPS);
    358 			error = EACCES;
    359 			goto bad;
    360 		}
    361 #endif /*XXX*/
    362 	}
    363 #if INET6
    364 	/* IPv6-in-IP encapsulation. */
    365 	if (prot == IPPROTO_IPV6) {
    366 		struct ip6_hdr ip6n;
    367 
    368 		/* ip6n will now contain the inner IPv6 header. */
    369 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
    370 
    371 #ifdef notyet
    372 		/*
    373 		 * Check that the inner source address is the same as
    374 		 * the proxy address, if available.
    375 		 */
    376 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    377 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    378 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    379 			&saidx->proxy.sin6.sin6_addr)) ||
    380 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    381 			saidx->proxy.sa.sa_family != 0)) {
    382 
    383 			char ip6buf[INET6_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),
    389 			    ipsec_address(&saidx->dst),
    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 			IPSEC_ASSERT(l > 0,
    479 			  ("ipsec6_common_input: l went zero or negative"));
    480 
    481 			nxt = ip6e.ip6e_nxt;
    482 		} while (protoff + l < *offp);
    483 
    484 		/* Malformed packet check */
    485 		if (protoff + l != *offp) {
    486 			DPRINTF(("ipsec6_common_input: bad packet header chain, "
    487 				"protoff %u, l %u, off %u\n", protoff, l, *offp));
    488 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
    489 				    AH_STAT_HDROPS,
    490 				    IPCOMP_STAT_HDROPS);
    491 			m_freem(*mp);
    492 			*mp = NULL;
    493 			return IPPROTO_DONE;
    494 		}
    495 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
    496 	}
    497 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
    498 	return IPPROTO_DONE;
    499 }
    500 
    501 /*
    502  * NB: ipsec_netbsd.c has a duplicate definition of esp6_ctlinput(),
    503  * with slightly ore recent multicast tests. These should be merged.
    504  * For now, ifdef accordingly.
    505  */
    506 #ifdef __FreeBSD__
    507 void
    508 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
    509 {
    510 	if (sa->sa_family != AF_INET6 ||
    511 	    sa->sa_len != sizeof(struct sockaddr_in6))
    512 		return;
    513 	if ((unsigned)cmd >= PRC_NCMDS)
    514 		return;
    515 
    516 	/* if the parameter is from icmp6, decode it. */
    517 	if (d !=  NULL) {
    518 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
    519 		struct mbuf *m = ip6cp->ip6c_m;
    520 		int off = ip6cp->ip6c_off;
    521 
    522 		struct ip6ctlparam ip6cp1;
    523 
    524 		/*
    525 		 * Notify the error to all possible sockets via pfctlinput2.
    526 		 * Since the upper layer information (such as protocol type,
    527 		 * source and destination ports) is embedded in the encrypted
    528 		 * data and might have been cut, we can't directly call
    529 		 * an upper layer ctlinput function. However, the pcbnotify
    530 		 * function will consider source and destination addresses
    531 		 * as well as the flow info value, and may be able to find
    532 		 * some PCB that should be notified.
    533 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
    534 		 * no possibility of an infinite loop of function calls,
    535 		 * because we don't pass the inner IPv6 header.
    536 		 */
    537 		memset(&ip6cp1, 0, sizeof(ip6cp1));
    538 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
    539 		pfctlinput2(cmd, sa, &ip6cp1);
    540 
    541 		/*
    542 		 * Then go to special cases that need ESP header information.
    543 		 * XXX: We assume that when ip6 is non NULL,
    544 		 * M and OFF are valid.
    545 		 */
    546 
    547 		if (cmd == PRC_MSGSIZE) {
    548 			struct secasvar *sav;
    549 			u_int32_t spi;
    550 			int valid;
    551 
    552 			/* check header length before using m_copydata */
    553 			if (m->m_pkthdr.len < off + sizeof (struct esp))
    554 				return;
    555 			m_copydata(m, off + offsetof(struct esp, esp_spi),
    556 				sizeof(u_int32_t), &spi);
    557 			/*
    558 			 * Check to see if we have a valid SA corresponding to
    559 			 * the address in the ICMP message payload.
    560 			 */
    561 			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
    562 					IPPROTO_ESP, spi);
    563 			valid = (sav != NULL);
    564 			if (sav)
    565 				KEY_FREESAV(&sav);
    566 
    567 			/* XXX Further validation? */
    568 
    569 			/*
    570 			 * Depending on whether the SA is "valid" and
    571 			 * routing table size (mtudisc_{hi,lo}wat), we will:
    572 			 * - recalcurate the new MTU and create the
    573 			 *   corresponding routing entry, or
    574 			 * - ignore the MTU change notification.
    575 			 */
    576 			icmp6_mtudisc_update(ip6cp, valid);
    577 		}
    578 	} else {
    579 		/* we normally notify any pcb here */
    580 	}
    581 }
    582 #endif /* __FreeBSD__ */
    583 
    584 extern	const struct ip6protosw inet6sw[];
    585 extern	u_char ip6_protox[];
    586 
    587 /*
    588  * IPsec input callback, called by the transform callback. Takes care of
    589  * filtering and other sanity checks on the processed packet.
    590  */
    591 int
    592 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
    593     struct m_tag *mt)
    594 {
    595 	int af __diagused, sproto;
    596 	struct ip6_hdr *ip6;
    597 	struct m_tag *mtag;
    598 	struct tdb_ident *tdbi;
    599 	struct secasindex *saidx;
    600 	int nxt;
    601 	u_int8_t prot, nxt8;
    602 	int error, nest;
    603 
    604 	IPSEC_ASSERT(m != NULL, ("ipsec6_common_input_cb: null mbuf"));
    605 	IPSEC_ASSERT(sav != NULL, ("ipsec6_common_input_cb: null SA"));
    606 	IPSEC_ASSERT(sav->sah != NULL, ("ipsec6_common_input_cb: null SAH"));
    607 	saidx = &sav->sah->saidx;
    608 	af = saidx->dst.sa.sa_family;
    609 	IPSEC_ASSERT(af == AF_INET6,
    610 		("ipsec6_common_input_cb: unexpected af %u", af));
    611 	sproto = saidx->proto;
    612 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    613 		sproto == IPPROTO_IPCOMP,
    614 		("ipsec6_common_input_cb: unexpected security protocol %u",
    615 		sproto));
    616 
    617 	/* Sanity check */
    618 	if (m == NULL) {
    619 		DPRINTF(("ipsec6_common_input_cb: null mbuf"));
    620 		IPSEC_ISTAT(sproto, ESP_STAT_BADKCR, AH_STAT_BADKCR,
    621 		    IPCOMP_STAT_BADKCR);
    622 		error = EINVAL;
    623 		goto bad;
    624 	}
    625 
    626 	/* Fix IPv6 header */
    627 	if (m->m_len < sizeof(struct ip6_hdr) &&
    628 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    629 
    630 		DPRINTF(("ipsec6_common_input_cb: processing failed "
    631 		    "for SA %s/%08lx\n", ipsec_address(&sav->sah->saidx.dst),
    632 		    (u_long) ntohl(sav->spi)));
    633 
    634 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    635 		    IPCOMP_STAT_HDROPS);
    636 		error = EACCES;
    637 		goto bad;
    638 	}
    639 
    640 	ip6 = mtod(m, struct ip6_hdr *);
    641 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    642 
    643 	/* Save protocol */
    644 	m_copydata(m, protoff, 1, &prot);
    645 
    646 #ifdef INET
    647 	/* IP-in-IP encapsulation */
    648 	if (prot == IPPROTO_IPIP) {
    649 		struct ip ipn;
    650 
    651 		/* ipn will now contain the inner IPv4 header */
    652 		m_copydata(m, skip, sizeof(struct ip), &ipn);
    653 
    654 #ifdef notyet
    655 		/*
    656 		 * Check that the inner source address is the same as
    657 		 * the proxy address, if available.
    658 		 */
    659 		if ((saidx->proxy.sa.sa_family == AF_INET &&
    660 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
    661 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
    662 		    (saidx->proxy.sa.sa_family != AF_INET &&
    663 			saidx->proxy.sa.sa_family != 0)) {
    664 
    665 			DPRINTF(("ipsec6_common_input_cb: inner "
    666 			    "source address %s doesn't correspond to "
    667 			    "expected proxy source %s, SA %s/%08lx\n",
    668 			    inet_ntoa4(ipn.ip_src),
    669 			    ipsec_address(&saidx->proxy),
    670 			    ipsec_address(&saidx->dst),
    671 			    (u_long) ntohl(sav->spi)));
    672 
    673 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    674 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    675 			error = EACCES;
    676 			goto bad;
    677 		}
    678 #endif /*XXX*/
    679 	}
    680 #endif /* INET */
    681 
    682 	/* IPv6-in-IP encapsulation */
    683 	if (prot == IPPROTO_IPV6) {
    684 		struct ip6_hdr ip6n;
    685 
    686 		/* ip6n will now contain the inner IPv6 header. */
    687 		m_copydata(m, skip, sizeof(struct ip6_hdr), &ip6n);
    688 
    689 #ifdef notyet
    690 		/*
    691 		 * Check that the inner source address is the same as
    692 		 * the proxy address, if available.
    693 		 */
    694 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
    695 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
    696 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
    697 			&saidx->proxy.sin6.sin6_addr)) ||
    698 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
    699 			saidx->proxy.sa.sa_family != 0)) {
    700 
    701 			char ip6buf[INET6_ADDRSTRLEN];
    702 			DPRINTF(("ipsec6_common_input_cb: inner "
    703 			    "source address %s doesn't correspond to "
    704 			    "expected proxy source %s, SA %s/%08lx\n",
    705 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
    706 			    ipsec_address(&saidx->proxy),
    707 			    ipsec_address(&saidx->dst),
    708 			    (u_long) ntohl(sav->spi)));
    709 
    710 			IPSEC_ISTAT(sproto, ESP_STAT_PDROPS,
    711 			    AH_STAT_PDROPS, IPCOMP_STAT_PDROPS);
    712 			error = EACCES;
    713 			goto bad;
    714 		}
    715 #endif /*XXX*/
    716 	}
    717 
    718 	/*
    719 	 * Record what we've done to the packet (under what SA it was
    720 	 * processed). If we've been passed an mtag, it means the packet
    721 	 * was already processed by an ethernet/crypto combo card and
    722 	 * thus has a tag attached with all the right information, but
    723 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
    724 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
    725 	 */
    726 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
    727 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
    728 		    sizeof(struct tdb_ident), M_NOWAIT);
    729 		if (mtag == NULL) {
    730 			DPRINTF(("ipsec_common_input_cb: failed to "
    731 			    "get tag\n"));
    732 			IPSEC_ISTAT(sproto, ESP_STAT_HDROPS,
    733 			    AH_STAT_HDROPS, IPCOMP_STAT_HDROPS);
    734 			error = ENOMEM;
    735 			goto bad;
    736 		}
    737 
    738 		tdbi = (struct tdb_ident *)(mtag + 1);
    739 		memcpy(&tdbi->dst, &saidx->dst, sizeof(union sockaddr_union));
    740 		tdbi->proto = sproto;
    741 		tdbi->spi = sav->spi;
    742 
    743 		m_tag_prepend(m, mtag);
    744 	} else {
    745 		if (mt != NULL)
    746 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
    747 			/* XXX do we need to mark m_flags??? */
    748 	}
    749 
    750 	key_sa_recordxfer(sav, m);
    751 
    752 	/* Retrieve new protocol */
    753 	m_copydata(m, protoff, sizeof(u_int8_t), &nxt8);
    754 
    755 	/*
    756 	 * See the end of ip6_input for this logic.
    757 	 * IPPROTO_IPV[46] case will be processed just like other ones
    758 	 */
    759 	nest = 0;
    760 	nxt = nxt8;
    761 	while (nxt != IPPROTO_DONE) {
    762 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    763 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    764 			error = EINVAL;
    765 			goto bad;
    766 		}
    767 
    768 		/*
    769 		 * Protection against faulty packet - there should be
    770 		 * more sanity checks in header chain processing.
    771 		 */
    772 		if (m->m_pkthdr.len < skip) {
    773 			IP6_STATINC(IP6_STAT_TOOSHORT);
    774 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
    775 				       ifs6_in_truncated);
    776 			error = EINVAL;
    777 			goto bad;
    778 		}
    779 		/*
    780 		 * Enforce IPsec policy checking if we are seeing last header.
    781 		 * note that we do not visit this with protocols with pcb layer
    782 		 * code - like udp/tcp/raw ip.
    783 		 */
    784 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    785 		    ipsec6_in_reject(m, NULL)) {
    786 			error = EINVAL;
    787 			goto bad;
    788 		}
    789 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
    790 	}
    791 	return 0;
    792 bad:
    793 	if (m)
    794 		m_freem(m);
    795 	return error;
    796 }
    797 #endif /* INET6 */
    798