1 /* $FreeBSD: src/sys/netinet6/esp_input.c,v 1.1.2.8 2003/01/23 21:06:47 sam Exp $ */
2 /* $KAME: esp_input.c,v 1.62 2002/01/07 11:39:57 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 * RFC1827/2406 Encapsulated Security Payload.
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
48 #include <sys/syslog.h>
51 #include <net/route.h>
52 #include <net/netisr.h>
53 #include <machine/cpu.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_ecn.h>
62 #include <netinet6/ip6_ecn.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_pcb.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet/icmp6.h>
70 #include <netinet6/ip6protosw.h>
73 #include <netinet6/ipsec.h>
75 #include <netinet6/ipsec6.h>
77 #include <netinet6/ah.h>
79 #include <netinet6/ah6.h>
81 #include <netinet6/esp.h>
83 #include <netinet6/esp6.h>
85 #include <netkey/key.h>
86 #include <netkey/keydb.h>
87 #include <netkey/key_debug.h>
89 #include <machine/stdarg.h>
91 #include <net/net_osdep.h>
96 (sizeof(struct esp) < sizeof(struct newesp) \
97 ? sizeof(struct newesp) : sizeof(struct esp))
100 #include <netinet/ipprotosw.h>
101 extern struct ipprotosw inetsw[];
105 esp4_input(struct mbuf *m, ...)
107 esp4_input(m, va_alist)
114 struct esptail esptail;
116 struct secasvar *sav = NULL;
119 const struct esp_algorithm *algo;
128 off = va_arg(ap, int);
129 proto = va_arg(ap, int);
132 /* sanity check for alignment. */
133 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
134 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
135 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
136 ipsecstat.in_inval++;
140 if (m->m_len < off + ESPMAXLEN) {
141 m = m_pullup(m, off + ESPMAXLEN);
144 "IPv4 ESP input: can't pullup in esp4_input\n"));
145 ipsecstat.in_inval++;
150 ip = mtod(m, struct ip *);
151 esp = (struct esp *)(((u_int8_t *)ip) + off);
153 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
155 hlen = ip->ip_hl << 2;
158 /* find the sassoc. */
161 if ((sav = key_allocsa(AF_INET,
162 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
163 IPPROTO_ESP, spi)) == 0) {
164 ipseclog((LOG_WARNING,
165 "IPv4 ESP input: no key association found for spi %u\n",
166 (u_int32_t)ntohl(spi)));
170 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
171 printf("DP esp4_input called to allocate SA:%p\n", sav));
172 if (sav->state != SADB_SASTATE_MATURE
173 && sav->state != SADB_SASTATE_DYING) {
175 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
176 (u_int32_t)ntohl(spi)));
177 ipsecstat.in_badspi++;
180 algo = esp_algorithm_lookup(sav->alg_enc);
182 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
183 "unsupported encryption algorithm for spi %u\n",
184 (u_int32_t)ntohl(spi)));
185 ipsecstat.in_badspi++;
189 /* check if we have proper ivlen information */
192 ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
193 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
194 ipsecstat.in_inval++;
198 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
199 && (sav->alg_auth && sav->key_auth)))
202 if (sav->alg_auth == SADB_X_AALG_NULL ||
203 sav->alg_auth == SADB_AALG_NONE)
207 * check for sequence number.
209 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
212 ipsecstat.in_espreplay++;
213 ipseclog((LOG_WARNING,
214 "replay packet in IPv4 ESP input: %s %s\n",
215 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
221 u_char sum0[AH_MAXSUMSIZE];
222 u_char sum[AH_MAXSUMSIZE];
223 const struct ah_algorithm *sumalgo;
226 sumalgo = ah_algorithm_lookup(sav->alg_auth);
229 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
230 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
231 ipsecstat.in_inval++;
234 if (AH_MAXSUMSIZE < siz) {
236 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
238 ipsecstat.in_inval++;
242 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
244 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
245 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
246 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
247 ipsecstat.in_espauthfail++;
251 if (bcmp(sum0, sum, siz) != 0) {
252 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
253 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
254 ipsecstat.in_espauthfail++;
258 /* strip off the authentication data */
260 ip = mtod(m, struct ip *);
262 ip->ip_len = ip->ip_len - siz;
264 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
266 m->m_flags |= M_AUTHIPDGM;
267 ipsecstat.in_espauthsucc++;
271 * update sequence number.
273 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
274 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
275 ipsecstat.in_espreplay++;
282 /* process main esp header. */
283 if (sav->flags & SADB_X_EXT_OLD) {
285 esplen = sizeof(struct esp);
288 if (sav->flags & SADB_X_EXT_DERIV)
289 esplen = sizeof(struct esp);
291 esplen = sizeof(struct newesp);
294 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
295 ipseclog((LOG_WARNING,
296 "IPv4 ESP input: packet too short\n"));
297 ipsecstat.in_inval++;
301 if (m->m_len < off + esplen + ivlen) {
302 m = m_pullup(m, off + esplen + ivlen);
305 "IPv4 ESP input: can't pullup in esp4_input\n"));
306 ipsecstat.in_inval++;
312 * pre-compute and cache intermediate key
314 if (esp_schedule(algo, sav) != 0) {
315 ipsecstat.in_inval++;
320 * decrypt the packet.
323 panic("internal error: no decrypt function");
324 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
325 /* m is already freed */
327 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
328 ipsec_logsastr(sav)));
329 ipsecstat.in_inval++;
332 ipsecstat.in_esphist[sav->alg_enc]++;
334 m->m_flags |= M_DECRYPTED;
337 * find the trailer of the ESP.
339 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
341 nxt = esptail.esp_nxt;
342 taillen = esptail.esp_padlen + sizeof(esptail);
344 if (m->m_pkthdr.len < taillen
345 || m->m_pkthdr.len - taillen < hlen) { /* ? */
346 ipseclog((LOG_WARNING,
347 "bad pad length in IPv4 ESP input: %s %s\n",
348 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
349 ipsecstat.in_inval++;
353 /* strip off the trailing pad area. */
357 ip->ip_len = ip->ip_len - taillen;
359 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
362 /* was it transmitted over the IPsec tunnel SA? */
363 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
365 * strip off all the headers that precedes ESP header.
366 * IP4 xx ESP IP4' payload -> IP4' payload
368 * XXX more sanity checks
369 * XXX relationship with gif?
374 m_adj(m, off + esplen + ivlen);
375 if (m->m_len < sizeof(*ip)) {
376 m = m_pullup(m, sizeof(*ip));
378 ipsecstat.in_inval++;
382 ip = mtod(m, struct ip *);
383 /* ECN consideration. */
384 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
385 if (!key_checktunnelsanity(sav, AF_INET,
386 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
387 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
388 "in IPv4 ESP input: %s %s\n",
389 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
390 ipsecstat.in_inval++;
394 key_sa_recordxfer(sav, m);
395 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
396 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
397 ipsecstat.in_nomem++;
402 if (IF_QFULL(&ipintrq)) {
403 ipsecstat.in_inval++;
407 IF_ENQUEUE(&ipintrq, m);
409 schednetisr(NETISR_IP); /* can be skipped but to make sure */
414 * strip off ESP header and IV.
415 * even in m_pulldown case, we need to strip off ESP so that
416 * we can always compute checksum for AH correctly.
420 stripsiz = esplen + ivlen;
422 ip = mtod(m, struct ip *);
423 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
424 m->m_data += stripsiz;
425 m->m_len -= stripsiz;
426 m->m_pkthdr.len -= stripsiz;
428 ip = mtod(m, struct ip *);
430 ip->ip_len = ip->ip_len - stripsiz;
432 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
436 key_sa_recordxfer(sav, m);
437 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
438 ipsecstat.in_nomem++;
442 if (nxt != IPPROTO_DONE) {
443 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
444 ipsec4_in_reject(m, NULL)) {
445 ipsecstat.in_polvio++;
448 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
455 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
456 printf("DP esp4_input call free SA:%p\n", sav));
459 ipsecstat.in_success++;
464 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
465 printf("DP esp4_input call free SA:%p\n", sav));
476 esp6_input(mp, offp, proto)
480 struct mbuf *m = *mp;
484 struct esptail esptail;
486 struct secasvar *sav = NULL;
489 const struct esp_algorithm *algo;
494 /* sanity check for alignment. */
495 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
496 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
497 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
498 ipsec6stat.in_inval++;
502 #ifndef PULLDOWN_TEST
503 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
504 esp = (struct esp *)(mtod(m, caddr_t) + off);
506 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
508 ipsec6stat.in_inval++;
512 ip6 = mtod(m, struct ip6_hdr *);
514 if (ntohs(ip6->ip6_plen) == 0) {
515 ipseclog((LOG_ERR, "IPv6 ESP input: "
516 "ESP with IPv6 jumbogram is not supported.\n"));
517 ipsec6stat.in_inval++;
521 /* find the sassoc. */
524 if ((sav = key_allocsa(AF_INET6,
525 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
526 IPPROTO_ESP, spi)) == 0) {
527 ipseclog((LOG_WARNING,
528 "IPv6 ESP input: no key association found for spi %u\n",
529 (u_int32_t)ntohl(spi)));
530 ipsec6stat.in_nosa++;
533 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
534 printf("DP esp6_input called to allocate SA:%p\n", sav));
535 if (sav->state != SADB_SASTATE_MATURE
536 && sav->state != SADB_SASTATE_DYING) {
538 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
539 (u_int32_t)ntohl(spi)));
540 ipsec6stat.in_badspi++;
543 algo = esp_algorithm_lookup(sav->alg_enc);
545 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
546 "unsupported encryption algorithm for spi %u\n",
547 (u_int32_t)ntohl(spi)));
548 ipsec6stat.in_badspi++;
552 /* check if we have proper ivlen information */
555 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
556 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
557 ipsec6stat.in_badspi++;
561 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
562 && (sav->alg_auth && sav->key_auth)))
565 if (sav->alg_auth == SADB_X_AALG_NULL ||
566 sav->alg_auth == SADB_AALG_NONE)
570 * check for sequence number.
572 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
575 ipsec6stat.in_espreplay++;
576 ipseclog((LOG_WARNING,
577 "replay packet in IPv6 ESP input: %s %s\n",
578 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
584 u_char sum0[AH_MAXSUMSIZE];
585 u_char sum[AH_MAXSUMSIZE];
586 const struct ah_algorithm *sumalgo;
589 sumalgo = ah_algorithm_lookup(sav->alg_auth);
592 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
593 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
594 ipsecstat.in_inval++;
597 if (AH_MAXSUMSIZE < siz) {
599 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
601 ipsec6stat.in_inval++;
605 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
607 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
608 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
609 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
610 ipsec6stat.in_espauthfail++;
614 if (bcmp(sum0, sum, siz) != 0) {
615 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
616 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
617 ipsec6stat.in_espauthfail++;
621 /* strip off the authentication data */
623 ip6 = mtod(m, struct ip6_hdr *);
624 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
626 m->m_flags |= M_AUTHIPDGM;
627 ipsec6stat.in_espauthsucc++;
631 * update sequence number.
633 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
634 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
635 ipsec6stat.in_espreplay++;
642 /* process main esp header. */
643 if (sav->flags & SADB_X_EXT_OLD) {
645 esplen = sizeof(struct esp);
648 if (sav->flags & SADB_X_EXT_DERIV)
649 esplen = sizeof(struct esp);
651 esplen = sizeof(struct newesp);
654 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
655 ipseclog((LOG_WARNING,
656 "IPv6 ESP input: packet too short\n"));
657 ipsec6stat.in_inval++;
661 #ifndef PULLDOWN_TEST
662 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
664 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
666 ipsec6stat.in_inval++;
671 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
674 * pre-compute and cache intermediate key
676 if (esp_schedule(algo, sav) != 0) {
677 ipsec6stat.in_inval++;
682 * decrypt the packet.
685 panic("internal error: no decrypt function");
686 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
687 /* m is already freed */
689 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
690 ipsec_logsastr(sav)));
691 ipsec6stat.in_inval++;
694 ipsec6stat.in_esphist[sav->alg_enc]++;
696 m->m_flags |= M_DECRYPTED;
699 * find the trailer of the ESP.
701 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
703 nxt = esptail.esp_nxt;
704 taillen = esptail.esp_padlen + sizeof(esptail);
706 if (m->m_pkthdr.len < taillen
707 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
708 ipseclog((LOG_WARNING,
709 "bad pad length in IPv6 ESP input: %s %s\n",
710 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
711 ipsec6stat.in_inval++;
715 /* strip off the trailing pad area. */
718 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
720 /* was it transmitted over the IPsec tunnel SA? */
721 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
723 * strip off all the headers that precedes ESP header.
724 * IP6 xx ESP IP6' payload -> IP6' payload
726 * XXX more sanity checks
727 * XXX relationship with gif?
729 u_int32_t flowinfo; /* net endian */
730 flowinfo = ip6->ip6_flow;
731 m_adj(m, off + esplen + ivlen);
732 if (m->m_len < sizeof(*ip6)) {
733 #ifndef PULLDOWN_TEST
735 * m_pullup is prohibited in KAME IPv6 input processing
736 * but there's no other way!
739 /* okay to pullup in m_pulldown style */
741 m = m_pullup(m, sizeof(*ip6));
743 ipsec6stat.in_inval++;
747 ip6 = mtod(m, struct ip6_hdr *);
748 /* ECN consideration. */
749 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
750 if (!key_checktunnelsanity(sav, AF_INET6,
751 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
752 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
753 "in IPv6 ESP input: %s %s\n",
754 ipsec6_logpacketstr(ip6, spi),
755 ipsec_logsastr(sav)));
756 ipsec6stat.in_inval++;
760 key_sa_recordxfer(sav, m);
761 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
762 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
763 ipsec6stat.in_nomem++;
768 if (IF_QFULL(&ip6intrq)) {
769 ipsec6stat.in_inval++;
773 IF_ENQUEUE(&ip6intrq, m);
775 schednetisr(NETISR_IPV6); /* can be skipped but to make sure */
780 * strip off ESP header and IV.
781 * even in m_pulldown case, we need to strip off ESP so that
782 * we can always compute checksum for AH correctly.
788 * Set the next header field of the previous header correctly.
790 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
793 stripsiz = esplen + ivlen;
795 ip6 = mtod(m, struct ip6_hdr *);
796 if (m->m_len >= stripsiz + off) {
797 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
798 m->m_data += stripsiz;
799 m->m_len -= stripsiz;
800 m->m_pkthdr.len -= stripsiz;
803 * this comes with no copy if the boundary is on
808 n = m_split(m, off, M_DONTWAIT);
810 /* m is retained by m_split */
815 /* m_cat does not update m_pkthdr.len */
816 m->m_pkthdr.len += n->m_pkthdr.len;
819 #ifndef PULLDOWN_TEST
821 * KAME requires that the packet to be contiguous on the
822 * mbuf. We need to make that sure.
823 * this kind of code should be avoided.
824 * XXX other conditions to avoid running this part?
826 if (m->m_len != m->m_pkthdr.len) {
827 struct mbuf *n = NULL;
830 MGETHDR(n, M_DONTWAIT, MT_HEADER);
834 if (n && n->m_pkthdr.len > maxlen) {
835 MCLGET(n, M_DONTWAIT);
837 if ((n->m_flags & M_EXT) == 0) {
843 printf("esp6_input: mbuf allocation failed\n");
847 if (n->m_pkthdr.len <= maxlen) {
848 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
849 n->m_len = n->m_pkthdr.len;
853 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
862 ip6 = mtod(m, struct ip6_hdr *);
863 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
865 key_sa_recordxfer(sav, m);
866 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
867 ipsec6stat.in_nomem++;
876 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
877 printf("DP esp6_input call free SA:%p\n", sav));
880 ipsec6stat.in_success++;
885 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
886 printf("DP esp6_input call free SA:%p\n", sav));
895 esp6_ctlinput(cmd, sa, d)
900 const struct newesp *espp;
902 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
903 struct secasvar *sav;
907 struct sockaddr_in6 *sa6_src, *sa6_dst;
909 if (sa->sa_family != AF_INET6 ||
910 sa->sa_len != sizeof(struct sockaddr_in6))
912 if ((unsigned)cmd >= PRC_NCMDS)
915 /* if the parameter is from icmp6, decode it. */
917 ip6cp = (struct ip6ctlparam *)d;
919 ip6 = ip6cp->ip6c_ip6;
920 off = ip6cp->ip6c_off;
928 * Notify the error to all possible sockets via pfctlinput2.
929 * Since the upper layer information (such as protocol type,
930 * source and destination ports) is embedded in the encrypted
931 * data and might have been cut, we can't directly call
932 * an upper layer ctlinput function. However, the pcbnotify
933 * function will consider source and destination addresses
934 * as well as the flow info value, and may be able to find
935 * some PCB that should be notified.
936 * Although pfctlinput2 will call esp6_ctlinput(), there is
937 * no possibility of an infinite loop of function calls,
938 * because we don't pass the inner IPv6 header.
940 bzero(&ip6cp1, sizeof(ip6cp1));
941 ip6cp1.ip6c_src = ip6cp->ip6c_src;
942 pfctlinput2(cmd, sa, (void *)&ip6cp1);
945 * Then go to special cases that need ESP header information.
946 * XXX: We assume that when ip6 is non NULL,
947 * M and OFF are valid.
950 /* check if we can safely examine src and dst ports */
951 if (m->m_pkthdr.len < off + sizeof(esp))
954 if (m->m_len < off + sizeof(esp)) {
956 * this should be rare case,
957 * so we compromise on this copy...
959 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
962 espp = (struct newesp*)(mtod(m, caddr_t) + off);
964 if (cmd == PRC_MSGSIZE) {
968 * Check to see if we have a valid SA corresponding to
969 * the address in the ICMP message payload.
971 sa6_src = ip6cp->ip6c_src;
972 sa6_dst = (struct sockaddr_in6 *)sa;
973 sav = key_allocsa(AF_INET6,
974 (caddr_t)&sa6_src->sin6_addr,
975 (caddr_t)&sa6_dst->sin6_addr,
976 IPPROTO_ESP, espp->esp_spi);
978 if (sav->state == SADB_SASTATE_MATURE ||
979 sav->state == SADB_SASTATE_DYING)
984 /* XXX Further validation? */
987 * Depending on the value of "valid" and routing table
988 * size (mtudisc_{hi,lo}wat), we will:
989 * - recalcurate the new MTU and create the
990 * corresponding routing entry, or
991 * - ignore the MTU change notification.
993 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
996 /* we normally notify any pcb here */