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 <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 extern struct protosw inetsw[];
109 struct esptail esptail;
111 struct secasvar *sav = NULL;
114 const struct esp_algorithm *algo;
120 /* sanity check for alignment. */
121 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
122 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
123 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
124 ipsecstat.in_inval++;
128 if (m->m_len < off + ESPMAXLEN) {
129 m = m_pullup(m, off + ESPMAXLEN);
132 "IPv4 ESP input: can't pullup in esp4_input\n"));
133 ipsecstat.in_inval++;
138 ip = mtod(m, struct ip *);
140 esp = (struct esp *)(((u_int8_t *)ip) + off);
142 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
144 hlen = ip->ip_hl << 2;
147 /* find the sassoc. */
150 if ((sav = key_allocsa(AF_INET,
151 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
152 IPPROTO_ESP, spi)) == 0) {
153 ipseclog((LOG_WARNING,
154 "IPv4 ESP input: no key association found for spi %u\n",
155 (u_int32_t)ntohl(spi)));
159 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
160 printf("DP esp4_input called to allocate SA:%p\n", sav));
161 if (sav->state != SADB_SASTATE_MATURE
162 && sav->state != SADB_SASTATE_DYING) {
164 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
165 (u_int32_t)ntohl(spi)));
166 ipsecstat.in_badspi++;
169 algo = esp_algorithm_lookup(sav->alg_enc);
171 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
172 "unsupported encryption algorithm for spi %u\n",
173 (u_int32_t)ntohl(spi)));
174 ipsecstat.in_badspi++;
178 /* check if we have proper ivlen information */
181 ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
182 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
183 ipsecstat.in_inval++;
187 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
188 && (sav->alg_auth && sav->key_auth)))
191 if (sav->alg_auth == SADB_X_AALG_NULL ||
192 sav->alg_auth == SADB_AALG_NONE)
196 * check for sequence number.
198 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
201 ipsecstat.in_espreplay++;
202 ipseclog((LOG_WARNING,
203 "replay packet in IPv4 ESP input: %s %s\n",
204 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
210 u_char sum0[AH_MAXSUMSIZE];
211 u_char sum[AH_MAXSUMSIZE];
212 const struct ah_algorithm *sumalgo;
215 sumalgo = ah_algorithm_lookup(sav->alg_auth);
218 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
219 if (AH_MAXSUMSIZE < siz) {
221 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
223 ipsecstat.in_inval++;
227 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
229 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
230 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
231 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
232 ipsecstat.in_espauthfail++;
236 if (bcmp(sum0, sum, siz) != 0) {
237 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
238 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
239 ipsecstat.in_espauthfail++;
243 /* strip off the authentication data */
245 ip = mtod(m, struct ip *);
247 ip->ip_len = ip->ip_len - siz;
249 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
251 m->m_flags |= M_AUTHIPDGM;
252 ipsecstat.in_espauthsucc++;
256 * update sequence number.
258 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
259 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
260 ipsecstat.in_espreplay++;
267 /* process main esp header. */
268 if (sav->flags & SADB_X_EXT_OLD) {
270 esplen = sizeof(struct esp);
273 if (sav->flags & SADB_X_EXT_DERIV)
274 esplen = sizeof(struct esp);
276 esplen = sizeof(struct newesp);
279 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
280 ipseclog((LOG_WARNING,
281 "IPv4 ESP input: packet too short\n"));
282 ipsecstat.in_inval++;
286 if (m->m_len < off + esplen + ivlen) {
287 m = m_pullup(m, off + esplen + ivlen);
290 "IPv4 ESP input: can't pullup in esp4_input\n"));
291 ipsecstat.in_inval++;
297 * pre-compute and cache intermediate key
299 if (esp_schedule(algo, sav) != 0) {
300 ipsecstat.in_inval++;
305 * decrypt the packet.
308 panic("internal error: no decrypt function");
309 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
310 /* m is already freed */
312 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
313 ipsec_logsastr(sav)));
314 ipsecstat.in_inval++;
317 ipsecstat.in_esphist[sav->alg_enc]++;
319 m->m_flags |= M_DECRYPTED;
322 * find the trailer of the ESP.
324 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
326 nxt = esptail.esp_nxt;
327 taillen = esptail.esp_padlen + sizeof(esptail);
329 if (m->m_pkthdr.len < taillen
330 || m->m_pkthdr.len - taillen < hlen) { /*?*/
331 ipseclog((LOG_WARNING,
332 "bad pad length in IPv4 ESP input: %s %s\n",
333 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
334 ipsecstat.in_inval++;
338 /* strip off the trailing pad area. */
342 ip->ip_len = ip->ip_len - taillen;
344 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
347 /* was it transmitted over the IPsec tunnel SA? */
348 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
350 * strip off all the headers that precedes ESP header.
351 * IP4 xx ESP IP4' payload -> IP4' payload
353 * XXX more sanity checks
354 * XXX relationship with gif?
359 m_adj(m, off + esplen + ivlen);
360 if (m->m_len < sizeof(*ip)) {
361 m = m_pullup(m, sizeof(*ip));
363 ipsecstat.in_inval++;
367 ip = mtod(m, struct ip *);
368 /* ECN consideration. */
369 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
370 if (!key_checktunnelsanity(sav, AF_INET,
371 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
372 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
373 "in IPv4 ESP input: %s %s\n",
374 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
375 ipsecstat.in_inval++;
379 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
380 /* drop it if it does not match the default policy */
381 if (ipsec4_in_reject(m, NULL)) {
382 ipsecstat.in_polvio++;
387 key_sa_recordxfer(sav, m);
388 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
389 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
390 ipsecstat.in_nomem++;
394 if (! IF_HANDOFF(&ipintrq, m, NULL)) {
395 ipsecstat.in_inval++;
400 schednetisr(NETISR_IP); /*can be skipped but to make sure*/
404 * strip off ESP header and IV.
405 * even in m_pulldown case, we need to strip off ESP so that
406 * we can always compute checksum for AH correctly.
410 stripsiz = esplen + ivlen;
412 ip = mtod(m, struct ip *);
413 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
414 m->m_data += stripsiz;
415 m->m_len -= stripsiz;
416 m->m_pkthdr.len -= stripsiz;
418 ip = mtod(m, struct ip *);
420 ip->ip_len = ip->ip_len - stripsiz;
422 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
426 key_sa_recordxfer(sav, m);
427 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
428 ipsecstat.in_nomem++;
432 if (nxt != IPPROTO_DONE) {
433 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
434 ipsec4_in_reject(m, NULL)) {
435 ipsecstat.in_polvio++;
438 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
445 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
446 printf("DP esp4_input call free SA:%p\n", sav));
449 ipsecstat.in_success++;
454 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
455 printf("DP esp4_input call free SA:%p\n", sav));
466 esp6_input(mp, offp, proto)
470 struct mbuf *m = *mp;
474 struct esptail esptail;
476 struct secasvar *sav = NULL;
479 const struct esp_algorithm *algo;
483 /* sanity check for alignment. */
484 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
485 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
486 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
487 ipsec6stat.in_inval++;
491 #ifndef PULLDOWN_TEST
492 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
493 esp = (struct esp *)(mtod(m, caddr_t) + off);
495 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
497 ipsec6stat.in_inval++;
501 ip6 = mtod(m, struct ip6_hdr *);
503 if (ntohs(ip6->ip6_plen) == 0) {
504 ipseclog((LOG_ERR, "IPv6 ESP input: "
505 "ESP with IPv6 jumbogram is not supported.\n"));
506 ipsec6stat.in_inval++;
510 /* find the sassoc. */
513 if ((sav = key_allocsa(AF_INET6,
514 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
515 IPPROTO_ESP, spi)) == 0) {
516 ipseclog((LOG_WARNING,
517 "IPv6 ESP input: no key association found for spi %u\n",
518 (u_int32_t)ntohl(spi)));
519 ipsec6stat.in_nosa++;
522 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
523 printf("DP esp6_input called to allocate SA:%p\n", sav));
524 if (sav->state != SADB_SASTATE_MATURE
525 && sav->state != SADB_SASTATE_DYING) {
527 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
528 (u_int32_t)ntohl(spi)));
529 ipsec6stat.in_badspi++;
532 algo = esp_algorithm_lookup(sav->alg_enc);
534 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
535 "unsupported encryption algorithm for spi %u\n",
536 (u_int32_t)ntohl(spi)));
537 ipsec6stat.in_badspi++;
541 /* check if we have proper ivlen information */
544 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
545 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
546 ipsec6stat.in_badspi++;
550 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
551 && (sav->alg_auth && sav->key_auth)))
554 if (sav->alg_auth == SADB_X_AALG_NULL ||
555 sav->alg_auth == SADB_AALG_NONE)
559 * check for sequence number.
561 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
564 ipsec6stat.in_espreplay++;
565 ipseclog((LOG_WARNING,
566 "replay packet in IPv6 ESP input: %s %s\n",
567 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
573 u_char sum0[AH_MAXSUMSIZE];
574 u_char sum[AH_MAXSUMSIZE];
575 const struct ah_algorithm *sumalgo;
578 sumalgo = ah_algorithm_lookup(sav->alg_auth);
581 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
582 if (AH_MAXSUMSIZE < siz) {
584 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
586 ipsec6stat.in_inval++;
590 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
592 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
593 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
594 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
595 ipsec6stat.in_espauthfail++;
599 if (bcmp(sum0, sum, siz) != 0) {
600 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
601 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
602 ipsec6stat.in_espauthfail++;
606 /* strip off the authentication data */
608 ip6 = mtod(m, struct ip6_hdr *);
609 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
611 m->m_flags |= M_AUTHIPDGM;
612 ipsec6stat.in_espauthsucc++;
616 * update sequence number.
618 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
619 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
620 ipsec6stat.in_espreplay++;
627 /* process main esp header. */
628 if (sav->flags & SADB_X_EXT_OLD) {
630 esplen = sizeof(struct esp);
633 if (sav->flags & SADB_X_EXT_DERIV)
634 esplen = sizeof(struct esp);
636 esplen = sizeof(struct newesp);
639 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
640 ipseclog((LOG_WARNING,
641 "IPv6 ESP input: packet too short\n"));
642 ipsec6stat.in_inval++;
646 #ifndef PULLDOWN_TEST
647 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /*XXX*/
649 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
651 ipsec6stat.in_inval++;
656 ip6 = mtod(m, struct ip6_hdr *); /*set it again just in case*/
659 * pre-compute and cache intermediate key
661 if (esp_schedule(algo, sav) != 0) {
662 ipsec6stat.in_inval++;
667 * decrypt the packet.
670 panic("internal error: no decrypt function");
671 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
672 /* m is already freed */
674 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
675 ipsec_logsastr(sav)));
676 ipsec6stat.in_inval++;
679 ipsec6stat.in_esphist[sav->alg_enc]++;
681 m->m_flags |= M_DECRYPTED;
684 * find the trailer of the ESP.
686 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
688 nxt = esptail.esp_nxt;
689 taillen = esptail.esp_padlen + sizeof(esptail);
691 if (m->m_pkthdr.len < taillen
692 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /*?*/
693 ipseclog((LOG_WARNING,
694 "bad pad length in IPv6 ESP input: %s %s\n",
695 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
696 ipsec6stat.in_inval++;
700 /* strip off the trailing pad area. */
703 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
705 /* was it transmitted over the IPsec tunnel SA? */
706 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
708 * strip off all the headers that precedes ESP header.
709 * IP6 xx ESP IP6' payload -> IP6' payload
711 * XXX more sanity checks
712 * XXX relationship with gif?
714 u_int32_t flowinfo; /*net endian*/
715 flowinfo = ip6->ip6_flow;
716 m_adj(m, off + esplen + ivlen);
717 if (m->m_len < sizeof(*ip6)) {
718 #ifndef PULLDOWN_TEST
720 * m_pullup is prohibited in KAME IPv6 input processing
721 * but there's no other way!
724 /* okay to pullup in m_pulldown style */
726 m = m_pullup(m, sizeof(*ip6));
728 ipsec6stat.in_inval++;
732 ip6 = mtod(m, struct ip6_hdr *);
733 /* ECN consideration. */
734 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
735 if (!key_checktunnelsanity(sav, AF_INET6,
736 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
737 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
738 "in IPv6 ESP input: %s %s\n",
739 ipsec6_logpacketstr(ip6, spi),
740 ipsec_logsastr(sav)));
741 ipsec6stat.in_inval++;
745 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
746 /* drop it if it does not match the default policy */
747 if (ipsec6_in_reject(m, NULL)) {
748 ipsec6stat.in_polvio++;
753 key_sa_recordxfer(sav, m);
754 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
755 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
756 ipsec6stat.in_nomem++;
760 if (! IF_HANDOFF(&ip6intrq, m, NULL)) {
761 ipsec6stat.in_inval++;
766 schednetisr(NETISR_IPV6); /*can be skipped but to make sure*/
770 * strip off ESP header and IV.
771 * even in m_pulldown case, we need to strip off ESP so that
772 * we can always compute checksum for AH correctly.
778 * Set the next header field of the previous header correctly.
780 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
783 stripsiz = esplen + ivlen;
785 ip6 = mtod(m, struct ip6_hdr *);
786 if (m->m_len >= stripsiz + off) {
787 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
788 m->m_data += stripsiz;
789 m->m_len -= stripsiz;
790 m->m_pkthdr.len -= stripsiz;
793 * this comes with no copy if the boundary is on
798 n = m_split(m, off, M_DONTWAIT);
800 /* m is retained by m_split */
805 /* m_cat does not update m_pkthdr.len */
806 m->m_pkthdr.len += n->m_pkthdr.len;
809 #ifndef PULLDOWN_TEST
811 * KAME requires that the packet to be contiguous on the
812 * mbuf. We need to make that sure.
813 * this kind of code should be avoided.
814 * XXX other conditions to avoid running this part?
816 if (m->m_len != m->m_pkthdr.len) {
817 struct mbuf *n = NULL;
820 MGETHDR(n, M_DONTWAIT, MT_HEADER);
824 if (n && m->m_pkthdr.len > maxlen) {
825 MCLGET(n, M_DONTWAIT);
827 if ((n->m_flags & M_EXT) == 0) {
833 printf("esp6_input: mbuf allocation failed\n");
837 if (m->m_pkthdr.len <= maxlen) {
838 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
839 n->m_len = m->m_pkthdr.len;
840 n->m_pkthdr.len = m->m_pkthdr.len;
844 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
847 n->m_pkthdr.len = m->m_pkthdr.len;
849 m->m_flags &= ~M_PKTHDR;
855 ip6 = mtod(m, struct ip6_hdr *);
856 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
858 key_sa_recordxfer(sav, m);
859 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
860 ipsec6stat.in_nomem++;
869 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
870 printf("DP esp6_input call free SA:%p\n", sav));
873 ipsec6stat.in_success++;
878 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
879 printf("DP esp6_input call free SA:%p\n", sav));
888 esp6_ctlinput(cmd, sa, d)
893 const struct newesp *espp;
895 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
896 struct secasvar *sav;
900 struct sockaddr_in6 sa6_src, sa6_dst;
902 if (sa->sa_family != AF_INET6 ||
903 sa->sa_len != sizeof(struct sockaddr_in6))
905 if ((unsigned)cmd >= PRC_NCMDS)
908 /* if the parameter is from icmp6, decode it. */
910 ip6cp = (struct ip6ctlparam *)d;
912 ip6 = ip6cp->ip6c_ip6;
913 off = ip6cp->ip6c_off;
921 * Notify the error to all possible sockets via pfctlinput2.
922 * Since the upper layer information (such as protocol type,
923 * source and destination ports) is embedded in the encrypted
924 * data and might have been cut, we can't directly call
925 * an upper layer ctlinput function. However, the pcbnotify
926 * function will consider source and destination addresses
927 * as well as the flow info value, and may be able to find
928 * some PCB that should be notified.
929 * Although pfctlinput2 will call esp6_ctlinput(), there is
930 * no possibility of an infinite loop of function calls,
931 * because we don't pass the inner IPv6 header.
933 bzero(&ip6cp1, sizeof(ip6cp1));
934 ip6cp1.ip6c_src = ip6cp->ip6c_src;
935 pfctlinput2(cmd, sa, (void *)&ip6cp1);
938 * Then go to special cases that need ESP header information.
939 * XXX: We assume that when ip6 is non NULL,
940 * M and OFF are valid.
943 /* check if we can safely examine src and dst ports */
944 if (m->m_pkthdr.len < off + sizeof(esp))
947 if (m->m_len < off + sizeof(esp)) {
949 * this should be rare case,
950 * so we compromise on this copy...
952 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
955 espp = (struct newesp*)(mtod(m, caddr_t) + off);
957 if (cmd == PRC_MSGSIZE) {
961 * Check to see if we have a valid SA corresponding to
962 * the address in the ICMP message payload.
964 sav = key_allocsa(AF_INET6,
965 (caddr_t)&sa6_src.sin6_addr,
966 (caddr_t)&sa6_dst, IPPROTO_ESP,
969 if (sav->state == SADB_SASTATE_MATURE ||
970 sav->state == SADB_SASTATE_DYING)
975 /* XXX Further validation? */
978 * Depending on the value of "valid" and routing table
979 * size (mtudisc_{hi,lo}wat), we will:
980 * - recalcurate the new MTU and create the
981 * corresponding routing entry, or
982 * - ignore the MTU change notification.
984 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
987 /* we normally notify any pcb here */