ipsec_output.c revision 124765
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 124765 2004-01-20 22:45:10Z 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
92120585Ssam	IPSEC_SPLASSERT_SOFTNET(__func__);
93105197Ssam
94120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
95120585Ssam	IPSEC_ASSERT(isr != NULL, ("null ISR"));
96105197Ssam	sav = isr->sav;
97120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
98120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
99105197Ssam
100105197Ssam	saidx = &sav->sah->saidx;
101105197Ssam	switch (saidx->dst.sa.sa_family) {
102105197Ssam#ifdef INET
103105197Ssam	case AF_INET:
104105197Ssam		/* Fix the header length, for AH processing. */
105105197Ssam		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
106105197Ssam		break;
107105197Ssam#endif /* INET */
108105197Ssam#ifdef INET6
109105197Ssam	case AF_INET6:
110105197Ssam		/* Fix the header length, for AH processing. */
111105197Ssam		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
112105197Ssam			error = ENXIO;
113105197Ssam			goto bad;
114105197Ssam		}
115105197Ssam		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
116105197Ssam			/* No jumbogram support. */
117105197Ssam			error = ENXIO;	/*?*/
118105197Ssam			goto bad;
119105197Ssam		}
120105197Ssam		mtod(m, struct ip6_hdr *)->ip6_plen =
121105197Ssam			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
122105197Ssam		break;
123105197Ssam#endif /* INET6 */
124105197Ssam	default:
125120585Ssam		DPRINTF(("%s: unknown protocol family %u\n", __func__,
126105197Ssam		    saidx->dst.sa.sa_family));
127105197Ssam		error = ENXIO;
128105197Ssam		goto bad;
129105197Ssam	}
130105197Ssam
131105197Ssam	/*
132105197Ssam	 * Add a record of what we've done or what needs to be done to the
133105197Ssam	 * packet.
134105197Ssam	 */
135105197Ssam	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
136105197Ssam			sizeof(struct tdb_ident), M_NOWAIT);
137105197Ssam	if (mtag == NULL) {
138120585Ssam		DPRINTF(("%s: could not get packet tag\n", __func__));
139105197Ssam		error = ENOMEM;
140105197Ssam		goto bad;
141105197Ssam	}
142105197Ssam
143105197Ssam	tdbi = (struct tdb_ident *)(mtag + 1);
144105197Ssam	tdbi->dst = saidx->dst;
145105197Ssam	tdbi->proto = saidx->proto;
146105197Ssam	tdbi->spi = sav->spi;
147105197Ssam	m_tag_prepend(m, mtag);
148105197Ssam
149105197Ssam	/*
150105197Ssam	 * If there's another (bundled) SA to apply, do so.
151105197Ssam	 * Note that this puts a burden on the kernel stack size.
152105197Ssam	 * If this is a problem we'll need to introduce a queue
153105197Ssam	 * to set the packet on so we can unwind the stack before
154105197Ssam	 * doing further processing.
155105197Ssam	 */
156105197Ssam	if (isr->next) {
157105197Ssam		newipsecstat.ips_out_bundlesa++;
158105197Ssam		return ipsec4_process_packet(m, isr->next, 0, 0);
159105197Ssam	}
160117056Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
161105197Ssam
162105197Ssam	/*
163105197Ssam	 * We're done with IPsec processing, transmit the packet using the
164105197Ssam	 * appropriate network protocol (IP or IPv6). SPD lookup will be
165105197Ssam	 * performed again there.
166105197Ssam	 */
167105197Ssam	switch (saidx->dst.sa.sa_family) {
168105197Ssam#ifdef INET
169105197Ssam	struct ip *ip;
170105197Ssam	case AF_INET:
171105197Ssam		ip = mtod(m, struct ip *);
172105197Ssam		ip->ip_len = ntohs(ip->ip_len);
173105197Ssam		ip->ip_off = ntohs(ip->ip_off);
174105197Ssam
175105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
176105197Ssam#endif /* INET */
177105197Ssam#ifdef INET6
178105197Ssam	case AF_INET6:
179105197Ssam		/*
180105197Ssam		 * We don't need massage, IPv6 header fields are always in
181105197Ssam		 * net endian.
182105197Ssam		 */
183105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
184105197Ssam#endif /* INET6 */
185105197Ssam	}
186105197Ssam	panic("ipsec_process_done");
187105197Ssambad:
188105197Ssam	m_freem(m);
189105197Ssam	KEY_FREESAV(&sav);
190105197Ssam	return (error);
191105197Ssam}
192105197Ssam
193105197Ssamstatic struct ipsecrequest *
194105197Ssamipsec_nextisr(
195105197Ssam	struct mbuf *m,
196105197Ssam	struct ipsecrequest *isr,
197105197Ssam	int af,
198105197Ssam	struct secasindex *saidx,
199105197Ssam	int *error
200105197Ssam)
201105197Ssam{
202105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
203105197Ssam			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
204105197Ssam	struct secasvar *sav;
205105197Ssam
206120585Ssam	IPSEC_SPLASSERT_SOFTNET(__func__);
207120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
208120585Ssam
209120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
210120585Ssam		("invalid address family %u", af));
211105197Ssamagain:
212105197Ssam	/*
213105197Ssam	 * Craft SA index to search for proper SA.  Note that
214105197Ssam	 * we only fillin unspecified SA peers for transport
215105197Ssam	 * mode; for tunnel mode they must already be filled in.
216105197Ssam	 */
217105197Ssam	*saidx = isr->saidx;
218105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
219105197Ssam		/* Fillin unspecified SA peers only for transport mode */
220105197Ssam		if (af == AF_INET) {
221105197Ssam			struct sockaddr_in *sin;
222105197Ssam			struct ip *ip = mtod(m, struct ip *);
223105197Ssam
224105197Ssam			if (saidx->src.sa.sa_len == 0) {
225105197Ssam				sin = &saidx->src.sin;
226105197Ssam				sin->sin_len = sizeof(*sin);
227105197Ssam				sin->sin_family = AF_INET;
228105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
229105197Ssam				sin->sin_addr = ip->ip_src;
230105197Ssam			}
231105197Ssam			if (saidx->dst.sa.sa_len == 0) {
232105197Ssam				sin = &saidx->dst.sin;
233105197Ssam				sin->sin_len = sizeof(*sin);
234105197Ssam				sin->sin_family = AF_INET;
235105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
236105197Ssam				sin->sin_addr = ip->ip_dst;
237105197Ssam			}
238105197Ssam		} else {
239105197Ssam			struct sockaddr_in6 *sin6;
240105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
241105197Ssam
242105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
243105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
244105197Ssam				sin6->sin6_len = sizeof(*sin6);
245105197Ssam				sin6->sin6_family = AF_INET6;
246105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
247105197Ssam				sin6->sin6_addr = ip6->ip6_src;
248105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
249105197Ssam					/* fix scope id for comparing SPD */
250105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
251105197Ssam					sin6->sin6_scope_id =
252105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
253105197Ssam				}
254105197Ssam			}
255105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
256105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
257105197Ssam				sin6->sin6_len = sizeof(*sin6);
258105197Ssam				sin6->sin6_family = AF_INET6;
259105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
260105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
261105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
262105197Ssam					/* fix scope id for comparing SPD */
263105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
264105197Ssam					sin6->sin6_scope_id =
265105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
266105197Ssam				}
267105197Ssam			}
268105197Ssam		}
269105197Ssam	}
270105197Ssam
271105197Ssam	/*
272105197Ssam	 * Lookup SA and validate it.
273105197Ssam	 */
274105197Ssam	*error = key_checkrequest(isr, saidx);
275105197Ssam	if (*error != 0) {
276105197Ssam		/*
277105197Ssam		 * IPsec processing is required, but no SA found.
278105197Ssam		 * I assume that key_acquire() had been called
279105197Ssam		 * to get/establish the SA. Here I discard
280105197Ssam		 * this packet because it is responsibility for
281105197Ssam		 * upper layer to retransmit the packet.
282105197Ssam		 */
283105197Ssam		newipsecstat.ips_out_nosa++;
284105197Ssam		goto bad;
285105197Ssam	}
286105197Ssam	sav = isr->sav;
287105197Ssam	if (sav == NULL) {		/* XXX valid return */
288120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
289120585Ssam			("no SA found, but required; level %u",
290105197Ssam			ipsec_get_reqlevel(isr)));
291120585Ssam		IPSECREQUEST_UNLOCK(isr);
292105197Ssam		isr = isr->next;
293105197Ssam		if (isr == NULL) {
294105197Ssam			/*XXXstatistic??*/
295105197Ssam			*error = EINVAL;		/*XXX*/
296105197Ssam			return isr;
297105197Ssam		}
298120585Ssam		IPSECREQUEST_LOCK(isr);
299105197Ssam		goto again;
300105197Ssam	}
301105197Ssam
302105197Ssam	/*
303105197Ssam	 * Check system global policy controls.
304105197Ssam	 */
305105197Ssam	if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
306105197Ssam	    (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
307105197Ssam	    (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
308120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
309120585Ssam			" to policy (check your sysctls)\n", __func__));
310105197Ssam		IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
311105197Ssam		    ipcompstat.ipcomps_pdrops);
312105197Ssam		*error = EHOSTUNREACH;
313105197Ssam		goto bad;
314105197Ssam	}
315105197Ssam
316105197Ssam	/*
317105197Ssam	 * Sanity check the SA contents for the caller
318105197Ssam	 * before they invoke the xform output method.
319105197Ssam	 */
320105197Ssam	if (sav->tdb_xform == NULL) {
321120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
322105197Ssam		IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
323105197Ssam		    ipcompstat.ipcomps_noxform);
324105197Ssam		*error = EHOSTUNREACH;
325105197Ssam		goto bad;
326105197Ssam	}
327105197Ssam	return isr;
328105197Ssambad:
329120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
330120585Ssam	IPSECREQUEST_UNLOCK(isr);
331105197Ssam	return NULL;
332105197Ssam#undef IPSEC_OSTAT
333105197Ssam}
334105197Ssam
335105197Ssam#ifdef INET
336105197Ssam/*
337105197Ssam * IPsec output logic for IPv4.
338105197Ssam */
339105197Ssamint
340105197Ssamipsec4_process_packet(
341105197Ssam	struct mbuf *m,
342105197Ssam	struct ipsecrequest *isr,
343105197Ssam	int flags,
344105197Ssam	int tunalready)
345105197Ssam{
346105197Ssam	struct secasindex saidx;
347105197Ssam	struct secasvar *sav;
348105197Ssam	struct ip *ip;
349119643Ssam	int error, i, off;
350105197Ssam
351120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
352120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
353105197Ssam
354120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
355105197Ssam
356105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
357105197Ssam	if (isr == NULL)
358105197Ssam		goto bad;
359105197Ssam
360105197Ssam	sav = isr->sav;
361105197Ssam	if (!tunalready) {
362105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
363105197Ssam		int setdf;
364105197Ssam
365105197Ssam		/*
366105197Ssam		 * Collect IP_DF state from the outer header.
367105197Ssam		 */
368105197Ssam		if (dst->sa.sa_family == AF_INET) {
369105197Ssam			if (m->m_len < sizeof (struct ip) &&
370105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
371105197Ssam				error = ENOBUFS;
372105197Ssam				goto bad;
373105197Ssam			}
374105197Ssam			ip = mtod(m, struct ip *);
375105197Ssam			/* Honor system-wide control of how to handle IP_DF */
376105197Ssam			switch (ip4_ipsec_dfbit) {
377105197Ssam			case 0:			/* clear in outer header */
378105197Ssam			case 1:			/* set in outer header */
379105197Ssam				setdf = ip4_ipsec_dfbit;
380105197Ssam				break;
381105197Ssam			default:		/* propagate to outer header */
382105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
383105197Ssam				break;
384105197Ssam			}
385105197Ssam		} else {
386105197Ssam			ip = NULL;		/* keep compiler happy */
387105197Ssam			setdf = 0;
388105197Ssam		}
389105197Ssam		/* Do the appropriate encapsulation, if necessary */
390105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
391105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
392105197Ssam#if 0
393105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
394105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
395105197Ssam#endif
396105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
397105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
398105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
399105197Ssam			struct mbuf *mp;
400105197Ssam
401105197Ssam			/* Fix IPv4 header checksum and length */
402105197Ssam			if (m->m_len < sizeof (struct ip) &&
403105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
404105197Ssam				error = ENOBUFS;
405105197Ssam				goto bad;
406105197Ssam			}
407105197Ssam			ip = mtod(m, struct ip *);
408105197Ssam			ip->ip_len = htons(m->m_pkthdr.len);
409105197Ssam			ip->ip_sum = 0;
410105197Ssam#ifdef _IP_VHL
411105197Ssam			if (ip->ip_vhl == IP_VHL_BORING)
412105197Ssam				ip->ip_sum = in_cksum_hdr(ip);
413105197Ssam			else
414105197Ssam				ip->ip_sum = in_cksum(m,
415105197Ssam					_IP_VHL_HL(ip->ip_vhl) << 2);
416105197Ssam#else
417105197Ssam			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
418105197Ssam#endif
419105197Ssam
420105197Ssam			/* Encapsulate the packet */
421105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
422105197Ssam			if (mp == NULL && !error) {
423105197Ssam				/* Should never happen. */
424120585Ssam				DPRINTF(("%s: ipip_output returns no mbuf and "
425120585Ssam					"no error!", __func__));
426105197Ssam				error = EFAULT;
427105197Ssam			}
428105197Ssam			if (error) {
429124765Ssam				if (mp) {
430124765Ssam					/* XXX: Should never happen! */
431105197Ssam					m_freem(mp);
432124765Ssam				}
433124765Ssam				m = NULL; /* ipip_output() already freed it */
434105197Ssam				goto bad;
435105197Ssam			}
436105197Ssam			m = mp, mp = NULL;
437105197Ssam			/*
438105197Ssam			 * ipip_output clears IP_DF in the new header.  If
439105197Ssam			 * we need to propagate IP_DF from the outer header,
440105197Ssam			 * then we have to do it here.
441105197Ssam			 *
442105197Ssam			 * XXX shouldn't assume what ipip_output does.
443105197Ssam			 */
444105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
445105197Ssam				if (m->m_len < sizeof (struct ip) &&
446105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
447105197Ssam					error = ENOBUFS;
448105197Ssam					goto bad;
449105197Ssam				}
450105197Ssam				ip = mtod(m, struct ip *);
451105197Ssam				ip->ip_off = ntohs(ip->ip_off);
452105197Ssam				ip->ip_off |= IP_DF;
453105197Ssam				ip->ip_off = htons(ip->ip_off);
454105197Ssam			}
455105197Ssam		}
456105197Ssam	}
457105197Ssam
458105197Ssam	/*
459105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
460105197Ssam	 * packet will be returned for transmission after crypto
461105197Ssam	 * processing, etc. are completed.  For encapsulation we
462105197Ssam	 * bypass this call because of the explicit call done above
463105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
464105197Ssam	 *
465105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
466105197Ssam	 *     for reclaiming their resources.
467105197Ssam	 */
468105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
469105197Ssam		ip = mtod(m, struct ip *);
470105197Ssam		i = ip->ip_hl << 2;
471105197Ssam		off = offsetof(struct ip, ip_p);
472105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
473105197Ssam	} else {
474105197Ssam		error = ipsec_process_done(m, isr);
475105197Ssam	}
476120585Ssam	IPSECREQUEST_UNLOCK(isr);
477105197Ssam	return error;
478105197Ssambad:
479120585Ssam	if (isr)
480120585Ssam		IPSECREQUEST_UNLOCK(isr);
481105197Ssam	if (m)
482105197Ssam		m_freem(m);
483105197Ssam	return error;
484105197Ssam}
485105197Ssam#endif
486105197Ssam
487105197Ssam#ifdef INET6
488105197Ssam/*
489105197Ssam * Chop IP6 header from the payload.
490105197Ssam */
491105197Ssamstatic struct mbuf *
492105197Ssamipsec6_splithdr(struct mbuf *m)
493105197Ssam{
494105197Ssam	struct mbuf *mh;
495105197Ssam	struct ip6_hdr *ip6;
496105197Ssam	int hlen;
497105197Ssam
498120585Ssam	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
499120585Ssam		("first mbuf too short, len %u", m->m_len));
500105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
501105197Ssam	hlen = sizeof(struct ip6_hdr);
502105197Ssam	if (m->m_len > hlen) {
503111119Simp		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
504105197Ssam		if (!mh) {
505105197Ssam			m_freem(m);
506105197Ssam			return NULL;
507105197Ssam		}
508108466Ssam		M_MOVE_PKTHDR(mh, m);
509105197Ssam		MH_ALIGN(mh, hlen);
510105197Ssam		m->m_len -= hlen;
511105197Ssam		m->m_data += hlen;
512105197Ssam		mh->m_next = m;
513105197Ssam		m = mh;
514105197Ssam		m->m_len = hlen;
515105197Ssam		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
516105197Ssam	} else if (m->m_len < hlen) {
517105197Ssam		m = m_pullup(m, hlen);
518105197Ssam		if (!m)
519105197Ssam			return NULL;
520105197Ssam	}
521105197Ssam	return m;
522105197Ssam}
523105197Ssam
524105197Ssam/*
525105197Ssam * IPsec output logic for IPv6, transport mode.
526105197Ssam */
527105197Ssamint
528105197Ssamipsec6_output_trans(
529105197Ssam	struct ipsec_output_state *state,
530105197Ssam	u_char *nexthdrp,
531105197Ssam	struct mbuf *mprev,
532105197Ssam	struct secpolicy *sp,
533105197Ssam	int flags,
534105197Ssam	int *tun)
535105197Ssam{
536105197Ssam	struct ipsecrequest *isr;
537105197Ssam	struct secasindex saidx;
538105197Ssam	int error = 0;
539105197Ssam	struct mbuf *m;
540105197Ssam
541120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
542120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
543120585Ssam	IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
544120585Ssam	IPSEC_ASSERT(mprev != NULL, ("null mprev"));
545120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
546120585Ssam	IPSEC_ASSERT(tun != NULL, ("null tun"));
547105197Ssam
548105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
549120585Ssam		printf("%s: applyed SP\n", __func__);
550105197Ssam		kdebug_secpolicy(sp));
551105197Ssam
552105197Ssam	isr = sp->req;
553105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
554105197Ssam		/* the rest will be handled by ipsec6_output_tunnel() */
555105197Ssam		*tun = 1;		/* need tunnel-mode processing */
556105197Ssam		return 0;
557105197Ssam	}
558105197Ssam
559105197Ssam	*tun = 0;
560105197Ssam	m = state->m;
561105197Ssam
562105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
563105197Ssam	if (isr == NULL) {
564105197Ssam#ifdef notdef
565105197Ssam		/* XXX should notification be done for all errors ? */
566105197Ssam		/*
567105197Ssam		 * Notify the fact that the packet is discarded
568105197Ssam		 * to ourselves. I believe this is better than
569105197Ssam		 * just silently discarding. (jinmei@kame.net)
570105197Ssam		 * XXX: should we restrict the error to TCP packets?
571105197Ssam		 * XXX: should we directly notify sockets via
572105197Ssam		 *      pfctlinputs?
573105197Ssam		 */
574105197Ssam		icmp6_error(m, ICMP6_DST_UNREACH,
575105197Ssam			    ICMP6_DST_UNREACH_ADMIN, 0);
576105197Ssam		m = NULL;	/* NB: icmp6_error frees mbuf */
577105197Ssam#endif
578105197Ssam		goto bad;
579105197Ssam	}
580105197Ssam
581105197Ssam	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
582105197Ssam		sizeof (struct ip6_hdr),
583105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
584105197Ssambad:
585105197Ssam	if (m)
586105197Ssam		m_freem(m);
587105197Ssam	state->m = NULL;
588105197Ssam	return error;
589105197Ssam}
590105197Ssam
591105197Ssamstatic int
592105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
593105197Ssam{
594105197Ssam	struct ip6_hdr *oip6;
595105197Ssam	struct ip6_hdr *ip6;
596105197Ssam	size_t plen;
597105197Ssam
598105197Ssam	/* can't tunnel between different AFs */
599105197Ssam	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
600105197Ssam	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
601105197Ssam		m_freem(m);
602105197Ssam		return EINVAL;
603105197Ssam	}
604120585Ssam	IPSEC_ASSERT(m->m_len != sizeof (struct ip6_hdr),
605120585Ssam		("mbuf wrong size; len %u", m->m_len));
606105197Ssam
607105197Ssam
608105197Ssam	/*
609105197Ssam	 * grow the mbuf to accomodate the new IPv6 header.
610105197Ssam	 */
611105197Ssam	plen = m->m_pkthdr.len;
612105197Ssam	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
613105197Ssam		struct mbuf *n;
614111119Simp		MGET(n, M_DONTWAIT, MT_DATA);
615105197Ssam		if (!n) {
616105197Ssam			m_freem(m);
617105197Ssam			return ENOBUFS;
618105197Ssam		}
619105197Ssam		n->m_len = sizeof(struct ip6_hdr);
620105197Ssam		n->m_next = m->m_next;
621105197Ssam		m->m_next = n;
622105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
623105197Ssam		oip6 = mtod(n, struct ip6_hdr *);
624105197Ssam	} else {
625105197Ssam		m->m_next->m_len += sizeof(struct ip6_hdr);
626105197Ssam		m->m_next->m_data -= sizeof(struct ip6_hdr);
627105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
628105197Ssam		oip6 = mtod(m->m_next, struct ip6_hdr *);
629105197Ssam	}
630105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
631113076Sdes	bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
632105197Ssam
633105197Ssam	/* Fake link-local scope-class addresses */
634105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
635105197Ssam		oip6->ip6_src.s6_addr16[1] = 0;
636105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
637105197Ssam		oip6->ip6_dst.s6_addr16[1] = 0;
638105197Ssam
639105197Ssam	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
640105197Ssam	/* ECN consideration. */
641105197Ssam	ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
642105197Ssam	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
643105197Ssam		ip6->ip6_plen = htons(plen);
644105197Ssam	else {
645105197Ssam		/* ip6->ip6_plen will be updated in ip6_output() */
646105197Ssam	}
647105197Ssam	ip6->ip6_nxt = IPPROTO_IPV6;
648105197Ssam	sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
649105197Ssam	sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
650105197Ssam	ip6->ip6_hlim = IPV6_DEFHLIM;
651105197Ssam
652105197Ssam	/* XXX Should ip6_src be updated later ? */
653105197Ssam
654105197Ssam	return 0;
655105197Ssam}
656105197Ssam
657105197Ssam/*
658105197Ssam * IPsec output logic for IPv6, tunnel mode.
659105197Ssam */
660105197Ssamint
661105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
662105197Ssam{
663105197Ssam	struct ip6_hdr *ip6;
664105197Ssam	struct ipsecrequest *isr;
665105197Ssam	struct secasindex saidx;
666105197Ssam	int error;
667105197Ssam	struct sockaddr_in6* dst6;
668105197Ssam	struct mbuf *m;
669105197Ssam
670120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
671120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
672120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
673105197Ssam
674105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
675120585Ssam		printf("%s: applyed SP\n", __func__);
676105197Ssam		kdebug_secpolicy(sp));
677105197Ssam
678105197Ssam	m = state->m;
679105197Ssam	/*
680105197Ssam	 * transport mode ipsec (before the 1st tunnel mode) is already
681105197Ssam	 * processed by ipsec6_output_trans().
682105197Ssam	 */
683105197Ssam	for (isr = sp->req; isr; isr = isr->next) {
684105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
685105197Ssam			break;
686105197Ssam	}
687105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
688105197Ssam	if (isr == NULL)
689105197Ssam		goto bad;
690105197Ssam
691105197Ssam	/*
692105197Ssam	 * There may be the case that SA status will be changed when
693105197Ssam	 * we are refering to one. So calling splsoftnet().
694105197Ssam	 */
695105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
696105197Ssam		/*
697105197Ssam		 * build IPsec tunnel.
698105197Ssam		 */
699105197Ssam		/* XXX should be processed with other familiy */
700105197Ssam		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
701120585Ssam			ipseclog((LOG_ERR, "%s: family mismatched between "
702120585Ssam			    "inner and outer, spi=%u\n", __func__,
703105197Ssam			    ntohl(isr->sav->spi)));
704105197Ssam			newipsecstat.ips_out_inval++;
705105197Ssam			error = EAFNOSUPPORT;
706105197Ssam			goto bad;
707105197Ssam		}
708105197Ssam
709105197Ssam		m = ipsec6_splithdr(m);
710105197Ssam		if (!m) {
711105197Ssam			newipsecstat.ips_out_nomem++;
712105197Ssam			error = ENOMEM;
713105197Ssam			goto bad;
714105197Ssam		}
715105197Ssam		error = ipsec6_encapsulate(m, isr->sav);
716105197Ssam		if (error) {
717105197Ssam			m = NULL;
718105197Ssam			goto bad;
719105197Ssam		}
720105197Ssam		ip6 = mtod(m, struct ip6_hdr *);
721105197Ssam
722105197Ssam		state->ro = &isr->sav->sah->sa_route;
723105197Ssam		state->dst = (struct sockaddr *)&state->ro->ro_dst;
724105197Ssam		dst6 = (struct sockaddr_in6 *)state->dst;
725105197Ssam		if (state->ro->ro_rt
726105197Ssam		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
727105197Ssam		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
728105197Ssam			RTFREE(state->ro->ro_rt);
729105197Ssam			state->ro->ro_rt = NULL;
730105197Ssam		}
731105197Ssam		if (state->ro->ro_rt == 0) {
732105197Ssam			bzero(dst6, sizeof(*dst6));
733105197Ssam			dst6->sin6_family = AF_INET6;
734105197Ssam			dst6->sin6_len = sizeof(*dst6);
735105197Ssam			dst6->sin6_addr = ip6->ip6_dst;
736105197Ssam			rtalloc(state->ro);
737105197Ssam		}
738105197Ssam		if (state->ro->ro_rt == 0) {
739105197Ssam			ip6stat.ip6s_noroute++;
740105197Ssam			newipsecstat.ips_out_noroute++;
741105197Ssam			error = EHOSTUNREACH;
742105197Ssam			goto bad;
743105197Ssam		}
744105197Ssam
745105197Ssam		/* adjust state->dst if tunnel endpoint is offlink */
746105197Ssam		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
747105197Ssam			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
748105197Ssam			dst6 = (struct sockaddr_in6 *)state->dst;
749105197Ssam		}
750105197Ssam	}
751105197Ssam
752105197Ssam	m = ipsec6_splithdr(m);
753105197Ssam	if (!m) {
754105197Ssam		newipsecstat.ips_out_nomem++;
755105197Ssam		error = ENOMEM;
756105197Ssam		goto bad;
757105197Ssam	}
758105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
759105197Ssam	return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
760105197Ssam		sizeof (struct ip6_hdr),
761105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
762105197Ssambad:
763105197Ssam	if (m)
764105197Ssam		m_freem(m);
765105197Ssam	state->m = NULL;
766105197Ssam	return error;
767105197Ssam}
768105197Ssam#endif /*INET6*/
769