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"
92 #include <Security/SecDH.h>
96 static vchar_t
*quick_ir1mx (phase2_handle_t
*, vchar_t
*, vchar_t
*);
97 static int get_proposal_r_remote (phase2_handle_t
*, int);
103 * begin Quick Mode as initiator. send pfkey getspi message to kernel.
106 quick_iprep(iph2
, msg
)
107 phase2_handle_t
*iph2
;
108 vchar_t
*msg
; /* must be null pointer */
110 int error
= ISAKMP_INTERNAL_ERROR
;
113 if (iph2
->status
!= IKEV1_STATE_QUICK_I_START
) {
115 "status mismatched %d.\n", iph2
->status
);
119 iph2
->msgid
= isakmp_newmsgid2(iph2
->ph1
);
120 if (iph2
->ivm
!= NULL
)
121 oakley_delivm(iph2
->ivm
);
122 iph2
->ivm
= oakley_newiv2(iph2
->ph1
, iph2
->msgid
);
123 if (iph2
->ivm
== NULL
)
126 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_GETSPISENT
);
128 /* don't anything if local test mode. */
134 /* send getspi message */
135 if (pk_sendgetspi(iph2
) < 0) {
137 "failed to send getspi message");
141 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
143 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
144 pfkey_timeover_stub
, iph2
);
154 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
157 quick_i1send(iph2
, msg
)
158 phase2_handle_t
*iph2
;
159 vchar_t
*msg
; /* must be null pointer */
161 vchar_t
*body
= NULL
;
162 vchar_t
*hash
= NULL
;
164 vchar_t
*natoa_i
= NULL
;
165 vchar_t
*natoa_r
= NULL
;
166 #endif /* ENABLE_NATT */
168 struct isakmp_gen
*gen
;
171 int error
= ISAKMP_INTERNAL_ERROR
;
172 int pfsgroup
, idci
, idcr
;
174 struct ipsecdoi_id_b
*id
, *id_p
;
179 "msg has to be NULL in this function.\n");
182 if (iph2
->status
!= IKEV1_STATE_QUICK_I_GETSPIDONE
) {
184 "status mismatched %d.\n", iph2
->status
);
188 /* create SA payload for my proposal */
189 if (ipsecdoi_setph2proposal(iph2
, FALSE
) < 0) {
191 "failed to set proposal");
195 /* generate NONCE value */
196 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
197 if (iph2
->nonce
== NULL
) {
199 "failed to generate NONCE");
204 * DH value calculation is kicked out into cfparse.y.
205 * because pfs group can not be negotiated, it's only to be checked
208 /* generate KE value if need */
209 pfsgroup
= iph2
->proposal
->pfs_group
;
211 /* DH group settting if PFS is required. */
212 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
214 "failed to set DH value.\n");
218 if (oakley_dh_generate(iph2
->pfsgrp
,
219 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
221 if (oakley_dh_generate(iph2
->pfsgrp
,
222 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
225 "failed to generate DH");
230 /* generate ID value */
231 if (ipsecdoi_setid2(iph2
) < 0) {
233 "failed to get ID.\n");
236 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "IDci:\n");
237 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "IDcr:\n");
240 * we do not attach IDci nor IDcr, under the following condition:
241 * - all proposals are transport mode
243 * - id payload suggests to encrypt all the traffic (no specific
246 id
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
;
247 id_p
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id_p
->v
;
248 if (id
->proto_id
== 0
249 && id_p
->proto_id
== 0
250 && iph2
->ph1
->rmconf
->support_proxy
== 0
251 && ipsecdoi_transportmode(iph2
->proposal
)) {
256 /* create SA;NONCE payload, and KE if need, and IDii, IDir. */
257 tlen
= + sizeof(*gen
) + iph2
->sa
->l
258 + sizeof(*gen
) + iph2
->nonce
->l
;
260 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
262 tlen
+= sizeof(*gen
) + iph2
->id
->l
;
264 tlen
+= sizeof(*gen
) + iph2
->id_p
->l
;
268 * RFC3947 5.2. if we propose UDP-Encapsulated-Transport
269 * we should send NAT-OA
271 if (ipsecdoi_any_transportmode(iph2
->proposal
)
272 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
273 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
274 if (natoa_type
== -1) {
276 "failed to generate NAT-OA payload.\n");
278 } else if (natoa_type
!= 0) {
279 tlen
+= sizeof(*gen
) + natoa_i
->l
;
280 tlen
+= sizeof(*gen
) + natoa_r
->l
;
282 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "initiator send NAT-OAi:\n");
283 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "initiator send NAT-OAr:\n");
288 body
= vmalloc(tlen
);
291 "failed to get buffer to send.\n");
298 p
= set_isakmp_payload(p
, iph2
->sa
, ISAKMP_NPTYPE_NONCE
);
300 /* add NONCE payload */
302 np
= ISAKMP_NPTYPE_KE
;
303 else if (idci
|| idcr
)
304 np
= ISAKMP_NPTYPE_ID
;
306 np
= (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
307 p
= set_isakmp_payload(p
, iph2
->nonce
, np
);
309 /* add KE payload if need. */
310 np
= (idci
|| idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
312 p
= set_isakmp_payload(p
, iph2
->dhpub
, np
);
315 np
= (idcr
) ? ISAKMP_NPTYPE_ID
: (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
317 p
= set_isakmp_payload(p
, iph2
->id
, np
);
321 p
= set_isakmp_payload(p
, iph2
->id_p
, natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
);
325 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
326 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
329 /* generate HASH(1) */
330 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, body
);
333 "failed to compute HASH");
337 /* send isakmp payload */
338 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
339 if (iph2
->sendbuf
== NULL
) {
341 "failed to get send buffer");
345 /* send the packet, add to the schedule to resend */
346 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
347 if (isakmp_ph2resend(iph2
) == -1) {
349 "failed to send packet");
353 /* change status of isakmp status entry */
354 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG1SENT
);
367 #endif /* ENABLE_NATT */
373 * receive from responder
374 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
377 quick_i2recv(iph2
, msg0
)
378 phase2_handle_t
*iph2
;
382 vchar_t
*hbuf
= NULL
; /* for hash computing. */
383 vchar_t
*pbuf
= NULL
; /* for payload parsing */
384 struct isakmp_parse_t
*pa
;
385 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
386 struct isakmp_pl_hash
*hash
= NULL
;
390 int error
= ISAKMP_INTERNAL_ERROR
;
391 struct sockaddr_storage
*natoa_i
= NULL
;
392 struct sockaddr_storage
*natoa_r
= NULL
;
395 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG1SENT
) {
397 "status mismatched %d.\n", iph2
->status
);
402 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
404 "Packet wasn't encrypted.\n");
407 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
410 "failed to decrypt");
414 /* create buffer for validating HASH(2) */
417 * 1. the first one must be HASH
418 * 2. the second one must be SA (added in isakmp-oakley-05!)
419 * 3. two IDs must be considered as IDci, then IDcr
421 pbuf
= isakmp_parse(msg
);
424 "failed to parse msg");
427 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
429 /* HASH payload is fixed postion */
430 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
432 "received invalid next payload type %d, "
434 pa
->type
, ISAKMP_NPTYPE_HASH
);
437 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
441 * this restriction was introduced in isakmp-oakley-05.
442 * we do not check this for backward compatibility.
443 * TODO: command line/config file option to enable/disable this code
445 /* HASH payload is fixed postion */
446 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
447 plog(ASL_LEVEL_WARNING
,
448 "received invalid next payload type %d, "
450 pa
->type
, ISAKMP_NPTYPE_HASH
);
453 /* allocate buffer for computing HASH(2) */
454 tlen
= iph2
->nonce
->l
455 + ntohl(isakmp
->len
) - sizeof(*isakmp
);
458 "invalid length (%lu,%d) while getting hash buffer.\n",
459 iph2
->nonce
->l
, ntohl(isakmp
->len
));
462 hbuf
= vmalloc(tlen
);
465 "failed to get hash buffer.\n");
468 p
= hbuf
->v
+ iph2
->nonce
->l
; /* retain the space for Ni_b */
471 * parse the payloads.
472 * copy non-HASH payloads into hbuf, so that we can validate HASH.
475 f_id
= 0; /* flag to use checking ID */
476 tlen
= 0; /* count payload length except of HASH payload. */
477 for (; pa
->type
; pa
++) {
479 /* copy to buffer for HASH */
480 /* Don't modify the payload */
481 memcpy(p
, pa
->ptr
, pa
->len
);
484 case ISAKMP_NPTYPE_SA
:
485 if (iph2
->sa_ret
!= NULL
) {
487 "Ignored, multiple SA "
488 "isn't supported.\n");
491 if (isakmp_p2ph(&iph2
->sa_ret
, pa
->ptr
) < 0) {
493 "failed to process SA payload");
498 case ISAKMP_NPTYPE_NONCE
:
499 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
501 "failed to process NONCE payload");
506 case ISAKMP_NPTYPE_KE
:
507 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
509 "failed to process KE payload");
514 case ISAKMP_NPTYPE_ID
:
518 if (iph2
->id
== NULL
|| iph2
->id_p
== NULL
) {
519 error
= ISAKMP_INTERNAL_ERROR
; // shouldn't happen
532 /* These ids may not match when natt is used with some devices.
533 * RFC 2407 says that the protocol and port fields should be ignored
534 * if they are zero, therefore they need to be checked individually.
536 struct ipsecdoi_id_b
*id_ptr
= ALIGNED_CAST(struct ipsecdoi_id_b
*)vp
->v
;
537 struct ipsecdoi_pl_id
*idp_ptr
= (struct ipsecdoi_pl_id
*)pa
->ptr
;
539 if (id_ptr
->type
!= idp_ptr
->b
.type
540 || (idp_ptr
->b
.proto_id
!= 0 && idp_ptr
->b
.proto_id
!= id_ptr
->proto_id
)
541 || (idp_ptr
->b
.port
!= 0 && idp_ptr
->b
.port
!= id_ptr
->port
)
542 || memcmp(vp
->v
+ sizeof(struct ipsecdoi_id_b
), (caddr_t
)pa
->ptr
+ sizeof(struct ipsecdoi_pl_id
),
543 vp
->l
- sizeof(struct ipsecdoi_id_b
))) {
544 // to support servers that use our external nat address as our ID
545 if (iph2
->ph1
->natt_flags
& NAT_DETECTED
) {
546 plog(ASL_LEVEL_WARNING
,
547 "mismatched ID was returned - ignored because nat traversal is being used.\n");
548 /* If I'm behind a nat and the ID is type address - save the address
549 * and port for when the peer rekeys.
551 if (f_id
== 0 && (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
)) {
552 if (lcconf
->ext_nat_id
)
553 vfree(lcconf
->ext_nat_id
);
554 if (idp_ptr
->h
.len
< sizeof(struct isakmp_gen
)) {
555 plog(ASL_LEVEL_ERR
, "invalid length (%d) while allocating external nat id.\n", idp_ptr
->h
.len
);
558 lcconf
->ext_nat_id
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
559 if (lcconf
->ext_nat_id
== NULL
) {
560 plog(ASL_LEVEL_ERR
, "memory error while allocating external nat id.\n");
563 memcpy(lcconf
->ext_nat_id
->v
, &(idp_ptr
->b
), lcconf
->ext_nat_id
->l
);
564 if (iph2
->ext_nat_id
)
565 vfree(iph2
->ext_nat_id
);
566 iph2
->ext_nat_id
= vdup(lcconf
->ext_nat_id
);
567 if (iph2
->ext_nat_id
== NULL
) {
568 plog(ASL_LEVEL_ERR
, "memory error while allocating ph2's external nat id.\n");
571 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id
->v
, iph2
->ext_nat_id
->l
, "external nat address saved.\n");
572 } else if (f_id
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
)) {
573 if (iph2
->ext_nat_id_p
)
574 vfree(iph2
->ext_nat_id_p
);
575 iph2
->ext_nat_id_p
= vmalloc(ntohs(idp_ptr
->h
.len
) - sizeof(struct isakmp_gen
));
576 if (iph2
->ext_nat_id_p
== NULL
) {
577 plog(ASL_LEVEL_ERR
, "memory error while allocating peers ph2's external nat id.\n");
580 memcpy(iph2
->ext_nat_id_p
->v
, &(idp_ptr
->b
), iph2
->ext_nat_id_p
->l
);
581 plogdump(ASL_LEVEL_DEBUG
, iph2
->ext_nat_id_p
->v
, iph2
->ext_nat_id_p
->l
, "peer's external nat address saved.\n");
584 plog(ASL_LEVEL_ERR
, "mismatched ID was returned.\n");
585 error
= ISAKMP_NTYPE_ATTRIBUTES_NOT_SUPPORTED
;
594 case ISAKMP_NPTYPE_N
:
595 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
599 case ISAKMP_NPTYPE_NATOA_DRAFT
:
600 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
601 case ISAKMP_NPTYPE_NATOA_RFC
:
604 struct sockaddr_storage
*daddr
;
606 isakmp_p2ph(&vp
, pa
->ptr
);
609 daddr
= process_natoa_payload(vp
);
611 if (natoa_i
== NULL
) {
613 plog(ASL_LEVEL_DEBUG
, "initiaor rcvd NAT-OA i: %s\n",
614 saddr2str((struct sockaddr
*)natoa_i
));
615 } else if (natoa_r
== NULL
) {
617 plog(ASL_LEVEL_DEBUG
, "initiator rcvd NAT-OA r: %s\n",
618 saddr2str((struct sockaddr
*)natoa_r
));
631 /* don't send information, see ident_r1recv() */
633 "ignore the packet, "
634 "received unexpecting payload type %d.\n",
641 /* compute true length of payload. */
645 /* payload existency check */
646 if (hash
== NULL
|| iph2
->sa_ret
== NULL
|| iph2
->nonce_p
== NULL
) {
648 "few isakmp message received.\n");
652 /* Fixed buffer for calculating HASH */
653 memcpy(hbuf
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
654 plog(ASL_LEVEL_DEBUG
,
655 "HASH allocated:hbuf->l=%zu actual:tlen=%zu\n",
656 hbuf
->l
, tlen
+ iph2
->nonce
->l
);
657 /* adjust buffer length for HASH */
658 hbuf
->l
= iph2
->nonce
->l
+ tlen
;
660 /* validate HASH(2) */
663 vchar_t
*my_hash
= NULL
;
666 r_hash
= (char *)hash
+ sizeof(*hash
);
668 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(2) received:");
670 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
671 if (my_hash
== NULL
) {
673 "failed to compute HASH");
677 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
681 plog(ASL_LEVEL_DEBUG
,
682 "HASH(2) mismatch.\n");
683 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
688 /* validity check SA payload sent from responder */
689 if (ipsecdoi_checkph2proposal(iph2
) < 0) {
691 "failed to validate SA proposal");
692 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
696 /* change status of isakmp status entry */
697 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG2RCVD
);
710 racoon_free(natoa_i
);
713 racoon_free(natoa_r
);
717 VPTRINIT(iph2
->sa_ret
);
718 VPTRINIT(iph2
->nonce_p
);
719 VPTRINIT(iph2
->dhpub_p
);
730 quick_i3send(iph2
, msg0
)
731 phase2_handle_t
*iph2
;
736 vchar_t
*hash
= NULL
;
739 int error
= ISAKMP_INTERNAL_ERROR
;
740 int packet_error
= -1;
743 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG2RCVD
) {
745 "status mismatched %d.\n", iph2
->status
);
749 /* generate HASH(3) */
753 plog(ASL_LEVEL_DEBUG
, "HASH(3) generate\n");
755 tmp
= vmalloc(iph2
->nonce
->l
+ iph2
->nonce_p
->l
);
758 "failed to get hash buffer.\n");
761 memcpy(tmp
->v
, iph2
->nonce
->v
, iph2
->nonce
->l
);
762 memcpy(tmp
->v
+ iph2
->nonce
->l
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
764 hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
769 "failed to compute HASH");
774 /* create buffer for isakmp payload */
775 tlen
= sizeof(struct isakmp
)
776 + sizeof(struct isakmp_gen
) + hash
->l
;
780 "failed to get buffer to send.\n");
784 /* create isakmp header */
785 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
788 "failed to create ISAKMP header");
792 /* add HASH(3) payload */
793 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_NONE
);
795 #ifdef HAVE_PRINT_ISAKMP_C
796 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
800 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
801 if (iph2
->sendbuf
== NULL
) {
803 "failed to encrypt packet");
807 /* if there is commit bit, need resending */
808 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
809 /* send the packet, add to the schedule to resend */
810 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
811 if (isakmp_ph2resend(iph2
) == -1) {
813 "failed to send packet, commit-bit");
817 /* send the packet */
818 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
820 "failed to send packet");
825 /* the sending message is added to the received-list. */
826 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
,
828 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
830 "failed to add a response packet to the tree.\n");
836 /* compute both of KEYMATs */
837 if (oakley_compute_keymat(iph2
, INITIATOR
) < 0) {
839 "failed to compute KEYMAT");
843 /* if there is commit bit don't set up SA now. */
844 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
845 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_MSG3SENT
);
850 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
852 /* Do UPDATE for initiator */
853 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
854 if (pk_sendupdate(iph2
) < 0) {
855 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
858 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
860 /* Do ADD for responder */
861 if (pk_sendadd(iph2
) < 0) {
862 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
865 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
881 * receive from responder
882 * HDR#*, HASH(4), notify
885 quick_i4recv(iph2
, msg0
)
886 phase2_handle_t
*iph2
;
890 vchar_t
*pbuf
= NULL
; /* for payload parsing */
891 struct isakmp_parse_t
*pa
;
892 struct isakmp_pl_hash
*hash
= NULL
;
893 vchar_t
*notify
= NULL
;
894 int error
= ISAKMP_INTERNAL_ERROR
;
895 int packet_error
= -1;
898 if (iph2
->status
!= IKEV1_STATE_QUICK_I_MSG3SENT
) {
900 "status mismatched %d.\n", iph2
->status
);
905 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
907 "Packet wasn't encrypted.\n");
910 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
913 "failed to decrypt packet\n");
917 /* validate the type of next payload */
918 pbuf
= isakmp_parse(msg
);
921 "failed to parse msg\n");
925 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
926 pa
->type
!= ISAKMP_NPTYPE_NONE
;
930 case ISAKMP_NPTYPE_HASH
:
931 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
933 case ISAKMP_NPTYPE_N
:
934 if (notify
!= NULL
) {
935 plog(ASL_LEVEL_WARNING
,
936 "Ignoring multiple notifications\n");
939 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
940 notify
= vmalloc(pa
->len
);
941 if (notify
== NULL
) {
943 "failed to get notify buffer.\n");
946 memcpy(notify
->v
, pa
->ptr
, notify
->l
);
949 /* don't send information, see ident_r1recv() */
951 "ignore the packet, "
952 "received unexpecting payload type %d.\n",
958 /* payload existency check */
961 "few isakmp message received.\n");
965 /* validate HASH(4) */
968 vchar_t
*my_hash
= NULL
;
972 r_hash
= (char *)hash
+ sizeof(*hash
);
974 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(4) validate:");
976 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
978 if (my_hash
== NULL
) {
980 "failed to compute HASH\n");
984 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
988 plog(ASL_LEVEL_DEBUG
,
989 "HASH(4) mismatch.\n");
990 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
997 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_ADDSA
);
999 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
1001 /* don't anything if local test mode. */
1007 /* Do UPDATE for initiator */
1008 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
1009 if (pk_sendupdate(iph2
) < 0) {
1010 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
1013 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
1015 /* Do ADD for responder */
1016 if (pk_sendadd(iph2
) < 0) {
1017 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
1020 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
1036 * receive from initiator
1037 * HDR*, HASH(1), SA, Ni [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1040 quick_r1recv(iph2
, msg0
)
1041 phase2_handle_t
*iph2
;
1044 vchar_t
*msg
= NULL
;
1045 vchar_t
*hbuf
= NULL
; /* for hash computing. */
1046 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1047 struct isakmp_parse_t
*pa
;
1048 struct isakmp
*isakmp
= (struct isakmp
*)msg0
->v
;
1049 struct isakmp_pl_hash
*hash
= NULL
;
1052 int f_id_order
; /* for ID payload detection */
1053 int error
= ISAKMP_INTERNAL_ERROR
;
1054 struct sockaddr_storage
*natoa_i
= NULL
;
1055 struct sockaddr_storage
*natoa_r
= NULL
;
1057 /* validity check */
1058 if (iph2
->status
!= IKEV1_STATE_QUICK_R_START
) {
1060 "status mismatched %d.\n", iph2
->status
);
1065 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1067 "Packet wasn't encrypted.\n");
1068 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1071 /* decrypt packet */
1072 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1075 "failed to decrypt packet\n");
1079 /* create buffer for using to validate HASH(1) */
1082 * 1. the first one must be HASH
1083 * 2. the second one must be SA (added in isakmp-oakley-05!)
1084 * 3. two IDs must be considered as IDci, then IDcr
1086 pbuf
= isakmp_parse(msg
);
1089 "failed to parse msg\n");
1092 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1094 /* HASH payload is fixed postion */
1095 if (pa
->type
!= ISAKMP_NPTYPE_HASH
) {
1097 "received invalid next payload type %d, "
1099 pa
->type
, ISAKMP_NPTYPE_HASH
);
1100 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1103 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1107 * this restriction was introduced in isakmp-oakley-05.
1108 * we do not check this for backward compatibility.
1109 * TODO: command line/config file option to enable/disable this code
1111 /* HASH payload is fixed postion */
1112 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1113 plog(ASL_LEVEL_WARNING
,
1114 "received invalid next payload type %d, "
1116 pa
->type
, ISAKMP_NPTYPE_SA
);
1117 error
= ISAKMP_NTYPE_BAD_PROPOSAL_SYNTAX
;
1120 /* allocate buffer for computing HASH(1) */
1121 tlen
= ntohl(isakmp
->len
) - sizeof(*isakmp
);
1123 plog(ASL_LEVEL_ERR
, "invalid length (%d) while extracting hash.\n",
1124 ntohl(isakmp
->len
));
1127 hbuf
= vmalloc(tlen
);
1130 "failed to get hash buffer.\n");
1136 * parse the payloads.
1137 * copy non-HASH payloads into hbuf, so that we can validate HASH.
1139 iph2
->sa
= NULL
; /* we don't support multi SAs. */
1140 iph2
->nonce_p
= NULL
;
1141 iph2
->dhpub_p
= NULL
;
1144 tlen
= 0; /* count payload length except of HASH payload. */
1147 * IDi2 MUST be immediatelly followed by IDr2. We allowed the
1148 * illegal case, but logged. First ID payload is to be IDi2.
1149 * And next ID payload is to be IDr2.
1153 for (; pa
->type
; pa
++) {
1155 /* copy to buffer for HASH */
1156 /* Don't modify the payload */
1157 memcpy(p
, pa
->ptr
, pa
->len
);
1159 if (pa
->type
!= ISAKMP_NPTYPE_ID
)
1163 case ISAKMP_NPTYPE_SA
:
1164 if (iph2
->sa
!= NULL
) {
1166 "Multi SAs isn't supported.\n");
1169 if (isakmp_p2ph(&iph2
->sa
, pa
->ptr
) < 0) {
1171 "failed to process SA payload\n");
1176 case ISAKMP_NPTYPE_NONCE
:
1177 if (isakmp_p2ph(&iph2
->nonce_p
, pa
->ptr
) < 0) {
1179 "failed to process NONCE payload\n");
1184 case ISAKMP_NPTYPE_KE
:
1185 if (isakmp_p2ph(&iph2
->dhpub_p
, pa
->ptr
) < 0) {
1187 "failed to process KE payload\n");
1192 case ISAKMP_NPTYPE_ID
:
1193 if (iph2
->id_p
== NULL
) {
1197 if (isakmp_p2ph(&iph2
->id_p
, pa
->ptr
) < 0) {
1199 "failed to process IDci2 payload\n");
1203 } else if (iph2
->id
== NULL
) {
1205 if (f_id_order
== 0) {
1207 "IDr2 payload is not "
1208 "immediatelly followed "
1209 "by IDi2. We allowed.\n");
1210 /* XXX we allowed in this case. */
1213 if (isakmp_p2ph(&iph2
->id
, pa
->ptr
) < 0) {
1215 "failed to process IDcr2 payload\n");
1219 plogdump(ASL_LEVEL_ERR
, iph2
->id
->v
, iph2
->id
->l
, "received too many ID payloads");
1220 error
= ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
1225 case ISAKMP_NPTYPE_N
:
1226 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1230 case ISAKMP_NPTYPE_NATOA_DRAFT
:
1231 case ISAKMP_NPTYPE_NATOA_BADDRAFT
:
1232 case ISAKMP_NPTYPE_NATOA_RFC
:
1235 struct sockaddr_storage
*daddr
;
1237 isakmp_p2ph(&vp
, pa
->ptr
);
1240 daddr
= process_natoa_payload(vp
);
1242 if (natoa_i
== NULL
) {
1244 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA i: %s\n",
1245 saddr2str((struct sockaddr
*)natoa_i
));
1246 } else if (natoa_r
== NULL
) {
1248 plog(ASL_LEVEL_DEBUG
, "responder rcvd NAT-OA r: %s\n",
1249 saddr2str((struct sockaddr
*)natoa_r
));
1263 "ignore the packet, "
1264 "received unexpected payload type %d.\n",
1266 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1272 /* compute true length of payload. */
1276 /* payload existency check */
1277 if (hash
== NULL
|| iph2
->sa
== NULL
|| iph2
->nonce_p
== NULL
) {
1279 "expected isakmp payloads missing.\n");
1280 error
= ISAKMP_NTYPE_PAYLOAD_MALFORMED
;
1285 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "received IDci2:");
1288 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "received IDcr2:");
1291 /* adjust buffer length for HASH */
1294 /* validate HASH(1) */
1297 vchar_t
*my_hash
= NULL
;
1300 r_hash
= (caddr_t
)hash
+ sizeof(*hash
);
1302 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(1) validate:");
1304 my_hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, hbuf
);
1305 if (my_hash
== NULL
) {
1307 "failed to compute HASH\n");
1311 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1316 "HASH(1) mismatch.\n");
1317 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1323 error
= get_sainfo_r(iph2
);
1326 "failed to get sainfo.\n");
1330 /* check the existence of ID payload and create responder's proposal */
1331 error
= get_proposal_r(iph2
);
1334 /* generate a policy template from peer's proposal */
1335 if (set_proposal_from_proposal(iph2
)) {
1337 "failed to generate a proposal template "
1338 "from client's proposal.\n");
1339 error
= ISAKMP_INTERNAL_ERROR
;
1344 /* select single proposal or reject it. */
1345 if (ipsecdoi_selectph2proposal(iph2
) < 0) {
1347 "failed to select proposal.\n");
1348 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1354 "failed to get proposal for responder.\n");
1358 /* check KE and attribute of PFS */
1359 if (iph2
->dhpub_p
!= NULL
&& iph2
->approval
->pfs_group
== 0) {
1361 "no PFS is specified, but peer sends KE.\n");
1362 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1365 if (iph2
->dhpub_p
== NULL
&& iph2
->approval
->pfs_group
!= 0) {
1367 "PFS is specified, but peer doesn't sends KE.\n");
1368 error
= ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
;
1372 ike_session_update_mode(iph2
); /* update the mode, now that we have a proposal */
1375 * save the packet from the initiator in order to resend the
1376 * responder's first packet against this packet.
1378 iph2
->msg1
= vdup(msg0
);
1380 /* change status of isakmp status entry */
1381 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG1RCVD
);
1394 racoon_free(natoa_i
);
1397 racoon_free(natoa_r
);
1403 VPTRINIT(iph2
->nonce_p
);
1404 VPTRINIT(iph2
->dhpub_p
);
1406 VPTRINIT(iph2
->id_p
);
1413 * call pfkey_getspi.
1416 quick_rprep(iph2
, msg
)
1417 phase2_handle_t
*iph2
;
1420 int error
= ISAKMP_INTERNAL_ERROR
;
1422 /* validity check */
1423 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG1RCVD
) {
1425 "status mismatched %d.\n", iph2
->status
);
1429 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_GETSPISENT
);
1431 /* send getspi message */
1432 if (pk_sendgetspi(iph2
) < 0) {
1434 "failed to send getspi");
1438 plog(ASL_LEVEL_DEBUG
, "pfkey getspi sent.\n");
1440 iph2
->sce
= sched_new(lcconf
->wait_ph2complete
,
1441 pfkey_timeover_stub
, iph2
);
1451 * HDR*, HASH(2), SA, Nr [, KE ] [, IDi2, IDr2 ] [, NAT-OAi, NAT-OAr ]
1454 quick_r2send(iph2
, msg
)
1455 phase2_handle_t
*iph2
;
1458 vchar_t
*body
= NULL
;
1459 vchar_t
*hash
= NULL
;
1460 vchar_t
*natoa_i
= NULL
;
1461 vchar_t
*natoa_r
= NULL
;
1463 struct isakmp_gen
*gen
;
1466 int error
= ISAKMP_INTERNAL_ERROR
;
1468 u_int8_t
*np_p
= NULL
;
1470 /* validity check */
1473 "msg has to be NULL in this function.\n");
1476 if (iph2
->status
!= IKEV1_STATE_QUICK_R_GETSPIDONE
) {
1478 "status mismatched %d.\n", iph2
->status
);
1482 /* update responders SPI */
1483 if (ipsecdoi_updatespi(iph2
) < 0) {
1484 plog(ASL_LEVEL_ERR
, "failed to update spi.\n");
1488 /* generate NONCE value */
1489 iph2
->nonce
= eay_set_random(iph2
->ph1
->rmconf
->nonce_size
);
1490 if (iph2
->nonce
== NULL
) {
1492 "failed to generate NONCE");
1496 /* generate KE value if need */
1497 pfsgroup
= iph2
->approval
->pfs_group
;
1498 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1499 /* DH group settting if PFS is required. */
1500 if (oakley_setdhgroup(pfsgroup
, &iph2
->pfsgrp
) < 0) {
1502 "failed to set DH value.\n");
1505 /* generate DH public value */
1507 if (oakley_dh_generate(iph2
->pfsgrp
,
1508 &iph2
->dhpub
, &iph2
->dhpriv
) < 0) {
1510 if (oakley_dh_generate(iph2
->pfsgrp
,
1511 &iph2
->dhpub
, &iph2
->publicKeySize
, &iph2
->dhC
) < 0) {
1514 "failed to generate DH public");
1519 /* create SA;NONCE payload, and KE and ID if need */
1520 tlen
= sizeof(*gen
) + iph2
->sa_ret
->l
1521 + sizeof(*gen
) + iph2
->nonce
->l
;
1522 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1523 tlen
+= (sizeof(*gen
) + iph2
->dhpub
->l
);
1524 if (iph2
->id_p
!= NULL
)
1525 tlen
+= (sizeof(*gen
) + iph2
->id_p
->l
1526 + sizeof(*gen
) + iph2
->id
->l
);
1530 * RFC3947 5.2. if we chose UDP-Encapsulated-Transport
1531 * we should send NAT-OA
1533 if (ipsecdoi_any_transportmode(iph2
->approval
)
1534 && (iph2
->ph1
->natt_flags
& NAT_DETECTED
)) {
1535 natoa_type
= create_natoa_payloads(iph2
, &natoa_i
, &natoa_r
);
1536 if (natoa_type
== -1) {
1538 "failed to create NATOA payloads");
1541 else if (natoa_type
!= 0) {
1542 tlen
+= sizeof(*gen
) + natoa_i
->l
;
1543 tlen
+= sizeof(*gen
) + natoa_r
->l
;
1545 //plogdump(ASL_LEVEL_DEBUG, natoa_i->v, natoa_i->l, "responder send NAT-OAi:");
1546 //plogdump(ASL_LEVEL_DEBUG, natoa_r->v, natoa_r->l, "responder send NAT-OAr:");
1551 plog(ASL_LEVEL_DEBUG
, "Approved SA\n");
1552 printsaprop0(ASL_LEVEL_DEBUG
, iph2
->approval
);
1554 body
= vmalloc(tlen
);
1557 "failed to get buffer to send.\n");
1562 /* make SA payload */
1563 p
= set_isakmp_payload(body
->v
, iph2
->sa_ret
, ISAKMP_NPTYPE_NONCE
);
1565 /* add NONCE payload */
1566 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1567 p
= set_isakmp_payload(p
, iph2
->nonce
,
1568 (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0)
1570 : (iph2
->id_p
!= NULL
1572 : (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
)));
1574 /* add KE payload if need. */
1575 if (iph2
->dhpub_p
!= NULL
&& pfsgroup
!= 0) {
1576 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1577 p
= set_isakmp_payload(p
, iph2
->dhpub
,
1578 (iph2
->id_p
== NULL
) ? (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
) : ISAKMP_NPTYPE_ID
);
1581 /* add ID payloads received. */
1582 if (iph2
->id_p
!= NULL
) {
1584 p
= set_isakmp_payload(p
, iph2
->id_p
, ISAKMP_NPTYPE_ID
);
1585 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "sending IDci2:");
1587 np_p
= &((struct isakmp_gen
*)p
)->np
; /* XXX */
1588 p
= set_isakmp_payload(p
, iph2
->id
, (natoa_type
? natoa_type
: ISAKMP_NPTYPE_NONE
));
1589 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "sending IDcr2:");
1592 /* add a RESPONDER-LIFETIME notify payload if needed */
1594 vchar_t
*data
= NULL
;
1595 struct saprop
*pp
= iph2
->approval
;
1598 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_SEC
) {
1599 u_int32_t v
= htonl((u_int32_t
)pp
->lifetime
);
1600 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1601 IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
1604 "failed to add RESPONDER-LIFETIME notify (type) payload");
1607 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1608 (caddr_t
)&v
, sizeof(v
));
1611 "failed to add RESPONDER-LIFETIME notify (value) payload");
1615 if (pp
->claim
& IPSECDOI_ATTR_SA_LD_TYPE_KB
) {
1616 u_int32_t v
= htonl((u_int32_t
)pp
->lifebyte
);
1617 data
= isakmp_add_attr_l(data
, IPSECDOI_ATTR_SA_LD_TYPE
,
1618 IPSECDOI_ATTR_SA_LD_TYPE_KB
);
1621 "failed to add RESPONDER-LIFETIME notify (type) payload");
1624 data
= isakmp_add_attr_v(data
, IPSECDOI_ATTR_SA_LD
,
1625 (caddr_t
)&v
, sizeof(v
));
1628 "failed to add RESPONDER-LIFETIME notify (value) payload");
1634 * XXX Is there only single RESPONDER-LIFETIME payload in a IKE message
1635 * in the case of SA bundle ?
1638 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1639 body
= isakmp_add_pl_n(body
, &np_p
,
1640 ISAKMP_NTYPE_RESPONDER_LIFETIME
, pr
, data
);
1643 "invalid RESPONDER-LIFETIME payload");
1645 return error
; /* XXX */
1654 p
= set_isakmp_payload(p
, natoa_i
, natoa_type
);
1655 p
= set_isakmp_payload(p
, natoa_r
, ISAKMP_NPTYPE_NONE
);
1658 /* generate HASH(2) */
1662 tmp
= vmalloc(iph2
->nonce_p
->l
+ body
->l
);
1665 "failed to get hash buffer.\n");
1668 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1669 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, body
->v
, body
->l
);
1671 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, tmp
);
1676 "failed to compute HASH");
1681 /* send isakmp payload */
1682 iph2
->sendbuf
= quick_ir1mx(iph2
, body
, hash
);
1683 if (iph2
->sendbuf
== NULL
) {
1685 "failed to get send buffer");
1689 /* send the packet, add to the schedule to resend */
1690 iph2
->retry_counter
= iph2
->ph1
->rmconf
->retry_counter
;
1691 if (isakmp_ph2resend(iph2
) == -1) {
1693 "failed to send packet");
1697 /* the sending message is added to the received-list. */
1698 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, iph2
->msg1
,
1699 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
1701 "failed to add a response packet to the tree.\n");
1705 /* change status of isakmp status entry */
1706 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG2SENT
);
1723 * receive from initiator
1727 quick_r3recv(iph2
, msg0
)
1728 phase2_handle_t
*iph2
;
1731 vchar_t
*msg
= NULL
;
1732 vchar_t
*pbuf
= NULL
; /* for payload parsing */
1733 struct isakmp_parse_t
*pa
;
1734 struct isakmp_pl_hash
*hash
= NULL
;
1735 int error
= ISAKMP_INTERNAL_ERROR
;
1737 /* validity check */
1738 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG2SENT
) {
1740 "status mismatched %d.\n", iph2
->status
);
1744 /* decrypt packet */
1745 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1747 "Packet wasn't encrypted.\n");
1750 msg
= oakley_do_decrypt(iph2
->ph1
, msg0
, iph2
->ivm
->iv
, iph2
->ivm
->ive
);
1753 "failed to decrypt packet\n");
1757 /* validate the type of next payload */
1758 pbuf
= isakmp_parse(msg
);
1761 "failed to parse msg\n");
1765 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1766 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1770 case ISAKMP_NPTYPE_HASH
:
1771 hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1773 case ISAKMP_NPTYPE_N
:
1774 isakmp_check_ph2_notify(pa
->ptr
, iph2
);
1777 /* don't send information, see ident_r1recv() */
1779 "ignore the packet, "
1780 "received unexpecting payload type %d.\n",
1786 /* payload existency check */
1789 "few isakmp message received.\n");
1793 /* validate HASH(3) */
1794 /* HASH(3) = prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b) */
1797 vchar_t
*my_hash
= NULL
;
1798 vchar_t
*tmp
= NULL
;
1801 r_hash
= (char *)hash
+ sizeof(*hash
);
1803 //plogdump(ASL_LEVEL_DEBUG, r_hash, ntohs(hash->h.len) - sizeof(*hash), "HASH(3) validate:");
1805 tmp
= vmalloc(iph2
->nonce_p
->l
+ iph2
->nonce
->l
);
1808 "failed to get hash buffer.\n");
1811 memcpy(tmp
->v
, iph2
->nonce_p
->v
, iph2
->nonce_p
->l
);
1812 memcpy(tmp
->v
+ iph2
->nonce_p
->l
, iph2
->nonce
->v
, iph2
->nonce
->l
);
1814 my_hash
= oakley_compute_hash3(iph2
->ph1
, iph2
->msgid
, tmp
);
1816 if (my_hash
== NULL
) {
1818 "failed to compute HASH\n");
1822 result
= timingsafe_bcmp(my_hash
->v
, r_hash
, my_hash
->l
);
1827 "HASH(3) mismatch.\n");
1828 error
= ISAKMP_NTYPE_INVALID_HASH_INFORMATION
;
1833 /* if there is commit bit, don't set up SA now. */
1834 if (ISSET(iph2
->flags
, ISAKMP_FLAG_C
)) {
1835 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_MSG3RCVD
);
1837 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
1851 * HDR#*, HASH(4), notify
1854 quick_r4send(iph2
, msg0
)
1855 phase2_handle_t
*iph2
;
1858 vchar_t
*buf
= NULL
;
1859 vchar_t
*myhash
= NULL
;
1860 struct isakmp_pl_n
*n
;
1861 vchar_t
*notify
= NULL
;
1864 int error
= ISAKMP_INTERNAL_ERROR
;
1866 /* validity check */
1867 if (iph2
->status
!= IKEV1_STATE_QUICK_R_MSG3RCVD
) {
1869 "status mismatched %d.\n", iph2
->status
);
1873 /* generate HASH(4) */
1874 /* XXX What can I do in the case of multiple different SA */
1875 plog(ASL_LEVEL_DEBUG
, "HASH(4) generate\n");
1877 /* XXX What should I do if there are multiple SAs ? */
1878 tlen
= sizeof(struct isakmp_pl_n
) + iph2
->approval
->head
->spisize
;
1879 notify
= vmalloc(tlen
);
1880 if (notify
== NULL
) {
1882 "failed to get notify buffer.\n");
1885 n
= (struct isakmp_pl_n
*)notify
->v
;
1886 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1887 n
->h
.len
= htons(tlen
);
1888 n
->doi
= htonl(IPSEC_DOI
);
1889 n
->proto_id
= iph2
->approval
->head
->proto_id
;
1890 n
->spi_size
= sizeof(iph2
->approval
->head
->spisize
);
1891 n
->type
= htons(ISAKMP_NTYPE_CONNECTED
);
1892 memcpy(n
+ 1, &iph2
->approval
->head
->spi
, iph2
->approval
->head
->spisize
);
1894 myhash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, notify
);
1895 if (myhash
== NULL
) {
1897 "failed to compute HASH");
1901 /* create buffer for isakmp payload */
1902 tlen
= sizeof(struct isakmp
)
1903 + sizeof(struct isakmp_gen
) + myhash
->l
1905 buf
= vmalloc(tlen
);
1908 "failed to get buffer to send.\n");
1912 /* create isakmp header */
1913 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
1916 "failed to set ISAKMP header");
1920 /* add HASH(4) payload */
1921 p
= set_isakmp_payload(p
, myhash
, ISAKMP_NPTYPE_N
);
1923 /* add notify payload */
1924 memcpy(p
, notify
->v
, notify
->l
);
1926 #ifdef HAVE_PRINT_ISAKMP_C
1927 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
1931 iph2
->sendbuf
= oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
1932 if (iph2
->sendbuf
== NULL
) {
1934 "failed to encrypt packet");
1938 /* send the packet */
1939 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
1941 "failed to send packet");
1945 /* the sending message is added to the received-list. */
1946 if (ike_session_add_recvdpkt(iph2
->ph1
->remote
, iph2
->ph1
->local
, iph2
->sendbuf
, msg0
,
1947 PH2_NON_ESP_EXTRA_LEN(iph2
, iph2
->sendbuf
), PH2_FRAG_FLAGS(iph2
)) == -1) {
1948 plog(ASL_LEVEL_ERR
,
1949 "failed to add a response packet to the tree.\n");
1953 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_COMMIT
);
1972 quick_rfinalize(iph2
, msg0
)
1973 phase2_handle_t
*iph2
;
1976 vchar_t
*msg
= NULL
;
1977 int error
= ISAKMP_INTERNAL_ERROR
;
1979 /* validity check */
1980 if (iph2
->status
!= IKEV1_STATE_QUICK_R_COMMIT
) {
1982 "status mismatched %d.\n", iph2
->status
);
1986 /* compute both of KEYMATs */
1987 if (oakley_compute_keymat(iph2
, RESPONDER
) < 0) {
1989 "failed to compute KEYMAT");
1993 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_ADDSA
);
1995 iph2
->flags
^= ISAKMP_FLAG_C
; /* reset bit */
1997 /* don't anything if local test mode. */
2003 /* Do UPDATE as responder */
2004 plog(ASL_LEVEL_DEBUG
, "call pk_sendupdate\n");
2005 if (pk_sendupdate(iph2
) < 0) {
2006 plog(ASL_LEVEL_ERR
, "pfkey update failed.\n");
2009 plog(ASL_LEVEL_DEBUG
, "pfkey update sent.\n");
2011 /* Do ADD for responder */
2012 if (pk_sendadd(iph2
) < 0) {
2013 plog(ASL_LEVEL_ERR
, "pfkey add failed.\n");
2016 plog(ASL_LEVEL_DEBUG
, "pfkey add sent.\n");
2019 * set policies into SPD if the policy is generated
2020 * from peer's policy.
2022 if (iph2
->spidx_gen
) {
2024 struct policyindex
*spidx
;
2025 struct sockaddr_storage addr
;
2027 struct sockaddr_storage
*src
= iph2
->src
;
2028 struct sockaddr_storage
*dst
= iph2
->dst
;
2030 /* make inbound policy */
2033 if (pk_sendspdupdate2(iph2
) < 0) {
2035 "pfkey spdupdate2(inbound) failed.\n");
2038 plog(ASL_LEVEL_DEBUG
,
2039 "pfkey spdupdate2(inbound) sent.\n");
2041 spidx
= iph2
->spidx_gen
;
2042 #ifdef HAVE_POLICY_FWD
2043 /* make forward policy if required */
2044 if (tunnel_mode_prop(iph2
->approval
)) {
2045 spidx
->dir
= IPSEC_DIR_FWD
;
2046 if (pk_sendspdupdate2(iph2
) < 0) {
2048 "pfkey spdupdate2(forward) failed.\n");
2051 plog(ASL_LEVEL_DEBUG
,
2052 "pfkey spdupdate2(forward) sent.\n");
2056 /* make outbound policy */
2059 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
2061 spidx
->src
= spidx
->dst
;
2063 pref
= spidx
->prefs
;
2064 spidx
->prefs
= spidx
->prefd
;
2065 spidx
->prefd
= pref
;
2067 if (pk_sendspdupdate2(iph2
) < 0) {
2069 "pfkey spdupdate2(outbound) failed.\n");
2072 plog(ASL_LEVEL_DEBUG
,
2073 "pfkey spdupdate2(outbound) sent.\n");
2075 /* spidx_gen is unnecessary any more */
2076 delsp_bothdir(iph2
->spidx_gen
);
2077 racoon_free(iph2
->spidx_gen
);
2078 iph2
->spidx_gen
= NULL
;
2079 iph2
->generated_spidx
=1;
2092 * create HASH, body (SA, NONCE) payload with isakmp header.
2095 quick_ir1mx(iph2
, body
, hash
)
2096 phase2_handle_t
*iph2
;
2097 vchar_t
*body
, *hash
;
2099 struct isakmp
*isakmp
;
2100 vchar_t
*buf
= NULL
, *new = NULL
;
2103 struct isakmp_gen
*gen
;
2104 int error
= ISAKMP_INTERNAL_ERROR
;
2106 /* create buffer for isakmp payload */
2107 tlen
= sizeof(*isakmp
)
2108 + sizeof(*gen
) + hash
->l
2110 buf
= vmalloc(tlen
);
2113 "failed to get buffer to send.\n");
2117 /* re-set encryption flag, for serurity. */
2118 iph2
->flags
|= ISAKMP_FLAG_E
;
2120 /* set isakmp header */
2121 p
= set_isakmp_header2(buf
, iph2
, ISAKMP_NPTYPE_HASH
);
2124 "failed to set ISAKMP header");
2128 /* add HASH payload */
2129 /* XXX is next type always SA ? */
2130 p
= set_isakmp_payload(p
, hash
, ISAKMP_NPTYPE_SA
);
2132 /* add body payload */
2133 memcpy(p
, body
->v
, body
->l
);
2135 #ifdef HAVE_PRINT_ISAKMP_C
2136 isakmp_printpacket(buf
, iph2
->ph1
->local
, iph2
->ph1
->remote
, 1);
2140 new = oakley_do_encrypt(iph2
->ph1
, buf
, iph2
->ivm
->ive
, iph2
->ivm
->iv
);
2143 "failed to encrypt packet");
2154 if (error
&& buf
!= NULL
) {
2163 * get remote's sainfo.
2164 * NOTE: this function is for responder.
2168 phase2_handle_t
*iph2
;
2170 vchar_t
*idsrc
= NULL
, *iddst
= NULL
;
2172 int error
= ISAKMP_INTERNAL_ERROR
;
2173 struct sainfo
*anonymous
= NULL
;
2175 if (iph2
->id
== NULL
) {
2176 switch (iph2
->src
->ss_family
) {
2178 prefixlen
= sizeof(struct in_addr
) << 3;
2181 prefixlen
= sizeof(struct in6_addr
) << 3;
2185 "invalid family: %d\n", iph2
->src
->ss_family
);
2188 idsrc
= ipsecdoi_sockaddr2id(iph2
->src
, prefixlen
,
2191 idsrc
= vdup(iph2
->id
);
2193 if (idsrc
== NULL
) {
2195 "failed to set ID for source.\n");
2199 if (iph2
->id_p
== NULL
) {
2200 switch (iph2
->dst
->ss_family
) {
2202 prefixlen
= sizeof(struct in_addr
) << 3;
2205 prefixlen
= sizeof(struct in6_addr
) << 3;
2209 "invalid family: %d\n", iph2
->dst
->ss_family
);
2212 iddst
= ipsecdoi_sockaddr2id(iph2
->dst
, prefixlen
,
2215 iddst
= vdup(iph2
->id_p
);
2217 if (iddst
== NULL
) {
2219 "failed to set ID for destination.\n");
2223 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 0);
2224 // track anonymous sainfo, because we'll try to find a better sainfo if this is a client
2225 if (iph2
->sainfo
&& iph2
->sainfo
->idsrc
== NULL
)
2226 anonymous
= iph2
->sainfo
;
2228 if (iph2
->sainfo
== NULL
||
2229 (anonymous
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2230 if ((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
)
2231 iph2
->sainfo
= getsainfo(idsrc
, iddst
, iph2
->ph1
->id_p
, 1);
2233 plog(ASL_LEVEL_DEBUG
,
2234 "get_sainfo_r case 1.\n");
2236 // still no sainfo (or anonymous): for client, fallback to sainfo used by a previous established phase2
2237 if (iph2
->sainfo
== NULL
||
2238 (iph2
->sainfo
->idsrc
== NULL
&& iph2
->parent_session
&& iph2
->parent_session
->is_client
)) {
2239 ike_session_get_sainfo_r(iph2
);
2241 plog(ASL_LEVEL_DEBUG
,
2242 "get_sainfo_r case 2.\n");
2244 // still no sainfo (or anonymous): fallback to sainfo picked by dst id
2245 if ((iph2
->sainfo
== NULL
|| iph2
->sainfo
->idsrc
== NULL
) && iph2
->id_p
) {
2246 plog(ASL_LEVEL_DEBUG
,
2247 "get_sainfo_r about to try dst id only.\n");
2248 iph2
->sainfo
= getsainfo_by_dst_id(iph2
->id_p
, iph2
->ph1
->id_p
);
2250 plog(ASL_LEVEL_DEBUG
,
2251 "get_sainfo_r case 3.\n");
2252 if (iph2
->sainfo
->idsrc
== NULL
)
2253 anonymous
= iph2
->sainfo
;
2258 if (iph2
->sainfo
== NULL
) {
2259 if (anonymous
== NULL
) {
2261 "failed to get sainfo.\n");
2264 iph2
->sainfo
= anonymous
;
2266 retain_sainfo(iph2
->sainfo
);
2268 #ifdef ENABLE_HYBRID
2269 /* xauth group inclusion check */
2270 if (iph2
->sainfo
->group
!= NULL
)
2271 if(group_check(iph2
->ph1
,&iph2
->sainfo
->group
->v
,1)) {
2273 "failed to group check");
2278 plog(ASL_LEVEL_DEBUG
,
2279 "selected sainfo: %s\n", sainfo2str(iph2
->sainfo
));
2292 get_proposal_r(iph2
)
2293 phase2_handle_t
*iph2
;
2295 int error
= get_proposal_r_remote(iph2
, 0);
2296 if (error
!= -2 && error
!= 0 &&
2297 (((iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) && lcconf
->ext_nat_id
!= NULL
) ||
2298 (iph2
->parent_session
&& iph2
->parent_session
->is_client
))) {
2299 if (iph2
->parent_session
&& iph2
->parent_session
->is_client
)
2300 error
= ike_session_get_proposal_r(iph2
);
2301 if (error
!= -2 && error
!= 0)
2302 error
= get_proposal_r_remote(iph2
, 1);
2308 * Copy both IP addresses in ID payloads into [src,dst]_id if both ID types
2309 * are IP address and same address family.
2310 * Then get remote's policy from SPD copied from kernel.
2311 * If the type of ID payload is address or subnet type, then the index is
2312 * made from the payload. If there is no ID payload, or the type of ID
2313 * payload is NOT address type, then the index is made from the address
2315 * NOTE: This function is only for responder.
2318 get_proposal_r_remote(iph2
, ignore_id
)
2319 phase2_handle_t
*iph2
;
2322 struct policyindex spidx
;
2323 struct secpolicy
*sp_in
, *sp_out
;
2324 int idi2type
= 0; /* switch whether copy IDs into id[src,dst]. */
2325 int error
= ISAKMP_INTERNAL_ERROR
;
2326 int generated_policy_exit_early
= 0;
2328 /* check the existence of ID payload */
2329 if ((iph2
->id_p
!= NULL
&& iph2
->id
== NULL
)
2330 || (iph2
->id_p
== NULL
&& iph2
->id
!= NULL
)) {
2332 "Both IDs wasn't found in payload.\n");
2333 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
2336 /* make sure if id[src,dst] is null (if use_remote_addr == 0). */
2337 if (!ignore_id
&& (iph2
->src_id
|| iph2
->dst_id
)) {
2339 "Why do ID[src,dst] exist already.\n");
2340 return ISAKMP_INTERNAL_ERROR
;
2343 plog(ASL_LEVEL_DEBUG
,
2344 "%s: ignore_id %x.\n", __FUNCTION__
, ignore_id
);
2346 memset(&spidx
, 0, sizeof(spidx
));
2348 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
2349 /* make a spidx; a key to search SPD */
2350 spidx
.dir
= IPSEC_DIR_INBOUND
;
2354 * make destination address in spidx from either ID payload
2355 * or phase 1 address into a address in spidx.
2356 * If behind a nat - use phase1 address because server's
2357 * use the nat's address in the ID payload.
2359 if (iph2
->id
!= NULL
2361 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2362 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
2363 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2364 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2365 /* get a destination address of a policy */
2366 error
= ipsecdoi_id2sockaddr(iph2
->id
, &spidx
.dst
,
2367 &spidx
.prefd
, &spidx
.ul_proto
, iph2
->version
);
2373 * get scopeid from the SA address.
2374 * note that the phase 1 source address is used as
2375 * a destination address to search for a inbound policy entry
2376 * because rcoon is responder.
2378 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
2379 error
= setscopeid(&spidx
.dst
, iph2
->src
);
2385 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
2386 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
2387 idi2type
= _XIDT(iph2
->id
);
2391 plog(ASL_LEVEL_DEBUG
,
2392 "Get a destination address of SP index "
2393 "from Phase 1 address "
2394 "due to no ID payloads found "
2395 "OR because ID type is not address.\n");
2398 * copy the SOURCE address of IKE into the DESTINATION address
2399 * of the key to search the SPD because the direction of policy
2402 memcpy(&spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
2403 switch (spidx
.dst
.ss_family
) {
2406 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.dst
;
2407 spidx
.prefd
= sizeof(struct in_addr
) << 3;
2408 s
->sin_port
= htons(0);
2413 spidx
.prefd
= sizeof(struct in6_addr
) << 3;
2422 /* make source address in spidx */
2423 if (iph2
->id_p
!= NULL
2425 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
2426 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
2427 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
2428 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
2429 /* get a source address of inbound SA */
2430 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &spidx
.src
,
2431 &spidx
.prefs
, &spidx
.ul_proto
, iph2
->version
);
2437 * get scopeid from the SA address.
2438 * for more detail, see above of this function.
2440 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
2441 error
= setscopeid(&spidx
.src
, iph2
->dst
);
2447 /* make id[src,dst] if both ID types are IP address and same */
2448 if (_XIDT(iph2
->id_p
) == idi2type
2449 && spidx
.dst
.ss_family
== spidx
.src
.ss_family
) {
2450 iph2
->src_id
= dupsaddr(&spidx
.dst
);
2451 if (iph2
->src_id
== NULL
) {
2453 "buffer allocation failed.\n");
2454 return ISAKMP_INTERNAL_ERROR
;
2456 iph2
->dst_id
= dupsaddr(&spidx
.src
);
2457 if (iph2
->dst_id
== NULL
) {
2459 "buffer allocation failed.\n");
2460 return ISAKMP_INTERNAL_ERROR
;
2465 plog(ASL_LEVEL_DEBUG
,
2466 "Get a source address of SP index "
2467 "from Phase 1 address "
2468 "due to no ID payloads found "
2469 "OR because ID type is not address.\n");
2471 /* see above comment. */
2472 memcpy(&spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
2473 switch (spidx
.src
.ss_family
) {
2476 struct sockaddr_in
*s
= (struct sockaddr_in
*)&spidx
.src
;
2477 spidx
.prefs
= sizeof(struct in_addr
) << 3;
2478 s
->sin_port
= htons(0);
2483 spidx
.prefs
= sizeof(struct in6_addr
) << 3;
2494 plog(ASL_LEVEL_DEBUG
,
2495 "get a src address from ID payload "
2496 "%s prefixlen=%u ul_proto=%u\n",
2497 saddr2str((struct sockaddr
*)&spidx
.src
),
2498 spidx
.prefs
, spidx
.ul_proto
);
2499 plog(ASL_LEVEL_DEBUG
,
2500 "get dst address from ID payload "
2501 "%s prefixlen=%u ul_proto=%u\n",
2502 saddr2str((struct sockaddr
*)&spidx
.dst
),
2503 spidx
.prefd
, spidx
.ul_proto
);
2506 * convert the ul_proto if it is 0
2507 * because 0 in ID payload means a wild card.
2509 if (spidx
.ul_proto
== 0)
2510 spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
2512 /* get inbound policy */
2513 sp_in
= getsp_r(&spidx
, iph2
);
2514 if (sp_in
== NULL
|| sp_in
->policy
== IPSEC_POLICY_GENERATE
) {
2515 if (iph2
->ph1
->rmconf
->gen_policy
) {
2517 plog(ASL_LEVEL_NOTICE
,
2518 "Update the generated policy : %s\n",
2521 plog(ASL_LEVEL_NOTICE
,
2523 "try to generate the policy : %s\n",
2525 iph2
->spidx_gen
= (struct policyindex
*)racoon_malloc(sizeof(spidx
));
2526 if (!iph2
->spidx_gen
) {
2528 "buffer allocation failed.\n");
2529 return ISAKMP_INTERNAL_ERROR
;
2531 memcpy(iph2
->spidx_gen
, &spidx
, sizeof(spidx
));
2532 generated_policy_exit_early
= 1; /* special value */
2535 "no policy found: %s\n", spidx2str(&spidx
));
2536 return ISAKMP_INTERNAL_ERROR
;
2540 /* get outbound policy */
2542 struct sockaddr_storage addr
;
2545 spidx
.dir
= IPSEC_DIR_OUTBOUND
;
2547 spidx
.src
= spidx
.dst
;
2550 spidx
.prefs
= spidx
.prefd
;
2553 sp_out
= getsp_r(&spidx
, iph2
);
2555 plog(ASL_LEVEL_WARNING
,
2556 "no outbound policy found: %s\n",
2561 iph2
->spid
= sp_out
->id
;
2566 plog(ASL_LEVEL_DEBUG
,
2567 "suitable SP found:%s\n", spidx2str(&spidx
));
2569 if (generated_policy_exit_early
) {
2574 * In the responder side, the inbound policy should be using IPsec.
2575 * outbound policy is not checked currently.
2577 if (sp_in
->policy
!= IPSEC_POLICY_IPSEC
) {
2579 "policy found, but no IPsec required: %s\n",
2581 return ISAKMP_INTERNAL_ERROR
;
2584 /* set new proposal derived from a policy into the iph2->proposal. */
2585 if (set_proposal_from_policy(iph2
, sp_in
, sp_out
) < 0) {
2587 "failed to create saprop.\n");
2588 return ISAKMP_INTERNAL_ERROR
;