ip6_ipsec.c revision 266800
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 266800 2014-05-28 12:45:27Z vanhu $");
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>
50257176Sglebius#include <net/if_var.h>
51171133Sgnn#include <net/route.h>
52195699Srwatson#include <net/vnet.h>
53171133Sgnn
54171133Sgnn#include <netinet/in.h>
55171133Sgnn#include <netinet/in_systm.h>
56171133Sgnn#include <netinet/in_var.h>
57171133Sgnn#include <netinet/ip.h>
58180085Sjulian#include <netinet/ip6.h>
59171133Sgnn#include <netinet/in_pcb.h>
60171133Sgnn#include <netinet/ip_var.h>
61171133Sgnn#include <netinet/ip_options.h>
62171133Sgnn
63171133Sgnn#include <machine/in_cksum.h>
64171133Sgnn
65171167Sgnn#ifdef IPSEC
66171133Sgnn#include <netipsec/ipsec.h>
67171133Sgnn#include <netipsec/ipsec6.h>
68171133Sgnn#include <netipsec/xform.h>
69171133Sgnn#include <netipsec/key.h>
70171133Sgnn#ifdef IPSEC_DEBUG
71171133Sgnn#include <netipsec/key_debug.h>
72171133Sgnn#else
73171133Sgnn#define	KEYDEBUG(lev,arg)
74171133Sgnn#endif
75171167Sgnn#endif /*IPSEC*/
76171133Sgnn
77171133Sgnn#include <netinet6/ip6_ipsec.h>
78180090Sbz#include <netinet6/ip6_var.h>
79171133Sgnn
80171133Sgnnextern	struct protosw inet6sw[];
81171133Sgnn
82195699Srwatson
83195699Srwatson#ifdef INET6
84195699Srwatson#ifdef IPSEC
85195699Srwatson#ifdef IPSEC_FILTERTUNNEL
86215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 1;
87195699Srwatson#else
88215701Sdimstatic VNET_DEFINE(int, ip6_ipsec6_filtertunnel) = 0;
89192648Sbz#endif
90195727Srwatson#define	V_ip6_ipsec6_filtertunnel	VNET(ip6_ipsec6_filtertunnel)
91192648Sbz
92195699SrwatsonSYSCTL_DECL(_net_inet6_ipsec6);
93195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ipsec6, OID_AUTO,
94195699Srwatson	filtertunnel, CTLFLAG_RW, &VNET_NAME(ip6_ipsec6_filtertunnel),  0,
95195699Srwatson	"If set filter packets from an IPsec tunnel.");
96195699Srwatson#endif /* IPSEC */
97195699Srwatson#endif /* INET6 */
98195699Srwatson
99171133Sgnn/*
100171133Sgnn * Check if we have to jump over firewall processing for this packet.
101222845Sbz * Called from ip6_input().
102171133Sgnn * 1 = jump over firewall, 0 = packet goes through firewall.
103171133Sgnn */
104171133Sgnnint
105171732Sbzip6_ipsec_filtertunnel(struct mbuf *m)
106171133Sgnn{
107230442Sbz#ifdef IPSEC
108192648Sbz
109171133Sgnn	/*
110222845Sbz	 * Bypass packet filtering for packets previously handled by IPsec.
111171133Sgnn	 */
112192648Sbz	if (!V_ip6_ipsec6_filtertunnel &&
113192648Sbz	    m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
114171133Sgnn		return 1;
115171133Sgnn#endif
116171133Sgnn	return 0;
117171133Sgnn}
118171133Sgnn
119171133Sgnn/*
120171133Sgnn * Check if this packet has an active SA and needs to be dropped instead
121171133Sgnn * of forwarded.
122222845Sbz * Called from ip6_input().
123171133Sgnn * 1 = drop packet, 0 = forward packet.
124171133Sgnn */
125171133Sgnnint
126171133Sgnnip6_ipsec_fwd(struct mbuf *m)
127171133Sgnn{
128171167Sgnn#ifdef IPSEC
129171133Sgnn	struct m_tag *mtag;
130171133Sgnn	struct tdb_ident *tdbi;
131171133Sgnn	struct secpolicy *sp;
132241686Sandre	int error;
133171133Sgnn	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
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		/*XXX error stat???*/
143222845Sbz		DPRINTF(("%s: no SP for forwarding\n", __func__));	/*XXX*/
144171133Sgnn		return 1;
145171133Sgnn	}
146171133Sgnn
147171133Sgnn	/*
148171133Sgnn	 * Check security policy against packet attributes.
149171133Sgnn	 */
150171133Sgnn	error = ipsec_in_reject(sp, m);
151171133Sgnn	KEY_FREESP(&sp);
152171133Sgnn	if (error) {
153249294Sae		IP6STAT_INC(ip6s_cantforward);
154171133Sgnn		return 1;
155171133Sgnn	}
156171167Sgnn#endif /* IPSEC */
157171133Sgnn	return 0;
158171133Sgnn}
159171133Sgnn
160171133Sgnn/*
161171133Sgnn * Check if protocol type doesn't have a further header and do IPSEC
162171133Sgnn * decryption or reject right now.  Protocols with further headers get
163171133Sgnn * their IPSEC treatment within the protocol specific processing.
164222845Sbz * Called from ip6_input().
165171133Sgnn * 1 = drop packet, 0 = continue processing packet.
166171133Sgnn */
167171133Sgnnint
168171133Sgnnip6_ipsec_input(struct mbuf *m, int nxt)
169171133Sgnn{
170171167Sgnn#ifdef IPSEC
171171133Sgnn	struct m_tag *mtag;
172171133Sgnn	struct tdb_ident *tdbi;
173171133Sgnn	struct secpolicy *sp;
174241686Sandre	int error;
175171133Sgnn	/*
176171133Sgnn	 * enforce IPsec policy checking if we are seeing last header.
177171133Sgnn	 * note that we do not visit this with protocols with pcb layer
178171133Sgnn	 * code - like udp/tcp/raw ip.
179171133Sgnn	 */
180171133Sgnn	if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
181171133Sgnn	    ipsec6_in_reject(m, NULL)) {
182171133Sgnn
183171133Sgnn		/*
184171133Sgnn		 * Check if the packet has already had IPsec processing
185171133Sgnn		 * done.  If so, then just pass it along.  This tag gets
186171133Sgnn		 * set during AH, ESP, etc. input handling, before the
187171133Sgnn		 * packet is returned to the ip input queue for delivery.
188171260Sdelphij		 */
189171133Sgnn		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
190171133Sgnn		if (mtag != NULL) {
191171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
192171133Sgnn			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
193171133Sgnn		} else {
194171133Sgnn			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
195171260Sdelphij						   IP_FORWARDING, &error);
196171133Sgnn		}
197171133Sgnn		if (sp != NULL) {
198171133Sgnn			/*
199171133Sgnn			 * Check security policy against packet attributes.
200171133Sgnn			 */
201171133Sgnn			error = ipsec_in_reject(sp, m);
202171133Sgnn			KEY_FREESP(&sp);
203171133Sgnn		} else {
204171133Sgnn			/* XXX error stat??? */
205171133Sgnn			error = EINVAL;
206222845Sbz			DPRINTF(("%s: no SP, packet discarded\n", __func__));/*XXX*/
207171133Sgnn			return 1;
208171133Sgnn		}
209171133Sgnn		if (error)
210171133Sgnn			return 1;
211171133Sgnn	}
212171167Sgnn#endif /* IPSEC */
213171133Sgnn	return 0;
214171133Sgnn}
215171133Sgnn
216171133Sgnn/*
217171133Sgnn * Called from ip6_output().
218171133Sgnn * 1 = drop packet, 0 = continue processing packet,
219171260Sdelphij * -1 = packet was reinjected and stop processing packet
220171260Sdelphij */
221171133Sgnn
222171133Sgnnint
223171133Sgnnip6_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error,
224266800Svanhu    struct ifnet **ifp)
225171133Sgnn{
226171167Sgnn#ifdef IPSEC
227266800Svanhu	struct secpolicy *sp = NULL;
228171133Sgnn	struct tdb_ident *tdbi;
229171133Sgnn	struct m_tag *mtag;
230171237Speter	/* XXX int s; */
231171133Sgnn	mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
232171133Sgnn	if (mtag != NULL) {
233171133Sgnn		tdbi = (struct tdb_ident *)(mtag + 1);
234266800Svanhu		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
235266800Svanhu		if (sp == NULL)
236171133Sgnn			*error = -EINVAL;	/* force silent drop */
237171133Sgnn		m_tag_delete(*m, mtag);
238171133Sgnn	} else {
239266800Svanhu		sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
240171133Sgnn					error, inp);
241171133Sgnn	}
242171133Sgnn
243171133Sgnn	/*
244171133Sgnn	 * There are four return cases:
245171260Sdelphij	 *    sp != NULL		    apply IPsec policy
246171133Sgnn	 *    sp == NULL, error == 0	    no IPsec handling needed
247171133Sgnn	 *    sp == NULL, error == -EINVAL  discard packet w/o error
248171133Sgnn	 *    sp == NULL, error != 0	    discard packet, report error
249171133Sgnn	 */
250266800Svanhu	if (sp != NULL) {
251171133Sgnn		/* Loop detection, check if ipsec processing already done */
252266800Svanhu		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
253171133Sgnn		for (mtag = m_tag_first(*m); mtag != NULL;
254171133Sgnn		     mtag = m_tag_next(*m, mtag)) {
255171133Sgnn			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
256171133Sgnn				continue;
257171133Sgnn			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
258171133Sgnn			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
259171133Sgnn				continue;
260171133Sgnn			/*
261238877Sbz			 * Check if policy has no SA associated with it.
262171133Sgnn			 * This can happen when an SP has yet to acquire
263171133Sgnn			 * an SA; e.g. on first reference.  If it occurs,
264171133Sgnn			 * then we let ipsec4_process_packet do its thing.
265171133Sgnn			 */
266266800Svanhu			if (sp->req->sav == NULL)
267171133Sgnn				break;
268171133Sgnn			tdbi = (struct tdb_ident *)(mtag + 1);
269266800Svanhu			if (tdbi->spi == sp->req->sav->spi &&
270266800Svanhu			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
271266800Svanhu			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
272171133Sgnn				 sizeof (union sockaddr_union)) == 0) {
273171133Sgnn				/*
274171133Sgnn				 * No IPsec processing is needed, free
275171133Sgnn				 * reference to SP.
276171133Sgnn				 *
277171133Sgnn				 * NB: null pointer to avoid free at
278171133Sgnn				 *     done: below.
279171133Sgnn				 */
280266800Svanhu				KEY_FREESP(&sp), sp = NULL;
281171133Sgnn				goto done;
282171133Sgnn			}
283171133Sgnn		}
284171133Sgnn
285171133Sgnn		/*
286171133Sgnn		 * Do delayed checksums now because we send before
287171133Sgnn		 * this is done in the normal processing path.
288171133Sgnn		 */
289238935Sbz#ifdef INET
290171133Sgnn		if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
291171133Sgnn			in_delayed_cksum(*m);
292171133Sgnn			(*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
293171133Sgnn		}
294238935Sbz#endif
295266800Svanhu		if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
296266800Svanhu			in6_delayed_cksum(*m, (*m)->m_pkthdr.len - sizeof(struct ip6_hdr),
297266800Svanhu							sizeof(struct ip6_hdr));
298266800Svanhu			(*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
299266800Svanhu		}
300266800Svanhu#ifdef SCTP
301266800Svanhu		if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
302266800Svanhu			sctp_delayed_cksum(*m, sizeof(struct ip6_hdr));
303266800Svanhu			(*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
304266800Svanhu		}
305266800Svanhu#endif
306171133Sgnn
307266800Svanhu		/* NB: callee frees mbuf */
308266800Svanhu		*error = ipsec6_process_packet(*m, sp->req);
309266800Svanhu
310266800Svanhu		if (*error == EJUSTRETURN) {
311266800Svanhu			/*
312266800Svanhu			 * We had a SP with a level of 'use' and no SA. We
313266800Svanhu			 * will just continue to process the packet without
314266800Svanhu			 * IPsec processing.
315266800Svanhu			 */
316266800Svanhu			*error = 0;
317266800Svanhu			goto done;
318266800Svanhu		}
319266800Svanhu
320171133Sgnn		/*
321171133Sgnn		 * Preserve KAME behaviour: ENOENT can be returned
322171133Sgnn		 * when an SA acquire is in progress.  Don't propagate
323171133Sgnn		 * this to user-level; it confuses applications.
324171133Sgnn		 *
325171133Sgnn		 * XXX this will go away when the SADB is redone.
326171133Sgnn		 */
327171133Sgnn		if (*error == ENOENT)
328171133Sgnn			*error = 0;
329266800Svanhu		goto reinjected;
330171133Sgnn	} else {	/* sp == NULL */
331171133Sgnn		if (*error != 0) {
332171133Sgnn			/*
333171133Sgnn			 * Hack: -EINVAL is used to signal that a packet
334171133Sgnn			 * should be silently discarded.  This is typically
335171133Sgnn			 * because we asked key management for an SA and
336171133Sgnn			 * it was delayed (e.g. kicked up to IKE).
337171133Sgnn			 */
338171133Sgnn			if (*error == -EINVAL)
339171133Sgnn				*error = 0;
340171133Sgnn			goto bad;
341171133Sgnn		} else {
342171133Sgnn			/* No IPsec processing for this packet. */
343171133Sgnn		}
344171133Sgnn	}
345171133Sgnndone:
346266800Svanhu	if (sp != NULL)
347266800Svanhu		KEY_FREESP(&sp);
348171133Sgnn	return 0;
349266800Svanhureinjected:
350266800Svanhu	if (sp != NULL)
351266800Svanhu		KEY_FREESP(&sp);
352171133Sgnn	return -1;
353171133Sgnnbad:
354266800Svanhu	if (sp != NULL)
355266800Svanhu		KEY_FREESP(&sp);
356171133Sgnn	return 1;
357171167Sgnn#endif /* IPSEC */
358171133Sgnn	return 0;
359171133Sgnn}
360171133Sgnn
361177166Sbz#if 0
362171133Sgnn/*
363171133Sgnn * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
364171133Sgnn * Called from ip_forward().
365171133Sgnn * Returns MTU suggestion for ICMP needfrag reply.
366171133Sgnn */
367171133Sgnnint
368171133Sgnnip6_ipsec_mtu(struct mbuf *m)
369171133Sgnn{
370171133Sgnn	int mtu = 0;
371171133Sgnn	/*
372171133Sgnn	 * If the packet is routed over IPsec tunnel, tell the
373171133Sgnn	 * originator the tunnel MTU.
374171133Sgnn	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
375171133Sgnn	 * XXX quickhack!!!
376171133Sgnn	 */
377177166Sbz#ifdef IPSEC
378171133Sgnn	struct secpolicy *sp = NULL;
379171133Sgnn	int ipsecerror;
380171133Sgnn	int ipsechdr;
381171133Sgnn	struct route *ro;
382171133Sgnn	sp = ipsec_getpolicybyaddr(m,
383171133Sgnn				   IPSEC_DIR_OUTBOUND,
384171133Sgnn				   IP_FORWARDING,
385171133Sgnn				   &ipsecerror);
386171133Sgnn	if (sp != NULL) {
387171133Sgnn		/* count IPsec header size */
388188306Sbz		ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
389171133Sgnn
390171133Sgnn		/*
391171133Sgnn		 * find the correct route for outer IPv4
392171133Sgnn		 * header, compute tunnel MTU.
393171133Sgnn		 */
394171133Sgnn		if (sp->req != NULL &&
395171133Sgnn		    sp->req->sav != NULL &&
396171133Sgnn		    sp->req->sav->sah != NULL) {
397214250Sbz			ro = &sp->req->sav->sah->route_cache.sa_route;
398171133Sgnn			if (ro->ro_rt && ro->ro_rt->rt_ifp) {
399262763Sglebius				mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu :
400171133Sgnn				    ro->ro_rt->rt_ifp->if_mtu;
401171133Sgnn				mtu -= ipsechdr;
402171133Sgnn			}
403171133Sgnn		}
404171133Sgnn		KEY_FREESP(&sp);
405177166Sbz	}
406171167Sgnn#endif /* IPSEC */
407177166Sbz	/* XXX else case missing. */
408171133Sgnn	return mtu;
409171133Sgnn}
410177166Sbz#endif
411