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$
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) {
167252692Sae		IPSECSTAT_INC(ips_out_bundlesa);
168238777Sbz		/* XXX-BZ currently only support same AF bundles. */
169221129Sbz		switch (saidx->dst.sa.sa_family) {
170221129Sbz#ifdef INET
171221129Sbz		case AF_INET:
172221129Sbz			return ipsec4_process_packet(m, isr->next, 0, 0);
173221129Sbz			/* NOTREACHED */
174221129Sbz#endif
175221129Sbz#ifdef notyet
176221129Sbz#ifdef INET6
177221129Sbz		case AF_INET6:
178221129Sbz			/* XXX */
179221129Sbz			ipsec6_output_trans()
180221129Sbz			ipsec6_output_tunnel()
181221129Sbz			/* NOTREACHED */
182221129Sbz#endif /* INET6 */
183221129Sbz#endif
184221129Sbz		default:
185221129Sbz			DPRINTF(("%s: unknown protocol family %u\n", __func__,
186221129Sbz			    saidx->dst.sa.sa_family));
187221129Sbz			error = ENXIO;
188221129Sbz			goto bad;
189221129Sbz		}
190105197Ssam	}
191117056Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
192105197Ssam
193223637Sbz	m_addr_changed(m);
194223637Sbz
195105197Ssam	/*
196105197Ssam	 * We're done with IPsec processing, transmit the packet using the
197105197Ssam	 * appropriate network protocol (IP or IPv6). SPD lookup will be
198105197Ssam	 * performed again there.
199105197Ssam	 */
200105197Ssam	switch (saidx->dst.sa.sa_family) {
201105197Ssam#ifdef INET
202105197Ssam	struct ip *ip;
203105197Ssam	case AF_INET:
204105197Ssam		ip = mtod(m, struct ip *);
205105197Ssam		ip->ip_len = ntohs(ip->ip_len);
206105197Ssam		ip->ip_off = ntohs(ip->ip_off);
207105197Ssam
208194062Svanhu#ifdef IPSEC_NAT_T
209194062Svanhu		/*
210194062Svanhu		 * If NAT-T is enabled, now that all IPsec processing is done
211194062Svanhu		 * insert UDP encapsulation header after IP header.
212194062Svanhu		 */
213194062Svanhu		if (sav->natt_type) {
214194062Svanhu			const int hlen = (ip->ip_hl << 2);
215194062Svanhu			int size, off;
216194062Svanhu			struct mbuf *mi;
217194062Svanhu			struct udphdr *udp;
218194062Svanhu
219194062Svanhu			size = sizeof(struct udphdr);
220194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
221194062Svanhu				/*
222194062Svanhu				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
223194062Svanhu				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
224194062Svanhu				 * ignoring possible AH mode
225194062Svanhu				 * non-IKE marker + non-ESP marker
226194062Svanhu				 * from draft-ietf-ipsec-udp-encaps-00.txt.
227194062Svanhu				 */
228194062Svanhu				size += sizeof(u_int64_t);
229194062Svanhu			}
230194062Svanhu			mi = m_makespace(m, hlen, size, &off);
231194062Svanhu			if (mi == NULL) {
232194062Svanhu				DPRINTF(("%s: m_makespace for udphdr failed\n",
233194062Svanhu				    __func__));
234194062Svanhu				error = ENOBUFS;
235194062Svanhu				goto bad;
236194062Svanhu			}
237194062Svanhu
238194062Svanhu			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
239194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
240194062Svanhu				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
241194062Svanhu			else
242194062Svanhu				udp->uh_sport =
243194062Svanhu					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
244194062Svanhu			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
245194062Svanhu			udp->uh_sum = 0;
246194062Svanhu			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
247194062Svanhu			ip->ip_len = m->m_pkthdr.len;
248194062Svanhu			ip->ip_p = IPPROTO_UDP;
249194062Svanhu
250194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
251194062Svanhu				*(u_int64_t *)(udp + 1) = 0;
252194062Svanhu		}
253194062Svanhu#endif /* IPSEC_NAT_T */
254194062Svanhu
255105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
256105197Ssam#endif /* INET */
257105197Ssam#ifdef INET6
258105197Ssam	case AF_INET6:
259105197Ssam		/*
260105197Ssam		 * We don't need massage, IPv6 header fields are always in
261105197Ssam		 * net endian.
262105197Ssam		 */
263105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
264105197Ssam#endif /* INET6 */
265105197Ssam	}
266105197Ssam	panic("ipsec_process_done");
267105197Ssambad:
268105197Ssam	m_freem(m);
269105197Ssam	return (error);
270105197Ssam}
271105197Ssam
272105197Ssamstatic struct ipsecrequest *
273105197Ssamipsec_nextisr(
274105197Ssam	struct mbuf *m,
275105197Ssam	struct ipsecrequest *isr,
276105197Ssam	int af,
277105197Ssam	struct secasindex *saidx,
278105197Ssam	int *error
279105197Ssam)
280105197Ssam{
281252693Sae#define	IPSEC_OSTAT(name)	do {		\
282252693Sae	if (isr->saidx.proto == IPPROTO_ESP)	\
283252693Sae		ESPSTAT_INC(esps_##name);	\
284252693Sae	else if (isr->saidx.proto == IPPROTO_AH)\
285252693Sae		AHSTAT_INC(ahs_##name);		\
286252693Sae	else					\
287252693Sae		IPCOMPSTAT_INC(ipcomps_##name);	\
288252693Sae} while (0)
289105197Ssam	struct secasvar *sav;
290105197Ssam
291120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
292120585Ssam
293120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
294120585Ssam		("invalid address family %u", af));
295105197Ssamagain:
296105197Ssam	/*
297105197Ssam	 * Craft SA index to search for proper SA.  Note that
298105197Ssam	 * we only fillin unspecified SA peers for transport
299105197Ssam	 * mode; for tunnel mode they must already be filled in.
300105197Ssam	 */
301105197Ssam	*saidx = isr->saidx;
302105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
303105197Ssam		/* Fillin unspecified SA peers only for transport mode */
304105197Ssam		if (af == AF_INET) {
305105197Ssam			struct sockaddr_in *sin;
306105197Ssam			struct ip *ip = mtod(m, struct ip *);
307105197Ssam
308105197Ssam			if (saidx->src.sa.sa_len == 0) {
309105197Ssam				sin = &saidx->src.sin;
310105197Ssam				sin->sin_len = sizeof(*sin);
311105197Ssam				sin->sin_family = AF_INET;
312105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
313105197Ssam				sin->sin_addr = ip->ip_src;
314105197Ssam			}
315105197Ssam			if (saidx->dst.sa.sa_len == 0) {
316105197Ssam				sin = &saidx->dst.sin;
317105197Ssam				sin->sin_len = sizeof(*sin);
318105197Ssam				sin->sin_family = AF_INET;
319105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
320105197Ssam				sin->sin_addr = ip->ip_dst;
321105197Ssam			}
322105197Ssam		} else {
323105197Ssam			struct sockaddr_in6 *sin6;
324105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
325105197Ssam
326105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
327105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
328105197Ssam				sin6->sin6_len = sizeof(*sin6);
329105197Ssam				sin6->sin6_family = AF_INET6;
330105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
331105197Ssam				sin6->sin6_addr = ip6->ip6_src;
332105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
333105197Ssam					/* fix scope id for comparing SPD */
334105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
335105197Ssam					sin6->sin6_scope_id =
336105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
337105197Ssam				}
338105197Ssam			}
339105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
340105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
341105197Ssam				sin6->sin6_len = sizeof(*sin6);
342105197Ssam				sin6->sin6_family = AF_INET6;
343105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
344105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
345105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
346105197Ssam					/* fix scope id for comparing SPD */
347105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
348105197Ssam					sin6->sin6_scope_id =
349105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
350105197Ssam				}
351105197Ssam			}
352105197Ssam		}
353105197Ssam	}
354105197Ssam
355105197Ssam	/*
356105197Ssam	 * Lookup SA and validate it.
357105197Ssam	 */
358105197Ssam	*error = key_checkrequest(isr, saidx);
359105197Ssam	if (*error != 0) {
360105197Ssam		/*
361105197Ssam		 * IPsec processing is required, but no SA found.
362105197Ssam		 * I assume that key_acquire() had been called
363105197Ssam		 * to get/establish the SA. Here I discard
364105197Ssam		 * this packet because it is responsibility for
365105197Ssam		 * upper layer to retransmit the packet.
366105197Ssam		 */
367252692Sae		IPSECSTAT_INC(ips_out_nosa);
368105197Ssam		goto bad;
369105197Ssam	}
370105197Ssam	sav = isr->sav;
371177175Sbz	if (sav == NULL) {
372120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
373120585Ssam			("no SA found, but required; level %u",
374105197Ssam			ipsec_get_reqlevel(isr)));
375120585Ssam		IPSECREQUEST_UNLOCK(isr);
376105197Ssam		isr = isr->next;
377177175Sbz		/*
378177175Sbz		 * If isr is NULL, we found a 'use' policy w/o SA.
379177175Sbz		 * Return w/o error and w/o isr so we can drop out
380177175Sbz		 * and continue w/o IPsec processing.
381177175Sbz		 */
382177175Sbz		if (isr == NULL)
383105197Ssam			return isr;
384120585Ssam		IPSECREQUEST_LOCK(isr);
385105197Ssam		goto again;
386105197Ssam	}
387105197Ssam
388105197Ssam	/*
389105197Ssam	 * Check system global policy controls.
390105197Ssam	 */
391181803Sbz	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
392181803Sbz	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
393181803Sbz	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
394120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
395120585Ssam			" to policy (check your sysctls)\n", __func__));
396252693Sae		IPSEC_OSTAT(pdrops);
397105197Ssam		*error = EHOSTUNREACH;
398105197Ssam		goto bad;
399105197Ssam	}
400105197Ssam
401105197Ssam	/*
402105197Ssam	 * Sanity check the SA contents for the caller
403105197Ssam	 * before they invoke the xform output method.
404105197Ssam	 */
405105197Ssam	if (sav->tdb_xform == NULL) {
406120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
407252693Sae		IPSEC_OSTAT(noxform);
408105197Ssam		*error = EHOSTUNREACH;
409105197Ssam		goto bad;
410105197Ssam	}
411105197Ssam	return isr;
412105197Ssambad:
413120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
414120585Ssam	IPSECREQUEST_UNLOCK(isr);
415105197Ssam	return NULL;
416105197Ssam#undef IPSEC_OSTAT
417105197Ssam}
418105197Ssam
419105197Ssam#ifdef INET
420105197Ssam/*
421105197Ssam * IPsec output logic for IPv4.
422105197Ssam */
423105197Ssamint
424105197Ssamipsec4_process_packet(
425105197Ssam	struct mbuf *m,
426105197Ssam	struct ipsecrequest *isr,
427105197Ssam	int flags,
428105197Ssam	int tunalready)
429105197Ssam{
430105197Ssam	struct secasindex saidx;
431105197Ssam	struct secasvar *sav;
432105197Ssam	struct ip *ip;
433119643Ssam	int error, i, off;
434105197Ssam
435120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
436120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
437105197Ssam
438120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
439105197Ssam
440105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
441177175Sbz	if (isr == NULL) {
442177175Sbz		if (error != 0)
443177175Sbz			goto bad;
444177175Sbz		return EJUSTRETURN;
445177175Sbz	}
446105197Ssam
447105197Ssam	sav = isr->sav;
448159965Sthompsa
449159965Sthompsa#ifdef DEV_ENC
450181627Svanhu	encif->if_opackets++;
451181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
452181627Svanhu
453174054Sbz	/* pass the mbuf to enc0 for bpf processing */
454174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
455159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
456174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
457159965Sthompsa		goto bad;
458159965Sthompsa#endif
459159965Sthompsa
460105197Ssam	if (!tunalready) {
461105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
462105197Ssam		int setdf;
463105197Ssam
464105197Ssam		/*
465105197Ssam		 * Collect IP_DF state from the outer header.
466105197Ssam		 */
467105197Ssam		if (dst->sa.sa_family == AF_INET) {
468105197Ssam			if (m->m_len < sizeof (struct ip) &&
469105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
470105197Ssam				error = ENOBUFS;
471105197Ssam				goto bad;
472105197Ssam			}
473105197Ssam			ip = mtod(m, struct ip *);
474105197Ssam			/* Honor system-wide control of how to handle IP_DF */
475181803Sbz			switch (V_ip4_ipsec_dfbit) {
476105197Ssam			case 0:			/* clear in outer header */
477105197Ssam			case 1:			/* set in outer header */
478181803Sbz				setdf = V_ip4_ipsec_dfbit;
479105197Ssam				break;
480105197Ssam			default:		/* propagate to outer header */
481105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
482105197Ssam				break;
483105197Ssam			}
484105197Ssam		} else {
485105197Ssam			ip = NULL;		/* keep compiler happy */
486105197Ssam			setdf = 0;
487105197Ssam		}
488105197Ssam		/* Do the appropriate encapsulation, if necessary */
489105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
490105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
491105197Ssam#if 0
492105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
493105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
494105197Ssam#endif
495105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
496105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
497105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
498105197Ssam			struct mbuf *mp;
499105197Ssam
500105197Ssam			/* Fix IPv4 header checksum and length */
501105197Ssam			if (m->m_len < sizeof (struct ip) &&
502105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
503105197Ssam				error = ENOBUFS;
504105197Ssam				goto bad;
505105197Ssam			}
506105197Ssam			ip = mtod(m, struct ip *);
507105197Ssam			ip->ip_len = htons(m->m_pkthdr.len);
508105197Ssam			ip->ip_sum = 0;
509105197Ssam			ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
510105197Ssam
511105197Ssam			/* Encapsulate the packet */
512105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
513105197Ssam			if (mp == NULL && !error) {
514105197Ssam				/* Should never happen. */
515120585Ssam				DPRINTF(("%s: ipip_output returns no mbuf and "
516120585Ssam					"no error!", __func__));
517105197Ssam				error = EFAULT;
518105197Ssam			}
519105197Ssam			if (error) {
520124765Ssam				if (mp) {
521124765Ssam					/* XXX: Should never happen! */
522105197Ssam					m_freem(mp);
523124765Ssam				}
524124765Ssam				m = NULL; /* ipip_output() already freed it */
525105197Ssam				goto bad;
526105197Ssam			}
527105197Ssam			m = mp, mp = NULL;
528105197Ssam			/*
529105197Ssam			 * ipip_output clears IP_DF in the new header.  If
530105197Ssam			 * we need to propagate IP_DF from the outer header,
531105197Ssam			 * then we have to do it here.
532105197Ssam			 *
533105197Ssam			 * XXX shouldn't assume what ipip_output does.
534105197Ssam			 */
535105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
536105197Ssam				if (m->m_len < sizeof (struct ip) &&
537105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
538105197Ssam					error = ENOBUFS;
539105197Ssam					goto bad;
540105197Ssam				}
541105197Ssam				ip = mtod(m, struct ip *);
542105197Ssam				ip->ip_off = ntohs(ip->ip_off);
543105197Ssam				ip->ip_off |= IP_DF;
544105197Ssam				ip->ip_off = htons(ip->ip_off);
545105197Ssam			}
546105197Ssam		}
547105197Ssam	}
548105197Ssam
549159965Sthompsa#ifdef DEV_ENC
550159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
551174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
552174054Sbz	/* pass the mbuf to enc0 for packet filtering */
553174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
554174054Sbz		goto bad;
555159965Sthompsa#endif
556159965Sthompsa
557105197Ssam	/*
558105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
559105197Ssam	 * packet will be returned for transmission after crypto
560105197Ssam	 * processing, etc. are completed.  For encapsulation we
561105197Ssam	 * bypass this call because of the explicit call done above
562105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
563105197Ssam	 *
564105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
565105197Ssam	 *     for reclaiming their resources.
566105197Ssam	 */
567105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
568105197Ssam		ip = mtod(m, struct ip *);
569105197Ssam		i = ip->ip_hl << 2;
570105197Ssam		off = offsetof(struct ip, ip_p);
571105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
572105197Ssam	} else {
573105197Ssam		error = ipsec_process_done(m, isr);
574105197Ssam	}
575120585Ssam	IPSECREQUEST_UNLOCK(isr);
576105197Ssam	return error;
577105197Ssambad:
578120585Ssam	if (isr)
579120585Ssam		IPSECREQUEST_UNLOCK(isr);
580105197Ssam	if (m)
581105197Ssam		m_freem(m);
582105197Ssam	return error;
583105197Ssam}
584105197Ssam#endif
585105197Ssam
586105197Ssam#ifdef INET6
587105197Ssam/*
588105197Ssam * Chop IP6 header from the payload.
589105197Ssam */
590105197Ssamstatic struct mbuf *
591105197Ssamipsec6_splithdr(struct mbuf *m)
592105197Ssam{
593105197Ssam	struct mbuf *mh;
594105197Ssam	struct ip6_hdr *ip6;
595105197Ssam	int hlen;
596105197Ssam
597120585Ssam	IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
598120585Ssam		("first mbuf too short, len %u", m->m_len));
599105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
600105197Ssam	hlen = sizeof(struct ip6_hdr);
601105197Ssam	if (m->m_len > hlen) {
602151967Sandre		MGETHDR(mh, M_DONTWAIT, MT_DATA);
603105197Ssam		if (!mh) {
604105197Ssam			m_freem(m);
605105197Ssam			return NULL;
606105197Ssam		}
607108466Ssam		M_MOVE_PKTHDR(mh, m);
608105197Ssam		MH_ALIGN(mh, hlen);
609105197Ssam		m->m_len -= hlen;
610105197Ssam		m->m_data += hlen;
611105197Ssam		mh->m_next = m;
612105197Ssam		m = mh;
613105197Ssam		m->m_len = hlen;
614105197Ssam		bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
615105197Ssam	} else if (m->m_len < hlen) {
616105197Ssam		m = m_pullup(m, hlen);
617105197Ssam		if (!m)
618105197Ssam			return NULL;
619105197Ssam	}
620105197Ssam	return m;
621105197Ssam}
622105197Ssam
623105197Ssam/*
624105197Ssam * IPsec output logic for IPv6, transport mode.
625105197Ssam */
626105197Ssamint
627105197Ssamipsec6_output_trans(
628105197Ssam	struct ipsec_output_state *state,
629105197Ssam	u_char *nexthdrp,
630105197Ssam	struct mbuf *mprev,
631105197Ssam	struct secpolicy *sp,
632105197Ssam	int flags,
633105197Ssam	int *tun)
634105197Ssam{
635105197Ssam	struct ipsecrequest *isr;
636105197Ssam	struct secasindex saidx;
637105197Ssam	int error = 0;
638105197Ssam	struct mbuf *m;
639105197Ssam
640120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
641120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
642120585Ssam	IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
643120585Ssam	IPSEC_ASSERT(mprev != NULL, ("null mprev"));
644120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
645120585Ssam	IPSEC_ASSERT(tun != NULL, ("null tun"));
646105197Ssam
647105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
648170122Sbz		printf("%s: applied SP\n", __func__);
649105197Ssam		kdebug_secpolicy(sp));
650105197Ssam
651105197Ssam	isr = sp->req;
652105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
653105197Ssam		/* the rest will be handled by ipsec6_output_tunnel() */
654105197Ssam		*tun = 1;		/* need tunnel-mode processing */
655105197Ssam		return 0;
656105197Ssam	}
657105197Ssam
658105197Ssam	*tun = 0;
659105197Ssam	m = state->m;
660105197Ssam
661171133Sgnn	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
662105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
663105197Ssam	if (isr == NULL) {
664177175Sbz		if (error != 0) {
665105197Ssam#ifdef notdef
666177175Sbz			/* XXX should notification be done for all errors ? */
667177175Sbz			/*
668177175Sbz			 * Notify the fact that the packet is discarded
669177175Sbz			 * to ourselves. I believe this is better than
670177175Sbz			 * just silently discarding. (jinmei@kame.net)
671177175Sbz			 * XXX: should we restrict the error to TCP packets?
672177175Sbz			 * XXX: should we directly notify sockets via
673177175Sbz			 *      pfctlinputs?
674177175Sbz			 */
675177175Sbz			icmp6_error(m, ICMP6_DST_UNREACH,
676177175Sbz				    ICMP6_DST_UNREACH_ADMIN, 0);
677177175Sbz			m = NULL;	/* NB: icmp6_error frees mbuf */
678105197Ssam#endif
679177175Sbz			goto bad;
680177175Sbz		}
681177175Sbz		return EJUSTRETURN;
682105197Ssam	}
683105197Ssam
684171133Sgnn	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
685171133Sgnn						  sizeof (struct ip6_hdr),
686171133Sgnn						  offsetof(struct ip6_hdr,
687171133Sgnn							   ip6_nxt));
688171133Sgnn	IPSECREQUEST_UNLOCK(isr);
689171133Sgnn	return error;
690105197Ssambad:
691171133Sgnn	if (isr)
692171133Sgnn		IPSECREQUEST_UNLOCK(isr);
693105197Ssam	if (m)
694105197Ssam		m_freem(m);
695105197Ssam	state->m = NULL;
696105197Ssam	return error;
697105197Ssam}
698105197Ssam
699105197Ssamstatic int
700105197Ssamipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
701105197Ssam{
702105197Ssam	struct ip6_hdr *oip6;
703105197Ssam	struct ip6_hdr *ip6;
704105197Ssam	size_t plen;
705105197Ssam
706105197Ssam	/* can't tunnel between different AFs */
707105197Ssam	if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
708105197Ssam	    sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
709105197Ssam		m_freem(m);
710105197Ssam		return EINVAL;
711105197Ssam	}
712171133Sgnn	IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
713120585Ssam		("mbuf wrong size; len %u", m->m_len));
714105197Ssam
715105197Ssam
716105197Ssam	/*
717105197Ssam	 * grow the mbuf to accomodate the new IPv6 header.
718105197Ssam	 */
719105197Ssam	plen = m->m_pkthdr.len;
720105197Ssam	if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
721105197Ssam		struct mbuf *n;
722111119Simp		MGET(n, M_DONTWAIT, MT_DATA);
723105197Ssam		if (!n) {
724105197Ssam			m_freem(m);
725105197Ssam			return ENOBUFS;
726105197Ssam		}
727105197Ssam		n->m_len = sizeof(struct ip6_hdr);
728105197Ssam		n->m_next = m->m_next;
729105197Ssam		m->m_next = n;
730105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
731105197Ssam		oip6 = mtod(n, struct ip6_hdr *);
732105197Ssam	} else {
733105197Ssam		m->m_next->m_len += sizeof(struct ip6_hdr);
734105197Ssam		m->m_next->m_data -= sizeof(struct ip6_hdr);
735105197Ssam		m->m_pkthdr.len += sizeof(struct ip6_hdr);
736105197Ssam		oip6 = mtod(m->m_next, struct ip6_hdr *);
737105197Ssam	}
738105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
739113076Sdes	bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
740105197Ssam
741105197Ssam	/* Fake link-local scope-class addresses */
742105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
743105197Ssam		oip6->ip6_src.s6_addr16[1] = 0;
744105197Ssam	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
745105197Ssam		oip6->ip6_dst.s6_addr16[1] = 0;
746105197Ssam
747105197Ssam	/* construct new IPv6 header. see RFC 2401 5.1.2.2 */
748105197Ssam	/* ECN consideration. */
749181803Sbz	ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
750105197Ssam	if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
751105197Ssam		ip6->ip6_plen = htons(plen);
752105197Ssam	else {
753105197Ssam		/* ip6->ip6_plen will be updated in ip6_output() */
754105197Ssam	}
755105197Ssam	ip6->ip6_nxt = IPPROTO_IPV6;
756171133Sgnn	ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
757171133Sgnn	ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
758105197Ssam	ip6->ip6_hlim = IPV6_DEFHLIM;
759105197Ssam
760105197Ssam	/* XXX Should ip6_src be updated later ? */
761105197Ssam
762105197Ssam	return 0;
763105197Ssam}
764105197Ssam
765105197Ssam/*
766105197Ssam * IPsec output logic for IPv6, tunnel mode.
767105197Ssam */
768105197Ssamint
769105197Ssamipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
770105197Ssam{
771105197Ssam	struct ip6_hdr *ip6;
772105197Ssam	struct ipsecrequest *isr;
773105197Ssam	struct secasindex saidx;
774105197Ssam	int error;
775213836Sbz	struct sockaddr_in6 *dst6;
776105197Ssam	struct mbuf *m;
777105197Ssam
778120585Ssam	IPSEC_ASSERT(state != NULL, ("null state"));
779120585Ssam	IPSEC_ASSERT(state->m != NULL, ("null m"));
780120585Ssam	IPSEC_ASSERT(sp != NULL, ("null sp"));
781105197Ssam
782105197Ssam	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
783170122Sbz		printf("%s: applied SP\n", __func__);
784105197Ssam		kdebug_secpolicy(sp));
785105197Ssam
786105197Ssam	m = state->m;
787105197Ssam	/*
788105197Ssam	 * transport mode ipsec (before the 1st tunnel mode) is already
789105197Ssam	 * processed by ipsec6_output_trans().
790105197Ssam	 */
791105197Ssam	for (isr = sp->req; isr; isr = isr->next) {
792105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
793105197Ssam			break;
794105197Ssam	}
795170123Sbz
796170123Sbz	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
797105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
798177175Sbz	if (isr == NULL) {
799177175Sbz		if (error != 0)
800177175Sbz			goto bad;
801177175Sbz		return EJUSTRETURN;
802177175Sbz	}
803105197Ssam
804174054Sbz#ifdef DEV_ENC
805181627Svanhu	encif->if_opackets++;
806181627Svanhu	encif->if_obytes += m->m_pkthdr.len;
807181627Svanhu
808174054Sbz	/* pass the mbuf to enc0 for bpf processing */
809174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
810174054Sbz	/* pass the mbuf to enc0 for packet filtering */
811174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
812174054Sbz		goto bad;
813174054Sbz#endif
814174054Sbz
815105197Ssam	/*
816105197Ssam	 * There may be the case that SA status will be changed when
817105197Ssam	 * we are refering to one. So calling splsoftnet().
818105197Ssam	 */
819105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
820105197Ssam		/*
821105197Ssam		 * build IPsec tunnel.
822105197Ssam		 */
823105197Ssam		/* XXX should be processed with other familiy */
824105197Ssam		if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
825120585Ssam			ipseclog((LOG_ERR, "%s: family mismatched between "
826120585Ssam			    "inner and outer, spi=%u\n", __func__,
827105197Ssam			    ntohl(isr->sav->spi)));
828252692Sae			IPSEC6STAT_INC(ips_out_inval);
829105197Ssam			error = EAFNOSUPPORT;
830105197Ssam			goto bad;
831105197Ssam		}
832105197Ssam
833105197Ssam		m = ipsec6_splithdr(m);
834105197Ssam		if (!m) {
835252692Sae			IPSEC6STAT_INC(ips_out_nomem);
836105197Ssam			error = ENOMEM;
837105197Ssam			goto bad;
838105197Ssam		}
839105197Ssam		error = ipsec6_encapsulate(m, isr->sav);
840105197Ssam		if (error) {
841105197Ssam			m = NULL;
842105197Ssam			goto bad;
843105197Ssam		}
844105197Ssam		ip6 = mtod(m, struct ip6_hdr *);
845105197Ssam
846214250Sbz		state->ro =
847214250Sbz		    (struct route *)&isr->sav->sah->route_cache.sin6_route;
848105197Ssam		state->dst = (struct sockaddr *)&state->ro->ro_dst;
849105197Ssam		dst6 = (struct sockaddr_in6 *)state->dst;
850105197Ssam		if (state->ro->ro_rt
851105197Ssam		 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
852105197Ssam		  || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
853105197Ssam			RTFREE(state->ro->ro_rt);
854105197Ssam			state->ro->ro_rt = NULL;
855105197Ssam		}
856187936Sbz		if (state->ro->ro_rt == NULL) {
857105197Ssam			bzero(dst6, sizeof(*dst6));
858105197Ssam			dst6->sin6_family = AF_INET6;
859105197Ssam			dst6->sin6_len = sizeof(*dst6);
860105197Ssam			dst6->sin6_addr = ip6->ip6_dst;
861232292Sbz			rtalloc_ign_fib(state->ro, 0UL, M_GETFIB(m));
862105197Ssam		}
863187936Sbz		if (state->ro->ro_rt == NULL) {
864250044Sae			IP6STAT_INC(ip6s_noroute);
865252692Sae			IPSEC6STAT_INC(ips_out_noroute);
866105197Ssam			error = EHOSTUNREACH;
867105197Ssam			goto bad;
868105197Ssam		}
869105197Ssam
870105197Ssam		/* adjust state->dst if tunnel endpoint is offlink */
871213837Sbz		if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
872105197Ssam			state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
873105197Ssam	}
874105197Ssam
875105197Ssam	m = ipsec6_splithdr(m);
876105197Ssam	if (!m) {
877252692Sae		IPSEC6STAT_INC(ips_out_nomem);
878105197Ssam		error = ENOMEM;
879105197Ssam		goto bad;
880105197Ssam	}
881105197Ssam	ip6 = mtod(m, struct ip6_hdr *);
882174054Sbz
883174054Sbz#ifdef DEV_ENC
884174054Sbz	/* pass the mbuf to enc0 for bpf processing */
885174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
886174054Sbz	/* pass the mbuf to enc0 for packet filtering */
887174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
888174054Sbz		goto bad;
889174054Sbz#endif
890174054Sbz
891170123Sbz	error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
892105197Ssam		sizeof (struct ip6_hdr),
893105197Ssam		offsetof(struct ip6_hdr, ip6_nxt));
894170123Sbz	IPSECREQUEST_UNLOCK(isr);
895170123Sbz	return error;
896105197Ssambad:
897170123Sbz	if (isr)
898170123Sbz		IPSECREQUEST_UNLOCK(isr);
899105197Ssam	if (m)
900105197Ssam		m_freem(m);
901105197Ssam	state->m = NULL;
902105197Ssam	return error;
903105197Ssam}
904105197Ssam#endif /*INET6*/
905