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 unsigned char tag
[algo
->icvlen
];
432 if ((*algo
->finalizedecrypt
)(sav
, tag
, algo
->icvlen
)) {
433 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
434 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
435 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
438 if (cc_cmp_safe(algo
->icvlen
, saved_icv
, tag
)) {
439 ipseclog((LOG_ERR
, "packet decryption ICV mismatch\n"));
440 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
441 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
447 * find the trailer of the ESP.
449 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
451 nxt
= esptail
.esp_nxt
;
452 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
454 if (m
->m_pkthdr
.len
< taillen
455 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
456 ipseclog((LOG_WARNING
,
457 "bad pad length in IPv4 ESP input: %s %s\n",
458 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
459 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
463 /* strip off the trailing pad area. */
465 ip
= mtod(m
, struct ip
*);
467 ip
->ip_len
= ip
->ip_len
- taillen
;
469 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
471 if (ip
->ip_p
== IPPROTO_UDP
) {
472 // offset includes the outer ip and udp header lengths.
473 if (m
->m_len
< off
) {
474 m
= m_pullup(m
, off
);
477 "IPv4 ESP input: invalid udp encapsulated ESP packet length \n"));
478 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
483 // check the UDP encap header to detect changes in the source port, and then strip the header
484 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
485 // if peer is behind nat and this is the latest esp packet
486 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
487 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
488 seq
&& sav
->replay
&&
489 seq
>= sav
->replay
->lastseq
) {
490 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip
+ off
);
491 if (encap_uh
->uh_sport
&&
492 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
493 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
496 ip
= esp4_input_strip_udp_encap(m
, off
);
497 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip
) + off
);
500 /* was it transmitted over the IPsec tunnel SA? */
501 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
503 struct sockaddr_storage addr
;
506 * strip off all the headers that precedes ESP header.
507 * IP4 xx ESP IP4' payload -> IP4' payload
509 * XXX more sanity checks
510 * XXX relationship with gif?
516 m_adj(m
, off
+ esplen
+ ivlen
);
517 if (ifamily
== AF_INET
) {
518 struct sockaddr_in
*ipaddr
;
520 if (m
->m_len
< sizeof(*ip
)) {
521 m
= m_pullup(m
, sizeof(*ip
));
523 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
527 ip
= mtod(m
, struct ip
*);
528 /* ECN consideration. */
531 if (ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
) == 0) {
532 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
536 if (otos
!= ip
->ip_tos
) {
537 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
538 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
539 sum
= (sum
>> 16) + (sum
& 0xffff);
540 sum
+= (sum
>> 16); /* add carry */
541 ip
->ip_sum
= htons(~sum
& 0xffff);
544 if (!key_checktunnelsanity(sav
, AF_INET
,
545 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
546 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
547 "in ESP input: %s %s\n",
548 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
549 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
553 bzero(&addr
, sizeof(addr
));
554 ipaddr
= (__typeof__(ipaddr
))&addr
;
555 ipaddr
->sin_family
= AF_INET
;
556 ipaddr
->sin_len
= sizeof(*ipaddr
);
557 ipaddr
->sin_addr
= ip
->ip_dst
;
559 } else if (ifamily
== AF_INET6
) {
560 struct sockaddr_in6
*ip6addr
;
563 * m_pullup is prohibited in KAME IPv6 input processing
564 * but there's no other way!
566 if (m
->m_len
< sizeof(*ip6
)) {
567 m
= m_pullup(m
, sizeof(*ip6
));
569 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
575 * Expect 32-bit aligned data pointer on strict-align
578 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
580 ip6
= mtod(m
, struct ip6_hdr
*);
582 /* ECN consideration. */
583 if (ip64_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip6
->ip6_flow
) == 0) {
584 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
588 if (!key_checktunnelsanity(sav
, AF_INET6
,
589 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
590 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
591 "in ESP input: %s %s\n",
592 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
593 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
597 bzero(&addr
, sizeof(addr
));
598 ip6addr
= (__typeof__(ip6addr
))&addr
;
599 ip6addr
->sin6_family
= AF_INET6
;
600 ip6addr
->sin6_len
= sizeof(*ip6addr
);
601 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
604 ipseclog((LOG_ERR
, "ipsec tunnel unsupported address family "
609 key_sa_recordxfer(sav
, m
);
610 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
611 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
612 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
616 // update the receiving interface address based on the inner address
617 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
619 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
623 /* Clear the csum flags, they can't be valid for the inner headers */
624 m
->m_pkthdr
.csum_flags
= 0;
626 // Input via IPSec interface
627 if (sav
->sah
->ipsec_if
!= NULL
) {
628 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
636 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
640 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 2,0,0,0,0);
643 * strip off ESP header and IV.
644 * even in m_pulldown case, we need to strip off ESP so that
645 * we can always compute checksum for AH correctly.
649 stripsiz
= esplen
+ ivlen
;
651 ip
= mtod(m
, struct ip
*);
652 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
653 m
->m_data
+= stripsiz
;
654 m
->m_len
-= stripsiz
;
655 m
->m_pkthdr
.len
-= stripsiz
;
657 ip
= mtod(m
, struct ip
*);
659 ip
->ip_len
= ip
->ip_len
- stripsiz
;
661 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
665 key_sa_recordxfer(sav
, m
);
666 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
667 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
672 * Set the csum valid flag, if we authenticated the
673 * packet, the payload shouldn't be corrupt unless
674 * it was corrupted before being signed on the other
677 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
678 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
679 m
->m_pkthdr
.csum_data
= 0xFFFF;
682 if (nxt
!= IPPROTO_DONE
) {
683 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
684 ipsec4_in_reject(m
, NULL
)) {
685 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
688 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 3,0,0,0,0);
690 /* translate encapsulated UDP port ? */
691 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
694 if (nxt
!= IPPROTO_UDP
) { /* not UPD packet - drop it */
695 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
699 if (m
->m_len
< off
+ sizeof(struct udphdr
)) {
700 m
= m_pullup(m
, off
+ sizeof(struct udphdr
));
703 "IPv4 ESP input: can't pullup UDP header in esp4_input\n"));
704 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
707 ip
= mtod(m
, struct ip
*);
709 udp
= (struct udphdr
*)(void *)(((u_int8_t
*)ip
) + off
);
711 lck_mtx_lock(sadb_mutex
);
712 if (sav
->natt_encapsulated_src_port
== 0) {
713 sav
->natt_encapsulated_src_port
= udp
->uh_sport
;
714 } else if (sav
->natt_encapsulated_src_port
!= udp
->uh_sport
) { /* something wrong */
715 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
716 lck_mtx_unlock(sadb_mutex
);
719 lck_mtx_unlock(sadb_mutex
);
720 udp
->uh_sport
= htons(sav
->remote_ike_port
);
724 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
725 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
726 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
728 // Input via IPSec interface
729 if (sav
->sah
->ipsec_if
!= NULL
) {
730 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
731 ip
->ip_off
= htons(ip
->ip_off
);
733 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
734 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
742 ip_proto_dispatch_in(m
, off
, nxt
, 0);
750 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
751 printf("DP esp4_input call free SA:0x%llx\n",
752 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
753 key_freesav(sav
, KEY_SADB_UNLOCKED
);
755 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
760 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
761 printf("DP esp4_input call free SA:0x%llx\n",
762 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
763 key_freesav(sav
, KEY_SADB_UNLOCKED
);
767 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 4,0,0,0,0);
774 esp6_input(struct mbuf
**mp
, int *offp
, int proto
)
776 #pragma unused(proto)
777 struct mbuf
*m
= *mp
;
782 struct esptail esptail
;
785 struct secasvar
*sav
= NULL
;
789 const struct esp_algorithm
*algo
;
794 /* sanity check for alignment. */
795 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
796 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
797 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
798 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
802 #ifndef PULLDOWN_TEST
803 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, {return IPPROTO_DONE
;});
804 esp
= (struct esp
*)(void *)(mtod(m
, caddr_t
) + off
);
806 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
808 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
812 /* Expect 32-bit data aligned pointer on strict-align platforms */
813 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
815 ip6
= mtod(m
, struct ip6_hdr
*);
817 if (ntohs(ip6
->ip6_plen
) == 0) {
818 ipseclog((LOG_ERR
, "IPv6 ESP input: "
819 "ESP with IPv6 jumbogram is not supported.\n"));
820 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
824 nproto
= ip6_get_prevhdr(m
, off
);
825 if (nproto
== NULL
|| (*nproto
!= IPPROTO_ESP
&&
826 !(*nproto
== IPPROTO_UDP
&& off
>= sizeof(struct udphdr
)))) {
827 ipseclog((LOG_DEBUG
, "IPv6 ESP input: invalid protocol type\n"));
828 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
832 /* find the sassoc. */
835 if ((sav
= key_allocsa(AF_INET6
,
836 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
837 IPPROTO_ESP
, spi
)) == 0) {
838 ipseclog((LOG_WARNING
,
839 "IPv6 ESP input: no key association found for spi %u\n",
840 (u_int32_t
)ntohl(spi
)));
841 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
844 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
845 printf("DP esp6_input called to allocate SA:0x%llx\n",
846 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
847 if (sav
->state
!= SADB_SASTATE_MATURE
848 && sav
->state
!= SADB_SASTATE_DYING
) {
850 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
851 (u_int32_t
)ntohl(spi
)));
852 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
855 algo
= esp_algorithm_lookup(sav
->alg_enc
);
857 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
858 "unsupported encryption algorithm for spi %u\n",
859 (u_int32_t
)ntohl(spi
)));
860 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
864 /* check if we have proper ivlen information */
867 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
868 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
869 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
873 seq
= ntohl(((struct newesp
*)esp
)->esp_seq
);
875 /* Save ICV from packet for verification later */
877 unsigned char saved_icv
[AH_MAXSUMSIZE
];
878 if (algo
->finalizedecrypt
) {
880 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) saved_icv
);
884 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
885 && (sav
->alg_auth
&& sav
->key_auth
)))
888 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
889 sav
->alg_auth
== SADB_AALG_NONE
)
893 * check for sequence number.
895 if (ipsec_chkreplay(seq
, sav
))
898 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
899 ipseclog((LOG_WARNING
,
900 "replay packet in IPv6 ESP input: %s %s\n",
901 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
907 u_char sum0
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
908 u_char sum
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
909 const struct ah_algorithm
*sumalgo
;
911 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
914 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
915 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
916 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
919 if (AH_MAXSUMSIZE
< siz
) {
921 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
923 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
927 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, (caddr_t
) &sum0
[0]);
929 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, 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
);
936 if (cc_cmp_safe(siz
, sum0
, sum
)) {
937 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
938 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
939 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthfail
);
945 /* strip off the authentication data */
947 ip6
= mtod(m
, struct ip6_hdr
*);
948 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
950 m
->m_flags
|= M_AUTHIPDGM
;
951 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espauthsucc
);
955 * update sequence number.
957 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
958 if (ipsec_updatereplay(seq
, sav
)) {
959 IPSEC_STAT_INCREMENT(ipsec6stat
.in_espreplay
);
966 /* process main esp header. */
967 if (sav
->flags
& SADB_X_EXT_OLD
) {
969 esplen
= sizeof(struct esp
);
972 if (sav
->flags
& SADB_X_EXT_DERIV
)
973 esplen
= sizeof(struct esp
);
975 esplen
= sizeof(struct newesp
);
978 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
979 ipseclog((LOG_WARNING
,
980 "IPv6 ESP input: packet too short\n"));
981 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
985 #ifndef PULLDOWN_TEST
986 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, return IPPROTO_DONE
); /*XXX*/
988 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
990 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
995 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
998 * pre-compute and cache intermediate key
1000 if (esp_schedule(algo
, sav
) != 0) {
1001 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1006 * decrypt the packet.
1009 panic("internal error: no decrypt function");
1010 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
1011 /* m is already freed */
1013 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
1014 ipsec_logsastr(sav
)));
1015 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1018 IPSEC_STAT_INCREMENT(ipsec6stat
.in_esphist
[sav
->alg_enc
]);
1020 m
->m_flags
|= M_DECRYPTED
;
1022 if (algo
->finalizedecrypt
)
1024 unsigned char tag
[algo
->icvlen
];
1025 if ((*algo
->finalizedecrypt
)(sav
, tag
, algo
->icvlen
)) {
1026 ipseclog((LOG_ERR
, "packet decryption ICV failure\n"));
1027 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1028 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
1031 if (cc_cmp_safe(algo
->icvlen
, saved_icv
, tag
)) {
1032 ipseclog((LOG_ERR
, "packet decryption ICV mismatch\n"));
1033 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1034 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
1040 * find the trailer of the ESP.
1042 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
1044 nxt
= esptail
.esp_nxt
;
1045 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
1047 if (m
->m_pkthdr
.len
< taillen
1048 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
1049 ipseclog((LOG_WARNING
,
1050 "bad pad length in IPv6 ESP input: %s %s\n",
1051 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
1052 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1056 /* strip off the trailing pad area. */
1058 ip6
= mtod(m
, struct ip6_hdr
*);
1059 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
1061 if (*nproto
== IPPROTO_UDP
) {
1062 // offset includes the outer ip and udp header lengths.
1063 if (m
->m_len
< off
) {
1064 m
= m_pullup(m
, off
);
1066 ipseclog((LOG_DEBUG
,
1067 "IPv6 ESP input: invalid udp encapsulated ESP packet length\n"));
1068 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1073 // check the UDP encap header to detect changes in the source port, and then strip the header
1074 off
-= sizeof(struct udphdr
); // off no longer includes the udphdr's size
1075 // if peer is behind nat and this is the latest esp packet
1076 if ((sav
->flags
& SADB_X_EXT_NATT_DETECTED_PEER
) != 0 &&
1077 (sav
->flags
& SADB_X_EXT_OLD
) == 0 &&
1078 seq
&& sav
->replay
&&
1079 seq
>= sav
->replay
->lastseq
) {
1080 struct udphdr
*encap_uh
= (__typeof__(encap_uh
))(void *)((caddr_t
)ip6
+ off
);
1081 if (encap_uh
->uh_sport
&&
1082 ntohs(encap_uh
->uh_sport
) != sav
->remote_ike_port
) {
1083 sav
->remote_ike_port
= ntohs(encap_uh
->uh_sport
);
1086 ip6
= esp6_input_strip_udp_encap(m
, off
);
1087 esp
= (struct esp
*)(void *)(((u_int8_t
*)ip6
) + off
);
1091 /* was it transmitted over the IPsec tunnel SA? */
1092 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
, &ifamily
)) {
1094 struct sockaddr_storage addr
;
1097 * strip off all the headers that precedes ESP header.
1098 * IP6 xx ESP IP6' payload -> IP6' payload
1100 * XXX more sanity checks
1101 * XXX relationship with gif?
1103 u_int32_t flowinfo
; /*net endian*/
1104 flowinfo
= ip6
->ip6_flow
;
1105 m_adj(m
, off
+ esplen
+ ivlen
);
1106 if (ifamily
== AF_INET6
) {
1107 struct sockaddr_in6
*ip6addr
;
1109 if (m
->m_len
< sizeof(*ip6
)) {
1110 #ifndef PULLDOWN_TEST
1112 * m_pullup is prohibited in KAME IPv6 input processing
1113 * but there's no other way!
1116 /* okay to pullup in m_pulldown style */
1118 m
= m_pullup(m
, sizeof(*ip6
));
1120 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1124 ip6
= mtod(m
, struct ip6_hdr
*);
1125 /* ECN consideration. */
1126 if (ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
) == 0) {
1127 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1130 if (!key_checktunnelsanity(sav
, AF_INET6
,
1131 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
1132 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1133 "in IPv6 ESP input: %s %s\n",
1134 ipsec6_logpacketstr(ip6
, spi
),
1135 ipsec_logsastr(sav
)));
1136 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
1140 bzero(&addr
, sizeof(addr
));
1141 ip6addr
= (__typeof__(ip6addr
))&addr
;
1142 ip6addr
->sin6_family
= AF_INET6
;
1143 ip6addr
->sin6_len
= sizeof(*ip6addr
);
1144 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
1145 } else if (ifamily
== AF_INET
) {
1146 struct sockaddr_in
*ipaddr
;
1148 if (m
->m_len
< sizeof(*ip
)) {
1149 m
= m_pullup(m
, sizeof(*ip
));
1151 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1159 ip
= mtod(m
, struct ip
*);
1161 /* ECN consideration. */
1162 if (ip46_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip
->ip_tos
) == 0) {
1163 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1167 if (otos
!= ip
->ip_tos
) {
1168 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
1169 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
1170 sum
= (sum
>> 16) + (sum
& 0xffff);
1171 sum
+= (sum
>> 16); /* add carry */
1172 ip
->ip_sum
= htons(~sum
& 0xffff);
1175 if (!key_checktunnelsanity(sav
, AF_INET
,
1176 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
1177 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
1178 "in ESP input: %s %s\n",
1179 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
1180 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
1184 bzero(&addr
, sizeof(addr
));
1185 ipaddr
= (__typeof__(ipaddr
))&addr
;
1186 ipaddr
->sin_family
= AF_INET
;
1187 ipaddr
->sin_len
= sizeof(*ipaddr
);
1188 ipaddr
->sin_addr
= ip
->ip_dst
;
1191 key_sa_recordxfer(sav
, m
);
1192 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
1193 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
1194 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1198 // update the receiving interface address based on the inner address
1199 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
1201 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
1205 // Input via IPSec interface
1206 if (sav
->sah
->ipsec_if
!= NULL
) {
1207 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1216 if (proto_input(ifamily
== AF_INET
? PF_INET
: PF_INET6
, m
) != 0)
1221 * strip off ESP header and IV.
1222 * even in m_pulldown case, we need to strip off ESP so that
1223 * we can always compute checksum for AH correctly.
1229 * Set the next header field of the previous header correctly.
1231 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
1234 stripsiz
= esplen
+ ivlen
;
1236 ip6
= mtod(m
, struct ip6_hdr
*);
1237 if (m
->m_len
>= stripsiz
+ off
) {
1238 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
1239 m
->m_data
+= stripsiz
;
1240 m
->m_len
-= stripsiz
;
1241 m
->m_pkthdr
.len
-= stripsiz
;
1244 * this comes with no copy if the boundary is on
1249 n
= m_split(m
, off
, M_DONTWAIT
);
1251 /* m is retained by m_split */
1255 /* m_cat does not update m_pkthdr.len */
1256 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
1260 #ifndef PULLDOWN_TEST
1262 * KAME requires that the packet to be contiguous on the
1263 * mbuf. We need to make that sure.
1264 * this kind of code should be avoided.
1265 * XXX other conditions to avoid running this part?
1267 if (m
->m_len
!= m
->m_pkthdr
.len
) {
1268 struct mbuf
*n
= NULL
;
1271 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1274 M_COPY_PKTHDR(n
, m
);
1275 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
1276 MCLGET(n
, M_DONTWAIT
);
1278 if ((n
->m_flags
& M_EXT
) == 0) {
1284 printf("esp6_input: mbuf allocation failed\n");
1288 if (m
->m_pkthdr
.len
<= maxlen
) {
1289 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
1290 n
->m_len
= m
->m_pkthdr
.len
;
1291 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1295 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
1297 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1300 m
->m_flags
&= ~M_PKTHDR
;
1306 ip6
= mtod(m
, struct ip6_hdr
*);
1307 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
1309 key_sa_recordxfer(sav
, m
);
1310 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
1311 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1316 * Set the csum valid flag, if we authenticated the
1317 * packet, the payload shouldn't be corrupt unless
1318 * it was corrupted before being signed on the other
1321 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
1322 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1323 m
->m_pkthdr
.csum_data
= 0xFFFF;
1326 // Input via IPSec interface
1327 if (sav
->sah
->ipsec_if
!= NULL
) {
1328 if (ipsec_inject_inbound_packet(sav
->sah
->ipsec_if
, m
) == 0) {
1343 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1344 printf("DP esp6_input call free SA:0x%llx\n",
1345 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1346 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1348 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1353 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1354 printf("DP esp6_input call free SA:0x%llx\n",
1355 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1356 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1360 return IPPROTO_DONE
;
1364 esp6_ctlinput(int cmd
, struct sockaddr
*sa
, void *d
)
1366 const struct newesp
*espp
;
1368 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
1369 struct secasvar
*sav
;
1370 struct ip6_hdr
*ip6
;
1373 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1375 if (sa
->sa_family
!= AF_INET6
||
1376 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1378 if ((unsigned)cmd
>= PRC_NCMDS
)
1381 /* if the parameter is from icmp6, decode it. */
1383 ip6cp
= (struct ip6ctlparam
*)d
;
1385 ip6
= ip6cp
->ip6c_ip6
;
1386 off
= ip6cp
->ip6c_off
;
1394 * Notify the error to all possible sockets via pfctlinput2.
1395 * Since the upper layer information (such as protocol type,
1396 * source and destination ports) is embedded in the encrypted
1397 * data and might have been cut, we can't directly call
1398 * an upper layer ctlinput function. However, the pcbnotify
1399 * function will consider source and destination addresses
1400 * as well as the flow info value, and may be able to find
1401 * some PCB that should be notified.
1402 * Although pfctlinput2 will call esp6_ctlinput(), there is
1403 * no possibility of an infinite loop of function calls,
1404 * because we don't pass the inner IPv6 header.
1406 bzero(&ip6cp1
, sizeof(ip6cp1
));
1407 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
1408 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
1411 * Then go to special cases that need ESP header information.
1412 * XXX: We assume that when ip6 is non NULL,
1413 * M and OFF are valid.
1416 /* check if we can safely examine src and dst ports */
1417 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
1420 if (m
->m_len
< off
+ sizeof(esp
)) {
1422 * this should be rare case,
1423 * so we compromise on this copy...
1425 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
1428 espp
= (struct newesp
*)(void *)(mtod(m
, caddr_t
) + off
);
1430 if (cmd
== PRC_MSGSIZE
) {
1434 * Check to see if we have a valid SA corresponding to
1435 * the address in the ICMP message payload.
1437 sa6_src
= ip6cp
->ip6c_src
;
1438 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1439 sav
= key_allocsa(AF_INET6
,
1440 (caddr_t
)&sa6_src
->sin6_addr
,
1441 (caddr_t
)&sa6_dst
->sin6_addr
,
1442 IPPROTO_ESP
, espp
->esp_spi
);
1444 if (sav
->state
== SADB_SASTATE_MATURE
||
1445 sav
->state
== SADB_SASTATE_DYING
)
1447 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1450 /* XXX Further validation? */
1453 * Depending on the value of "valid" and routing table
1454 * size (mtudisc_{hi,lo}wat), we will:
1455 * - recalcurate the new MTU and create the
1456 * corresponding routing entry, or
1457 * - ignore the MTU change notification.
1459 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1462 /* we normally notify any pcb here */