1155179Sandre/*-
2155179Sandre * Copyright (c) 1982, 1986, 1988, 1993
3155179Sandre *      The Regents of the University of California.  All rights reserved.
4155179Sandre *
5155179Sandre * Redistribution and use in source and binary forms, with or without
6155179Sandre * modification, are permitted provided that the following conditions
7155179Sandre * are met:
8155179Sandre * 1. Redistributions of source code must retain the above copyright
9155179Sandre *    notice, this list of conditions and the following disclaimer.
10155179Sandre * 2. Redistributions in binary form must reproduce the above copyright
11155179Sandre *    notice, this list of conditions and the following disclaimer in the
12155179Sandre *    documentation and/or other materials provided with the distribution.
13155179Sandre * 4. Neither the name of the University nor the names of its contributors
14155179Sandre *    may be used to endorse or promote products derived from this software
15155179Sandre *    without specific prior written permission.
16155179Sandre *
17155179Sandre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18155179Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19155179Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20155179Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21155179Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22155179Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23155179Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24155179Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25155179Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26155179Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27155179Sandre * SUCH DAMAGE.
28155179Sandre */
29155179Sandre
30172467Ssilby#include <sys/cdefs.h>
31172467Ssilby__FBSDID("$FreeBSD$");
32172467Ssilby
33155179Sandre#include "opt_ipsec.h"
34188066Srrs#include "opt_sctp.h"
35155179Sandre
36155179Sandre#include <sys/param.h>
37155179Sandre#include <sys/systm.h>
38177175Sbz#include <sys/errno.h>
39155179Sandre#include <sys/kernel.h>
40155179Sandre#include <sys/malloc.h>
41155179Sandre#include <sys/mbuf.h>
42155179Sandre#include <sys/protosw.h>
43155179Sandre#include <sys/socket.h>
44155179Sandre#include <sys/socketvar.h>
45155179Sandre#include <sys/sysctl.h>
46155179Sandre
47155179Sandre#include <net/if.h>
48155179Sandre#include <net/route.h>
49195699Srwatson#include <net/vnet.h>
50155179Sandre
51155179Sandre#include <netinet/in.h>
52155179Sandre#include <netinet/in_systm.h>
53155179Sandre#include <netinet/in_var.h>
54155179Sandre#include <netinet/ip.h>
55155179Sandre#include <netinet/in_pcb.h>
56155179Sandre#include <netinet/ip_var.h>
57155179Sandre#include <netinet/ip_options.h>
58155179Sandre#include <netinet/ip_ipsec.h>
59188066Srrs#ifdef SCTP
60188066Srrs#include <netinet/sctp_crc32.h>
61188066Srrs#endif
62155179Sandre
63155179Sandre#include <machine/in_cksum.h>
64155179Sandre
65171167Sgnn#ifdef IPSEC
66155179Sandre#include <netipsec/ipsec.h>
67155179Sandre#include <netipsec/xform.h>
68155179Sandre#include <netipsec/key.h>
69171167Sgnn#endif /*IPSEC*/
70155179Sandre
71155179Sandreextern	struct protosw inetsw[];
72155179Sandre
73195699Srwatson#ifdef IPSEC
74195699Srwatson#ifdef IPSEC_FILTERTUNNEL
75215701Sdimstatic VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 1;
76195699Srwatson#else
77215701Sdimstatic VNET_DEFINE(int, ip4_ipsec_filtertunnel) = 0;
78192648Sbz#endif
79195727Srwatson#define	V_ip4_ipsec_filtertunnel VNET(ip4_ipsec_filtertunnel)
80192648Sbz
81195699SrwatsonSYSCTL_DECL(_net_inet_ipsec);
82195699SrwatsonSYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, filtertunnel,
83195699Srwatson	CTLFLAG_RW, &VNET_NAME(ip4_ipsec_filtertunnel), 0,
84195699Srwatson	"If set filter packets from an IPsec tunnel.");
85195699Srwatson#endif /* IPSEC */
86195699Srwatson
87155179Sandre/*
88155179Sandre * Check if we have to jump over firewall processing for this packet.
89155179Sandre * Called from ip_input().
90155179Sandre * 1 = jump over firewall, 0 = packet goes through firewall.
91155179Sandre */
92155179Sandreint
93171732Sbzip_ipsec_filtertunnel(struct mbuf *m)
94155179Sandre{
95230442Sbz#ifdef IPSEC
96192648Sbz
97155179Sandre	/*
98222845Sbz	 * Bypass packet filtering for packets previously handled by IPsec.
99155179Sandre	 */
100192648Sbz	if (!V_ip4_ipsec_filtertunnel &&
101192648Sbz	    m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
102155179Sandre		return 1;
103155179Sandre#endif
104155179Sandre	return 0;
105155179Sandre}
106155179Sandre
107155179Sandre/*
108155179Sandre * Check if this packet has an active SA and needs to be dropped instead
109155179Sandre * of forwarded.
110155179Sandre * Called from ip_input().
111155179Sandre * 1 = drop packet, 0 = forward packet.
112155179Sandre */
113155179Sandreint
114155179Sandreip_ipsec_fwd(struct mbuf *m)
115155179Sandre{
116171167Sgnn#ifdef IPSEC
117155179Sandre	struct m_tag *mtag;
118155179Sandre	struct tdb_ident *tdbi;
119155179Sandre	struct secpolicy *sp;
120241686Sandre	int error;
121171133Sgnn
122155179Sandre	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
123155179Sandre	if (mtag != NULL) {
124155179Sandre		tdbi = (struct tdb_ident *)(mtag + 1);
125155179Sandre		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
126155179Sandre	} else {
127155179Sandre		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
128155179Sandre					   IP_FORWARDING, &error);
129155179Sandre	}
130155179Sandre	if (sp == NULL) {	/* NB: can happen if error */
131155179Sandre		/*XXX error stat???*/
132155179Sandre		DPRINTF(("ip_input: no SP for forwarding\n"));	/*XXX*/
133155179Sandre		return 1;
134155179Sandre	}
135155179Sandre
136155179Sandre	/*
137155179Sandre	 * Check security policy against packet attributes.
138155179Sandre	 */
139155179Sandre	error = ipsec_in_reject(sp, m);
140155179Sandre	KEY_FREESP(&sp);
141155179Sandre	if (error) {
142190951Srwatson		IPSTAT_INC(ips_cantforward);
143155179Sandre		return 1;
144155179Sandre	}
145171167Sgnn#endif /* IPSEC */
146155179Sandre	return 0;
147155179Sandre}
148155179Sandre
149155179Sandre/*
150155179Sandre * Check if protocol type doesn't have a further header and do IPSEC
151155179Sandre * decryption or reject right now.  Protocols with further headers get
152155179Sandre * their IPSEC treatment within the protocol specific processing.
153155179Sandre * Called from ip_input().
154155179Sandre * 1 = drop packet, 0 = continue processing packet.
155155179Sandre */
156155179Sandreint
157155179Sandreip_ipsec_input(struct mbuf *m)
158155179Sandre{
159195699Srwatson#ifdef IPSEC
160155179Sandre	struct ip *ip = mtod(m, struct ip *);
161155179Sandre	struct m_tag *mtag;
162155179Sandre	struct tdb_ident *tdbi;
163155179Sandre	struct secpolicy *sp;
164241686Sandre	int error;
165155179Sandre	/*
166155179Sandre	 * enforce IPsec policy checking if we are seeing last header.
167155179Sandre	 * note that we do not visit this with protocols with pcb layer
168155179Sandre	 * code - like udp/tcp/raw ip.
169155179Sandre	 */
170155179Sandre	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
171155179Sandre		/*
172155179Sandre		 * Check if the packet has already had IPsec processing
173155179Sandre		 * done.  If so, then just pass it along.  This tag gets
174155179Sandre		 * set during AH, ESP, etc. input handling, before the
175155179Sandre		 * packet is returned to the ip input queue for delivery.
176155179Sandre		 */
177155179Sandre		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
178155179Sandre		if (mtag != NULL) {
179155179Sandre			tdbi = (struct tdb_ident *)(mtag + 1);
180155179Sandre			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
181155179Sandre		} else {
182155179Sandre			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
183155179Sandre						   IP_FORWARDING, &error);
184155179Sandre		}
185155179Sandre		if (sp != NULL) {
186155179Sandre			/*
187155179Sandre			 * Check security policy against packet attributes.
188155179Sandre			 */
189155179Sandre			error = ipsec_in_reject(sp, m);
190155179Sandre			KEY_FREESP(&sp);
191155179Sandre		} else {
192155179Sandre			/* XXX error stat??? */
193155179Sandre			error = EINVAL;
194155179Sandre			DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
195155179Sandre			return 1;
196155179Sandre		}
197155179Sandre		if (error)
198155179Sandre			return 1;
199155179Sandre	}
200171167Sgnn#endif /* IPSEC */
201155179Sandre	return 0;
202155179Sandre}
203155179Sandre
204155179Sandre/*
205155179Sandre * Compute the MTU for a forwarded packet that gets IPSEC encapsulated.
206155179Sandre * Called from ip_forward().
207155179Sandre * Returns MTU suggestion for ICMP needfrag reply.
208155179Sandre */
209155179Sandreint
210178029Sbzip_ipsec_mtu(struct mbuf *m, int mtu)
211155179Sandre{
212155179Sandre	/*
213155179Sandre	 * If the packet is routed over IPsec tunnel, tell the
214155179Sandre	 * originator the tunnel MTU.
215155179Sandre	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
216155179Sandre	 * XXX quickhack!!!
217155179Sandre	 */
218155179Sandre	struct secpolicy *sp = NULL;
219155179Sandre	int ipsecerror;
220155179Sandre	int ipsechdr;
221155179Sandre	struct route *ro;
222155179Sandre	sp = ipsec_getpolicybyaddr(m,
223155179Sandre				   IPSEC_DIR_OUTBOUND,
224155179Sandre				   IP_FORWARDING,
225155179Sandre				   &ipsecerror);
226155179Sandre	if (sp != NULL) {
227155179Sandre		/* count IPsec header size */
228188306Sbz		ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
229155179Sandre
230155179Sandre		/*
231155179Sandre		 * find the correct route for outer IPv4
232155179Sandre		 * header, compute tunnel MTU.
233155179Sandre		 */
234155179Sandre		if (sp->req != NULL &&
235155179Sandre		    sp->req->sav != NULL &&
236155179Sandre		    sp->req->sav->sah != NULL) {
237214250Sbz			ro = &sp->req->sav->sah->route_cache.sa_route;
238155179Sandre			if (ro->ro_rt && ro->ro_rt->rt_ifp) {
239263478Sglebius				mtu = ro->ro_rt->rt_mtu ? ro->ro_rt->rt_mtu :
240155179Sandre				    ro->ro_rt->rt_ifp->if_mtu;
241155179Sandre				mtu -= ipsechdr;
242155179Sandre			}
243155179Sandre		}
244155179Sandre		KEY_FREESP(&sp);
245155179Sandre	}
246155179Sandre	return mtu;
247155179Sandre}
248155179Sandre
249155179Sandre/*
250155179Sandre *
251155179Sandre * Called from ip_output().
252155179Sandre * 1 = drop packet, 0 = continue processing packet,
253171167Sgnn * -1 = packet was reinjected and stop processing packet
254155179Sandre */
255155179Sandreint
256199102Straszip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error)
257155179Sandre{
258171167Sgnn#ifdef IPSEC
259155179Sandre	struct secpolicy *sp = NULL;
260155179Sandre	struct tdb_ident *tdbi;
261155179Sandre	struct m_tag *mtag;
262155179Sandre	/*
263155179Sandre	 * Check the security policy (SP) for the packet and, if
264155179Sandre	 * required, do IPsec-related processing.  There are two
265155179Sandre	 * cases here; the first time a packet is sent through
266155179Sandre	 * it will be untagged and handled by ipsec4_checkpolicy.
267155179Sandre	 * If the packet is resubmitted to ip_output (e.g. after
268155179Sandre	 * AH, ESP, etc. processing), there will be a tag to bypass
269155179Sandre	 * the lookup and related policy checking.
270155179Sandre	 */
271155179Sandre	mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
272155179Sandre	if (mtag != NULL) {
273155179Sandre		tdbi = (struct tdb_ident *)(mtag + 1);
274155179Sandre		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
275155179Sandre		if (sp == NULL)
276155179Sandre			*error = -EINVAL;	/* force silent drop */
277155179Sandre		m_tag_delete(*m, mtag);
278155179Sandre	} else {
279155179Sandre		sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags,
280155179Sandre					error, inp);
281155179Sandre	}
282155179Sandre	/*
283155179Sandre	 * There are four return cases:
284155179Sandre	 *    sp != NULL	 	    apply IPsec policy
285155179Sandre	 *    sp == NULL, error == 0	    no IPsec handling needed
286155179Sandre	 *    sp == NULL, error == -EINVAL  discard packet w/o error
287155179Sandre	 *    sp == NULL, error != 0	    discard packet, report error
288155179Sandre	 */
289155179Sandre	if (sp != NULL) {
290155179Sandre		/* Loop detection, check if ipsec processing already done */
291155179Sandre		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
292155179Sandre		for (mtag = m_tag_first(*m); mtag != NULL;
293155179Sandre		     mtag = m_tag_next(*m, mtag)) {
294155179Sandre			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
295155179Sandre				continue;
296155179Sandre			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
297155179Sandre			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
298155179Sandre				continue;
299155179Sandre			/*
300155179Sandre			 * Check if policy has an SA associated with it.
301155179Sandre			 * This can happen when an SP has yet to acquire
302155179Sandre			 * an SA; e.g. on first reference.  If it occurs,
303155179Sandre			 * then we let ipsec4_process_packet do its thing.
304155179Sandre			 */
305155179Sandre			if (sp->req->sav == NULL)
306155179Sandre				break;
307155179Sandre			tdbi = (struct tdb_ident *)(mtag + 1);
308155179Sandre			if (tdbi->spi == sp->req->sav->spi &&
309155179Sandre			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
310155179Sandre			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
311155179Sandre				 sizeof (union sockaddr_union)) == 0) {
312155179Sandre				/*
313155179Sandre				 * No IPsec processing is needed, free
314155179Sandre				 * reference to SP.
315155179Sandre				 *
316155179Sandre				 * NB: null pointer to avoid free at
317155179Sandre				 *     done: below.
318155179Sandre				 */
319155179Sandre				KEY_FREESP(&sp), sp = NULL;
320155179Sandre				goto done;
321155179Sandre			}
322155179Sandre		}
323155179Sandre
324155179Sandre		/*
325155179Sandre		 * Do delayed checksums now because we send before
326155179Sandre		 * this is done in the normal processing path.
327155179Sandre		 */
328155179Sandre		if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
329155179Sandre			in_delayed_cksum(*m);
330155179Sandre			(*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
331155179Sandre		}
332188066Srrs#ifdef SCTP
333188066Srrs		if ((*m)->m_pkthdr.csum_flags & CSUM_SCTP) {
334241913Sglebius			struct ip *ip = mtod(*m, struct ip *);
335241913Sglebius
336205104Srrs			sctp_delayed_cksum(*m, (uint32_t)(ip->ip_hl << 2));
337188066Srrs			(*m)->m_pkthdr.csum_flags &= ~CSUM_SCTP;
338188066Srrs		}
339188066Srrs#endif
340155179Sandre
341155179Sandre		/* NB: callee frees mbuf */
342155179Sandre		*error = ipsec4_process_packet(*m, sp->req, *flags, 0);
343177175Sbz		if (*error == EJUSTRETURN) {
344177175Sbz			/*
345177175Sbz			 * We had a SP with a level of 'use' and no SA. We
346177175Sbz			 * will just continue to process the packet without
347177175Sbz			 * IPsec processing and return without error.
348177175Sbz			 */
349177175Sbz			*error = 0;
350177175Sbz			goto done;
351177175Sbz		}
352155179Sandre		/*
353155179Sandre		 * Preserve KAME behaviour: ENOENT can be returned
354155179Sandre		 * when an SA acquire is in progress.  Don't propagate
355155179Sandre		 * this to user-level; it confuses applications.
356155179Sandre		 *
357155179Sandre		 * XXX this will go away when the SADB is redone.
358155179Sandre		 */
359155179Sandre		if (*error == ENOENT)
360155179Sandre			*error = 0;
361155179Sandre		goto reinjected;
362155179Sandre	} else {	/* sp == NULL */
363155179Sandre
364155179Sandre		if (*error != 0) {
365155179Sandre			/*
366155179Sandre			 * Hack: -EINVAL is used to signal that a packet
367155179Sandre			 * should be silently discarded.  This is typically
368155179Sandre			 * because we asked key management for an SA and
369155179Sandre			 * it was delayed (e.g. kicked up to IKE).
370155179Sandre			 */
371155179Sandre			if (*error == -EINVAL)
372155179Sandre				*error = 0;
373155179Sandre			goto bad;
374155179Sandre		} else {
375155179Sandre			/* No IPsec processing for this packet. */
376155179Sandre		}
377155179Sandre	}
378155179Sandredone:
379155179Sandre	if (sp != NULL)
380155179Sandre		KEY_FREESP(&sp);
381155179Sandre	return 0;
382155179Sandrereinjected:
383155179Sandre	if (sp != NULL)
384155179Sandre		KEY_FREESP(&sp);
385155179Sandre	return -1;
386155179Sandrebad:
387155179Sandre	if (sp != NULL)
388155179Sandre		KEY_FREESP(&sp);
389155179Sandre	return 1;
390171167Sgnn#endif /* IPSEC */
391155179Sandre	return 0;
392155179Sandre}
393