ipsec_output.c revision 221129
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 221129 2011-04-27 19:28:42Z 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++;
168221129Sbz		sav = isr->next->sav;
169221129Sbz		saidx = &sav->sah->saidx;
170221129Sbz		switch (saidx->dst.sa.sa_family) {
171221129Sbz#ifdef INET
172221129Sbz		case AF_INET:
173221129Sbz			return ipsec4_process_packet(m, isr->next, 0, 0);
174221129Sbz			/* NOTREACHED */
175221129Sbz#endif
176221129Sbz#ifdef notyet
177221129Sbz#ifdef INET6
178221129Sbz		case AF_INET6:
179221129Sbz			/* XXX */
180221129Sbz			ipsec6_output_trans()
181221129Sbz			ipsec6_output_tunnel()
182221129Sbz			/* NOTREACHED */
183221129Sbz#endif /* INET6 */
184221129Sbz#endif
185221129Sbz		default:
186221129Sbz			DPRINTF(("%s: unknown protocol family %u\n", __func__,
187221129Sbz			    saidx->dst.sa.sa_family));
188221129Sbz			error = ENXIO;
189221129Sbz			goto bad;
190221129Sbz		}
191105197Ssam	}
192117056Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
193105197Ssam
194105197Ssam	/*
195105197Ssam	 * We're done with IPsec processing, transmit the packet using the
196105197Ssam	 * appropriate network protocol (IP or IPv6). SPD lookup will be
197105197Ssam	 * performed again there.
198105197Ssam	 */
199105197Ssam	switch (saidx->dst.sa.sa_family) {
200105197Ssam#ifdef INET
201105197Ssam	struct ip *ip;
202105197Ssam	case AF_INET:
203105197Ssam		ip = mtod(m, struct ip *);
204105197Ssam		ip->ip_len = ntohs(ip->ip_len);
205105197Ssam		ip->ip_off = ntohs(ip->ip_off);
206105197Ssam
207194062Svanhu#ifdef IPSEC_NAT_T
208194062Svanhu		/*
209194062Svanhu		 * If NAT-T is enabled, now that all IPsec processing is done
210194062Svanhu		 * insert UDP encapsulation header after IP header.
211194062Svanhu		 */
212194062Svanhu		if (sav->natt_type) {
213194062Svanhu#ifdef _IP_VHL
214194062Svanhu			const int hlen = IP_VHL_HL(ip->ip_vhl);
215194062Svanhu#else
216194062Svanhu			const int hlen = (ip->ip_hl << 2);
217194062Svanhu#endif
218194062Svanhu			int size, off;
219194062Svanhu			struct mbuf *mi;
220194062Svanhu			struct udphdr *udp;
221194062Svanhu
222194062Svanhu			size = sizeof(struct udphdr);
223194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
224194062Svanhu				/*
225194062Svanhu				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
226194062Svanhu				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
227194062Svanhu				 * ignoring possible AH mode
228194062Svanhu				 * non-IKE marker + non-ESP marker
229194062Svanhu				 * from draft-ietf-ipsec-udp-encaps-00.txt.
230194062Svanhu				 */
231194062Svanhu				size += sizeof(u_int64_t);
232194062Svanhu			}
233194062Svanhu			mi = m_makespace(m, hlen, size, &off);
234194062Svanhu			if (mi == NULL) {
235194062Svanhu				DPRINTF(("%s: m_makespace for udphdr failed\n",
236194062Svanhu				    __func__));
237194062Svanhu				error = ENOBUFS;
238194062Svanhu				goto bad;
239194062Svanhu			}
240194062Svanhu
241194062Svanhu			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
242194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
243194062Svanhu				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
244194062Svanhu			else
245194062Svanhu				udp->uh_sport =
246194062Svanhu					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
247194062Svanhu			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
248194062Svanhu			udp->uh_sum = 0;
249194062Svanhu			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
250194062Svanhu			ip->ip_len = m->m_pkthdr.len;
251194062Svanhu			ip->ip_p = IPPROTO_UDP;
252194062Svanhu
253194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
254194062Svanhu				*(u_int64_t *)(udp + 1) = 0;
255194062Svanhu		}
256194062Svanhu#endif /* IPSEC_NAT_T */
257194062Svanhu
258105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
259105197Ssam#endif /* INET */
260105197Ssam#ifdef INET6
261105197Ssam	case AF_INET6:
262105197Ssam		/*
263105197Ssam		 * We don't need massage, IPv6 header fields are always in
264105197Ssam		 * net endian.
265105197Ssam		 */
266105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
267105197Ssam#endif /* INET6 */
268105197Ssam	}
269105197Ssam	panic("ipsec_process_done");
270105197Ssambad:
271105197Ssam	m_freem(m);
272105197Ssam	return (error);
273105197Ssam}
274105197Ssam
275105197Ssamstatic struct ipsecrequest *
276105197Ssamipsec_nextisr(
277105197Ssam	struct mbuf *m,
278105197Ssam	struct ipsecrequest *isr,
279105197Ssam	int af,
280105197Ssam	struct secasindex *saidx,
281105197Ssam	int *error
282105197Ssam)
283105197Ssam{
284105197Ssam#define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
285105197Ssam			    isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
286105197Ssam	struct secasvar *sav;
287105197Ssam
288120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
289120585Ssam
290120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
291120585Ssam		("invalid address family %u", af));
292105197Ssamagain:
293105197Ssam	/*
294105197Ssam	 * Craft SA index to search for proper SA.  Note that
295105197Ssam	 * we only fillin unspecified SA peers for transport
296105197Ssam	 * mode; for tunnel mode they must already be filled in.
297105197Ssam	 */
298105197Ssam	*saidx = isr->saidx;
299105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
300105197Ssam		/* Fillin unspecified SA peers only for transport mode */
301105197Ssam		if (af == AF_INET) {
302105197Ssam			struct sockaddr_in *sin;
303105197Ssam			struct ip *ip = mtod(m, struct ip *);
304105197Ssam
305105197Ssam			if (saidx->src.sa.sa_len == 0) {
306105197Ssam				sin = &saidx->src.sin;
307105197Ssam				sin->sin_len = sizeof(*sin);
308105197Ssam				sin->sin_family = AF_INET;
309105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
310105197Ssam				sin->sin_addr = ip->ip_src;
311105197Ssam			}
312105197Ssam			if (saidx->dst.sa.sa_len == 0) {
313105197Ssam				sin = &saidx->dst.sin;
314105197Ssam				sin->sin_len = sizeof(*sin);
315105197Ssam				sin->sin_family = AF_INET;
316105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
317105197Ssam				sin->sin_addr = ip->ip_dst;
318105197Ssam			}
319105197Ssam		} else {
320105197Ssam			struct sockaddr_in6 *sin6;
321105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
322105197Ssam
323105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
324105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
325105197Ssam				sin6->sin6_len = sizeof(*sin6);
326105197Ssam				sin6->sin6_family = AF_INET6;
327105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
328105197Ssam				sin6->sin6_addr = ip6->ip6_src;
329105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
330105197Ssam					/* fix scope id for comparing SPD */
331105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
332105197Ssam					sin6->sin6_scope_id =
333105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
334105197Ssam				}
335105197Ssam			}
336105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
337105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
338105197Ssam				sin6->sin6_len = sizeof(*sin6);
339105197Ssam				sin6->sin6_family = AF_INET6;
340105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
341105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
342105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
343105197Ssam					/* fix scope id for comparing SPD */
344105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
345105197Ssam					sin6->sin6_scope_id =
346105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
347105197Ssam				}
348105197Ssam			}
349105197Ssam		}
350105197Ssam	}
351105197Ssam
352105197Ssam	/*
353105197Ssam	 * Lookup SA and validate it.
354105197Ssam	 */
355105197Ssam	*error = key_checkrequest(isr, saidx);
356105197Ssam	if (*error != 0) {
357105197Ssam		/*
358105197Ssam		 * IPsec processing is required, but no SA found.
359105197Ssam		 * I assume that key_acquire() had been called
360105197Ssam		 * to get/establish the SA. Here I discard
361105197Ssam		 * this packet because it is responsibility for
362105197Ssam		 * upper layer to retransmit the packet.
363105197Ssam		 */
364181803Sbz		V_ipsec4stat.ips_out_nosa++;
365105197Ssam		goto bad;
366105197Ssam	}
367105197Ssam	sav = isr->sav;
368177175Sbz	if (sav == NULL) {
369120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
370120585Ssam			("no SA found, but required; level %u",
371105197Ssam			ipsec_get_reqlevel(isr)));
372120585Ssam		IPSECREQUEST_UNLOCK(isr);
373105197Ssam		isr = isr->next;
374177175Sbz		/*
375177175Sbz		 * If isr is NULL, we found a 'use' policy w/o SA.
376177175Sbz		 * Return w/o error and w/o isr so we can drop out
377177175Sbz		 * and continue w/o IPsec processing.
378177175Sbz		 */
379177175Sbz		if (isr == NULL)
380105197Ssam			return isr;
381120585Ssam		IPSECREQUEST_LOCK(isr);
382105197Ssam		goto again;
383105197Ssam	}
384105197Ssam
385105197Ssam	/*
386105197Ssam	 * Check system global policy controls.
387105197Ssam	 */
388181803Sbz	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
389181803Sbz	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
390181803Sbz	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
391120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
392120585Ssam			" to policy (check your sysctls)\n", __func__));
393181803Sbz		IPSEC_OSTAT(V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
394181803Sbz		    V_ipcompstat.ipcomps_pdrops);
395105197Ssam		*error = EHOSTUNREACH;
396105197Ssam		goto bad;
397105197Ssam	}
398105197Ssam
399105197Ssam	/*
400105197Ssam	 * Sanity check the SA contents for the caller
401105197Ssam	 * before they invoke the xform output method.
402105197Ssam	 */
403105197Ssam	if (sav->tdb_xform == NULL) {
404120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
405181803Sbz		IPSEC_OSTAT(V_espstat.esps_noxform, V_ahstat.ahs_noxform,
406181803Sbz		    V_ipcompstat.ipcomps_noxform);
407105197Ssam		*error = EHOSTUNREACH;
408105197Ssam		goto bad;
409105197Ssam	}
410105197Ssam	return isr;
411105197Ssambad:
412120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
413120585Ssam	IPSECREQUEST_UNLOCK(isr);
414105197Ssam	return NULL;
415105197Ssam#undef IPSEC_OSTAT
416105197Ssam}
417105197Ssam
418105197Ssam#ifdef INET
419105197Ssam/*
420105197Ssam * IPsec output logic for IPv4.
421105197Ssam */
422105197Ssamint
423105197Ssamipsec4_process_packet(
424105197Ssam	struct mbuf *m,
425105197Ssam	struct ipsecrequest *isr,
426105197Ssam	int flags,
427105197Ssam	int tunalready)
428105197Ssam{
429105197Ssam	struct secasindex saidx;
430105197Ssam	struct secasvar *sav;
431105197Ssam	struct ip *ip;
432119643Ssam	int error, i, off;
433105197Ssam
434120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
435120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
436105197Ssam
437120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
438105197Ssam
439105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
440177175Sbz	if (isr == NULL) {
441177175Sbz		if (error != 0)
442177175Sbz			goto bad;
443177175Sbz		return EJUSTRETURN;
444177175Sbz	}
445105197Ssam
446105197Ssam	sav = isr->sav;
447159965Sthompsa
448159965Sthompsa#ifdef DEV_ENC
449181627Svanhu	encif->if_opackets++;
450181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
451181627Svanhu
452174054Sbz	/* pass the mbuf to enc0 for bpf processing */
453174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
454159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
455174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
456159965Sthompsa		goto bad;
457159965Sthompsa#endif
458159965Sthompsa
459105197Ssam	if (!tunalready) {
460105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
461105197Ssam		int setdf;
462105197Ssam
463105197Ssam		/*
464105197Ssam		 * Collect IP_DF state from the outer header.
465105197Ssam		 */
466105197Ssam		if (dst->sa.sa_family == AF_INET) {
467105197Ssam			if (m->m_len < sizeof (struct ip) &&
468105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
469105197Ssam				error = ENOBUFS;
470105197Ssam				goto bad;
471105197Ssam			}
472105197Ssam			ip = mtod(m, struct ip *);
473105197Ssam			/* Honor system-wide control of how to handle IP_DF */
474181803Sbz			switch (V_ip4_ipsec_dfbit) {
475105197Ssam			case 0:			/* clear in outer header */
476105197Ssam			case 1:			/* set in outer header */
477181803Sbz				setdf = V_ip4_ipsec_dfbit;
478105197Ssam				break;
479105197Ssam			default:		/* propagate to outer header */
480105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
481105197Ssam				break;
482105197Ssam			}
483105197Ssam		} else {
484105197Ssam			ip = NULL;		/* keep compiler happy */
485105197Ssam			setdf = 0;
486105197Ssam		}
487105197Ssam		/* Do the appropriate encapsulation, if necessary */
488105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
489105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
490105197Ssam#if 0
491105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
492105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
493105197Ssam#endif
494105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
495105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
496105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
497105197Ssam			struct mbuf *mp;
498105197Ssam
499105197Ssam			/* Fix IPv4 header checksum and length */
500105197Ssam			if (m->m_len < sizeof (struct ip) &&
501105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
502105197Ssam				error = ENOBUFS;
503105197Ssam				goto bad;
504105197Ssam			}
505105197Ssam			ip = mtod(m, struct ip *);
506105197Ssam			ip->ip_len = htons(m->m_pkthdr.len);
507105197Ssam			ip->ip_sum = 0;
508105197Ssam#ifdef _IP_VHL
509105197Ssam			if (ip->ip_vhl == IP_VHL_BORING)
510105197Ssam				ip->ip_sum = in_cksum_hdr(ip);
511105197Ssam			else
512105197Ssam				ip->ip_sum = in_cksum(m,
513105197Ssam					_IP_VHL_HL(ip->ip_vhl) << 2);
514105197Ssam#else
515105197Ssam			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
516105197Ssam#endif
517105197Ssam
518105197Ssam			/* Encapsulate the packet */
519105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
520105197Ssam			if (mp == NULL && !error) {
521105197Ssam				/* Should never happen. */
522120585Ssam				DPRINTF(("%s: ipip_output returns no mbuf and "
523120585Ssam					"no error!", __func__));
524105197Ssam				error = EFAULT;
525105197Ssam			}
526105197Ssam			if (error) {
527124765Ssam				if (mp) {
528124765Ssam					/* XXX: Should never happen! */
529105197Ssam					m_freem(mp);
530124765Ssam				}
531124765Ssam				m = NULL; /* ipip_output() already freed it */
532105197Ssam				goto bad;
533105197Ssam			}
534105197Ssam			m = mp, mp = NULL;
535105197Ssam			/*
536105197Ssam			 * ipip_output clears IP_DF in the new header.  If
537105197Ssam			 * we need to propagate IP_DF from the outer header,
538105197Ssam			 * then we have to do it here.
539105197Ssam			 *
540105197Ssam			 * XXX shouldn't assume what ipip_output does.
541105197Ssam			 */
542105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
543105197Ssam				if (m->m_len < sizeof (struct ip) &&
544105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
545105197Ssam					error = ENOBUFS;
546105197Ssam					goto bad;
547105197Ssam				}
548105197Ssam				ip = mtod(m, struct ip *);
549105197Ssam				ip->ip_off = ntohs(ip->ip_off);
550105197Ssam				ip->ip_off |= IP_DF;
551105197Ssam				ip->ip_off = htons(ip->ip_off);
552105197Ssam			}
553105197Ssam		}
554105197Ssam	}
555105197Ssam
556159965Sthompsa#ifdef DEV_ENC
557159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
558174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
559174054Sbz	/* pass the mbuf to enc0 for packet filtering */
560174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
561174054Sbz		goto bad;
562159965Sthompsa#endif
563159965Sthompsa
564105197Ssam	/*
565105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
566105197Ssam	 * packet will be returned for transmission after crypto
567105197Ssam	 * processing, etc. are completed.  For encapsulation we
568105197Ssam	 * bypass this call because of the explicit call done above
569105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
570105197Ssam	 *
571105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
572105197Ssam	 *     for reclaiming their resources.
573105197Ssam	 */
574105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
575105197Ssam		ip = mtod(m, struct ip *);
576105197Ssam		i = ip->ip_hl << 2;
577105197Ssam		off = offsetof(struct ip, ip_p);
578105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
579105197Ssam	} else {
580105197Ssam		error = ipsec_process_done(m, isr);
581105197Ssam	}
582120585Ssam	IPSECREQUEST_UNLOCK(isr);
583105197Ssam	return error;
584105197Ssambad:
585120585Ssam	if (isr)
586120585Ssam		IPSECREQUEST_UNLOCK(isr);
587105197Ssam	if (m)
588105197Ssam		m_freem(m);
589105197Ssam	return error;
590105197Ssam}
591105197Ssam#endif
592105197Ssam
593105197Ssam#ifdef INET6
594105197Ssam/*
595105197Ssam * Chop IP6 header from the payload.
596105197Ssam */
597105197Ssamstatic struct mbuf *
598105197Ssamipsec6_splithdr(struct mbuf *m)
599105197Ssam{
600105197Ssam	struct mbuf *mh;
601105197Ssam	struct ip6_hdr *ip6;
602105197Ssam	int hlen;
603105197Ssam
604120585Ssam	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
605120585Ssam		("first mbuf too short, len %u", m->m_len));
606105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
607105197Ssam	hlen = sizeof(struct ip6_hdr);
608105197Ssam	if (m->m_len > hlen) {
609151967Sandre		MGETHDR(mh, M_DONTWAIT, MT_DATA);
610105197Ssam		if (!mh) {
611105197Ssam			m_freem(m);
612105197Ssam			return NULL;
613105197Ssam		}
614108466Ssam		M_MOVE_PKTHDR(mh, m);
615105197Ssam		MH_ALIGN(mh, hlen);
616105197Ssam		m->m_len -= hlen;
617105197Ssam		m->m_data += hlen;
618105197Ssam		mh->m_next = m;
619105197Ssam		m = mh;
620105197Ssam		m->m_len = hlen;
621105197Ssam		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
622105197Ssam	} else if (m->m_len < hlen) {
623105197Ssam		m = m_pullup(m, hlen);
624105197Ssam		if (!m)
625105197Ssam			return NULL;
626105197Ssam	}
627105197Ssam	return m;
628105197Ssam}
629105197Ssam
630105197Ssam/*
631105197Ssam * IPsec output logic for IPv6, transport mode.
632105197Ssam */
633105197Ssamint
634105197Ssamipsec6_output_trans(
635105197Ssam	struct ipsec_output_state *state,
636105197Ssam	u_char *nexthdrp,
637105197Ssam	struct mbuf *mprev,
638105197Ssam	struct secpolicy *sp,
639105197Ssam	int flags,
640105197Ssam	int *tun)
641105197Ssam{
642105197Ssam	struct ipsecrequest *isr;
643105197Ssam	struct secasindex saidx;
644105197Ssam	int error = 0;
645105197Ssam	struct mbuf *m;
646105197Ssam
647120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
648120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
649120585Ssam	IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
650120585Ssam	IPSEC_ASSERT(mprev != NULL, ("null mprev"));
651120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
652120585Ssam	IPSEC_ASSERT(tun != NULL, ("null tun"));
653105197Ssam
654105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
655170122Sbz		printf("%s: applied SP\n", __func__);
656105197Ssam		kdebug_secpolicy(sp));
657105197Ssam
658105197Ssam	isr = sp->req;
659105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
660105197Ssam		/* the rest will be handled by ipsec6_output_tunnel() */
661105197Ssam		*tun = 1;		/* need tunnel-mode processing */
662105197Ssam		return 0;
663105197Ssam	}
664105197Ssam
665105197Ssam	*tun = 0;
666105197Ssam	m = state->m;
667105197Ssam
668171133Sgnn	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
669105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
670105197Ssam	if (isr == NULL) {
671177175Sbz		if (error != 0) {
672105197Ssam#ifdef notdef
673177175Sbz			/* XXX should notification be done for all errors ? */
674177175Sbz			/*
675177175Sbz			 * Notify the fact that the packet is discarded
676177175Sbz			 * to ourselves. I believe this is better than
677177175Sbz			 * just silently discarding. (jinmei@kame.net)
678177175Sbz			 * XXX: should we restrict the error to TCP packets?
679177175Sbz			 * XXX: should we directly notify sockets via
680177175Sbz			 *      pfctlinputs?
681177175Sbz			 */
682177175Sbz			icmp6_error(m, ICMP6_DST_UNREACH,
683177175Sbz				    ICMP6_DST_UNREACH_ADMIN, 0);
684177175Sbz			m = NULL;	/* NB: icmp6_error frees mbuf */
685105197Ssam#endif
686177175Sbz			goto bad;
687177175Sbz		}
688177175Sbz		return EJUSTRETURN;
689105197Ssam	}
690105197Ssam
691171133Sgnn	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
692171133Sgnn						  sizeof (struct ip6_hdr),
693171133Sgnn						  offsetof(struct ip6_hdr,
694171133Sgnn							   ip6_nxt));
695171133Sgnn	IPSECREQUEST_UNLOCK(isr);
696171133Sgnn	return error;
697105197Ssambad:
698171133Sgnn	if (isr)
699171133Sgnn		IPSECREQUEST_UNLOCK(isr);
700105197Ssam	if (m)
701105197Ssam		m_freem(m);
702105197Ssam	state->m = NULL;
703105197Ssam	return error;
704105197Ssam}
705105197Ssam
706105197Ssamstatic int
707105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
708105197Ssam{
709105197Ssam	struct ip6_hdr *oip6;
710105197Ssam	struct ip6_hdr *ip6;
711105197Ssam	size_t plen;
712105197Ssam
713105197Ssam	/* can't tunnel between different AFs */
714105197Ssam	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
715105197Ssam	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
716105197Ssam		m_freem(m);
717105197Ssam		return EINVAL;
718105197Ssam	}
719171133Sgnn	IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
720120585Ssam		("mbuf wrong size; len %u", m->m_len));
721105197Ssam
722105197Ssam
723105197Ssam	/*
724105197Ssam	 * grow the mbuf to accomodate the new IPv6 header.
725105197Ssam	 */
726105197Ssam	plen = m->m_pkthdr.len;
727105197Ssam	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
728105197Ssam		struct mbuf *n;
729111119Simp		MGET(n, M_DONTWAIT, MT_DATA);
730105197Ssam		if (!n) {
731105197Ssam			m_freem(m);
732105197Ssam			return ENOBUFS;
733105197Ssam		}
734105197Ssam		n->m_len = sizeof(struct ip6_hdr);
735105197Ssam		n->m_next = m->m_next;
736105197Ssam		m->m_next = n;
737105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
738105197Ssam		oip6 = mtod(n, struct ip6_hdr *);
739105197Ssam	} else {
740105197Ssam		m->m_next->m_len += sizeof(struct ip6_hdr);
741105197Ssam		m->m_next->m_data -= sizeof(struct ip6_hdr);
742105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
743105197Ssam		oip6 = mtod(m->m_next, struct ip6_hdr *);
744105197Ssam	}
745105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
746113076Sdes	bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
747105197Ssam
748105197Ssam	/* Fake link-local scope-class addresses */
749105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
750105197Ssam		oip6->ip6_src.s6_addr16[1] = 0;
751105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
752105197Ssam		oip6->ip6_dst.s6_addr16[1] = 0;
753105197Ssam
754105197Ssam	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
755105197Ssam	/* ECN consideration. */
756181803Sbz	ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
757105197Ssam	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
758105197Ssam		ip6->ip6_plen = htons(plen);
759105197Ssam	else {
760105197Ssam		/* ip6->ip6_plen will be updated in ip6_output() */
761105197Ssam	}
762105197Ssam	ip6->ip6_nxt = IPPROTO_IPV6;
763171133Sgnn	ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
764171133Sgnn	ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
765105197Ssam	ip6->ip6_hlim = IPV6_DEFHLIM;
766105197Ssam
767105197Ssam	/* XXX Should ip6_src be updated later ? */
768105197Ssam
769105197Ssam	return 0;
770105197Ssam}
771105197Ssam
772105197Ssam/*
773105197Ssam * IPsec output logic for IPv6, tunnel mode.
774105197Ssam */
775105197Ssamint
776105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
777105197Ssam{
778105197Ssam	struct ip6_hdr *ip6;
779105197Ssam	struct ipsecrequest *isr;
780105197Ssam	struct secasindex saidx;
781105197Ssam	int error;
782213836Sbz	struct sockaddr_in6 *dst6;
783105197Ssam	struct mbuf *m;
784105197Ssam
785120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
786120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
787120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
788105197Ssam
789105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
790170122Sbz		printf("%s: applied SP\n", __func__);
791105197Ssam		kdebug_secpolicy(sp));
792105197Ssam
793105197Ssam	m = state->m;
794105197Ssam	/*
795105197Ssam	 * transport mode ipsec (before the 1st tunnel mode) is already
796105197Ssam	 * processed by ipsec6_output_trans().
797105197Ssam	 */
798105197Ssam	for (isr = sp->req; isr; isr = isr->next) {
799105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
800105197Ssam			break;
801105197Ssam	}
802170123Sbz
803170123Sbz	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
804105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
805177175Sbz	if (isr == NULL) {
806177175Sbz		if (error != 0)
807177175Sbz			goto bad;
808177175Sbz		return EJUSTRETURN;
809177175Sbz	}
810105197Ssam
811174054Sbz#ifdef DEV_ENC
812181627Svanhu	encif->if_opackets++;
813181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
814181627Svanhu
815174054Sbz	/* pass the mbuf to enc0 for bpf processing */
816174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
817174054Sbz	/* pass the mbuf to enc0 for packet filtering */
818174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
819174054Sbz		goto bad;
820174054Sbz#endif
821174054Sbz
822105197Ssam	/*
823105197Ssam	 * There may be the case that SA status will be changed when
824105197Ssam	 * we are refering to one. So calling splsoftnet().
825105197Ssam	 */
826105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
827105197Ssam		/*
828105197Ssam		 * build IPsec tunnel.
829105197Ssam		 */
830105197Ssam		/* XXX should be processed with other familiy */
831105197Ssam		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
832120585Ssam			ipseclog((LOG_ERR, "%s: family mismatched between "
833120585Ssam			    "inner and outer, spi=%u\n", __func__,
834105197Ssam			    ntohl(isr->sav->spi)));
835181803Sbz			V_ipsec6stat.ips_out_inval++;
836105197Ssam			error = EAFNOSUPPORT;
837105197Ssam			goto bad;
838105197Ssam		}
839105197Ssam
840105197Ssam		m = ipsec6_splithdr(m);
841105197Ssam		if (!m) {
842181803Sbz			V_ipsec6stat.ips_out_nomem++;
843105197Ssam			error = ENOMEM;
844105197Ssam			goto bad;
845105197Ssam		}
846105197Ssam		error = ipsec6_encapsulate(m, isr->sav);
847105197Ssam		if (error) {
848105197Ssam			m = NULL;
849105197Ssam			goto bad;
850105197Ssam		}
851105197Ssam		ip6 = mtod(m, struct ip6_hdr *);
852105197Ssam
853214250Sbz		state->ro =
854214250Sbz		    (struct route *)&isr->sav->sah->route_cache.sin6_route;
855105197Ssam		state->dst = (struct sockaddr *)&state->ro->ro_dst;
856105197Ssam		dst6 = (struct sockaddr_in6 *)state->dst;
857105197Ssam		if (state->ro->ro_rt
858105197Ssam		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
859105197Ssam		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
860105197Ssam			RTFREE(state->ro->ro_rt);
861105197Ssam			state->ro->ro_rt = NULL;
862105197Ssam		}
863187936Sbz		if (state->ro->ro_rt == NULL) {
864105197Ssam			bzero(dst6, sizeof(*dst6));
865105197Ssam			dst6->sin6_family = AF_INET6;
866105197Ssam			dst6->sin6_len = sizeof(*dst6);
867105197Ssam			dst6->sin6_addr = ip6->ip6_dst;
868105197Ssam			rtalloc(state->ro);
869105197Ssam		}
870187936Sbz		if (state->ro->ro_rt == NULL) {
871181803Sbz			V_ip6stat.ip6s_noroute++;
872181803Sbz			V_ipsec6stat.ips_out_noroute++;
873105197Ssam			error = EHOSTUNREACH;
874105197Ssam			goto bad;
875105197Ssam		}
876105197Ssam
877105197Ssam		/* adjust state->dst if tunnel endpoint is offlink */
878213837Sbz		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
879105197Ssam			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
880105197Ssam	}
881105197Ssam
882105197Ssam	m = ipsec6_splithdr(m);
883105197Ssam	if (!m) {
884181803Sbz		V_ipsec6stat.ips_out_nomem++;
885105197Ssam		error = ENOMEM;
886105197Ssam		goto bad;
887105197Ssam	}
888105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
889174054Sbz
890174054Sbz#ifdef DEV_ENC
891174054Sbz	/* pass the mbuf to enc0 for bpf processing */
892174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
893174054Sbz	/* pass the mbuf to enc0 for packet filtering */
894174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
895174054Sbz		goto bad;
896174054Sbz#endif
897174054Sbz
898170123Sbz	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
899105197Ssam		sizeof (struct ip6_hdr),
900105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
901170123Sbz	IPSECREQUEST_UNLOCK(isr);
902170123Sbz	return error;
903105197Ssambad:
904170123Sbz	if (isr)
905170123Sbz		IPSECREQUEST_UNLOCK(isr);
906105197Ssam	if (m)
907105197Ssam		m_freem(m);
908105197Ssam	state->m = NULL;
909105197Ssam	return error;
910105197Ssam}
911105197Ssam#endif /*INET6*/
912