ipsec_output.c revision 281693
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 281693 2015-04-18 16:46:31Z 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"));
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);
173275708Sae			return ipsec4_process_packet(m, isr->next);
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
423281692Saestatic int
424281692Saeipsec_encap(struct mbuf **mp, struct secasindex *saidx)
425281692Sae{
426281692Sae#ifdef INET6
427281692Sae	struct ip6_hdr *ip6;
428281692Sae#endif
429281692Sae	struct ip *ip;
430281692Sae	int setdf;
431281692Sae	uint8_t itos, proto;
432281692Sae
433281692Sae	ip = mtod(*mp, struct ip *);
434281692Sae	switch (ip->ip_v) {
435105197Ssam#ifdef INET
436281692Sae	case IPVERSION:
437281692Sae		proto = IPPROTO_IPIP;
438281692Sae		/*
439281692Sae		 * Collect IP_DF state from the inner header
440281692Sae		 * and honor system-wide control of how to handle it.
441281692Sae		 */
442281692Sae		switch (V_ip4_ipsec_dfbit) {
443281692Sae		case 0:	/* clear in outer header */
444281692Sae		case 1:	/* set in outer header */
445281692Sae			setdf = V_ip4_ipsec_dfbit;
446281692Sae			break;
447281692Sae		default:/* propagate to outer header */
448281692Sae			setdf = (ip->ip_off & ntohs(IP_DF)) != 0;
449281692Sae		}
450281692Sae		itos = ip->ip_tos;
451281692Sae		break;
452281692Sae#endif
453281692Sae#ifdef INET6
454281692Sae	case (IPV6_VERSION >> 4):
455281692Sae		proto = IPPROTO_IPV6;
456281692Sae		ip6 = mtod(*mp, struct ip6_hdr *);
457281692Sae		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
458281692Sae		setdf = V_ip4_ipsec_dfbit ? 1: 0;
459281692Sae		/* scoped address handling */
460281692Sae		in6_clearscope(&ip6->ip6_src);
461281692Sae		in6_clearscope(&ip6->ip6_dst);
462281692Sae		break;
463281692Sae#endif
464281692Sae	default:
465281692Sae		return (EAFNOSUPPORT);
466281692Sae	}
467281692Sae	switch (saidx->dst.sa.sa_family) {
468281692Sae#ifdef INET
469281692Sae	case AF_INET:
470281692Sae		if (saidx->src.sa.sa_family != AF_INET ||
471281692Sae		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
472281692Sae		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
473281692Sae			return (EINVAL);
474281692Sae		M_PREPEND(*mp, sizeof(struct ip), M_NOWAIT);
475281692Sae		if (*mp == NULL)
476281692Sae			return (ENOBUFS);
477281692Sae		ip = mtod(*mp, struct ip *);
478281692Sae		ip->ip_v = IPVERSION;
479281692Sae		ip->ip_hl = sizeof(struct ip) >> 2;
480281692Sae		ip->ip_p = proto;
481281692Sae		ip->ip_len = htons((*mp)->m_pkthdr.len);
482281692Sae		ip->ip_ttl = V_ip_defttl;
483281692Sae		ip->ip_sum = 0;
484281692Sae		ip->ip_off = setdf ? htons(IP_DF): 0;
485281692Sae		ip->ip_src = saidx->src.sin.sin_addr;
486281692Sae		ip->ip_dst = saidx->dst.sin.sin_addr;
487281692Sae		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
488281692Sae		ip_fillid(ip);
489281692Sae		break;
490281692Sae#endif /* INET */
491281692Sae#ifdef INET6
492281692Sae	case AF_INET6:
493281692Sae		if (saidx->src.sa.sa_family != AF_INET6 ||
494281692Sae		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
495281692Sae		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
496281692Sae			return (EINVAL);
497281692Sae		M_PREPEND(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
498281692Sae		if (*mp == NULL)
499281692Sae			return (ENOBUFS);
500281692Sae		ip6 = mtod(*mp, struct ip6_hdr *);
501281692Sae		ip6->ip6_flow = 0;
502281692Sae		ip6->ip6_vfc = IPV6_VERSION;
503281692Sae		ip6->ip6_hlim = V_ip6_defhlim;
504281692Sae		ip6->ip6_nxt = proto;
505281692Sae		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
506281693Sae		/* For link-local address embed scope zone id */
507281693Sae		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
508281693Sae			ip6->ip6_dst.s6_addr16[1] =
509281693Sae			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
510281692Sae		ip6->ip6_src = saidx->src.sin6.sin6_addr;
511281693Sae		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
512281693Sae			ip6->ip6_src.s6_addr16[1] =
513281693Sae			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
514281692Sae		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
515281692Sae		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
516281692Sae		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
517281692Sae		break;
518281692Sae#endif /* INET6 */
519281692Sae	default:
520281692Sae		return (EAFNOSUPPORT);
521281692Sae	}
522281692Sae	return (0);
523281692Sae}
524281692Sae
525281692Sae#ifdef INET
526105197Ssam/*
527105197Ssam * IPsec output logic for IPv4.
528105197Ssam */
529105197Ssamint
530275708Saeipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
531105197Ssam{
532275708Sae	union sockaddr_union *dst;
533105197Ssam	struct secasindex saidx;
534105197Ssam	struct secasvar *sav;
535105197Ssam	struct ip *ip;
536281692Sae	int error, i, off;
537105197Ssam
538120585Ssam	IPSEC_ASSERT(m != NULL, ("null mbuf"));
539120585Ssam	IPSEC_ASSERT(isr != NULL, ("null isr"));
540105197Ssam
541120585Ssam	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
542105197Ssam
543105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
544177175Sbz	if (isr == NULL) {
545177175Sbz		if (error != 0)
546177175Sbz			goto bad;
547177175Sbz		return EJUSTRETURN;
548177175Sbz	}
549105197Ssam
550105197Ssam	sav = isr->sav;
551275708Sae	if (m->m_len < sizeof(struct ip) &&
552275708Sae	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
553275708Sae		error = ENOBUFS;
554275708Sae		goto bad;
555275708Sae	}
556275708Sae	ip = mtod(m, struct ip *);
557275708Sae	dst = &sav->sah->saidx.dst;
558159965Sthompsa#ifdef DEV_ENC
559271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
560271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
561181627Svanhu
562174054Sbz	/* pass the mbuf to enc0 for bpf processing */
563174054Sbz	ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
564159965Sthompsa	/* pass the mbuf to enc0 for packet filtering */
565174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
566159965Sthompsa		goto bad;
567159965Sthompsa#endif
568275708Sae	/* Do the appropriate encapsulation, if necessary */
569275708Sae	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
570275708Sae	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
571275708Sae	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
572275708Sae	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
573275708Sae	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
574275708Sae		/* Fix IPv4 header checksum and length */
575275708Sae		ip->ip_len = htons(m->m_pkthdr.len);
576275708Sae		ip->ip_sum = 0;
577275708Sae		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
578281692Sae		error = ipsec_encap(&m, &sav->sah->saidx);
579275708Sae		if (error != 0) {
580281692Sae			DPRINTF(("%s: encapsulation for SA %s->%s "
581281692Sae			    "SPI 0x%08x failed with error %d\n", __func__,
582281692Sae			    ipsec_address(&sav->sah->saidx.src),
583281692Sae			    ipsec_address(&sav->sah->saidx.dst),
584281692Sae			    ntohl(sav->spi), error));
585275708Sae			goto bad;
586275708Sae		}
587105197Ssam	}
588159965Sthompsa#ifdef DEV_ENC
589159965Sthompsa	/* pass the mbuf to enc0 for bpf processing */
590266800Svanhu	ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
591174054Sbz	/* pass the mbuf to enc0 for packet filtering */
592174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
593174054Sbz		goto bad;
594159965Sthompsa#endif
595159965Sthompsa
596105197Ssam	/*
597105197Ssam	 * Dispatch to the appropriate IPsec transform logic.  The
598105197Ssam	 * packet will be returned for transmission after crypto
599281692Sae	 * processing, etc. are completed.
600105197Ssam	 *
601105197Ssam	 * NB: m & sav are ``passed to caller'' who's reponsible for
602105197Ssam	 *     for reclaiming their resources.
603105197Ssam	 */
604281692Sae	switch(dst->sa.sa_family) {
605281692Sae	case AF_INET:
606281692Sae		ip = mtod(m, struct ip *);
607281692Sae		i = ip->ip_hl << 2;
608281692Sae		off = offsetof(struct ip, ip_p);
609281692Sae		break;
610266800Svanhu#ifdef INET6
611281692Sae	case AF_INET6:
612281692Sae		i = sizeof(struct ip6_hdr);
613281692Sae		off = offsetof(struct ip6_hdr, ip6_nxt);
614281692Sae		break;
615266800Svanhu#endif /* INET6 */
616281692Sae	default:
617266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
618281692Sae		    __func__, dst->sa.sa_family));
619281692Sae		error = EPFNOSUPPORT;
620281692Sae		IPSECSTAT_INC(ips_out_inval);
621281692Sae		goto bad;
622105197Ssam	}
623281692Sae	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
624120585Ssam	IPSECREQUEST_UNLOCK(isr);
625281692Sae	return (error);
626105197Ssambad:
627120585Ssam	if (isr)
628120585Ssam		IPSECREQUEST_UNLOCK(isr);
629105197Ssam	if (m)
630105197Ssam		m_freem(m);
631105197Ssam	return error;
632105197Ssam}
633105197Ssam#endif
634105197Ssam
635266800Svanhu
636105197Ssam#ifdef INET6
637105197Ssamstatic int
638266800Svanhuin6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
639105197Ssam{
640266800Svanhu	struct in6_addr ia2;
641105197Ssam
642266800Svanhu	memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
643266800Svanhu	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
644266800Svanhu		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
645105197Ssam
646266800Svanhu	return IN6_ARE_ADDR_EQUAL(ia, &ia2);
647105197Ssam}
648105197Ssam
649105197Ssam/*
650266800Svanhu * IPsec output logic for IPv6.
651105197Ssam */
652105197Ssamint
653266800Svanhuipsec6_process_packet(
654266800Svanhu	struct mbuf *m,
655266800Svanhu 	struct ipsecrequest *isr
656266800Svanhu    )
657105197Ssam{
658266800Svanhu	struct secasindex saidx;
659266800Svanhu	struct secasvar *sav;
660105197Ssam	struct ip6_hdr *ip6;
661266800Svanhu	int error, i, off;
662266800Svanhu	union sockaddr_union *dst;
663105197Ssam
664266800Svanhu	IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
665266800Svanhu	IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
666105197Ssam
667266800Svanhu	IPSECREQUEST_LOCK(isr);		/* insure SA contents don't change */
668105197Ssam
669105197Ssam	isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
670177175Sbz	if (isr == NULL) {
671177175Sbz		if (error != 0)
672177175Sbz			goto bad;
673266800Svanhu		return EJUSTRETURN;
674177175Sbz	}
675266800Svanhu	sav = isr->sav;
676266800Svanhu	dst = &sav->sah->saidx.dst;
677266800Svanhu
678274454Sae	ip6 = mtod(m, struct ip6_hdr *);
679274454Sae	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
680174054Sbz#ifdef DEV_ENC
681271862Sglebius	if_inc_counter(encif, IFCOUNTER_OPACKETS, 1);
682271862Sglebius	if_inc_counter(encif, IFCOUNTER_OBYTES, m->m_pkthdr.len);
683181627Svanhu
684174054Sbz	/* pass the mbuf to enc0 for bpf processing */
685174054Sbz	ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
686174054Sbz	/* pass the mbuf to enc0 for packet filtering */
687174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
688174054Sbz		goto bad;
689266800Svanhu#endif /* DEV_ENC */
690174054Sbz
691266800Svanhu	/* Do the appropriate encapsulation, if necessary */
692266800Svanhu	if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
693266800Svanhu	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
694266800Svanhu	    ((dst->sa.sa_family == AF_INET6) &&
695266800Svanhu	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
696266800Svanhu	     (!in6_sa_equal_addrwithscope(&dst->sin6,
697266800Svanhu				  &ip6->ip6_dst)))) {
698266800Svanhu		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
699266800Svanhu			/* No jumbogram support. */
700266800Svanhu			error = ENXIO;   /*XXX*/
701105197Ssam			goto bad;
702105197Ssam		}
703281692Sae		error = ipsec_encap(&m, &sav->sah->saidx);
704281692Sae		if (error != 0) {
705281692Sae			DPRINTF(("%s: encapsulation for SA %s->%s "
706281692Sae			    "SPI 0x%08x failed with error %d\n", __func__,
707281692Sae			    ipsec_address(&sav->sah->saidx.src),
708281692Sae			    ipsec_address(&sav->sah->saidx.dst),
709281692Sae			    ntohl(sav->spi), error));
710105197Ssam			goto bad;
711105197Ssam		}
712105197Ssam	}
713105197Ssam
714174054Sbz#ifdef DEV_ENC
715266800Svanhu	ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
716174054Sbz	/* pass the mbuf to enc0 for packet filtering */
717174054Sbz	if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
718174054Sbz		goto bad;
719266800Svanhu#endif /* DEV_ENC */
720174054Sbz
721266800Svanhu	switch(dst->sa.sa_family) {
722266800Svanhu#ifdef INET
723266800Svanhu	case AF_INET:
724266800Svanhu		{
725266800Svanhu		struct ip *ip;
726266800Svanhu		ip = mtod(m, struct ip *);
727266800Svanhu		i = ip->ip_hl << 2;
728266800Svanhu		off = offsetof(struct ip, ip_p);
729266800Svanhu		}
730266800Svanhu		break;
731266800Svanhu#endif /* AF_INET */
732266800Svanhu	case AF_INET6:
733266800Svanhu		i = sizeof(struct ip6_hdr);
734266800Svanhu		off = offsetof(struct ip6_hdr, ip6_nxt);
735266800Svanhu		break;
736266800Svanhu	default:
737266800Svanhu		DPRINTF(("%s: unsupported protocol family %u\n",
738266800Svanhu				 __func__, dst->sa.sa_family));
739266800Svanhu		error = EPFNOSUPPORT;
740266800Svanhu		IPSEC6STAT_INC(ips_out_inval);
741266800Svanhu		goto bad;
742266800Svanhu	}
743266800Svanhu	error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
744170123Sbz	IPSECREQUEST_UNLOCK(isr);
745170123Sbz	return error;
746105197Ssambad:
747266800Svanhu
748170123Sbz	if (isr)
749170123Sbz		IPSECREQUEST_UNLOCK(isr);
750105197Ssam	if (m)
751105197Ssam		m_freem(m);
752105197Ssam	return error;
753105197Ssam}
754266822Sbz#endif /*INET6*/
755