2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
62 * RFC1827/2406 Encapsulated Security Payload.
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
69 #include <sys/mcache.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/errno.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
79 #include <net/if_ipsec.h>
80 #include <net/route.h>
81 #include <kern/cpu_number.h>
82 #include <kern/locks.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip_ecn.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/udp.h>
93 #include <netinet6/ip6_ecn.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6protosw.h>
104 #include <netinet6/ipsec.h>
106 #include <netinet6/ipsec6.h>
108 #include <netinet6/ah.h>
110 #include <netinet6/ah6.h>
112 #include <netinet6/esp.h>
114 #include <netinet6/esp6.h>
116 #include <netkey/key.h>
117 #include <netkey/keydb.h>
118 #include <netkey/key_debug.h>
120 #include <net/kpi_protocol.h>
121 #include <netinet/kpi_ipfilter_var.h>
123 #include <net/net_osdep.h>
124 #include <mach/sdt.h>
125 #include <corecrypto/cc.h>
127 #include <sys/kdebug.h>
128 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
129 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
130 #define DBG_FNC_ESPIN NETDBG_CODE(DBG_NETIPSEC, (6 << 8))
131 #define DBG_FNC_DECRYPT NETDBG_CODE(DBG_NETIPSEC, (7 << 8))
132 #define IPLEN_FLIPPED
134 extern lck_mtx_t
*sadb_mutex
;
138 (sizeof(struct esp) < sizeof(struct newesp) \
139 ? sizeof(struct newesp) : sizeof(struct esp))
142 esp4_input_strip_udp_encap (struct mbuf
*m
, int iphlen
)
144 // strip the udp header that's encapsulating ESP
146 size_t stripsiz
= sizeof(struct udphdr
);
148 ip
= mtod(m
, __typeof__(ip
));
149 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), iphlen
);
150 m
->m_data
+= stripsiz
;
151 m
->m_len
-= stripsiz
;
152 m
->m_pkthdr
.len
-= stripsiz
;
153 ip
= mtod(m
, __typeof__(ip
));
154 ip
->ip_len
= ip
->ip_len
- stripsiz
;
155 ip
->ip_p
= IPPROTO_ESP
;
159 static struct ip6_hdr
*
160 esp6_input_strip_udp_encap (struct mbuf
*m
, int ip6hlen
)
162 // strip the udp header that's encapsulating ESP
164 size_t stripsiz
= sizeof(struct udphdr
);
166 ip6
= mtod(m
, __typeof__(ip6
));
167 ovbcopy((caddr_t
)ip6
, (caddr_t
)(((u_char
*)ip6
) + stripsiz
), ip6hlen
);
168 m
->m_data
+= stripsiz
;
169 m
->m_len
-= stripsiz
;
170 m
->m_pkthdr
.len
-= stripsiz
;
171 ip6
= mtod(m
, __typeof__(ip6
));
172 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
173 ip6
->ip6_nxt
= IPPROTO_ESP
;
178 esp4_input(struct mbuf
*m
, int off
)
185 struct esptail esptail
;
188 struct secasvar
*sav
= NULL
;
191 const struct esp_algorithm
*algo
;
197 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_START
, 0,0,0,0,0);
198 /* sanity check for alignment. */
199 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
200 ipseclog((LOG_ERR
, "IPv4 ESP input: packet alignment problem "
201 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
202 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
206 if (m
->m_len
< off
+ ESPMAXLEN
) {
207 m
= m_pullup(m
, off
+ ESPMAXLEN
);
210 "IPv4 ESP input: can't pullup in esp4_input\n"));
211 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
216 /* Expect 32-bit aligned data pointer on strict-align platforms */
217 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
219 ip
= mtod(m
, struct ip
*);
220 // expect udp-encap and esp packets only
221 if (ip
->ip_p
!= IPPROTO_ESP
&&
222 !(ip
->ip_p
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
))) {
224 "IPv4 ESP input: invalid protocol type\n"));
225 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
228 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
230 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
232 hlen
= ip
->ip_hl
<< 2;
235 /* find the sassoc. */
238 if ((sav
= key_allocsa(AF_INET
,
239 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
240 IPPROTO_ESP
, spi
)) == 0) {
241 ipseclog((LOG_WARNING
,
242 "IPv4 ESP input: no key association found for spi %u\n",
243 (u_int32_t
)ntohl(spi
)));
244 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
247 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
248 printf("DP esp4_input called to allocate SA:0x%llx\n",
249 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
250 if (sav
->state
!= SADB_SASTATE_MATURE
251 && sav
->state
!= SADB_SASTATE_DYING
) {
253 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
254 (u_int32_t
)ntohl(spi
)));
255 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
258 algo
= esp_algorithm_lookup(sav
->alg_enc
);
260 ipseclog((LOG_DEBUG
, "IPv4 ESP input: "
261 "unsupported encryption algorithm for spi %u\n",
262 (u_int32_t
)ntohl(spi
)));
263 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
267 /* check if we have proper ivlen information */
270 ipseclog((LOG_ERR
, "inproper ivlen in IPv4 ESP input: %s %s\n",
271 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
272 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
276 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
278 /* Save ICV from packet for verification later */
280 unsigned char saved_icv
[AH_MAXSUMSIZE
];
281 if (algo
->finalizedecrypt
) {
283 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) saved_icv
);
287 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
288 && (sav
->alg_auth
&& sav
->key_auth
)))
291 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
292 sav
->alg_auth
== SADB_AALG_NONE
)
296 * check for sequence number.
298 if (ipsec_chkreplay(seq
, sav
))
301 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
302 ipseclog((LOG_WARNING
,
303 "replay packet in IPv4 ESP input: %s %s\n",
304 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
310 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
311 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
312 const struct ah_algorithm
*sumalgo
;
314 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
317 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
318 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
319 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
322 if (AH_MAXSUMSIZE
< siz
) {
324 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
326 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
330 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
332 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
333 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
334 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
335 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
339 if (cc_cmp_safe(siz
, sum0
, sum
)) {
340 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
341 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
342 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
348 /* strip off the authentication data */
350 ip
= mtod(m
, struct ip
*);
352 ip
->ip_len
= ip
->ip_len
- siz
;
354 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
356 m
->m_flags
|= M_AUTHIPDGM
;
357 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthsucc
);
361 * update sequence number.
363 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
364 if (ipsec_updatereplay(seq
, sav
)) {
365 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
372 /* process main esp header. */
373 if (sav
->flags
& SADB_X_EXT_OLD
) {
375 esplen
= sizeof(struct esp
);
378 if (sav
->flags
& SADB_X_EXT_DERIV
)
379 esplen
= sizeof(struct esp
);
381 esplen
= sizeof(struct newesp
);
384 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
385 ipseclog((LOG_WARNING
,
386 "IPv4 ESP input: packet too short\n"));
387 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
391 if (m
->m_len
< off
+ esplen
+ ivlen
) {
392 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
395 "IPv4 ESP input: can't pullup in esp4_input\n"));
396 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
402 * pre-compute and cache intermediate key
404 if (esp_schedule(algo
, sav
) != 0) {
405 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
410 * decrypt the packet.
413 panic("internal error: no decrypt function");
414 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_START
, 0,0,0,0,0);
415 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
416 /* m is already freed */
418 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
419 ipsec_logsastr(sav
)));
420 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
421 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
424 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 2,0,0,0,0);
425 IPSEC_STAT_INCREMENT(ipsecstat
.in_esphist
[sav
->alg_enc
]);
427 m
->m_flags
|= M_DECRYPTED
;
429 if (algo
->finalizedecrypt
)
431 if ((*algo
->finalizedecrypt
)(sav
, saved_icv
, algo
->icvlen
)) {
432 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
433 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
434 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
440 * find the trailer of the ESP.
442 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
444 nxt
= esptail
.esp_nxt
;
445 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
447 if (m
->m_pkthdr
.len
< taillen
448 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
449 ipseclog((LOG_WARNING
,
450 "bad pad length in IPv4 ESP input: %s %s\n",
451 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
452 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
456 /* strip off the trailing pad area. */
458 ip
= mtod(m
, struct ip
*);
460 ip
->ip_len
= ip
->ip_len
- taillen
;
462 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
464 if (ip
->ip_p
== IPPROTO_UDP
) {
465 // offset includes the outer ip and udp header lengths.
466 if (m
->m_len
< off
) {
467 m
= m_pullup(m
, off
);
470 "IPv4 ESP input: invalid udp encapsulated ESP packet length \n"));
471 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
476 // check the UDP encap header to detect changes in the source port, and then strip the header
477 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
478 // if peer is behind nat and this is the latest esp packet
479 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
480 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
481 seq
&& sav
->replay
&&
482 seq
>= sav
->replay
->lastseq
) {
483 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip
+ off
);
484 if (encap_uh
->uh_sport
&&
485 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
486 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
489 ip
= esp4_input_strip_udp_encap(m
, off
);
490 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
493 /* was it transmitted over the IPsec tunnel SA? */
494 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
496 struct sockaddr_storage addr
;
499 * strip off all the headers that precedes ESP header.
500 * IP4 xx ESP IP4' payload -> IP4' payload
502 * XXX more sanity checks
503 * XXX relationship with gif?
509 m_adj(m
, off
+ esplen
+ ivlen
);
510 if (ifamily
== AF_INET
) {
511 struct sockaddr_in
*ipaddr
;
513 if (m
->m_len
< sizeof(*ip
)) {
514 m
= m_pullup(m
, sizeof(*ip
));
516 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
520 ip
= mtod(m
, struct ip
*);
521 /* ECN consideration. */
524 if (ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
) == 0) {
525 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
529 if (otos
!= ip
->ip_tos
) {
530 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
531 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
532 sum
= (sum
>> 16) + (sum
& 0xffff);
533 sum
+= (sum
>> 16); /* add carry */
534 ip
->ip_sum
= htons(~sum
& 0xffff);
537 if (!key_checktunnelsanity(sav
, AF_INET
,
538 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
539 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
540 "in ESP input: %s %s\n",
541 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
542 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
546 bzero(&addr
, sizeof(addr
));
547 ipaddr
= (__typeof__(ipaddr
))&addr
;
548 ipaddr
->sin_family
= AF_INET
;
549 ipaddr
->sin_len
= sizeof(*ipaddr
);
550 ipaddr
->sin_addr
= ip
->ip_dst
;
552 } else if (ifamily
== AF_INET6
) {
553 struct sockaddr_in6
*ip6addr
;
556 * m_pullup is prohibited in KAME IPv6 input processing
557 * but there's no other way!
559 if (m
->m_len
< sizeof(*ip6
)) {
560 m
= m_pullup(m
, sizeof(*ip6
));
562 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
568 * Expect 32-bit aligned data pointer on strict-align
571 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
573 ip6
= mtod(m
, struct ip6_hdr
*);
575 /* ECN consideration. */
576 if (ip64_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip6
->ip6_flow
) == 0) {
577 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
581 if (!key_checktunnelsanity(sav
, AF_INET6
,
582 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
583 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
584 "in ESP input: %s %s\n",
585 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
586 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
590 bzero(&addr
, sizeof(addr
));
591 ip6addr
= (__typeof__(ip6addr
))&addr
;
592 ip6addr
->sin6_family
= AF_INET6
;
593 ip6addr
->sin6_len
= sizeof(*ip6addr
);
594 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
597 ipseclog((LOG_ERR
, "ipsec tunnel unsupported address family "
602 key_sa_recordxfer(sav
, m
);
603 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
604 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
605 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
609 // update the receiving interface address based on the inner address
610 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
612 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
616 /* Clear the csum flags, they can't be valid for the inner headers */
617 m
->m_pkthdr
.csum_flags
= 0;
619 // Input via IPSec interface
620 if (sav
->sah
->ipsec_if
!= NULL
) {
621 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
629 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
633 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 2,0,0,0,0);
636 * strip off ESP header and IV.
637 * even in m_pulldown case, we need to strip off ESP so that
638 * we can always compute checksum for AH correctly.
642 stripsiz
= esplen
+ ivlen
;
644 ip
= mtod(m
, struct ip
*);
645 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
646 m
->m_data
+= stripsiz
;
647 m
->m_len
-= stripsiz
;
648 m
->m_pkthdr
.len
-= stripsiz
;
650 ip
= mtod(m
, struct ip
*);
652 ip
->ip_len
= ip
->ip_len
- stripsiz
;
654 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
658 key_sa_recordxfer(sav
, m
);
659 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
660 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
665 * Set the csum valid flag, if we authenticated the
666 * packet, the payload shouldn't be corrupt unless
667 * it was corrupted before being signed on the other
670 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
671 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
672 m
->m_pkthdr
.csum_data
= 0xFFFF;
675 if (nxt
!= IPPROTO_DONE
) {
676 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
677 ipsec4_in_reject(m
, NULL
)) {
678 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
681 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 3,0,0,0,0);
683 /* translate encapsulated UDP port ? */
684 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
687 if (nxt
!= IPPROTO_UDP
) { /* not UPD packet - drop it */
688 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
692 if (m
->m_len
< off
+ sizeof(struct udphdr
)) {
693 m
= m_pullup(m
, off
+ sizeof(struct udphdr
));
696 "IPv4 ESP input: can't pullup UDP header in esp4_input\n"));
697 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
700 ip
= mtod(m
, struct ip
*);
702 udp
= (struct udphdr
*)(void *)(((u_int8_t
*)ip
) + off
);
704 lck_mtx_lock(sadb_mutex
);
705 if (sav
->natt_encapsulated_src_port
== 0) {
706 sav
->natt_encapsulated_src_port
= udp
->uh_sport
;
707 } else if (sav
->natt_encapsulated_src_port
!= udp
->uh_sport
) { /* something wrong */
708 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
709 lck_mtx_unlock(sadb_mutex
);
712 lck_mtx_unlock(sadb_mutex
);
713 udp
->uh_sport
= htons(sav
->remote_ike_port
);
717 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
718 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
719 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
721 // Input via IPSec interface
722 if (sav
->sah
->ipsec_if
!= NULL
) {
723 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
724 ip
->ip_off
= htons(ip
->ip_off
);
726 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
727 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
735 ip_proto_dispatch_in(m
, off
, nxt
, 0);
743 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
744 printf("DP esp4_input call free SA:0x%llx\n",
745 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
746 key_freesav(sav
, KEY_SADB_UNLOCKED
);
748 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
753 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
754 printf("DP esp4_input call free SA:0x%llx\n",
755 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
756 key_freesav(sav
, KEY_SADB_UNLOCKED
);
760 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 4,0,0,0,0);
767 esp6_input(struct mbuf
**mp
, int *offp
, int proto
)
769 #pragma unused(proto)
770 struct mbuf
*m
= *mp
;
775 struct esptail esptail
;
778 struct secasvar
*sav
= NULL
;
782 const struct esp_algorithm
*algo
;
787 /* sanity check for alignment. */
788 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
789 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
790 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
791 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
795 #ifndef PULLDOWN_TEST
796 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, {return IPPROTO_DONE
;});
797 esp
= (struct esp
*)(void *)(mtod(m
, caddr_t
) + off
);
799 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
801 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
805 /* Expect 32-bit data aligned pointer on strict-align platforms */
806 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
808 ip6
= mtod(m
, struct ip6_hdr
*);
810 if (ntohs(ip6
->ip6_plen
) == 0) {
811 ipseclog((LOG_ERR
, "IPv6 ESP input: "
812 "ESP with IPv6 jumbogram is not supported.\n"));
813 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
817 nproto
= ip6_get_prevhdr(m
, off
);
818 if (nproto
== NULL
|| (*nproto
!= IPPROTO_ESP
&&
819 !(*nproto
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
)))) {
820 ipseclog((LOG_DEBUG
, "IPv6 ESP input: invalid protocol type\n"));
821 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
825 /* find the sassoc. */
828 if ((sav
= key_allocsa(AF_INET6
,
829 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
830 IPPROTO_ESP
, spi
)) == 0) {
831 ipseclog((LOG_WARNING
,
832 "IPv6 ESP input: no key association found for spi %u\n",
833 (u_int32_t
)ntohl(spi
)));
834 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
837 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
838 printf("DP esp6_input called to allocate SA:0x%llx\n",
839 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
840 if (sav
->state
!= SADB_SASTATE_MATURE
841 && sav
->state
!= SADB_SASTATE_DYING
) {
843 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
844 (u_int32_t
)ntohl(spi
)));
845 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
848 algo
= esp_algorithm_lookup(sav
->alg_enc
);
850 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
851 "unsupported encryption algorithm for spi %u\n",
852 (u_int32_t
)ntohl(spi
)));
853 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
857 /* check if we have proper ivlen information */
860 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
861 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
862 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
866 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
868 /* Save ICV from packet for verification later */
870 unsigned char saved_icv
[AH_MAXSUMSIZE
];
871 if (algo
->finalizedecrypt
) {
873 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) saved_icv
);
877 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
878 && (sav
->alg_auth
&& sav
->key_auth
)))
881 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
882 sav
->alg_auth
== SADB_AALG_NONE
)
886 * check for sequence number.
888 if (ipsec_chkreplay(seq
, sav
))
891 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
892 ipseclog((LOG_WARNING
,
893 "replay packet in IPv6 ESP input: %s %s\n",
894 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
900 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
901 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
902 const struct ah_algorithm
*sumalgo
;
904 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
907 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
908 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
909 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
912 if (AH_MAXSUMSIZE
< siz
) {
914 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
916 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
920 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
922 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
923 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
924 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
925 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
929 if (cc_cmp_safe(siz
, sum0
, sum
)) {
930 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
931 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
932 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
938 /* strip off the authentication data */
940 ip6
= mtod(m
, struct ip6_hdr
*);
941 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
943 m
->m_flags
|= M_AUTHIPDGM
;
944 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthsucc
);
948 * update sequence number.
950 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
951 if (ipsec_updatereplay(seq
, sav
)) {
952 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
959 /* process main esp header. */
960 if (sav
->flags
& SADB_X_EXT_OLD
) {
962 esplen
= sizeof(struct esp
);
965 if (sav
->flags
& SADB_X_EXT_DERIV
)
966 esplen
= sizeof(struct esp
);
968 esplen
= sizeof(struct newesp
);
971 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
972 ipseclog((LOG_WARNING
,
973 "IPv6 ESP input: packet too short\n"));
974 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
978 #ifndef PULLDOWN_TEST
979 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, return IPPROTO_DONE
); /*XXX*/
981 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
983 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
988 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
991 * pre-compute and cache intermediate key
993 if (esp_schedule(algo
, sav
) != 0) {
994 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
999 * decrypt the packet.
1002 panic("internal error: no decrypt function");
1003 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
1004 /* m is already freed */
1006 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
1007 ipsec_logsastr(sav
)));
1008 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1011 IPSEC_STAT_INCREMENT(ipsec6stat
.in_esphist
[sav
->alg_enc
]);
1013 m
->m_flags
|= M_DECRYPTED
;
1015 if (algo
->finalizedecrypt
)
1017 if ((*algo
->finalizedecrypt
)(sav
, saved_icv
, algo
->icvlen
)) {
1018 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
1019 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1020 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
1026 * find the trailer of the ESP.
1028 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
1030 nxt
= esptail
.esp_nxt
;
1031 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
1033 if (m
->m_pkthdr
.len
< taillen
1034 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
1035 ipseclog((LOG_WARNING
,
1036 "bad pad length in IPv6 ESP input: %s %s\n",
1037 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
1038 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1042 /* strip off the trailing pad area. */
1044 ip6
= mtod(m
, struct ip6_hdr
*);
1045 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
1047 if (*nproto
== IPPROTO_UDP
) {
1048 // offset includes the outer ip and udp header lengths.
1049 if (m
->m_len
< off
) {
1050 m
= m_pullup(m
, off
);
1052 ipseclog((LOG_DEBUG
,
1053 "IPv6 ESP input: invalid udp encapsulated ESP packet length\n"));
1054 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1059 // check the UDP encap header to detect changes in the source port, and then strip the header
1060 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
1061 // if peer is behind nat and this is the latest esp packet
1062 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
1063 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
1064 seq
&& sav
->replay
&&
1065 seq
>= sav
->replay
->lastseq
) {
1066 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip6
+ off
);
1067 if (encap_uh
->uh_sport
&&
1068 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
1069 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
1072 ip6
= esp6_input_strip_udp_encap(m
, off
);
1073 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip6
) + off
);
1077 /* was it transmitted over the IPsec tunnel SA? */
1078 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
1080 struct sockaddr_storage addr
;
1083 * strip off all the headers that precedes ESP header.
1084 * IP6 xx ESP IP6' payload -> IP6' payload
1086 * XXX more sanity checks
1087 * XXX relationship with gif?
1089 u_int32_t flowinfo
; /*net endian*/
1090 flowinfo
= ip6
->ip6_flow
;
1091 m_adj(m
, off
+ esplen
+ ivlen
);
1092 if (ifamily
== AF_INET6
) {
1093 struct sockaddr_in6
*ip6addr
;
1095 if (m
->m_len
< sizeof(*ip6
)) {
1096 #ifndef PULLDOWN_TEST
1098 * m_pullup is prohibited in KAME IPv6 input processing
1099 * but there's no other way!
1102 /* okay to pullup in m_pulldown style */
1104 m
= m_pullup(m
, sizeof(*ip6
));
1106 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1110 ip6
= mtod(m
, struct ip6_hdr
*);
1111 /* ECN consideration. */
1112 if (ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
) == 0) {
1113 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1116 if (!key_checktunnelsanity(sav
, AF_INET6
,
1117 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
1118 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1119 "in IPv6 ESP input: %s %s\n",
1120 ipsec6_logpacketstr(ip6
, spi
),
1121 ipsec_logsastr(sav
)));
1122 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1126 bzero(&addr
, sizeof(addr
));
1127 ip6addr
= (__typeof__(ip6addr
))&addr
;
1128 ip6addr
->sin6_family
= AF_INET6
;
1129 ip6addr
->sin6_len
= sizeof(*ip6addr
);
1130 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
1131 } else if (ifamily
== AF_INET
) {
1132 struct sockaddr_in
*ipaddr
;
1134 if (m
->m_len
< sizeof(*ip
)) {
1135 m
= m_pullup(m
, sizeof(*ip
));
1137 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1145 ip
= mtod(m
, struct ip
*);
1147 /* ECN consideration. */
1148 if (ip46_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip
->ip_tos
) == 0) {
1149 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1153 if (otos
!= ip
->ip_tos
) {
1154 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
1155 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
1156 sum
= (sum
>> 16) + (sum
& 0xffff);
1157 sum
+= (sum
>> 16); /* add carry */
1158 ip
->ip_sum
= htons(~sum
& 0xffff);
1161 if (!key_checktunnelsanity(sav
, AF_INET
,
1162 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
1163 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1164 "in ESP input: %s %s\n",
1165 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
1166 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1170 bzero(&addr
, sizeof(addr
));
1171 ipaddr
= (__typeof__(ipaddr
))&addr
;
1172 ipaddr
->sin_family
= AF_INET
;
1173 ipaddr
->sin_len
= sizeof(*ipaddr
);
1174 ipaddr
->sin_addr
= ip
->ip_dst
;
1177 key_sa_recordxfer(sav
, m
);
1178 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
1179 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
1180 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1184 // update the receiving interface address based on the inner address
1185 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
1187 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
1191 // Input via IPSec interface
1192 if (sav
->sah
->ipsec_if
!= NULL
) {
1193 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1202 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
1207 * strip off ESP header and IV.
1208 * even in m_pulldown case, we need to strip off ESP so that
1209 * we can always compute checksum for AH correctly.
1215 * Set the next header field of the previous header correctly.
1217 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
1220 stripsiz
= esplen
+ ivlen
;
1222 ip6
= mtod(m
, struct ip6_hdr
*);
1223 if (m
->m_len
>= stripsiz
+ off
) {
1224 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
1225 m
->m_data
+= stripsiz
;
1226 m
->m_len
-= stripsiz
;
1227 m
->m_pkthdr
.len
-= stripsiz
;
1230 * this comes with no copy if the boundary is on
1235 n
= m_split(m
, off
, M_DONTWAIT
);
1237 /* m is retained by m_split */
1241 /* m_cat does not update m_pkthdr.len */
1242 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
1246 #ifndef PULLDOWN_TEST
1248 * KAME requires that the packet to be contiguous on the
1249 * mbuf. We need to make that sure.
1250 * this kind of code should be avoided.
1251 * XXX other conditions to avoid running this part?
1253 if (m
->m_len
!= m
->m_pkthdr
.len
) {
1254 struct mbuf
*n
= NULL
;
1257 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1260 M_COPY_PKTHDR(n
, m
);
1261 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
1262 MCLGET(n
, M_DONTWAIT
);
1264 if ((n
->m_flags
& M_EXT
) == 0) {
1270 printf("esp6_input: mbuf allocation failed\n");
1274 if (m
->m_pkthdr
.len
<= maxlen
) {
1275 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
1276 n
->m_len
= m
->m_pkthdr
.len
;
1277 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1281 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
1283 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1286 m
->m_flags
&= ~M_PKTHDR
;
1292 ip6
= mtod(m
, struct ip6_hdr
*);
1293 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
1295 key_sa_recordxfer(sav
, m
);
1296 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
1297 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1302 * Set the csum valid flag, if we authenticated the
1303 * packet, the payload shouldn't be corrupt unless
1304 * it was corrupted before being signed on the other
1307 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
1308 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1309 m
->m_pkthdr
.csum_data
= 0xFFFF;
1312 // Input via IPSec interface
1313 if (sav
->sah
->ipsec_if
!= NULL
) {
1314 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1329 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1330 printf("DP esp6_input call free SA:0x%llx\n",
1331 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1332 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1334 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1339 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1340 printf("DP esp6_input call free SA:0x%llx\n",
1341 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1342 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1346 return IPPROTO_DONE
;
1350 esp6_ctlinput(int cmd
, struct sockaddr
*sa
, void *d
)
1352 const struct newesp
*espp
;
1354 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
1355 struct secasvar
*sav
;
1356 struct ip6_hdr
*ip6
;
1359 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1361 if (sa
->sa_family
!= AF_INET6
||
1362 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1364 if ((unsigned)cmd
>= PRC_NCMDS
)
1367 /* if the parameter is from icmp6, decode it. */
1369 ip6cp
= (struct ip6ctlparam
*)d
;
1371 ip6
= ip6cp
->ip6c_ip6
;
1372 off
= ip6cp
->ip6c_off
;
1380 * Notify the error to all possible sockets via pfctlinput2.
1381 * Since the upper layer information (such as protocol type,
1382 * source and destination ports) is embedded in the encrypted
1383 * data and might have been cut, we can't directly call
1384 * an upper layer ctlinput function. However, the pcbnotify
1385 * function will consider source and destination addresses
1386 * as well as the flow info value, and may be able to find
1387 * some PCB that should be notified.
1388 * Although pfctlinput2 will call esp6_ctlinput(), there is
1389 * no possibility of an infinite loop of function calls,
1390 * because we don't pass the inner IPv6 header.
1392 bzero(&ip6cp1
, sizeof(ip6cp1
));
1393 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
1394 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
1397 * Then go to special cases that need ESP header information.
1398 * XXX: We assume that when ip6 is non NULL,
1399 * M and OFF are valid.
1402 /* check if we can safely examine src and dst ports */
1403 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
1406 if (m
->m_len
< off
+ sizeof(esp
)) {
1408 * this should be rare case,
1409 * so we compromise on this copy...
1411 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
1414 espp
= (struct newesp
*)(void *)(mtod(m
, caddr_t
) + off
);
1416 if (cmd
== PRC_MSGSIZE
) {
1420 * Check to see if we have a valid SA corresponding to
1421 * the address in the ICMP message payload.
1423 sa6_src
= ip6cp
->ip6c_src
;
1424 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1425 sav
= key_allocsa(AF_INET6
,
1426 (caddr_t
)&sa6_src
->sin6_addr
,
1427 (caddr_t
)&sa6_dst
->sin6_addr
,
1428 IPPROTO_ESP
, espp
->esp_spi
);
1430 if (sav
->state
== SADB_SASTATE_MATURE
||
1431 sav
->state
== SADB_SASTATE_DYING
)
1433 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1436 /* XXX Further validation? */
1439 * Depending on the value of "valid" and routing table
1440 * size (mtudisc_{hi,lo}wat), we will:
1441 * - recalcurate the new MTU and create the
1442 * corresponding routing entry, or
1443 * - ignore the MTU change notification.
1445 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1448 /* we normally notify any pcb here */