]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ah_input.c
67a664ec62312ff5c89858cf89bda424b1174436
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/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $ */
30 /* $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 kjc 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 * RFC1826/2402 authentication header.
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/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/ip_ecn.h>
90 #include <netinet/in_pcb.h>
92 #include <netinet6/ip6_ecn.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/ip6protosw.h>
103 #include <netinet6/ipsec.h>
105 #include <netinet6/ipsec6.h>
107 #include <netinet6/ah.h>
109 #include <netinet6/ah6.h>
111 #include <netkey/key.h>
112 #include <netkey/keydb.h>
114 #include <netkey/key_debug.h>
116 #define KEYDEBUG(lev, arg)
119 #include <net/kpi_protocol.h>
120 #include <netinet/kpi_ipfilter_var.h>
121 #include <mach/sdt.h>
123 #include <net/net_osdep.h>
125 #define IPLEN_FLIPPED
127 extern lck_mtx_t
*sadb_mutex
;
131 ah4_input(struct mbuf
*m
, int off
)
136 const struct ah_algorithm
*algo
;
140 struct secasvar
*sav
= NULL
;
146 if (m
->m_len
< off
+ sizeof(struct newah
)) {
147 m
= m_pullup(m
, off
+ sizeof(struct newah
));
149 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup;"
150 "dropping the packet for simplicity\n"));
151 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
156 /* Expect 32-bit aligned data pointer on strict-align platforms */
157 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
159 ip
= mtod(m
, struct ip
*);
160 ah
= (struct ah
*)(void *)(((caddr_t
)ip
) + off
);
163 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
165 hlen
= ip
->ip_hl
<< 2;
168 /* find the sassoc. */
171 if ((sav
= key_allocsa(AF_INET
,
172 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
173 IPPROTO_AH
, spi
)) == 0) {
174 ipseclog((LOG_WARNING
,
175 "IPv4 AH input: no key association found for spi %u\n",
176 (u_int32_t
)ntohl(spi
)));
177 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
180 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
181 printf("DP ah4_input called to allocate SA:0x%llx\n",
182 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
183 if (sav
->state
!= SADB_SASTATE_MATURE
184 && sav
->state
!= SADB_SASTATE_DYING
) {
186 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
187 (u_int32_t
)ntohl(spi
)));
188 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
192 algo
= ah_algorithm_lookup(sav
->alg_auth
);
194 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
195 "unsupported authentication algorithm for spi %u\n",
196 (u_int32_t
)ntohl(spi
)));
197 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
201 siz
= (*algo
->sumsiz
)(sav
);
202 siz1
= ((siz
+ 3) & ~(4 - 1));
205 * sanity checks for header, 1.
210 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
213 * Here, we do not do "siz1 == siz". This is because the way
214 * RFC240[34] section 2 is written. They do not require truncation
216 * For example, Microsoft IPsec stack attaches 160 bits of
217 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
218 * 32 bits of padding is attached.
220 * There are two downsides to this specification.
221 * They have no real harm, however, they leave us fuzzy feeling.
222 * - if we attach more than 96 bits of authentication data onto AH,
223 * we will never notice about possible modification by rogue
224 * intermediate nodes.
225 * Since extra bits in AH checksum is never used, this constitutes
226 * no real issue, however, it is wacky.
227 * - even if the peer attaches big authentication data, we will never
228 * notice the difference, since longer authentication data will just
231 * We may need some clarification in the spec.
234 ipseclog((LOG_NOTICE
, "sum length too short in IPv4 AH input "
235 "(%u, should be at least %u): %s\n",
236 (u_int32_t
)siz1
, (u_int32_t
)siz
,
237 ipsec4_logpacketstr(ip
, spi
)));
238 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
241 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
242 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv4 AH input "
243 "(%d should be %u): %s\n",
244 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
245 ipsec4_logpacketstr(ip
, spi
)));
246 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
250 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
) {
251 m
= m_pullup(m
, off
+ sizeof(struct ah
) + sizoff
+ siz1
);
253 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup\n"));
254 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
257 /* Expect 32-bit aligned data ptr on strict-align platforms */
258 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
260 ip
= mtod(m
, struct ip
*);
261 ah
= (struct ah
*)(void *)(((caddr_t
)ip
) + off
);
266 * check for sequence number.
268 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] != NULL
) {
269 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
, 0)) {
272 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
273 ipseclog((LOG_WARNING
,
274 "replay packet in IPv4 AH input: %s %s\n",
275 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
281 * alright, it seems sane. now we are going to check the
282 * cryptographic checksum.
284 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
286 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
287 "couldn't alloc temporary region for cksum\n"));
288 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
293 * some of IP header fields are flipped to the host endian.
294 * convert them back to network endian. VERY stupid.
296 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
297 ip
->ip_off
= htons(ip
->ip_off
);
298 if (ah4_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
300 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
303 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahhist
[sav
->alg_auth
]);
307 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
308 ip
->ip_off
= ntohs(ip
->ip_off
);
311 caddr_t sumpos
= NULL
;
313 if (sav
->flags
& SADB_X_EXT_OLD
) {
315 sumpos
= (caddr_t
)(ah
+ 1);
318 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
321 if (bcmp(sumpos
, cksum
, siz
) != 0) {
322 ipseclog((LOG_WARNING
,
323 "checksum mismatch in IPv4 AH input: %s %s\n",
324 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
326 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
333 m
->m_flags
|= M_AUTHIPHDR
;
334 m
->m_flags
|= M_AUTHIPDGM
;
338 * looks okey, but we need more sanity check.
339 * XXX should elaborate.
341 if (ah
->ah_nxt
== IPPROTO_IPIP
|| ah
->ah_nxt
== IPPROTO_IP
) {
345 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
347 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
+ hlen
) {
348 m
= m_pullup(m
, off
+ sizeof(struct ah
)
349 + sizoff
+ siz1
+ hlen
);
352 "IPv4 AH input: can't pullup\n"));
353 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
358 nip
= (struct ip
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
359 if (nip
->ip_src
.s_addr
!= ip
->ip_src
.s_addr
360 || nip
->ip_dst
.s_addr
!= ip
->ip_dst
.s_addr
) {
361 m
->m_flags
&= ~M_AUTHIPHDR
;
362 m
->m_flags
&= ~M_AUTHIPDGM
;
366 else if (ah
->ah_nxt
== IPPROTO_IPV6
) {
367 m
->m_flags
&= ~M_AUTHIPHDR
;
368 m
->m_flags
&= ~M_AUTHIPDGM
;
373 if (m
->m_flags
& M_AUTHIPHDR
374 && m
->m_flags
& M_AUTHIPDGM
) {
377 "IPv4 AH input: authentication succeess\n"));
379 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthsucc
);
381 ipseclog((LOG_WARNING
,
382 "authentication failed in IPv4 AH input: %s %s\n",
383 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
384 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
389 * update sequence number.
391 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] != NULL
) {
392 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
, 0)) {
393 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
398 /* was it transmitted over the IPsec tunnel SA? */
399 if (sav
->flags
& SADB_X_EXT_OLD
) {
401 stripsiz
= sizeof(struct ah
) + siz1
;
404 stripsiz
= sizeof(struct newah
) + siz1
;
406 if (ipsec4_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
, &ifamily
)) {
408 struct sockaddr_storage addr
;
409 struct sockaddr_in
*ipaddr
;
412 * strip off all the headers that precedes AH.
413 * IP xx AH IP' payload -> IP' payload
415 * XXX more sanity checks
416 * XXX relationship with gif?
421 if (ifamily
== AF_INET6
) {
422 ipseclog((LOG_NOTICE
, "ipsec tunnel protocol mismatch "
423 "in IPv4 AH input: %s\n", ipsec_logsastr(sav
)));
427 m_adj(m
, off
+ stripsiz
);
428 if (m
->m_len
< sizeof(*ip
)) {
429 m
= m_pullup(m
, sizeof(*ip
));
431 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
435 ip
= mtod(m
, struct ip
*);
437 /* ECN consideration. */
438 if (ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
) == 0) {
439 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
443 if (otos
!= ip
->ip_tos
) {
444 sum
= ~ntohs(ip
->ip_sum
) & 0xffff;
445 sum
+= (~otos
& 0xffff) + ip
->ip_tos
;
446 sum
= (sum
>> 16) + (sum
& 0xffff);
447 sum
+= (sum
>> 16); /* add carry */
448 ip
->ip_sum
= htons(~sum
& 0xffff);
451 if (!key_checktunnelsanity(sav
, AF_INET
,
452 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
453 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
454 "in IPv4 AH input: %s %s\n",
455 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
456 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
462 * Should the inner packet be considered authentic?
463 * My current answer is: NO.
465 * host1 -- gw1 === gw2 -- host2
466 * In this case, gw2 can trust the authenticity of the
467 * outer packet, but NOT inner. Packet may be altered
468 * between host1 and gw1.
470 * host1 -- gw1 === host2
471 * This case falls into the same scenario as above.
474 * This case is the only case when we may be able to leave
475 * M_AUTHIPHDR and M_AUTHIPDGM set.
476 * However, if host1 is wrongly configured, and allows
477 * attacker to inject some packet with src=host1 and
478 * dst=host2, you are in risk.
480 m
->m_flags
&= ~M_AUTHIPHDR
;
481 m
->m_flags
&= ~M_AUTHIPDGM
;
484 key_sa_recordxfer(sav
, m
);
485 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
486 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
487 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
491 bzero(&addr
, sizeof(addr
));
492 ipaddr
= (__typeof__(ipaddr
)) & addr
;
493 ipaddr
->sin_family
= AF_INET
;
494 ipaddr
->sin_len
= sizeof(*ipaddr
);
495 ipaddr
->sin_addr
= ip
->ip_dst
;
497 // update the receiving interface address based on the inner address
498 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
500 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
504 // Input via IPsec interface
505 lck_mtx_lock(sadb_mutex
);
506 ifnet_t ipsec_if
= sav
->sah
->ipsec_if
;
507 if (ipsec_if
!= NULL
) {
508 // If an interface is found, add a reference count before dropping the lock
509 ifnet_reference(ipsec_if
);
511 lck_mtx_unlock(sadb_mutex
);
512 if (ipsec_if
!= NULL
) {
513 errno_t inject_error
= ipsec_inject_inbound_packet(ipsec_if
, m
);
514 ifnet_release(ipsec_if
);
515 if (inject_error
== 0) {
523 if (proto_input(PF_INET
, m
) != 0) {
532 ip
= mtod(m
, struct ip
*);
534 * We do deep-copy since KAME requires that
535 * the packet is placed in a single external mbuf.
537 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
538 m
->m_data
+= stripsiz
;
539 m
->m_len
-= stripsiz
;
540 m
->m_pkthdr
.len
-= stripsiz
;
542 if (m
->m_len
< sizeof(*ip
)) {
543 m
= m_pullup(m
, sizeof(*ip
));
545 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
549 ip
= mtod(m
, struct ip
*);
551 ip
->ip_len
= ip
->ip_len
- stripsiz
;
553 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
556 /* forget about IP hdr checksum, the check has already been passed */
558 key_sa_recordxfer(sav
, m
);
559 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
560 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
564 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
565 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
566 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
568 if (nxt
!= IPPROTO_DONE
) {
569 // Input via IPsec interface
570 lck_mtx_lock(sadb_mutex
);
571 ifnet_t ipsec_if
= sav
->sah
->ipsec_if
;
572 if (ipsec_if
!= NULL
) {
573 // If an interface is found, add a reference count before dropping the lock
574 ifnet_reference(ipsec_if
);
576 lck_mtx_unlock(sadb_mutex
);
577 if (ipsec_if
!= NULL
) {
578 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
579 ip
->ip_off
= htons(ip
->ip_off
);
581 ip
->ip_sum
= ip_cksum_hdr_in(m
, hlen
);
582 errno_t inject_error
= ipsec_inject_inbound_packet(ipsec_if
, m
);
583 ifnet_release(ipsec_if
);
584 if (inject_error
== 0) {
592 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
593 ipsec4_in_reject(m
, NULL
)) {
594 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
597 ip_proto_dispatch_in(m
, off
, nxt
, 0);
605 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
606 printf("DP ah4_input call free SA:0x%llx\n",
607 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
608 key_freesav(sav
, KEY_SADB_UNLOCKED
);
610 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
615 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
616 printf("DP ah4_input call free SA:0x%llx\n",
617 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
618 key_freesav(sav
, KEY_SADB_UNLOCKED
);
629 ah6_input(struct mbuf
**mp
, int *offp
, int proto
)
631 #pragma unused(proto)
632 struct mbuf
*m
= *mp
;
637 const struct ah_algorithm
*algo
;
641 struct secasvar
*sav
= NULL
;
646 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
), {return IPPROTO_DONE
;});
647 ah
= (struct ah
*)(void *)(mtod(m
, caddr_t
) + off
);
648 /* Expect 32-bit aligned data pointer on strict-align platforms */
649 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
651 ip6
= mtod(m
, struct ip6_hdr
*);
654 /* find the sassoc. */
657 if (ntohs(ip6
->ip6_plen
) == 0) {
658 ipseclog((LOG_ERR
, "IPv6 AH input: "
659 "AH with IPv6 jumbogram is not supported.\n"));
660 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
664 if ((sav
= key_allocsa(AF_INET6
,
665 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
666 IPPROTO_AH
, spi
)) == 0) {
667 ipseclog((LOG_WARNING
,
668 "IPv6 AH input: no key association found for spi %u\n",
669 (u_int32_t
)ntohl(spi
)));
670 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
673 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
674 printf("DP ah6_input called to allocate SA:0x%llx\n",
675 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
676 if (sav
->state
!= SADB_SASTATE_MATURE
677 && sav
->state
!= SADB_SASTATE_DYING
) {
679 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
680 (u_int32_t
)ntohl(spi
)));
681 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
685 algo
= ah_algorithm_lookup(sav
->alg_auth
);
687 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
688 "unsupported authentication algorithm for spi %u\n",
689 (u_int32_t
)ntohl(spi
)));
690 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
694 siz
= (*algo
->sumsiz
)(sav
);
695 siz1
= ((siz
+ 3) & ~(4 - 1));
698 * sanity checks for header, 1.
703 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
706 * Here, we do not do "siz1 == siz". See ah4_input() for complete
710 ipseclog((LOG_NOTICE
, "sum length too short in IPv6 AH input "
711 "(%u, should be at least %u): %s\n",
712 (u_int32_t
)siz1
, (u_int32_t
)siz
,
713 ipsec6_logpacketstr(ip6
, spi
)));
714 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
717 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
718 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv6 AH input "
719 "(%d should be %u): %s\n",
720 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
721 ipsec6_logpacketstr(ip6
, spi
)));
722 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
725 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
,
726 {return IPPROTO_DONE
;});
730 * check for sequence number.
732 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] != NULL
) {
733 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
, 0)) {
736 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
737 ipseclog((LOG_WARNING
,
738 "replay packet in IPv6 AH input: %s %s\n",
739 ipsec6_logpacketstr(ip6
, spi
),
740 ipsec_logsastr(sav
)));
746 * alright, it seems sane. now we are going to check the
747 * cryptographic checksum.
749 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
751 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
752 "couldn't alloc temporary region for cksum\n"));
753 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
757 if (ah6_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
759 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
762 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahhist
[sav
->alg_auth
]);
765 caddr_t sumpos
= NULL
;
767 if (sav
->flags
& SADB_X_EXT_OLD
) {
769 sumpos
= (caddr_t
)(ah
+ 1);
772 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
775 if (bcmp(sumpos
, cksum
, siz
) != 0) {
776 ipseclog((LOG_WARNING
,
777 "checksum mismatch in IPv6 AH input: %s %s\n",
778 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
780 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
787 m
->m_flags
|= M_AUTHIPHDR
;
788 m
->m_flags
|= M_AUTHIPDGM
;
792 * looks okey, but we need more sanity check.
793 * XXX should elaborate.
795 if (ah
->ah_nxt
== IPPROTO_IPV6
) {
796 struct ip6_hdr
*nip6
;
799 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
801 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
802 + sizeof(struct ip6_hdr
),
803 {return IPPROTO_DONE
;});
805 nip6
= (struct ip6_hdr
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
806 if (!IN6_ARE_ADDR_EQUAL(&nip6
->ip6_src
, &ip6
->ip6_src
)
807 || !IN6_ARE_ADDR_EQUAL(&nip6
->ip6_dst
, &ip6
->ip6_dst
)) {
808 m
->m_flags
&= ~M_AUTHIPHDR
;
809 m
->m_flags
&= ~M_AUTHIPDGM
;
811 } else if (ah
->ah_nxt
== IPPROTO_IPIP
) {
812 m
->m_flags
&= ~M_AUTHIPHDR
;
813 m
->m_flags
&= ~M_AUTHIPDGM
;
814 } else if (ah
->ah_nxt
== IPPROTO_IP
) {
815 m
->m_flags
&= ~M_AUTHIPHDR
;
816 m
->m_flags
&= ~M_AUTHIPDGM
;
820 if (m
->m_flags
& M_AUTHIPHDR
821 && m
->m_flags
& M_AUTHIPDGM
) {
824 "IPv6 AH input: authentication succeess\n"));
826 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthsucc
);
828 ipseclog((LOG_WARNING
,
829 "authentication failed in IPv6 AH input: %s %s\n",
830 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
831 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
836 * update sequence number.
838 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] != NULL
) {
839 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
, 0)) {
840 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
845 /* was it transmitted over the IPsec tunnel SA? */
846 if (sav
->flags
& SADB_X_EXT_OLD
) {
848 stripsiz
= sizeof(struct ah
) + siz1
;
851 stripsiz
= sizeof(struct newah
) + siz1
;
853 if (ipsec6_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
, &ifamily
)) {
855 struct sockaddr_storage addr
;
856 struct sockaddr_in6
*ip6addr
;
858 * strip off all the headers that precedes AH.
859 * IP6 xx AH IP6' payload -> IP6' payload
861 * XXX more sanity checks
862 * XXX relationship with gif?
864 u_int32_t flowinfo
; /*net endian*/
866 if (ifamily
== AF_INET
) {
867 ipseclog((LOG_NOTICE
, "ipsec tunnel protocol mismatch "
868 "in IPv6 AH input: %s\n", ipsec_logsastr(sav
)));
872 flowinfo
= ip6
->ip6_flow
;
873 m_adj(m
, off
+ stripsiz
);
874 if (m
->m_len
< sizeof(*ip6
)) {
876 * m_pullup is prohibited in KAME IPv6 input processing
877 * but there's no other way!
879 m
= m_pullup(m
, sizeof(*ip6
));
881 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
885 ip6
= mtod(m
, struct ip6_hdr
*);
886 /* ECN consideration. */
887 if (ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
) == 0) {
888 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
891 if (!key_checktunnelsanity(sav
, AF_INET6
,
892 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
893 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
894 "in IPv6 AH input: %s %s\n",
895 ipsec6_logpacketstr(ip6
, spi
),
896 ipsec_logsastr(sav
)));
897 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
903 * should the inner packet be considered authentic?
904 * see comment in ah4_input().
906 m
->m_flags
&= ~M_AUTHIPHDR
;
907 m
->m_flags
&= ~M_AUTHIPDGM
;
910 key_sa_recordxfer(sav
, m
);
911 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
912 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
913 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
917 bzero(&addr
, sizeof(addr
));
918 ip6addr
= (__typeof__(ip6addr
)) & addr
;
919 ip6addr
->sin6_family
= AF_INET6
;
920 ip6addr
->sin6_len
= sizeof(*ip6addr
);
921 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
923 // update the receiving interface address based on the inner address
924 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
926 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
930 // Input via IPsec interface
931 lck_mtx_lock(sadb_mutex
);
932 ifnet_t ipsec_if
= sav
->sah
->ipsec_if
;
933 if (ipsec_if
!= NULL
) {
934 // If an interface is found, add a reference count before dropping the lock
935 ifnet_reference(ipsec_if
);
937 lck_mtx_unlock(sadb_mutex
);
938 if (ipsec_if
!= NULL
) {
939 errno_t inject_error
= ipsec_inject_inbound_packet(ipsec_if
, m
);
940 ifnet_release(ipsec_if
);
941 if (inject_error
== 0) {
950 if (proto_input(PF_INET6
, m
) != 0) {
961 * Copy the value of the next header field of AH to the
962 * next header field of the previous header.
963 * This is necessary because AH will be stripped off below.
965 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
968 ip6
= mtod(m
, struct ip6_hdr
*);
970 * We do deep-copy since KAME requires that
971 * the packet is placed in a single mbuf.
973 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
974 m
->m_data
+= stripsiz
;
975 m
->m_len
-= stripsiz
;
976 m
->m_pkthdr
.len
-= stripsiz
;
977 ip6
= mtod(m
, struct ip6_hdr
*);
979 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
981 key_sa_recordxfer(sav
, m
);
982 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
983 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
987 // Input via IPsec interface
988 lck_mtx_lock(sadb_mutex
);
989 ifnet_t ipsec_if
= sav
->sah
->ipsec_if
;
990 if (ipsec_if
!= NULL
) {
991 // If an interface is found, add a reference count before dropping the lock
992 ifnet_reference(ipsec_if
);
994 lck_mtx_unlock(sadb_mutex
);
995 if (ipsec_if
!= NULL
) {
996 errno_t inject_error
= ipsec_inject_inbound_packet(ipsec_if
, m
);
997 ifnet_release(ipsec_if
);
998 if (inject_error
== 0) {
1012 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1013 printf("DP ah6_input call free SA:0x%llx\n",
1014 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1015 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1017 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1022 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1023 printf("DP ah6_input call free SA:0x%llx\n",
1024 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1025 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1030 return IPPROTO_DONE
;
1034 ah6_ctlinput(int cmd
, struct sockaddr
*sa
, void *d
)
1036 const struct newah
*ahp
;
1038 struct secasvar
*sav
;
1039 struct ip6_hdr
*ip6
;
1041 struct ip6ctlparam
*ip6cp
= NULL
;
1043 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1045 if (sa
->sa_family
!= AF_INET6
||
1046 sa
->sa_len
!= sizeof(struct sockaddr_in6
)) {
1049 if ((unsigned)cmd
>= PRC_NCMDS
) {
1053 /* if the parameter is from icmp6, decode it. */
1055 ip6cp
= (struct ip6ctlparam
*)d
;
1057 ip6
= ip6cp
->ip6c_ip6
;
1058 off
= ip6cp
->ip6c_off
;
1066 * XXX: We assume that when ip6 is non NULL,
1067 * M and OFF are valid.
1070 /* check if we can safely examine src and dst ports */
1071 if (m
->m_pkthdr
.len
< off
+ sizeof(ah
)) {
1075 if (m
->m_len
< off
+ sizeof(ah
)) {
1077 * this should be rare case,
1078 * so we compromise on this copy...
1080 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1083 ahp
= (struct newah
*)(void *)(mtod(m
, caddr_t
) + off
);
1086 if (cmd
== PRC_MSGSIZE
) {
1090 * Check to see if we have a valid SA corresponding to
1091 * the address in the ICMP message payload.
1093 sa6_src
= ip6cp
->ip6c_src
;
1094 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1095 sav
= key_allocsa(AF_INET6
,
1096 (caddr_t
)&sa6_src
->sin6_addr
,
1097 (caddr_t
)&sa6_dst
->sin6_addr
,
1098 IPPROTO_AH
, ahp
->ah_spi
);
1100 if (sav
->state
== SADB_SASTATE_MATURE
||
1101 sav
->state
== SADB_SASTATE_DYING
) {
1104 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1107 /* XXX Further validation? */
1110 * Depending on the value of "valid" and routing table
1111 * size (mtudisc_{hi,lo}wat), we will:
1112 * - recalcurate the new MTU and create the
1113 * corresponding routing entry, or
1114 * - ignore the MTU change notification.
1116 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1119 /* we normally notify single pcb here */
1121 /* we normally notify any pcb here */