2 /* $KAME: esp_input.c,v 1.55 2001/03/23 08:08:47 itojun 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 <netinet/in_pcb.h>
68 #include <netinet6/in6_pcb.h>
69 #include <netinet6/ip6_var.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 <netinet6/esp.h>
84 #include <netinet6/esp6.h>
86 #include <netkey/key.h>
87 #include <netkey/keydb.h>
88 #include <netkey/key_debug.h>
90 #include <machine/stdarg.h>
92 #include <net/net_osdep.h>
97 (sizeof(struct esp) < sizeof(struct newesp) \
98 ? sizeof(struct newesp) : sizeof(struct esp))
101 extern struct protosw inetsw[];
110 struct esptail esptail;
112 struct secasvar *sav = NULL;
115 const struct esp_algorithm *algo;
121 /* sanity check for alignment. */
122 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
123 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
124 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
125 ipsecstat.in_inval++;
129 if (m->m_len < off + ESPMAXLEN) {
130 m = m_pullup(m, off + ESPMAXLEN);
133 "IPv4 ESP input: can't pullup in esp4_input\n"));
134 ipsecstat.in_inval++;
139 ip = mtod(m, struct ip *);
141 esp = (struct esp *)(((u_int8_t *)ip) + off);
143 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
145 hlen = ip->ip_hl << 2;
148 /* find the sassoc. */
151 if ((sav = key_allocsa(AF_INET,
152 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
153 IPPROTO_ESP, spi)) == 0) {
154 ipseclog((LOG_WARNING,
155 "IPv4 ESP input: no key association found for spi %u\n",
156 (u_int32_t)ntohl(spi)));
160 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
161 printf("DP esp4_input called to allocate SA:%p\n", sav));
162 if (sav->state != SADB_SASTATE_MATURE
163 && sav->state != SADB_SASTATE_DYING) {
165 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
166 (u_int32_t)ntohl(spi)));
167 ipsecstat.in_badspi++;
170 algo = esp_algorithm_lookup(sav->alg_enc);
172 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
173 "unsupported encryption algorithm for spi %u\n",
174 (u_int32_t)ntohl(spi)));
175 ipsecstat.in_badspi++;
179 /* check if we have proper ivlen information */
182 ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
183 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
184 ipsecstat.in_inval++;
188 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
189 && (sav->alg_auth && sav->key_auth)))
192 if (sav->alg_auth == SADB_X_AALG_NULL ||
193 sav->alg_auth == SADB_AALG_NONE)
197 * check for sequence number.
199 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
202 ipsecstat.in_espreplay++;
203 ipseclog((LOG_WARNING,
204 "replay packet in IPv4 ESP input: %s %s\n",
205 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
211 u_char sum0[AH_MAXSUMSIZE];
212 u_char sum[AH_MAXSUMSIZE];
213 const struct ah_algorithm *sumalgo;
216 sumalgo = ah_algorithm_lookup(sav->alg_auth);
219 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
220 if (AH_MAXSUMSIZE < siz) {
222 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
224 ipsecstat.in_inval++;
228 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
230 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
231 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
232 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
233 ipsecstat.in_espauthfail++;
237 if (bcmp(sum0, sum, siz) != 0) {
238 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
239 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
240 ipsecstat.in_espauthfail++;
244 /* strip off the authentication data */
246 ip = mtod(m, struct ip *);
248 ip->ip_len = ip->ip_len - siz;
250 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
252 m->m_flags |= M_AUTHIPDGM;
253 ipsecstat.in_espauthsucc++;
257 * update sequence number.
259 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
260 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
261 ipsecstat.in_espreplay++;
268 /* process main esp header. */
269 if (sav->flags & SADB_X_EXT_OLD) {
271 esplen = sizeof(struct esp);
274 if (sav->flags & SADB_X_EXT_DERIV)
275 esplen = sizeof(struct esp);
277 esplen = sizeof(struct newesp);
280 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
281 ipseclog((LOG_WARNING,
282 "IPv4 ESP input: packet too short\n"));
283 ipsecstat.in_inval++;
287 if (m->m_len < off + esplen + ivlen) {
288 m = m_pullup(m, off + esplen + ivlen);
291 "IPv4 ESP input: can't pullup in esp4_input\n"));
292 ipsecstat.in_inval++;
298 * pre-compute and cache intermediate key
300 if (esp_schedule(algo, sav) != 0) {
301 ipsecstat.in_inval++;
306 * decrypt the packet.
309 panic("internal error: no decrypt function");
310 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
311 /* m is already freed */
313 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
314 ipsec_logsastr(sav)));
315 ipsecstat.in_inval++;
318 ipsecstat.in_esphist[sav->alg_enc]++;
320 m->m_flags |= M_DECRYPTED;
323 * find the trailer of the ESP.
325 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
327 nxt = esptail.esp_nxt;
328 taillen = esptail.esp_padlen + sizeof(esptail);
330 if (m->m_pkthdr.len < taillen
331 || m->m_pkthdr.len - taillen < hlen) { /*?*/
332 ipseclog((LOG_WARNING,
333 "bad pad length in IPv4 ESP input: %s %s\n",
334 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
335 ipsecstat.in_inval++;
339 /* strip off the trailing pad area. */
343 ip->ip_len = ip->ip_len - taillen;
345 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
348 /* was it transmitted over the IPsec tunnel SA? */
349 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
351 * strip off all the headers that precedes ESP header.
352 * IP4 xx ESP IP4' payload -> IP4' payload
354 * XXX more sanity checks
355 * XXX relationship with gif?
360 m_adj(m, off + esplen + ivlen);
361 if (m->m_len < sizeof(*ip)) {
362 m = m_pullup(m, sizeof(*ip));
364 ipsecstat.in_inval++;
368 ip = mtod(m, struct ip *);
369 /* ECN consideration. */
370 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
371 if (!key_checktunnelsanity(sav, AF_INET,
372 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
373 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
374 "in IPv4 ESP input: %s %s\n",
375 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
376 ipsecstat.in_inval++;
380 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
381 /* drop it if it does not match the default policy */
382 if (ipsec4_in_reject(m, NULL)) {
383 ipsecstat.in_polvio++;
388 key_sa_recordxfer(sav, m);
389 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
390 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
391 ipsecstat.in_nomem++;
395 if (! IF_HANDOFF(&ipintrq, m, NULL)) {
396 ipsecstat.in_inval++;
401 schednetisr(NETISR_IP); /*can be skipped but to make sure*/
405 * strip off ESP header and IV.
406 * even in m_pulldown case, we need to strip off ESP so that
407 * we can always compute checksum for AH correctly.
411 stripsiz = esplen + ivlen;
413 ip = mtod(m, struct ip *);
414 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
415 m->m_data += stripsiz;
416 m->m_len -= stripsiz;
417 m->m_pkthdr.len -= stripsiz;
419 ip = mtod(m, struct ip *);
421 ip->ip_len = ip->ip_len - stripsiz;
423 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
427 key_sa_recordxfer(sav, m);
428 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
429 ipsecstat.in_nomem++;
433 if (nxt != IPPROTO_DONE) {
434 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
435 ipsec4_in_reject(m, NULL)) {
436 ipsecstat.in_polvio++;
439 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
446 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
447 printf("DP esp4_input call free SA:%p\n", sav));
450 ipsecstat.in_success++;
455 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
456 printf("DP esp4_input call free SA:%p\n", sav));
467 esp6_input(mp, offp, proto)
471 struct mbuf *m = *mp;
475 struct esptail esptail;
477 struct secasvar *sav = NULL;
480 const struct esp_algorithm *algo;
484 /* sanity check for alignment. */
485 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
486 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
487 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
488 ipsec6stat.in_inval++;
492 #ifndef PULLDOWN_TEST
493 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
494 esp = (struct esp *)(mtod(m, caddr_t) + off);
496 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
498 ipsec6stat.in_inval++;
502 ip6 = mtod(m, struct ip6_hdr *);
504 if (ntohs(ip6->ip6_plen) == 0) {
505 ipseclog((LOG_ERR, "IPv6 ESP input: "
506 "ESP with IPv6 jumbogram is not supported.\n"));
507 ipsec6stat.in_inval++;
511 /* find the sassoc. */
514 if ((sav = key_allocsa(AF_INET6,
515 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
516 IPPROTO_ESP, spi)) == 0) {
517 ipseclog((LOG_WARNING,
518 "IPv6 ESP input: no key association found for spi %u\n",
519 (u_int32_t)ntohl(spi)));
520 ipsec6stat.in_nosa++;
523 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
524 printf("DP esp6_input called to allocate SA:%p\n", sav));
525 if (sav->state != SADB_SASTATE_MATURE
526 && sav->state != SADB_SASTATE_DYING) {
528 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
529 (u_int32_t)ntohl(spi)));
530 ipsec6stat.in_badspi++;
533 algo = esp_algorithm_lookup(sav->alg_enc);
535 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
536 "unsupported encryption algorithm for spi %u\n",
537 (u_int32_t)ntohl(spi)));
538 ipsec6stat.in_badspi++;
542 /* check if we have proper ivlen information */
545 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
546 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
547 ipsec6stat.in_badspi++;
551 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
552 && (sav->alg_auth && sav->key_auth)))
555 if (sav->alg_auth == SADB_X_AALG_NULL ||
556 sav->alg_auth == SADB_AALG_NONE)
560 * check for sequence number.
562 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
565 ipsec6stat.in_espreplay++;
566 ipseclog((LOG_WARNING,
567 "replay packet in IPv6 ESP input: %s %s\n",
568 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
574 u_char sum0[AH_MAXSUMSIZE];
575 u_char sum[AH_MAXSUMSIZE];
576 const struct ah_algorithm *sumalgo;
579 sumalgo = ah_algorithm_lookup(sav->alg_auth);
582 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
583 if (AH_MAXSUMSIZE < siz) {
585 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
587 ipsec6stat.in_inval++;
591 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
593 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
594 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
595 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
596 ipsec6stat.in_espauthfail++;
600 if (bcmp(sum0, sum, siz) != 0) {
601 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
602 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
603 ipsec6stat.in_espauthfail++;
607 /* strip off the authentication data */
609 ip6 = mtod(m, struct ip6_hdr *);
610 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
612 m->m_flags |= M_AUTHIPDGM;
613 ipsec6stat.in_espauthsucc++;
617 * update sequence number.
619 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
620 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
621 ipsec6stat.in_espreplay++;
628 /* process main esp header. */
629 if (sav->flags & SADB_X_EXT_OLD) {
631 esplen = sizeof(struct esp);
634 if (sav->flags & SADB_X_EXT_DERIV)
635 esplen = sizeof(struct esp);
637 esplen = sizeof(struct newesp);
640 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
641 ipseclog((LOG_WARNING,
642 "IPv6 ESP input: packet too short\n"));
643 ipsec6stat.in_inval++;
647 #ifndef PULLDOWN_TEST
648 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /*XXX*/
650 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
652 ipsec6stat.in_inval++;
657 ip6 = mtod(m, struct ip6_hdr *); /*set it again just in case*/
660 * pre-compute and cache intermediate key
662 if (esp_schedule(algo, sav) != 0) {
663 ipsec6stat.in_inval++;
668 * decrypt the packet.
671 panic("internal error: no decrypt function");
672 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
673 /* m is already freed */
675 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
676 ipsec_logsastr(sav)));
677 ipsec6stat.in_inval++;
680 ipsec6stat.in_esphist[sav->alg_enc]++;
682 m->m_flags |= M_DECRYPTED;
685 * find the trailer of the ESP.
687 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
689 nxt = esptail.esp_nxt;
690 taillen = esptail.esp_padlen + sizeof(esptail);
692 if (m->m_pkthdr.len < taillen
693 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /*?*/
694 ipseclog((LOG_WARNING,
695 "bad pad length in IPv6 ESP input: %s %s\n",
696 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
697 ipsec6stat.in_inval++;
701 /* strip off the trailing pad area. */
704 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
706 /* was it transmitted over the IPsec tunnel SA? */
707 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
709 * strip off all the headers that precedes ESP header.
710 * IP6 xx ESP IP6' payload -> IP6' payload
712 * XXX more sanity checks
713 * XXX relationship with gif?
715 u_int32_t flowinfo; /*net endian*/
716 flowinfo = ip6->ip6_flow;
717 m_adj(m, off + esplen + ivlen);
718 if (m->m_len < sizeof(*ip6)) {
719 #ifndef PULLDOWN_TEST
721 * m_pullup is prohibited in KAME IPv6 input processing
722 * but there's no other way!
725 /* okay to pullup in m_pulldown style */
727 m = m_pullup(m, sizeof(*ip6));
729 ipsec6stat.in_inval++;
733 ip6 = mtod(m, struct ip6_hdr *);
734 /* ECN consideration. */
735 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
736 if (!key_checktunnelsanity(sav, AF_INET6,
737 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
738 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
739 "in IPv6 ESP input: %s %s\n",
740 ipsec6_logpacketstr(ip6, spi),
741 ipsec_logsastr(sav)));
742 ipsec6stat.in_inval++;
746 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
747 /* drop it if it does not match the default policy */
748 if (ipsec6_in_reject(m, NULL)) {
749 ipsec6stat.in_polvio++;
754 key_sa_recordxfer(sav, m);
755 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
756 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
757 ipsec6stat.in_nomem++;
761 if (! IF_HANDOFF(&ip6intrq, m, NULL)) {
762 ipsec6stat.in_inval++;
767 schednetisr(NETISR_IPV6); /*can be skipped but to make sure*/
771 * strip off ESP header and IV.
772 * even in m_pulldown case, we need to strip off ESP so that
773 * we can always compute checksum for AH correctly.
779 * Set the next header field of the previous header correctly.
781 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
784 stripsiz = esplen + ivlen;
786 ip6 = mtod(m, struct ip6_hdr *);
787 if (m->m_len >= stripsiz + off) {
788 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
789 m->m_data += stripsiz;
790 m->m_len -= stripsiz;
791 m->m_pkthdr.len -= stripsiz;
794 * this comes with no copy if the boundary is on
799 n = m_split(m, off, M_DONTWAIT);
801 /* m is retained by m_split */
806 /* m_cat does not update m_pkthdr.len */
807 m->m_pkthdr.len += n->m_pkthdr.len;
810 #ifndef PULLDOWN_TEST
812 * KAME requires that the packet to be contiguous on the
813 * mbuf. We need to make that sure.
814 * this kind of code should be avoided.
815 * XXX other conditions to avoid running this part?
817 if (m->m_len != m->m_pkthdr.len) {
818 struct mbuf *n = NULL;
821 MGETHDR(n, M_DONTWAIT, MT_HEADER);
825 if (n && m->m_pkthdr.len > maxlen) {
826 MCLGET(n, M_DONTWAIT);
828 if ((n->m_flags & M_EXT) == 0) {
834 printf("esp6_input: mbuf allocation failed\n");
838 if (m->m_pkthdr.len <= maxlen) {
839 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
840 n->m_len = m->m_pkthdr.len;
841 n->m_pkthdr.len = m->m_pkthdr.len;
845 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
848 n->m_pkthdr.len = m->m_pkthdr.len;
850 m->m_flags &= ~M_PKTHDR;
856 ip6 = mtod(m, struct ip6_hdr *);
857 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
859 key_sa_recordxfer(sav, m);
860 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
861 ipsec6stat.in_nomem++;
870 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
871 printf("DP esp6_input call free SA:%p\n", sav));
874 ipsec6stat.in_success++;
879 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
880 printf("DP esp6_input call free SA:%p\n", sav));
889 esp6_ctlinput(cmd, sa, d)
894 const struct newesp *espp;
896 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
897 struct secasvar *sav;
901 struct sockaddr_in6 sa6_src, sa6_dst;
903 if (sa->sa_family != AF_INET6 ||
904 sa->sa_len != sizeof(struct sockaddr_in6))
906 if ((unsigned)cmd >= PRC_NCMDS)
909 /* if the parameter is from icmp6, decode it. */
911 ip6cp = (struct ip6ctlparam *)d;
913 ip6 = ip6cp->ip6c_ip6;
914 off = ip6cp->ip6c_off;
918 off = 0; /* calm gcc */
923 * Notify the error to all possible sockets via pfctlinput2.
924 * Since the upper layer information (such as protocol type,
925 * source and destination ports) is embedded in the encrypted
926 * data and might have been cut, we can't directly call
927 * an upper layer ctlinput function. However, the pcbnotify
928 * function will consider source and destination addresses
929 * as well as the flow info value, and may be able to find
930 * some PCB that should be notified.
931 * Although pfctlinput2 will call esp6_ctlinput(), there is
932 * no possibility of an infinite loop of function calls,
933 * because we don't pass the inner IPv6 header.
935 bzero(&ip6cp1, sizeof(ip6cp1));
936 ip6cp1.ip6c_src = ip6cp->ip6c_src;
937 pfctlinput2(cmd, sa, (void *)&ip6cp1);
940 * Then go to special cases that need ESP header information.
941 * XXX: We assume that when ip6 is non NULL,
942 * M and OFF are valid.
945 /* check if we can safely examine src and dst ports */
946 if (m->m_pkthdr.len < off + sizeof(esp))
949 if (m->m_len < off + sizeof(esp)) {
951 * this should be rare case,
952 * so we compromise on this copy...
954 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
957 espp = (struct newesp*)(mtod(m, caddr_t) + off);
959 if (cmd == PRC_MSGSIZE) {
963 * Check to see if we have a valid SA corresponding to
964 * the address in the ICMP message payload.
966 sav = key_allocsa(AF_INET6,
967 (caddr_t)&sa6_src.sin6_addr,
968 (caddr_t)&sa6_dst, IPPROTO_ESP,
971 if (sav->state == SADB_SASTATE_MATURE ||
972 sav->state == SADB_SASTATE_DYING)
977 /* XXX Further validation? */
980 * Depending on the value of "valid" and routing table
981 * size (mtudisc_{hi,lo}wat), we will:
982 * - recalcurate the new MTU and create the
983 * corresponding routing entry, or
984 * - ignore the MTU change notification.
986 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
989 /* we normally notify any pcb here */