ipsec_output.c revision 112758
1112758Ssam/*-
2112758Ssam * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3112758Ssam * All rights reserved.
4112758Ssam *
5112758Ssam * Redistribution and use in source and binary forms, with or without
6112758Ssam * modification, are permitted provided that the following conditions
7112758Ssam * are met:
8112758Ssam * 1. Redistributions of source code must retain the above copyright
9112758Ssam *    notice, this list of conditions and the following disclaimer.
10112758Ssam * 2. Redistributions in binary form must reproduce the above copyright
11112758Ssam *    notice, this list of conditions and the following disclaimer in the
12112758Ssam *    documentation and/or other materials provided with the distribution.
13112758Ssam *
14112758Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15112758Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16112758Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17112758Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18112758Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19112758Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20112758Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21112758Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22112758Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23112758Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24112758Ssam * SUCH DAMAGE.
25112758Ssam *
26112758Ssam * $FreeBSD: head/sys/netipsec/ipsec_output.c 112758 2003-03-28 20:28:05Z sam $
27112758Ssam */
28105197Ssam
29105197Ssam/*
30105197Ssam * IPsec output processing.
31105197Ssam */
32105197Ssam#include "opt_inet.h"
33105197Ssam#include "opt_inet6.h"
34105197Ssam#include "opt_ipsec.h"
35105197Ssam
36105197Ssam#include <sys/param.h>
37105197Ssam#include <sys/systm.h>
38105197Ssam#include <sys/mbuf.h>
39105197Ssam#include <sys/domain.h>
40105197Ssam#include <sys/protosw.h>
41105197Ssam#include <sys/socket.h>
42105197Ssam#include <sys/errno.h>
43105197Ssam#include <sys/syslog.h>
44105197Ssam
45105197Ssam#include <net/if.h>
46105197Ssam#include <net/route.h>
47105197Ssam
48105197Ssam#include <netinet/in.h>
49105197Ssam#include <netinet/in_systm.h>
50105197Ssam#include <netinet/ip.h>
51105197Ssam#include <netinet/ip_var.h>
52105197Ssam#include <netinet/in_var.h>
53105197Ssam#include <netinet/ip_ecn.h>
54105197Ssam#ifdef INET6
55105197Ssam#include <netinet6/ip6_ecn.h>
56105197Ssam#endif
57105197Ssam
58105197Ssam#include <netinet/ip6.h>
59105197Ssam#ifdef INET6
60105197Ssam#include <netinet6/ip6_var.h>
61105197Ssam#endif
62105197Ssam#include <netinet/in_pcb.h>
63105197Ssam#ifdef INET6
64105197Ssam#include <netinet/icmp6.h>
65105197Ssam#endif
66105197Ssam
67105197Ssam#include <netipsec/ipsec.h>
68105197Ssam#ifdef INET6
69105197Ssam#include <netipsec/ipsec6.h>
70105197Ssam#endif
71105197Ssam#include <netipsec/ah_var.h>
72105197Ssam#include <netipsec/esp_var.h>
73105197Ssam#include <netipsec/ipcomp_var.h>
74105197Ssam
75105197Ssam#include <netipsec/xform.h>
76105197Ssam
77105197Ssam#include <netipsec/key.h>
78105197Ssam#include <netipsec/keydb.h>
79105197Ssam#include <netipsec/key_debug.h>
80105197Ssam
81105197Ssam#include <machine/in_cksum.h>
82105197Ssam
83105197Ssamint
84105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
85105197Ssam{
86105197Ssam	struct tdb_ident *tdbi;
87105197Ssam	struct m_tag *mtag;
88105197Ssam	struct secasvar *sav;
89105197Ssam	struct secasindex *saidx;
90105197Ssam	int error;
91105197Ssam
92105197Ssam#if 0
93105197Ssam	SPLASSERT(net, "ipsec_process_done");
94105197Ssam#endif
95105197Ssam
96105197Ssam	KASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
97105197Ssam	KASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
98105197Ssam	sav = isr->sav;
99105197Ssam	KASSERT(sav != NULL, ("ipsec_process_done: null SA"));
100105197Ssam	KASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
101105197Ssam
102105197Ssam	saidx = &sav->sah->saidx;
103105197Ssam	switch (saidx->dst.sa.sa_family) {
104105197Ssam#ifdef INET
105105197Ssam	case AF_INET:
106105197Ssam		/* Fix the header length, for AH processing. */
107105197Ssam		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
108105197Ssam		break;
109105197Ssam#endif /* INET */
110105197Ssam#ifdef INET6
111105197Ssam	case AF_INET6:
112105197Ssam		/* Fix the header length, for AH processing. */
113105197Ssam		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
114105197Ssam			error = ENXIO;
115105197Ssam			goto bad;
116105197Ssam		}
117105197Ssam		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
118105197Ssam			/* No jumbogram support. */
119105197Ssam			error = ENXIO;	/*?*/
120105197Ssam			goto bad;
121105197Ssam		}
122105197Ssam		mtod(m, struct ip6_hdr *)->ip6_plen =
123105197Ssam			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
124105197Ssam		break;
125105197Ssam#endif /* INET6 */
126105197Ssam	default:
127105197Ssam		DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
128105197Ssam		    saidx->dst.sa.sa_family));
129105197Ssam		error = ENXIO;
130105197Ssam		goto bad;
131105197Ssam	}
132105197Ssam
133105197Ssam	/*
134105197Ssam	 * Add a record of what we've done or what needs to be done to the
135105197Ssam	 * packet.
136105197Ssam	 */
137105197Ssam	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
138105197Ssam			sizeof(struct tdb_ident), M_NOWAIT);
139105197Ssam	if (mtag == NULL) {
140105197Ssam		DPRINTF(("ipsec_process_done: could not get packet tag\n"));
141105197Ssam		error = ENOMEM;
142105197Ssam		goto bad;
143105197Ssam	}
144105197Ssam
145105197Ssam	tdbi = (struct tdb_ident *)(mtag + 1);
146105197Ssam	tdbi->dst = saidx->dst;
147105197Ssam	tdbi->proto = saidx->proto;
148105197Ssam	tdbi->spi = sav->spi;
149105197Ssam	m_tag_prepend(m, mtag);
150105197Ssam
151105197Ssam	/*
152105197Ssam	 * If there's another (bundled) SA to apply, do so.
153105197Ssam	 * Note that this puts a burden on the kernel stack size.
154105197Ssam	 * If this is a problem we'll need to introduce a queue
155105197Ssam	 * to set the packet on so we can unwind the stack before
156105197Ssam	 * doing further processing.
157105197Ssam	 */
158105197Ssam	if (isr->next) {
159105197Ssam		newipsecstat.ips_out_bundlesa++;
160105197Ssam		return ipsec4_process_packet(m, isr->next, 0, 0);
161105197Ssam	}
162105197Ssam
163105197Ssam	/*
164105197Ssam	 * We're done with IPsec processing, transmit the packet using the
165105197Ssam	 * appropriate network protocol (IP or IPv6). SPD lookup will be
166105197Ssam	 * performed again there.
167105197Ssam	 */
168105197Ssam	switch (saidx->dst.sa.sa_family) {
169105197Ssam#ifdef INET
170105197Ssam	struct ip *ip;
171105197Ssam	case AF_INET:
172105197Ssam		ip = mtod(m, struct ip *);
173105197Ssam		ip->ip_len = ntohs(ip->ip_len);
174105197Ssam		ip->ip_off = ntohs(ip->ip_off);
175105197Ssam
176105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
177105197Ssam#endif /* INET */
178105197Ssam#ifdef INET6
179105197Ssam	case AF_INET6:
180105197Ssam		/*
181105197Ssam		 * We don't need massage, IPv6 header fields are always in
182105197Ssam		 * net endian.
183105197Ssam		 */
184105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
185105197Ssam#endif /* INET6 */
186105197Ssam	}
187105197Ssam	panic("ipsec_process_done");
188105197Ssambad:
189105197Ssam	m_freem(m);
190105197Ssam	KEY_FREESAV(&sav);
191105197Ssam	return (error);
192105197Ssam}
193105197Ssam
194105197Ssamstatic struct ipsecrequest *
195105197Ssamipsec_nextisr(
196105197Ssam	struct mbuf *m,
197105197Ssam	struct ipsecrequest *isr,
198105197Ssam	int af,
199105197Ssam	struct secasindex *saidx,
200105197Ssam	int *error
201105197Ssam)
202105197Ssam{
203105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
204105197Ssam			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
205105197Ssam	struct secasvar *sav;
206105197Ssam
207105197Ssam#if 0
208105197Ssam	SPLASSERT(net, "ipsec_nextisr");
209105197Ssam#endif
210105197Ssam	KASSERT(af == AF_INET || af == AF_INET6,
211105197Ssam		("ipsec_nextisr: invalid address family %u", af));
212105197Ssamagain:
213105197Ssam	/*
214105197Ssam	 * Craft SA index to search for proper SA.  Note that
215105197Ssam	 * we only fillin unspecified SA peers for transport
216105197Ssam	 * mode; for tunnel mode they must already be filled in.
217105197Ssam	 */
218105197Ssam	*saidx = isr->saidx;
219105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
220105197Ssam		/* Fillin unspecified SA peers only for transport mode */
221105197Ssam		if (af == AF_INET) {
222105197Ssam			struct sockaddr_in *sin;
223105197Ssam			struct ip *ip = mtod(m, struct ip *);
224105197Ssam
225105197Ssam			if (saidx->src.sa.sa_len == 0) {
226105197Ssam				sin = &saidx->src.sin;
227105197Ssam				sin->sin_len = sizeof(*sin);
228105197Ssam				sin->sin_family = AF_INET;
229105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
230105197Ssam				sin->sin_addr = ip->ip_src;
231105197Ssam			}
232105197Ssam			if (saidx->dst.sa.sa_len == 0) {
233105197Ssam				sin = &saidx->dst.sin;
234105197Ssam				sin->sin_len = sizeof(*sin);
235105197Ssam				sin->sin_family = AF_INET;
236105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
237105197Ssam				sin->sin_addr = ip->ip_dst;
238105197Ssam			}
239105197Ssam		} else {
240105197Ssam			struct sockaddr_in6 *sin6;
241105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
242105197Ssam
243105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
244105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
245105197Ssam				sin6->sin6_len = sizeof(*sin6);
246105197Ssam				sin6->sin6_family = AF_INET6;
247105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
248105197Ssam				sin6->sin6_addr = ip6->ip6_src;
249105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
250105197Ssam					/* fix scope id for comparing SPD */
251105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
252105197Ssam					sin6->sin6_scope_id =
253105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
254105197Ssam				}
255105197Ssam			}
256105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
257105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
258105197Ssam				sin6->sin6_len = sizeof(*sin6);
259105197Ssam				sin6->sin6_family = AF_INET6;
260105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
261105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
262105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
263105197Ssam					/* fix scope id for comparing SPD */
264105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
265105197Ssam					sin6->sin6_scope_id =
266105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
267105197Ssam				}
268105197Ssam			}
269105197Ssam		}
270105197Ssam	}
271105197Ssam
272105197Ssam	/*
273105197Ssam	 * Lookup SA and validate it.
274105197Ssam	 */
275105197Ssam	*error = key_checkrequest(isr, saidx);
276105197Ssam	if (*error != 0) {
277105197Ssam		/*
278105197Ssam		 * IPsec processing is required, but no SA found.
279105197Ssam		 * I assume that key_acquire() had been called
280105197Ssam		 * to get/establish the SA. Here I discard
281105197Ssam		 * this packet because it is responsibility for
282105197Ssam		 * upper layer to retransmit the packet.
283105197Ssam		 */
284105197Ssam		newipsecstat.ips_out_nosa++;
285105197Ssam		goto bad;
286105197Ssam	}
287105197Ssam	sav = isr->sav;
288105197Ssam	if (sav == NULL) {		/* XXX valid return */
289105197Ssam		KASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
290105197Ssam			("ipsec_nextisr: no SA found, but required; level %u",
291105197Ssam			ipsec_get_reqlevel(isr)));
292105197Ssam		isr = isr->next;
293105197Ssam		if (isr == NULL) {
294105197Ssam			/*XXXstatistic??*/
295105197Ssam			*error = EINVAL;		/*XXX*/
296105197Ssam			return isr;
297105197Ssam		}
298105197Ssam		goto again;
299105197Ssam	}
300105197Ssam
301105197Ssam	/*
302105197Ssam	 * Check system global policy controls.
303105197Ssam	 */
304105197Ssam	if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
305105197Ssam	    (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
306105197Ssam	    (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
307105197Ssam		DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
308105197Ssam			" to policy (check your sysctls)\n"));
309105197Ssam		IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
310105197Ssam		    ipcompstat.ipcomps_pdrops);
311105197Ssam		*error = EHOSTUNREACH;
312105197Ssam		goto bad;
313105197Ssam	}
314105197Ssam
315105197Ssam	/*
316105197Ssam	 * Sanity check the SA contents for the caller
317105197Ssam	 * before they invoke the xform output method.
318105197Ssam	 */
319105197Ssam	if (sav->tdb_xform == NULL) {
320105197Ssam		DPRINTF(("ipsec_nextisr: no transform for SA\n"));
321105197Ssam		IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
322105197Ssam		    ipcompstat.ipcomps_noxform);
323105197Ssam		*error = EHOSTUNREACH;
324105197Ssam		goto bad;
325105197Ssam	}
326105197Ssam	return isr;
327105197Ssambad:
328105197Ssam	KASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
329105197Ssam	return NULL;
330105197Ssam#undef IPSEC_OSTAT
331105197Ssam}
332105197Ssam
333105197Ssam#ifdef INET
334105197Ssam/*
335105197Ssam * IPsec output logic for IPv4.
336105197Ssam */
337105197Ssamint
338105197Ssamipsec4_process_packet(
339105197Ssam	struct mbuf *m,
340105197Ssam	struct ipsecrequest *isr,
341105197Ssam	int flags,
342105197Ssam	int tunalready)
343105197Ssam{
344105197Ssam	struct secasindex saidx;
345105197Ssam	struct secasvar *sav;
346105197Ssam	struct ip *ip;
347105197Ssam	int s, error, i, off;
348105197Ssam
349105197Ssam	KASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
350105197Ssam	KASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
351105197Ssam
352105197Ssam	s = splnet();			/* insure SA contents don't change */
353105197Ssam
354105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
355105197Ssam	if (isr == NULL)
356105197Ssam		goto bad;
357105197Ssam
358105197Ssam	sav = isr->sav;
359105197Ssam	if (!tunalready) {
360105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
361105197Ssam		int setdf;
362105197Ssam
363105197Ssam		/*
364105197Ssam		 * Collect IP_DF state from the outer header.
365105197Ssam		 */
366105197Ssam		if (dst->sa.sa_family == AF_INET) {
367105197Ssam			if (m->m_len < sizeof (struct ip) &&
368105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
369105197Ssam				error = ENOBUFS;
370105197Ssam				goto bad;
371105197Ssam			}
372105197Ssam			ip = mtod(m, struct ip *);
373105197Ssam			/* Honor system-wide control of how to handle IP_DF */
374105197Ssam			switch (ip4_ipsec_dfbit) {
375105197Ssam			case 0:			/* clear in outer header */
376105197Ssam			case 1:			/* set in outer header */
377105197Ssam				setdf = ip4_ipsec_dfbit;
378105197Ssam				break;
379105197Ssam			default:		/* propagate to outer header */
380105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
381105197Ssam				break;
382105197Ssam			}
383105197Ssam		} else {
384105197Ssam			ip = NULL;		/* keep compiler happy */
385105197Ssam			setdf = 0;
386105197Ssam		}
387105197Ssam		/* Do the appropriate encapsulation, if necessary */
388105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
389105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
390105197Ssam#if 0
391105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
392105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
393105197Ssam#endif
394105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
395105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
396105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
397105197Ssam			struct mbuf *mp;
398105197Ssam
399105197Ssam			/* Fix IPv4 header checksum and length */
400105197Ssam			if (m->m_len < sizeof (struct ip) &&
401105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
402105197Ssam				error = ENOBUFS;
403105197Ssam				goto bad;
404105197Ssam			}
405105197Ssam			ip = mtod(m, struct ip *);
406105197Ssam			ip->ip_len = htons(m->m_pkthdr.len);
407105197Ssam			ip->ip_sum = 0;
408105197Ssam#ifdef _IP_VHL
409105197Ssam			if (ip->ip_vhl == IP_VHL_BORING)
410105197Ssam				ip->ip_sum = in_cksum_hdr(ip);
411105197Ssam			else
412105197Ssam				ip->ip_sum = in_cksum(m,
413105197Ssam					_IP_VHL_HL(ip->ip_vhl) << 2);
414105197Ssam#else
415105197Ssam			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
416105197Ssam#endif
417105197Ssam
418105197Ssam			/* Encapsulate the packet */
419105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
420105197Ssam			if (mp == NULL && !error) {
421105197Ssam				/* Should never happen. */
422105197Ssam				DPRINTF(("ipsec4_process_packet: ipip_output "
423105197Ssam					"returns no mbuf and no error!"));
424105197Ssam				error = EFAULT;
425105197Ssam			}
426105197Ssam			if (error) {
427105197Ssam				if (mp)
428105197Ssam					m_freem(mp);
429105197Ssam				goto bad;
430105197Ssam			}
431105197Ssam			m = mp, mp = NULL;
432105197Ssam			/*
433105197Ssam			 * ipip_output clears IP_DF in the new header.  If
434105197Ssam			 * we need to propagate IP_DF from the outer header,
435105197Ssam			 * then we have to do it here.
436105197Ssam			 *
437105197Ssam			 * XXX shouldn't assume what ipip_output does.
438105197Ssam			 */
439105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
440105197Ssam				if (m->m_len < sizeof (struct ip) &&
441105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
442105197Ssam					error = ENOBUFS;
443105197Ssam					goto bad;
444105197Ssam				}
445105197Ssam				ip = mtod(m, struct ip *);
446105197Ssam				ip->ip_off = ntohs(ip->ip_off);
447105197Ssam				ip->ip_off |= IP_DF;
448105197Ssam				ip->ip_off = htons(ip->ip_off);
449105197Ssam			}
450105197Ssam		}
451105197Ssam	}
452105197Ssam
453105197Ssam	/*
454105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
455105197Ssam	 * packet will be returned for transmission after crypto
456105197Ssam	 * processing, etc. are completed.  For encapsulation we
457105197Ssam	 * bypass this call because of the explicit call done above
458105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
459105197Ssam	 *
460105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
461105197Ssam	 *     for reclaiming their resources.
462105197Ssam	 */
463105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
464105197Ssam		ip = mtod(m, struct ip *);
465105197Ssam		i = ip->ip_hl << 2;
466105197Ssam		off = offsetof(struct ip, ip_p);
467105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
468105197Ssam	} else {
469105197Ssam		error = ipsec_process_done(m, isr);
470105197Ssam	}
471105197Ssam	splx(s);
472105197Ssam	return error;
473105197Ssambad:
474105197Ssam	splx(s);
475105197Ssam	if (m)
476105197Ssam		m_freem(m);
477105197Ssam	return error;
478105197Ssam}
479105197Ssam#endif
480105197Ssam
481105197Ssam#ifdef INET6
482105197Ssam/*
483105197Ssam * Chop IP6 header from the payload.
484105197Ssam */
485105197Ssamstatic struct mbuf *
486105197Ssamipsec6_splithdr(struct mbuf *m)
487105197Ssam{
488105197Ssam	struct mbuf *mh;
489105197Ssam	struct ip6_hdr *ip6;
490105197Ssam	int hlen;
491105197Ssam
492105197Ssam	KASSERT(m->m_len >= sizeof (struct ip6_hdr),
493105197Ssam		("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
494105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
495105197Ssam	hlen = sizeof(struct ip6_hdr);
496105197Ssam	if (m->m_len > hlen) {
497111119Simp		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
498105197Ssam		if (!mh) {
499105197Ssam			m_freem(m);
500105197Ssam			return NULL;
501105197Ssam		}
502108466Ssam		M_MOVE_PKTHDR(mh, m);
503105197Ssam		MH_ALIGN(mh, hlen);
504105197Ssam		m->m_len -= hlen;
505105197Ssam		m->m_data += hlen;
506105197Ssam		mh->m_next = m;
507105197Ssam		m = mh;
508105197Ssam		m->m_len = hlen;
509105197Ssam		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
510105197Ssam	} else if (m->m_len < hlen) {
511105197Ssam		m = m_pullup(m, hlen);
512105197Ssam		if (!m)
513105197Ssam			return NULL;
514105197Ssam	}
515105197Ssam	return m;
516105197Ssam}
517105197Ssam
518105197Ssam/*
519105197Ssam * IPsec output logic for IPv6, transport mode.
520105197Ssam */
521105197Ssamint
522105197Ssamipsec6_output_trans(
523105197Ssam	struct ipsec_output_state *state,
524105197Ssam	u_char *nexthdrp,
525105197Ssam	struct mbuf *mprev,
526105197Ssam	struct secpolicy *sp,
527105197Ssam	int flags,
528105197Ssam	int *tun)
529105197Ssam{
530105197Ssam	struct ipsecrequest *isr;
531105197Ssam	struct secasindex saidx;
532105197Ssam	int error = 0;
533105197Ssam	struct mbuf *m;
534105197Ssam
535105197Ssam	KASSERT(state != NULL, ("ipsec6_output: null state"));
536105197Ssam	KASSERT(state->m != NULL, ("ipsec6_output: null m"));
537105197Ssam	KASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
538105197Ssam	KASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
539105197Ssam	KASSERT(sp != NULL, ("ipsec6_output: null sp"));
540105197Ssam	KASSERT(tun != NULL, ("ipsec6_output: null tun"));
541105197Ssam
542105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
543105197Ssam		printf("ipsec6_output_trans: applyed SP\n");
544105197Ssam		kdebug_secpolicy(sp));
545105197Ssam
546105197Ssam	isr = sp->req;
547105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
548105197Ssam		/* the rest will be handled by ipsec6_output_tunnel() */
549105197Ssam		*tun = 1;		/* need tunnel-mode processing */
550105197Ssam		return 0;
551105197Ssam	}
552105197Ssam
553105197Ssam	*tun = 0;
554105197Ssam	m = state->m;
555105197Ssam
556105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
557105197Ssam	if (isr == NULL) {
558105197Ssam#ifdef notdef
559105197Ssam		/* XXX should notification be done for all errors ? */
560105197Ssam		/*
561105197Ssam		 * Notify the fact that the packet is discarded
562105197Ssam		 * to ourselves. I believe this is better than
563105197Ssam		 * just silently discarding. (jinmei@kame.net)
564105197Ssam		 * XXX: should we restrict the error to TCP packets?
565105197Ssam		 * XXX: should we directly notify sockets via
566105197Ssam		 *      pfctlinputs?
567105197Ssam		 */
568105197Ssam		icmp6_error(m, ICMP6_DST_UNREACH,
569105197Ssam			    ICMP6_DST_UNREACH_ADMIN, 0);
570105197Ssam		m = NULL;	/* NB: icmp6_error frees mbuf */
571105197Ssam#endif
572105197Ssam		goto bad;
573105197Ssam	}
574105197Ssam
575105197Ssam	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
576105197Ssam		sizeof (struct ip6_hdr),
577105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
578105197Ssambad:
579105197Ssam	if (m)
580105197Ssam		m_freem(m);
581105197Ssam	state->m = NULL;
582105197Ssam	return error;
583105197Ssam}
584105197Ssam
585105197Ssamstatic int
586105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
587105197Ssam{
588105197Ssam	struct ip6_hdr *oip6;
589105197Ssam	struct ip6_hdr *ip6;
590105197Ssam	size_t plen;
591105197Ssam
592105197Ssam	/* can't tunnel between different AFs */
593105197Ssam	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
594105197Ssam	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
595105197Ssam		m_freem(m);
596105197Ssam		return EINVAL;
597105197Ssam	}
598105197Ssam	KASSERT(m->m_len != sizeof (struct ip6_hdr),
599105197Ssam		("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
600105197Ssam
601105197Ssam
602105197Ssam	/*
603105197Ssam	 * grow the mbuf to accomodate the new IPv6 header.
604105197Ssam	 */
605105197Ssam	plen = m->m_pkthdr.len;
606105197Ssam	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
607105197Ssam		struct mbuf *n;
608111119Simp		MGET(n, M_DONTWAIT, MT_DATA);
609105197Ssam		if (!n) {
610105197Ssam			m_freem(m);
611105197Ssam			return ENOBUFS;
612105197Ssam		}
613105197Ssam		n->m_len = sizeof(struct ip6_hdr);
614105197Ssam		n->m_next = m->m_next;
615105197Ssam		m->m_next = n;
616105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
617105197Ssam		oip6 = mtod(n, struct ip6_hdr *);
618105197Ssam	} else {
619105197Ssam		m->m_next->m_len += sizeof(struct ip6_hdr);
620105197Ssam		m->m_next->m_data -= sizeof(struct ip6_hdr);
621105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
622105197Ssam		oip6 = mtod(m->m_next, struct ip6_hdr *);
623105197Ssam	}
624105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
625105197Ssam	ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
626105197Ssam
627105197Ssam	/* Fake link-local scope-class addresses */
628105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
629105197Ssam		oip6->ip6_src.s6_addr16[1] = 0;
630105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
631105197Ssam		oip6->ip6_dst.s6_addr16[1] = 0;
632105197Ssam
633105197Ssam	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
634105197Ssam	/* ECN consideration. */
635105197Ssam	ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
636105197Ssam	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
637105197Ssam		ip6->ip6_plen = htons(plen);
638105197Ssam	else {
639105197Ssam		/* ip6->ip6_plen will be updated in ip6_output() */
640105197Ssam	}
641105197Ssam	ip6->ip6_nxt = IPPROTO_IPV6;
642105197Ssam	sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
643105197Ssam	sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
644105197Ssam	ip6->ip6_hlim = IPV6_DEFHLIM;
645105197Ssam
646105197Ssam	/* XXX Should ip6_src be updated later ? */
647105197Ssam
648105197Ssam	return 0;
649105197Ssam}
650105197Ssam
651105197Ssam/*
652105197Ssam * IPsec output logic for IPv6, tunnel mode.
653105197Ssam */
654105197Ssamint
655105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
656105197Ssam{
657105197Ssam	struct ip6_hdr *ip6;
658105197Ssam	struct ipsecrequest *isr;
659105197Ssam	struct secasindex saidx;
660105197Ssam	int error;
661105197Ssam	struct sockaddr_in6* dst6;
662105197Ssam	struct mbuf *m;
663105197Ssam
664105197Ssam	KASSERT(state != NULL, ("ipsec6_output: null state"));
665105197Ssam	KASSERT(state->m != NULL, ("ipsec6_output: null m"));
666105197Ssam	KASSERT(sp != NULL, ("ipsec6_output: null sp"));
667105197Ssam
668105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
669105197Ssam		printf("ipsec6_output_tunnel: applyed SP\n");
670105197Ssam		kdebug_secpolicy(sp));
671105197Ssam
672105197Ssam	m = state->m;
673105197Ssam	/*
674105197Ssam	 * transport mode ipsec (before the 1st tunnel mode) is already
675105197Ssam	 * processed by ipsec6_output_trans().
676105197Ssam	 */
677105197Ssam	for (isr = sp->req; isr; isr = isr->next) {
678105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
679105197Ssam			break;
680105197Ssam	}
681105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
682105197Ssam	if (isr == NULL)
683105197Ssam		goto bad;
684105197Ssam
685105197Ssam	/*
686105197Ssam	 * There may be the case that SA status will be changed when
687105197Ssam	 * we are refering to one. So calling splsoftnet().
688105197Ssam	 */
689105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
690105197Ssam		/*
691105197Ssam		 * build IPsec tunnel.
692105197Ssam		 */
693105197Ssam		/* XXX should be processed with other familiy */
694105197Ssam		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
695105197Ssam			ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
696105197Ssam			    "family mismatched between inner and outer, spi=%u\n",
697105197Ssam			    ntohl(isr->sav->spi)));
698105197Ssam			newipsecstat.ips_out_inval++;
699105197Ssam			error = EAFNOSUPPORT;
700105197Ssam			goto bad;
701105197Ssam		}
702105197Ssam
703105197Ssam		m = ipsec6_splithdr(m);
704105197Ssam		if (!m) {
705105197Ssam			newipsecstat.ips_out_nomem++;
706105197Ssam			error = ENOMEM;
707105197Ssam			goto bad;
708105197Ssam		}
709105197Ssam		error = ipsec6_encapsulate(m, isr->sav);
710105197Ssam		if (error) {
711105197Ssam			m = NULL;
712105197Ssam			goto bad;
713105197Ssam		}
714105197Ssam		ip6 = mtod(m, struct ip6_hdr *);
715105197Ssam
716105197Ssam		state->ro = &isr->sav->sah->sa_route;
717105197Ssam		state->dst = (struct sockaddr *)&state->ro->ro_dst;
718105197Ssam		dst6 = (struct sockaddr_in6 *)state->dst;
719105197Ssam		if (state->ro->ro_rt
720105197Ssam		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
721105197Ssam		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
722105197Ssam			RTFREE(state->ro->ro_rt);
723105197Ssam			state->ro->ro_rt = NULL;
724105197Ssam		}
725105197Ssam		if (state->ro->ro_rt == 0) {
726105197Ssam			bzero(dst6, sizeof(*dst6));
727105197Ssam			dst6->sin6_family = AF_INET6;
728105197Ssam			dst6->sin6_len = sizeof(*dst6);
729105197Ssam			dst6->sin6_addr = ip6->ip6_dst;
730105197Ssam			rtalloc(state->ro);
731105197Ssam		}
732105197Ssam		if (state->ro->ro_rt == 0) {
733105197Ssam			ip6stat.ip6s_noroute++;
734105197Ssam			newipsecstat.ips_out_noroute++;
735105197Ssam			error = EHOSTUNREACH;
736105197Ssam			goto bad;
737105197Ssam		}
738105197Ssam
739105197Ssam		/* adjust state->dst if tunnel endpoint is offlink */
740105197Ssam		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
741105197Ssam			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
742105197Ssam			dst6 = (struct sockaddr_in6 *)state->dst;
743105197Ssam		}
744105197Ssam	}
745105197Ssam
746105197Ssam	m = ipsec6_splithdr(m);
747105197Ssam	if (!m) {
748105197Ssam		newipsecstat.ips_out_nomem++;
749105197Ssam		error = ENOMEM;
750105197Ssam		goto bad;
751105197Ssam	}
752105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
753105197Ssam	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
754105197Ssam		sizeof (struct ip6_hdr),
755105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
756105197Ssambad:
757105197Ssam	if (m)
758105197Ssam		m_freem(m);
759105197Ssam	state->m = NULL;
760105197Ssam	return error;
761105197Ssam}
762105197Ssam#endif /*INET6*/
763