ip6_ipsec.c revision 171167
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 * $FreeBSD: head/sys/netinet6/ip6_ipsec.c 171167 2007-07-03 12:13:45Z gnn $
30171133Sgnn */
31171133Sgnn
32171133Sgnn#include "opt_ipsec.h"
33171133Sgnn
34171133Sgnn#include <sys/param.h>
35171133Sgnn#include <sys/systm.h>
36171133Sgnn#include <sys/kernel.h>
37171133Sgnn#include <sys/mac.h>
38171133Sgnn#include <sys/malloc.h>
39171133Sgnn#include <sys/mbuf.h>
40171133Sgnn#include <sys/protosw.h>
41171133Sgnn#include <sys/socket.h>
42171133Sgnn#include <sys/socketvar.h>
43171133Sgnn#include <sys/sysctl.h>
44171133Sgnn
45171133Sgnn#include <net/if.h>
46171133Sgnn#include <net/route.h>
47171133Sgnn
48171133Sgnn#include <netinet/in.h>
49171133Sgnn#include <netinet/in_systm.h>
50171133Sgnn#include <netinet/in_var.h>
51171133Sgnn#include <netinet/ip.h>
52171133Sgnn#include <netinet/in_pcb.h>
53171133Sgnn#include <netinet/ip_var.h>
54171133Sgnn#include <netinet/ip_options.h>
55171133Sgnn
56171133Sgnn#include <machine/in_cksum.h>
57171133Sgnn
58171167Sgnn#ifdef IPSEC
59171133Sgnn#include <netipsec/ipsec.h>
60171133Sgnn#include <netipsec/ipsec6.h>
61171133Sgnn#include <netipsec/xform.h>
62171133Sgnn#include <netipsec/key.h>
63171133Sgnn#ifdef IPSEC_DEBUG
64171133Sgnn#include <netipsec/key_debug.h>
65171133Sgnn#else
66171133Sgnn#define	KEYDEBUG(lev,arg)
67171133Sgnn#endif
68171167Sgnn#endif /*IPSEC*/
69171133Sgnn
70171133Sgnn#include <netinet6/ip6_ipsec.h>
71171133Sgnn
72171133Sgnnextern	struct protosw inet6sw[];
73171133Sgnn
74171133Sgnn/*
75171133Sgnn * Check if we have to jump over firewall processing for this packet.
76171133Sgnn * Called from ip_input().
77171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall.
78171133Sgnn */
79171133Sgnnint
80171133Sgnnip6_ipsec_filtergif(struct mbuf *m)
81171133Sgnn{
82171167Sgnn#if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
83171133Sgnn	/*
84171133Sgnn	 * Bypass packet filtering for packets from a tunnel (gif).
85171133Sgnn	 */
86171133Sgnn	if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
87171133Sgnn		return 1;
88171133Sgnn#endif
89171133Sgnn	return 0;
90171133Sgnn}
91171133Sgnn
92171133Sgnn/*
93171133Sgnn * Check if this packet has an active SA and needs to be dropped instead
94171133Sgnn * of forwarded.
95171133Sgnn * Called from ip_input().
96171133Sgnn * 1 = drop packet, 0 = forward packet.
97171133Sgnn */
98171133Sgnnint
99171133Sgnnip6_ipsec_fwd(struct mbuf *m)
100171133Sgnn{
101171167Sgnn#ifdef IPSEC
102171133Sgnn	struct m_tag *mtag;
103171133Sgnn	struct tdb_ident *tdbi;
104171133Sgnn	struct secpolicy *sp;
105171133Sgnn	int s, error;
106171133Sgnn	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
107171133Sgnn	s = splnet();
108171133Sgnn	if (mtag != NULL) {
109171133Sgnn		tdbi = (struct tdb_ident *)(mtag + 1);
110171133Sgnn		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
111171133Sgnn	} else {
112171133Sgnn		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
113171133Sgnn					   IP_FORWARDING, &error);
114171133Sgnn	}
115171133Sgnn	if (sp == NULL) {	/* NB: can happen if error */
116171133Sgnn		splx(s);
117171133Sgnn		/*XXX error stat???*/
118171133Sgnn		DPRINTF(("ip_input: no SP for forwarding\n"));	/*XXX*/
119171133Sgnn		return 1;
120171133Sgnn	}
121171133Sgnn
122171133Sgnn	/*
123171133Sgnn	 * Check security policy against packet attributes.
124171133Sgnn	 */
125171133Sgnn	error = ipsec_in_reject(sp, m);
126171133Sgnn	KEY_FREESP(&sp);
127171133Sgnn	splx(s);
128171133Sgnn	if (error) {
129171133Sgnn		ipstat.ips_cantforward++;
130171133Sgnn		return 1;
131171133Sgnn	}
132171167Sgnn#endif /* IPSEC */
133171133Sgnn	return 0;
134171133Sgnn}
135171133Sgnn
136171133Sgnn/*
137171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC
138171133Sgnn * decryption or reject right now.  Protocols with further headers get
139171133Sgnn * their IPSEC treatment within the protocol specific processing.
140171133Sgnn * Called from ip_input().
141171133Sgnn * 1 = drop packet, 0 = continue processing packet.
142171133Sgnn */
143171133Sgnnint
144171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt)
145171133Sgnn
146171133Sgnn{
147171167Sgnn#ifdef IPSEC
148171133Sgnn	struct m_tag *mtag;
149171133Sgnn	struct tdb_ident *tdbi;
150171133Sgnn	struct secpolicy *sp;
151171133Sgnn	int s, error;
152171133Sgnn	/*
153171133Sgnn	 * enforce IPsec policy checking if we are seeing last header.
154171133Sgnn	 * note that we do not visit this with protocols with pcb layer
155171133Sgnn	 * code - like udp/tcp/raw ip.
156171133Sgnn	 */
157171133Sgnn	if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
158171133Sgnn	    ipsec6_in_reject(m, NULL)) {
159171133Sgnn
160171133Sgnn		/*
161171133Sgnn		 * Check if the packet has already had IPsec processing
162171133Sgnn		 * done.  If so, then just pass it along.  This tag gets
163171133Sgnn		 * set during AH, ESP, etc. input handling, before the
164171133Sgnn		 * packet is returned to the ip input queue for delivery.
165171133Sgnn		 */
166171133Sgnn		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
167171133Sgnn		s = splnet();
168171133Sgnn		if (mtag != NULL) {
169171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
170171133Sgnn			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
171171133Sgnn		} else {
172171133Sgnn			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
173171133Sgnn						   IP_FORWARDING, &error);
174171133Sgnn		}
175171133Sgnn		if (sp != NULL) {
176171133Sgnn			/*
177171133Sgnn			 * Check security policy against packet attributes.
178171133Sgnn			 */
179171133Sgnn			error = ipsec_in_reject(sp, m);
180171133Sgnn			KEY_FREESP(&sp);
181171133Sgnn		} else {
182171133Sgnn			/* XXX error stat??? */
183171133Sgnn			error = EINVAL;
184171133Sgnn			DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
185171133Sgnn			return 1;
186171133Sgnn		}
187171133Sgnn		splx(s);
188171133Sgnn		if (error)
189171133Sgnn			return 1;
190171133Sgnn	}
191171167Sgnn#endif /* IPSEC */
192171133Sgnn	return 0;
193171133Sgnn}
194171133Sgnn
195171133Sgnn/*
196171133Sgnn * Called from ip6_output().
197171133Sgnn * 1 = drop packet, 0 = continue processing packet,
198171167Sgnn * -1 = packet was reinjected and stop processing packet
199171133Sgnn */
200171133Sgnn
201171133Sgnnint
202171133Sgnnip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
203171133Sgnn		 struct ifnet **ifp, struct secpolicy **sp)
204171133Sgnn{
205171167Sgnn#ifdef IPSEC
206171133Sgnn	struct tdb_ident *tdbi;
207171133Sgnn	struct m_tag *mtag;
208171133Sgnn	int s;
209171133Sgnn	if (sp == NULL)
210171133Sgnn		return 1;
211171133Sgnn	mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
212171133Sgnn	if (mtag != NULL) {
213171133Sgnn		tdbi = (struct tdb_ident *)(mtag + 1);
214171133Sgnn		*sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
215171133Sgnn		if (*sp == NULL)
216171133Sgnn			*error = -EINVAL;	/* force silent drop */
217171133Sgnn		m_tag_delete(*m, mtag);
218171133Sgnn	} else {
219171133Sgnn		*sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
220171133Sgnn					error, inp);
221171133Sgnn	}
222171133Sgnn
223171133Sgnn	/*
224171133Sgnn	 * There are four return cases:
225171133Sgnn	 *    sp != NULL	 	    apply IPsec policy
226171133Sgnn	 *    sp == NULL, error == 0	    no IPsec handling needed
227171133Sgnn	 *    sp == NULL, error == -EINVAL  discard packet w/o error
228171133Sgnn	 *    sp == NULL, error != 0	    discard packet, report error
229171133Sgnn	 */
230171133Sgnn	if (*sp != NULL) {
231171133Sgnn		/* Loop detection, check if ipsec processing already done */
232171133Sgnn		KASSERT((*sp)->req != NULL, ("ip_output: no ipsec request"));
233171133Sgnn		for (mtag = m_tag_first(*m); mtag != NULL;
234171133Sgnn		     mtag = m_tag_next(*m, mtag)) {
235171133Sgnn			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
236171133Sgnn				continue;
237171133Sgnn			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
238171133Sgnn			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
239171133Sgnn				continue;
240171133Sgnn			/*
241171133Sgnn			 * Check if policy has an SA associated with it.
242171133Sgnn			 * This can happen when an SP has yet to acquire
243171133Sgnn			 * an SA; e.g. on first reference.  If it occurs,
244171133Sgnn			 * then we let ipsec4_process_packet do its thing.
245171133Sgnn			 */
246171133Sgnn			if ((*sp)->req->sav == NULL)
247171133Sgnn				break;
248171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
249171133Sgnn			if (tdbi->spi == (*sp)->req->sav->spi &&
250171133Sgnn			    tdbi->proto == (*sp)->req->sav->sah->saidx.proto &&
251171133Sgnn			    bcmp(&tdbi->dst, &(*sp)->req->sav->sah->saidx.dst,
252171133Sgnn				 sizeof (union sockaddr_union)) == 0) {
253171133Sgnn				/*
254171133Sgnn				 * No IPsec processing is needed, free
255171133Sgnn				 * reference to SP.
256171133Sgnn				 *
257171133Sgnn				 * NB: null pointer to avoid free at
258171133Sgnn				 *     done: below.
259171133Sgnn				 */
260171133Sgnn				KEY_FREESP(sp), sp = NULL;
261171133Sgnn				splx(s);
262171133Sgnn				goto done;
263171133Sgnn			}
264171133Sgnn		}
265171133Sgnn
266171133Sgnn		/*
267171133Sgnn		 * Do delayed checksums now because we send before
268171133Sgnn		 * this is done in the normal processing path.
269171133Sgnn		 */
270171133Sgnn		if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
271171133Sgnn			in_delayed_cksum(*m);
272171133Sgnn			(*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
273171133Sgnn		}
274171133Sgnn
275171133Sgnn		/*
276171133Sgnn		 * Preserve KAME behaviour: ENOENT can be returned
277171133Sgnn		 * when an SA acquire is in progress.  Don't propagate
278171133Sgnn		 * this to user-level; it confuses applications.
279171133Sgnn		 *
280171133Sgnn		 * XXX this will go away when the SADB is redone.
281171133Sgnn		 */
282171133Sgnn		if (*error == ENOENT)
283171133Sgnn			*error = 0;
284171133Sgnn		goto do_ipsec;
285171133Sgnn	} else {	/* sp == NULL */
286171133Sgnn		if (*error != 0) {
287171133Sgnn			/*
288171133Sgnn			 * Hack: -EINVAL is used to signal that a packet
289171133Sgnn			 * should be silently discarded.  This is typically
290171133Sgnn			 * because we asked key management for an SA and
291171133Sgnn			 * it was delayed (e.g. kicked up to IKE).
292171133Sgnn			 */
293171133Sgnn			if (*error == -EINVAL)
294171133Sgnn				*error = 0;
295171133Sgnn			goto bad;
296171133Sgnn		} else {
297171133Sgnn			/* No IPsec processing for this packet. */
298171133Sgnn		}
299171133Sgnn	}
300171133Sgnndone:
301171133Sgnn	if (sp != NULL)
302171133Sgnn		if (*sp != NULL)
303171133Sgnn			KEY_FREESP(sp);
304171133Sgnn	return 0;
305171133Sgnndo_ipsec:
306171133Sgnn	return -1;
307171133Sgnnbad:
308171133Sgnn	if (sp != NULL)
309171133Sgnn		if (*sp != NULL)
310171133Sgnn			KEY_FREESP(sp);
311171133Sgnn	return 1;
312171167Sgnn#endif /* IPSEC */
313171133Sgnn	return 0;
314171133Sgnn}
315171133Sgnn
316171133Sgnn/*
317171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
318171133Sgnn * Called from ip_forward().
319171133Sgnn * Returns MTU suggestion for ICMP needfrag reply.
320171133Sgnn */
321171133Sgnnint
322171133Sgnnip6_ipsec_mtu(struct mbuf *m)
323171133Sgnn{
324171133Sgnn	int mtu = 0;
325171133Sgnn	/*
326171133Sgnn	 * If the packet is routed over IPsec tunnel, tell the
327171133Sgnn	 * originator the tunnel MTU.
328171133Sgnn	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
329171133Sgnn	 * XXX quickhack!!!
330171133Sgnn	 */
331171133Sgnn	struct secpolicy *sp = NULL;
332171133Sgnn	int ipsecerror;
333171133Sgnn	int ipsechdr;
334171133Sgnn	struct route *ro;
335171167Sgnn#ifdef IPSEC
336171133Sgnn	sp = ipsec_getpolicybyaddr(m,
337171133Sgnn				   IPSEC_DIR_OUTBOUND,
338171133Sgnn				   IP_FORWARDING,
339171133Sgnn				   &ipsecerror);
340171167Sgnn#endif /* IPSEC */
341171133Sgnn	if (sp != NULL) {
342171133Sgnn		/* count IPsec header size */
343171133Sgnn		ipsechdr = ipsec4_hdrsiz(m,
344171133Sgnn					 IPSEC_DIR_OUTBOUND,
345171133Sgnn					 NULL);
346171133Sgnn
347171133Sgnn		/*
348171133Sgnn		 * find the correct route for outer IPv4
349171133Sgnn		 * header, compute tunnel MTU.
350171133Sgnn		 */
351171133Sgnn		if (sp->req != NULL &&
352171133Sgnn		    sp->req->sav != NULL &&
353171133Sgnn		    sp->req->sav->sah != NULL) {
354171133Sgnn			ro = &sp->req->sav->sah->sa_route;
355171133Sgnn			if (ro->ro_rt && ro->ro_rt->rt_ifp) {
356171133Sgnn				mtu =
357171133Sgnn				    ro->ro_rt->rt_rmx.rmx_mtu ?
358171133Sgnn				    ro->ro_rt->rt_rmx.rmx_mtu :
359171133Sgnn				    ro->ro_rt->rt_ifp->if_mtu;
360171133Sgnn				mtu -= ipsechdr;
361171133Sgnn			}
362171133Sgnn		}
363171167Sgnn#ifdef IPSEC
364171133Sgnn		KEY_FREESP(&sp);
365171167Sgnn#endif /* IPSEC */
366171133Sgnn	}
367171133Sgnn	return mtu;
368171133Sgnn}
369171133Sgnn
370