2 /* $KAME: ah_input.c,v 1.59 2001/05/16 04:01:27 jinmei 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 extern struct protosw inetsw[];
107 const struct ah_algorithm *algo;
111 struct secasvar *sav = NULL;
117 #ifndef PULLDOWN_TEST
118 if (m->m_len < off + sizeof(struct newah)) {
119 m = m_pullup(m, off + sizeof(struct newah));
121 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
122 "dropping the packet for simplicity\n"));
123 ipsecstat.in_inval++;
128 ip = mtod(m, struct ip *);
130 ah = (struct ah *)(((caddr_t)ip) + off);
132 ip = mtod(m, struct ip *);
134 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
136 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
137 "dropping the packet for simplicity\n"));
138 ipsecstat.in_inval++;
144 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
146 hlen = ip->ip_hl << 2;
149 /* find the sassoc. */
152 if ((sav = key_allocsa(AF_INET,
153 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
154 IPPROTO_AH, spi)) == 0) {
155 ipseclog((LOG_WARNING,
156 "IPv4 AH input: no key association found for spi %u\n",
157 (u_int32_t)ntohl(spi)));
161 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
162 printf("DP ah4_input called to allocate SA:%p\n", sav));
163 if (sav->state != SADB_SASTATE_MATURE
164 && sav->state != SADB_SASTATE_DYING) {
166 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
167 (u_int32_t)ntohl(spi)));
168 ipsecstat.in_badspi++;
172 algo = ah_algorithm_lookup(sav->alg_auth);
174 ipseclog((LOG_DEBUG, "IPv4 AH input: "
175 "unsupported authentication algorithm for spi %u\n",
176 (u_int32_t)ntohl(spi)));
177 ipsecstat.in_badspi++;
181 siz = (*algo->sumsiz)(sav);
182 siz1 = ((siz + 3) & ~(4 - 1));
185 * sanity checks for header, 1.
190 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
193 * Here, we do not do "siz1 == siz". This is because the way
194 * RFC240[34] section 2 is written. They do not require truncation
196 * For example, Microsoft IPsec stack attaches 160 bits of
197 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
198 * 32 bits of padding is attached.
200 * There are two downsides to this specification.
201 * They have no real harm, however, they leave us fuzzy feeling.
202 * - if we attach more than 96 bits of authentication data onto AH,
203 * we will never notice about possible modification by rogue
204 * intermediate nodes.
205 * Since extra bits in AH checksum is never used, this constitutes
206 * no real issue, however, it is wacky.
207 * - even if the peer attaches big authentication data, we will never
208 * notice the difference, since longer authentication data will just
211 * We may need some clarification in the spec.
214 ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input "
215 "(%lu, should be at least %lu): %s\n",
216 (u_long)siz1, (u_long)siz,
217 ipsec4_logpacketstr(ip, spi)));
218 ipsecstat.in_inval++;
221 if ((ah->ah_len << 2) - sizoff != siz1) {
222 ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input "
223 "(%d should be %lu): %s\n",
224 (ah->ah_len << 2) - sizoff, (u_long)siz1,
225 ipsec4_logpacketstr(ip, spi)));
226 ipsecstat.in_inval++;
230 #ifndef PULLDOWN_TEST
231 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) {
232 m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1);
234 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
235 ipsecstat.in_inval++;
239 ip = mtod(m, struct ip *);
240 ah = (struct ah *)(((caddr_t)ip) + off);
243 IP6_EXTHDR_GET(ah, struct ah *, m, off,
244 sizeof(struct ah) + sizoff + siz1);
246 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
247 ipsecstat.in_inval++;
254 * check for sequence number.
256 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
257 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
260 ipsecstat.in_ahreplay++;
261 ipseclog((LOG_WARNING,
262 "replay packet in IPv4 AH input: %s %s\n",
263 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
269 * alright, it seems sane. now we are going to check the
270 * cryptographic checksum.
272 cksum = malloc(siz1, M_TEMP, M_NOWAIT);
274 ipseclog((LOG_DEBUG, "IPv4 AH input: "
275 "couldn't alloc temporary region for cksum\n"));
276 ipsecstat.in_inval++;
281 * some of IP header fields are flipped to the host endian.
282 * convert them back to network endian. VERY stupid.
284 ip->ip_len = htons(ip->ip_len + hlen);
285 ip->ip_off = htons(ip->ip_off);
286 if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
288 ipsecstat.in_inval++;
291 ipsecstat.in_ahhist[sav->alg_auth]++;
295 ip->ip_len = ntohs(ip->ip_len) - hlen;
296 ip->ip_off = ntohs(ip->ip_off);
299 caddr_t sumpos = NULL;
301 if (sav->flags & SADB_X_EXT_OLD) {
303 sumpos = (caddr_t)(ah + 1);
306 sumpos = (caddr_t)(((struct newah *)ah) + 1);
309 if (bcmp(sumpos, cksum, siz) != 0) {
310 ipseclog((LOG_WARNING,
311 "checksum mismatch in IPv4 AH input: %s %s\n",
312 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
314 ipsecstat.in_ahauthfail++;
321 m->m_flags |= M_AUTHIPHDR;
322 m->m_flags |= M_AUTHIPDGM;
326 * looks okey, but we need more sanity check.
327 * XXX should elaborate.
329 if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) {
333 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
335 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) {
336 m = m_pullup(m, off + sizeof(struct ah)
337 + sizoff + siz1 + hlen);
340 "IPv4 AH input: can't pullup\n"));
341 ipsecstat.in_inval++;
346 nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1);
347 if (nip->ip_src.s_addr != ip->ip_src.s_addr
348 || nip->ip_dst.s_addr != ip->ip_dst.s_addr) {
349 m->m_flags &= ~M_AUTHIPHDR;
350 m->m_flags &= ~M_AUTHIPDGM;
354 else if (ah->ah_nxt == IPPROTO_IPV6) {
355 m->m_flags &= ~M_AUTHIPHDR;
356 m->m_flags &= ~M_AUTHIPDGM;
361 if (m->m_flags & M_AUTHIPHDR
362 && m->m_flags & M_AUTHIPDGM) {
365 "IPv4 AH input: authentication succeess\n"));
367 ipsecstat.in_ahauthsucc++;
369 ipseclog((LOG_WARNING,
370 "authentication failed in IPv4 AH input: %s %s\n",
371 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
372 ipsecstat.in_ahauthfail++;
377 * update sequence number.
379 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
380 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
381 ipsecstat.in_ahreplay++;
386 /* was it transmitted over the IPsec tunnel SA? */
387 if (sav->flags & SADB_X_EXT_OLD) {
389 stripsiz = sizeof(struct ah) + siz1;
392 stripsiz = sizeof(struct newah) + siz1;
394 if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) {
396 * strip off all the headers that precedes AH.
397 * IP xx AH IP' payload -> IP' payload
399 * XXX more sanity checks
400 * XXX relationship with gif?
405 m_adj(m, off + stripsiz);
406 if (m->m_len < sizeof(*ip)) {
407 m = m_pullup(m, sizeof(*ip));
409 ipsecstat.in_inval++;
413 ip = mtod(m, struct ip *);
414 /* ECN consideration. */
415 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
416 if (!key_checktunnelsanity(sav, AF_INET,
417 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
418 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
419 "in IPv4 AH input: %s %s\n",
420 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
421 ipsecstat.in_inval++;
425 #if 0 /* XXX should we call ipfw rather than ipsec_in_reject? */
426 /* drop it if it does not match the default policy */
427 if (ipsec4_in_reject(m, NULL)) {
428 ipsecstat.in_polvio++;
435 * Should the inner packet be considered authentic?
436 * My current answer is: NO.
438 * host1 -- gw1 === gw2 -- host2
439 * In this case, gw2 can trust the authenticity of the
440 * outer packet, but NOT inner. Packet may be altered
441 * between host1 and gw1.
443 * host1 -- gw1 === host2
444 * This case falls into the same scenario as above.
447 * This case is the only case when we may be able to leave
448 * M_AUTHIPHDR and M_AUTHIPDGM set.
449 * However, if host1 is wrongly configured, and allows
450 * attacker to inject some packet with src=host1 and
451 * dst=host2, you are in risk.
453 m->m_flags &= ~M_AUTHIPHDR;
454 m->m_flags &= ~M_AUTHIPDGM;
457 key_sa_recordxfer(sav, m);
458 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
459 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
460 ipsecstat.in_nomem++;
464 if (! IF_HANDOFF(&ipintrq, m, NULL)) {
465 ipsecstat.in_inval++;
470 schednetisr(NETISR_IP); /*can be skipped but to make sure*/
477 ip = mtod(m, struct ip *);
478 #ifndef PULLDOWN_TEST
480 * We do deep-copy since KAME requires that
481 * the packet is placed in a single external mbuf.
483 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
484 m->m_data += stripsiz;
485 m->m_len -= stripsiz;
486 m->m_pkthdr.len -= stripsiz;
489 * even in m_pulldown case, we need to strip off AH so that
490 * we can compute checksum for multiple AH correctly.
492 if (m->m_len >= stripsiz + off) {
493 ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
494 m->m_data += stripsiz;
495 m->m_len -= stripsiz;
496 m->m_pkthdr.len -= stripsiz;
499 * this comes with no copy if the boundary is on
504 n = m_split(m, off, M_DONTWAIT);
506 /* m is retained by m_split */
511 /* m_cat does not update m_pkthdr.len */
512 m->m_pkthdr.len += n->m_pkthdr.len;
516 if (m->m_len < sizeof(*ip)) {
517 m = m_pullup(m, sizeof(*ip));
519 ipsecstat.in_inval++;
523 ip = mtod(m, struct ip *);
525 ip->ip_len = ip->ip_len - stripsiz;
527 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
530 /* forget about IP hdr checksum, the check has already been passed */
532 key_sa_recordxfer(sav, m);
533 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
534 ipsecstat.in_nomem++;
538 if (nxt != IPPROTO_DONE) {
539 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
540 ipsec4_in_reject(m, NULL)) {
541 ipsecstat.in_polvio++;
544 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
551 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
552 printf("DP ah4_input call free SA:%p\n", sav));
555 ipsecstat.in_success++;
560 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
561 printf("DP ah4_input call free SA:%p\n", sav));
572 ah6_input(mp, offp, proto)
576 struct mbuf *m = *mp;
581 const struct ah_algorithm *algo;
585 struct secasvar *sav = NULL;
589 #ifndef PULLDOWN_TEST
590 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
591 ah = (struct ah *)(mtod(m, caddr_t) + off);
593 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
595 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
596 ipsec6stat.in_inval++;
600 ip6 = mtod(m, struct ip6_hdr *);
603 /* find the sassoc. */
606 if (ntohs(ip6->ip6_plen) == 0) {
607 ipseclog((LOG_ERR, "IPv6 AH input: "
608 "AH with IPv6 jumbogram is not supported.\n"));
609 ipsec6stat.in_inval++;
613 if ((sav = key_allocsa(AF_INET6,
614 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
615 IPPROTO_AH, spi)) == 0) {
616 ipseclog((LOG_WARNING,
617 "IPv6 AH input: no key association found for spi %u\n",
618 (u_int32_t)ntohl(spi)));
619 ipsec6stat.in_nosa++;
622 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
623 printf("DP ah6_input called to allocate SA:%p\n", sav));
624 if (sav->state != SADB_SASTATE_MATURE
625 && sav->state != SADB_SASTATE_DYING) {
627 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
628 (u_int32_t)ntohl(spi)));
629 ipsec6stat.in_badspi++;
633 algo = ah_algorithm_lookup(sav->alg_auth);
635 ipseclog((LOG_DEBUG, "IPv6 AH input: "
636 "unsupported authentication algorithm for spi %u\n",
637 (u_int32_t)ntohl(spi)));
638 ipsec6stat.in_badspi++;
642 siz = (*algo->sumsiz)(sav);
643 siz1 = ((siz + 3) & ~(4 - 1));
646 * sanity checks for header, 1.
651 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
654 * Here, we do not do "siz1 == siz". See ah4_input() for complete
658 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
659 "(%lu, should be at least %lu): %s\n",
660 (u_long)siz1, (u_long)siz,
661 ipsec6_logpacketstr(ip6, spi)));
662 ipsec6stat.in_inval++;
665 if ((ah->ah_len << 2) - sizoff != siz1) {
666 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
667 "(%d should be %lu): %s\n",
668 (ah->ah_len << 2) - sizoff, (u_long)siz1,
669 ipsec6_logpacketstr(ip6, spi)));
670 ipsec6stat.in_inval++;
673 #ifndef PULLDOWN_TEST
674 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
676 IP6_EXTHDR_GET(ah, struct ah *, m, off,
677 sizeof(struct ah) + sizoff + siz1);
679 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
680 ipsec6stat.in_inval++;
688 * check for sequence number.
690 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
691 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
694 ipsec6stat.in_ahreplay++;
695 ipseclog((LOG_WARNING,
696 "replay packet in IPv6 AH input: %s %s\n",
697 ipsec6_logpacketstr(ip6, spi),
698 ipsec_logsastr(sav)));
704 * alright, it seems sane. now we are going to check the
705 * cryptographic checksum.
707 cksum = malloc(siz1, M_TEMP, M_NOWAIT);
709 ipseclog((LOG_DEBUG, "IPv6 AH input: "
710 "couldn't alloc temporary region for cksum\n"));
711 ipsec6stat.in_inval++;
715 if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
717 ipsec6stat.in_inval++;
720 ipsec6stat.in_ahhist[sav->alg_auth]++;
723 caddr_t sumpos = NULL;
725 if (sav->flags & SADB_X_EXT_OLD) {
727 sumpos = (caddr_t)(ah + 1);
730 sumpos = (caddr_t)(((struct newah *)ah) + 1);
733 if (bcmp(sumpos, cksum, siz) != 0) {
734 ipseclog((LOG_WARNING,
735 "checksum mismatch in IPv6 AH input: %s %s\n",
736 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
738 ipsec6stat.in_ahauthfail++;
745 m->m_flags |= M_AUTHIPHDR;
746 m->m_flags |= M_AUTHIPDGM;
750 * looks okey, but we need more sanity check.
751 * XXX should elaborate.
753 if (ah->ah_nxt == IPPROTO_IPV6) {
754 struct ip6_hdr *nip6;
757 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
759 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
760 + sizeof(struct ip6_hdr), IPPROTO_DONE);
762 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
763 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
764 || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
765 m->m_flags &= ~M_AUTHIPHDR;
766 m->m_flags &= ~M_AUTHIPDGM;
768 } else if (ah->ah_nxt == IPPROTO_IPIP) {
769 m->m_flags &= ~M_AUTHIPHDR;
770 m->m_flags &= ~M_AUTHIPDGM;
771 } else if (ah->ah_nxt == IPPROTO_IP) {
772 m->m_flags &= ~M_AUTHIPHDR;
773 m->m_flags &= ~M_AUTHIPDGM;
777 if (m->m_flags & M_AUTHIPHDR
778 && m->m_flags & M_AUTHIPDGM) {
781 "IPv6 AH input: authentication succeess\n"));
783 ipsec6stat.in_ahauthsucc++;
785 ipseclog((LOG_WARNING,
786 "authentication failed in IPv6 AH input: %s %s\n",
787 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
788 ipsec6stat.in_ahauthfail++;
793 * update sequence number.
795 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
796 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
797 ipsec6stat.in_ahreplay++;
802 /* was it transmitted over the IPsec tunnel SA? */
803 if (sav->flags & SADB_X_EXT_OLD) {
805 stripsiz = sizeof(struct ah) + siz1;
808 stripsiz = sizeof(struct newah) + siz1;
810 if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
812 * strip off all the headers that precedes AH.
813 * IP6 xx AH IP6' payload -> IP6' payload
815 * XXX more sanity checks
816 * XXX relationship with gif?
818 u_int32_t flowinfo; /*net endian*/
820 flowinfo = ip6->ip6_flow;
821 m_adj(m, off + stripsiz);
822 if (m->m_len < sizeof(*ip6)) {
824 * m_pullup is prohibited in KAME IPv6 input processing
825 * but there's no other way!
827 m = m_pullup(m, sizeof(*ip6));
829 ipsec6stat.in_inval++;
833 ip6 = mtod(m, struct ip6_hdr *);
834 /* ECN consideration. */
835 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
836 if (!key_checktunnelsanity(sav, AF_INET6,
837 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
838 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
839 "in IPv6 AH input: %s %s\n",
840 ipsec6_logpacketstr(ip6, spi),
841 ipsec_logsastr(sav)));
842 ipsec6stat.in_inval++;
846 #if 0 /* XXX should we call ipfw rather than ipsec_in_reject? */
847 /* drop it if it does not match the default policy */
848 if (ipsec6_in_reject(m, NULL)) {
849 ipsec6stat.in_polvio++;
856 * should the inner packet be considered authentic?
857 * see comment in ah4_input().
859 m->m_flags &= ~M_AUTHIPHDR;
860 m->m_flags &= ~M_AUTHIPDGM;
863 key_sa_recordxfer(sav, m);
864 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
865 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
866 ipsec6stat.in_nomem++;
870 if (! IF_HANDOFF(&ip6intrq, m, NULL)) {
871 ipsec6stat.in_inval++;
876 schednetisr(NETISR_IPV6); /*can be skipped but to make sure*/
885 * Copy the value of the next header field of AH to the
886 * next header field of the previous header.
887 * This is necessary because AH will be stripped off below.
889 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
892 ip6 = mtod(m, struct ip6_hdr *);
893 #ifndef PULLDOWN_TEST
895 * We do deep-copy since KAME requires that
896 * the packet is placed in a single mbuf.
898 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
899 m->m_data += stripsiz;
900 m->m_len -= stripsiz;
901 m->m_pkthdr.len -= stripsiz;
904 * even in m_pulldown case, we need to strip off AH so that
905 * we can compute checksum for multiple AH correctly.
907 if (m->m_len >= stripsiz + off) {
908 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
909 m->m_data += stripsiz;
910 m->m_len -= stripsiz;
911 m->m_pkthdr.len -= stripsiz;
914 * this comes with no copy if the boundary is on
919 n = m_split(m, off, M_DONTWAIT);
921 /* m is retained by m_split */
926 /* m_cat does not update m_pkthdr.len */
927 m->m_pkthdr.len += n->m_pkthdr.len;
930 ip6 = mtod(m, struct ip6_hdr *);
932 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
934 key_sa_recordxfer(sav, m);
935 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
936 ipsec6stat.in_nomem++;
945 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
946 printf("DP ah6_input call free SA:%p\n", sav));
949 ipsec6stat.in_success++;
954 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
955 printf("DP ah6_input call free SA:%p\n", sav));
964 ah6_ctlinput(cmd, sa, d)
969 const struct newah *ahp;
971 struct secasvar *sav;
974 struct ip6ctlparam *ip6cp = NULL;
976 struct sockaddr_in6 sa6_src, sa6_dst;
978 if (sa->sa_family != AF_INET6 ||
979 sa->sa_len != sizeof(struct sockaddr_in6))
981 if ((unsigned)cmd >= PRC_NCMDS)
984 /* if the parameter is from icmp6, decode it. */
986 ip6cp = (struct ip6ctlparam *)d;
988 ip6 = ip6cp->ip6c_ip6;
989 off = ip6cp->ip6c_off;
997 * XXX: We assume that when ip6 is non NULL,
998 * M and OFF are valid.
1001 /* check if we can safely examine src and dst ports */
1002 if (m->m_pkthdr.len < off + sizeof(ah))
1005 if (m->m_len < off + sizeof(ah)) {
1007 * this should be rare case,
1008 * so we compromise on this copy...
1010 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1013 ahp = (struct newah *)(mtod(m, caddr_t) + off);
1015 if (cmd == PRC_MSGSIZE) {
1019 * Check to see if we have a valid SA corresponding to
1020 * the address in the ICMP message payload.
1022 sav = key_allocsa(AF_INET6,
1023 (caddr_t)&sa6_src.sin6_addr,
1024 (caddr_t)&sa6_dst.sin6_addr,
1025 IPPROTO_AH, ahp->ah_spi);
1027 if (sav->state == SADB_SASTATE_MATURE ||
1028 sav->state == SADB_SASTATE_DYING)
1033 /* XXX Further validation? */
1036 * Depending on the value of "valid" and routing table
1037 * size (mtudisc_{hi,lo}wat), we will:
1038 * - recalcurate the new MTU and create the
1039 * corresponding routing entry, or
1040 * - ignore the MTU change notification.
1042 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1045 /* we normally notify single pcb here */
1047 /* we normally notify any pcb here */