1 /* $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 kjc Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC1826/2402 authentication header.
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <machine/cpu.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_ecn.h>
63 #include <netinet6/ip6_ecn.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/in6_pcb.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/ip6protosw.h>
74 #include <netinet6/ipsec.h>
76 #include <netinet6/ipsec6.h>
78 #include <netinet6/ah.h>
80 #include <netinet6/ah6.h>
82 #include <netkey/key.h>
83 #include <netkey/keydb.h>
85 #include <netkey/key_debug.h>
87 #define KEYDEBUG(lev,arg)
90 #include <machine/stdarg.h>
92 #include <net/net_osdep.h>
97 #include <netinet/ipprotosw.h>
98 extern struct ipprotosw inetsw[];
102 ah4_input(struct mbuf *m, ...)
104 ah4_input(m, va_alist)
112 const struct ah_algorithm *algo;
116 struct secasvar *sav = NULL;
125 off = va_arg(ap, int);
126 proto = va_arg(ap, int);
129 #ifndef PULLDOWN_TEST
130 if (m->m_len < off + sizeof(struct newah)) {
131 m = m_pullup(m, off + sizeof(struct newah));
133 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
134 "dropping the packet for simplicity\n"));
135 ipsecstat.in_inval++;
140 ip = mtod(m, struct ip *);
141 ah = (struct ah *)(((caddr_t)ip) + off);
143 ip = mtod(m, struct ip *);
144 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
146 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
147 "dropping the packet for simplicity\n"));
148 ipsecstat.in_inval++;
154 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
156 hlen = ip->ip_hl << 2;
159 /* find the sassoc. */
162 if ((sav = key_allocsa(AF_INET,
163 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
164 IPPROTO_AH, spi)) == 0) {
165 ipseclog((LOG_WARNING,
166 "IPv4 AH input: no key association found for spi %u\n",
167 (u_int32_t)ntohl(spi)));
171 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
172 printf("DP ah4_input called to allocate SA:%p\n", sav));
173 if (sav->state != SADB_SASTATE_MATURE
174 && sav->state != SADB_SASTATE_DYING) {
176 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
177 (u_int32_t)ntohl(spi)));
178 ipsecstat.in_badspi++;
182 algo = ah_algorithm_lookup(sav->alg_auth);
184 ipseclog((LOG_DEBUG, "IPv4 AH input: "
185 "unsupported authentication algorithm for spi %u\n",
186 (u_int32_t)ntohl(spi)));
187 ipsecstat.in_badspi++;
191 siz = (*algo->sumsiz)(sav);
192 siz1 = ((siz + 3) & ~(4 - 1));
195 * sanity checks for header, 1.
200 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
203 * Here, we do not do "siz1 == siz". This is because the way
204 * RFC240[34] section 2 is written. They do not require truncation
206 * For example, Microsoft IPsec stack attaches 160 bits of
207 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
208 * 32 bits of padding is attached.
210 * There are two downsides to this specification.
211 * They have no real harm, however, they leave us fuzzy feeling.
212 * - if we attach more than 96 bits of authentication data onto AH,
213 * we will never notice about possible modification by rogue
214 * intermediate nodes.
215 * Since extra bits in AH checksum is never used, this constitutes
216 * no real issue, however, it is wacky.
217 * - even if the peer attaches big authentication data, we will never
218 * notice the difference, since longer authentication data will just
221 * We may need some clarification in the spec.
224 ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input "
225 "(%lu, should be at least %lu): %s\n",
226 (u_long)siz1, (u_long)siz,
227 ipsec4_logpacketstr(ip, spi)));
228 ipsecstat.in_inval++;
231 if ((ah->ah_len << 2) - sizoff != siz1) {
232 ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input "
233 "(%d should be %lu): %s\n",
234 (ah->ah_len << 2) - sizoff, (u_long)siz1,
235 ipsec4_logpacketstr(ip, spi)));
236 ipsecstat.in_inval++;
240 #ifndef PULLDOWN_TEST
241 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) {
242 m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1);
244 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
245 ipsecstat.in_inval++;
249 ip = mtod(m, struct ip *);
250 ah = (struct ah *)(((caddr_t)ip) + off);
253 IP6_EXTHDR_GET(ah, struct ah *, m, off,
254 sizeof(struct ah) + sizoff + siz1);
256 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
257 ipsecstat.in_inval++;
264 * check for sequence number.
266 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
267 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
270 ipsecstat.in_ahreplay++;
271 ipseclog((LOG_WARNING,
272 "replay packet in IPv4 AH input: %s %s\n",
273 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
279 * alright, it seems sane. now we are going to check the
280 * cryptographic checksum.
282 cksum = malloc(siz1, M_TEMP, M_NOWAIT);
284 ipseclog((LOG_DEBUG, "IPv4 AH input: "
285 "couldn't alloc temporary region for cksum\n"));
286 ipsecstat.in_inval++;
291 * some of IP header fields are flipped to the host endian.
292 * convert them back to network endian. VERY stupid.
294 ip->ip_len = htons(ip->ip_len + hlen);
295 ip->ip_off = htons(ip->ip_off);
296 if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
298 ipsecstat.in_inval++;
301 ipsecstat.in_ahhist[sav->alg_auth]++;
305 ip->ip_len = ntohs(ip->ip_len) - hlen;
306 ip->ip_off = ntohs(ip->ip_off);
309 caddr_t sumpos = NULL;
311 if (sav->flags & SADB_X_EXT_OLD) {
313 sumpos = (caddr_t)(ah + 1);
316 sumpos = (caddr_t)(((struct newah *)ah) + 1);
319 if (bcmp(sumpos, cksum, siz) != 0) {
320 ipseclog((LOG_WARNING,
321 "checksum mismatch in IPv4 AH input: %s %s\n",
322 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
324 ipsecstat.in_ahauthfail++;
331 m->m_flags |= M_AUTHIPHDR;
332 m->m_flags |= M_AUTHIPDGM;
336 * looks okey, but we need more sanity check.
337 * XXX should elaborate.
339 if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) {
343 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
345 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) {
346 m = m_pullup(m, off + sizeof(struct ah)
347 + sizoff + siz1 + hlen);
350 "IPv4 AH input: can't pullup\n"));
351 ipsecstat.in_inval++;
356 nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1);
357 if (nip->ip_src.s_addr != ip->ip_src.s_addr
358 || nip->ip_dst.s_addr != ip->ip_dst.s_addr) {
359 m->m_flags &= ~M_AUTHIPHDR;
360 m->m_flags &= ~M_AUTHIPDGM;
364 else if (ah->ah_nxt == IPPROTO_IPV6) {
365 m->m_flags &= ~M_AUTHIPHDR;
366 m->m_flags &= ~M_AUTHIPDGM;
371 if (m->m_flags & M_AUTHIPHDR
372 && m->m_flags & M_AUTHIPDGM) {
375 "IPv4 AH input: authentication succeess\n"));
377 ipsecstat.in_ahauthsucc++;
379 ipseclog((LOG_WARNING,
380 "authentication failed in IPv4 AH input: %s %s\n",
381 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
382 ipsecstat.in_ahauthfail++;
387 * update sequence number.
389 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
390 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
391 ipsecstat.in_ahreplay++;
396 /* was it transmitted over the IPsec tunnel SA? */
397 if (sav->flags & SADB_X_EXT_OLD) {
399 stripsiz = sizeof(struct ah) + siz1;
402 stripsiz = sizeof(struct newah) + siz1;
404 if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) {
406 * strip off all the headers that precedes AH.
407 * IP xx AH IP' payload -> IP' payload
409 * XXX more sanity checks
410 * XXX relationship with gif?
415 m_adj(m, off + stripsiz);
416 if (m->m_len < sizeof(*ip)) {
417 m = m_pullup(m, sizeof(*ip));
419 ipsecstat.in_inval++;
423 ip = mtod(m, struct ip *);
424 /* ECN consideration. */
425 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
426 if (!key_checktunnelsanity(sav, AF_INET,
427 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
428 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
429 "in IPv4 AH input: %s %s\n",
430 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
431 ipsecstat.in_inval++;
437 * Should the inner packet be considered authentic?
438 * My current answer is: NO.
440 * host1 -- gw1 === gw2 -- host2
441 * In this case, gw2 can trust the authenticity of the
442 * outer packet, but NOT inner. Packet may be altered
443 * between host1 and gw1.
445 * host1 -- gw1 === host2
446 * This case falls into the same scenario as above.
449 * This case is the only case when we may be able to leave
450 * M_AUTHIPHDR and M_AUTHIPDGM set.
451 * However, if host1 is wrongly configured, and allows
452 * attacker to inject some packet with src=host1 and
453 * dst=host2, you are in risk.
455 m->m_flags &= ~M_AUTHIPHDR;
456 m->m_flags &= ~M_AUTHIPDGM;
459 key_sa_recordxfer(sav, m);
460 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
461 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
462 ipsecstat.in_nomem++;
467 if (IF_QFULL(&ipintrq)) {
468 ipsecstat.in_inval++;
472 IF_ENQUEUE(&ipintrq, m);
474 schednetisr(NETISR_IP); /* can be skipped but to make sure */
482 ip = mtod(m, struct ip *);
483 #ifndef PULLDOWN_TEST
485 * We do deep-copy since KAME requires that
486 * the packet is placed in a single external mbuf.
488 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
489 m->m_data += stripsiz;
490 m->m_len -= stripsiz;
491 m->m_pkthdr.len -= stripsiz;
494 * even in m_pulldown case, we need to strip off AH so that
495 * we can compute checksum for multiple AH correctly.
497 if (m->m_len >= stripsiz + off) {
498 ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
499 m->m_data += stripsiz;
500 m->m_len -= stripsiz;
501 m->m_pkthdr.len -= stripsiz;
504 * this comes with no copy if the boundary is on
509 n = m_split(m, off, M_DONTWAIT);
511 /* m is retained by m_split */
516 /* m_cat does not update m_pkthdr.len */
517 m->m_pkthdr.len += n->m_pkthdr.len;
521 if (m->m_len < sizeof(*ip)) {
522 m = m_pullup(m, sizeof(*ip));
524 ipsecstat.in_inval++;
528 ip = mtod(m, struct ip *);
530 ip->ip_len = ip->ip_len - stripsiz;
532 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
535 /* forget about IP hdr checksum, the check has already been passed */
537 key_sa_recordxfer(sav, m);
538 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
539 ipsecstat.in_nomem++;
543 if (nxt != IPPROTO_DONE) {
544 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
545 ipsec4_in_reject(m, NULL)) {
546 ipsecstat.in_polvio++;
549 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
556 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
557 printf("DP ah4_input call free SA:%p\n", sav));
560 ipsecstat.in_success++;
565 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
566 printf("DP ah4_input call free SA:%p\n", sav));
577 ah6_input(mp, offp, proto)
581 struct mbuf *m = *mp;
586 const struct ah_algorithm *algo;
590 struct secasvar *sav = NULL;
595 #ifndef PULLDOWN_TEST
596 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
597 ah = (struct ah *)(mtod(m, caddr_t) + off);
599 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
601 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
602 ipsec6stat.in_inval++;
606 ip6 = mtod(m, struct ip6_hdr *);
609 /* find the sassoc. */
612 if (ntohs(ip6->ip6_plen) == 0) {
613 ipseclog((LOG_ERR, "IPv6 AH input: "
614 "AH with IPv6 jumbogram is not supported.\n"));
615 ipsec6stat.in_inval++;
619 if ((sav = key_allocsa(AF_INET6,
620 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
621 IPPROTO_AH, spi)) == 0) {
622 ipseclog((LOG_WARNING,
623 "IPv6 AH input: no key association found for spi %u\n",
624 (u_int32_t)ntohl(spi)));
625 ipsec6stat.in_nosa++;
628 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
629 printf("DP ah6_input called to allocate SA:%p\n", sav));
630 if (sav->state != SADB_SASTATE_MATURE
631 && sav->state != SADB_SASTATE_DYING) {
633 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
634 (u_int32_t)ntohl(spi)));
635 ipsec6stat.in_badspi++;
639 algo = ah_algorithm_lookup(sav->alg_auth);
641 ipseclog((LOG_DEBUG, "IPv6 AH input: "
642 "unsupported authentication algorithm for spi %u\n",
643 (u_int32_t)ntohl(spi)));
644 ipsec6stat.in_badspi++;
648 siz = (*algo->sumsiz)(sav);
649 siz1 = ((siz + 3) & ~(4 - 1));
652 * sanity checks for header, 1.
657 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
660 * Here, we do not do "siz1 == siz". See ah4_input() for complete
664 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
665 "(%lu, should be at least %lu): %s\n",
666 (u_long)siz1, (u_long)siz,
667 ipsec6_logpacketstr(ip6, spi)));
668 ipsec6stat.in_inval++;
671 if ((ah->ah_len << 2) - sizoff != siz1) {
672 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
673 "(%d should be %lu): %s\n",
674 (ah->ah_len << 2) - sizoff, (u_long)siz1,
675 ipsec6_logpacketstr(ip6, spi)));
676 ipsec6stat.in_inval++;
679 #ifndef PULLDOWN_TEST
680 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
682 IP6_EXTHDR_GET(ah, struct ah *, m, off,
683 sizeof(struct ah) + sizoff + siz1);
685 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
686 ipsec6stat.in_inval++;
694 * check for sequence number.
696 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
697 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
700 ipsec6stat.in_ahreplay++;
701 ipseclog((LOG_WARNING,
702 "replay packet in IPv6 AH input: %s %s\n",
703 ipsec6_logpacketstr(ip6, spi),
704 ipsec_logsastr(sav)));
710 * alright, it seems sane. now we are going to check the
711 * cryptographic checksum.
713 cksum = malloc(siz1, M_TEMP, M_NOWAIT);
715 ipseclog((LOG_DEBUG, "IPv6 AH input: "
716 "couldn't alloc temporary region for cksum\n"));
717 ipsec6stat.in_inval++;
721 if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
723 ipsec6stat.in_inval++;
726 ipsec6stat.in_ahhist[sav->alg_auth]++;
729 caddr_t sumpos = NULL;
731 if (sav->flags & SADB_X_EXT_OLD) {
733 sumpos = (caddr_t)(ah + 1);
736 sumpos = (caddr_t)(((struct newah *)ah) + 1);
739 if (bcmp(sumpos, cksum, siz) != 0) {
740 ipseclog((LOG_WARNING,
741 "checksum mismatch in IPv6 AH input: %s %s\n",
742 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
744 ipsec6stat.in_ahauthfail++;
751 m->m_flags |= M_AUTHIPHDR;
752 m->m_flags |= M_AUTHIPDGM;
756 * looks okey, but we need more sanity check.
757 * XXX should elaborate.
759 if (ah->ah_nxt == IPPROTO_IPV6) {
760 struct ip6_hdr *nip6;
763 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
765 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
766 + sizeof(struct ip6_hdr), IPPROTO_DONE);
768 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
769 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
770 || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
771 m->m_flags &= ~M_AUTHIPHDR;
772 m->m_flags &= ~M_AUTHIPDGM;
774 } else if (ah->ah_nxt == IPPROTO_IPIP) {
775 m->m_flags &= ~M_AUTHIPHDR;
776 m->m_flags &= ~M_AUTHIPDGM;
777 } else if (ah->ah_nxt == IPPROTO_IP) {
778 m->m_flags &= ~M_AUTHIPHDR;
779 m->m_flags &= ~M_AUTHIPDGM;
783 if (m->m_flags & M_AUTHIPHDR
784 && m->m_flags & M_AUTHIPDGM) {
787 "IPv6 AH input: authentication succeess\n"));
789 ipsec6stat.in_ahauthsucc++;
791 ipseclog((LOG_WARNING,
792 "authentication failed in IPv6 AH input: %s %s\n",
793 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
794 ipsec6stat.in_ahauthfail++;
799 * update sequence number.
801 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
802 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
803 ipsec6stat.in_ahreplay++;
808 /* was it transmitted over the IPsec tunnel SA? */
809 if (sav->flags & SADB_X_EXT_OLD) {
811 stripsiz = sizeof(struct ah) + siz1;
814 stripsiz = sizeof(struct newah) + siz1;
816 if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
818 * strip off all the headers that precedes AH.
819 * IP6 xx AH IP6' payload -> IP6' payload
821 * XXX more sanity checks
822 * XXX relationship with gif?
824 u_int32_t flowinfo; /* net endian */
826 flowinfo = ip6->ip6_flow;
827 m_adj(m, off + stripsiz);
828 if (m->m_len < sizeof(*ip6)) {
830 * m_pullup is prohibited in KAME IPv6 input processing
831 * but there's no other way!
833 m = m_pullup(m, sizeof(*ip6));
835 ipsec6stat.in_inval++;
839 ip6 = mtod(m, struct ip6_hdr *);
840 /* ECN consideration. */
841 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
842 if (!key_checktunnelsanity(sav, AF_INET6,
843 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
844 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
845 "in IPv6 AH input: %s %s\n",
846 ipsec6_logpacketstr(ip6, spi),
847 ipsec_logsastr(sav)));
848 ipsec6stat.in_inval++;
854 * should the inner packet be considered authentic?
855 * see comment in ah4_input().
857 m->m_flags &= ~M_AUTHIPHDR;
858 m->m_flags &= ~M_AUTHIPDGM;
861 key_sa_recordxfer(sav, m);
862 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
863 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
864 ipsec6stat.in_nomem++;
869 if (IF_QFULL(&ip6intrq)) {
870 ipsec6stat.in_inval++;
874 IF_ENQUEUE(&ip6intrq, m);
876 schednetisr(NETISR_IPV6); /* can be skipped but to make sure */
886 * Copy the value of the next header field of AH to the
887 * next header field of the previous header.
888 * This is necessary because AH will be stripped off below.
890 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
893 ip6 = mtod(m, struct ip6_hdr *);
894 #ifndef PULLDOWN_TEST
896 * We do deep-copy since KAME requires that
897 * the packet is placed in a single mbuf.
899 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
900 m->m_data += stripsiz;
901 m->m_len -= stripsiz;
902 m->m_pkthdr.len -= stripsiz;
905 * even in m_pulldown case, we need to strip off AH so that
906 * we can compute checksum for multiple AH correctly.
908 if (m->m_len >= stripsiz + off) {
909 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
910 m->m_data += stripsiz;
911 m->m_len -= stripsiz;
912 m->m_pkthdr.len -= stripsiz;
915 * this comes with no copy if the boundary is on
920 n = m_split(m, off, M_DONTWAIT);
922 /* m is retained by m_split */
927 /* m_cat does not update m_pkthdr.len */
928 m->m_pkthdr.len += n->m_pkthdr.len;
931 ip6 = mtod(m, struct ip6_hdr *);
933 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
935 key_sa_recordxfer(sav, m);
936 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
937 ipsec6stat.in_nomem++;
946 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
947 printf("DP ah6_input call free SA:%p\n", sav));
950 ipsec6stat.in_success++;
955 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
956 printf("DP ah6_input call free SA:%p\n", sav));
965 ah6_ctlinput(cmd, sa, d)
970 const struct newah *ahp;
972 struct secasvar *sav;
975 struct ip6ctlparam *ip6cp = NULL;
977 struct sockaddr_in6 *sa6_src, *sa6_dst;
979 if (sa->sa_family != AF_INET6 ||
980 sa->sa_len != sizeof(struct sockaddr_in6))
982 if ((unsigned)cmd >= PRC_NCMDS)
985 /* if the parameter is from icmp6, decode it. */
987 ip6cp = (struct ip6ctlparam *)d;
989 ip6 = ip6cp->ip6c_ip6;
990 off = ip6cp->ip6c_off;
998 * XXX: We assume that when ip6 is non NULL,
999 * M and OFF are valid.
1002 /* check if we can safely examine src and dst ports */
1003 if (m->m_pkthdr.len < off + sizeof(ah))
1006 if (m->m_len < off + sizeof(ah)) {
1008 * this should be rare case,
1009 * so we compromise on this copy...
1011 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1014 ahp = (struct newah *)(mtod(m, caddr_t) + off);
1016 if (cmd == PRC_MSGSIZE) {
1020 * Check to see if we have a valid SA corresponding to
1021 * the address in the ICMP message payload.
1023 sa6_src = ip6cp->ip6c_src;
1024 sa6_dst = (struct sockaddr_in6 *)sa;
1025 sav = key_allocsa(AF_INET6,
1026 (caddr_t)&sa6_src->sin6_addr,
1027 (caddr_t)&sa6_dst->sin6_addr,
1028 IPPROTO_AH, ahp->ah_spi);
1030 if (sav->state == SADB_SASTATE_MATURE ||
1031 sav->state == SADB_SASTATE_DYING)
1036 /* XXX Further validation? */
1039 * Depending on the value of "valid" and routing table
1040 * size (mtudisc_{hi,lo}wat), we will:
1041 * - recalcurate the new MTU and create the
1042 * corresponding routing entry, or
1043 * - ignore the MTU change notification.
1045 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1048 /* we normally notify single pcb here */
1050 /* we normally notify any pcb here */