]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_input.c
1 /* $FreeBSD: src/sys/netinet6/esp_input.c,v 1.1.2.3 2001/07/03 11:01:50 ume Exp $ */
2 /* $KAME: esp_input.c,v 1.55 2001/03/23 08:08:47 itojun 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.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
50 #include <net/route.h>
51 #include <net/netisr.h>
52 #include <kern/cpu_number.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_ecn.h>
60 #include <netinet/in_pcb.h>
62 #include <netinet6/ip6_ecn.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_pcb.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet/icmp6.h>
70 #include <netinet6/ip6protosw.h>
73 #include <netinet6/ipsec.h>
75 #include <netinet6/ipsec6.h>
77 #include <netinet6/ah.h>
79 #include <netinet6/ah6.h>
81 #include <netinet6/esp.h>
83 #include <netinet6/esp6.h>
85 #include <netkey/key.h>
86 #include <netkey/keydb.h>
87 #include <netkey/key_debug.h>
90 #include <net/net_osdep.h>
92 #include <sys/kdebug.h>
93 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
94 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
95 #define DBG_FNC_ESPIN NETDBG_CODE(DBG_NETIPSEC, (6 << 8))
96 #define DBG_FNC_DECRYPT NETDBG_CODE(DBG_NETIPSEC, (7 << 8))
100 extern struct protosw inetsw
[];
103 (sizeof(struct esp) < sizeof(struct newesp) \
104 ? sizeof(struct newesp) : sizeof(struct esp))
113 struct esptail esptail
;
115 struct secasvar
*sav
= NULL
;
118 const struct esp_algorithm
*algo
;
124 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_START
, 0,0,0,0,0);
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 printf("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
, "inproper 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 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_START
, 0,0,0,0,0);
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
++;
324 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 1,0,0,0,0);
327 KERNEL_DEBUG(DBG_FNC_DECRYPT
| DBG_FUNC_END
, 2,0,0,0,0);
328 ipsecstat
.in_esphist
[sav
->alg_enc
]++;
330 m
->m_flags
|= M_DECRYPTED
;
333 * find the trailer of the ESP.
335 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
337 nxt
= esptail
.esp_nxt
;
338 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
340 if (m
->m_pkthdr
.len
< taillen
341 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
342 ipseclog((LOG_WARNING
,
343 "bad pad length in IPv4 ESP input: %s %s\n",
344 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
345 ipsecstat
.in_inval
++;
349 /* strip off the trailing pad area. */
353 ip
->ip_len
= ip
->ip_len
- taillen
;
355 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
358 /* was it transmitted over the IPsec tunnel SA? */
359 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
361 * strip off all the headers that precedes ESP header.
362 * IP4 xx ESP IP4' payload -> IP4' payload
364 * XXX more sanity checks
365 * XXX relationship with gif?
370 m_adj(m
, off
+ esplen
+ ivlen
);
371 if (m
->m_len
< sizeof(*ip
)) {
372 m
= m_pullup(m
, sizeof(*ip
));
374 ipsecstat
.in_inval
++;
378 ip
= mtod(m
, struct ip
*);
379 /* ECN consideration. */
380 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
381 if (!key_checktunnelsanity(sav
, AF_INET
,
382 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
383 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
384 "in IPv4 ESP input: %s %s\n",
385 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
386 ipsecstat
.in_inval
++;
390 key_sa_recordxfer(sav
, m
);
391 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
392 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
393 ipsecstat
.in_nomem
++;
397 /* Clear the csum flags, they can't be valid for the inner headers */
398 m
->m_pkthdr
.csum_flags
= 0;
401 if (IF_QFULL(&ipintrq
)) {
402 ipsecstat
.in_inval
++;
406 IF_ENQUEUE(&ipintrq
, m
);
408 schednetisr(NETISR_IP
); /*can be skipped but to make sure*/
411 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 2,0,0,0,0);
414 * strip off ESP header and IV.
415 * even in m_pulldown case, we need to strip off ESP so that
416 * we can always compute checksum for AH correctly.
420 stripsiz
= esplen
+ ivlen
;
422 ip
= mtod(m
, struct ip
*);
423 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
424 m
->m_data
+= stripsiz
;
425 m
->m_len
-= stripsiz
;
426 m
->m_pkthdr
.len
-= stripsiz
;
428 ip
= mtod(m
, struct ip
*);
430 ip
->ip_len
= ip
->ip_len
- stripsiz
;
432 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
436 key_sa_recordxfer(sav
, m
);
437 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
438 ipsecstat
.in_nomem
++;
443 * Set the csum valid flag, if we authenticated the
444 * packet, the payload shouldn't be corrupt unless
445 * it was corrupted before being signed on the other
448 if (nxt
== IPPROTO_TCP
|| nxt
== IPPROTO_UDP
) {
449 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
450 m
->m_pkthdr
.csum_data
= 0xFFFF;
453 if (nxt
!= IPPROTO_DONE
) {
454 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
455 ipsec4_in_reject(m
, NULL
)) {
456 ipsecstat
.in_polvio
++;
459 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 3,0,0,0,0);
460 (*ip_protox
[nxt
]->pr_input
)(m
, off
);
467 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
468 printf("DP esp4_input call free SA:%p\n", sav
));
471 ipsecstat
.in_success
++;
476 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
477 printf("DP esp4_input call free SA:%p\n", sav
));
482 KERNEL_DEBUG(DBG_FNC_ESPIN
| DBG_FUNC_END
, 4,0,0,0,0);
493 struct mbuf
*m
= *mp
;
497 struct esptail esptail
;
499 struct secasvar
*sav
= NULL
;
502 const struct esp_algorithm
*algo
;
507 /* sanity check for alignment. */
508 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
509 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
510 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
511 ipsec6stat
.in_inval
++;
515 #ifndef PULLDOWN_TEST
516 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, IPPROTO_DONE
);
517 esp
= (struct esp
*)(mtod(m
, caddr_t
) + off
);
519 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
521 ipsec6stat
.in_inval
++;
525 ip6
= mtod(m
, struct ip6_hdr
*);
527 if (ntohs(ip6
->ip6_plen
) == 0) {
528 ipseclog((LOG_ERR
, "IPv6 ESP input: "
529 "ESP with IPv6 jumbogram is not supported.\n"));
530 ipsec6stat
.in_inval
++;
534 /* find the sassoc. */
537 if ((sav
= key_allocsa(AF_INET6
,
538 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
539 IPPROTO_ESP
, spi
)) == 0) {
540 ipseclog((LOG_WARNING
,
541 "IPv6 ESP input: no key association found for spi %u\n",
542 (u_int32_t
)ntohl(spi
)));
543 ipsec6stat
.in_nosa
++;
546 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
547 printf("DP esp6_input called to allocate SA:%p\n", sav
));
548 if (sav
->state
!= SADB_SASTATE_MATURE
549 && sav
->state
!= SADB_SASTATE_DYING
) {
551 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
552 (u_int32_t
)ntohl(spi
)));
553 ipsec6stat
.in_badspi
++;
556 algo
= esp_algorithm_lookup(sav
->alg_enc
);
558 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
559 "unsupported encryption algorithm for spi %u\n",
560 (u_int32_t
)ntohl(spi
)));
561 ipsec6stat
.in_badspi
++;
565 /* check if we have proper ivlen information */
568 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
569 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
570 ipsec6stat
.in_badspi
++;
574 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
575 && (sav
->alg_auth
&& sav
->key_auth
)))
578 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
579 sav
->alg_auth
== SADB_AALG_NONE
)
583 * check for sequence number.
585 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
588 ipsec6stat
.in_espreplay
++;
589 ipseclog((LOG_WARNING
,
590 "replay packet in IPv6 ESP input: %s %s\n",
591 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
597 u_char sum0
[AH_MAXSUMSIZE
];
598 u_char sum
[AH_MAXSUMSIZE
];
599 const struct ah_algorithm
*sumalgo
;
602 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
605 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
606 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
607 ipsecstat
.in_inval
++;
610 if (AH_MAXSUMSIZE
< siz
) {
612 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
614 ipsec6stat
.in_inval
++;
618 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
620 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
621 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
622 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
623 ipsec6stat
.in_espauthfail
++;
627 if (bcmp(sum0
, sum
, siz
) != 0) {
628 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
629 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
630 ipsec6stat
.in_espauthfail
++;
634 /* strip off the authentication data */
636 ip6
= mtod(m
, struct ip6_hdr
*);
637 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
639 m
->m_flags
|= M_AUTHIPDGM
;
640 ipsec6stat
.in_espauthsucc
++;
644 * update sequence number.
646 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
647 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
648 ipsec6stat
.in_espreplay
++;
655 /* process main esp header. */
656 if (sav
->flags
& SADB_X_EXT_OLD
) {
658 esplen
= sizeof(struct esp
);
661 if (sav
->flags
& SADB_X_EXT_DERIV
)
662 esplen
= sizeof(struct esp
);
664 esplen
= sizeof(struct newesp
);
667 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
668 ipseclog((LOG_WARNING
,
669 "IPv6 ESP input: packet too short\n"));
670 ipsec6stat
.in_inval
++;
674 #ifndef PULLDOWN_TEST
675 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, IPPROTO_DONE
); /*XXX*/
677 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
679 ipsec6stat
.in_inval
++;
684 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
687 * pre-compute and cache intermediate key
689 if (esp_schedule(algo
, sav
) != 0) {
690 ipsec6stat
.in_inval
++;
695 * decrypt the packet.
698 panic("internal error: no decrypt function");
699 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
700 /* m is already freed */
702 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
703 ipsec_logsastr(sav
)));
704 ipsec6stat
.in_inval
++;
707 ipsec6stat
.in_esphist
[sav
->alg_enc
]++;
709 m
->m_flags
|= M_DECRYPTED
;
712 * find the trailer of the ESP.
714 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
716 nxt
= esptail
.esp_nxt
;
717 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
719 if (m
->m_pkthdr
.len
< taillen
720 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
721 ipseclog((LOG_WARNING
,
722 "bad pad length in IPv6 ESP input: %s %s\n",
723 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
724 ipsec6stat
.in_inval
++;
728 /* strip off the trailing pad area. */
731 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
733 /* was it transmitted over the IPsec tunnel SA? */
734 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
736 * strip off all the headers that precedes ESP header.
737 * IP6 xx ESP IP6' payload -> IP6' payload
739 * XXX more sanity checks
740 * XXX relationship with gif?
742 u_int32_t flowinfo
; /*net endian*/
743 flowinfo
= ip6
->ip6_flow
;
744 m_adj(m
, off
+ esplen
+ ivlen
);
745 if (m
->m_len
< sizeof(*ip6
)) {
746 #ifndef PULLDOWN_TEST
748 * m_pullup is prohibited in KAME IPv6 input processing
749 * but there's no other way!
752 /* okay to pullup in m_pulldown style */
754 m
= m_pullup(m
, sizeof(*ip6
));
756 ipsec6stat
.in_inval
++;
760 ip6
= mtod(m
, struct ip6_hdr
*);
761 /* ECN consideration. */
762 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
763 if (!key_checktunnelsanity(sav
, AF_INET6
,
764 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
765 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
766 "in IPv6 ESP input: %s %s\n",
767 ipsec6_logpacketstr(ip6
, spi
),
768 ipsec_logsastr(sav
)));
769 ipsec6stat
.in_inval
++;
773 key_sa_recordxfer(sav
, m
);
774 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
775 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
776 ipsec6stat
.in_nomem
++;
781 if (IF_QFULL(&ip6intrq
)) {
782 ipsec6stat
.in_inval
++;
786 IF_ENQUEUE(&ip6intrq
, m
);
788 schednetisr(NETISR_IPV6
); /*can be skipped but to make sure*/
793 * strip off ESP header and IV.
794 * even in m_pulldown case, we need to strip off ESP so that
795 * we can always compute checksum for AH correctly.
801 * Set the next header field of the previous header correctly.
803 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
806 stripsiz
= esplen
+ ivlen
;
808 ip6
= mtod(m
, struct ip6_hdr
*);
809 if (m
->m_len
>= stripsiz
+ off
) {
810 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
811 m
->m_data
+= stripsiz
;
812 m
->m_len
-= stripsiz
;
813 m
->m_pkthdr
.len
-= stripsiz
;
816 * this comes with no copy if the boundary is on
821 n
= m_split(m
, off
, M_DONTWAIT
);
823 /* m is retained by m_split */
827 /* m_cat does not update m_pkthdr.len */
828 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
832 #ifndef PULLDOWN_TEST
834 * KAME requires that the packet to be contiguous on the
835 * mbuf. We need to make that sure.
836 * this kind of code should be avoided.
837 * XXX other conditions to avoid running this part?
839 if (m
->m_len
!= m
->m_pkthdr
.len
) {
840 struct mbuf
*n
= NULL
;
843 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
847 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
848 MCLGET(n
, M_DONTWAIT
);
850 if ((n
->m_flags
& M_EXT
) == 0) {
856 printf("esp6_input: mbuf allocation failed\n");
860 if (m
->m_pkthdr
.len
<= maxlen
) {
861 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
862 n
->m_len
= m
->m_pkthdr
.len
;
863 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
867 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
869 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
872 m
->m_flags
&= ~M_PKTHDR
;
878 ip6
= mtod(m
, struct ip6_hdr
*);
879 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
881 key_sa_recordxfer(sav
, m
);
882 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
883 ipsec6stat
.in_nomem
++;
892 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
893 printf("DP esp6_input call free SA:%p\n", sav
));
896 ipsec6stat
.in_success
++;
901 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
902 printf("DP esp6_input call free SA:%p\n", sav
));
911 esp6_ctlinput(cmd
, sa
, d
)
916 const struct newesp
*espp
;
918 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
919 struct secasvar
*sav
;
923 struct sockaddr_in6
*sa6_src
, *sa6_dst
;
925 if (sa
->sa_family
!= AF_INET6
||
926 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
928 if ((unsigned)cmd
>= PRC_NCMDS
)
931 /* if the parameter is from icmp6, decode it. */
933 ip6cp
= (struct ip6ctlparam
*)d
;
935 ip6
= ip6cp
->ip6c_ip6
;
936 off
= ip6cp
->ip6c_off
;
944 * Notify the error to all possible sockets via pfctlinput2.
945 * Since the upper layer information (such as protocol type,
946 * source and destination ports) is embedded in the encrypted
947 * data and might have been cut, we can't directly call
948 * an upper layer ctlinput function. However, the pcbnotify
949 * function will consider source and destination addresses
950 * as well as the flow info value, and may be able to find
951 * some PCB that should be notified.
952 * Although pfctlinput2 will call esp6_ctlinput(), there is
953 * no possibility of an infinite loop of function calls,
954 * because we don't pass the inner IPv6 header.
956 bzero(&ip6cp1
, sizeof(ip6cp1
));
957 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
958 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
961 * Then go to special cases that need ESP header information.
962 * XXX: We assume that when ip6 is non NULL,
963 * M and OFF are valid.
966 /* check if we can safely examine src and dst ports */
967 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
970 if (m
->m_len
< off
+ sizeof(esp
)) {
972 * this should be rare case,
973 * so we compromise on this copy...
975 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
978 espp
= (struct newesp
*)(mtod(m
, caddr_t
) + off
);
980 if (cmd
== PRC_MSGSIZE
) {
984 * Check to see if we have a valid SA corresponding to
985 * the address in the ICMP message payload.
987 sa6_src
= ip6cp
->ip6c_src
;
988 sa6_dst
= (struct sockaddr_in6
*)sa
;
989 sav
= key_allocsa(AF_INET6
,
990 (caddr_t
)&sa6_src
->sin6_addr
,
991 (caddr_t
)&sa6_dst
->sin6_addr
,
992 IPPROTO_ESP
, espp
->esp_spi
);
994 if (sav
->state
== SADB_SASTATE_MATURE
||
995 sav
->state
== SADB_SASTATE_DYING
)
1000 /* XXX Further validation? */
1003 * Depending on the value of "valid" and routing table
1004 * size (mtudisc_{hi,lo}wat), we will:
1005 * - recalcurate the new MTU and create the
1006 * corresponding routing entry, or
1007 * - ignore the MTU change notification.
1009 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1012 /* we normally notify any pcb here */