xref: /freebsd/sys/netipsec/ipsec_output.c (revision fcf596178b5f2be36424ecbc1b6a3224b29c91d2)
1aaea26efSSam Leffler /*-
2aaea26efSSam Leffler  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3*fcf59617SAndrey V. Elsukov  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
4aaea26efSSam Leffler  * All rights reserved.
5aaea26efSSam Leffler  *
6aaea26efSSam Leffler  * Redistribution and use in source and binary forms, with or without
7aaea26efSSam Leffler  * modification, are permitted provided that the following conditions
8aaea26efSSam Leffler  * are met:
9aaea26efSSam Leffler  * 1. Redistributions of source code must retain the above copyright
10aaea26efSSam Leffler  *    notice, this list of conditions and the following disclaimer.
11aaea26efSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
12aaea26efSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
13aaea26efSSam Leffler  *    documentation and/or other materials provided with the distribution.
14aaea26efSSam Leffler  *
15aaea26efSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16aaea26efSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17aaea26efSSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18aaea26efSSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19aaea26efSSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20aaea26efSSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21aaea26efSSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22aaea26efSSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23aaea26efSSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24aaea26efSSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25aaea26efSSam Leffler  * SUCH DAMAGE.
26aaea26efSSam Leffler  *
27aaea26efSSam Leffler  * $FreeBSD$
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"
36*fcf59617SAndrey 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>
5488768458SSam Leffler #include <netinet/in_systm.h>
5588768458SSam Leffler #include <netinet/ip.h>
5688768458SSam Leffler #include <netinet/ip_var.h>
5788768458SSam Leffler #include <netinet/in_var.h>
5888768458SSam Leffler #include <netinet/ip_ecn.h>
5988768458SSam Leffler #ifdef INET6
6088768458SSam Leffler #include <netinet6/ip6_ecn.h>
6188768458SSam Leffler #endif
6288768458SSam Leffler 
6388768458SSam Leffler #include <netinet/ip6.h>
6488768458SSam Leffler #ifdef INET6
6588768458SSam Leffler #include <netinet6/ip6_var.h>
6661f37615SAndrey V. Elsukov #include <netinet6/scope6_var.h>
6788768458SSam Leffler #endif
6888768458SSam Leffler #include <netinet/in_pcb.h>
6988768458SSam Leffler #ifdef INET6
7088768458SSam Leffler #include <netinet/icmp6.h>
7188768458SSam Leffler #endif
72*fcf59617SAndrey V. Elsukov #ifdef SCTP
73*fcf59617SAndrey V. Elsukov #include <netinet/sctp_crc32.h>
74*fcf59617SAndrey V. Elsukov #endif
7588768458SSam Leffler 
76*fcf59617SAndrey V. Elsukov #include <netinet/udp.h>
77*fcf59617SAndrey V. Elsukov #include <netipsec/ah.h>
78*fcf59617SAndrey V. Elsukov #include <netipsec/esp.h>
7988768458SSam Leffler #include <netipsec/ipsec.h>
8088768458SSam Leffler #ifdef INET6
8188768458SSam Leffler #include <netipsec/ipsec6.h>
8288768458SSam Leffler #endif
8388768458SSam Leffler #include <netipsec/ah_var.h>
8488768458SSam Leffler #include <netipsec/esp_var.h>
8588768458SSam Leffler #include <netipsec/ipcomp_var.h>
8688768458SSam Leffler 
8788768458SSam Leffler #include <netipsec/xform.h>
8888768458SSam Leffler 
8988768458SSam Leffler #include <netipsec/key.h>
9088768458SSam Leffler #include <netipsec/keydb.h>
9188768458SSam Leffler #include <netipsec/key_debug.h>
9288768458SSam Leffler 
9388768458SSam Leffler #include <machine/in_cksum.h>
9488768458SSam Leffler 
95*fcf59617SAndrey V. Elsukov #define	IPSEC_OSTAT_INC(proto, name)	do {		\
96*fcf59617SAndrey V. Elsukov 	if ((proto) == IPPROTO_ESP)	\
97*fcf59617SAndrey V. Elsukov 		ESPSTAT_INC(esps_##name);	\
98*fcf59617SAndrey V. Elsukov 	else if ((proto) == IPPROTO_AH)\
99*fcf59617SAndrey V. Elsukov 		AHSTAT_INC(ahs_##name);		\
100*fcf59617SAndrey V. Elsukov 	else					\
101*fcf59617SAndrey V. Elsukov 		IPCOMPSTAT_INC(ipcomps_##name);	\
102*fcf59617SAndrey V. Elsukov } while (0)
103*fcf59617SAndrey V. Elsukov 
104*fcf59617SAndrey V. Elsukov static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
105*fcf59617SAndrey V. Elsukov 
106*fcf59617SAndrey V. Elsukov #ifdef INET
107*fcf59617SAndrey V. Elsukov static struct secasvar *
108*fcf59617SAndrey V. Elsukov ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
109*fcf59617SAndrey V. Elsukov {
110*fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
111*fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
112*fcf59617SAndrey V. Elsukov 	struct sockaddr_in *sin;
113*fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
114*fcf59617SAndrey V. Elsukov 	struct ip *ip;
115*fcf59617SAndrey V. Elsukov 
116*fcf59617SAndrey V. Elsukov 	/*
117*fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
118*fcf59617SAndrey V. Elsukov 	 */
119*fcf59617SAndrey V. Elsukov next:
120*fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
121*fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
122*fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
123*fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
124*fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
125*fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
126*fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
127*fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
128*fcf59617SAndrey V. Elsukov 		return (NULL);
129*fcf59617SAndrey V. Elsukov 	}
130*fcf59617SAndrey V. Elsukov 	/*
131*fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
132*fcf59617SAndrey V. Elsukov 	 * we only initialize unspecified SA peers for transport
133*fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
134*fcf59617SAndrey V. Elsukov 	 */
135*fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
136*fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
137*fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
138*fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
139*fcf59617SAndrey V. Elsukov 		if (saidx->src.sa.sa_len == 0) {
140*fcf59617SAndrey V. Elsukov 			sin = &saidx->src.sin;
141*fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
142*fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
143*fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
144*fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_src;
145*fcf59617SAndrey V. Elsukov 		}
146*fcf59617SAndrey V. Elsukov 		if (saidx->dst.sa.sa_len == 0) {
147*fcf59617SAndrey V. Elsukov 			sin = &saidx->dst.sin;
148*fcf59617SAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
149*fcf59617SAndrey V. Elsukov 			sin->sin_family = AF_INET;
150*fcf59617SAndrey V. Elsukov 			sin->sin_port = IPSEC_PORT_ANY;
151*fcf59617SAndrey V. Elsukov 			sin->sin_addr = ip->ip_dst;
152*fcf59617SAndrey V. Elsukov 		}
153*fcf59617SAndrey V. Elsukov 	} else
154*fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
155*fcf59617SAndrey V. Elsukov 	/*
156*fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
157*fcf59617SAndrey V. Elsukov 	 */
158*fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
159*fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
160*fcf59617SAndrey V. Elsukov 		IPSECSTAT_INC(ips_out_nosa);
161*fcf59617SAndrey V. Elsukov 		if (*error != 0)
162*fcf59617SAndrey V. Elsukov 			return (NULL);
163*fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
164*fcf59617SAndrey V. Elsukov 			/*
165*fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
166*fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
167*fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
168*fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
169*fcf59617SAndrey V. Elsukov 			 */
170*fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
171*fcf59617SAndrey V. Elsukov 				goto next;
172*fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
173*fcf59617SAndrey V. Elsukov 		}
174*fcf59617SAndrey V. Elsukov 		return (NULL);
175*fcf59617SAndrey V. Elsukov 	}
176*fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
177*fcf59617SAndrey V. Elsukov 	return (sav);
178*fcf59617SAndrey V. Elsukov }
179*fcf59617SAndrey V. Elsukov 
180*fcf59617SAndrey V. Elsukov /*
181*fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv4.
182*fcf59617SAndrey V. Elsukov  */
183*fcf59617SAndrey V. Elsukov static int
184*fcf59617SAndrey V. Elsukov ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
185*fcf59617SAndrey V. Elsukov {
186*fcf59617SAndrey V. Elsukov 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
187*fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
188*fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
189*fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
190*fcf59617SAndrey V. Elsukov 	struct ip *ip;
191*fcf59617SAndrey V. Elsukov 	int error, i, off;
192*fcf59617SAndrey V. Elsukov 
193*fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
194*fcf59617SAndrey V. Elsukov 
195*fcf59617SAndrey V. Elsukov 	/*
196*fcf59617SAndrey V. Elsukov 	 * We hold the reference to SP. Content of SP couldn't be changed.
197*fcf59617SAndrey V. Elsukov 	 * Craft secasindex and do lookup for suitable SA.
198*fcf59617SAndrey V. Elsukov 	 * Then do encapsulation if needed and call xform's output.
199*fcf59617SAndrey V. Elsukov 	 * We need to store SP in the xform callback parameters.
200*fcf59617SAndrey V. Elsukov 	 * In xform callback we will extract SP and it can be used to
201*fcf59617SAndrey V. Elsukov 	 * determine next transform. At the end of transform we can
202*fcf59617SAndrey V. Elsukov 	 * release reference to SP.
203*fcf59617SAndrey V. Elsukov 	 */
204*fcf59617SAndrey V. Elsukov 	sav = ipsec4_allocsa(m, sp, &idx, &error);
205*fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
206*fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
207*fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
208*fcf59617SAndrey V. Elsukov 			return (error);
209*fcf59617SAndrey V. Elsukov 		}
210*fcf59617SAndrey V. Elsukov 		goto bad;
211*fcf59617SAndrey V. Elsukov 	}
212*fcf59617SAndrey V. Elsukov 	/*
213*fcf59617SAndrey V. Elsukov 	 * XXXAE: most likely ip_sum at this point is wrong.
214*fcf59617SAndrey V. Elsukov 	 */
215*fcf59617SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET, IPSEC_ENC_BEFORE);
216*fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
217*fcf59617SAndrey V. Elsukov 		goto bad;
218*fcf59617SAndrey V. Elsukov 
219*fcf59617SAndrey V. Elsukov 	ip = mtod(m, struct ip *);
220*fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
221*fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
222*fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
223*fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
224*fcf59617SAndrey V. Elsukov 	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
225*fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
226*fcf59617SAndrey V. Elsukov 	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
227*fcf59617SAndrey V. Elsukov 		/* Fix IPv4 header checksum and length */
228*fcf59617SAndrey V. Elsukov 		ip->ip_len = htons(m->m_pkthdr.len);
229*fcf59617SAndrey V. Elsukov 		ip->ip_sum = 0;
230*fcf59617SAndrey V. Elsukov 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
231*fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
232*fcf59617SAndrey V. Elsukov 		if (error != 0) {
233*fcf59617SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SA %s->%s "
234*fcf59617SAndrey V. Elsukov 			    "SPI 0x%08x failed with error %d\n", __func__,
235*fcf59617SAndrey V. Elsukov 			    ipsec_address(&sav->sah->saidx.src, sbuf,
236*fcf59617SAndrey V. Elsukov 				sizeof(sbuf)),
237*fcf59617SAndrey V. Elsukov 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
238*fcf59617SAndrey V. Elsukov 				sizeof(dbuf)), ntohl(sav->spi), error));
239*fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
240*fcf59617SAndrey V. Elsukov 			goto bad;
241*fcf59617SAndrey V. Elsukov 		}
242*fcf59617SAndrey V. Elsukov 	}
243*fcf59617SAndrey V. Elsukov 
244*fcf59617SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
245*fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
246*fcf59617SAndrey V. Elsukov 		goto bad;
247*fcf59617SAndrey V. Elsukov 
248*fcf59617SAndrey V. Elsukov 	/*
249*fcf59617SAndrey V. Elsukov 	 * Dispatch to the appropriate IPsec transform logic.  The
250*fcf59617SAndrey V. Elsukov 	 * packet will be returned for transmission after crypto
251*fcf59617SAndrey V. Elsukov 	 * processing, etc. are completed.
252*fcf59617SAndrey V. Elsukov 	 *
253*fcf59617SAndrey V. Elsukov 	 * NB: m & sav are ``passed to caller'' who's responsible for
254*fcf59617SAndrey V. Elsukov 	 *     reclaiming their resources.
255*fcf59617SAndrey V. Elsukov 	 */
256*fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
257*fcf59617SAndrey V. Elsukov 	case AF_INET:
258*fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
259*fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
260*fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
261*fcf59617SAndrey V. Elsukov 		break;
262*fcf59617SAndrey V. Elsukov #ifdef INET6
263*fcf59617SAndrey V. Elsukov 	case AF_INET6:
264*fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
265*fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
266*fcf59617SAndrey V. Elsukov 		break;
267*fcf59617SAndrey V. Elsukov #endif /* INET6 */
268*fcf59617SAndrey V. Elsukov 	default:
269*fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
270*fcf59617SAndrey V. Elsukov 		    __func__, dst->sa.sa_family));
271*fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
272*fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
273*fcf59617SAndrey V. Elsukov 		goto bad;
274*fcf59617SAndrey V. Elsukov 	}
275*fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
276*fcf59617SAndrey V. Elsukov 	if (error != 0) {
277*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
278*fcf59617SAndrey V. Elsukov 		key_freesp(&sp);
279*fcf59617SAndrey V. Elsukov 	}
280*fcf59617SAndrey V. Elsukov 	return (error);
281*fcf59617SAndrey V. Elsukov bad:
282*fcf59617SAndrey V. Elsukov 	IPSECSTAT_INC(ips_out_inval);
283*fcf59617SAndrey V. Elsukov 	if (m != NULL)
284*fcf59617SAndrey V. Elsukov 		m_freem(m);
285*fcf59617SAndrey V. Elsukov 	if (sav != NULL)
286*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
287*fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
288*fcf59617SAndrey V. Elsukov 	return (error);
289*fcf59617SAndrey V. Elsukov }
2907b495c44SVANHULLEBUS Yvan 
29188768458SSam Leffler int
292*fcf59617SAndrey V. Elsukov ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
293*fcf59617SAndrey V. Elsukov     struct inpcb *inp)
29488768458SSam Leffler {
295*fcf59617SAndrey V. Elsukov 
296*fcf59617SAndrey V. Elsukov 	return (ipsec4_perform_request(m, sp, 0));
297*fcf59617SAndrey V. Elsukov }
298*fcf59617SAndrey V. Elsukov 
299*fcf59617SAndrey V. Elsukov static int
300*fcf59617SAndrey V. Elsukov ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
301*fcf59617SAndrey V. Elsukov {
302*fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
30388768458SSam Leffler 	int error;
30488768458SSam Leffler 
305*fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
306*fcf59617SAndrey V. Elsukov 	sp = ipsec4_checkpolicy(m, inp, &error);
307*fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
308*fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
309*fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
310*fcf59617SAndrey V. Elsukov 			m_freem(m);
311*fcf59617SAndrey V. Elsukov 			return (EACCES);
312*fcf59617SAndrey V. Elsukov 		}
313*fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
314*fcf59617SAndrey V. Elsukov 	}
315*fcf59617SAndrey V. Elsukov 
316*fcf59617SAndrey V. Elsukov 	/*
317*fcf59617SAndrey V. Elsukov 	 * Usually we have to have tunnel mode IPsec security policy
318*fcf59617SAndrey V. Elsukov 	 * when we are forwarding a packet. Otherwise we could not handle
319*fcf59617SAndrey V. Elsukov 	 * encrypted replies, because they are not destined for us. But
320*fcf59617SAndrey V. Elsukov 	 * some users are doing source address translation for forwarded
321*fcf59617SAndrey V. Elsukov 	 * packets, and thus, even if they are forwarded, the replies will
322*fcf59617SAndrey V. Elsukov 	 * return back to us.
323*fcf59617SAndrey V. Elsukov 	 */
324*fcf59617SAndrey V. Elsukov 	if (!forwarding) {
325*fcf59617SAndrey V. Elsukov 		/*
326*fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
327*fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
328*fcf59617SAndrey V. Elsukov 		 */
329*fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
330*fcf59617SAndrey V. Elsukov 			in_delayed_cksum(m);
331*fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
332*fcf59617SAndrey V. Elsukov 		}
333*fcf59617SAndrey V. Elsukov #ifdef SCTP
334*fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
335*fcf59617SAndrey V. Elsukov 			struct ip *ip = mtod(m, struct ip *);
336*fcf59617SAndrey V. Elsukov 
337*fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
338*fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
339*fcf59617SAndrey V. Elsukov 		}
340*fcf59617SAndrey V. Elsukov #endif
341*fcf59617SAndrey V. Elsukov 	}
342*fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
343*fcf59617SAndrey V. Elsukov 	error = ipsec4_process_packet(m, sp, inp);
344*fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
345*fcf59617SAndrey V. Elsukov 		/*
346*fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
347*fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
348*fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
349*fcf59617SAndrey V. Elsukov 		 */
350*fcf59617SAndrey V. Elsukov 		return (0);
351*fcf59617SAndrey V. Elsukov 	}
352*fcf59617SAndrey V. Elsukov 	if (error == 0)
353*fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
354*fcf59617SAndrey V. Elsukov 	return (error);
355*fcf59617SAndrey V. Elsukov }
356*fcf59617SAndrey V. Elsukov 
357*fcf59617SAndrey V. Elsukov /*
358*fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv4.
359*fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
360*fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
361*fcf59617SAndrey V. Elsukov  */
362*fcf59617SAndrey V. Elsukov int
363*fcf59617SAndrey V. Elsukov ipsec4_output(struct mbuf *m, struct inpcb *inp)
364*fcf59617SAndrey V. Elsukov {
365*fcf59617SAndrey V. Elsukov 
366*fcf59617SAndrey V. Elsukov 	/*
367*fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
368*fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
369*fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
370*fcf59617SAndrey V. Elsukov 	 */
371*fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
372*fcf59617SAndrey V. Elsukov 		return (0);
373*fcf59617SAndrey V. Elsukov 
374*fcf59617SAndrey V. Elsukov 	return (ipsec4_common_output(m, inp, 0));
375*fcf59617SAndrey V. Elsukov }
376*fcf59617SAndrey V. Elsukov 
377*fcf59617SAndrey V. Elsukov /*
378*fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv4.
379*fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
380*fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
381*fcf59617SAndrey V. Elsukov  */
382*fcf59617SAndrey V. Elsukov int
383*fcf59617SAndrey V. Elsukov ipsec4_forward(struct mbuf *m)
384*fcf59617SAndrey V. Elsukov {
385*fcf59617SAndrey V. Elsukov 
386*fcf59617SAndrey V. Elsukov 	/*
387*fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
388*fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
389*fcf59617SAndrey V. Elsukov 	 */
390*fcf59617SAndrey V. Elsukov 	if (ipsec4_in_reject(m, NULL) != 0) {
391*fcf59617SAndrey V. Elsukov 		m_freem(m);
392*fcf59617SAndrey V. Elsukov 		return (EACCES);
393*fcf59617SAndrey V. Elsukov 	}
394*fcf59617SAndrey V. Elsukov 	return (ipsec4_common_output(m, NULL, 1));
395*fcf59617SAndrey V. Elsukov }
396*fcf59617SAndrey V. Elsukov #endif
397*fcf59617SAndrey V. Elsukov 
398*fcf59617SAndrey V. Elsukov #ifdef INET6
399*fcf59617SAndrey V. Elsukov static int
400*fcf59617SAndrey V. Elsukov in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
401*fcf59617SAndrey V. Elsukov     const struct in6_addr *ia)
402*fcf59617SAndrey V. Elsukov {
403*fcf59617SAndrey V. Elsukov 	struct in6_addr ia2;
404*fcf59617SAndrey V. Elsukov 
405*fcf59617SAndrey V. Elsukov 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
406*fcf59617SAndrey V. Elsukov 		memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
407*fcf59617SAndrey V. Elsukov 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
408*fcf59617SAndrey V. Elsukov 		return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
409*fcf59617SAndrey V. Elsukov 	}
410*fcf59617SAndrey V. Elsukov 	return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
411*fcf59617SAndrey V. Elsukov }
412*fcf59617SAndrey V. Elsukov 
413*fcf59617SAndrey V. Elsukov static struct secasvar *
414*fcf59617SAndrey V. Elsukov ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
415*fcf59617SAndrey V. Elsukov {
416*fcf59617SAndrey V. Elsukov 	struct secasindex *saidx, tmpsaidx;
417*fcf59617SAndrey V. Elsukov 	struct ipsecrequest *isr;
418*fcf59617SAndrey V. Elsukov 	struct sockaddr_in6 *sin6;
419*fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
420*fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
421*fcf59617SAndrey V. Elsukov 
422*fcf59617SAndrey V. Elsukov 	/*
423*fcf59617SAndrey V. Elsukov 	 * Check system global policy controls.
424*fcf59617SAndrey V. Elsukov 	 */
425*fcf59617SAndrey V. Elsukov next:
426*fcf59617SAndrey V. Elsukov 	isr = sp->req[*pidx];
427*fcf59617SAndrey V. Elsukov 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
428*fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
429*fcf59617SAndrey V. Elsukov 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
430*fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: IPsec outbound packet dropped due"
431*fcf59617SAndrey V. Elsukov 			" to policy (check your sysctls)\n", __func__));
432*fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
433*fcf59617SAndrey V. Elsukov 		*error = EHOSTUNREACH;
434*fcf59617SAndrey V. Elsukov 		return (NULL);
435*fcf59617SAndrey V. Elsukov 	}
436*fcf59617SAndrey V. Elsukov 	/*
437*fcf59617SAndrey V. Elsukov 	 * Craft SA index to search for proper SA.  Note that
438*fcf59617SAndrey V. Elsukov 	 * we only fillin unspecified SA peers for transport
439*fcf59617SAndrey V. Elsukov 	 * mode; for tunnel mode they must already be filled in.
440*fcf59617SAndrey V. Elsukov 	 */
441*fcf59617SAndrey V. Elsukov 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
442*fcf59617SAndrey V. Elsukov 		saidx = &tmpsaidx;
443*fcf59617SAndrey V. Elsukov 		*saidx = isr->saidx;
444*fcf59617SAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
445*fcf59617SAndrey V. Elsukov 		if (saidx->src.sin6.sin6_len == 0) {
446*fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->src;
447*fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
448*fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
449*fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
450*fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_src;
451*fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
452*fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
453*fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
454*fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
455*fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_src.s6_addr16[1]);
456*fcf59617SAndrey V. Elsukov 			}
457*fcf59617SAndrey V. Elsukov 		}
458*fcf59617SAndrey V. Elsukov 		if (saidx->dst.sin6.sin6_len == 0) {
459*fcf59617SAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)&saidx->dst;
460*fcf59617SAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
461*fcf59617SAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
462*fcf59617SAndrey V. Elsukov 			sin6->sin6_port = IPSEC_PORT_ANY;
463*fcf59617SAndrey V. Elsukov 			sin6->sin6_addr = ip6->ip6_dst;
464*fcf59617SAndrey V. Elsukov 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
465*fcf59617SAndrey V. Elsukov 				/* fix scope id for comparing SPD */
466*fcf59617SAndrey V. Elsukov 				sin6->sin6_addr.s6_addr16[1] = 0;
467*fcf59617SAndrey V. Elsukov 				sin6->sin6_scope_id =
468*fcf59617SAndrey V. Elsukov 				    ntohs(ip6->ip6_dst.s6_addr16[1]);
469*fcf59617SAndrey V. Elsukov 			}
470*fcf59617SAndrey V. Elsukov 		}
471*fcf59617SAndrey V. Elsukov 	} else
472*fcf59617SAndrey V. Elsukov 		saidx = &sp->req[*pidx]->saidx;
473*fcf59617SAndrey V. Elsukov 	/*
474*fcf59617SAndrey V. Elsukov 	 * Lookup SA and validate it.
475*fcf59617SAndrey V. Elsukov 	 */
476*fcf59617SAndrey V. Elsukov 	sav = key_allocsa_policy(sp, saidx, error);
477*fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
478*fcf59617SAndrey V. Elsukov 		IPSEC6STAT_INC(ips_out_nosa);
479*fcf59617SAndrey V. Elsukov 		if (*error != 0)
480*fcf59617SAndrey V. Elsukov 			return (NULL);
481*fcf59617SAndrey V. Elsukov 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
482*fcf59617SAndrey V. Elsukov 			/*
483*fcf59617SAndrey V. Elsukov 			 * We have no SA and policy that doesn't require
484*fcf59617SAndrey V. Elsukov 			 * this IPsec transform, thus we can continue w/o
485*fcf59617SAndrey V. Elsukov 			 * IPsec processing, i.e. return EJUSTRETURN.
486*fcf59617SAndrey V. Elsukov 			 * But first check if there is some bundled transform.
487*fcf59617SAndrey V. Elsukov 			 */
488*fcf59617SAndrey V. Elsukov 			if (sp->tcount > ++(*pidx))
489*fcf59617SAndrey V. Elsukov 				goto next;
490*fcf59617SAndrey V. Elsukov 			*error = EJUSTRETURN;
491*fcf59617SAndrey V. Elsukov 		}
492*fcf59617SAndrey V. Elsukov 		return (NULL);
493*fcf59617SAndrey V. Elsukov 	}
494*fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
495*fcf59617SAndrey V. Elsukov 	return (sav);
496*fcf59617SAndrey V. Elsukov }
497*fcf59617SAndrey V. Elsukov 
498*fcf59617SAndrey V. Elsukov /*
499*fcf59617SAndrey V. Elsukov  * IPsec output logic for IPv6.
500*fcf59617SAndrey V. Elsukov  */
501*fcf59617SAndrey V. Elsukov static int
502*fcf59617SAndrey V. Elsukov ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
503*fcf59617SAndrey V. Elsukov {
504*fcf59617SAndrey V. Elsukov 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
505*fcf59617SAndrey V. Elsukov 	struct ipsec_ctx_data ctx;
506*fcf59617SAndrey V. Elsukov 	union sockaddr_union *dst;
507*fcf59617SAndrey V. Elsukov 	struct secasvar *sav;
508*fcf59617SAndrey V. Elsukov 	struct ip6_hdr *ip6;
509*fcf59617SAndrey V. Elsukov 	int error, i, off;
510*fcf59617SAndrey V. Elsukov 
511*fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
512*fcf59617SAndrey V. Elsukov 
513*fcf59617SAndrey V. Elsukov 	sav = ipsec6_allocsa(m, sp, &idx, &error);
514*fcf59617SAndrey V. Elsukov 	if (sav == NULL) {
515*fcf59617SAndrey V. Elsukov 		if (error == EJUSTRETURN) { /* No IPsec required */
516*fcf59617SAndrey V. Elsukov 			key_freesp(&sp);
517*fcf59617SAndrey V. Elsukov 			return (error);
518*fcf59617SAndrey V. Elsukov 		}
519*fcf59617SAndrey V. Elsukov 		goto bad;
520*fcf59617SAndrey V. Elsukov 	}
521*fcf59617SAndrey V. Elsukov 
522*fcf59617SAndrey V. Elsukov 	/* Fix IP length in case if it is not set yet. */
523*fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
524*fcf59617SAndrey V. Elsukov 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
525*fcf59617SAndrey V. Elsukov 
526*fcf59617SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET6, IPSEC_ENC_BEFORE);
527*fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
528*fcf59617SAndrey V. Elsukov 		goto bad;
529*fcf59617SAndrey V. Elsukov 
530*fcf59617SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
531*fcf59617SAndrey V. Elsukov 	dst = &sav->sah->saidx.dst;
532*fcf59617SAndrey V. Elsukov 
533*fcf59617SAndrey V. Elsukov 	/* Do the appropriate encapsulation, if necessary */
534*fcf59617SAndrey V. Elsukov 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
535*fcf59617SAndrey V. Elsukov 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
536*fcf59617SAndrey V. Elsukov 	    ((dst->sa.sa_family == AF_INET6) &&
537*fcf59617SAndrey V. Elsukov 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
538*fcf59617SAndrey V. Elsukov 	     (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
539*fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
540*fcf59617SAndrey V. Elsukov 			/* No jumbogram support. */
541*fcf59617SAndrey V. Elsukov 			error = ENXIO;   /*XXX*/
542*fcf59617SAndrey V. Elsukov 			goto bad;
543*fcf59617SAndrey V. Elsukov 		}
544*fcf59617SAndrey V. Elsukov 		error = ipsec_encap(&m, &sav->sah->saidx);
545*fcf59617SAndrey V. Elsukov 		if (error != 0) {
546*fcf59617SAndrey V. Elsukov 			DPRINTF(("%s: encapsulation for SA %s->%s "
547*fcf59617SAndrey V. Elsukov 			    "SPI 0x%08x failed with error %d\n", __func__,
548*fcf59617SAndrey V. Elsukov 			    ipsec_address(&sav->sah->saidx.src, sbuf,
549*fcf59617SAndrey V. Elsukov 				sizeof(sbuf)),
550*fcf59617SAndrey V. Elsukov 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
551*fcf59617SAndrey V. Elsukov 				sizeof(dbuf)), ntohl(sav->spi), error));
552*fcf59617SAndrey V. Elsukov 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
553*fcf59617SAndrey V. Elsukov 			goto bad;
554*fcf59617SAndrey V. Elsukov 		}
555*fcf59617SAndrey V. Elsukov 	}
556*fcf59617SAndrey V. Elsukov 
557*fcf59617SAndrey V. Elsukov 	IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
558*fcf59617SAndrey V. Elsukov 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
559*fcf59617SAndrey V. Elsukov 		goto bad;
560*fcf59617SAndrey V. Elsukov 
561*fcf59617SAndrey V. Elsukov 	switch(dst->sa.sa_family) {
562*fcf59617SAndrey V. Elsukov #ifdef INET
563*fcf59617SAndrey V. Elsukov 	case AF_INET:
564*fcf59617SAndrey V. Elsukov 		{
565*fcf59617SAndrey V. Elsukov 		struct ip *ip;
566*fcf59617SAndrey V. Elsukov 		ip = mtod(m, struct ip *);
567*fcf59617SAndrey V. Elsukov 		i = ip->ip_hl << 2;
568*fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip, ip_p);
569*fcf59617SAndrey V. Elsukov 		}
570*fcf59617SAndrey V. Elsukov 		break;
571*fcf59617SAndrey V. Elsukov #endif /* AF_INET */
572*fcf59617SAndrey V. Elsukov 	case AF_INET6:
573*fcf59617SAndrey V. Elsukov 		i = sizeof(struct ip6_hdr);
574*fcf59617SAndrey V. Elsukov 		off = offsetof(struct ip6_hdr, ip6_nxt);
575*fcf59617SAndrey V. Elsukov 		break;
576*fcf59617SAndrey V. Elsukov 	default:
577*fcf59617SAndrey V. Elsukov 		DPRINTF(("%s: unsupported protocol family %u\n",
578*fcf59617SAndrey V. Elsukov 				 __func__, dst->sa.sa_family));
579*fcf59617SAndrey V. Elsukov 		error = EPFNOSUPPORT;
580*fcf59617SAndrey V. Elsukov 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
581*fcf59617SAndrey V. Elsukov 		goto bad;
582*fcf59617SAndrey V. Elsukov 	}
583*fcf59617SAndrey V. Elsukov 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
584*fcf59617SAndrey V. Elsukov 	if (error != 0) {
585*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
586*fcf59617SAndrey V. Elsukov 		key_freesp(&sp);
587*fcf59617SAndrey V. Elsukov 	}
588*fcf59617SAndrey V. Elsukov 	return (error);
589*fcf59617SAndrey V. Elsukov bad:
590*fcf59617SAndrey V. Elsukov 	IPSEC6STAT_INC(ips_out_inval);
591*fcf59617SAndrey V. Elsukov 	if (m != NULL)
592*fcf59617SAndrey V. Elsukov 		m_freem(m);
593*fcf59617SAndrey V. Elsukov 	if (sav != NULL)
594*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
595*fcf59617SAndrey V. Elsukov 	key_freesp(&sp);
596*fcf59617SAndrey V. Elsukov 	return (error);
597*fcf59617SAndrey V. Elsukov }
598*fcf59617SAndrey V. Elsukov 
599*fcf59617SAndrey V. Elsukov int
600*fcf59617SAndrey V. Elsukov ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
601*fcf59617SAndrey V. Elsukov     struct inpcb *inp)
602*fcf59617SAndrey V. Elsukov {
603*fcf59617SAndrey V. Elsukov 
604*fcf59617SAndrey V. Elsukov 	return (ipsec6_perform_request(m, sp, 0));
605*fcf59617SAndrey V. Elsukov }
606*fcf59617SAndrey V. Elsukov 
607*fcf59617SAndrey V. Elsukov static int
608*fcf59617SAndrey V. Elsukov ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
609*fcf59617SAndrey V. Elsukov {
610*fcf59617SAndrey V. Elsukov 	struct secpolicy *sp;
611*fcf59617SAndrey V. Elsukov 	int error;
612*fcf59617SAndrey V. Elsukov 
613*fcf59617SAndrey V. Elsukov 	/* Lookup for the corresponding outbound security policy */
614*fcf59617SAndrey V. Elsukov 	sp = ipsec6_checkpolicy(m, inp, &error);
615*fcf59617SAndrey V. Elsukov 	if (sp == NULL) {
616*fcf59617SAndrey V. Elsukov 		if (error == -EINVAL) {
617*fcf59617SAndrey V. Elsukov 			/* Discarded by policy. */
618*fcf59617SAndrey V. Elsukov 			m_freem(m);
619*fcf59617SAndrey V. Elsukov 			return (EACCES);
620*fcf59617SAndrey V. Elsukov 		}
621*fcf59617SAndrey V. Elsukov 		return (0); /* No IPsec required. */
622*fcf59617SAndrey V. Elsukov 	}
623*fcf59617SAndrey V. Elsukov 
624*fcf59617SAndrey V. Elsukov 	if (!forwarding) {
625*fcf59617SAndrey V. Elsukov 		/*
626*fcf59617SAndrey V. Elsukov 		 * Do delayed checksums now because we send before
627*fcf59617SAndrey V. Elsukov 		 * this is done in the normal processing path.
628*fcf59617SAndrey V. Elsukov 		 */
629*fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
630*fcf59617SAndrey V. Elsukov 			in6_delayed_cksum(m, m->m_pkthdr.len -
631*fcf59617SAndrey V. Elsukov 			    sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
632*fcf59617SAndrey V. Elsukov 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
633*fcf59617SAndrey V. Elsukov 		}
634*fcf59617SAndrey V. Elsukov #ifdef SCTP
635*fcf59617SAndrey V. Elsukov 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
636*fcf59617SAndrey V. Elsukov 			sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
637*fcf59617SAndrey V. Elsukov 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
638*fcf59617SAndrey V. Elsukov 		}
639*fcf59617SAndrey V. Elsukov #endif
640*fcf59617SAndrey V. Elsukov 	}
641*fcf59617SAndrey V. Elsukov 	/* NB: callee frees mbuf and releases reference to SP */
642*fcf59617SAndrey V. Elsukov 	error = ipsec6_process_packet(m, sp, inp);
643*fcf59617SAndrey V. Elsukov 	if (error == EJUSTRETURN) {
644*fcf59617SAndrey V. Elsukov 		/*
645*fcf59617SAndrey V. Elsukov 		 * We had a SP with a level of 'use' and no SA. We
646*fcf59617SAndrey V. Elsukov 		 * will just continue to process the packet without
647*fcf59617SAndrey V. Elsukov 		 * IPsec processing and return without error.
648*fcf59617SAndrey V. Elsukov 		 */
649*fcf59617SAndrey V. Elsukov 		return (0);
650*fcf59617SAndrey V. Elsukov 	}
651*fcf59617SAndrey V. Elsukov 	if (error == 0)
652*fcf59617SAndrey V. Elsukov 		return (EINPROGRESS); /* consumed by IPsec */
653*fcf59617SAndrey V. Elsukov 	return (error);
654*fcf59617SAndrey V. Elsukov }
655*fcf59617SAndrey V. Elsukov 
656*fcf59617SAndrey V. Elsukov /*
657*fcf59617SAndrey V. Elsukov  * IPSEC_OUTPUT() method implementation for IPv6.
658*fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
659*fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
660*fcf59617SAndrey V. Elsukov  */
661*fcf59617SAndrey V. Elsukov int
662*fcf59617SAndrey V. Elsukov ipsec6_output(struct mbuf *m, struct inpcb *inp)
663*fcf59617SAndrey V. Elsukov {
664*fcf59617SAndrey V. Elsukov 
665*fcf59617SAndrey V. Elsukov 	/*
666*fcf59617SAndrey V. Elsukov 	 * If the packet is resubmitted to ip_output (e.g. after
667*fcf59617SAndrey V. Elsukov 	 * AH, ESP, etc. processing), there will be a tag to bypass
668*fcf59617SAndrey V. Elsukov 	 * the lookup and related policy checking.
669*fcf59617SAndrey V. Elsukov 	 */
670*fcf59617SAndrey V. Elsukov 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
671*fcf59617SAndrey V. Elsukov 		return (0);
672*fcf59617SAndrey V. Elsukov 
673*fcf59617SAndrey V. Elsukov 	return (ipsec6_common_output(m, inp, 0));
674*fcf59617SAndrey V. Elsukov }
675*fcf59617SAndrey V. Elsukov 
676*fcf59617SAndrey V. Elsukov /*
677*fcf59617SAndrey V. Elsukov  * IPSEC_FORWARD() method implementation for IPv6.
678*fcf59617SAndrey V. Elsukov  * 0 - no IPsec handling needed
679*fcf59617SAndrey V. Elsukov  * other values - mbuf consumed by IPsec.
680*fcf59617SAndrey V. Elsukov  */
681*fcf59617SAndrey V. Elsukov int
682*fcf59617SAndrey V. Elsukov ipsec6_forward(struct mbuf *m)
683*fcf59617SAndrey V. Elsukov {
684*fcf59617SAndrey V. Elsukov 
685*fcf59617SAndrey V. Elsukov 	/*
686*fcf59617SAndrey V. Elsukov 	 * Check if this packet has an active inbound SP and needs to be
687*fcf59617SAndrey V. Elsukov 	 * dropped instead of forwarded.
688*fcf59617SAndrey V. Elsukov 	 */
689*fcf59617SAndrey V. Elsukov 	if (ipsec6_in_reject(m, NULL) != 0) {
690*fcf59617SAndrey V. Elsukov 		m_freem(m);
691*fcf59617SAndrey V. Elsukov 		return (EACCES);
692*fcf59617SAndrey V. Elsukov 	}
693*fcf59617SAndrey V. Elsukov 	return (ipsec6_common_output(m, NULL, 1));
694*fcf59617SAndrey V. Elsukov }
695*fcf59617SAndrey V. Elsukov #endif /* INET6 */
696*fcf59617SAndrey V. Elsukov 
697*fcf59617SAndrey V. Elsukov int
698*fcf59617SAndrey V. Elsukov ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
699*fcf59617SAndrey V. Elsukov     u_int idx)
700*fcf59617SAndrey V. Elsukov {
701*fcf59617SAndrey V. Elsukov 	struct xform_history *xh;
702*fcf59617SAndrey V. Elsukov 	struct secasindex *saidx;
703*fcf59617SAndrey V. Elsukov 	struct m_tag *mtag;
704*fcf59617SAndrey V. Elsukov 	int error;
70588768458SSam Leffler 
70688768458SSam Leffler 	saidx = &sav->sah->saidx;
70788768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
70888768458SSam Leffler #ifdef INET
70988768458SSam Leffler 	case AF_INET:
71088768458SSam Leffler 		/* Fix the header length, for AH processing. */
71188768458SSam Leffler 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
71288768458SSam Leffler 		break;
71388768458SSam Leffler #endif /* INET */
71488768458SSam Leffler #ifdef INET6
71588768458SSam Leffler 	case AF_INET6:
71688768458SSam Leffler 		/* Fix the header length, for AH processing. */
71788768458SSam Leffler 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
71888768458SSam Leffler 			error = ENXIO;
71988768458SSam Leffler 			goto bad;
72088768458SSam Leffler 		}
72188768458SSam Leffler 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
72288768458SSam Leffler 			/* No jumbogram support. */
72388768458SSam Leffler 			error = ENXIO;	/*?*/
72488768458SSam Leffler 			goto bad;
72588768458SSam Leffler 		}
72688768458SSam Leffler 		mtod(m, struct ip6_hdr *)->ip6_plen =
72788768458SSam Leffler 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
72888768458SSam Leffler 		break;
72988768458SSam Leffler #endif /* INET6 */
73088768458SSam Leffler 	default:
7319ffa9677SSam Leffler 		DPRINTF(("%s: unknown protocol family %u\n", __func__,
73288768458SSam Leffler 		    saidx->dst.sa.sa_family));
73388768458SSam Leffler 		error = ENXIO;
73488768458SSam Leffler 		goto bad;
73588768458SSam Leffler 	}
73688768458SSam Leffler 
73788768458SSam Leffler 	/*
738*fcf59617SAndrey V. Elsukov 	 * Add a record of what we've done to the packet.
73988768458SSam Leffler 	 */
740*fcf59617SAndrey V. Elsukov 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
74188768458SSam Leffler 	if (mtag == NULL) {
7429ffa9677SSam Leffler 		DPRINTF(("%s: could not get packet tag\n", __func__));
74388768458SSam Leffler 		error = ENOMEM;
74488768458SSam Leffler 		goto bad;
74588768458SSam Leffler 	}
74688768458SSam Leffler 
747*fcf59617SAndrey V. Elsukov 	xh = (struct xform_history *)(mtag + 1);
748*fcf59617SAndrey V. Elsukov 	xh->dst = saidx->dst;
749*fcf59617SAndrey V. Elsukov 	xh->proto = saidx->proto;
750*fcf59617SAndrey V. Elsukov 	xh->mode = saidx->mode;
751*fcf59617SAndrey V. Elsukov 	xh->spi = sav->spi;
75288768458SSam Leffler 	m_tag_prepend(m, mtag);
75388768458SSam Leffler 
75459959de5SErmal Luçi 	key_sa_recordxfer(sav, m);		/* record data transfer */
75559959de5SErmal Luçi 
75688768458SSam Leffler 	/*
75788768458SSam Leffler 	 * If there's another (bundled) SA to apply, do so.
75888768458SSam Leffler 	 * Note that this puts a burden on the kernel stack size.
75988768458SSam Leffler 	 * If this is a problem we'll need to introduce a queue
76088768458SSam Leffler 	 * to set the packet on so we can unwind the stack before
76188768458SSam Leffler 	 * doing further processing.
76288768458SSam Leffler 	 */
763*fcf59617SAndrey V. Elsukov 	if (++idx < sp->tcount) {
764db178eb8SBjoern A. Zeeb 		switch (saidx->dst.sa.sa_family) {
765db178eb8SBjoern A. Zeeb #ifdef INET
766db178eb8SBjoern A. Zeeb 		case AF_INET:
767*fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
768f9d8f665SAndrey V. Elsukov 			IPSECSTAT_INC(ips_out_bundlesa);
769*fcf59617SAndrey V. Elsukov 			return (ipsec4_perform_request(m, sp, idx));
770db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
771db178eb8SBjoern A. Zeeb #endif
772db178eb8SBjoern A. Zeeb #ifdef INET6
773db178eb8SBjoern A. Zeeb 		case AF_INET6:
774*fcf59617SAndrey V. Elsukov 			key_freesav(&sav);
775f9d8f665SAndrey V. Elsukov 			IPSEC6STAT_INC(ips_out_bundlesa);
776*fcf59617SAndrey V. Elsukov 			return (ipsec6_perform_request(m, sp, idx));
777db178eb8SBjoern A. Zeeb 			/* NOTREACHED */
778db178eb8SBjoern A. Zeeb #endif /* INET6 */
779db178eb8SBjoern A. Zeeb 		default:
780db178eb8SBjoern A. Zeeb 			DPRINTF(("%s: unknown protocol family %u\n", __func__,
781db178eb8SBjoern A. Zeeb 			    saidx->dst.sa.sa_family));
782*fcf59617SAndrey V. Elsukov 			error = EPFNOSUPPORT;
783db178eb8SBjoern A. Zeeb 			goto bad;
784db178eb8SBjoern A. Zeeb 		}
78588768458SSam Leffler 	}
78688768458SSam Leffler 
787*fcf59617SAndrey V. Elsukov 	key_freesp(&sp), sp = NULL;	/* Release reference to SP */
788*fcf59617SAndrey V. Elsukov #ifdef INET
789*fcf59617SAndrey V. Elsukov 	/*
790*fcf59617SAndrey V. Elsukov 	 * Do UDP encapsulation if SA requires it.
791*fcf59617SAndrey V. Elsukov 	 */
792*fcf59617SAndrey V. Elsukov 	if (sav->natt != NULL) {
793*fcf59617SAndrey V. Elsukov 		error = udp_ipsec_output(m, sav);
794*fcf59617SAndrey V. Elsukov 		if (error != 0)
795*fcf59617SAndrey V. Elsukov 			goto bad;
796*fcf59617SAndrey V. Elsukov 	}
797*fcf59617SAndrey V. Elsukov #endif /* INET */
79888768458SSam Leffler 	/*
79988768458SSam Leffler 	 * We're done with IPsec processing, transmit the packet using the
8006508929bSAndrey V. Elsukov 	 * appropriate network protocol (IP or IPv6).
80188768458SSam Leffler 	 */
80288768458SSam Leffler 	switch (saidx->dst.sa.sa_family) {
80388768458SSam Leffler #ifdef INET
80488768458SSam Leffler 	case AF_INET:
805*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
80688768458SSam Leffler 		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
80788768458SSam Leffler #endif /* INET */
80888768458SSam Leffler #ifdef INET6
80988768458SSam Leffler 	case AF_INET6:
810*fcf59617SAndrey V. Elsukov 		key_freesav(&sav);
81188768458SSam Leffler 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
81288768458SSam Leffler #endif /* INET6 */
81388768458SSam Leffler 	}
81488768458SSam Leffler 	panic("ipsec_process_done");
81588768458SSam Leffler bad:
81688768458SSam Leffler 	m_freem(m);
817*fcf59617SAndrey V. Elsukov 	key_freesav(&sav);
818*fcf59617SAndrey V. Elsukov 	if (sp != NULL)
819*fcf59617SAndrey V. Elsukov 		key_freesp(&sp);
82088768458SSam Leffler 	return (error);
82188768458SSam Leffler }
82288768458SSam Leffler 
823*fcf59617SAndrey V. Elsukov /*
824*fcf59617SAndrey V. Elsukov  * ipsec_prepend() is optimized version of M_PREPEND().
825*fcf59617SAndrey V. Elsukov  * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
826*fcf59617SAndrey V. Elsukov  * It is expected that after IP encapsulation some IPsec transform will
827*fcf59617SAndrey V. Elsukov  * be performed. Each IPsec transform inserts its variable length header
828*fcf59617SAndrey V. Elsukov  * just after outer IP header using m_makespace(). If given mbuf has not
829*fcf59617SAndrey V. Elsukov  * enough free space at the beginning, we allocate new mbuf and reserve
830*fcf59617SAndrey V. Elsukov  * some space at the beginning and at the end.
831*fcf59617SAndrey V. Elsukov  * This helps avoid allocating of new mbuf and data copying in m_makespace(),
832*fcf59617SAndrey V. Elsukov  * we place outer header in the middle of mbuf's data with reserved leading
833*fcf59617SAndrey V. Elsukov  * and trailing space:
834*fcf59617SAndrey V. Elsukov  *	[ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
835*fcf59617SAndrey V. Elsukov  * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
836*fcf59617SAndrey V. Elsukov  * be used to inject AH/ESP/IPCOMP header.
837*fcf59617SAndrey V. Elsukov  */
838*fcf59617SAndrey V. Elsukov #define	IPSEC_TRAILINGSPACE	(sizeof(struct udphdr) +/* NAT-T */	\
839*fcf59617SAndrey V. Elsukov     max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN,	/* ESP + IV */	\
840*fcf59617SAndrey V. Elsukov 	sizeof(struct newah) + HASH_MAX_LEN		/* AH + ICV */))
841*fcf59617SAndrey V. Elsukov static struct mbuf *
842*fcf59617SAndrey V. Elsukov ipsec_prepend(struct mbuf *m, int len, int how)
84388768458SSam Leffler {
844*fcf59617SAndrey V. Elsukov 	struct mbuf *n;
84588768458SSam Leffler 
846*fcf59617SAndrey V. Elsukov 	M_ASSERTPKTHDR(m);
847*fcf59617SAndrey V. Elsukov 	IPSEC_ASSERT(len < MHLEN, ("wrong length"));
848*fcf59617SAndrey V. Elsukov 	if (M_LEADINGSPACE(m) >= len) {
849*fcf59617SAndrey V. Elsukov 		/* No need to allocate new mbuf. */
850*fcf59617SAndrey V. Elsukov 		m->m_data -= len;
851*fcf59617SAndrey V. Elsukov 		m->m_len += len;
852*fcf59617SAndrey V. Elsukov 		m->m_pkthdr.len += len;
853*fcf59617SAndrey V. Elsukov 		return (m);
85488768458SSam Leffler 	}
855*fcf59617SAndrey V. Elsukov 	n = m_gethdr(how, m->m_type);
856*fcf59617SAndrey V. Elsukov 	if (n == NULL) {
857*fcf59617SAndrey V. Elsukov 		m_freem(m);
858*fcf59617SAndrey V. Elsukov 		return (NULL);
85988768458SSam Leffler 	}
860*fcf59617SAndrey V. Elsukov 	m_move_pkthdr(n, m);
861*fcf59617SAndrey V. Elsukov 	n->m_next = m;
862*fcf59617SAndrey V. Elsukov 	if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
863*fcf59617SAndrey V. Elsukov 		m_align(n, len + IPSEC_TRAILINGSPACE);
864*fcf59617SAndrey V. Elsukov 	n->m_len = len;
865*fcf59617SAndrey V. Elsukov 	n->m_pkthdr.len += len;
866*fcf59617SAndrey V. Elsukov 	return (n);
86788768458SSam Leffler }
86888768458SSam Leffler 
86961f37615SAndrey V. Elsukov static int
87061f37615SAndrey V. Elsukov ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
87161f37615SAndrey V. Elsukov {
87261f37615SAndrey V. Elsukov #ifdef INET6
87361f37615SAndrey V. Elsukov 	struct ip6_hdr *ip6;
87461f37615SAndrey V. Elsukov #endif
87561f37615SAndrey V. Elsukov 	struct ip *ip;
87661f37615SAndrey V. Elsukov 	int setdf;
87761f37615SAndrey V. Elsukov 	uint8_t itos, proto;
87861f37615SAndrey V. Elsukov 
87961f37615SAndrey V. Elsukov 	ip = mtod(*mp, struct ip *);
88061f37615SAndrey V. Elsukov 	switch (ip->ip_v) {
88161f37615SAndrey V. Elsukov #ifdef INET
88261f37615SAndrey V. Elsukov 	case IPVERSION:
88361f37615SAndrey V. Elsukov 		proto = IPPROTO_IPIP;
88461f37615SAndrey V. Elsukov 		/*
88561f37615SAndrey V. Elsukov 		 * Collect IP_DF state from the inner header
88661f37615SAndrey V. Elsukov 		 * and honor system-wide control of how to handle it.
88761f37615SAndrey V. Elsukov 		 */
88861f37615SAndrey V. Elsukov 		switch (V_ip4_ipsec_dfbit) {
88961f37615SAndrey V. Elsukov 		case 0:	/* clear in outer header */
89061f37615SAndrey V. Elsukov 		case 1:	/* set in outer header */
89161f37615SAndrey V. Elsukov 			setdf = V_ip4_ipsec_dfbit;
89261f37615SAndrey V. Elsukov 			break;
89361f37615SAndrey V. Elsukov 		default:/* propagate to outer header */
8946f814d0eSAndrey V. Elsukov 			setdf = (ip->ip_off & htons(IP_DF)) != 0;
89561f37615SAndrey V. Elsukov 		}
89661f37615SAndrey V. Elsukov 		itos = ip->ip_tos;
89761f37615SAndrey V. Elsukov 		break;
89861f37615SAndrey V. Elsukov #endif
89961f37615SAndrey V. Elsukov #ifdef INET6
90061f37615SAndrey V. Elsukov 	case (IPV6_VERSION >> 4):
90161f37615SAndrey V. Elsukov 		proto = IPPROTO_IPV6;
90261f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
90361f37615SAndrey V. Elsukov 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
90461f37615SAndrey V. Elsukov 		setdf = V_ip4_ipsec_dfbit ? 1: 0;
90561f37615SAndrey V. Elsukov 		/* scoped address handling */
90661f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_src);
90761f37615SAndrey V. Elsukov 		in6_clearscope(&ip6->ip6_dst);
90861f37615SAndrey V. Elsukov 		break;
90961f37615SAndrey V. Elsukov #endif
91061f37615SAndrey V. Elsukov 	default:
91161f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
91261f37615SAndrey V. Elsukov 	}
91361f37615SAndrey V. Elsukov 	switch (saidx->dst.sa.sa_family) {
91461f37615SAndrey V. Elsukov #ifdef INET
91561f37615SAndrey V. Elsukov 	case AF_INET:
91661f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET ||
91761f37615SAndrey V. Elsukov 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
91861f37615SAndrey V. Elsukov 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
91961f37615SAndrey V. Elsukov 			return (EINVAL);
920*fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
92161f37615SAndrey V. Elsukov 		if (*mp == NULL)
92261f37615SAndrey V. Elsukov 			return (ENOBUFS);
92361f37615SAndrey V. Elsukov 		ip = mtod(*mp, struct ip *);
92461f37615SAndrey V. Elsukov 		ip->ip_v = IPVERSION;
92561f37615SAndrey V. Elsukov 		ip->ip_hl = sizeof(struct ip) >> 2;
92661f37615SAndrey V. Elsukov 		ip->ip_p = proto;
92761f37615SAndrey V. Elsukov 		ip->ip_len = htons((*mp)->m_pkthdr.len);
92861f37615SAndrey V. Elsukov 		ip->ip_ttl = V_ip_defttl;
92961f37615SAndrey V. Elsukov 		ip->ip_sum = 0;
93061f37615SAndrey V. Elsukov 		ip->ip_off = setdf ? htons(IP_DF): 0;
93161f37615SAndrey V. Elsukov 		ip->ip_src = saidx->src.sin.sin_addr;
93261f37615SAndrey V. Elsukov 		ip->ip_dst = saidx->dst.sin.sin_addr;
93361f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
93461f37615SAndrey V. Elsukov 		ip_fillid(ip);
93561f37615SAndrey V. Elsukov 		break;
93661f37615SAndrey V. Elsukov #endif /* INET */
93761f37615SAndrey V. Elsukov #ifdef INET6
93861f37615SAndrey V. Elsukov 	case AF_INET6:
93961f37615SAndrey V. Elsukov 		if (saidx->src.sa.sa_family != AF_INET6 ||
94061f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
94161f37615SAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
94261f37615SAndrey V. Elsukov 			return (EINVAL);
943*fcf59617SAndrey V. Elsukov 		*mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
94461f37615SAndrey V. Elsukov 		if (*mp == NULL)
94561f37615SAndrey V. Elsukov 			return (ENOBUFS);
94661f37615SAndrey V. Elsukov 		ip6 = mtod(*mp, struct ip6_hdr *);
94761f37615SAndrey V. Elsukov 		ip6->ip6_flow = 0;
94861f37615SAndrey V. Elsukov 		ip6->ip6_vfc = IPV6_VERSION;
94961f37615SAndrey V. Elsukov 		ip6->ip6_hlim = V_ip6_defhlim;
95061f37615SAndrey V. Elsukov 		ip6->ip6_nxt = proto;
95161f37615SAndrey V. Elsukov 		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
9521ae800e7SAndrey V. Elsukov 		/* For link-local address embed scope zone id */
9531ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
9541ae800e7SAndrey V. Elsukov 			ip6->ip6_dst.s6_addr16[1] =
9551ae800e7SAndrey V. Elsukov 			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
95661f37615SAndrey V. Elsukov 		ip6->ip6_src = saidx->src.sin6.sin6_addr;
9571ae800e7SAndrey V. Elsukov 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
9581ae800e7SAndrey V. Elsukov 			ip6->ip6_src.s6_addr16[1] =
9591ae800e7SAndrey V. Elsukov 			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
96061f37615SAndrey V. Elsukov 		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
96161f37615SAndrey V. Elsukov 		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
96261f37615SAndrey V. Elsukov 		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
96361f37615SAndrey V. Elsukov 		break;
96461f37615SAndrey V. Elsukov #endif /* INET6 */
96561f37615SAndrey V. Elsukov 	default:
96661f37615SAndrey V. Elsukov 		return (EAFNOSUPPORT);
96761f37615SAndrey V. Elsukov 	}
968*fcf59617SAndrey V. Elsukov 	(*mp)->m_flags &= ~(M_BCAST | M_MCAST);
96961f37615SAndrey V. Elsukov 	return (0);
97061f37615SAndrey V. Elsukov }
97161f37615SAndrey V. Elsukov 
972