ip6_ipsec.c revision 238877
1171133Sgnn/*-
2171133Sgnn * Copyright (c) 1982, 1986, 1988, 1993
3171133Sgnn *      The Regents of the University of California.  All rights reserved.
4171133Sgnn *
5171133Sgnn * Redistribution and use in source and binary forms, with or without
6171133Sgnn * modification, are permitted provided that the following conditions
7171133Sgnn * are met:
8171133Sgnn * 1. Redistributions of source code must retain the above copyright
9171133Sgnn *    notice, this list of conditions and the following disclaimer.
10171133Sgnn * 2. Redistributions in binary form must reproduce the above copyright
11171133Sgnn *    notice, this list of conditions and the following disclaimer in the
12171133Sgnn *    documentation and/or other materials provided with the distribution.
13171133Sgnn * 4. Neither the name of the University nor the names of its contributors
14171133Sgnn *    may be used to endorse or promote products derived from this software
15171133Sgnn *    without specific prior written permission.
16171133Sgnn *
17171133Sgnn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18171133Sgnn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19171133Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20171133Sgnn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21171133Sgnn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22171133Sgnn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23171133Sgnn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24171133Sgnn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25171133Sgnn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26171133Sgnn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27171133Sgnn * SUCH DAMAGE.
28171133Sgnn */
29171133Sgnn
30174510Sobrien#include <sys/cdefs.h>
31174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/ip6_ipsec.c 238877 2012-07-29 00:44:41Z bz $");
32174510Sobrien
33221129Sbz#include "opt_inet.h"
34195699Srwatson#include "opt_inet6.h"
35171133Sgnn#include "opt_ipsec.h"
36171133Sgnn
37171133Sgnn#include <sys/param.h>
38171133Sgnn#include <sys/systm.h>
39171133Sgnn#include <sys/kernel.h>
40171133Sgnn#include <sys/mac.h>
41171133Sgnn#include <sys/malloc.h>
42171133Sgnn#include <sys/mbuf.h>
43171133Sgnn#include <sys/protosw.h>
44171133Sgnn#include <sys/socket.h>
45171133Sgnn#include <sys/socketvar.h>
46171133Sgnn#include <sys/sysctl.h>
47221129Sbz#include <sys/syslog.h>
48171133Sgnn
49171133Sgnn#include <net/if.h>
50171133Sgnn#include <net/route.h>
51195699Srwatson#include <net/vnet.h>
52171133Sgnn
53171133Sgnn#include <netinet/in.h>
54171133Sgnn#include <netinet/in_systm.h>
55171133Sgnn#include <netinet/in_var.h>
56171133Sgnn#include <netinet/ip.h>
57180085Sjulian#include <netinet/ip6.h>
58171133Sgnn#include <netinet/in_pcb.h>
59171133Sgnn#include <netinet/ip_var.h>
60171133Sgnn#include <netinet/ip_options.h>
61171133Sgnn
62171133Sgnn#include <machine/in_cksum.h>
63171133Sgnn
64171167Sgnn#ifdef IPSEC
65171133Sgnn#include <netipsec/ipsec.h>
66171133Sgnn#include <netipsec/ipsec6.h>
67171133Sgnn#include <netipsec/xform.h>
68171133Sgnn#include <netipsec/key.h>
69171133Sgnn#ifdef IPSEC_DEBUG
70171133Sgnn#include <netipsec/key_debug.h>
71171133Sgnn#else
72171133Sgnn#define	KEYDEBUG(lev,arg)
73171133Sgnn#endif
74171167Sgnn#endif /*IPSEC*/
75171133Sgnn
76171133Sgnn#include <netinet6/ip6_ipsec.h>
77180090Sbz#include <netinet6/ip6_var.h>
78171133Sgnn
79171133Sgnnextern	struct protosw inet6sw[];
80171133Sgnn
81195699Srwatson
82195699Srwatson#ifdef INET6
83195699Srwatson#ifdef IPSEC
84195699Srwatson#ifdef IPSEC_FILTERTUNNEL
85215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1;
86195699Srwatson#else
87215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0;
88192648Sbz#endif
89195727Srwatson#define	V_ip6_ipsec6_filtertunnel	VNET(ip6_ipsec6_filtertunnel)
90192648Sbz
91195699SrwatsonSYSCTL_DECL(_net_inet6_ipsec6);
92195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ipsec6, OID_AUTO,
93195699Srwatson	filtertunnel, CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel),  0,
94195699Srwatson	"If set filter packets from an IPsec tunnel.");
95195699Srwatson#endif /* IPSEC */
96195699Srwatson#endif /* INET6 */
97195699Srwatson
98171133Sgnn/*
99171133Sgnn * Check if we have to jump over firewall processing for this packet.
100222845Sbz * Called from ip6_input().
101171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall.
102171133Sgnn */
103171133Sgnnint
104171732Sbzip6_ipsec_filtertunnel(struct mbuf *m)
105171133Sgnn{
106230442Sbz#ifdef IPSEC
107192648Sbz
108171133Sgnn	/*
109222845Sbz	 * Bypass packet filtering for packets previously handled by IPsec.
110171133Sgnn	 */
111192648Sbz	if (!V_ip6_ipsec6_filtertunnel &&
112192648Sbz	    m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
113171133Sgnn		return 1;
114171133Sgnn#endif
115171133Sgnn	return 0;
116171133Sgnn}
117171133Sgnn
118171133Sgnn/*
119171133Sgnn * Check if this packet has an active SA and needs to be dropped instead
120171133Sgnn * of forwarded.
121222845Sbz * Called from ip6_input().
122171133Sgnn * 1 = drop packet, 0 = forward packet.
123171133Sgnn */
124171133Sgnnint
125171133Sgnnip6_ipsec_fwd(struct mbuf *m)
126171133Sgnn{
127171167Sgnn#ifdef IPSEC
128171133Sgnn	struct m_tag *mtag;
129171133Sgnn	struct tdb_ident *tdbi;
130171133Sgnn	struct secpolicy *sp;
131171133Sgnn	int s, error;
132171133Sgnn	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
133171133Sgnn	s = splnet();
134171133Sgnn	if (mtag != NULL) {
135171133Sgnn		tdbi = (struct tdb_ident *)(mtag + 1);
136171133Sgnn		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
137171133Sgnn	} else {
138171133Sgnn		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
139171260Sdelphij					   IP_FORWARDING, &error);
140171133Sgnn	}
141171133Sgnn	if (sp == NULL) {	/* NB: can happen if error */
142171133Sgnn		splx(s);
143171133Sgnn		/*XXX error stat???*/
144222845Sbz		DPRINTF(("%s: no SP for forwarding\n", __func__));	/*XXX*/
145171133Sgnn		return 1;
146171133Sgnn	}
147171133Sgnn
148171133Sgnn	/*
149171133Sgnn	 * Check security policy against packet attributes.
150171133Sgnn	 */
151171133Sgnn	error = ipsec_in_reject(sp, m);
152171133Sgnn	KEY_FREESP(&sp);
153171133Sgnn	splx(s);
154171133Sgnn	if (error) {
155181803Sbz		V_ip6stat.ip6s_cantforward++;
156171133Sgnn		return 1;
157171133Sgnn	}
158171167Sgnn#endif /* IPSEC */
159171133Sgnn	return 0;
160171133Sgnn}
161171133Sgnn
162171133Sgnn/*
163171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC
164171133Sgnn * decryption or reject right now.  Protocols with further headers get
165171133Sgnn * their IPSEC treatment within the protocol specific processing.
166222845Sbz * Called from ip6_input().
167171133Sgnn * 1 = drop packet, 0 = continue processing packet.
168171133Sgnn */
169171133Sgnnint
170171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt)
171171133Sgnn{
172171167Sgnn#ifdef IPSEC
173171133Sgnn	struct m_tag *mtag;
174171133Sgnn	struct tdb_ident *tdbi;
175171133Sgnn	struct secpolicy *sp;
176171133Sgnn	int s, error;
177171133Sgnn	/*
178171133Sgnn	 * enforce IPsec policy checking if we are seeing last header.
179171133Sgnn	 * note that we do not visit this with protocols with pcb layer
180171133Sgnn	 * code - like udp/tcp/raw ip.
181171133Sgnn	 */
182171133Sgnn	if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
183171133Sgnn	    ipsec6_in_reject(m, NULL)) {
184171133Sgnn
185171133Sgnn		/*
186171133Sgnn		 * Check if the packet has already had IPsec processing
187171133Sgnn		 * done.  If so, then just pass it along.  This tag gets
188171133Sgnn		 * set during AH, ESP, etc. input handling, before the
189171133Sgnn		 * packet is returned to the ip input queue for delivery.
190171260Sdelphij		 */
191171133Sgnn		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
192171133Sgnn		s = splnet();
193171133Sgnn		if (mtag != NULL) {
194171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
195171133Sgnn			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
196171133Sgnn		} else {
197171133Sgnn			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
198171260Sdelphij						   IP_FORWARDING, &error);
199171133Sgnn		}
200171133Sgnn		if (sp != NULL) {
201171133Sgnn			/*
202171133Sgnn			 * Check security policy against packet attributes.
203171133Sgnn			 */
204171133Sgnn			error = ipsec_in_reject(sp, m);
205171133Sgnn			KEY_FREESP(&sp);
206171133Sgnn		} else {
207171133Sgnn			/* XXX error stat??? */
208171133Sgnn			error = EINVAL;
209222845Sbz			DPRINTF(("%s: no SP, packet discarded\n", __func__));/*XXX*/
210171133Sgnn			return 1;
211171133Sgnn		}
212171133Sgnn		splx(s);
213171133Sgnn		if (error)
214171133Sgnn			return 1;
215171133Sgnn	}
216171167Sgnn#endif /* IPSEC */
217171133Sgnn	return 0;
218171133Sgnn}
219171133Sgnn
220171133Sgnn/*
221171133Sgnn * Called from ip6_output().
222171133Sgnn * 1 = drop packet, 0 = continue processing packet,
223171260Sdelphij * -1 = packet was reinjected and stop processing packet
224171260Sdelphij */
225171133Sgnn
226171133Sgnnint
227171133Sgnnip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
228171260Sdelphij    struct ifnet **ifp, struct secpolicy **sp)
229171133Sgnn{
230171167Sgnn#ifdef IPSEC
231171133Sgnn	struct tdb_ident *tdbi;
232171133Sgnn	struct m_tag *mtag;
233171237Speter	/* XXX int s; */
234171133Sgnn	if (sp == NULL)
235171133Sgnn		return 1;
236171133Sgnn	mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
237171133Sgnn	if (mtag != NULL) {
238171133Sgnn		tdbi = (struct tdb_ident *)(mtag + 1);
239171133Sgnn		*sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
240171133Sgnn		if (*sp == NULL)
241171133Sgnn			*error = -EINVAL;	/* force silent drop */
242171133Sgnn		m_tag_delete(*m, mtag);
243171133Sgnn	} else {
244171133Sgnn		*sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
245171133Sgnn					error, inp);
246171133Sgnn	}
247171133Sgnn
248171133Sgnn	/*
249171133Sgnn	 * There are four return cases:
250171260Sdelphij	 *    sp != NULL		    apply IPsec policy
251171133Sgnn	 *    sp == NULL, error == 0	    no IPsec handling needed
252171133Sgnn	 *    sp == NULL, error == -EINVAL  discard packet w/o error
253171133Sgnn	 *    sp == NULL, error != 0	    discard packet, report error
254171133Sgnn	 */
255171133Sgnn	if (*sp != NULL) {
256171133Sgnn		/* Loop detection, check if ipsec processing already done */
257171133Sgnn		KASSERT((*sp)->req != NULL, ("ip_output: no ipsec request"));
258171133Sgnn		for (mtag = m_tag_first(*m); mtag != NULL;
259171133Sgnn		     mtag = m_tag_next(*m, mtag)) {
260171133Sgnn			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
261171133Sgnn				continue;
262171133Sgnn			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
263171133Sgnn			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
264171133Sgnn				continue;
265171133Sgnn			/*
266238877Sbz			 * Check if policy has no SA associated with it.
267171133Sgnn			 * This can happen when an SP has yet to acquire
268171133Sgnn			 * an SA; e.g. on first reference.  If it occurs,
269171133Sgnn			 * then we let ipsec4_process_packet do its thing.
270171133Sgnn			 */
271171133Sgnn			if ((*sp)->req->sav == NULL)
272171133Sgnn				break;
273171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
274171133Sgnn			if (tdbi->spi == (*sp)->req->sav->spi &&
275171133Sgnn			    tdbi->proto == (*sp)->req->sav->sah->saidx.proto &&
276171133Sgnn			    bcmp(&tdbi->dst, &(*sp)->req->sav->sah->saidx.dst,
277171133Sgnn				 sizeof (union sockaddr_union)) == 0) {
278171133Sgnn				/*
279171133Sgnn				 * No IPsec processing is needed, free
280171133Sgnn				 * reference to SP.
281171133Sgnn				 *
282171133Sgnn				 * NB: null pointer to avoid free at
283171133Sgnn				 *     done: below.
284171133Sgnn				 */
285177167Sbz				KEY_FREESP(sp), *sp = NULL;
286171237Speter				/* XXX splx(s); */
287171133Sgnn				goto done;
288171133Sgnn			}
289171133Sgnn		}
290171133Sgnn
291171133Sgnn		/*
292171133Sgnn		 * Do delayed checksums now because we send before
293171133Sgnn		 * this is done in the normal processing path.
294236170Sbz		 * XXX-BZ CSUM_DELAY_DATA_IPV6?
295171133Sgnn		 */
296171133Sgnn		if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
297221129Sbz			ipseclog((LOG_DEBUG,
298221129Sbz			    "%s: we do not support IPv4 over IPv6", __func__));
299221129Sbz#ifdef INET
300171133Sgnn			in_delayed_cksum(*m);
301221129Sbz#endif
302171133Sgnn			(*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
303171133Sgnn		}
304171133Sgnn
305171133Sgnn		/*
306171133Sgnn		 * Preserve KAME behaviour: ENOENT can be returned
307171133Sgnn		 * when an SA acquire is in progress.  Don't propagate
308171133Sgnn		 * this to user-level; it confuses applications.
309171133Sgnn		 *
310171133Sgnn		 * XXX this will go away when the SADB is redone.
311171133Sgnn		 */
312171133Sgnn		if (*error == ENOENT)
313171133Sgnn			*error = 0;
314171133Sgnn		goto do_ipsec;
315171133Sgnn	} else {	/* sp == NULL */
316171133Sgnn		if (*error != 0) {
317171133Sgnn			/*
318171133Sgnn			 * Hack: -EINVAL is used to signal that a packet
319171133Sgnn			 * should be silently discarded.  This is typically
320171133Sgnn			 * because we asked key management for an SA and
321171133Sgnn			 * it was delayed (e.g. kicked up to IKE).
322171133Sgnn			 */
323171133Sgnn			if (*error == -EINVAL)
324171133Sgnn				*error = 0;
325171133Sgnn			goto bad;
326171133Sgnn		} else {
327171133Sgnn			/* No IPsec processing for this packet. */
328171133Sgnn		}
329171133Sgnn	}
330171133Sgnndone:
331171133Sgnn	return 0;
332171133Sgnndo_ipsec:
333171133Sgnn	return -1;
334171133Sgnnbad:
335171133Sgnn	return 1;
336171167Sgnn#endif /* IPSEC */
337171133Sgnn	return 0;
338171133Sgnn}
339171133Sgnn
340177166Sbz#if 0
341171133Sgnn/*
342171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
343171133Sgnn * Called from ip_forward().
344171133Sgnn * Returns MTU suggestion for ICMP needfrag reply.
345171133Sgnn */
346171133Sgnnint
347171133Sgnnip6_ipsec_mtu(struct mbuf *m)
348171133Sgnn{
349171133Sgnn	int mtu = 0;
350171133Sgnn	/*
351171133Sgnn	 * If the packet is routed over IPsec tunnel, tell the
352171133Sgnn	 * originator the tunnel MTU.
353171133Sgnn	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
354171133Sgnn	 * XXX quickhack!!!
355171133Sgnn	 */
356177166Sbz#ifdef IPSEC
357171133Sgnn	struct secpolicy *sp = NULL;
358171133Sgnn	int ipsecerror;
359171133Sgnn	int ipsechdr;
360171133Sgnn	struct route *ro;
361171133Sgnn	sp = ipsec_getpolicybyaddr(m,
362171133Sgnn				   IPSEC_DIR_OUTBOUND,
363171133Sgnn				   IP_FORWARDING,
364171133Sgnn				   &ipsecerror);
365171133Sgnn	if (sp != NULL) {
366171133Sgnn		/* count IPsec header size */
367188306Sbz		ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
368171133Sgnn
369171133Sgnn		/*
370171133Sgnn		 * find the correct route for outer IPv4
371171133Sgnn		 * header, compute tunnel MTU.
372171133Sgnn		 */
373171133Sgnn		if (sp->req != NULL &&
374171133Sgnn		    sp->req->sav != NULL &&
375171133Sgnn		    sp->req->sav->sah != NULL) {
376214250Sbz			ro = &sp->req->sav->sah->route_cache.sa_route;
377171133Sgnn			if (ro->ro_rt && ro->ro_rt->rt_ifp) {
378171133Sgnn				mtu =
379171133Sgnn				    ro->ro_rt->rt_rmx.rmx_mtu ?
380171133Sgnn				    ro->ro_rt->rt_rmx.rmx_mtu :
381171133Sgnn				    ro->ro_rt->rt_ifp->if_mtu;
382171133Sgnn				mtu -= ipsechdr;
383171133Sgnn			}
384171133Sgnn		}
385171133Sgnn		KEY_FREESP(&sp);
386177166Sbz	}
387171167Sgnn#endif /* IPSEC */
388177166Sbz	/* XXX else case missing. */
389171133Sgnn	return mtu;
390171133Sgnn}
391177166Sbz#endif
392