xref: /freebsd/sys/netipsec/ipsec_output.c (revision 302affcb04f3f591582be6eabbfc59b066598f79)
1 /*-
2  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3  * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * IPsec output processing.
32  */
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
36 #include "opt_sctp.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/hhook.h>
46 #include <sys/syslog.h>
47 
48 #include <net/if.h>
49 #include <net/if_enc.h>
50 #include <net/if_var.h>
51 #include <net/vnet.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_ecn.h>
59 #ifdef INET6
60 #include <netinet6/ip6_ecn.h>
61 #endif
62 
63 #include <netinet/ip6.h>
64 #ifdef INET6
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/scope6_var.h>
67 #endif
68 #include <netinet/in_pcb.h>
69 #ifdef INET6
70 #include <netinet/icmp6.h>
71 #endif
72 #ifdef SCTP
73 #include <netinet/sctp_crc32.h>
74 #endif
75 
76 #include <netinet/udp.h>
77 #include <netipsec/ah.h>
78 #include <netipsec/esp.h>
79 #include <netipsec/ipsec.h>
80 #ifdef INET6
81 #include <netipsec/ipsec6.h>
82 #endif
83 #include <netipsec/ah_var.h>
84 #include <netipsec/esp_var.h>
85 #include <netipsec/ipcomp_var.h>
86 
87 #include <netipsec/xform.h>
88 
89 #include <netipsec/key.h>
90 #include <netipsec/keydb.h>
91 #include <netipsec/key_debug.h>
92 
93 #include <machine/in_cksum.h>
94 
95 #define	IPSEC_OSTAT_INC(proto, name)	do {		\
96 	if ((proto) == IPPROTO_ESP)	\
97 		ESPSTAT_INC(esps_##name);	\
98 	else if ((proto) == IPPROTO_AH)\
99 		AHSTAT_INC(ahs_##name);		\
100 	else					\
101 		IPCOMPSTAT_INC(ipcomps_##name);	\
102 } while (0)
103 
104 static int ipsec_encap(struct mbuf **mp, struct secasindex *saidx);
105 
106 #ifdef INET
107 static struct secasvar *
108 ipsec4_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
109 {
110 	struct secasindex *saidx, tmpsaidx;
111 	struct ipsecrequest *isr;
112 	struct sockaddr_in *sin;
113 	struct secasvar *sav;
114 	struct ip *ip;
115 
116 	/*
117 	 * Check system global policy controls.
118 	 */
119 next:
120 	isr = sp->req[*pidx];
121 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
122 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
123 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
124 		DPRINTF(("%s: IPsec outbound packet dropped due"
125 			" to policy (check your sysctls)\n", __func__));
126 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
127 		*error = EHOSTUNREACH;
128 		return (NULL);
129 	}
130 	/*
131 	 * Craft SA index to search for proper SA.  Note that
132 	 * we only initialize unspecified SA peers for transport
133 	 * mode; for tunnel mode they must already be filled in.
134 	 */
135 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
136 		saidx = &tmpsaidx;
137 		*saidx = isr->saidx;
138 		ip = mtod(m, struct ip *);
139 		if (saidx->src.sa.sa_len == 0) {
140 			sin = &saidx->src.sin;
141 			sin->sin_len = sizeof(*sin);
142 			sin->sin_family = AF_INET;
143 			sin->sin_port = IPSEC_PORT_ANY;
144 			sin->sin_addr = ip->ip_src;
145 		}
146 		if (saidx->dst.sa.sa_len == 0) {
147 			sin = &saidx->dst.sin;
148 			sin->sin_len = sizeof(*sin);
149 			sin->sin_family = AF_INET;
150 			sin->sin_port = IPSEC_PORT_ANY;
151 			sin->sin_addr = ip->ip_dst;
152 		}
153 	} else
154 		saidx = &sp->req[*pidx]->saidx;
155 	/*
156 	 * Lookup SA and validate it.
157 	 */
158 	sav = key_allocsa_policy(sp, saidx, error);
159 	if (sav == NULL) {
160 		IPSECSTAT_INC(ips_out_nosa);
161 		if (*error != 0)
162 			return (NULL);
163 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
164 			/*
165 			 * We have no SA and policy that doesn't require
166 			 * this IPsec transform, thus we can continue w/o
167 			 * IPsec processing, i.e. return EJUSTRETURN.
168 			 * But first check if there is some bundled transform.
169 			 */
170 			if (sp->tcount > ++(*pidx))
171 				goto next;
172 			*error = EJUSTRETURN;
173 		}
174 		return (NULL);
175 	}
176 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
177 	return (sav);
178 }
179 
180 /*
181  * IPsec output logic for IPv4.
182  */
183 static int
184 ipsec4_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
185 {
186 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
187 	struct ipsec_ctx_data ctx;
188 	union sockaddr_union *dst;
189 	struct secasvar *sav;
190 	struct ip *ip;
191 	int error, i, off;
192 
193 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
194 
195 	/*
196 	 * We hold the reference to SP. Content of SP couldn't be changed.
197 	 * Craft secasindex and do lookup for suitable SA.
198 	 * Then do encapsulation if needed and call xform's output.
199 	 * We need to store SP in the xform callback parameters.
200 	 * In xform callback we will extract SP and it can be used to
201 	 * determine next transform. At the end of transform we can
202 	 * release reference to SP.
203 	 */
204 	sav = ipsec4_allocsa(m, sp, &idx, &error);
205 	if (sav == NULL) {
206 		if (error == EJUSTRETURN) { /* No IPsec required */
207 			key_freesp(&sp);
208 			return (error);
209 		}
210 		goto bad;
211 	}
212 	/*
213 	 * XXXAE: most likely ip_sum at this point is wrong.
214 	 */
215 	IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET, IPSEC_ENC_BEFORE);
216 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
217 		goto bad;
218 
219 	ip = mtod(m, struct ip *);
220 	dst = &sav->sah->saidx.dst;
221 	/* Do the appropriate encapsulation, if necessary */
222 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
223 	    dst->sa.sa_family != AF_INET ||	    /* PF mismatch */
224 	    (dst->sa.sa_family == AF_INET &&	    /* Proxy */
225 	     dst->sin.sin_addr.s_addr != INADDR_ANY &&
226 	     dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
227 		/* Fix IPv4 header checksum and length */
228 		ip->ip_len = htons(m->m_pkthdr.len);
229 		ip->ip_sum = 0;
230 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
231 		error = ipsec_encap(&m, &sav->sah->saidx);
232 		if (error != 0) {
233 			DPRINTF(("%s: encapsulation for SA %s->%s "
234 			    "SPI 0x%08x failed with error %d\n", __func__,
235 			    ipsec_address(&sav->sah->saidx.src, sbuf,
236 				sizeof(sbuf)),
237 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
238 				sizeof(dbuf)), ntohl(sav->spi), error));
239 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
240 			goto bad;
241 		}
242 	}
243 
244 	IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
245 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
246 		goto bad;
247 
248 	/*
249 	 * Dispatch to the appropriate IPsec transform logic.  The
250 	 * packet will be returned for transmission after crypto
251 	 * processing, etc. are completed.
252 	 *
253 	 * NB: m & sav are ``passed to caller'' who's responsible for
254 	 *     reclaiming their resources.
255 	 */
256 	switch(dst->sa.sa_family) {
257 	case AF_INET:
258 		ip = mtod(m, struct ip *);
259 		i = ip->ip_hl << 2;
260 		off = offsetof(struct ip, ip_p);
261 		break;
262 #ifdef INET6
263 	case AF_INET6:
264 		i = sizeof(struct ip6_hdr);
265 		off = offsetof(struct ip6_hdr, ip6_nxt);
266 		break;
267 #endif /* INET6 */
268 	default:
269 		DPRINTF(("%s: unsupported protocol family %u\n",
270 		    __func__, dst->sa.sa_family));
271 		error = EPFNOSUPPORT;
272 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
273 		goto bad;
274 	}
275 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
276 	return (error);
277 bad:
278 	IPSECSTAT_INC(ips_out_inval);
279 	if (m != NULL)
280 		m_freem(m);
281 	if (sav != NULL)
282 		key_freesav(&sav);
283 	key_freesp(&sp);
284 	return (error);
285 }
286 
287 int
288 ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp,
289     struct inpcb *inp)
290 {
291 
292 	return (ipsec4_perform_request(m, sp, 0));
293 }
294 
295 static int
296 ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
297 {
298 	struct secpolicy *sp;
299 	int error;
300 
301 	/* Lookup for the corresponding outbound security policy */
302 	sp = ipsec4_checkpolicy(m, inp, &error);
303 	if (sp == NULL) {
304 		if (error == -EINVAL) {
305 			/* Discarded by policy. */
306 			m_freem(m);
307 			return (EACCES);
308 		}
309 		return (0); /* No IPsec required. */
310 	}
311 
312 	/*
313 	 * Usually we have to have tunnel mode IPsec security policy
314 	 * when we are forwarding a packet. Otherwise we could not handle
315 	 * encrypted replies, because they are not destined for us. But
316 	 * some users are doing source address translation for forwarded
317 	 * packets, and thus, even if they are forwarded, the replies will
318 	 * return back to us.
319 	 */
320 	if (!forwarding) {
321 		/*
322 		 * Do delayed checksums now because we send before
323 		 * this is done in the normal processing path.
324 		 */
325 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
326 			in_delayed_cksum(m);
327 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
328 		}
329 #ifdef SCTP
330 		if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
331 			struct ip *ip = mtod(m, struct ip *);
332 
333 			sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
334 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
335 		}
336 #endif
337 	}
338 	/* NB: callee frees mbuf and releases reference to SP */
339 	error = ipsec4_process_packet(m, sp, inp);
340 	if (error == EJUSTRETURN) {
341 		/*
342 		 * We had a SP with a level of 'use' and no SA. We
343 		 * will just continue to process the packet without
344 		 * IPsec processing and return without error.
345 		 */
346 		return (0);
347 	}
348 	if (error == 0)
349 		return (EINPROGRESS); /* consumed by IPsec */
350 	return (error);
351 }
352 
353 /*
354  * IPSEC_OUTPUT() method implementation for IPv4.
355  * 0 - no IPsec handling needed
356  * other values - mbuf consumed by IPsec.
357  */
358 int
359 ipsec4_output(struct mbuf *m, struct inpcb *inp)
360 {
361 
362 	/*
363 	 * If the packet is resubmitted to ip_output (e.g. after
364 	 * AH, ESP, etc. processing), there will be a tag to bypass
365 	 * the lookup and related policy checking.
366 	 */
367 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
368 		return (0);
369 
370 	return (ipsec4_common_output(m, inp, 0));
371 }
372 
373 /*
374  * IPSEC_FORWARD() method implementation for IPv4.
375  * 0 - no IPsec handling needed
376  * other values - mbuf consumed by IPsec.
377  */
378 int
379 ipsec4_forward(struct mbuf *m)
380 {
381 
382 	/*
383 	 * Check if this packet has an active inbound SP and needs to be
384 	 * dropped instead of forwarded.
385 	 */
386 	if (ipsec4_in_reject(m, NULL) != 0) {
387 		m_freem(m);
388 		return (EACCES);
389 	}
390 	return (ipsec4_common_output(m, NULL, 1));
391 }
392 #endif
393 
394 #ifdef INET6
395 static int
396 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa,
397     const struct in6_addr *ia)
398 {
399 	struct in6_addr ia2;
400 
401 	if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr)) {
402 		memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
403 		ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
404 		return (IN6_ARE_ADDR_EQUAL(ia, &ia2));
405 	}
406 	return (IN6_ARE_ADDR_EQUAL(&sa->sin6_addr, ia));
407 }
408 
409 static struct secasvar *
410 ipsec6_allocsa(struct mbuf *m, struct secpolicy *sp, u_int *pidx, int *error)
411 {
412 	struct secasindex *saidx, tmpsaidx;
413 	struct ipsecrequest *isr;
414 	struct sockaddr_in6 *sin6;
415 	struct secasvar *sav;
416 	struct ip6_hdr *ip6;
417 
418 	/*
419 	 * Check system global policy controls.
420 	 */
421 next:
422 	isr = sp->req[*pidx];
423 	if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
424 	    (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
425 	    (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
426 		DPRINTF(("%s: IPsec outbound packet dropped due"
427 			" to policy (check your sysctls)\n", __func__));
428 		IPSEC_OSTAT_INC(isr->saidx.proto, pdrops);
429 		*error = EHOSTUNREACH;
430 		return (NULL);
431 	}
432 	/*
433 	 * Craft SA index to search for proper SA.  Note that
434 	 * we only fillin unspecified SA peers for transport
435 	 * mode; for tunnel mode they must already be filled in.
436 	 */
437 	if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
438 		saidx = &tmpsaidx;
439 		*saidx = isr->saidx;
440 		ip6 = mtod(m, struct ip6_hdr *);
441 		if (saidx->src.sin6.sin6_len == 0) {
442 			sin6 = (struct sockaddr_in6 *)&saidx->src;
443 			sin6->sin6_len = sizeof(*sin6);
444 			sin6->sin6_family = AF_INET6;
445 			sin6->sin6_port = IPSEC_PORT_ANY;
446 			sin6->sin6_addr = ip6->ip6_src;
447 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
448 				/* fix scope id for comparing SPD */
449 				sin6->sin6_addr.s6_addr16[1] = 0;
450 				sin6->sin6_scope_id =
451 				    ntohs(ip6->ip6_src.s6_addr16[1]);
452 			}
453 		}
454 		if (saidx->dst.sin6.sin6_len == 0) {
455 			sin6 = (struct sockaddr_in6 *)&saidx->dst;
456 			sin6->sin6_len = sizeof(*sin6);
457 			sin6->sin6_family = AF_INET6;
458 			sin6->sin6_port = IPSEC_PORT_ANY;
459 			sin6->sin6_addr = ip6->ip6_dst;
460 			if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
461 				/* fix scope id for comparing SPD */
462 				sin6->sin6_addr.s6_addr16[1] = 0;
463 				sin6->sin6_scope_id =
464 				    ntohs(ip6->ip6_dst.s6_addr16[1]);
465 			}
466 		}
467 	} else
468 		saidx = &sp->req[*pidx]->saidx;
469 	/*
470 	 * Lookup SA and validate it.
471 	 */
472 	sav = key_allocsa_policy(sp, saidx, error);
473 	if (sav == NULL) {
474 		IPSEC6STAT_INC(ips_out_nosa);
475 		if (*error != 0)
476 			return (NULL);
477 		if (ipsec_get_reqlevel(sp, *pidx) != IPSEC_LEVEL_REQUIRE) {
478 			/*
479 			 * We have no SA and policy that doesn't require
480 			 * this IPsec transform, thus we can continue w/o
481 			 * IPsec processing, i.e. return EJUSTRETURN.
482 			 * But first check if there is some bundled transform.
483 			 */
484 			if (sp->tcount > ++(*pidx))
485 				goto next;
486 			*error = EJUSTRETURN;
487 		}
488 		return (NULL);
489 	}
490 	IPSEC_ASSERT(sav->tdb_xform != NULL, ("SA with NULL tdb_xform"));
491 	return (sav);
492 }
493 
494 /*
495  * IPsec output logic for IPv6.
496  */
497 static int
498 ipsec6_perform_request(struct mbuf *m, struct secpolicy *sp, u_int idx)
499 {
500 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
501 	struct ipsec_ctx_data ctx;
502 	union sockaddr_union *dst;
503 	struct secasvar *sav;
504 	struct ip6_hdr *ip6;
505 	int error, i, off;
506 
507 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
508 
509 	sav = ipsec6_allocsa(m, sp, &idx, &error);
510 	if (sav == NULL) {
511 		if (error == EJUSTRETURN) { /* No IPsec required */
512 			key_freesp(&sp);
513 			return (error);
514 		}
515 		goto bad;
516 	}
517 
518 	/* Fix IP length in case if it is not set yet. */
519 	ip6 = mtod(m, struct ip6_hdr *);
520 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
521 
522 	IPSEC_INIT_CTX(&ctx, &m, sav, AF_INET6, IPSEC_ENC_BEFORE);
523 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
524 		goto bad;
525 
526 	ip6 = mtod(m, struct ip6_hdr *); /* pfil can change mbuf */
527 	dst = &sav->sah->saidx.dst;
528 
529 	/* Do the appropriate encapsulation, if necessary */
530 	if (sp->req[idx]->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
531 	    dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
532 	    ((dst->sa.sa_family == AF_INET6) &&
533 	     (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
534 	     (!in6_sa_equal_addrwithscope(&dst->sin6, &ip6->ip6_dst)))) {
535 		if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
536 			/* No jumbogram support. */
537 			error = ENXIO;   /*XXX*/
538 			goto bad;
539 		}
540 		error = ipsec_encap(&m, &sav->sah->saidx);
541 		if (error != 0) {
542 			DPRINTF(("%s: encapsulation for SA %s->%s "
543 			    "SPI 0x%08x failed with error %d\n", __func__,
544 			    ipsec_address(&sav->sah->saidx.src, sbuf,
545 				sizeof(sbuf)),
546 			    ipsec_address(&sav->sah->saidx.dst, dbuf,
547 				sizeof(dbuf)), ntohl(sav->spi), error));
548 			/* XXXAE: IPSEC_OSTAT_INC(tunnel); */
549 			goto bad;
550 		}
551 	}
552 
553 	IPSEC_INIT_CTX(&ctx, &m, sav, dst->sa.sa_family, IPSEC_ENC_AFTER);
554 	if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_OUT)) != 0)
555 		goto bad;
556 
557 	switch(dst->sa.sa_family) {
558 #ifdef INET
559 	case AF_INET:
560 		{
561 		struct ip *ip;
562 		ip = mtod(m, struct ip *);
563 		i = ip->ip_hl << 2;
564 		off = offsetof(struct ip, ip_p);
565 		}
566 		break;
567 #endif /* AF_INET */
568 	case AF_INET6:
569 		i = sizeof(struct ip6_hdr);
570 		off = offsetof(struct ip6_hdr, ip6_nxt);
571 		break;
572 	default:
573 		DPRINTF(("%s: unsupported protocol family %u\n",
574 				 __func__, dst->sa.sa_family));
575 		error = EPFNOSUPPORT;
576 		IPSEC_OSTAT_INC(sav->sah->saidx.proto, nopf);
577 		goto bad;
578 	}
579 	error = (*sav->tdb_xform->xf_output)(m, sp, sav, idx, i, off);
580 	return (error);
581 bad:
582 	IPSEC6STAT_INC(ips_out_inval);
583 	if (m != NULL)
584 		m_freem(m);
585 	if (sav != NULL)
586 		key_freesav(&sav);
587 	key_freesp(&sp);
588 	return (error);
589 }
590 
591 int
592 ipsec6_process_packet(struct mbuf *m, struct secpolicy *sp,
593     struct inpcb *inp)
594 {
595 
596 	return (ipsec6_perform_request(m, sp, 0));
597 }
598 
599 static int
600 ipsec6_common_output(struct mbuf *m, struct inpcb *inp, int forwarding)
601 {
602 	struct secpolicy *sp;
603 	int error;
604 
605 	/* Lookup for the corresponding outbound security policy */
606 	sp = ipsec6_checkpolicy(m, inp, &error);
607 	if (sp == NULL) {
608 		if (error == -EINVAL) {
609 			/* Discarded by policy. */
610 			m_freem(m);
611 			return (EACCES);
612 		}
613 		return (0); /* No IPsec required. */
614 	}
615 
616 	if (!forwarding) {
617 		/*
618 		 * Do delayed checksums now because we send before
619 		 * this is done in the normal processing path.
620 		 */
621 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
622 			in6_delayed_cksum(m, m->m_pkthdr.len -
623 			    sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
624 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
625 		}
626 #ifdef SCTP
627 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
628 			sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
629 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
630 		}
631 #endif
632 	}
633 	/* NB: callee frees mbuf and releases reference to SP */
634 	error = ipsec6_process_packet(m, sp, inp);
635 	if (error == EJUSTRETURN) {
636 		/*
637 		 * We had a SP with a level of 'use' and no SA. We
638 		 * will just continue to process the packet without
639 		 * IPsec processing and return without error.
640 		 */
641 		return (0);
642 	}
643 	if (error == 0)
644 		return (EINPROGRESS); /* consumed by IPsec */
645 	return (error);
646 }
647 
648 /*
649  * IPSEC_OUTPUT() method implementation for IPv6.
650  * 0 - no IPsec handling needed
651  * other values - mbuf consumed by IPsec.
652  */
653 int
654 ipsec6_output(struct mbuf *m, struct inpcb *inp)
655 {
656 
657 	/*
658 	 * If the packet is resubmitted to ip_output (e.g. after
659 	 * AH, ESP, etc. processing), there will be a tag to bypass
660 	 * the lookup and related policy checking.
661 	 */
662 	if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL)
663 		return (0);
664 
665 	return (ipsec6_common_output(m, inp, 0));
666 }
667 
668 /*
669  * IPSEC_FORWARD() method implementation for IPv6.
670  * 0 - no IPsec handling needed
671  * other values - mbuf consumed by IPsec.
672  */
673 int
674 ipsec6_forward(struct mbuf *m)
675 {
676 
677 	/*
678 	 * Check if this packet has an active inbound SP and needs to be
679 	 * dropped instead of forwarded.
680 	 */
681 	if (ipsec6_in_reject(m, NULL) != 0) {
682 		m_freem(m);
683 		return (EACCES);
684 	}
685 	return (ipsec6_common_output(m, NULL, 1));
686 }
687 #endif /* INET6 */
688 
689 int
690 ipsec_process_done(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
691     u_int idx)
692 {
693 	struct xform_history *xh;
694 	struct secasindex *saidx;
695 	struct m_tag *mtag;
696 	int error;
697 
698 	saidx = &sav->sah->saidx;
699 	switch (saidx->dst.sa.sa_family) {
700 #ifdef INET
701 	case AF_INET:
702 		/* Fix the header length, for AH processing. */
703 		mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
704 		break;
705 #endif /* INET */
706 #ifdef INET6
707 	case AF_INET6:
708 		/* Fix the header length, for AH processing. */
709 		if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
710 			error = ENXIO;
711 			goto bad;
712 		}
713 		if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
714 			/* No jumbogram support. */
715 			error = ENXIO;	/*?*/
716 			goto bad;
717 		}
718 		mtod(m, struct ip6_hdr *)->ip6_plen =
719 			htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
720 		break;
721 #endif /* INET6 */
722 	default:
723 		DPRINTF(("%s: unknown protocol family %u\n", __func__,
724 		    saidx->dst.sa.sa_family));
725 		error = ENXIO;
726 		goto bad;
727 	}
728 
729 	/*
730 	 * Add a record of what we've done to the packet.
731 	 */
732 	mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, sizeof(*xh), M_NOWAIT);
733 	if (mtag == NULL) {
734 		DPRINTF(("%s: could not get packet tag\n", __func__));
735 		error = ENOMEM;
736 		goto bad;
737 	}
738 
739 	xh = (struct xform_history *)(mtag + 1);
740 	xh->dst = saidx->dst;
741 	xh->proto = saidx->proto;
742 	xh->mode = saidx->mode;
743 	xh->spi = sav->spi;
744 	m_tag_prepend(m, mtag);
745 
746 	key_sa_recordxfer(sav, m);		/* record data transfer */
747 
748 	/*
749 	 * If there's another (bundled) SA to apply, do so.
750 	 * Note that this puts a burden on the kernel stack size.
751 	 * If this is a problem we'll need to introduce a queue
752 	 * to set the packet on so we can unwind the stack before
753 	 * doing further processing.
754 	 */
755 	if (++idx < sp->tcount) {
756 		switch (saidx->dst.sa.sa_family) {
757 #ifdef INET
758 		case AF_INET:
759 			key_freesav(&sav);
760 			IPSECSTAT_INC(ips_out_bundlesa);
761 			return (ipsec4_perform_request(m, sp, idx));
762 			/* NOTREACHED */
763 #endif
764 #ifdef INET6
765 		case AF_INET6:
766 			key_freesav(&sav);
767 			IPSEC6STAT_INC(ips_out_bundlesa);
768 			return (ipsec6_perform_request(m, sp, idx));
769 			/* NOTREACHED */
770 #endif /* INET6 */
771 		default:
772 			DPRINTF(("%s: unknown protocol family %u\n", __func__,
773 			    saidx->dst.sa.sa_family));
774 			error = EPFNOSUPPORT;
775 			goto bad;
776 		}
777 	}
778 
779 	key_freesp(&sp), sp = NULL;	/* Release reference to SP */
780 #ifdef INET
781 	/*
782 	 * Do UDP encapsulation if SA requires it.
783 	 */
784 	if (sav->natt != NULL) {
785 		error = udp_ipsec_output(m, sav);
786 		if (error != 0)
787 			goto bad;
788 	}
789 #endif /* INET */
790 	/*
791 	 * We're done with IPsec processing, transmit the packet using the
792 	 * appropriate network protocol (IP or IPv6).
793 	 */
794 	switch (saidx->dst.sa.sa_family) {
795 #ifdef INET
796 	case AF_INET:
797 		key_freesav(&sav);
798 		return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
799 #endif /* INET */
800 #ifdef INET6
801 	case AF_INET6:
802 		key_freesav(&sav);
803 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
804 #endif /* INET6 */
805 	}
806 	panic("ipsec_process_done");
807 bad:
808 	m_freem(m);
809 	key_freesav(&sav);
810 	if (sp != NULL)
811 		key_freesp(&sp);
812 	return (error);
813 }
814 
815 /*
816  * ipsec_prepend() is optimized version of M_PREPEND().
817  * ipsec_encap() is called by IPsec output routine for tunnel mode SA.
818  * It is expected that after IP encapsulation some IPsec transform will
819  * be performed. Each IPsec transform inserts its variable length header
820  * just after outer IP header using m_makespace(). If given mbuf has not
821  * enough free space at the beginning, we allocate new mbuf and reserve
822  * some space at the beginning and at the end.
823  * This helps avoid allocating of new mbuf and data copying in m_makespace(),
824  * we place outer header in the middle of mbuf's data with reserved leading
825  * and trailing space:
826  *	[ LEADINGSPACE ][ Outer IP header ][ TRAILINGSPACE ]
827  * LEADINGSPACE will be used to add ethernet header, TRAILINGSPACE will
828  * be used to inject AH/ESP/IPCOMP header.
829  */
830 #define	IPSEC_TRAILINGSPACE	(sizeof(struct udphdr) +/* NAT-T */	\
831     max(sizeof(struct newesp) + EALG_MAX_BLOCK_LEN,	/* ESP + IV */	\
832 	sizeof(struct newah) + HASH_MAX_LEN		/* AH + ICV */))
833 static struct mbuf *
834 ipsec_prepend(struct mbuf *m, int len, int how)
835 {
836 	struct mbuf *n;
837 
838 	M_ASSERTPKTHDR(m);
839 	IPSEC_ASSERT(len < MHLEN, ("wrong length"));
840 	if (M_LEADINGSPACE(m) >= len) {
841 		/* No need to allocate new mbuf. */
842 		m->m_data -= len;
843 		m->m_len += len;
844 		m->m_pkthdr.len += len;
845 		return (m);
846 	}
847 	n = m_gethdr(how, m->m_type);
848 	if (n == NULL) {
849 		m_freem(m);
850 		return (NULL);
851 	}
852 	m_move_pkthdr(n, m);
853 	n->m_next = m;
854 	if (len + IPSEC_TRAILINGSPACE < M_SIZE(n))
855 		m_align(n, len + IPSEC_TRAILINGSPACE);
856 	n->m_len = len;
857 	n->m_pkthdr.len += len;
858 	return (n);
859 }
860 
861 static int
862 ipsec_encap(struct mbuf **mp, struct secasindex *saidx)
863 {
864 #ifdef INET6
865 	struct ip6_hdr *ip6;
866 #endif
867 	struct ip *ip;
868 	int setdf;
869 	uint8_t itos, proto;
870 
871 	ip = mtod(*mp, struct ip *);
872 	switch (ip->ip_v) {
873 #ifdef INET
874 	case IPVERSION:
875 		proto = IPPROTO_IPIP;
876 		/*
877 		 * Collect IP_DF state from the inner header
878 		 * and honor system-wide control of how to handle it.
879 		 */
880 		switch (V_ip4_ipsec_dfbit) {
881 		case 0:	/* clear in outer header */
882 		case 1:	/* set in outer header */
883 			setdf = V_ip4_ipsec_dfbit;
884 			break;
885 		default:/* propagate to outer header */
886 			setdf = (ip->ip_off & htons(IP_DF)) != 0;
887 		}
888 		itos = ip->ip_tos;
889 		break;
890 #endif
891 #ifdef INET6
892 	case (IPV6_VERSION >> 4):
893 		proto = IPPROTO_IPV6;
894 		ip6 = mtod(*mp, struct ip6_hdr *);
895 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
896 		setdf = V_ip4_ipsec_dfbit ? 1: 0;
897 		/* scoped address handling */
898 		in6_clearscope(&ip6->ip6_src);
899 		in6_clearscope(&ip6->ip6_dst);
900 		break;
901 #endif
902 	default:
903 		return (EAFNOSUPPORT);
904 	}
905 	switch (saidx->dst.sa.sa_family) {
906 #ifdef INET
907 	case AF_INET:
908 		if (saidx->src.sa.sa_family != AF_INET ||
909 		    saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
910 		    saidx->dst.sin.sin_addr.s_addr == INADDR_ANY)
911 			return (EINVAL);
912 		*mp = ipsec_prepend(*mp, sizeof(struct ip), M_NOWAIT);
913 		if (*mp == NULL)
914 			return (ENOBUFS);
915 		ip = mtod(*mp, struct ip *);
916 		ip->ip_v = IPVERSION;
917 		ip->ip_hl = sizeof(struct ip) >> 2;
918 		ip->ip_p = proto;
919 		ip->ip_len = htons((*mp)->m_pkthdr.len);
920 		ip->ip_ttl = V_ip_defttl;
921 		ip->ip_sum = 0;
922 		ip->ip_off = setdf ? htons(IP_DF): 0;
923 		ip->ip_src = saidx->src.sin.sin_addr;
924 		ip->ip_dst = saidx->dst.sin.sin_addr;
925 		ip_ecn_ingress(V_ip4_ipsec_ecn, &ip->ip_tos, &itos);
926 		ip_fillid(ip);
927 		break;
928 #endif /* INET */
929 #ifdef INET6
930 	case AF_INET6:
931 		if (saidx->src.sa.sa_family != AF_INET6 ||
932 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr) ||
933 		    IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr))
934 			return (EINVAL);
935 		*mp = ipsec_prepend(*mp, sizeof(struct ip6_hdr), M_NOWAIT);
936 		if (*mp == NULL)
937 			return (ENOBUFS);
938 		ip6 = mtod(*mp, struct ip6_hdr *);
939 		ip6->ip6_flow = 0;
940 		ip6->ip6_vfc = IPV6_VERSION;
941 		ip6->ip6_hlim = V_ip6_defhlim;
942 		ip6->ip6_nxt = proto;
943 		ip6->ip6_dst = saidx->dst.sin6.sin6_addr;
944 		/* For link-local address embed scope zone id */
945 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
946 			ip6->ip6_dst.s6_addr16[1] =
947 			    htons(saidx->dst.sin6.sin6_scope_id & 0xffff);
948 		ip6->ip6_src = saidx->src.sin6.sin6_addr;
949 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
950 			ip6->ip6_src.s6_addr16[1] =
951 			    htons(saidx->src.sin6.sin6_scope_id & 0xffff);
952 		ip6->ip6_plen = htons((*mp)->m_pkthdr.len - sizeof(*ip6));
953 		ip_ecn_ingress(V_ip6_ipsec_ecn, &proto, &itos);
954 		ip6->ip6_flow |= htonl((uint32_t)proto << 20);
955 		break;
956 #endif /* INET6 */
957 	default:
958 		return (EAFNOSUPPORT);
959 	}
960 	(*mp)->m_flags &= ~(M_BCAST | M_MCAST);
961 	return (0);
962 }
963 
964