ipsec_input.c revision 221129
1105197Ssam/*	$FreeBSD: head/sys/netipsec/ipsec_input.c 221129 2011-04-27 19:28:42Z bz $	*/
2112758Ssam/*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
3139823Simp/*-
4112758Ssam * The authors of this code are John Ioannidis (ji@tla.org),
5112758Ssam * Angelos D. Keromytis (kermit@csd.uch.gr) and
6112758Ssam * Niels Provos (provos@physnet.uni-hamburg.de).
7112758Ssam *
8112758Ssam * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9112758Ssam * in November 1995.
10112758Ssam *
11112758Ssam * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12112758Ssam * by Angelos D. Keromytis.
13112758Ssam *
14112758Ssam * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15112758Ssam * and Niels Provos.
16112758Ssam *
17112758Ssam * Additional features in 1999 by Angelos D. Keromytis.
18112758Ssam *
19112758Ssam * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20112758Ssam * Angelos D. Keromytis and Niels Provos.
21112758Ssam * Copyright (c) 2001, Angelos D. Keromytis.
22112758Ssam *
23112758Ssam * Permission to use, copy, and modify this software with or without fee
24112758Ssam * is hereby granted, provided that this entire notice is included in
25112758Ssam * all copies of any software which is or includes a copy or
26112758Ssam * modification of this software.
27112758Ssam * You may use this code under the GNU public license if you so wish. Please
28112758Ssam * contribute changes back to the authors under this freer than GPL license
29112758Ssam * so that we may further the use of strong encryption without limitations to
30112758Ssam * all.
31112758Ssam *
32112758Ssam * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33112758Ssam * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34112758Ssam * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35112758Ssam * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
36112758Ssam * PURPOSE.
37112758Ssam */
38105197Ssam
39105197Ssam/*
40105197Ssam * IPsec input processing.
41105197Ssam */
42105197Ssam
43105197Ssam#include "opt_inet.h"
44105197Ssam#include "opt_inet6.h"
45105197Ssam#include "opt_ipsec.h"
46159965Sthompsa#include "opt_enc.h"
47105197Ssam
48105197Ssam#include <sys/param.h>
49105197Ssam#include <sys/systm.h>
50105197Ssam#include <sys/malloc.h>
51105197Ssam#include <sys/mbuf.h>
52105197Ssam#include <sys/domain.h>
53105197Ssam#include <sys/protosw.h>
54105197Ssam#include <sys/socket.h>
55105197Ssam#include <sys/errno.h>
56105197Ssam#include <sys/syslog.h>
57105197Ssam
58105197Ssam#include <net/if.h>
59171497Sbz#include <net/pfil.h>
60105197Ssam#include <net/route.h>
61105197Ssam#include <net/netisr.h>
62195699Srwatson#include <net/vnet.h>
63105197Ssam
64105197Ssam#include <netinet/in.h>
65105197Ssam#include <netinet/in_systm.h>
66105197Ssam#include <netinet/ip.h>
67105197Ssam#include <netinet/ip_var.h>
68105197Ssam#include <netinet/in_var.h>
69105197Ssam
70105197Ssam#include <netinet/ip6.h>
71105197Ssam#ifdef INET6
72105197Ssam#include <netinet6/ip6_var.h>
73105197Ssam#endif
74105197Ssam#include <netinet/in_pcb.h>
75105197Ssam#ifdef INET6
76105197Ssam#include <netinet/icmp6.h>
77105197Ssam#endif
78105197Ssam
79105197Ssam#include <netipsec/ipsec.h>
80105197Ssam#ifdef INET6
81105197Ssam#include <netipsec/ipsec6.h>
82105197Ssam#endif
83105197Ssam#include <netipsec/ah_var.h>
84105197Ssam#include <netipsec/esp.h>
85105197Ssam#include <netipsec/esp_var.h>
86105197Ssam#include <netipsec/ipcomp_var.h>
87105197Ssam
88105197Ssam#include <netipsec/key.h>
89105197Ssam#include <netipsec/keydb.h>
90105197Ssam
91105197Ssam#include <netipsec/xform.h>
92105197Ssam#include <netinet6/ip6protosw.h>
93105197Ssam
94105197Ssam#include <machine/in_cksum.h>
95105197Ssam#include <machine/stdarg.h>
96105197Ssam
97181627Svanhu#ifdef DEV_ENC
98181627Svanhu#include <net/if_enc.h>
99181627Svanhu#endif
100181627Svanhu
101181627Svanhu
102105197Ssam#define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
103105197Ssam			    (p) == IPPROTO_AH ? (y)++ : (z)++)
104105197Ssam
105193947Sbz#ifdef INET
106120585Ssamstatic void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
107193947Sbz#endif
108120585Ssam
109105197Ssam/*
110105197Ssam * ipsec_common_input gets called when an IPsec-protected packet
111214351Sthomas * is received by IPv4 or IPv6.  Its job is to find the right SA
112170793Sbz * and call the appropriate transform.  The transform callback
113105197Ssam * takes care of further processing (like ingress filtering).
114105197Ssam */
115105197Ssamstatic int
116105197Ssamipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
117105197Ssam{
118105197Ssam	union sockaddr_union dst_address;
119105197Ssam	struct secasvar *sav;
120105197Ssam	u_int32_t spi;
121119643Ssam	int error;
122221129Sbz#ifdef INET
123194062Svanhu#ifdef IPSEC_NAT_T
124194062Svanhu	struct m_tag *tag;
125194062Svanhu#endif
126221129Sbz#endif
127105197Ssam
128181803Sbz	IPSEC_ISTAT(sproto, V_espstat.esps_input, V_ahstat.ahs_input,
129181803Sbz		V_ipcompstat.ipcomps_input);
130105197Ssam
131120585Ssam	IPSEC_ASSERT(m != NULL, ("null packet"));
132105197Ssam
133170792Sbz	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
134170792Sbz		sproto == IPPROTO_IPCOMP,
135170792Sbz		("unexpected security protocol %u", sproto));
136170792Sbz
137181803Sbz	if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
138181803Sbz	    (sproto == IPPROTO_AH && !V_ah_enable) ||
139181803Sbz	    (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
140105197Ssam		m_freem(m);
141181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
142181803Sbz		    V_ipcompstat.ipcomps_pdrops);
143105197Ssam		return EOPNOTSUPP;
144105197Ssam	}
145105197Ssam
146105197Ssam	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
147105197Ssam		m_freem(m);
148181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
149181803Sbz		    V_ipcompstat.ipcomps_hdrops);
150120585Ssam		DPRINTF(("%s: packet too small\n", __func__));
151105197Ssam		return EINVAL;
152105197Ssam	}
153105197Ssam
154105197Ssam	/* Retrieve the SPI from the relevant IPsec header */
155105197Ssam	if (sproto == IPPROTO_ESP)
156105197Ssam		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
157105197Ssam	else if (sproto == IPPROTO_AH)
158105197Ssam		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
159105197Ssam		    (caddr_t) &spi);
160105197Ssam	else if (sproto == IPPROTO_IPCOMP) {
161105197Ssam		u_int16_t cpi;
162105197Ssam		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
163105197Ssam		    (caddr_t) &cpi);
164105197Ssam		spi = ntohl(htons(cpi));
165105197Ssam	}
166105197Ssam
167105197Ssam	/*
168105197Ssam	 * Find the SA and (indirectly) call the appropriate
169105197Ssam	 * kernel crypto routine. The resulting mbuf chain is a valid
170105197Ssam	 * IP packet ready to go through input processing.
171105197Ssam	 */
172105197Ssam	bzero(&dst_address, sizeof (dst_address));
173105197Ssam	dst_address.sa.sa_family = af;
174105197Ssam	switch (af) {
175105197Ssam#ifdef INET
176105197Ssam	case AF_INET:
177105197Ssam		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
178105197Ssam		m_copydata(m, offsetof(struct ip, ip_dst),
179105197Ssam		    sizeof(struct in_addr),
180105197Ssam		    (caddr_t) &dst_address.sin.sin_addr);
181194062Svanhu#ifdef IPSEC_NAT_T
182194062Svanhu		/* Find the source port for NAT-T; see udp*_espdecap. */
183194062Svanhu		tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
184194062Svanhu		if (tag != NULL)
185194062Svanhu			dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
186194062Svanhu#endif /* IPSEC_NAT_T */
187105197Ssam		break;
188105197Ssam#endif /* INET */
189105197Ssam#ifdef INET6
190105197Ssam	case AF_INET6:
191105197Ssam		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
192105197Ssam		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
193105197Ssam		    sizeof(struct in6_addr),
194105197Ssam		    (caddr_t) &dst_address.sin6.sin6_addr);
195105197Ssam		break;
196105197Ssam#endif /* INET6 */
197105197Ssam	default:
198120585Ssam		DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
199105197Ssam		m_freem(m);
200181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_nopf, V_ahstat.ahs_nopf,
201181803Sbz		    V_ipcompstat.ipcomps_nopf);
202105197Ssam		return EPFNOSUPPORT;
203105197Ssam	}
204105197Ssam
205105197Ssam	/* NB: only pass dst since key_allocsa follows RFC2401 */
206105197Ssam	sav = KEY_ALLOCSA(&dst_address, sproto, spi);
207105197Ssam	if (sav == NULL) {
208120585Ssam		DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
209120585Ssam			  __func__, ipsec_address(&dst_address),
210105197Ssam			  (u_long) ntohl(spi), sproto));
211181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_notdb, V_ahstat.ahs_notdb,
212181803Sbz		    V_ipcompstat.ipcomps_notdb);
213105197Ssam		m_freem(m);
214105197Ssam		return ENOENT;
215105197Ssam	}
216105197Ssam
217105197Ssam	if (sav->tdb_xform == NULL) {
218120585Ssam		DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
219120585Ssam			 __func__, ipsec_address(&dst_address),
220105197Ssam			 (u_long) ntohl(spi), sproto));
221181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_noxform, V_ahstat.ahs_noxform,
222181803Sbz		    V_ipcompstat.ipcomps_noxform);
223105197Ssam		KEY_FREESAV(&sav);
224105197Ssam		m_freem(m);
225105197Ssam		return ENXIO;
226105197Ssam	}
227105197Ssam
228105197Ssam	/*
229105197Ssam	 * Call appropriate transform and return -- callback takes care of
230105197Ssam	 * everything else.
231105197Ssam	 */
232105197Ssam	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
233105197Ssam	KEY_FREESAV(&sav);
234105197Ssam	return error;
235105197Ssam}
236105197Ssam
237105197Ssam#ifdef INET
238105197Ssam/*
239105197Ssam * Common input handler for IPv4 AH, ESP, and IPCOMP.
240105197Ssam */
241105197Ssamint
242105197Ssamipsec4_common_input(struct mbuf *m, ...)
243105197Ssam{
244105197Ssam	va_list ap;
245105197Ssam	int off, nxt;
246105197Ssam
247105197Ssam	va_start(ap, m);
248105197Ssam	off = va_arg(ap, int);
249105197Ssam	nxt = va_arg(ap, int);
250105197Ssam	va_end(ap);
251105197Ssam
252105197Ssam	return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
253105197Ssam				  AF_INET, nxt);
254105197Ssam}
255105197Ssam
256106680Ssamvoid
257106680Ssamah4_input(struct mbuf *m, int off)
258106680Ssam{
259106680Ssam	ipsec4_common_input(m, off, IPPROTO_AH);
260106680Ssam}
261120585Ssamvoid
262120585Ssamah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
263120585Ssam{
264120585Ssam	if (sa->sa_family == AF_INET &&
265120585Ssam	    sa->sa_len == sizeof(struct sockaddr_in))
266120585Ssam		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
267120585Ssam}
268106680Ssam
269106680Ssamvoid
270106680Ssamesp4_input(struct mbuf *m, int off)
271106680Ssam{
272106680Ssam	ipsec4_common_input(m, off, IPPROTO_ESP);
273106680Ssam}
274120585Ssamvoid
275120585Ssamesp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
276120585Ssam{
277120585Ssam	if (sa->sa_family == AF_INET &&
278120585Ssam	    sa->sa_len == sizeof(struct sockaddr_in))
279120585Ssam		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
280120585Ssam}
281106680Ssam
282106680Ssamvoid
283106680Ssamipcomp4_input(struct mbuf *m, int off)
284106680Ssam{
285106680Ssam	ipsec4_common_input(m, off, IPPROTO_IPCOMP);
286106680Ssam}
287106680Ssam
288105197Ssam/*
289105197Ssam * IPsec input callback for INET protocols.
290105197Ssam * This routine is called as the transform callback.
291105197Ssam * Takes care of filtering and other sanity checks on
292105197Ssam * the processed packet.
293105197Ssam */
294105197Ssamint
295105197Ssamipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
296105197Ssam			int skip, int protoff, struct m_tag *mt)
297105197Ssam{
298105197Ssam	int prot, af, sproto;
299105197Ssam	struct ip *ip;
300105197Ssam	struct m_tag *mtag;
301105197Ssam	struct tdb_ident *tdbi;
302105197Ssam	struct secasindex *saidx;
303105197Ssam	int error;
304165222Sbz#ifdef INET6
305165118Sbz#ifdef notyet
306165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
307165118Sbz#endif
308165118Sbz#endif
309105197Ssam
310120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
311120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
312120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
313105197Ssam	saidx = &sav->sah->saidx;
314105197Ssam	af = saidx->dst.sa.sa_family;
315120585Ssam	IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
316105197Ssam	sproto = saidx->proto;
317120585Ssam	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
318105197Ssam		sproto == IPPROTO_IPCOMP,
319120585Ssam		("unexpected security protocol %u", sproto));
320105197Ssam
321105197Ssam	/* Sanity check */
322105197Ssam	if (m == NULL) {
323120585Ssam		DPRINTF(("%s: null mbuf", __func__));
324181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
325181803Sbz		    V_ipcompstat.ipcomps_badkcr);
326105197Ssam		KEY_FREESAV(&sav);
327105197Ssam		return EINVAL;
328105197Ssam	}
329105197Ssam
330105197Ssam	if (skip != 0) {
331105197Ssam		/* Fix IPv4 header */
332105197Ssam		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
333120585Ssam			DPRINTF(("%s: processing failed for SA %s/%08lx\n",
334120585Ssam			    __func__, ipsec_address(&sav->sah->saidx.dst),
335105197Ssam			    (u_long) ntohl(sav->spi)));
336181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
337181803Sbz			    V_ipcompstat.ipcomps_hdrops);
338105197Ssam			error = ENOBUFS;
339105197Ssam			goto bad;
340105197Ssam		}
341105197Ssam
342105197Ssam		ip = mtod(m, struct ip *);
343105197Ssam		ip->ip_len = htons(m->m_pkthdr.len);
344105197Ssam		ip->ip_off = htons(ip->ip_off);
345105197Ssam		ip->ip_sum = 0;
346105197Ssam		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
347105197Ssam	} else {
348105197Ssam		ip = mtod(m, struct ip *);
349105197Ssam	}
350105197Ssam	prot = ip->ip_p;
351105197Ssam
352159215Sgnn#ifdef notyet
353105197Ssam	/* IP-in-IP encapsulation */
354105197Ssam	if (prot == IPPROTO_IPIP) {
355105197Ssam		struct ip ipn;
356105197Ssam
357118888Ssam		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
358181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
359181803Sbz			    V_ahstat.ahs_hdrops,
360181803Sbz			    V_ipcompstat.ipcomps_hdrops);
361118888Ssam			error = EINVAL;
362118888Ssam			goto bad;
363118888Ssam		}
364105197Ssam		/* ipn will now contain the inner IPv4 header */
365105197Ssam		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
366105197Ssam		    (caddr_t) &ipn);
367105197Ssam
368105197Ssam		/* XXX PROXY address isn't recorded in SAH */
369105197Ssam		/*
370105197Ssam		 * Check that the inner source address is the same as
371105197Ssam		 * the proxy address, if available.
372105197Ssam		 */
373105197Ssam		if ((saidx->proxy.sa.sa_family == AF_INET &&
374105197Ssam		    saidx->proxy.sin.sin_addr.s_addr !=
375105197Ssam		    INADDR_ANY &&
376105197Ssam		    ipn.ip_src.s_addr !=
377105197Ssam		    saidx->proxy.sin.sin_addr.s_addr) ||
378105197Ssam		    (saidx->proxy.sa.sa_family != AF_INET &&
379105197Ssam			saidx->proxy.sa.sa_family != 0)) {
380105197Ssam
381120585Ssam			DPRINTF(("%s: inner source address %s doesn't "
382120585Ssam			    "correspond to expected proxy source %s, "
383120585Ssam			    "SA %s/%08lx\n", __func__,
384105197Ssam			    inet_ntoa4(ipn.ip_src),
385105197Ssam			    ipsp_address(saidx->proxy),
386105197Ssam			    ipsp_address(saidx->dst),
387105197Ssam			    (u_long) ntohl(sav->spi)));
388105197Ssam
389181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
390181803Sbz			    V_ahstat.ahs_pdrops,
391181803Sbz			    V_ipcompstat.ipcomps_pdrops);
392105197Ssam			error = EACCES;
393105197Ssam			goto bad;
394105197Ssam		}
395105197Ssam	}
396159237Spjd#ifdef INET6
397105197Ssam	/* IPv6-in-IP encapsulation. */
398105197Ssam	if (prot == IPPROTO_IPV6) {
399105197Ssam		struct ip6_hdr ip6n;
400105197Ssam
401118888Ssam		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
402181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
403181803Sbz			    V_ahstat.ahs_hdrops,
404181803Sbz			    V_ipcompstat.ipcomps_hdrops);
405118888Ssam			error = EINVAL;
406118888Ssam			goto bad;
407118888Ssam		}
408105197Ssam		/* ip6n will now contain the inner IPv6 header. */
409105197Ssam		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
410105197Ssam		    (caddr_t) &ip6n);
411105197Ssam
412105197Ssam		/*
413105197Ssam		 * Check that the inner source address is the same as
414105197Ssam		 * the proxy address, if available.
415105197Ssam		 */
416105197Ssam		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
417105197Ssam		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
418105197Ssam		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
419105197Ssam			&saidx->proxy.sin6.sin6_addr)) ||
420105197Ssam		    (saidx->proxy.sa.sa_family != AF_INET6 &&
421105197Ssam			saidx->proxy.sa.sa_family != 0)) {
422105197Ssam
423120585Ssam			DPRINTF(("%s: inner source address %s doesn't "
424120585Ssam			    "correspond to expected proxy source %s, "
425120585Ssam			    "SA %s/%08lx\n", __func__,
426165118Sbz			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
427105197Ssam			    ipsec_address(&saidx->proxy),
428105197Ssam			    ipsec_address(&saidx->dst),
429105197Ssam			    (u_long) ntohl(sav->spi)));
430105197Ssam
431181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
432181803Sbz			    V_ahstat.ahs_pdrops,
433181803Sbz			    V_ipcompstat.ipcomps_pdrops);
434105197Ssam			error = EACCES;
435105197Ssam			goto bad;
436105197Ssam		}
437105197Ssam	}
438105197Ssam#endif /* INET6 */
439159215Sgnn#endif /*XXX*/
440105197Ssam
441105197Ssam	/*
442105197Ssam	 * Record what we've done to the packet (under what SA it was
443105197Ssam	 * processed). If we've been passed an mtag, it means the packet
444105197Ssam	 * was already processed by an ethernet/crypto combo card and
445105197Ssam	 * thus has a tag attached with all the right information, but
446105197Ssam	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
447105197Ssam	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
448105197Ssam	 */
449105197Ssam	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
450105197Ssam		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
451105197Ssam		    sizeof(struct tdb_ident), M_NOWAIT);
452105197Ssam		if (mtag == NULL) {
453120585Ssam			DPRINTF(("%s: failed to get tag\n", __func__));
454181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
455181803Sbz			    V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
456105197Ssam			error = ENOMEM;
457105197Ssam			goto bad;
458105197Ssam		}
459105197Ssam
460105197Ssam		tdbi = (struct tdb_ident *)(mtag + 1);
461105197Ssam		bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
462105197Ssam		tdbi->proto = sproto;
463105197Ssam		tdbi->spi = sav->spi;
464174054Sbz		/* Cache those two for enc(4) in xform_ipip. */
465174054Sbz		tdbi->alg_auth = sav->alg_auth;
466174054Sbz		tdbi->alg_enc = sav->alg_enc;
467105197Ssam
468105197Ssam		m_tag_prepend(m, mtag);
469170797Sbz	} else if (mt != NULL) {
470105197Ssam		mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
471105197Ssam		/* XXX do we need to mark m_flags??? */
472105197Ssam	}
473105197Ssam
474105197Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
475105197Ssam
476159965Sthompsa#ifdef DEV_ENC
477181627Svanhu	encif->if_ipackets++;
478181627Svanhu	encif->if_ibytes += m->m_pkthdr.len;
479181627Svanhu
480105197Ssam	/*
481159965Sthompsa	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
482159965Sthompsa	 * packet later after it has been decapsulated.
483159965Sthompsa	 */
484174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
485159965Sthompsa
486159965Sthompsa	if (prot != IPPROTO_IPIP)
487174054Sbz		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
488159965Sthompsa			return (error);
489159965Sthompsa#endif
490159965Sthompsa
491159965Sthompsa	/*
492105197Ssam	 * Re-dispatch via software interrupt.
493105197Ssam	 */
494208508Sbz	if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) {
495181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_qfull, V_ahstat.ahs_qfull,
496181803Sbz			    V_ipcompstat.ipcomps_qfull);
497105197Ssam
498120585Ssam		DPRINTF(("%s: queue full; proto %u packet dropped\n",
499120585Ssam			__func__, sproto));
500134391Sandre		return error;
501105197Ssam	}
502105197Ssam	return 0;
503105197Ssambad:
504105197Ssam	m_freem(m);
505105197Ssam	return error;
506105197Ssam}
507120585Ssam
508120585Ssamvoid
509120585Ssamipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
510120585Ssam{
511120585Ssam	/* XXX nothing just yet */
512120585Ssam}
513105197Ssam#endif /* INET */
514105197Ssam
515105197Ssam#ifdef INET6
516105197Ssam/* IPv6 AH wrapper. */
517105197Ssamint
518105197Ssamipsec6_common_input(struct mbuf **mp, int *offp, int proto)
519105197Ssam{
520105197Ssam	int l = 0;
521105197Ssam	int protoff;
522105197Ssam	struct ip6_ext ip6e;
523105197Ssam
524105197Ssam	if (*offp < sizeof(struct ip6_hdr)) {
525120585Ssam		DPRINTF(("%s: bad offset %u\n", __func__, *offp));
526105197Ssam		return IPPROTO_DONE;
527105197Ssam	} else if (*offp == sizeof(struct ip6_hdr)) {
528105197Ssam		protoff = offsetof(struct ip6_hdr, ip6_nxt);
529105197Ssam	} else {
530105197Ssam		/* Chase down the header chain... */
531105197Ssam		protoff = sizeof(struct ip6_hdr);
532105197Ssam
533105197Ssam		do {
534105197Ssam			protoff += l;
535105197Ssam			m_copydata(*mp, protoff, sizeof(ip6e),
536105197Ssam			    (caddr_t) &ip6e);
537105197Ssam
538105197Ssam			if (ip6e.ip6e_nxt == IPPROTO_AH)
539105197Ssam				l = (ip6e.ip6e_len + 2) << 2;
540105197Ssam			else
541105197Ssam				l = (ip6e.ip6e_len + 1) << 3;
542120585Ssam			IPSEC_ASSERT(l > 0, ("l went zero or negative"));
543105197Ssam		} while (protoff + l < *offp);
544105197Ssam
545105197Ssam		/* Malformed packet check */
546105197Ssam		if (protoff + l != *offp) {
547120585Ssam			DPRINTF(("%s: bad packet header chain, protoff %u, "
548120585Ssam				"l %u, off %u\n", __func__, protoff, l, *offp));
549181803Sbz			IPSEC_ISTAT(proto, V_espstat.esps_hdrops,
550181803Sbz				    V_ahstat.ahs_hdrops,
551181803Sbz				    V_ipcompstat.ipcomps_hdrops);
552105197Ssam			m_freem(*mp);
553105197Ssam			*mp = NULL;
554105197Ssam			return IPPROTO_DONE;
555105197Ssam		}
556105197Ssam		protoff += offsetof(struct ip6_ext, ip6e_nxt);
557105197Ssam	}
558105197Ssam	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
559105197Ssam	return IPPROTO_DONE;
560105197Ssam}
561105197Ssam
562105197Ssam/*
563105197Ssam * IPsec input callback, called by the transform callback. Takes care of
564105197Ssam * filtering and other sanity checks on the processed packet.
565105197Ssam */
566105197Ssamint
567105197Ssamipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
568105197Ssam    struct m_tag *mt)
569105197Ssam{
570105197Ssam	int prot, af, sproto;
571105197Ssam	struct ip6_hdr *ip6;
572105197Ssam	struct m_tag *mtag;
573105197Ssam	struct tdb_ident *tdbi;
574105197Ssam	struct secasindex *saidx;
575105197Ssam	int nxt;
576105197Ssam	u_int8_t nxt8;
577105197Ssam	int error, nest;
578165118Sbz#ifdef notyet
579165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
580165118Sbz#endif
581105197Ssam
582120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
583120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
584120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
585105197Ssam	saidx = &sav->sah->saidx;
586105197Ssam	af = saidx->dst.sa.sa_family;
587120585Ssam	IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
588105197Ssam	sproto = saidx->proto;
589120585Ssam	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
590105197Ssam		sproto == IPPROTO_IPCOMP,
591120585Ssam		("unexpected security protocol %u", sproto));
592105197Ssam
593105197Ssam	/* Sanity check */
594105197Ssam	if (m == NULL) {
595120585Ssam		DPRINTF(("%s: null mbuf", __func__));
596181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
597181803Sbz		    V_ipcompstat.ipcomps_badkcr);
598105197Ssam		error = EINVAL;
599105197Ssam		goto bad;
600105197Ssam	}
601105197Ssam
602105197Ssam	/* Fix IPv6 header */
603105197Ssam	if (m->m_len < sizeof(struct ip6_hdr) &&
604105197Ssam	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
605105197Ssam
606120585Ssam		DPRINTF(("%s: processing failed for SA %s/%08lx\n",
607120585Ssam		    __func__, ipsec_address(&sav->sah->saidx.dst),
608105197Ssam		    (u_long) ntohl(sav->spi)));
609105197Ssam
610181803Sbz		IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
611181803Sbz		    V_ipcompstat.ipcomps_hdrops);
612105197Ssam		error = EACCES;
613105197Ssam		goto bad;
614105197Ssam	}
615105197Ssam
616105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
617105197Ssam	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
618105197Ssam
619105197Ssam	/* Save protocol */
620105197Ssam	m_copydata(m, protoff, 1, (unsigned char *) &prot);
621105197Ssam
622159215Sgnn#ifdef notyet
623105197Ssam#ifdef INET
624105197Ssam	/* IP-in-IP encapsulation */
625105197Ssam	if (prot == IPPROTO_IPIP) {
626105197Ssam		struct ip ipn;
627105197Ssam
628118888Ssam		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
629181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
630181803Sbz			    V_ahstat.ahs_hdrops,
631181803Sbz			    V_ipcompstat.ipcomps_hdrops);
632118888Ssam			error = EINVAL;
633118888Ssam			goto bad;
634118888Ssam		}
635105197Ssam		/* ipn will now contain the inner IPv4 header */
636105197Ssam		m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
637105197Ssam
638105197Ssam		/*
639105197Ssam		 * Check that the inner source address is the same as
640105197Ssam		 * the proxy address, if available.
641105197Ssam		 */
642105197Ssam		if ((saidx->proxy.sa.sa_family == AF_INET &&
643105197Ssam		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
644105197Ssam		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
645105197Ssam		    (saidx->proxy.sa.sa_family != AF_INET &&
646105197Ssam			saidx->proxy.sa.sa_family != 0)) {
647105197Ssam
648120585Ssam			DPRINTF(("%s: inner source address %s doesn't "
649120585Ssam			    "correspond to expected proxy source %s, "
650120585Ssam			    "SA %s/%08lx\n", __func__,
651105197Ssam			    inet_ntoa4(ipn.ip_src),
652105197Ssam			    ipsec_address(&saidx->proxy),
653105197Ssam			    ipsec_address(&saidx->dst),
654105197Ssam			    (u_long) ntohl(sav->spi)));
655105197Ssam
656181803Sbz			IPSEC_ISTATsproto, (V_espstat.esps_pdrops,
657181803Sbz			    V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
658105197Ssam			error = EACCES;
659105197Ssam			goto bad;
660105197Ssam		}
661105197Ssam	}
662105197Ssam#endif /* INET */
663105197Ssam
664105197Ssam	/* IPv6-in-IP encapsulation */
665105197Ssam	if (prot == IPPROTO_IPV6) {
666105197Ssam		struct ip6_hdr ip6n;
667105197Ssam
668118888Ssam		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
669181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
670181803Sbz			    V_ahstat.ahs_hdrops,
671181803Sbz			    V_ipcompstat.ipcomps_hdrops);
672118888Ssam			error = EINVAL;
673118888Ssam			goto bad;
674118888Ssam		}
675105197Ssam		/* ip6n will now contain the inner IPv6 header. */
676105197Ssam		m_copydata(m, skip, sizeof(struct ip6_hdr),
677105197Ssam		    (caddr_t) &ip6n);
678105197Ssam
679105197Ssam		/*
680105197Ssam		 * Check that the inner source address is the same as
681105197Ssam		 * the proxy address, if available.
682105197Ssam		 */
683105197Ssam		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
684105197Ssam		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
685105197Ssam		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
686105197Ssam			&saidx->proxy.sin6.sin6_addr)) ||
687105197Ssam		    (saidx->proxy.sa.sa_family != AF_INET6 &&
688105197Ssam			saidx->proxy.sa.sa_family != 0)) {
689105197Ssam
690120585Ssam			DPRINTF(("%s: inner source address %s doesn't "
691120585Ssam			    "correspond to expected proxy source %s, "
692120585Ssam			    "SA %s/%08lx\n", __func__,
693165118Sbz			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
694105197Ssam			    ipsec_address(&saidx->proxy),
695105197Ssam			    ipsec_address(&saidx->dst),
696105197Ssam			    (u_long) ntohl(sav->spi)));
697105197Ssam
698181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
699181803Sbz			    V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
700105197Ssam			error = EACCES;
701105197Ssam			goto bad;
702105197Ssam		}
703159215Sgnn	}
704105197Ssam#endif /*XXX*/
705105197Ssam
706105197Ssam	/*
707105197Ssam	 * Record what we've done to the packet (under what SA it was
708105197Ssam	 * processed). If we've been passed an mtag, it means the packet
709105197Ssam	 * was already processed by an ethernet/crypto combo card and
710105197Ssam	 * thus has a tag attached with all the right information, but
711105197Ssam	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
712105197Ssam	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
713105197Ssam	 */
714105197Ssam	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
715105197Ssam		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
716105197Ssam		    sizeof(struct tdb_ident), M_NOWAIT);
717105197Ssam		if (mtag == NULL) {
718120585Ssam			DPRINTF(("%s: failed to get tag\n", __func__));
719181803Sbz			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
720181803Sbz			    V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
721105197Ssam			error = ENOMEM;
722105197Ssam			goto bad;
723105197Ssam		}
724105197Ssam
725105197Ssam		tdbi = (struct tdb_ident *)(mtag + 1);
726105197Ssam		bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
727105197Ssam		tdbi->proto = sproto;
728105197Ssam		tdbi->spi = sav->spi;
729174054Sbz		/* Cache those two for enc(4) in xform_ipip. */
730174054Sbz		tdbi->alg_auth = sav->alg_auth;
731174054Sbz		tdbi->alg_enc = sav->alg_enc;
732105197Ssam
733105197Ssam		m_tag_prepend(m, mtag);
734105197Ssam	} else {
735120585Ssam		if (mt != NULL)
736120585Ssam			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
737105197Ssam		/* XXX do we need to mark m_flags??? */
738105197Ssam	}
739105197Ssam
740105197Ssam	key_sa_recordxfer(sav, m);
741105197Ssam
742174054Sbz#ifdef DEV_ENC
743181627Svanhu	encif->if_ipackets++;
744181627Svanhu	encif->if_ibytes += m->m_pkthdr.len;
745181627Svanhu
746174054Sbz	/*
747174054Sbz	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
748174054Sbz	 * packet later after it has been decapsulated.
749174054Sbz	 */
750174054Sbz	ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
751174054Sbz
752174054Sbz	/* XXX-BZ does not make sense. */
753174054Sbz	if (prot != IPPROTO_IPIP)
754174054Sbz		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
755174054Sbz			return (error);
756174054Sbz#endif
757174054Sbz
758105197Ssam	/* Retrieve new protocol */
759105197Ssam	m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
760105197Ssam
761105197Ssam	/*
762105197Ssam	 * See the end of ip6_input for this logic.
763105197Ssam	 * IPPROTO_IPV[46] case will be processed just like other ones
764105197Ssam	 */
765105197Ssam	nest = 0;
766105197Ssam	nxt = nxt8;
767105197Ssam	while (nxt != IPPROTO_DONE) {
768181803Sbz		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
769181803Sbz			V_ip6stat.ip6s_toomanyhdr++;
770105197Ssam			error = EINVAL;
771105197Ssam			goto bad;
772105197Ssam		}
773105197Ssam
774105197Ssam		/*
775105197Ssam		 * Protection against faulty packet - there should be
776105197Ssam		 * more sanity checks in header chain processing.
777105197Ssam		 */
778105197Ssam		if (m->m_pkthdr.len < skip) {
779181803Sbz			V_ip6stat.ip6s_tooshort++;
780105197Ssam			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
781105197Ssam			error = EINVAL;
782105197Ssam			goto bad;
783105197Ssam		}
784105197Ssam		/*
785105197Ssam		 * Enforce IPsec policy checking if we are seeing last header.
786105197Ssam		 * note that we do not visit this with protocols with pcb layer
787105197Ssam		 * code - like udp/tcp/raw ip.
788105197Ssam		 */
789105197Ssam		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
790105197Ssam		    ipsec6_in_reject(m, NULL)) {
791105197Ssam			error = EINVAL;
792105197Ssam			goto bad;
793105197Ssam		}
794105197Ssam		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
795105197Ssam	}
796105197Ssam	return 0;
797105197Ssambad:
798105197Ssam	if (m)
799105197Ssam		m_freem(m);
800105197Ssam	return error;
801105197Ssam}
802120585Ssam
803120585Ssamvoid
804120585Ssamesp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
805120585Ssam{
806172149Sgnn	struct ip6ctlparam *ip6cp = NULL;
807172149Sgnn	struct mbuf *m = NULL;
808172149Sgnn	struct ip6_hdr *ip6;
809172149Sgnn	int off;
810172149Sgnn
811120585Ssam	if (sa->sa_family != AF_INET6 ||
812120585Ssam	    sa->sa_len != sizeof(struct sockaddr_in6))
813120585Ssam		return;
814120585Ssam	if ((unsigned)cmd >= PRC_NCMDS)
815120585Ssam		return;
816120585Ssam
817120585Ssam	/* if the parameter is from icmp6, decode it. */
818172149Sgnn	if (d != NULL) {
819172149Sgnn		ip6cp = (struct ip6ctlparam *)d;
820172149Sgnn		m = ip6cp->ip6c_m;
821172149Sgnn		ip6 = ip6cp->ip6c_ip6;
822172149Sgnn		off = ip6cp->ip6c_off;
823172149Sgnn	} else {
824172149Sgnn		m = NULL;
825172149Sgnn		ip6 = NULL;
826172149Sgnn		off = 0;	/* calm gcc */
827172149Sgnn	}
828120585Ssam
829172149Sgnn	if (ip6 != NULL) {
830172149Sgnn
831120585Ssam		struct ip6ctlparam ip6cp1;
832120585Ssam
833120585Ssam		/*
834120585Ssam		 * Notify the error to all possible sockets via pfctlinput2.
835120585Ssam		 * Since the upper layer information (such as protocol type,
836120585Ssam		 * source and destination ports) is embedded in the encrypted
837120585Ssam		 * data and might have been cut, we can't directly call
838120585Ssam		 * an upper layer ctlinput function. However, the pcbnotify
839120585Ssam		 * function will consider source and destination addresses
840120585Ssam		 * as well as the flow info value, and may be able to find
841120585Ssam		 * some PCB that should be notified.
842120585Ssam		 * Although pfctlinput2 will call esp6_ctlinput(), there is
843120585Ssam		 * no possibility of an infinite loop of function calls,
844120585Ssam		 * because we don't pass the inner IPv6 header.
845120585Ssam		 */
846120585Ssam		bzero(&ip6cp1, sizeof(ip6cp1));
847120585Ssam		ip6cp1.ip6c_src = ip6cp->ip6c_src;
848120585Ssam		pfctlinput2(cmd, sa, (void *)&ip6cp1);
849120585Ssam
850120585Ssam		/*
851120585Ssam		 * Then go to special cases that need ESP header information.
852120585Ssam		 * XXX: We assume that when ip6 is non NULL,
853120585Ssam		 * M and OFF are valid.
854120585Ssam		 */
855120585Ssam
856120585Ssam		if (cmd == PRC_MSGSIZE) {
857120585Ssam			struct secasvar *sav;
858120585Ssam			u_int32_t spi;
859120585Ssam			int valid;
860120585Ssam
861120585Ssam			/* check header length before using m_copydata */
862120585Ssam			if (m->m_pkthdr.len < off + sizeof (struct esp))
863120585Ssam				return;
864120585Ssam			m_copydata(m, off + offsetof(struct esp, esp_spi),
865120585Ssam				sizeof(u_int32_t), (caddr_t) &spi);
866120585Ssam			/*
867120585Ssam			 * Check to see if we have a valid SA corresponding to
868120585Ssam			 * the address in the ICMP message payload.
869120585Ssam			 */
870120585Ssam			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
871120585Ssam					IPPROTO_ESP, spi);
872120585Ssam			valid = (sav != NULL);
873120585Ssam			if (sav)
874120585Ssam				KEY_FREESAV(&sav);
875120585Ssam
876120585Ssam			/* XXX Further validation? */
877120585Ssam
878120585Ssam			/*
879120585Ssam			 * Depending on whether the SA is "valid" and
880120585Ssam			 * routing table size (mtudisc_{hi,lo}wat), we will:
881120585Ssam			 * - recalcurate the new MTU and create the
882120585Ssam			 *   corresponding routing entry, or
883120585Ssam			 * - ignore the MTU change notification.
884120585Ssam			 */
885120585Ssam			icmp6_mtudisc_update(ip6cp, valid);
886120585Ssam		}
887120585Ssam	} else {
888120585Ssam		/* we normally notify any pcb here */
889120585Ssam	}
890120585Ssam}
891105197Ssam#endif /* INET6 */
892