xref: /netbsd-src/sys/netipsec/ipsec_input.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /*	$NetBSD: ipsec_input.c,v 1.81 2024/07/05 04:31:54 rin Exp $	*/
2e2c8a664Smaxv /*	$FreeBSD: ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $	*/
309a25ecbSthorpej /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
409a25ecbSthorpej 
509a25ecbSthorpej /*
609a25ecbSthorpej  * The authors of this code are John Ioannidis (ji@tla.org),
709a25ecbSthorpej  * Angelos D. Keromytis (kermit@csd.uch.gr) and
809a25ecbSthorpej  * Niels Provos (provos@physnet.uni-hamburg.de).
909a25ecbSthorpej  *
1009a25ecbSthorpej  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
1109a25ecbSthorpej  * in November 1995.
1209a25ecbSthorpej  *
1309a25ecbSthorpej  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
1409a25ecbSthorpej  * by Angelos D. Keromytis.
1509a25ecbSthorpej  *
1609a25ecbSthorpej  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
1709a25ecbSthorpej  * and Niels Provos.
1809a25ecbSthorpej  *
1909a25ecbSthorpej  * Additional features in 1999 by Angelos D. Keromytis.
2009a25ecbSthorpej  *
2109a25ecbSthorpej  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
2209a25ecbSthorpej  * Angelos D. Keromytis and Niels Provos.
2309a25ecbSthorpej  * Copyright (c) 2001, Angelos D. Keromytis.
2409a25ecbSthorpej  *
2509a25ecbSthorpej  * Permission to use, copy, and modify this software with or without fee
2609a25ecbSthorpej  * is hereby granted, provided that this entire notice is included in
2709a25ecbSthorpej  * all copies of any software which is or includes a copy or
2809a25ecbSthorpej  * modification of this software.
2909a25ecbSthorpej  * You may use this code under the GNU public license if you so wish. Please
3009a25ecbSthorpej  * contribute changes back to the authors under this freer than GPL license
3109a25ecbSthorpej  * so that we may further the use of strong encryption without limitations to
3209a25ecbSthorpej  * all.
3309a25ecbSthorpej  *
3409a25ecbSthorpej  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
3509a25ecbSthorpej  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
3609a25ecbSthorpej  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
3709a25ecbSthorpej  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
3809a25ecbSthorpej  * PURPOSE.
3909a25ecbSthorpej  */
4074029031Sjonathan 
4174029031Sjonathan #include <sys/cdefs.h>
42*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.81 2024/07/05 04:31:54 rin Exp $");
4374029031Sjonathan 
4474029031Sjonathan /*
4574029031Sjonathan  * IPsec input processing.
4674029031Sjonathan  */
4774029031Sjonathan 
4880d40a78Sozaki-r #if defined(_KERNEL_OPT)
4974029031Sjonathan #include "opt_inet.h"
5080d40a78Sozaki-r #endif
5174029031Sjonathan 
5274029031Sjonathan #include <sys/param.h>
5374029031Sjonathan #include <sys/systm.h>
5474029031Sjonathan #include <sys/mbuf.h>
5574029031Sjonathan #include <sys/domain.h>
5674029031Sjonathan #include <sys/protosw.h>
5774029031Sjonathan #include <sys/socket.h>
5874029031Sjonathan #include <sys/errno.h>
5974029031Sjonathan #include <sys/syslog.h>
6074029031Sjonathan 
6174029031Sjonathan #include <net/if.h>
6274029031Sjonathan #include <net/route.h>
6374029031Sjonathan 
6474029031Sjonathan #include <netinet/in.h>
6574029031Sjonathan #include <netinet/in_systm.h>
6674029031Sjonathan #include <netinet/ip.h>
6774029031Sjonathan #include <netinet/ip_var.h>
6874029031Sjonathan #include <netinet/in_var.h>
6900cbca9cSdrochner #include <netinet/in_proto.h>
70c4c3d29fSchristos #include <netinet/udp.h>
71c4c3d29fSchristos #include <netinet/tcp.h>
7274029031Sjonathan 
7374029031Sjonathan #include <netinet/ip6.h>
7474029031Sjonathan #ifdef INET6
7541756529Smaxv #include <netinet6/in6.h>
7674029031Sjonathan #include <netinet6/ip6_var.h>
77680fd686Sthorpej #include <netinet6/ip6_private.h>
78a301ba88Sdrochner #include <netinet6/scope6_var.h>
7974029031Sjonathan #endif
8074029031Sjonathan #include <netinet/in_pcb.h>
8174029031Sjonathan 
8274029031Sjonathan #include <netipsec/ipsec.h>
83caf49ea5Sthorpej #include <netipsec/ipsec_private.h>
8474029031Sjonathan #ifdef INET6
8574029031Sjonathan #include <netipsec/ipsec6.h>
8674029031Sjonathan #endif
8774029031Sjonathan #include <netipsec/ah_var.h>
8874029031Sjonathan #include <netipsec/esp.h>
8974029031Sjonathan #include <netipsec/esp_var.h>
9074029031Sjonathan #include <netipsec/ipcomp_var.h>
9174029031Sjonathan 
929355900eStls #include <netipsec/key.h>
939355900eStls #include <netipsec/keydb.h>
9474029031Sjonathan 
9574029031Sjonathan #include <netipsec/xform.h>
9674029031Sjonathan #include <netinet6/ip6protosw.h>
9774029031Sjonathan 
98caf49ea5Sthorpej #define	IPSEC_ISTAT(p, x, y, z)						\
99caf49ea5Sthorpej do {									\
100caf49ea5Sthorpej 	switch (p) {							\
101caf49ea5Sthorpej 	case IPPROTO_ESP:						\
102caf49ea5Sthorpej 		ESP_STATINC(x);						\
103caf49ea5Sthorpej 		break;							\
104caf49ea5Sthorpej 	case IPPROTO_AH:						\
105caf49ea5Sthorpej 		AH_STATINC(y);						\
106caf49ea5Sthorpej 		break;							\
107caf49ea5Sthorpej 	default:							\
108caf49ea5Sthorpej 		IPCOMP_STATINC(z);					\
109caf49ea5Sthorpej 		break;							\
110caf49ea5Sthorpej 	}								\
111caf49ea5Sthorpej } while (/*CONSTCOND*/0)
11274029031Sjonathan 
11374029031Sjonathan /*
114c4c3d29fSchristos  * fixup TCP/UDP checksum
115c4c3d29fSchristos  *
116c4c3d29fSchristos  * XXX: if we have NAT-OA payload from IKE server,
117c4c3d29fSchristos  *      we must do the differential update of checksum.
118c4c3d29fSchristos  *
119cdc507f0Sandvar  * XXX: NAT-OAi/NAT-OAr derived from IKE initiator/responder.
120c4c3d29fSchristos  *      how to know the IKE side from kernel?
121c4c3d29fSchristos  */
122c4c3d29fSchristos static struct mbuf *
ipsec4_fixup_checksum(struct mbuf * m)123c4c3d29fSchristos ipsec4_fixup_checksum(struct mbuf *m)
124c4c3d29fSchristos {
125c4c3d29fSchristos 	struct ip *ip;
126c4c3d29fSchristos 	struct tcphdr *th;
127c4c3d29fSchristos 	struct udphdr *uh;
128c4c3d29fSchristos 	int poff, off;
129c4c3d29fSchristos 	int plen;
130c4c3d29fSchristos 
1319d97a0ebSozaki-r 	if (m->m_len < sizeof(*ip)) {
132c4c3d29fSchristos 		m = m_pullup(m, sizeof(*ip));
1339d97a0ebSozaki-r 		if (m == NULL)
1349d97a0ebSozaki-r 			return NULL;
1359d97a0ebSozaki-r 	}
136c4c3d29fSchristos 	ip = mtod(m, struct ip *);
137c4c3d29fSchristos 	poff = ip->ip_hl << 2;
138c4c3d29fSchristos 	plen = ntohs(ip->ip_len) - poff;
139c4c3d29fSchristos 
140c4c3d29fSchristos 	switch (ip->ip_p) {
141c4c3d29fSchristos 	case IPPROTO_TCP:
142a1d8c752Smaxv 		M_REGION_GET(th, struct tcphdr *, m, poff, sizeof(*th));
143c4c3d29fSchristos 		if (th == NULL)
144c4c3d29fSchristos 			return NULL;
145c4c3d29fSchristos 		off = th->th_off << 2;
146c4c3d29fSchristos 		if (off < sizeof(*th) || off > plen) {
147c4c3d29fSchristos 			m_freem(m);
148c4c3d29fSchristos 			return NULL;
149c4c3d29fSchristos 		}
150c4c3d29fSchristos 		th->th_sum = 0;
151c4c3d29fSchristos 		th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen);
152c4c3d29fSchristos 		break;
153c4c3d29fSchristos 	case IPPROTO_UDP:
154a1d8c752Smaxv 		M_REGION_GET(uh, struct udphdr *, m, poff, sizeof(*uh));
155c4c3d29fSchristos 		if (uh == NULL)
156c4c3d29fSchristos 			return NULL;
157c4c3d29fSchristos 		off = sizeof(*uh);
158c4c3d29fSchristos 		if (off > plen) {
159c4c3d29fSchristos 			m_freem(m);
160c4c3d29fSchristos 			return NULL;
161c4c3d29fSchristos 		}
162c4c3d29fSchristos 		uh->uh_sum = 0;
163c4c3d29fSchristos 		uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen);
164c4c3d29fSchristos 		break;
165c4c3d29fSchristos 	default:
166c4c3d29fSchristos 		/* no checksum */
167c4c3d29fSchristos 		return m;
168c4c3d29fSchristos 	}
169c4c3d29fSchristos 
170c4c3d29fSchristos 	return m;
171c4c3d29fSchristos }
172c4c3d29fSchristos 
17374fced2cSmaxv static void
nat_t_ports_get(struct mbuf * m,uint16_t * dport,uint16_t * sport)17474fced2cSmaxv nat_t_ports_get(struct mbuf *m, uint16_t *dport, uint16_t *sport)
17574fced2cSmaxv {
17674fced2cSmaxv 	struct m_tag *tag;
17774fced2cSmaxv 
1785c987100Smaxv 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS))) {
17974fced2cSmaxv 		*sport = ((uint16_t *)(tag + 1))[0];
18074fced2cSmaxv 		*dport = ((uint16_t *)(tag + 1))[1];
18174fced2cSmaxv 	} else
18274fced2cSmaxv 		*sport = *dport = 0;
18374fced2cSmaxv }
18474fced2cSmaxv 
1850aad2e35Schristos static uint32_t
spi_get(struct mbuf * m,int sproto,int skip)1860aad2e35Schristos spi_get(struct mbuf *m, int sproto, int skip)
1870aad2e35Schristos {
1880aad2e35Schristos 	uint32_t spi;
1890aad2e35Schristos 	uint16_t cpi;
1900aad2e35Schristos 
1910aad2e35Schristos 	switch (sproto) {
1920aad2e35Schristos 	case IPPROTO_ESP:
1930aad2e35Schristos 		m_copydata(m, skip, sizeof(spi), &spi);
1940aad2e35Schristos 		return spi;
1950aad2e35Schristos 	case IPPROTO_AH:
1960aad2e35Schristos 		m_copydata(m, skip + sizeof(spi), sizeof(spi), &spi);
1970aad2e35Schristos 		return spi;
1980aad2e35Schristos 	case IPPROTO_IPCOMP:
1990aad2e35Schristos 		m_copydata(m, skip + sizeof(cpi), sizeof(cpi), &cpi);
2000aad2e35Schristos 		return htonl(ntohs(cpi));
2010aad2e35Schristos 	default:
2020aad2e35Schristos 		panic("%s called with bad protocol number: %d\n", __func__,
2030aad2e35Schristos 		    sproto);
2040aad2e35Schristos 	}
2050aad2e35Schristos }
2060aad2e35Schristos 
2070aad2e35Schristos 
208c4c3d29fSchristos /*
20974029031Sjonathan  * ipsec_common_input gets called when an IPsec-protected packet
21032338100Smaxv  * is received by IPv4 or IPv6.  Its job is to find the right SA
21132338100Smaxv  * and call the appropriate transform.  The transform callback
21274029031Sjonathan  * takes care of further processing (like ingress filtering).
21374029031Sjonathan  */
21474029031Sjonathan static int
ipsec_common_input(struct mbuf * m,int skip,int protoff,int af,int sproto)21574029031Sjonathan ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
21674029031Sjonathan {
2178b09e22cSknakahara 	char buf[IPSEC_ADDRSTRLEN], buf2[IPSEC_ADDRSTRLEN];
2188b09e22cSknakahara 	union sockaddr_union src_address, dst_address;
21974029031Sjonathan 	struct secasvar *sav;
22074029031Sjonathan 	u_int32_t spi;
221213e873fSchristos 	u_int16_t sport;
222213e873fSchristos 	u_int16_t dport;
223986909fbSozaki-r 	int error;
22474029031Sjonathan 
225caf49ea5Sthorpej 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
226caf49ea5Sthorpej 		IPCOMP_STAT_INPUT);
22774029031Sjonathan 
2282620e166Sozaki-r 	KASSERT(m != NULL);
22974029031Sjonathan 
23074029031Sjonathan 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
23174029031Sjonathan 	    (sproto == IPPROTO_AH && !ah_enable) ||
23274029031Sjonathan 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
23374029031Sjonathan 		m_freem(m);
234caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
235caf49ea5Sthorpej 		    IPCOMP_STAT_PDROPS);
23674029031Sjonathan 		return EOPNOTSUPP;
23774029031Sjonathan 	}
23874029031Sjonathan 
23974029031Sjonathan 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
24074029031Sjonathan 		m_freem(m);
241caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
242caf49ea5Sthorpej 		    IPCOMP_STAT_HDROPS);
243290dc492Sozaki-r 		IPSECLOG(LOG_DEBUG, "packet too small\n");
24474029031Sjonathan 		return EINVAL;
24574029031Sjonathan 	}
24674029031Sjonathan 
24774029031Sjonathan 	/* Retrieve the SPI from the relevant IPsec header */
2480aad2e35Schristos 	spi = spi_get(m, sproto, skip);
24974029031Sjonathan 
2504ddfe916Sdegroote 	/* find the source port for NAT-T */
251213e873fSchristos 	nat_t_ports_get(m, &dport, &sport);
2524ddfe916Sdegroote 
25374029031Sjonathan 	/*
25474029031Sjonathan 	 * Find the SA and (indirectly) call the appropriate
25574029031Sjonathan 	 * kernel crypto routine. The resulting mbuf chain is a valid
25674029031Sjonathan 	 * IP packet ready to go through input processing.
25774029031Sjonathan 	 */
2588b09e22cSknakahara 	memset(&src_address, 0, sizeof (src_address));
259c363a9cbScegger 	memset(&dst_address, 0, sizeof(dst_address));
2608b09e22cSknakahara 	src_address.sa.sa_family = af;
26174029031Sjonathan 	dst_address.sa.sa_family = af;
26274029031Sjonathan 	switch (af) {
26374029031Sjonathan #ifdef INET
26474029031Sjonathan 	case AF_INET:
2658b09e22cSknakahara 		src_address.sin.sin_len = sizeof(struct sockaddr_in);
26674029031Sjonathan 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
2678b09e22cSknakahara 		m_copydata(m, offsetof(struct ip, ip_src),
2688b09e22cSknakahara 		    sizeof(struct in_addr),
2698b09e22cSknakahara 		    &src_address.sin.sin_addr);
27074029031Sjonathan 		m_copydata(m, offsetof(struct ip, ip_dst),
27174029031Sjonathan 		    sizeof(struct in_addr),
272dd86ba72Sdegroote 		    &dst_address.sin.sin_addr);
27374029031Sjonathan 		break;
27416a6b570Smaxv #endif
27574029031Sjonathan #ifdef INET6
27674029031Sjonathan 	case AF_INET6:
2778b09e22cSknakahara 		src_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
27874029031Sjonathan 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
2798b09e22cSknakahara 		m_copydata(m, offsetof(struct ip6_hdr, ip6_src),
2808b09e22cSknakahara 		    sizeof(struct in6_addr),
2818b09e22cSknakahara 		    &src_address.sin6.sin6_addr);
28274029031Sjonathan 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
28374029031Sjonathan 		    sizeof(struct in6_addr),
284dd86ba72Sdegroote 		    &dst_address.sin6.sin6_addr);
285a301ba88Sdrochner 		if (sa6_recoverscope(&dst_address.sin6)) {
286a301ba88Sdrochner 			m_freem(m);
287a301ba88Sdrochner 			return EINVAL;
288a301ba88Sdrochner 		}
28974029031Sjonathan 		break;
29016a6b570Smaxv #endif
29174029031Sjonathan 	default:
292290dc492Sozaki-r 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
29374029031Sjonathan 		m_freem(m);
294caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
295caf49ea5Sthorpej 		    IPCOMP_STAT_NOPF);
29674029031Sjonathan 		return EPFNOSUPPORT;
29774029031Sjonathan 	}
29874029031Sjonathan 
2996f13f59fSozaki-r 	/* NB: only pass dst since key_lookup_sa follows RFC2401 */
3006f13f59fSozaki-r 	sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
30174029031Sjonathan 	if (sav == NULL) {
3028b09e22cSknakahara 		static struct timeval lasttime = {0, 0};
3038b09e22cSknakahara 		static int curpps = 0;
3048b09e22cSknakahara 
3058b09e22cSknakahara 		if (!ipsec_debug && ppsratecheck(&lasttime, &curpps, 1)) {
3068b09e22cSknakahara 			if (sport || dport) {
3078b09e22cSknakahara 				log(LOG_INFO,
3088b09e22cSknakahara 				    "no key association found for SA"
3098b09e22cSknakahara 				    " %s[%u]-%s[%u]/SPI 0x%08lx\n",
3108b09e22cSknakahara 				    ipsec_address(&src_address, buf, sizeof(buf)),
3118b09e22cSknakahara 				    ntohs(sport),
3128b09e22cSknakahara 				    ipsec_address(&dst_address, buf2, sizeof(buf2)),
3138b09e22cSknakahara 				    ntohs(dport),
3148b09e22cSknakahara 				    (u_long) ntohl(spi));
3158b09e22cSknakahara 			} else {
3168b09e22cSknakahara 				log(LOG_INFO,
3178b09e22cSknakahara 				    "no key association found for"
3188b09e22cSknakahara 				    " SA %s-%s/SPI 0x%08lx\n",
3198b09e22cSknakahara 				    ipsec_address(&src_address, buf, sizeof(buf)),
3208b09e22cSknakahara 				    ipsec_address(&src_address, buf2, sizeof(buf2)),
3218b09e22cSknakahara 				    (u_long) ntohl(spi));
3228b09e22cSknakahara 			}
3238b09e22cSknakahara 		} else if (ipsec_debug) {
324290dc492Sozaki-r 			IPSECLOG(LOG_DEBUG,
3258b09e22cSknakahara 			    "no key association found for SA "
3268b09e22cSknakahara 			    "%s-%s/SPI 0x%08lx/PROTO %u/PORT %u-%u\n",
3278b09e22cSknakahara 			    ipsec_address(&src_address, buf, sizeof(buf)),
3288b09e22cSknakahara 			    ipsec_address(&dst_address, buf2, sizeof(buf2)),
3298b09e22cSknakahara 			     (u_long) ntohl(spi), sproto, ntohs(dport), ntohs(sport));
3308b09e22cSknakahara 		}
331caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
332caf49ea5Sthorpej 		    IPCOMP_STAT_NOTDB);
33374029031Sjonathan 		m_freem(m);
33474029031Sjonathan 		return ENOENT;
33574029031Sjonathan 	}
33674029031Sjonathan 
337765c1e87Sozaki-r 	KASSERT(sav->tdb_xform != NULL);
33874029031Sjonathan 
33974029031Sjonathan 	/*
34074029031Sjonathan 	 * Call appropriate transform and return -- callback takes care of
34174029031Sjonathan 	 * everything else.
34274029031Sjonathan 	 */
34374029031Sjonathan 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
3448be5cabcSozaki-r 	KEY_SA_UNREF(&sav);
34574029031Sjonathan 	return error;
34674029031Sjonathan }
34774029031Sjonathan 
34874029031Sjonathan #ifdef INET
34974029031Sjonathan /*
35074029031Sjonathan  * Common input handler for IPv4 AH, ESP, and IPCOMP.
35174029031Sjonathan  */
352f3ab6286Sjonathan void
ipsec4_common_input(struct mbuf * m,int off,int proto)35315652348Smaxv ipsec4_common_input(struct mbuf *m, int off, int proto)
35474029031Sjonathan {
355f3ab6286Sjonathan 	(void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
35615652348Smaxv 	    AF_INET, proto);
35774029031Sjonathan }
35874029031Sjonathan 
35974029031Sjonathan /*
36074029031Sjonathan  * IPsec input callback for INET protocols.
36174029031Sjonathan  * This routine is called as the transform callback.
36274029031Sjonathan  * Takes care of filtering and other sanity checks on
36374029031Sjonathan  * the processed packet.
36474029031Sjonathan  */
36574029031Sjonathan int
ipsec4_common_input_cb(struct mbuf * m,struct secasvar * sav,int skip,int protoff)36674029031Sjonathan ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
3672b6b0bfcSozaki-r     int skip, int protoff)
36874029031Sjonathan {
369a370d57cSmrg 	int prot, af __diagused, sproto;
37074029031Sjonathan 	struct ip *ip;
37174029031Sjonathan 	struct secasindex *saidx;
37274029031Sjonathan 	int error;
37374029031Sjonathan 
3741d337420Smaxv 	if (__predict_false(m == NULL)) {
3751d337420Smaxv 		panic("%s: NULL mbuf", __func__);
3761d337420Smaxv 	}
3770f3ae052Smaxv 	if (__predict_false(skip < sizeof(struct ip))) {
3780f3ae052Smaxv 		panic("%s: short skip", __func__);
3790f3ae052Smaxv 	}
3801d337420Smaxv 
3812620e166Sozaki-r 	KASSERT(sav != NULL);
38274029031Sjonathan 	saidx = &sav->sah->saidx;
38374029031Sjonathan 	af = saidx->dst.sa.sa_family;
3842620e166Sozaki-r 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
38574029031Sjonathan 	sproto = saidx->proto;
3862620e166Sozaki-r 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
38774029031Sjonathan 	    sproto == IPPROTO_IPCOMP,
3882620e166Sozaki-r 	    "unexpected security protocol %u", sproto);
38974029031Sjonathan 
3900f3ae052Smaxv 	/*
3910f3ae052Smaxv 	 * Update the IPv4 header. The length of the packet may have changed,
3920f3ae052Smaxv 	 * so fix it, and recompute the checksum.
3930f3ae052Smaxv 	 */
39474029031Sjonathan 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
395dd8c81f5Sryo 		char buf[IPSEC_ADDRSTRLEN];
396c4c3d29fSchristos cantpull:
397c4c3d29fSchristos 		IPSECLOG(LOG_DEBUG,
398c4c3d29fSchristos 		    "processing failed for SA %s/%08lx\n",
399c4c3d29fSchristos 		    ipsec_address(&sav->sah->saidx.dst, buf,
400c4c3d29fSchristos 		    sizeof(buf)), (u_long) ntohl(sav->spi));
401caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
402caf49ea5Sthorpej 		    IPCOMP_STAT_HDROPS);
40374029031Sjonathan 		error = ENOBUFS;
40474029031Sjonathan 		goto bad;
40574029031Sjonathan 	}
40674029031Sjonathan 	ip = mtod(m, struct ip *);
40774029031Sjonathan 	ip->ip_len = htons(m->m_pkthdr.len);
408c4c3d29fSchristos 	ip->ip_sum = 0;
409c4c3d29fSchristos 	ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
410c4c3d29fSchristos 
411c4c3d29fSchristos 	/*
412c4c3d29fSchristos 	 * Update TCP/UDP checksum
413c4c3d29fSchristos 	 * XXX: should only do it in NAT-T case
414c4c3d29fSchristos 	 * XXX: should do it incrementally, see FreeBSD code.
415c4c3d29fSchristos 	 */
416c4c3d29fSchristos 	m = ipsec4_fixup_checksum(m);
417c4c3d29fSchristos 	if (m == NULL)
418c4c3d29fSchristos 		goto cantpull;
41999712ef7Smaxv 	ip = mtod(m, struct ip *);
420c4c3d29fSchristos 
42174029031Sjonathan 	prot = ip->ip_p;
42274029031Sjonathan 
423f75d3c05Smaxv 	M_VERIFY_PACKET(m);
424f75d3c05Smaxv 
42541756529Smaxv 	key_sa_recordxfer(sav, m);
42674029031Sjonathan 
4277c7b1739Sriastradh 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
428b39a0dd1Smaxv 	    ipsec_in_reject(m, NULL)) {
42900cbca9cSdrochner 		error = EINVAL;
43000cbca9cSdrochner 		goto bad;
43174029031Sjonathan 	}
432e2f99c2dSknakahara 
433e2f99c2dSknakahara 	/*
4341cd43426Sandvar 	 * There is no struct ifnet for tunnel mode IP-IP tunnel connection,
435e2f99c2dSknakahara 	 * so we cannot write filtering rule to the inner packet.
436e2f99c2dSknakahara 	 */
437e2f99c2dSknakahara 	if (saidx->mode == IPSEC_MODE_TUNNEL)
438e2f99c2dSknakahara 		m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
439e2f99c2dSknakahara 
4407c7b1739Sriastradh 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
44174029031Sjonathan 	return 0;
44216a6b570Smaxv 
44374029031Sjonathan bad:
44474029031Sjonathan 	m_freem(m);
44574029031Sjonathan 	return error;
44674029031Sjonathan }
44774029031Sjonathan #endif /* INET */
44874029031Sjonathan 
44974029031Sjonathan #ifdef INET6
45074029031Sjonathan int
ipsec6_common_input(struct mbuf ** mp,int * offp,int proto)45174029031Sjonathan ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
45274029031Sjonathan {
45374029031Sjonathan 	int l = 0;
4544b552d0bSdrochner 	int protoff, nxt;
45574029031Sjonathan 	struct ip6_ext ip6e;
45674029031Sjonathan 
45774029031Sjonathan 	if (*offp < sizeof(struct ip6_hdr)) {
458290dc492Sozaki-r 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
459cd9bf26dSdrochner 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
460cd9bf26dSdrochner 			    IPCOMP_STAT_HDROPS);
461cd9bf26dSdrochner 		m_freem(*mp);
46274029031Sjonathan 		return IPPROTO_DONE;
46374029031Sjonathan 	} else if (*offp == sizeof(struct ip6_hdr)) {
46474029031Sjonathan 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
46574029031Sjonathan 	} else {
46674029031Sjonathan 		/* Chase down the header chain... */
46774029031Sjonathan 		protoff = sizeof(struct ip6_hdr);
4684b552d0bSdrochner 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
46974029031Sjonathan 
47074029031Sjonathan 		do {
47174029031Sjonathan 			protoff += l;
472dd86ba72Sdegroote 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
47374029031Sjonathan 
4744b552d0bSdrochner 			if (nxt == IPPROTO_AH)
47574029031Sjonathan 				l = (ip6e.ip6e_len + 2) << 2;
47661a2b14bSmaxv 			else if (nxt == IPPROTO_FRAGMENT)
47761a2b14bSmaxv 				l = sizeof(struct ip6_frag);
47874029031Sjonathan 			else
47974029031Sjonathan 				l = (ip6e.ip6e_len + 1) << 3;
4802620e166Sozaki-r 			KASSERT(l > 0);
4814b552d0bSdrochner 
4824b552d0bSdrochner 			nxt = ip6e.ip6e_nxt;
48374029031Sjonathan 		} while (protoff + l < *offp);
48474029031Sjonathan 
48574029031Sjonathan 		/* Malformed packet check */
48674029031Sjonathan 		if (protoff + l != *offp) {
487290dc492Sozaki-r 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
488290dc492Sozaki-r 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
489caf49ea5Sthorpej 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
490caf49ea5Sthorpej 				    AH_STAT_HDROPS,
491caf49ea5Sthorpej 				    IPCOMP_STAT_HDROPS);
49274029031Sjonathan 			m_freem(*mp);
49374029031Sjonathan 			*mp = NULL;
49474029031Sjonathan 			return IPPROTO_DONE;
49574029031Sjonathan 		}
49674029031Sjonathan 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
49774029031Sjonathan 	}
49874029031Sjonathan 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
49974029031Sjonathan 	return IPPROTO_DONE;
50074029031Sjonathan }
50174029031Sjonathan 
50274029031Sjonathan /*
50374029031Sjonathan  * IPsec input callback, called by the transform callback. Takes care of
50474029031Sjonathan  * filtering and other sanity checks on the processed packet.
50574029031Sjonathan  */
50674029031Sjonathan int
ipsec6_common_input_cb(struct mbuf * m,struct secasvar * sav,int skip,int protoff)5072b6b0bfcSozaki-r ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
5082b6b0bfcSozaki-r     int protoff)
50974029031Sjonathan {
51081f1fa87Sozaki-r 	int af __diagused, sproto;
51174029031Sjonathan 	struct ip6_hdr *ip6;
51274029031Sjonathan 	struct secasindex *saidx;
51374029031Sjonathan 	int nxt;
51441756529Smaxv 	u_int8_t prot;
51574029031Sjonathan 	int error, nest;
51674029031Sjonathan 
5171d337420Smaxv 	if (__predict_false(m == NULL)) {
5181d337420Smaxv 		panic("%s: NULL mbuf", __func__);
5191d337420Smaxv 	}
5201d337420Smaxv 
5212620e166Sozaki-r 	KASSERT(sav != NULL);
52274029031Sjonathan 	saidx = &sav->sah->saidx;
52374029031Sjonathan 	af = saidx->dst.sa.sa_family;
5242620e166Sozaki-r 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
52574029031Sjonathan 	sproto = saidx->proto;
5262620e166Sozaki-r 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
52774029031Sjonathan 	    sproto == IPPROTO_IPCOMP,
5282620e166Sozaki-r 	    "unexpected security protocol %u", sproto);
52974029031Sjonathan 
53074029031Sjonathan 	/* Fix IPv6 header */
53174029031Sjonathan 	if (m->m_len < sizeof(struct ip6_hdr) &&
53274029031Sjonathan 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
533dd8c81f5Sryo 		char buf[IPSEC_ADDRSTRLEN];
534290dc492Sozaki-r 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
535290dc492Sozaki-r 		    ipsec_address(&sav->sah->saidx.dst,
536290dc492Sozaki-r 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
537caf49ea5Sthorpej 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
538caf49ea5Sthorpej 		    IPCOMP_STAT_HDROPS);
53974029031Sjonathan 		error = EACCES;
54074029031Sjonathan 		goto bad;
54174029031Sjonathan 	}
54274029031Sjonathan 
54374029031Sjonathan 	ip6 = mtod(m, struct ip6_hdr *);
54474029031Sjonathan 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
54574029031Sjonathan 
54641756529Smaxv 	m_copydata(m, protoff, sizeof(prot), &prot);
54774029031Sjonathan 
54874029031Sjonathan 	key_sa_recordxfer(sav, m);
54974029031Sjonathan 
55074029031Sjonathan 	/*
55174029031Sjonathan 	 * See the end of ip6_input for this logic.
55274029031Sjonathan 	 * IPPROTO_IPV[46] case will be processed just like other ones
55374029031Sjonathan 	 */
55474029031Sjonathan 	nest = 0;
55541756529Smaxv 	nxt = prot;
55674029031Sjonathan 	while (nxt != IPPROTO_DONE) {
55774029031Sjonathan 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
558680fd686Sthorpej 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
55974029031Sjonathan 			error = EINVAL;
56074029031Sjonathan 			goto bad;
56174029031Sjonathan 		}
56274029031Sjonathan 
563f75d3c05Smaxv 		M_VERIFY_PACKET(m);
564f75d3c05Smaxv 
56574029031Sjonathan 		/*
56674029031Sjonathan 		 * Protection against faulty packet - there should be
56774029031Sjonathan 		 * more sanity checks in header chain processing.
56874029031Sjonathan 		 */
56974029031Sjonathan 		if (m->m_pkthdr.len < skip) {
570680fd686Sthorpej 			IP6_STATINC(IP6_STAT_TOOSHORT);
571fe6d4275Sozaki-r 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
572fe6d4275Sozaki-r 			    ifs6_in_truncated);
57374029031Sjonathan 			error = EINVAL;
57474029031Sjonathan 			goto bad;
57574029031Sjonathan 		}
57616a6b570Smaxv 
57774029031Sjonathan 		/*
57874029031Sjonathan 		 * Enforce IPsec policy checking if we are seeing last header.
57932338100Smaxv 		 * Note that we do not visit this with protocols with pcb layer
58074029031Sjonathan 		 * code - like udp/tcp/raw ip.
58174029031Sjonathan 		 */
58274029031Sjonathan 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
583b39a0dd1Smaxv 		    ipsec_in_reject(m, NULL)) {
58474029031Sjonathan 			error = EINVAL;
58574029031Sjonathan 			goto bad;
58674029031Sjonathan 		}
587e2f99c2dSknakahara 
588e2f99c2dSknakahara 		/*
5891cd43426Sandvar 		 * There is no struct ifnet for tunnel mode IP-IP tunnel connection,
590e2f99c2dSknakahara 		 * so we cannot write filtering rule to the inner packet.
591e2f99c2dSknakahara 		 */
592e2f99c2dSknakahara 		if (saidx->mode == IPSEC_MODE_TUNNEL)
593e2f99c2dSknakahara 			m->m_pkthdr.pkthdr_flags |= PKTHDR_FLAG_IPSEC_SKIP_PFIL;
594e2f99c2dSknakahara 
59574029031Sjonathan 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
59674029031Sjonathan 	}
59774029031Sjonathan 	return 0;
5980f3ae052Smaxv 
59974029031Sjonathan bad:
60074029031Sjonathan 	m_freem(m);
60174029031Sjonathan 	return error;
60274029031Sjonathan }
60374029031Sjonathan #endif /* INET6 */
604