]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ah_input.c
8d13ca5e50a27fac399e4f2d62c67b41e57d8568
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/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/route.h>
80 #include <kern/cpu_number.h>
81 #include <kern/locks.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/ip_ecn.h>
89 #include <netinet/in_pcb.h>
91 #include <netinet6/ip6_ecn.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/ip6protosw.h>
102 #include <netinet6/ipsec.h>
104 #include <netinet6/ipsec6.h>
106 #include <netinet6/ah.h>
108 #include <netinet6/ah6.h>
110 #include <netkey/key.h>
111 #include <netkey/keydb.h>
113 #include <netkey/key_debug.h>
115 #define KEYDEBUG(lev,arg)
118 #include <net/kpi_protocol.h>
119 #include <netinet/kpi_ipfilter_var.h>
120 #include <mach/sdt.h>
122 #include <net/net_osdep.h>
124 #define IPLEN_FLIPPED
128 ah4_input(struct mbuf
*m
, int off
)
133 const struct ah_algorithm
*algo
;
137 struct secasvar
*sav
= NULL
;
143 #ifndef PULLDOWN_TEST
144 if (m
->m_len
< off
+ sizeof(struct newah
)) {
145 m
= m_pullup(m
, off
+ sizeof(struct newah
));
147 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup;"
148 "dropping the packet for simplicity\n"));
149 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
154 /* Expect 32-bit aligned data pointer on strict-align platforms */
155 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
157 ip
= mtod(m
, struct ip
*);
158 ah
= (struct ah
*)(void *)(((caddr_t
)ip
) + off
);
160 /* Expect 32-bit aligned data pointer on strict-align platforms */
161 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
163 ip
= mtod(m
, struct ip
*);
164 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
, sizeof(struct newah
));
166 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup;"
167 "dropping the packet for simplicity\n"));
168 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
174 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
176 hlen
= ip
->ip_hl
<< 2;
179 /* find the sassoc. */
182 if ((sav
= key_allocsa(AF_INET
,
183 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
184 IPPROTO_AH
, spi
)) == 0) {
185 ipseclog((LOG_WARNING
,
186 "IPv4 AH input: no key association found for spi %u\n",
187 (u_int32_t
)ntohl(spi
)));
188 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
191 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
192 printf("DP ah4_input called to allocate SA:0x%llx\n",
193 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
194 if (sav
->state
!= SADB_SASTATE_MATURE
195 && sav
->state
!= SADB_SASTATE_DYING
) {
197 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
198 (u_int32_t
)ntohl(spi
)));
199 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
203 algo
= ah_algorithm_lookup(sav
->alg_auth
);
205 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
206 "unsupported authentication algorithm for spi %u\n",
207 (u_int32_t
)ntohl(spi
)));
208 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
212 siz
= (*algo
->sumsiz
)(sav
);
213 siz1
= ((siz
+ 3) & ~(4 - 1));
216 * sanity checks for header, 1.
221 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
224 * Here, we do not do "siz1 == siz". This is because the way
225 * RFC240[34] section 2 is written. They do not require truncation
227 * For example, Microsoft IPsec stack attaches 160 bits of
228 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
229 * 32 bits of padding is attached.
231 * There are two downsides to this specification.
232 * They have no real harm, however, they leave us fuzzy feeling.
233 * - if we attach more than 96 bits of authentication data onto AH,
234 * we will never notice about possible modification by rogue
235 * intermediate nodes.
236 * Since extra bits in AH checksum is never used, this constitutes
237 * no real issue, however, it is wacky.
238 * - even if the peer attaches big authentication data, we will never
239 * notice the difference, since longer authentication data will just
242 * We may need some clarification in the spec.
245 ipseclog((LOG_NOTICE
, "sum length too short in IPv4 AH input "
246 "(%lu, should be at least %lu): %s\n",
247 (u_int32_t
)siz1
, (u_int32_t
)siz
,
248 ipsec4_logpacketstr(ip
, spi
)));
249 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
252 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
253 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv4 AH input "
254 "(%d should be %lu): %s\n",
255 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
256 ipsec4_logpacketstr(ip
, spi
)));
257 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
261 #ifndef PULLDOWN_TEST
262 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
) {
263 m
= m_pullup(m
, off
+ sizeof(struct ah
) + sizoff
+ siz1
);
265 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup\n"));
266 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
269 /* Expect 32-bit aligned data ptr on strict-align platforms */
270 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
272 ip
= mtod(m
, struct ip
*);
273 ah
= (struct ah
*)(void *)(((caddr_t
)ip
) + off
);
276 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
,
277 sizeof(struct ah
) + sizoff
+ siz1
);
279 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup\n"));
280 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
287 * check for sequence number.
289 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
290 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
))
293 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
294 ipseclog((LOG_WARNING
,
295 "replay packet in IPv4 AH input: %s %s\n",
296 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
302 * alright, it seems sane. now we are going to check the
303 * cryptographic checksum.
305 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
307 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
308 "couldn't alloc temporary region for cksum\n"));
309 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
314 * some of IP header fields are flipped to the host endian.
315 * convert them back to network endian. VERY stupid.
317 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
318 ip
->ip_off
= htons(ip
->ip_off
);
319 if (ah4_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
321 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
324 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahhist
[sav
->alg_auth
]);
328 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
329 ip
->ip_off
= ntohs(ip
->ip_off
);
332 caddr_t sumpos
= NULL
;
334 if (sav
->flags
& SADB_X_EXT_OLD
) {
336 sumpos
= (caddr_t
)(ah
+ 1);
339 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
342 if (bcmp(sumpos
, cksum
, siz
) != 0) {
343 ipseclog((LOG_WARNING
,
344 "checksum mismatch in IPv4 AH input: %s %s\n",
345 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
347 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
354 m
->m_flags
|= M_AUTHIPHDR
;
355 m
->m_flags
|= M_AUTHIPDGM
;
359 * looks okey, but we need more sanity check.
360 * XXX should elaborate.
362 if (ah
->ah_nxt
== IPPROTO_IPIP
|| ah
->ah_nxt
== IPPROTO_IP
) {
366 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
368 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
+ hlen
) {
369 m
= m_pullup(m
, off
+ sizeof(struct ah
)
370 + sizoff
+ siz1
+ hlen
);
373 "IPv4 AH input: can't pullup\n"));
374 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
379 nip
= (struct ip
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
380 if (nip
->ip_src
.s_addr
!= ip
->ip_src
.s_addr
381 || nip
->ip_dst
.s_addr
!= ip
->ip_dst
.s_addr
) {
382 m
->m_flags
&= ~M_AUTHIPHDR
;
383 m
->m_flags
&= ~M_AUTHIPDGM
;
387 else if (ah
->ah_nxt
== IPPROTO_IPV6
) {
388 m
->m_flags
&= ~M_AUTHIPHDR
;
389 m
->m_flags
&= ~M_AUTHIPDGM
;
394 if (m
->m_flags
& M_AUTHIPHDR
395 && m
->m_flags
& M_AUTHIPDGM
) {
398 "IPv4 AH input: authentication succeess\n"));
400 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthsucc
);
402 ipseclog((LOG_WARNING
,
403 "authentication failed in IPv4 AH input: %s %s\n",
404 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
405 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
410 * update sequence number.
412 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
413 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
)) {
414 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
419 /* was it transmitted over the IPsec tunnel SA? */
420 if (sav
->flags
& SADB_X_EXT_OLD
) {
422 stripsiz
= sizeof(struct ah
) + siz1
;
425 stripsiz
= sizeof(struct newah
) + siz1
;
427 if (ipsec4_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
, &ifamily
)) {
429 struct sockaddr_storage addr
;
432 * strip off all the headers that precedes AH.
433 * IP xx AH IP' payload -> IP' payload
435 * XXX more sanity checks
436 * XXX relationship with gif?
440 if (ifamily
== AF_INET6
) {
441 ipseclog((LOG_NOTICE
, "ipsec tunnel protocol mismatch "
442 "in IPv4 AH input: %s\n", ipsec_logsastr(sav
)));
446 m_adj(m
, off
+ stripsiz
);
447 if (m
->m_len
< sizeof(*ip
)) {
448 m
= m_pullup(m
, sizeof(*ip
));
450 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
454 ip
= mtod(m
, struct ip
*);
455 /* ECN consideration. */
456 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
457 if (!key_checktunnelsanity(sav
, AF_INET
,
458 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
459 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
460 "in IPv4 AH input: %s %s\n",
461 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
462 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
468 * Should the inner packet be considered authentic?
469 * My current answer is: NO.
471 * host1 -- gw1 === gw2 -- host2
472 * In this case, gw2 can trust the authenticity of the
473 * outer packet, but NOT inner. Packet may be altered
474 * between host1 and gw1.
476 * host1 -- gw1 === host2
477 * This case falls into the same scenario as above.
480 * This case is the only case when we may be able to leave
481 * M_AUTHIPHDR and M_AUTHIPDGM set.
482 * However, if host1 is wrongly configured, and allows
483 * attacker to inject some packet with src=host1 and
484 * dst=host2, you are in risk.
486 m
->m_flags
&= ~M_AUTHIPHDR
;
487 m
->m_flags
&= ~M_AUTHIPDGM
;
490 key_sa_recordxfer(sav
, m
);
491 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
492 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
493 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
497 if (ip_doscopedroute
) {
498 struct sockaddr_in
*ipaddr
;
500 bzero(&addr
, sizeof(addr
));
501 ipaddr
= (__typeof__(ipaddr
))&addr
;
502 ipaddr
->sin_family
= AF_INET
;
503 ipaddr
->sin_len
= sizeof(*ipaddr
);
504 ipaddr
->sin_addr
= ip
->ip_dst
;
506 // update the receiving interface address based on the inner address
507 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
509 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
513 if (proto_input(PF_INET
, m
) != 0)
521 ip
= mtod(m
, struct ip
*);
522 #ifndef PULLDOWN_TEST
524 * We do deep-copy since KAME requires that
525 * the packet is placed in a single external mbuf.
527 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
528 m
->m_data
+= stripsiz
;
529 m
->m_len
-= stripsiz
;
530 m
->m_pkthdr
.len
-= stripsiz
;
533 * even in m_pulldown case, we need to strip off AH so that
534 * we can compute checksum for multiple AH correctly.
536 if (m
->m_len
>= stripsiz
+ off
) {
537 ovbcopy((caddr_t
)ip
, ((caddr_t
)ip
) + stripsiz
, off
);
538 m
->m_data
+= stripsiz
;
539 m
->m_len
-= stripsiz
;
540 m
->m_pkthdr
.len
-= stripsiz
;
543 * this comes with no copy if the boundary is on
548 n
= m_split(m
, off
, M_DONTWAIT
);
550 /* m is retained by m_split */
554 /* m_cat does not update m_pkthdr.len */
555 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
560 if (m
->m_len
< sizeof(*ip
)) {
561 m
= m_pullup(m
, sizeof(*ip
));
563 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
567 ip
= mtod(m
, struct ip
*);
569 ip
->ip_len
= ip
->ip_len
- stripsiz
;
571 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
574 /* forget about IP hdr checksum, the check has already been passed */
576 key_sa_recordxfer(sav
, m
);
577 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
578 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
582 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
583 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
584 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
586 if (nxt
!= IPPROTO_DONE
) {
587 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
588 ipsec4_in_reject(m
, NULL
)) {
589 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
592 ip_proto_dispatch_in(m
, off
, nxt
, 0);
599 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
600 printf("DP ah4_input call free SA:0x%llx\n",
601 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
602 key_freesav(sav
, KEY_SADB_UNLOCKED
);
604 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
609 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
610 printf("DP ah4_input call free SA:0x%llx\n",
611 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
612 key_freesav(sav
, KEY_SADB_UNLOCKED
);
622 ah6_input(struct mbuf
**mp
, int *offp
, int proto
)
624 #pragma unused(proto)
625 struct mbuf
*m
= *mp
;
630 const struct ah_algorithm
*algo
;
634 struct secasvar
*sav
= NULL
;
639 #ifndef PULLDOWN_TEST
640 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
), {return IPPROTO_DONE
;});
641 ah
= (struct ah
*)(void *)(mtod(m
, caddr_t
) + off
);
643 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
, sizeof(struct newah
));
645 ipseclog((LOG_DEBUG
, "IPv6 AH input: can't pullup\n"));
646 ipsec6stat
.in_inval
++;
650 /* Expect 32-bit aligned data pointer on strict-align platforms */
651 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
653 ip6
= mtod(m
, struct ip6_hdr
*);
656 /* find the sassoc. */
659 if (ntohs(ip6
->ip6_plen
) == 0) {
660 ipseclog((LOG_ERR
, "IPv6 AH input: "
661 "AH with IPv6 jumbogram is not supported.\n"));
662 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
666 if ((sav
= key_allocsa(AF_INET6
,
667 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
668 IPPROTO_AH
, spi
)) == 0) {
669 ipseclog((LOG_WARNING
,
670 "IPv6 AH input: no key association found for spi %u\n",
671 (u_int32_t
)ntohl(spi
)));
672 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
675 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
676 printf("DP ah6_input called to allocate SA:0x%llx\n",
677 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
678 if (sav
->state
!= SADB_SASTATE_MATURE
679 && sav
->state
!= SADB_SASTATE_DYING
) {
681 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
682 (u_int32_t
)ntohl(spi
)));
683 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
687 algo
= ah_algorithm_lookup(sav
->alg_auth
);
689 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
690 "unsupported authentication algorithm for spi %u\n",
691 (u_int32_t
)ntohl(spi
)));
692 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
696 siz
= (*algo
->sumsiz
)(sav
);
697 siz1
= ((siz
+ 3) & ~(4 - 1));
700 * sanity checks for header, 1.
705 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
708 * Here, we do not do "siz1 == siz". See ah4_input() for complete
712 ipseclog((LOG_NOTICE
, "sum length too short in IPv6 AH input "
713 "(%lu, should be at least %lu): %s\n",
714 (u_int32_t
)siz1
, (u_int32_t
)siz
,
715 ipsec6_logpacketstr(ip6
, spi
)));
716 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
719 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
720 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv6 AH input "
721 "(%d should be %lu): %s\n",
722 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
723 ipsec6_logpacketstr(ip6
, spi
)));
724 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
727 #ifndef PULLDOWN_TEST
728 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
,
729 {return IPPROTO_DONE
;});
731 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
,
732 sizeof(struct ah
) + sizoff
+ siz1
);
734 ipseclog((LOG_NOTICE
, "couldn't pullup gather IPv6 AH checksum part"));
735 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
743 * check for sequence number.
745 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
746 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
))
749 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
750 ipseclog((LOG_WARNING
,
751 "replay packet in IPv6 AH input: %s %s\n",
752 ipsec6_logpacketstr(ip6
, spi
),
753 ipsec_logsastr(sav
)));
759 * alright, it seems sane. now we are going to check the
760 * cryptographic checksum.
762 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
764 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
765 "couldn't alloc temporary region for cksum\n"));
766 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
770 if (ah6_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
772 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
775 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahhist
[sav
->alg_auth
]);
778 caddr_t sumpos
= NULL
;
780 if (sav
->flags
& SADB_X_EXT_OLD
) {
782 sumpos
= (caddr_t
)(ah
+ 1);
785 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
788 if (bcmp(sumpos
, cksum
, siz
) != 0) {
789 ipseclog((LOG_WARNING
,
790 "checksum mismatch in IPv6 AH input: %s %s\n",
791 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
793 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
800 m
->m_flags
|= M_AUTHIPHDR
;
801 m
->m_flags
|= M_AUTHIPDGM
;
805 * looks okey, but we need more sanity check.
806 * XXX should elaborate.
808 if (ah
->ah_nxt
== IPPROTO_IPV6
) {
809 struct ip6_hdr
*nip6
;
812 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
814 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
815 + sizeof(struct ip6_hdr
),
816 {return IPPROTO_DONE
;});
818 nip6
= (struct ip6_hdr
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
819 if (!IN6_ARE_ADDR_EQUAL(&nip6
->ip6_src
, &ip6
->ip6_src
)
820 || !IN6_ARE_ADDR_EQUAL(&nip6
->ip6_dst
, &ip6
->ip6_dst
)) {
821 m
->m_flags
&= ~M_AUTHIPHDR
;
822 m
->m_flags
&= ~M_AUTHIPDGM
;
824 } else if (ah
->ah_nxt
== IPPROTO_IPIP
) {
825 m
->m_flags
&= ~M_AUTHIPHDR
;
826 m
->m_flags
&= ~M_AUTHIPDGM
;
827 } else if (ah
->ah_nxt
== IPPROTO_IP
) {
828 m
->m_flags
&= ~M_AUTHIPHDR
;
829 m
->m_flags
&= ~M_AUTHIPDGM
;
833 if (m
->m_flags
& M_AUTHIPHDR
834 && m
->m_flags
& M_AUTHIPDGM
) {
837 "IPv6 AH input: authentication succeess\n"));
839 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthsucc
);
841 ipseclog((LOG_WARNING
,
842 "authentication failed in IPv6 AH input: %s %s\n",
843 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
844 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
849 * update sequence number.
851 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
852 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
)) {
853 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
858 /* was it transmitted over the IPsec tunnel SA? */
859 if (sav
->flags
& SADB_X_EXT_OLD
) {
861 stripsiz
= sizeof(struct ah
) + siz1
;
864 stripsiz
= sizeof(struct newah
) + siz1
;
866 if (ipsec6_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
)) {
868 struct sockaddr_storage addr
;
871 * strip off all the headers that precedes AH.
872 * IP6 xx AH IP6' payload -> IP6' payload
874 * XXX more sanity checks
875 * XXX relationship with gif?
877 u_int32_t flowinfo
; /*net endian*/
879 flowinfo
= ip6
->ip6_flow
;
880 m_adj(m
, off
+ stripsiz
);
881 if (m
->m_len
< sizeof(*ip6
)) {
883 * m_pullup is prohibited in KAME IPv6 input processing
884 * but there's no other way!
886 m
= m_pullup(m
, sizeof(*ip6
));
888 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
892 ip6
= mtod(m
, struct ip6_hdr
*);
893 /* ECN consideration. */
894 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
895 if (!key_checktunnelsanity(sav
, AF_INET6
,
896 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
897 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
898 "in IPv6 AH input: %s %s\n",
899 ipsec6_logpacketstr(ip6
, spi
),
900 ipsec_logsastr(sav
)));
901 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
907 * should the inner packet be considered authentic?
908 * see comment in ah4_input().
910 m
->m_flags
&= ~M_AUTHIPHDR
;
911 m
->m_flags
&= ~M_AUTHIPDGM
;
914 key_sa_recordxfer(sav
, m
);
915 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
916 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
917 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
921 if (ip6_doscopedroute
) {
922 struct sockaddr_in6
*ip6addr
;
924 bzero(&addr
, sizeof(addr
));
925 ip6addr
= (__typeof__(ip6addr
))&addr
;
926 ip6addr
->sin6_family
= AF_INET6
;
927 ip6addr
->sin6_len
= sizeof(*ip6addr
);
928 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
930 // update the receiving interface address based on the inner address
931 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
933 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
938 if (proto_input(PF_INET6
, m
) != 0)
948 * Copy the value of the next header field of AH to the
949 * next header field of the previous header.
950 * This is necessary because AH will be stripped off below.
952 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
955 ip6
= mtod(m
, struct ip6_hdr
*);
956 #ifndef PULLDOWN_TEST
958 * We do deep-copy since KAME requires that
959 * the packet is placed in a single mbuf.
961 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
962 m
->m_data
+= stripsiz
;
963 m
->m_len
-= stripsiz
;
964 m
->m_pkthdr
.len
-= stripsiz
;
967 * even in m_pulldown case, we need to strip off AH so that
968 * we can compute checksum for multiple AH correctly.
970 if (m
->m_len
>= stripsiz
+ off
) {
971 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
972 m
->m_data
+= stripsiz
;
973 m
->m_len
-= stripsiz
;
974 m
->m_pkthdr
.len
-= stripsiz
;
977 * this comes with no copy if the boundary is on
982 n
= m_split(m
, off
, M_DONTWAIT
);
984 /* m is retained by m_split */
988 /* m_cat does not update m_pkthdr.len */
989 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
993 ip6
= mtod(m
, struct ip6_hdr
*);
995 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
997 key_sa_recordxfer(sav
, m
);
998 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
999 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
1008 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1009 printf("DP ah6_input call free SA:0x%llx\n",
1010 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1011 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1013 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1018 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1019 printf("DP ah6_input call free SA:0x%llx\n",
1020 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
1021 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1025 return IPPROTO_DONE
;
1029 ah6_ctlinput(cmd
, sa
, d
)
1031 struct sockaddr
*sa
;
1034 const struct newah
*ahp
;
1036 struct secasvar
*sav
;
1037 struct ip6_hdr
*ip6
;
1039 struct ip6ctlparam
*ip6cp
= NULL
;
1041 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1043 if (sa
->sa_family
!= AF_INET6
||
1044 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1046 if ((unsigned)cmd
>= PRC_NCMDS
)
1049 /* if the parameter is from icmp6, decode it. */
1051 ip6cp
= (struct ip6ctlparam
*)d
;
1053 ip6
= ip6cp
->ip6c_ip6
;
1054 off
= ip6cp
->ip6c_off
;
1062 * XXX: We assume that when ip6 is non NULL,
1063 * M and OFF are valid.
1066 /* check if we can safely examine src and dst ports */
1067 if (m
->m_pkthdr
.len
< off
+ sizeof(ah
))
1070 if (m
->m_len
< off
+ sizeof(ah
)) {
1072 * this should be rare case,
1073 * so we compromise on this copy...
1075 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1078 ahp
= (struct newah
*)(void *)(mtod(m
, caddr_t
) + off
);
1080 if (cmd
== PRC_MSGSIZE
) {
1084 * Check to see if we have a valid SA corresponding to
1085 * the address in the ICMP message payload.
1087 sa6_src
= ip6cp
->ip6c_src
;
1088 sa6_dst
= (struct sockaddr_in6
*)(void *)sa
;
1089 sav
= key_allocsa(AF_INET6
,
1090 (caddr_t
)&sa6_src
->sin6_addr
,
1091 (caddr_t
)&sa6_dst
->sin6_addr
,
1092 IPPROTO_AH
, ahp
->ah_spi
);
1094 if (sav
->state
== SADB_SASTATE_MATURE
||
1095 sav
->state
== SADB_SASTATE_DYING
)
1097 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1100 /* XXX Further validation? */
1103 * Depending on the value of "valid" and routing table
1104 * size (mtudisc_{hi,lo}wat), we will:
1105 * - recalcurate the new MTU and create the
1106 * corresponding routing entry, or
1107 * - ignore the MTU change notification.
1109 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1112 /* we normally notify single pcb here */
1114 /* we normally notify any pcb here */