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