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