]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_input.c
3d5e88900029c0a97f3d5053e94792c074134ea8
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>
95 extern struct protosw inetsw
[];
98 (sizeof(struct esp) < sizeof(struct newesp) \
99 ? sizeof(struct newesp) : sizeof(struct esp))
108 struct esptail esptail
;
110 struct secasvar
*sav
= NULL
;
113 const struct esp_algorithm
*algo
;
119 /* sanity check for alignment. */
120 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
121 ipseclog((LOG_ERR
, "IPv4 ESP input: packet alignment problem "
122 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
123 ipsecstat
.in_inval
++;
127 if (m
->m_len
< off
+ ESPMAXLEN
) {
128 m
= m_pullup(m
, off
+ ESPMAXLEN
);
131 "IPv4 ESP input: can't pullup in esp4_input\n"));
132 ipsecstat
.in_inval
++;
137 ip
= mtod(m
, struct ip
*);
138 esp
= (struct esp
*)(((u_int8_t
*)ip
) + off
);
140 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
142 hlen
= ip
->ip_hl
<< 2;
145 /* find the sassoc. */
148 if ((sav
= key_allocsa(AF_INET
,
149 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
,
150 IPPROTO_ESP
, spi
)) == 0) {
151 ipseclog((LOG_WARNING
,
152 "IPv4 ESP input: no key association found for spi %u\n",
153 (u_int32_t
)ntohl(spi
)));
157 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
158 printf("DP esp4_input called to allocate SA:%p\n", sav
));
159 if (sav
->state
!= SADB_SASTATE_MATURE
160 && sav
->state
!= SADB_SASTATE_DYING
) {
162 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
163 (u_int32_t
)ntohl(spi
)));
164 ipsecstat
.in_badspi
++;
167 algo
= esp_algorithm_lookup(sav
->alg_enc
);
169 ipseclog((LOG_DEBUG
, "IPv4 ESP input: "
170 "unsupported encryption algorithm for spi %u\n",
171 (u_int32_t
)ntohl(spi
)));
172 ipsecstat
.in_badspi
++;
176 /* check if we have proper ivlen information */
179 ipseclog((LOG_ERR
, "inproper ivlen in IPv4 ESP input: %s %s\n",
180 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
181 ipsecstat
.in_inval
++;
185 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
186 && (sav
->alg_auth
&& sav
->key_auth
)))
189 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
190 sav
->alg_auth
== SADB_AALG_NONE
)
194 * check for sequence number.
196 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
199 ipsecstat
.in_espreplay
++;
200 ipseclog((LOG_WARNING
,
201 "replay packet in IPv4 ESP input: %s %s\n",
202 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
208 u_char sum0
[AH_MAXSUMSIZE
];
209 u_char sum
[AH_MAXSUMSIZE
];
210 const struct ah_algorithm
*sumalgo
;
213 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
216 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
217 if (AH_MAXSUMSIZE
< siz
) {
219 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
221 ipsecstat
.in_inval
++;
225 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
227 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
228 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
229 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
230 ipsecstat
.in_espauthfail
++;
234 if (bcmp(sum0
, sum
, siz
) != 0) {
235 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
236 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
237 ipsecstat
.in_espauthfail
++;
241 /* strip off the authentication data */
243 ip
= mtod(m
, struct ip
*);
245 ip
->ip_len
= ip
->ip_len
- siz
;
247 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
249 m
->m_flags
|= M_AUTHIPDGM
;
250 ipsecstat
.in_espauthsucc
++;
254 * update sequence number.
256 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
257 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
258 ipsecstat
.in_espreplay
++;
265 /* process main esp header. */
266 if (sav
->flags
& SADB_X_EXT_OLD
) {
268 esplen
= sizeof(struct esp
);
271 if (sav
->flags
& SADB_X_EXT_DERIV
)
272 esplen
= sizeof(struct esp
);
274 esplen
= sizeof(struct newesp
);
277 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
278 ipseclog((LOG_WARNING
,
279 "IPv4 ESP input: packet too short\n"));
280 ipsecstat
.in_inval
++;
284 if (m
->m_len
< off
+ esplen
+ ivlen
) {
285 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
288 "IPv4 ESP input: can't pullup in esp4_input\n"));
289 ipsecstat
.in_inval
++;
295 * pre-compute and cache intermediate key
297 if (esp_schedule(algo
, sav
) != 0) {
298 ipsecstat
.in_inval
++;
303 * decrypt the packet.
306 panic("internal error: no decrypt function");
307 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
308 /* m is already freed */
310 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
311 ipsec_logsastr(sav
)));
312 ipsecstat
.in_inval
++;
315 ipsecstat
.in_esphist
[sav
->alg_enc
]++;
317 m
->m_flags
|= M_DECRYPTED
;
320 * find the trailer of the ESP.
322 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
324 nxt
= esptail
.esp_nxt
;
325 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
327 if (m
->m_pkthdr
.len
< taillen
328 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
329 ipseclog((LOG_WARNING
,
330 "bad pad length in IPv4 ESP input: %s %s\n",
331 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
332 ipsecstat
.in_inval
++;
336 /* strip off the trailing pad area. */
340 ip
->ip_len
= ip
->ip_len
- taillen
;
342 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
345 /* was it transmitted over the IPsec tunnel SA? */
346 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
348 * strip off all the headers that precedes ESP header.
349 * IP4 xx ESP IP4' payload -> IP4' payload
351 * XXX more sanity checks
352 * XXX relationship with gif?
357 m_adj(m
, off
+ esplen
+ ivlen
);
358 if (m
->m_len
< sizeof(*ip
)) {
359 m
= m_pullup(m
, sizeof(*ip
));
361 ipsecstat
.in_inval
++;
365 ip
= mtod(m
, struct ip
*);
366 /* ECN consideration. */
367 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
368 if (!key_checktunnelsanity(sav
, AF_INET
,
369 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
370 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
371 "in IPv4 ESP input: %s %s\n",
372 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
373 ipsecstat
.in_inval
++;
377 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
378 /* drop it if it does not match the default policy */
379 if (ipsec4_in_reject(m
, NULL
)) {
380 ipsecstat
.in_polvio
++;
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
++;
393 if (IF_QFULL(&ipintrq
)) {
394 ipsecstat
.in_inval
++;
398 IF_ENQUEUE(&ipintrq
, m
);
400 schednetisr(NETISR_IP
); /*can be skipped but to make sure*/
405 * strip off ESP header and IV.
406 * even in m_pulldown case, we need to strip off ESP so that
407 * we can always compute checksum for AH correctly.
411 stripsiz
= esplen
+ ivlen
;
413 ip
= mtod(m
, struct ip
*);
414 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
415 m
->m_data
+= stripsiz
;
416 m
->m_len
-= stripsiz
;
417 m
->m_pkthdr
.len
-= stripsiz
;
419 ip
= mtod(m
, struct ip
*);
421 ip
->ip_len
= ip
->ip_len
- stripsiz
;
423 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
427 key_sa_recordxfer(sav
, m
);
428 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
429 ipsecstat
.in_nomem
++;
433 if (nxt
!= IPPROTO_DONE
) {
434 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
435 ipsec4_in_reject(m
, NULL
)) {
436 ipsecstat
.in_polvio
++;
439 (*ip_protox
[nxt
]->pr_input
)(m
, off
);
446 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
447 printf("DP esp4_input call free SA:%p\n", sav
));
450 ipsecstat
.in_success
++;
455 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
456 printf("DP esp4_input call free SA:%p\n", sav
));
467 esp6_input(mp
, offp
, proto
)
471 struct mbuf
*m
= *mp
;
475 struct esptail esptail
;
477 struct secasvar
*sav
= NULL
;
480 const struct esp_algorithm
*algo
;
485 /* sanity check for alignment. */
486 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
487 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
488 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
489 ipsec6stat
.in_inval
++;
493 #ifndef PULLDOWN_TEST
494 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, IPPROTO_DONE
);
495 esp
= (struct esp
*)(mtod(m
, caddr_t
) + off
);
497 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
499 ipsec6stat
.in_inval
++;
503 ip6
= mtod(m
, struct ip6_hdr
*);
505 if (ntohs(ip6
->ip6_plen
) == 0) {
506 ipseclog((LOG_ERR
, "IPv6 ESP input: "
507 "ESP with IPv6 jumbogram is not supported.\n"));
508 ipsec6stat
.in_inval
++;
512 /* find the sassoc. */
515 if ((sav
= key_allocsa(AF_INET6
,
516 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
517 IPPROTO_ESP
, spi
)) == 0) {
518 ipseclog((LOG_WARNING
,
519 "IPv6 ESP input: no key association found for spi %u\n",
520 (u_int32_t
)ntohl(spi
)));
521 ipsec6stat
.in_nosa
++;
524 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
525 printf("DP esp6_input called to allocate SA:%p\n", sav
));
526 if (sav
->state
!= SADB_SASTATE_MATURE
527 && sav
->state
!= SADB_SASTATE_DYING
) {
529 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
530 (u_int32_t
)ntohl(spi
)));
531 ipsec6stat
.in_badspi
++;
534 algo
= esp_algorithm_lookup(sav
->alg_enc
);
536 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
537 "unsupported encryption algorithm for spi %u\n",
538 (u_int32_t
)ntohl(spi
)));
539 ipsec6stat
.in_badspi
++;
543 /* check if we have proper ivlen information */
546 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
547 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
548 ipsec6stat
.in_badspi
++;
552 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
553 && (sav
->alg_auth
&& sav
->key_auth
)))
556 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
557 sav
->alg_auth
== SADB_AALG_NONE
)
561 * check for sequence number.
563 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
566 ipsec6stat
.in_espreplay
++;
567 ipseclog((LOG_WARNING
,
568 "replay packet in IPv6 ESP input: %s %s\n",
569 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
575 u_char sum0
[AH_MAXSUMSIZE
];
576 u_char sum
[AH_MAXSUMSIZE
];
577 const struct ah_algorithm
*sumalgo
;
580 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
583 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
584 if (AH_MAXSUMSIZE
< siz
) {
586 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
588 ipsec6stat
.in_inval
++;
592 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
594 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
595 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
596 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
597 ipsec6stat
.in_espauthfail
++;
601 if (bcmp(sum0
, sum
, siz
) != 0) {
602 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
603 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
604 ipsec6stat
.in_espauthfail
++;
608 /* strip off the authentication data */
610 ip6
= mtod(m
, struct ip6_hdr
*);
611 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
613 m
->m_flags
|= M_AUTHIPDGM
;
614 ipsec6stat
.in_espauthsucc
++;
618 * update sequence number.
620 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
621 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
622 ipsec6stat
.in_espreplay
++;
629 /* process main esp header. */
630 if (sav
->flags
& SADB_X_EXT_OLD
) {
632 esplen
= sizeof(struct esp
);
635 if (sav
->flags
& SADB_X_EXT_DERIV
)
636 esplen
= sizeof(struct esp
);
638 esplen
= sizeof(struct newesp
);
641 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
642 ipseclog((LOG_WARNING
,
643 "IPv6 ESP input: packet too short\n"));
644 ipsec6stat
.in_inval
++;
648 #ifndef PULLDOWN_TEST
649 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, IPPROTO_DONE
); /*XXX*/
651 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
653 ipsec6stat
.in_inval
++;
658 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
661 * pre-compute and cache intermediate key
663 if (esp_schedule(algo
, sav
) != 0) {
664 ipsec6stat
.in_inval
++;
669 * decrypt the packet.
672 panic("internal error: no decrypt function");
673 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
674 /* m is already freed */
676 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
677 ipsec_logsastr(sav
)));
678 ipsec6stat
.in_inval
++;
681 ipsec6stat
.in_esphist
[sav
->alg_enc
]++;
683 m
->m_flags
|= M_DECRYPTED
;
686 * find the trailer of the ESP.
688 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
690 nxt
= esptail
.esp_nxt
;
691 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
693 if (m
->m_pkthdr
.len
< taillen
694 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
695 ipseclog((LOG_WARNING
,
696 "bad pad length in IPv6 ESP input: %s %s\n",
697 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
698 ipsec6stat
.in_inval
++;
702 /* strip off the trailing pad area. */
705 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
707 /* was it transmitted over the IPsec tunnel SA? */
708 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
710 * strip off all the headers that precedes ESP header.
711 * IP6 xx ESP IP6' payload -> IP6' payload
713 * XXX more sanity checks
714 * XXX relationship with gif?
716 u_int32_t flowinfo
; /*net endian*/
717 flowinfo
= ip6
->ip6_flow
;
718 m_adj(m
, off
+ esplen
+ ivlen
);
719 if (m
->m_len
< sizeof(*ip6
)) {
720 #ifndef PULLDOWN_TEST
722 * m_pullup is prohibited in KAME IPv6 input processing
723 * but there's no other way!
726 /* okay to pullup in m_pulldown style */
728 m
= m_pullup(m
, sizeof(*ip6
));
730 ipsec6stat
.in_inval
++;
734 ip6
= mtod(m
, struct ip6_hdr
*);
735 /* ECN consideration. */
736 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
737 if (!key_checktunnelsanity(sav
, AF_INET6
,
738 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
739 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
740 "in IPv6 ESP input: %s %s\n",
741 ipsec6_logpacketstr(ip6
, spi
),
742 ipsec_logsastr(sav
)));
743 ipsec6stat
.in_inval
++;
747 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
748 /* drop it if it does not match the default policy */
749 if (ipsec6_in_reject(m
, NULL
)) {
750 ipsec6stat
.in_polvio
++;
755 key_sa_recordxfer(sav
, m
);
756 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
757 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
758 ipsec6stat
.in_nomem
++;
763 if (IF_QFULL(&ip6intrq
)) {
764 ipsec6stat
.in_inval
++;
768 IF_ENQUEUE(&ip6intrq
, m
);
770 schednetisr(NETISR_IPV6
); /*can be skipped but to make sure*/
775 * strip off ESP header and IV.
776 * even in m_pulldown case, we need to strip off ESP so that
777 * we can always compute checksum for AH correctly.
783 * Set the next header field of the previous header correctly.
785 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
788 stripsiz
= esplen
+ ivlen
;
790 ip6
= mtod(m
, struct ip6_hdr
*);
791 if (m
->m_len
>= stripsiz
+ off
) {
792 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
793 m
->m_data
+= stripsiz
;
794 m
->m_len
-= stripsiz
;
795 m
->m_pkthdr
.len
-= stripsiz
;
798 * this comes with no copy if the boundary is on
803 n
= m_split(m
, off
, M_DONTWAIT
);
805 /* m is retained by m_split */
810 /* m_cat does not update m_pkthdr.len */
811 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
814 #ifndef PULLDOWN_TEST
816 * KAME requires that the packet to be contiguous on the
817 * mbuf. We need to make that sure.
818 * this kind of code should be avoided.
819 * XXX other conditions to avoid running this part?
821 if (m
->m_len
!= m
->m_pkthdr
.len
) {
822 struct mbuf
*n
= NULL
;
825 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
829 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
830 MCLGET(n
, M_DONTWAIT
);
832 if ((n
->m_flags
& M_EXT
) == 0) {
838 printf("esp6_input: mbuf allocation failed\n");
842 if (m
->m_pkthdr
.len
<= maxlen
) {
843 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
844 n
->m_len
= m
->m_pkthdr
.len
;
845 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
849 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
852 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
854 m
->m_flags
&= ~M_PKTHDR
;
860 ip6
= mtod(m
, struct ip6_hdr
*);
861 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
863 key_sa_recordxfer(sav
, m
);
864 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
865 ipsec6stat
.in_nomem
++;
874 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
875 printf("DP esp6_input call free SA:%p\n", sav
));
878 ipsec6stat
.in_success
++;
883 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
884 printf("DP esp6_input call free SA:%p\n", sav
));
893 esp6_ctlinput(cmd
, sa
, d
)
898 const struct newesp
*espp
;
900 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
901 struct secasvar
*sav
;
905 struct sockaddr_in6 sa6_src
, sa6_dst
;
907 if (sa
->sa_family
!= AF_INET6
||
908 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
910 if ((unsigned)cmd
>= PRC_NCMDS
)
913 /* if the parameter is from icmp6, decode it. */
915 ip6cp
= (struct ip6ctlparam
*)d
;
917 ip6
= ip6cp
->ip6c_ip6
;
918 off
= ip6cp
->ip6c_off
;
926 * Notify the error to all possible sockets via pfctlinput2.
927 * Since the upper layer information (such as protocol type,
928 * source and destination ports) is embedded in the encrypted
929 * data and might have been cut, we can't directly call
930 * an upper layer ctlinput function. However, the pcbnotify
931 * function will consider source and destination addresses
932 * as well as the flow info value, and may be able to find
933 * some PCB that should be notified.
934 * Although pfctlinput2 will call esp6_ctlinput(), there is
935 * no possibility of an infinite loop of function calls,
936 * because we don't pass the inner IPv6 header.
938 bzero(&ip6cp1
, sizeof(ip6cp1
));
939 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
940 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
943 * Then go to special cases that need ESP header information.
944 * XXX: We assume that when ip6 is non NULL,
945 * M and OFF are valid.
948 /* check if we can safely examine src and dst ports */
949 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
952 if (m
->m_len
< off
+ sizeof(esp
)) {
954 * this should be rare case,
955 * so we compromise on this copy...
957 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
960 espp
= (struct newesp
*)(mtod(m
, caddr_t
) + off
);
962 if (cmd
== PRC_MSGSIZE
) {
966 * Check to see if we have a valid SA corresponding to
967 * the address in the ICMP message payload.
969 sav
= key_allocsa(AF_INET6
,
970 (caddr_t
)&sa6_src
.sin6_addr
,
971 (caddr_t
)&sa6_dst
, IPPROTO_ESP
,
974 if (sav
->state
== SADB_SASTATE_MATURE
||
975 sav
->state
== SADB_SASTATE_DYING
)
980 /* XXX Further validation? */
983 * Depending on the value of "valid" and routing table
984 * size (mtudisc_{hi,lo}wat), we will:
985 * - recalcurate the new MTU and create the
986 * corresponding routing entry, or
987 * - ignore the MTU change notification.
989 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
992 /* we normally notify any pcb here */