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.17 2008/05/27 01:10:43 dillon 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 extern struct protosw inetsw
[];
104 esp4_input(struct mbuf
*m
, ...)
109 struct esptail esptail
;
111 struct secasvar
*sav
= NULL
;
114 const struct esp_algorithm
*algo
;
121 off
= __va_arg(ap
, int);
122 proto
= __va_arg(ap
, int);
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 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
148 hlen
= ip
->ip_hl
<< 2;
151 /* find the sassoc. */
154 if ((sav
= key_allocsa(AF_INET
,
155 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
156 IPPROTO_ESP
, spi
)) == 0) {
157 ipseclog((LOG_WARNING
,
158 "IPv4 ESP input: no key association found for spi %u\n",
159 (u_int32_t
)ntohl(spi
)));
163 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
164 kprintf("DP esp4_input called to allocate SA:%p\n", sav
));
165 if (sav
->state
!= SADB_SASTATE_MATURE
166 && sav
->state
!= SADB_SASTATE_DYING
) {
168 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
169 (u_int32_t
)ntohl(spi
)));
170 ipsecstat
.in_badspi
++;
173 algo
= esp_algorithm_lookup(sav
->alg_enc
);
175 ipseclog((LOG_DEBUG
, "IPv4 ESP input: "
176 "unsupported encryption algorithm for spi %u\n",
177 (u_int32_t
)ntohl(spi
)));
178 ipsecstat
.in_badspi
++;
182 /* check if we have proper ivlen information */
185 ipseclog((LOG_ERR
, "improper ivlen in IPv4 ESP input: %s %s\n",
186 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
187 ipsecstat
.in_inval
++;
191 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
192 && (sav
->alg_auth
&& sav
->key_auth
)))
195 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
196 sav
->alg_auth
== SADB_AALG_NONE
)
200 * check for sequence number.
202 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
205 ipsecstat
.in_espreplay
++;
206 ipseclog((LOG_WARNING
,
207 "replay packet in IPv4 ESP input: %s %s\n",
208 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
214 u_char sum0
[AH_MAXSUMSIZE
];
215 u_char sum
[AH_MAXSUMSIZE
];
216 const struct ah_algorithm
*sumalgo
;
219 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
222 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
223 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
224 ipsecstat
.in_inval
++;
227 if (AH_MAXSUMSIZE
< siz
) {
229 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
231 ipsecstat
.in_inval
++;
235 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
237 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
238 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
239 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
240 ipsecstat
.in_espauthfail
++;
244 if (bcmp(sum0
, sum
, siz
) != 0) {
245 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
246 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
247 ipsecstat
.in_espauthfail
++;
251 /* strip off the authentication data */
253 ip
= mtod(m
, struct ip
*);
255 ip
->ip_len
= ip
->ip_len
- siz
;
257 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
259 m
->m_flags
|= M_AUTHIPDGM
;
260 ipsecstat
.in_espauthsucc
++;
264 * update sequence number.
266 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
267 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
268 ipsecstat
.in_espreplay
++;
275 /* process main esp header. */
276 if (sav
->flags
& SADB_X_EXT_OLD
) {
278 esplen
= sizeof(struct esp
);
281 if (sav
->flags
& SADB_X_EXT_DERIV
)
282 esplen
= sizeof(struct esp
);
284 esplen
= sizeof(struct newesp
);
287 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
288 ipseclog((LOG_WARNING
,
289 "IPv4 ESP input: packet too short\n"));
290 ipsecstat
.in_inval
++;
294 if (m
->m_len
< off
+ esplen
+ ivlen
) {
295 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
298 "IPv4 ESP input: can't pullup in esp4_input\n"));
299 ipsecstat
.in_inval
++;
305 * pre-compute and cache intermediate key
307 if (esp_schedule(algo
, sav
) != 0) {
308 ipsecstat
.in_inval
++;
313 * decrypt the packet.
316 panic("internal error: no decrypt function");
317 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
318 /* m is already freed */
320 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
321 ipsec_logsastr(sav
)));
322 ipsecstat
.in_inval
++;
325 ipsecstat
.in_esphist
[sav
->alg_enc
]++;
327 m
->m_flags
|= M_DECRYPTED
;
330 * find the trailer of the ESP.
332 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
334 nxt
= esptail
.esp_nxt
;
335 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
337 if (m
->m_pkthdr
.len
< taillen
||
338 m
->m_pkthdr
.len
- taillen
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
339 ipseclog((LOG_WARNING
,
340 "bad pad length in IPv4 ESP input: %s %s\n",
341 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
342 ipsecstat
.in_inval
++;
346 /* strip off the trailing pad area. */
350 ip
->ip_len
= ip
->ip_len
- taillen
;
352 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
355 /* was it transmitted over the IPsec tunnel SA? */
356 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
358 * strip off all the headers that precedes ESP header.
359 * IP4 xx ESP IP4' payload -> IP4' payload
361 * XXX more sanity checks
362 * XXX relationship with gif?
367 m_adj(m
, off
+ esplen
+ ivlen
);
368 if (m
->m_len
< sizeof(*ip
)) {
369 m
= m_pullup(m
, sizeof(*ip
));
371 ipsecstat
.in_inval
++;
375 ip
= mtod(m
, struct ip
*);
376 /* ECN consideration. */
377 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
378 if (!key_checktunnelsanity(sav
, AF_INET
,
379 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
380 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
381 "in IPv4 ESP input: %s %s\n",
382 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
383 ipsecstat
.in_inval
++;
387 key_sa_recordxfer(sav
, m
);
388 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
389 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
390 ipsecstat
.in_nomem
++;
394 if (netisr_queue(NETISR_IP
, m
)) {
395 ipsecstat
.in_inval
++;
403 * strip off ESP header and IV.
404 * even in m_pulldown case, we need to strip off ESP so that
405 * we can always compute checksum for AH correctly.
409 stripsiz
= esplen
+ ivlen
;
411 ip
= mtod(m
, struct ip
*);
412 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
413 m
->m_data
+= stripsiz
;
414 m
->m_len
-= stripsiz
;
415 m
->m_pkthdr
.len
-= stripsiz
;
417 ip
= mtod(m
, struct ip
*);
419 ip
->ip_len
= ip
->ip_len
- stripsiz
;
421 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
425 key_sa_recordxfer(sav
, m
);
426 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
427 ipsecstat
.in_nomem
++;
431 if (nxt
!= IPPROTO_DONE
) {
432 if ((inetsw
[ip_protox
[nxt
]].pr_flags
& PR_LASTHDR
) &&
433 ipsec4_in_reject(m
, NULL
)) {
434 ipsecstat
.in_polvio
++;
437 if (!ip_lengthcheck(&m
)) {
438 /* freed in ip_lengthcheck() */
441 (*inetsw
[ip_protox
[nxt
]].pr_input
)(m
, off
, nxt
);
448 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
449 kprintf("DP esp4_input call free SA:%p\n", sav
));
452 ipsecstat
.in_success
++;
457 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
458 kprintf("DP esp4_input call free SA:%p\n", sav
));
469 esp6_input(struct mbuf
**mp
, int *offp
, int proto
)
471 struct mbuf
*m
= *mp
;
475 struct esptail esptail
;
477 struct secasvar
*sav
= NULL
;
480 const struct esp_algorithm
*algo
;
484 /* sanity check for alignment. */
485 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
486 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
487 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
488 ipsec6stat
.in_inval
++;
492 #ifndef PULLDOWN_TEST
493 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, IPPROTO_DONE
);
494 esp
= (struct esp
*)(mtod(m
, caddr_t
) + off
);
496 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
498 ipsec6stat
.in_inval
++;
502 ip6
= mtod(m
, struct ip6_hdr
*);
504 if (ntohs(ip6
->ip6_plen
) == 0) {
505 ipseclog((LOG_ERR
, "IPv6 ESP input: "
506 "ESP with IPv6 jumbogram is not supported.\n"));
507 ipsec6stat
.in_inval
++;
511 /* find the sassoc. */
514 if ((sav
= key_allocsa(AF_INET6
,
515 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
516 IPPROTO_ESP
, spi
)) == 0) {
517 ipseclog((LOG_WARNING
,
518 "IPv6 ESP input: no key association found for spi %u\n",
519 (u_int32_t
)ntohl(spi
)));
520 ipsec6stat
.in_nosa
++;
523 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
524 kprintf("DP esp6_input called to allocate SA:%p\n", sav
));
525 if (sav
->state
!= SADB_SASTATE_MATURE
526 && sav
->state
!= SADB_SASTATE_DYING
) {
528 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
529 (u_int32_t
)ntohl(spi
)));
530 ipsec6stat
.in_badspi
++;
533 algo
= esp_algorithm_lookup(sav
->alg_enc
);
535 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
536 "unsupported encryption algorithm for spi %u\n",
537 (u_int32_t
)ntohl(spi
)));
538 ipsec6stat
.in_badspi
++;
542 /* check if we have proper ivlen information */
545 ipseclog((LOG_ERR
, "improper ivlen in IPv6 ESP input: %s %s\n",
546 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
547 ipsec6stat
.in_badspi
++;
551 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
552 && (sav
->alg_auth
&& sav
->key_auth
)))
555 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
556 sav
->alg_auth
== SADB_AALG_NONE
)
560 * check for sequence number.
562 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
565 ipsec6stat
.in_espreplay
++;
566 ipseclog((LOG_WARNING
,
567 "replay packet in IPv6 ESP input: %s %s\n",
568 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
574 u_char sum0
[AH_MAXSUMSIZE
];
575 u_char sum
[AH_MAXSUMSIZE
];
576 const struct ah_algorithm
*sumalgo
;
579 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
582 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
583 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
584 ipsec6stat
.in_inval
++;
587 if (AH_MAXSUMSIZE
< siz
) {
589 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
591 ipsec6stat
.in_inval
++;
595 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
597 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
598 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
599 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
600 ipsec6stat
.in_espauthfail
++;
604 if (bcmp(sum0
, sum
, siz
) != 0) {
605 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
606 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
607 ipsec6stat
.in_espauthfail
++;
611 /* strip off the authentication data */
613 ip6
= mtod(m
, struct ip6_hdr
*);
614 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
616 m
->m_flags
|= M_AUTHIPDGM
;
617 ipsec6stat
.in_espauthsucc
++;
621 * update sequence number.
623 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
624 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
625 ipsec6stat
.in_espreplay
++;
632 /* process main esp header. */
633 if (sav
->flags
& SADB_X_EXT_OLD
) {
635 esplen
= sizeof(struct esp
);
638 if (sav
->flags
& SADB_X_EXT_DERIV
)
639 esplen
= sizeof(struct esp
);
641 esplen
= sizeof(struct newesp
);
644 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
645 ipseclog((LOG_WARNING
,
646 "IPv6 ESP input: packet too short\n"));
647 ipsec6stat
.in_inval
++;
651 #ifndef PULLDOWN_TEST
652 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, IPPROTO_DONE
); /* XXX */
654 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
656 ipsec6stat
.in_inval
++;
661 ip6
= mtod(m
, struct ip6_hdr
*); /* set it again just in case */
664 * pre-compute and cache intermediate key
666 if (esp_schedule(algo
, sav
) != 0) {
667 ipsec6stat
.in_inval
++;
672 * decrypt the packet.
675 panic("internal error: no decrypt function");
676 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
677 /* m is already freed */
679 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
680 ipsec_logsastr(sav
)));
681 ipsec6stat
.in_inval
++;
684 ipsec6stat
.in_esphist
[sav
->alg_enc
]++;
686 m
->m_flags
|= M_DECRYPTED
;
689 * find the trailer of the ESP.
691 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
693 nxt
= esptail
.esp_nxt
;
694 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
696 if (m
->m_pkthdr
.len
< taillen
697 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /* ? */
698 ipseclog((LOG_WARNING
,
699 "bad pad length in IPv6 ESP input: %s %s\n",
700 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
701 ipsec6stat
.in_inval
++;
705 /* strip off the trailing pad area. */
708 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
710 /* was it transmitted over the IPsec tunnel SA? */
711 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
713 * strip off all the headers that precedes ESP header.
714 * IP6 xx ESP IP6' payload -> IP6' payload
716 * XXX more sanity checks
717 * XXX relationship with gif?
719 u_int32_t flowinfo
; /* net endian */
720 flowinfo
= ip6
->ip6_flow
;
721 m_adj(m
, off
+ esplen
+ ivlen
);
722 if (m
->m_len
< sizeof(*ip6
)) {
723 #ifndef PULLDOWN_TEST
725 * m_pullup is prohibited in KAME IPv6 input processing
726 * but there's no other way!
729 /* okay to pullup in m_pulldown style */
731 m
= m_pullup(m
, sizeof(*ip6
));
733 ipsec6stat
.in_inval
++;
737 ip6
= mtod(m
, struct ip6_hdr
*);
738 /* ECN consideration. */
739 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
740 if (!key_checktunnelsanity(sav
, AF_INET6
,
741 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
742 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
743 "in IPv6 ESP input: %s %s\n",
744 ipsec6_logpacketstr(ip6
, spi
),
745 ipsec_logsastr(sav
)));
746 ipsec6stat
.in_inval
++;
750 key_sa_recordxfer(sav
, m
);
751 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
752 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
753 ipsec6stat
.in_nomem
++;
757 if (netisr_queue(NETISR_IPV6
, m
)) {
758 ipsec6stat
.in_inval
++;
766 * strip off ESP header and IV.
767 * even in m_pulldown case, we need to strip off ESP so that
768 * we can always compute checksum for AH correctly.
774 * Set the next header field of the previous header correctly.
776 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
779 stripsiz
= esplen
+ ivlen
;
781 ip6
= mtod(m
, struct ip6_hdr
*);
782 if (m
->m_len
>= stripsiz
+ off
) {
783 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
784 m
->m_data
+= stripsiz
;
785 m
->m_len
-= stripsiz
;
786 m
->m_pkthdr
.len
-= stripsiz
;
789 * this comes with no copy if the boundary is on
794 n
= m_split(m
, off
, MB_DONTWAIT
);
796 /* m is retained by m_split */
800 /* m_cat does not update m_pkthdr.len */
801 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
805 #ifndef PULLDOWN_TEST
807 * KAME requires that the packet to be contiguous on the
808 * mbuf. We need to make that sure.
809 * this kind of code should be avoided.
810 * XXX other conditions to avoid running this part?
812 if (m
->m_len
!= m
->m_pkthdr
.len
) {
813 struct mbuf
*n
= NULL
;
816 n
= m_getb(m
->m_pkthdr
.len
, MB_DONTWAIT
, MT_HEADER
,
819 kprintf("esp6_input: mbuf allocation failed\n");
824 maxlen
= (n
->m_flags
& M_EXT
) ? MCLBYTES
: MHLEN
;
825 if (n
->m_pkthdr
.len
<= maxlen
) {
826 m_copydata(m
, 0, n
->m_pkthdr
.len
, mtod(n
, caddr_t
));
827 n
->m_len
= n
->m_pkthdr
.len
;
831 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
840 ip6
= mtod(m
, struct ip6_hdr
*);
841 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
843 key_sa_recordxfer(sav
, m
);
844 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
845 ipsec6stat
.in_nomem
++;
854 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
855 kprintf("DP esp6_input call free SA:%p\n", sav
));
858 ipsec6stat
.in_success
++;
863 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
864 kprintf("DP esp6_input call free SA:%p\n", sav
));
873 esp6_ctlinput(int cmd
, struct sockaddr
*sa
, void *d
)
875 const struct newesp
*espp
;
877 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
878 struct secasvar
*sav
;
882 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
884 if (sa
->sa_family
!= AF_INET6
||
885 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
887 if ((unsigned)cmd
>= PRC_NCMDS
)
890 /* if the parameter is from icmp6, decode it. */
892 ip6cp
= (struct ip6ctlparam
*)d
;
894 ip6
= ip6cp
->ip6c_ip6
;
895 off
= ip6cp
->ip6c_off
;
899 off
= 0; /* fix warning */
904 * Notify the error to all possible sockets via pfctlinput2.
905 * Since the upper layer information (such as protocol type,
906 * source and destination ports) is embedded in the encrypted
907 * data and might have been cut, we can't directly call
908 * an upper layer ctlinput function. However, the pcbnotify
909 * function will consider source and destination addresses
910 * as well as the flow info value, and may be able to find
911 * some PCB that should be notified.
912 * Although kpfctlinput2 will call esp6_ctlinput(), there is
913 * no possibility of an infinite loop of function calls,
914 * because we don't pass the inner IPv6 header.
916 bzero(&ip6cp1
, sizeof(ip6cp1
));
917 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
918 kpfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
921 * Then go to special cases that need ESP header information.
922 * XXX: We assume that when ip6 is non NULL,
923 * M and OFF are valid.
926 /* check if we can safely examine src and dst ports */
927 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
930 if (m
->m_len
< off
+ sizeof(esp
)) {
932 * this should be rare case,
933 * so we compromise on this copy...
935 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
938 espp
= (struct newesp
*)(mtod(m
, caddr_t
) + off
);
940 if (cmd
== PRC_MSGSIZE
) {
944 * Check to see if we have a valid SA corresponding to
945 * the address in the ICMP message payload.
947 sa6_src
= ip6cp
->ip6c_src
;
948 sa6_dst
= (struct sockaddr_in6
*)sa
;
949 sav
= key_allocsa(AF_INET6
,
950 (caddr_t
)&sa6_src
->sin6_addr
,
951 (caddr_t
)&sa6_dst
->sin6_addr
,
952 IPPROTO_ESP
, espp
->esp_spi
);
954 if (sav
->state
== SADB_SASTATE_MATURE
||
955 sav
->state
== SADB_SASTATE_DYING
)
960 /* XXX Further validation? */
963 * Depending on the value of "valid" and routing table
964 * size (mtudisc_{hi,lo}wat), we will:
965 * - recalcurate the new MTU and create the
966 * corresponding routing entry, or
967 * - ignore the MTU change notification.
969 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
972 /* we normally notify any pcb here */