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
;
168 struct esptail esptail
;
171 struct secasvar
*sav
= NULL
;
174 const struct esp_algorithm
*algo
;
180 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_START
, 0,0,0,0,0);
181 /* sanity check for alignment. */
182 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
183 ipseclog((LOG_ERR
, "IPv4 ESP input: packet alignment problem "
184 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
185 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
189 if (m
->m_len
< off
+ ESPMAXLEN
) {
190 m
= m_pullup(m
, off
+ ESPMAXLEN
);
193 "IPv4 ESP input: can't pullup in esp4_input\n"));
194 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
199 /* Expect 32-bit aligned data pointer on strict-align platforms */
200 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
202 ip
= mtod(m
, struct ip
*);
203 // expect udp-encap and esp packets only
204 if (ip
->ip_p
!= IPPROTO_ESP
&&
205 !(ip
->ip_p
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
))) {
207 "IPv4 ESP input: invalid protocol type\n"));
208 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
211 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
213 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
215 hlen
= ip
->ip_hl
<< 2;
218 /* find the sassoc. */
221 if ((sav
= key_allocsa(AF_INET
,
222 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
223 IPPROTO_ESP
, spi
)) == 0) {
224 ipseclog((LOG_WARNING
,
225 "IPv4 ESP input: no key association found for spi %u\n",
226 (u_int32_t
)ntohl(spi
)));
227 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
230 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
231 printf("DP esp4_input called to allocate SA:0x%llx\n",
232 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
233 if (sav
->state
!= SADB_SASTATE_MATURE
234 && sav
->state
!= SADB_SASTATE_DYING
) {
236 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
237 (u_int32_t
)ntohl(spi
)));
238 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
241 algo
= esp_algorithm_lookup(sav
->alg_enc
);
243 ipseclog((LOG_DEBUG
, "IPv4 ESP input: "
244 "unsupported encryption algorithm for spi %u\n",
245 (u_int32_t
)ntohl(spi
)));
246 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
250 /* check if we have proper ivlen information */
253 ipseclog((LOG_ERR
, "inproper ivlen in IPv4 ESP input: %s %s\n",
254 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
255 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
259 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
260 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
261 && (sav
->alg_auth
&& sav
->key_auth
)))
264 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
265 sav
->alg_auth
== SADB_AALG_NONE
)
269 * check for sequence number.
271 if (ipsec_chkreplay(seq
, sav
))
274 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
275 ipseclog((LOG_WARNING
,
276 "replay packet in IPv4 ESP input: %s %s\n",
277 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
283 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
284 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
285 const struct ah_algorithm
*sumalgo
;
288 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
291 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
292 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
293 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
296 if (AH_MAXSUMSIZE
< siz
) {
298 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
300 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
304 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
306 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
307 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
308 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
309 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
313 if (bcmp(sum0
, sum
, siz
) != 0) {
314 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
315 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
316 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthfail
);
320 /* strip off the authentication data */
322 ip
= mtod(m
, struct ip
*);
324 ip
->ip_len
= ip
->ip_len
- siz
;
326 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
328 m
->m_flags
|= M_AUTHIPDGM
;
329 IPSEC_STAT_INCREMENT(ipsecstat
.in_espauthsucc
);
333 * update sequence number.
335 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
336 if (ipsec_updatereplay(seq
, sav
)) {
337 IPSEC_STAT_INCREMENT(ipsecstat
.in_espreplay
);
344 /* process main esp header. */
345 if (sav
->flags
& SADB_X_EXT_OLD
) {
347 esplen
= sizeof(struct esp
);
350 if (sav
->flags
& SADB_X_EXT_DERIV
)
351 esplen
= sizeof(struct esp
);
353 esplen
= sizeof(struct newesp
);
356 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
357 ipseclog((LOG_WARNING
,
358 "IPv4 ESP input: packet too short\n"));
359 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
363 if (m
->m_len
< off
+ esplen
+ ivlen
) {
364 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
367 "IPv4 ESP input: can't pullup in esp4_input\n"));
368 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
374 * pre-compute and cache intermediate key
376 if (esp_schedule(algo
, sav
) != 0) {
377 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
382 * decrypt the packet.
385 panic("internal error: no decrypt function");
386 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_START
, 0,0,0,0,0);
387 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
388 /* m is already freed */
390 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
391 ipsec_logsastr(sav
)));
392 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
393 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
396 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 2,0,0,0,0);
397 IPSEC_STAT_INCREMENT(ipsecstat
.in_esphist
[sav
->alg_enc
]);
399 m
->m_flags
|= M_DECRYPTED
;
402 * find the trailer of the ESP.
404 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
406 nxt
= esptail
.esp_nxt
;
407 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
409 if (m
->m_pkthdr
.len
< taillen
410 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
411 ipseclog((LOG_WARNING
,
412 "bad pad length in IPv4 ESP input: %s %s\n",
413 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
414 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
418 /* strip off the trailing pad area. */
420 ip
= mtod(m
, struct ip
*);
422 ip
->ip_len
= ip
->ip_len
- taillen
;
424 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
426 if (ip
->ip_p
== IPPROTO_UDP
) {
427 // offset includes the outer ip and udp header lengths.
428 if (m
->m_len
< off
) {
429 m
= m_pullup(m
, off
);
432 "IPv4 ESP input: invalid udp encapsulated ESP packet length \n"));
433 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
438 // check the UDP encap header to detect changes in the source port, and then strip the header
439 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
440 // if peer is behind nat and this is the latest esp packet
441 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
442 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
443 seq
&& sav
->replay
&&
444 seq
>= sav
->replay
->lastseq
) {
445 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip
+ off
);
446 if (encap_uh
->uh_sport
&&
447 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
448 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
451 ip
= esp4_input_strip_UDP_encap(m
, off
);
452 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
455 if (sav
->utun_is_keepalive_fn
) {
456 if (sav
->utun_is_keepalive_fn(sav
->utun_pcb
, &m
, nxt
, sav
->flags
, (off
+ esplen
+ ivlen
))) {
458 // not really bad, we just wanna exit
459 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
466 /* was it transmitted over the IPsec tunnel SA? */
467 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
469 struct sockaddr_storage addr
;
472 * strip off all the headers that precedes ESP header.
473 * IP4 xx ESP IP4' payload -> IP4' payload
475 * XXX more sanity checks
476 * XXX relationship with gif?
481 m_adj(m
, off
+ esplen
+ ivlen
);
482 if (ifamily
== AF_INET
) {
483 struct sockaddr_in
*ipaddr
;
485 if (m
->m_len
< sizeof(*ip
)) {
486 m
= m_pullup(m
, sizeof(*ip
));
488 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
492 ip
= mtod(m
, struct ip
*);
493 /* ECN consideration. */
494 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
495 if (!key_checktunnelsanity(sav
, AF_INET
,
496 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
497 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
498 "in ESP input: %s %s\n",
499 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
500 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
504 if (ip_doscopedroute
) {
505 bzero(&addr
, sizeof(addr
));
506 ipaddr
= (__typeof__(ipaddr
))&addr
;
507 ipaddr
->sin_family
= AF_INET
;
508 ipaddr
->sin_len
= sizeof(*ipaddr
);
509 ipaddr
->sin_addr
= ip
->ip_dst
;
512 } else if (ifamily
== AF_INET6
) {
513 struct sockaddr_in6
*ip6addr
;
516 * m_pullup is prohibited in KAME IPv6 input processing
517 * but there's no other way!
519 if (m
->m_len
< sizeof(*ip6
)) {
520 m
= m_pullup(m
, sizeof(*ip6
));
522 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
528 * Expect 32-bit aligned data pointer on strict-align
531 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
533 ip6
= mtod(m
, struct ip6_hdr
*);
535 /* ECN consideration. */
536 /* XXX To be fixed later if needed */
537 // ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
539 if (!key_checktunnelsanity(sav
, AF_INET6
,
540 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
541 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
542 "in ESP input: %s %s\n",
543 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
544 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
548 if (ip6_doscopedroute
) {
549 bzero(&addr
, sizeof(addr
));
550 ip6addr
= (__typeof__(ip6addr
))&addr
;
551 ip6addr
->sin6_family
= AF_INET6
;
552 ip6addr
->sin6_len
= sizeof(*ip6addr
);
553 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
557 ipseclog((LOG_ERR
, "ipsec tunnel unsupported address family "
562 key_sa_recordxfer(sav
, m
);
563 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
564 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
565 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
569 if (ip_doscopedroute
|| ip6_doscopedroute
) {
570 // update the receiving interface address based on the inner address
571 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
573 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
578 /* Clear the csum flags, they can't be valid for the inner headers */
579 m
->m_pkthdr
.csum_flags
= 0;
581 // Input via IPSec interface
582 if (sav
->sah
->ipsec_if
!= NULL
) {
583 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
591 if (sav
->utun_in_fn
) {
592 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, ifamily
== AF_INET
? PF_INET
: PF_INET6
))) {
594 // we just wanna exit since packet has been completely processed
599 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
603 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 2,0,0,0,0);
606 * strip off ESP header and IV.
607 * even in m_pulldown case, we need to strip off ESP so that
608 * we can always compute checksum for AH correctly.
612 stripsiz
= esplen
+ ivlen
;
614 ip
= mtod(m
, struct ip
*);
615 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
616 m
->m_data
+= stripsiz
;
617 m
->m_len
-= stripsiz
;
618 m
->m_pkthdr
.len
-= stripsiz
;
620 ip
= mtod(m
, struct ip
*);
622 ip
->ip_len
= ip
->ip_len
- stripsiz
;
624 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
628 key_sa_recordxfer(sav
, m
);
629 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
630 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
635 * Set the csum valid flag, if we authenticated the
636 * packet, the payload shouldn't be corrupt unless
637 * it was corrupted before being signed on the other
640 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
641 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
642 m
->m_pkthdr
.csum_data
= 0xFFFF;
645 if (nxt
!= IPPROTO_DONE
) {
646 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
647 ipsec4_in_reject(m
, NULL
)) {
648 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
651 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 3,0,0,0,0);
653 /* translate encapsulated UDP port ? */
654 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
657 if (nxt
!= IPPROTO_UDP
) { /* not UPD packet - drop it */
658 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
662 if (m
->m_len
< off
+ sizeof(struct udphdr
)) {
663 m
= m_pullup(m
, off
+ sizeof(struct udphdr
));
666 "IPv4 ESP input: can't pullup UDP header in esp4_input\n"));
667 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
670 ip
= mtod(m
, struct ip
*);
672 udp
= (struct udphdr
*)(void *)(((u_int8_t
*)ip
) + off
);
674 lck_mtx_lock(sadb_mutex
);
675 if (sav
->natt_encapsulated_src_port
== 0) {
676 sav
->natt_encapsulated_src_port
= udp
->uh_sport
;
677 } else if (sav
->natt_encapsulated_src_port
!= udp
->uh_sport
) { /* something wrong */
678 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
679 lck_mtx_unlock(sadb_mutex
);
682 lck_mtx_unlock(sadb_mutex
);
683 udp
->uh_sport
= htons(sav
->remote_ike_port
);
687 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
688 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
689 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
691 // Input via IPSec interface
692 if (sav
->sah
->ipsec_if
!= NULL
) {
693 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
694 ip
->ip_off
= htons(ip
->ip_off
);
696 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
697 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
705 if (sav
->utun_in_fn
) {
706 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET
))) {
708 // we just wanna exit since packet has been completely processed
713 ip_proto_dispatch_in(m
, off
, nxt
, 0);
721 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
722 printf("DP esp4_input call free SA:0x%llx\n",
723 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
724 key_freesav(sav
, KEY_SADB_UNLOCKED
);
726 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
731 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
732 printf("DP esp4_input call free SA:0x%llx\n",
733 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
734 key_freesav(sav
, KEY_SADB_UNLOCKED
);
738 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 4,0,0,0,0);
745 esp6_input(struct mbuf
**mp
, int *offp
, int proto
)
747 #pragma unused(proto)
748 struct mbuf
*m
= *mp
;
752 struct esptail esptail
;
755 struct secasvar
*sav
= NULL
;
758 const struct esp_algorithm
*algo
;
762 /* sanity check for alignment. */
763 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
764 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
765 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
766 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
770 #ifndef PULLDOWN_TEST
771 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, {return IPPROTO_DONE
;});
772 esp
= (struct esp
*)(void *)(mtod(m
, caddr_t
) + off
);
774 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
776 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
780 /* Expect 32-bit data aligned pointer on strict-align platforms */
781 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
783 ip6
= mtod(m
, struct ip6_hdr
*);
785 if (ntohs(ip6
->ip6_plen
) == 0) {
786 ipseclog((LOG_ERR
, "IPv6 ESP input: "
787 "ESP with IPv6 jumbogram is not supported.\n"));
788 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
792 /* find the sassoc. */
795 if ((sav
= key_allocsa(AF_INET6
,
796 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
797 IPPROTO_ESP
, spi
)) == 0) {
798 ipseclog((LOG_WARNING
,
799 "IPv6 ESP input: no key association found for spi %u\n",
800 (u_int32_t
)ntohl(spi
)));
801 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
804 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
805 printf("DP esp6_input called to allocate SA:0x%llx\n",
806 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
807 if (sav
->state
!= SADB_SASTATE_MATURE
808 && sav
->state
!= SADB_SASTATE_DYING
) {
810 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
811 (u_int32_t
)ntohl(spi
)));
812 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
815 algo
= esp_algorithm_lookup(sav
->alg_enc
);
817 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
818 "unsupported encryption algorithm for spi %u\n",
819 (u_int32_t
)ntohl(spi
)));
820 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
824 /* check if we have proper ivlen information */
827 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
828 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
829 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
833 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
835 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
836 && (sav
->alg_auth
&& sav
->key_auth
)))
839 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
840 sav
->alg_auth
== SADB_AALG_NONE
)
844 * check for sequence number.
846 if (ipsec_chkreplay(seq
, sav
))
849 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
850 ipseclog((LOG_WARNING
,
851 "replay packet in IPv6 ESP input: %s %s\n",
852 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
858 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
859 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
860 const struct ah_algorithm
*sumalgo
;
863 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
866 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
867 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
868 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
871 if (AH_MAXSUMSIZE
< siz
) {
873 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
875 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
879 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
881 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
882 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
883 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
884 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
888 if (bcmp(sum0
, sum
, siz
) != 0) {
889 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
890 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
891 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
895 /* strip off the authentication data */
897 ip6
= mtod(m
, struct ip6_hdr
*);
898 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
900 m
->m_flags
|= M_AUTHIPDGM
;
901 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthsucc
);
905 * update sequence number.
907 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
908 if (ipsec_updatereplay(seq
, sav
)) {
909 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
916 /* process main esp header. */
917 if (sav
->flags
& SADB_X_EXT_OLD
) {
919 esplen
= sizeof(struct esp
);
922 if (sav
->flags
& SADB_X_EXT_DERIV
)
923 esplen
= sizeof(struct esp
);
925 esplen
= sizeof(struct newesp
);
928 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
929 ipseclog((LOG_WARNING
,
930 "IPv6 ESP input: packet too short\n"));
931 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
935 #ifndef PULLDOWN_TEST
936 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, return IPPROTO_DONE
); /*XXX*/
938 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
940 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
945 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
948 * pre-compute and cache intermediate key
950 if (esp_schedule(algo
, sav
) != 0) {
951 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
956 * decrypt the packet.
959 panic("internal error: no decrypt function");
960 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
961 /* m is already freed */
963 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
964 ipsec_logsastr(sav
)));
965 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
968 IPSEC_STAT_INCREMENT(ipsec6stat
.in_esphist
[sav
->alg_enc
]);
970 m
->m_flags
|= M_DECRYPTED
;
973 * find the trailer of the ESP.
975 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
977 nxt
= esptail
.esp_nxt
;
978 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
980 if (m
->m_pkthdr
.len
< taillen
981 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
982 ipseclog((LOG_WARNING
,
983 "bad pad length in IPv6 ESP input: %s %s\n",
984 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
985 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
989 /* strip off the trailing pad area. */
991 ip6
= mtod(m
, struct ip6_hdr
*);
992 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
994 if (sav
->utun_is_keepalive_fn
) {
995 if (sav
->utun_is_keepalive_fn(sav
->utun_pcb
, &m
, nxt
, sav
->flags
, (off
+ esplen
+ ivlen
))) {
997 // not really bad, we just wanna exit
998 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1005 /* was it transmitted over the IPsec tunnel SA? */
1006 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
1008 struct sockaddr_storage addr
;
1011 * strip off all the headers that precedes ESP header.
1012 * IP6 xx ESP IP6' payload -> IP6' payload
1014 * XXX more sanity checks
1015 * XXX relationship with gif?
1017 u_int32_t flowinfo
; /*net endian*/
1018 flowinfo
= ip6
->ip6_flow
;
1019 m_adj(m
, off
+ esplen
+ ivlen
);
1020 if (m
->m_len
< sizeof(*ip6
)) {
1021 #ifndef PULLDOWN_TEST
1023 * m_pullup is prohibited in KAME IPv6 input processing
1024 * but there's no other way!
1027 /* okay to pullup in m_pulldown style */
1029 m
= m_pullup(m
, sizeof(*ip6
));
1031 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1035 ip6
= mtod(m
, struct ip6_hdr
*);
1036 /* ECN consideration. */
1037 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
1038 if (!key_checktunnelsanity(sav
, AF_INET6
,
1039 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
1040 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1041 "in IPv6 ESP input: %s %s\n",
1042 ipsec6_logpacketstr(ip6
, spi
),
1043 ipsec_logsastr(sav
)));
1044 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1048 key_sa_recordxfer(sav
, m
);
1049 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
1050 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
1051 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1055 if (ip6_doscopedroute
) {
1056 struct sockaddr_in6
*ip6addr
;
1058 bzero(&addr
, sizeof(addr
));
1059 ip6addr
= (__typeof__(ip6addr
))&addr
;
1060 ip6addr
->sin6_family
= AF_INET6
;
1061 ip6addr
->sin6_len
= sizeof(*ip6addr
);
1062 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
1064 // update the receiving interface address based on the inner address
1065 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
1067 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
1072 // Input via IPSec interface
1073 if (sav
->sah
->ipsec_if
!= NULL
) {
1074 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1083 if (sav
->utun_in_fn
) {
1084 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET6
))) {
1086 // we just wanna exit since packet has been completely processed
1091 if (proto_input(PF_INET6
, m
) != 0)
1096 * strip off ESP header and IV.
1097 * even in m_pulldown case, we need to strip off ESP so that
1098 * we can always compute checksum for AH correctly.
1104 * Set the next header field of the previous header correctly.
1106 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
1109 stripsiz
= esplen
+ ivlen
;
1111 ip6
= mtod(m
, struct ip6_hdr
*);
1112 if (m
->m_len
>= stripsiz
+ off
) {
1113 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
1114 m
->m_data
+= stripsiz
;
1115 m
->m_len
-= stripsiz
;
1116 m
->m_pkthdr
.len
-= stripsiz
;
1119 * this comes with no copy if the boundary is on
1124 n
= m_split(m
, off
, M_DONTWAIT
);
1126 /* m is retained by m_split */
1130 /* m_cat does not update m_pkthdr.len */
1131 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
1135 #ifndef PULLDOWN_TEST
1137 * KAME requires that the packet to be contiguous on the
1138 * mbuf. We need to make that sure.
1139 * this kind of code should be avoided.
1140 * XXX other conditions to avoid running this part?
1142 if (m
->m_len
!= m
->m_pkthdr
.len
) {
1143 struct mbuf
*n
= NULL
;
1146 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1149 M_COPY_PKTHDR(n
, m
);
1150 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
1151 MCLGET(n
, M_DONTWAIT
);
1153 if ((n
->m_flags
& M_EXT
) == 0) {
1159 printf("esp6_input: mbuf allocation failed\n");
1163 if (m
->m_pkthdr
.len
<= maxlen
) {
1164 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
1165 n
->m_len
= m
->m_pkthdr
.len
;
1166 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1170 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
1172 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1175 m
->m_flags
&= ~M_PKTHDR
;
1181 ip6
= mtod(m
, struct ip6_hdr
*);
1182 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
1184 key_sa_recordxfer(sav
, m
);
1185 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
1186 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1190 // Input via IPSec interface
1191 if (sav
->sah
->ipsec_if
!= NULL
) {
1192 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1201 if (sav
->utun_in_fn
) {
1202 if (!(sav
->utun_in_fn(sav
->utun_pcb
, &m
, PF_INET6
))) {
1204 // we just wanna exit since packet has been completely processed
1214 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1215 printf("DP esp6_input call free SA:0x%llx\n",
1216 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1217 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1219 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1224 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1225 printf("DP esp6_input call free SA:0x%llx\n",
1226 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1227 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1231 return IPPROTO_DONE
;
1235 esp6_ctlinput(cmd
, sa
, d
)
1237 struct sockaddr
*sa
;
1240 const struct newesp
*espp
;
1242 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
1243 struct secasvar
*sav
;
1244 struct ip6_hdr
*ip6
;
1247 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1249 if (sa
->sa_family
!= AF_INET6
||
1250 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1252 if ((unsigned)cmd
>= PRC_NCMDS
)
1255 /* if the parameter is from icmp6, decode it. */
1257 ip6cp
= (struct ip6ctlparam
*)d
;
1259 ip6
= ip6cp
->ip6c_ip6
;
1260 off
= ip6cp
->ip6c_off
;
1268 * Notify the error to all possible sockets via pfctlinput2.
1269 * Since the upper layer information (such as protocol type,
1270 * source and destination ports) is embedded in the encrypted
1271 * data and might have been cut, we can't directly call
1272 * an upper layer ctlinput function. However, the pcbnotify
1273 * function will consider source and destination addresses
1274 * as well as the flow info value, and may be able to find
1275 * some PCB that should be notified.
1276 * Although pfctlinput2 will call esp6_ctlinput(), there is
1277 * no possibility of an infinite loop of function calls,
1278 * because we don't pass the inner IPv6 header.
1280 bzero(&ip6cp1
, sizeof(ip6cp1
));
1281 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
1282 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
1285 * Then go to special cases that need ESP header information.
1286 * XXX: We assume that when ip6 is non NULL,
1287 * M and OFF are valid.
1290 /* check if we can safely examine src and dst ports */
1291 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
1294 if (m
->m_len
< off
+ sizeof(esp
)) {
1296 * this should be rare case,
1297 * so we compromise on this copy...
1299 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
1302 espp
= (struct newesp
*)(void *)(mtod(m
, caddr_t
) + off
);
1304 if (cmd
== PRC_MSGSIZE
) {
1308 * Check to see if we have a valid SA corresponding to
1309 * the address in the ICMP message payload.
1311 sa6_src
= ip6cp
->ip6c_src
;
1312 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1313 sav
= key_allocsa(AF_INET6
,
1314 (caddr_t
)&sa6_src
->sin6_addr
,
1315 (caddr_t
)&sa6_dst
->sin6_addr
,
1316 IPPROTO_ESP
, espp
->esp_spi
);
1318 if (sav
->state
== SADB_SASTATE_MATURE
||
1319 sav
->state
== SADB_SASTATE_DYING
)
1321 key_freesav(sav
, KEY_SADB_LOCKED
);
1324 /* XXX Further validation? */
1327 * Depending on the value of "valid" and routing table
1328 * size (mtudisc_{hi,lo}wat), we will:
1329 * - recalcurate the new MTU and create the
1330 * corresponding routing entry, or
1331 * - ignore the MTU change notification.
1333 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1336 /* we normally notify any pcb here */