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>
50 #include <sys/thread2.h>
51 #include <sys/msgport2.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
56 #include <machine/cpu.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip_ecn.h>
65 #include <netinet6/ip6_ecn.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_pcb.h>
71 #include <netinet6/ip6_var.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 <netinet6/esp.h>
86 #include <netinet6/esp6.h>
88 #include <netproto/key/key.h>
89 #include <netproto/key/keydb.h>
90 #include <netproto/key/key_debug.h>
92 #include <machine/stdarg.h>
94 #include <net/net_osdep.h>
99 (sizeof(struct esp) < sizeof(struct newesp) \
100 ? sizeof(struct newesp) : sizeof(struct esp))
103 extern struct protosw inetsw[];
106 esp4_input(struct mbuf **mp, int *offp, int proto)
111 struct esptail esptail;
114 struct secasvar *sav = NULL;
117 const struct esp_algorithm *algo;
126 /* sanity check for alignment. */
127 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
128 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
129 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
130 ipsecstat.in_inval++;
134 if (m->m_len < off + ESPMAXLEN) {
135 m = m_pullup(m, off + ESPMAXLEN);
138 "IPv4 ESP input: can't pullup in esp4_input\n"));
139 ipsecstat.in_inval++;
144 ip = mtod(m, struct ip *);
145 esp = (struct esp *)(((u_int8_t *)ip) + off);
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_ESP, spi)) == NULL) {
158 ipseclog((LOG_WARNING,
159 "IPv4 ESP input: no key association found for spi %u\n",
160 (u_int32_t)ntohl(spi)));
164 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
165 kprintf("DP esp4_input called to allocate SA:%p\n", sav));
166 if (sav->state != SADB_SASTATE_MATURE
167 && sav->state != SADB_SASTATE_DYING) {
169 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
170 (u_int32_t)ntohl(spi)));
171 ipsecstat.in_badspi++;
174 algo = esp_algorithm_lookup(sav->alg_enc);
176 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
177 "unsupported encryption algorithm for spi %u\n",
178 (u_int32_t)ntohl(spi)));
179 ipsecstat.in_badspi++;
183 /* check if we have proper ivlen information */
186 ipseclog((LOG_ERR, "improper ivlen in IPv4 ESP input: %s %s\n",
187 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
188 ipsecstat.in_inval++;
192 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
193 && (sav->alg_auth && sav->key_auth)))
196 if (sav->alg_auth == SADB_X_AALG_NULL ||
197 sav->alg_auth == SADB_AALG_NONE)
201 * check for sequence number.
203 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
206 ipsecstat.in_espreplay++;
207 ipseclog((LOG_WARNING,
208 "replay packet in IPv4 ESP input: %s %s\n",
209 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
215 u_char sum0[AH_MAXSUMSIZE];
216 u_char sum[AH_MAXSUMSIZE];
217 const struct ah_algorithm *sumalgo;
220 sumalgo = ah_algorithm_lookup(sav->alg_auth);
223 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
224 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
225 ipsecstat.in_inval++;
228 if (AH_MAXSUMSIZE < siz) {
230 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
232 ipsecstat.in_inval++;
236 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
238 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
239 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
240 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
241 ipsecstat.in_espauthfail++;
245 if (bcmp(sum0, sum, siz) != 0) {
246 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
247 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
248 ipsecstat.in_espauthfail++;
252 /* strip off the authentication data */
254 ip = mtod(m, struct ip *);
256 ip->ip_len = ip->ip_len - siz;
258 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
260 m->m_flags |= M_AUTHIPDGM;
261 ipsecstat.in_espauthsucc++;
265 * update sequence number.
267 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
268 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
269 ipsecstat.in_espreplay++;
276 /* process main esp header. */
277 if (sav->flags & SADB_X_EXT_OLD) {
279 esplen = sizeof(struct esp);
282 if (sav->flags & SADB_X_EXT_DERIV)
283 esplen = sizeof(struct esp);
285 esplen = sizeof(struct newesp);
288 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
289 ipseclog((LOG_WARNING,
290 "IPv4 ESP input: packet too short\n"));
291 ipsecstat.in_inval++;
295 if (m->m_len < off + esplen + ivlen) {
296 m = m_pullup(m, off + esplen + ivlen);
299 "IPv4 ESP input: can't pullup in esp4_input\n"));
300 ipsecstat.in_inval++;
306 * pre-compute and cache intermediate key
308 if (esp_schedule(algo, sav) != 0) {
309 ipsecstat.in_inval++;
314 * decrypt the packet.
317 panic("internal error: no decrypt function");
318 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
319 /* m is already freed */
321 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
322 ipsec_logsastr(sav)));
323 ipsecstat.in_inval++;
326 ipsecstat.in_esphist[sav->alg_enc]++;
328 m->m_flags |= M_DECRYPTED;
331 * find the trailer of the ESP.
333 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
335 nxt = esptail.esp_nxt;
336 taillen = esptail.esp_padlen + sizeof(esptail);
338 if (m->m_pkthdr.len < taillen ||
339 m->m_pkthdr.len - taillen < off + esplen + ivlen + sizeof(esptail)) {
340 ipseclog((LOG_WARNING,
341 "bad pad length in IPv4 ESP input: %s %s\n",
342 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
343 ipsecstat.in_inval++;
347 /* strip off the trailing pad area. */
351 ip->ip_len = ip->ip_len - taillen;
353 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
356 /* was it transmitted over the IPsec tunnel SA? */
357 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
359 * strip off all the headers that precedes ESP header.
360 * IP4 xx ESP IP4' payload -> IP4' payload
362 * XXX more sanity checks
363 * XXX relationship with gif?
368 m_adj(m, off + esplen + ivlen);
369 if (m->m_len < sizeof(*ip)) {
370 m = m_pullup(m, sizeof(*ip));
372 ipsecstat.in_inval++;
376 ip = mtod(m, struct ip *);
377 /* ECN consideration. */
378 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
379 if (!key_checktunnelsanity(sav, AF_INET,
380 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
381 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
382 "in IPv4 ESP input: %s %s\n",
383 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
384 ipsecstat.in_inval++;
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 (netisr_queue(NETISR_IP, m)) {
396 ipsecstat.in_inval++;
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) &&
434 ipsec4_in_reject(m, NULL)) {
435 ipsecstat.in_polvio++;
438 if (!ip_lengthcheck(&m, 0)) {
439 /* freed in ip_lengthcheck() */
444 (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
452 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
453 kprintf("DP esp4_input call free SA:%p\n", sav));
456 ipsecstat.in_success++;
457 return(IPPROTO_DONE);
461 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
462 kprintf("DP esp4_input call free SA:%p\n", sav));
467 return(IPPROTO_DONE);
473 esp6_input(struct mbuf **mp, int *offp, int proto)
475 struct mbuf *m = *mp;
479 struct esptail esptail;
481 struct secasvar *sav = NULL;
484 const struct esp_algorithm *algo;
488 /* sanity check for alignment. */
489 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
490 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
491 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
492 ipsec6stat.in_inval++;
496 #ifndef PULLDOWN_TEST
497 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
498 esp = (struct esp *)(mtod(m, caddr_t) + off);
500 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
502 ipsec6stat.in_inval++;
506 ip6 = mtod(m, struct ip6_hdr *);
508 if (ntohs(ip6->ip6_plen) == 0) {
509 ipseclog((LOG_ERR, "IPv6 ESP input: "
510 "ESP with IPv6 jumbogram is not supported.\n"));
511 ipsec6stat.in_inval++;
515 /* find the sassoc. */
518 if ((sav = key_allocsa(AF_INET6,
519 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
520 IPPROTO_ESP, spi)) == NULL) {
521 ipseclog((LOG_WARNING,
522 "IPv6 ESP input: no key association found for spi %u\n",
523 (u_int32_t)ntohl(spi)));
524 ipsec6stat.in_nosa++;
527 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
528 kprintf("DP esp6_input called to allocate SA:%p\n", sav));
529 if (sav->state != SADB_SASTATE_MATURE
530 && sav->state != SADB_SASTATE_DYING) {
532 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
533 (u_int32_t)ntohl(spi)));
534 ipsec6stat.in_badspi++;
537 algo = esp_algorithm_lookup(sav->alg_enc);
539 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
540 "unsupported encryption algorithm for spi %u\n",
541 (u_int32_t)ntohl(spi)));
542 ipsec6stat.in_badspi++;
546 /* check if we have proper ivlen information */
549 ipseclog((LOG_ERR, "improper ivlen in IPv6 ESP input: %s %s\n",
550 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
551 ipsec6stat.in_badspi++;
555 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
556 && (sav->alg_auth && sav->key_auth)))
559 if (sav->alg_auth == SADB_X_AALG_NULL ||
560 sav->alg_auth == SADB_AALG_NONE)
564 * check for sequence number.
566 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
569 ipsec6stat.in_espreplay++;
570 ipseclog((LOG_WARNING,
571 "replay packet in IPv6 ESP input: %s %s\n",
572 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
578 u_char sum0[AH_MAXSUMSIZE];
579 u_char sum[AH_MAXSUMSIZE];
580 const struct ah_algorithm *sumalgo;
583 sumalgo = ah_algorithm_lookup(sav->alg_auth);
586 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
587 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
588 ipsec6stat.in_inval++;
591 if (AH_MAXSUMSIZE < siz) {
593 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
595 ipsec6stat.in_inval++;
599 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
601 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
602 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
603 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
604 ipsec6stat.in_espauthfail++;
608 if (bcmp(sum0, sum, siz) != 0) {
609 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
610 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
611 ipsec6stat.in_espauthfail++;
615 /* strip off the authentication data */
617 ip6 = mtod(m, struct ip6_hdr *);
618 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
620 m->m_flags |= M_AUTHIPDGM;
621 ipsec6stat.in_espauthsucc++;
625 * update sequence number.
627 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
628 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
629 ipsec6stat.in_espreplay++;
636 /* process main esp header. */
637 if (sav->flags & SADB_X_EXT_OLD) {
639 esplen = sizeof(struct esp);
642 if (sav->flags & SADB_X_EXT_DERIV)
643 esplen = sizeof(struct esp);
645 esplen = sizeof(struct newesp);
648 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
649 ipseclog((LOG_WARNING,
650 "IPv6 ESP input: packet too short\n"));
651 ipsec6stat.in_inval++;
655 #ifndef PULLDOWN_TEST
656 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
658 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
660 ipsec6stat.in_inval++;
665 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
668 * pre-compute and cache intermediate key
670 if (esp_schedule(algo, sav) != 0) {
671 ipsec6stat.in_inval++;
676 * decrypt the packet.
679 panic("internal error: no decrypt function");
680 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
681 /* m is already freed */
683 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
684 ipsec_logsastr(sav)));
685 ipsec6stat.in_inval++;
688 ipsec6stat.in_esphist[sav->alg_enc]++;
690 m->m_flags |= M_DECRYPTED;
693 * find the trailer of the ESP.
695 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
697 nxt = esptail.esp_nxt;
698 taillen = esptail.esp_padlen + sizeof(esptail);
700 if (m->m_pkthdr.len < taillen
701 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
702 ipseclog((LOG_WARNING,
703 "bad pad length in IPv6 ESP input: %s %s\n",
704 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
705 ipsec6stat.in_inval++;
709 /* strip off the trailing pad area. */
712 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
714 /* was it transmitted over the IPsec tunnel SA? */
715 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
717 * strip off all the headers that precedes ESP header.
718 * IP6 xx ESP IP6' payload -> IP6' payload
720 * XXX more sanity checks
721 * XXX relationship with gif?
723 u_int32_t flowinfo; /* net endian */
724 flowinfo = ip6->ip6_flow;
725 m_adj(m, off + esplen + ivlen);
726 if (m->m_len < sizeof(*ip6)) {
727 #ifndef PULLDOWN_TEST
729 * m_pullup is prohibited in KAME IPv6 input processing
730 * but there's no other way!
733 /* okay to pullup in m_pulldown style */
735 m = m_pullup(m, sizeof(*ip6));
737 ipsec6stat.in_inval++;
741 ip6 = mtod(m, struct ip6_hdr *);
742 /* ECN consideration. */
743 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
744 if (!key_checktunnelsanity(sav, AF_INET6,
745 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
746 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
747 "in IPv6 ESP input: %s %s\n",
748 ipsec6_logpacketstr(ip6, spi),
749 ipsec_logsastr(sav)));
750 ipsec6stat.in_inval++;
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 (netisr_queue(NETISR_IPV6, m)) {
762 ipsec6stat.in_inval++;
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, MB_DONTWAIT);
800 /* m is retained by m_split */
804 /* m_cat does not update m_pkthdr.len */
805 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 n = m_getb(m->m_pkthdr.len, MB_DONTWAIT, MT_HEADER,
823 kprintf("esp6_input: mbuf allocation failed\n");
828 maxlen = (n->m_flags & M_EXT) ? MCLBYTES : MHLEN;
829 if (n->m_pkthdr.len <= maxlen) {
830 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
831 n->m_len = n->m_pkthdr.len;
835 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
844 ip6 = mtod(m, struct ip6_hdr *);
845 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
847 key_sa_recordxfer(sav, m);
848 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
849 ipsec6stat.in_nomem++;
858 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
859 kprintf("DP esp6_input call free SA:%p\n", sav));
862 ipsec6stat.in_success++;
867 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
868 kprintf("DP esp6_input call free SA:%p\n", sav));
877 esp6_ctlinput(netmsg_t msg)
879 int cmd = msg->ctlinput.nm_cmd;
880 struct sockaddr *sa = msg->ctlinput.nm_arg;
881 void *d = msg->ctlinput.nm_extra;
882 const struct newesp *espp;
884 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
885 struct secasvar *sav;
889 struct sockaddr_in6 *sa6_src, *sa6_dst;
891 if (sa->sa_family != AF_INET6 ||
892 sa->sa_len != sizeof(struct sockaddr_in6))
894 if ((unsigned)cmd >= PRC_NCMDS)
897 /* if the parameter is from icmp6, decode it. */
899 ip6cp = (struct ip6ctlparam *)d;
901 ip6 = ip6cp->ip6c_ip6;
902 off = ip6cp->ip6c_off;
906 off = 0; /* fix warning */
911 * Notify the error to all possible sockets via pfctlinput2.
912 * Since the upper layer information (such as protocol type,
913 * source and destination ports) is embedded in the encrypted
914 * data and might have been cut, we can't directly call
915 * an upper layer ctlinput function. However, the pcbnotify
916 * function will consider source and destination addresses
917 * as well as the flow info value, and may be able to find
918 * some PCB that should be notified.
919 * Although kpfctlinput2 will call esp6_ctlinput(), there is
920 * no possibility of an infinite loop of function calls,
921 * because we don't pass the inner IPv6 header.
923 bzero(&ip6cp1, sizeof(ip6cp1));
924 ip6cp1.ip6c_src = ip6cp->ip6c_src;
925 kpfctlinput2(cmd, sa, (void *)&ip6cp1);
928 * Then go to special cases that need ESP header information.
929 * XXX: We assume that when ip6 is non NULL,
930 * M and OFF are valid.
933 /* check if we can safely examine src and dst ports */
934 if (m->m_pkthdr.len < off + sizeof(esp))
937 if (m->m_len < off + sizeof(esp)) {
939 * this should be rare case,
940 * so we compromise on this copy...
942 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
945 espp = (struct newesp*)(mtod(m, caddr_t) + off);
947 if (cmd == PRC_MSGSIZE) {
951 * Check to see if we have a valid SA corresponding to
952 * the address in the ICMP message payload.
954 sa6_src = ip6cp->ip6c_src;
955 sa6_dst = (struct sockaddr_in6 *)sa;
956 sav = key_allocsa(AF_INET6,
957 (caddr_t)&sa6_src->sin6_addr,
958 (caddr_t)&sa6_dst->sin6_addr,
959 IPPROTO_ESP, espp->esp_spi);
961 if (sav->state == SADB_SASTATE_MATURE ||
962 sav->state == SADB_SASTATE_DYING)
967 /* XXX Further validation? */
970 * Depending on the value of "valid" and routing table
971 * size (mtudisc_{hi,lo}wat), we will:
972 * - recalcurate the new MTU and create the
973 * corresponding routing entry, or
974 * - ignore the MTU change notification.
976 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
979 /* we normally notify any pcb here */
982 lwkt_replymsg(&msg->ctlinput.base.lmsg, 0);