ipsec_output.c revision 274467
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 274467 2014-11-13 12:58:33Z ae $
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>
47257176Sglebius#include <net/if_var.h>
48171497Sbz#include <net/pfil.h>
49105197Ssam#include <net/route.h>
50195699Srwatson#include <net/vnet.h>
51105197Ssam
52105197Ssam#include <netinet/in.h>
53105197Ssam#include <netinet/in_systm.h>
54105197Ssam#include <netinet/ip.h>
55105197Ssam#include <netinet/ip_var.h>
56105197Ssam#include <netinet/in_var.h>
57105197Ssam#include <netinet/ip_ecn.h>
58105197Ssam#ifdef INET6
59105197Ssam#include <netinet6/ip6_ecn.h>
60105197Ssam#endif
61105197Ssam
62105197Ssam#include <netinet/ip6.h>
63105197Ssam#ifdef INET6
64105197Ssam#include <netinet6/ip6_var.h>
65105197Ssam#endif
66105197Ssam#include <netinet/in_pcb.h>
67105197Ssam#ifdef INET6
68105197Ssam#include <netinet/icmp6.h>
69105197Ssam#endif
70105197Ssam
71105197Ssam#include <netipsec/ipsec.h>
72105197Ssam#ifdef INET6
73105197Ssam#include <netipsec/ipsec6.h>
74105197Ssam#endif
75105197Ssam#include <netipsec/ah_var.h>
76105197Ssam#include <netipsec/esp_var.h>
77105197Ssam#include <netipsec/ipcomp_var.h>
78105197Ssam
79105197Ssam#include <netipsec/xform.h>
80105197Ssam
81105197Ssam#include <netipsec/key.h>
82105197Ssam#include <netipsec/keydb.h>
83105197Ssam#include <netipsec/key_debug.h>
84105197Ssam
85105197Ssam#include <machine/in_cksum.h>
86105197Ssam
87194062Svanhu#ifdef IPSEC_NAT_T
88194062Svanhu#include <netinet/udp.h>
89194062Svanhu#endif
90194062Svanhu
91181627Svanhu#ifdef DEV_ENC
92181627Svanhu#include <net/if_enc.h>
93181627Svanhu#endif
94181627Svanhu
95181627Svanhu
96105197Ssamint
97105197Ssamipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
98105197Ssam{
99105197Ssam	struct tdb_ident *tdbi;
100105197Ssam	struct m_tag *mtag;
101105197Ssam	struct secasvar *sav;
102105197Ssam	struct secasindex *saidx;
103105197Ssam	int error;
104105197Ssam
105120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
106120585Ssam	IPSEC_ASSERT(isr != NULL, ("null ISR"));
107105197Ssam	sav = isr->sav;
108120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
109120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
110105197Ssam
111105197Ssam	saidx = &sav->sah->saidx;
112105197Ssam	switch (saidx->dst.sa.sa_family) {
113105197Ssam#ifdef INET
114105197Ssam	case AF_INET:
115105197Ssam		/* Fix the header length, for AH processing. */
116105197Ssam		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
117105197Ssam		break;
118105197Ssam#endif /* INET */
119105197Ssam#ifdef INET6
120105197Ssam	case AF_INET6:
121105197Ssam		/* Fix the header length, for AH processing. */
122105197Ssam		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
123105197Ssam			error = ENXIO;
124105197Ssam			goto bad;
125105197Ssam		}
126105197Ssam		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
127105197Ssam			/* No jumbogram support. */
128105197Ssam			error = ENXIO;	/*?*/
129105197Ssam			goto bad;
130105197Ssam		}
131105197Ssam		mtod(m, struct ip6_hdr *)->ip6_plen =
132105197Ssam			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
133105197Ssam		break;
134105197Ssam#endif /* INET6 */
135105197Ssam	default:
136120585Ssam		DPRINTF(("%s: unknown protocol family %u\n", __func__,
137105197Ssam		    saidx->dst.sa.sa_family));
138105197Ssam		error = ENXIO;
139105197Ssam		goto bad;
140105197Ssam	}
141105197Ssam
142105197Ssam	/*
143105197Ssam	 * Add a record of what we've done or what needs to be done to the
144105197Ssam	 * packet.
145105197Ssam	 */
146105197Ssam	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
147105197Ssam			sizeof(struct tdb_ident), M_NOWAIT);
148105197Ssam	if (mtag == NULL) {
149120585Ssam		DPRINTF(("%s: could not get packet tag\n", __func__));
150105197Ssam		error = ENOMEM;
151105197Ssam		goto bad;
152105197Ssam	}
153105197Ssam
154105197Ssam	tdbi = (struct tdb_ident *)(mtag + 1);
155105197Ssam	tdbi->dst = saidx->dst;
156105197Ssam	tdbi->proto = saidx->proto;
157105197Ssam	tdbi->spi = sav->spi;
158105197Ssam	m_tag_prepend(m, mtag);
159105197Ssam
160105197Ssam	/*
161105197Ssam	 * If there's another (bundled) SA to apply, do so.
162105197Ssam	 * Note that this puts a burden on the kernel stack size.
163105197Ssam	 * If this is a problem we'll need to introduce a queue
164105197Ssam	 * to set the packet on so we can unwind the stack before
165105197Ssam	 * doing further processing.
166105197Ssam	 */
167105197Ssam	if (isr->next) {
168238700Sbz		/* XXX-BZ currently only support same AF bundles. */
169221129Sbz		switch (saidx->dst.sa.sa_family) {
170221129Sbz#ifdef INET
171221129Sbz		case AF_INET:
172274467Sae			IPSECSTAT_INC(ips_out_bundlesa);
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 */
180274467Sae			IPSEC6STAT_INC(ips_out_bundlesa);
181266800Svanhu			return ipsec6_process_packet(m, isr->next);
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	case AF_INET:
202194062Svanhu#ifdef IPSEC_NAT_T
203194062Svanhu		/*
204194062Svanhu		 * If NAT-T is enabled, now that all IPsec processing is done
205194062Svanhu		 * insert UDP encapsulation header after IP header.
206194062Svanhu		 */
207194062Svanhu		if (sav->natt_type) {
208241919Sglebius			struct ip *ip = mtod(m, struct ip *);
209194062Svanhu			const int hlen = (ip->ip_hl << 2);
210194062Svanhu			int size, off;
211194062Svanhu			struct mbuf *mi;
212194062Svanhu			struct udphdr *udp;
213194062Svanhu
214194062Svanhu			size = sizeof(struct udphdr);
215194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
216194062Svanhu				/*
217194062Svanhu				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
218194062Svanhu				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
219194062Svanhu				 * ignoring possible AH mode
220194062Svanhu				 * non-IKE marker + non-ESP marker
221194062Svanhu				 * from draft-ietf-ipsec-udp-encaps-00.txt.
222194062Svanhu				 */
223194062Svanhu				size += sizeof(u_int64_t);
224194062Svanhu			}
225194062Svanhu			mi = m_makespace(m, hlen, size, &off);
226194062Svanhu			if (mi == NULL) {
227194062Svanhu				DPRINTF(("%s: m_makespace for udphdr failed\n",
228194062Svanhu				    __func__));
229194062Svanhu				error = ENOBUFS;
230194062Svanhu				goto bad;
231194062Svanhu			}
232194062Svanhu
233194062Svanhu			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
234194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
235194062Svanhu				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
236194062Svanhu			else
237194062Svanhu				udp->uh_sport =
238194062Svanhu					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
239194062Svanhu			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
240194062Svanhu			udp->uh_sum = 0;
241194062Svanhu			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
242241919Sglebius			ip->ip_len = htons(m->m_pkthdr.len);
243194062Svanhu			ip->ip_p = IPPROTO_UDP;
244194062Svanhu
245194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
246194062Svanhu				*(u_int64_t *)(udp + 1) = 0;
247194062Svanhu		}
248194062Svanhu#endif /* IPSEC_NAT_T */
249194062Svanhu
250105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
251105197Ssam#endif /* INET */
252105197Ssam#ifdef INET6
253105197Ssam	case AF_INET6:
254105197Ssam		/*
255105197Ssam		 * We don't need massage, IPv6 header fields are always in
256105197Ssam		 * net endian.
257105197Ssam		 */
258105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
259105197Ssam#endif /* INET6 */
260105197Ssam	}
261105197Ssam	panic("ipsec_process_done");
262105197Ssambad:
263105197Ssam	m_freem(m);
264105197Ssam	return (error);
265105197Ssam}
266105197Ssam
267105197Ssamstatic struct ipsecrequest *
268105197Ssamipsec_nextisr(
269105197Ssam	struct mbuf *m,
270105197Ssam	struct ipsecrequest *isr,
271105197Ssam	int af,
272105197Ssam	struct secasindex *saidx,
273105197Ssam	int *error
274105197Ssam)
275105197Ssam{
276252028Sae#define	IPSEC_OSTAT(name)	do {		\
277252028Sae	if (isr->saidx.proto == IPPROTO_ESP)	\
278252028Sae		ESPSTAT_INC(esps_##name);	\
279252028Sae	else if (isr->saidx.proto == IPPROTO_AH)\
280252028Sae		AHSTAT_INC(ahs_##name);		\
281252028Sae	else					\
282252028Sae		IPCOMPSTAT_INC(ipcomps_##name);	\
283252028Sae} while (0)
284105197Ssam	struct secasvar *sav;
285105197Ssam
286120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
287120585Ssam
288120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
289120585Ssam		("invalid address family %u", af));
290105197Ssamagain:
291105197Ssam	/*
292105197Ssam	 * Craft SA index to search for proper SA.  Note that
293105197Ssam	 * we only fillin unspecified SA peers for transport
294105197Ssam	 * mode; for tunnel mode they must already be filled in.
295105197Ssam	 */
296105197Ssam	*saidx = isr->saidx;
297105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
298105197Ssam		/* Fillin unspecified SA peers only for transport mode */
299105197Ssam		if (af == AF_INET) {
300105197Ssam			struct sockaddr_in *sin;
301105197Ssam			struct ip *ip = mtod(m, struct ip *);
302105197Ssam
303105197Ssam			if (saidx->src.sa.sa_len == 0) {
304105197Ssam				sin = &saidx->src.sin;
305105197Ssam				sin->sin_len = sizeof(*sin);
306105197Ssam				sin->sin_family = AF_INET;
307105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
308105197Ssam				sin->sin_addr = ip->ip_src;
309105197Ssam			}
310105197Ssam			if (saidx->dst.sa.sa_len == 0) {
311105197Ssam				sin = &saidx->dst.sin;
312105197Ssam				sin->sin_len = sizeof(*sin);
313105197Ssam				sin->sin_family = AF_INET;
314105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
315105197Ssam				sin->sin_addr = ip->ip_dst;
316105197Ssam			}
317105197Ssam		} else {
318105197Ssam			struct sockaddr_in6 *sin6;
319105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
320105197Ssam
321105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
322105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
323105197Ssam				sin6->sin6_len = sizeof(*sin6);
324105197Ssam				sin6->sin6_family = AF_INET6;
325105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
326105197Ssam				sin6->sin6_addr = ip6->ip6_src;
327105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
328105197Ssam					/* fix scope id for comparing SPD */
329105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
330105197Ssam					sin6->sin6_scope_id =
331105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
332105197Ssam				}
333105197Ssam			}
334105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
335105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
336105197Ssam				sin6->sin6_len = sizeof(*sin6);
337105197Ssam				sin6->sin6_family = AF_INET6;
338105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
339105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
340105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
341105197Ssam					/* fix scope id for comparing SPD */
342105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
343105197Ssam					sin6->sin6_scope_id =
344105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
345105197Ssam				}
346105197Ssam			}
347105197Ssam		}
348105197Ssam	}
349105197Ssam
350105197Ssam	/*
351105197Ssam	 * Lookup SA and validate it.
352105197Ssam	 */
353105197Ssam	*error = key_checkrequest(isr, saidx);
354105197Ssam	if (*error != 0) {
355105197Ssam		/*
356105197Ssam		 * IPsec processing is required, but no SA found.
357105197Ssam		 * I assume that key_acquire() had been called
358105197Ssam		 * to get/establish the SA. Here I discard
359105197Ssam		 * this packet because it is responsibility for
360105197Ssam		 * upper layer to retransmit the packet.
361105197Ssam		 */
362274434Sae		switch(af) {
363274434Sae		case AF_INET:
364274434Sae			IPSECSTAT_INC(ips_out_nosa);
365274434Sae			break;
366274434Sae#ifdef INET6
367274434Sae		case AF_INET6:
368274434Sae			IPSEC6STAT_INC(ips_out_nosa);
369274434Sae			break;
370274434Sae#endif
371274434Sae		}
372105197Ssam		goto bad;
373105197Ssam	}
374105197Ssam	sav = isr->sav;
375177175Sbz	if (sav == NULL) {
376120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
377120585Ssam			("no SA found, but required; level %u",
378105197Ssam			ipsec_get_reqlevel(isr)));
379120585Ssam		IPSECREQUEST_UNLOCK(isr);
380105197Ssam		isr = isr->next;
381177175Sbz		/*
382177175Sbz		 * If isr is NULL, we found a 'use' policy w/o SA.
383177175Sbz		 * Return w/o error and w/o isr so we can drop out
384177175Sbz		 * and continue w/o IPsec processing.
385177175Sbz		 */
386177175Sbz		if (isr == NULL)
387105197Ssam			return isr;
388120585Ssam		IPSECREQUEST_LOCK(isr);
389105197Ssam		goto again;
390105197Ssam	}
391105197Ssam
392105197Ssam	/*
393105197Ssam	 * Check system global policy controls.
394105197Ssam	 */
395181803Sbz	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
396181803Sbz	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
397181803Sbz	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
398120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
399120585Ssam			" to policy (check your sysctls)\n", __func__));
400252028Sae		IPSEC_OSTAT(pdrops);
401105197Ssam		*error = EHOSTUNREACH;
402105197Ssam		goto bad;
403105197Ssam	}
404105197Ssam
405105197Ssam	/*
406105197Ssam	 * Sanity check the SA contents for the caller
407105197Ssam	 * before they invoke the xform output method.
408105197Ssam	 */
409105197Ssam	if (sav->tdb_xform == NULL) {
410120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
411252028Sae		IPSEC_OSTAT(noxform);
412105197Ssam		*error = EHOSTUNREACH;
413105197Ssam		goto bad;
414105197Ssam	}
415105197Ssam	return isr;
416105197Ssambad:
417120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
418120585Ssam	IPSECREQUEST_UNLOCK(isr);
419105197Ssam	return NULL;
420105197Ssam#undef IPSEC_OSTAT
421105197Ssam}
422105197Ssam
423105197Ssam#ifdef INET
424105197Ssam/*
425105197Ssam * IPsec output logic for IPv4.
426105197Ssam */
427105197Ssamint
428105197Ssamipsec4_process_packet(
429105197Ssam	struct mbuf *m,
430105197Ssam	struct ipsecrequest *isr,
431105197Ssam	int flags,
432105197Ssam	int tunalready)
433105197Ssam{
434105197Ssam	struct secasindex saidx;
435105197Ssam	struct secasvar *sav;
436105197Ssam	struct ip *ip;
437119643Ssam	int error, i, off;
438105197Ssam
439120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
440120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
441105197Ssam
442120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
443105197Ssam
444105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
445177175Sbz	if (isr == NULL) {
446177175Sbz		if (error != 0)
447177175Sbz			goto bad;
448177175Sbz		return EJUSTRETURN;
449177175Sbz	}
450105197Ssam
451105197Ssam	sav = isr->sav;
452159965Sthompsa
453159965Sthompsa#ifdef DEV_ENC
454271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
455271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
456181627Svanhu
457174054Sbz	/* pass the mbuf to enc0 for bpf processing */
458174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
459159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
460174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
461159965Sthompsa		goto bad;
462159965Sthompsa#endif
463159965Sthompsa
464105197Ssam	if (!tunalready) {
465105197Ssam		union sockaddr_union *dst = &sav->sah->saidx.dst;
466105197Ssam		int setdf;
467105197Ssam
468105197Ssam		/*
469105197Ssam		 * Collect IP_DF state from the outer header.
470105197Ssam		 */
471105197Ssam		if (dst->sa.sa_family == AF_INET) {
472105197Ssam			if (m->m_len < sizeof (struct ip) &&
473105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
474105197Ssam				error = ENOBUFS;
475105197Ssam				goto bad;
476105197Ssam			}
477105197Ssam			ip = mtod(m, struct ip *);
478105197Ssam			/* Honor system-wide control of how to handle IP_DF */
479181803Sbz			switch (V_ip4_ipsec_dfbit) {
480105197Ssam			case 0:			/* clear in outer header */
481105197Ssam			case 1:			/* set in outer header */
482181803Sbz				setdf = V_ip4_ipsec_dfbit;
483105197Ssam				break;
484105197Ssam			default:		/* propagate to outer header */
485105197Ssam				setdf = ntohs(ip->ip_off & IP_DF);
486105197Ssam				break;
487105197Ssam			}
488105197Ssam		} else {
489105197Ssam			ip = NULL;		/* keep compiler happy */
490105197Ssam			setdf = 0;
491105197Ssam		}
492105197Ssam		/* Do the appropriate encapsulation, if necessary */
493105197Ssam		if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
494105197Ssam		    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
495105197Ssam#if 0
496105197Ssam		    (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
497105197Ssam		    sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
498105197Ssam#endif
499105197Ssam		    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
500105197Ssam		     dst->sin.sin_addr.s_addr != INADDR_ANY &&
501105197Ssam		     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
502105197Ssam			struct mbuf *mp;
503105197Ssam
504105197Ssam			/* Fix IPv4 header checksum and length */
505105197Ssam			if (m->m_len < sizeof (struct ip) &&
506105197Ssam			    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
507105197Ssam				error = ENOBUFS;
508105197Ssam				goto bad;
509105197Ssam			}
510105197Ssam			ip = mtod(m, struct ip *);
511268083Szec			if (ip->ip_v == IPVERSION) {
512268083Szec				ip->ip_len = htons(m->m_pkthdr.len);
513268083Szec				ip->ip_sum = 0;
514268083Szec				ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
515268083Szec			}
516105197Ssam
517105197Ssam			/* Encapsulate the packet */
518105197Ssam			error = ipip_output(m, isr, &mp, 0, 0);
519105197Ssam			if (mp == NULL && !error) {
520105197Ssam				/* Should never happen. */
521120585Ssam				DPRINTF(("%s: ipip_output returns no mbuf and "
522120585Ssam					"no error!", __func__));
523105197Ssam				error = EFAULT;
524105197Ssam			}
525105197Ssam			if (error) {
526124765Ssam				if (mp) {
527124765Ssam					/* XXX: Should never happen! */
528105197Ssam					m_freem(mp);
529124765Ssam				}
530124765Ssam				m = NULL; /* ipip_output() already freed it */
531105197Ssam				goto bad;
532105197Ssam			}
533105197Ssam			m = mp, mp = NULL;
534105197Ssam			/*
535105197Ssam			 * ipip_output clears IP_DF in the new header.  If
536105197Ssam			 * we need to propagate IP_DF from the outer header,
537105197Ssam			 * then we have to do it here.
538105197Ssam			 *
539105197Ssam			 * XXX shouldn't assume what ipip_output does.
540105197Ssam			 */
541105197Ssam			if (dst->sa.sa_family == AF_INET && setdf) {
542105197Ssam				if (m->m_len < sizeof (struct ip) &&
543105197Ssam				    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
544105197Ssam					error = ENOBUFS;
545105197Ssam					goto bad;
546105197Ssam				}
547105197Ssam				ip = mtod(m, struct ip *);
548105197Ssam				ip->ip_off = ntohs(ip->ip_off);
549105197Ssam				ip->ip_off |= IP_DF;
550105197Ssam				ip->ip_off = htons(ip->ip_off);
551105197Ssam			}
552105197Ssam		}
553105197Ssam	}
554105197Ssam
555159965Sthompsa#ifdef DEV_ENC
556159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
557266800Svanhu	ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
558174054Sbz	/* pass the mbuf to enc0 for packet filtering */
559174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
560174054Sbz		goto bad;
561159965Sthompsa#endif
562159965Sthompsa
563105197Ssam	/*
564105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
565105197Ssam	 * packet will be returned for transmission after crypto
566105197Ssam	 * processing, etc. are completed.  For encapsulation we
567105197Ssam	 * bypass this call because of the explicit call done above
568105197Ssam	 * (necessary to deal with IP_DF handling for IPv4).
569105197Ssam	 *
570105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
571105197Ssam	 *     for reclaiming their resources.
572105197Ssam	 */
573105197Ssam	if (sav->tdb_xform->xf_type != XF_IP4) {
574266800Svanhu		union sockaddr_union *dst = &sav->sah->saidx.dst;
575266800Svanhu		switch(dst->sa.sa_family) {
576266800Svanhu		case AF_INET:
577266800Svanhu			ip = mtod(m, struct ip *);
578266800Svanhu			i = ip->ip_hl << 2;
579266800Svanhu			off = offsetof(struct ip, ip_p);
580266800Svanhu			break;
581266800Svanhu#ifdef INET6
582266800Svanhu		case AF_INET6:
583266800Svanhu			i = sizeof(struct ip6_hdr);
584266800Svanhu			off = offsetof(struct ip6_hdr, ip6_nxt);
585266800Svanhu			break;
586266800Svanhu#endif /* INET6 */
587266800Svanhu		default:
588266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
589266800Svanhu				 __func__, dst->sa.sa_family));
590266800Svanhu			error = EPFNOSUPPORT;
591266822Sbz			IPSECSTAT_INC(ips_out_inval);
592266800Svanhu			goto bad;
593266800Svanhu		}
594105197Ssam		error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
595105197Ssam	} else {
596105197Ssam		error = ipsec_process_done(m, isr);
597105197Ssam	}
598120585Ssam	IPSECREQUEST_UNLOCK(isr);
599105197Ssam	return error;
600105197Ssambad:
601120585Ssam	if (isr)
602120585Ssam		IPSECREQUEST_UNLOCK(isr);
603105197Ssam	if (m)
604105197Ssam		m_freem(m);
605105197Ssam	return error;
606105197Ssam}
607105197Ssam#endif
608105197Ssam
609266800Svanhu
610105197Ssam#ifdef INET6
611105197Ssamstatic int
612266800Svanhuin6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
613105197Ssam{
614266800Svanhu	struct in6_addr ia2;
615105197Ssam
616266800Svanhu	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
617266800Svanhu	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
618266800Svanhu		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
619105197Ssam
620266800Svanhu	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
621105197Ssam}
622105197Ssam
623105197Ssam/*
624266800Svanhu * IPsec output logic for IPv6.
625105197Ssam */
626105197Ssamint
627266800Svanhuipsec6_process_packet(
628266800Svanhu	struct mbuf *m,
629266800Svanhu 	struct ipsecrequest *isr
630266800Svanhu    )
631105197Ssam{
632266800Svanhu	struct secasindex saidx;
633266800Svanhu	struct secasvar *sav;
634105197Ssam	struct ip6_hdr *ip6;
635266800Svanhu	int error, i, off;
636266800Svanhu	union sockaddr_union *dst;
637105197Ssam
638266800Svanhu	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
639266800Svanhu	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
640105197Ssam
641266800Svanhu	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
642105197Ssam
643105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
644177175Sbz	if (isr == NULL) {
645177175Sbz		if (error != 0)
646177175Sbz			goto bad;
647266800Svanhu		return EJUSTRETURN;
648177175Sbz	}
649105197Ssam
650266800Svanhu	sav = isr->sav;
651266800Svanhu	dst = &sav->sah->saidx.dst;
652266800Svanhu
653274454Sae	ip6 = mtod(m, struct ip6_hdr *);
654274454Sae	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
655174054Sbz#ifdef DEV_ENC
656271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
657271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
658181627Svanhu
659174054Sbz	/* pass the mbuf to enc0 for bpf processing */
660174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
661174054Sbz	/* pass the mbuf to enc0 for packet filtering */
662174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
663174054Sbz		goto bad;
664266800Svanhu#endif /* DEV_ENC */
665174054Sbz
666266800Svanhu	/* Do the appropriate encapsulation, if necessary */
667266800Svanhu	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
668266800Svanhu	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
669266800Svanhu	    ((dst->sa.sa_family == AF_INET6) &&
670266800Svanhu	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
671266800Svanhu	     (!in6_sa_equal_addrwithscope(&dst->sin6,
672266800Svanhu				  &ip6->ip6_dst)))) {
673266800Svanhu		struct mbuf *mp;
674266800Svanhu
675266800Svanhu		/* Fix IPv6 header payload length. */
676266800Svanhu		if (m->m_len < sizeof(struct ip6_hdr))
677266800Svanhu			if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
678266800Svanhu				error = ENOBUFS;
679266800Svanhu				goto bad;
680266800Svanhu			}
681266800Svanhu
682266800Svanhu		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
683266800Svanhu			/* No jumbogram support. */
684266800Svanhu			error = ENXIO;   /*XXX*/
685105197Ssam			goto bad;
686105197Ssam		}
687105197Ssam
688266800Svanhu		/* Encapsulate the packet */
689266800Svanhu		error = ipip_output(m, isr, &mp, 0, 0);
690266800Svanhu		if (mp == NULL && !error) {
691266800Svanhu			/* Should never happen. */
692266800Svanhu			DPRINTF(("ipsec6_process_packet: ipip_output "
693266800Svanhu				 "returns no mbuf and no error!"));
694266800Svanhu			error = EFAULT;
695105197Ssam			goto bad;
696105197Ssam		}
697266800Svanhu
698105197Ssam		if (error) {
699266800Svanhu			if (mp) {
700266800Svanhu				/* XXX: Should never happen! */
701266800Svanhu				m_freem(mp);
702266800Svanhu			}
703266800Svanhu			m = NULL; /* ipip_output() already freed it */
704105197Ssam			goto bad;
705105197Ssam		}
706105197Ssam
707266800Svanhu		m = mp;
708266800Svanhu		mp = NULL;
709105197Ssam	}
710105197Ssam
711174054Sbz#ifdef DEV_ENC
712266800Svanhu	ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
713174054Sbz	/* pass the mbuf to enc0 for packet filtering */
714174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
715174054Sbz		goto bad;
716266800Svanhu#endif /* DEV_ENC */
717174054Sbz
718266800Svanhu	switch(dst->sa.sa_family) {
719266800Svanhu#ifdef INET
720266800Svanhu	case AF_INET:
721266800Svanhu		{
722266800Svanhu		struct ip *ip;
723266800Svanhu		ip = mtod(m, struct ip *);
724266800Svanhu		i = ip->ip_hl << 2;
725266800Svanhu		off = offsetof(struct ip, ip_p);
726266800Svanhu		}
727266800Svanhu		break;
728266800Svanhu#endif /* AF_INET */
729266800Svanhu	case AF_INET6:
730266800Svanhu		i = sizeof(struct ip6_hdr);
731266800Svanhu		off = offsetof(struct ip6_hdr, ip6_nxt);
732266800Svanhu		break;
733266800Svanhu	default:
734266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
735266800Svanhu				 __func__, dst->sa.sa_family));
736266800Svanhu		error = EPFNOSUPPORT;
737266800Svanhu		IPSEC6STAT_INC(ips_out_inval);
738266800Svanhu		goto bad;
739266800Svanhu	}
740266800Svanhu	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
741170123Sbz	IPSECREQUEST_UNLOCK(isr);
742170123Sbz	return error;
743105197Ssambad:
744266800Svanhu
745170123Sbz	if (isr)
746170123Sbz		IPSECREQUEST_UNLOCK(isr);
747105197Ssam	if (m)
748105197Ssam		m_freem(m);
749105197Ssam	return error;
750105197Ssam}
751266822Sbz#endif /*INET6*/
752