]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_input.c
549b37d410e39bbea4c44db0afa13c8f3f440a04
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 (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
218 ipsecstat
.in_inval
++;
221 if (AH_MAXSUMSIZE
< siz
) {
223 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
225 ipsecstat
.in_inval
++;
229 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
231 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
232 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
233 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
234 ipsecstat
.in_espauthfail
++;
238 if (bcmp(sum0
, sum
, siz
) != 0) {
239 ipseclog((LOG_WARNING
, "auth fail in IPv4 ESP input: %s %s\n",
240 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
241 ipsecstat
.in_espauthfail
++;
245 /* strip off the authentication data */
247 ip
= mtod(m
, struct ip
*);
249 ip
->ip_len
= ip
->ip_len
- siz
;
251 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - siz
);
253 m
->m_flags
|= M_AUTHIPDGM
;
254 ipsecstat
.in_espauthsucc
++;
258 * update sequence number.
260 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
261 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
262 ipsecstat
.in_espreplay
++;
269 /* process main esp header. */
270 if (sav
->flags
& SADB_X_EXT_OLD
) {
272 esplen
= sizeof(struct esp
);
275 if (sav
->flags
& SADB_X_EXT_DERIV
)
276 esplen
= sizeof(struct esp
);
278 esplen
= sizeof(struct newesp
);
281 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
282 ipseclog((LOG_WARNING
,
283 "IPv4 ESP input: packet too short\n"));
284 ipsecstat
.in_inval
++;
288 if (m
->m_len
< off
+ esplen
+ ivlen
) {
289 m
= m_pullup(m
, off
+ esplen
+ ivlen
);
292 "IPv4 ESP input: can't pullup in esp4_input\n"));
293 ipsecstat
.in_inval
++;
299 * pre-compute and cache intermediate key
301 if (esp_schedule(algo
, sav
) != 0) {
302 ipsecstat
.in_inval
++;
307 * decrypt the packet.
310 panic("internal error: no decrypt function");
311 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
312 /* m is already freed */
314 ipseclog((LOG_ERR
, "decrypt fail in IPv4 ESP input: %s\n",
315 ipsec_logsastr(sav
)));
316 ipsecstat
.in_inval
++;
319 ipsecstat
.in_esphist
[sav
->alg_enc
]++;
321 m
->m_flags
|= M_DECRYPTED
;
324 * find the trailer of the ESP.
326 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
328 nxt
= esptail
.esp_nxt
;
329 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
331 if (m
->m_pkthdr
.len
< taillen
332 || m
->m_pkthdr
.len
- taillen
< hlen
) { /*?*/
333 ipseclog((LOG_WARNING
,
334 "bad pad length in IPv4 ESP input: %s %s\n",
335 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
336 ipsecstat
.in_inval
++;
340 /* strip off the trailing pad area. */
344 ip
->ip_len
= ip
->ip_len
- taillen
;
346 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - taillen
);
349 /* was it transmitted over the IPsec tunnel SA? */
350 if (ipsec4_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
352 * strip off all the headers that precedes ESP header.
353 * IP4 xx ESP IP4' payload -> IP4' payload
355 * XXX more sanity checks
356 * XXX relationship with gif?
361 m_adj(m
, off
+ esplen
+ ivlen
);
362 if (m
->m_len
< sizeof(*ip
)) {
363 m
= m_pullup(m
, sizeof(*ip
));
365 ipsecstat
.in_inval
++;
369 ip
= mtod(m
, struct ip
*);
370 /* ECN consideration. */
371 ip_ecn_egress(ip4_ipsec_ecn
, &tos
, &ip
->ip_tos
);
372 if (!key_checktunnelsanity(sav
, AF_INET
,
373 (caddr_t
)&ip
->ip_src
, (caddr_t
)&ip
->ip_dst
)) {
374 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
375 "in IPv4 ESP input: %s %s\n",
376 ipsec4_logpacketstr(ip
, spi
), ipsec_logsastr(sav
)));
377 ipsecstat
.in_inval
++;
381 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
382 /* drop it if it does not match the default policy */
383 if (ipsec4_in_reject(m
, NULL
)) {
384 ipsecstat
.in_polvio
++;
389 key_sa_recordxfer(sav
, m
);
390 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
391 ipsec_addhist(m
, IPPROTO_IPV4
, 0) != 0) {
392 ipsecstat
.in_nomem
++;
397 if (IF_QFULL(&ipintrq
)) {
398 ipsecstat
.in_inval
++;
402 IF_ENQUEUE(&ipintrq
, m
);
404 schednetisr(NETISR_IP
); /*can be skipped but to make sure*/
409 * strip off ESP header and IV.
410 * even in m_pulldown case, we need to strip off ESP so that
411 * we can always compute checksum for AH correctly.
415 stripsiz
= esplen
+ ivlen
;
417 ip
= mtod(m
, struct ip
*);
418 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), off
);
419 m
->m_data
+= stripsiz
;
420 m
->m_len
-= stripsiz
;
421 m
->m_pkthdr
.len
-= stripsiz
;
423 ip
= mtod(m
, struct ip
*);
425 ip
->ip_len
= ip
->ip_len
- stripsiz
;
427 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - stripsiz
);
431 key_sa_recordxfer(sav
, m
);
432 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
433 ipsecstat
.in_nomem
++;
437 if (nxt
!= IPPROTO_DONE
) {
438 if ((ip_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 &&
439 ipsec4_in_reject(m
, NULL
)) {
440 ipsecstat
.in_polvio
++;
443 (*ip_protox
[nxt
]->pr_input
)(m
, off
);
450 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
451 printf("DP esp4_input call free SA:%p\n", sav
));
454 ipsecstat
.in_success
++;
459 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
460 printf("DP esp4_input call free SA:%p\n", sav
));
471 esp6_input(mp
, offp
, proto
)
475 struct mbuf
*m
= *mp
;
479 struct esptail esptail
;
481 struct secasvar
*sav
= NULL
;
484 const struct esp_algorithm
*algo
;
489 /* sanity check for alignment. */
490 if (off
% 4 != 0 || m
->m_pkthdr
.len
% 4 != 0) {
491 ipseclog((LOG_ERR
, "IPv6 ESP input: packet alignment problem "
492 "(off=%d, pktlen=%d)\n", off
, m
->m_pkthdr
.len
));
493 ipsec6stat
.in_inval
++;
497 #ifndef PULLDOWN_TEST
498 IP6_EXTHDR_CHECK(m
, off
, ESPMAXLEN
, IPPROTO_DONE
);
499 esp
= (struct esp
*)(mtod(m
, caddr_t
) + off
);
501 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, ESPMAXLEN
);
503 ipsec6stat
.in_inval
++;
507 ip6
= mtod(m
, struct ip6_hdr
*);
509 if (ntohs(ip6
->ip6_plen
) == 0) {
510 ipseclog((LOG_ERR
, "IPv6 ESP input: "
511 "ESP with IPv6 jumbogram is not supported.\n"));
512 ipsec6stat
.in_inval
++;
516 /* find the sassoc. */
519 if ((sav
= key_allocsa(AF_INET6
,
520 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
,
521 IPPROTO_ESP
, spi
)) == 0) {
522 ipseclog((LOG_WARNING
,
523 "IPv6 ESP input: no key association found for spi %u\n",
524 (u_int32_t
)ntohl(spi
)));
525 ipsec6stat
.in_nosa
++;
528 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
529 printf("DP esp6_input called to allocate SA:%p\n", sav
));
530 if (sav
->state
!= SADB_SASTATE_MATURE
531 && sav
->state
!= SADB_SASTATE_DYING
) {
533 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
534 (u_int32_t
)ntohl(spi
)));
535 ipsec6stat
.in_badspi
++;
538 algo
= esp_algorithm_lookup(sav
->alg_enc
);
540 ipseclog((LOG_DEBUG
, "IPv6 ESP input: "
541 "unsupported encryption algorithm for spi %u\n",
542 (u_int32_t
)ntohl(spi
)));
543 ipsec6stat
.in_badspi
++;
547 /* check if we have proper ivlen information */
550 ipseclog((LOG_ERR
, "inproper ivlen in IPv6 ESP input: %s %s\n",
551 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
552 ipsec6stat
.in_badspi
++;
556 if (!((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
557 && (sav
->alg_auth
&& sav
->key_auth
)))
560 if (sav
->alg_auth
== SADB_X_AALG_NULL
||
561 sav
->alg_auth
== SADB_AALG_NONE
)
565 * check for sequence number.
567 if (ipsec_chkreplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
))
570 ipsec6stat
.in_espreplay
++;
571 ipseclog((LOG_WARNING
,
572 "replay packet in IPv6 ESP input: %s %s\n",
573 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
579 u_char sum0
[AH_MAXSUMSIZE
];
580 u_char sum
[AH_MAXSUMSIZE
];
581 const struct ah_algorithm
*sumalgo
;
584 sumalgo
= ah_algorithm_lookup(sav
->alg_auth
);
587 siz
= (((*sumalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
588 if (m
->m_pkthdr
.len
< off
+ ESPMAXLEN
+ siz
) {
589 ipsecstat
.in_inval
++;
592 if (AH_MAXSUMSIZE
< siz
) {
594 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
596 ipsec6stat
.in_inval
++;
600 m_copydata(m
, m
->m_pkthdr
.len
- siz
, siz
, &sum0
[0]);
602 if (esp_auth(m
, off
, m
->m_pkthdr
.len
- off
- siz
, sav
, sum
)) {
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 if (bcmp(sum0
, sum
, siz
) != 0) {
610 ipseclog((LOG_WARNING
, "auth fail in IPv6 ESP input: %s %s\n",
611 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
612 ipsec6stat
.in_espauthfail
++;
616 /* strip off the authentication data */
618 ip6
= mtod(m
, struct ip6_hdr
*);
619 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - siz
);
621 m
->m_flags
|= M_AUTHIPDGM
;
622 ipsec6stat
.in_espauthsucc
++;
626 * update sequence number.
628 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
) {
629 if (ipsec_updatereplay(ntohl(((struct newesp
*)esp
)->esp_seq
), sav
)) {
630 ipsec6stat
.in_espreplay
++;
637 /* process main esp header. */
638 if (sav
->flags
& SADB_X_EXT_OLD
) {
640 esplen
= sizeof(struct esp
);
643 if (sav
->flags
& SADB_X_EXT_DERIV
)
644 esplen
= sizeof(struct esp
);
646 esplen
= sizeof(struct newesp
);
649 if (m
->m_pkthdr
.len
< off
+ esplen
+ ivlen
+ sizeof(esptail
)) {
650 ipseclog((LOG_WARNING
,
651 "IPv6 ESP input: packet too short\n"));
652 ipsec6stat
.in_inval
++;
656 #ifndef PULLDOWN_TEST
657 IP6_EXTHDR_CHECK(m
, off
, esplen
+ ivlen
, IPPROTO_DONE
); /*XXX*/
659 IP6_EXTHDR_GET(esp
, struct esp
*, m
, off
, esplen
+ ivlen
);
661 ipsec6stat
.in_inval
++;
666 ip6
= mtod(m
, struct ip6_hdr
*); /*set it again just in case*/
669 * pre-compute and cache intermediate key
671 if (esp_schedule(algo
, sav
) != 0) {
672 ipsec6stat
.in_inval
++;
677 * decrypt the packet.
680 panic("internal error: no decrypt function");
681 if ((*algo
->decrypt
)(m
, off
, sav
, algo
, ivlen
)) {
682 /* m is already freed */
684 ipseclog((LOG_ERR
, "decrypt fail in IPv6 ESP input: %s\n",
685 ipsec_logsastr(sav
)));
686 ipsec6stat
.in_inval
++;
689 ipsec6stat
.in_esphist
[sav
->alg_enc
]++;
691 m
->m_flags
|= M_DECRYPTED
;
694 * find the trailer of the ESP.
696 m_copydata(m
, m
->m_pkthdr
.len
- sizeof(esptail
), sizeof(esptail
),
698 nxt
= esptail
.esp_nxt
;
699 taillen
= esptail
.esp_padlen
+ sizeof(esptail
);
701 if (m
->m_pkthdr
.len
< taillen
702 || m
->m_pkthdr
.len
- taillen
< sizeof(struct ip6_hdr
)) { /*?*/
703 ipseclog((LOG_WARNING
,
704 "bad pad length in IPv6 ESP input: %s %s\n",
705 ipsec6_logpacketstr(ip6
, spi
), ipsec_logsastr(sav
)));
706 ipsec6stat
.in_inval
++;
710 /* strip off the trailing pad area. */
713 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - taillen
);
715 /* was it transmitted over the IPsec tunnel SA? */
716 if (ipsec6_tunnel_validate(m
, off
+ esplen
+ ivlen
, nxt
, sav
)) {
718 * strip off all the headers that precedes ESP header.
719 * IP6 xx ESP IP6' payload -> IP6' payload
721 * XXX more sanity checks
722 * XXX relationship with gif?
724 u_int32_t flowinfo
; /*net endian*/
725 flowinfo
= ip6
->ip6_flow
;
726 m_adj(m
, off
+ esplen
+ ivlen
);
727 if (m
->m_len
< sizeof(*ip6
)) {
728 #ifndef PULLDOWN_TEST
730 * m_pullup is prohibited in KAME IPv6 input processing
731 * but there's no other way!
734 /* okay to pullup in m_pulldown style */
736 m
= m_pullup(m
, sizeof(*ip6
));
738 ipsec6stat
.in_inval
++;
742 ip6
= mtod(m
, struct ip6_hdr
*);
743 /* ECN consideration. */
744 ip6_ecn_egress(ip6_ipsec_ecn
, &flowinfo
, &ip6
->ip6_flow
);
745 if (!key_checktunnelsanity(sav
, AF_INET6
,
746 (caddr_t
)&ip6
->ip6_src
, (caddr_t
)&ip6
->ip6_dst
)) {
747 ipseclog((LOG_ERR
, "ipsec tunnel address mismatch "
748 "in IPv6 ESP input: %s %s\n",
749 ipsec6_logpacketstr(ip6
, spi
),
750 ipsec_logsastr(sav
)));
751 ipsec6stat
.in_inval
++;
755 #if 0 /* XXX should call ipfw rather than ipsec_in_reject, shouldn't it ? */
756 /* drop it if it does not match the default policy */
757 if (ipsec6_in_reject(m
, NULL
)) {
758 ipsec6stat
.in_polvio
++;
763 key_sa_recordxfer(sav
, m
);
764 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0 ||
765 ipsec_addhist(m
, IPPROTO_IPV6
, 0) != 0) {
766 ipsec6stat
.in_nomem
++;
771 if (IF_QFULL(&ip6intrq
)) {
772 ipsec6stat
.in_inval
++;
776 IF_ENQUEUE(&ip6intrq
, m
);
778 schednetisr(NETISR_IPV6
); /*can be skipped but to make sure*/
783 * strip off ESP header and IV.
784 * even in m_pulldown case, we need to strip off ESP so that
785 * we can always compute checksum for AH correctly.
791 * Set the next header field of the previous header correctly.
793 prvnxtp
= ip6_get_prevhdr(m
, off
); /* XXX */
796 stripsiz
= esplen
+ ivlen
;
798 ip6
= mtod(m
, struct ip6_hdr
*);
799 if (m
->m_len
>= stripsiz
+ off
) {
800 ovbcopy((caddr_t
)ip6
, ((caddr_t
)ip6
) + stripsiz
, off
);
801 m
->m_data
+= stripsiz
;
802 m
->m_len
-= stripsiz
;
803 m
->m_pkthdr
.len
-= stripsiz
;
806 * this comes with no copy if the boundary is on
811 n
= m_split(m
, off
, M_DONTWAIT
);
813 /* m is retained by m_split */
818 /* m_cat does not update m_pkthdr.len */
819 m
->m_pkthdr
.len
+= n
->m_pkthdr
.len
;
822 #ifndef PULLDOWN_TEST
824 * KAME requires that the packet to be contiguous on the
825 * mbuf. We need to make that sure.
826 * this kind of code should be avoided.
827 * XXX other conditions to avoid running this part?
829 if (m
->m_len
!= m
->m_pkthdr
.len
) {
830 struct mbuf
*n
= NULL
;
833 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
837 if (n
&& m
->m_pkthdr
.len
> maxlen
) {
838 MCLGET(n
, M_DONTWAIT
);
840 if ((n
->m_flags
& M_EXT
) == 0) {
846 printf("esp6_input: mbuf allocation failed\n");
850 if (m
->m_pkthdr
.len
<= maxlen
) {
851 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
852 n
->m_len
= m
->m_pkthdr
.len
;
853 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
857 m_copydata(m
, 0, maxlen
, mtod(n
, caddr_t
));
860 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
862 m
->m_flags
&= ~M_PKTHDR
;
868 ip6
= mtod(m
, struct ip6_hdr
*);
869 ip6
->ip6_plen
= htons(ntohs(ip6
->ip6_plen
) - stripsiz
);
871 key_sa_recordxfer(sav
, m
);
872 if (ipsec_addhist(m
, IPPROTO_ESP
, spi
) != 0) {
873 ipsec6stat
.in_nomem
++;
882 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
883 printf("DP esp6_input call free SA:%p\n", sav
));
886 ipsec6stat
.in_success
++;
891 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
892 printf("DP esp6_input call free SA:%p\n", sav
));
901 esp6_ctlinput(cmd
, sa
, d
)
906 const struct newesp
*espp
;
908 struct ip6ctlparam
*ip6cp
= NULL
, ip6cp1
;
909 struct secasvar
*sav
;
913 struct sockaddr_in6 sa6_src
, sa6_dst
;
915 if (sa
->sa_family
!= AF_INET6
||
916 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
918 if ((unsigned)cmd
>= PRC_NCMDS
)
921 /* if the parameter is from icmp6, decode it. */
923 ip6cp
= (struct ip6ctlparam
*)d
;
925 ip6
= ip6cp
->ip6c_ip6
;
926 off
= ip6cp
->ip6c_off
;
934 * Notify the error to all possible sockets via pfctlinput2.
935 * Since the upper layer information (such as protocol type,
936 * source and destination ports) is embedded in the encrypted
937 * data and might have been cut, we can't directly call
938 * an upper layer ctlinput function. However, the pcbnotify
939 * function will consider source and destination addresses
940 * as well as the flow info value, and may be able to find
941 * some PCB that should be notified.
942 * Although pfctlinput2 will call esp6_ctlinput(), there is
943 * no possibility of an infinite loop of function calls,
944 * because we don't pass the inner IPv6 header.
946 bzero(&ip6cp1
, sizeof(ip6cp1
));
947 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
948 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
951 * Then go to special cases that need ESP header information.
952 * XXX: We assume that when ip6 is non NULL,
953 * M and OFF are valid.
956 /* check if we can safely examine src and dst ports */
957 if (m
->m_pkthdr
.len
< off
+ sizeof(esp
))
960 if (m
->m_len
< off
+ sizeof(esp
)) {
962 * this should be rare case,
963 * so we compromise on this copy...
965 m_copydata(m
, off
, sizeof(esp
), (caddr_t
)&esp
);
968 espp
= (struct newesp
*)(mtod(m
, caddr_t
) + off
);
970 if (cmd
== PRC_MSGSIZE
) {
974 * Check to see if we have a valid SA corresponding to
975 * the address in the ICMP message payload.
977 sav
= key_allocsa(AF_INET6
,
978 (caddr_t
)&sa6_src
.sin6_addr
,
979 (caddr_t
)&sa6_dst
, IPPROTO_ESP
,
982 if (sav
->state
== SADB_SASTATE_MATURE
||
983 sav
->state
== SADB_SASTATE_DYING
)
988 /* XXX Further validation? */
991 * Depending on the value of "valid" and routing table
992 * size (mtudisc_{hi,lo}wat), we will:
993 * - recalcurate the new MTU and create the
994 * corresponding routing entry, or
995 * - ignore the MTU change notification.
997 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
1000 /* we normally notify any pcb here */