ipsec_output.c revision 213837
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 213837 2010-10-14 15:15:22Z bz $
27112758Ssam */
28105197Ssam
29105197Ssam/*
30105197Ssam * IPsec output processing.
31105197Ssam */
32105197Ssam#include "opt_inet.h"
33105197Ssam#include "opt_inet6.h"
34105197Ssam#include "opt_ipsec.h"
35159965Sthompsa#include "opt_enc.h"
36105197Ssam
37105197Ssam#include <sys/param.h>
38105197Ssam#include <sys/systm.h>
39105197Ssam#include <sys/mbuf.h>
40105197Ssam#include <sys/domain.h>
41105197Ssam#include <sys/protosw.h>
42105197Ssam#include <sys/socket.h>
43105197Ssam#include <sys/errno.h>
44105197Ssam#include <sys/syslog.h>
45105197Ssam
46105197Ssam#include <net/if.h>
47171497Sbz#include <net/pfil.h>
48105197Ssam#include <net/route.h>
49195699Srwatson#include <net/vnet.h>
50105197Ssam
51105197Ssam#include <netinet/in.h>
52105197Ssam#include <netinet/in_systm.h>
53105197Ssam#include <netinet/ip.h>
54105197Ssam#include <netinet/ip_var.h>
55105197Ssam#include <netinet/in_var.h>
56105197Ssam#include <netinet/ip_ecn.h>
57105197Ssam#ifdef INET6
58105197Ssam#include <netinet6/ip6_ecn.h>
59105197Ssam#endif
60105197Ssam
61105197Ssam#include <netinet/ip6.h>
62105197Ssam#ifdef INET6
63105197Ssam#include <netinet6/ip6_var.h>
64105197Ssam#endif
65105197Ssam#include <netinet/in_pcb.h>
66105197Ssam#ifdef INET6
67105197Ssam#include <netinet/icmp6.h>
68105197Ssam#endif
69105197Ssam
70105197Ssam#include <netipsec/ipsec.h>
71105197Ssam#ifdef INET6
72105197Ssam#include <netipsec/ipsec6.h>
73105197Ssam#endif
74105197Ssam#include <netipsec/ah_var.h>
75105197Ssam#include <netipsec/esp_var.h>
76105197Ssam#include <netipsec/ipcomp_var.h>
77105197Ssam
78105197Ssam#include <netipsec/xform.h>
79105197Ssam
80105197Ssam#include <netipsec/key.h>
81105197Ssam#include <netipsec/keydb.h>
82105197Ssam#include <netipsec/key_debug.h>
83105197Ssam
84105197Ssam#include <machine/in_cksum.h>
85105197Ssam
86194062Svanhu#ifdef IPSEC_NAT_T
87194062Svanhu#include <netinet/udp.h>
88194062Svanhu#endif
89194062Svanhu
90181627Svanhu#ifdef DEV_ENC
91181627Svanhu#include <net/if_enc.h>
92181627Svanhu#endif
93181627Svanhu
94181627Svanhu
95105197Ssamint
96105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
97105197Ssam{
98105197Ssam	struct tdb_ident *tdbi;
99105197Ssam	struct m_tag *mtag;
100105197Ssam	struct secasvar *sav;
101105197Ssam	struct secasindex *saidx;
102105197Ssam	int error;
103105197Ssam
104120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
105120585Ssam	IPSEC_ASSERT(isr != NULL, ("null ISR"));
106105197Ssam	sav = isr->sav;
107120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
108120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
109105197Ssam
110105197Ssam	saidx = &sav->sah->saidx;
111105197Ssam	switch (saidx->dst.sa.sa_family) {
112105197Ssam#ifdef INET
113105197Ssam	case AF_INET:
114105197Ssam		/* Fix the header length, for AH processing. */
115105197Ssam		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
116105197Ssam		break;
117105197Ssam#endif /* INET */
118105197Ssam#ifdef INET6
119105197Ssam	case AF_INET6:
120105197Ssam		/* Fix the header length, for AH processing. */
121105197Ssam		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
122105197Ssam			error = ENXIO;
123105197Ssam			goto bad;
124105197Ssam		}
125105197Ssam		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
126105197Ssam			/* No jumbogram support. */
127105197Ssam			error = ENXIO;	/*?*/
128105197Ssam			goto bad;
129105197Ssam		}
130105197Ssam		mtod(m, struct ip6_hdr *)->ip6_plen =
131105197Ssam			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
132105197Ssam		break;
133105197Ssam#endif /* INET6 */
134105197Ssam	default:
135120585Ssam		DPRINTF(("%s: unknown protocol family %u\n", __func__,
136105197Ssam		    saidx->dst.sa.sa_family));
137105197Ssam		error = ENXIO;
138105197Ssam		goto bad;
139105197Ssam	}
140105197Ssam
141105197Ssam	/*
142105197Ssam	 * Add a record of what we've done or what needs to be done to the
143105197Ssam	 * packet.
144105197Ssam	 */
145105197Ssam	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
146105197Ssam			sizeof(struct tdb_ident), M_NOWAIT);
147105197Ssam	if (mtag == NULL) {
148120585Ssam		DPRINTF(("%s: could not get packet tag\n", __func__));
149105197Ssam		error = ENOMEM;
150105197Ssam		goto bad;
151105197Ssam	}
152105197Ssam
153105197Ssam	tdbi = (struct tdb_ident *)(mtag + 1);
154105197Ssam	tdbi->dst = saidx->dst;
155105197Ssam	tdbi->proto = saidx->proto;
156105197Ssam	tdbi->spi = sav->spi;
157105197Ssam	m_tag_prepend(m, mtag);
158105197Ssam
159105197Ssam	/*
160105197Ssam	 * If there's another (bundled) SA to apply, do so.
161105197Ssam	 * Note that this puts a burden on the kernel stack size.
162105197Ssam	 * If this is a problem we'll need to introduce a queue
163105197Ssam	 * to set the packet on so we can unwind the stack before
164105197Ssam	 * doing further processing.
165105197Ssam	 */
166105197Ssam	if (isr->next) {
167181803Sbz		V_ipsec4stat.ips_out_bundlesa++;
168105197Ssam		return ipsec4_process_packet(m, isr->next, 0, 0);
169105197Ssam	}
170117056Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
171105197Ssam
172105197Ssam	/*
173105197Ssam	 * We're done with IPsec processing, transmit the packet using the
174105197Ssam	 * appropriate network protocol (IP or IPv6). SPD lookup will be
175105197Ssam	 * performed again there.
176105197Ssam	 */
177105197Ssam	switch (saidx->dst.sa.sa_family) {
178105197Ssam#ifdef INET
179105197Ssam	struct ip *ip;
180105197Ssam	case AF_INET:
181105197Ssam		ip = mtod(m, struct ip *);
182105197Ssam		ip->ip_len = ntohs(ip->ip_len);
183105197Ssam		ip->ip_off = ntohs(ip->ip_off);
184105197Ssam
185194062Svanhu#ifdef IPSEC_NAT_T
186194062Svanhu		/*
187194062Svanhu		 * If NAT-T is enabled, now that all IPsec processing is done
188194062Svanhu		 * insert UDP encapsulation header after IP header.
189194062Svanhu		 */
190194062Svanhu		if (sav->natt_type) {
191194062Svanhu#ifdef _IP_VHL
192194062Svanhu			const int hlen = IP_VHL_HL(ip->ip_vhl);
193194062Svanhu#else
194194062Svanhu			const int hlen = (ip->ip_hl << 2);
195194062Svanhu#endif
196194062Svanhu			int size, off;
197194062Svanhu			struct mbuf *mi;
198194062Svanhu			struct udphdr *udp;
199194062Svanhu
200194062Svanhu			size = sizeof(struct udphdr);
201194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
202194062Svanhu				/*
203194062Svanhu				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
204194062Svanhu				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
205194062Svanhu				 * ignoring possible AH mode
206194062Svanhu				 * non-IKE marker + non-ESP marker
207194062Svanhu				 * from draft-ietf-ipsec-udp-encaps-00.txt.
208194062Svanhu				 */
209194062Svanhu				size += sizeof(u_int64_t);
210194062Svanhu			}
211194062Svanhu			mi = m_makespace(m, hlen, size, &off);
212194062Svanhu			if (mi == NULL) {
213194062Svanhu				DPRINTF(("%s: m_makespace for udphdr failed\n",
214194062Svanhu				    __func__));
215194062Svanhu				error = ENOBUFS;
216194062Svanhu				goto bad;
217194062Svanhu			}
218194062Svanhu
219194062Svanhu			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
220194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
221194062Svanhu				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
222194062Svanhu			else
223194062Svanhu				udp->uh_sport =
224194062Svanhu					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
225194062Svanhu			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
226194062Svanhu			udp->uh_sum = 0;
227194062Svanhu			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
228194062Svanhu			ip->ip_len = m->m_pkthdr.len;
229194062Svanhu			ip->ip_p = IPPROTO_UDP;
230194062Svanhu
231194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
232194062Svanhu				*(u_int64_t *)(udp + 1) = 0;
233194062Svanhu		}
234194062Svanhu#endif /* IPSEC_NAT_T */
235194062Svanhu
236105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
237105197Ssam#endif /* INET */
238105197Ssam#ifdef INET6
239105197Ssam	case AF_INET6:
240105197Ssam		/*
241105197Ssam		 * We don't need massage, IPv6 header fields are always in
242105197Ssam		 * net endian.
243105197Ssam		 */
244105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
245105197Ssam#endif /* INET6 */
246105197Ssam	}
247105197Ssam	panic("ipsec_process_done");
248105197Ssambad:
249105197Ssam	m_freem(m);
250105197Ssam	KEY_FREESAV(&sav);
251105197Ssam	return (error);
252105197Ssam}
253105197Ssam
254105197Ssamstatic struct ipsecrequest *
255105197Ssamipsec_nextisr(
256105197Ssam	struct mbuf *m,
257105197Ssam	struct ipsecrequest *isr,
258105197Ssam	int af,
259105197Ssam	struct secasindex *saidx,
260105197Ssam	int *error
261105197Ssam)
262105197Ssam{
263105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
264105197Ssam			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
265105197Ssam	struct secasvar *sav;
266105197Ssam
267120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
268120585Ssam
269120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
270120585Ssam		("invalid address family %u", af));
271105197Ssamagain:
272105197Ssam	/*
273105197Ssam	 * Craft SA index to search for proper SA.  Note that
274105197Ssam	 * we only fillin unspecified SA peers for transport
275105197Ssam	 * mode; for tunnel mode they must already be filled in.
276105197Ssam	 */
277105197Ssam	*saidx = isr->saidx;
278105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
279105197Ssam		/* Fillin unspecified SA peers only for transport mode */
280105197Ssam		if (af == AF_INET) {
281105197Ssam			struct sockaddr_in *sin;
282105197Ssam			struct ip *ip = mtod(m, struct ip *);
283105197Ssam
284105197Ssam			if (saidx->src.sa.sa_len == 0) {
285105197Ssam				sin = &saidx->src.sin;
286105197Ssam				sin->sin_len = sizeof(*sin);
287105197Ssam				sin->sin_family = AF_INET;
288105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
289105197Ssam				sin->sin_addr = ip->ip_src;
290105197Ssam			}
291105197Ssam			if (saidx->dst.sa.sa_len == 0) {
292105197Ssam				sin = &saidx->dst.sin;
293105197Ssam				sin->sin_len = sizeof(*sin);
294105197Ssam				sin->sin_family = AF_INET;
295105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
296105197Ssam				sin->sin_addr = ip->ip_dst;
297105197Ssam			}
298105197Ssam		} else {
299105197Ssam			struct sockaddr_in6 *sin6;
300105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
301105197Ssam
302105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
303105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
304105197Ssam				sin6->sin6_len = sizeof(*sin6);
305105197Ssam				sin6->sin6_family = AF_INET6;
306105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
307105197Ssam				sin6->sin6_addr = ip6->ip6_src;
308105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
309105197Ssam					/* fix scope id for comparing SPD */
310105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
311105197Ssam					sin6->sin6_scope_id =
312105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
313105197Ssam				}
314105197Ssam			}
315105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
316105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
317105197Ssam				sin6->sin6_len = sizeof(*sin6);
318105197Ssam				sin6->sin6_family = AF_INET6;
319105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
320105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
321105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
322105197Ssam					/* fix scope id for comparing SPD */
323105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
324105197Ssam					sin6->sin6_scope_id =
325105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
326105197Ssam				}
327105197Ssam			}
328105197Ssam		}
329105197Ssam	}
330105197Ssam
331105197Ssam	/*
332105197Ssam	 * Lookup SA and validate it.
333105197Ssam	 */
334105197Ssam	*error = key_checkrequest(isr, saidx);
335105197Ssam	if (*error != 0) {
336105197Ssam		/*
337105197Ssam		 * IPsec processing is required, but no SA found.
338105197Ssam		 * I assume that key_acquire() had been called
339105197Ssam		 * to get/establish the SA. Here I discard
340105197Ssam		 * this packet because it is responsibility for
341105197Ssam		 * upper layer to retransmit the packet.
342105197Ssam		 */
343181803Sbz		V_ipsec4stat.ips_out_nosa++;
344105197Ssam		goto bad;
345105197Ssam	}
346105197Ssam	sav = isr->sav;
347177175Sbz	if (sav == NULL) {
348120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
349120585Ssam			("no SA found, but required; level %u",
350105197Ssam			ipsec_get_reqlevel(isr)));
351120585Ssam		IPSECREQUEST_UNLOCK(isr);
352105197Ssam		isr = isr->next;
353177175Sbz		/*
354177175Sbz		 * If isr is NULL, we found a 'use' policy w/o SA.
355177175Sbz		 * Return w/o error and w/o isr so we can drop out
356177175Sbz		 * and continue w/o IPsec processing.
357177175Sbz		 */
358177175Sbz		if (isr == NULL)
359105197Ssam			return isr;
360120585Ssam		IPSECREQUEST_LOCK(isr);
361105197Ssam		goto again;
362105197Ssam	}
363105197Ssam
364105197Ssam	/*
365105197Ssam	 * Check system global policy controls.
366105197Ssam	 */
367181803Sbz	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
368181803Sbz	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
369181803Sbz	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
370120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
371120585Ssam			" to policy (check your sysctls)\n", __func__));
372181803Sbz		IPSEC_OSTAT(V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
373181803Sbz		    V_ipcompstat.ipcomps_pdrops);
374105197Ssam		*error = EHOSTUNREACH;
375105197Ssam		goto bad;
376105197Ssam	}
377105197Ssam
378105197Ssam	/*
379105197Ssam	 * Sanity check the SA contents for the caller
380105197Ssam	 * before they invoke the xform output method.
381105197Ssam	 */
382105197Ssam	if (sav->tdb_xform == NULL) {
383120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
384181803Sbz		IPSEC_OSTAT(V_espstat.esps_noxform, V_ahstat.ahs_noxform,
385181803Sbz		    V_ipcompstat.ipcomps_noxform);
386105197Ssam		*error = EHOSTUNREACH;
387105197Ssam		goto bad;
388105197Ssam	}
389105197Ssam	return isr;
390105197Ssambad:
391120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
392120585Ssam	IPSECREQUEST_UNLOCK(isr);
393105197Ssam	return NULL;
394105197Ssam#undef IPSEC_OSTAT
395105197Ssam}
396105197Ssam
397105197Ssam#ifdef INET
398105197Ssam/*
399105197Ssam * IPsec output logic for IPv4.
400105197Ssam */
401105197Ssamint
402105197Ssamipsec4_process_packet(
403105197Ssam	struct mbuf *m,
404105197Ssam	struct ipsecrequest *isr,
405105197Ssam	int flags,
406105197Ssam	int tunalready)
407105197Ssam{
408105197Ssam	struct secasindex saidx;
409105197Ssam	struct secasvar *sav;
410105197Ssam	struct ip *ip;
411119643Ssam	int error, i, off;
412105197Ssam
413120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
414120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
415105197Ssam
416120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
417105197Ssam
418105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
419177175Sbz	if (isr == NULL) {
420177175Sbz		if (error != 0)
421177175Sbz			goto bad;
422177175Sbz		return EJUSTRETURN;
423177175Sbz	}
424105197Ssam
425105197Ssam	sav = isr->sav;
426159965Sthompsa
427159965Sthompsa#ifdef DEV_ENC
428181627Svanhu	encif->if_opackets++;
429181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
430181627Svanhu
431174054Sbz	/* pass the mbuf to enc0 for bpf processing */
432174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
433159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
434174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
435159965Sthompsa		goto bad;
436159965Sthompsa#endif
437159965Sthompsa
438105197Ssam	if (!tunalready) {
439105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
440105197Ssam		int setdf;
441105197Ssam
442105197Ssam		/*
443105197Ssam		 * Collect IP_DF state from the outer header.
444105197Ssam		 */
445105197Ssam		if (dst->sa.sa_family == AF_INET) {
446105197Ssam			if (m->m_len < sizeof (struct ip) &&
447105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
448105197Ssam				error = ENOBUFS;
449105197Ssam				goto bad;
450105197Ssam			}
451105197Ssam			ip = mtod(m, struct ip *);
452105197Ssam			/* Honor system-wide control of how to handle IP_DF */
453181803Sbz			switch (V_ip4_ipsec_dfbit) {
454105197Ssam			case 0:			/* clear in outer header */
455105197Ssam			case 1:			/* set in outer header */
456181803Sbz				setdf = V_ip4_ipsec_dfbit;
457105197Ssam				break;
458105197Ssam			default:		/* propagate to outer header */
459105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
460105197Ssam				break;
461105197Ssam			}
462105197Ssam		} else {
463105197Ssam			ip = NULL;		/* keep compiler happy */
464105197Ssam			setdf = 0;
465105197Ssam		}
466105197Ssam		/* Do the appropriate encapsulation, if necessary */
467105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
468105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
469105197Ssam#if 0
470105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
471105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
472105197Ssam#endif
473105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
474105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
475105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
476105197Ssam			struct mbuf *mp;
477105197Ssam
478105197Ssam			/* Fix IPv4 header checksum and length */
479105197Ssam			if (m->m_len < sizeof (struct ip) &&
480105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
481105197Ssam				error = ENOBUFS;
482105197Ssam				goto bad;
483105197Ssam			}
484105197Ssam			ip = mtod(m, struct ip *);
485105197Ssam			ip->ip_len = htons(m->m_pkthdr.len);
486105197Ssam			ip->ip_sum = 0;
487105197Ssam#ifdef _IP_VHL
488105197Ssam			if (ip->ip_vhl == IP_VHL_BORING)
489105197Ssam				ip->ip_sum = in_cksum_hdr(ip);
490105197Ssam			else
491105197Ssam				ip->ip_sum = in_cksum(m,
492105197Ssam					_IP_VHL_HL(ip->ip_vhl) << 2);
493105197Ssam#else
494105197Ssam			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
495105197Ssam#endif
496105197Ssam
497105197Ssam			/* Encapsulate the packet */
498105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
499105197Ssam			if (mp == NULL && !error) {
500105197Ssam				/* Should never happen. */
501120585Ssam				DPRINTF(("%s: ipip_output returns no mbuf and "
502120585Ssam					"no error!", __func__));
503105197Ssam				error = EFAULT;
504105197Ssam			}
505105197Ssam			if (error) {
506124765Ssam				if (mp) {
507124765Ssam					/* XXX: Should never happen! */
508105197Ssam					m_freem(mp);
509124765Ssam				}
510124765Ssam				m = NULL; /* ipip_output() already freed it */
511105197Ssam				goto bad;
512105197Ssam			}
513105197Ssam			m = mp, mp = NULL;
514105197Ssam			/*
515105197Ssam			 * ipip_output clears IP_DF in the new header.  If
516105197Ssam			 * we need to propagate IP_DF from the outer header,
517105197Ssam			 * then we have to do it here.
518105197Ssam			 *
519105197Ssam			 * XXX shouldn't assume what ipip_output does.
520105197Ssam			 */
521105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
522105197Ssam				if (m->m_len < sizeof (struct ip) &&
523105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
524105197Ssam					error = ENOBUFS;
525105197Ssam					goto bad;
526105197Ssam				}
527105197Ssam				ip = mtod(m, struct ip *);
528105197Ssam				ip->ip_off = ntohs(ip->ip_off);
529105197Ssam				ip->ip_off |= IP_DF;
530105197Ssam				ip->ip_off = htons(ip->ip_off);
531105197Ssam			}
532105197Ssam		}
533105197Ssam	}
534105197Ssam
535159965Sthompsa#ifdef DEV_ENC
536159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
537174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
538174054Sbz	/* pass the mbuf to enc0 for packet filtering */
539174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
540174054Sbz		goto bad;
541159965Sthompsa#endif
542159965Sthompsa
543105197Ssam	/*
544105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
545105197Ssam	 * packet will be returned for transmission after crypto
546105197Ssam	 * processing, etc. are completed.  For encapsulation we
547105197Ssam	 * bypass this call because of the explicit call done above
548105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
549105197Ssam	 *
550105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
551105197Ssam	 *     for reclaiming their resources.
552105197Ssam	 */
553105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
554105197Ssam		ip = mtod(m, struct ip *);
555105197Ssam		i = ip->ip_hl << 2;
556105197Ssam		off = offsetof(struct ip, ip_p);
557105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
558105197Ssam	} else {
559105197Ssam		error = ipsec_process_done(m, isr);
560105197Ssam	}
561120585Ssam	IPSECREQUEST_UNLOCK(isr);
562105197Ssam	return error;
563105197Ssambad:
564120585Ssam	if (isr)
565120585Ssam		IPSECREQUEST_UNLOCK(isr);
566105197Ssam	if (m)
567105197Ssam		m_freem(m);
568105197Ssam	return error;
569105197Ssam}
570105197Ssam#endif
571105197Ssam
572105197Ssam#ifdef INET6
573105197Ssam/*
574105197Ssam * Chop IP6 header from the payload.
575105197Ssam */
576105197Ssamstatic struct mbuf *
577105197Ssamipsec6_splithdr(struct mbuf *m)
578105197Ssam{
579105197Ssam	struct mbuf *mh;
580105197Ssam	struct ip6_hdr *ip6;
581105197Ssam	int hlen;
582105197Ssam
583120585Ssam	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
584120585Ssam		("first mbuf too short, len %u", m->m_len));
585105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
586105197Ssam	hlen = sizeof(struct ip6_hdr);
587105197Ssam	if (m->m_len > hlen) {
588151967Sandre		MGETHDR(mh, M_DONTWAIT, MT_DATA);
589105197Ssam		if (!mh) {
590105197Ssam			m_freem(m);
591105197Ssam			return NULL;
592105197Ssam		}
593108466Ssam		M_MOVE_PKTHDR(mh, m);
594105197Ssam		MH_ALIGN(mh, hlen);
595105197Ssam		m->m_len -= hlen;
596105197Ssam		m->m_data += hlen;
597105197Ssam		mh->m_next = m;
598105197Ssam		m = mh;
599105197Ssam		m->m_len = hlen;
600105197Ssam		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
601105197Ssam	} else if (m->m_len < hlen) {
602105197Ssam		m = m_pullup(m, hlen);
603105197Ssam		if (!m)
604105197Ssam			return NULL;
605105197Ssam	}
606105197Ssam	return m;
607105197Ssam}
608105197Ssam
609105197Ssam/*
610105197Ssam * IPsec output logic for IPv6, transport mode.
611105197Ssam */
612105197Ssamint
613105197Ssamipsec6_output_trans(
614105197Ssam	struct ipsec_output_state *state,
615105197Ssam	u_char *nexthdrp,
616105197Ssam	struct mbuf *mprev,
617105197Ssam	struct secpolicy *sp,
618105197Ssam	int flags,
619105197Ssam	int *tun)
620105197Ssam{
621105197Ssam	struct ipsecrequest *isr;
622105197Ssam	struct secasindex saidx;
623105197Ssam	int error = 0;
624105197Ssam	struct mbuf *m;
625105197Ssam
626120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
627120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
628120585Ssam	IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
629120585Ssam	IPSEC_ASSERT(mprev != NULL, ("null mprev"));
630120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
631120585Ssam	IPSEC_ASSERT(tun != NULL, ("null tun"));
632105197Ssam
633105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
634170122Sbz		printf("%s: applied SP\n", __func__);
635105197Ssam		kdebug_secpolicy(sp));
636105197Ssam
637105197Ssam	isr = sp->req;
638105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
639105197Ssam		/* the rest will be handled by ipsec6_output_tunnel() */
640105197Ssam		*tun = 1;		/* need tunnel-mode processing */
641105197Ssam		return 0;
642105197Ssam	}
643105197Ssam
644105197Ssam	*tun = 0;
645105197Ssam	m = state->m;
646105197Ssam
647171133Sgnn	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
648105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
649105197Ssam	if (isr == NULL) {
650177175Sbz		if (error != 0) {
651105197Ssam#ifdef notdef
652177175Sbz			/* XXX should notification be done for all errors ? */
653177175Sbz			/*
654177175Sbz			 * Notify the fact that the packet is discarded
655177175Sbz			 * to ourselves. I believe this is better than
656177175Sbz			 * just silently discarding. (jinmei@kame.net)
657177175Sbz			 * XXX: should we restrict the error to TCP packets?
658177175Sbz			 * XXX: should we directly notify sockets via
659177175Sbz			 *      pfctlinputs?
660177175Sbz			 */
661177175Sbz			icmp6_error(m, ICMP6_DST_UNREACH,
662177175Sbz				    ICMP6_DST_UNREACH_ADMIN, 0);
663177175Sbz			m = NULL;	/* NB: icmp6_error frees mbuf */
664105197Ssam#endif
665177175Sbz			goto bad;
666177175Sbz		}
667177175Sbz		return EJUSTRETURN;
668105197Ssam	}
669105197Ssam
670171133Sgnn	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
671171133Sgnn						  sizeof (struct ip6_hdr),
672171133Sgnn						  offsetof(struct ip6_hdr,
673171133Sgnn							   ip6_nxt));
674171133Sgnn	IPSECREQUEST_UNLOCK(isr);
675171133Sgnn	return error;
676105197Ssambad:
677171133Sgnn	if (isr)
678171133Sgnn		IPSECREQUEST_UNLOCK(isr);
679105197Ssam	if (m)
680105197Ssam		m_freem(m);
681105197Ssam	state->m = NULL;
682105197Ssam	return error;
683105197Ssam}
684105197Ssam
685105197Ssamstatic int
686105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
687105197Ssam{
688105197Ssam	struct ip6_hdr *oip6;
689105197Ssam	struct ip6_hdr *ip6;
690105197Ssam	size_t plen;
691105197Ssam
692105197Ssam	/* can't tunnel between different AFs */
693105197Ssam	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
694105197Ssam	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
695105197Ssam		m_freem(m);
696105197Ssam		return EINVAL;
697105197Ssam	}
698171133Sgnn	IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
699120585Ssam		("mbuf wrong size; len %u", m->m_len));
700105197Ssam
701105197Ssam
702105197Ssam	/*
703105197Ssam	 * grow the mbuf to accomodate the new IPv6 header.
704105197Ssam	 */
705105197Ssam	plen = m->m_pkthdr.len;
706105197Ssam	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
707105197Ssam		struct mbuf *n;
708111119Simp		MGET(n, M_DONTWAIT, MT_DATA);
709105197Ssam		if (!n) {
710105197Ssam			m_freem(m);
711105197Ssam			return ENOBUFS;
712105197Ssam		}
713105197Ssam		n->m_len = sizeof(struct ip6_hdr);
714105197Ssam		n->m_next = m->m_next;
715105197Ssam		m->m_next = n;
716105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
717105197Ssam		oip6 = mtod(n, struct ip6_hdr *);
718105197Ssam	} else {
719105197Ssam		m->m_next->m_len += sizeof(struct ip6_hdr);
720105197Ssam		m->m_next->m_data -= sizeof(struct ip6_hdr);
721105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
722105197Ssam		oip6 = mtod(m->m_next, struct ip6_hdr *);
723105197Ssam	}
724105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
725113076Sdes	bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
726105197Ssam
727105197Ssam	/* Fake link-local scope-class addresses */
728105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
729105197Ssam		oip6->ip6_src.s6_addr16[1] = 0;
730105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
731105197Ssam		oip6->ip6_dst.s6_addr16[1] = 0;
732105197Ssam
733105197Ssam	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
734105197Ssam	/* ECN consideration. */
735181803Sbz	ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
736105197Ssam	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
737105197Ssam		ip6->ip6_plen = htons(plen);
738105197Ssam	else {
739105197Ssam		/* ip6->ip6_plen will be updated in ip6_output() */
740105197Ssam	}
741105197Ssam	ip6->ip6_nxt = IPPROTO_IPV6;
742171133Sgnn	ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
743171133Sgnn	ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
744105197Ssam	ip6->ip6_hlim = IPV6_DEFHLIM;
745105197Ssam
746105197Ssam	/* XXX Should ip6_src be updated later ? */
747105197Ssam
748105197Ssam	return 0;
749105197Ssam}
750105197Ssam
751105197Ssam/*
752105197Ssam * IPsec output logic for IPv6, tunnel mode.
753105197Ssam */
754105197Ssamint
755105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
756105197Ssam{
757105197Ssam	struct ip6_hdr *ip6;
758105197Ssam	struct ipsecrequest *isr;
759105197Ssam	struct secasindex saidx;
760105197Ssam	int error;
761213836Sbz	struct sockaddr_in6 *dst6;
762105197Ssam	struct mbuf *m;
763105197Ssam
764120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
765120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
766120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
767105197Ssam
768105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
769170122Sbz		printf("%s: applied SP\n", __func__);
770105197Ssam		kdebug_secpolicy(sp));
771105197Ssam
772105197Ssam	m = state->m;
773105197Ssam	/*
774105197Ssam	 * transport mode ipsec (before the 1st tunnel mode) is already
775105197Ssam	 * processed by ipsec6_output_trans().
776105197Ssam	 */
777105197Ssam	for (isr = sp->req; isr; isr = isr->next) {
778105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
779105197Ssam			break;
780105197Ssam	}
781170123Sbz
782170123Sbz	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
783105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
784177175Sbz	if (isr == NULL) {
785177175Sbz		if (error != 0)
786177175Sbz			goto bad;
787177175Sbz		return EJUSTRETURN;
788177175Sbz	}
789105197Ssam
790174054Sbz#ifdef DEV_ENC
791181627Svanhu	encif->if_opackets++;
792181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
793181627Svanhu
794174054Sbz	/* pass the mbuf to enc0 for bpf processing */
795174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
796174054Sbz	/* pass the mbuf to enc0 for packet filtering */
797174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
798174054Sbz		goto bad;
799174054Sbz#endif
800174054Sbz
801105197Ssam	/*
802105197Ssam	 * There may be the case that SA status will be changed when
803105197Ssam	 * we are refering to one. So calling splsoftnet().
804105197Ssam	 */
805105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
806105197Ssam		/*
807105197Ssam		 * build IPsec tunnel.
808105197Ssam		 */
809105197Ssam		/* XXX should be processed with other familiy */
810105197Ssam		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
811120585Ssam			ipseclog((LOG_ERR, "%s: family mismatched between "
812120585Ssam			    "inner and outer, spi=%u\n", __func__,
813105197Ssam			    ntohl(isr->sav->spi)));
814181803Sbz			V_ipsec6stat.ips_out_inval++;
815105197Ssam			error = EAFNOSUPPORT;
816105197Ssam			goto bad;
817105197Ssam		}
818105197Ssam
819105197Ssam		m = ipsec6_splithdr(m);
820105197Ssam		if (!m) {
821181803Sbz			V_ipsec6stat.ips_out_nomem++;
822105197Ssam			error = ENOMEM;
823105197Ssam			goto bad;
824105197Ssam		}
825105197Ssam		error = ipsec6_encapsulate(m, isr->sav);
826105197Ssam		if (error) {
827105197Ssam			m = NULL;
828105197Ssam			goto bad;
829105197Ssam		}
830105197Ssam		ip6 = mtod(m, struct ip6_hdr *);
831105197Ssam
832105197Ssam		state->ro = &isr->sav->sah->sa_route;
833105197Ssam		state->dst = (struct sockaddr *)&state->ro->ro_dst;
834105197Ssam		dst6 = (struct sockaddr_in6 *)state->dst;
835105197Ssam		if (state->ro->ro_rt
836105197Ssam		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
837105197Ssam		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
838105197Ssam			RTFREE(state->ro->ro_rt);
839105197Ssam			state->ro->ro_rt = NULL;
840105197Ssam		}
841187936Sbz		if (state->ro->ro_rt == NULL) {
842105197Ssam			bzero(dst6, sizeof(*dst6));
843105197Ssam			dst6->sin6_family = AF_INET6;
844105197Ssam			dst6->sin6_len = sizeof(*dst6);
845105197Ssam			dst6->sin6_addr = ip6->ip6_dst;
846105197Ssam			rtalloc(state->ro);
847105197Ssam		}
848187936Sbz		if (state->ro->ro_rt == NULL) {
849181803Sbz			V_ip6stat.ip6s_noroute++;
850181803Sbz			V_ipsec6stat.ips_out_noroute++;
851105197Ssam			error = EHOSTUNREACH;
852105197Ssam			goto bad;
853105197Ssam		}
854105197Ssam
855105197Ssam		/* adjust state->dst if tunnel endpoint is offlink */
856213837Sbz		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
857105197Ssam			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
858105197Ssam	}
859105197Ssam
860105197Ssam	m = ipsec6_splithdr(m);
861105197Ssam	if (!m) {
862181803Sbz		V_ipsec6stat.ips_out_nomem++;
863105197Ssam		error = ENOMEM;
864105197Ssam		goto bad;
865105197Ssam	}
866105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
867174054Sbz
868174054Sbz#ifdef DEV_ENC
869174054Sbz	/* pass the mbuf to enc0 for bpf processing */
870174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
871174054Sbz	/* pass the mbuf to enc0 for packet filtering */
872174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
873174054Sbz		goto bad;
874174054Sbz#endif
875174054Sbz
876170123Sbz	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
877105197Ssam		sizeof (struct ip6_hdr),
878105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
879170123Sbz	IPSECREQUEST_UNLOCK(isr);
880170123Sbz	return error;
881105197Ssambad:
882170123Sbz	if (isr)
883170123Sbz		IPSECREQUEST_UNLOCK(isr);
884105197Ssam	if (m)
885105197Ssam		m_freem(m);
886105197Ssam	state->m = NULL;
887105197Ssam	return error;
888105197Ssam}
889105197Ssam#endif /*INET6*/
890