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.8 2004/10/20 05:00:36 hsu 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) &&
434 ipsec4_in_reject(m, NULL)) {
435 ipsecstat.in_polvio++;
438 if (!ip_lengthcheck(&m)) {
439 m = NULL; /* freed in ip_lengthcheck() */
442 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
449 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
450 printf("DP esp4_input call free SA:%p\n", sav));
453 ipsecstat.in_success++;
458 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
459 printf("DP esp4_input call free SA:%p\n", sav));
470 esp6_input(mp, offp, proto)
474 struct mbuf *m = *mp;
478 struct esptail esptail;
480 struct secasvar *sav = NULL;
483 const struct esp_algorithm *algo;
487 /* sanity check for alignment. */
488 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
489 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
490 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
491 ipsec6stat.in_inval++;
495 #ifndef PULLDOWN_TEST
496 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
497 esp = (struct esp *)(mtod(m, caddr_t) + off);
499 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
501 ipsec6stat.in_inval++;
505 ip6 = mtod(m, struct ip6_hdr *);
507 if (ntohs(ip6->ip6_plen) == 0) {
508 ipseclog((LOG_ERR, "IPv6 ESP input: "
509 "ESP with IPv6 jumbogram is not supported.\n"));
510 ipsec6stat.in_inval++;
514 /* find the sassoc. */
517 if ((sav = key_allocsa(AF_INET6,
518 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
519 IPPROTO_ESP, spi)) == 0) {
520 ipseclog((LOG_WARNING,
521 "IPv6 ESP input: no key association found for spi %u\n",
522 (u_int32_t)ntohl(spi)));
523 ipsec6stat.in_nosa++;
526 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
527 printf("DP esp6_input called to allocate SA:%p\n", sav));
528 if (sav->state != SADB_SASTATE_MATURE
529 && sav->state != SADB_SASTATE_DYING) {
531 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
532 (u_int32_t)ntohl(spi)));
533 ipsec6stat.in_badspi++;
536 algo = esp_algorithm_lookup(sav->alg_enc);
538 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
539 "unsupported encryption algorithm for spi %u\n",
540 (u_int32_t)ntohl(spi)));
541 ipsec6stat.in_badspi++;
545 /* check if we have proper ivlen information */
548 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
549 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
550 ipsec6stat.in_badspi++;
554 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
555 && (sav->alg_auth && sav->key_auth)))
558 if (sav->alg_auth == SADB_X_AALG_NULL ||
559 sav->alg_auth == SADB_AALG_NONE)
563 * check for sequence number.
565 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
568 ipsec6stat.in_espreplay++;
569 ipseclog((LOG_WARNING,
570 "replay packet in IPv6 ESP input: %s %s\n",
571 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
577 u_char sum0[AH_MAXSUMSIZE];
578 u_char sum[AH_MAXSUMSIZE];
579 const struct ah_algorithm *sumalgo;
582 sumalgo = ah_algorithm_lookup(sav->alg_auth);
585 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
586 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
587 ipsecstat.in_inval++;
590 if (AH_MAXSUMSIZE < siz) {
592 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
594 ipsec6stat.in_inval++;
598 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
600 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
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 if (bcmp(sum0, sum, siz) != 0) {
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 /* strip off the authentication data */
616 ip6 = mtod(m, struct ip6_hdr *);
617 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
619 m->m_flags |= M_AUTHIPDGM;
620 ipsec6stat.in_espauthsucc++;
624 * update sequence number.
626 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
627 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
628 ipsec6stat.in_espreplay++;
635 /* process main esp header. */
636 if (sav->flags & SADB_X_EXT_OLD) {
638 esplen = sizeof(struct esp);
641 if (sav->flags & SADB_X_EXT_DERIV)
642 esplen = sizeof(struct esp);
644 esplen = sizeof(struct newesp);
647 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
648 ipseclog((LOG_WARNING,
649 "IPv6 ESP input: packet too short\n"));
650 ipsec6stat.in_inval++;
654 #ifndef PULLDOWN_TEST
655 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
657 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
659 ipsec6stat.in_inval++;
664 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
667 * pre-compute and cache intermediate key
669 if (esp_schedule(algo, sav) != 0) {
670 ipsec6stat.in_inval++;
675 * decrypt the packet.
678 panic("internal error: no decrypt function");
679 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
680 /* m is already freed */
682 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
683 ipsec_logsastr(sav)));
684 ipsec6stat.in_inval++;
687 ipsec6stat.in_esphist[sav->alg_enc]++;
689 m->m_flags |= M_DECRYPTED;
692 * find the trailer of the ESP.
694 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
696 nxt = esptail.esp_nxt;
697 taillen = esptail.esp_padlen + sizeof(esptail);
699 if (m->m_pkthdr.len < taillen
700 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
701 ipseclog((LOG_WARNING,
702 "bad pad length in IPv6 ESP input: %s %s\n",
703 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
704 ipsec6stat.in_inval++;
708 /* strip off the trailing pad area. */
711 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
713 /* was it transmitted over the IPsec tunnel SA? */
714 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
716 * strip off all the headers that precedes ESP header.
717 * IP6 xx ESP IP6' payload -> IP6' payload
719 * XXX more sanity checks
720 * XXX relationship with gif?
722 u_int32_t flowinfo; /* net endian */
723 flowinfo = ip6->ip6_flow;
724 m_adj(m, off + esplen + ivlen);
725 if (m->m_len < sizeof(*ip6)) {
726 #ifndef PULLDOWN_TEST
728 * m_pullup is prohibited in KAME IPv6 input processing
729 * but there's no other way!
732 /* okay to pullup in m_pulldown style */
734 m = m_pullup(m, sizeof(*ip6));
736 ipsec6stat.in_inval++;
740 ip6 = mtod(m, struct ip6_hdr *);
741 /* ECN consideration. */
742 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
743 if (!key_checktunnelsanity(sav, AF_INET6,
744 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
745 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
746 "in IPv6 ESP input: %s %s\n",
747 ipsec6_logpacketstr(ip6, spi),
748 ipsec_logsastr(sav)));
749 ipsec6stat.in_inval++;
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 (netisr_queue(NETISR_IPV6, m)) {
761 ipsec6stat.in_inval++;
769 * strip off ESP header and IV.
770 * even in m_pulldown case, we need to strip off ESP so that
771 * we can always compute checksum for AH correctly.
777 * Set the next header field of the previous header correctly.
779 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
782 stripsiz = esplen + ivlen;
784 ip6 = mtod(m, struct ip6_hdr *);
785 if (m->m_len >= stripsiz + off) {
786 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
787 m->m_data += stripsiz;
788 m->m_len -= stripsiz;
789 m->m_pkthdr.len -= stripsiz;
792 * this comes with no copy if the boundary is on
797 n = m_split(m, off, MB_DONTWAIT);
799 /* m is retained by m_split */
804 /* m_cat does not update m_pkthdr.len */
805 m->m_pkthdr.len += n->m_pkthdr.len;
808 #ifndef PULLDOWN_TEST
810 * KAME requires that the packet to be contiguous on the
811 * mbuf. We need to make that sure.
812 * this kind of code should be avoided.
813 * XXX other conditions to avoid running this part?
815 if (m->m_len != m->m_pkthdr.len) {
816 struct mbuf *n = NULL;
819 MGETHDR(n, MB_DONTWAIT, MT_HEADER);
823 if (n && n->m_pkthdr.len > maxlen) {
824 MCLGET(n, MB_DONTWAIT);
826 if ((n->m_flags & M_EXT) == 0) {
832 printf("esp6_input: mbuf allocation failed\n");
836 if (n->m_pkthdr.len <= maxlen) {
837 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
838 n->m_len = n->m_pkthdr.len;
842 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
851 ip6 = mtod(m, struct ip6_hdr *);
852 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
854 key_sa_recordxfer(sav, m);
855 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
856 ipsec6stat.in_nomem++;
865 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
866 printf("DP esp6_input call free SA:%p\n", sav));
869 ipsec6stat.in_success++;
874 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
875 printf("DP esp6_input call free SA:%p\n", sav));
884 esp6_ctlinput(cmd, sa, d)
889 const struct newesp *espp;
891 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
892 struct secasvar *sav;
896 struct sockaddr_in6 *sa6_src, *sa6_dst;
898 if (sa->sa_family != AF_INET6 ||
899 sa->sa_len != sizeof(struct sockaddr_in6))
901 if ((unsigned)cmd >= PRC_NCMDS)
904 /* if the parameter is from icmp6, decode it. */
906 ip6cp = (struct ip6ctlparam *)d;
908 ip6 = ip6cp->ip6c_ip6;
909 off = ip6cp->ip6c_off;
913 off = 0; /* fix warning */
918 * Notify the error to all possible sockets via pfctlinput2.
919 * Since the upper layer information (such as protocol type,
920 * source and destination ports) is embedded in the encrypted
921 * data and might have been cut, we can't directly call
922 * an upper layer ctlinput function. However, the pcbnotify
923 * function will consider source and destination addresses
924 * as well as the flow info value, and may be able to find
925 * some PCB that should be notified.
926 * Although pfctlinput2 will call esp6_ctlinput(), there is
927 * no possibility of an infinite loop of function calls,
928 * because we don't pass the inner IPv6 header.
930 bzero(&ip6cp1, sizeof(ip6cp1));
931 ip6cp1.ip6c_src = ip6cp->ip6c_src;
932 pfctlinput2(cmd, sa, (void *)&ip6cp1);
935 * Then go to special cases that need ESP header information.
936 * XXX: We assume that when ip6 is non NULL,
937 * M and OFF are valid.
940 /* check if we can safely examine src and dst ports */
941 if (m->m_pkthdr.len < off + sizeof(esp))
944 if (m->m_len < off + sizeof(esp)) {
946 * this should be rare case,
947 * so we compromise on this copy...
949 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
952 espp = (struct newesp*)(mtod(m, caddr_t) + off);
954 if (cmd == PRC_MSGSIZE) {
958 * Check to see if we have a valid SA corresponding to
959 * the address in the ICMP message payload.
961 sa6_src = ip6cp->ip6c_src;
962 sa6_dst = (struct sockaddr_in6 *)sa;
963 sav = key_allocsa(AF_INET6,
964 (caddr_t)&sa6_src->sin6_addr,
965 (caddr_t)&sa6_dst->sin6_addr,
966 IPPROTO_ESP, espp->esp_spi);
968 if (sav->state == SADB_SASTATE_MATURE ||
969 sav->state == SADB_SASTATE_DYING)
974 /* XXX Further validation? */
977 * Depending on the value of "valid" and routing table
978 * size (mtudisc_{hi,lo}wat), we will:
979 * - recalcurate the new MTU and create the
980 * corresponding routing entry, or
981 * - ignore the MTU change notification.
983 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
986 /* we normally notify any pcb here */