xref: /freebsd/sys/netipsec/ipsec_output.c (revision de1da299daaa4d26f5a4aba733d9b2880dc0be59)
1aaea26efSSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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  */
2988768458SSam Leffler 
3088768458SSam Leffler /*
3188768458SSam Leffler  * IPsec output processing.
3288768458SSam Leffler  */
3388768458SSam Leffler #include "opt_inet.h"
3488768458SSam Leffler #include "opt_inet6.h"
3588768458SSam Leffler #include "opt_ipsec.h"
36fcf59617SAndrey V. Elsukov #include "opt_sctp.h"
3788768458SSam Leffler 
3888768458SSam Leffler #include <sys/param.h>
3988768458SSam Leffler #include <sys/systm.h>
4088768458SSam Leffler #include <sys/mbuf.h>
4188768458SSam Leffler #include <sys/domain.h>
4288768458SSam Leffler #include <sys/protosw.h>
4388768458SSam Leffler #include <sys/socket.h>
4488768458SSam Leffler #include <sys/errno.h>
45ef91a976SAndrey V. Elsukov #include <sys/hhook.h>
4688768458SSam Leffler #include <sys/syslog.h>
4788768458SSam Leffler 
4888768458SSam Leffler #include <net/if.h>
49ef91a976SAndrey V. Elsukov #include <net/if_enc.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
51eddfbb76SRobert Watson #include <net/vnet.h>
5288768458SSam Leffler 
5388768458SSam Leffler #include <netinet/in.h>
54e68b3792SGleb Smirnoff #include <netinet/in_pcb.h>
5588768458SSam Leffler #include <netinet/in_systm.h>
5688768458SSam Leffler #include <netinet/ip.h>
5788768458SSam Leffler #include <netinet/ip_var.h>
5888768458SSam Leffler #include <netinet/in_var.h>
5988768458SSam Leffler #include <netinet/ip_ecn.h>
6088768458SSam Leffler #ifdef INET6
6188768458SSam Leffler #include <netinet6/ip6_ecn.h>
6288768458SSam Leffler #endif
636b66194bSKornel Duleba #include <netinet/ip_icmp.h>
646b66194bSKornel Duleba #include <netinet/tcp_var.h>
6588768458SSam Leffler 
6688768458SSam Leffler #include <netinet/ip6.h>
6788768458SSam Leffler #ifdef INET6
6888768458SSam Leffler #include <netinet6/ip6_var.h>
6961f37615SAndrey V. Elsukov #include <netinet6/scope6_var.h>
7088768458SSam Leffler #endif
7188768458SSam Leffler #include <netinet/in_pcb.h>
7288768458SSam Leffler #ifdef INET6
7388768458SSam Leffler #include <netinet/icmp6.h>
7488768458SSam Leffler #endif
7595033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
76fcf59617SAndrey V. Elsukov #include <netinet/sctp_crc32.h>
77fcf59617SAndrey V. Elsukov #endif
7888768458SSam Leffler 
79fcf59617SAndrey V. Elsukov #include <netinet/udp.h>
80fcf59617SAndrey V. Elsukov #include <netipsec/ah.h>
81fcf59617SAndrey V. Elsukov #include <netipsec/esp.h>
8288768458SSam Leffler #include <netipsec/ipsec.h>
8388768458SSam Leffler #ifdef INET6
8488768458SSam Leffler #include <netipsec/ipsec6.h>
8588768458SSam Leffler #endif
86809fef29SGleb Smirnoff #include <netipsec/ipsec_support.h>
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);
1099dfc8606SBartlomiej Grzesik static size_t ipsec_get_pmtu(struct secasvar *sav);
110fcf59617SAndrey V. Elsukov 
111fcf59617SAndrey V. Elsukov #ifdef INET
112fcf59617SAndrey V. Elsukov static struct secasvar *
113*de1da299SKonstantin Belousov ipsec4_allocsa(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
114*de1da299SKonstantin Belousov     u_int *pidx, int *error)
115fcf59617SAndrey V. Elsukov {
116fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
117fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
118fcf59617SAndrey V. Elsukov 	struct sockaddr_in *sin;
119fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
120fcf59617SAndrey V. Elsukov 	struct ip *ip;
121fcf59617SAndrey V. Elsukov 
122fcf59617SAndrey V. Elsukov 	/*
123fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
124fcf59617SAndrey V. Elsukov 	 */
125fcf59617SAndrey V. Elsukov next:
126fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
127fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
128fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
129fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
130fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
131fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
132fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
133fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
134fcf59617SAndrey V. Elsukov 		return (NULL);
135fcf59617SAndrey V. Elsukov 	}
136fcf59617SAndrey V. Elsukov 	/*
137fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
138fcf59617SAndrey V. Elsukov 	 * we only initialize unspecified SA peers for transport
139fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
140fcf59617SAndrey V. Elsukov 	 */
141fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
142fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
143fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
144fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
145fcf59617SAndrey V. Elsukov 		if (saidx->src.sa.sa_len == 0) {
146fcf59617SAndrey V. Elsukov 			sin = &saidx->src.sin;
147fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
148fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
149fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
150fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_src;
151fcf59617SAndrey V. Elsukov 		}
152fcf59617SAndrey V. Elsukov 		if (saidx->dst.sa.sa_len == 0) {
153fcf59617SAndrey V. Elsukov 			sin = &saidx->dst.sin;
154fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
155fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
156fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
157fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_dst;
158fcf59617SAndrey V. Elsukov 		}
159fcf59617SAndrey V. Elsukov 	} else
160fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
161fcf59617SAndrey V. Elsukov 	/*
162fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
163fcf59617SAndrey V. Elsukov 	 */
164fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
165fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
166fcf59617SAndrey V. Elsukov 		IPSECSTAT_INC(ips_out_nosa);
167fcf59617SAndrey V. Elsukov 		if (*error != 0)
168fcf59617SAndrey V. Elsukov 			return (NULL);
169fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
170fcf59617SAndrey V. Elsukov 			/*
171fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
172fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
173fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
174fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
175fcf59617SAndrey V. Elsukov 			 */
176fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
177fcf59617SAndrey V. Elsukov 				goto next;
178fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
179fcf59617SAndrey V. Elsukov 		}
180fcf59617SAndrey V. Elsukov 		return (NULL);
181fcf59617SAndrey V. Elsukov 	}
182fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
183fcf59617SAndrey V. Elsukov 	return (sav);
184fcf59617SAndrey V. Elsukov }
185fcf59617SAndrey V. Elsukov 
186fcf59617SAndrey V. Elsukov /*
187fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv4.
188fcf59617SAndrey V. Elsukov  */
189fcf59617SAndrey V. Elsukov static int
190*de1da299SKonstantin Belousov ipsec4_perform_request(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
1911a01e0e7SAndrey V. Elsukov     struct inpcb *inp, u_int idx)
192fcf59617SAndrey V. Elsukov {
193fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
194fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
195fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
196fcf59617SAndrey V. Elsukov 	struct ip *ip;
197fcf59617SAndrey V. Elsukov 	int error, i, off;
198fcf59617SAndrey V. Elsukov 
199fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
200fcf59617SAndrey V. Elsukov 
201fcf59617SAndrey V. Elsukov 	/*
202fcf59617SAndrey V. Elsukov 	 * We hold the reference to SP. Content of SP couldn't be changed.
203fcf59617SAndrey V. Elsukov 	 * Craft secasindex and do lookup for suitable SA.
204fcf59617SAndrey V. Elsukov 	 * Then do encapsulation if needed and call xform's output.
205fcf59617SAndrey V. Elsukov 	 * We need to store SP in the xform callback parameters.
206fcf59617SAndrey V. Elsukov 	 * In xform callback we will extract SP and it can be used to
207fcf59617SAndrey V. Elsukov 	 * determine next transform. At the end of transform we can
208fcf59617SAndrey V. Elsukov 	 * release reference to SP.
209fcf59617SAndrey V. Elsukov 	 */
210*de1da299SKonstantin Belousov 	sav = ipsec4_allocsa(ifp, m, sp, &idx, &error);
211fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
212fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
213fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
214fcf59617SAndrey V. Elsukov 			return (error);
215fcf59617SAndrey V. Elsukov 		}
216fcf59617SAndrey V. Elsukov 		goto bad;
217fcf59617SAndrey V. Elsukov 	}
218fcf59617SAndrey V. Elsukov 	/*
219fcf59617SAndrey V. Elsukov 	 * XXXAE: most likely ip_sum at this point is wrong.
220fcf59617SAndrey V. Elsukov 	 */
2211a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET, IPSEC_ENC_BEFORE);
222fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
223fcf59617SAndrey V. Elsukov 		goto bad;
224fcf59617SAndrey V. Elsukov 
225fcf59617SAndrey V. Elsukov 	ip = mtod(m, struct ip *);
226fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
227fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
228fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
229fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
230fcf59617SAndrey V. Elsukov 	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
231fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
232fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
233fcf59617SAndrey V. Elsukov 		/* Fix IPv4 header checksum and length */
234fcf59617SAndrey V. Elsukov 		ip->ip_len = htons(m->m_pkthdr.len);
235fcf59617SAndrey V. Elsukov 		ip->ip_sum = 0;
236fcf59617SAndrey V. Elsukov 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
237fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
238fcf59617SAndrey V. Elsukov 		if (error != 0) {
2397f1f6591SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
2407f1f6591SAndrey V. Elsukov 			    "with error %d\n", __func__, ntohl(sav->spi),
2417f1f6591SAndrey V. Elsukov 			    error));
242fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
243fcf59617SAndrey V. Elsukov 			goto bad;
244fcf59617SAndrey V. Elsukov 		}
2451a01e0e7SAndrey V. Elsukov 		inp = NULL;
246fcf59617SAndrey V. Elsukov 	}
247fcf59617SAndrey V. Elsukov 
2481a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
249fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
250fcf59617SAndrey V. Elsukov 		goto bad;
251fcf59617SAndrey V. Elsukov 
252fcf59617SAndrey V. Elsukov 	/*
253fcf59617SAndrey V. Elsukov 	 * Dispatch to the appropriate IPsec transform logic.  The
254fcf59617SAndrey V. Elsukov 	 * packet will be returned for transmission after crypto
255fcf59617SAndrey V. Elsukov 	 * processing, etc. are completed.
256fcf59617SAndrey V. Elsukov 	 *
257fcf59617SAndrey V. Elsukov 	 * NB: m & sav are ``passed to caller'' who's responsible for
258fcf59617SAndrey V. Elsukov 	 *     reclaiming their resources.
259fcf59617SAndrey V. Elsukov 	 */
260fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
261fcf59617SAndrey V. Elsukov 	case AF_INET:
262fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
263fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
264fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
265fcf59617SAndrey V. Elsukov 		break;
266fcf59617SAndrey V. Elsukov #ifdef INET6
267fcf59617SAndrey V. Elsukov 	case AF_INET6:
268fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
269fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
270fcf59617SAndrey V. Elsukov 		break;
271fcf59617SAndrey V. Elsukov #endif /* INET6 */
272fcf59617SAndrey V. Elsukov 	default:
273fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
274fcf59617SAndrey V. Elsukov 		    __func__, dst->sa.sa_family));
275fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
276fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
277fcf59617SAndrey V. Elsukov 		goto bad;
278fcf59617SAndrey V. Elsukov 	}
279fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
280fcf59617SAndrey V. Elsukov 	return (error);
281fcf59617SAndrey V. Elsukov bad:
282fcf59617SAndrey V. Elsukov 	IPSECSTAT_INC(ips_out_inval);
283fcf59617SAndrey V. Elsukov 	if (m != NULL)
284fcf59617SAndrey V. Elsukov 		m_freem(m);
285fcf59617SAndrey V. Elsukov 	if (sav != NULL)
286fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
287fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
288fcf59617SAndrey V. Elsukov 	return (error);
289fcf59617SAndrey V. Elsukov }
2907b495c44SVANHULLEBUS Yvan 
29188768458SSam Leffler int
292*de1da299SKonstantin Belousov ipsec4_process_packet(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
293fcf59617SAndrey V. Elsukov     struct inpcb *inp)
29488768458SSam Leffler {
295fcf59617SAndrey V. Elsukov 
296*de1da299SKonstantin Belousov 	return (ipsec4_perform_request(ifp, m, sp, inp, 0));
297fcf59617SAndrey V. Elsukov }
298fcf59617SAndrey V. Elsukov 
2996b66194bSKornel Duleba int
300*de1da299SKonstantin Belousov ipsec4_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
301*de1da299SKonstantin Belousov     int forwarding)
3026b66194bSKornel Duleba {
3036b66194bSKornel Duleba 	struct secasvar *sav;
3046b66194bSKornel Duleba 	struct ip *ip;
3056b66194bSKornel Duleba 	size_t hlen, pmtu;
3066b66194bSKornel Duleba 	uint32_t idx;
3076b66194bSKornel Duleba 	int error;
3086b66194bSKornel Duleba 
3096b66194bSKornel Duleba 	/* Don't check PMTU if the frame won't have DF bit set. */
3106b66194bSKornel Duleba 	if (!V_ip4_ipsec_dfbit)
3116b66194bSKornel Duleba 		return (0);
3126b66194bSKornel Duleba 	if (V_ip4_ipsec_dfbit == 1)
3136b66194bSKornel Duleba 		goto setdf;
3146b66194bSKornel Duleba 
3156b66194bSKornel Duleba 	/* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */
3166b66194bSKornel Duleba 	ip = mtod(m, struct ip *);
3176b66194bSKornel Duleba 	if (!(ip->ip_off & htons(IP_DF)))
3186b66194bSKornel Duleba 		return (0);
3196b66194bSKornel Duleba 
3206b66194bSKornel Duleba setdf:
3216b66194bSKornel Duleba 	idx = sp->tcount - 1;
322*de1da299SKonstantin Belousov 	sav = ipsec4_allocsa(ifp, m, sp, &idx, &error);
3236b66194bSKornel Duleba 	if (sav == NULL) {
3246b66194bSKornel Duleba 		key_freesp(&sp);
325a16771deSKornel Duleba 		/*
326a16771deSKornel Duleba 		 * No matching SA was found and SADB_ACQUIRE message was generated.
327a16771deSKornel Duleba 		 * Since we have matched a SP to this packet drop it silently.
328a16771deSKornel Duleba 		 */
329a16771deSKornel Duleba 		if (error == 0)
330a16771deSKornel Duleba 			error = EINPROGRESS;
3316b66194bSKornel Duleba 		if (error != EJUSTRETURN)
3326b66194bSKornel Duleba 			m_freem(m);
3336b66194bSKornel Duleba 
3346b66194bSKornel Duleba 		return (error);
3356b66194bSKornel Duleba 	}
3366b66194bSKornel Duleba 
3379dfc8606SBartlomiej Grzesik 	pmtu = ipsec_get_pmtu(sav);
338b4220bf3SBartlomiej Grzesik 	if (pmtu == 0) {
3399dfc8606SBartlomiej Grzesik 		key_freesav(&sav);
3406b66194bSKornel Duleba 		return (0);
341b4220bf3SBartlomiej Grzesik 	}
342b4220bf3SBartlomiej Grzesik 
3436b66194bSKornel Duleba 	hlen = ipsec_hdrsiz_internal(sp);
3449dfc8606SBartlomiej Grzesik 	key_freesav(&sav);
3459dfc8606SBartlomiej Grzesik 
3466b66194bSKornel Duleba 	if (m_length(m, NULL) + hlen > pmtu) {
3476b66194bSKornel Duleba 		/*
3486b66194bSKornel Duleba 		 * If we're forwarding generate ICMP message here,
3498deba29cSWojciech Macek 		 * so that it contains pmtu subtracted by header size.
3506b66194bSKornel Duleba 		 * Set error to EINPROGRESS, in order for the frame
3516b66194bSKornel Duleba 		 * to be dropped silently.
3526b66194bSKornel Duleba 		 */
3536b66194bSKornel Duleba 		if (forwarding) {
3546b66194bSKornel Duleba 			if (pmtu > hlen)
3556b66194bSKornel Duleba 				icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
3566b66194bSKornel Duleba 				    0, pmtu - hlen);
3576b66194bSKornel Duleba 			else
3586b66194bSKornel Duleba 				m_freem(m);
3596b66194bSKornel Duleba 
3606b66194bSKornel Duleba 			key_freesp(&sp);
3616b66194bSKornel Duleba 			return (EINPROGRESS); /* Pretend that we consumed it. */
3626b66194bSKornel Duleba 		} else {
3636b66194bSKornel Duleba 			m_freem(m);
3646b66194bSKornel Duleba 			key_freesp(&sp);
3656b66194bSKornel Duleba 			return (EMSGSIZE);
3666b66194bSKornel Duleba 		}
3676b66194bSKornel Duleba 	}
3686b66194bSKornel Duleba 
3696b66194bSKornel Duleba 	return (0);
3706b66194bSKornel Duleba }
3716b66194bSKornel Duleba 
372fcf59617SAndrey V. Elsukov static int
373*de1da299SKonstantin Belousov ipsec4_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
374*de1da299SKonstantin Belousov     int forwarding)
375fcf59617SAndrey V. Elsukov {
376fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
37788768458SSam Leffler 	int error;
37888768458SSam Leffler 
379fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
38022bbefb2SAndrey V. Elsukov 	sp = ipsec4_checkpolicy(m, inp, &error, !forwarding);
381fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
382fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
383fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
384fcf59617SAndrey V. Elsukov 			m_freem(m);
385fcf59617SAndrey V. Elsukov 			return (EACCES);
386fcf59617SAndrey V. Elsukov 		}
387fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
388fcf59617SAndrey V. Elsukov 	}
389fcf59617SAndrey V. Elsukov 
390fcf59617SAndrey V. Elsukov 	/*
391fcf59617SAndrey V. Elsukov 	 * Usually we have to have tunnel mode IPsec security policy
392fcf59617SAndrey V. Elsukov 	 * when we are forwarding a packet. Otherwise we could not handle
393fcf59617SAndrey V. Elsukov 	 * encrypted replies, because they are not destined for us. But
394fcf59617SAndrey V. Elsukov 	 * some users are doing source address translation for forwarded
395fcf59617SAndrey V. Elsukov 	 * packets, and thus, even if they are forwarded, the replies will
396fcf59617SAndrey V. Elsukov 	 * return back to us.
397fcf59617SAndrey V. Elsukov 	 */
398fcf59617SAndrey V. Elsukov 	if (!forwarding) {
399fcf59617SAndrey V. Elsukov 		/*
400fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
401fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
402fcf59617SAndrey V. Elsukov 		 */
403fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
404fcf59617SAndrey V. Elsukov 			in_delayed_cksum(m);
405fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
406fcf59617SAndrey V. Elsukov 		}
40795033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
408fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
4098e9313caSJohn Baldwin 			struct ip *ip;
410fcf59617SAndrey V. Elsukov 
4118e9313caSJohn Baldwin 			ip = mtod(m, struct ip *);
412fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
413fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
414fcf59617SAndrey V. Elsukov 		}
415fcf59617SAndrey V. Elsukov #endif
416fcf59617SAndrey V. Elsukov 	}
417fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
418*de1da299SKonstantin Belousov 	error = ipsec4_check_pmtu(ifp, m, sp, forwarding);
4196b66194bSKornel Duleba 	if (error != 0) {
4206b66194bSKornel Duleba 		if (error == EJUSTRETURN)
4216b66194bSKornel Duleba 			return (0);
4226b66194bSKornel Duleba 
4236b66194bSKornel Duleba 		return (error);
4246b66194bSKornel Duleba 	}
4256b66194bSKornel Duleba 
426*de1da299SKonstantin Belousov 	error = ipsec4_process_packet(ifp, m, sp, inp);
427fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
428fcf59617SAndrey V. Elsukov 		/*
429fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
430fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
431fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
432fcf59617SAndrey V. Elsukov 		 */
433fcf59617SAndrey V. Elsukov 		return (0);
434fcf59617SAndrey V. Elsukov 	}
435fcf59617SAndrey V. Elsukov 	if (error == 0)
436fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
437fcf59617SAndrey V. Elsukov 	return (error);
438fcf59617SAndrey V. Elsukov }
439fcf59617SAndrey V. Elsukov 
440fcf59617SAndrey V. Elsukov /*
441fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv4.
442fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
443fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
444fcf59617SAndrey V. Elsukov  */
445fcf59617SAndrey V. Elsukov int
446*de1da299SKonstantin Belousov ipsec4_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp)
447fcf59617SAndrey V. Elsukov {
448fcf59617SAndrey V. Elsukov 
449fcf59617SAndrey V. Elsukov 	/*
450fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
451fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
452fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
453fcf59617SAndrey V. Elsukov 	 */
454fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
455fcf59617SAndrey V. Elsukov 		return (0);
456fcf59617SAndrey V. Elsukov 
457*de1da299SKonstantin Belousov 	return (ipsec4_common_output(ifp, m, inp, 0));
458fcf59617SAndrey V. Elsukov }
459fcf59617SAndrey V. Elsukov 
460fcf59617SAndrey V. Elsukov /*
461fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv4.
462fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
463fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
464fcf59617SAndrey V. Elsukov  */
465fcf59617SAndrey V. Elsukov int
466fcf59617SAndrey V. Elsukov ipsec4_forward(struct mbuf *m)
467fcf59617SAndrey V. Elsukov {
468fcf59617SAndrey V. Elsukov 
469fcf59617SAndrey V. Elsukov 	/*
470fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
471fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
472fcf59617SAndrey V. Elsukov 	 */
473fcf59617SAndrey V. Elsukov 	if (ipsec4_in_reject(m, NULL) != 0) {
474fcf59617SAndrey V. Elsukov 		m_freem(m);
475fcf59617SAndrey V. Elsukov 		return (EACCES);
476fcf59617SAndrey V. Elsukov 	}
477*de1da299SKonstantin Belousov 	return (ipsec4_common_output(NULL /* XXXKIB */, m, NULL, 1));
478fcf59617SAndrey V. Elsukov }
479fcf59617SAndrey V. Elsukov #endif
480fcf59617SAndrey V. Elsukov 
481fcf59617SAndrey V. Elsukov #ifdef INET6
482fcf59617SAndrey V. Elsukov static int
483fcf59617SAndrey V. Elsukov in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
484fcf59617SAndrey V. Elsukov     const struct in6_addr *ia)
485fcf59617SAndrey V. Elsukov {
486fcf59617SAndrey V. Elsukov 	struct in6_addr ia2;
487fcf59617SAndrey V. Elsukov 
488fcf59617SAndrey V. Elsukov 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
489fcf59617SAndrey V. Elsukov 		memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
490fcf59617SAndrey V. Elsukov 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
491fcf59617SAndrey V. Elsukov 		return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
492fcf59617SAndrey V. Elsukov 	}
493fcf59617SAndrey V. Elsukov 	return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
494fcf59617SAndrey V. Elsukov }
495fcf59617SAndrey V. Elsukov 
496fcf59617SAndrey V. Elsukov static struct secasvar *
497*de1da299SKonstantin Belousov ipsec6_allocsa(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
498*de1da299SKonstantin Belousov     u_int *pidx, int *error)
499fcf59617SAndrey V. Elsukov {
500fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
501fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
502fcf59617SAndrey V. Elsukov 	struct sockaddr_in6 *sin6;
503fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
504fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
505fcf59617SAndrey V. Elsukov 
506fcf59617SAndrey V. Elsukov 	/*
507fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
508fcf59617SAndrey V. Elsukov 	 */
509fcf59617SAndrey V. Elsukov next:
510fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
511fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
512fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
513fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
514fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
515fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
516fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
517fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
518fcf59617SAndrey V. Elsukov 		return (NULL);
519fcf59617SAndrey V. Elsukov 	}
520fcf59617SAndrey V. Elsukov 	/*
521fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
522fcf59617SAndrey V. Elsukov 	 * we only fillin unspecified SA peers for transport
523fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
524fcf59617SAndrey V. Elsukov 	 */
525fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
526fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
527fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
528fcf59617SAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
529fcf59617SAndrey V. Elsukov 		if (saidx->src.sin6.sin6_len == 0) {
530fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->src;
531fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
532fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
533fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
534fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_src;
535fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
536fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
537fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
538fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
539fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_src.s6_addr16[1]);
540fcf59617SAndrey V. Elsukov 			}
541fcf59617SAndrey V. Elsukov 		}
542fcf59617SAndrey V. Elsukov 		if (saidx->dst.sin6.sin6_len == 0) {
543fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->dst;
544fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
545fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
546fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
547fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_dst;
548fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
549fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
550fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
551fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
552fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_dst.s6_addr16[1]);
553fcf59617SAndrey V. Elsukov 			}
554fcf59617SAndrey V. Elsukov 		}
555fcf59617SAndrey V. Elsukov 	} else
556fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
557fcf59617SAndrey V. Elsukov 	/*
558fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
559fcf59617SAndrey V. Elsukov 	 */
560fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
561fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
562fcf59617SAndrey V. Elsukov 		IPSEC6STAT_INC(ips_out_nosa);
563fcf59617SAndrey V. Elsukov 		if (*error != 0)
564fcf59617SAndrey V. Elsukov 			return (NULL);
565fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
566fcf59617SAndrey V. Elsukov 			/*
567fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
568fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
569fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
570fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
571fcf59617SAndrey V. Elsukov 			 */
572fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
573fcf59617SAndrey V. Elsukov 				goto next;
574fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
575fcf59617SAndrey V. Elsukov 		}
576fcf59617SAndrey V. Elsukov 		return (NULL);
577fcf59617SAndrey V. Elsukov 	}
578fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
579fcf59617SAndrey V. Elsukov 	return (sav);
580fcf59617SAndrey V. Elsukov }
581fcf59617SAndrey V. Elsukov 
582fcf59617SAndrey V. Elsukov /*
583fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv6.
584fcf59617SAndrey V. Elsukov  */
585fcf59617SAndrey V. Elsukov static int
586*de1da299SKonstantin Belousov ipsec6_perform_request(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
5871a01e0e7SAndrey V. Elsukov     struct inpcb *inp, u_int idx)
588fcf59617SAndrey V. Elsukov {
589fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
590fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
591fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
592fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
593fcf59617SAndrey V. Elsukov 	int error, i, off;
594fcf59617SAndrey V. Elsukov 
595fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
596fcf59617SAndrey V. Elsukov 
597*de1da299SKonstantin Belousov 	sav = ipsec6_allocsa(ifp, m, sp, &idx, &error);
598fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
599fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
600fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
601fcf59617SAndrey V. Elsukov 			return (error);
602fcf59617SAndrey V. Elsukov 		}
603fcf59617SAndrey V. Elsukov 		goto bad;
604fcf59617SAndrey V. Elsukov 	}
605fcf59617SAndrey V. Elsukov 
606fcf59617SAndrey V. Elsukov 	/* Fix IP length in case if it is not set yet. */
607fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
608fcf59617SAndrey V. Elsukov 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
609fcf59617SAndrey V. Elsukov 
6101a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, AF_INET6, IPSEC_ENC_BEFORE);
611fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
612fcf59617SAndrey V. Elsukov 		goto bad;
613fcf59617SAndrey V. Elsukov 
614fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
615fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
616fcf59617SAndrey V. Elsukov 
617fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
618fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
619fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
620fcf59617SAndrey V. Elsukov 	    ((dst->sa.sa_family == AF_INET6) &&
621fcf59617SAndrey V. Elsukov 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
622fcf59617SAndrey V. Elsukov 	     (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
623fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
624fcf59617SAndrey V. Elsukov 			/* No jumbogram support. */
625fcf59617SAndrey V. Elsukov 			error = ENXIO;   /*XXX*/
626fcf59617SAndrey V. Elsukov 			goto bad;
627fcf59617SAndrey V. Elsukov 		}
628fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
629fcf59617SAndrey V. Elsukov 		if (error != 0) {
6307f1f6591SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SPI 0x%08x failed "
6317f1f6591SAndrey V. Elsukov 			    "with error %d\n", __func__, ntohl(sav->spi),
6327f1f6591SAndrey V. Elsukov 			    error));
633fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
634fcf59617SAndrey V. Elsukov 			goto bad;
635fcf59617SAndrey V. Elsukov 		}
6361a01e0e7SAndrey V. Elsukov 		inp = NULL;
637fcf59617SAndrey V. Elsukov 	}
638fcf59617SAndrey V. Elsukov 
6391a01e0e7SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, inp, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
640fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
641fcf59617SAndrey V. Elsukov 		goto bad;
642fcf59617SAndrey V. Elsukov 
643fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
644fcf59617SAndrey V. Elsukov #ifdef INET
645fcf59617SAndrey V. Elsukov 	case AF_INET:
646fcf59617SAndrey V. Elsukov 		{
647fcf59617SAndrey V. Elsukov 		struct ip *ip;
648fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
649fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
650fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
651fcf59617SAndrey V. Elsukov 		}
652fcf59617SAndrey V. Elsukov 		break;
653fcf59617SAndrey V. Elsukov #endif /* AF_INET */
654fcf59617SAndrey V. Elsukov 	case AF_INET6:
655fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
656fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
657fcf59617SAndrey V. Elsukov 		break;
658fcf59617SAndrey V. Elsukov 	default:
659fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
660fcf59617SAndrey V. Elsukov 				 __func__, dst->sa.sa_family));
661fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
662fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
663fcf59617SAndrey V. Elsukov 		goto bad;
664fcf59617SAndrey V. Elsukov 	}
665fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
666fcf59617SAndrey V. Elsukov 	return (error);
667fcf59617SAndrey V. Elsukov bad:
668fcf59617SAndrey V. Elsukov 	IPSEC6STAT_INC(ips_out_inval);
669fcf59617SAndrey V. Elsukov 	if (m != NULL)
670fcf59617SAndrey V. Elsukov 		m_freem(m);
671fcf59617SAndrey V. Elsukov 	if (sav != NULL)
672fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
673fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
674fcf59617SAndrey V. Elsukov 	return (error);
675fcf59617SAndrey V. Elsukov }
676fcf59617SAndrey V. Elsukov 
677fcf59617SAndrey V. Elsukov int
678*de1da299SKonstantin Belousov ipsec6_process_packet(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
679fcf59617SAndrey V. Elsukov     struct inpcb *inp)
680fcf59617SAndrey V. Elsukov {
681fcf59617SAndrey V. Elsukov 
682*de1da299SKonstantin Belousov 	return (ipsec6_perform_request(ifp, m, sp, inp, 0));
683fcf59617SAndrey V. Elsukov }
684fcf59617SAndrey V. Elsukov 
6859dfc8606SBartlomiej Grzesik /*
6869dfc8606SBartlomiej Grzesik  * IPv6 implementation is based on IPv4 implementation.
6879dfc8606SBartlomiej Grzesik  */
6889dfc8606SBartlomiej Grzesik int
689*de1da299SKonstantin Belousov ipsec6_check_pmtu(struct ifnet *ifp, struct mbuf *m, struct secpolicy *sp,
690*de1da299SKonstantin Belousov     int forwarding)
6919dfc8606SBartlomiej Grzesik {
6929dfc8606SBartlomiej Grzesik 	struct secasvar *sav;
6939dfc8606SBartlomiej Grzesik 	size_t hlen, pmtu;
6949dfc8606SBartlomiej Grzesik 	uint32_t idx;
6959dfc8606SBartlomiej Grzesik 	int error;
6969dfc8606SBartlomiej Grzesik 
6979dfc8606SBartlomiej Grzesik 	/*
6989dfc8606SBartlomiej Grzesik 	 * According to RFC8200 L3 fragmentation is supposed to be done only on
6999dfc8606SBartlomiej Grzesik 	 * locally generated packets. During L3 forwarding packets that are too
7009dfc8606SBartlomiej Grzesik 	 * big are always supposed to be dropped, with an ICMPv6 packet being
7019dfc8606SBartlomiej Grzesik 	 * sent back.
7029dfc8606SBartlomiej Grzesik 	 */
7039dfc8606SBartlomiej Grzesik 	if (!forwarding)
7049dfc8606SBartlomiej Grzesik 		return (0);
7059dfc8606SBartlomiej Grzesik 
7069dfc8606SBartlomiej Grzesik 	idx = sp->tcount - 1;
707*de1da299SKonstantin Belousov 	sav = ipsec6_allocsa(ifp, m, sp, &idx, &error);
7089dfc8606SBartlomiej Grzesik 	if (sav == NULL) {
7099dfc8606SBartlomiej Grzesik 		key_freesp(&sp);
7109dfc8606SBartlomiej Grzesik 		/*
7119dfc8606SBartlomiej Grzesik 		 * No matching SA was found and SADB_ACQUIRE message was generated.
7129dfc8606SBartlomiej Grzesik 		 * Since we have matched a SP to this packet drop it silently.
7139dfc8606SBartlomiej Grzesik 		 */
7149dfc8606SBartlomiej Grzesik 		if (error == 0)
7159dfc8606SBartlomiej Grzesik 			error = EINPROGRESS;
7169dfc8606SBartlomiej Grzesik 		if (error != EJUSTRETURN)
7179dfc8606SBartlomiej Grzesik 			m_freem(m);
7189dfc8606SBartlomiej Grzesik 
7199dfc8606SBartlomiej Grzesik 		return (error);
7209dfc8606SBartlomiej Grzesik 	}
7219dfc8606SBartlomiej Grzesik 
7229dfc8606SBartlomiej Grzesik 	pmtu = ipsec_get_pmtu(sav);
7239dfc8606SBartlomiej Grzesik 	if (pmtu == 0) {
7249dfc8606SBartlomiej Grzesik 		key_freesav(&sav);
7259dfc8606SBartlomiej Grzesik 		return (0);
7269dfc8606SBartlomiej Grzesik 	}
7279dfc8606SBartlomiej Grzesik 
7289dfc8606SBartlomiej Grzesik 	hlen = ipsec_hdrsiz_internal(sp);
7299dfc8606SBartlomiej Grzesik 	key_freesav(&sav);
7309dfc8606SBartlomiej Grzesik 
7319dfc8606SBartlomiej Grzesik 	if (m_length(m, NULL) + hlen > pmtu) {
7329dfc8606SBartlomiej Grzesik 		/*
7339dfc8606SBartlomiej Grzesik 		 * If we're forwarding generate ICMPv6 message here,
734c27214f0SWojciech Macek 		 * so that it contains pmtu subtracted by header size.
7359dfc8606SBartlomiej Grzesik 		 * Set error to EINPROGRESS, in order for the frame
7369dfc8606SBartlomiej Grzesik 		 * to be dropped silently.
7379dfc8606SBartlomiej Grzesik 		 */
7389dfc8606SBartlomiej Grzesik 		if (forwarding) {
7399dfc8606SBartlomiej Grzesik 			if (pmtu > hlen)
7409dfc8606SBartlomiej Grzesik 				icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, pmtu - hlen);
7419dfc8606SBartlomiej Grzesik 			else
7429dfc8606SBartlomiej Grzesik 				m_freem(m);
7439dfc8606SBartlomiej Grzesik 
7449dfc8606SBartlomiej Grzesik 			key_freesp(&sp);
7459dfc8606SBartlomiej Grzesik 			return (EINPROGRESS); /* Pretend that we consumed it. */
7469dfc8606SBartlomiej Grzesik 		}
7479dfc8606SBartlomiej Grzesik 	}
7489dfc8606SBartlomiej Grzesik 
7499dfc8606SBartlomiej Grzesik 	return (0);
7509dfc8606SBartlomiej Grzesik }
7519dfc8606SBartlomiej Grzesik 
752fcf59617SAndrey V. Elsukov static int
753*de1da299SKonstantin Belousov ipsec6_common_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp,
754*de1da299SKonstantin Belousov     int forwarding)
755fcf59617SAndrey V. Elsukov {
756fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
757fcf59617SAndrey V. Elsukov 	int error;
758fcf59617SAndrey V. Elsukov 
759fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
76022bbefb2SAndrey V. Elsukov 	sp = ipsec6_checkpolicy(m, inp, &error, !forwarding);
761fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
762fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
763fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
764fcf59617SAndrey V. Elsukov 			m_freem(m);
765fcf59617SAndrey V. Elsukov 			return (EACCES);
766fcf59617SAndrey V. Elsukov 		}
767fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
768fcf59617SAndrey V. Elsukov 	}
769fcf59617SAndrey V. Elsukov 
770fcf59617SAndrey V. Elsukov 	if (!forwarding) {
771fcf59617SAndrey V. Elsukov 		/*
772fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
773fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
774fcf59617SAndrey V. Elsukov 		 */
775fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
776fcf59617SAndrey V. Elsukov 			in6_delayed_cksum(m, m->m_pkthdr.len -
777fcf59617SAndrey V. Elsukov 			    sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
778fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
779fcf59617SAndrey V. Elsukov 		}
78095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
781fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
782fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
783fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
784fcf59617SAndrey V. Elsukov 		}
785fcf59617SAndrey V. Elsukov #endif
786fcf59617SAndrey V. Elsukov 	}
7879dfc8606SBartlomiej Grzesik 
788*de1da299SKonstantin Belousov 	error = ipsec6_check_pmtu(ifp, m, sp, forwarding);
7899dfc8606SBartlomiej Grzesik 	if (error != 0) {
7909dfc8606SBartlomiej Grzesik 		if (error == EJUSTRETURN)
7919dfc8606SBartlomiej Grzesik 			return (0);
7929dfc8606SBartlomiej Grzesik 
7939dfc8606SBartlomiej Grzesik 		return (error);
7949dfc8606SBartlomiej Grzesik 	}
7959dfc8606SBartlomiej Grzesik 
796fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
797*de1da299SKonstantin Belousov 	error = ipsec6_process_packet(ifp, m, sp, inp);
798fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
799fcf59617SAndrey V. Elsukov 		/*
800fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
801fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
802fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
803fcf59617SAndrey V. Elsukov 		 */
804fcf59617SAndrey V. Elsukov 		return (0);
805fcf59617SAndrey V. Elsukov 	}
806fcf59617SAndrey V. Elsukov 	if (error == 0)
807fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
808fcf59617SAndrey V. Elsukov 	return (error);
809fcf59617SAndrey V. Elsukov }
810fcf59617SAndrey V. Elsukov 
811fcf59617SAndrey V. Elsukov /*
812fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv6.
813fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
814fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
815fcf59617SAndrey V. Elsukov  */
816fcf59617SAndrey V. Elsukov int
817*de1da299SKonstantin Belousov ipsec6_output(struct ifnet *ifp, struct mbuf *m, struct inpcb *inp)
818fcf59617SAndrey V. Elsukov {
819fcf59617SAndrey V. Elsukov 
820fcf59617SAndrey V. Elsukov 	/*
821fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
822fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
823fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
824fcf59617SAndrey V. Elsukov 	 */
825fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
826fcf59617SAndrey V. Elsukov 		return (0);
827fcf59617SAndrey V. Elsukov 
828*de1da299SKonstantin Belousov 	return (ipsec6_common_output(ifp, m, inp, 0));
829fcf59617SAndrey V. Elsukov }
830fcf59617SAndrey V. Elsukov 
831fcf59617SAndrey V. Elsukov /*
832fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv6.
833fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
834fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
835fcf59617SAndrey V. Elsukov  */
836fcf59617SAndrey V. Elsukov int
837fcf59617SAndrey V. Elsukov ipsec6_forward(struct mbuf *m)
838fcf59617SAndrey V. Elsukov {
839fcf59617SAndrey V. Elsukov 
840fcf59617SAndrey V. Elsukov 	/*
841fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
842fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
843fcf59617SAndrey V. Elsukov 	 */
844fcf59617SAndrey V. Elsukov 	if (ipsec6_in_reject(m, NULL) != 0) {
845fcf59617SAndrey V. Elsukov 		m_freem(m);
846fcf59617SAndrey V. Elsukov 		return (EACCES);
847fcf59617SAndrey V. Elsukov 	}
848*de1da299SKonstantin Belousov 	return (ipsec6_common_output(NULL /* XXXKIB */, m, NULL, 1));
849fcf59617SAndrey V. Elsukov }
850fcf59617SAndrey V. Elsukov #endif /* INET6 */
851fcf59617SAndrey V. Elsukov 
852fcf59617SAndrey V. Elsukov int
853fcf59617SAndrey V. Elsukov ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
854fcf59617SAndrey V. Elsukov     u_int idx)
855fcf59617SAndrey V. Elsukov {
856f82eb2a6SJohn Baldwin 	struct epoch_tracker et;
857fcf59617SAndrey V. Elsukov 	struct xform_history *xh;
858fcf59617SAndrey V. Elsukov 	struct secasindex *saidx;
859fcf59617SAndrey V. Elsukov 	struct m_tag *mtag;
860fcf59617SAndrey V. Elsukov 	int error;
86188768458SSam Leffler 
86288768458SSam Leffler 	saidx = &sav->sah->saidx;
86388768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
86488768458SSam Leffler #ifdef INET
86588768458SSam Leffler 	case AF_INET:
86688768458SSam Leffler 		/* Fix the header length, for AH processing. */
86788768458SSam Leffler 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
86888768458SSam Leffler 		break;
86988768458SSam Leffler #endif /* INET */
87088768458SSam Leffler #ifdef INET6
87188768458SSam Leffler 	case AF_INET6:
87288768458SSam Leffler 		/* Fix the header length, for AH processing. */
87388768458SSam Leffler 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
87488768458SSam Leffler 			error = ENXIO;
87588768458SSam Leffler 			goto bad;
87688768458SSam Leffler 		}
87788768458SSam Leffler 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
87888768458SSam Leffler 			/* No jumbogram support. */
87988768458SSam Leffler 			error = ENXIO;	/*?*/
88088768458SSam Leffler 			goto bad;
88188768458SSam Leffler 		}
88288768458SSam Leffler 		mtod(m, struct ip6_hdr *)->ip6_plen =
88388768458SSam Leffler 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
88488768458SSam Leffler 		break;
88588768458SSam Leffler #endif /* INET6 */
88688768458SSam Leffler 	default:
8879ffa9677SSam Leffler 		DPRINTF(("%s: unknown protocol family %u\n", __func__,
88888768458SSam Leffler 		    saidx->dst.sa.sa_family));
88988768458SSam Leffler 		error = ENXIO;
89088768458SSam Leffler 		goto bad;
89188768458SSam Leffler 	}
89288768458SSam Leffler 
89388768458SSam Leffler 	/*
894fcf59617SAndrey V. Elsukov 	 * Add a record of what we've done to the packet.
89588768458SSam Leffler 	 */
896fcf59617SAndrey V. Elsukov 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
89788768458SSam Leffler 	if (mtag == NULL) {
8989ffa9677SSam Leffler 		DPRINTF(("%s: could not get packet tag\n", __func__));
89988768458SSam Leffler 		error = ENOMEM;
90088768458SSam Leffler 		goto bad;
90188768458SSam Leffler 	}
90288768458SSam Leffler 
903fcf59617SAndrey V. Elsukov 	xh = (struct xform_history *)(mtag + 1);
904fcf59617SAndrey V. Elsukov 	xh->dst = saidx->dst;
905fcf59617SAndrey V. Elsukov 	xh->proto = saidx->proto;
906fcf59617SAndrey V. Elsukov 	xh->mode = saidx->mode;
907fcf59617SAndrey V. Elsukov 	xh->spi = sav->spi;
90888768458SSam Leffler 	m_tag_prepend(m, mtag);
90988768458SSam Leffler 
91059959de5SErmal Luçi 	key_sa_recordxfer(sav, m);		/* record data transfer */
91159959de5SErmal Luçi 
91288768458SSam Leffler 	/*
91388768458SSam Leffler 	 * If there's another (bundled) SA to apply, do so.
91488768458SSam Leffler 	 * Note that this puts a burden on the kernel stack size.
91588768458SSam Leffler 	 * If this is a problem we'll need to introduce a queue
91688768458SSam Leffler 	 * to set the packet on so we can unwind the stack before
91788768458SSam Leffler 	 * doing further processing.
91888768458SSam Leffler 	 */
919fcf59617SAndrey V. Elsukov 	if (++idx < sp->tcount) {
920db178eb8SBjoern A. Zeeb 		switch (saidx->dst.sa.sa_family) {
921db178eb8SBjoern A. Zeeb #ifdef INET
922db178eb8SBjoern A. Zeeb 		case AF_INET:
923fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
924f9d8f665SAndrey V. Elsukov 			IPSECSTAT_INC(ips_out_bundlesa);
925*de1da299SKonstantin Belousov 			return (ipsec4_perform_request(NULL, m, sp, NULL,
926*de1da299SKonstantin Belousov 			    idx));
927db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
928db178eb8SBjoern A. Zeeb #endif
929db178eb8SBjoern A. Zeeb #ifdef INET6
930db178eb8SBjoern A. Zeeb 		case AF_INET6:
931fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
932f9d8f665SAndrey V. Elsukov 			IPSEC6STAT_INC(ips_out_bundlesa);
933*de1da299SKonstantin Belousov 			return (ipsec6_perform_request(NULL, m, sp, NULL,
934*de1da299SKonstantin Belousov 			    idx));
935db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
936db178eb8SBjoern A. Zeeb #endif /* INET6 */
937db178eb8SBjoern A. Zeeb 		default:
938db178eb8SBjoern A. Zeeb 			DPRINTF(("%s: unknown protocol family %u\n", __func__,
939db178eb8SBjoern A. Zeeb 			    saidx->dst.sa.sa_family));
940fcf59617SAndrey V. Elsukov 			error = EPFNOSUPPORT;
941db178eb8SBjoern A. Zeeb 			goto bad;
942db178eb8SBjoern A. Zeeb 		}
94388768458SSam Leffler 	}
94488768458SSam Leffler 
945fcf59617SAndrey V. Elsukov 	key_freesp(&sp), sp = NULL;	/* Release reference to SP */
94680044c78SXavier Beaudouin #if defined(INET) || defined(INET6)
947fcf59617SAndrey V. Elsukov 	/*
948fcf59617SAndrey V. Elsukov 	 * Do UDP encapsulation if SA requires it.
949fcf59617SAndrey V. Elsukov 	 */
950fcf59617SAndrey V. Elsukov 	if (sav->natt != NULL) {
951fcf59617SAndrey V. Elsukov 		error = udp_ipsec_output(m, sav);
952fcf59617SAndrey V. Elsukov 		if (error != 0)
953fcf59617SAndrey V. Elsukov 			goto bad;
954fcf59617SAndrey V. Elsukov 	}
95580044c78SXavier Beaudouin #endif /* INET || INET6 */
95688768458SSam Leffler 	/*
95788768458SSam Leffler 	 * We're done with IPsec processing, transmit the packet using the
9586508929bSAndrey V. Elsukov 	 * appropriate network protocol (IP or IPv6).
95988768458SSam Leffler 	 */
960f82eb2a6SJohn Baldwin 	NET_EPOCH_ENTER(et);
96188768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
96288768458SSam Leffler #ifdef INET
96388768458SSam Leffler 	case AF_INET:
964fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
965f82eb2a6SJohn Baldwin 		error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
966f82eb2a6SJohn Baldwin 		break;
96788768458SSam Leffler #endif /* INET */
96888768458SSam Leffler #ifdef INET6
96988768458SSam Leffler 	case AF_INET6:
970fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
971f82eb2a6SJohn Baldwin 		error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
972f82eb2a6SJohn Baldwin 		break;
97388768458SSam Leffler #endif /* INET6 */
974f82eb2a6SJohn Baldwin 	default:
97588768458SSam Leffler 		panic("ipsec_process_done");
976f82eb2a6SJohn Baldwin 	}
977f82eb2a6SJohn Baldwin 	NET_EPOCH_EXIT(et);
978f82eb2a6SJohn Baldwin 	return (error);
97988768458SSam Leffler bad:
98088768458SSam Leffler 	m_freem(m);
981fcf59617SAndrey V. Elsukov 	key_freesav(&sav);
982fcf59617SAndrey V. Elsukov 	if (sp != NULL)
983fcf59617SAndrey V. Elsukov 		key_freesp(&sp);
98488768458SSam Leffler 	return (error);
98588768458SSam Leffler }
98688768458SSam Leffler 
987fcf59617SAndrey V. Elsukov /*
988fcf59617SAndrey V. Elsukov  * ipsec_prepend() is optimized version of M_PREPEND().
989fcf59617SAndrey V. Elsukov  * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
990fcf59617SAndrey V. Elsukov  * It is expected that after IP encapsulation some IPsec transform will
991fcf59617SAndrey V. Elsukov  * be performed. Each IPsec transform inserts its variable length header
992fcf59617SAndrey V. Elsukov  * just after outer IP header using m_makespace(). If given mbuf has not
993fcf59617SAndrey V. Elsukov  * enough free space at the beginning, we allocate new mbuf and reserve
994fcf59617SAndrey V. Elsukov  * some space at the beginning and at the end.
995fcf59617SAndrey V. Elsukov  * This helps avoid allocating of new mbuf and data copying in m_makespace(),
996fcf59617SAndrey V. Elsukov  * we place outer header in the middle of mbuf's data with reserved leading
997fcf59617SAndrey V. Elsukov  * and trailing space:
998fcf59617SAndrey V. Elsukov  *	[ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
999fcf59617SAndrey V. Elsukov  * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
1000fcf59617SAndrey V. Elsukov  * be used to inject AH/ESP/IPCOMP header.
1001fcf59617SAndrey V. Elsukov  */
1002fcf59617SAndrey V. Elsukov #define	IPSEC_TRAILINGSPACE	(sizeof(struct udphdr) +/* NAT-T */	\
1003fcf59617SAndrey V. Elsukov     max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN,	/* ESP + IV */	\
1004fcf59617SAndrey V. Elsukov 	sizeof(struct newah) + HASH_MAX_LEN		/* AH + ICV */))
1005fcf59617SAndrey V. Elsukov static struct mbuf *
1006fcf59617SAndrey V. Elsukov ipsec_prepend(struct mbuf *m, int len, int how)
100788768458SSam Leffler {
1008fcf59617SAndrey V. Elsukov 	struct mbuf *n;
100988768458SSam Leffler 
1010fcf59617SAndrey V. Elsukov 	M_ASSERTPKTHDR(m);
1011fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(len < MHLEN, ("wrong length"));
1012fcf59617SAndrey V. Elsukov 	if (M_LEADINGSPACE(m) >= len) {
1013fcf59617SAndrey V. Elsukov 		/* No need to allocate new mbuf. */
1014fcf59617SAndrey V. Elsukov 		m->m_data -= len;
1015fcf59617SAndrey V. Elsukov 		m->m_len += len;
1016fcf59617SAndrey V. Elsukov 		m->m_pkthdr.len += len;
1017fcf59617SAndrey V. Elsukov 		return (m);
101888768458SSam Leffler 	}
1019fcf59617SAndrey V. Elsukov 	n = m_gethdr(how, m->m_type);
1020fcf59617SAndrey V. Elsukov 	if (n == NULL) {
1021fcf59617SAndrey V. Elsukov 		m_freem(m);
1022fcf59617SAndrey V. Elsukov 		return (NULL);
102388768458SSam Leffler 	}
1024fcf59617SAndrey V. Elsukov 	m_move_pkthdr(n, m);
1025fcf59617SAndrey V. Elsukov 	n->m_next = m;
1026fcf59617SAndrey V. Elsukov 	if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
1027fcf59617SAndrey V. Elsukov 		m_align(n, len + IPSEC_TRAILINGSPACE);
1028fcf59617SAndrey V. Elsukov 	n->m_len = len;
1029fcf59617SAndrey V. Elsukov 	n->m_pkthdr.len += len;
1030fcf59617SAndrey V. Elsukov 	return (n);
103188768458SSam Leffler }
103288768458SSam Leffler 
10339dfc8606SBartlomiej Grzesik static size_t
10349dfc8606SBartlomiej Grzesik ipsec_get_pmtu(struct secasvar *sav)
10359dfc8606SBartlomiej Grzesik {
10369dfc8606SBartlomiej Grzesik 	union sockaddr_union *dst;
10379dfc8606SBartlomiej Grzesik 	struct in_conninfo inc;
10389dfc8606SBartlomiej Grzesik 	size_t pmtu;
10399dfc8606SBartlomiej Grzesik 
10409dfc8606SBartlomiej Grzesik 	dst = &sav->sah->saidx.dst;
10419dfc8606SBartlomiej Grzesik 	memset(&inc, 0, sizeof(inc));
10429dfc8606SBartlomiej Grzesik 
10439dfc8606SBartlomiej Grzesik 	switch (dst->sa.sa_family) {
10449dfc8606SBartlomiej Grzesik #ifdef INET
10459dfc8606SBartlomiej Grzesik 	case AF_INET:
10469dfc8606SBartlomiej Grzesik 		inc.inc_faddr = satosin(&dst->sa)->sin_addr;
10479dfc8606SBartlomiej Grzesik 		break;
10489dfc8606SBartlomiej Grzesik #endif
10499dfc8606SBartlomiej Grzesik #ifdef INET6
10509dfc8606SBartlomiej Grzesik 	case AF_INET6:
10519dfc8606SBartlomiej Grzesik 		inc.inc6_faddr = satosin6(&dst->sa)->sin6_addr;
10529dfc8606SBartlomiej Grzesik 		inc.inc_flags |= INC_ISIPV6;
10539dfc8606SBartlomiej Grzesik 		break;
10549dfc8606SBartlomiej Grzesik #endif
10559dfc8606SBartlomiej Grzesik 	default:
10569dfc8606SBartlomiej Grzesik 		return (0);
10579dfc8606SBartlomiej Grzesik 	}
10589dfc8606SBartlomiej Grzesik 
10599dfc8606SBartlomiej Grzesik 	pmtu = tcp_hc_getmtu(&inc);
10609dfc8606SBartlomiej Grzesik 	if (pmtu != 0)
10619dfc8606SBartlomiej Grzesik 		return (pmtu);
10629dfc8606SBartlomiej Grzesik 
10639dfc8606SBartlomiej Grzesik 	/* No entry in hostcache. Assume that PMTU is equal to link's MTU */
10649dfc8606SBartlomiej Grzesik 	switch (dst->sa.sa_family) {
10659dfc8606SBartlomiej Grzesik #ifdef INET
10669dfc8606SBartlomiej Grzesik 	case AF_INET:
10679dfc8606SBartlomiej Grzesik 		pmtu = tcp_maxmtu(&inc, NULL);
10689dfc8606SBartlomiej Grzesik 		break;
10699dfc8606SBartlomiej Grzesik #endif
10709dfc8606SBartlomiej Grzesik #ifdef INET6
10719dfc8606SBartlomiej Grzesik 	case AF_INET6:
10729dfc8606SBartlomiej Grzesik 		pmtu = tcp_maxmtu6(&inc, NULL);
10739dfc8606SBartlomiej Grzesik 		break;
10749dfc8606SBartlomiej Grzesik #endif
10759dfc8606SBartlomiej Grzesik 	default:
10769dfc8606SBartlomiej Grzesik 		return (0);
10779dfc8606SBartlomiej Grzesik 	}
10789dfc8606SBartlomiej Grzesik 	if (pmtu == 0)
10799dfc8606SBartlomiej Grzesik 		return (0);
10809dfc8606SBartlomiej Grzesik 
10819dfc8606SBartlomiej Grzesik 	tcp_hc_updatemtu(&inc, pmtu);
10829dfc8606SBartlomiej Grzesik 
10839dfc8606SBartlomiej Grzesik 	return (pmtu);
10849dfc8606SBartlomiej Grzesik }
10859dfc8606SBartlomiej Grzesik 
108661f37615SAndrey V. Elsukov static int
108761f37615SAndrey V. Elsukov ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
108861f37615SAndrey V. Elsukov {
108961f37615SAndrey V. Elsukov #ifdef INET6
109061f37615SAndrey V. Elsukov 	struct ip6_hdr *ip6;
109161f37615SAndrey V. Elsukov #endif
109261f37615SAndrey V. Elsukov 	struct ip *ip;
109340461785SJohn Baldwin #ifdef INET
109461f37615SAndrey V. Elsukov 	int setdf;
109540461785SJohn Baldwin #endif
109661f37615SAndrey V. Elsukov 	uint8_t itos, proto;
109761f37615SAndrey V. Elsukov 
109861f37615SAndrey V. Elsukov 	ip = mtod(*mp, struct ip *);
109961f37615SAndrey V. Elsukov 	switch (ip->ip_v) {
110061f37615SAndrey V. Elsukov #ifdef INET
110161f37615SAndrey V. Elsukov 	case IPVERSION:
110261f37615SAndrey V. Elsukov 		proto = IPPROTO_IPIP;
110361f37615SAndrey V. Elsukov 		/*
110461f37615SAndrey V. Elsukov 		 * Collect IP_DF state from the inner header
110561f37615SAndrey V. Elsukov 		 * and honor system-wide control of how to handle it.
110661f37615SAndrey V. Elsukov 		 */
110761f37615SAndrey V. Elsukov 		switch (V_ip4_ipsec_dfbit) {
110861f37615SAndrey V. Elsukov 		case 0:	/* clear in outer header */
110961f37615SAndrey V. Elsukov 		case 1:	/* set in outer header */
111061f37615SAndrey V. Elsukov 			setdf = V_ip4_ipsec_dfbit;
111161f37615SAndrey V. Elsukov 			break;
111261f37615SAndrey V. Elsukov 		default:/* propagate to outer header */
11136f814d0eSAndrey V. Elsukov 			setdf = (ip->ip_off & htons(IP_DF)) != 0;
111461f37615SAndrey V. Elsukov 		}
111561f37615SAndrey V. Elsukov 		itos = ip->ip_tos;
111661f37615SAndrey V. Elsukov 		break;
111761f37615SAndrey V. Elsukov #endif
111861f37615SAndrey V. Elsukov #ifdef INET6
111961f37615SAndrey V. Elsukov 	case (IPV6_VERSION >> 4):
112061f37615SAndrey V. Elsukov 		proto = IPPROTO_IPV6;
112161f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
112261f37615SAndrey V. Elsukov 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
112361f37615SAndrey V. Elsukov 		/* scoped address handling */
112461f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_src);
112561f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_dst);
112661f37615SAndrey V. Elsukov 		break;
112761f37615SAndrey V. Elsukov #endif
112861f37615SAndrey V. Elsukov 	default:
112961f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
113061f37615SAndrey V. Elsukov 	}
113161f37615SAndrey V. Elsukov 	switch (saidx->dst.sa.sa_family) {
113261f37615SAndrey V. Elsukov #ifdef INET
113361f37615SAndrey V. Elsukov 	case AF_INET:
113461f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET ||
113561f37615SAndrey V. Elsukov 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
113661f37615SAndrey V. Elsukov 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
113761f37615SAndrey V. Elsukov 			return (EINVAL);
1138fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
113961f37615SAndrey V. Elsukov 		if (*mp == NULL)
114061f37615SAndrey V. Elsukov 			return (ENOBUFS);
114161f37615SAndrey V. Elsukov 		ip = mtod(*mp, struct ip *);
114261f37615SAndrey V. Elsukov 		ip->ip_v = IPVERSION;
114361f37615SAndrey V. Elsukov 		ip->ip_hl = sizeof(struct ip) >> 2;
114461f37615SAndrey V. Elsukov 		ip->ip_p = proto;
114561f37615SAndrey V. Elsukov 		ip->ip_len = htons((*mp)->m_pkthdr.len);
114661f37615SAndrey V. Elsukov 		ip->ip_ttl = V_ip_defttl;
114761f37615SAndrey V. Elsukov 		ip->ip_sum = 0;
114861f37615SAndrey V. Elsukov 		ip->ip_off = setdf ? htons(IP_DF): 0;
114961f37615SAndrey V. Elsukov 		ip->ip_src = saidx->src.sin.sin_addr;
115061f37615SAndrey V. Elsukov 		ip->ip_dst = saidx->dst.sin.sin_addr;
115161f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
115261f37615SAndrey V. Elsukov 		ip_fillid(ip);
115361f37615SAndrey V. Elsukov 		break;
115461f37615SAndrey V. Elsukov #endif /* INET */
115561f37615SAndrey V. Elsukov #ifdef INET6
115661f37615SAndrey V. Elsukov 	case AF_INET6:
115761f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET6 ||
115861f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
115961f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
116061f37615SAndrey V. Elsukov 			return (EINVAL);
1161fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
116261f37615SAndrey V. Elsukov 		if (*mp == NULL)
116361f37615SAndrey V. Elsukov 			return (ENOBUFS);
116461f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
116561f37615SAndrey V. Elsukov 		ip6->ip6_flow = 0;
116661f37615SAndrey V. Elsukov 		ip6->ip6_vfc = IPV6_VERSION;
116761f37615SAndrey V. Elsukov 		ip6->ip6_hlim = V_ip6_defhlim;
116861f37615SAndrey V. Elsukov 		ip6->ip6_nxt = proto;
116961f37615SAndrey V. Elsukov 		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
11701ae800e7SAndrey V. Elsukov 		/* For link-local address embed scope zone id */
11711ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
11721ae800e7SAndrey V. Elsukov 			ip6->ip6_dst.s6_addr16[1] =
11731ae800e7SAndrey V. Elsukov 			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
117461f37615SAndrey V. Elsukov 		ip6->ip6_src = saidx->src.sin6.sin6_addr;
11751ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
11761ae800e7SAndrey V. Elsukov 			ip6->ip6_src.s6_addr16[1] =
11771ae800e7SAndrey V. Elsukov 			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
117861f37615SAndrey V. Elsukov 		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
117961f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
118061f37615SAndrey V. Elsukov 		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
118161f37615SAndrey V. Elsukov 		break;
118261f37615SAndrey V. Elsukov #endif /* INET6 */
118361f37615SAndrey V. Elsukov 	default:
118461f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
118561f37615SAndrey V. Elsukov 	}
1186fcf59617SAndrey V. Elsukov 	(*mp)->m_flags &= ~(M_BCAST | M_MCAST);
118761f37615SAndrey V. Elsukov 	return (0);
118861f37615SAndrey V. Elsukov }
1189