1 /* $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/ah_input.c,v 1.14 2007/11/26 11:43:09 sephe Exp $ */
3 /* $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 kjc Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * RFC1826/2402 authentication header.
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
50 #include <sys/syslog.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
55 #include <machine/cpu.h>
56 #include <machine/stdarg.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_ecn.h>
65 #include <netinet6/ip6_ecn.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/in6_pcb.h>
72 #include <netinet/icmp6.h>
73 #include <netinet6/ip6protosw.h>
76 #include <netinet6/ipsec.h>
78 #include <netinet6/ipsec6.h>
80 #include <netinet6/ah.h>
82 #include <netinet6/ah6.h>
84 #include <netproto/key/key.h>
85 #include <netproto/key/keydb.h>
87 #include <netproto/key/key_debug.h>
89 #define KEYDEBUG(lev,arg)
92 #include <machine/stdarg.h>
94 #include <net/net_osdep.h>
99 extern struct protosw inetsw[];
102 ah4_input(struct mbuf **mp, int *offp, int proto)
109 const struct ah_algorithm *algo;
113 struct secasvar *sav = NULL;
122 #ifndef PULLDOWN_TEST
123 if (m->m_len < off + sizeof(struct newah)) {
124 m = m_pullup(m, off + sizeof(struct newah));
126 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
127 "dropping the packet for simplicity\n"));
128 ipsecstat.in_inval++;
133 ip = mtod(m, struct ip *);
134 ah = (struct ah *)(((caddr_t)ip) + off);
136 ip = mtod(m, struct ip *);
137 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
139 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
140 "dropping the packet for simplicity\n"));
141 ipsecstat.in_inval++;
147 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
149 hlen = ip->ip_hl << 2;
152 /* find the sassoc. */
155 if ((sav = key_allocsa(AF_INET,
156 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
157 IPPROTO_AH, spi)) == 0) {
158 ipseclog((LOG_WARNING,
159 "IPv4 AH input: no key association found for spi %u\n",
160 (u_int32_t)ntohl(spi)));
164 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
165 kprintf("DP ah4_input called to allocate SA:%p\n", sav));
166 if (sav->state != SADB_SASTATE_MATURE
167 && sav->state != SADB_SASTATE_DYING) {
169 "IPv4 AH input: non-mature/dying SA found for spi %u\n",
170 (u_int32_t)ntohl(spi)));
171 ipsecstat.in_badspi++;
175 algo = ah_algorithm_lookup(sav->alg_auth);
177 ipseclog((LOG_DEBUG, "IPv4 AH input: "
178 "unsupported authentication algorithm for spi %u\n",
179 (u_int32_t)ntohl(spi)));
180 ipsecstat.in_badspi++;
184 siz = (*algo->sumsiz)(sav);
185 siz1 = ((siz + 3) & ~(4 - 1));
188 * sanity checks for header, 1.
193 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
196 * Here, we do not do "siz1 == siz". This is because the way
197 * RFC240[34] section 2 is written. They do not require truncation
199 * For example, Microsoft IPsec stack attaches 160 bits of
200 * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1,
201 * 32 bits of padding is attached.
203 * There are two downsides to this specification.
204 * They have no real harm, however, they leave us fuzzy feeling.
205 * - if we attach more than 96 bits of authentication data onto AH,
206 * we will never notice about possible modification by rogue
207 * intermediate nodes.
208 * Since extra bits in AH checksum is never used, this constitutes
209 * no real issue, however, it is wacky.
210 * - even if the peer attaches big authentication data, we will never
211 * notice the difference, since longer authentication data will just
214 * We may need some clarification in the spec.
217 ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input "
218 "(%lu, should be at least %lu): %s\n",
219 (u_long)siz1, (u_long)siz,
220 ipsec4_logpacketstr(ip, spi)));
221 ipsecstat.in_inval++;
224 if ((ah->ah_len << 2) - sizoff != siz1) {
225 ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input "
226 "(%d should be %lu): %s\n",
227 (ah->ah_len << 2) - sizoff, (u_long)siz1,
228 ipsec4_logpacketstr(ip, spi)));
229 ipsecstat.in_inval++;
233 #ifndef PULLDOWN_TEST
234 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) {
235 m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1);
237 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
238 ipsecstat.in_inval++;
242 ip = mtod(m, struct ip *);
243 ah = (struct ah *)(((caddr_t)ip) + off);
246 IP6_EXTHDR_GET(ah, struct ah *, m, off,
247 sizeof(struct ah) + sizoff + siz1);
249 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
250 ipsecstat.in_inval++;
257 * check for sequence number.
259 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
260 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
263 ipsecstat.in_ahreplay++;
264 ipseclog((LOG_WARNING,
265 "replay packet in IPv4 AH input: %s %s\n",
266 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
272 * alright, it seems sane. now we are going to check the
273 * cryptographic checksum.
275 cksum = kmalloc(siz1, M_TEMP, M_NOWAIT);
277 ipseclog((LOG_DEBUG, "IPv4 AH input: "
278 "couldn't alloc temporary region for cksum\n"));
279 ipsecstat.in_inval++;
284 * some of IP header fields are flipped to the host endian.
285 * convert them back to network endian. VERY stupid.
287 ip->ip_len = htons(ip->ip_len + hlen);
288 ip->ip_off = htons(ip->ip_off);
289 if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
290 kfree(cksum, M_TEMP);
291 ipsecstat.in_inval++;
294 ipsecstat.in_ahhist[sav->alg_auth]++;
298 ip->ip_len = ntohs(ip->ip_len) - hlen;
299 ip->ip_off = ntohs(ip->ip_off);
302 caddr_t sumpos = NULL;
304 if (sav->flags & SADB_X_EXT_OLD) {
306 sumpos = (caddr_t)(ah + 1);
309 sumpos = (caddr_t)(((struct newah *)ah) + 1);
312 if (bcmp(sumpos, cksum, siz) != 0) {
313 ipseclog((LOG_WARNING,
314 "checksum mismatch in IPv4 AH input: %s %s\n",
315 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
316 kfree(cksum, M_TEMP);
317 ipsecstat.in_ahauthfail++;
322 kfree(cksum, M_TEMP);
324 m->m_flags |= M_AUTHIPHDR;
325 m->m_flags |= M_AUTHIPDGM;
329 * looks okey, but we need more sanity check.
330 * XXX should elaborate.
332 if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) {
336 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
338 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) {
339 m = m_pullup(m, off + sizeof(struct ah)
340 + sizoff + siz1 + hlen);
343 "IPv4 AH input: can't pullup\n"));
344 ipsecstat.in_inval++;
349 nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1);
350 if (nip->ip_src.s_addr != ip->ip_src.s_addr
351 || nip->ip_dst.s_addr != ip->ip_dst.s_addr) {
352 m->m_flags &= ~M_AUTHIPHDR;
353 m->m_flags &= ~M_AUTHIPDGM;
357 else if (ah->ah_nxt == IPPROTO_IPV6) {
358 m->m_flags &= ~M_AUTHIPHDR;
359 m->m_flags &= ~M_AUTHIPDGM;
364 if (m->m_flags & M_AUTHIPHDR
365 && m->m_flags & M_AUTHIPDGM) {
368 "IPv4 AH input: authentication succeess\n"));
370 ipsecstat.in_ahauthsucc++;
372 ipseclog((LOG_WARNING,
373 "authentication failed in IPv4 AH input: %s %s\n",
374 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
375 ipsecstat.in_ahauthfail++;
380 * update sequence number.
382 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
383 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
384 ipsecstat.in_ahreplay++;
389 /* was it transmitted over the IPsec tunnel SA? */
390 if (sav->flags & SADB_X_EXT_OLD) {
392 stripsiz = sizeof(struct ah) + siz1;
395 stripsiz = sizeof(struct newah) + siz1;
397 if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) {
399 * strip off all the headers that precedes AH.
400 * IP xx AH IP' payload -> IP' payload
402 * XXX more sanity checks
403 * XXX relationship with gif?
408 m_adj(m, off + stripsiz);
409 if (m->m_len < sizeof(*ip)) {
410 m = m_pullup(m, sizeof(*ip));
412 ipsecstat.in_inval++;
416 ip = mtod(m, struct ip *);
417 /* ECN consideration. */
418 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
419 if (!key_checktunnelsanity(sav, AF_INET,
420 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
421 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
422 "in IPv4 AH input: %s %s\n",
423 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
424 ipsecstat.in_inval++;
430 * Should the inner packet be considered authentic?
431 * My current answer is: NO.
433 * host1 -- gw1 === gw2 -- host2
434 * In this case, gw2 can trust the authenticity of the
435 * outer packet, but NOT inner. Packet may be altered
436 * between host1 and gw1.
438 * host1 -- gw1 === host2
439 * This case falls into the same scenario as above.
442 * This case is the only case when we may be able to leave
443 * M_AUTHIPHDR and M_AUTHIPDGM set.
444 * However, if host1 is wrongly configured, and allows
445 * attacker to inject some packet with src=host1 and
446 * dst=host2, you are in risk.
448 m->m_flags &= ~M_AUTHIPHDR;
449 m->m_flags &= ~M_AUTHIPDGM;
452 key_sa_recordxfer(sav, m);
453 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
454 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
455 ipsecstat.in_nomem++;
459 if (netisr_queue(NETISR_IP, m)) {
460 ipsecstat.in_inval++;
471 ip = mtod(m, struct ip *);
472 #ifndef PULLDOWN_TEST
474 * We do deep-copy since KAME requires that
475 * the packet is placed in a single external mbuf.
477 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
478 m->m_data += stripsiz;
479 m->m_len -= stripsiz;
480 m->m_pkthdr.len -= stripsiz;
483 * even in m_pulldown case, we need to strip off AH so that
484 * we can compute checksum for multiple AH correctly.
486 if (m->m_len >= stripsiz + off) {
487 ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
488 m->m_data += stripsiz;
489 m->m_len -= stripsiz;
490 m->m_pkthdr.len -= stripsiz;
493 * this comes with no copy if the boundary is on
498 n = m_split(m, off, MB_DONTWAIT);
500 /* m is retained by m_split */
504 /* m_cat does not update m_pkthdr.len */
505 m->m_pkthdr.len += n->m_pkthdr.len;
510 if (m->m_len < sizeof(*ip)) {
511 m = m_pullup(m, sizeof(*ip));
513 ipsecstat.in_inval++;
517 ip = mtod(m, struct ip *);
519 ip->ip_len = ip->ip_len - stripsiz;
521 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
524 /* forget about IP hdr checksum, the check has already been passed */
526 key_sa_recordxfer(sav, m);
527 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
528 ipsecstat.in_nomem++;
532 if (nxt != IPPROTO_DONE) {
533 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) &&
534 ipsec4_in_reject(m, NULL)) {
535 ipsecstat.in_polvio++;
538 if (!ip_lengthcheck(&m, 0)) {
539 /* freed in ip_lengthcheck() */
544 (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
552 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
553 kprintf("DP ah4_input call free SA:%p\n", sav));
556 ipsecstat.in_success++;
557 return(IPPROTO_DONE);
561 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
562 kprintf("DP ah4_input call free SA:%p\n", sav));
567 return(IPPROTO_DONE);
573 ah6_input(struct mbuf **mp, int *offp, int proto)
575 struct mbuf *m = *mp;
580 const struct ah_algorithm *algo;
584 struct secasvar *sav = NULL;
588 #ifndef PULLDOWN_TEST
589 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
590 ah = (struct ah *)(mtod(m, caddr_t) + off);
592 IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
594 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
595 ipsec6stat.in_inval++;
599 ip6 = mtod(m, struct ip6_hdr *);
602 /* find the sassoc. */
605 if (ntohs(ip6->ip6_plen) == 0) {
606 ipseclog((LOG_ERR, "IPv6 AH input: "
607 "AH with IPv6 jumbogram is not supported.\n"));
608 ipsec6stat.in_inval++;
612 if ((sav = key_allocsa(AF_INET6,
613 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
614 IPPROTO_AH, spi)) == 0) {
615 ipseclog((LOG_WARNING,
616 "IPv6 AH input: no key association found for spi %u\n",
617 (u_int32_t)ntohl(spi)));
618 ipsec6stat.in_nosa++;
621 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
622 kprintf("DP ah6_input called to allocate SA:%p\n", sav));
623 if (sav->state != SADB_SASTATE_MATURE
624 && sav->state != SADB_SASTATE_DYING) {
626 "IPv6 AH input: non-mature/dying SA found for spi %u; ",
627 (u_int32_t)ntohl(spi)));
628 ipsec6stat.in_badspi++;
632 algo = ah_algorithm_lookup(sav->alg_auth);
634 ipseclog((LOG_DEBUG, "IPv6 AH input: "
635 "unsupported authentication algorithm for spi %u\n",
636 (u_int32_t)ntohl(spi)));
637 ipsec6stat.in_badspi++;
641 siz = (*algo->sumsiz)(sav);
642 siz1 = ((siz + 3) & ~(4 - 1));
645 * sanity checks for header, 1.
650 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
653 * Here, we do not do "siz1 == siz". See ah4_input() for complete
657 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
658 "(%lu, should be at least %lu): %s\n",
659 (u_long)siz1, (u_long)siz,
660 ipsec6_logpacketstr(ip6, spi)));
661 ipsec6stat.in_inval++;
664 if ((ah->ah_len << 2) - sizoff != siz1) {
665 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
666 "(%d should be %lu): %s\n",
667 (ah->ah_len << 2) - sizoff, (u_long)siz1,
668 ipsec6_logpacketstr(ip6, spi)));
669 ipsec6stat.in_inval++;
672 #ifndef PULLDOWN_TEST
673 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
675 IP6_EXTHDR_GET(ah, struct ah *, m, off,
676 sizeof(struct ah) + sizoff + siz1);
678 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
679 ipsec6stat.in_inval++;
687 * check for sequence number.
689 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
690 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
693 ipsec6stat.in_ahreplay++;
694 ipseclog((LOG_WARNING,
695 "replay packet in IPv6 AH input: %s %s\n",
696 ipsec6_logpacketstr(ip6, spi),
697 ipsec_logsastr(sav)));
703 * alright, it seems sane. now we are going to check the
704 * cryptographic checksum.
706 cksum = kmalloc(siz1, M_TEMP, M_NOWAIT);
708 ipseclog((LOG_DEBUG, "IPv6 AH input: "
709 "couldn't alloc temporary region for cksum\n"));
710 ipsec6stat.in_inval++;
714 if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
715 kfree(cksum, M_TEMP);
716 ipsec6stat.in_inval++;
719 ipsec6stat.in_ahhist[sav->alg_auth]++;
722 caddr_t sumpos = NULL;
724 if (sav->flags & SADB_X_EXT_OLD) {
726 sumpos = (caddr_t)(ah + 1);
729 sumpos = (caddr_t)(((struct newah *)ah) + 1);
732 if (bcmp(sumpos, cksum, siz) != 0) {
733 ipseclog((LOG_WARNING,
734 "checksum mismatch in IPv6 AH input: %s %s\n",
735 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
736 kfree(cksum, M_TEMP);
737 ipsec6stat.in_ahauthfail++;
742 kfree(cksum, M_TEMP);
744 m->m_flags |= M_AUTHIPHDR;
745 m->m_flags |= M_AUTHIPDGM;
749 * looks okey, but we need more sanity check.
750 * XXX should elaborate.
752 if (ah->ah_nxt == IPPROTO_IPV6) {
753 struct ip6_hdr *nip6;
756 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
758 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
759 + sizeof(struct ip6_hdr), IPPROTO_DONE);
761 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
762 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
763 || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
764 m->m_flags &= ~M_AUTHIPHDR;
765 m->m_flags &= ~M_AUTHIPDGM;
767 } else if (ah->ah_nxt == IPPROTO_IPIP) {
768 m->m_flags &= ~M_AUTHIPHDR;
769 m->m_flags &= ~M_AUTHIPDGM;
770 } else if (ah->ah_nxt == IPPROTO_IP) {
771 m->m_flags &= ~M_AUTHIPHDR;
772 m->m_flags &= ~M_AUTHIPDGM;
776 if (m->m_flags & M_AUTHIPHDR
777 && m->m_flags & M_AUTHIPDGM) {
780 "IPv6 AH input: authentication succeess\n"));
782 ipsec6stat.in_ahauthsucc++;
784 ipseclog((LOG_WARNING,
785 "authentication failed in IPv6 AH input: %s %s\n",
786 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
787 ipsec6stat.in_ahauthfail++;
792 * update sequence number.
794 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
795 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
796 ipsec6stat.in_ahreplay++;
801 /* was it transmitted over the IPsec tunnel SA? */
802 if (sav->flags & SADB_X_EXT_OLD) {
804 stripsiz = sizeof(struct ah) + siz1;
807 stripsiz = sizeof(struct newah) + siz1;
809 if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
811 * strip off all the headers that precedes AH.
812 * IP6 xx AH IP6' payload -> IP6' payload
814 * XXX more sanity checks
815 * XXX relationship with gif?
817 u_int32_t flowinfo; /* net endian */
819 flowinfo = ip6->ip6_flow;
820 m_adj(m, off + stripsiz);
821 if (m->m_len < sizeof(*ip6)) {
823 * m_pullup is prohibited in KAME IPv6 input processing
824 * but there's no other way!
826 m = m_pullup(m, sizeof(*ip6));
828 ipsec6stat.in_inval++;
832 ip6 = mtod(m, struct ip6_hdr *);
833 /* ECN consideration. */
834 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
835 if (!key_checktunnelsanity(sav, AF_INET6,
836 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
837 ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
838 "in IPv6 AH input: %s %s\n",
839 ipsec6_logpacketstr(ip6, spi),
840 ipsec_logsastr(sav)));
841 ipsec6stat.in_inval++;
847 * should the inner packet be considered authentic?
848 * see comment in ah4_input().
850 m->m_flags &= ~M_AUTHIPHDR;
851 m->m_flags &= ~M_AUTHIPDGM;
854 key_sa_recordxfer(sav, m);
855 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
856 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
857 ipsec6stat.in_nomem++;
861 if (netisr_queue(NETISR_IPV6, m)) {
862 ipsec6stat.in_inval++;
875 * Copy the value of the next header field of AH to the
876 * next header field of the previous header.
877 * This is necessary because AH will be stripped off below.
879 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
882 ip6 = mtod(m, struct ip6_hdr *);
883 #ifndef PULLDOWN_TEST
885 * We do deep-copy since KAME requires that
886 * the packet is placed in a single mbuf.
888 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
889 m->m_data += stripsiz;
890 m->m_len -= stripsiz;
891 m->m_pkthdr.len -= stripsiz;
894 * even in m_pulldown case, we need to strip off AH so that
895 * we can compute checksum for multiple AH correctly.
897 if (m->m_len >= stripsiz + off) {
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 * this comes with no copy if the boundary is on
909 n = m_split(m, off, MB_DONTWAIT);
911 /* m is retained by m_split */
915 /* m_cat does not update m_pkthdr.len */
916 m->m_pkthdr.len += n->m_pkthdr.len;
920 ip6 = mtod(m, struct ip6_hdr *);
922 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
924 key_sa_recordxfer(sav, m);
925 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
926 ipsec6stat.in_nomem++;
935 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
936 kprintf("DP ah6_input call free SA:%p\n", sav));
939 ipsec6stat.in_success++;
944 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
945 kprintf("DP ah6_input call free SA:%p\n", sav));
954 ah6_ctlinput(int cmd, struct sockaddr *sa, void *d)
956 const struct newah *ahp;
958 struct secasvar *sav;
961 struct ip6ctlparam *ip6cp = NULL;
963 struct sockaddr_in6 *sa6_src, *sa6_dst;
965 if (sa->sa_family != AF_INET6 ||
966 sa->sa_len != sizeof(struct sockaddr_in6))
968 if ((unsigned)cmd >= PRC_NCMDS)
971 /* if the parameter is from icmp6, decode it. */
973 ip6cp = (struct ip6ctlparam *)d;
975 ip6 = ip6cp->ip6c_ip6;
976 off = ip6cp->ip6c_off;
980 off = 0; /* fix warning */
985 * XXX: We assume that when ip6 is non NULL,
986 * M and OFF are valid.
989 /* check if we can safely examine src and dst ports */
990 if (m->m_pkthdr.len < off + sizeof(ah))
993 if (m->m_len < off + sizeof(ah)) {
995 * this should be rare case,
996 * so we compromise on this copy...
998 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1001 ahp = (struct newah *)(mtod(m, caddr_t) + off);
1003 if (cmd == PRC_MSGSIZE) {
1007 * Check to see if we have a valid SA corresponding to
1008 * the address in the ICMP message payload.
1010 sa6_src = ip6cp->ip6c_src;
1011 sa6_dst = (struct sockaddr_in6 *)sa;
1012 sav = key_allocsa(AF_INET6,
1013 (caddr_t)&sa6_src->sin6_addr,
1014 (caddr_t)&sa6_dst->sin6_addr,
1015 IPPROTO_AH, ahp->ah_spi);
1017 if (sav->state == SADB_SASTATE_MATURE ||
1018 sav->state == SADB_SASTATE_DYING)
1023 /* XXX Further validation? */
1026 * Depending on the value of "valid" and routing table
1027 * size (mtudisc_{hi,lo}wat), we will:
1028 * - recalcurate the new MTU and create the
1029 * corresponding routing entry, or
1030 * - ignore the MTU change notification.
1032 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1035 /* we normally notify single pcb here */
1037 /* we normally notify any pcb here */