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;
125 /* sanity check for alignment. */
126 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
127 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
128 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
129 ipsecstat.in_inval++;
133 if (m->m_len < off + ESPMAXLEN) {
134 m = m_pullup(m, off + ESPMAXLEN);
137 "IPv4 ESP input: can't pullup in esp4_input\n"));
138 ipsecstat.in_inval++;
143 ip = mtod(m, struct ip *);
144 esp = (struct esp *)(((u_int8_t *)ip) + off);
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)) == NULL) {
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 kprintf("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, "improper 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_char sum0[AH_MAXSUMSIZE];
210 u_char 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, &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 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
373 if (!key_checktunnelsanity(sav, AF_INET,
374 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
375 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
376 "in IPv4 ESP input: %s %s\n",
377 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
378 ipsecstat.in_inval++;
382 key_sa_recordxfer(sav, m);
383 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
384 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
385 ipsecstat.in_nomem++;
389 if (netisr_queue(NETISR_IP, m)) {
390 ipsecstat.in_inval++;
398 * strip off ESP header and IV.
399 * even in m_pulldown case, we need to strip off ESP so that
400 * we can always compute checksum for AH correctly.
404 stripsiz = esplen + ivlen;
406 ip = mtod(m, struct ip *);
407 bcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
408 m->m_data += stripsiz;
409 m->m_len -= stripsiz;
410 m->m_pkthdr.len -= stripsiz;
412 ip = mtod(m, struct ip *);
414 ip->ip_len = ip->ip_len - stripsiz;
416 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
420 key_sa_recordxfer(sav, m);
421 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
422 ipsecstat.in_nomem++;
426 if (nxt != IPPROTO_DONE) {
427 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) &&
428 ipsec4_in_reject(m, NULL)) {
429 ipsecstat.in_polvio++;
432 if (!ip_lengthcheck(&m, 0)) {
433 /* freed in ip_lengthcheck() */
438 (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
446 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
447 kprintf("DP esp4_input call free SA:%p\n", sav));
450 ipsecstat.in_success++;
451 return(IPPROTO_DONE);
455 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
456 kprintf("DP esp4_input call free SA:%p\n", sav));
461 return(IPPROTO_DONE);
467 esp6_input(struct mbuf **mp, int *offp, int proto)
469 struct mbuf *m = *mp;
473 struct esptail esptail;
475 struct secasvar *sav = NULL;
478 const struct esp_algorithm *algo;
482 /* sanity check for alignment. */
483 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
484 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
485 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
486 ipsec6stat.in_inval++;
490 #ifndef PULLDOWN_TEST
491 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
492 esp = (struct esp *)(mtod(m, caddr_t) + off);
494 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
496 ipsec6stat.in_inval++;
500 ip6 = mtod(m, struct ip6_hdr *);
502 if (ntohs(ip6->ip6_plen) == 0) {
503 ipseclog((LOG_ERR, "IPv6 ESP input: "
504 "ESP with IPv6 jumbogram is not supported.\n"));
505 ipsec6stat.in_inval++;
509 /* find the sassoc. */
512 if ((sav = key_allocsa(AF_INET6,
513 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
514 IPPROTO_ESP, spi)) == NULL) {
515 ipseclog((LOG_WARNING,
516 "IPv6 ESP input: no key association found for spi %u\n",
517 (u_int32_t)ntohl(spi)));
518 ipsec6stat.in_nosa++;
521 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
522 kprintf("DP esp6_input called to allocate SA:%p\n", sav));
523 if (sav->state != SADB_SASTATE_MATURE
524 && sav->state != SADB_SASTATE_DYING) {
526 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
527 (u_int32_t)ntohl(spi)));
528 ipsec6stat.in_badspi++;
531 algo = esp_algorithm_lookup(sav->alg_enc);
533 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
534 "unsupported encryption algorithm for spi %u\n",
535 (u_int32_t)ntohl(spi)));
536 ipsec6stat.in_badspi++;
540 /* check if we have proper ivlen information */
543 ipseclog((LOG_ERR, "improper ivlen in IPv6 ESP input: %s %s\n",
544 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
545 ipsec6stat.in_badspi++;
549 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
550 && (sav->alg_auth && sav->key_auth)))
553 if (sav->alg_auth == SADB_X_AALG_NULL ||
554 sav->alg_auth == SADB_AALG_NONE)
558 * check for sequence number.
560 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
563 ipsec6stat.in_espreplay++;
564 ipseclog((LOG_WARNING,
565 "replay packet in IPv6 ESP input: %s %s\n",
566 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
572 u_char sum0[AH_MAXSUMSIZE];
573 u_char sum[AH_MAXSUMSIZE];
574 const struct ah_algorithm *sumalgo;
577 sumalgo = ah_algorithm_lookup(sav->alg_auth);
580 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
581 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
582 ipsec6stat.in_inval++;
585 if (AH_MAXSUMSIZE < siz) {
587 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
589 ipsec6stat.in_inval++;
593 m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
595 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
596 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
597 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
598 ipsec6stat.in_espauthfail++;
602 if (bcmp(sum0, sum, siz) != 0) {
603 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
604 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
605 ipsec6stat.in_espauthfail++;
609 /* strip off the authentication data */
611 ip6 = mtod(m, struct ip6_hdr *);
612 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
614 m->m_flags |= M_AUTHIPDGM;
615 ipsec6stat.in_espauthsucc++;
619 * update sequence number.
621 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
622 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
623 ipsec6stat.in_espreplay++;
630 /* process main esp header. */
631 if (sav->flags & SADB_X_EXT_OLD) {
633 esplen = sizeof(struct esp);
636 if (sav->flags & SADB_X_EXT_DERIV)
637 esplen = sizeof(struct esp);
639 esplen = sizeof(struct newesp);
642 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
643 ipseclog((LOG_WARNING,
644 "IPv6 ESP input: packet too short\n"));
645 ipsec6stat.in_inval++;
649 #ifndef PULLDOWN_TEST
650 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
652 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
654 ipsec6stat.in_inval++;
659 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
662 * pre-compute and cache intermediate key
664 if (esp_schedule(algo, sav) != 0) {
665 ipsec6stat.in_inval++;
670 * decrypt the packet.
673 panic("internal error: no decrypt function");
674 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
675 /* m is already freed */
677 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
678 ipsec_logsastr(sav)));
679 ipsec6stat.in_inval++;
682 ipsec6stat.in_esphist[sav->alg_enc]++;
684 m->m_flags |= M_DECRYPTED;
687 * find the trailer of the ESP.
689 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
691 nxt = esptail.esp_nxt;
692 taillen = esptail.esp_padlen + sizeof(esptail);
694 if (m->m_pkthdr.len < taillen
695 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
696 ipseclog((LOG_WARNING,
697 "bad pad length in IPv6 ESP input: %s %s\n",
698 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
699 ipsec6stat.in_inval++;
703 /* strip off the trailing pad area. */
706 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
708 /* was it transmitted over the IPsec tunnel SA? */
709 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
711 * strip off all the headers that precedes ESP header.
712 * IP6 xx ESP IP6' payload -> IP6' payload
714 * XXX more sanity checks
715 * XXX relationship with gif?
717 u_int32_t flowinfo; /* net endian */
718 flowinfo = ip6->ip6_flow;
719 m_adj(m, off + esplen + ivlen);
720 if (m->m_len < sizeof(*ip6)) {
721 #ifndef PULLDOWN_TEST
723 * m_pullup is prohibited in KAME IPv6 input processing
724 * but there's no other way!
727 /* okay to pullup in m_pulldown style */
729 m = m_pullup(m, sizeof(*ip6));
731 ipsec6stat.in_inval++;
735 ip6 = mtod(m, struct ip6_hdr *);
736 /* ECN consideration. */
737 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
738 if (!key_checktunnelsanity(sav, AF_INET6,
739 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
740 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
741 "in IPv6 ESP input: %s %s\n",
742 ipsec6_logpacketstr(ip6, spi),
743 ipsec_logsastr(sav)));
744 ipsec6stat.in_inval++;
748 key_sa_recordxfer(sav, m);
749 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
750 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
751 ipsec6stat.in_nomem++;
755 if (netisr_queue(NETISR_IPV6, m)) {
756 ipsec6stat.in_inval++;
764 * strip off ESP header and IV.
765 * even in m_pulldown case, we need to strip off ESP so that
766 * we can always compute checksum for AH correctly.
772 * Set the next header field of the previous header correctly.
774 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
777 stripsiz = esplen + ivlen;
779 ip6 = mtod(m, struct ip6_hdr *);
780 if (m->m_len >= stripsiz + off) {
781 bcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
782 m->m_data += stripsiz;
783 m->m_len -= stripsiz;
784 m->m_pkthdr.len -= stripsiz;
787 * this comes with no copy if the boundary is on
792 n = m_split(m, off, M_NOWAIT);
794 /* m is retained by m_split */
798 /* m_cat does not update m_pkthdr.len */
799 m->m_pkthdr.len += n->m_pkthdr.len;
803 #ifndef PULLDOWN_TEST
805 * KAME requires that the packet to be contiguous on the
806 * mbuf. We need to make that sure.
807 * this kind of code should be avoided.
808 * XXX other conditions to avoid running this part?
810 if (m->m_len != m->m_pkthdr.len) {
811 struct mbuf *n = NULL;
814 n = m_getb(m->m_pkthdr.len, M_NOWAIT, MT_HEADER,
817 kprintf("esp6_input: mbuf allocation failed\n");
822 maxlen = (n->m_flags & M_EXT) ? MCLBYTES : MHLEN;
823 if (n->m_pkthdr.len <= maxlen) {
824 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
825 n->m_len = n->m_pkthdr.len;
829 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
838 ip6 = mtod(m, struct ip6_hdr *);
839 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
841 key_sa_recordxfer(sav, m);
842 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
843 ipsec6stat.in_nomem++;
852 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
853 kprintf("DP esp6_input call free SA:%p\n", sav));
856 ipsec6stat.in_success++;
861 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
862 kprintf("DP esp6_input call free SA:%p\n", sav));
871 esp6_ctlinput(netmsg_t msg)
873 int cmd = msg->ctlinput.nm_cmd;
874 struct sockaddr *sa = msg->ctlinput.nm_arg;
875 void *d = msg->ctlinput.nm_extra;
876 const struct newesp *espp;
878 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
879 struct secasvar *sav;
883 struct sockaddr_in6 *sa6_src, *sa6_dst;
885 if (sa->sa_family != AF_INET6 ||
886 sa->sa_len != sizeof(struct sockaddr_in6))
888 if ((unsigned)cmd >= PRC_NCMDS)
891 /* if the parameter is from icmp6, decode it. */
893 ip6cp = (struct ip6ctlparam *)d;
895 ip6 = ip6cp->ip6c_ip6;
896 off = ip6cp->ip6c_off;
900 off = 0; /* fix warning */
905 * Notify the error to all possible sockets via pfctlinput2.
906 * Since the upper layer information (such as protocol type,
907 * source and destination ports) is embedded in the encrypted
908 * data and might have been cut, we can't directly call
909 * an upper layer ctlinput function. However, the pcbnotify
910 * function will consider source and destination addresses
911 * as well as the flow info value, and may be able to find
912 * some PCB that should be notified.
913 * Although kpfctlinput2 will call esp6_ctlinput(), there is
914 * no possibility of an infinite loop of function calls,
915 * because we don't pass the inner IPv6 header.
917 bzero(&ip6cp1, sizeof(ip6cp1));
918 ip6cp1.ip6c_src = ip6cp->ip6c_src;
919 kpfctlinput2(cmd, sa, (void *)&ip6cp1);
922 * Then go to special cases that need ESP header information.
923 * XXX: We assume that when ip6 is non NULL,
924 * M and OFF are valid.
927 /* check if we can safely examine src and dst ports */
928 if (m->m_pkthdr.len < off + sizeof(esp))
931 if (m->m_len < off + sizeof(esp)) {
933 * this should be rare case,
934 * so we compromise on this copy...
936 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
939 espp = (struct newesp*)(mtod(m, caddr_t) + off);
941 if (cmd == PRC_MSGSIZE) {
945 * Check to see if we have a valid SA corresponding to
946 * the address in the ICMP message payload.
948 sa6_src = ip6cp->ip6c_src;
949 sa6_dst = (struct sockaddr_in6 *)sa;
950 sav = key_allocsa(AF_INET6,
951 (caddr_t)&sa6_src->sin6_addr,
952 (caddr_t)&sa6_dst->sin6_addr,
953 IPPROTO_ESP, espp->esp_spi);
955 if (sav->state == SADB_SASTATE_MATURE ||
956 sav->state == SADB_SASTATE_DYING)
961 /* XXX Further validation? */
964 * Depending on the value of "valid" and routing table
965 * size (mtudisc_{hi,lo}wat), we will:
966 * - recalcurate the new MTU and create the
967 * corresponding routing entry, or
968 * - ignore the MTU change notification.
970 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
973 /* we normally notify any pcb here */
976 lwkt_replymsg(&msg->ctlinput.base.lmsg, 0);