]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ah_input.c
a448295b7bb8afa0cf185014589d1b3aa9aa1dbf
2 * Copyright (c) 2008 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/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/errno.h>
74 #include <sys/kernel.h>
75 #include <sys/syslog.h>
78 #include <net/route.h>
79 #include <kern/cpu_number.h>
80 #include <kern/locks.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_ecn.h>
88 #include <netinet/in_pcb.h>
90 #include <netinet6/ip6_ecn.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/ip6protosw.h>
101 #include <netinet6/ipsec.h>
103 #include <netinet6/ipsec6.h>
105 #include <netinet6/ah.h>
107 #include <netinet6/ah6.h>
109 #include <netkey/key.h>
110 #include <netkey/keydb.h>
112 #include <netkey/key_debug.h>
114 #define KEYDEBUG(lev,arg)
117 #include <net/kpi_protocol.h>
118 #include <netinet/kpi_ipfilter_var.h>
119 #include <mach/sdt.h>
121 #include <net/net_osdep.h>
123 #define IPLEN_FLIPPED
126 extern struct protosw inetsw
[];
129 ah4_input(struct mbuf
*m
, int off
)
134 const struct ah_algorithm
*algo
;
138 struct secasvar
*sav
= NULL
;
144 #ifndef PULLDOWN_TEST
145 if (m
->m_len
< off
+ sizeof(struct newah
)) {
146 m
= m_pullup(m
, off
+ sizeof(struct newah
));
148 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup;"
149 "dropping the packet for simplicity\n"));
150 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
155 ip
= mtod(m
, struct ip
*);
156 ah
= (struct ah
*)(((caddr_t
)ip
) + off
);
158 ip
= mtod(m
, struct ip
*);
159 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
, sizeof(struct newah
));
161 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup;"
162 "dropping the packet for simplicity\n"));
163 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
169 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
171 hlen
= ip
->ip_hl
<< 2;
174 /* find the sassoc. */
177 if ((sav
= key_allocsa(AF_INET
,
178 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
179 IPPROTO_AH
, spi
)) == 0) {
180 ipseclog((LOG_WARNING
,
181 "IPv4 AH input: no key association found for spi %u\n",
182 (u_int32_t
)ntohl(spi
)));
183 IPSEC_STAT_INCREMENT(ipsecstat
.in_nosa
);
186 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
187 printf("DP ah4_input called to allocate SA:%p\n", sav
));
188 if (sav
->state
!= SADB_SASTATE_MATURE
189 && sav
->state
!= SADB_SASTATE_DYING
) {
191 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
192 (u_int32_t
)ntohl(spi
)));
193 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
197 algo
= ah_algorithm_lookup(sav
->alg_auth
);
199 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
200 "unsupported authentication algorithm for spi %u\n",
201 (u_int32_t
)ntohl(spi
)));
202 IPSEC_STAT_INCREMENT(ipsecstat
.in_badspi
);
206 siz
= (*algo
->sumsiz
)(sav
);
207 siz1
= ((siz
+ 3) & ~(4 - 1));
210 * sanity checks for header, 1.
215 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
218 * Here, we do not do "siz1 == siz". This is because the way
219 * RFC240[34] section 2 is written. They do not require truncation
221 * For example, Microsoft IPsec stack attaches 160 bits of
222 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
223 * 32 bits of padding is attached.
225 * There are two downsides to this specification.
226 * They have no real harm, however, they leave us fuzzy feeling.
227 * - if we attach more than 96 bits of authentication data onto AH,
228 * we will never notice about possible modification by rogue
229 * intermediate nodes.
230 * Since extra bits in AH checksum is never used, this constitutes
231 * no real issue, however, it is wacky.
232 * - even if the peer attaches big authentication data, we will never
233 * notice the difference, since longer authentication data will just
236 * We may need some clarification in the spec.
239 ipseclog((LOG_NOTICE
, "sum length too short in IPv4 AH input "
240 "(%lu, should be at least %lu): %s\n",
241 (u_int32_t
)siz1
, (u_int32_t
)siz
,
242 ipsec4_logpacketstr(ip
, spi
)));
243 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
246 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
247 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv4 AH input "
248 "(%d should be %lu): %s\n",
249 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
250 ipsec4_logpacketstr(ip
, spi
)));
251 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
255 #ifndef PULLDOWN_TEST
256 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
) {
257 m
= m_pullup(m
, off
+ sizeof(struct ah
) + sizoff
+ siz1
);
259 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup\n"));
260 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
264 ip
= mtod(m
, struct ip
*);
265 ah
= (struct ah
*)(((caddr_t
)ip
) + off
);
268 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
,
269 sizeof(struct ah
) + sizoff
+ siz1
);
271 ipseclog((LOG_DEBUG
, "IPv4 AH input: can't pullup\n"));
272 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
279 * check for sequence number.
281 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
282 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
))
285 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
286 ipseclog((LOG_WARNING
,
287 "replay packet in IPv4 AH input: %s %s\n",
288 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
294 * alright, it seems sane. now we are going to check the
295 * cryptographic checksum.
297 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
299 ipseclog((LOG_DEBUG
, "IPv4 AH input: "
300 "couldn't alloc temporary region for cksum\n"));
301 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
306 * some of IP header fields are flipped to the host endian.
307 * convert them back to network endian. VERY stupid.
309 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
310 ip
->ip_off
= htons(ip
->ip_off
);
311 if (ah4_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
313 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
316 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahhist
[sav
->alg_auth
]);
320 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
321 ip
->ip_off
= ntohs(ip
->ip_off
);
324 caddr_t sumpos
= NULL
;
326 if (sav
->flags
& SADB_X_EXT_OLD
) {
328 sumpos
= (caddr_t
)(ah
+ 1);
331 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
334 if (bcmp(sumpos
, cksum
, siz
) != 0) {
335 ipseclog((LOG_WARNING
,
336 "checksum mismatch in IPv4 AH input: %s %s\n",
337 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
339 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
346 m
->m_flags
|= M_AUTHIPHDR
;
347 m
->m_flags
|= M_AUTHIPDGM
;
351 * looks okey, but we need more sanity check.
352 * XXX should elaborate.
354 if (ah
->ah_nxt
== IPPROTO_IPIP
|| ah
->ah_nxt
== IPPROTO_IP
) {
358 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
360 if (m
->m_len
< off
+ sizeof(struct ah
) + sizoff
+ siz1
+ hlen
) {
361 m
= m_pullup(m
, off
+ sizeof(struct ah
)
362 + sizoff
+ siz1
+ hlen
);
365 "IPv4 AH input: can't pullup\n"));
366 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
371 nip
= (struct ip
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
372 if (nip
->ip_src
.s_addr
!= ip
->ip_src
.s_addr
373 || nip
->ip_dst
.s_addr
!= ip
->ip_dst
.s_addr
) {
374 m
->m_flags
&= ~M_AUTHIPHDR
;
375 m
->m_flags
&= ~M_AUTHIPDGM
;
379 else if (ah
->ah_nxt
== IPPROTO_IPV6
) {
380 m
->m_flags
&= ~M_AUTHIPHDR
;
381 m
->m_flags
&= ~M_AUTHIPDGM
;
386 if (m
->m_flags
& M_AUTHIPHDR
387 && m
->m_flags
& M_AUTHIPDGM
) {
390 "IPv4 AH input: authentication succeess\n"));
392 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthsucc
);
394 ipseclog((LOG_WARNING
,
395 "authentication failed in IPv4 AH input: %s %s\n",
396 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
397 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahauthfail
);
402 * update sequence number.
404 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
405 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
)) {
406 IPSEC_STAT_INCREMENT(ipsecstat
.in_ahreplay
);
411 /* was it transmitted over the IPsec tunnel SA? */
412 if (sav
->flags
& SADB_X_EXT_OLD
) {
414 stripsiz
= sizeof(struct ah
) + siz1
;
417 stripsiz
= sizeof(struct newah
) + siz1
;
419 if (ipsec4_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
, &ifamily
)) {
421 struct sockaddr_storage addr
;
424 * strip off all the headers that precedes AH.
425 * IP xx AH IP' payload -> IP' payload
427 * XXX more sanity checks
428 * XXX relationship with gif?
432 if (ifamily
== AF_INET6
) {
433 ipseclog((LOG_NOTICE
, "ipsec tunnel protocol mismatch "
434 "in IPv4 AH input: %s\n", ipsec_logsastr(sav
)));
438 m_adj(m
, off
+ stripsiz
);
439 if (m
->m_len
< sizeof(*ip
)) {
440 m
= m_pullup(m
, sizeof(*ip
));
442 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
446 ip
= mtod(m
, struct ip
*);
447 /* ECN consideration. */
448 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
449 if (!key_checktunnelsanity(sav
, AF_INET
,
450 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
451 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
452 "in IPv4 AH input: %s %s\n",
453 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
454 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
460 * Should the inner packet be considered authentic?
461 * My current answer is: NO.
463 * host1 -- gw1 === gw2 -- host2
464 * In this case, gw2 can trust the authenticity of the
465 * outer packet, but NOT inner. Packet may be altered
466 * between host1 and gw1.
468 * host1 -- gw1 === host2
469 * This case falls into the same scenario as above.
472 * This case is the only case when we may be able to leave
473 * M_AUTHIPHDR and M_AUTHIPDGM set.
474 * However, if host1 is wrongly configured, and allows
475 * attacker to inject some packet with src=host1 and
476 * dst=host2, you are in risk.
478 m
->m_flags
&= ~M_AUTHIPHDR
;
479 m
->m_flags
&= ~M_AUTHIPDGM
;
482 key_sa_recordxfer(sav
, m
);
483 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
484 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
485 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
489 if (ip_doscopedroute
) {
490 struct sockaddr_in
*ipaddr
;
492 bzero(&addr
, sizeof(addr
));
493 ipaddr
= (__typeof__(ipaddr
))&addr
;
494 ipaddr
->sin_family
= AF_INET
;
495 ipaddr
->sin_len
= sizeof(*ipaddr
);
496 ipaddr
->sin_addr
= ip
->ip_dst
;
498 // update the receiving interface address based on the inner address
499 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
501 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
505 if (proto_input(PF_INET
, m
) != 0)
513 ip
= mtod(m
, struct ip
*);
514 #ifndef PULLDOWN_TEST
516 * We do deep-copy since KAME requires that
517 * the packet is placed in a single external mbuf.
519 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
520 m
->m_data
+= stripsiz
;
521 m
->m_len
-= stripsiz
;
522 m
->m_pkthdr
.len
-= stripsiz
;
525 * even in m_pulldown case, we need to strip off AH so that
526 * we can compute checksum for multiple AH correctly.
528 if (m
->m_len
>= stripsiz
+ off
) {
529 ovbcopy((caddr_t
)ip
, ((caddr_t
)ip
) + stripsiz
, off
);
530 m
->m_data
+= stripsiz
;
531 m
->m_len
-= stripsiz
;
532 m
->m_pkthdr
.len
-= stripsiz
;
535 * this comes with no copy if the boundary is on
540 n
= m_split(m
, off
, M_DONTWAIT
);
542 /* m is retained by m_split */
546 /* m_cat does not update m_pkthdr.len */
547 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
552 if (m
->m_len
< sizeof(*ip
)) {
553 m
= m_pullup(m
, sizeof(*ip
));
555 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
559 ip
= mtod(m
, struct ip
*);
561 ip
->ip_len
= ip
->ip_len
- stripsiz
;
563 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
566 /* forget about IP hdr checksum, the check has already been passed */
568 key_sa_recordxfer(sav
, m
);
569 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
570 IPSEC_STAT_INCREMENT(ipsecstat
.in_nomem
);
574 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
575 struct ip
*, ip
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
576 struct ip
*, ip
, struct ip6_hdr
*, NULL
);
578 if (nxt
!= IPPROTO_DONE
) {
579 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
580 ipsec4_in_reject(m
, NULL
)) {
581 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
584 ip_proto_dispatch_in(m
, off
, nxt
, 0);
591 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
592 printf("DP ah4_input call free SA:%p\n", sav
));
593 key_freesav(sav
, KEY_SADB_UNLOCKED
);
595 IPSEC_STAT_INCREMENT(ipsecstat
.in_success
);
600 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
601 printf("DP ah4_input call free SA:%p\n", sav
));
602 key_freesav(sav
, KEY_SADB_UNLOCKED
);
612 ah6_input(struct mbuf
**mp
, int *offp
, int proto
)
614 #pragma unused(proto)
615 struct mbuf
*m
= *mp
;
620 const struct ah_algorithm
*algo
;
624 struct secasvar
*sav
= NULL
;
629 #ifndef PULLDOWN_TEST
630 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
), {return IPPROTO_DONE
;});
631 ah
= (struct ah
*)(mtod(m
, caddr_t
) + off
);
633 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
, sizeof(struct newah
));
635 ipseclog((LOG_DEBUG
, "IPv6 AH input: can't pullup\n"));
636 ipsec6stat
.in_inval
++;
640 ip6
= mtod(m
, struct ip6_hdr
*);
643 /* find the sassoc. */
646 if (ntohs(ip6
->ip6_plen
) == 0) {
647 ipseclog((LOG_ERR
, "IPv6 AH input: "
648 "AH with IPv6 jumbogram is not supported.\n"));
649 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
653 if ((sav
= key_allocsa(AF_INET6
,
654 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
655 IPPROTO_AH
, spi
)) == 0) {
656 ipseclog((LOG_WARNING
,
657 "IPv6 AH input: no key association found for spi %u\n",
658 (u_int32_t
)ntohl(spi
)));
659 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nosa
);
662 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
663 printf("DP ah6_input called to allocate SA:%p\n", sav
));
664 if (sav
->state
!= SADB_SASTATE_MATURE
665 && sav
->state
!= SADB_SASTATE_DYING
) {
667 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
668 (u_int32_t
)ntohl(spi
)));
669 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
673 algo
= ah_algorithm_lookup(sav
->alg_auth
);
675 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
676 "unsupported authentication algorithm for spi %u\n",
677 (u_int32_t
)ntohl(spi
)));
678 IPSEC_STAT_INCREMENT(ipsec6stat
.in_badspi
);
682 siz
= (*algo
->sumsiz
)(sav
);
683 siz1
= ((siz
+ 3) & ~(4 - 1));
686 * sanity checks for header, 1.
691 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
694 * Here, we do not do "siz1 == siz". See ah4_input() for complete
698 ipseclog((LOG_NOTICE
, "sum length too short in IPv6 AH input "
699 "(%lu, should be at least %lu): %s\n",
700 (u_int32_t
)siz1
, (u_int32_t
)siz
,
701 ipsec6_logpacketstr(ip6
, spi
)));
702 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
705 if ((ah
->ah_len
<< 2) - sizoff
!= siz1
) {
706 ipseclog((LOG_NOTICE
, "sum length mismatch in IPv6 AH input "
707 "(%d should be %lu): %s\n",
708 (ah
->ah_len
<< 2) - sizoff
, (u_int32_t
)siz1
,
709 ipsec6_logpacketstr(ip6
, spi
)));
710 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
713 #ifndef PULLDOWN_TEST
714 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
,
715 {return IPPROTO_DONE
;});
717 IP6_EXTHDR_GET(ah
, struct ah
*, m
, off
,
718 sizeof(struct ah
) + sizoff
+ siz1
);
720 ipseclog((LOG_NOTICE
, "couldn't pullup gather IPv6 AH checksum part"));
721 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
729 * check for sequence number.
731 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
732 if (ipsec_chkreplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
))
735 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
736 ipseclog((LOG_WARNING
,
737 "replay packet in IPv6 AH input: %s %s\n",
738 ipsec6_logpacketstr(ip6
, spi
),
739 ipsec_logsastr(sav
)));
745 * alright, it seems sane. now we are going to check the
746 * cryptographic checksum.
748 cksum
= _MALLOC(siz1
, M_TEMP
, M_NOWAIT
);
750 ipseclog((LOG_DEBUG
, "IPv6 AH input: "
751 "couldn't alloc temporary region for cksum\n"));
752 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
756 if (ah6_calccksum(m
, (caddr_t
)cksum
, siz1
, algo
, sav
)) {
758 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
761 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahhist
[sav
->alg_auth
]);
764 caddr_t sumpos
= NULL
;
766 if (sav
->flags
& SADB_X_EXT_OLD
) {
768 sumpos
= (caddr_t
)(ah
+ 1);
771 sumpos
= (caddr_t
)(((struct newah
*)ah
) + 1);
774 if (bcmp(sumpos
, cksum
, siz
) != 0) {
775 ipseclog((LOG_WARNING
,
776 "checksum mismatch in IPv6 AH input: %s %s\n",
777 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
779 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
786 m
->m_flags
|= M_AUTHIPHDR
;
787 m
->m_flags
|= M_AUTHIPDGM
;
791 * looks okey, but we need more sanity check.
792 * XXX should elaborate.
794 if (ah
->ah_nxt
== IPPROTO_IPV6
) {
795 struct ip6_hdr
*nip6
;
798 sizoff
= (sav
->flags
& SADB_X_EXT_OLD
) ? 0 : 4;
800 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct ah
) + sizoff
+ siz1
801 + sizeof(struct ip6_hdr
),
802 {return IPPROTO_DONE
;});
804 nip6
= (struct ip6_hdr
*)((u_char
*)(ah
+ 1) + sizoff
+ siz1
);
805 if (!IN6_ARE_ADDR_EQUAL(&nip6
->ip6_src
, &ip6
->ip6_src
)
806 || !IN6_ARE_ADDR_EQUAL(&nip6
->ip6_dst
, &ip6
->ip6_dst
)) {
807 m
->m_flags
&= ~M_AUTHIPHDR
;
808 m
->m_flags
&= ~M_AUTHIPDGM
;
810 } else if (ah
->ah_nxt
== IPPROTO_IPIP
) {
811 m
->m_flags
&= ~M_AUTHIPHDR
;
812 m
->m_flags
&= ~M_AUTHIPDGM
;
813 } else if (ah
->ah_nxt
== IPPROTO_IP
) {
814 m
->m_flags
&= ~M_AUTHIPHDR
;
815 m
->m_flags
&= ~M_AUTHIPDGM
;
819 if (m
->m_flags
& M_AUTHIPHDR
820 && m
->m_flags
& M_AUTHIPDGM
) {
823 "IPv6 AH input: authentication succeess\n"));
825 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthsucc
);
827 ipseclog((LOG_WARNING
,
828 "authentication failed in IPv6 AH input: %s %s\n",
829 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
830 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahauthfail
);
835 * update sequence number.
837 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
838 if (ipsec_updatereplay(ntohl(((struct newah
*)ah
)->ah_seq
), sav
)) {
839 IPSEC_STAT_INCREMENT(ipsec6stat
.in_ahreplay
);
844 /* was it transmitted over the IPsec tunnel SA? */
845 if (sav
->flags
& SADB_X_EXT_OLD
) {
847 stripsiz
= sizeof(struct ah
) + siz1
;
850 stripsiz
= sizeof(struct newah
) + siz1
;
852 if (ipsec6_tunnel_validate(m
, off
+ stripsiz
, nxt
, sav
)) {
854 struct sockaddr_storage addr
;
857 * strip off all the headers that precedes AH.
858 * IP6 xx AH IP6' payload -> IP6' payload
860 * XXX more sanity checks
861 * XXX relationship with gif?
863 u_int32_t flowinfo
; /*net endian*/
865 flowinfo
= ip6
->ip6_flow
;
866 m_adj(m
, off
+ stripsiz
);
867 if (m
->m_len
< sizeof(*ip6
)) {
869 * m_pullup is prohibited in KAME IPv6 input processing
870 * but there's no other way!
872 m
= m_pullup(m
, sizeof(*ip6
));
874 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
878 ip6
= mtod(m
, struct ip6_hdr
*);
879 /* ECN consideration. */
880 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
881 if (!key_checktunnelsanity(sav
, AF_INET6
,
882 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
883 ipseclog((LOG_NOTICE
, "ipsec tunnel address mismatch "
884 "in IPv6 AH input: %s %s\n",
885 ipsec6_logpacketstr(ip6
, spi
),
886 ipsec_logsastr(sav
)));
887 IPSEC_STAT_INCREMENT(ipsec6stat
.in_inval
);
893 * should the inner packet be considered authentic?
894 * see comment in ah4_input().
896 m
->m_flags
&= ~M_AUTHIPHDR
;
897 m
->m_flags
&= ~M_AUTHIPDGM
;
900 key_sa_recordxfer(sav
, m
);
901 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0 ||
902 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
903 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
907 if (ip6_doscopedroute
) {
908 struct sockaddr_in6
*ip6addr
;
910 bzero(&addr
, sizeof(addr
));
911 ip6addr
= (__typeof__(ip6addr
))&addr
;
912 ip6addr
->sin6_family
= AF_INET6
;
913 ip6addr
->sin6_len
= sizeof(*ip6addr
);
914 ip6addr
->sin6_addr
= ip6
->ip6_dst
;
916 // update the receiving interface address based on the inner address
917 ifa
= ifa_ifwithaddr((struct sockaddr
*)&addr
);
919 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
924 if (proto_input(PF_INET6
, m
) != 0)
934 * Copy the value of the next header field of AH to the
935 * next header field of the previous header.
936 * This is necessary because AH will be stripped off below.
938 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
941 ip6
= mtod(m
, struct ip6_hdr
*);
942 #ifndef PULLDOWN_TEST
944 * We do deep-copy since KAME requires that
945 * the packet is placed in a single mbuf.
947 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
948 m
->m_data
+= stripsiz
;
949 m
->m_len
-= stripsiz
;
950 m
->m_pkthdr
.len
-= stripsiz
;
953 * even in m_pulldown case, we need to strip off AH so that
954 * we can compute checksum for multiple AH correctly.
956 if (m
->m_len
>= stripsiz
+ off
) {
957 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
958 m
->m_data
+= stripsiz
;
959 m
->m_len
-= stripsiz
;
960 m
->m_pkthdr
.len
-= stripsiz
;
963 * this comes with no copy if the boundary is on
968 n
= m_split(m
, off
, M_DONTWAIT
);
970 /* m is retained by m_split */
974 /* m_cat does not update m_pkthdr.len */
975 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
979 ip6
= mtod(m
, struct ip6_hdr
*);
981 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
983 key_sa_recordxfer(sav
, m
);
984 if (ipsec_addhist(m
, IPPROTO_AH
, spi
) != 0) {
985 IPSEC_STAT_INCREMENT(ipsec6stat
.in_nomem
);
994 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
995 printf("DP ah6_input call free SA:%p\n", sav
));
996 key_freesav(sav
, KEY_SADB_UNLOCKED
);
998 IPSEC_STAT_INCREMENT(ipsec6stat
.in_success
);
1003 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1004 printf("DP ah6_input call free SA:%p\n", sav
));
1005 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1009 return IPPROTO_DONE
;
1013 ah6_ctlinput(cmd
, sa
, d
)
1015 struct sockaddr
*sa
;
1018 const struct newah
*ahp
;
1020 struct secasvar
*sav
;
1021 struct ip6_hdr
*ip6
;
1023 struct ip6ctlparam
*ip6cp
= NULL
;
1025 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
1027 if (sa
->sa_family
!= AF_INET6
||
1028 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1030 if ((unsigned)cmd
>= PRC_NCMDS
)
1033 /* if the parameter is from icmp6, decode it. */
1035 ip6cp
= (struct ip6ctlparam
*)d
;
1037 ip6
= ip6cp
->ip6c_ip6
;
1038 off
= ip6cp
->ip6c_off
;
1046 * XXX: We assume that when ip6 is non NULL,
1047 * M and OFF are valid.
1050 /* check if we can safely examine src and dst ports */
1051 if (m
->m_pkthdr
.len
< off
+ sizeof(ah
))
1054 if (m
->m_len
< off
+ sizeof(ah
)) {
1056 * this should be rare case,
1057 * so we compromise on this copy...
1059 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1062 ahp
= (struct newah
*)(mtod(m
, caddr_t
) + off
);
1064 if (cmd
== PRC_MSGSIZE
) {
1068 * Check to see if we have a valid SA corresponding to
1069 * the address in the ICMP message payload.
1071 sa6_src
= ip6cp
->ip6c_src
;
1072 sa6_dst
= (struct sockaddr_in6
*)sa
;
1073 sav
= key_allocsa(AF_INET6
,
1074 (caddr_t
)&sa6_src
->sin6_addr
,
1075 (caddr_t
)&sa6_dst
->sin6_addr
,
1076 IPPROTO_AH
, ahp
->ah_spi
);
1078 if (sav
->state
== SADB_SASTATE_MATURE
||
1079 sav
->state
== SADB_SASTATE_DYING
)
1081 key_freesav(sav
, KEY_SADB_UNLOCKED
);
1084 /* XXX Further validation? */
1087 * Depending on the value of "valid" and routing table
1088 * size (mtudisc_{hi,lo}wat), we will:
1089 * - recalcurate the new MTU and create the
1090 * corresponding routing entry, or
1091 * - ignore the MTU change notification.
1093 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1096 /* we normally notify single pcb here */
1098 /* we normally notify any pcb here */