2 * Copyright (c) 2008-2013 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>
126 #include <sys/kdebug.h>
127 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
128 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
129 #define DBG_FNC_ESPIN NETDBG_CODE(DBG_NETIPSEC, (6 << 8))
130 #define DBG_FNC_DECRYPT NETDBG_CODE(DBG_NETIPSEC, (7 << 8))
131 #define IPLEN_FLIPPED
133 extern lck_mtx_t
*sadb_mutex
;
137 (sizeof(struct esp) < sizeof(struct newesp) \
138 ? sizeof(struct newesp) : sizeof(struct esp))
141 esp4_input_strip_udp_encap (struct mbuf
*m
, int iphlen
)
143 // strip the udp header that's encapsulating ESP
145 size_t stripsiz
= sizeof(struct udphdr
);
147 ip
= mtod(m
, __typeof__(ip
));
148 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), iphlen
);
149 m
->m_data
+= stripsiz
;
150 m
->m_len
-= stripsiz
;
151 m
->m_pkthdr
.len
-= stripsiz
;
152 ip
= mtod(m
, __typeof__(ip
));
153 ip
->ip_len
= ip
->ip_len
- stripsiz
;
154 ip
->ip_p
= IPPROTO_ESP
;
158 static struct ip6_hdr
*
159 esp6_input_strip_udp_encap (struct mbuf
*m
, int ip6hlen
)
161 // strip the udp header that's encapsulating ESP
163 size_t stripsiz
= sizeof(struct udphdr
);
165 ip6
= mtod(m
, __typeof__(ip6
));
166 ovbcopy((caddr_t
)ip6
, (caddr_t
)(((u_char
*)ip6
) + stripsiz
), ip6hlen
);
167 m
->m_data
+= stripsiz
;
168 m
->m_len
-= stripsiz
;
169 m
->m_pkthdr
.len
-= stripsiz
;
170 ip6
= mtod(m
, __typeof__(ip6
));
171 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
172 ip6
->ip6_nxt
= IPPROTO_ESP
;
186 struct esptail esptail
;
189 struct secasvar
*sav
= NULL
;
192 const struct esp_algorithm
*algo
;
198 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_START
, 0,0,0,0,0);
199 /* sanity check for alignment. */
200 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
201 ipseclog((LOG_ERR
, "IPv4 ESP input: packet alignment problem "
202 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
203 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
207 if (m
->m_len
< off
+ ESPMAXLEN
) {
208 m
= m_pullup(m
, off
+ ESPMAXLEN
);
211 "IPv4 ESP input: can't pullup in esp4_input\n"));
212 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
217 /* Expect 32-bit aligned data pointer on strict-align platforms */
218 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
220 ip
= mtod(m
, struct ip
*);
221 // expect udp-encap and esp packets only
222 if (ip
->ip_p
!= IPPROTO_ESP
&&
223 !(ip
->ip_p
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
))) {
225 "IPv4 ESP input: invalid protocol type\n"));
226 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
229 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
231 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
233 hlen
= ip
->ip_hl
<< 2;
236 /* find the sassoc. */
239 if ((sav
= key_allocsa(AF_INET
,
240 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
241 IPPROTO_ESP
, spi
)) == 0) {
242 ipseclog((LOG_WARNING
,
243 "IPv4 ESP input: no key association found for spi %u\n",
244 (u_int32_t
)ntohl(spi
)));
245 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
248 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
249 printf("DP esp4_input called to allocate SA:0x%llx\n",
250 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
251 if (sav
->state
!= SADB_SASTATE_MATURE
252 && sav
->state
!= SADB_SASTATE_DYING
) {
254 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
255 (u_int32_t
)ntohl(spi
)));
256 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
259 algo
= esp_algorithm_lookup(sav
->alg_enc
);
261 ipseclog((LOG_DEBUG
, "IPv4 ESP input: "
262 "unsupported encryption algorithm for spi %u\n",
263 (u_int32_t
)ntohl(spi
)));
264 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
268 /* check if we have proper ivlen information */
271 ipseclog((LOG_ERR
, "inproper ivlen in IPv4 ESP input: %s %s\n",
272 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
273 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
277 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
279 /* Save ICV from packet for verification later */
281 unsigned char saved_icv
[AH_MAXSUMSIZE
];
282 if (algo
->finalizedecrypt
) {
284 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) saved_icv
);
288 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
289 && (sav
->alg_auth
&& sav
->key_auth
)))
292 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
293 sav
->alg_auth
== SADB_AALG_NONE
)
297 * check for sequence number.
299 if (ipsec_chkreplay(seq
, sav
))
302 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
303 ipseclog((LOG_WARNING
,
304 "replay packet in IPv4 ESP input: %s %s\n",
305 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
311 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
312 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
313 const struct ah_algorithm
*sumalgo
;
315 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
318 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
319 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
320 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
323 if (AH_MAXSUMSIZE
< siz
) {
325 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
327 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
331 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
333 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
334 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
335 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
336 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
340 if (bcmp(sum0
, sum
, siz
) != 0) {
341 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
342 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
343 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
349 /* strip off the authentication data */
351 ip
= mtod(m
, struct ip
*);
353 ip
->ip_len
= ip
->ip_len
- siz
;
355 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
357 m
->m_flags
|= M_AUTHIPDGM
;
358 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthsucc
);
362 * update sequence number.
364 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
365 if (ipsec_updatereplay(seq
, sav
)) {
366 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
373 /* process main esp header. */
374 if (sav
->flags
& SADB_X_EXT_OLD
) {
376 esplen
= sizeof(struct esp
);
379 if (sav
->flags
& SADB_X_EXT_DERIV
)
380 esplen
= sizeof(struct esp
);
382 esplen
= sizeof(struct newesp
);
385 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
386 ipseclog((LOG_WARNING
,
387 "IPv4 ESP input: packet too short\n"));
388 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
392 if (m
->m_len
< off
+ esplen
+ ivlen
) {
393 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
396 "IPv4 ESP input: can't pullup in esp4_input\n"));
397 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
403 * pre-compute and cache intermediate key
405 if (esp_schedule(algo
, sav
) != 0) {
406 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
411 * decrypt the packet.
414 panic("internal error: no decrypt function");
415 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_START
, 0,0,0,0,0);
416 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
417 /* m is already freed */
419 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
420 ipsec_logsastr(sav
)));
421 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
422 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
425 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 2,0,0,0,0);
426 IPSEC_STAT_INCREMENT(ipsecstat
.in_esphist
[sav
->alg_enc
]);
428 m
->m_flags
|= M_DECRYPTED
;
430 if (algo
->finalizedecrypt
)
432 unsigned char tag
[algo
->icvlen
];
433 if ((*algo
->finalizedecrypt
)(sav
, tag
, algo
->icvlen
)) {
434 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
435 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
436 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
439 if (memcmp(saved_icv
, tag
, algo
->icvlen
)) {
440 ipseclog((LOG_ERR
, "packet decryption ICV mismatch\n"));
441 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
442 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
448 * find the trailer of the ESP.
450 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
452 nxt
= esptail
.esp_nxt
;
453 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
455 if (m
->m_pkthdr
.len
< taillen
456 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
457 ipseclog((LOG_WARNING
,
458 "bad pad length in IPv4 ESP input: %s %s\n",
459 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
460 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
464 /* strip off the trailing pad area. */
466 ip
= mtod(m
, struct ip
*);
468 ip
->ip_len
= ip
->ip_len
- taillen
;
470 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
472 if (ip
->ip_p
== IPPROTO_UDP
) {
473 // offset includes the outer ip and udp header lengths.
474 if (m
->m_len
< off
) {
475 m
= m_pullup(m
, off
);
478 "IPv4 ESP input: invalid udp encapsulated ESP packet length \n"));
479 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
484 // check the UDP encap header to detect changes in the source port, and then strip the header
485 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
486 // if peer is behind nat and this is the latest esp packet
487 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
488 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
489 seq
&& sav
->replay
&&
490 seq
>= sav
->replay
->lastseq
) {
491 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip
+ off
);
492 if (encap_uh
->uh_sport
&&
493 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
494 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
497 ip
= esp4_input_strip_udp_encap(m
, off
);
498 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
501 if (sav
->utun_is_keepalive_fn
) {
502 if (sav
->utun_is_keepalive_fn(sav
->utun_pcb
, &m
, nxt
, sav
->flags
, (off
+ esplen
+ ivlen
))) {
504 // not really bad, we just wanna exit
505 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
512 /* was it transmitted over the IPsec tunnel SA? */
513 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
515 struct sockaddr_storage addr
;
518 * strip off all the headers that precedes ESP header.
519 * IP4 xx ESP IP4' payload -> IP4' payload
521 * XXX more sanity checks
522 * XXX relationship with gif?
528 m_adj(m
, off
+ esplen
+ ivlen
);
529 if (ifamily
== AF_INET
) {
530 struct sockaddr_in
*ipaddr
;
532 if (m
->m_len
< sizeof(*ip
)) {
533 m
= m_pullup(m
, sizeof(*ip
));
535 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
539 ip
= mtod(m
, struct ip
*);
540 /* ECN consideration. */
543 if (ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
) == 0) {
544 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
548 if (otos
!= ip
->ip_tos
) {
549 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
550 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
551 sum
= (sum
>> 16) + (sum
& 0xffff);
552 sum
+= (sum
>> 16); /* add carry */
553 ip
->ip_sum
= htons(~sum
& 0xffff);
556 if (!key_checktunnelsanity(sav
, AF_INET
,
557 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
558 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
559 "in ESP input: %s %s\n",
560 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
561 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
565 if (ip_doscopedroute
) {
566 bzero(&addr
, sizeof(addr
));
567 ipaddr
= (__typeof__(ipaddr
))&addr
;
568 ipaddr
->sin_family
= AF_INET
;
569 ipaddr
->sin_len
= sizeof(*ipaddr
);
570 ipaddr
->sin_addr
= ip
->ip_dst
;
573 } else if (ifamily
== AF_INET6
) {
574 struct sockaddr_in6
*ip6addr
;
577 * m_pullup is prohibited in KAME IPv6 input processing
578 * but there's no other way!
580 if (m
->m_len
< sizeof(*ip6
)) {
581 m
= m_pullup(m
, sizeof(*ip6
));
583 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
589 * Expect 32-bit aligned data pointer on strict-align
592 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
594 ip6
= mtod(m
, struct ip6_hdr
*);
596 /* ECN consideration. */
597 if (ip64_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip6
->ip6_flow
) == 0) {
598 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
602 if (!key_checktunnelsanity(sav
, AF_INET6
,
603 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
604 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
605 "in ESP input: %s %s\n",
606 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
607 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
611 if (ip6_doscopedroute
) {
612 bzero(&addr
, sizeof(addr
));
613 ip6addr
= (__typeof__(ip6addr
))&addr
;
614 ip6addr
->sin6_family
= AF_INET6
;
615 ip6addr
->sin6_len
= sizeof(*ip6addr
);
616 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
620 ipseclog((LOG_ERR
, "ipsec tunnel unsupported address family "
625 key_sa_recordxfer(sav
, m
);
626 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
627 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
628 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
632 if (ip_doscopedroute
|| ip6_doscopedroute
) {
633 // update the receiving interface address based on the inner address
634 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
636 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
641 /* Clear the csum flags, they can't be valid for the inner headers */
642 m
->m_pkthdr
.csum_flags
= 0;
644 // Input via IPSec interface
645 if (sav
->sah
->ipsec_if
!= NULL
) {
646 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
654 if (sav
->utun_in_fn
) {
655 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, ifamily
== AF_INET
? PF_INET
: PF_INET6
))) {
657 // we just wanna exit since packet has been completely processed
662 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
666 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 2,0,0,0,0);
669 * strip off ESP header and IV.
670 * even in m_pulldown case, we need to strip off ESP so that
671 * we can always compute checksum for AH correctly.
675 stripsiz
= esplen
+ ivlen
;
677 ip
= mtod(m
, struct ip
*);
678 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
679 m
->m_data
+= stripsiz
;
680 m
->m_len
-= stripsiz
;
681 m
->m_pkthdr
.len
-= stripsiz
;
683 ip
= mtod(m
, struct ip
*);
685 ip
->ip_len
= ip
->ip_len
- stripsiz
;
687 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
691 key_sa_recordxfer(sav
, m
);
692 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
693 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
698 * Set the csum valid flag, if we authenticated the
699 * packet, the payload shouldn't be corrupt unless
700 * it was corrupted before being signed on the other
703 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
704 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
705 m
->m_pkthdr
.csum_data
= 0xFFFF;
708 if (nxt
!= IPPROTO_DONE
) {
709 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
710 ipsec4_in_reject(m
, NULL
)) {
711 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
714 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 3,0,0,0,0);
716 /* translate encapsulated UDP port ? */
717 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
720 if (nxt
!= IPPROTO_UDP
) { /* not UPD packet - drop it */
721 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
725 if (m
->m_len
< off
+ sizeof(struct udphdr
)) {
726 m
= m_pullup(m
, off
+ sizeof(struct udphdr
));
729 "IPv4 ESP input: can't pullup UDP header in esp4_input\n"));
730 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
733 ip
= mtod(m
, struct ip
*);
735 udp
= (struct udphdr
*)(void *)(((u_int8_t
*)ip
) + off
);
737 lck_mtx_lock(sadb_mutex
);
738 if (sav
->natt_encapsulated_src_port
== 0) {
739 sav
->natt_encapsulated_src_port
= udp
->uh_sport
;
740 } else if (sav
->natt_encapsulated_src_port
!= udp
->uh_sport
) { /* something wrong */
741 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
742 lck_mtx_unlock(sadb_mutex
);
745 lck_mtx_unlock(sadb_mutex
);
746 udp
->uh_sport
= htons(sav
->remote_ike_port
);
750 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
751 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
752 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
754 // Input via IPSec interface
755 if (sav
->sah
->ipsec_if
!= NULL
) {
756 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
757 ip
->ip_off
= htons(ip
->ip_off
);
759 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
760 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
768 if (sav
->utun_in_fn
) {
769 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET
))) {
771 // we just wanna exit since packet has been completely processed
776 ip_proto_dispatch_in(m
, off
, nxt
, 0);
784 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
785 printf("DP esp4_input call free SA:0x%llx\n",
786 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
787 key_freesav(sav
, KEY_SADB_UNLOCKED
);
789 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
794 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
795 printf("DP esp4_input call free SA:0x%llx\n",
796 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
797 key_freesav(sav
, KEY_SADB_UNLOCKED
);
801 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 4,0,0,0,0);
808 esp6_input(struct mbuf
**mp
, int *offp
, int proto
)
810 #pragma unused(proto)
811 struct mbuf
*m
= *mp
;
816 struct esptail esptail
;
819 struct secasvar
*sav
= NULL
;
823 const struct esp_algorithm
*algo
;
828 /* sanity check for alignment. */
829 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
830 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
831 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
832 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
836 #ifndef PULLDOWN_TEST
837 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, {return IPPROTO_DONE
;});
838 esp
= (struct esp
*)(void *)(mtod(m
, caddr_t
) + off
);
840 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
842 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
846 /* Expect 32-bit data aligned pointer on strict-align platforms */
847 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
849 ip6
= mtod(m
, struct ip6_hdr
*);
851 if (ntohs(ip6
->ip6_plen
) == 0) {
852 ipseclog((LOG_ERR
, "IPv6 ESP input: "
853 "ESP with IPv6 jumbogram is not supported.\n"));
854 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
858 nproto
= ip6_get_prevhdr(m
, off
);
859 if (nproto
== NULL
|| (*nproto
!= IPPROTO_ESP
&&
860 !(*nproto
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
)))) {
861 ipseclog((LOG_DEBUG
, "IPv6 ESP input: invalid protocol type\n"));
862 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
866 /* find the sassoc. */
869 if ((sav
= key_allocsa(AF_INET6
,
870 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
871 IPPROTO_ESP
, spi
)) == 0) {
872 ipseclog((LOG_WARNING
,
873 "IPv6 ESP input: no key association found for spi %u\n",
874 (u_int32_t
)ntohl(spi
)));
875 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
878 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
879 printf("DP esp6_input called to allocate SA:0x%llx\n",
880 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
881 if (sav
->state
!= SADB_SASTATE_MATURE
882 && sav
->state
!= SADB_SASTATE_DYING
) {
884 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
885 (u_int32_t
)ntohl(spi
)));
886 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
889 algo
= esp_algorithm_lookup(sav
->alg_enc
);
891 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
892 "unsupported encryption algorithm for spi %u\n",
893 (u_int32_t
)ntohl(spi
)));
894 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
898 /* check if we have proper ivlen information */
901 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
902 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
903 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
907 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
909 /* Save ICV from packet for verification later */
911 unsigned char saved_icv
[AH_MAXSUMSIZE
];
912 if (algo
->finalizedecrypt
) {
914 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) saved_icv
);
918 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
919 && (sav
->alg_auth
&& sav
->key_auth
)))
922 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
923 sav
->alg_auth
== SADB_AALG_NONE
)
927 * check for sequence number.
929 if (ipsec_chkreplay(seq
, sav
))
932 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
933 ipseclog((LOG_WARNING
,
934 "replay packet in IPv6 ESP input: %s %s\n",
935 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
941 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
942 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
943 const struct ah_algorithm
*sumalgo
;
945 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
948 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
949 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
950 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
953 if (AH_MAXSUMSIZE
< siz
) {
955 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
957 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
961 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
963 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
964 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
965 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
966 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
970 if (bcmp(sum0
, sum
, siz
) != 0) {
971 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
972 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
973 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
979 /* strip off the authentication data */
981 ip6
= mtod(m
, struct ip6_hdr
*);
982 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
984 m
->m_flags
|= M_AUTHIPDGM
;
985 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthsucc
);
989 * update sequence number.
991 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
992 if (ipsec_updatereplay(seq
, sav
)) {
993 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
1000 /* process main esp header. */
1001 if (sav
->flags
& SADB_X_EXT_OLD
) {
1003 esplen
= sizeof(struct esp
);
1006 if (sav
->flags
& SADB_X_EXT_DERIV
)
1007 esplen
= sizeof(struct esp
);
1009 esplen
= sizeof(struct newesp
);
1012 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
1013 ipseclog((LOG_WARNING
,
1014 "IPv6 ESP input: packet too short\n"));
1015 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1019 #ifndef PULLDOWN_TEST
1020 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, return IPPROTO_DONE
); /*XXX*/
1022 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
1024 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1029 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
1032 * pre-compute and cache intermediate key
1034 if (esp_schedule(algo
, sav
) != 0) {
1035 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1040 * decrypt the packet.
1043 panic("internal error: no decrypt function");
1044 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
1045 /* m is already freed */
1047 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
1048 ipsec_logsastr(sav
)));
1049 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1052 IPSEC_STAT_INCREMENT(ipsec6stat
.in_esphist
[sav
->alg_enc
]);
1054 m
->m_flags
|= M_DECRYPTED
;
1056 if (algo
->finalizedecrypt
)
1058 unsigned char tag
[algo
->icvlen
];
1059 if ((*algo
->finalizedecrypt
)(sav
, tag
, algo
->icvlen
)) {
1060 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
1061 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1062 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
1065 if (memcmp(saved_icv
, tag
, algo
->icvlen
)) {
1066 ipseclog((LOG_ERR
, "packet decryption ICV mismatch\n"));
1067 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1068 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
1074 * find the trailer of the ESP.
1076 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
1078 nxt
= esptail
.esp_nxt
;
1079 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
1081 if (m
->m_pkthdr
.len
< taillen
1082 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
1083 ipseclog((LOG_WARNING
,
1084 "bad pad length in IPv6 ESP input: %s %s\n",
1085 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
1086 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1090 /* strip off the trailing pad area. */
1092 ip6
= mtod(m
, struct ip6_hdr
*);
1093 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
1095 if (sav
->utun_is_keepalive_fn
) {
1096 if (sav
->utun_is_keepalive_fn(sav
->utun_pcb
, &m
, nxt
, sav
->flags
, (off
+ esplen
+ ivlen
))) {
1098 // not really bad, we just wanna exit
1099 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1106 if (*nproto
== IPPROTO_UDP
) {
1107 // offset includes the outer ip and udp header lengths.
1108 if (m
->m_len
< off
) {
1109 m
= m_pullup(m
, off
);
1111 ipseclog((LOG_DEBUG
,
1112 "IPv6 ESP input: invalid udp encapsulated ESP packet length\n"));
1113 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1118 // check the UDP encap header to detect changes in the source port, and then strip the header
1119 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
1120 // if peer is behind nat and this is the latest esp packet
1121 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
1122 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
1123 seq
&& sav
->replay
&&
1124 seq
>= sav
->replay
->lastseq
) {
1125 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip6
+ off
);
1126 if (encap_uh
->uh_sport
&&
1127 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
1128 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
1131 ip6
= esp6_input_strip_udp_encap(m
, off
);
1132 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip6
) + off
);
1136 /* was it transmitted over the IPsec tunnel SA? */
1137 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
1139 struct sockaddr_storage addr
;
1142 * strip off all the headers that precedes ESP header.
1143 * IP6 xx ESP IP6' payload -> IP6' payload
1145 * XXX more sanity checks
1146 * XXX relationship with gif?
1148 u_int32_t flowinfo
; /*net endian*/
1149 flowinfo
= ip6
->ip6_flow
;
1150 m_adj(m
, off
+ esplen
+ ivlen
);
1151 if (ifamily
== AF_INET6
) {
1152 if (m
->m_len
< sizeof(*ip6
)) {
1153 #ifndef PULLDOWN_TEST
1155 * m_pullup is prohibited in KAME IPv6 input processing
1156 * but there's no other way!
1159 /* okay to pullup in m_pulldown style */
1161 m
= m_pullup(m
, sizeof(*ip6
));
1163 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1167 ip6
= mtod(m
, struct ip6_hdr
*);
1168 /* ECN consideration. */
1169 if (ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
) == 0) {
1170 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1173 if (!key_checktunnelsanity(sav
, AF_INET6
,
1174 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
1175 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1176 "in IPv6 ESP input: %s %s\n",
1177 ipsec6_logpacketstr(ip6
, spi
),
1178 ipsec_logsastr(sav
)));
1179 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1183 if (ip6_doscopedroute
) {
1184 struct sockaddr_in6
*ip6addr
;
1186 bzero(&addr
, sizeof(addr
));
1187 ip6addr
= (__typeof__(ip6addr
))&addr
;
1188 ip6addr
->sin6_family
= AF_INET6
;
1189 ip6addr
->sin6_len
= sizeof(*ip6addr
);
1190 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
1192 } else if (ifamily
== AF_INET
) {
1193 struct sockaddr_in
*ipaddr
;
1195 if (m
->m_len
< sizeof(*ip
)) {
1196 m
= m_pullup(m
, sizeof(*ip
));
1198 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1206 ip
= mtod(m
, struct ip
*);
1208 /* ECN consideration. */
1209 if (ip46_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip
->ip_tos
) == 0) {
1210 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1214 if (otos
!= ip
->ip_tos
) {
1215 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
1216 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
1217 sum
= (sum
>> 16) + (sum
& 0xffff);
1218 sum
+= (sum
>> 16); /* add carry */
1219 ip
->ip_sum
= htons(~sum
& 0xffff);
1222 if (!key_checktunnelsanity(sav
, AF_INET
,
1223 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
1224 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1225 "in ESP input: %s %s\n",
1226 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
1227 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1231 if (ip_doscopedroute
) {
1232 bzero(&addr
, sizeof(addr
));
1233 ipaddr
= (__typeof__(ipaddr
))&addr
;
1234 ipaddr
->sin_family
= AF_INET
;
1235 ipaddr
->sin_len
= sizeof(*ipaddr
);
1236 ipaddr
->sin_addr
= ip
->ip_dst
;
1240 key_sa_recordxfer(sav
, m
);
1241 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
1242 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
1243 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1247 if (ip_doscopedroute
|| ip6_doscopedroute
) {
1248 // update the receiving interface address based on the inner address
1249 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
1251 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
1256 // Input via IPSec interface
1257 if (sav
->sah
->ipsec_if
!= NULL
) {
1258 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1267 if (sav
->utun_in_fn
) {
1268 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET6
))) {
1270 // we just wanna exit since packet has been completely processed
1275 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
1280 * strip off ESP header and IV.
1281 * even in m_pulldown case, we need to strip off ESP so that
1282 * we can always compute checksum for AH correctly.
1288 * Set the next header field of the previous header correctly.
1290 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
1293 stripsiz
= esplen
+ ivlen
;
1295 ip6
= mtod(m
, struct ip6_hdr
*);
1296 if (m
->m_len
>= stripsiz
+ off
) {
1297 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
1298 m
->m_data
+= stripsiz
;
1299 m
->m_len
-= stripsiz
;
1300 m
->m_pkthdr
.len
-= stripsiz
;
1303 * this comes with no copy if the boundary is on
1308 n
= m_split(m
, off
, M_DONTWAIT
);
1310 /* m is retained by m_split */
1314 /* m_cat does not update m_pkthdr.len */
1315 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
1319 #ifndef PULLDOWN_TEST
1321 * KAME requires that the packet to be contiguous on the
1322 * mbuf. We need to make that sure.
1323 * this kind of code should be avoided.
1324 * XXX other conditions to avoid running this part?
1326 if (m
->m_len
!= m
->m_pkthdr
.len
) {
1327 struct mbuf
*n
= NULL
;
1330 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1333 M_COPY_PKTHDR(n
, m
);
1334 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
1335 MCLGET(n
, M_DONTWAIT
);
1337 if ((n
->m_flags
& M_EXT
) == 0) {
1343 printf("esp6_input: mbuf allocation failed\n");
1347 if (m
->m_pkthdr
.len
<= maxlen
) {
1348 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
1349 n
->m_len
= m
->m_pkthdr
.len
;
1350 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1354 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
1356 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1359 m
->m_flags
&= ~M_PKTHDR
;
1365 ip6
= mtod(m
, struct ip6_hdr
*);
1366 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
1368 key_sa_recordxfer(sav
, m
);
1369 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
1370 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1375 * Set the csum valid flag, if we authenticated the
1376 * packet, the payload shouldn't be corrupt unless
1377 * it was corrupted before being signed on the other
1380 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
1381 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1382 m
->m_pkthdr
.csum_data
= 0xFFFF;
1385 // Input via IPSec interface
1386 if (sav
->sah
->ipsec_if
!= NULL
) {
1387 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1396 if (sav
->utun_in_fn
) {
1397 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET6
))) {
1399 // we just wanna exit since packet has been completely processed
1409 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1410 printf("DP esp6_input call free SA:0x%llx\n",
1411 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1412 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1414 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1419 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1420 printf("DP esp6_input call free SA:0x%llx\n",
1421 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1422 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1426 return IPPROTO_DONE
;
1430 esp6_ctlinput(cmd
, sa
, d
)
1432 struct sockaddr
*sa
;
1435 const struct newesp
*espp
;
1437 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
1438 struct secasvar
*sav
;
1439 struct ip6_hdr
*ip6
;
1442 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1444 if (sa
->sa_family
!= AF_INET6
||
1445 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1447 if ((unsigned)cmd
>= PRC_NCMDS
)
1450 /* if the parameter is from icmp6, decode it. */
1452 ip6cp
= (struct ip6ctlparam
*)d
;
1454 ip6
= ip6cp
->ip6c_ip6
;
1455 off
= ip6cp
->ip6c_off
;
1463 * Notify the error to all possible sockets via pfctlinput2.
1464 * Since the upper layer information (such as protocol type,
1465 * source and destination ports) is embedded in the encrypted
1466 * data and might have been cut, we can't directly call
1467 * an upper layer ctlinput function. However, the pcbnotify
1468 * function will consider source and destination addresses
1469 * as well as the flow info value, and may be able to find
1470 * some PCB that should be notified.
1471 * Although pfctlinput2 will call esp6_ctlinput(), there is
1472 * no possibility of an infinite loop of function calls,
1473 * because we don't pass the inner IPv6 header.
1475 bzero(&ip6cp1
, sizeof(ip6cp1
));
1476 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
1477 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
1480 * Then go to special cases that need ESP header information.
1481 * XXX: We assume that when ip6 is non NULL,
1482 * M and OFF are valid.
1485 /* check if we can safely examine src and dst ports */
1486 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
1489 if (m
->m_len
< off
+ sizeof(esp
)) {
1491 * this should be rare case,
1492 * so we compromise on this copy...
1494 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
1497 espp
= (struct newesp
*)(void *)(mtod(m
, caddr_t
) + off
);
1499 if (cmd
== PRC_MSGSIZE
) {
1503 * Check to see if we have a valid SA corresponding to
1504 * the address in the ICMP message payload.
1506 sa6_src
= ip6cp
->ip6c_src
;
1507 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1508 sav
= key_allocsa(AF_INET6
,
1509 (caddr_t
)&sa6_src
->sin6_addr
,
1510 (caddr_t
)&sa6_dst
->sin6_addr
,
1511 IPPROTO_ESP
, espp
->esp_spi
);
1513 if (sav
->state
== SADB_SASTATE_MATURE
||
1514 sav
->state
== SADB_SASTATE_DYING
)
1516 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1519 /* XXX Further validation? */
1522 * Depending on the value of "valid" and routing table
1523 * size (mtudisc_{hi,lo}wat), we will:
1524 * - recalcurate the new MTU and create the
1525 * corresponding routing entry, or
1526 * - ignore the MTU change notification.
1528 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1531 /* we normally notify any pcb here */