ipsec_output.c revision 282139
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 282139 2015-04-28 11:03:47Z 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>
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>
64281692Sae#include <netinet6/scope6_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"));
107282046Sae	IPSEC_ASSERT(isr->sp != NULL, ("NULL isr->sp"));
108105197Ssam	sav = isr->sav;
109120585Ssam	IPSEC_ASSERT(sav != NULL, ("null SA"));
110120585Ssam	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
111105197Ssam
112105197Ssam	saidx = &sav->sah->saidx;
113105197Ssam	switch (saidx->dst.sa.sa_family) {
114105197Ssam#ifdef INET
115105197Ssam	case AF_INET:
116105197Ssam		/* Fix the header length, for AH processing. */
117105197Ssam		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
118105197Ssam		break;
119105197Ssam#endif /* INET */
120105197Ssam#ifdef INET6
121105197Ssam	case AF_INET6:
122105197Ssam		/* Fix the header length, for AH processing. */
123105197Ssam		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
124105197Ssam			error = ENXIO;
125105197Ssam			goto bad;
126105197Ssam		}
127105197Ssam		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
128105197Ssam			/* No jumbogram support. */
129105197Ssam			error = ENXIO;	/*?*/
130105197Ssam			goto bad;
131105197Ssam		}
132105197Ssam		mtod(m, struct ip6_hdr *)->ip6_plen =
133105197Ssam			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
134105197Ssam		break;
135105197Ssam#endif /* INET6 */
136105197Ssam	default:
137120585Ssam		DPRINTF(("%s: unknown protocol family %u\n", __func__,
138105197Ssam		    saidx->dst.sa.sa_family));
139105197Ssam		error = ENXIO;
140105197Ssam		goto bad;
141105197Ssam	}
142105197Ssam
143105197Ssam	/*
144105197Ssam	 * Add a record of what we've done or what needs to be done to the
145105197Ssam	 * packet.
146105197Ssam	 */
147105197Ssam	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
148105197Ssam			sizeof(struct tdb_ident), M_NOWAIT);
149105197Ssam	if (mtag == NULL) {
150120585Ssam		DPRINTF(("%s: could not get packet tag\n", __func__));
151105197Ssam		error = ENOMEM;
152105197Ssam		goto bad;
153105197Ssam	}
154105197Ssam
155105197Ssam	tdbi = (struct tdb_ident *)(mtag + 1);
156105197Ssam	tdbi->dst = saidx->dst;
157105197Ssam	tdbi->proto = saidx->proto;
158105197Ssam	tdbi->spi = sav->spi;
159105197Ssam	m_tag_prepend(m, mtag);
160105197Ssam
161105197Ssam	/*
162105197Ssam	 * If there's another (bundled) SA to apply, do so.
163105197Ssam	 * Note that this puts a burden on the kernel stack size.
164105197Ssam	 * If this is a problem we'll need to introduce a queue
165105197Ssam	 * to set the packet on so we can unwind the stack before
166105197Ssam	 * doing further processing.
167282046Sae	 *
168282046Sae	 * If ipsec[46]_process_packet() will successfully queue
169282046Sae	 * the request, we need to take additional reference to SP,
170282046Sae	 * because xform callback will release reference.
171105197Ssam	 */
172105197Ssam	if (isr->next) {
173238700Sbz		/* XXX-BZ currently only support same AF bundles. */
174221129Sbz		switch (saidx->dst.sa.sa_family) {
175221129Sbz#ifdef INET
176221129Sbz		case AF_INET:
177274467Sae			IPSECSTAT_INC(ips_out_bundlesa);
178282046Sae			key_addref(isr->sp);
179282046Sae			error = ipsec4_process_packet(m, isr->next);
180282046Sae			if (error != 0)
181282046Sae				KEY_FREESP(&isr->sp);
182282046Sae			return (error);
183221129Sbz			/* NOTREACHED */
184221129Sbz#endif
185221129Sbz#ifdef notyet
186221129Sbz#ifdef INET6
187221129Sbz		case AF_INET6:
188221129Sbz			/* XXX */
189274467Sae			IPSEC6STAT_INC(ips_out_bundlesa);
190282046Sae			key_addref(isr->sp);
191282046Sae			error = ipsec6_process_packet(m, isr->next);
192282046Sae			if (error != 0)
193282046Sae				KEY_FREESP(&isr->sp);
194282046Sae			return (error);
195221129Sbz			/* NOTREACHED */
196221129Sbz#endif /* INET6 */
197221129Sbz#endif
198221129Sbz		default:
199221129Sbz			DPRINTF(("%s: unknown protocol family %u\n", __func__,
200221129Sbz			    saidx->dst.sa.sa_family));
201221129Sbz			error = ENXIO;
202221129Sbz			goto bad;
203221129Sbz		}
204105197Ssam	}
205117056Ssam	key_sa_recordxfer(sav, m);		/* record data transfer */
206105197Ssam
207105197Ssam	/*
208105197Ssam	 * We're done with IPsec processing, transmit the packet using the
209282139Sae	 * appropriate network protocol (IP or IPv6).
210105197Ssam	 */
211105197Ssam	switch (saidx->dst.sa.sa_family) {
212105197Ssam#ifdef INET
213105197Ssam	case AF_INET:
214194062Svanhu#ifdef IPSEC_NAT_T
215194062Svanhu		/*
216194062Svanhu		 * If NAT-T is enabled, now that all IPsec processing is done
217194062Svanhu		 * insert UDP encapsulation header after IP header.
218194062Svanhu		 */
219194062Svanhu		if (sav->natt_type) {
220241919Sglebius			struct ip *ip = mtod(m, struct ip *);
221194062Svanhu			const int hlen = (ip->ip_hl << 2);
222194062Svanhu			int size, off;
223194062Svanhu			struct mbuf *mi;
224194062Svanhu			struct udphdr *udp;
225194062Svanhu
226194062Svanhu			size = sizeof(struct udphdr);
227194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
228194062Svanhu				/*
229194062Svanhu				 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
230194062Svanhu				 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
231194062Svanhu				 * ignoring possible AH mode
232194062Svanhu				 * non-IKE marker + non-ESP marker
233194062Svanhu				 * from draft-ietf-ipsec-udp-encaps-00.txt.
234194062Svanhu				 */
235194062Svanhu				size += sizeof(u_int64_t);
236194062Svanhu			}
237194062Svanhu			mi = m_makespace(m, hlen, size, &off);
238194062Svanhu			if (mi == NULL) {
239194062Svanhu				DPRINTF(("%s: m_makespace for udphdr failed\n",
240194062Svanhu				    __func__));
241194062Svanhu				error = ENOBUFS;
242194062Svanhu				goto bad;
243194062Svanhu			}
244194062Svanhu
245194062Svanhu			udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
246194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
247194062Svanhu				udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
248194062Svanhu			else
249194062Svanhu				udp->uh_sport =
250194062Svanhu					KEY_PORTFROMSADDR(&sav->sah->saidx.src);
251194062Svanhu			udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
252194062Svanhu			udp->uh_sum = 0;
253194062Svanhu			udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
254241919Sglebius			ip->ip_len = htons(m->m_pkthdr.len);
255194062Svanhu			ip->ip_p = IPPROTO_UDP;
256194062Svanhu
257194062Svanhu			if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
258194062Svanhu				*(u_int64_t *)(udp + 1) = 0;
259194062Svanhu		}
260194062Svanhu#endif /* IPSEC_NAT_T */
261194062Svanhu
262105197Ssam		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
263105197Ssam#endif /* INET */
264105197Ssam#ifdef INET6
265105197Ssam	case AF_INET6:
266105197Ssam		/*
267105197Ssam		 * We don't need massage, IPv6 header fields are always in
268105197Ssam		 * net endian.
269105197Ssam		 */
270105197Ssam		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
271105197Ssam#endif /* INET6 */
272105197Ssam	}
273105197Ssam	panic("ipsec_process_done");
274105197Ssambad:
275105197Ssam	m_freem(m);
276105197Ssam	return (error);
277105197Ssam}
278105197Ssam
279105197Ssamstatic struct ipsecrequest *
280105197Ssamipsec_nextisr(
281105197Ssam	struct mbuf *m,
282105197Ssam	struct ipsecrequest *isr,
283105197Ssam	int af,
284105197Ssam	struct secasindex *saidx,
285105197Ssam	int *error
286105197Ssam)
287105197Ssam{
288252028Sae#define	IPSEC_OSTAT(name)	do {		\
289252028Sae	if (isr->saidx.proto == IPPROTO_ESP)	\
290252028Sae		ESPSTAT_INC(esps_##name);	\
291252028Sae	else if (isr->saidx.proto == IPPROTO_AH)\
292252028Sae		AHSTAT_INC(ahs_##name);		\
293252028Sae	else					\
294252028Sae		IPCOMPSTAT_INC(ipcomps_##name);	\
295252028Sae} while (0)
296105197Ssam	struct secasvar *sav;
297105197Ssam
298120585Ssam	IPSECREQUEST_LOCK_ASSERT(isr);
299120585Ssam
300120585Ssam	IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
301120585Ssam		("invalid address family %u", af));
302105197Ssamagain:
303105197Ssam	/*
304105197Ssam	 * Craft SA index to search for proper SA.  Note that
305105197Ssam	 * we only fillin unspecified SA peers for transport
306105197Ssam	 * mode; for tunnel mode they must already be filled in.
307105197Ssam	 */
308105197Ssam	*saidx = isr->saidx;
309105197Ssam	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
310105197Ssam		/* Fillin unspecified SA peers only for transport mode */
311105197Ssam		if (af == AF_INET) {
312105197Ssam			struct sockaddr_in *sin;
313105197Ssam			struct ip *ip = mtod(m, struct ip *);
314105197Ssam
315105197Ssam			if (saidx->src.sa.sa_len == 0) {
316105197Ssam				sin = &saidx->src.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_src;
321105197Ssam			}
322105197Ssam			if (saidx->dst.sa.sa_len == 0) {
323105197Ssam				sin = &saidx->dst.sin;
324105197Ssam				sin->sin_len = sizeof(*sin);
325105197Ssam				sin->sin_family = AF_INET;
326105197Ssam				sin->sin_port = IPSEC_PORT_ANY;
327105197Ssam				sin->sin_addr = ip->ip_dst;
328105197Ssam			}
329105197Ssam		} else {
330105197Ssam			struct sockaddr_in6 *sin6;
331105197Ssam			struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
332105197Ssam
333105197Ssam			if (saidx->src.sin6.sin6_len == 0) {
334105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->src;
335105197Ssam				sin6->sin6_len = sizeof(*sin6);
336105197Ssam				sin6->sin6_family = AF_INET6;
337105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
338105197Ssam				sin6->sin6_addr = ip6->ip6_src;
339105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
340105197Ssam					/* fix scope id for comparing SPD */
341105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
342105197Ssam					sin6->sin6_scope_id =
343105197Ssam					    ntohs(ip6->ip6_src.s6_addr16[1]);
344105197Ssam				}
345105197Ssam			}
346105197Ssam			if (saidx->dst.sin6.sin6_len == 0) {
347105197Ssam				sin6 = (struct sockaddr_in6 *)&saidx->dst;
348105197Ssam				sin6->sin6_len = sizeof(*sin6);
349105197Ssam				sin6->sin6_family = AF_INET6;
350105197Ssam				sin6->sin6_port = IPSEC_PORT_ANY;
351105197Ssam				sin6->sin6_addr = ip6->ip6_dst;
352105197Ssam				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
353105197Ssam					/* fix scope id for comparing SPD */
354105197Ssam					sin6->sin6_addr.s6_addr16[1] = 0;
355105197Ssam					sin6->sin6_scope_id =
356105197Ssam					    ntohs(ip6->ip6_dst.s6_addr16[1]);
357105197Ssam				}
358105197Ssam			}
359105197Ssam		}
360105197Ssam	}
361105197Ssam
362105197Ssam	/*
363105197Ssam	 * Lookup SA and validate it.
364105197Ssam	 */
365105197Ssam	*error = key_checkrequest(isr, saidx);
366105197Ssam	if (*error != 0) {
367105197Ssam		/*
368105197Ssam		 * IPsec processing is required, but no SA found.
369105197Ssam		 * I assume that key_acquire() had been called
370105197Ssam		 * to get/establish the SA. Here I discard
371105197Ssam		 * this packet because it is responsibility for
372105197Ssam		 * upper layer to retransmit the packet.
373105197Ssam		 */
374274434Sae		switch(af) {
375274434Sae		case AF_INET:
376274434Sae			IPSECSTAT_INC(ips_out_nosa);
377274434Sae			break;
378274434Sae#ifdef INET6
379274434Sae		case AF_INET6:
380274434Sae			IPSEC6STAT_INC(ips_out_nosa);
381274434Sae			break;
382274434Sae#endif
383274434Sae		}
384105197Ssam		goto bad;
385105197Ssam	}
386105197Ssam	sav = isr->sav;
387177175Sbz	if (sav == NULL) {
388120585Ssam		IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
389120585Ssam			("no SA found, but required; level %u",
390105197Ssam			ipsec_get_reqlevel(isr)));
391120585Ssam		IPSECREQUEST_UNLOCK(isr);
392105197Ssam		isr = isr->next;
393177175Sbz		/*
394177175Sbz		 * If isr is NULL, we found a 'use' policy w/o SA.
395177175Sbz		 * Return w/o error and w/o isr so we can drop out
396177175Sbz		 * and continue w/o IPsec processing.
397177175Sbz		 */
398177175Sbz		if (isr == NULL)
399105197Ssam			return isr;
400120585Ssam		IPSECREQUEST_LOCK(isr);
401105197Ssam		goto again;
402105197Ssam	}
403105197Ssam
404105197Ssam	/*
405105197Ssam	 * Check system global policy controls.
406105197Ssam	 */
407181803Sbz	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
408181803Sbz	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
409181803Sbz	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
410120585Ssam		DPRINTF(("%s: IPsec outbound packet dropped due"
411120585Ssam			" to policy (check your sysctls)\n", __func__));
412252028Sae		IPSEC_OSTAT(pdrops);
413105197Ssam		*error = EHOSTUNREACH;
414105197Ssam		goto bad;
415105197Ssam	}
416105197Ssam
417105197Ssam	/*
418105197Ssam	 * Sanity check the SA contents for the caller
419105197Ssam	 * before they invoke the xform output method.
420105197Ssam	 */
421105197Ssam	if (sav->tdb_xform == NULL) {
422120585Ssam		DPRINTF(("%s: no transform for SA\n", __func__));
423252028Sae		IPSEC_OSTAT(noxform);
424105197Ssam		*error = EHOSTUNREACH;
425105197Ssam		goto bad;
426105197Ssam	}
427105197Ssam	return isr;
428105197Ssambad:
429120585Ssam	IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
430120585Ssam	IPSECREQUEST_UNLOCK(isr);
431105197Ssam	return NULL;
432105197Ssam#undef IPSEC_OSTAT
433105197Ssam}
434105197Ssam
435281692Saestatic int
436281692Saeipsec_encap(struct mbuf **mp, struct secasindex *saidx)
437281692Sae{
438281692Sae#ifdef INET6
439281692Sae	struct ip6_hdr *ip6;
440281692Sae#endif
441281692Sae	struct ip *ip;
442281692Sae	int setdf;
443281692Sae	uint8_t itos, proto;
444281692Sae
445281692Sae	ip = mtod(*mp, struct ip *);
446281692Sae	switch (ip->ip_v) {
447105197Ssam#ifdef INET
448281692Sae	case IPVERSION:
449281692Sae		proto = IPPROTO_IPIP;
450281692Sae		/*
451281692Sae		 * Collect IP_DF state from the inner header
452281692Sae		 * and honor system-wide control of how to handle it.
453281692Sae		 */
454281692Sae		switch (V_ip4_ipsec_dfbit) {
455281692Sae		case 0:	/* clear in outer header */
456281692Sae		case 1:	/* set in outer header */
457281692Sae			setdf = V_ip4_ipsec_dfbit;
458281692Sae			break;
459281692Sae		default:/* propagate to outer header */
460281692Sae			setdf = (ip->ip_off & ntohs(IP_DF)) != 0;
461281692Sae		}
462281692Sae		itos = ip->ip_tos;
463281692Sae		break;
464281692Sae#endif
465281692Sae#ifdef INET6
466281692Sae	case (IPV6_VERSION >> 4):
467281692Sae		proto = IPPROTO_IPV6;
468281692Sae		ip6 = mtod(*mp, struct ip6_hdr *);
469281692Sae		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
470281692Sae		setdf = V_ip4_ipsec_dfbit ? 1: 0;
471281692Sae		/* scoped address handling */
472281692Sae		in6_clearscope(&ip6->ip6_src);
473281692Sae		in6_clearscope(&ip6->ip6_dst);
474281692Sae		break;
475281692Sae#endif
476281692Sae	default:
477281692Sae		return (EAFNOSUPPORT);
478281692Sae	}
479281692Sae	switch (saidx->dst.sa.sa_family) {
480281692Sae#ifdef INET
481281692Sae	case AF_INET:
482281692Sae		if (saidx->src.sa.sa_family != AF_INET ||
483281692Sae		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
484281692Sae		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
485281692Sae			return (EINVAL);
486281692Sae		M_PREPEND(*mp, sizeof(struct ip), M_NOWAIT);
487281692Sae		if (*mp == NULL)
488281692Sae			return (ENOBUFS);
489281692Sae		ip = mtod(*mp, struct ip *);
490281692Sae		ip->ip_v = IPVERSION;
491281692Sae		ip->ip_hl = sizeof(struct ip) >> 2;
492281692Sae		ip->ip_p = proto;
493281692Sae		ip->ip_len = htons((*mp)->m_pkthdr.len);
494281692Sae		ip->ip_ttl = V_ip_defttl;
495281692Sae		ip->ip_sum = 0;
496281692Sae		ip->ip_off = setdf ? htons(IP_DF): 0;
497281692Sae		ip->ip_src = saidx->src.sin.sin_addr;
498281692Sae		ip->ip_dst = saidx->dst.sin.sin_addr;
499281692Sae		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
500281692Sae		ip_fillid(ip);
501281692Sae		break;
502281692Sae#endif /* INET */
503281692Sae#ifdef INET6
504281692Sae	case AF_INET6:
505281692Sae		if (saidx->src.sa.sa_family != AF_INET6 ||
506281692Sae		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
507281692Sae		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
508281692Sae			return (EINVAL);
509281692Sae		M_PREPEND(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
510281692Sae		if (*mp == NULL)
511281692Sae			return (ENOBUFS);
512281692Sae		ip6 = mtod(*mp, struct ip6_hdr *);
513281692Sae		ip6->ip6_flow = 0;
514281692Sae		ip6->ip6_vfc = IPV6_VERSION;
515281692Sae		ip6->ip6_hlim = V_ip6_defhlim;
516281692Sae		ip6->ip6_nxt = proto;
517281692Sae		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
518281693Sae		/* For link-local address embed scope zone id */
519281693Sae		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
520281693Sae			ip6->ip6_dst.s6_addr16[1] =
521281693Sae			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
522281692Sae		ip6->ip6_src = saidx->src.sin6.sin6_addr;
523281693Sae		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
524281693Sae			ip6->ip6_src.s6_addr16[1] =
525281693Sae			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
526281692Sae		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
527281692Sae		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
528281692Sae		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
529281692Sae		break;
530281692Sae#endif /* INET6 */
531281692Sae	default:
532281692Sae		return (EAFNOSUPPORT);
533281692Sae	}
534281692Sae	return (0);
535281692Sae}
536281692Sae
537281692Sae#ifdef INET
538105197Ssam/*
539105197Ssam * IPsec output logic for IPv4.
540105197Ssam */
541105197Ssamint
542275708Saeipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
543105197Ssam{
544281695Sae	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
545275708Sae	union sockaddr_union *dst;
546105197Ssam	struct secasindex saidx;
547105197Ssam	struct secasvar *sav;
548105197Ssam	struct ip *ip;
549281692Sae	int error, i, off;
550105197Ssam
551120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
552120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
553105197Ssam
554120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
555105197Ssam
556105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
557177175Sbz	if (isr == NULL) {
558177175Sbz		if (error != 0)
559177175Sbz			goto bad;
560177175Sbz		return EJUSTRETURN;
561177175Sbz	}
562105197Ssam
563105197Ssam	sav = isr->sav;
564275708Sae	if (m->m_len < sizeof(struct ip) &&
565275708Sae	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
566275708Sae		error = ENOBUFS;
567275708Sae		goto bad;
568275708Sae	}
569275708Sae	ip = mtod(m, struct ip *);
570275708Sae	dst = &sav->sah->saidx.dst;
571159965Sthompsa#ifdef DEV_ENC
572271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
573271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
574181627Svanhu
575174054Sbz	/* pass the mbuf to enc0 for bpf processing */
576174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
577159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
578174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
579159965Sthompsa		goto bad;
580282132Sae	ip = mtod(m, struct ip *);
581159965Sthompsa#endif
582275708Sae	/* Do the appropriate encapsulation, if necessary */
583275708Sae	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
584275708Sae	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
585275708Sae	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
586275708Sae	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
587275708Sae	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
588275708Sae		/* Fix IPv4 header checksum and length */
589275708Sae		ip->ip_len = htons(m->m_pkthdr.len);
590275708Sae		ip->ip_sum = 0;
591275708Sae		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
592281692Sae		error = ipsec_encap(&m, &sav->sah->saidx);
593275708Sae		if (error != 0) {
594281692Sae			DPRINTF(("%s: encapsulation for SA %s->%s "
595281692Sae			    "SPI 0x%08x failed with error %d\n", __func__,
596281695Sae			    ipsec_address(&sav->sah->saidx.src, sbuf,
597281695Sae				sizeof(sbuf)),
598281695Sae			    ipsec_address(&sav->sah->saidx.dst, dbuf,
599281695Sae				sizeof(dbuf)), ntohl(sav->spi), error));
600275708Sae			goto bad;
601275708Sae		}
602105197Ssam	}
603159965Sthompsa#ifdef DEV_ENC
604159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
605266800Svanhu	ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
606174054Sbz	/* pass the mbuf to enc0 for packet filtering */
607174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
608174054Sbz		goto bad;
609159965Sthompsa#endif
610159965Sthompsa
611105197Ssam	/*
612105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
613105197Ssam	 * packet will be returned for transmission after crypto
614281692Sae	 * processing, etc. are completed.
615105197Ssam	 *
616105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
617105197Ssam	 *     for reclaiming their resources.
618105197Ssam	 */
619281692Sae	switch(dst->sa.sa_family) {
620281692Sae	case AF_INET:
621281692Sae		ip = mtod(m, struct ip *);
622281692Sae		i = ip->ip_hl << 2;
623281692Sae		off = offsetof(struct ip, ip_p);
624281692Sae		break;
625266800Svanhu#ifdef INET6
626281692Sae	case AF_INET6:
627281692Sae		i = sizeof(struct ip6_hdr);
628281692Sae		off = offsetof(struct ip6_hdr, ip6_nxt);
629281692Sae		break;
630266800Svanhu#endif /* INET6 */
631281692Sae	default:
632266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
633281692Sae		    __func__, dst->sa.sa_family));
634281692Sae		error = EPFNOSUPPORT;
635281692Sae		IPSECSTAT_INC(ips_out_inval);
636281692Sae		goto bad;
637105197Ssam	}
638281692Sae	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
639120585Ssam	IPSECREQUEST_UNLOCK(isr);
640281692Sae	return (error);
641105197Ssambad:
642120585Ssam	if (isr)
643120585Ssam		IPSECREQUEST_UNLOCK(isr);
644105197Ssam	if (m)
645105197Ssam		m_freem(m);
646105197Ssam	return error;
647105197Ssam}
648105197Ssam#endif
649105197Ssam
650266800Svanhu
651105197Ssam#ifdef INET6
652105197Ssamstatic int
653266800Svanhuin6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
654105197Ssam{
655266800Svanhu	struct in6_addr ia2;
656105197Ssam
657266800Svanhu	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
658266800Svanhu	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
659266800Svanhu		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
660105197Ssam
661266800Svanhu	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
662105197Ssam}
663105197Ssam
664105197Ssam/*
665266800Svanhu * IPsec output logic for IPv6.
666105197Ssam */
667105197Ssamint
668281695Saeipsec6_process_packet(struct mbuf *m, struct ipsecrequest *isr)
669105197Ssam{
670281695Sae	char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
671266800Svanhu	struct secasindex saidx;
672266800Svanhu	struct secasvar *sav;
673105197Ssam	struct ip6_hdr *ip6;
674266800Svanhu	int error, i, off;
675266800Svanhu	union sockaddr_union *dst;
676105197Ssam
677266800Svanhu	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
678266800Svanhu	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
679105197Ssam
680266800Svanhu	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
681105197Ssam
682105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
683177175Sbz	if (isr == NULL) {
684177175Sbz		if (error != 0)
685177175Sbz			goto bad;
686266800Svanhu		return EJUSTRETURN;
687177175Sbz	}
688266800Svanhu	sav = isr->sav;
689266800Svanhu	dst = &sav->sah->saidx.dst;
690266800Svanhu
691274454Sae	ip6 = mtod(m, struct ip6_hdr *);
692274454Sae	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
693174054Sbz#ifdef DEV_ENC
694271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
695271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
696181627Svanhu
697174054Sbz	/* pass the mbuf to enc0 for bpf processing */
698174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
699174054Sbz	/* pass the mbuf to enc0 for packet filtering */
700174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
701174054Sbz		goto bad;
702282132Sae	ip6 = mtod(m, struct ip6_hdr *);
703266800Svanhu#endif /* DEV_ENC */
704174054Sbz
705266800Svanhu	/* Do the appropriate encapsulation, if necessary */
706266800Svanhu	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
707266800Svanhu	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
708266800Svanhu	    ((dst->sa.sa_family == AF_INET6) &&
709266800Svanhu	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
710266800Svanhu	     (!in6_sa_equal_addrwithscope(&dst->sin6,
711266800Svanhu				  &ip6->ip6_dst)))) {
712266800Svanhu		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
713266800Svanhu			/* No jumbogram support. */
714266800Svanhu			error = ENXIO;   /*XXX*/
715105197Ssam			goto bad;
716105197Ssam		}
717281692Sae		error = ipsec_encap(&m, &sav->sah->saidx);
718281692Sae		if (error != 0) {
719281692Sae			DPRINTF(("%s: encapsulation for SA %s->%s "
720281692Sae			    "SPI 0x%08x failed with error %d\n", __func__,
721281695Sae			    ipsec_address(&sav->sah->saidx.src, sbuf,
722281695Sae				sizeof(sbuf)),
723281695Sae			    ipsec_address(&sav->sah->saidx.dst, dbuf,
724281695Sae				sizeof(dbuf)), ntohl(sav->spi), error));
725105197Ssam			goto bad;
726105197Ssam		}
727105197Ssam	}
728105197Ssam
729174054Sbz#ifdef DEV_ENC
730266800Svanhu	ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
731174054Sbz	/* pass the mbuf to enc0 for packet filtering */
732174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
733174054Sbz		goto bad;
734266800Svanhu#endif /* DEV_ENC */
735174054Sbz
736266800Svanhu	switch(dst->sa.sa_family) {
737266800Svanhu#ifdef INET
738266800Svanhu	case AF_INET:
739266800Svanhu		{
740266800Svanhu		struct ip *ip;
741266800Svanhu		ip = mtod(m, struct ip *);
742266800Svanhu		i = ip->ip_hl << 2;
743266800Svanhu		off = offsetof(struct ip, ip_p);
744266800Svanhu		}
745266800Svanhu		break;
746266800Svanhu#endif /* AF_INET */
747266800Svanhu	case AF_INET6:
748266800Svanhu		i = sizeof(struct ip6_hdr);
749266800Svanhu		off = offsetof(struct ip6_hdr, ip6_nxt);
750266800Svanhu		break;
751266800Svanhu	default:
752266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
753266800Svanhu				 __func__, dst->sa.sa_family));
754266800Svanhu		error = EPFNOSUPPORT;
755266800Svanhu		IPSEC6STAT_INC(ips_out_inval);
756266800Svanhu		goto bad;
757266800Svanhu	}
758266800Svanhu	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
759170123Sbz	IPSECREQUEST_UNLOCK(isr);
760170123Sbz	return error;
761105197Ssambad:
762266800Svanhu
763170123Sbz	if (isr)
764170123Sbz		IPSECREQUEST_UNLOCK(isr);
765105197Ssam	if (m)
766105197Ssam		m_freem(m);
767105197Ssam	return error;
768105197Ssam}
769266822Sbz#endif /*INET6*/
770