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