1 /* $FreeBSD: src/sys/netinet6/esp_input.c,v 1.1.2.8 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/esp_input.c,v 1.7 2004/06/03 18:30:04 joerg Exp $ */
3 /* $KAME: esp_input.c,v 1.62 2002/01/07 11:39:57 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 * RFC1827/2406 Encapsulated Security Payload.
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <machine/cpu.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_ecn.h>
63 #include <netinet6/ip6_ecn.h>
67 #include <netinet/ip6.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 <netproto/key/key.h>
87 #include <netproto/key/keydb.h>
88 #include <netproto/key/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 #include <netinet/ipprotosw.h>
102 extern struct ipprotosw inetsw[];
105 esp4_input(struct mbuf *m, ...)
110 struct esptail esptail;
112 struct secasvar *sav = NULL;
115 const struct esp_algorithm *algo;
122 off = __va_arg(ap, int);
123 proto = __va_arg(ap, int);
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)) == 0) {
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 printf("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, "inproper 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 < hlen) { /* ? */
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) != 0 &&
434 ipsec4_in_reject(m, NULL)) {
435 ipsecstat.in_polvio++;
438 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
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 (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
583 ipsecstat.in_inval++;
586 if (AH_MAXSUMSIZE < siz) {
588 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
590 ipsec6stat.in_inval++;
594 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
596 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
597 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
598 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
599 ipsec6stat.in_espauthfail++;
603 if (bcmp(sum0, sum, siz) != 0) {
604 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
605 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
606 ipsec6stat.in_espauthfail++;
610 /* strip off the authentication data */
612 ip6 = mtod(m, struct ip6_hdr *);
613 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
615 m->m_flags |= M_AUTHIPDGM;
616 ipsec6stat.in_espauthsucc++;
620 * update sequence number.
622 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
623 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
624 ipsec6stat.in_espreplay++;
631 /* process main esp header. */
632 if (sav->flags & SADB_X_EXT_OLD) {
634 esplen = sizeof(struct esp);
637 if (sav->flags & SADB_X_EXT_DERIV)
638 esplen = sizeof(struct esp);
640 esplen = sizeof(struct newesp);
643 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
644 ipseclog((LOG_WARNING,
645 "IPv6 ESP input: packet too short\n"));
646 ipsec6stat.in_inval++;
650 #ifndef PULLDOWN_TEST
651 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
653 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
655 ipsec6stat.in_inval++;
660 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
663 * pre-compute and cache intermediate key
665 if (esp_schedule(algo, sav) != 0) {
666 ipsec6stat.in_inval++;
671 * decrypt the packet.
674 panic("internal error: no decrypt function");
675 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
676 /* m is already freed */
678 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
679 ipsec_logsastr(sav)));
680 ipsec6stat.in_inval++;
683 ipsec6stat.in_esphist[sav->alg_enc]++;
685 m->m_flags |= M_DECRYPTED;
688 * find the trailer of the ESP.
690 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
692 nxt = esptail.esp_nxt;
693 taillen = esptail.esp_padlen + sizeof(esptail);
695 if (m->m_pkthdr.len < taillen
696 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
697 ipseclog((LOG_WARNING,
698 "bad pad length in IPv6 ESP input: %s %s\n",
699 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
700 ipsec6stat.in_inval++;
704 /* strip off the trailing pad area. */
707 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
709 /* was it transmitted over the IPsec tunnel SA? */
710 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
712 * strip off all the headers that precedes ESP header.
713 * IP6 xx ESP IP6' payload -> IP6' payload
715 * XXX more sanity checks
716 * XXX relationship with gif?
718 u_int32_t flowinfo; /* net endian */
719 flowinfo = ip6->ip6_flow;
720 m_adj(m, off + esplen + ivlen);
721 if (m->m_len < sizeof(*ip6)) {
722 #ifndef PULLDOWN_TEST
724 * m_pullup is prohibited in KAME IPv6 input processing
725 * but there's no other way!
728 /* okay to pullup in m_pulldown style */
730 m = m_pullup(m, sizeof(*ip6));
732 ipsec6stat.in_inval++;
736 ip6 = mtod(m, struct ip6_hdr *);
737 /* ECN consideration. */
738 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
739 if (!key_checktunnelsanity(sav, AF_INET6,
740 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
741 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
742 "in IPv6 ESP input: %s %s\n",
743 ipsec6_logpacketstr(ip6, spi),
744 ipsec_logsastr(sav)));
745 ipsec6stat.in_inval++;
749 key_sa_recordxfer(sav, m);
750 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
751 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
752 ipsec6stat.in_nomem++;
756 if (netisr_queue(NETISR_IPV6, m)) {
757 ipsec6stat.in_inval++;
765 * strip off ESP header and IV.
766 * even in m_pulldown case, we need to strip off ESP so that
767 * we can always compute checksum for AH correctly.
773 * Set the next header field of the previous header correctly.
775 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
778 stripsiz = esplen + ivlen;
780 ip6 = mtod(m, struct ip6_hdr *);
781 if (m->m_len >= stripsiz + off) {
782 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
783 m->m_data += stripsiz;
784 m->m_len -= stripsiz;
785 m->m_pkthdr.len -= stripsiz;
788 * this comes with no copy if the boundary is on
793 n = m_split(m, off, MB_DONTWAIT);
795 /* m is retained by m_split */
800 /* m_cat does not update m_pkthdr.len */
801 m->m_pkthdr.len += n->m_pkthdr.len;
804 #ifndef PULLDOWN_TEST
806 * KAME requires that the packet to be contiguous on the
807 * mbuf. We need to make that sure.
808 * this kind of code should be avoided.
809 * XXX other conditions to avoid running this part?
811 if (m->m_len != m->m_pkthdr.len) {
812 struct mbuf *n = NULL;
815 MGETHDR(n, MB_DONTWAIT, MT_HEADER);
819 if (n && n->m_pkthdr.len > maxlen) {
820 MCLGET(n, MB_DONTWAIT);
822 if ((n->m_flags & M_EXT) == 0) {
828 printf("esp6_input: mbuf allocation failed\n");
832 if (n->m_pkthdr.len <= maxlen) {
833 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
834 n->m_len = n->m_pkthdr.len;
838 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
847 ip6 = mtod(m, struct ip6_hdr *);
848 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
850 key_sa_recordxfer(sav, m);
851 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
852 ipsec6stat.in_nomem++;
861 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
862 printf("DP esp6_input call free SA:%p\n", sav));
865 ipsec6stat.in_success++;
870 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
871 printf("DP esp6_input call free SA:%p\n", sav));
880 esp6_ctlinput(cmd, sa, d)
885 const struct newesp *espp;
887 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
888 struct secasvar *sav;
892 struct sockaddr_in6 *sa6_src, *sa6_dst;
894 if (sa->sa_family != AF_INET6 ||
895 sa->sa_len != sizeof(struct sockaddr_in6))
897 if ((unsigned)cmd >= PRC_NCMDS)
900 /* if the parameter is from icmp6, decode it. */
902 ip6cp = (struct ip6ctlparam *)d;
904 ip6 = ip6cp->ip6c_ip6;
905 off = ip6cp->ip6c_off;
909 off = 0; /* fix warning */
914 * Notify the error to all possible sockets via pfctlinput2.
915 * Since the upper layer information (such as protocol type,
916 * source and destination ports) is embedded in the encrypted
917 * data and might have been cut, we can't directly call
918 * an upper layer ctlinput function. However, the pcbnotify
919 * function will consider source and destination addresses
920 * as well as the flow info value, and may be able to find
921 * some PCB that should be notified.
922 * Although pfctlinput2 will call esp6_ctlinput(), there is
923 * no possibility of an infinite loop of function calls,
924 * because we don't pass the inner IPv6 header.
926 bzero(&ip6cp1, sizeof(ip6cp1));
927 ip6cp1.ip6c_src = ip6cp->ip6c_src;
928 pfctlinput2(cmd, sa, (void *)&ip6cp1);
931 * Then go to special cases that need ESP header information.
932 * XXX: We assume that when ip6 is non NULL,
933 * M and OFF are valid.
936 /* check if we can safely examine src and dst ports */
937 if (m->m_pkthdr.len < off + sizeof(esp))
940 if (m->m_len < off + sizeof(esp)) {
942 * this should be rare case,
943 * so we compromise on this copy...
945 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
948 espp = (struct newesp*)(mtod(m, caddr_t) + off);
950 if (cmd == PRC_MSGSIZE) {
954 * Check to see if we have a valid SA corresponding to
955 * the address in the ICMP message payload.
957 sa6_src = ip6cp->ip6c_src;
958 sa6_dst = (struct sockaddr_in6 *)sa;
959 sav = key_allocsa(AF_INET6,
960 (caddr_t)&sa6_src->sin6_addr,
961 (caddr_t)&sa6_dst->sin6_addr,
962 IPPROTO_ESP, espp->esp_spi);
964 if (sav->state == SADB_SASTATE_MATURE ||
965 sav->state == SADB_SASTATE_DYING)
970 /* XXX Further validation? */
973 * Depending on the value of "valid" and routing table
974 * size (mtudisc_{hi,lo}wat), we will:
975 * - recalcurate the new MTU and create the
976 * corresponding routing entry, or
977 * - ignore the MTU change notification.
979 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
982 /* we normally notify any pcb here */