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