1 /* $NetBSD: isakmp_quick.c,v 1.11.4.1 2007/08/01 11:52:21 vanhu Exp $ */
3 /* Id: isakmp_quick.c,v 1.29 2006/08/22 18:17:17 manubsd 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
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
51 # include <sys/time.h>
57 #ifndef HAVE_NETINET6_IPSEC
58 #include <netinet/ipsec.h>
60 #include <netinet6/ipsec.h>
71 #include "localconf.h"
72 #include "remoteconf.h"
76 #include "isakmp_var.h"
78 #include "isakmp_inf.h"
79 #include "isakmp_quick.h"
81 #include "ipsec_doi.h"
82 #include "crypto_openssl.h"
85 #include "algorithm.h"
90 #include "nattraversal.h"
91 #include "ipsecSessionTracer.h"
92 #include "ipsecMessageTracer.h"
94 #include <Security/SecDH.h>
98 static vchar_t
*quick_ir1mx (phase2_handle_t
*, vchar_t
*, vchar_t
*);
99 static int get_proposal_r_remote (phase2_handle_t
*, int);
105 * begin Quick Mode as initiator. send pfkey getspi message to kernel.
108 quick_iprep(iph2
, msg
)
109 phase2_handle_t
*iph2
;
110 vchar_t
*msg
; /* must be null pointer */
112 int error
= ISAKMP_INTERNAL_ERROR
;
115 if (iph2
->status
!= IKEV1_STATE_QUICK_I_START
) {
117 "status mismatched %d.\n", iph2
->status
);
121 iph2
->msgid
= isakmp_newmsgid2(iph2
->ph1
);
122 if (iph2
->ivm
!= NULL
)
123 oakley_delivm(iph2
->ivm
);
124 iph2
->ivm
= oakley_newiv2(iph2
->ph1
, iph2
->msgid
);
125 if (iph2
->ivm
== NULL
)
128 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_GETSPISENT
);
130 /* don't anything if local test mode. */
136 /* send getspi message */
137 if (pk_sendgetspi(iph2
) < 0) {
139 "failed to send getspi message");
143 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
145 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
146 pfkey_timeover_stub
, iph2
);
156 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
159 quick_i1send(iph2
, msg
)
160 phase2_handle_t
*iph2
;
161 vchar_t
*msg
; /* must be null pointer */
163 vchar_t
*body
= NULL
;
164 vchar_t
*hash
= NULL
;
166 vchar_t
*natoa_i
= NULL
;
167 vchar_t
*natoa_r
= NULL
;
168 #endif /* ENABLE_NATT */
170 struct isakmp_gen
*gen
;
173 int error
= ISAKMP_INTERNAL_ERROR
;
174 int pfsgroup
, idci
, idcr
;
176 struct ipsecdoi_id_b
*id
, *id_p
;
181 "msg has to be NULL in this function.\n");
184 if (iph2
->status
!= IKEV1_STATE_QUICK_I_GETSPIDONE
) {
186 "status mismatched %d.\n", iph2
->status
);
190 /* create SA payload for my proposal */
191 if (ipsecdoi_setph2proposal(iph2
, FALSE
) < 0) {
193 "failed to set proposal");
197 /* generate NONCE value */
198 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
199 if (iph2
->nonce
== NULL
) {
201 "failed to generate NONCE");
206 * DH value calculation is kicked out into cfparse.y.
207 * because pfs group can not be negotiated, it's only to be checked
210 /* generate KE value if need */
211 pfsgroup
= iph2
->proposal
->pfs_group
;
213 /* DH group settting if PFS is required. */
214 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
216 "failed to set DH value.\n");
220 if (oakley_dh_generate(iph2
->pfsgrp
,
221 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
223 if (oakley_dh_generate(iph2
->pfsgrp
,
224 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
227 "failed to generate DH");
232 /* generate ID value */
233 if (ipsecdoi_setid2(iph2
) < 0) {
235 "failed to get ID.\n");
238 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "IDci:\n");
239 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "IDcr:\n");
242 * we do not attach IDci nor IDcr, under the following condition:
243 * - all proposals are transport mode
245 * - id payload suggests to encrypt all the traffic (no specific
248 id
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
;
249 id_p
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id_p
->v
;
250 if (id
->proto_id
== 0
251 && id_p
->proto_id
== 0
252 && iph2
->ph1
->rmconf
->support_proxy
== 0
253 && ipsecdoi_transportmode(iph2
->proposal
)) {
258 /* create SA;NONCE payload, and KE if need, and IDii, IDir. */
259 tlen
= + sizeof(*gen
) + iph2
->sa
->l
260 + sizeof(*gen
) + iph2
->nonce
->l
;
262 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
264 tlen
+= sizeof(*gen
) + iph2
->id
->l
;
266 tlen
+= sizeof(*gen
) + iph2
->id_p
->l
;
270 * RFC3947 5.2. if we propose UDP-Encapsulated-Transport
271 * we should send NAT-OA
273 if (ipsecdoi_any_transportmode(iph2
->proposal
)
274 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
275 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
276 if (natoa_type
== -1) {
278 "failed to generate NAT-OA payload.\n");
280 } else if (natoa_type
!= 0) {
281 tlen
+= sizeof(*gen
) + natoa_i
->l
;
282 tlen
+= sizeof(*gen
) + natoa_r
->l
;
284 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "initiator send NAT-OAi:\n");
285 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "initiator send NAT-OAr:\n");
290 body
= vmalloc(tlen
);
293 "failed to get buffer to send.\n");
300 p
= set_isakmp_payload(p
, iph2
->sa
, ISAKMP_NPTYPE_NONCE
);
302 /* add NONCE payload */
304 np
= ISAKMP_NPTYPE_KE
;
305 else if (idci
|| idcr
)
306 np
= ISAKMP_NPTYPE_ID
;
308 np
= (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
309 p
= set_isakmp_payload(p
, iph2
->nonce
, np
);
311 /* add KE payload if need. */
312 np
= (idci
|| idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
314 p
= set_isakmp_payload(p
, iph2
->dhpub
, np
);
317 np
= (idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
319 p
= set_isakmp_payload(p
, iph2
->id
, np
);
323 p
= set_isakmp_payload(p
, iph2
->id_p
, natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
327 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
328 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
331 /* generate HASH(1) */
332 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, body
);
335 "failed to compute HASH");
339 /* send isakmp payload */
340 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
341 if (iph2
->sendbuf
== NULL
) {
343 "failed to get send buffer");
347 /* send the packet, add to the schedule to resend */
348 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
349 if (isakmp_ph2resend(iph2
) == -1) {
351 "failed to send packet");
355 /* change status of isakmp status entry */
356 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG1SENT
);
360 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
361 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
362 CONSTSTR("Initiator, Quick-Mode message 1"),
367 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
368 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
369 CONSTSTR("Initiator, Quick-Mode Message 1"),
370 CONSTSTR("Failed to transmit Quick-Mode Message 1"));
381 #endif /* ENABLE_NATT */
387 * receive from responder
388 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
391 quick_i2recv(iph2
, msg0
)
392 phase2_handle_t
*iph2
;
396 vchar_t
*hbuf
= NULL
; /* for hash computing. */
397 vchar_t
*pbuf
= NULL
; /* for payload parsing */
398 struct isakmp_parse_t
*pa
;
399 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
400 struct isakmp_pl_hash
*hash
= NULL
;
404 int error
= ISAKMP_INTERNAL_ERROR
;
405 struct sockaddr_storage
*natoa_i
= NULL
;
406 struct sockaddr_storage
*natoa_r
= NULL
;
409 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG1SENT
) {
411 "status mismatched %d.\n", iph2
->status
);
416 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
418 "Packet wasn't encrypted.\n");
421 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
424 "failed to decrypt");
428 /* create buffer for validating HASH(2) */
431 * 1. the first one must be HASH
432 * 2. the second one must be SA (added in isakmp-oakley-05!)
433 * 3. two IDs must be considered as IDci, then IDcr
435 pbuf
= isakmp_parse(msg
);
438 "failed to parse msg");
441 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
443 /* HASH payload is fixed postion */
444 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
446 "received invalid next payload type %d, "
448 pa
->type
, ISAKMP_NPTYPE_HASH
);
451 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
455 * this restriction was introduced in isakmp-oakley-05.
456 * we do not check this for backward compatibility.
457 * TODO: command line/config file option to enable/disable this code
459 /* HASH payload is fixed postion */
460 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
461 plog(ASL_LEVEL_WARNING
,
462 "received invalid next payload type %d, "
464 pa
->type
, ISAKMP_NPTYPE_HASH
);
467 /* allocate buffer for computing HASH(2) */
468 tlen
= iph2
->nonce
->l
469 + ntohl(isakmp
->len
) - sizeof(*isakmp
);
472 "invalid length (%lu,%d) while getting hash buffer.\n",
473 iph2
->nonce
->l
, ntohl(isakmp
->len
));
476 hbuf
= vmalloc(tlen
);
479 "failed to get hash buffer.\n");
482 p
= hbuf
->v
+ iph2
->nonce
->l
; /* retain the space for Ni_b */
485 * parse the payloads.
486 * copy non-HASH payloads into hbuf, so that we can validate HASH.
489 f_id
= 0; /* flag to use checking ID */
490 tlen
= 0; /* count payload length except of HASH payload. */
491 for (; pa
->type
; pa
++) {
493 /* copy to buffer for HASH */
494 /* Don't modify the payload */
495 memcpy(p
, pa
->ptr
, pa
->len
);
498 case ISAKMP_NPTYPE_SA
:
499 if (iph2
->sa_ret
!= NULL
) {
501 "Ignored, multiple SA "
502 "isn't supported.\n");
505 if (isakmp_p2ph(&iph2
->sa_ret
, pa
->ptr
) < 0) {
507 "failed to process SA payload");
512 case ISAKMP_NPTYPE_NONCE
:
513 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
515 "failed to process NONCE payload");
520 case ISAKMP_NPTYPE_KE
:
521 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
523 "failed to process KE payload");
528 case ISAKMP_NPTYPE_ID
:
532 if (iph2
->id
== NULL
|| iph2
->id_p
== NULL
) {
533 error
= ISAKMP_INTERNAL_ERROR
; // shouldn't happen
546 /* These ids may not match when natt is used with some devices.
547 * RFC 2407 says that the protocol and port fields should be ignored
548 * if they are zero, therefore they need to be checked individually.
550 struct ipsecdoi_id_b
*id_ptr
= ALIGNED_CAST(struct ipsecdoi_id_b
*)vp
->v
;
551 struct ipsecdoi_pl_id
*idp_ptr
= (struct ipsecdoi_pl_id
*)pa
->ptr
;
553 if (id_ptr
->type
!= idp_ptr
->b
.type
554 || (idp_ptr
->b
.proto_id
!= 0 && idp_ptr
->b
.proto_id
!= id_ptr
->proto_id
)
555 || (idp_ptr
->b
.port
!= 0 && idp_ptr
->b
.port
!= id_ptr
->port
)
556 || memcmp(vp
->v
+ sizeof(struct ipsecdoi_id_b
), (caddr_t
)pa
->ptr
+ sizeof(struct ipsecdoi_pl_id
),
557 vp
->l
- sizeof(struct ipsecdoi_id_b
))) {
558 // to support servers that use our external nat address as our ID
559 if (iph2
->ph1
->natt_flags
& NAT_DETECTED
) {
560 plog(ASL_LEVEL_WARNING
,
561 "mismatched ID was returned - ignored because nat traversal is being used.\n");
562 /* If I'm behind a nat and the ID is type address - save the address
563 * and port for when the peer rekeys.
565 if (f_id
== 0 && (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
)) {
566 if (lcconf
->ext_nat_id
)
567 vfree(lcconf
->ext_nat_id
);
568 if (idp_ptr
->h
.len
< sizeof(struct isakmp_gen
)) {
569 plog(ASL_LEVEL_ERR
, "invalid length (%d) while allocating external nat id.\n", idp_ptr
->h
.len
);
572 lcconf
->ext_nat_id
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
573 if (lcconf
->ext_nat_id
== NULL
) {
574 plog(ASL_LEVEL_ERR
, "memory error while allocating external nat id.\n");
577 memcpy(lcconf
->ext_nat_id
->v
, &(idp_ptr
->b
), lcconf
->ext_nat_id
->l
);
578 if (iph2
->ext_nat_id
)
579 vfree(iph2
->ext_nat_id
);
580 iph2
->ext_nat_id
= vdup(lcconf
->ext_nat_id
);
581 if (iph2
->ext_nat_id
== NULL
) {
582 plog(ASL_LEVEL_ERR
, "memory error while allocating ph2's external nat id.\n");
585 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id
->v
, iph2
->ext_nat_id
->l
, "external nat address saved.\n");
586 } else if (f_id
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
)) {
587 if (iph2
->ext_nat_id_p
)
588 vfree(iph2
->ext_nat_id_p
);
589 iph2
->ext_nat_id_p
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
590 if (iph2
->ext_nat_id_p
== NULL
) {
591 plog(ASL_LEVEL_ERR
, "memory error while allocating peers ph2's external nat id.\n");
594 memcpy(iph2
->ext_nat_id_p
->v
, &(idp_ptr
->b
), iph2
->ext_nat_id_p
->l
);
595 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id_p
->v
, iph2
->ext_nat_id_p
->l
, "peer's external nat address saved.\n");
598 plog(ASL_LEVEL_ERR
, "mismatched ID was returned.\n");
599 error
= ISAKMP_NTYPE_ATTRIBUTES_NOT_SUPPORTED
;
608 case ISAKMP_NPTYPE_N
:
609 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
613 case ISAKMP_NPTYPE_NATOA_DRAFT
:
614 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
615 case ISAKMP_NPTYPE_NATOA_RFC
:
618 struct sockaddr_storage
*daddr
;
620 isakmp_p2ph(&vp
, pa
->ptr
);
623 daddr
= process_natoa_payload(vp
);
625 if (natoa_i
== NULL
) {
627 plog(ASL_LEVEL_DEBUG
, "initiaor rcvd NAT-OA i: %s\n",
628 saddr2str((struct sockaddr
*)natoa_i
));
629 } else if (natoa_r
== NULL
) {
631 plog(ASL_LEVEL_DEBUG
, "initiator rcvd NAT-OA r: %s\n",
632 saddr2str((struct sockaddr
*)natoa_r
));
645 /* don't send information, see ident_r1recv() */
647 "ignore the packet, "
648 "received unexpecting payload type %d.\n",
655 /* compute true length of payload. */
659 /* payload existency check */
660 if (hash
== NULL
|| iph2
->sa_ret
== NULL
|| iph2
->nonce_p
== NULL
) {
662 "few isakmp message received.\n");
666 /* Fixed buffer for calculating HASH */
667 memcpy(hbuf
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
668 plog(ASL_LEVEL_DEBUG
,
669 "HASH allocated:hbuf->l=%zu actual:tlen=%zu\n",
670 hbuf
->l
, tlen
+ iph2
->nonce
->l
);
671 /* adjust buffer length for HASH */
672 hbuf
->l
= iph2
->nonce
->l
+ tlen
;
674 /* validate HASH(2) */
677 vchar_t
*my_hash
= NULL
;
680 r_hash
= (char *)hash
+ sizeof(*hash
);
682 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(2) received:");
684 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
685 if (my_hash
== NULL
) {
687 "failed to compute HASH");
691 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
695 plog(ASL_LEVEL_DEBUG
,
696 "HASH(2) mismatch.\n");
697 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
702 /* validity check SA payload sent from responder */
703 if (ipsecdoi_checkph2proposal(iph2
) < 0) {
705 "failed to validate SA proposal");
706 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
710 /* change status of isakmp status entry */
711 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG2RCVD
);
715 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
716 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
717 CONSTSTR("Initiator, Quick-Mode message 2"),
722 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
723 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
724 CONSTSTR("Initiator, Quick-Mode Message 2"),
725 CONSTSTR("Failed to process Quick-Mode Message 2 "));
736 racoon_free(natoa_i
);
739 racoon_free(natoa_r
);
743 VPTRINIT(iph2
->sa_ret
);
744 VPTRINIT(iph2
->nonce_p
);
745 VPTRINIT(iph2
->dhpub_p
);
756 quick_i3send(iph2
, msg0
)
757 phase2_handle_t
*iph2
;
762 vchar_t
*hash
= NULL
;
765 int error
= ISAKMP_INTERNAL_ERROR
;
766 int packet_error
= -1;
769 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG2RCVD
) {
771 "status mismatched %d.\n", iph2
->status
);
775 /* generate HASH(3) */
779 plog(ASL_LEVEL_DEBUG
, "HASH(3) generate\n");
781 tmp
= vmalloc(iph2
->nonce
->l
+ iph2
->nonce_p
->l
);
784 "failed to get hash buffer.\n");
787 memcpy(tmp
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
788 memcpy(tmp
->v
+ iph2
->nonce
->l
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
790 hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
795 "failed to compute HASH");
800 /* create buffer for isakmp payload */
801 tlen
= sizeof(struct isakmp
)
802 + sizeof(struct isakmp_gen
) + hash
->l
;
806 "failed to get buffer to send.\n");
810 /* create isakmp header */
811 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
814 "failed to create ISAKMP header");
818 /* add HASH(3) payload */
819 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_NONE
);
821 #ifdef HAVE_PRINT_ISAKMP_C
822 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
826 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
827 if (iph2
->sendbuf
== NULL
) {
829 "failed to encrypt packet");
833 /* if there is commit bit, need resending */
834 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
835 /* send the packet, add to the schedule to resend */
836 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
837 if (isakmp_ph2resend(iph2
) == -1) {
839 "failed to send packet, commit-bit");
843 /* send the packet */
844 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
846 "failed to send packet");
851 /* the sending message is added to the received-list. */
852 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
,
854 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
856 "failed to add a response packet to the tree.\n");
860 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
861 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
862 CONSTSTR("Initiator, Quick-Mode message 3"),
866 /* compute both of KEYMATs */
867 if (oakley_compute_keymat(iph2
, INITIATOR
) < 0) {
869 "failed to compute KEYMAT");
873 /* if there is commit bit don't set up SA now. */
874 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
875 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG3SENT
);
880 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
882 /* Do UPDATE for initiator */
883 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
884 if (pk_sendupdate(iph2
) < 0) {
885 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
888 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
890 /* Do ADD for responder */
891 if (pk_sendadd(iph2
) < 0) {
892 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
895 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
901 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
902 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
903 CONSTSTR("Initiator, Quick-Mode Message 3"),
904 CONSTSTR("Failed to transmit Quick-Mode Message 3"));
917 * receive from responder
918 * HDR#*, HASH(4), notify
921 quick_i4recv(iph2
, msg0
)
922 phase2_handle_t
*iph2
;
926 vchar_t
*pbuf
= NULL
; /* for payload parsing */
927 struct isakmp_parse_t
*pa
;
928 struct isakmp_pl_hash
*hash
= NULL
;
929 vchar_t
*notify
= NULL
;
930 int error
= ISAKMP_INTERNAL_ERROR
;
931 int packet_error
= -1;
934 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG3SENT
) {
936 "status mismatched %d.\n", iph2
->status
);
941 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
943 "Packet wasn't encrypted.\n");
946 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
949 "failed to decrypt packet\n");
953 /* validate the type of next payload */
954 pbuf
= isakmp_parse(msg
);
957 "failed to parse msg\n");
961 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
962 pa
->type
!= ISAKMP_NPTYPE_NONE
;
966 case ISAKMP_NPTYPE_HASH
:
967 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
969 case ISAKMP_NPTYPE_N
:
970 if (notify
!= NULL
) {
971 plog(ASL_LEVEL_WARNING
,
972 "Ignoring multiple notifications\n");
975 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
976 notify
= vmalloc(pa
->len
);
977 if (notify
== NULL
) {
979 "failed to get notify buffer.\n");
982 memcpy(notify
->v
, pa
->ptr
, notify
->l
);
985 /* don't send information, see ident_r1recv() */
987 "ignore the packet, "
988 "received unexpecting payload type %d.\n",
994 /* payload existency check */
997 "few isakmp message received.\n");
1001 /* validate HASH(4) */
1004 vchar_t
*my_hash
= NULL
;
1005 vchar_t
*tmp
= NULL
;
1008 r_hash
= (char *)hash
+ sizeof(*hash
);
1010 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(4) validate:");
1012 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
1014 if (my_hash
== NULL
) {
1016 "failed to compute HASH\n");
1020 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1024 plog(ASL_LEVEL_DEBUG
,
1025 "HASH(4) mismatch.\n");
1026 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1031 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1032 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1033 CONSTSTR("Initiator, Quick-Mode message 4"),
1037 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
1039 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
1041 /* don't anything if local test mode. */
1047 /* Do UPDATE for initiator */
1048 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
1049 if (pk_sendupdate(iph2
) < 0) {
1050 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
1053 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
1055 /* Do ADD for responder */
1056 if (pk_sendadd(iph2
) < 0) {
1057 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
1060 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
1066 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1067 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1068 CONSTSTR("Initiator, Quick-Mode Message 4"),
1069 CONSTSTR("Failed to process Quick-Mode Message 4"));
1082 * receive from initiator
1083 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1086 quick_r1recv(iph2
, msg0
)
1087 phase2_handle_t
*iph2
;
1090 vchar_t
*msg
= NULL
;
1091 vchar_t
*hbuf
= NULL
; /* for hash computing. */
1092 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1093 struct isakmp_parse_t
*pa
;
1094 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
1095 struct isakmp_pl_hash
*hash
= NULL
;
1098 int f_id_order
; /* for ID payload detection */
1099 int error
= ISAKMP_INTERNAL_ERROR
;
1100 struct sockaddr_storage
*natoa_i
= NULL
;
1101 struct sockaddr_storage
*natoa_r
= NULL
;
1103 /* validity check */
1104 if (iph2
->status
!= IKEV1_STATE_QUICK_R_START
) {
1106 "status mismatched %d.\n", iph2
->status
);
1111 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1113 "Packet wasn't encrypted.\n");
1114 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1117 /* decrypt packet */
1118 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1121 "failed to decrypt packet\n");
1125 /* create buffer for using to validate HASH(1) */
1128 * 1. the first one must be HASH
1129 * 2. the second one must be SA (added in isakmp-oakley-05!)
1130 * 3. two IDs must be considered as IDci, then IDcr
1132 pbuf
= isakmp_parse(msg
);
1135 "failed to parse msg\n");
1138 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1140 /* HASH payload is fixed postion */
1141 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
1143 "received invalid next payload type %d, "
1145 pa
->type
, ISAKMP_NPTYPE_HASH
);
1146 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1149 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1153 * this restriction was introduced in isakmp-oakley-05.
1154 * we do not check this for backward compatibility.
1155 * TODO: command line/config file option to enable/disable this code
1157 /* HASH payload is fixed postion */
1158 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1159 plog(ASL_LEVEL_WARNING
,
1160 "received invalid next payload type %d, "
1162 pa
->type
, ISAKMP_NPTYPE_SA
);
1163 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1166 /* allocate buffer for computing HASH(1) */
1167 tlen
= ntohl(isakmp
->len
) - sizeof(*isakmp
);
1169 plog(ASL_LEVEL_ERR
, "invalid length (%d) while extracting hash.\n",
1170 ntohl(isakmp
->len
));
1173 hbuf
= vmalloc(tlen
);
1176 "failed to get hash buffer.\n");
1182 * parse the payloads.
1183 * copy non-HASH payloads into hbuf, so that we can validate HASH.
1185 iph2
->sa
= NULL
; /* we don't support multi SAs. */
1186 iph2
->nonce_p
= NULL
;
1187 iph2
->dhpub_p
= NULL
;
1190 tlen
= 0; /* count payload length except of HASH payload. */
1193 * IDi2 MUST be immediatelly followed by IDr2. We allowed the
1194 * illegal case, but logged. First ID payload is to be IDi2.
1195 * And next ID payload is to be IDr2.
1199 for (; pa
->type
; pa
++) {
1201 /* copy to buffer for HASH */
1202 /* Don't modify the payload */
1203 memcpy(p
, pa
->ptr
, pa
->len
);
1205 if (pa
->type
!= ISAKMP_NPTYPE_ID
)
1209 case ISAKMP_NPTYPE_SA
:
1210 if (iph2
->sa
!= NULL
) {
1212 "Multi SAs isn't supported.\n");
1215 if (isakmp_p2ph(&iph2
->sa
, pa
->ptr
) < 0) {
1217 "failed to process SA payload\n");
1222 case ISAKMP_NPTYPE_NONCE
:
1223 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
1225 "failed to process NONCE payload\n");
1230 case ISAKMP_NPTYPE_KE
:
1231 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
1233 "failed to process KE payload\n");
1238 case ISAKMP_NPTYPE_ID
:
1239 if (iph2
->id_p
== NULL
) {
1243 if (isakmp_p2ph(&iph2
->id_p
, pa
->ptr
) < 0) {
1245 "failed to process IDci2 payload\n");
1249 } else if (iph2
->id
== NULL
) {
1251 if (f_id_order
== 0) {
1253 "IDr2 payload is not "
1254 "immediatelly followed "
1255 "by IDi2. We allowed.\n");
1256 /* XXX we allowed in this case. */
1259 if (isakmp_p2ph(&iph2
->id
, pa
->ptr
) < 0) {
1261 "failed to process IDcr2 payload\n");
1265 plogdump(ASL_LEVEL_ERR
, iph2
->id
->v
, iph2
->id
->l
, "received too many ID payloads");
1266 error
= ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1271 case ISAKMP_NPTYPE_N
:
1272 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1276 case ISAKMP_NPTYPE_NATOA_DRAFT
:
1277 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
1278 case ISAKMP_NPTYPE_NATOA_RFC
:
1281 struct sockaddr_storage
*daddr
;
1283 isakmp_p2ph(&vp
, pa
->ptr
);
1286 daddr
= process_natoa_payload(vp
);
1288 if (natoa_i
== NULL
) {
1290 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA i: %s\n",
1291 saddr2str((struct sockaddr
*)natoa_i
));
1292 } else if (natoa_r
== NULL
) {
1294 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA r: %s\n",
1295 saddr2str((struct sockaddr
*)natoa_r
));
1309 "ignore the packet, "
1310 "received unexpected payload type %d.\n",
1312 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1318 /* compute true length of payload. */
1322 /* payload existency check */
1323 if (hash
== NULL
|| iph2
->sa
== NULL
|| iph2
->nonce_p
== NULL
) {
1325 "expected isakmp payloads missing.\n");
1326 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1331 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "received IDci2:");
1334 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "received IDcr2:");
1337 /* adjust buffer length for HASH */
1340 /* validate HASH(1) */
1343 vchar_t
*my_hash
= NULL
;
1346 r_hash
= (caddr_t
)hash
+ sizeof(*hash
);
1348 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(1) validate:");
1350 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
1351 if (my_hash
== NULL
) {
1353 "failed to compute HASH\n");
1357 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1362 "HASH(1) mismatch.\n");
1363 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1369 error
= get_sainfo_r(iph2
);
1372 "failed to get sainfo.\n");
1376 /* check the existence of ID payload and create responder's proposal */
1377 error
= get_proposal_r(iph2
);
1380 /* generate a policy template from peer's proposal */
1381 if (set_proposal_from_proposal(iph2
)) {
1383 "failed to generate a proposal template "
1384 "from client's proposal.\n");
1385 error
= ISAKMP_INTERNAL_ERROR
;
1390 /* select single proposal or reject it. */
1391 if (ipsecdoi_selectph2proposal(iph2
) < 0) {
1393 "failed to select proposal.\n");
1394 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1400 "failed to get proposal for responder.\n");
1404 /* check KE and attribute of PFS */
1405 if (iph2
->dhpub_p
!= NULL
&& iph2
->approval
->pfs_group
== 0) {
1407 "no PFS is specified, but peer sends KE.\n");
1408 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1411 if (iph2
->dhpub_p
== NULL
&& iph2
->approval
->pfs_group
!= 0) {
1413 "PFS is specified, but peer doesn't sends KE.\n");
1414 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1418 ike_session_update_mode(iph2
); /* update the mode, now that we have a proposal */
1421 * save the packet from the initiator in order to resend the
1422 * responder's first packet against this packet.
1424 iph2
->msg1
= vdup(msg0
);
1426 /* change status of isakmp status entry */
1427 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG1RCVD
);
1431 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1432 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1433 CONSTSTR("Responder, Quick-Mode message 1"),
1438 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1439 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1440 CONSTSTR("Responder, Quick-Mode Message 1"),
1441 CONSTSTR("Failed to process Quick-Mode Message 1"));
1452 racoon_free(natoa_i
);
1455 racoon_free(natoa_r
);
1461 VPTRINIT(iph2
->nonce_p
);
1462 VPTRINIT(iph2
->dhpub_p
);
1464 VPTRINIT(iph2
->id_p
);
1471 * call pfkey_getspi.
1474 quick_rprep(iph2
, msg
)
1475 phase2_handle_t
*iph2
;
1478 int error
= ISAKMP_INTERNAL_ERROR
;
1480 /* validity check */
1481 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG1RCVD
) {
1483 "status mismatched %d.\n", iph2
->status
);
1487 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_GETSPISENT
);
1489 /* send getspi message */
1490 if (pk_sendgetspi(iph2
) < 0) {
1492 "failed to send getspi");
1496 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
1498 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
1499 pfkey_timeover_stub
, iph2
);
1509 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1512 quick_r2send(iph2
, msg
)
1513 phase2_handle_t
*iph2
;
1516 vchar_t
*body
= NULL
;
1517 vchar_t
*hash
= NULL
;
1518 vchar_t
*natoa_i
= NULL
;
1519 vchar_t
*natoa_r
= NULL
;
1521 struct isakmp_gen
*gen
;
1524 int error
= ISAKMP_INTERNAL_ERROR
;
1526 u_int8_t
*np_p
= NULL
;
1528 /* validity check */
1531 "msg has to be NULL in this function.\n");
1534 if (iph2
->status
!= IKEV1_STATE_QUICK_R_GETSPIDONE
) {
1536 "status mismatched %d.\n", iph2
->status
);
1540 /* update responders SPI */
1541 if (ipsecdoi_updatespi(iph2
) < 0) {
1542 plog(ASL_LEVEL_ERR
, "failed to update spi.\n");
1546 /* generate NONCE value */
1547 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
1548 if (iph2
->nonce
== NULL
) {
1550 "failed to generate NONCE");
1554 /* generate KE value if need */
1555 pfsgroup
= iph2
->approval
->pfs_group
;
1556 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1557 /* DH group settting if PFS is required. */
1558 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
1560 "failed to set DH value.\n");
1563 /* generate DH public value */
1565 if (oakley_dh_generate(iph2
->pfsgrp
,
1566 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
1568 if (oakley_dh_generate(iph2
->pfsgrp
,
1569 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
1572 "failed to generate DH public");
1577 /* create SA;NONCE payload, and KE and ID if need */
1578 tlen
= sizeof(*gen
) + iph2
->sa_ret
->l
1579 + sizeof(*gen
) + iph2
->nonce
->l
;
1580 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1581 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
1582 if (iph2
->id_p
!= NULL
)
1583 tlen
+= (sizeof(*gen
) + iph2
->id_p
->l
1584 + sizeof(*gen
) + iph2
->id
->l
);
1588 * RFC3947 5.2. if we chose UDP-Encapsulated-Transport
1589 * we should send NAT-OA
1591 if (ipsecdoi_any_transportmode(iph2
->approval
)
1592 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
1593 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
1594 if (natoa_type
== -1) {
1596 "failed to create NATOA payloads");
1599 else if (natoa_type
!= 0) {
1600 tlen
+= sizeof(*gen
) + natoa_i
->l
;
1601 tlen
+= sizeof(*gen
) + natoa_r
->l
;
1603 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "responder send NAT-OAi:");
1604 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "responder send NAT-OAr:");
1609 plog(ASL_LEVEL_DEBUG
, "Approved SA\n");
1610 printsaprop0(ASL_LEVEL_DEBUG
, iph2
->approval
);
1612 body
= vmalloc(tlen
);
1615 "failed to get buffer to send.\n");
1620 /* make SA payload */
1621 p
= set_isakmp_payload(body
->v
, iph2
->sa_ret
, ISAKMP_NPTYPE_NONCE
);
1623 /* add NONCE payload */
1624 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1625 p
= set_isakmp_payload(p
, iph2
->nonce
,
1626 (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1628 : (iph2
->id_p
!= NULL
1630 : (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
)));
1632 /* add KE payload if need. */
1633 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1634 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1635 p
= set_isakmp_payload(p
, iph2
->dhpub
,
1636 (iph2
->id_p
== NULL
) ? (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
) : ISAKMP_NPTYPE_ID
);
1639 /* add ID payloads received. */
1640 if (iph2
->id_p
!= NULL
) {
1642 p
= set_isakmp_payload(p
, iph2
->id_p
, ISAKMP_NPTYPE_ID
);
1643 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "sending IDci2:");
1645 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1646 p
= set_isakmp_payload(p
, iph2
->id
, (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
));
1647 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "sending IDcr2:");
1650 /* add a RESPONDER-LIFETIME notify payload if needed */
1652 vchar_t
*data
= NULL
;
1653 struct saprop
*pp
= iph2
->approval
;
1656 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_SEC
) {
1657 u_int32_t v
= htonl((u_int32_t
)pp
->lifetime
);
1658 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1659 IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
1662 "failed to add RESPONDER-LIFETIME notify (type) payload");
1665 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1666 (caddr_t
)&v
, sizeof(v
));
1669 "failed to add RESPONDER-LIFETIME notify (value) payload");
1673 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_KB
) {
1674 u_int32_t v
= htonl((u_int32_t
)pp
->lifebyte
);
1675 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1676 IPSECDOI_ATTR_SA_LD_TYPE_KB
);
1679 "failed to add RESPONDER-LIFETIME notify (type) payload");
1682 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1683 (caddr_t
)&v
, sizeof(v
));
1686 "failed to add RESPONDER-LIFETIME notify (value) payload");
1692 * XXX Is there only single RESPONDER-LIFETIME payload in a IKE message
1693 * in the case of SA bundle ?
1696 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1697 body
= isakmp_add_pl_n(body
, &np_p
,
1698 ISAKMP_NTYPE_RESPONDER_LIFETIME
, pr
, data
);
1701 "invalid RESPONDER-LIFETIME payload");
1703 return error
; /* XXX */
1712 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
1713 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
1716 /* generate HASH(2) */
1720 tmp
= vmalloc(iph2
->nonce_p
->l
+ body
->l
);
1723 "failed to get hash buffer.\n");
1726 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1727 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, body
->v
, body
->l
);
1729 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, tmp
);
1734 "failed to compute HASH");
1739 /* send isakmp payload */
1740 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
1741 if (iph2
->sendbuf
== NULL
) {
1743 "failed to get send buffer");
1747 /* send the packet, add to the schedule to resend */
1748 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
1749 if (isakmp_ph2resend(iph2
) == -1) {
1751 "failed to send packet");
1755 /* the sending message is added to the received-list. */
1756 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, iph2
->msg1
,
1757 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
1759 "failed to add a response packet to the tree.\n");
1763 /* change status of isakmp status entry */
1764 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG2SENT
);
1768 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1769 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1770 CONSTSTR("Responder, Quick-Mode message 2"),
1775 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1776 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1777 CONSTSTR("Responder, Quick-Mode Message 2"),
1778 CONSTSTR("Failed to transmit Quick-Mode Message 2"));
1793 * receive from initiator
1797 quick_r3recv(iph2
, msg0
)
1798 phase2_handle_t
*iph2
;
1801 vchar_t
*msg
= NULL
;
1802 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1803 struct isakmp_parse_t
*pa
;
1804 struct isakmp_pl_hash
*hash
= NULL
;
1805 int error
= ISAKMP_INTERNAL_ERROR
;
1807 /* validity check */
1808 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG2SENT
) {
1810 "status mismatched %d.\n", iph2
->status
);
1814 /* decrypt packet */
1815 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1817 "Packet wasn't encrypted.\n");
1820 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1823 "failed to decrypt packet\n");
1827 /* validate the type of next payload */
1828 pbuf
= isakmp_parse(msg
);
1831 "failed to parse msg\n");
1835 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1836 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1840 case ISAKMP_NPTYPE_HASH
:
1841 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1843 case ISAKMP_NPTYPE_N
:
1844 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1847 /* don't send information, see ident_r1recv() */
1849 "ignore the packet, "
1850 "received unexpecting payload type %d.\n",
1856 /* payload existency check */
1859 "few isakmp message received.\n");
1863 /* validate HASH(3) */
1864 /* HASH(3) = prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b) */
1867 vchar_t
*my_hash
= NULL
;
1868 vchar_t
*tmp
= NULL
;
1871 r_hash
= (char *)hash
+ sizeof(*hash
);
1873 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(3) validate:");
1875 tmp
= vmalloc(iph2
->nonce_p
->l
+ iph2
->nonce
->l
);
1878 "failed to get hash buffer.\n");
1881 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1882 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, iph2
->nonce
->v
, iph2
->nonce
->l
);
1884 my_hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
1886 if (my_hash
== NULL
) {
1888 "failed to compute HASH\n");
1892 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1897 "HASH(3) mismatch.\n");
1898 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1903 /* if there is commit bit, don't set up SA now. */
1904 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
1905 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG3RCVD
);
1907 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
1911 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1912 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1913 CONSTSTR("Responder, Quick-Mode message 3"),
1918 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1919 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1920 CONSTSTR("Responder, Quick-Mode Message 3"),
1921 CONSTSTR("Failed to process Quick-Mode Message 3"));
1933 * HDR#*, HASH(4), notify
1936 quick_r4send(iph2
, msg0
)
1937 phase2_handle_t
*iph2
;
1940 vchar_t
*buf
= NULL
;
1941 vchar_t
*myhash
= NULL
;
1942 struct isakmp_pl_n
*n
;
1943 vchar_t
*notify
= NULL
;
1946 int error
= ISAKMP_INTERNAL_ERROR
;
1948 /* validity check */
1949 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG3RCVD
) {
1951 "status mismatched %d.\n", iph2
->status
);
1955 /* generate HASH(4) */
1956 /* XXX What can I do in the case of multiple different SA */
1957 plog(ASL_LEVEL_DEBUG
, "HASH(4) generate\n");
1959 /* XXX What should I do if there are multiple SAs ? */
1960 tlen
= sizeof(struct isakmp_pl_n
) + iph2
->approval
->head
->spisize
;
1961 notify
= vmalloc(tlen
);
1962 if (notify
== NULL
) {
1964 "failed to get notify buffer.\n");
1967 n
= (struct isakmp_pl_n
*)notify
->v
;
1968 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1969 n
->h
.len
= htons(tlen
);
1970 n
->doi
= htonl(IPSEC_DOI
);
1971 n
->proto_id
= iph2
->approval
->head
->proto_id
;
1972 n
->spi_size
= sizeof(iph2
->approval
->head
->spisize
);
1973 n
->type
= htons(ISAKMP_NTYPE_CONNECTED
);
1974 memcpy(n
+ 1, &iph2
->approval
->head
->spi
, iph2
->approval
->head
->spisize
);
1976 myhash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
1977 if (myhash
== NULL
) {
1979 "failed to compute HASH");
1983 /* create buffer for isakmp payload */
1984 tlen
= sizeof(struct isakmp
)
1985 + sizeof(struct isakmp_gen
) + myhash
->l
1987 buf
= vmalloc(tlen
);
1990 "failed to get buffer to send.\n");
1994 /* create isakmp header */
1995 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
1998 "failed to set ISAKMP header");
2002 /* add HASH(4) payload */
2003 p
= set_isakmp_payload(p
, myhash
, ISAKMP_NPTYPE_N
);
2005 /* add notify payload */
2006 memcpy(p
, notify
->v
, notify
->l
);
2008 #ifdef HAVE_PRINT_ISAKMP_C
2009 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
2013 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
2014 if (iph2
->sendbuf
== NULL
) {
2016 "failed to encrypt packet");
2020 /* send the packet */
2021 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
2023 "failed to send packet");
2027 /* the sending message is added to the received-list. */
2028 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, msg0
,
2029 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
2030 plog(ASL_LEVEL_ERR
,
2031 "failed to add a response packet to the tree.\n");
2035 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
2039 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
2040 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
2041 CONSTSTR("Responder, Quick-Mode message 4"),
2046 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
2047 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
2048 CONSTSTR("Responder, Quick-Mode Message 4"),
2049 CONSTSTR("Failed to transmit Quick-Mode Message 4"));
2066 quick_rfinalize(iph2
, msg0
)
2067 phase2_handle_t
*iph2
;
2070 vchar_t
*msg
= NULL
;
2071 int error
= ISAKMP_INTERNAL_ERROR
;
2073 /* validity check */
2074 if (iph2
->status
!= IKEV1_STATE_QUICK_R_COMMIT
) {
2076 "status mismatched %d.\n", iph2
->status
);
2080 /* compute both of KEYMATs */
2081 if (oakley_compute_keymat(iph2
, RESPONDER
) < 0) {
2083 "failed to compute KEYMAT");
2087 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_ADDSA
);
2089 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
2091 /* don't anything if local test mode. */
2097 /* Do UPDATE as responder */
2098 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
2099 if (pk_sendupdate(iph2
) < 0) {
2100 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
2103 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
2105 /* Do ADD for responder */
2106 if (pk_sendadd(iph2
) < 0) {
2107 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
2110 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
2113 * set policies into SPD if the policy is generated
2114 * from peer's policy.
2116 if (iph2
->spidx_gen
) {
2118 struct policyindex
*spidx
;
2119 struct sockaddr_storage addr
;
2121 struct sockaddr_storage
*src
= iph2
->src
;
2122 struct sockaddr_storage
*dst
= iph2
->dst
;
2124 /* make inbound policy */
2127 if (pk_sendspdupdate2(iph2
) < 0) {
2129 "pfkey spdupdate2(inbound) failed.\n");
2132 plog(ASL_LEVEL_DEBUG
,
2133 "pfkey spdupdate2(inbound) sent.\n");
2135 spidx
= iph2
->spidx_gen
;
2136 #ifdef HAVE_POLICY_FWD
2137 /* make forward policy if required */
2138 if (tunnel_mode_prop(iph2
->approval
)) {
2139 spidx
->dir
= IPSEC_DIR_FWD
;
2140 if (pk_sendspdupdate2(iph2
) < 0) {
2142 "pfkey spdupdate2(forward) failed.\n");
2145 plog(ASL_LEVEL_DEBUG
,
2146 "pfkey spdupdate2(forward) sent.\n");
2150 /* make outbound policy */
2153 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
2155 spidx
->src
= spidx
->dst
;
2157 pref
= spidx
->prefs
;
2158 spidx
->prefs
= spidx
->prefd
;
2159 spidx
->prefd
= pref
;
2161 if (pk_sendspdupdate2(iph2
) < 0) {
2163 "pfkey spdupdate2(outbound) failed.\n");
2166 plog(ASL_LEVEL_DEBUG
,
2167 "pfkey spdupdate2(outbound) sent.\n");
2169 /* spidx_gen is unnecessary any more */
2170 delsp_bothdir(iph2
->spidx_gen
);
2171 racoon_free(iph2
->spidx_gen
);
2172 iph2
->spidx_gen
= NULL
;
2173 iph2
->generated_spidx
=1;
2186 * create HASH, body (SA, NONCE) payload with isakmp header.
2189 quick_ir1mx(iph2
, body
, hash
)
2190 phase2_handle_t
*iph2
;
2191 vchar_t
*body
, *hash
;
2193 struct isakmp
*isakmp
;
2194 vchar_t
*buf
= NULL
, *new = NULL
;
2197 struct isakmp_gen
*gen
;
2198 int error
= ISAKMP_INTERNAL_ERROR
;
2200 /* create buffer for isakmp payload */
2201 tlen
= sizeof(*isakmp
)
2202 + sizeof(*gen
) + hash
->l
2204 buf
= vmalloc(tlen
);
2207 "failed to get buffer to send.\n");
2211 /* re-set encryption flag, for serurity. */
2212 iph2
->flags
|= ISAKMP_FLAG_E
;
2214 /* set isakmp header */
2215 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
2218 "failed to set ISAKMP header");
2222 /* add HASH payload */
2223 /* XXX is next type always SA ? */
2224 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_SA
);
2226 /* add body payload */
2227 memcpy(p
, body
->v
, body
->l
);
2229 #ifdef HAVE_PRINT_ISAKMP_C
2230 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
2234 new = oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
2237 "failed to encrypt packet");
2248 if (error
&& buf
!= NULL
) {
2257 * get remote's sainfo.
2258 * NOTE: this function is for responder.
2262 phase2_handle_t
*iph2
;
2264 vchar_t
*idsrc
= NULL
, *iddst
= NULL
;
2266 int error
= ISAKMP_INTERNAL_ERROR
;
2267 struct sainfo
*anonymous
= NULL
;
2269 if (iph2
->id
== NULL
) {
2270 switch (iph2
->src
->ss_family
) {
2272 prefixlen
= sizeof(struct in_addr
) << 3;
2275 prefixlen
= sizeof(struct in6_addr
) << 3;
2279 "invalid family: %d\n", iph2
->src
->ss_family
);
2282 idsrc
= ipsecdoi_sockaddr2id(iph2
->src
, prefixlen
,
2285 idsrc
= vdup(iph2
->id
);
2287 if (idsrc
== NULL
) {
2289 "failed to set ID for source.\n");
2293 if (iph2
->id_p
== NULL
) {
2294 switch (iph2
->dst
->ss_family
) {
2296 prefixlen
= sizeof(struct in_addr
) << 3;
2299 prefixlen
= sizeof(struct in6_addr
) << 3;
2303 "invalid family: %d\n", iph2
->dst
->ss_family
);
2306 iddst
= ipsecdoi_sockaddr2id(iph2
->dst
, prefixlen
,
2309 iddst
= vdup(iph2
->id_p
);
2311 if (iddst
== NULL
) {
2313 "failed to set ID for destination.\n");
2317 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 0);
2318 // track anonymous sainfo, because we'll try to find a better sainfo if this is a client
2319 if (iph2
->sainfo
&& iph2
->sainfo
->idsrc
== NULL
)
2320 anonymous
= iph2
->sainfo
;
2322 if (iph2
->sainfo
== NULL
||
2323 (anonymous
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2324 if ((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
)
2325 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 1);
2327 plog(ASL_LEVEL_DEBUG
,
2328 "get_sainfo_r case 1.\n");
2330 // still no sainfo (or anonymous): for client, fallback to sainfo used by a previous established phase2
2331 if (iph2
->sainfo
== NULL
||
2332 (iph2
->sainfo
->idsrc
== NULL
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2333 ike_session_get_sainfo_r(iph2
);
2335 plog(ASL_LEVEL_DEBUG
,
2336 "get_sainfo_r case 2.\n");
2338 // still no sainfo (or anonymous): fallback to sainfo picked by dst id
2339 if ((iph2
->sainfo
== NULL
|| iph2
->sainfo
->idsrc
== NULL
) && iph2
->id_p
) {
2340 plog(ASL_LEVEL_DEBUG
,
2341 "get_sainfo_r about to try dst id only.\n");
2342 iph2
->sainfo
= getsainfo_by_dst_id(iph2
->id_p
, iph2
->ph1
->id_p
);
2344 plog(ASL_LEVEL_DEBUG
,
2345 "get_sainfo_r case 3.\n");
2346 if (iph2
->sainfo
->idsrc
== NULL
)
2347 anonymous
= iph2
->sainfo
;
2352 if (iph2
->sainfo
== NULL
) {
2353 if (anonymous
== NULL
) {
2355 "failed to get sainfo.\n");
2358 iph2
->sainfo
= anonymous
;
2360 retain_sainfo(iph2
->sainfo
);
2362 #ifdef ENABLE_HYBRID
2363 /* xauth group inclusion check */
2364 if (iph2
->sainfo
->group
!= NULL
)
2365 if(group_check(iph2
->ph1
,&iph2
->sainfo
->group
->v
,1)) {
2367 "failed to group check");
2372 plog(ASL_LEVEL_DEBUG
,
2373 "selected sainfo: %s\n", sainfo2str(iph2
->sainfo
));
2386 get_proposal_r(iph2
)
2387 phase2_handle_t
*iph2
;
2389 int error
= get_proposal_r_remote(iph2
, 0);
2390 if (error
!= -2 && error
!= 0 &&
2391 (((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
) ||
2392 (iph2
->parent_session
&& iph2
->parent_session
->is_client
))) {
2393 if (iph2
->parent_session
&& iph2
->parent_session
->is_client
)
2394 error
= ike_session_get_proposal_r(iph2
);
2395 if (error
!= -2 && error
!= 0)
2396 error
= get_proposal_r_remote(iph2
, 1);
2402 * Copy both IP addresses in ID payloads into [src,dst]_id if both ID types
2403 * are IP address and same address family.
2404 * Then get remote's policy from SPD copied from kernel.
2405 * If the type of ID payload is address or subnet type, then the index is
2406 * made from the payload. If there is no ID payload, or the type of ID
2407 * payload is NOT address type, then the index is made from the address
2409 * NOTE: This function is only for responder.
2412 get_proposal_r_remote(iph2
, ignore_id
)
2413 phase2_handle_t
*iph2
;
2416 struct policyindex spidx
;
2417 struct secpolicy
*sp_in
, *sp_out
;
2418 int idi2type
= 0; /* switch whether copy IDs into id[src,dst]. */
2419 int error
= ISAKMP_INTERNAL_ERROR
;
2420 int generated_policy_exit_early
= 0;
2422 /* check the existence of ID payload */
2423 if ((iph2
->id_p
!= NULL
&& iph2
->id
== NULL
)
2424 || (iph2
->id_p
== NULL
&& iph2
->id
!= NULL
)) {
2426 "Both IDs wasn't found in payload.\n");
2427 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2430 /* make sure if id[src,dst] is null (if use_remote_addr == 0). */
2431 if (!ignore_id
&& (iph2
->src_id
|| iph2
->dst_id
)) {
2433 "Why do ID[src,dst] exist already.\n");
2434 return ISAKMP_INTERNAL_ERROR
;
2437 plog(ASL_LEVEL_DEBUG
,
2438 "%s: ignore_id %x.\n", __FUNCTION__
, ignore_id
);
2440 memset(&spidx
, 0, sizeof(spidx
));
2442 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
2443 /* make a spidx; a key to search SPD */
2444 spidx
.dir
= IPSEC_DIR_INBOUND
;
2448 * make destination address in spidx from either ID payload
2449 * or phase 1 address into a address in spidx.
2450 * If behind a nat - use phase1 address because server's
2451 * use the nat's address in the ID payload.
2453 if (iph2
->id
!= NULL
2455 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2456 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
2457 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2458 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2459 /* get a destination address of a policy */
2460 error
= ipsecdoi_id2sockaddr(iph2
->id
, &spidx
.dst
,
2461 &spidx
.prefd
, &spidx
.ul_proto
, iph2
->version
);
2467 * get scopeid from the SA address.
2468 * note that the phase 1 source address is used as
2469 * a destination address to search for a inbound policy entry
2470 * because rcoon is responder.
2472 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
2473 error
= setscopeid(&spidx
.dst
, iph2
->src
);
2479 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2480 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
2481 idi2type
= _XIDT(iph2
->id
);
2485 plog(ASL_LEVEL_DEBUG
,
2486 "Get a destination address of SP index "
2487 "from Phase 1 address "
2488 "due to no ID payloads found "
2489 "OR because ID type is not address.\n");
2492 * copy the SOURCE address of IKE into the DESTINATION address
2493 * of the key to search the SPD because the direction of policy
2496 memcpy(&spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
2497 switch (spidx
.dst
.ss_family
) {
2500 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.dst
;
2501 spidx
.prefd
= sizeof(struct in_addr
) << 3;
2502 s
->sin_port
= htons(0);
2507 spidx
.prefd
= sizeof(struct in6_addr
) << 3;
2516 /* make source address in spidx */
2517 if (iph2
->id_p
!= NULL
2519 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
2520 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
2521 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2522 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2523 /* get a source address of inbound SA */
2524 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &spidx
.src
,
2525 &spidx
.prefs
, &spidx
.ul_proto
, iph2
->version
);
2531 * get scopeid from the SA address.
2532 * for more detail, see above of this function.
2534 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
2535 error
= setscopeid(&spidx
.src
, iph2
->dst
);
2541 /* make id[src,dst] if both ID types are IP address and same */
2542 if (_XIDT(iph2
->id_p
) == idi2type
2543 && spidx
.dst
.ss_family
== spidx
.src
.ss_family
) {
2544 iph2
->src_id
= dupsaddr(&spidx
.dst
);
2545 if (iph2
->src_id
== NULL
) {
2547 "buffer allocation failed.\n");
2548 return ISAKMP_INTERNAL_ERROR
;
2550 iph2
->dst_id
= dupsaddr(&spidx
.src
);
2551 if (iph2
->dst_id
== NULL
) {
2553 "buffer allocation failed.\n");
2554 return ISAKMP_INTERNAL_ERROR
;
2559 plog(ASL_LEVEL_DEBUG
,
2560 "Get a source address of SP index "
2561 "from Phase 1 address "
2562 "due to no ID payloads found "
2563 "OR because ID type is not address.\n");
2565 /* see above comment. */
2566 memcpy(&spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
2567 switch (spidx
.src
.ss_family
) {
2570 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.src
;
2571 spidx
.prefs
= sizeof(struct in_addr
) << 3;
2572 s
->sin_port
= htons(0);
2577 spidx
.prefs
= sizeof(struct in6_addr
) << 3;
2588 plog(ASL_LEVEL_DEBUG
,
2589 "get a src address from ID payload "
2590 "%s prefixlen=%u ul_proto=%u\n",
2591 saddr2str((struct sockaddr
*)&spidx
.src
),
2592 spidx
.prefs
, spidx
.ul_proto
);
2593 plog(ASL_LEVEL_DEBUG
,
2594 "get dst address from ID payload "
2595 "%s prefixlen=%u ul_proto=%u\n",
2596 saddr2str((struct sockaddr
*)&spidx
.dst
),
2597 spidx
.prefd
, spidx
.ul_proto
);
2600 * convert the ul_proto if it is 0
2601 * because 0 in ID payload means a wild card.
2603 if (spidx
.ul_proto
== 0)
2604 spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
2606 /* get inbound policy */
2607 sp_in
= getsp_r(&spidx
, iph2
);
2608 if (sp_in
== NULL
|| sp_in
->policy
== IPSEC_POLICY_GENERATE
) {
2609 if (iph2
->ph1
->rmconf
->gen_policy
) {
2611 plog(ASL_LEVEL_NOTICE
,
2612 "Update the generated policy : %s\n",
2615 plog(ASL_LEVEL_NOTICE
,
2617 "try to generate the policy : %s\n",
2619 iph2
->spidx_gen
= (struct policyindex
*)racoon_malloc(sizeof(spidx
));
2620 if (!iph2
->spidx_gen
) {
2622 "buffer allocation failed.\n");
2623 return ISAKMP_INTERNAL_ERROR
;
2625 memcpy(iph2
->spidx_gen
, &spidx
, sizeof(spidx
));
2626 generated_policy_exit_early
= 1; /* special value */
2629 "no policy found: %s\n", spidx2str(&spidx
));
2630 return ISAKMP_INTERNAL_ERROR
;
2634 /* get outbound policy */
2636 struct sockaddr_storage addr
;
2639 spidx
.dir
= IPSEC_DIR_OUTBOUND
;
2641 spidx
.src
= spidx
.dst
;
2644 spidx
.prefs
= spidx
.prefd
;
2647 sp_out
= getsp_r(&spidx
, iph2
);
2649 plog(ASL_LEVEL_WARNING
,
2650 "no outbound policy found: %s\n",
2655 iph2
->spid
= sp_out
->id
;
2660 plog(ASL_LEVEL_DEBUG
,
2661 "suitable SP found:%s\n", spidx2str(&spidx
));
2663 if (generated_policy_exit_early
) {
2668 * In the responder side, the inbound policy should be using IPsec.
2669 * outbound policy is not checked currently.
2671 if (sp_in
->policy
!= IPSEC_POLICY_IPSEC
) {
2673 "policy found, but no IPsec required: %s\n",
2675 return ISAKMP_INTERNAL_ERROR
;
2678 /* set new proposal derived from a policy into the iph2->proposal. */
2679 if (set_proposal_from_policy(iph2
, sp_in
, sp_out
) < 0) {
2681 "failed to create saprop.\n");
2682 return ISAKMP_INTERNAL_ERROR
;