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