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
;
203 plog(ASL_LEVEL_DEBUG
, "receive Information.\n");
205 encrypted
= ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
);
206 msgid
= ((struct isakmp
*)msg0
->v
)->msgid
;
208 /* Use new IV to decrypt Informational message. */
210 struct isakmp_ivm
*ivm
;
212 if (iph1
->ivm
== NULL
) {
213 plog(ASL_LEVEL_ERR
, "iph1->ivm == NULL\n");
214 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
215 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
216 CONSTSTR("Information message"),
217 CONSTSTR("Failed to process Information Message (no IV)"));
222 ivm
= oakley_newiv2(iph1
, ((struct isakmp
*)msg0
->v
)->msgid
);
225 "failed to compute IV\n");
226 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
227 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
228 CONSTSTR("Information message"),
229 CONSTSTR("Failed to process Information Message (can't compute IV)"));
233 msg
= oakley_do_decrypt(iph1
, msg0
, ivm
->iv
, ivm
->ive
);
237 "failed to decrypt packet\n");
238 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
239 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
240 CONSTSTR("Information message"),
241 CONSTSTR("Failed to decrypt Information message"));
249 if (msg
->l
< sizeof(*isakmp
) + sizeof(*gen
)) {
251 "ignore information because the "
252 "message is way too short\n");
256 isakmp
= (struct isakmp
*)msg
->v
;
257 gen
= (struct isakmp_gen
*)((caddr_t
)isakmp
+ sizeof(struct isakmp
));
261 if (isakmp
->np
!= ISAKMP_NPTYPE_HASH
) {
263 "ignore information because the "
264 "message has no hash payload.\n");
268 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
269 (!iph1
->approval
|| !iph1
->skeyid_a
)) {
271 "ignore information because ISAKMP-SA "
272 "has not been established yet.\n");
277 if (msg
->l
< sizeof(*isakmp
) + ntohs(gen
->len
) + sizeof(*nd
)) {
279 "ignore information because the "
280 "message is too short\n");
284 p
= (caddr_t
) gen
+ sizeof(struct isakmp_gen
);
285 nd
= (struct isakmp_gen
*) ((caddr_t
) gen
+ ntohs(gen
->len
));
287 /* nd length check */
288 if (ntohs(nd
->len
) > msg
->l
- (sizeof(struct isakmp
) +
291 "too long payload length (broken message?)\n");
295 if (ntohs(nd
->len
) < sizeof(*nd
)) {
297 "too short payload length (broken message?)\n");
301 payload
= vmalloc(ntohs(nd
->len
));
302 if (payload
== NULL
) {
304 "cannot allocate memory\n");
308 memcpy(payload
->v
, (caddr_t
) nd
, ntohs(nd
->len
));
311 hash
= oakley_compute_hash1(iph1
, isakmp
->msgid
, payload
);
314 "cannot compute hash\n");
320 if (ntohs(gen
->len
) - sizeof(struct isakmp_gen
) != hash
->l
) {
322 "ignore information due to hash length mismatch\n");
329 if (timingsafe_bcmp(p
, hash
->v
, hash
->l
) != 0) {
331 "ignore information due to hash mismatch\n");
338 plog(ASL_LEVEL_DEBUG
, "hash validated.\n");
343 /* make sure phase 1 was not yet at encrypted state */
344 switch (iph1
->etype
) {
345 case ISAKMP_ETYPE_AGG
:
346 // %%%%% should also check for unity/mode cfg - last pkt is encrypted in such cases
347 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
348 ((iph1
->side
== INITIATOR
&& iph1
->status
== IKEV1_STATE_AGG_I_MSG3SENT
) ||
349 (iph1
->side
== RESPONDER
&& iph1
->status
== IKEV1_STATE_AGG_R_MSG3RCVD
))) {
353 case ISAKMP_ETYPE_IDENT
:
354 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
355 ((iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_IDENT_I_MSG5SENT
356 || iph1
->status
== IKEV1_STATE_IDENT_I_MSG6RCVD
)) ||
357 (iph1
->side
== RESPONDER
&& (iph1
->status
== IKEV1_STATE_IDENT_R_MSG5RCVD
)))) {
362 if ((np
== ISAKMP_NPTYPE_NONE
) &&
363 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
364 (iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_AGG_I_MSG1SENT
))) {
365 // proposal rejected by peer, terminate now.
370 "%s message must be encrypted, status 0x%x, side %d\n",
371 s_isakmp_nptype(np
), iph1
->status
, iph1
->side
);
377 if (!(pbuf
= isakmp_parse(msg
))) {
379 "failed to parse msg");
385 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
; pa
->type
; pa
++) { // Wcast-align fix (void*) - aligned buffer of aligned (unpacked) structs
387 case ISAKMP_NPTYPE_HASH
:
390 case ISAKMP_NPTYPE_N
:
391 if ((ntohs(((struct isakmp_pl_n
*)pa
->ptr
)->type
) == ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
) &&
392 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
393 (iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_AGG_I_MSG1SENT
))) {
394 // proposal rejected by peer, terminate now.
397 "%s message with %s notification receveid, status 0x%x, side %d\n",
398 s_isakmp_nptype(np
), s_isakmp_notify_msg(ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
), iph1
->status
, iph1
->side
);
401 error
= isakmp_info_recv_n(iph1
,
402 (struct isakmp_pl_n
*)pa
->ptr
,
405 case ISAKMP_NPTYPE_D
:
406 error
= isakmp_info_recv_d(iph1
,
407 (struct isakmp_pl_d
*)pa
->ptr
,
410 case ISAKMP_NPTYPE_NONCE
:
411 /* XXX to be 6.4.2 ike-01.txt */
412 /* XXX IV is to be synchronized. */
414 "ignore Acknowledged Informational\n");
417 /* don't send information, see isakmp_ident_r1() */
420 "reject the packet, "
421 "received unexpected payload type %s.\n",
422 s_isakmp_nptype(gen
->np
));
430 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
431 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
432 CONSTSTR("Information message"),
437 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
438 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
439 CONSTSTR("Information message"),
440 CONSTSTR("Failed to process Information Message"));
447 ike_session_t
*session
= NULL
;
449 if (session
= iph1
->parent_session
) {
450 gettimeofday(&session
->stop_timestamp
, NULL
);
451 if (!session
->term_reason
) {
452 session
->term_reason
= ike_session_stopped_by_peer
;
454 ike_session_purge_ph1s_by_session(session
);
461 * handling of Notification payload
464 isakmp_info_recv_n(phase1_handle_t
*iph1
, struct isakmp_pl_n
*notify
, u_int32_t msgid
, int encrypted
)
472 type
= ntohs(notify
->type
);
475 case ISAKMP_NTYPE_CONNECTED
:
476 case ISAKMP_NTYPE_REPLAY_STATUS
:
478 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
482 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
484 return(isakmp_ph1_responder_lifetime(iph1
,
485 (struct isakmp_pl_resp_lifetime
*)notify
));
488 case ISAKMP_NTYPE_INITIAL_CONTACT
:
490 info_recv_initialcontact(iph1
);
495 case ISAKMP_NTYPE_R_U_THERE
:
497 return isakmp_info_recv_r_u(iph1
,
498 (struct isakmp_pl_ru
*)notify
, msgid
);
500 case ISAKMP_NTYPE_R_U_THERE_ACK
:
502 return isakmp_info_recv_r_u_ack(iph1
,
503 (struct isakmp_pl_ru
*)notify
, msgid
);
506 #ifdef ENABLE_VPNCONTROL_PORT
507 case ISAKMP_NTYPE_LOAD_BALANCE
:
508 isakmp_info_recv_lb(iph1
, (struct isakmp_pl_lb
*)notify
, encrypted
);
514 /* XXX there is a potential of dos attack. */
515 if(type
>= ISAKMP_NTYPE_MINERROR
&&
516 type
<= ISAKMP_NTYPE_MAXERROR
) {
518 /* don't think this realy deletes ph1 ? */
520 "Delete Phase 1 handle.\n");
523 if (ike_session_getph2bymsgid(iph1
, msgid
) == NULL
) {
525 "Fatal %s notify messsage, "
526 "Phase 1 should be deleted.\n",
527 s_isakmp_notify_msg(type
));
530 "Fatal %s notify messsage, "
531 "Phase 2 should be deleted.\n",
532 s_isakmp_notify_msg(type
));
537 "Unhandled notify message %s, "
538 "no Phase 2 handle found.\n",
539 s_isakmp_notify_msg(type
));
545 /* get spi if specified and allocate */
546 if(notify
->spi_size
> 0) {
547 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
549 "Invalid spi_size in notification payload.\n");
552 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
554 plog(ASL_LEVEL_DEBUG
,
555 "Notification message %d:%s, "
556 "doi=%d proto_id=%d spi=%s(size=%d).\n",
557 type
, s_isakmp_notify_msg(type
),
558 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
563 /* Send the message data to the logs */
564 if(type
>= ISAKMP_NTYPE_MINERROR
&&
565 type
<= ISAKMP_NTYPE_MAXERROR
) {
566 l
= ntohs(notify
->h
.len
) - sizeof(*notify
) - notify
->spi_size
;
568 nraw
= (char*)notify
;
569 nraw
+= sizeof(*notify
) + notify
->spi_size
;
570 if ((ndata
= vmalloc(l
)) != NULL
) {
571 memcpy(ndata
->v
, nraw
, ndata
->l
);
574 binsanitize(ndata
->v
, ndata
->l
));
578 "Cannot allocate memory\n");
585 #ifdef ENABLE_VPNCONTROL_PORT
587 isakmp_info_vpncontrol_notify_ike_failed (phase1_handle_t
*iph1
, int isakmp_info_initiator
, int type
, vchar_t
*data
)
589 u_int32_t address
= iph1_get_remote_v4_address(iph1
);
590 u_int32_t fail_reason
;
592 /* notify the API that we have received the delete */
594 if (isakmp_info_initiator
== FROM_REMOTE
) {
595 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
596 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
599 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
600 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is pre-mature\n");
601 } else if (expired
) {
602 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
603 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is expired\n");
607 vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
, 0, NULL
);
611 if (type
== ISAKMP_INTERNAL_ERROR
||
612 type
<= ISAKMP_NTYPE_UNEQUAL_PAYLOAD_LENGTHS
) {
613 int premature
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_PREMATURE
);
614 int expired
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_EXPIRED
);
615 int subjname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJNAME
);
616 int subjaltname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJALTNAME
);
619 fail_reason
= VPNCTL_NTYPE_PEER_CERT_PREMATURE
;
620 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is pre-mature\n");
621 } else if (expired
) {
622 fail_reason
= VPNCTL_NTYPE_PEER_CERT_EXPIRED
;
623 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is expired\n");
624 } else if (subjname
) {
625 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJNAME
;
626 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject name not valid\n");
627 } else if (subjaltname
) {
628 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJALTNAME
;
629 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject alternate name not valid\n");
633 (void)vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
,
634 (data
? data
->l
: 0), (u_int8_t
*)(data
? data
->v
: NULL
));
639 #endif /* ENABLE_VPNCONTROL_PORT */
642 * handling of Deletion payload
645 isakmp_info_recv_d(phase1_handle_t
*iph1
, struct isakmp_pl_d
*delete, u_int32_t msgid
, int encrypted
)
648 phase1_handle_t
*del_ph1
;
654 if (ntohl(delete->doi
) != IPSEC_DOI
) {
656 "delete payload with invalid doi:%d.\n",
660 * At deconnexion time, Cisco VPN client does this
661 * with a zero DOI. Don't give up in that situation.
663 if (((iph1
->mode_cfg
->flags
&
664 ISAKMP_CFG_VENDORID_UNITY
) == 0) || (delete->doi
!= 0))
671 num_spi
= ntohs(delete->num_spi
);
672 tlen
= ntohs(delete->h
.len
) - sizeof(struct isakmp_pl_d
);
674 if (tlen
!= num_spi
* delete->spi_size
) {
676 "deletion payload with invalid length.\n");
680 plog(ASL_LEVEL_DEBUG
,
681 "delete payload for protocol %s\n",
682 s_ipsecdoi_proto(delete->proto_id
));
684 if(!iph1
->rmconf
->weak_phase1_check
&& !encrypted
) {
685 plog(ASL_LEVEL_WARNING
,
686 "Ignoring unencrypted delete payload "
687 "(check the weak_phase1_check option)\n");
691 switch (delete->proto_id
) {
692 case IPSECDOI_PROTO_ISAKMP
:
693 if (delete->spi_size
!= sizeof(isakmp_index
)) {
695 "delete payload with strange spi "
696 "size %d(proto_id:%d)\n",
697 delete->spi_size
, delete->proto_id
);
701 del_ph1
= ike_session_getph1byindex(iph1
->parent_session
, (isakmp_index
*)(delete + 1));
704 // hack: start a rekey now, if one was pending (only for client).
705 if (del_ph1
->sce_rekey
&&
706 del_ph1
->parent_session
&&
707 del_ph1
->parent_session
->is_client
&&
708 del_ph1
->parent_session
->established
&&
709 !(del_ph1
->rmconf
->natt_multiple_user
&&
710 del_ph1
->parent_session
->is_l2tpvpn_ipsec
)) {
711 isakmp_ph1rekeyexpire(del_ph1
, FALSE
);
715 SCHED_KILL(del_ph1
->scr
);
718 * Do not delete IPsec SAs when receiving an IKE delete notification.
719 * Just delete the IKE SA.
721 #ifdef ENABLE_VPNCONTROL_PORT
722 if (del_ph1
->started_by_api
|| (del_ph1
->is_rekey
&& del_ph1
->parent_session
&& del_ph1
->parent_session
->is_client
)) {
723 if (ike_session_islast_ph1(del_ph1
)) {
724 isakmp_info_vpncontrol_notify_ike_failed(del_ph1
, FROM_REMOTE
, VPNCTL_NTYPE_PH1_DELETE
, NULL
);
728 if (del_ph1
->rmconf
->natt_multiple_user
&&
729 del_ph1
->parent_session
->is_l2tpvpn_ipsec
) {
730 plog(ASL_LEVEL_DEBUG
, "Ignoring IKE delete from peer for L2TP server\n");
733 isakmp_ph1expire(del_ph1
);
737 case IPSECDOI_PROTO_IPSEC_AH
:
738 case IPSECDOI_PROTO_IPSEC_ESP
:
739 if (delete->spi_size
!= sizeof(u_int32_t
)) {
741 "delete payload with strange spi "
742 "size %d(proto_id:%d)\n",
743 delete->spi_size
, delete->proto_id
);
746 if (iph1
->rmconf
->natt_multiple_user
&&
747 iph1
->parent_session
->is_l2tpvpn_ipsec
) {
748 uint32_t *ph2_spi
= ALIGNED_CAST(u_int32_t
*)(delete + 1);
749 phase2_handle_t
*iph2
= ike_session_getph2bysaidx(iph1
->local
, iph1
->remote
, delete->proto_id
, ph2_spi
[0]);
752 iph2
->is_defunct
= 1;
753 plog(ASL_LEVEL_DEBUG
, "Ignoring SA delete from peer for L2TP server\n");
757 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
758 ALIGNED_CAST(u_int32_t
*)(delete + 1), num_spi
, NULL
, NULL
); // Wcast-align fix (void*) - delete payload is aligned
761 case IPSECDOI_PROTO_IPCOMP
:
762 /* need to handle both 16bit/32bit SPI */
763 memset(&spi
, 0, sizeof(spi
));
764 if (delete->spi_size
== sizeof(spi
.spi16
[1])) {
765 memcpy(&spi
.spi16
[1], delete + 1,
766 sizeof(spi
.spi16
[1]));
767 } else if (delete->spi_size
== sizeof(spi
.spi32
))
768 memcpy(&spi
.spi32
, delete + 1, sizeof(spi
.spi32
));
771 "delete payload with strange spi "
772 "size %d(proto_id:%d)\n",
773 delete->spi_size
, delete->proto_id
);
776 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
777 &spi
.spi32
, num_spi
, NULL
, NULL
);
782 "deletion message received, "
783 "invalid proto_id: %d\n",
788 plog(ASL_LEVEL_DEBUG
, "purged SAs.\n");
794 * send Delete payload (for ISAKMP SA) in Informational exchange.
797 isakmp_info_send_d1(phase1_handle_t
*iph1
)
799 struct isakmp_pl_d
*d
;
800 vchar_t
*payload
= NULL
;
804 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
))
807 /* create delete payload */
809 /* send SPIs of inbound SAs. */
810 /* XXX should send outbound SAs's ? */
811 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
812 payload
= vmalloc(tlen
);
813 if (payload
== NULL
) {
815 "failed to get buffer for payload.\n");
819 d
= (struct isakmp_pl_d
*)payload
->v
;
820 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
821 d
->h
.len
= htons(tlen
);
822 d
->doi
= htonl(IPSEC_DOI
);
823 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
824 d
->spi_size
= sizeof(isakmp_index
);
825 d
->num_spi
= htons(1);
826 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
828 error
= isakmp_info_send_common(iph1
, payload
,
832 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
833 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
834 CONSTSTR("Delete ISAKMP-SA"),
835 CONSTSTR("Failed to transmit Delete-ISAKMP-SA message"));
837 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
838 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
839 CONSTSTR("Delete ISAKMP-SA"),
847 * send Delete payload (for IPsec SA) in Informational exchange, based on
848 * pfkey msg. It sends always single SPI.
851 isakmp_info_send_d2(phase2_handle_t
*iph2
)
853 phase1_handle_t
*iph1
;
855 struct isakmp_pl_d
*d
;
856 vchar_t
*payload
= NULL
;
861 if (!FSM_STATE_IS_ESTABLISHED(iph2
->status
))
865 * don't send delete information if there is no phase 1 handler.
866 * It's nonsensical to negotiate phase 1 to send the information.
868 iph1
= ike_session_get_established_ph1(iph2
->parent_session
);
870 iph1
= ike_session_getph1byaddr(iph2
->parent_session
, iph2
->src
, iph2
->dst
);
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"));
881 plog(ASL_LEVEL_DEBUG
,
882 "No ph1 handler found, could not send DELETE_SA\n");
886 /* create delete payload */
887 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
889 /* send SPIs of inbound SAs. */
891 * XXX should I send outbound SAs's ?
892 * I send inbound SAs's SPI only at the moment because I can't
893 * decode any more if peer send encoded packet without aware of
894 * deletion of SA. Outbound SAs don't come under the situation.
896 tlen
= sizeof(*d
) + pr
->spisize
;
897 payload
= vmalloc(tlen
);
898 if (payload
== NULL
) {
899 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
900 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
901 CONSTSTR("Information message"),
902 CONSTSTR("Failed to transmit Information message"));
903 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
904 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
905 CONSTSTR("Delete IPSEC-SA"),
906 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
908 "failed to get buffer for payload.\n");
912 d
= (struct isakmp_pl_d
*)payload
->v
;
913 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
914 d
->h
.len
= htons(tlen
);
915 d
->doi
= htonl(IPSEC_DOI
);
916 d
->proto_id
= pr
->proto_id
;
917 d
->spi_size
= pr
->spisize
;
918 d
->num_spi
= htons(1);
920 * XXX SPI bits are left-filled, for use with IPComp.
921 * we should be switching to variable-length spi field...
923 spi
= (u_int8_t
*)&pr
->spi
;
924 spi
+= sizeof(pr
->spi
);
926 memcpy(d
+ 1, spi
, pr
->spisize
);
928 error
= isakmp_info_send_common(iph1
, payload
,
932 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
933 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
934 CONSTSTR("Delete IPSEC-SA"),
935 CONSTSTR("Failed to transmit Delete-IPSEC-SA"));
937 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
938 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
939 CONSTSTR("Delete IPSEC-SA"),
948 * send Notification payload (without ISAKMP SA) in an Informational exchange
951 isakmp_info_send_nx(struct isakmp
*isakmp
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
,
952 int type
, vchar_t
*data
)
954 phase1_handle_t
*iph1
= NULL
;
955 struct remoteconf
*rmconf
;
956 vchar_t
*payload
= NULL
;
959 struct isakmp_pl_n
*n
;
960 int spisiz
= 0; /* see below */
961 ike_session_t
*sess
= ike_session_get_session(local
, remote
, FALSE
, NULL
);
963 /* search appropreate configuration */
964 rmconf
= getrmconf(remote
);
965 if (rmconf
== NULL
) {
966 IPSECSESSIONTRACEREVENT(sess
,
967 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
968 CONSTSTR("Information message"),
969 CONSTSTR("Failed to transmit Information message (no remote configuration)"));
971 "no configuration found for peer address.\n");
975 /* add new entry to isakmp status table. */
976 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
978 IPSECSESSIONTRACEREVENT(sess
,
979 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
980 CONSTSTR("Information message"),
981 CONSTSTR("Failed to transmit Information message (no new Phase 1)"));
983 "failed to allocate ph1");
987 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
988 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
989 fsm_set_state(&iph1
->status
, IKEV1_STATE_INFO
);
990 iph1
->rmconf
= rmconf
;
991 retain_rmconf(iph1
->rmconf
);
992 iph1
->side
= INITIATOR
;
993 iph1
->version
= isakmp
->v
;
995 iph1
->msgid
= 0; /* XXX */
997 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1004 iph1
->frag_chain
= NULL
;
1007 /* copy remote address */
1008 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
1009 IPSECSESSIONTRACEREVENT(sess
,
1010 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1011 CONSTSTR("Information message"),
1012 CONSTSTR("Failed to transmit Information Message (can't copy Phase 1 addresses)"));
1014 "failed to copy ph1 addresses");
1016 iph1
= NULL
; /* deleted in copy_ph1addresses */
1020 tlen
= sizeof(*n
) + spisiz
;
1023 payload
= vmalloc(tlen
);
1024 if (payload
== NULL
) {
1025 IPSECSESSIONTRACEREVENT(sess
,
1026 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1027 CONSTSTR("Information message"),
1028 CONSTSTR("Failed to transmit Information Message (can't allocate payload)"));
1030 "failed to get buffer to send.\n");
1035 n
= (struct isakmp_pl_n
*)payload
->v
;
1036 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1037 n
->h
.len
= htons(tlen
);
1038 n
->doi
= htonl(IPSEC_DOI
);
1039 n
->proto_id
= IPSECDOI_KEY_IKE
;
1040 n
->spi_size
= spisiz
;
1041 n
->type
= htons(type
);
1043 memset(n
+ 1, 0, spisiz
); /* XXX spisiz is always 0 */
1045 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1047 #ifdef ENABLE_VPNCONTROL_PORT
1048 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1050 if (ike_session_link_phase1(sess
, iph1
))
1053 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
1056 IPSECSESSIONTRACEREVENT(sess
,
1057 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1058 CONSTSTR("Without ISAKMP-SA"),
1059 CONSTSTR("Failed to transmit Without-ISAKMP-SA message"));
1061 IPSECSESSIONTRACEREVENT(sess
,
1062 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1063 CONSTSTR("Without ISAKMP-SA"),
1069 ike_session_unlink_phase1(iph1
);
1075 * send Notification payload (with ISAKMP SA) in an Informational exchange
1078 isakmp_info_send_n1(phase1_handle_t
*iph1
, int type
, vchar_t
*data
)
1080 vchar_t
*payload
= NULL
;
1083 struct isakmp_pl_n
*n
;
1087 * note on SPI size: which description is correct? I have chosen
1090 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
1091 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
1092 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
1093 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
1094 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
1096 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
||
1097 type
== ISAKMP_NTYPE_LOAD_BALANCE
)
1098 spisiz
= sizeof(isakmp_index
);
1102 tlen
= sizeof(*n
) + spisiz
;
1105 payload
= vmalloc(tlen
);
1106 if (payload
== NULL
) {
1107 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1108 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1109 CONSTSTR("ISAKMP-SA"),
1110 CONSTSTR("Failed to transmit ISAKMP-SA message (can't allocate payload)"));
1112 "failed to get buffer to send.\n");
1116 n
= (struct isakmp_pl_n
*)payload
->v
;
1117 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1118 n
->h
.len
= htons(tlen
);
1119 n
->doi
= htonl(iph1
->rmconf
->doitype
);
1120 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
1121 n
->spi_size
= spisiz
;
1122 n
->type
= htons(type
);
1124 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
1126 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1128 #ifdef ENABLE_VPNCONTROL_PORT
1129 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1132 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
1135 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1136 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1137 CONSTSTR("ISAKMP-SA"),
1138 CONSTSTR("Can't transmit ISAKMP-SA message"));
1140 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1141 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1142 CONSTSTR("ISAKMP-SA"),
1150 * send Notification payload (with IPsec SA) in an Informational exchange
1153 isakmp_info_send_n2(phase2_handle_t
*iph2
, int type
, vchar_t
*data
)
1155 phase1_handle_t
*iph1
= iph2
->ph1
;
1156 vchar_t
*payload
= NULL
;
1159 struct isakmp_pl_n
*n
;
1162 if (!iph2
->approval
)
1165 pr
= iph2
->approval
->head
;
1167 /* XXX must be get proper spi */
1168 tlen
= sizeof(*n
) + pr
->spisize
;
1171 payload
= vmalloc(tlen
);
1172 if (payload
== NULL
) {
1173 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1174 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1175 CONSTSTR("IPSEC-SA"),
1176 CONSTSTR("Failed to transmit IPSEC-SA message (can't allocate payload)"));
1178 "failed to get buffer to send.\n");
1182 n
= (struct isakmp_pl_n
*)payload
->v
;
1183 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1184 n
->h
.len
= htons(tlen
);
1185 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1186 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1187 n
->spi_size
= pr
->spisize
;
1188 n
->type
= htons(type
);
1189 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign
1191 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1193 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
1194 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
1197 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1198 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1199 CONSTSTR("IPSEC-SA"),
1200 CONSTSTR("Failed to transmit IPSEC-SA message"));
1202 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1203 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1204 CONSTSTR("IPSEC-SA"),
1213 * When ph1->skeyid_a == NULL, send message without encoding.
1216 isakmp_info_send_common(phase1_handle_t
*iph1
, vchar_t
*payload
, u_int32_t np
, int flags
)
1218 phase2_handle_t
*iph2
= NULL
;
1219 vchar_t
*hash
= NULL
;
1220 struct isakmp
*isakmp
;
1221 struct isakmp_gen
*gen
;
1226 /* add new entry to isakmp status table */
1227 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_INFO
);
1230 "failed to allocate ph2");
1234 iph2
->dst
= dupsaddr(iph1
->remote
);
1235 if (iph2
->dst
== NULL
) {
1237 "failed to duplicate remote address");
1238 ike_session_delph2(iph2
);
1241 iph2
->src
= dupsaddr(iph1
->local
);
1242 if (iph2
->src
== NULL
) {
1244 "failed to duplicate local address");
1245 ike_session_delph2(iph2
);
1248 switch (iph1
->remote
->ss_family
) {
1250 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1251 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1252 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1257 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1258 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1259 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1265 "invalid family: %d\n", iph1
->remote
->ss_family
);
1266 ike_session_delph2(iph2
);
1269 iph2
->side
= INITIATOR
;
1270 fsm_set_state(&iph2
->status
, IKEV1_STATE_INFO
);
1271 iph2
->msgid
= isakmp_newmsgid2(iph1
);
1273 /* get IV and HASH(1) if skeyid_a was generated. */
1274 if (iph1
->skeyid_a
!= NULL
) {
1275 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1276 if (iph2
->ivm
== NULL
) {
1278 "failed to generate IV");
1279 ike_session_delph2(iph2
);
1283 /* generate HASH(1) */
1284 hash
= oakley_compute_hash1(iph1
, iph2
->msgid
, payload
);
1287 "failed to generate HASH");
1288 ike_session_delph2(iph2
);
1292 /* initialized total buffer length */
1294 tlen
+= sizeof(*gen
);
1296 /* IKE-SA is not established */
1299 /* initialized total buffer length */
1302 if ((flags
& ISAKMP_FLAG_A
) == 0)
1303 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
1305 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
1307 ike_session_link_ph2_to_ph1(iph1
, iph2
);
1309 tlen
+= sizeof(*isakmp
) + payload
->l
;
1311 /* create buffer for isakmp payload */
1312 iph2
->sendbuf
= vmalloc(tlen
);
1313 if (iph2
->sendbuf
== NULL
) {
1315 "failed to get buffer to send.\n");
1319 /* create isakmp header */
1320 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
1321 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1322 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1323 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
1324 isakmp
->v
= iph1
->version
;
1325 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
1326 isakmp
->flags
= iph2
->flags
;
1327 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
1328 isakmp
->len
= htonl(tlen
);
1329 p
= (char *)(isakmp
+ 1);
1331 /* create HASH payload */
1333 gen
= (struct isakmp_gen
*)p
;
1334 gen
->np
= np
& 0xff;
1335 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
1337 memcpy(p
, hash
->v
, hash
->l
);
1342 memcpy(p
, payload
->v
, payload
->l
);
1345 #ifdef HAVE_PRINT_ISAKMP_C
1346 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1350 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
1353 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
1355 VPTRINIT(iph2
->sendbuf
);
1358 "failed to encrypt packet");
1361 iph2
->sendbuf
= tmp
;
1364 /* HDR*, HASH(1), N */
1365 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
1367 "failed to send packet");
1368 VPTRINIT(iph2
->sendbuf
);
1372 plog(ASL_LEVEL_DEBUG
,
1373 "sendto Information %s.\n", s_isakmp_nptype(np
));
1376 * don't resend notify message because peer can use Acknowledged
1377 * Informational if peer requires the reply of the notify message.
1380 /* XXX If Acknowledged Informational required, don't delete ph2handle */
1382 VPTRINIT(iph2
->sendbuf
);
1383 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1384 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1385 CONSTSTR("Information message"),
1392 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1393 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1394 CONSTSTR("Information message"),
1395 CONSTSTR("Failed to transmit Information message"));
1402 ike_session_unlink_phase2(iph2
);
1407 * add a notify payload to buffer by reallocating buffer.
1408 * If buf == NULL, the function only create a notify payload.
1410 * XXX Which is SPI to be included, inbound or outbound ?
1413 isakmp_add_pl_n(vchar_t
*buf0
, u_int8_t
**np_p
, int type
, struct saproto
*pr
, vchar_t
*data
)
1415 vchar_t
*buf
= NULL
;
1416 struct isakmp_pl_n
*n
;
1421 **np_p
= ISAKMP_NPTYPE_N
;
1423 tlen
= sizeof(*n
) + pr
->spisize
;
1429 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
1431 buf
= vmalloc(tlen
);
1434 "failed to get a payload buffer.\n");
1438 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
1439 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1440 n
->h
.len
= htons(tlen
);
1441 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1442 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1443 n
->spi_size
= pr
->spisize
;
1444 n
->type
= htons(type
);
1445 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign with cast
1447 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1449 /* save the pointer of next payload type */
1457 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
)
1459 vchar_t
*buf
= NULL
;
1460 struct sadb_msg
*msg
, *next
, *end
;
1462 struct sadb_lifetime
*lt
;
1463 struct sockaddr_storage
*src
, *dst
;
1464 phase2_handle_t
*iph2
;
1467 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1469 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
1471 plog(ASL_LEVEL_DEBUG
,
1472 "pfkey_dump_sadb returned nothing.\n");
1476 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1477 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1480 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1482 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1483 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1488 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1490 "pfkey_check (%s)\n", ipsec_strerror());
1495 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1497 || !mhp
[SADB_EXT_ADDRESS_SRC
]
1498 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
1502 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1503 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1504 lt
= ALIGNED_CAST(struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
1506 created
= lt
->sadb_lifetime_addtime
;
1510 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1511 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1516 /* XXX n^2 algorithm, inefficient */
1518 /* don't delete inbound SAs at the moment (just save them in inbound_spi) */
1519 /* XXX should we remove SAs with opposite direction as well? */
1520 if (CMPSADDR2(dst0
, dst
)) {
1525 for (i
= 0; i
< n
; i
++) {
1528 if (spi
[i
] != sa
->sadb_sa_spi
)
1532 * delete a relative phase 2 handler.
1533 * continue to process if no relative phase 2 handler
1536 if (inbound_spi
&& max_inbound_spi
&& j
< *max_inbound_spi
) {
1537 i_spi
= &inbound_spi
[j
];
1541 iph2
= ike_session_getph2bysaidx2(src
, dst
, proto
, spi
[i
], i_spi
);
1543 pfkey_send_delete(lcconf
->sock_pfkey
,
1544 msg
->sadb_msg_satype
,
1546 src
, dst
, sa
->sadb_sa_spi
);
1550 ike_session_unlink_phase2(iph2
);
1556 plog(ASL_LEVEL_INFO
, "Purged IPsec-SA proto_id=%s spi=%u.\n",
1557 s_ipsecdoi_proto(proto
),
1564 if (max_inbound_spi
) {
1565 *max_inbound_spi
= j
;
1573 * delete all phase2 sa relatived to the destination address.
1574 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1575 * an INITIAL-CONTACT if we have contacted the peer. This matches the
1576 * Sun IKE behavior, and makes rekeying work much better when the peer
1580 info_recv_initialcontact(phase1_handle_t
*iph1
)
1582 vchar_t
*buf
= NULL
;
1583 struct sadb_msg
*msg
, *next
, *end
;
1585 struct sockaddr_storage
*src
, *dst
;
1586 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1588 phase2_handle_t
*iph2
;
1596 // TODO: make sure that is_rekey is cleared for this. and session indicates the same
1598 loc
= racoon_strdup(saddrwop2str(iph1
->local
));
1599 rem
= racoon_strdup(saddrwop2str(iph1
->remote
));
1604 * Purge all IPSEC-SAs for the peer. We can do this
1605 * the easy way (using a PF_KEY SADB_DELETE extension)
1606 * or we can do it the hard way.
1608 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1609 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
1611 plog(ASL_LEVEL_INFO
,
1612 "purging %s SAs for %s -> %s\n",
1613 pfkey_satypes
[i
].ps_name
, loc
, rem
);
1614 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1615 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1616 iph1
->local
, iph1
->remote
) == -1) {
1618 "delete_all %s -> %s failed for %s (%s)\n",
1620 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1624 ike_session_deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
1626 plog(ASL_LEVEL_INFO
,
1627 "purging %s SAs for %s -> %s\n",
1628 pfkey_satypes
[i
].ps_name
, rem
, loc
);
1629 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1630 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1631 iph1
->remote
, iph1
->local
) == -1) {
1633 "delete_all %s -> %s failed for %s (%s)\n",
1635 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1639 ike_session_deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1651 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1653 plog(ASL_LEVEL_DEBUG
,
1654 "pfkey_dump_sadb returned nothing.\n");
1658 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1659 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1662 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1664 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1665 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1670 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1672 "pfkey_check (%s)\n", ipsec_strerror());
1677 if (mhp
[SADB_EXT_SA
] == NULL
1678 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1679 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1683 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
]; // Wcast-align fix (void*) - buffer of pointers to aligned structs
1684 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1685 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1687 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1688 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1694 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1695 * announces the sender of the message was rebooted.
1696 * it is interpreted to delete all SAs which source address
1697 * is the sender of the message.
1698 * racoon only deletes SA which is matched both the
1699 * source address and the destination accress.
1703 * XXX RFC 3947 says that whe MUST NOT use IP+port to find old SAs
1706 if(iph1
->natt_flags
& NAT_DETECTED
){
1707 if (CMPSADDR(iph1
->local
, src
) == 0 &&
1708 CMPSADDR(iph1
->remote
, dst
) == 0)
1710 else if (CMPSADDR(iph1
->remote
, src
) == 0 &&
1711 CMPSADDR(iph1
->local
, dst
) == 0)
1719 /* If there is no NAT-T, we don't have to check addr + port...
1720 * XXX what about a configuration with a remote peers which is not
1721 * NATed, but which NATs some other peers ?
1722 * Here, the INITIAl-CONTACT would also flush all those NATed peers !!
1724 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1725 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1727 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1728 cmpsaddrwop(iph1
->local
, dst
) == 0)
1736 * Make sure this is an SATYPE that we manage.
1737 * This is gross; too bad we couldn't do it the
1740 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1741 if (pfkey_satypes
[i
].ps_satype
==
1742 msg
->sadb_msg_satype
)
1745 if (i
== pfkey_nsatypes
) {
1750 plog(ASL_LEVEL_INFO
,
1751 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1752 pfkey_send_delete(lcconf
->sock_pfkey
,
1753 msg
->sadb_msg_satype
,
1754 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1757 * delete a relative phase 2 handler.
1758 * continue to process if no relative phase 2 handler
1761 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1762 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1765 ike_session_unlink_phase2(iph2
);
1775 isakmp_check_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase1_handle_t
*iph1
)
1777 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1779 plog(ASL_LEVEL_DEBUG
,
1780 "Notify Message received\n");
1782 switch (ntohs(notify
->type
)) {
1783 case ISAKMP_NTYPE_CONNECTED
:
1784 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1785 case ISAKMP_NTYPE_REPLAY_STATUS
:
1786 case ISAKMP_NTYPE_HEARTBEAT
:
1787 #ifdef ENABLE_HYBRID
1788 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1790 plog(ASL_LEVEL_WARNING
,
1791 "Ignore %s notification.\n",
1792 s_isakmp_notify_msg(ntohs(notify
->type
)));
1794 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1795 plog(ASL_LEVEL_WARNING
,
1796 "Ignore INITIAL-CONTACT notification, "
1797 "because it is only accepted after Phase 1.\n");
1799 case ISAKMP_NTYPE_LOAD_BALANCE
:
1800 plog(ASL_LEVEL_WARNING
,
1801 "Ignore LOAD-BALANCE notification, "
1802 "because it is only accepted after Phase 1.\n");
1805 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1807 "Received unknown notification type %s.\n",
1808 s_isakmp_notify_msg(ntohs(notify
->type
)));
1815 isakmp_check_ph2_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase2_handle_t
*iph2
)
1817 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1819 plog(ASL_LEVEL_DEBUG
,
1820 "Phase 2 Notify Message received\n");
1822 switch (ntohs(notify
->type
)) {
1823 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1824 return((void)isakmp_ph2_responder_lifetime(iph2
,
1825 (struct isakmp_pl_resp_lifetime
*)notify
));
1827 case ISAKMP_NTYPE_CONNECTED
:
1828 case ISAKMP_NTYPE_REPLAY_STATUS
:
1829 case ISAKMP_NTYPE_HEARTBEAT
:
1830 #ifdef ENABLE_HYBRID
1831 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1833 plog(ASL_LEVEL_WARNING
,
1834 "Ignore %s notification.\n",
1835 s_isakmp_notify_msg(ntohs(notify
->type
)));
1837 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1838 plog(ASL_LEVEL_WARNING
,
1839 "Ignore INITIAL-CONTACT notification, "
1840 "because it is only accepted after Phase 1.\n");
1842 case ISAKMP_NTYPE_LOAD_BALANCE
:
1843 plog(ASL_LEVEL_WARNING
,
1844 "Ignore LOAD-BALANCE notification, "
1845 "because it is only accepted after Phase 1.\n");
1848 isakmp_info_send_n1(iph2
->ph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1850 "Received unknown notification type %s.\n",
1851 s_isakmp_notify_msg(ntohs(notify
->type
)));
1857 #ifdef ENABLE_VPNCONTROL_PORT
1859 isakmp_info_recv_lb(phase1_handle_t
*iph1
, struct isakmp_pl_lb
*n
, int encrypted
)
1862 if (iph1
->side
!= INITIATOR
)
1864 plog(ASL_LEVEL_DEBUG
,
1865 "LOAD-BALANCE notification ignored - we are not the initiator.\n");
1869 plog(ASL_LEVEL_DEBUG
,
1870 "LOAD-BALANCE notification ignored - not protected.\n");
1873 if (ntohs(n
->h
.len
) != sizeof(struct isakmp_pl_lb
)) {
1874 plog(ASL_LEVEL_DEBUG
,
1875 "Invalid length of payload\n");
1879 vpncontrol_notify_ike_failed(ISAKMP_NTYPE_LOAD_BALANCE
, FROM_REMOTE
,
1880 iph1_get_remote_v4_address(iph1
), 4, (u_int8_t
*)(&(n
->address
)));
1882 plog(ASL_LEVEL_NOTICE
,
1883 "Received LOAD_BALANCE notification.\n");
1885 if (((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
!= ntohl(n
->address
)) {
1886 plog(ASL_LEVEL_DEBUG
,
1887 "Deleting old Phase 1 because of LOAD_BALANCE notification - redirect address=%x.\n",
1890 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1891 isakmp_info_send_d1(iph1
);
1893 isakmp_ph1expire(iph1
);
1902 isakmp_info_recv_r_u (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1904 struct isakmp_pl_ru
*ru_ack
;
1905 vchar_t
*payload
= NULL
;
1909 plog(ASL_LEVEL_DEBUG
,
1910 "DPD R-U-There received\n");
1912 /* XXX should compare cookies with iph1->index?
1913 Or is this already done by calling function? */
1914 tlen
= sizeof(*ru_ack
);
1915 payload
= vmalloc(tlen
);
1916 if (payload
== NULL
) {
1917 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1918 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1919 CONSTSTR("R-U-THERE? ACK"),
1920 CONSTSTR("Failed to transmit DPD response"));
1922 "failed to get buffer to send.\n");
1926 ru_ack
= (struct isakmp_pl_ru
*)payload
->v
;
1927 ru_ack
->h
.np
= ISAKMP_NPTYPE_NONE
;
1928 ru_ack
->h
.len
= htons(tlen
);
1929 ru_ack
->doi
= htonl(IPSEC_DOI
);
1930 ru_ack
->type
= htons(ISAKMP_NTYPE_R_U_THERE_ACK
);
1931 ru_ack
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ? */
1932 ru_ack
->spi_size
= sizeof(isakmp_index
);
1933 memcpy(ru_ack
->i_ck
, ru
->i_ck
, sizeof(cookie_t
));
1934 memcpy(ru_ack
->r_ck
, ru
->r_ck
, sizeof(cookie_t
));
1935 ru_ack
->data
= ru
->data
;
1937 /* XXX Should we do FLAG_A ? */
1938 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
,
1942 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1943 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1944 CONSTSTR("R-U-THERE? ACK"),
1945 CONSTSTR("Failed to transmit DPD ack"));
1947 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1948 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1949 CONSTSTR("R-U-THERE? ACK"),
1953 plog(ASL_LEVEL_DEBUG
, "received a valid R-U-THERE, ACK sent\n");
1955 /* Should we mark tunnel as active ? */
1960 isakmp_info_recv_r_u_ack (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1963 plog(ASL_LEVEL_DEBUG
,
1964 "DPD R-U-There-Ack received\n");
1966 /* XXX Maintain window of acceptable sequence numbers ?
1967 * => ru->data <= iph2->dpd_seq &&
1968 * ru->data >= iph2->dpd_seq - iph2->dpd_fails ? */
1969 if (ntohl(ru
->data
) != iph1
->dpd_seq
) {
1971 "Wrong DPD sequence number (%d, %d expected).\n",
1972 ntohl(ru
->data
), iph1
->dpd_seq
);
1976 if (memcmp(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
)) ||
1977 memcmp(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
))) {
1979 "Cookie mismatch in DPD ACK!.\n");
1983 iph1
->dpd_fails
= 0;
1988 iph1
->dpd_lastack
= time(NULL
);
1990 SCHED_KILL(iph1
->dpd_r_u
);
1992 isakmp_sched_r_u(iph1
, 0);
1994 if (iph1
->side
== INITIATOR
) {
1995 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1996 IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RESP
,
1997 CONSTSTR("Initiator DPD Response"),
2000 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2001 IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RESP
,
2002 CONSTSTR("Responder DPD Response"),
2005 plog(ASL_LEVEL_DEBUG
, "received an R-U-THERE-ACK\n");
2007 #ifdef ENABLE_VPNCONTROL_PORT
2008 vpncontrol_notify_peer_resp_ph1(1, iph1
);
2009 #endif /* ENABLE_VPNCONTROL_PORT */
2016 * send Delete payload (for ISAKMP SA) in Informational exchange.
2019 isakmp_info_send_r_u(void *arg
)
2021 phase1_handle_t
*iph1
= arg
;
2023 /* create R-U-THERE payload */
2024 struct isakmp_pl_ru
*ru
;
2025 vchar_t
*payload
= NULL
;
2029 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2030 plog(ASL_LEVEL_DEBUG
, "DPD r-u send aborted, invalid Phase 1 status %d....\n",
2035 if (iph1
->dpd_fails
>= iph1
->rmconf
->dpd_maxfails
) {
2036 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2037 IPSECSESSIONEVENTCODE_IKEV1_DPD_MAX_RETRANSMIT
,
2038 CONSTSTR("DPD maximum retransmits"),
2039 CONSTSTR("maxed-out of DPD requests without receiving an ack"));
2041 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_PEER_DEAD
, FROM_LOCAL
, iph1_get_remote_v4_address(iph1
), 0, NULL
);
2044 plog(ASL_LEVEL_DEBUG
,
2045 "DPD: remote seems to be dead\n");
2047 /* Do not reschedule here: phase1 is deleted,
2048 * DPD will be reactivated when a new ph1 will be negociated
2054 payload
= vmalloc(tlen
);
2055 if (payload
== NULL
) {
2056 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2057 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2058 CONSTSTR("R-U-THERE?"),
2059 CONSTSTR("Failed to transmit DPD request"));
2061 "failed to get buffer for payload.\n");
2064 ru
= (struct isakmp_pl_ru
*)payload
->v
;
2065 ru
->h
.np
= ISAKMP_NPTYPE_NONE
;
2066 ru
->h
.len
= htons(tlen
);
2067 ru
->doi
= htonl(IPSEC_DOI
);
2068 ru
->type
= htons(ISAKMP_NTYPE_R_U_THERE
);
2069 ru
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ?*/
2070 ru
->spi_size
= sizeof(isakmp_index
);
2072 memcpy(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
));
2073 memcpy(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
));
2075 if (iph1
->dpd_seq
== 0){
2076 /* generate a random seq which is not too big */
2078 iph1
->dpd_seq
= rand() & 0x0fff;
2081 ru
->data
= htonl(iph1
->dpd_seq
);
2083 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
2086 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2087 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2088 CONSTSTR("R-U-THERE?"),
2089 CONSTSTR("Failed to transmit DPD request"));
2091 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2092 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
2093 CONSTSTR("R-U-THERE?"),
2097 if (iph1
->side
== INITIATOR
) {
2098 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2099 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_REQ
,
2100 CONSTSTR("Initiator DPD Request"),
2103 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2104 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_REQ
,
2105 CONSTSTR("Responder DPD Request"),
2108 plog(ASL_LEVEL_DEBUG
,
2109 "DPD R-U-There sent (%d)\n", error
);
2111 /* will be decreased if ACK received... */
2114 /* Reschedule the r_u_there with a short delay,
2115 * will be deleted/rescheduled if ACK received before */
2116 isakmp_sched_r_u(iph1
, 1);
2118 plog(ASL_LEVEL_DEBUG
,
2119 "rescheduling send_r_u (%d).\n", iph1
->rmconf
->dpd_retry
);
2123 * monitor DPD (ALGORITHM_INBOUND_DETECT) Informational exchange.
2126 isakmp_info_monitor_r_u_algo_inbound_detect (phase1_handle_t
*iph1
)
2128 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2129 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) aborted, invalid Phase 1 status %d....\n",
2134 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) ....\n");
2136 // check phase1 for ike packets received from peer
2137 if (iph1
->peer_sent_ike
) {
2138 // yes, reshedule check
2139 iph1
->peer_sent_ike
= 0;
2141 /* ike packets received from peer... reschedule dpd */
2142 isakmp_sched_r_u(iph1
, 0);
2144 plog(ASL_LEVEL_DEBUG
,
2145 "ike packets received from peer... reschedule monitor.\n");
2150 // after ike packets, next we check if any data was received
2151 if (!iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2152 isakmp_info_send_r_u(iph1
);
2154 isakmp_sched_r_u(iph1
, 0);
2156 plog(ASL_LEVEL_DEBUG
,
2157 "rescheduling DPD monitoring (for ALGORITHM_INBOUND_DETECT).\n");
2159 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2163 * monitor DPD (ALGORITHM_BLACKHOLE_DETECT) Informational exchange.
2166 isakmp_info_monitor_r_u_algo_blackhole_detect (phase1_handle_t
*iph1
)
2168 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2169 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) aborted, invalid Phase 1 status %d....\n",
2174 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) ....\n");
2176 // check if data was sent but none was received
2177 if (iph1
->parent_session
->i_sent_data_sc_dpd
&&
2178 !iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2179 isakmp_info_send_r_u(iph1
);
2181 isakmp_sched_r_u(iph1
, 0);
2183 plog(ASL_LEVEL_DEBUG
,
2184 "rescheduling DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) i = %d, peer %d.\n",
2185 iph1
->parent_session
->i_sent_data_sc_dpd
,
2186 iph1
->parent_session
->peer_sent_data_sc_dpd
);
2188 iph1
->parent_session
->i_sent_data_sc_dpd
= 0;
2189 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2193 * monitor DPD Informational exchange.
2196 isakmp_info_monitor_r_u(void *arg
)
2198 phase1_handle_t
*iph1
= arg
;
2200 if (iph1
&& iph1
->rmconf
) {
2201 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
) {
2202 isakmp_info_monitor_r_u_algo_inbound_detect(iph1
);
2203 } else if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2204 isakmp_info_monitor_r_u_algo_blackhole_detect(iph1
);
2206 plog(ASL_LEVEL_DEBUG
, "DPD monitoring aborted, invalid algorithm %d....\n",
2207 iph1
->rmconf
->dpd_algo
);
2212 /* Schedule a new R-U-THERE */
2214 isakmp_sched_r_u(phase1_handle_t
*iph1
, int retry
)
2217 iph1
->rmconf
== NULL
)
2221 if(iph1
->dpd_support
== 0 ||
2222 iph1
->rmconf
->dpd_interval
== 0)
2226 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_retry
,
2227 isakmp_info_send_r_u
, iph1
);
2229 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
||
2230 iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2231 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2232 isakmp_info_monitor_r_u
, iph1
);
2234 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2235 isakmp_info_send_r_u
, iph1
);
2243 * punts dpd for later because of some activity that:
2244 * 1) implicitly does dpd (e.g. phase2 exchanges), or
2245 * 2) indicates liveness (e.g. received ike packets).
2248 isakmp_reschedule_info_monitor_if_pending (phase1_handle_t
*iph1
, char *reason
)
2251 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2252 !iph1
->dpd_support
||
2253 !iph1
->rmconf
->dpd_interval
||
2254 iph1
->rmconf
->dpd_algo
== DPD_ALGO_DEFAULT
) {
2258 if (!iph1
->peer_sent_ike
) {
2259 SCHED_KILL(iph1
->dpd_r_u
);
2261 isakmp_sched_r_u(iph1
, 0);
2263 plog(ASL_LEVEL_DEBUG
,
2264 "%s... rescheduling send_r_u.\n",
2267 iph1
->peer_sent_ike
++;