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>
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;
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 *);
139 esp = (struct esp *)(((u_int8_t *)ip) + off);
141 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
143 hlen = ip->ip_hl << 2;
146 /* find the sassoc. */
149 if ((sav = key_allocsa(AF_INET,
150 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
151 IPPROTO_ESP, spi)) == 0) {
152 ipseclog((LOG_WARNING,
153 "IPv4 ESP input: no key association found for spi %u\n",
154 (u_int32_t)ntohl(spi)));
158 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
159 printf("DP esp4_input called to allocate SA:%p\n", sav));
160 if (sav->state != SADB_SASTATE_MATURE
161 && sav->state != SADB_SASTATE_DYING) {
163 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
164 (u_int32_t)ntohl(spi)));
165 ipsecstat.in_badspi++;
168 algo = esp_algorithm_lookup(sav->alg_enc);
170 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
171 "unsupported encryption algorithm for spi %u\n",
172 (u_int32_t)ntohl(spi)));
173 ipsecstat.in_badspi++;
177 /* check if we have proper ivlen information */
180 ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
181 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
182 ipsecstat.in_inval++;
186 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
187 && (sav->alg_auth && sav->key_auth)))
190 if (sav->alg_auth == SADB_X_AALG_NULL ||
191 sav->alg_auth == SADB_AALG_NONE)
195 * check for sequence number.
197 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
200 ipsecstat.in_espreplay++;
201 ipseclog((LOG_WARNING,
202 "replay packet in IPv4 ESP input: %s %s\n",
203 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
209 u_int8_t sum0[AH_MAXSUMSIZE];
210 u_int8_t sum[AH_MAXSUMSIZE];
211 const struct ah_algorithm *sumalgo;
214 sumalgo = ah_algorithm_lookup(sav->alg_auth);
217 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
218 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
219 ipsecstat.in_inval++;
222 if (AH_MAXSUMSIZE < siz) {
224 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
226 ipsecstat.in_inval++;
230 m_copydata(m, m->m_pkthdr.len - siz, siz, (caddr_t)&sum0[0]);
232 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
233 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
234 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
235 ipsecstat.in_espauthfail++;
239 if (bcmp(sum0, sum, siz) != 0) {
240 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
241 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
242 ipsecstat.in_espauthfail++;
246 /* strip off the authentication data */
248 ip = mtod(m, struct ip *);
250 ip->ip_len = ip->ip_len - siz;
252 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
254 m->m_flags |= M_AUTHIPDGM;
255 ipsecstat.in_espauthsucc++;
259 * update sequence number.
261 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
262 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
263 ipsecstat.in_espreplay++;
270 /* process main esp header. */
271 if (sav->flags & SADB_X_EXT_OLD) {
273 esplen = sizeof(struct esp);
276 if (sav->flags & SADB_X_EXT_DERIV)
277 esplen = sizeof(struct esp);
279 esplen = sizeof(struct newesp);
282 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
283 ipseclog((LOG_WARNING,
284 "IPv4 ESP input: packet too short\n"));
285 ipsecstat.in_inval++;
289 if (m->m_len < off + esplen + ivlen) {
290 m = m_pullup(m, off + esplen + ivlen);
293 "IPv4 ESP input: can't pullup in esp4_input\n"));
294 ipsecstat.in_inval++;
300 * pre-compute and cache intermediate key
302 if (esp_schedule(algo, sav) != 0) {
303 ipsecstat.in_inval++;
308 * decrypt the packet.
311 panic("internal error: no decrypt function");
312 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
313 /* m is already freed */
315 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
316 ipsec_logsastr(sav)));
317 ipsecstat.in_inval++;
320 ipsecstat.in_esphist[sav->alg_enc]++;
322 m->m_flags |= M_DECRYPTED;
325 * find the trailer of the ESP.
327 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
329 nxt = esptail.esp_nxt;
330 taillen = esptail.esp_padlen + sizeof(esptail);
332 if (m->m_pkthdr.len < taillen ||
333 m->m_pkthdr.len - taillen < off + esplen + ivlen + sizeof(esptail)) {
334 ipseclog((LOG_WARNING,
335 "bad pad length in IPv4 ESP input: %s %s\n",
336 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
337 ipsecstat.in_inval++;
341 /* strip off the trailing pad area. */
345 ip->ip_len = ip->ip_len - taillen;
347 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
350 /* was it transmitted over the IPsec tunnel SA? */
351 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
353 * strip off all the headers that precedes ESP header.
354 * IP4 xx ESP IP4' payload -> IP4' payload
356 * XXX more sanity checks
357 * XXX relationship with gif?
362 m_adj(m, off + esplen + ivlen);
363 if (m->m_len < sizeof(*ip)) {
364 m = m_pullup(m, sizeof(*ip));
366 ipsecstat.in_inval++;
370 ip = mtod(m, struct ip *);
371 /* ECN consideration. */
372 if (!ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos)) {
373 ipsecstat.in_inval++;
376 if (!key_checktunnelsanity(sav, AF_INET,
377 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
378 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
379 "in IPv4 ESP input: %s %s\n",
380 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
381 ipsecstat.in_inval++;
385 key_sa_recordxfer(sav, m);
386 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
387 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
388 ipsecstat.in_nomem++;
392 if (netisr_queue(NETISR_IP, m)) { /* (0) on success. */
393 ipsecstat.in_inval++;
401 * strip off ESP header and IV.
402 * even in m_pulldown case, we need to strip off ESP so that
403 * we can always compute checksum for AH correctly.
407 stripsiz = esplen + ivlen;
409 ip = mtod(m, struct ip *);
410 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
411 m->m_data += stripsiz;
412 m->m_len -= stripsiz;
413 m->m_pkthdr.len -= stripsiz;
415 ip = mtod(m, struct ip *);
417 ip->ip_len = ip->ip_len - stripsiz;
419 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
423 key_sa_recordxfer(sav, m);
424 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
425 ipsecstat.in_nomem++;
429 if (nxt != IPPROTO_DONE) {
430 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
431 ipsec4_in_reject(m, NULL)) {
432 ipsecstat.in_polvio++;
435 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
442 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
443 printf("DP esp4_input call free SA:%p\n", sav));
446 ipsecstat.in_success++;
451 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
452 printf("DP esp4_input call free SA:%p\n", sav));
463 esp6_input(mp, offp, proto)
467 struct mbuf *m = *mp;
471 struct esptail esptail;
473 struct secasvar *sav = NULL;
476 const struct esp_algorithm *algo;
480 /* sanity check for alignment. */
481 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
482 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
483 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
484 ipsec6stat.in_inval++;
488 #ifndef PULLDOWN_TEST
489 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
490 esp = (struct esp *)(mtod(m, caddr_t) + off);
492 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
494 ipsec6stat.in_inval++;
498 ip6 = mtod(m, struct ip6_hdr *);
500 if (ntohs(ip6->ip6_plen) == 0) {
501 ipseclog((LOG_ERR, "IPv6 ESP input: "
502 "ESP with IPv6 jumbogram is not supported.\n"));
503 ipsec6stat.in_inval++;
507 /* find the sassoc. */
510 if ((sav = key_allocsa(AF_INET6,
511 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
512 IPPROTO_ESP, spi)) == 0) {
513 ipseclog((LOG_WARNING,
514 "IPv6 ESP input: no key association found for spi %u\n",
515 (u_int32_t)ntohl(spi)));
516 ipsec6stat.in_nosa++;
519 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
520 printf("DP esp6_input called to allocate SA:%p\n", sav));
521 if (sav->state != SADB_SASTATE_MATURE
522 && sav->state != SADB_SASTATE_DYING) {
524 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
525 (u_int32_t)ntohl(spi)));
526 ipsec6stat.in_badspi++;
529 algo = esp_algorithm_lookup(sav->alg_enc);
531 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
532 "unsupported encryption algorithm for spi %u\n",
533 (u_int32_t)ntohl(spi)));
534 ipsec6stat.in_badspi++;
538 /* check if we have proper ivlen information */
541 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
542 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
543 ipsec6stat.in_badspi++;
547 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
548 && (sav->alg_auth && sav->key_auth)))
551 if (sav->alg_auth == SADB_X_AALG_NULL ||
552 sav->alg_auth == SADB_AALG_NONE)
556 * check for sequence number.
558 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
561 ipsec6stat.in_espreplay++;
562 ipseclog((LOG_WARNING,
563 "replay packet in IPv6 ESP input: %s %s\n",
564 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
570 u_char sum0[AH_MAXSUMSIZE];
571 u_char sum[AH_MAXSUMSIZE];
572 const struct ah_algorithm *sumalgo;
575 sumalgo = ah_algorithm_lookup(sav->alg_auth);
578 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
579 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
580 ipsec6stat.in_inval++;
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, (caddr_t)&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 m = m_pullup(m, sizeof(*ip6));
721 ipsec6stat.in_inval++;
725 ip6 = mtod(m, struct ip6_hdr *);
726 /* ECN consideration. */
727 if (!ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow)) {
728 ipsec6stat.in_inval++;
731 if (!key_checktunnelsanity(sav, AF_INET6,
732 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
733 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
734 "in IPv6 ESP input: %s %s\n",
735 ipsec6_logpacketstr(ip6, spi),
736 ipsec_logsastr(sav)));
737 ipsec6stat.in_inval++;
741 key_sa_recordxfer(sav, m);
742 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
743 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
744 ipsec6stat.in_nomem++;
748 if (netisr_queue(NETISR_IPV6, m)) { /* (0) on success. */
749 ipsec6stat.in_inval++;
757 * strip off ESP header and IV.
758 * even in m_pulldown case, we need to strip off ESP so that
759 * we can always compute checksum for AH correctly.
765 * Set the next header field of the previous header correctly.
767 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
770 stripsiz = esplen + ivlen;
772 ip6 = mtod(m, struct ip6_hdr *);
773 if (m->m_len >= stripsiz + off) {
774 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
775 m->m_data += stripsiz;
776 m->m_len -= stripsiz;
777 m->m_pkthdr.len -= stripsiz;
780 * this comes with no copy if the boundary is on
785 n = m_split(m, off, M_DONTWAIT);
787 /* m is retained by m_split */
791 /* m_cat does not update m_pkthdr.len */
792 m->m_pkthdr.len += n->m_pkthdr.len;
796 #ifndef PULLDOWN_TEST
798 * KAME requires that the packet to be contiguous on the
799 * mbuf. We need to make that sure.
800 * this kind of code should be avoided.
801 * XXX other conditions to avoid running this part?
803 if (m->m_len != m->m_pkthdr.len) {
804 struct mbuf *n = NULL;
807 MGETHDR(n, M_DONTWAIT, MT_HEADER);
811 if (n && n->m_pkthdr.len > maxlen) {
812 MCLGET(n, M_DONTWAIT);
814 if ((n->m_flags & M_EXT) == 0) {
820 printf("esp6_input: mbuf allocation failed\n");
824 if (n->m_pkthdr.len <= maxlen) {
825 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
826 n->m_len = n->m_pkthdr.len;
830 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
839 ip6 = mtod(m, struct ip6_hdr *);
840 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
842 key_sa_recordxfer(sav, m);
843 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
844 ipsec6stat.in_nomem++;
853 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
854 printf("DP esp6_input call free SA:%p\n", sav));
857 ipsec6stat.in_success++;
862 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
863 printf("DP esp6_input call free SA:%p\n", sav));
872 esp6_ctlinput(cmd, sa, d)
877 const struct newesp *espp;
879 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
880 struct secasvar *sav;
884 struct sockaddr_in6 *sa6_src, *sa6_dst;
886 if (sa->sa_family != AF_INET6 ||
887 sa->sa_len != sizeof(struct sockaddr_in6))
889 if ((unsigned)cmd >= PRC_NCMDS)
892 /* if the parameter is from icmp6, decode it. */
894 ip6cp = (struct ip6ctlparam *)d;
896 ip6 = ip6cp->ip6c_ip6;
897 off = ip6cp->ip6c_off;
901 off = 0; /* calm gcc */
906 * Notify the error to all possible sockets via pfctlinput2.
907 * Since the upper layer information (such as protocol type,
908 * source and destination ports) is embedded in the encrypted
909 * data and might have been cut, we can't directly call
910 * an upper layer ctlinput function. However, the pcbnotify
911 * function will consider source and destination addresses
912 * as well as the flow info value, and may be able to find
913 * some PCB that should be notified.
914 * Although pfctlinput2 will call esp6_ctlinput(), there is
915 * no possibility of an infinite loop of function calls,
916 * because we don't pass the inner IPv6 header.
918 bzero(&ip6cp1, sizeof(ip6cp1));
919 ip6cp1.ip6c_src = ip6cp->ip6c_src;
920 pfctlinput2(cmd, sa, (void *)&ip6cp1);
923 * Then go to special cases that need ESP header information.
924 * XXX: We assume that when ip6 is non NULL,
925 * M and OFF are valid.
928 /* check if we can safely examine src and dst ports */
929 if (m->m_pkthdr.len < off + sizeof(esp))
932 if (m->m_len < off + sizeof(esp)) {
934 * this should be rare case,
935 * so we compromise on this copy...
937 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
940 espp = (struct newesp*)(mtod(m, caddr_t) + off);
942 if (cmd == PRC_MSGSIZE) {
946 * Check to see if we have a valid SA corresponding to
947 * the address in the ICMP message payload.
949 sa6_src = ip6cp->ip6c_src;
950 sa6_dst = (struct sockaddr_in6 *)sa;
951 sav = key_allocsa(AF_INET6,
952 (caddr_t)&sa6_src->sin6_addr,
953 (caddr_t)&sa6_dst->sin6_addr,
954 IPPROTO_ESP, espp->esp_spi);
956 if (sav->state == SADB_SASTATE_MATURE ||
957 sav->state == SADB_SASTATE_DYING)
962 /* XXX Further validation? */
965 * Depending on the value of "valid" and routing table
966 * size (mtudisc_{hi,lo}wat), we will:
967 * - recalcurate the new MTU and create the
968 * corresponding routing entry, or
969 * - ignore the MTU change notification.
971 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
974 /* we normally notify any pcb here */