xref: /freebsd/sys/netipsec/ipsec_output.c (revision 9dfc8606eb80a49a0b071ff17dbbfa307d7248be)
1aaea26efSSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
4aaea26efSSam Leffler  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5fcf59617SAndrey V. Elsukov  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
6aaea26efSSam Leffler  * All rights reserved.
7aaea26efSSam Leffler  *
8aaea26efSSam Leffler  * Redistribution and use in source and binary forms, with or without
9aaea26efSSam Leffler  * modification, are permitted provided that the following conditions
10aaea26efSSam Leffler  * are met:
11aaea26efSSam Leffler  * 1. Redistributions of source code must retain the above copyright
12aaea26efSSam Leffler  *    notice, this list of conditions and the following disclaimer.
13aaea26efSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
14aaea26efSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
15aaea26efSSam Leffler  *    documentation and/or other materials provided with the distribution.
16aaea26efSSam Leffler  *
17aaea26efSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18aaea26efSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19aaea26efSSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20aaea26efSSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21aaea26efSSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22aaea26efSSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23aaea26efSSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24aaea26efSSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25aaea26efSSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26aaea26efSSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27aaea26efSSam Leffler  * SUCH DAMAGE.
28aaea26efSSam Leffler  *
29aaea26efSSam Leffler  * $FreeBSD$
30aaea26efSSam Leffler  */
3188768458SSam Leffler 
3288768458SSam Leffler /*
3388768458SSam Leffler  * IPsec output processing.
3488768458SSam Leffler  */
3588768458SSam Leffler #include "opt_inet.h"
3688768458SSam Leffler #include "opt_inet6.h"
3788768458SSam Leffler #include "opt_ipsec.h"
38fcf59617SAndrey V. Elsukov #include "opt_sctp.h"
3988768458SSam Leffler 
4088768458SSam Leffler #include <sys/param.h>
4188768458SSam Leffler #include <sys/systm.h>
4288768458SSam Leffler #include <sys/mbuf.h>
4388768458SSam Leffler #include <sys/domain.h>
4488768458SSam Leffler #include <sys/protosw.h>
4588768458SSam Leffler #include <sys/socket.h>
4688768458SSam Leffler #include <sys/errno.h>
47ef91a976SAndrey V. Elsukov #include <sys/hhook.h>
4888768458SSam Leffler #include <sys/syslog.h>
4988768458SSam Leffler 
5088768458SSam Leffler #include <net/if.h>
51ef91a976SAndrey V. Elsukov #include <net/if_enc.h>
5276039bc8SGleb Smirnoff #include <net/if_var.h>
53eddfbb76SRobert Watson #include <net/vnet.h>
5488768458SSam Leffler 
5588768458SSam Leffler #include <netinet/in.h>
5688768458SSam Leffler #include <netinet/in_systm.h>
5788768458SSam Leffler #include <netinet/ip.h>
5888768458SSam Leffler #include <netinet/ip_var.h>
5988768458SSam Leffler #include <netinet/in_var.h>
6088768458SSam Leffler #include <netinet/ip_ecn.h>
6188768458SSam Leffler #ifdef INET6
6288768458SSam Leffler #include <netinet6/ip6_ecn.h>
6388768458SSam Leffler #endif
646b66194bSKornel Duleba #include <netinet/ip_icmp.h>
656b66194bSKornel Duleba #include <netinet/tcp_var.h>
6688768458SSam Leffler 
6788768458SSam Leffler #include <netinet/ip6.h>
6888768458SSam Leffler #ifdef INET6
6988768458SSam Leffler #include <netinet6/ip6_var.h>
7061f37615SAndrey V. Elsukov #include <netinet6/scope6_var.h>
7188768458SSam Leffler #endif
7288768458SSam Leffler #include <netinet/in_pcb.h>
7388768458SSam Leffler #ifdef INET6
7488768458SSam Leffler #include <netinet/icmp6.h>
7588768458SSam Leffler #endif
7695033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
77fcf59617SAndrey V. Elsukov #include <netinet/sctp_crc32.h>
78fcf59617SAndrey V. Elsukov #endif
7988768458SSam Leffler 
80fcf59617SAndrey V. Elsukov #include <netinet/udp.h>
81fcf59617SAndrey V. Elsukov #include <netipsec/ah.h>
82fcf59617SAndrey V. Elsukov #include <netipsec/esp.h>
8388768458SSam Leffler #include <netipsec/ipsec.h>
8488768458SSam Leffler #ifdef INET6
8588768458SSam Leffler #include <netipsec/ipsec6.h>
8688768458SSam Leffler #endif
8788768458SSam Leffler #include <netipsec/ah_var.h>
8888768458SSam Leffler #include <netipsec/esp_var.h>
8988768458SSam Leffler #include <netipsec/ipcomp_var.h>
9088768458SSam Leffler 
9188768458SSam Leffler #include <netipsec/xform.h>
9288768458SSam Leffler 
9388768458SSam Leffler #include <netipsec/key.h>
9488768458SSam Leffler #include <netipsec/keydb.h>
9588768458SSam Leffler #include <netipsec/key_debug.h>
9688768458SSam Leffler 
9788768458SSam Leffler #include <machine/in_cksum.h>
9888768458SSam Leffler 
99fcf59617SAndrey V. Elsukov #define	IPSEC_OSTAT_INC(proto, name)	do {		\
100fcf59617SAndrey V. Elsukov 	if ((proto) == IPPROTO_ESP)	\
101fcf59617SAndrey V. Elsukov 		ESPSTAT_INC(esps_##name);	\
102fcf59617SAndrey V. Elsukov 	else if ((proto) == IPPROTO_AH)\
103fcf59617SAndrey V. Elsukov 		AHSTAT_INC(ahs_##name);		\
104fcf59617SAndrey V. Elsukov 	else					\
105fcf59617SAndrey V. Elsukov 		IPCOMPSTAT_INC(ipcomps_##name);	\
106fcf59617SAndrey V. Elsukov } while (0)
107fcf59617SAndrey V. Elsukov 
108fcf59617SAndrey V. Elsukov static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
109*9dfc8606SBartlomiej Grzesik static size_t ipsec_get_pmtu(struct secasvar *sav);
110fcf59617SAndrey V. Elsukov 
111fcf59617SAndrey V. Elsukov #ifdef INET
112fcf59617SAndrey V. Elsukov static struct secasvar *
113fcf59617SAndrey V. Elsukov ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
114fcf59617SAndrey V. Elsukov {
115fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
116fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
117fcf59617SAndrey V. Elsukov 	struct sockaddr_in *sin;
118fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
119fcf59617SAndrey V. Elsukov 	struct ip *ip;
120fcf59617SAndrey V. Elsukov 
121fcf59617SAndrey V. Elsukov 	/*
122fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
123fcf59617SAndrey V. Elsukov 	 */
124fcf59617SAndrey V. Elsukov next:
125fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
126fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
127fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
128fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
129fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
130fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
131fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
132fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
133fcf59617SAndrey V. Elsukov 		return (NULL);
134fcf59617SAndrey V. Elsukov 	}
135fcf59617SAndrey V. Elsukov 	/*
136fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
137fcf59617SAndrey V. Elsukov 	 * we only initialize unspecified SA peers for transport
138fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
139fcf59617SAndrey V. Elsukov 	 */
140fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
141fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
142fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
143fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
144fcf59617SAndrey V. Elsukov 		if (saidx->src.sa.sa_len == 0) {
145fcf59617SAndrey V. Elsukov 			sin = &saidx->src.sin;
146fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
147fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
148fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
149fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_src;
150fcf59617SAndrey V. Elsukov 		}
151fcf59617SAndrey V. Elsukov 		if (saidx->dst.sa.sa_len == 0) {
152fcf59617SAndrey V. Elsukov 			sin = &saidx->dst.sin;
153fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
154fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
155fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
156fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_dst;
157fcf59617SAndrey V. Elsukov 		}
158fcf59617SAndrey V. Elsukov 	} else
159fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
160fcf59617SAndrey V. Elsukov 	/*
161fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
162fcf59617SAndrey V. Elsukov 	 */
163fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
164fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
165fcf59617SAndrey V. Elsukov 		IPSECSTAT_INC(ips_out_nosa);
166fcf59617SAndrey V. Elsukov 		if (*error != 0)
167fcf59617SAndrey V. Elsukov 			return (NULL);
168fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
169fcf59617SAndrey V. Elsukov 			/*
170fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
171fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
172fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
173fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
174fcf59617SAndrey V. Elsukov 			 */
175fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
176fcf59617SAndrey V. Elsukov 				goto next;
177fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
178fcf59617SAndrey V. Elsukov 		}
179fcf59617SAndrey V. Elsukov 		return (NULL);
180fcf59617SAndrey V. Elsukov 	}
181fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
182fcf59617SAndrey V. Elsukov 	return (sav);
183fcf59617SAndrey V. Elsukov }
184fcf59617SAndrey V. Elsukov 
185fcf59617SAndrey V. Elsukov /*
186fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv4.
187fcf59617SAndrey V. Elsukov  */
188fcf59617SAndrey V. Elsukov static int
1891a01e0e7SAndrey V. Elsukov ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp,
1901a01e0e7SAndrey V. Elsukov     struct inpcb *inp, u_int idx)
191fcf59617SAndrey V. Elsukov {
192fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
193fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
194fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
195fcf59617SAndrey V. Elsukov 	struct ip *ip;
196fcf59617SAndrey V. Elsukov 	int error, i, off;
197fcf59617SAndrey V. Elsukov 
198fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
199fcf59617SAndrey V. Elsukov 
200fcf59617SAndrey V. Elsukov 	/*
201fcf59617SAndrey V. Elsukov 	 * We hold the reference to SP. Content of SP couldn't be changed.
202fcf59617SAndrey V. Elsukov 	 * Craft secasindex and do lookup for suitable SA.
203fcf59617SAndrey V. Elsukov 	 * Then do encapsulation if needed and call xform's output.
204fcf59617SAndrey V. Elsukov 	 * We need to store SP in the xform callback parameters.
205fcf59617SAndrey V. Elsukov 	 * In xform callback we will extract SP and it can be used to
206fcf59617SAndrey V. Elsukov 	 * determine next transform. At the end of transform we can
207fcf59617SAndrey V. Elsukov 	 * release reference to SP.
208fcf59617SAndrey V. Elsukov 	 */
209fcf59617SAndrey V. Elsukov 	sav = ipsec4_allocsa(m, sp, &idx, &error);
210fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
211fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
212fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
213fcf59617SAndrey V. Elsukov 			return (error);
214fcf59617SAndrey V. Elsukov 		}
215fcf59617SAndrey V. Elsukov 		goto bad;
216fcf59617SAndrey V. Elsukov 	}
217fcf59617SAndrey V. Elsukov 	/*
218fcf59617SAndrey V. Elsukov 	 * XXXAE: most likely ip_sum at this point is wrong.
219fcf59617SAndrey V. Elsukov 	 */
2201a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE);
221fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
222fcf59617SAndrey V. Elsukov 		goto bad;
223fcf59617SAndrey V. Elsukov 
224fcf59617SAndrey V. Elsukov 	ip = mtod(m, struct ip *);
225fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
226fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
227fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
228fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
229fcf59617SAndrey V. Elsukov 	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
230fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
231fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
232fcf59617SAndrey V. Elsukov 		/* Fix IPv4 header checksum and length */
233fcf59617SAndrey V. Elsukov 		ip->ip_len = htons(m->m_pkthdr.len);
234fcf59617SAndrey V. Elsukov 		ip->ip_sum = 0;
235fcf59617SAndrey V. Elsukov 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
236fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
237fcf59617SAndrey V. Elsukov 		if (error != 0) {
2387f1f6591SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
2397f1f6591SAndrey V. Elsukov 			    "with error %d\n", __func__, ntohl(sav->spi),
2407f1f6591SAndrey V. Elsukov 			    error));
241fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
242fcf59617SAndrey V. Elsukov 			goto bad;
243fcf59617SAndrey V. Elsukov 		}
2441a01e0e7SAndrey V. Elsukov 		inp = NULL;
245fcf59617SAndrey V. Elsukov 	}
246fcf59617SAndrey V. Elsukov 
2471a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
248fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
249fcf59617SAndrey V. Elsukov 		goto bad;
250fcf59617SAndrey V. Elsukov 
251fcf59617SAndrey V. Elsukov 	/*
252fcf59617SAndrey V. Elsukov 	 * Dispatch to the appropriate IPsec transform logic.  The
253fcf59617SAndrey V. Elsukov 	 * packet will be returned for transmission after crypto
254fcf59617SAndrey V. Elsukov 	 * processing, etc. are completed.
255fcf59617SAndrey V. Elsukov 	 *
256fcf59617SAndrey V. Elsukov 	 * NB: m & sav are ``passed to caller'' who's responsible for
257fcf59617SAndrey V. Elsukov 	 *     reclaiming their resources.
258fcf59617SAndrey V. Elsukov 	 */
259fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
260fcf59617SAndrey V. Elsukov 	case AF_INET:
261fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
262fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
263fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
264fcf59617SAndrey V. Elsukov 		break;
265fcf59617SAndrey V. Elsukov #ifdef INET6
266fcf59617SAndrey V. Elsukov 	case AF_INET6:
267fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
268fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
269fcf59617SAndrey V. Elsukov 		break;
270fcf59617SAndrey V. Elsukov #endif /* INET6 */
271fcf59617SAndrey V. Elsukov 	default:
272fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
273fcf59617SAndrey V. Elsukov 		    __func__, dst->sa.sa_family));
274fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
275fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
276fcf59617SAndrey V. Elsukov 		goto bad;
277fcf59617SAndrey V. Elsukov 	}
278fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
279fcf59617SAndrey V. Elsukov 	return (error);
280fcf59617SAndrey V. Elsukov bad:
281fcf59617SAndrey V. Elsukov 	IPSECSTAT_INC(ips_out_inval);
282fcf59617SAndrey V. Elsukov 	if (m != NULL)
283fcf59617SAndrey V. Elsukov 		m_freem(m);
284fcf59617SAndrey V. Elsukov 	if (sav != NULL)
285fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
286fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
287fcf59617SAndrey V. Elsukov 	return (error);
288fcf59617SAndrey V. Elsukov }
2897b495c44SVANHULLEBUS Yvan 
29088768458SSam Leffler int
291fcf59617SAndrey V. Elsukov ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
292fcf59617SAndrey V. Elsukov     struct inpcb *inp)
29388768458SSam Leffler {
294fcf59617SAndrey V. Elsukov 
2951a01e0e7SAndrey V. Elsukov 	return (ipsec4_perform_request(m, sp, inp, 0));
296fcf59617SAndrey V. Elsukov }
297fcf59617SAndrey V. Elsukov 
2986b66194bSKornel Duleba int
2996b66194bSKornel Duleba ipsec4_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding)
3006b66194bSKornel Duleba {
3016b66194bSKornel Duleba 	struct secasvar *sav;
3026b66194bSKornel Duleba 	struct ip *ip;
3036b66194bSKornel Duleba 	size_t hlen, pmtu;
3046b66194bSKornel Duleba 	uint32_t idx;
3056b66194bSKornel Duleba 	int error;
3066b66194bSKornel Duleba 
3076b66194bSKornel Duleba 	/* Don't check PMTU if the frame won't have DF bit set. */
3086b66194bSKornel Duleba 	if (!V_ip4_ipsec_dfbit)
3096b66194bSKornel Duleba 		return (0);
3106b66194bSKornel Duleba 	if (V_ip4_ipsec_dfbit == 1)
3116b66194bSKornel Duleba 		goto setdf;
3126b66194bSKornel Duleba 
3136b66194bSKornel Duleba 	/* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */
3146b66194bSKornel Duleba 	ip = mtod(m, struct ip *);
3156b66194bSKornel Duleba 	if (!(ip->ip_off & htons(IP_DF)))
3166b66194bSKornel Duleba 		return (0);
3176b66194bSKornel Duleba 
3186b66194bSKornel Duleba setdf:
3196b66194bSKornel Duleba 	idx = sp->tcount - 1;
3206b66194bSKornel Duleba 	sav = ipsec4_allocsa(m, sp, &idx, &error);
3216b66194bSKornel Duleba 	if (sav == NULL) {
3226b66194bSKornel Duleba 		key_freesp(&sp);
323a16771deSKornel Duleba 		/*
324a16771deSKornel Duleba 		 * No matching SA was found and SADB_ACQUIRE message was generated.
325a16771deSKornel Duleba 		 * Since we have matched a SP to this packet drop it silently.
326a16771deSKornel Duleba 		 */
327a16771deSKornel Duleba 		if (error == 0)
328a16771deSKornel Duleba 			error = EINPROGRESS;
3296b66194bSKornel Duleba 		if (error != EJUSTRETURN)
3306b66194bSKornel Duleba 			m_freem(m);
3316b66194bSKornel Duleba 
3326b66194bSKornel Duleba 		return (error);
3336b66194bSKornel Duleba 	}
3346b66194bSKornel Duleba 
335*9dfc8606SBartlomiej Grzesik 	pmtu = ipsec_get_pmtu(sav);
336b4220bf3SBartlomiej Grzesik 	if (pmtu == 0) {
337*9dfc8606SBartlomiej Grzesik 		key_freesav(&sav);
3386b66194bSKornel Duleba 		return (0);
339b4220bf3SBartlomiej Grzesik 	}
340b4220bf3SBartlomiej Grzesik 
3416b66194bSKornel Duleba 	hlen = ipsec_hdrsiz_internal(sp);
342*9dfc8606SBartlomiej Grzesik 	key_freesav(&sav);
343*9dfc8606SBartlomiej Grzesik 
3446b66194bSKornel Duleba 	if (m_length(m, NULL) + hlen > pmtu) {
3456b66194bSKornel Duleba 		/*
3466b66194bSKornel Duleba 		 * If we're forwarding generate ICMP message here,
347b4220bf3SBartlomiej Grzesik 		 * so that it contains pmtu substraced by header size.
3486b66194bSKornel Duleba 		 * Set error to EINPROGRESS, in order for the frame
3496b66194bSKornel Duleba 		 * to be dropped silently.
3506b66194bSKornel Duleba 		 */
3516b66194bSKornel Duleba 		if (forwarding) {
3526b66194bSKornel Duleba 			if (pmtu > hlen)
3536b66194bSKornel Duleba 				icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
3546b66194bSKornel Duleba 				    0, pmtu - hlen);
3556b66194bSKornel Duleba 			else
3566b66194bSKornel Duleba 				m_freem(m);
3576b66194bSKornel Duleba 
3586b66194bSKornel Duleba 			key_freesp(&sp);
3596b66194bSKornel Duleba 			return (EINPROGRESS); /* Pretend that we consumed it. */
3606b66194bSKornel Duleba 		} else {
3616b66194bSKornel Duleba 			m_freem(m);
3626b66194bSKornel Duleba 			key_freesp(&sp);
3636b66194bSKornel Duleba 			return (EMSGSIZE);
3646b66194bSKornel Duleba 		}
3656b66194bSKornel Duleba 	}
3666b66194bSKornel Duleba 
3676b66194bSKornel Duleba 	return (0);
3686b66194bSKornel Duleba }
3696b66194bSKornel Duleba 
370fcf59617SAndrey V. Elsukov static int
371fcf59617SAndrey V. Elsukov ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
372fcf59617SAndrey V. Elsukov {
373fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
37488768458SSam Leffler 	int error;
37588768458SSam Leffler 
376fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
37722bbefb2SAndrey V. Elsukov 	sp = ipsec4_checkpolicy(m, inp, &error, !forwarding);
378fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
379fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
380fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
381fcf59617SAndrey V. Elsukov 			m_freem(m);
382fcf59617SAndrey V. Elsukov 			return (EACCES);
383fcf59617SAndrey V. Elsukov 		}
384fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
385fcf59617SAndrey V. Elsukov 	}
386fcf59617SAndrey V. Elsukov 
387fcf59617SAndrey V. Elsukov 	/*
388fcf59617SAndrey V. Elsukov 	 * Usually we have to have tunnel mode IPsec security policy
389fcf59617SAndrey V. Elsukov 	 * when we are forwarding a packet. Otherwise we could not handle
390fcf59617SAndrey V. Elsukov 	 * encrypted replies, because they are not destined for us. But
391fcf59617SAndrey V. Elsukov 	 * some users are doing source address translation for forwarded
392fcf59617SAndrey V. Elsukov 	 * packets, and thus, even if they are forwarded, the replies will
393fcf59617SAndrey V. Elsukov 	 * return back to us.
394fcf59617SAndrey V. Elsukov 	 */
395fcf59617SAndrey V. Elsukov 	if (!forwarding) {
396fcf59617SAndrey V. Elsukov 		/*
397fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
398fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
399fcf59617SAndrey V. Elsukov 		 */
400fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
4018e9313caSJohn Baldwin 			m = mb_unmapped_to_ext(m);
4028e9313caSJohn Baldwin 			if (m == NULL) {
4038e9313caSJohn Baldwin 				IPSECSTAT_INC(ips_out_nomem);
4048e9313caSJohn Baldwin 				key_freesp(&sp);
4058e9313caSJohn Baldwin 				return (ENOBUFS);
4068e9313caSJohn Baldwin 			}
407fcf59617SAndrey V. Elsukov 			in_delayed_cksum(m);
408fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
409fcf59617SAndrey V. Elsukov 		}
41095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
411fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
4128e9313caSJohn Baldwin 			struct ip *ip;
413fcf59617SAndrey V. Elsukov 
4148e9313caSJohn Baldwin 			m = mb_unmapped_to_ext(m);
4158e9313caSJohn Baldwin 			if (m == NULL) {
4168e9313caSJohn Baldwin 				IPSECSTAT_INC(ips_out_nomem);
4178e9313caSJohn Baldwin 				key_freesp(&sp);
4188e9313caSJohn Baldwin 				return (ENOBUFS);
4198e9313caSJohn Baldwin 			}
4208e9313caSJohn Baldwin 			ip = mtod(m, struct ip *);
421fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
422fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
423fcf59617SAndrey V. Elsukov 		}
424fcf59617SAndrey V. Elsukov #endif
425fcf59617SAndrey V. Elsukov 	}
426fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
4276b66194bSKornel Duleba 	error = ipsec4_check_pmtu(m, sp, forwarding);
4286b66194bSKornel Duleba 	if (error != 0) {
4296b66194bSKornel Duleba 		if (error == EJUSTRETURN)
4306b66194bSKornel Duleba 			return (0);
4316b66194bSKornel Duleba 
4326b66194bSKornel Duleba 		return (error);
4336b66194bSKornel Duleba 	}
4346b66194bSKornel Duleba 
435fcf59617SAndrey V. Elsukov 	error = ipsec4_process_packet(m, sp, inp);
436fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
437fcf59617SAndrey V. Elsukov 		/*
438fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
439fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
440fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
441fcf59617SAndrey V. Elsukov 		 */
442fcf59617SAndrey V. Elsukov 		return (0);
443fcf59617SAndrey V. Elsukov 	}
444fcf59617SAndrey V. Elsukov 	if (error == 0)
445fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
446fcf59617SAndrey V. Elsukov 	return (error);
447fcf59617SAndrey V. Elsukov }
448fcf59617SAndrey V. Elsukov 
449fcf59617SAndrey V. Elsukov /*
450fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv4.
451fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
452fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
453fcf59617SAndrey V. Elsukov  */
454fcf59617SAndrey V. Elsukov int
455fcf59617SAndrey V. Elsukov ipsec4_output(struct mbuf *m, struct inpcb *inp)
456fcf59617SAndrey V. Elsukov {
457fcf59617SAndrey V. Elsukov 
458fcf59617SAndrey V. Elsukov 	/*
459fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
460fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
461fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
462fcf59617SAndrey V. Elsukov 	 */
463fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
464fcf59617SAndrey V. Elsukov 		return (0);
465fcf59617SAndrey V. Elsukov 
466fcf59617SAndrey V. Elsukov 	return (ipsec4_common_output(m, inp, 0));
467fcf59617SAndrey V. Elsukov }
468fcf59617SAndrey V. Elsukov 
469fcf59617SAndrey V. Elsukov /*
470fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv4.
471fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
472fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
473fcf59617SAndrey V. Elsukov  */
474fcf59617SAndrey V. Elsukov int
475fcf59617SAndrey V. Elsukov ipsec4_forward(struct mbuf *m)
476fcf59617SAndrey V. Elsukov {
477fcf59617SAndrey V. Elsukov 
478fcf59617SAndrey V. Elsukov 	/*
479fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
480fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
481fcf59617SAndrey V. Elsukov 	 */
482fcf59617SAndrey V. Elsukov 	if (ipsec4_in_reject(m, NULL) != 0) {
483fcf59617SAndrey V. Elsukov 		m_freem(m);
484fcf59617SAndrey V. Elsukov 		return (EACCES);
485fcf59617SAndrey V. Elsukov 	}
486fcf59617SAndrey V. Elsukov 	return (ipsec4_common_output(m, NULL, 1));
487fcf59617SAndrey V. Elsukov }
488fcf59617SAndrey V. Elsukov #endif
489fcf59617SAndrey V. Elsukov 
490fcf59617SAndrey V. Elsukov #ifdef INET6
491fcf59617SAndrey V. Elsukov static int
492fcf59617SAndrey V. Elsukov in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
493fcf59617SAndrey V. Elsukov     const struct in6_addr *ia)
494fcf59617SAndrey V. Elsukov {
495fcf59617SAndrey V. Elsukov 	struct in6_addr ia2;
496fcf59617SAndrey V. Elsukov 
497fcf59617SAndrey V. Elsukov 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
498fcf59617SAndrey V. Elsukov 		memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
499fcf59617SAndrey V. Elsukov 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
500fcf59617SAndrey V. Elsukov 		return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
501fcf59617SAndrey V. Elsukov 	}
502fcf59617SAndrey V. Elsukov 	return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
503fcf59617SAndrey V. Elsukov }
504fcf59617SAndrey V. Elsukov 
505fcf59617SAndrey V. Elsukov static struct secasvar *
506fcf59617SAndrey V. Elsukov ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
507fcf59617SAndrey V. Elsukov {
508fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
509fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
510fcf59617SAndrey V. Elsukov 	struct sockaddr_in6 *sin6;
511fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
512fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
513fcf59617SAndrey V. Elsukov 
514fcf59617SAndrey V. Elsukov 	/*
515fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
516fcf59617SAndrey V. Elsukov 	 */
517fcf59617SAndrey V. Elsukov next:
518fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
519fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
520fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
521fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
522fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
523fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
524fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
525fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
526fcf59617SAndrey V. Elsukov 		return (NULL);
527fcf59617SAndrey V. Elsukov 	}
528fcf59617SAndrey V. Elsukov 	/*
529fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
530fcf59617SAndrey V. Elsukov 	 * we only fillin unspecified SA peers for transport
531fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
532fcf59617SAndrey V. Elsukov 	 */
533fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
534fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
535fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
536fcf59617SAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
537fcf59617SAndrey V. Elsukov 		if (saidx->src.sin6.sin6_len == 0) {
538fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->src;
539fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
540fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
541fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
542fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_src;
543fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
544fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
545fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
546fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
547fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_src.s6_addr16[1]);
548fcf59617SAndrey V. Elsukov 			}
549fcf59617SAndrey V. Elsukov 		}
550fcf59617SAndrey V. Elsukov 		if (saidx->dst.sin6.sin6_len == 0) {
551fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->dst;
552fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
553fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
554fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
555fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_dst;
556fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
557fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
558fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
559fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
560fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_dst.s6_addr16[1]);
561fcf59617SAndrey V. Elsukov 			}
562fcf59617SAndrey V. Elsukov 		}
563fcf59617SAndrey V. Elsukov 	} else
564fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
565fcf59617SAndrey V. Elsukov 	/*
566fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
567fcf59617SAndrey V. Elsukov 	 */
568fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
569fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
570fcf59617SAndrey V. Elsukov 		IPSEC6STAT_INC(ips_out_nosa);
571fcf59617SAndrey V. Elsukov 		if (*error != 0)
572fcf59617SAndrey V. Elsukov 			return (NULL);
573fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
574fcf59617SAndrey V. Elsukov 			/*
575fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
576fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
577fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
578fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
579fcf59617SAndrey V. Elsukov 			 */
580fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
581fcf59617SAndrey V. Elsukov 				goto next;
582fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
583fcf59617SAndrey V. Elsukov 		}
584fcf59617SAndrey V. Elsukov 		return (NULL);
585fcf59617SAndrey V. Elsukov 	}
586fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
587fcf59617SAndrey V. Elsukov 	return (sav);
588fcf59617SAndrey V. Elsukov }
589fcf59617SAndrey V. Elsukov 
590fcf59617SAndrey V. Elsukov /*
591fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv6.
592fcf59617SAndrey V. Elsukov  */
593fcf59617SAndrey V. Elsukov static int
5941a01e0e7SAndrey V. Elsukov ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp,
5951a01e0e7SAndrey V. Elsukov     struct inpcb *inp, u_int idx)
596fcf59617SAndrey V. Elsukov {
597fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
598fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
599fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
600fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
601fcf59617SAndrey V. Elsukov 	int error, i, off;
602fcf59617SAndrey V. Elsukov 
603fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
604fcf59617SAndrey V. Elsukov 
605fcf59617SAndrey V. Elsukov 	sav = ipsec6_allocsa(m, sp, &idx, &error);
606fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
607fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
608fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
609fcf59617SAndrey V. Elsukov 			return (error);
610fcf59617SAndrey V. Elsukov 		}
611fcf59617SAndrey V. Elsukov 		goto bad;
612fcf59617SAndrey V. Elsukov 	}
613fcf59617SAndrey V. Elsukov 
614fcf59617SAndrey V. Elsukov 	/* Fix IP length in case if it is not set yet. */
615fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
616fcf59617SAndrey V. Elsukov 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
617fcf59617SAndrey V. Elsukov 
6181a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE);
619fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
620fcf59617SAndrey V. Elsukov 		goto bad;
621fcf59617SAndrey V. Elsukov 
622fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
623fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
624fcf59617SAndrey V. Elsukov 
625fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
626fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
627fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
628fcf59617SAndrey V. Elsukov 	    ((dst->sa.sa_family == AF_INET6) &&
629fcf59617SAndrey V. Elsukov 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
630fcf59617SAndrey V. Elsukov 	     (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
631fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
632fcf59617SAndrey V. Elsukov 			/* No jumbogram support. */
633fcf59617SAndrey V. Elsukov 			error = ENXIO;   /*XXX*/
634fcf59617SAndrey V. Elsukov 			goto bad;
635fcf59617SAndrey V. Elsukov 		}
636fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
637fcf59617SAndrey V. Elsukov 		if (error != 0) {
6387f1f6591SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
6397f1f6591SAndrey V. Elsukov 			    "with error %d\n", __func__, ntohl(sav->spi),
6407f1f6591SAndrey V. Elsukov 			    error));
641fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
642fcf59617SAndrey V. Elsukov 			goto bad;
643fcf59617SAndrey V. Elsukov 		}
6441a01e0e7SAndrey V. Elsukov 		inp = NULL;
645fcf59617SAndrey V. Elsukov 	}
646fcf59617SAndrey V. Elsukov 
6471a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
648fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
649fcf59617SAndrey V. Elsukov 		goto bad;
650fcf59617SAndrey V. Elsukov 
651fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
652fcf59617SAndrey V. Elsukov #ifdef INET
653fcf59617SAndrey V. Elsukov 	case AF_INET:
654fcf59617SAndrey V. Elsukov 		{
655fcf59617SAndrey V. Elsukov 		struct ip *ip;
656fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
657fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
658fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
659fcf59617SAndrey V. Elsukov 		}
660fcf59617SAndrey V. Elsukov 		break;
661fcf59617SAndrey V. Elsukov #endif /* AF_INET */
662fcf59617SAndrey V. Elsukov 	case AF_INET6:
663fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
664fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
665fcf59617SAndrey V. Elsukov 		break;
666fcf59617SAndrey V. Elsukov 	default:
667fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
668fcf59617SAndrey V. Elsukov 				 __func__, dst->sa.sa_family));
669fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
670fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
671fcf59617SAndrey V. Elsukov 		goto bad;
672fcf59617SAndrey V. Elsukov 	}
673fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
674fcf59617SAndrey V. Elsukov 	return (error);
675fcf59617SAndrey V. Elsukov bad:
676fcf59617SAndrey V. Elsukov 	IPSEC6STAT_INC(ips_out_inval);
677fcf59617SAndrey V. Elsukov 	if (m != NULL)
678fcf59617SAndrey V. Elsukov 		m_freem(m);
679fcf59617SAndrey V. Elsukov 	if (sav != NULL)
680fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
681fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
682fcf59617SAndrey V. Elsukov 	return (error);
683fcf59617SAndrey V. Elsukov }
684fcf59617SAndrey V. Elsukov 
685fcf59617SAndrey V. Elsukov int
686fcf59617SAndrey V. Elsukov ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
687fcf59617SAndrey V. Elsukov     struct inpcb *inp)
688fcf59617SAndrey V. Elsukov {
689fcf59617SAndrey V. Elsukov 
6901a01e0e7SAndrey V. Elsukov 	return (ipsec6_perform_request(m, sp, inp, 0));
691fcf59617SAndrey V. Elsukov }
692fcf59617SAndrey V. Elsukov 
693*9dfc8606SBartlomiej Grzesik /*
694*9dfc8606SBartlomiej Grzesik  * IPv6 implementation is based on IPv4 implementation.
695*9dfc8606SBartlomiej Grzesik  */
696*9dfc8606SBartlomiej Grzesik int
697*9dfc8606SBartlomiej Grzesik ipsec6_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding)
698*9dfc8606SBartlomiej Grzesik {
699*9dfc8606SBartlomiej Grzesik 	struct secasvar *sav;
700*9dfc8606SBartlomiej Grzesik 	size_t hlen, pmtu;
701*9dfc8606SBartlomiej Grzesik 	uint32_t idx;
702*9dfc8606SBartlomiej Grzesik 	int error;
703*9dfc8606SBartlomiej Grzesik 
704*9dfc8606SBartlomiej Grzesik 	/*
705*9dfc8606SBartlomiej Grzesik 	 * According to RFC8200 L3 fragmentation is supposed to be done only on
706*9dfc8606SBartlomiej Grzesik 	 * locally generated packets. During L3 forwarding packets that are too
707*9dfc8606SBartlomiej Grzesik 	 * big are always supposed to be dropped, with an ICMPv6 packet being
708*9dfc8606SBartlomiej Grzesik 	 * sent back.
709*9dfc8606SBartlomiej Grzesik 	 */
710*9dfc8606SBartlomiej Grzesik 	if (!forwarding)
711*9dfc8606SBartlomiej Grzesik 		return (0);
712*9dfc8606SBartlomiej Grzesik 
713*9dfc8606SBartlomiej Grzesik 	idx = sp->tcount - 1;
714*9dfc8606SBartlomiej Grzesik 	sav = ipsec6_allocsa(m, sp, &idx, &error);
715*9dfc8606SBartlomiej Grzesik 	if (sav == NULL) {
716*9dfc8606SBartlomiej Grzesik 		key_freesp(&sp);
717*9dfc8606SBartlomiej Grzesik 		/*
718*9dfc8606SBartlomiej Grzesik 		 * No matching SA was found and SADB_ACQUIRE message was generated.
719*9dfc8606SBartlomiej Grzesik 		 * Since we have matched a SP to this packet drop it silently.
720*9dfc8606SBartlomiej Grzesik 		 */
721*9dfc8606SBartlomiej Grzesik 		if (error == 0)
722*9dfc8606SBartlomiej Grzesik 			error = EINPROGRESS;
723*9dfc8606SBartlomiej Grzesik 		if (error != EJUSTRETURN)
724*9dfc8606SBartlomiej Grzesik 			m_freem(m);
725*9dfc8606SBartlomiej Grzesik 
726*9dfc8606SBartlomiej Grzesik 		return (error);
727*9dfc8606SBartlomiej Grzesik 	}
728*9dfc8606SBartlomiej Grzesik 
729*9dfc8606SBartlomiej Grzesik 	pmtu = ipsec_get_pmtu(sav);
730*9dfc8606SBartlomiej Grzesik 	if (pmtu == 0) {
731*9dfc8606SBartlomiej Grzesik 		key_freesav(&sav);
732*9dfc8606SBartlomiej Grzesik 		return (0);
733*9dfc8606SBartlomiej Grzesik 	}
734*9dfc8606SBartlomiej Grzesik 
735*9dfc8606SBartlomiej Grzesik 	hlen = ipsec_hdrsiz_internal(sp);
736*9dfc8606SBartlomiej Grzesik 	key_freesav(&sav);
737*9dfc8606SBartlomiej Grzesik 
738*9dfc8606SBartlomiej Grzesik 	if (m_length(m, NULL) + hlen > pmtu) {
739*9dfc8606SBartlomiej Grzesik 		/*
740*9dfc8606SBartlomiej Grzesik 		 * If we're forwarding generate ICMPv6 message here,
741*9dfc8606SBartlomiej Grzesik 		 * so that it contains pmtu substracted by header size.
742*9dfc8606SBartlomiej Grzesik 		 * Set error to EINPROGRESS, in order for the frame
743*9dfc8606SBartlomiej Grzesik 		 * to be dropped silently.
744*9dfc8606SBartlomiej Grzesik 		 */
745*9dfc8606SBartlomiej Grzesik 		if (forwarding) {
746*9dfc8606SBartlomiej Grzesik 			if (pmtu > hlen)
747*9dfc8606SBartlomiej Grzesik 				icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, pmtu - hlen);
748*9dfc8606SBartlomiej Grzesik 			else
749*9dfc8606SBartlomiej Grzesik 				m_freem(m);
750*9dfc8606SBartlomiej Grzesik 
751*9dfc8606SBartlomiej Grzesik 			key_freesp(&sp);
752*9dfc8606SBartlomiej Grzesik 			return (EINPROGRESS); /* Pretend that we consumed it. */
753*9dfc8606SBartlomiej Grzesik 		}
754*9dfc8606SBartlomiej Grzesik 	}
755*9dfc8606SBartlomiej Grzesik 
756*9dfc8606SBartlomiej Grzesik 	return (0);
757*9dfc8606SBartlomiej Grzesik }
758*9dfc8606SBartlomiej Grzesik 
759fcf59617SAndrey V. Elsukov static int
760fcf59617SAndrey V. Elsukov ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
761fcf59617SAndrey V. Elsukov {
762fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
763fcf59617SAndrey V. Elsukov 	int error;
764fcf59617SAndrey V. Elsukov 
765fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
76622bbefb2SAndrey V. Elsukov 	sp = ipsec6_checkpolicy(m, inp, &error, !forwarding);
767fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
768fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
769fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
770fcf59617SAndrey V. Elsukov 			m_freem(m);
771fcf59617SAndrey V. Elsukov 			return (EACCES);
772fcf59617SAndrey V. Elsukov 		}
773fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
774fcf59617SAndrey V. Elsukov 	}
775fcf59617SAndrey V. Elsukov 
776fcf59617SAndrey V. Elsukov 	if (!forwarding) {
777fcf59617SAndrey V. Elsukov 		/*
778fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
779fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
780fcf59617SAndrey V. Elsukov 		 */
781fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
7828e9313caSJohn Baldwin 			m = mb_unmapped_to_ext(m);
7838e9313caSJohn Baldwin 			if (m == NULL) {
7848e9313caSJohn Baldwin 				IPSEC6STAT_INC(ips_out_nomem);
7858e9313caSJohn Baldwin 				key_freesp(&sp);
7868e9313caSJohn Baldwin 				return (ENOBUFS);
7878e9313caSJohn Baldwin 			}
788fcf59617SAndrey V. Elsukov 			in6_delayed_cksum(m, m->m_pkthdr.len -
789fcf59617SAndrey V. Elsukov 			    sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
790fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
791fcf59617SAndrey V. Elsukov 		}
79295033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
793fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
7948e9313caSJohn Baldwin 			m = mb_unmapped_to_ext(m);
7958e9313caSJohn Baldwin 			if (m == NULL) {
7968e9313caSJohn Baldwin 				IPSEC6STAT_INC(ips_out_nomem);
7978e9313caSJohn Baldwin 				key_freesp(&sp);
7988e9313caSJohn Baldwin 				return (ENOBUFS);
7998e9313caSJohn Baldwin 			}
800fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
801fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
802fcf59617SAndrey V. Elsukov 		}
803fcf59617SAndrey V. Elsukov #endif
804fcf59617SAndrey V. Elsukov 	}
805*9dfc8606SBartlomiej Grzesik 
806*9dfc8606SBartlomiej Grzesik 	error = ipsec6_check_pmtu(m, sp, forwarding);
807*9dfc8606SBartlomiej Grzesik 	if (error != 0) {
808*9dfc8606SBartlomiej Grzesik 		if (error == EJUSTRETURN)
809*9dfc8606SBartlomiej Grzesik 			return (0);
810*9dfc8606SBartlomiej Grzesik 
811*9dfc8606SBartlomiej Grzesik 		return (error);
812*9dfc8606SBartlomiej Grzesik 	}
813*9dfc8606SBartlomiej Grzesik 
814fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
815fcf59617SAndrey V. Elsukov 	error = ipsec6_process_packet(m, sp, inp);
816fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
817fcf59617SAndrey V. Elsukov 		/*
818fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
819fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
820fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
821fcf59617SAndrey V. Elsukov 		 */
822fcf59617SAndrey V. Elsukov 		return (0);
823fcf59617SAndrey V. Elsukov 	}
824fcf59617SAndrey V. Elsukov 	if (error == 0)
825fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
826fcf59617SAndrey V. Elsukov 	return (error);
827fcf59617SAndrey V. Elsukov }
828fcf59617SAndrey V. Elsukov 
829fcf59617SAndrey V. Elsukov /*
830fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv6.
831fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
832fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
833fcf59617SAndrey V. Elsukov  */
834fcf59617SAndrey V. Elsukov int
835fcf59617SAndrey V. Elsukov ipsec6_output(struct mbuf *m, struct inpcb *inp)
836fcf59617SAndrey V. Elsukov {
837fcf59617SAndrey V. Elsukov 
838fcf59617SAndrey V. Elsukov 	/*
839fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
840fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
841fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
842fcf59617SAndrey V. Elsukov 	 */
843fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
844fcf59617SAndrey V. Elsukov 		return (0);
845fcf59617SAndrey V. Elsukov 
846fcf59617SAndrey V. Elsukov 	return (ipsec6_common_output(m, inp, 0));
847fcf59617SAndrey V. Elsukov }
848fcf59617SAndrey V. Elsukov 
849fcf59617SAndrey V. Elsukov /*
850fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv6.
851fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
852fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
853fcf59617SAndrey V. Elsukov  */
854fcf59617SAndrey V. Elsukov int
855fcf59617SAndrey V. Elsukov ipsec6_forward(struct mbuf *m)
856fcf59617SAndrey V. Elsukov {
857fcf59617SAndrey V. Elsukov 
858fcf59617SAndrey V. Elsukov 	/*
859fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
860fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
861fcf59617SAndrey V. Elsukov 	 */
862fcf59617SAndrey V. Elsukov 	if (ipsec6_in_reject(m, NULL) != 0) {
863fcf59617SAndrey V. Elsukov 		m_freem(m);
864fcf59617SAndrey V. Elsukov 		return (EACCES);
865fcf59617SAndrey V. Elsukov 	}
866fcf59617SAndrey V. Elsukov 	return (ipsec6_common_output(m, NULL, 1));
867fcf59617SAndrey V. Elsukov }
868fcf59617SAndrey V. Elsukov #endif /* INET6 */
869fcf59617SAndrey V. Elsukov 
870fcf59617SAndrey V. Elsukov int
871fcf59617SAndrey V. Elsukov ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
872fcf59617SAndrey V. Elsukov     u_int idx)
873fcf59617SAndrey V. Elsukov {
874f82eb2a6SJohn Baldwin 	struct epoch_tracker et;
875fcf59617SAndrey V. Elsukov 	struct xform_history *xh;
876fcf59617SAndrey V. Elsukov 	struct secasindex *saidx;
877fcf59617SAndrey V. Elsukov 	struct m_tag *mtag;
878fcf59617SAndrey V. Elsukov 	int error;
87988768458SSam Leffler 
88088768458SSam Leffler 	saidx = &sav->sah->saidx;
88188768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
88288768458SSam Leffler #ifdef INET
88388768458SSam Leffler 	case AF_INET:
88488768458SSam Leffler 		/* Fix the header length, for AH processing. */
88588768458SSam Leffler 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
88688768458SSam Leffler 		break;
88788768458SSam Leffler #endif /* INET */
88888768458SSam Leffler #ifdef INET6
88988768458SSam Leffler 	case AF_INET6:
89088768458SSam Leffler 		/* Fix the header length, for AH processing. */
89188768458SSam Leffler 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
89288768458SSam Leffler 			error = ENXIO;
89388768458SSam Leffler 			goto bad;
89488768458SSam Leffler 		}
89588768458SSam Leffler 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
89688768458SSam Leffler 			/* No jumbogram support. */
89788768458SSam Leffler 			error = ENXIO;	/*?*/
89888768458SSam Leffler 			goto bad;
89988768458SSam Leffler 		}
90088768458SSam Leffler 		mtod(m, struct ip6_hdr *)->ip6_plen =
90188768458SSam Leffler 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
90288768458SSam Leffler 		break;
90388768458SSam Leffler #endif /* INET6 */
90488768458SSam Leffler 	default:
9059ffa9677SSam Leffler 		DPRINTF(("%s: unknown protocol family %u\n", __func__,
90688768458SSam Leffler 		    saidx->dst.sa.sa_family));
90788768458SSam Leffler 		error = ENXIO;
90888768458SSam Leffler 		goto bad;
90988768458SSam Leffler 	}
91088768458SSam Leffler 
91188768458SSam Leffler 	/*
912fcf59617SAndrey V. Elsukov 	 * Add a record of what we've done to the packet.
91388768458SSam Leffler 	 */
914fcf59617SAndrey V. Elsukov 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
91588768458SSam Leffler 	if (mtag == NULL) {
9169ffa9677SSam Leffler 		DPRINTF(("%s: could not get packet tag\n", __func__));
91788768458SSam Leffler 		error = ENOMEM;
91888768458SSam Leffler 		goto bad;
91988768458SSam Leffler 	}
92088768458SSam Leffler 
921fcf59617SAndrey V. Elsukov 	xh = (struct xform_history *)(mtag + 1);
922fcf59617SAndrey V. Elsukov 	xh->dst = saidx->dst;
923fcf59617SAndrey V. Elsukov 	xh->proto = saidx->proto;
924fcf59617SAndrey V. Elsukov 	xh->mode = saidx->mode;
925fcf59617SAndrey V. Elsukov 	xh->spi = sav->spi;
92688768458SSam Leffler 	m_tag_prepend(m, mtag);
92788768458SSam Leffler 
92859959de5SErmal Luçi 	key_sa_recordxfer(sav, m);		/* record data transfer */
92959959de5SErmal Luçi 
93088768458SSam Leffler 	/*
93188768458SSam Leffler 	 * If there's another (bundled) SA to apply, do so.
93288768458SSam Leffler 	 * Note that this puts a burden on the kernel stack size.
93388768458SSam Leffler 	 * If this is a problem we'll need to introduce a queue
93488768458SSam Leffler 	 * to set the packet on so we can unwind the stack before
93588768458SSam Leffler 	 * doing further processing.
93688768458SSam Leffler 	 */
937fcf59617SAndrey V. Elsukov 	if (++idx < sp->tcount) {
938db178eb8SBjoern A. Zeeb 		switch (saidx->dst.sa.sa_family) {
939db178eb8SBjoern A. Zeeb #ifdef INET
940db178eb8SBjoern A. Zeeb 		case AF_INET:
941fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
942f9d8f665SAndrey V. Elsukov 			IPSECSTAT_INC(ips_out_bundlesa);
9431a01e0e7SAndrey V. Elsukov 			return (ipsec4_perform_request(m, sp, NULL, idx));
944db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
945db178eb8SBjoern A. Zeeb #endif
946db178eb8SBjoern A. Zeeb #ifdef INET6
947db178eb8SBjoern A. Zeeb 		case AF_INET6:
948fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
949f9d8f665SAndrey V. Elsukov 			IPSEC6STAT_INC(ips_out_bundlesa);
9501a01e0e7SAndrey V. Elsukov 			return (ipsec6_perform_request(m, sp, NULL, idx));
951db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
952db178eb8SBjoern A. Zeeb #endif /* INET6 */
953db178eb8SBjoern A. Zeeb 		default:
954db178eb8SBjoern A. Zeeb 			DPRINTF(("%s: unknown protocol family %u\n", __func__,
955db178eb8SBjoern A. Zeeb 			    saidx->dst.sa.sa_family));
956fcf59617SAndrey V. Elsukov 			error = EPFNOSUPPORT;
957db178eb8SBjoern A. Zeeb 			goto bad;
958db178eb8SBjoern A. Zeeb 		}
95988768458SSam Leffler 	}
96088768458SSam Leffler 
961fcf59617SAndrey V. Elsukov 	key_freesp(&sp), sp = NULL;	/* Release reference to SP */
962fcf59617SAndrey V. Elsukov #ifdef INET
963fcf59617SAndrey V. Elsukov 	/*
964fcf59617SAndrey V. Elsukov 	 * Do UDP encapsulation if SA requires it.
965fcf59617SAndrey V. Elsukov 	 */
966fcf59617SAndrey V. Elsukov 	if (sav->natt != NULL) {
967fcf59617SAndrey V. Elsukov 		error = udp_ipsec_output(m, sav);
968fcf59617SAndrey V. Elsukov 		if (error != 0)
969fcf59617SAndrey V. Elsukov 			goto bad;
970fcf59617SAndrey V. Elsukov 	}
971fcf59617SAndrey V. Elsukov #endif /* INET */
97288768458SSam Leffler 	/*
97388768458SSam Leffler 	 * We're done with IPsec processing, transmit the packet using the
9746508929bSAndrey V. Elsukov 	 * appropriate network protocol (IP or IPv6).
97588768458SSam Leffler 	 */
976f82eb2a6SJohn Baldwin 	NET_EPOCH_ENTER(et);
97788768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
97888768458SSam Leffler #ifdef INET
97988768458SSam Leffler 	case AF_INET:
980fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
981f82eb2a6SJohn Baldwin 		error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
982f82eb2a6SJohn Baldwin 		break;
98388768458SSam Leffler #endif /* INET */
98488768458SSam Leffler #ifdef INET6
98588768458SSam Leffler 	case AF_INET6:
986fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
987f82eb2a6SJohn Baldwin 		error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
988f82eb2a6SJohn Baldwin 		break;
98988768458SSam Leffler #endif /* INET6 */
990f82eb2a6SJohn Baldwin 	default:
99188768458SSam Leffler 		panic("ipsec_process_done");
992f82eb2a6SJohn Baldwin 	}
993f82eb2a6SJohn Baldwin 	NET_EPOCH_EXIT(et);
994f82eb2a6SJohn Baldwin 	return (error);
99588768458SSam Leffler bad:
99688768458SSam Leffler 	m_freem(m);
997fcf59617SAndrey V. Elsukov 	key_freesav(&sav);
998fcf59617SAndrey V. Elsukov 	if (sp != NULL)
999fcf59617SAndrey V. Elsukov 		key_freesp(&sp);
100088768458SSam Leffler 	return (error);
100188768458SSam Leffler }
100288768458SSam Leffler 
1003fcf59617SAndrey V. Elsukov /*
1004fcf59617SAndrey V. Elsukov  * ipsec_prepend() is optimized version of M_PREPEND().
1005fcf59617SAndrey V. Elsukov  * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
1006fcf59617SAndrey V. Elsukov  * It is expected that after IP encapsulation some IPsec transform will
1007fcf59617SAndrey V. Elsukov  * be performed. Each IPsec transform inserts its variable length header
1008fcf59617SAndrey V. Elsukov  * just after outer IP header using m_makespace(). If given mbuf has not
1009fcf59617SAndrey V. Elsukov  * enough free space at the beginning, we allocate new mbuf and reserve
1010fcf59617SAndrey V. Elsukov  * some space at the beginning and at the end.
1011fcf59617SAndrey V. Elsukov  * This helps avoid allocating of new mbuf and data copying in m_makespace(),
1012fcf59617SAndrey V. Elsukov  * we place outer header in the middle of mbuf's data with reserved leading
1013fcf59617SAndrey V. Elsukov  * and trailing space:
1014fcf59617SAndrey V. Elsukov  *	[ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
1015fcf59617SAndrey V. Elsukov  * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
1016fcf59617SAndrey V. Elsukov  * be used to inject AH/ESP/IPCOMP header.
1017fcf59617SAndrey V. Elsukov  */
1018fcf59617SAndrey V. Elsukov #define	IPSEC_TRAILINGSPACE	(sizeof(struct udphdr) +/* NAT-T */	\
1019fcf59617SAndrey V. Elsukov     max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN,	/* ESP + IV */	\
1020fcf59617SAndrey V. Elsukov 	sizeof(struct newah) + HASH_MAX_LEN		/* AH + ICV */))
1021fcf59617SAndrey V. Elsukov static struct mbuf *
1022fcf59617SAndrey V. Elsukov ipsec_prepend(struct mbuf *m, int len, int how)
102388768458SSam Leffler {
1024fcf59617SAndrey V. Elsukov 	struct mbuf *n;
102588768458SSam Leffler 
1026fcf59617SAndrey V. Elsukov 	M_ASSERTPKTHDR(m);
1027fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(len < MHLEN, ("wrong length"));
1028fcf59617SAndrey V. Elsukov 	if (M_LEADINGSPACE(m) >= len) {
1029fcf59617SAndrey V. Elsukov 		/* No need to allocate new mbuf. */
1030fcf59617SAndrey V. Elsukov 		m->m_data -= len;
1031fcf59617SAndrey V. Elsukov 		m->m_len += len;
1032fcf59617SAndrey V. Elsukov 		m->m_pkthdr.len += len;
1033fcf59617SAndrey V. Elsukov 		return (m);
103488768458SSam Leffler 	}
1035fcf59617SAndrey V. Elsukov 	n = m_gethdr(how, m->m_type);
1036fcf59617SAndrey V. Elsukov 	if (n == NULL) {
1037fcf59617SAndrey V. Elsukov 		m_freem(m);
1038fcf59617SAndrey V. Elsukov 		return (NULL);
103988768458SSam Leffler 	}
1040fcf59617SAndrey V. Elsukov 	m_move_pkthdr(n, m);
1041fcf59617SAndrey V. Elsukov 	n->m_next = m;
1042fcf59617SAndrey V. Elsukov 	if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
1043fcf59617SAndrey V. Elsukov 		m_align(n, len + IPSEC_TRAILINGSPACE);
1044fcf59617SAndrey V. Elsukov 	n->m_len = len;
1045fcf59617SAndrey V. Elsukov 	n->m_pkthdr.len += len;
1046fcf59617SAndrey V. Elsukov 	return (n);
104788768458SSam Leffler }
104888768458SSam Leffler 
1049*9dfc8606SBartlomiej Grzesik static size_t
1050*9dfc8606SBartlomiej Grzesik ipsec_get_pmtu(struct secasvar *sav)
1051*9dfc8606SBartlomiej Grzesik {
1052*9dfc8606SBartlomiej Grzesik 	union sockaddr_union *dst;
1053*9dfc8606SBartlomiej Grzesik 	struct in_conninfo inc;
1054*9dfc8606SBartlomiej Grzesik 	size_t pmtu;
1055*9dfc8606SBartlomiej Grzesik 
1056*9dfc8606SBartlomiej Grzesik 	dst = &sav->sah->saidx.dst;
1057*9dfc8606SBartlomiej Grzesik 	memset(&inc, 0, sizeof(inc));
1058*9dfc8606SBartlomiej Grzesik 
1059*9dfc8606SBartlomiej Grzesik 	switch (dst->sa.sa_family) {
1060*9dfc8606SBartlomiej Grzesik #ifdef INET
1061*9dfc8606SBartlomiej Grzesik 	case AF_INET:
1062*9dfc8606SBartlomiej Grzesik 		inc.inc_faddr = satosin(&dst->sa)->sin_addr;
1063*9dfc8606SBartlomiej Grzesik 		break;
1064*9dfc8606SBartlomiej Grzesik #endif
1065*9dfc8606SBartlomiej Grzesik #ifdef INET6
1066*9dfc8606SBartlomiej Grzesik 	case AF_INET6:
1067*9dfc8606SBartlomiej Grzesik 		inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr;
1068*9dfc8606SBartlomiej Grzesik 		inc.inc_flags |= INC_ISIPV6;
1069*9dfc8606SBartlomiej Grzesik 		break;
1070*9dfc8606SBartlomiej Grzesik #endif
1071*9dfc8606SBartlomiej Grzesik 	default:
1072*9dfc8606SBartlomiej Grzesik 		return (0);
1073*9dfc8606SBartlomiej Grzesik 	}
1074*9dfc8606SBartlomiej Grzesik 
1075*9dfc8606SBartlomiej Grzesik 	pmtu = tcp_hc_getmtu(&inc);
1076*9dfc8606SBartlomiej Grzesik 	if (pmtu != 0)
1077*9dfc8606SBartlomiej Grzesik 		return (pmtu);
1078*9dfc8606SBartlomiej Grzesik 
1079*9dfc8606SBartlomiej Grzesik 	/* No entry in hostcache. Assume that PMTU is equal to link's MTU */
1080*9dfc8606SBartlomiej Grzesik 	switch (dst->sa.sa_family) {
1081*9dfc8606SBartlomiej Grzesik #ifdef INET
1082*9dfc8606SBartlomiej Grzesik 	case AF_INET:
1083*9dfc8606SBartlomiej Grzesik 		pmtu = tcp_maxmtu(&inc, NULL);
1084*9dfc8606SBartlomiej Grzesik 		break;
1085*9dfc8606SBartlomiej Grzesik #endif
1086*9dfc8606SBartlomiej Grzesik #ifdef INET6
1087*9dfc8606SBartlomiej Grzesik 	case AF_INET6:
1088*9dfc8606SBartlomiej Grzesik 		pmtu = tcp_maxmtu6(&inc, NULL);
1089*9dfc8606SBartlomiej Grzesik 		break;
1090*9dfc8606SBartlomiej Grzesik #endif
1091*9dfc8606SBartlomiej Grzesik 	default:
1092*9dfc8606SBartlomiej Grzesik 		return (0);
1093*9dfc8606SBartlomiej Grzesik 	}
1094*9dfc8606SBartlomiej Grzesik 	if (pmtu == 0)
1095*9dfc8606SBartlomiej Grzesik 		return (0);
1096*9dfc8606SBartlomiej Grzesik 
1097*9dfc8606SBartlomiej Grzesik 	tcp_hc_updatemtu(&inc, pmtu);
1098*9dfc8606SBartlomiej Grzesik 
1099*9dfc8606SBartlomiej Grzesik 	return (pmtu);
1100*9dfc8606SBartlomiej Grzesik }
1101*9dfc8606SBartlomiej Grzesik 
110261f37615SAndrey V. Elsukov static int
110361f37615SAndrey V. Elsukov ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
110461f37615SAndrey V. Elsukov {
110561f37615SAndrey V. Elsukov #ifdef INET6
110661f37615SAndrey V. Elsukov 	struct ip6_hdr *ip6;
110761f37615SAndrey V. Elsukov #endif
110861f37615SAndrey V. Elsukov 	struct ip *ip;
110961f37615SAndrey V. Elsukov 	int setdf;
111061f37615SAndrey V. Elsukov 	uint8_t itos, proto;
111161f37615SAndrey V. Elsukov 
111261f37615SAndrey V. Elsukov 	ip = mtod(*mp, struct ip *);
111361f37615SAndrey V. Elsukov 	switch (ip->ip_v) {
111461f37615SAndrey V. Elsukov #ifdef INET
111561f37615SAndrey V. Elsukov 	case IPVERSION:
111661f37615SAndrey V. Elsukov 		proto = IPPROTO_IPIP;
111761f37615SAndrey V. Elsukov 		/*
111861f37615SAndrey V. Elsukov 		 * Collect IP_DF state from the inner header
111961f37615SAndrey V. Elsukov 		 * and honor system-wide control of how to handle it.
112061f37615SAndrey V. Elsukov 		 */
112161f37615SAndrey V. Elsukov 		switch (V_ip4_ipsec_dfbit) {
112261f37615SAndrey V. Elsukov 		case 0:	/* clear in outer header */
112361f37615SAndrey V. Elsukov 		case 1:	/* set in outer header */
112461f37615SAndrey V. Elsukov 			setdf = V_ip4_ipsec_dfbit;
112561f37615SAndrey V. Elsukov 			break;
112661f37615SAndrey V. Elsukov 		default:/* propagate to outer header */
11276f814d0eSAndrey V. Elsukov 			setdf = (ip->ip_off & htons(IP_DF)) != 0;
112861f37615SAndrey V. Elsukov 		}
112961f37615SAndrey V. Elsukov 		itos = ip->ip_tos;
113061f37615SAndrey V. Elsukov 		break;
113161f37615SAndrey V. Elsukov #endif
113261f37615SAndrey V. Elsukov #ifdef INET6
113361f37615SAndrey V. Elsukov 	case (IPV6_VERSION >> 4):
113461f37615SAndrey V. Elsukov 		proto = IPPROTO_IPV6;
113561f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
113661f37615SAndrey V. Elsukov 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
113761f37615SAndrey V. Elsukov 		setdf = V_ip4_ipsec_dfbit ? 1: 0;
113861f37615SAndrey V. Elsukov 		/* scoped address handling */
113961f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_src);
114061f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_dst);
114161f37615SAndrey V. Elsukov 		break;
114261f37615SAndrey V. Elsukov #endif
114361f37615SAndrey V. Elsukov 	default:
114461f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
114561f37615SAndrey V. Elsukov 	}
114661f37615SAndrey V. Elsukov 	switch (saidx->dst.sa.sa_family) {
114761f37615SAndrey V. Elsukov #ifdef INET
114861f37615SAndrey V. Elsukov 	case AF_INET:
114961f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET ||
115061f37615SAndrey V. Elsukov 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
115161f37615SAndrey V. Elsukov 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
115261f37615SAndrey V. Elsukov 			return (EINVAL);
1153fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
115461f37615SAndrey V. Elsukov 		if (*mp == NULL)
115561f37615SAndrey V. Elsukov 			return (ENOBUFS);
115661f37615SAndrey V. Elsukov 		ip = mtod(*mp, struct ip *);
115761f37615SAndrey V. Elsukov 		ip->ip_v = IPVERSION;
115861f37615SAndrey V. Elsukov 		ip->ip_hl = sizeof(struct ip) >> 2;
115961f37615SAndrey V. Elsukov 		ip->ip_p = proto;
116061f37615SAndrey V. Elsukov 		ip->ip_len = htons((*mp)->m_pkthdr.len);
116161f37615SAndrey V. Elsukov 		ip->ip_ttl = V_ip_defttl;
116261f37615SAndrey V. Elsukov 		ip->ip_sum = 0;
116361f37615SAndrey V. Elsukov 		ip->ip_off = setdf ? htons(IP_DF): 0;
116461f37615SAndrey V. Elsukov 		ip->ip_src = saidx->src.sin.sin_addr;
116561f37615SAndrey V. Elsukov 		ip->ip_dst = saidx->dst.sin.sin_addr;
116661f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
116761f37615SAndrey V. Elsukov 		ip_fillid(ip);
116861f37615SAndrey V. Elsukov 		break;
116961f37615SAndrey V. Elsukov #endif /* INET */
117061f37615SAndrey V. Elsukov #ifdef INET6
117161f37615SAndrey V. Elsukov 	case AF_INET6:
117261f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET6 ||
117361f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
117461f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
117561f37615SAndrey V. Elsukov 			return (EINVAL);
1176fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
117761f37615SAndrey V. Elsukov 		if (*mp == NULL)
117861f37615SAndrey V. Elsukov 			return (ENOBUFS);
117961f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
118061f37615SAndrey V. Elsukov 		ip6->ip6_flow = 0;
118161f37615SAndrey V. Elsukov 		ip6->ip6_vfc = IPV6_VERSION;
118261f37615SAndrey V. Elsukov 		ip6->ip6_hlim = V_ip6_defhlim;
118361f37615SAndrey V. Elsukov 		ip6->ip6_nxt = proto;
118461f37615SAndrey V. Elsukov 		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
11851ae800e7SAndrey V. Elsukov 		/* For link-local address embed scope zone id */
11861ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
11871ae800e7SAndrey V. Elsukov 			ip6->ip6_dst.s6_addr16[1] =
11881ae800e7SAndrey V. Elsukov 			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
118961f37615SAndrey V. Elsukov 		ip6->ip6_src = saidx->src.sin6.sin6_addr;
11901ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
11911ae800e7SAndrey V. Elsukov 			ip6->ip6_src.s6_addr16[1] =
11921ae800e7SAndrey V. Elsukov 			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
119361f37615SAndrey V. Elsukov 		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
119461f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
119561f37615SAndrey V. Elsukov 		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
119661f37615SAndrey V. Elsukov 		break;
119761f37615SAndrey V. Elsukov #endif /* INET6 */
119861f37615SAndrey V. Elsukov 	default:
119961f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
120061f37615SAndrey V. Elsukov 	}
1201fcf59617SAndrey V. Elsukov 	(*mp)->m_flags &= ~(M_BCAST | M_MCAST);
120261f37615SAndrey V. Elsukov 	return (0);
120361f37615SAndrey V. Elsukov }
1204