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>
60 #ifndef HAVE_NETINET6_IPSEC
61 #include <netinet/ipsec.h>
63 #include <netinet6/ipsec.h>
74 #include "localconf.h"
75 #include "remoteconf.h"
79 #include "isakmp_var.h"
81 #include "isakmp_inf.h"
82 #include "isakmp_quick.h"
84 #include "ipsec_doi.h"
85 #include "crypto_openssl.h"
88 #include "algorithm.h"
93 #include "nattraversal.h"
94 #include "ipsecSessionTracer.h"
95 #include "ipsecMessageTracer.h"
97 #include <Security/SecDH.h>
101 static vchar_t
*quick_ir1mx (phase2_handle_t
*, vchar_t
*, vchar_t
*);
102 static int get_proposal_r_remote (phase2_handle_t
*, int);
108 * begin Quick Mode as initiator. send pfkey getspi message to kernel.
111 quick_iprep(iph2
, msg
)
112 phase2_handle_t
*iph2
;
113 vchar_t
*msg
; /* must be null pointer */
115 int error
= ISAKMP_INTERNAL_ERROR
;
118 if (iph2
->status
!= IKEV1_STATE_QUICK_I_START
) {
120 "status mismatched %d.\n", iph2
->status
);
124 iph2
->msgid
= isakmp_newmsgid2(iph2
->ph1
);
125 if (iph2
->ivm
!= NULL
)
126 oakley_delivm(iph2
->ivm
);
127 iph2
->ivm
= oakley_newiv2(iph2
->ph1
, iph2
->msgid
);
128 if (iph2
->ivm
== NULL
)
131 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_GETSPISENT
);
133 /* don't anything if local test mode. */
139 /* send getspi message */
140 if (pk_sendgetspi(iph2
) < 0) {
142 "failed to send getspi message");
146 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
148 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
149 pfkey_timeover_stub
, iph2
);
159 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
162 quick_i1send(iph2
, msg
)
163 phase2_handle_t
*iph2
;
164 vchar_t
*msg
; /* must be null pointer */
166 vchar_t
*body
= NULL
;
167 vchar_t
*hash
= NULL
;
169 vchar_t
*natoa_i
= NULL
;
170 vchar_t
*natoa_r
= NULL
;
171 #endif /* ENABLE_NATT */
173 struct isakmp_gen
*gen
;
176 int error
= ISAKMP_INTERNAL_ERROR
;
177 int pfsgroup
, idci
, idcr
;
179 struct ipsecdoi_id_b
*id
, *id_p
;
184 "msg has to be NULL in this function.\n");
187 if (iph2
->status
!= IKEV1_STATE_QUICK_I_GETSPIDONE
) {
189 "status mismatched %d.\n", iph2
->status
);
193 /* create SA payload for my proposal */
194 if (ipsecdoi_setph2proposal(iph2
, FALSE
) < 0) {
196 "failed to set proposal");
200 /* generate NONCE value */
201 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
202 if (iph2
->nonce
== NULL
) {
204 "failed to generate NONCE");
209 * DH value calculation is kicked out into cfparse.y.
210 * because pfs group can not be negotiated, it's only to be checked
213 /* generate KE value if need */
214 pfsgroup
= iph2
->proposal
->pfs_group
;
216 /* DH group settting if PFS is required. */
217 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
219 "failed to set DH value.\n");
223 if (oakley_dh_generate(iph2
->pfsgrp
,
224 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
226 if (oakley_dh_generate(iph2
->pfsgrp
,
227 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
230 "failed to generate DH");
235 /* generate ID value */
236 if (ipsecdoi_setid2(iph2
) < 0) {
238 "failed to get ID.\n");
241 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "IDci:\n");
242 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "IDcr:\n");
245 * we do not attach IDci nor IDcr, under the following condition:
246 * - all proposals are transport mode
248 * - id payload suggests to encrypt all the traffic (no specific
251 id
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
;
252 id_p
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id_p
->v
;
253 if (id
->proto_id
== 0
254 && id_p
->proto_id
== 0
255 && iph2
->ph1
->rmconf
->support_proxy
== 0
256 && ipsecdoi_transportmode(iph2
->proposal
)) {
261 /* create SA;NONCE payload, and KE if need, and IDii, IDir. */
262 tlen
= + sizeof(*gen
) + iph2
->sa
->l
263 + sizeof(*gen
) + iph2
->nonce
->l
;
265 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
267 tlen
+= sizeof(*gen
) + iph2
->id
->l
;
269 tlen
+= sizeof(*gen
) + iph2
->id_p
->l
;
273 * RFC3947 5.2. if we propose UDP-Encapsulated-Transport
274 * we should send NAT-OA
276 if (ipsecdoi_any_transportmode(iph2
->proposal
)
277 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
278 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
279 if (natoa_type
== -1) {
281 "failed to generate NAT-OA payload.\n");
283 } else if (natoa_type
!= 0) {
284 tlen
+= sizeof(*gen
) + natoa_i
->l
;
285 tlen
+= sizeof(*gen
) + natoa_r
->l
;
287 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "initiator send NAT-OAi:\n");
288 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "initiator send NAT-OAr:\n");
293 body
= vmalloc(tlen
);
296 "failed to get buffer to send.\n");
303 p
= set_isakmp_payload(p
, iph2
->sa
, ISAKMP_NPTYPE_NONCE
);
305 /* add NONCE payload */
307 np
= ISAKMP_NPTYPE_KE
;
308 else if (idci
|| idcr
)
309 np
= ISAKMP_NPTYPE_ID
;
311 np
= (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
312 p
= set_isakmp_payload(p
, iph2
->nonce
, np
);
314 /* add KE payload if need. */
315 np
= (idci
|| idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
317 p
= set_isakmp_payload(p
, iph2
->dhpub
, np
);
320 np
= (idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
322 p
= set_isakmp_payload(p
, iph2
->id
, np
);
326 p
= set_isakmp_payload(p
, iph2
->id_p
, natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
330 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
331 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
334 /* generate HASH(1) */
335 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, body
);
338 "failed to compute HASH");
342 /* send isakmp payload */
343 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
344 if (iph2
->sendbuf
== NULL
) {
346 "failed to get send buffer");
350 /* send the packet, add to the schedule to resend */
351 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
352 if (isakmp_ph2resend(iph2
) == -1) {
354 "failed to send packet");
358 /* change status of isakmp status entry */
359 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG1SENT
);
363 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
364 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
365 CONSTSTR("Initiator, Quick-Mode message 1"),
370 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
371 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
372 CONSTSTR("Initiator, Quick-Mode Message 1"),
373 CONSTSTR("Failed to transmit Quick-Mode Message 1"));
384 #endif /* ENABLE_NATT */
390 * receive from responder
391 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
394 quick_i2recv(iph2
, msg0
)
395 phase2_handle_t
*iph2
;
399 vchar_t
*hbuf
= NULL
; /* for hash computing. */
400 vchar_t
*pbuf
= NULL
; /* for payload parsing */
401 struct isakmp_parse_t
*pa
;
402 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
403 struct isakmp_pl_hash
*hash
= NULL
;
407 int error
= ISAKMP_INTERNAL_ERROR
;
408 struct sockaddr_storage
*natoa_i
= NULL
;
409 struct sockaddr_storage
*natoa_r
= NULL
;
412 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG1SENT
) {
414 "status mismatched %d.\n", iph2
->status
);
419 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
421 "Packet wasn't encrypted.\n");
424 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
427 "failed to decrypt");
431 /* create buffer for validating HASH(2) */
434 * 1. the first one must be HASH
435 * 2. the second one must be SA (added in isakmp-oakley-05!)
436 * 3. two IDs must be considered as IDci, then IDcr
438 pbuf
= isakmp_parse(msg
);
441 "failed to parse msg");
444 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
446 /* HASH payload is fixed postion */
447 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
449 "received invalid next payload type %d, "
451 pa
->type
, ISAKMP_NPTYPE_HASH
);
454 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
458 * this restriction was introduced in isakmp-oakley-05.
459 * we do not check this for backward compatibility.
460 * TODO: command line/config file option to enable/disable this code
462 /* HASH payload is fixed postion */
463 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
464 plog(ASL_LEVEL_WARNING
,
465 "received invalid next payload type %d, "
467 pa
->type
, ISAKMP_NPTYPE_HASH
);
470 /* allocate buffer for computing HASH(2) */
471 tlen
= iph2
->nonce
->l
472 + ntohl(isakmp
->len
) - sizeof(*isakmp
);
475 "invalid length (%lu,%d) while getting hash buffer.\n",
476 iph2
->nonce
->l
, ntohl(isakmp
->len
));
479 hbuf
= vmalloc(tlen
);
482 "failed to get hash buffer.\n");
485 p
= hbuf
->v
+ iph2
->nonce
->l
; /* retain the space for Ni_b */
488 * parse the payloads.
489 * copy non-HASH payloads into hbuf, so that we can validate HASH.
492 f_id
= 0; /* flag to use checking ID */
493 tlen
= 0; /* count payload length except of HASH payload. */
494 for (; pa
->type
; pa
++) {
496 /* copy to buffer for HASH */
497 /* Don't modify the payload */
498 memcpy(p
, pa
->ptr
, pa
->len
);
501 case ISAKMP_NPTYPE_SA
:
502 if (iph2
->sa_ret
!= NULL
) {
504 "Ignored, multiple SA "
505 "isn't supported.\n");
508 if (isakmp_p2ph(&iph2
->sa_ret
, pa
->ptr
) < 0) {
510 "failed to process SA payload");
515 case ISAKMP_NPTYPE_NONCE
:
516 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
518 "failed to process NONCE payload");
523 case ISAKMP_NPTYPE_KE
:
524 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
526 "failed to process KE payload");
531 case ISAKMP_NPTYPE_ID
:
535 if (iph2
->id
== NULL
|| iph2
->id_p
== NULL
) {
536 error
= ISAKMP_INTERNAL_ERROR
; // shouldn't happen
549 /* These ids may not match when natt is used with some devices.
550 * RFC 2407 says that the protocol and port fields should be ignored
551 * if they are zero, therefore they need to be checked individually.
553 struct ipsecdoi_id_b
*id_ptr
= ALIGNED_CAST(struct ipsecdoi_id_b
*)vp
->v
;
554 struct ipsecdoi_pl_id
*idp_ptr
= (struct ipsecdoi_pl_id
*)pa
->ptr
;
556 if (id_ptr
->type
!= idp_ptr
->b
.type
557 || (idp_ptr
->b
.proto_id
!= 0 && idp_ptr
->b
.proto_id
!= id_ptr
->proto_id
)
558 || (idp_ptr
->b
.port
!= 0 && idp_ptr
->b
.port
!= id_ptr
->port
)
559 || memcmp(vp
->v
+ sizeof(struct ipsecdoi_id_b
), (caddr_t
)pa
->ptr
+ sizeof(struct ipsecdoi_pl_id
),
560 vp
->l
- sizeof(struct ipsecdoi_id_b
))) {
561 // to support servers that use our external nat address as our ID
562 if (iph2
->ph1
->natt_flags
& NAT_DETECTED
) {
563 plog(ASL_LEVEL_WARNING
,
564 "mismatched ID was returned - ignored because nat traversal is being used.\n");
565 /* If I'm behind a nat and the ID is type address - save the address
566 * and port for when the peer rekeys.
568 if (f_id
== 0 && (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
)) {
569 if (lcconf
->ext_nat_id
)
570 vfree(lcconf
->ext_nat_id
);
571 if (idp_ptr
->h
.len
< sizeof(struct isakmp_gen
)) {
572 plog(ASL_LEVEL_ERR
, "invalid length (%d) while allocating external nat id.\n", idp_ptr
->h
.len
);
575 lcconf
->ext_nat_id
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
576 if (lcconf
->ext_nat_id
== NULL
) {
577 plog(ASL_LEVEL_ERR
, "memory error while allocating external nat id.\n");
580 memcpy(lcconf
->ext_nat_id
->v
, &(idp_ptr
->b
), lcconf
->ext_nat_id
->l
);
581 if (iph2
->ext_nat_id
)
582 vfree(iph2
->ext_nat_id
);
583 iph2
->ext_nat_id
= vdup(lcconf
->ext_nat_id
);
584 if (iph2
->ext_nat_id
== NULL
) {
585 plog(ASL_LEVEL_ERR
, "memory error while allocating ph2's external nat id.\n");
588 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id
->v
, iph2
->ext_nat_id
->l
, "external nat address saved.\n");
589 } else if (f_id
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
)) {
590 if (iph2
->ext_nat_id_p
)
591 vfree(iph2
->ext_nat_id_p
);
592 iph2
->ext_nat_id_p
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
593 if (iph2
->ext_nat_id_p
== NULL
) {
594 plog(ASL_LEVEL_ERR
, "memory error while allocating peers ph2's external nat id.\n");
597 memcpy(iph2
->ext_nat_id_p
->v
, &(idp_ptr
->b
), iph2
->ext_nat_id_p
->l
);
598 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id_p
->v
, iph2
->ext_nat_id_p
->l
, "peer's external nat address saved.\n");
601 plog(ASL_LEVEL_ERR
, "mismatched ID was returned.\n");
602 error
= ISAKMP_NTYPE_ATTRIBUTES_NOT_SUPPORTED
;
611 case ISAKMP_NPTYPE_N
:
612 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
616 case ISAKMP_NPTYPE_NATOA_DRAFT
:
617 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
618 case ISAKMP_NPTYPE_NATOA_RFC
:
621 struct sockaddr_storage
*daddr
;
623 isakmp_p2ph(&vp
, pa
->ptr
);
626 daddr
= process_natoa_payload(vp
);
628 if (natoa_i
== NULL
) {
630 plog(ASL_LEVEL_DEBUG
, "initiaor rcvd NAT-OA i: %s\n",
631 saddr2str((struct sockaddr
*)natoa_i
));
632 } else if (natoa_r
== NULL
) {
634 plog(ASL_LEVEL_DEBUG
, "initiator rcvd NAT-OA r: %s\n",
635 saddr2str((struct sockaddr
*)natoa_r
));
648 /* don't send information, see ident_r1recv() */
650 "ignore the packet, "
651 "received unexpecting payload type %d.\n",
658 /* compute true length of payload. */
662 /* payload existency check */
663 if (hash
== NULL
|| iph2
->sa_ret
== NULL
|| iph2
->nonce_p
== NULL
) {
665 "few isakmp message received.\n");
669 /* Fixed buffer for calculating HASH */
670 memcpy(hbuf
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
671 plog(ASL_LEVEL_DEBUG
,
672 "HASH allocated:hbuf->l=%zu actual:tlen=%zu\n",
673 hbuf
->l
, tlen
+ iph2
->nonce
->l
);
674 /* adjust buffer length for HASH */
675 hbuf
->l
= iph2
->nonce
->l
+ tlen
;
677 /* validate HASH(2) */
680 vchar_t
*my_hash
= NULL
;
683 r_hash
= (char *)hash
+ sizeof(*hash
);
685 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(2) received:");
687 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
688 if (my_hash
== NULL
) {
690 "failed to compute HASH");
694 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
698 plog(ASL_LEVEL_DEBUG
,
699 "HASH(2) mismatch.\n");
700 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
705 /* validity check SA payload sent from responder */
706 if (ipsecdoi_checkph2proposal(iph2
) < 0) {
708 "failed to validate SA proposal");
709 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
713 /* change status of isakmp status entry */
714 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG2RCVD
);
718 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
719 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
720 CONSTSTR("Initiator, Quick-Mode message 2"),
725 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
726 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
727 CONSTSTR("Initiator, Quick-Mode Message 2"),
728 CONSTSTR("Failed to process Quick-Mode Message 2 "));
739 racoon_free(natoa_i
);
742 racoon_free(natoa_r
);
746 VPTRINIT(iph2
->sa_ret
);
747 VPTRINIT(iph2
->nonce_p
);
748 VPTRINIT(iph2
->dhpub_p
);
759 quick_i3send(iph2
, msg0
)
760 phase2_handle_t
*iph2
;
765 vchar_t
*hash
= NULL
;
768 int error
= ISAKMP_INTERNAL_ERROR
;
769 int packet_error
= -1;
772 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG2RCVD
) {
774 "status mismatched %d.\n", iph2
->status
);
778 /* generate HASH(3) */
782 plog(ASL_LEVEL_DEBUG
, "HASH(3) generate\n");
784 tmp
= vmalloc(iph2
->nonce
->l
+ iph2
->nonce_p
->l
);
787 "failed to get hash buffer.\n");
790 memcpy(tmp
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
791 memcpy(tmp
->v
+ iph2
->nonce
->l
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
793 hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
798 "failed to compute HASH");
803 /* create buffer for isakmp payload */
804 tlen
= sizeof(struct isakmp
)
805 + sizeof(struct isakmp_gen
) + hash
->l
;
809 "failed to get buffer to send.\n");
813 /* create isakmp header */
814 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
817 "failed to create ISAKMP header");
821 /* add HASH(3) payload */
822 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_NONE
);
824 #ifdef HAVE_PRINT_ISAKMP_C
825 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
829 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
830 if (iph2
->sendbuf
== NULL
) {
832 "failed to encrypt packet");
836 /* if there is commit bit, need resending */
837 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
838 /* send the packet, add to the schedule to resend */
839 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
840 if (isakmp_ph2resend(iph2
) == -1) {
842 "failed to send packet, commit-bit");
846 /* send the packet */
847 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
849 "failed to send packet");
854 /* the sending message is added to the received-list. */
855 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
,
857 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
859 "failed to add a response packet to the tree.\n");
863 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
864 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
865 CONSTSTR("Initiator, Quick-Mode message 3"),
869 /* compute both of KEYMATs */
870 if (oakley_compute_keymat(iph2
, INITIATOR
) < 0) {
872 "failed to compute KEYMAT");
876 /* if there is commit bit don't set up SA now. */
877 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
878 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG3SENT
);
883 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
885 /* Do UPDATE for initiator */
886 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
887 if (pk_sendupdate(iph2
) < 0) {
888 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
891 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
893 /* Do ADD for responder */
894 if (pk_sendadd(iph2
) < 0) {
895 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
898 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
904 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
905 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
906 CONSTSTR("Initiator, Quick-Mode Message 3"),
907 CONSTSTR("Failed to transmit Quick-Mode Message 3"));
920 * receive from responder
921 * HDR#*, HASH(4), notify
924 quick_i4recv(iph2
, msg0
)
925 phase2_handle_t
*iph2
;
929 vchar_t
*pbuf
= NULL
; /* for payload parsing */
930 struct isakmp_parse_t
*pa
;
931 struct isakmp_pl_hash
*hash
= NULL
;
932 vchar_t
*notify
= NULL
;
933 int error
= ISAKMP_INTERNAL_ERROR
;
934 int packet_error
= -1;
937 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG3SENT
) {
939 "status mismatched %d.\n", iph2
->status
);
944 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
946 "Packet wasn't encrypted.\n");
949 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
952 "failed to decrypt packet\n");
956 /* validate the type of next payload */
957 pbuf
= isakmp_parse(msg
);
960 "failed to parse msg\n");
964 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
965 pa
->type
!= ISAKMP_NPTYPE_NONE
;
969 case ISAKMP_NPTYPE_HASH
:
970 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
972 case ISAKMP_NPTYPE_N
:
973 if (notify
!= NULL
) {
974 plog(ASL_LEVEL_WARNING
,
975 "Ignoring multiple notifications\n");
978 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
979 notify
= vmalloc(pa
->len
);
980 if (notify
== NULL
) {
982 "failed to get notify buffer.\n");
985 memcpy(notify
->v
, pa
->ptr
, notify
->l
);
988 /* don't send information, see ident_r1recv() */
990 "ignore the packet, "
991 "received unexpecting payload type %d.\n",
997 /* payload existency check */
1000 "few isakmp message received.\n");
1004 /* validate HASH(4) */
1007 vchar_t
*my_hash
= NULL
;
1008 vchar_t
*tmp
= NULL
;
1011 r_hash
= (char *)hash
+ sizeof(*hash
);
1013 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(4) validate:");
1015 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
1017 if (my_hash
== NULL
) {
1019 "failed to compute HASH\n");
1023 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1027 plog(ASL_LEVEL_DEBUG
,
1028 "HASH(4) mismatch.\n");
1029 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1034 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1035 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1036 CONSTSTR("Initiator, Quick-Mode message 4"),
1040 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
1042 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
1044 /* don't anything if local test mode. */
1050 /* Do UPDATE for initiator */
1051 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
1052 if (pk_sendupdate(iph2
) < 0) {
1053 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
1056 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
1058 /* Do ADD for responder */
1059 if (pk_sendadd(iph2
) < 0) {
1060 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
1063 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
1069 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1070 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1071 CONSTSTR("Initiator, Quick-Mode Message 4"),
1072 CONSTSTR("Failed to process Quick-Mode Message 4"));
1085 * receive from initiator
1086 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1089 quick_r1recv(iph2
, msg0
)
1090 phase2_handle_t
*iph2
;
1093 vchar_t
*msg
= NULL
;
1094 vchar_t
*hbuf
= NULL
; /* for hash computing. */
1095 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1096 struct isakmp_parse_t
*pa
;
1097 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
1098 struct isakmp_pl_hash
*hash
= NULL
;
1101 int f_id_order
; /* for ID payload detection */
1102 int error
= ISAKMP_INTERNAL_ERROR
;
1103 struct sockaddr_storage
*natoa_i
= NULL
;
1104 struct sockaddr_storage
*natoa_r
= NULL
;
1106 /* validity check */
1107 if (iph2
->status
!= IKEV1_STATE_QUICK_R_START
) {
1109 "status mismatched %d.\n", iph2
->status
);
1114 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1116 "Packet wasn't encrypted.\n");
1117 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1120 /* decrypt packet */
1121 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1124 "failed to decrypt packet\n");
1128 /* create buffer for using to validate HASH(1) */
1131 * 1. the first one must be HASH
1132 * 2. the second one must be SA (added in isakmp-oakley-05!)
1133 * 3. two IDs must be considered as IDci, then IDcr
1135 pbuf
= isakmp_parse(msg
);
1138 "failed to parse msg\n");
1141 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1143 /* HASH payload is fixed postion */
1144 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
1146 "received invalid next payload type %d, "
1148 pa
->type
, ISAKMP_NPTYPE_HASH
);
1149 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1152 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1156 * this restriction was introduced in isakmp-oakley-05.
1157 * we do not check this for backward compatibility.
1158 * TODO: command line/config file option to enable/disable this code
1160 /* HASH payload is fixed postion */
1161 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1162 plog(ASL_LEVEL_WARNING
,
1163 "received invalid next payload type %d, "
1165 pa
->type
, ISAKMP_NPTYPE_SA
);
1166 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1169 /* allocate buffer for computing HASH(1) */
1170 tlen
= ntohl(isakmp
->len
) - sizeof(*isakmp
);
1172 plog(ASL_LEVEL_ERR
, "invalid length (%d) while extracting hash.\n",
1173 ntohl(isakmp
->len
));
1176 hbuf
= vmalloc(tlen
);
1179 "failed to get hash buffer.\n");
1185 * parse the payloads.
1186 * copy non-HASH payloads into hbuf, so that we can validate HASH.
1188 iph2
->sa
= NULL
; /* we don't support multi SAs. */
1189 iph2
->nonce_p
= NULL
;
1190 iph2
->dhpub_p
= NULL
;
1193 tlen
= 0; /* count payload length except of HASH payload. */
1196 * IDi2 MUST be immediatelly followed by IDr2. We allowed the
1197 * illegal case, but logged. First ID payload is to be IDi2.
1198 * And next ID payload is to be IDr2.
1202 for (; pa
->type
; pa
++) {
1204 /* copy to buffer for HASH */
1205 /* Don't modify the payload */
1206 memcpy(p
, pa
->ptr
, pa
->len
);
1208 if (pa
->type
!= ISAKMP_NPTYPE_ID
)
1212 case ISAKMP_NPTYPE_SA
:
1213 if (iph2
->sa
!= NULL
) {
1215 "Multi SAs isn't supported.\n");
1218 if (isakmp_p2ph(&iph2
->sa
, pa
->ptr
) < 0) {
1220 "failed to process SA payload\n");
1225 case ISAKMP_NPTYPE_NONCE
:
1226 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
1228 "failed to process NONCE payload\n");
1233 case ISAKMP_NPTYPE_KE
:
1234 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
1236 "failed to process KE payload\n");
1241 case ISAKMP_NPTYPE_ID
:
1242 if (iph2
->id_p
== NULL
) {
1246 if (isakmp_p2ph(&iph2
->id_p
, pa
->ptr
) < 0) {
1248 "failed to process IDci2 payload\n");
1252 } else if (iph2
->id
== NULL
) {
1254 if (f_id_order
== 0) {
1256 "IDr2 payload is not "
1257 "immediatelly followed "
1258 "by IDi2. We allowed.\n");
1259 /* XXX we allowed in this case. */
1262 if (isakmp_p2ph(&iph2
->id
, pa
->ptr
) < 0) {
1264 "failed to process IDcr2 payload\n");
1268 plogdump(ASL_LEVEL_ERR
, iph2
->id
->v
, iph2
->id
->l
, "received too many ID payloads");
1269 error
= ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1274 case ISAKMP_NPTYPE_N
:
1275 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1279 case ISAKMP_NPTYPE_NATOA_DRAFT
:
1280 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
1281 case ISAKMP_NPTYPE_NATOA_RFC
:
1284 struct sockaddr_storage
*daddr
;
1286 isakmp_p2ph(&vp
, pa
->ptr
);
1289 daddr
= process_natoa_payload(vp
);
1291 if (natoa_i
== NULL
) {
1293 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA i: %s\n",
1294 saddr2str((struct sockaddr
*)natoa_i
));
1295 } else if (natoa_r
== NULL
) {
1297 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA r: %s\n",
1298 saddr2str((struct sockaddr
*)natoa_r
));
1312 "ignore the packet, "
1313 "received unexpected payload type %d.\n",
1315 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1321 /* compute true length of payload. */
1325 /* payload existency check */
1326 if (hash
== NULL
|| iph2
->sa
== NULL
|| iph2
->nonce_p
== NULL
) {
1328 "expected isakmp payloads missing.\n");
1329 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1334 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "received IDci2:");
1337 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "received IDcr2:");
1340 /* adjust buffer length for HASH */
1343 /* validate HASH(1) */
1346 vchar_t
*my_hash
= NULL
;
1349 r_hash
= (caddr_t
)hash
+ sizeof(*hash
);
1351 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(1) validate:");
1353 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
1354 if (my_hash
== NULL
) {
1356 "failed to compute HASH\n");
1360 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1365 "HASH(1) mismatch.\n");
1366 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1372 error
= get_sainfo_r(iph2
);
1375 "failed to get sainfo.\n");
1379 /* check the existence of ID payload and create responder's proposal */
1380 error
= get_proposal_r(iph2
);
1383 /* generate a policy template from peer's proposal */
1384 if (set_proposal_from_proposal(iph2
)) {
1386 "failed to generate a proposal template "
1387 "from client's proposal.\n");
1388 return ISAKMP_INTERNAL_ERROR
;
1392 /* select single proposal or reject it. */
1393 if (ipsecdoi_selectph2proposal(iph2
) < 0) {
1395 "failed to select proposal.\n");
1396 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1402 "failed to get proposal for responder.\n");
1406 /* check KE and attribute of PFS */
1407 if (iph2
->dhpub_p
!= NULL
&& iph2
->approval
->pfs_group
== 0) {
1409 "no PFS is specified, but peer sends KE.\n");
1410 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1413 if (iph2
->dhpub_p
== NULL
&& iph2
->approval
->pfs_group
!= 0) {
1415 "PFS is specified, but peer doesn't sends KE.\n");
1416 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1420 ike_session_update_mode(iph2
); /* update the mode, now that we have a proposal */
1423 * save the packet from the initiator in order to resend the
1424 * responder's first packet against this packet.
1426 iph2
->msg1
= vdup(msg0
);
1428 /* change status of isakmp status entry */
1429 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG1RCVD
);
1433 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1434 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1435 CONSTSTR("Responder, Quick-Mode message 1"),
1440 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1441 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1442 CONSTSTR("Responder, Quick-Mode Message 1"),
1443 CONSTSTR("Failed to process Quick-Mode Message 1"));
1454 racoon_free(natoa_i
);
1457 racoon_free(natoa_r
);
1463 VPTRINIT(iph2
->nonce_p
);
1464 VPTRINIT(iph2
->dhpub_p
);
1466 VPTRINIT(iph2
->id_p
);
1473 * call pfkey_getspi.
1476 quick_rprep(iph2
, msg
)
1477 phase2_handle_t
*iph2
;
1480 int error
= ISAKMP_INTERNAL_ERROR
;
1482 /* validity check */
1483 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG1RCVD
) {
1485 "status mismatched %d.\n", iph2
->status
);
1489 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_GETSPISENT
);
1491 /* send getspi message */
1492 if (pk_sendgetspi(iph2
) < 0) {
1494 "failed to send getspi");
1498 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
1500 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
1501 pfkey_timeover_stub
, iph2
);
1511 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1514 quick_r2send(iph2
, msg
)
1515 phase2_handle_t
*iph2
;
1518 vchar_t
*body
= NULL
;
1519 vchar_t
*hash
= NULL
;
1520 vchar_t
*natoa_i
= NULL
;
1521 vchar_t
*natoa_r
= NULL
;
1523 struct isakmp_gen
*gen
;
1526 int error
= ISAKMP_INTERNAL_ERROR
;
1528 u_int8_t
*np_p
= NULL
;
1530 /* validity check */
1533 "msg has to be NULL in this function.\n");
1536 if (iph2
->status
!= IKEV1_STATE_QUICK_R_GETSPIDONE
) {
1538 "status mismatched %d.\n", iph2
->status
);
1542 /* update responders SPI */
1543 if (ipsecdoi_updatespi(iph2
) < 0) {
1544 plog(ASL_LEVEL_ERR
, "failed to update spi.\n");
1548 /* generate NONCE value */
1549 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
1550 if (iph2
->nonce
== NULL
) {
1552 "failed to generate NONCE");
1556 /* generate KE value if need */
1557 pfsgroup
= iph2
->approval
->pfs_group
;
1558 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1559 /* DH group settting if PFS is required. */
1560 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
1562 "failed to set DH value.\n");
1565 /* generate DH public value */
1567 if (oakley_dh_generate(iph2
->pfsgrp
,
1568 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
1570 if (oakley_dh_generate(iph2
->pfsgrp
,
1571 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
1574 "failed to generate DH public");
1579 /* create SA;NONCE payload, and KE and ID if need */
1580 tlen
= sizeof(*gen
) + iph2
->sa_ret
->l
1581 + sizeof(*gen
) + iph2
->nonce
->l
;
1582 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1583 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
1584 if (iph2
->id_p
!= NULL
)
1585 tlen
+= (sizeof(*gen
) + iph2
->id_p
->l
1586 + sizeof(*gen
) + iph2
->id
->l
);
1590 * RFC3947 5.2. if we chose UDP-Encapsulated-Transport
1591 * we should send NAT-OA
1593 if (ipsecdoi_any_transportmode(iph2
->approval
)
1594 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
1595 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
1596 if (natoa_type
== -1) {
1598 "failed to create NATOA payloads");
1601 else if (natoa_type
!= 0) {
1602 tlen
+= sizeof(*gen
) + natoa_i
->l
;
1603 tlen
+= sizeof(*gen
) + natoa_r
->l
;
1605 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "responder send NAT-OAi:");
1606 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "responder send NAT-OAr:");
1611 plog(ASL_LEVEL_DEBUG
, "Approved SA\n");
1612 printsaprop0(ASL_LEVEL_DEBUG
, iph2
->approval
);
1614 body
= vmalloc(tlen
);
1617 "failed to get buffer to send.\n");
1622 /* make SA payload */
1623 p
= set_isakmp_payload(body
->v
, iph2
->sa_ret
, ISAKMP_NPTYPE_NONCE
);
1625 /* add NONCE payload */
1626 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1627 p
= set_isakmp_payload(p
, iph2
->nonce
,
1628 (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1630 : (iph2
->id_p
!= NULL
1632 : (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
)));
1634 /* add KE payload if need. */
1635 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1636 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1637 p
= set_isakmp_payload(p
, iph2
->dhpub
,
1638 (iph2
->id_p
== NULL
) ? (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
) : ISAKMP_NPTYPE_ID
);
1641 /* add ID payloads received. */
1642 if (iph2
->id_p
!= NULL
) {
1644 p
= set_isakmp_payload(p
, iph2
->id_p
, ISAKMP_NPTYPE_ID
);
1645 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "sending IDci2:");
1647 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1648 p
= set_isakmp_payload(p
, iph2
->id
, (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
));
1649 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "sending IDcr2:");
1652 /* add a RESPONDER-LIFETIME notify payload if needed */
1654 vchar_t
*data
= NULL
;
1655 struct saprop
*pp
= iph2
->approval
;
1658 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_SEC
) {
1659 u_int32_t v
= htonl((u_int32_t
)pp
->lifetime
);
1660 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1661 IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
1664 "failed to add RESPONDER-LIFETIME notify (type) payload");
1667 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1668 (caddr_t
)&v
, sizeof(v
));
1671 "failed to add RESPONDER-LIFETIME notify (value) payload");
1675 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_KB
) {
1676 u_int32_t v
= htonl((u_int32_t
)pp
->lifebyte
);
1677 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1678 IPSECDOI_ATTR_SA_LD_TYPE_KB
);
1681 "failed to add RESPONDER-LIFETIME notify (type) payload");
1684 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1685 (caddr_t
)&v
, sizeof(v
));
1688 "failed to add RESPONDER-LIFETIME notify (value) payload");
1694 * XXX Is there only single RESPONDER-LIFETIME payload in a IKE message
1695 * in the case of SA bundle ?
1698 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1699 body
= isakmp_add_pl_n(body
, &np_p
,
1700 ISAKMP_NTYPE_RESPONDER_LIFETIME
, pr
, data
);
1703 "invalid RESPONDER-LIFETIME payload");
1705 return error
; /* XXX */
1714 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
1715 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
1718 /* generate HASH(2) */
1722 tmp
= vmalloc(iph2
->nonce_p
->l
+ body
->l
);
1725 "failed to get hash buffer.\n");
1728 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1729 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, body
->v
, body
->l
);
1731 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, tmp
);
1736 "failed to compute HASH");
1741 /* send isakmp payload */
1742 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
1743 if (iph2
->sendbuf
== NULL
) {
1745 "failed to get send buffer");
1749 /* send the packet, add to the schedule to resend */
1750 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
1751 if (isakmp_ph2resend(iph2
) == -1) {
1753 "failed to send packet");
1757 /* the sending message is added to the received-list. */
1758 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, iph2
->msg1
,
1759 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
1761 "failed to add a response packet to the tree.\n");
1765 /* change status of isakmp status entry */
1766 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG2SENT
);
1770 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1771 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1772 CONSTSTR("Responder, Quick-Mode message 2"),
1777 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1778 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1779 CONSTSTR("Responder, Quick-Mode Message 2"),
1780 CONSTSTR("Failed to transmit Quick-Mode Message 2"));
1795 * receive from initiator
1799 quick_r3recv(iph2
, msg0
)
1800 phase2_handle_t
*iph2
;
1803 vchar_t
*msg
= NULL
;
1804 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1805 struct isakmp_parse_t
*pa
;
1806 struct isakmp_pl_hash
*hash
= NULL
;
1807 int error
= ISAKMP_INTERNAL_ERROR
;
1809 /* validity check */
1810 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG2SENT
) {
1812 "status mismatched %d.\n", iph2
->status
);
1816 /* decrypt packet */
1817 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1819 "Packet wasn't encrypted.\n");
1822 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1825 "failed to decrypt packet\n");
1829 /* validate the type of next payload */
1830 pbuf
= isakmp_parse(msg
);
1833 "failed to parse msg\n");
1837 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1838 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1842 case ISAKMP_NPTYPE_HASH
:
1843 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1845 case ISAKMP_NPTYPE_N
:
1846 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1849 /* don't send information, see ident_r1recv() */
1851 "ignore the packet, "
1852 "received unexpecting payload type %d.\n",
1858 /* payload existency check */
1861 "few isakmp message received.\n");
1865 /* validate HASH(3) */
1866 /* HASH(3) = prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b) */
1869 vchar_t
*my_hash
= NULL
;
1870 vchar_t
*tmp
= NULL
;
1873 r_hash
= (char *)hash
+ sizeof(*hash
);
1875 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(3) validate:");
1877 tmp
= vmalloc(iph2
->nonce_p
->l
+ iph2
->nonce
->l
);
1880 "failed to get hash buffer.\n");
1883 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1884 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, iph2
->nonce
->v
, iph2
->nonce
->l
);
1886 my_hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
1888 if (my_hash
== NULL
) {
1890 "failed to compute HASH\n");
1894 result
= memcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1899 "HASH(3) mismatch.\n");
1900 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1905 /* if there is commit bit, don't set up SA now. */
1906 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
1907 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG3RCVD
);
1909 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
1913 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1914 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1915 CONSTSTR("Responder, Quick-Mode message 3"),
1920 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1921 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1922 CONSTSTR("Responder, Quick-Mode Message 3"),
1923 CONSTSTR("Failed to process Quick-Mode Message 3"));
1935 * HDR#*, HASH(4), notify
1938 quick_r4send(iph2
, msg0
)
1939 phase2_handle_t
*iph2
;
1942 vchar_t
*buf
= NULL
;
1943 vchar_t
*myhash
= NULL
;
1944 struct isakmp_pl_n
*n
;
1945 vchar_t
*notify
= NULL
;
1948 int error
= ISAKMP_INTERNAL_ERROR
;
1950 /* validity check */
1951 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG3RCVD
) {
1953 "status mismatched %d.\n", iph2
->status
);
1957 /* generate HASH(4) */
1958 /* XXX What can I do in the case of multiple different SA */
1959 plog(ASL_LEVEL_DEBUG
, "HASH(4) generate\n");
1961 /* XXX What should I do if there are multiple SAs ? */
1962 tlen
= sizeof(struct isakmp_pl_n
) + iph2
->approval
->head
->spisize
;
1963 notify
= vmalloc(tlen
);
1964 if (notify
== NULL
) {
1966 "failed to get notify buffer.\n");
1969 n
= (struct isakmp_pl_n
*)notify
->v
;
1970 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1971 n
->h
.len
= htons(tlen
);
1972 n
->doi
= htonl(IPSEC_DOI
);
1973 n
->proto_id
= iph2
->approval
->head
->proto_id
;
1974 n
->spi_size
= sizeof(iph2
->approval
->head
->spisize
);
1975 n
->type
= htons(ISAKMP_NTYPE_CONNECTED
);
1976 memcpy(n
+ 1, &iph2
->approval
->head
->spi
, iph2
->approval
->head
->spisize
);
1978 myhash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
1979 if (myhash
== NULL
) {
1981 "failed to compute HASH");
1985 /* create buffer for isakmp payload */
1986 tlen
= sizeof(struct isakmp
)
1987 + sizeof(struct isakmp_gen
) + myhash
->l
1989 buf
= vmalloc(tlen
);
1992 "failed to get buffer to send.\n");
1996 /* create isakmp header */
1997 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
2000 "failed to set ISAKMP header");
2004 /* add HASH(4) payload */
2005 p
= set_isakmp_payload(p
, myhash
, ISAKMP_NPTYPE_N
);
2007 /* add notify payload */
2008 memcpy(p
, notify
->v
, notify
->l
);
2010 #ifdef HAVE_PRINT_ISAKMP_C
2011 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
2015 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
2016 if (iph2
->sendbuf
== NULL
) {
2018 "failed to encrypt packet");
2022 /* send the packet */
2023 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
2025 "failed to send packet");
2029 /* the sending message is added to the received-list. */
2030 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, msg0
,
2031 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
2032 plog(ASL_LEVEL_ERR
,
2033 "failed to add a response packet to the tree.\n");
2037 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
2041 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
2042 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
2043 CONSTSTR("Responder, Quick-Mode message 4"),
2048 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
2049 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
2050 CONSTSTR("Responder, Quick-Mode Message 4"),
2051 CONSTSTR("Failed to transmit Quick-Mode Message 4"));
2068 quick_rfinalize(iph2
, msg0
)
2069 phase2_handle_t
*iph2
;
2072 vchar_t
*msg
= NULL
;
2073 int error
= ISAKMP_INTERNAL_ERROR
;
2075 /* validity check */
2076 if (iph2
->status
!= IKEV1_STATE_QUICK_R_COMMIT
) {
2078 "status mismatched %d.\n", iph2
->status
);
2082 /* compute both of KEYMATs */
2083 if (oakley_compute_keymat(iph2
, RESPONDER
) < 0) {
2085 "failed to compute KEYMAT");
2089 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_ADDSA
);
2091 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
2093 /* don't anything if local test mode. */
2099 /* Do UPDATE as responder */
2100 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
2101 if (pk_sendupdate(iph2
) < 0) {
2102 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
2105 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
2107 /* Do ADD for responder */
2108 if (pk_sendadd(iph2
) < 0) {
2109 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
2112 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
2115 * set policies into SPD if the policy is generated
2116 * from peer's policy.
2118 if (iph2
->spidx_gen
) {
2120 struct policyindex
*spidx
;
2121 struct sockaddr_storage addr
;
2123 struct sockaddr_storage
*src
= iph2
->src
;
2124 struct sockaddr_storage
*dst
= iph2
->dst
;
2126 /* make inbound policy */
2129 if (pk_sendspdupdate2(iph2
) < 0) {
2131 "pfkey spdupdate2(inbound) failed.\n");
2134 plog(ASL_LEVEL_DEBUG
,
2135 "pfkey spdupdate2(inbound) sent.\n");
2137 spidx
= iph2
->spidx_gen
;
2138 #ifdef HAVE_POLICY_FWD
2139 /* make forward policy if required */
2140 if (tunnel_mode_prop(iph2
->approval
)) {
2141 spidx
->dir
= IPSEC_DIR_FWD
;
2142 if (pk_sendspdupdate2(iph2
) < 0) {
2144 "pfkey spdupdate2(forward) failed.\n");
2147 plog(ASL_LEVEL_DEBUG
,
2148 "pfkey spdupdate2(forward) sent.\n");
2152 /* make outbound policy */
2155 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
2157 spidx
->src
= spidx
->dst
;
2159 pref
= spidx
->prefs
;
2160 spidx
->prefs
= spidx
->prefd
;
2161 spidx
->prefd
= pref
;
2163 if (pk_sendspdupdate2(iph2
) < 0) {
2165 "pfkey spdupdate2(outbound) failed.\n");
2168 plog(ASL_LEVEL_DEBUG
,
2169 "pfkey spdupdate2(outbound) sent.\n");
2171 /* spidx_gen is unnecessary any more */
2172 delsp_bothdir(iph2
->spidx_gen
);
2173 racoon_free(iph2
->spidx_gen
);
2174 iph2
->spidx_gen
= NULL
;
2175 iph2
->generated_spidx
=1;
2188 * create HASH, body (SA, NONCE) payload with isakmp header.
2191 quick_ir1mx(iph2
, body
, hash
)
2192 phase2_handle_t
*iph2
;
2193 vchar_t
*body
, *hash
;
2195 struct isakmp
*isakmp
;
2196 vchar_t
*buf
= NULL
, *new = NULL
;
2199 struct isakmp_gen
*gen
;
2200 int error
= ISAKMP_INTERNAL_ERROR
;
2202 /* create buffer for isakmp payload */
2203 tlen
= sizeof(*isakmp
)
2204 + sizeof(*gen
) + hash
->l
2206 buf
= vmalloc(tlen
);
2209 "failed to get buffer to send.\n");
2213 /* re-set encryption flag, for serurity. */
2214 iph2
->flags
|= ISAKMP_FLAG_E
;
2216 /* set isakmp header */
2217 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
2220 "failed to set ISAKMP header");
2224 /* add HASH payload */
2225 /* XXX is next type always SA ? */
2226 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_SA
);
2228 /* add body payload */
2229 memcpy(p
, body
->v
, body
->l
);
2231 #ifdef HAVE_PRINT_ISAKMP_C
2232 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
2236 new = oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
2239 "failed to encrypt packet");
2250 if (error
&& buf
!= NULL
) {
2259 * get remote's sainfo.
2260 * NOTE: this function is for responder.
2264 phase2_handle_t
*iph2
;
2266 vchar_t
*idsrc
= NULL
, *iddst
= NULL
;
2268 int error
= ISAKMP_INTERNAL_ERROR
;
2269 struct sainfo
*anonymous
= NULL
;
2271 if (iph2
->id
== NULL
) {
2272 switch (iph2
->src
->ss_family
) {
2274 prefixlen
= sizeof(struct in_addr
) << 3;
2277 prefixlen
= sizeof(struct in6_addr
) << 3;
2281 "invalid family: %d\n", iph2
->src
->ss_family
);
2284 idsrc
= ipsecdoi_sockaddr2id(iph2
->src
, prefixlen
,
2287 idsrc
= vdup(iph2
->id
);
2289 if (idsrc
== NULL
) {
2291 "failed to set ID for source.\n");
2295 if (iph2
->id_p
== NULL
) {
2296 switch (iph2
->dst
->ss_family
) {
2298 prefixlen
= sizeof(struct in_addr
) << 3;
2301 prefixlen
= sizeof(struct in6_addr
) << 3;
2305 "invalid family: %d\n", iph2
->dst
->ss_family
);
2308 iddst
= ipsecdoi_sockaddr2id(iph2
->dst
, prefixlen
,
2311 iddst
= vdup(iph2
->id_p
);
2313 if (iddst
== NULL
) {
2315 "failed to set ID for destination.\n");
2319 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 0);
2320 // track anonymous sainfo, because we'll try to find a better sainfo if this is a client
2321 if (iph2
->sainfo
&& iph2
->sainfo
->idsrc
== NULL
)
2322 anonymous
= iph2
->sainfo
;
2324 if (iph2
->sainfo
== NULL
||
2325 (anonymous
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2326 if ((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
)
2327 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 1);
2329 plog(ASL_LEVEL_DEBUG
,
2330 "get_sainfo_r case 1.\n");
2332 // still no sainfo (or anonymous): for client, fallback to sainfo used by a previous established phase2
2333 if (iph2
->sainfo
== NULL
||
2334 (iph2
->sainfo
->idsrc
== NULL
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2335 ike_session_get_sainfo_r(iph2
);
2337 plog(ASL_LEVEL_DEBUG
,
2338 "get_sainfo_r case 2.\n");
2340 // still no sainfo (or anonymous): fallback to sainfo picked by dst id
2341 if ((iph2
->sainfo
== NULL
|| iph2
->sainfo
->idsrc
== NULL
) && iph2
->id_p
) {
2342 plog(ASL_LEVEL_DEBUG
,
2343 "get_sainfo_r about to try dst id only.\n");
2344 iph2
->sainfo
= getsainfo_by_dst_id(iph2
->id_p
, iph2
->ph1
->id_p
);
2346 plog(ASL_LEVEL_DEBUG
,
2347 "get_sainfo_r case 3.\n");
2348 if (iph2
->sainfo
->idsrc
== NULL
)
2349 anonymous
= iph2
->sainfo
;
2354 if (iph2
->sainfo
== NULL
) {
2355 if (anonymous
== NULL
) {
2357 "failed to get sainfo.\n");
2360 iph2
->sainfo
= anonymous
;
2362 retain_sainfo(iph2
->sainfo
);
2364 #ifdef ENABLE_HYBRID
2365 /* xauth group inclusion check */
2366 if (iph2
->sainfo
->group
!= NULL
)
2367 if(group_check(iph2
->ph1
,&iph2
->sainfo
->group
->v
,1)) {
2369 "failed to group check");
2374 plog(ASL_LEVEL_DEBUG
,
2375 "selected sainfo: %s\n", sainfo2str(iph2
->sainfo
));
2388 get_proposal_r(iph2
)
2389 phase2_handle_t
*iph2
;
2391 int error
= get_proposal_r_remote(iph2
, 0);
2392 if (error
!= -2 && error
!= 0 &&
2393 (((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
) ||
2394 (iph2
->parent_session
&& iph2
->parent_session
->is_client
))) {
2395 if (iph2
->parent_session
&& iph2
->parent_session
->is_client
)
2396 error
= ike_session_get_proposal_r(iph2
);
2397 if (error
!= -2 && error
!= 0)
2398 error
= get_proposal_r_remote(iph2
, 1);
2404 * Copy both IP addresses in ID payloads into [src,dst]_id if both ID types
2405 * are IP address and same address family.
2406 * Then get remote's policy from SPD copied from kernel.
2407 * If the type of ID payload is address or subnet type, then the index is
2408 * made from the payload. If there is no ID payload, or the type of ID
2409 * payload is NOT address type, then the index is made from the address
2411 * NOTE: This function is only for responder.
2414 get_proposal_r_remote(iph2
, ignore_id
)
2415 phase2_handle_t
*iph2
;
2418 struct policyindex spidx
;
2419 struct secpolicy
*sp_in
, *sp_out
;
2420 int idi2type
= 0; /* switch whether copy IDs into id[src,dst]. */
2421 int error
= ISAKMP_INTERNAL_ERROR
;
2422 int generated_policy_exit_early
= 0;
2424 /* check the existence of ID payload */
2425 if ((iph2
->id_p
!= NULL
&& iph2
->id
== NULL
)
2426 || (iph2
->id_p
== NULL
&& iph2
->id
!= NULL
)) {
2428 "Both IDs wasn't found in payload.\n");
2429 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2432 /* make sure if id[src,dst] is null (if use_remote_addr == 0). */
2433 if (!ignore_id
&& (iph2
->src_id
|| iph2
->dst_id
)) {
2435 "Why do ID[src,dst] exist already.\n");
2436 return ISAKMP_INTERNAL_ERROR
;
2439 plog(ASL_LEVEL_DEBUG
,
2440 "%s: ignore_id %x.\n", __FUNCTION__
, ignore_id
);
2442 memset(&spidx
, 0, sizeof(spidx
));
2444 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
2445 /* make a spidx; a key to search SPD */
2446 spidx
.dir
= IPSEC_DIR_INBOUND
;
2450 * make destination address in spidx from either ID payload
2451 * or phase 1 address into a address in spidx.
2452 * If behind a nat - use phase1 address because server's
2453 * use the nat's address in the ID payload.
2455 if (iph2
->id
!= NULL
2457 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2458 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
2459 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2460 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2461 /* get a destination address of a policy */
2462 error
= ipsecdoi_id2sockaddr(iph2
->id
, &spidx
.dst
,
2463 &spidx
.prefd
, &spidx
.ul_proto
, iph2
->version
);
2469 * get scopeid from the SA address.
2470 * note that the phase 1 source address is used as
2471 * a destination address to search for a inbound policy entry
2472 * because rcoon is responder.
2474 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
2475 error
= setscopeid(&spidx
.dst
, iph2
->src
);
2481 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2482 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
2483 idi2type
= _XIDT(iph2
->id
);
2487 plog(ASL_LEVEL_DEBUG
,
2488 "Get a destination address of SP index "
2489 "from Phase 1 address "
2490 "due to no ID payloads found "
2491 "OR because ID type is not address.\n");
2494 * copy the SOURCE address of IKE into the DESTINATION address
2495 * of the key to search the SPD because the direction of policy
2498 memcpy(&spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
2499 switch (spidx
.dst
.ss_family
) {
2502 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.dst
;
2503 spidx
.prefd
= sizeof(struct in_addr
) << 3;
2504 s
->sin_port
= htons(0);
2509 spidx
.prefd
= sizeof(struct in6_addr
) << 3;
2518 /* make source address in spidx */
2519 if (iph2
->id_p
!= NULL
2521 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
2522 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
2523 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2524 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2525 /* get a source address of inbound SA */
2526 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &spidx
.src
,
2527 &spidx
.prefs
, &spidx
.ul_proto
, iph2
->version
);
2533 * get scopeid from the SA address.
2534 * for more detail, see above of this function.
2536 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
2537 error
= setscopeid(&spidx
.src
, iph2
->dst
);
2543 /* make id[src,dst] if both ID types are IP address and same */
2544 if (_XIDT(iph2
->id_p
) == idi2type
2545 && spidx
.dst
.ss_family
== spidx
.src
.ss_family
) {
2546 iph2
->src_id
= dupsaddr(&spidx
.dst
);
2547 if (iph2
->src_id
== NULL
) {
2549 "buffer allocation failed.\n");
2550 return ISAKMP_INTERNAL_ERROR
;
2552 iph2
->dst_id
= dupsaddr(&spidx
.src
);
2553 if (iph2
->dst_id
== NULL
) {
2555 "buffer allocation failed.\n");
2556 return ISAKMP_INTERNAL_ERROR
;
2561 plog(ASL_LEVEL_DEBUG
,
2562 "Get a source address of SP index "
2563 "from Phase 1 address "
2564 "due to no ID payloads found "
2565 "OR because ID type is not address.\n");
2567 /* see above comment. */
2568 memcpy(&spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
2569 switch (spidx
.src
.ss_family
) {
2572 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.src
;
2573 spidx
.prefs
= sizeof(struct in_addr
) << 3;
2574 s
->sin_port
= htons(0);
2579 spidx
.prefs
= sizeof(struct in6_addr
) << 3;
2590 plog(ASL_LEVEL_DEBUG
,
2591 "get a src address from ID payload "
2592 "%s prefixlen=%u ul_proto=%u\n",
2593 saddr2str((struct sockaddr
*)&spidx
.src
),
2594 spidx
.prefs
, spidx
.ul_proto
);
2595 plog(ASL_LEVEL_DEBUG
,
2596 "get dst address from ID payload "
2597 "%s prefixlen=%u ul_proto=%u\n",
2598 saddr2str((struct sockaddr
*)&spidx
.dst
),
2599 spidx
.prefd
, spidx
.ul_proto
);
2602 * convert the ul_proto if it is 0
2603 * because 0 in ID payload means a wild card.
2605 if (spidx
.ul_proto
== 0)
2606 spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
2608 /* get inbound policy */
2609 sp_in
= getsp_r(&spidx
, iph2
);
2610 if (sp_in
== NULL
|| sp_in
->policy
== IPSEC_POLICY_GENERATE
) {
2611 if (iph2
->ph1
->rmconf
->gen_policy
) {
2613 plog(ASL_LEVEL_INFO
,
2614 "Update the generated policy : %s\n",
2617 plog(ASL_LEVEL_INFO
,
2619 "try to generate the policy : %s\n",
2621 iph2
->spidx_gen
= (struct policyindex
*)racoon_malloc(sizeof(spidx
));
2622 if (!iph2
->spidx_gen
) {
2624 "buffer allocation failed.\n");
2625 return ISAKMP_INTERNAL_ERROR
;
2627 memcpy(iph2
->spidx_gen
, &spidx
, sizeof(spidx
));
2628 generated_policy_exit_early
= 1; /* special value */
2631 "no policy found: %s\n", spidx2str(&spidx
));
2632 return ISAKMP_INTERNAL_ERROR
;
2636 /* get outbound policy */
2638 struct sockaddr_storage addr
;
2641 spidx
.dir
= IPSEC_DIR_OUTBOUND
;
2643 spidx
.src
= spidx
.dst
;
2646 spidx
.prefs
= spidx
.prefd
;
2649 sp_out
= getsp_r(&spidx
, iph2
);
2651 plog(ASL_LEVEL_WARNING
,
2652 "no outbound policy found: %s\n",
2657 iph2
->spid
= sp_out
->id
;
2662 plog(ASL_LEVEL_DEBUG
,
2663 "suitable SP found:%s\n", spidx2str(&spidx
));
2665 if (generated_policy_exit_early
) {
2670 * In the responder side, the inbound policy should be using IPsec.
2671 * outbound policy is not checked currently.
2673 if (sp_in
->policy
!= IPSEC_POLICY_IPSEC
) {
2675 "policy found, but no IPsec required: %s\n",
2677 return ISAKMP_INTERNAL_ERROR
;
2680 /* set new proposal derived from a policy into the iph2->proposal. */
2681 if (set_proposal_from_policy(iph2
, sp_in
, sp_out
) < 0) {
2683 "failed to create saprop.\n");
2684 return ISAKMP_INTERNAL_ERROR
;