1 /* $NetBSD: isakmp_inf.c,v 1.14.4.8 2007/08/01 11:52:20 vanhu Exp $ */
3 /* Id: isakmp_inf.c,v 1.44 2006/05/06 20:45:52 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
35 #include "racoon_types.h"
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
41 #include <net/pfkeyv2.h>
42 #include <netinet/in.h>
43 #include <sys/queue.h>
44 #ifndef HAVE_NETINET6_IPSEC
45 #include <netinet/ipsec.h>
47 #include <netinet6/ipsec.h>
54 #if TIME_WITH_SYS_TIME
55 # include <sys/time.h>
59 # include <sys/time.h>
79 #include "ike_session.h"
81 #include "localconf.h"
82 #include "remoteconf.h"
87 #include "isakmp_var.h"
90 #include "isakmp_xauth.h"
91 #include "isakmp_unity.h"
92 #include "isakmp_cfg.h"
94 #include "isakmp_inf.h"
96 #include "ipsec_doi.h"
97 #include "crypto_openssl.h"
100 #include "algorithm.h"
101 #include "proposal.h"
102 #include "strnames.h"
104 #include "nattraversal.h"
106 #include "vpn_control_var.h"
107 #include "vpn_control.h"
108 #include "ike_session.h"
109 #include "ipsecSessionTracer.h"
110 #include "ipsecMessageTracer.h"
112 /* information exchange */
113 static int isakmp_info_recv_n (phase1_handle_t
*, struct isakmp_pl_n
*, u_int32_t
, int);
114 static int isakmp_info_recv_d (phase1_handle_t
*, struct isakmp_pl_d
*, u_int32_t
, int);
117 static int isakmp_info_recv_r_u (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
118 static int isakmp_info_recv_r_u_ack (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
121 #ifdef ENABLE_VPNCONTROL_PORT
122 static int isakmp_info_recv_lb (phase1_handle_t
*, struct isakmp_pl_lb
*lb
, int);
126 isakmp_ph1_responder_lifetime (phase1_handle_t
*iph1
, struct isakmp_pl_resp_lifetime
*notify
)
130 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
132 "invalid spi_size in notification payload.\n");
135 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
137 plog(ASL_LEVEL_DEBUG
,
138 "notification message ISAKMP-SA RESPONDER-LIFETIME, "
139 "doi=%d proto_id=%d spi=%s(size=%d).\n",
140 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
144 struct isakmp_pl_attr
*attrpl
;
145 int len
= ntohs(notify
->h
.len
) - (sizeof(*notify
) + notify
->spi_size
);
147 attrpl
= (struct isakmp_pl_attr
*)((char *)(notify
+ 1) + notify
->spi_size
);
157 isakmp_ph2_responder_lifetime (phase2_handle_t
*iph2
, struct isakmp_pl_resp_lifetime
*notify
)
161 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
163 "invalid spi_size in notification payload.\n");
166 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
168 plog(ASL_LEVEL_DEBUG
,
169 "notification message IPSEC-SA RESPONDER-LIFETIME, "
170 "doi=%d proto_id=%d spi=%s(size=%d).\n",
171 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
180 * Information Exchange
183 * receive Information
186 isakmp_info_recv(phase1_handle_t
*iph1
, vchar_t
*msg0
)
189 vchar_t
*pbuf
= NULL
;
192 struct isakmp
*isakmp
;
193 struct isakmp_gen
*gen
;
194 struct isakmp_parse_t
*pa
;
196 vchar_t
*hash
, *payload
;
197 struct isakmp_gen
*nd
;
202 plog(ASL_LEVEL_DEBUG
, "receive Information.\n");
204 encrypted
= ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
);
205 msgid
= ((struct isakmp
*)msg0
->v
)->msgid
;
207 /* Use new IV to decrypt Informational message. */
209 struct isakmp_ivm
*ivm
;
211 if (iph1
->ivm
== NULL
) {
212 plog(ASL_LEVEL_ERR
, "iph1->ivm == NULL\n");
213 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
214 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
215 CONSTSTR("Information message"),
216 CONSTSTR("Failed to process Information Message (no IV)"));
221 ivm
= oakley_newiv2(iph1
, ((struct isakmp
*)msg0
->v
)->msgid
);
224 "failed to compute IV\n");
225 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
226 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
227 CONSTSTR("Information message"),
228 CONSTSTR("Failed to process Information Message (can't compute IV)"));
232 msg
= oakley_do_decrypt(iph1
, msg0
, ivm
->iv
, ivm
->ive
);
236 "failed to decrypt packet\n");
237 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
238 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
239 CONSTSTR("Information message"),
240 CONSTSTR("Failed to decrypt Information message"));
248 if (msg
->l
< sizeof(*isakmp
) + sizeof(*gen
)) {
250 "ignore information because the "
251 "message is way too short\n");
255 isakmp
= (struct isakmp
*)msg
->v
;
256 gen
= (struct isakmp_gen
*)((caddr_t
)isakmp
+ sizeof(struct isakmp
));
260 if (isakmp
->np
!= ISAKMP_NPTYPE_HASH
) {
262 "ignore information because the "
263 "message has no hash payload.\n");
267 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
268 (!iph1
->approval
|| !iph1
->skeyid_a
)) {
270 "ignore information because ISAKMP-SA "
271 "has not been established yet.\n");
276 if (msg
->l
< sizeof(*isakmp
) + ntohs(gen
->len
) + sizeof(*nd
)) {
278 "ignore information because the "
279 "message is too short\n");
283 p
= (caddr_t
) gen
+ sizeof(struct isakmp_gen
);
284 nd
= (struct isakmp_gen
*) ((caddr_t
) gen
+ ntohs(gen
->len
));
286 /* nd length check */
287 if (ntohs(nd
->len
) > msg
->l
- (sizeof(struct isakmp
) +
290 "too long payload length (broken message?)\n");
294 if (ntohs(nd
->len
) < sizeof(*nd
)) {
296 "too short payload length (broken message?)\n");
300 payload
= vmalloc(ntohs(nd
->len
));
301 if (payload
== NULL
) {
303 "cannot allocate memory\n");
307 memcpy(payload
->v
, (caddr_t
) nd
, ntohs(nd
->len
));
310 hash
= oakley_compute_hash1(iph1
, isakmp
->msgid
, payload
);
313 "cannot compute hash\n");
319 if (ntohs(gen
->len
) - sizeof(struct isakmp_gen
) != hash
->l
) {
321 "ignore information due to hash length mismatch\n");
328 if (memcmp(p
, hash
->v
, hash
->l
) != 0) {
330 "ignore information due to hash mismatch\n");
337 plog(ASL_LEVEL_DEBUG
, "hash validated.\n");
342 /* make sure phase 1 was not yet at encrypted state */
343 switch (iph1
->etype
) {
344 case ISAKMP_ETYPE_AGG
:
345 // %%%%% should also check for unity/mode cfg - last pkt is encrypted in such cases
346 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
347 ((iph1
->side
== INITIATOR
&& iph1
->status
== IKEV1_STATE_AGG_I_MSG3SENT
) ||
348 (iph1
->side
== RESPONDER
&& iph1
->status
== IKEV1_STATE_AGG_R_MSG3RCVD
))) {
351 case ISAKMP_ETYPE_IDENT
:
352 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
353 ((iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_IDENT_I_MSG5SENT
354 || iph1
->status
== IKEV1_STATE_IDENT_I_MSG6RCVD
)) ||
355 (iph1
->side
== RESPONDER
&& (iph1
->status
== IKEV1_STATE_IDENT_R_MSG5RCVD
)))) {
361 "%s message must be encrypted\n",
362 s_isakmp_nptype(np
));
368 if (!(pbuf
= isakmp_parse(msg
))) {
370 "failed to parse msg");
376 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
; pa
->type
; pa
++) { // Wcast-align fix (void*) - aligned buffer of aligned (unpacked) structs
378 case ISAKMP_NPTYPE_HASH
:
381 case ISAKMP_NPTYPE_N
:
382 error
= isakmp_info_recv_n(iph1
,
383 (struct isakmp_pl_n
*)pa
->ptr
,
386 case ISAKMP_NPTYPE_D
:
387 error
= isakmp_info_recv_d(iph1
,
388 (struct isakmp_pl_d
*)pa
->ptr
,
391 case ISAKMP_NPTYPE_NONCE
:
392 /* XXX to be 6.4.2 ike-01.txt */
393 /* XXX IV is to be synchronized. */
395 "ignore Acknowledged Informational\n");
398 /* don't send information, see isakmp_ident_r1() */
401 "reject the packet, "
402 "received unexpected payload type %s.\n",
403 s_isakmp_nptype(gen
->np
));
411 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
412 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
413 CONSTSTR("Information message"),
418 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
419 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
420 CONSTSTR("Information message"),
421 CONSTSTR("Failed to process Information Message"));
431 * handling of Notification payload
434 isakmp_info_recv_n(phase1_handle_t
*iph1
, struct isakmp_pl_n
*notify
, u_int32_t msgid
, int encrypted
)
442 type
= ntohs(notify
->type
);
445 case ISAKMP_NTYPE_CONNECTED
:
446 case ISAKMP_NTYPE_REPLAY_STATUS
:
448 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
452 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
454 return(isakmp_ph1_responder_lifetime(iph1
,
455 (struct isakmp_pl_resp_lifetime
*)notify
));
458 case ISAKMP_NTYPE_INITIAL_CONTACT
:
460 info_recv_initialcontact(iph1
);
465 case ISAKMP_NTYPE_R_U_THERE
:
467 return isakmp_info_recv_r_u(iph1
,
468 (struct isakmp_pl_ru
*)notify
, msgid
);
470 case ISAKMP_NTYPE_R_U_THERE_ACK
:
472 return isakmp_info_recv_r_u_ack(iph1
,
473 (struct isakmp_pl_ru
*)notify
, msgid
);
476 #ifdef ENABLE_VPNCONTROL_PORT
477 case ISAKMP_NTYPE_LOAD_BALANCE
:
478 isakmp_info_recv_lb(iph1
, (struct isakmp_pl_lb
*)notify
, encrypted
);
484 /* XXX there is a potential of dos attack. */
485 if(type
>= ISAKMP_NTYPE_MINERROR
&&
486 type
<= ISAKMP_NTYPE_MAXERROR
) {
488 /* don't think this realy deletes ph1 ? */
490 "Delete Phase 1 handle.\n");
493 if (ike_session_getph2bymsgid(iph1
, msgid
) == NULL
) {
495 "Fatal %s notify messsage, "
496 "Phase 1 should be deleted.\n",
497 s_isakmp_notify_msg(type
));
500 "Fatal %s notify messsage, "
501 "Phase 2 should be deleted.\n",
502 s_isakmp_notify_msg(type
));
507 "Unhandled notify message %s, "
508 "no Phase 2 handle found.\n",
509 s_isakmp_notify_msg(type
));
515 /* get spi if specified and allocate */
516 if(notify
->spi_size
> 0) {
517 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
519 "Invalid spi_size in notification payload.\n");
522 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
524 plog(ASL_LEVEL_DEBUG
,
525 "Notification message %d:%s, "
526 "doi=%d proto_id=%d spi=%s(size=%d).\n",
527 type
, s_isakmp_notify_msg(type
),
528 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
533 /* Send the message data to the logs */
534 if(type
>= ISAKMP_NTYPE_MINERROR
&&
535 type
<= ISAKMP_NTYPE_MAXERROR
) {
536 l
= ntohs(notify
->h
.len
) - sizeof(*notify
) - notify
->spi_size
;
538 nraw
= (char*)notify
;
539 nraw
+= sizeof(*notify
) + notify
->spi_size
;
540 if ((ndata
= vmalloc(l
)) != NULL
) {
541 memcpy(ndata
->v
, nraw
, ndata
->l
);
544 binsanitize(ndata
->v
, ndata
->l
));
548 "Cannot allocate memory\n");
555 #ifdef ENABLE_VPNCONTROL_PORT
557 isakmp_info_vpncontrol_notify_ike_failed (phase1_handle_t
*iph1
, int isakmp_info_initiator
, int type
, vchar_t
*data
)
560 u_int32_t fail_reason
;
562 /* notify the API that we have received the delete */
563 if (iph1
->remote
->ss_family
== AF_INET
)
564 address
= ((struct sockaddr_in
*)(iph1
->remote
))->sin_addr
.s_addr
;
568 if (isakmp_info_initiator
== FROM_REMOTE
) {
569 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
570 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
573 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
574 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is pre-mature\n");
575 } else if (expired
) {
576 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
577 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is expired\n");
581 vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
, 0, NULL
);
585 if (type
== ISAKMP_INTERNAL_ERROR
||
586 type
<= ISAKMP_NTYPE_UNEQUAL_PAYLOAD_LENGTHS
) {
587 int premature
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_PREMATURE
);
588 int expired
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_EXPIRED
);
589 int subjname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJNAME
);
590 int subjaltname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJALTNAME
);
593 fail_reason
= VPNCTL_NTYPE_PEER_CERT_PREMATURE
;
594 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is pre-mature\n");
595 } else if (expired
) {
596 fail_reason
= VPNCTL_NTYPE_PEER_CERT_EXPIRED
;
597 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is expired\n");
598 } else if (subjname
) {
599 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJNAME
;
600 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject name not valid\n");
601 } else if (subjaltname
) {
602 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJALTNAME
;
603 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject alternate name not valid\n");
607 (void)vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
,
608 (data
? data
->l
: 0), (u_int8_t
*)(data
? data
->v
: NULL
));
613 #endif /* ENABLE_VPNCONTROL_PORT */
616 * handling of Deletion payload
619 isakmp_info_recv_d(phase1_handle_t
*iph1
, struct isakmp_pl_d
*delete, u_int32_t msgid
, int encrypted
)
622 phase1_handle_t
*del_ph1
;
628 if (ntohl(delete->doi
) != IPSEC_DOI
) {
630 "delete payload with invalid doi:%d.\n",
634 * At deconnexion time, Cisco VPN client does this
635 * with a zero DOI. Don't give up in that situation.
637 if (((iph1
->mode_cfg
->flags
&
638 ISAKMP_CFG_VENDORID_UNITY
) == 0) || (delete->doi
!= 0))
645 num_spi
= ntohs(delete->num_spi
);
646 tlen
= ntohs(delete->h
.len
) - sizeof(struct isakmp_pl_d
);
648 if (tlen
!= num_spi
* delete->spi_size
) {
650 "deletion payload with invalid length.\n");
654 plog(ASL_LEVEL_DEBUG
,
655 "delete payload for protocol %s\n",
656 s_ipsecdoi_proto(delete->proto_id
));
658 if(!iph1
->rmconf
->weak_phase1_check
&& !encrypted
) {
659 plog(ASL_LEVEL_WARNING
,
660 "Ignoring unencrypted delete payload "
661 "(check the weak_phase1_check option)\n");
665 switch (delete->proto_id
) {
666 case IPSECDOI_PROTO_ISAKMP
:
667 if (delete->spi_size
!= sizeof(isakmp_index
)) {
669 "delete payload with strange spi "
670 "size %d(proto_id:%d)\n",
671 delete->spi_size
, delete->proto_id
);
675 del_ph1
= ike_session_getph1byindex(iph1
->parent_session
, (isakmp_index
*)(delete + 1));
678 // hack: start a rekey now, if one was pending (only for client).
679 if (del_ph1
->sce_rekey
&&
680 del_ph1
->parent_session
&&
681 del_ph1
->parent_session
->is_client
&&
682 del_ph1
->parent_session
->established
&&
683 !(del_ph1
->rmconf
->natt_multiple_user
&&
684 del_ph1
->parent_session
->is_l2tpvpn_ipsec
)) {
685 isakmp_ph1rekeyexpire(del_ph1
, FALSE
);
689 SCHED_KILL(del_ph1
->scr
);
692 * Do not delete IPsec SAs when receiving an IKE delete notification.
693 * Just delete the IKE SA.
695 #ifdef ENABLE_VPNCONTROL_PORT
696 if (del_ph1
->started_by_api
|| (del_ph1
->is_rekey
&& del_ph1
->parent_session
&& del_ph1
->parent_session
->is_client
)) {
697 if (ike_session_islast_ph1(del_ph1
)) {
698 isakmp_info_vpncontrol_notify_ike_failed(del_ph1
, FROM_REMOTE
, VPNCTL_NTYPE_PH1_DELETE
, NULL
);
702 if (del_ph1
->rmconf
->natt_multiple_user
&&
703 del_ph1
->parent_session
->is_l2tpvpn_ipsec
) {
704 plog(ASL_LEVEL_DEBUG
, "Ignoring IKE delete from peer for L2TP server\n");
707 isakmp_ph1expire(del_ph1
);
711 case IPSECDOI_PROTO_IPSEC_AH
:
712 case IPSECDOI_PROTO_IPSEC_ESP
:
713 if (delete->spi_size
!= sizeof(u_int32_t
)) {
715 "delete payload with strange spi "
716 "size %d(proto_id:%d)\n",
717 delete->spi_size
, delete->proto_id
);
720 if (iph1
->rmconf
->natt_multiple_user
&&
721 iph1
->parent_session
->is_l2tpvpn_ipsec
) {
722 uint32_t *ph2_spi
= ALIGNED_CAST(u_int32_t
*)(delete + 1);
723 phase2_handle_t
*iph2
= ike_session_getph2bysaidx(iph1
->local
, iph1
->remote
, delete->proto_id
, ph2_spi
[0]);
726 iph2
->is_defunct
= 1;
727 plog(ASL_LEVEL_DEBUG
, "Ignoring SA delete from peer for L2TP server\n");
731 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
732 ALIGNED_CAST(u_int32_t
*)(delete + 1), num_spi
, NULL
, NULL
); // Wcast-align fix (void*) - delete payload is aligned
735 case IPSECDOI_PROTO_IPCOMP
:
736 /* need to handle both 16bit/32bit SPI */
737 memset(&spi
, 0, sizeof(spi
));
738 if (delete->spi_size
== sizeof(spi
.spi16
[1])) {
739 memcpy(&spi
.spi16
[1], delete + 1,
740 sizeof(spi
.spi16
[1]));
741 } else if (delete->spi_size
== sizeof(spi
.spi32
))
742 memcpy(&spi
.spi32
, delete + 1, sizeof(spi
.spi32
));
745 "delete payload with strange spi "
746 "size %d(proto_id:%d)\n",
747 delete->spi_size
, delete->proto_id
);
750 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
751 &spi
.spi32
, num_spi
, NULL
, NULL
);
756 "deletion message received, "
757 "invalid proto_id: %d\n",
762 plog(ASL_LEVEL_DEBUG
, "purged SAs.\n");
768 * send Delete payload (for ISAKMP SA) in Informational exchange.
771 isakmp_info_send_d1(phase1_handle_t
*iph1
)
773 struct isakmp_pl_d
*d
;
774 vchar_t
*payload
= NULL
;
778 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
))
781 /* create delete payload */
783 /* send SPIs of inbound SAs. */
784 /* XXX should send outbound SAs's ? */
785 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
786 payload
= vmalloc(tlen
);
787 if (payload
== NULL
) {
789 "failed to get buffer for payload.\n");
793 d
= (struct isakmp_pl_d
*)payload
->v
;
794 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
795 d
->h
.len
= htons(tlen
);
796 d
->doi
= htonl(IPSEC_DOI
);
797 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
798 d
->spi_size
= sizeof(isakmp_index
);
799 d
->num_spi
= htons(1);
800 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
802 error
= isakmp_info_send_common(iph1
, payload
,
806 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
807 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
808 CONSTSTR("Delete ISAKMP-SA"),
809 CONSTSTR("Failed to transmit Delete-ISAKMP-SA message"));
811 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
812 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
813 CONSTSTR("Delete ISAKMP-SA"),
821 * send Delete payload (for IPsec SA) in Informational exchange, based on
822 * pfkey msg. It sends always single SPI.
825 isakmp_info_send_d2(phase2_handle_t
*iph2
)
827 phase1_handle_t
*iph1
;
829 struct isakmp_pl_d
*d
;
830 vchar_t
*payload
= NULL
;
835 if (!FSM_STATE_IS_ESTABLISHED(iph2
->status
))
839 * don't send delete information if there is no phase 1 handler.
840 * It's nonsensical to negotiate phase 1 to send the information.
842 iph1
= ike_session_get_established_ph1(iph2
->parent_session
);
844 iph1
= ike_session_getph1byaddr(iph2
->parent_session
, iph2
->src
, iph2
->dst
);
847 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
848 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
849 CONSTSTR("Information message"),
850 CONSTSTR("Failed to transmit Information message"));
851 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
852 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
853 CONSTSTR("Delete IPSEC-SA"),
854 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
855 plog(ASL_LEVEL_DEBUG
,
856 "No ph1 handler found, could not send DELETE_SA\n");
860 /* create delete payload */
861 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
863 /* send SPIs of inbound SAs. */
865 * XXX should I send outbound SAs's ?
866 * I send inbound SAs's SPI only at the moment because I can't
867 * decode any more if peer send encoded packet without aware of
868 * deletion of SA. Outbound SAs don't come under the situation.
870 tlen
= sizeof(*d
) + pr
->spisize
;
871 payload
= vmalloc(tlen
);
872 if (payload
== NULL
) {
873 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
874 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
875 CONSTSTR("Information message"),
876 CONSTSTR("Failed to transmit Information message"));
877 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
878 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
879 CONSTSTR("Delete IPSEC-SA"),
880 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
882 "failed to get buffer for payload.\n");
886 d
= (struct isakmp_pl_d
*)payload
->v
;
887 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
888 d
->h
.len
= htons(tlen
);
889 d
->doi
= htonl(IPSEC_DOI
);
890 d
->proto_id
= pr
->proto_id
;
891 d
->spi_size
= pr
->spisize
;
892 d
->num_spi
= htons(1);
894 * XXX SPI bits are left-filled, for use with IPComp.
895 * we should be switching to variable-length spi field...
897 spi
= (u_int8_t
*)&pr
->spi
;
898 spi
+= sizeof(pr
->spi
);
900 memcpy(d
+ 1, spi
, pr
->spisize
);
902 error
= isakmp_info_send_common(iph1
, payload
,
906 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
907 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
908 CONSTSTR("Delete IPSEC-SA"),
909 CONSTSTR("Failed to transmit Delete-IPSEC-SA"));
911 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
912 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
913 CONSTSTR("Delete IPSEC-SA"),
922 * send Notification payload (without ISAKMP SA) in an Informational exchange
925 isakmp_info_send_nx(struct isakmp
*isakmp
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
,
926 int type
, vchar_t
*data
)
928 phase1_handle_t
*iph1
= NULL
;
929 struct remoteconf
*rmconf
;
930 vchar_t
*payload
= NULL
;
933 struct isakmp_pl_n
*n
;
934 int spisiz
= 0; /* see below */
935 ike_session_t
*sess
= ike_session_get_session(local
, remote
, FALSE
, NULL
);
937 /* search appropreate configuration */
938 rmconf
= getrmconf(remote
);
939 if (rmconf
== NULL
) {
940 IPSECSESSIONTRACEREVENT(sess
,
941 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
942 CONSTSTR("Information message"),
943 CONSTSTR("Failed to transmit Information message (no remote configuration)"));
945 "no configuration found for peer address.\n");
949 /* add new entry to isakmp status table. */
950 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
952 IPSECSESSIONTRACEREVENT(sess
,
953 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
954 CONSTSTR("Information message"),
955 CONSTSTR("Failed to transmit Information message (no new Phase 1)"));
957 "failed to allocate ph1");
961 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
962 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
963 fsm_set_state(&iph1
->status
, IKEV1_STATE_INFO
);
964 iph1
->rmconf
= rmconf
;
965 retain_rmconf(iph1
->rmconf
);
966 iph1
->side
= INITIATOR
;
967 iph1
->version
= isakmp
->v
;
969 iph1
->msgid
= 0; /* XXX */
971 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
978 iph1
->frag_chain
= NULL
;
981 /* copy remote address */
982 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
983 IPSECSESSIONTRACEREVENT(sess
,
984 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
985 CONSTSTR("Information message"),
986 CONSTSTR("Failed to transmit Information Message (can't copy Phase 1 addresses)"));
988 "failed to copy ph1 addresses");
990 iph1
= NULL
; /* deleted in copy_ph1addresses */
994 tlen
= sizeof(*n
) + spisiz
;
997 payload
= vmalloc(tlen
);
998 if (payload
== NULL
) {
999 IPSECSESSIONTRACEREVENT(sess
,
1000 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1001 CONSTSTR("Information message"),
1002 CONSTSTR("Failed to transmit Information Message (can't allocate payload)"));
1004 "failed to get buffer to send.\n");
1009 n
= (struct isakmp_pl_n
*)payload
->v
;
1010 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1011 n
->h
.len
= htons(tlen
);
1012 n
->doi
= htonl(IPSEC_DOI
);
1013 n
->proto_id
= IPSECDOI_KEY_IKE
;
1014 n
->spi_size
= spisiz
;
1015 n
->type
= htons(type
);
1017 memset(n
+ 1, 0, spisiz
); /* XXX spisiz is always 0 */
1019 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1021 #ifdef ENABLE_VPNCONTROL_PORT
1022 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1024 if (ike_session_link_phase1(sess
, iph1
))
1027 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
1030 IPSECSESSIONTRACEREVENT(sess
,
1031 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1032 CONSTSTR("Without ISAKMP-SA"),
1033 CONSTSTR("Failed to transmit Without-ISAKMP-SA message"));
1035 IPSECSESSIONTRACEREVENT(sess
,
1036 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1037 CONSTSTR("Without ISAKMP-SA"),
1043 ike_session_unlink_phase1(iph1
);
1049 * send Notification payload (with ISAKMP SA) in an Informational exchange
1052 isakmp_info_send_n1(phase1_handle_t
*iph1
, int type
, vchar_t
*data
)
1054 vchar_t
*payload
= NULL
;
1057 struct isakmp_pl_n
*n
;
1061 * note on SPI size: which description is correct? I have chosen
1064 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
1065 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
1066 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
1067 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
1068 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
1070 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
||
1071 type
== ISAKMP_NTYPE_LOAD_BALANCE
)
1072 spisiz
= sizeof(isakmp_index
);
1076 tlen
= sizeof(*n
) + spisiz
;
1079 payload
= vmalloc(tlen
);
1080 if (payload
== NULL
) {
1081 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1082 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1083 CONSTSTR("ISAKMP-SA"),
1084 CONSTSTR("Failed to transmit ISAKMP-SA message (can't allocate payload)"));
1086 "failed to get buffer to send.\n");
1090 n
= (struct isakmp_pl_n
*)payload
->v
;
1091 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1092 n
->h
.len
= htons(tlen
);
1093 n
->doi
= htonl(iph1
->rmconf
->doitype
);
1094 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
1095 n
->spi_size
= spisiz
;
1096 n
->type
= htons(type
);
1098 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
1100 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1102 #ifdef ENABLE_VPNCONTROL_PORT
1103 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1106 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
1109 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1110 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1111 CONSTSTR("ISAKMP-SA"),
1112 CONSTSTR("Can't transmit ISAKMP-SA message"));
1114 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1115 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1116 CONSTSTR("ISAKMP-SA"),
1124 * send Notification payload (with IPsec SA) in an Informational exchange
1127 isakmp_info_send_n2(phase2_handle_t
*iph2
, int type
, vchar_t
*data
)
1129 phase1_handle_t
*iph1
= iph2
->ph1
;
1130 vchar_t
*payload
= NULL
;
1133 struct isakmp_pl_n
*n
;
1136 if (!iph2
->approval
)
1139 pr
= iph2
->approval
->head
;
1141 /* XXX must be get proper spi */
1142 tlen
= sizeof(*n
) + pr
->spisize
;
1145 payload
= vmalloc(tlen
);
1146 if (payload
== NULL
) {
1147 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1148 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1149 CONSTSTR("IPSEC-SA"),
1150 CONSTSTR("Failed to transmit IPSEC-SA message (can't allocate payload)"));
1152 "failed to get buffer to send.\n");
1156 n
= (struct isakmp_pl_n
*)payload
->v
;
1157 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1158 n
->h
.len
= htons(tlen
);
1159 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1160 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1161 n
->spi_size
= pr
->spisize
;
1162 n
->type
= htons(type
);
1163 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign
1165 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1167 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
1168 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
1171 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1172 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1173 CONSTSTR("IPSEC-SA"),
1174 CONSTSTR("Failed to transmit IPSEC-SA message"));
1176 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1177 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1178 CONSTSTR("IPSEC-SA"),
1187 * When ph1->skeyid_a == NULL, send message without encoding.
1190 isakmp_info_send_common(phase1_handle_t
*iph1
, vchar_t
*payload
, u_int32_t np
, int flags
)
1192 phase2_handle_t
*iph2
= NULL
;
1193 vchar_t
*hash
= NULL
;
1194 struct isakmp
*isakmp
;
1195 struct isakmp_gen
*gen
;
1200 /* add new entry to isakmp status table */
1201 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_INFO
);
1204 "failed to allocate ph2");
1208 iph2
->dst
= dupsaddr(iph1
->remote
);
1209 if (iph2
->dst
== NULL
) {
1211 "failed to duplicate remote address");
1212 ike_session_delph2(iph2
);
1215 iph2
->src
= dupsaddr(iph1
->local
);
1216 if (iph2
->src
== NULL
) {
1218 "failed to duplicate local address");
1219 ike_session_delph2(iph2
);
1222 switch (iph1
->remote
->ss_family
) {
1224 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1225 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1226 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1231 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1232 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1233 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1239 "invalid family: %d\n", iph1
->remote
->ss_family
);
1240 ike_session_delph2(iph2
);
1243 iph2
->side
= INITIATOR
;
1244 fsm_set_state(&iph2
->status
, IKEV1_STATE_INFO
);
1245 iph2
->msgid
= isakmp_newmsgid2(iph1
);
1247 /* get IV and HASH(1) if skeyid_a was generated. */
1248 if (iph1
->skeyid_a
!= NULL
) {
1249 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1250 if (iph2
->ivm
== NULL
) {
1252 "failed to generate IV");
1253 ike_session_delph2(iph2
);
1257 /* generate HASH(1) */
1258 hash
= oakley_compute_hash1(iph1
, iph2
->msgid
, payload
);
1261 "failed to generate HASH");
1262 ike_session_delph2(iph2
);
1266 /* initialized total buffer length */
1268 tlen
+= sizeof(*gen
);
1270 /* IKE-SA is not established */
1273 /* initialized total buffer length */
1276 if ((flags
& ISAKMP_FLAG_A
) == 0)
1277 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
1279 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
1281 ike_session_link_ph2_to_ph1(iph1
, iph2
);
1283 tlen
+= sizeof(*isakmp
) + payload
->l
;
1285 /* create buffer for isakmp payload */
1286 iph2
->sendbuf
= vmalloc(tlen
);
1287 if (iph2
->sendbuf
== NULL
) {
1289 "failed to get buffer to send.\n");
1293 /* create isakmp header */
1294 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
1295 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1296 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1297 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
1298 isakmp
->v
= iph1
->version
;
1299 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
1300 isakmp
->flags
= iph2
->flags
;
1301 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
1302 isakmp
->len
= htonl(tlen
);
1303 p
= (char *)(isakmp
+ 1);
1305 /* create HASH payload */
1307 gen
= (struct isakmp_gen
*)p
;
1308 gen
->np
= np
& 0xff;
1309 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
1311 memcpy(p
, hash
->v
, hash
->l
);
1316 memcpy(p
, payload
->v
, payload
->l
);
1319 #ifdef HAVE_PRINT_ISAKMP_C
1320 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1324 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
1327 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
1329 VPTRINIT(iph2
->sendbuf
);
1332 "failed to encrypt packet");
1335 iph2
->sendbuf
= tmp
;
1338 /* HDR*, HASH(1), N */
1339 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
1341 "failed to send packet");
1342 VPTRINIT(iph2
->sendbuf
);
1346 plog(ASL_LEVEL_DEBUG
,
1347 "sendto Information %s.\n", s_isakmp_nptype(np
));
1350 * don't resend notify message because peer can use Acknowledged
1351 * Informational if peer requires the reply of the notify message.
1354 /* XXX If Acknowledged Informational required, don't delete ph2handle */
1356 VPTRINIT(iph2
->sendbuf
);
1357 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1358 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1359 CONSTSTR("Information message"),
1366 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1367 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1368 CONSTSTR("Information message"),
1369 CONSTSTR("Failed to transmit Information message"));
1376 ike_session_unlink_phase2(iph2
);
1381 * add a notify payload to buffer by reallocating buffer.
1382 * If buf == NULL, the function only create a notify payload.
1384 * XXX Which is SPI to be included, inbound or outbound ?
1387 isakmp_add_pl_n(vchar_t
*buf0
, u_int8_t
**np_p
, int type
, struct saproto
*pr
, vchar_t
*data
)
1389 vchar_t
*buf
= NULL
;
1390 struct isakmp_pl_n
*n
;
1395 **np_p
= ISAKMP_NPTYPE_N
;
1397 tlen
= sizeof(*n
) + pr
->spisize
;
1403 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
1405 buf
= vmalloc(tlen
);
1408 "failed to get a payload buffer.\n");
1412 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
1413 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1414 n
->h
.len
= htons(tlen
);
1415 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1416 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1417 n
->spi_size
= pr
->spisize
;
1418 n
->type
= htons(type
);
1419 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign with cast
1421 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1423 /* save the pointer of next payload type */
1431 purge_ipsec_spi(struct sockaddr_storage
*dst0
, int proto
, u_int32_t
*spi
/*network byteorder*/, size_t n
, u_int32_t
*inbound_spi
, size_t *max_inbound_spi
)
1433 vchar_t
*buf
= NULL
;
1434 struct sadb_msg
*msg
, *next
, *end
;
1436 struct sadb_lifetime
*lt
;
1437 struct sockaddr_storage
*src
, *dst
;
1438 phase2_handle_t
*iph2
;
1441 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1443 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
1445 plog(ASL_LEVEL_DEBUG
,
1446 "pfkey_dump_sadb returned nothing.\n");
1450 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1451 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1454 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1456 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1457 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1462 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1464 "pfkey_check (%s)\n", ipsec_strerror());
1469 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1471 || !mhp
[SADB_EXT_ADDRESS_SRC
]
1472 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
1476 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1477 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1478 lt
= ALIGNED_CAST(struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
1480 created
= lt
->sadb_lifetime_addtime
;
1484 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1485 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1490 /* XXX n^2 algorithm, inefficient */
1492 /* don't delete inbound SAs at the moment (just save them in inbound_spi) */
1493 /* XXX should we remove SAs with opposite direction as well? */
1494 if (CMPSADDR2(dst0
, dst
)) {
1499 for (i
= 0; i
< n
; i
++) {
1502 if (spi
[i
] != sa
->sadb_sa_spi
)
1506 * delete a relative phase 2 handler.
1507 * continue to process if no relative phase 2 handler
1510 if (inbound_spi
&& max_inbound_spi
&& j
< *max_inbound_spi
) {
1511 i_spi
= &inbound_spi
[j
];
1515 iph2
= ike_session_getph2bysaidx2(src
, dst
, proto
, spi
[i
], i_spi
);
1517 pfkey_send_delete(lcconf
->sock_pfkey
,
1518 msg
->sadb_msg_satype
,
1520 src
, dst
, sa
->sadb_sa_spi
);
1524 ike_session_unlink_phase2(iph2
);
1530 plog(ASL_LEVEL_INFO
, "Purged IPsec-SA proto_id=%s spi=%u.\n",
1531 s_ipsecdoi_proto(proto
),
1538 if (max_inbound_spi
) {
1539 *max_inbound_spi
= j
;
1547 * delete all phase2 sa relatived to the destination address.
1548 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1549 * an INITIAL-CONTACT if we have contacted the peer. This matches the
1550 * Sun IKE behavior, and makes rekeying work much better when the peer
1554 info_recv_initialcontact(phase1_handle_t
*iph1
)
1556 vchar_t
*buf
= NULL
;
1557 struct sadb_msg
*msg
, *next
, *end
;
1559 struct sockaddr_storage
*src
, *dst
;
1560 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1562 phase2_handle_t
*iph2
;
1570 // TODO: make sure that is_rekey is cleared for this. and session indicates the same
1572 loc
= racoon_strdup(saddrwop2str(iph1
->local
));
1573 rem
= racoon_strdup(saddrwop2str(iph1
->remote
));
1578 * Purge all IPSEC-SAs for the peer. We can do this
1579 * the easy way (using a PF_KEY SADB_DELETE extension)
1580 * or we can do it the hard way.
1582 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1583 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
1585 plog(ASL_LEVEL_INFO
,
1586 "purging %s SAs for %s -> %s\n",
1587 pfkey_satypes
[i
].ps_name
, loc
, rem
);
1588 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1589 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1590 iph1
->local
, iph1
->remote
) == -1) {
1592 "delete_all %s -> %s failed for %s (%s)\n",
1594 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1598 ike_session_deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
1600 plog(ASL_LEVEL_INFO
,
1601 "purging %s SAs for %s -> %s\n",
1602 pfkey_satypes
[i
].ps_name
, rem
, loc
);
1603 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1604 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1605 iph1
->remote
, iph1
->local
) == -1) {
1607 "delete_all %s -> %s failed for %s (%s)\n",
1609 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1613 ike_session_deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1625 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1627 plog(ASL_LEVEL_DEBUG
,
1628 "pfkey_dump_sadb returned nothing.\n");
1632 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1633 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1636 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1638 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1639 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1644 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1646 "pfkey_check (%s)\n", ipsec_strerror());
1651 if (mhp
[SADB_EXT_SA
] == NULL
1652 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1653 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1657 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
]; // Wcast-align fix (void*) - buffer of pointers to aligned structs
1658 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1659 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1661 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1662 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1668 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1669 * announces the sender of the message was rebooted.
1670 * it is interpreted to delete all SAs which source address
1671 * is the sender of the message.
1672 * racoon only deletes SA which is matched both the
1673 * source address and the destination accress.
1677 * XXX RFC 3947 says that whe MUST NOT use IP+port to find old SAs
1680 if(iph1
->natt_flags
& NAT_DETECTED
){
1681 if (CMPSADDR(iph1
->local
, src
) == 0 &&
1682 CMPSADDR(iph1
->remote
, dst
) == 0)
1684 else if (CMPSADDR(iph1
->remote
, src
) == 0 &&
1685 CMPSADDR(iph1
->local
, dst
) == 0)
1693 /* If there is no NAT-T, we don't have to check addr + port...
1694 * XXX what about a configuration with a remote peers which is not
1695 * NATed, but which NATs some other peers ?
1696 * Here, the INITIAl-CONTACT would also flush all those NATed peers !!
1698 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1699 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1701 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1702 cmpsaddrwop(iph1
->local
, dst
) == 0)
1710 * Make sure this is an SATYPE that we manage.
1711 * This is gross; too bad we couldn't do it the
1714 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1715 if (pfkey_satypes
[i
].ps_satype
==
1716 msg
->sadb_msg_satype
)
1719 if (i
== pfkey_nsatypes
) {
1724 plog(ASL_LEVEL_INFO
,
1725 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1726 pfkey_send_delete(lcconf
->sock_pfkey
,
1727 msg
->sadb_msg_satype
,
1728 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1731 * delete a relative phase 2 handler.
1732 * continue to process if no relative phase 2 handler
1735 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1736 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1739 ike_session_unlink_phase2(iph2
);
1749 isakmp_check_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase1_handle_t
*iph1
)
1751 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1753 plog(ASL_LEVEL_DEBUG
,
1754 "Notify Message received\n");
1756 switch (ntohs(notify
->type
)) {
1757 case ISAKMP_NTYPE_CONNECTED
:
1758 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1759 case ISAKMP_NTYPE_REPLAY_STATUS
:
1760 case ISAKMP_NTYPE_HEARTBEAT
:
1761 #ifdef ENABLE_HYBRID
1762 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1764 plog(ASL_LEVEL_WARNING
,
1765 "Ignore %s notification.\n",
1766 s_isakmp_notify_msg(ntohs(notify
->type
)));
1768 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1769 plog(ASL_LEVEL_WARNING
,
1770 "Ignore INITIAL-CONTACT notification, "
1771 "because it is only accepted after Phase 1.\n");
1773 case ISAKMP_NTYPE_LOAD_BALANCE
:
1774 plog(ASL_LEVEL_WARNING
,
1775 "Ignore LOAD-BALANCE notification, "
1776 "because it is only accepted after Phase 1.\n");
1779 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1781 "Received unknown notification type %s.\n",
1782 s_isakmp_notify_msg(ntohs(notify
->type
)));
1789 isakmp_check_ph2_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase2_handle_t
*iph2
)
1791 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1793 plog(ASL_LEVEL_DEBUG
,
1794 "Phase 2 Notify Message received\n");
1796 switch (ntohs(notify
->type
)) {
1797 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1798 return((void)isakmp_ph2_responder_lifetime(iph2
,
1799 (struct isakmp_pl_resp_lifetime
*)notify
));
1801 case ISAKMP_NTYPE_CONNECTED
:
1802 case ISAKMP_NTYPE_REPLAY_STATUS
:
1803 case ISAKMP_NTYPE_HEARTBEAT
:
1804 #ifdef ENABLE_HYBRID
1805 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1807 plog(ASL_LEVEL_WARNING
,
1808 "Ignore %s notification.\n",
1809 s_isakmp_notify_msg(ntohs(notify
->type
)));
1811 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1812 plog(ASL_LEVEL_WARNING
,
1813 "Ignore INITIAL-CONTACT notification, "
1814 "because it is only accepted after Phase 1.\n");
1816 case ISAKMP_NTYPE_LOAD_BALANCE
:
1817 plog(ASL_LEVEL_WARNING
,
1818 "Ignore LOAD-BALANCE notification, "
1819 "because it is only accepted after Phase 1.\n");
1822 isakmp_info_send_n1(iph2
->ph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1824 "Received unknown notification type %s.\n",
1825 s_isakmp_notify_msg(ntohs(notify
->type
)));
1831 #ifdef ENABLE_VPNCONTROL_PORT
1833 isakmp_info_recv_lb(phase1_handle_t
*iph1
, struct isakmp_pl_lb
*n
, int encrypted
)
1836 if (iph1
->side
!= INITIATOR
)
1838 plog(ASL_LEVEL_DEBUG
,
1839 "LOAD-BALANCE notification ignored - we are not the initiator.\n");
1842 if (iph1
->remote
->ss_family
!= AF_INET
) {
1843 plog(ASL_LEVEL_DEBUG
,
1844 "LOAD-BALANCE notification ignored - only supported for IPv4.\n");
1848 plog(ASL_LEVEL_DEBUG
,
1849 "LOAD-BALANCE notification ignored - not protected.\n");
1852 if (ntohs(n
->h
.len
) != sizeof(struct isakmp_pl_lb
)) {
1853 plog(ASL_LEVEL_DEBUG
,
1854 "Invalid length of payload\n");
1857 vpncontrol_notify_ike_failed(ISAKMP_NTYPE_LOAD_BALANCE
, FROM_REMOTE
,
1858 ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
, 4, (u_int8_t
*)(&(n
->address
)));
1860 plog(ASL_LEVEL_NOTICE
,
1861 "Received LOAD_BALANCE notification.\n");
1863 if (((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
!= ntohl(n
->address
)) {
1864 plog(ASL_LEVEL_DEBUG
,
1865 "Deleting old Phase 1 because of LOAD_BALANCE notification - redirect address=%x.\n",
1868 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1869 isakmp_info_send_d1(iph1
);
1871 isakmp_ph1expire(iph1
);
1880 isakmp_info_recv_r_u (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1882 struct isakmp_pl_ru
*ru_ack
;
1883 vchar_t
*payload
= NULL
;
1887 plog(ASL_LEVEL_DEBUG
,
1888 "DPD R-U-There received\n");
1890 /* XXX should compare cookies with iph1->index?
1891 Or is this already done by calling function? */
1892 tlen
= sizeof(*ru_ack
);
1893 payload
= vmalloc(tlen
);
1894 if (payload
== NULL
) {
1895 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1896 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1897 CONSTSTR("R-U-THERE? ACK"),
1898 CONSTSTR("Failed to transmit DPD response"));
1900 "failed to get buffer to send.\n");
1904 ru_ack
= (struct isakmp_pl_ru
*)payload
->v
;
1905 ru_ack
->h
.np
= ISAKMP_NPTYPE_NONE
;
1906 ru_ack
->h
.len
= htons(tlen
);
1907 ru_ack
->doi
= htonl(IPSEC_DOI
);
1908 ru_ack
->type
= htons(ISAKMP_NTYPE_R_U_THERE_ACK
);
1909 ru_ack
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ? */
1910 ru_ack
->spi_size
= sizeof(isakmp_index
);
1911 memcpy(ru_ack
->i_ck
, ru
->i_ck
, sizeof(cookie_t
));
1912 memcpy(ru_ack
->r_ck
, ru
->r_ck
, sizeof(cookie_t
));
1913 ru_ack
->data
= ru
->data
;
1915 /* XXX Should we do FLAG_A ? */
1916 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
,
1920 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1921 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1922 CONSTSTR("R-U-THERE? ACK"),
1923 CONSTSTR("Failed to transmit DPD ack"));
1925 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1926 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1927 CONSTSTR("R-U-THERE? ACK"),
1931 plog(ASL_LEVEL_DEBUG
, "received a valid R-U-THERE, ACK sent\n");
1933 /* Should we mark tunnel as active ? */
1938 isakmp_info_recv_r_u_ack (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1941 plog(ASL_LEVEL_DEBUG
,
1942 "DPD R-U-There-Ack received\n");
1944 /* XXX Maintain window of acceptable sequence numbers ?
1945 * => ru->data <= iph2->dpd_seq &&
1946 * ru->data >= iph2->dpd_seq - iph2->dpd_fails ? */
1947 if (ntohl(ru
->data
) != iph1
->dpd_seq
) {
1949 "Wrong DPD sequence number (%d, %d expected).\n",
1950 ntohl(ru
->data
), iph1
->dpd_seq
);
1954 if (memcmp(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
)) ||
1955 memcmp(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
))) {
1957 "Cookie mismatch in DPD ACK!.\n");
1961 iph1
->dpd_fails
= 0;
1966 iph1
->dpd_lastack
= time(NULL
);
1968 SCHED_KILL(iph1
->dpd_r_u
);
1970 isakmp_sched_r_u(iph1
, 0);
1972 if (iph1
->side
== INITIATOR
) {
1973 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1974 IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RESP
,
1975 CONSTSTR("Initiator DPD Response"),
1978 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1979 IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RESP
,
1980 CONSTSTR("Responder DPD Response"),
1983 plog(ASL_LEVEL_DEBUG
, "received an R-U-THERE-ACK\n");
1985 #ifdef ENABLE_VPNCONTROL_PORT
1986 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1987 #endif /* ENABLE_VPNCONTROL_PORT */
1994 * send Delete payload (for ISAKMP SA) in Informational exchange.
1997 isakmp_info_send_r_u(void *arg
)
1999 phase1_handle_t
*iph1
= arg
;
2001 /* create R-U-THERE payload */
2002 struct isakmp_pl_ru
*ru
;
2003 vchar_t
*payload
= NULL
;
2007 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2008 plog(ASL_LEVEL_DEBUG
, "DPD r-u send aborted, invalid Phase 1 status %d....\n",
2013 if (iph1
->dpd_fails
>= iph1
->rmconf
->dpd_maxfails
) {
2016 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2017 IPSECSESSIONEVENTCODE_IKEV1_DPD_MAX_RETRANSMIT
,
2018 CONSTSTR("DPD maximum retransmits"),
2019 CONSTSTR("maxed-out of DPD requests without receiving an ack"));
2021 if (iph1
->remote
->ss_family
== AF_INET
)
2022 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
2025 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_PEER_DEAD
, FROM_LOCAL
, address
, 0, NULL
);
2028 plog(ASL_LEVEL_DEBUG
,
2029 "DPD: remote seems to be dead\n");
2031 /* Do not reschedule here: phase1 is deleted,
2032 * DPD will be reactivated when a new ph1 will be negociated
2038 payload
= vmalloc(tlen
);
2039 if (payload
== NULL
) {
2040 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2041 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2042 CONSTSTR("R-U-THERE?"),
2043 CONSTSTR("Failed to transmit DPD request"));
2045 "failed to get buffer for payload.\n");
2048 ru
= (struct isakmp_pl_ru
*)payload
->v
;
2049 ru
->h
.np
= ISAKMP_NPTYPE_NONE
;
2050 ru
->h
.len
= htons(tlen
);
2051 ru
->doi
= htonl(IPSEC_DOI
);
2052 ru
->type
= htons(ISAKMP_NTYPE_R_U_THERE
);
2053 ru
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ?*/
2054 ru
->spi_size
= sizeof(isakmp_index
);
2056 memcpy(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
));
2057 memcpy(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
));
2059 if (iph1
->dpd_seq
== 0){
2060 /* generate a random seq which is not too big */
2062 iph1
->dpd_seq
= rand() & 0x0fff;
2065 ru
->data
= htonl(iph1
->dpd_seq
);
2067 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
2070 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2071 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2072 CONSTSTR("R-U-THERE?"),
2073 CONSTSTR("Failed to transmit DPD request"));
2075 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2076 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
2077 CONSTSTR("R-U-THERE?"),
2081 if (iph1
->side
== INITIATOR
) {
2082 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2083 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_REQ
,
2084 CONSTSTR("Initiator DPD Request"),
2087 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2088 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_REQ
,
2089 CONSTSTR("Responder DPD Request"),
2092 plog(ASL_LEVEL_DEBUG
,
2093 "DPD R-U-There sent (%d)\n", error
);
2095 /* will be decreased if ACK received... */
2098 /* Reschedule the r_u_there with a short delay,
2099 * will be deleted/rescheduled if ACK received before */
2100 isakmp_sched_r_u(iph1
, 1);
2102 plog(ASL_LEVEL_DEBUG
,
2103 "rescheduling send_r_u (%d).\n", iph1
->rmconf
->dpd_retry
);
2107 * monitor DPD (ALGORITHM_INBOUND_DETECT) Informational exchange.
2110 isakmp_info_monitor_r_u_algo_inbound_detect (phase1_handle_t
*iph1
)
2112 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2113 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) aborted, invalid Phase 1 status %d....\n",
2118 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) ....\n");
2120 // check phase1 for ike packets received from peer
2121 if (iph1
->peer_sent_ike
) {
2122 // yes, reshedule check
2123 iph1
->peer_sent_ike
= 0;
2125 /* ike packets received from peer... reschedule dpd */
2126 isakmp_sched_r_u(iph1
, 0);
2128 plog(ASL_LEVEL_DEBUG
,
2129 "ike packets received from peer... reschedule monitor.\n");
2134 // after ike packets, next we check if any data was received
2135 if (!iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2136 isakmp_info_send_r_u(iph1
);
2138 isakmp_sched_r_u(iph1
, 0);
2140 plog(ASL_LEVEL_DEBUG
,
2141 "rescheduling DPD monitoring (for ALGORITHM_INBOUND_DETECT).\n");
2143 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2147 * monitor DPD (ALGORITHM_BLACKHOLE_DETECT) Informational exchange.
2150 isakmp_info_monitor_r_u_algo_blackhole_detect (phase1_handle_t
*iph1
)
2152 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2153 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) aborted, invalid Phase 1 status %d....\n",
2158 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) ....\n");
2160 // check if data was sent but none was received
2161 if (iph1
->parent_session
->i_sent_data_sc_dpd
&&
2162 !iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2163 isakmp_info_send_r_u(iph1
);
2165 isakmp_sched_r_u(iph1
, 0);
2167 plog(ASL_LEVEL_DEBUG
,
2168 "rescheduling DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) i = %d, peer %d.\n",
2169 iph1
->parent_session
->i_sent_data_sc_dpd
,
2170 iph1
->parent_session
->peer_sent_data_sc_dpd
);
2172 iph1
->parent_session
->i_sent_data_sc_dpd
= 0;
2173 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2177 * monitor DPD Informational exchange.
2180 isakmp_info_monitor_r_u(void *arg
)
2182 phase1_handle_t
*iph1
= arg
;
2184 if (iph1
&& iph1
->rmconf
) {
2185 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
) {
2186 isakmp_info_monitor_r_u_algo_inbound_detect(iph1
);
2187 } else if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2188 isakmp_info_monitor_r_u_algo_blackhole_detect(iph1
);
2190 plog(ASL_LEVEL_DEBUG
, "DPD monitoring aborted, invalid algorithm %d....\n",
2191 iph1
->rmconf
->dpd_algo
);
2196 /* Schedule a new R-U-THERE */
2198 isakmp_sched_r_u(phase1_handle_t
*iph1
, int retry
)
2201 iph1
->rmconf
== NULL
)
2205 if(iph1
->dpd_support
== 0 ||
2206 iph1
->rmconf
->dpd_interval
== 0)
2210 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_retry
,
2211 isakmp_info_send_r_u
, iph1
);
2213 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
||
2214 iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2215 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2216 isakmp_info_monitor_r_u
, iph1
);
2218 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2219 isakmp_info_send_r_u
, iph1
);
2227 * punts dpd for later because of some activity that:
2228 * 1) implicitly does dpd (e.g. phase2 exchanges), or
2229 * 2) indicates liveness (e.g. received ike packets).
2232 isakmp_reschedule_info_monitor_if_pending (phase1_handle_t
*iph1
, char *reason
)
2235 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2236 !iph1
->dpd_support
||
2237 !iph1
->rmconf
->dpd_interval
||
2238 iph1
->rmconf
->dpd_algo
== DPD_ALGO_DEFAULT
) {
2242 if (!iph1
->peer_sent_ike
) {
2243 SCHED_KILL(iph1
->dpd_r_u
);
2245 isakmp_sched_r_u(iph1
, 0);
2247 plog(ASL_LEVEL_DEBUG
,
2248 "%s... rescheduling send_r_u.\n",
2251 iph1
->peer_sent_ike
++;