xref: /xnu-8792.41.9/bsd/netinet6/esp_input.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1 /*
2  * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*	$FreeBSD: src/sys/netinet6/esp_input.c,v 1.1.2.3 2001/07/03 11:01:50 ume Exp $	*/
30 /*	$KAME: esp_input.c,v 1.55 2001/03/23 08:08:47 itojun Exp $	*/
31 
32 /*
33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the project nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 #define _IP_VHL
62 
63 /*
64  * RFC1827/2406 Encapsulated Security Payload.
65  */
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/mcache.h>
72 #include <sys/domain.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 
80 #include <net/if.h>
81 #include <net/if_ipsec.h>
82 #include <net/multi_layer_pkt_log.h>
83 #include <net/route.h>
84 #include <net/if_ports_used.h>
85 #include <kern/cpu_number.h>
86 #include <kern/locks.h>
87 
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip_ecn.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/udp.h>
96 #include <netinet/tcp.h>
97 #include <netinet/in_tclass.h>
98 #include <netinet6/ip6_ecn.h>
99 
100 #include <netinet/ip6.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6protosw.h>
105 
106 #include <netinet6/ipsec.h>
107 #include <netinet6/ipsec6.h>
108 #include <netinet6/ah.h>
109 #include <netinet6/ah6.h>
110 #include <netinet6/esp.h>
111 #include <netinet6/esp6.h>
112 #include <netkey/key.h>
113 #include <netkey/keydb.h>
114 #include <netkey/key_debug.h>
115 
116 #include <net/kpi_protocol.h>
117 #include <netinet/kpi_ipfilter_var.h>
118 
119 #include <net/net_osdep.h>
120 #include <mach/sdt.h>
121 #include <corecrypto/cc.h>
122 
123 #include <sys/kdebug.h>
124 #define DBG_LAYER_BEG           NETDBG_CODE(DBG_NETIPSEC, 1)
125 #define DBG_LAYER_END           NETDBG_CODE(DBG_NETIPSEC, 3)
126 #define DBG_FNC_ESPIN           NETDBG_CODE(DBG_NETIPSEC, (6 << 8))
127 #define DBG_FNC_DECRYPT         NETDBG_CODE(DBG_NETIPSEC, (7 << 8))
128 #define IPLEN_FLIPPED
129 
130 #define ESPMAXLEN \
131 	(sizeof(struct esp) < sizeof(struct newesp) \
132 	        ? sizeof(struct newesp) : sizeof(struct esp))
133 
134 static struct ip *
esp4_input_strip_udp_encap(struct mbuf * m,int iphlen)135 esp4_input_strip_udp_encap(struct mbuf *m, int iphlen)
136 {
137 	// strip the udp header that's encapsulating ESP
138 	struct ip *ip;
139 	u_int8_t stripsiz = (u_int8_t)sizeof(struct udphdr);
140 
141 	ip = mtod(m, __typeof__(ip));
142 	ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), iphlen);
143 	m->m_data += stripsiz;
144 	m->m_len -= stripsiz;
145 	m->m_pkthdr.len -= stripsiz;
146 	ip = mtod(m, __typeof__(ip));
147 	ip->ip_len = ip->ip_len - stripsiz;
148 	ip->ip_p = IPPROTO_ESP;
149 	return ip;
150 }
151 
152 static struct ip6_hdr *
esp6_input_strip_udp_encap(struct mbuf * m,int ip6hlen)153 esp6_input_strip_udp_encap(struct mbuf *m, int ip6hlen)
154 {
155 	// strip the udp header that's encapsulating ESP
156 	struct ip6_hdr *ip6;
157 	u_int8_t stripsiz = (u_int8_t)sizeof(struct udphdr);
158 
159 	ip6 = mtod(m, __typeof__(ip6));
160 	ovbcopy((caddr_t)ip6, (caddr_t)(((u_char *)ip6) + stripsiz), ip6hlen);
161 	m->m_data += stripsiz;
162 	m->m_len -= stripsiz;
163 	m->m_pkthdr.len -= stripsiz;
164 	ip6 = mtod(m, __typeof__(ip6));
165 	ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
166 	ip6->ip6_nxt = IPPROTO_ESP;
167 	return ip6;
168 }
169 
170 static void
esp_input_log(struct mbuf * m,struct secasvar * sav,u_int32_t spi,u_int32_t seq)171 esp_input_log(struct mbuf *m, struct secasvar *sav, u_int32_t spi, u_int32_t seq)
172 {
173 	if (net_mpklog_enabled &&
174 	    (sav->sah->ipsec_if->if_xflags & IFXF_MPK_LOG) == IFXF_MPK_LOG) {
175 		struct tcphdr th = {};
176 		u_int32_t proto_len = 0;
177 		u_int8_t iphlen = 0;
178 		u_int8_t proto = 0;
179 
180 		struct ip *inner_ip = mtod(m, struct ip *);
181 		if (IP_VHL_V(inner_ip->ip_vhl) == 4) {
182 			iphlen = (u_int8_t)(IP_VHL_HL(inner_ip->ip_vhl) << 2);
183 			proto = inner_ip->ip_p;
184 		} else if (IP_VHL_V(inner_ip->ip_vhl) == 6) {
185 			struct ip6_hdr *inner_ip6 = mtod(m, struct ip6_hdr *);
186 			iphlen = sizeof(struct ip6_hdr);
187 			proto = inner_ip6->ip6_nxt;
188 		}
189 
190 		if (proto == IPPROTO_TCP) {
191 			if ((int)(iphlen + sizeof(th)) <= m->m_pkthdr.len) {
192 				m_copydata(m, iphlen, sizeof(th), (u_int8_t *)&th);
193 			}
194 
195 			proto_len = m->m_pkthdr.len - iphlen - (th.th_off << 2);
196 			MPKL_ESP_INPUT_TCP(esp_mpkl_log_object,
197 			    ntohl(spi), seq,
198 			    ntohs(th.th_sport), ntohs(th.th_dport),
199 			    ntohl(th.th_seq), proto_len);
200 		}
201 	}
202 }
203 
204 void
esp4_input(struct mbuf * m,int off)205 esp4_input(struct mbuf *m, int off)
206 {
207 	(void)esp4_input_extended(m, off, NULL);
208 }
209 
210 struct mbuf *
esp4_input_extended(struct mbuf * m,int off,ifnet_t interface)211 esp4_input_extended(struct mbuf *m, int off, ifnet_t interface)
212 {
213 	struct ip *ip;
214 	struct ip6_hdr *ip6;
215 	struct esp *esp;
216 	struct esptail esptail;
217 	u_int32_t spi;
218 	u_int32_t seq;
219 	struct secasvar *sav = NULL;
220 	size_t taillen;
221 	u_int16_t nxt;
222 	const struct esp_algorithm *algo;
223 	int ivlen;
224 	size_t esplen;
225 	u_int8_t hlen;
226 	sa_family_t     ifamily;
227 	struct mbuf *out_m = NULL;
228 	mbuf_traffic_class_t traffic_class = 0;
229 
230 	KERNEL_DEBUG(DBG_FNC_ESPIN | DBG_FUNC_START, 0, 0, 0, 0, 0);
231 	/* sanity check for alignment. */
232 	if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
233 		ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
234 		    "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
235 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
236 		goto bad;
237 	}
238 
239 	if (m->m_len < off + ESPMAXLEN) {
240 		m = m_pullup(m, off + ESPMAXLEN);
241 		if (!m) {
242 			ipseclog((LOG_DEBUG,
243 			    "IPv4 ESP input: can't pullup in esp4_input\n"));
244 			IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
245 			goto bad;
246 		}
247 	}
248 
249 	m->m_pkthdr.csum_flags &= ~CSUM_RX_FLAGS;
250 
251 	/* Expect 32-bit aligned data pointer on strict-align platforms */
252 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
253 
254 	ip = mtod(m, struct ip *);
255 	// expect udp-encap and esp packets only
256 	if (ip->ip_p != IPPROTO_ESP &&
257 	    !(ip->ip_p == IPPROTO_UDP && off >= sizeof(struct udphdr))) {
258 		ipseclog((LOG_DEBUG,
259 		    "IPv4 ESP input: invalid protocol type\n"));
260 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
261 		goto bad;
262 	}
263 	esp = (struct esp *)(void *)(((u_int8_t *)ip) + off);
264 #ifdef _IP_VHL
265 	hlen = (u_int8_t)(IP_VHL_HL(ip->ip_vhl) << 2);
266 #else
267 	hlen = ip->ip_hl << 2;
268 #endif
269 
270 	/* find the sassoc. */
271 	spi = esp->esp_spi;
272 
273 	if ((sav = key_allocsa_extended(AF_INET,
274 	    (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst, IFSCOPE_NONE,
275 	    IPPROTO_ESP, spi, interface)) == 0) {
276 		ipseclog((LOG_WARNING,
277 		    "IPv4 ESP input: no key association found for spi %u (0x%08x)\n",
278 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi)));
279 		IPSEC_STAT_INCREMENT(ipsecstat.in_nosa);
280 		goto bad;
281 	}
282 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
283 	    printf("DP esp4_input called to allocate SA:0x%llx\n",
284 	    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
285 	if (sav->state != SADB_SASTATE_MATURE
286 	    && sav->state != SADB_SASTATE_DYING) {
287 		ipseclog((LOG_DEBUG,
288 		    "IPv4 ESP input: non-mature/dying SA found for spi %u (0x%08x)\n",
289 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi)));
290 		IPSEC_STAT_INCREMENT(ipsecstat.in_badspi);
291 		goto bad;
292 	}
293 	algo = esp_algorithm_lookup(sav->alg_enc);
294 	if (!algo) {
295 		ipseclog((LOG_DEBUG, "IPv4 ESP input: "
296 		    "unsupported encryption algorithm for spi %u (0x%08x)\n",
297 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi)));
298 		IPSEC_STAT_INCREMENT(ipsecstat.in_badspi);
299 		goto bad;
300 	}
301 
302 	/* check if we have proper ivlen information */
303 	ivlen = sav->ivlen;
304 	if (ivlen < 0) {
305 		ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
306 		    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
307 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
308 		goto bad;
309 	}
310 
311 	seq = ntohl(((struct newesp *)esp)->esp_seq);
312 
313 	if ((sav->flags2 & SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) ==
314 	    SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) {
315 		u_int8_t dscp = ip->ip_tos >> IPTOS_DSCP_SHIFT;
316 		traffic_class = rfc4594_dscp_to_tc(dscp);
317 	}
318 
319 	if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL &&
320 	    ((sav->alg_auth && sav->key_auth) || algo->finalizedecrypt))) {
321 		goto noreplaycheck;
322 	}
323 
324 	if ((sav->alg_auth == SADB_X_AALG_NULL || sav->alg_auth == SADB_AALG_NONE) &&
325 	    !algo->finalizedecrypt) {
326 		goto noreplaycheck;
327 	}
328 
329 	/*
330 	 * check for sequence number.
331 	 */
332 	_CASSERT(MBUF_TC_MAX <= UINT8_MAX);
333 	if (ipsec_chkreplay(seq, sav, (u_int8_t)traffic_class)) {
334 		; /*okey*/
335 	} else {
336 		IPSEC_STAT_INCREMENT(ipsecstat.in_espreplay);
337 		ipseclog((LOG_WARNING,
338 		    "replay packet in IPv4 ESP input: seq(%u) tc(%u) %s %s\n",
339 		    seq, (u_int8_t)traffic_class, ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
340 		goto bad;
341 	}
342 
343 	/* Save ICV from packet for verification later */
344 	size_t siz = 0;
345 	unsigned char saved_icv[AH_MAXSUMSIZE] __attribute__((aligned(4)));
346 	if (algo->finalizedecrypt) {
347 		siz = algo->icvlen;
348 		VERIFY(siz <= USHRT_MAX);
349 		m_copydata(m, m->m_pkthdr.len - (u_short)siz, (u_short)siz, (caddr_t) saved_icv);
350 	} else {
351 		/* check ICV immediately */
352 		u_char sum0[AH_MAXSUMSIZE] __attribute__((aligned(4)));
353 		u_char sum[AH_MAXSUMSIZE] __attribute__((aligned(4)));
354 		const struct ah_algorithm *sumalgo;
355 
356 		sumalgo = ah_algorithm_lookup(sav->alg_auth);
357 		if (!sumalgo) {
358 			goto noreplaycheck;
359 		}
360 		siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
361 		if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
362 			IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
363 			goto bad;
364 		}
365 		if (AH_MAXSUMSIZE < siz) {
366 			ipseclog((LOG_DEBUG,
367 			    "internal error: AH_MAXSUMSIZE must be larger than %u\n",
368 			    (u_int32_t)siz));
369 			IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
370 			goto bad;
371 		}
372 
373 		m_copydata(m, m->m_pkthdr.len - (int)siz, (int)siz, (caddr_t) &sum0[0]);
374 
375 		if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
376 			ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
377 			    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
378 			IPSEC_STAT_INCREMENT(ipsecstat.in_espauthfail);
379 			goto bad;
380 		}
381 
382 		if (cc_cmp_safe(siz, sum0, sum)) {
383 			ipseclog((LOG_WARNING, "cc_cmp fail in IPv4 ESP input: %s %s\n",
384 			    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
385 			IPSEC_STAT_INCREMENT(ipsecstat.in_espauthfail);
386 			goto bad;
387 		}
388 
389 		m->m_flags |= M_AUTHIPDGM;
390 		IPSEC_STAT_INCREMENT(ipsecstat.in_espauthsucc);
391 
392 		/*
393 		 * update replay window.
394 		 */
395 		if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL) {
396 			if (ipsec_updatereplay(seq, sav, (u_int8_t)traffic_class)) {
397 				IPSEC_STAT_INCREMENT(ipsecstat.in_espreplay);
398 				goto bad;
399 			}
400 		}
401 	}
402 
403 
404 	/* strip off the authentication data */
405 	m_adj(m, (int)-siz);
406 	ip = mtod(m, struct ip *);
407 #ifdef IPLEN_FLIPPED
408 	ip->ip_len = ip->ip_len - (u_short)siz;
409 #else
410 	ip->ip_len = htons(ntohs(ip->ip_len) - siz);
411 #endif
412 
413 noreplaycheck:
414 
415 	/* process main esp header. */
416 	if (sav->flags & SADB_X_EXT_OLD) {
417 		/* RFC 1827 */
418 		esplen = sizeof(struct esp);
419 	} else {
420 		/* RFC 2406 */
421 		if (sav->flags & SADB_X_EXT_DERIV) {
422 			esplen = sizeof(struct esp);
423 		} else {
424 			esplen = sizeof(struct newesp);
425 		}
426 	}
427 
428 	if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
429 		ipseclog((LOG_WARNING,
430 		    "IPv4 ESP input: packet too short\n"));
431 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
432 		goto bad;
433 	}
434 
435 	if (m->m_len < off + esplen + ivlen) {
436 		m = m_pullup(m, (int)(off + esplen + ivlen));
437 		if (!m) {
438 			ipseclog((LOG_DEBUG,
439 			    "IPv4 ESP input: can't pullup in esp4_input\n"));
440 			IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
441 			goto bad;
442 		}
443 	}
444 
445 	/*
446 	 * pre-compute and cache intermediate key
447 	 */
448 	if (esp_schedule(algo, sav) != 0) {
449 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
450 		goto bad;
451 	}
452 
453 	/*
454 	 * decrypt the packet.
455 	 */
456 	if (!algo->decrypt) {
457 		panic("internal error: no decrypt function");
458 	}
459 	KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_START, 0, 0, 0, 0, 0);
460 	if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
461 		/* m is already freed */
462 		m = NULL;
463 		ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
464 		    ipsec_logsastr(sav)));
465 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
466 		KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 1, 0, 0, 0, 0);
467 		goto bad;
468 	}
469 	KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 2, 0, 0, 0, 0);
470 	IPSEC_STAT_INCREMENT(ipsecstat.in_esphist[sav->alg_enc]);
471 
472 	m->m_flags |= M_DECRYPTED;
473 
474 	if (algo->finalizedecrypt) {
475 		if ((*algo->finalizedecrypt)(sav, saved_icv, algo->icvlen)) {
476 			ipseclog((LOG_ERR, "esp4 packet decryption ICV failure: %s\n",
477 			    ipsec_logsastr(sav)));
478 			IPSEC_STAT_INCREMENT(ipsecstat.in_espauthfail);
479 			KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 1, 0, 0, 0, 0);
480 			goto bad;
481 		} else {
482 			m->m_flags |= M_AUTHIPDGM;
483 			IPSEC_STAT_INCREMENT(ipsecstat.in_espauthsucc);
484 
485 			/*
486 			 * update replay window.
487 			 */
488 			if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL) {
489 				if (ipsec_updatereplay(seq, sav, (u_int8_t)traffic_class)) {
490 					IPSEC_STAT_INCREMENT(ipsecstat.in_espreplay);
491 					goto bad;
492 				}
493 			}
494 		}
495 	}
496 
497 	/*
498 	 * find the trailer of the ESP.
499 	 */
500 	m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
501 	    (caddr_t)&esptail);
502 	nxt = esptail.esp_nxt;
503 	taillen = esptail.esp_padlen + sizeof(esptail);
504 
505 	if (m->m_pkthdr.len < taillen
506 	    || m->m_pkthdr.len - taillen < hlen) { /*?*/
507 		ipseclog((LOG_WARNING,
508 		    "bad pad length in IPv4 ESP input: %s %s\n",
509 		    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
510 		IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
511 		goto bad;
512 	}
513 
514 	/* strip off the trailing pad area. */
515 	m_adj(m, (int)-taillen);
516 	ip = mtod(m, struct ip *);
517 #ifdef IPLEN_FLIPPED
518 	ip->ip_len = ip->ip_len - (u_short)taillen;
519 #else
520 	ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
521 #endif
522 	if (ip->ip_p == IPPROTO_UDP) {
523 		// offset includes the outer ip and udp header lengths.
524 		if (m->m_len < off) {
525 			m = m_pullup(m, off);
526 			if (!m) {
527 				ipseclog((LOG_DEBUG,
528 				    "IPv4 ESP input: invalid udp encapsulated ESP packet length \n"));
529 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
530 				goto bad;
531 			}
532 			ip = mtod(m, struct ip *);
533 		}
534 
535 		// check the UDP encap header to detect changes in the source port, and then strip the header
536 		off -= sizeof(struct udphdr); // off no longer includes the udphdr's size
537 		// if peer is behind nat and this is the latest esp packet
538 		if ((sav->flags & SADB_X_EXT_NATT_DETECTED_PEER) != 0 &&
539 		    (sav->flags & SADB_X_EXT_OLD) == 0 &&
540 		    seq && sav->replay[traffic_class] &&
541 		    seq >= sav->replay[traffic_class]->lastseq) {
542 			struct udphdr *encap_uh = (__typeof__(encap_uh))(void *)((caddr_t)ip + off);
543 			if (encap_uh->uh_sport &&
544 			    ntohs(encap_uh->uh_sport) != sav->remote_ike_port) {
545 				sav->remote_ike_port = ntohs(encap_uh->uh_sport);
546 			}
547 		}
548 		ip = esp4_input_strip_udp_encap(m, off);
549 		esp = (struct esp *)(void *)(((u_int8_t *)ip) + off);
550 	}
551 
552 	/* was it transmitted over the IPsec tunnel SA? */
553 	if (ipsec4_tunnel_validate(m, (int)(off + esplen + ivlen), nxt, sav, &ifamily)) {
554 		ifaddr_t ifa;
555 		struct sockaddr_storage addr;
556 
557 		/*
558 		 * strip off all the headers that precedes ESP header.
559 		 *	IP4 xx ESP IP4' payload -> IP4' payload
560 		 *
561 		 * XXX more sanity checks
562 		 * XXX relationship with gif?
563 		 */
564 		u_int8_t tos, otos;
565 		u_int8_t inner_ip_proto = 0;
566 		int sum;
567 
568 		tos = ip->ip_tos;
569 		m_adj(m, (int)(off + esplen + ivlen));
570 		if (ifamily == AF_INET) {
571 			struct sockaddr_in *ipaddr;
572 
573 			if (m->m_len < sizeof(*ip)) {
574 				m = m_pullup(m, sizeof(*ip));
575 				if (!m) {
576 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
577 					goto bad;
578 				}
579 			}
580 			ip = mtod(m, struct ip *);
581 			/* ECN consideration. */
582 
583 			otos = ip->ip_tos;
584 			if (ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos) == 0) {
585 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
586 				goto bad;
587 			}
588 
589 			if (otos != ip->ip_tos) {
590 				sum = ~ntohs(ip->ip_sum) & 0xffff;
591 				sum += (~otos & 0xffff) + ip->ip_tos;
592 				sum = (sum >> 16) + (sum & 0xffff);
593 				sum += (sum >> 16); /* add carry */
594 				ip->ip_sum = htons(~sum & 0xffff);
595 			}
596 
597 			if (!key_checktunnelsanity(sav, AF_INET,
598 			    (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
599 				ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
600 				    "in ESP input: %s %s\n",
601 				    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
602 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
603 				goto bad;
604 			}
605 
606 			inner_ip_proto = ip->ip_p;
607 
608 			bzero(&addr, sizeof(addr));
609 			ipaddr = (__typeof__(ipaddr)) & addr;
610 			ipaddr->sin_family = AF_INET;
611 			ipaddr->sin_len = sizeof(*ipaddr);
612 			ipaddr->sin_addr = ip->ip_dst;
613 		} else if (ifamily == AF_INET6) {
614 			struct sockaddr_in6 *ip6addr;
615 
616 			/*
617 			 * m_pullup is prohibited in KAME IPv6 input processing
618 			 * but there's no other way!
619 			 */
620 			if (m->m_len < sizeof(*ip6)) {
621 				m = m_pullup(m, sizeof(*ip6));
622 				if (!m) {
623 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
624 					goto bad;
625 				}
626 			}
627 
628 			/*
629 			 * Expect 32-bit aligned data pointer on strict-align
630 			 * platforms.
631 			 */
632 			MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
633 
634 			ip6 = mtod(m, struct ip6_hdr *);
635 
636 			/* ECN consideration. */
637 			if (ip64_ecn_egress(ip4_ipsec_ecn, &tos, &ip6->ip6_flow) == 0) {
638 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
639 				goto bad;
640 			}
641 
642 			if (!key_checktunnelsanity(sav, AF_INET6,
643 			    (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
644 				ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
645 				    "in ESP input: %s %s\n",
646 				    ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
647 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
648 				goto bad;
649 			}
650 
651 			inner_ip_proto = ip6->ip6_nxt;
652 
653 			bzero(&addr, sizeof(addr));
654 			ip6addr = (__typeof__(ip6addr)) & addr;
655 			ip6addr->sin6_family = AF_INET6;
656 			ip6addr->sin6_len = sizeof(*ip6addr);
657 			ip6addr->sin6_addr = ip6->ip6_dst;
658 		} else {
659 			ipseclog((LOG_ERR, "ipsec tunnel unsupported address family "
660 			    "in ESP input\n"));
661 			goto bad;
662 		}
663 
664 		key_sa_recordxfer(sav, m);
665 		if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
666 		    ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
667 			IPSEC_STAT_INCREMENT(ipsecstat.in_nomem);
668 			goto bad;
669 		}
670 
671 		// update the receiving interface address based on the inner address
672 		ifa = ifa_ifwithaddr((struct sockaddr *)&addr);
673 		if (ifa) {
674 			m->m_pkthdr.rcvif = ifa->ifa_ifp;
675 			IFA_REMREF(ifa);
676 		}
677 
678 		/* Clear the csum flags, they can't be valid for the inner headers */
679 		m->m_pkthdr.csum_flags = 0;
680 
681 		// Input via IPsec interface
682 		lck_mtx_lock(sadb_mutex);
683 		ifnet_t ipsec_if = sav->sah->ipsec_if;
684 		if (ipsec_if != NULL) {
685 			// If an interface is found, add a reference count before dropping the lock
686 			ifnet_reference(ipsec_if);
687 		}
688 		lck_mtx_unlock(sadb_mutex);
689 
690 		if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
691 			if (m->m_pkthdr.rcvif != NULL) {
692 				if_ports_used_match_mbuf(m->m_pkthdr.rcvif, ifamily, m);
693 			} else {
694 				ipseclog((LOG_ERR, "no input interface for ipsec wake packet\n"));
695 			}
696 		}
697 
698 		if (ipsec_if != NULL) {
699 			esp_input_log(m, sav, spi, seq);
700 			ipsec_save_wake_packet(m, ntohl(spi), seq);
701 
702 			// Return mbuf
703 			if (interface != NULL &&
704 			    interface == ipsec_if) {
705 				out_m = m;
706 				ifnet_release(ipsec_if);
707 				goto done;
708 			}
709 
710 			errno_t inject_error = ipsec_inject_inbound_packet(ipsec_if, m);
711 			ifnet_release(ipsec_if);
712 
713 			if (inject_error == 0) {
714 				m = NULL;
715 				goto done;
716 			} else {
717 				goto bad;
718 			}
719 		}
720 
721 		if (proto_input(ifamily == AF_INET ? PF_INET : PF_INET6, m) != 0) {
722 			goto bad;
723 		}
724 
725 		nxt = IPPROTO_DONE;
726 		KERNEL_DEBUG(DBG_FNC_ESPIN | DBG_FUNC_END, 2, 0, 0, 0, 0);
727 	} else {
728 		/*
729 		 * strip off ESP header and IV.
730 		 * even in m_pulldown case, we need to strip off ESP so that
731 		 * we can always compute checksum for AH correctly.
732 		 */
733 		size_t stripsiz;
734 
735 		stripsiz = esplen + ivlen;
736 
737 		ip = mtod(m, struct ip *);
738 		ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
739 		m->m_data += stripsiz;
740 		m->m_len -= stripsiz;
741 		m->m_pkthdr.len -= stripsiz;
742 
743 		ip = mtod(m, struct ip *);
744 #ifdef IPLEN_FLIPPED
745 		ip->ip_len = ip->ip_len - (u_short)stripsiz;
746 #else
747 		ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
748 #endif
749 		ip->ip_p = (u_int8_t)nxt;
750 
751 		key_sa_recordxfer(sav, m);
752 		if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
753 			IPSEC_STAT_INCREMENT(ipsecstat.in_nomem);
754 			goto bad;
755 		}
756 
757 		/*
758 		 * Set the csum valid flag, if we authenticated the
759 		 * packet, the payload shouldn't be corrupt unless
760 		 * it was corrupted before being signed on the other
761 		 * side.
762 		 */
763 		if (nxt == IPPROTO_TCP || nxt == IPPROTO_UDP) {
764 			m->m_pkthdr.csum_flags = CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
765 			m->m_pkthdr.csum_data = 0xFFFF;
766 			_CASSERT(offsetof(struct pkthdr, csum_data) == offsetof(struct pkthdr, csum_rx_val));
767 		}
768 
769 		if (nxt != IPPROTO_DONE) {
770 			if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 &&
771 			    ipsec4_in_reject(m, NULL)) {
772 				IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
773 				goto bad;
774 			}
775 			KERNEL_DEBUG(DBG_FNC_ESPIN | DBG_FUNC_END, 3, 0, 0, 0, 0);
776 
777 			/* translate encapsulated UDP port ? */
778 			if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) {
779 				struct udphdr   *udp;
780 
781 				if (nxt != IPPROTO_UDP) {       /* not UPD packet - drop it */
782 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
783 					goto bad;
784 				}
785 
786 				if (m->m_len < off + sizeof(struct udphdr)) {
787 					m = m_pullup(m, off + sizeof(struct udphdr));
788 					if (!m) {
789 						ipseclog((LOG_DEBUG,
790 						    "IPv4 ESP input: can't pullup UDP header in esp4_input\n"));
791 						IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
792 						goto bad;
793 					}
794 					ip = mtod(m, struct ip *);
795 				}
796 				udp = (struct udphdr *)(void *)(((u_int8_t *)ip) + off);
797 
798 				lck_mtx_lock(sadb_mutex);
799 				if (sav->natt_encapsulated_src_port == 0) {
800 					sav->natt_encapsulated_src_port = udp->uh_sport;
801 				} else if (sav->natt_encapsulated_src_port != udp->uh_sport) {  /* something wrong */
802 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
803 					lck_mtx_unlock(sadb_mutex);
804 					goto bad;
805 				}
806 				lck_mtx_unlock(sadb_mutex);
807 				udp->uh_sport = htons(sav->remote_ike_port);
808 				udp->uh_sum = 0;
809 			}
810 
811 			DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
812 			    struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif,
813 			    struct ip *, ip, struct ip6_hdr *, NULL);
814 
815 			// Input via IPsec interface legacy path
816 			lck_mtx_lock(sadb_mutex);
817 			ifnet_t ipsec_if = sav->sah->ipsec_if;
818 			if (ipsec_if != NULL) {
819 				// If an interface is found, add a reference count before dropping the lock
820 				ifnet_reference(ipsec_if);
821 			}
822 			lck_mtx_unlock(sadb_mutex);
823 			if (ipsec_if != NULL) {
824 				int mlen;
825 				if ((mlen = m_length2(m, NULL)) < hlen) {
826 					ipseclog((LOG_DEBUG,
827 					    "IPv4 ESP input: decrypted packet too short %d < %u\n",
828 					    mlen, hlen));
829 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
830 					ifnet_release(ipsec_if);
831 					goto bad;
832 				}
833 				ip->ip_len = htons(ip->ip_len + hlen);
834 				ip->ip_off = htons(ip->ip_off);
835 				ip->ip_sum = 0;
836 				ip->ip_sum = ip_cksum_hdr_in(m, hlen);
837 
838 				esp_input_log(m, sav, spi, seq);
839 				ipsec_save_wake_packet(m, ntohl(spi), seq);
840 
841 				if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
842 					if_ports_used_match_mbuf(ipsec_if, PF_INET, m);
843 				}
844 
845 				// Return mbuf
846 				if (interface != NULL &&
847 				    interface == ipsec_if) {
848 					out_m = m;
849 					ifnet_release(ipsec_if);
850 					goto done;
851 				}
852 
853 				errno_t inject_error = ipsec_inject_inbound_packet(ipsec_if, m);
854 				ifnet_release(ipsec_if);
855 
856 				if (inject_error == 0) {
857 					m = NULL;
858 					goto done;
859 				} else {
860 					goto bad;
861 				}
862 			}
863 
864 			if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
865 				if_ports_used_match_mbuf(m->m_pkthdr.rcvif, PF_INET, m);
866 				if (m->m_pkthdr.rcvif == NULL) {
867 					ipseclog((LOG_ERR, "no input interface for ipsec wake packet\n"));
868 				}
869 			}
870 
871 			ip_proto_dispatch_in(m, off, (u_int8_t)nxt, 0);
872 		} else {
873 			m_freem(m);
874 		}
875 		m = NULL;
876 	}
877 
878 done:
879 	if (sav) {
880 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
881 		    printf("DP esp4_input call free SA:0x%llx\n",
882 		    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
883 		key_freesav(sav, KEY_SADB_UNLOCKED);
884 	}
885 	IPSEC_STAT_INCREMENT(ipsecstat.in_success);
886 	return out_m;
887 bad:
888 	if (sav) {
889 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
890 		    printf("DP esp4_input call free SA:0x%llx\n",
891 		    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
892 		key_freesav(sav, KEY_SADB_UNLOCKED);
893 	}
894 	if (m) {
895 		m_freem(m);
896 	}
897 	KERNEL_DEBUG(DBG_FNC_ESPIN | DBG_FUNC_END, 4, 0, 0, 0, 0);
898 	return out_m;
899 }
900 
901 int
esp6_input(struct mbuf ** mp,int * offp,int proto)902 esp6_input(struct mbuf **mp, int *offp, int proto)
903 {
904 	return esp6_input_extended(mp, offp, proto, NULL);
905 }
906 
907 int
esp6_input_extended(struct mbuf ** mp,int * offp,int proto,ifnet_t interface)908 esp6_input_extended(struct mbuf **mp, int *offp, int proto, ifnet_t interface)
909 {
910 #pragma unused(proto)
911 	struct mbuf *m = *mp;
912 	int off = *offp;
913 	struct ip *ip;
914 	struct ip6_hdr *ip6;
915 	struct esp *esp;
916 	struct esptail esptail;
917 	u_int32_t spi;
918 	u_int32_t seq;
919 	struct secasvar *sav = NULL;
920 	u_int16_t nxt;
921 	char *nproto;
922 	const struct esp_algorithm *algo;
923 	int ivlen;
924 	size_t esplen;
925 	u_int16_t taillen;
926 	sa_family_t ifamily;
927 	mbuf_traffic_class_t traffic_class = 0;
928 
929 	/* sanity check for alignment. */
930 	if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
931 		ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
932 		    "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
933 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
934 		goto bad;
935 	}
936 
937 #ifndef PULLDOWN_TEST
938 	IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, {return IPPROTO_DONE;});
939 	esp = (struct esp *)(void *)(mtod(m, caddr_t) + off);
940 #else
941 	IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
942 	if (esp == NULL) {
943 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
944 		return IPPROTO_DONE;
945 	}
946 #endif
947 	m->m_pkthdr.csum_flags &= ~CSUM_RX_FLAGS;
948 
949 	/* Expect 32-bit data aligned pointer on strict-align platforms */
950 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
951 
952 	ip6 = mtod(m, struct ip6_hdr *);
953 
954 	if (ntohs(ip6->ip6_plen) == 0) {
955 		ipseclog((LOG_ERR, "IPv6 ESP input: "
956 		    "ESP with IPv6 jumbogram is not supported.\n"));
957 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
958 		goto bad;
959 	}
960 
961 	nproto = ip6_get_prevhdr(m, off);
962 	if (nproto == NULL || (*nproto != IPPROTO_ESP &&
963 	    !(*nproto == IPPROTO_UDP && off >= sizeof(struct udphdr)))) {
964 		ipseclog((LOG_DEBUG, "IPv6 ESP input: invalid protocol type\n"));
965 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
966 		goto bad;
967 	}
968 
969 	/* find the sassoc. */
970 	spi = esp->esp_spi;
971 
972 	if ((sav = key_allocsa_extended(AF_INET6,
973 	    (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst, interface != NULL ? interface->if_index : IFSCOPE_UNKNOWN,
974 	    IPPROTO_ESP, spi, interface)) == 0) {
975 		ipseclog((LOG_WARNING,
976 		    "IPv6 ESP input: no key association found for spi %u (0x%08x) seq %u"
977 		    " src %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
978 		    " dst %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x if %s\n",
979 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi), ntohl(((struct newesp *)esp)->esp_seq),
980 		    ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[0]), ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[1]),
981 		    ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[2]), ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[3]),
982 		    ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[4]), ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[5]),
983 		    ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[6]), ntohs(ip6->ip6_src.__u6_addr.__u6_addr16[7]),
984 		    ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[0]), ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[1]),
985 		    ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[2]), ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[3]),
986 		    ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[4]), ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[5]),
987 		    ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[6]), ntohs(ip6->ip6_dst.__u6_addr.__u6_addr16[7]),
988 		    ((interface != NULL) ? if_name(interface) : "NONE")));
989 		IPSEC_STAT_INCREMENT(ipsec6stat.in_nosa);
990 		goto bad;
991 	}
992 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
993 	    printf("DP esp6_input called to allocate SA:0x%llx\n",
994 	    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
995 	if (sav->state != SADB_SASTATE_MATURE
996 	    && sav->state != SADB_SASTATE_DYING) {
997 		ipseclog((LOG_DEBUG,
998 		    "IPv6 ESP input: non-mature/dying SA found for spi %u (0x%08x)\n",
999 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi)));
1000 		IPSEC_STAT_INCREMENT(ipsec6stat.in_badspi);
1001 		goto bad;
1002 	}
1003 	algo = esp_algorithm_lookup(sav->alg_enc);
1004 	if (!algo) {
1005 		ipseclog((LOG_DEBUG, "IPv6 ESP input: "
1006 		    "unsupported encryption algorithm for spi %u (0x%08x)\n",
1007 		    (u_int32_t)ntohl(spi), (u_int32_t)ntohl(spi)));
1008 		IPSEC_STAT_INCREMENT(ipsec6stat.in_badspi);
1009 		goto bad;
1010 	}
1011 
1012 	/* check if we have proper ivlen information */
1013 	ivlen = sav->ivlen;
1014 	if (ivlen < 0) {
1015 		ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
1016 		    ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
1017 		IPSEC_STAT_INCREMENT(ipsec6stat.in_badspi);
1018 		goto bad;
1019 	}
1020 
1021 	seq = ntohl(((struct newesp *)esp)->esp_seq);
1022 
1023 	if ((sav->flags2 & SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) ==
1024 	    SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) {
1025 		u_int8_t dscp = (ntohl(ip6->ip6_flow) & IP6FLOW_DSCP_MASK) >> IP6FLOW_DSCP_SHIFT;
1026 		traffic_class = rfc4594_dscp_to_tc(dscp);
1027 	}
1028 
1029 	if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL &&
1030 	    ((sav->alg_auth && sav->key_auth) || algo->finalizedecrypt))) {
1031 		goto noreplaycheck;
1032 	}
1033 
1034 	if ((sav->alg_auth == SADB_X_AALG_NULL || sav->alg_auth == SADB_AALG_NONE) &&
1035 	    !algo->finalizedecrypt) {
1036 		goto noreplaycheck;
1037 	}
1038 
1039 	/*
1040 	 * check for sequence number.
1041 	 */
1042 	if (ipsec_chkreplay(seq, sav, (u_int8_t)traffic_class)) {
1043 		; /*okey*/
1044 	} else {
1045 		IPSEC_STAT_INCREMENT(ipsec6stat.in_espreplay);
1046 		ipseclog((LOG_WARNING,
1047 		    "replay packet in IPv6 ESP input: seq(%u) tc(%u) %s %s\n",
1048 		    seq, (u_int8_t)traffic_class, ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
1049 		goto bad;
1050 	}
1051 
1052 	/* Save ICV from packet for verification later */
1053 	size_t siz = 0;
1054 	unsigned char saved_icv[AH_MAXSUMSIZE] __attribute__((aligned(4)));
1055 	if (algo->finalizedecrypt) {
1056 		siz = algo->icvlen;
1057 		VERIFY(siz <= UINT16_MAX);
1058 		m_copydata(m, m->m_pkthdr.len - (int)siz, (int)siz, (caddr_t) saved_icv);
1059 	} else {
1060 		/* check ICV immediately */
1061 		u_char sum0[AH_MAXSUMSIZE] __attribute__((aligned(4)));
1062 		u_char sum[AH_MAXSUMSIZE] __attribute__((aligned(4)));
1063 		const struct ah_algorithm *sumalgo;
1064 
1065 		sumalgo = ah_algorithm_lookup(sav->alg_auth);
1066 		if (!sumalgo) {
1067 			goto noreplaycheck;
1068 		}
1069 		siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
1070 		if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
1071 			IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1072 			goto bad;
1073 		}
1074 		if (AH_MAXSUMSIZE < siz) {
1075 			ipseclog((LOG_DEBUG,
1076 			    "internal error: AH_MAXSUMSIZE must be larger than %u\n",
1077 			    (u_int32_t)siz));
1078 			IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1079 			goto bad;
1080 		}
1081 
1082 		m_copydata(m, m->m_pkthdr.len - (int)siz, (int)siz, (caddr_t) &sum0[0]);
1083 
1084 		if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
1085 			ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
1086 			    ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
1087 			IPSEC_STAT_INCREMENT(ipsec6stat.in_espauthfail);
1088 			goto bad;
1089 		}
1090 
1091 		if (cc_cmp_safe(siz, sum0, sum)) {
1092 			ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
1093 			    ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
1094 			IPSEC_STAT_INCREMENT(ipsec6stat.in_espauthfail);
1095 			goto bad;
1096 		}
1097 
1098 		m->m_flags |= M_AUTHIPDGM;
1099 		IPSEC_STAT_INCREMENT(ipsec6stat.in_espauthsucc);
1100 
1101 		/*
1102 		 * update replay window.
1103 		 */
1104 		if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL) {
1105 			if (ipsec_updatereplay(seq, sav, (u_int8_t)traffic_class)) {
1106 				IPSEC_STAT_INCREMENT(ipsec6stat.in_espreplay);
1107 				goto bad;
1108 			}
1109 		}
1110 	}
1111 
1112 	/* strip off the authentication data */
1113 	m_adj(m, (int)-siz);
1114 	ip6 = mtod(m, struct ip6_hdr *);
1115 	ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - (u_int16_t)siz);
1116 
1117 noreplaycheck:
1118 
1119 	/* process main esp header. */
1120 	if (sav->flags & SADB_X_EXT_OLD) {
1121 		/* RFC 1827 */
1122 		esplen = sizeof(struct esp);
1123 	} else {
1124 		/* RFC 2406 */
1125 		if (sav->flags & SADB_X_EXT_DERIV) {
1126 			esplen = sizeof(struct esp);
1127 		} else {
1128 			esplen = sizeof(struct newesp);
1129 		}
1130 	}
1131 
1132 	if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
1133 		ipseclog((LOG_WARNING,
1134 		    "IPv6 ESP input: packet too short\n"));
1135 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1136 		goto bad;
1137 	}
1138 
1139 #ifndef PULLDOWN_TEST
1140 	IP6_EXTHDR_CHECK(m, off, (int)(esplen + ivlen), return IPPROTO_DONE);  /*XXX*/
1141 #else
1142 	IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
1143 	if (esp == NULL) {
1144 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1145 		m = NULL;
1146 		goto bad;
1147 	}
1148 #endif
1149 	ip6 = mtod(m, struct ip6_hdr *);        /*set it again just in case*/
1150 
1151 	/*
1152 	 * pre-compute and cache intermediate key
1153 	 */
1154 	if (esp_schedule(algo, sav) != 0) {
1155 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1156 		goto bad;
1157 	}
1158 
1159 	/*
1160 	 * decrypt the packet.
1161 	 */
1162 	if (!algo->decrypt) {
1163 		panic("internal error: no decrypt function");
1164 	}
1165 	KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_START, 0, 0, 0, 0, 0);
1166 	if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
1167 		/* m is already freed */
1168 		m = NULL;
1169 		ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
1170 		    ipsec_logsastr(sav)));
1171 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1172 		KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 1, 0, 0, 0, 0);
1173 		goto bad;
1174 	}
1175 	KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 2, 0, 0, 0, 0);
1176 	IPSEC_STAT_INCREMENT(ipsec6stat.in_esphist[sav->alg_enc]);
1177 
1178 	m->m_flags |= M_DECRYPTED;
1179 
1180 	if (algo->finalizedecrypt) {
1181 		if ((*algo->finalizedecrypt)(sav, saved_icv, algo->icvlen)) {
1182 			ipseclog((LOG_ERR, "esp6 packet decryption ICV failure: %s\n",
1183 			    ipsec_logsastr(sav)));
1184 			IPSEC_STAT_INCREMENT(ipsec6stat.in_espauthfail);
1185 			KERNEL_DEBUG(DBG_FNC_DECRYPT | DBG_FUNC_END, 1, 0, 0, 0, 0);
1186 			goto bad;
1187 		} else {
1188 			m->m_flags |= M_AUTHIPDGM;
1189 			IPSEC_STAT_INCREMENT(ipsec6stat.in_espauthsucc);
1190 
1191 			/*
1192 			 * update replay window.
1193 			 */
1194 			if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] != NULL) {
1195 				if (ipsec_updatereplay(seq, sav, (u_int8_t)traffic_class)) {
1196 					IPSEC_STAT_INCREMENT(ipsec6stat.in_espreplay);
1197 					goto bad;
1198 				}
1199 			}
1200 		}
1201 	}
1202 
1203 	/*
1204 	 * find the trailer of the ESP.
1205 	 */
1206 	m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
1207 	    (caddr_t)&esptail);
1208 	nxt = esptail.esp_nxt;
1209 	taillen = esptail.esp_padlen + sizeof(esptail);
1210 
1211 	if (m->m_pkthdr.len < taillen
1212 	    || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) {    /*?*/
1213 		ipseclog((LOG_WARNING,
1214 		    "bad pad length in IPv6 ESP input: %s %s\n",
1215 		    ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
1216 		IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1217 		goto bad;
1218 	}
1219 
1220 	/* strip off the trailing pad area. */
1221 	m_adj(m, -taillen);
1222 	ip6 = mtod(m, struct ip6_hdr *);
1223 	ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
1224 
1225 	if (*nproto == IPPROTO_UDP) {
1226 		// offset includes the outer ip and udp header lengths.
1227 		if (m->m_len < off) {
1228 			m = m_pullup(m, off);
1229 			if (!m) {
1230 				ipseclog((LOG_DEBUG,
1231 				    "IPv6 ESP input: invalid udp encapsulated ESP packet length\n"));
1232 				IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1233 				goto bad;
1234 			}
1235 			ip6 = mtod(m, struct ip6_hdr *);
1236 		}
1237 
1238 		// check the UDP encap header to detect changes in the source port, and then strip the header
1239 		off -= sizeof(struct udphdr); // off no longer includes the udphdr's size
1240 		// if peer is behind nat and this is the latest esp packet
1241 		if ((sav->flags & SADB_X_EXT_NATT_DETECTED_PEER) != 0 &&
1242 		    (sav->flags & SADB_X_EXT_OLD) == 0 &&
1243 		    seq && sav->replay[traffic_class] &&
1244 		    seq >= sav->replay[traffic_class]->lastseq) {
1245 			struct udphdr *encap_uh = (__typeof__(encap_uh))(void *)((caddr_t)ip6 + off);
1246 			if (encap_uh->uh_sport &&
1247 			    ntohs(encap_uh->uh_sport) != sav->remote_ike_port) {
1248 				sav->remote_ike_port = ntohs(encap_uh->uh_sport);
1249 			}
1250 		}
1251 		ip6 = esp6_input_strip_udp_encap(m, off);
1252 		esp = (struct esp *)(void *)(((u_int8_t *)ip6) + off);
1253 	}
1254 
1255 
1256 	/* was it transmitted over the IPsec tunnel SA? */
1257 	if (ipsec6_tunnel_validate(m, (int)(off + esplen + ivlen), nxt, sav, &ifamily)) {
1258 		ifaddr_t ifa;
1259 		struct sockaddr_storage addr;
1260 		u_int8_t inner_ip_proto = 0;
1261 
1262 		/*
1263 		 * strip off all the headers that precedes ESP header.
1264 		 *	IP6 xx ESP IP6' payload -> IP6' payload
1265 		 *
1266 		 * XXX more sanity checks
1267 		 * XXX relationship with gif?
1268 		 */
1269 		u_int32_t flowinfo;     /*net endian*/
1270 		flowinfo = ip6->ip6_flow;
1271 		m_adj(m, (int)(off + esplen + ivlen));
1272 		if (ifamily == AF_INET6) {
1273 			struct sockaddr_in6 *ip6addr;
1274 
1275 			if (m->m_len < sizeof(*ip6)) {
1276 #ifndef PULLDOWN_TEST
1277 				/*
1278 				 * m_pullup is prohibited in KAME IPv6 input processing
1279 				 * but there's no other way!
1280 				 */
1281 #else
1282 				/* okay to pullup in m_pulldown style */
1283 #endif
1284 				m = m_pullup(m, sizeof(*ip6));
1285 				if (!m) {
1286 					IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1287 					goto bad;
1288 				}
1289 			}
1290 			ip6 = mtod(m, struct ip6_hdr *);
1291 			/* ECN consideration. */
1292 			if (ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow) == 0) {
1293 				IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1294 				goto bad;
1295 			}
1296 			if (!key_checktunnelsanity(sav, AF_INET6,
1297 			    (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
1298 				ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
1299 				    "in IPv6 ESP input: %s %s\n",
1300 				    ipsec6_logpacketstr(ip6, spi),
1301 				    ipsec_logsastr(sav)));
1302 				IPSEC_STAT_INCREMENT(ipsec6stat.in_inval);
1303 				goto bad;
1304 			}
1305 
1306 			inner_ip_proto = ip6->ip6_nxt;
1307 
1308 			bzero(&addr, sizeof(addr));
1309 			ip6addr = (__typeof__(ip6addr)) & addr;
1310 			ip6addr->sin6_family = AF_INET6;
1311 			ip6addr->sin6_len = sizeof(*ip6addr);
1312 			ip6addr->sin6_addr = ip6->ip6_dst;
1313 		} else if (ifamily == AF_INET) {
1314 			struct sockaddr_in *ipaddr;
1315 
1316 			if (m->m_len < sizeof(*ip)) {
1317 				m = m_pullup(m, sizeof(*ip));
1318 				if (!m) {
1319 					IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
1320 					goto bad;
1321 				}
1322 			}
1323 
1324 			u_int8_t otos;
1325 			int sum;
1326 
1327 			ip = mtod(m, struct ip *);
1328 			otos = ip->ip_tos;
1329 			/* ECN consideration. */
1330 			if (ip46_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip->ip_tos) == 0) {
1331 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
1332 				goto bad;
1333 			}
1334 
1335 			if (otos != ip->ip_tos) {
1336 				sum = ~ntohs(ip->ip_sum) & 0xffff;
1337 				sum += (~otos & 0xffff) + ip->ip_tos;
1338 				sum = (sum >> 16) + (sum & 0xffff);
1339 				sum += (sum >> 16); /* add carry */
1340 				ip->ip_sum = htons(~sum & 0xffff);
1341 			}
1342 
1343 			if (!key_checktunnelsanity(sav, AF_INET,
1344 			    (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
1345 				ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
1346 				    "in ESP input: %s %s\n",
1347 				    ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
1348 				IPSEC_STAT_INCREMENT(ipsecstat.in_inval);
1349 				goto bad;
1350 			}
1351 
1352 			inner_ip_proto = ip->ip_p;
1353 
1354 			bzero(&addr, sizeof(addr));
1355 			ipaddr = (__typeof__(ipaddr)) & addr;
1356 			ipaddr->sin_family = AF_INET;
1357 			ipaddr->sin_len = sizeof(*ipaddr);
1358 			ipaddr->sin_addr = ip->ip_dst;
1359 		}
1360 
1361 		key_sa_recordxfer(sav, m);
1362 		if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
1363 		    ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
1364 			IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem);
1365 			goto bad;
1366 		}
1367 
1368 		// update the receiving interface address based on the inner address
1369 		ifa = ifa_ifwithaddr((struct sockaddr *)&addr);
1370 		if (ifa) {
1371 			m->m_pkthdr.rcvif = ifa->ifa_ifp;
1372 			IFA_REMREF(ifa);
1373 		}
1374 
1375 		// Input via IPsec interface
1376 		lck_mtx_lock(sadb_mutex);
1377 		ifnet_t ipsec_if = sav->sah->ipsec_if;
1378 		if (ipsec_if != NULL) {
1379 			// If an interface is found, add a reference count before dropping the lock
1380 			ifnet_reference(ipsec_if);
1381 		}
1382 		lck_mtx_unlock(sadb_mutex);
1383 
1384 		if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
1385 			if_ports_used_match_mbuf(m->m_pkthdr.rcvif, ifamily, m);
1386 			if (m->m_pkthdr.rcvif == NULL) {
1387 				ipseclog((LOG_ERR, "no input interface for ipsec wake packet\n"));
1388 			}
1389 		}
1390 
1391 		if (ipsec_if != NULL) {
1392 			esp_input_log(m, sav, spi, seq);
1393 			ipsec_save_wake_packet(m, ntohl(spi), seq);
1394 
1395 			// Return mbuf
1396 			if (interface != NULL &&
1397 			    interface == ipsec_if) {
1398 				ifnet_release(ipsec_if);
1399 				goto done;
1400 			}
1401 
1402 			errno_t inject_error = ipsec_inject_inbound_packet(ipsec_if, m);
1403 			ifnet_release(ipsec_if);
1404 
1405 			if (inject_error == 0) {
1406 				m = NULL;
1407 				nxt = IPPROTO_DONE;
1408 				goto done;
1409 			} else {
1410 				goto bad;
1411 			}
1412 		}
1413 
1414 		if (proto_input(ifamily == AF_INET ? PF_INET : PF_INET6, m) != 0) {
1415 			goto bad;
1416 		}
1417 		nxt = IPPROTO_DONE;
1418 	} else {
1419 		/*
1420 		 * strip off ESP header and IV.
1421 		 * even in m_pulldown case, we need to strip off ESP so that
1422 		 * we can always compute checksum for AH correctly.
1423 		 */
1424 		u_int16_t stripsiz;
1425 		char *prvnxtp;
1426 
1427 		/*
1428 		 * Set the next header field of the previous header correctly.
1429 		 */
1430 		prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
1431 		*prvnxtp = (u_int8_t)nxt;
1432 
1433 		VERIFY(esplen + ivlen <= UINT16_MAX);
1434 		stripsiz = (u_int16_t)(esplen + ivlen);
1435 
1436 		ip6 = mtod(m, struct ip6_hdr *);
1437 		if (m->m_len >= stripsiz + off) {
1438 			ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
1439 			m->m_data += stripsiz;
1440 			m->m_len -= stripsiz;
1441 			m->m_pkthdr.len -= stripsiz;
1442 		} else {
1443 			/*
1444 			 * this comes with no copy if the boundary is on
1445 			 * cluster
1446 			 */
1447 			struct mbuf *n;
1448 
1449 			n = m_split(m, off, M_DONTWAIT);
1450 			if (n == NULL) {
1451 				/* m is retained by m_split */
1452 				goto bad;
1453 			}
1454 			m_adj(n, stripsiz);
1455 			/* m_cat does not update m_pkthdr.len */
1456 			m->m_pkthdr.len += n->m_pkthdr.len;
1457 			m_cat(m, n);
1458 		}
1459 
1460 #ifndef PULLDOWN_TEST
1461 		/*
1462 		 * KAME requires that the packet to be contiguous on the
1463 		 * mbuf.  We need to make that sure.
1464 		 * this kind of code should be avoided.
1465 		 * XXX other conditions to avoid running this part?
1466 		 */
1467 		if (m->m_len != m->m_pkthdr.len) {
1468 			struct mbuf *n = NULL;
1469 			int maxlen;
1470 
1471 			MGETHDR(n, M_DONTWAIT, MT_HEADER);      /* MAC-OK */
1472 			maxlen = MHLEN;
1473 			if (n) {
1474 				M_COPY_PKTHDR(n, m);
1475 			}
1476 			if (n && m->m_pkthdr.len > maxlen) {
1477 				MCLGET(n, M_DONTWAIT);
1478 				maxlen = MCLBYTES;
1479 				if ((n->m_flags & M_EXT) == 0) {
1480 					m_free(n);
1481 					n = NULL;
1482 				}
1483 			}
1484 			if (!n) {
1485 				printf("esp6_input: mbuf allocation failed\n");
1486 				goto bad;
1487 			}
1488 
1489 			if (m->m_pkthdr.len <= maxlen) {
1490 				m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
1491 				n->m_len = m->m_pkthdr.len;
1492 				n->m_pkthdr.len = m->m_pkthdr.len;
1493 				n->m_next = NULL;
1494 				m_freem(m);
1495 			} else {
1496 				m_copydata(m, 0, maxlen, mtod(n, caddr_t));
1497 				n->m_len = maxlen;
1498 				n->m_pkthdr.len = m->m_pkthdr.len;
1499 				n->m_next = m;
1500 				m_adj(m, maxlen);
1501 				m->m_flags &= ~M_PKTHDR;
1502 			}
1503 			m = n;
1504 		}
1505 #endif
1506 		ip6 = mtod(m, struct ip6_hdr *);
1507 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
1508 
1509 		key_sa_recordxfer(sav, m);
1510 		if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
1511 			IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem);
1512 			goto bad;
1513 		}
1514 
1515 		/*
1516 		 * Set the csum valid flag, if we authenticated the
1517 		 * packet, the payload shouldn't be corrupt unless
1518 		 * it was corrupted before being signed on the other
1519 		 * side.
1520 		 */
1521 		if (nxt == IPPROTO_TCP || nxt == IPPROTO_UDP) {
1522 			m->m_pkthdr.csum_flags = CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1523 			m->m_pkthdr.csum_data = 0xFFFF;
1524 			_CASSERT(offsetof(struct pkthdr, csum_data) == offsetof(struct pkthdr, csum_rx_val));
1525 		}
1526 
1527 		// Input via IPsec interface
1528 		lck_mtx_lock(sadb_mutex);
1529 		ifnet_t ipsec_if = sav->sah->ipsec_if;
1530 		if (ipsec_if != NULL) {
1531 			// If an interface is found, add a reference count before dropping the lock
1532 			ifnet_reference(ipsec_if);
1533 		}
1534 		lck_mtx_unlock(sadb_mutex);
1535 		if (ipsec_if != NULL) {
1536 			esp_input_log(m, sav, spi, seq);
1537 			ipsec_save_wake_packet(m, ntohl(spi), seq);
1538 
1539 			if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
1540 				if_ports_used_match_mbuf(ipsec_if, PF_INET6, m);
1541 			}
1542 
1543 			// Return mbuf
1544 			if (interface != NULL &&
1545 			    interface == ipsec_if) {
1546 				ifnet_release(ipsec_if);
1547 				goto done;
1548 			}
1549 
1550 			errno_t inject_error = ipsec_inject_inbound_packet(ipsec_if, m);
1551 			ifnet_release(ipsec_if);
1552 
1553 			if (inject_error == 0) {
1554 				m = NULL;
1555 				nxt = IPPROTO_DONE;
1556 				goto done;
1557 			} else {
1558 				goto bad;
1559 			}
1560 		} else {
1561 			if ((m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) == PKTF_WAKE_PKT) {
1562 				if_ports_used_match_mbuf(m->m_pkthdr.rcvif, PF_INET, m);
1563 				if (m->m_pkthdr.rcvif == NULL) {
1564 					ipseclog((LOG_ERR, "no input interface for ipsec wake packet\n"));
1565 				}
1566 			}
1567 		}
1568 	}
1569 
1570 done:
1571 	*offp = off;
1572 	*mp = m;
1573 	if (sav) {
1574 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1575 		    printf("DP esp6_input call free SA:0x%llx\n",
1576 		    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
1577 		key_freesav(sav, KEY_SADB_UNLOCKED);
1578 	}
1579 	IPSEC_STAT_INCREMENT(ipsec6stat.in_success);
1580 	return nxt;
1581 
1582 bad:
1583 	if (sav) {
1584 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1585 		    printf("DP esp6_input call free SA:0x%llx\n",
1586 		    (uint64_t)VM_KERNEL_ADDRPERM(sav)));
1587 		key_freesav(sav, KEY_SADB_UNLOCKED);
1588 	}
1589 	if (m) {
1590 		m_freem(m);
1591 	}
1592 	if (interface != NULL) {
1593 		*mp = NULL;
1594 	}
1595 	return IPPROTO_DONE;
1596 }
1597 
1598 void
esp6_ctlinput(int cmd,struct sockaddr * sa,void * d,__unused struct ifnet * ifp)1599 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp)
1600 {
1601 	const struct newesp *espp;
1602 	struct newesp esp;
1603 	struct ip6ctlparam *ip6cp = NULL, ip6cp1;
1604 	struct secasvar *sav;
1605 	struct ip6_hdr *ip6;
1606 	struct mbuf *m;
1607 	int off = 0;
1608 	struct sockaddr_in6 *sa6_src, *sa6_dst;
1609 
1610 	if (sa->sa_family != AF_INET6 ||
1611 	    sa->sa_len != sizeof(struct sockaddr_in6)) {
1612 		return;
1613 	}
1614 	if ((unsigned)cmd >= PRC_NCMDS) {
1615 		return;
1616 	}
1617 
1618 	/* if the parameter is from icmp6, decode it. */
1619 	if (d != NULL) {
1620 		ip6cp = (struct ip6ctlparam *)d;
1621 		m = ip6cp->ip6c_m;
1622 		ip6 = ip6cp->ip6c_ip6;
1623 		off = ip6cp->ip6c_off;
1624 	} else {
1625 		m = NULL;
1626 		ip6 = NULL;
1627 	}
1628 
1629 	if (ip6) {
1630 		/*
1631 		 * Notify the error to all possible sockets via pfctlinput2.
1632 		 * Since the upper layer information (such as protocol type,
1633 		 * source and destination ports) is embedded in the encrypted
1634 		 * data and might have been cut, we can't directly call
1635 		 * an upper layer ctlinput function. However, the pcbnotify
1636 		 * function will consider source and destination addresses
1637 		 * as well as the flow info value, and may be able to find
1638 		 * some PCB that should be notified.
1639 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
1640 		 * no possibility of an infinite loop of function calls,
1641 		 * because we don't pass the inner IPv6 header.
1642 		 */
1643 		bzero(&ip6cp1, sizeof(ip6cp1));
1644 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
1645 		pfctlinput2(cmd, sa, (void *)&ip6cp1);
1646 
1647 		/*
1648 		 * Then go to special cases that need ESP header information.
1649 		 * XXX: We assume that when ip6 is non NULL,
1650 		 * M and OFF are valid.
1651 		 */
1652 
1653 		/* check if we can safely examine src and dst ports */
1654 		if (m->m_pkthdr.len < off + sizeof(esp)) {
1655 			return;
1656 		}
1657 
1658 		if (m->m_len < off + sizeof(esp)) {
1659 			/*
1660 			 * this should be rare case,
1661 			 * so we compromise on this copy...
1662 			 */
1663 			m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
1664 			espp = &esp;
1665 		} else {
1666 			espp = (struct newesp*)(void *)(mtod(m, caddr_t) + off);
1667 		}
1668 
1669 		if (cmd == PRC_MSGSIZE) {
1670 			int valid = 0;
1671 
1672 			/*
1673 			 * Check to see if we have a valid SA corresponding to
1674 			 * the address in the ICMP message payload.
1675 			 */
1676 			sa6_src = ip6cp->ip6c_src;
1677 			sa6_dst = (struct sockaddr_in6 *)(void *)sa;
1678 			sav = key_allocsa(AF_INET6,
1679 			    (caddr_t)&sa6_src->sin6_addr,
1680 			    (caddr_t)&sa6_dst->sin6_addr,
1681 			    sa6_dst->sin6_scope_id,
1682 			    IPPROTO_ESP, espp->esp_spi);
1683 			if (sav) {
1684 				if (sav->state == SADB_SASTATE_MATURE ||
1685 				    sav->state == SADB_SASTATE_DYING) {
1686 					valid++;
1687 				}
1688 				key_freesav(sav, KEY_SADB_UNLOCKED);
1689 			}
1690 
1691 			/* XXX Further validation? */
1692 
1693 			/*
1694 			 * Depending on the value of "valid" and routing table
1695 			 * size (mtudisc_{hi,lo}wat), we will:
1696 			 * - recalcurate the new MTU and create the
1697 			 *   corresponding routing entry, or
1698 			 * - ignore the MTU change notification.
1699 			 */
1700 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1701 		}
1702 	} else {
1703 		/* we normally notify any pcb here */
1704 	}
1705 }
1706