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"
95 #include "ikev2_info_rfc.h"
97 #include "ipsec_doi.h"
98 #include "crypto_openssl.h"
101 #include "algorithm.h"
102 #include "proposal.h"
103 #include "strnames.h"
105 #include "nattraversal.h"
107 #include "vpn_control_var.h"
108 #include "vpn_control.h"
109 #include "ike_session.h"
110 #include "ipsecSessionTracer.h"
111 #include "ipsecMessageTracer.h"
113 /* information exchange */
114 static int isakmp_info_recv_n (phase1_handle_t
*, struct isakmp_pl_n
*, u_int32_t
, int);
115 static int isakmp_info_recv_d (phase1_handle_t
*, struct isakmp_pl_d
*, u_int32_t
, int);
118 static int isakmp_info_recv_r_u (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
119 static int isakmp_info_recv_r_u_ack (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
122 #ifdef ENABLE_VPNCONTROL_PORT
123 static int isakmp_info_recv_lb (phase1_handle_t
*, struct isakmp_pl_lb
*lb
, int);
127 isakmp_ph1_responder_lifetime (phase1_handle_t
*iph1
, struct isakmp_pl_resp_lifetime
*notify
)
131 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
133 "invalid spi_size in notification payload.\n");
136 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
138 plog(ASL_LEVEL_DEBUG
,
139 "notification message ISAKMP-SA RESPONDER-LIFETIME, "
140 "doi=%d proto_id=%d spi=%s(size=%d).\n",
141 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
145 struct isakmp_pl_attr
*attrpl
;
146 int len
= ntohs(notify
->h
.len
) - (sizeof(*notify
) + notify
->spi_size
);
148 attrpl
= (struct isakmp_pl_attr
*)((char *)(notify
+ 1) + notify
->spi_size
);
158 isakmp_ph2_responder_lifetime (phase2_handle_t
*iph2
, struct isakmp_pl_resp_lifetime
*notify
)
162 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
164 "invalid spi_size in notification payload.\n");
167 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
169 plog(ASL_LEVEL_DEBUG
,
170 "notification message IPSEC-SA RESPONDER-LIFETIME, "
171 "doi=%d proto_id=%d spi=%s(size=%d).\n",
172 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
181 * Information Exchange
184 * receive Information
187 isakmp_info_recv(phase1_handle_t
*iph1
, vchar_t
*msg0
)
190 vchar_t
*pbuf
= NULL
;
193 struct isakmp
*isakmp
;
194 struct isakmp_gen
*gen
;
195 struct isakmp_parse_t
*pa
;
197 vchar_t
*hash
, *payload
;
198 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 (memcmp(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
))) {
352 case ISAKMP_ETYPE_IDENT
:
353 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
354 ((iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_IDENT_I_MSG5SENT
355 || iph1
->status
== IKEV1_STATE_IDENT_I_MSG6RCVD
)) ||
356 (iph1
->side
== RESPONDER
&& (iph1
->status
== IKEV1_STATE_IDENT_R_MSG5RCVD
)))) {
362 "%s message must be encrypted\n",
363 s_isakmp_nptype(np
));
369 if (!(pbuf
= isakmp_parse(msg
))) {
371 "failed to parse msg");
377 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
; pa
->type
; pa
++) { // Wcast-align fix (void*) - aligned buffer of aligned (unpacked) structs
379 case ISAKMP_NPTYPE_HASH
:
382 case ISAKMP_NPTYPE_N
:
383 error
= isakmp_info_recv_n(iph1
,
384 (struct isakmp_pl_n
*)pa
->ptr
,
387 case ISAKMP_NPTYPE_D
:
388 error
= isakmp_info_recv_d(iph1
,
389 (struct isakmp_pl_d
*)pa
->ptr
,
392 case ISAKMP_NPTYPE_NONCE
:
393 /* XXX to be 6.4.2 ike-01.txt */
394 /* XXX IV is to be synchronized. */
396 "ignore Acknowledged Informational\n");
399 /* don't send information, see isakmp_ident_r1() */
402 "reject the packet, "
403 "received unexpected payload type %s.\n",
404 s_isakmp_nptype(gen
->np
));
412 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
413 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
414 CONSTSTR("Information message"),
419 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
420 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
421 CONSTSTR("Information message"),
422 CONSTSTR("Failed to process Information Message"));
432 * handling of Notification payload
435 isakmp_info_recv_n(phase1_handle_t
*iph1
, struct isakmp_pl_n
*notify
, u_int32_t msgid
, int encrypted
)
443 type
= ntohs(notify
->type
);
446 case ISAKMP_NTYPE_CONNECTED
:
447 case ISAKMP_NTYPE_REPLAY_STATUS
:
449 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
453 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
455 return(isakmp_ph1_responder_lifetime(iph1
,
456 (struct isakmp_pl_resp_lifetime
*)notify
));
459 case ISAKMP_NTYPE_INITIAL_CONTACT
:
461 info_recv_initialcontact(iph1
);
466 case ISAKMP_NTYPE_R_U_THERE
:
468 return isakmp_info_recv_r_u(iph1
,
469 (struct isakmp_pl_ru
*)notify
, msgid
);
471 case ISAKMP_NTYPE_R_U_THERE_ACK
:
473 return isakmp_info_recv_r_u_ack(iph1
,
474 (struct isakmp_pl_ru
*)notify
, msgid
);
477 #ifdef ENABLE_VPNCONTROL_PORT
478 case ISAKMP_NTYPE_LOAD_BALANCE
:
479 isakmp_info_recv_lb(iph1
, (struct isakmp_pl_lb
*)notify
, encrypted
);
485 /* XXX there is a potential of dos attack. */
486 if(type
>= ISAKMP_NTYPE_MINERROR
&&
487 type
<= ISAKMP_NTYPE_MAXERROR
) {
489 /* don't think this realy deletes ph1 ? */
491 "Delete Phase 1 handle.\n");
494 if (ike_session_getph2bymsgid(iph1
, msgid
) == NULL
) {
496 "Fatal %s notify messsage, "
497 "Phase 1 should be deleted.\n",
498 s_isakmp_notify_msg(type
));
501 "Fatal %s notify messsage, "
502 "Phase 2 should be deleted.\n",
503 s_isakmp_notify_msg(type
));
508 "Unhandled notify message %s, "
509 "no Phase 2 handle found.\n",
510 s_isakmp_notify_msg(type
));
516 /* get spi if specified and allocate */
517 if(notify
->spi_size
> 0) {
518 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
520 "Invalid spi_size in notification payload.\n");
523 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
525 plog(ASL_LEVEL_DEBUG
,
526 "Notification message %d:%s, "
527 "doi=%d proto_id=%d spi=%s(size=%d).\n",
528 type
, s_isakmp_notify_msg(type
),
529 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
534 /* Send the message data to the logs */
535 if(type
>= ISAKMP_NTYPE_MINERROR
&&
536 type
<= ISAKMP_NTYPE_MAXERROR
) {
537 l
= ntohs(notify
->h
.len
) - sizeof(*notify
) - notify
->spi_size
;
539 nraw
= (char*)notify
;
540 nraw
+= sizeof(*notify
) + notify
->spi_size
;
541 if ((ndata
= vmalloc(l
)) != NULL
) {
542 memcpy(ndata
->v
, nraw
, ndata
->l
);
545 binsanitize(ndata
->v
, ndata
->l
));
549 "Cannot allocate memory\n");
556 #ifdef ENABLE_VPNCONTROL_PORT
558 isakmp_info_vpncontrol_notify_ike_failed (phase1_handle_t
*iph1
, int isakmp_info_initiator
, int type
, vchar_t
*data
)
561 u_int32_t fail_reason
;
563 /* notify the API that we have received the delete */
564 if (iph1
->remote
->ss_family
== AF_INET
)
565 address
= ((struct sockaddr_in
*)(iph1
->remote
))->sin_addr
.s_addr
;
569 if (isakmp_info_initiator
== FROM_REMOTE
) {
570 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
571 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
574 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
575 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is pre-mature\n");
576 } else if (expired
) {
577 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
578 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is expired\n");
582 vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
, 0, NULL
);
586 if (type
== ISAKMP_INTERNAL_ERROR
||
587 type
<= ISAKMP_NTYPE_UNEQUAL_PAYLOAD_LENGTHS
) {
588 int premature
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_PREMATURE
);
589 int expired
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_EXPIRED
);
590 int subjname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJNAME
);
591 int subjaltname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJALTNAME
);
594 fail_reason
= VPNCTL_NTYPE_PEER_CERT_PREMATURE
;
595 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is pre-mature\n");
596 } else if (expired
) {
597 fail_reason
= VPNCTL_NTYPE_PEER_CERT_EXPIRED
;
598 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is expired\n");
599 } else if (subjname
) {
600 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJNAME
;
601 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject name not valid\n");
602 } else if (subjaltname
) {
603 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJALTNAME
;
604 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject alternate name not valid\n");
608 (void)vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
,
609 (data
? data
->l
: 0), (u_int8_t
*)(data
? data
->v
: NULL
));
614 #endif /* ENABLE_VPNCONTROL_PORT */
617 * handling of Deletion payload
620 isakmp_info_recv_d(phase1_handle_t
*iph1
, struct isakmp_pl_d
*delete, u_int32_t msgid
, int encrypted
)
623 phase1_handle_t
*del_ph1
;
629 if (ntohl(delete->doi
) != IPSEC_DOI
) {
631 "delete payload with invalid doi:%d.\n",
635 * At deconnexion time, Cisco VPN client does this
636 * with a zero DOI. Don't give up in that situation.
638 if (((iph1
->mode_cfg
->flags
&
639 ISAKMP_CFG_VENDORID_UNITY
) == 0) || (delete->doi
!= 0))
646 num_spi
= ntohs(delete->num_spi
);
647 tlen
= ntohs(delete->h
.len
) - sizeof(struct isakmp_pl_d
);
649 if (tlen
!= num_spi
* delete->spi_size
) {
651 "deletion payload with invalid length.\n");
655 plog(ASL_LEVEL_DEBUG
,
656 "delete payload for protocol %s\n",
657 s_ipsecdoi_proto(delete->proto_id
));
659 if(!iph1
->rmconf
->weak_phase1_check
&& !encrypted
) {
660 plog(ASL_LEVEL_WARNING
,
661 "Ignoring unencrypted delete payload "
662 "(check the weak_phase1_check option)\n");
666 switch (delete->proto_id
) {
667 case IPSECDOI_PROTO_ISAKMP
:
668 if (delete->spi_size
!= sizeof(isakmp_index
)) {
670 "delete payload with strange spi "
671 "size %d(proto_id:%d)\n",
672 delete->spi_size
, delete->proto_id
);
676 del_ph1
= ike_session_getph1byindex(iph1
->parent_session
, (isakmp_index
*)(delete + 1));
679 // hack: start a rekey now, if one was pending (only for client).
680 if (del_ph1
->sce_rekey
&&
681 del_ph1
->parent_session
&&
682 del_ph1
->parent_session
->is_client
&&
683 del_ph1
->parent_session
->established
) {
684 isakmp_ph1rekeyexpire(del_ph1
, FALSE
);
688 SCHED_KILL(del_ph1
->scr
);
691 * Do not delete IPsec SAs when receiving an IKE delete notification.
692 * Just delete the IKE SA.
694 #ifdef ENABLE_VPNCONTROL_PORT
695 if (del_ph1
->started_by_api
|| (del_ph1
->is_rekey
&& del_ph1
->parent_session
&& del_ph1
->parent_session
->is_client
)) {
696 if (ike_session_islast_ph1(del_ph1
)) {
697 isakmp_info_vpncontrol_notify_ike_failed(del_ph1
, FROM_REMOTE
, VPNCTL_NTYPE_PH1_DELETE
, NULL
);
701 isakmp_ph1expire(del_ph1
);
705 case IPSECDOI_PROTO_IPSEC_AH
:
706 case IPSECDOI_PROTO_IPSEC_ESP
:
707 if (delete->spi_size
!= sizeof(u_int32_t
)) {
709 "delete payload with strange spi "
710 "size %d(proto_id:%d)\n",
711 delete->spi_size
, delete->proto_id
);
714 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
715 ALIGNED_CAST(u_int32_t
*)(delete + 1), num_spi
, NULL
, NULL
); // Wcast-align fix (void*) - delete payload is aligned
718 case IPSECDOI_PROTO_IPCOMP
:
719 /* need to handle both 16bit/32bit SPI */
720 memset(&spi
, 0, sizeof(spi
));
721 if (delete->spi_size
== sizeof(spi
.spi16
[1])) {
722 memcpy(&spi
.spi16
[1], delete + 1,
723 sizeof(spi
.spi16
[1]));
724 } else if (delete->spi_size
== sizeof(spi
.spi32
))
725 memcpy(&spi
.spi32
, delete + 1, sizeof(spi
.spi32
));
728 "delete payload with strange spi "
729 "size %d(proto_id:%d)\n",
730 delete->spi_size
, delete->proto_id
);
733 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
734 &spi
.spi32
, num_spi
, NULL
, NULL
);
739 "deletion message received, "
740 "invalid proto_id: %d\n",
745 plog(ASL_LEVEL_DEBUG
, "purged SAs.\n");
751 * send Delete payload (for ISAKMP SA) in Informational exchange.
754 isakmp_info_send_d1(phase1_handle_t
*iph1
)
756 struct isakmp_pl_d
*d
;
757 vchar_t
*payload
= NULL
;
761 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
))
764 /* create delete payload */
766 /* send SPIs of inbound SAs. */
767 /* XXX should send outbound SAs's ? */
768 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
769 payload
= vmalloc(tlen
);
770 if (payload
== NULL
) {
772 "failed to get buffer for payload.\n");
776 d
= (struct isakmp_pl_d
*)payload
->v
;
777 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
778 d
->h
.len
= htons(tlen
);
779 d
->doi
= htonl(IPSEC_DOI
);
780 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
781 d
->spi_size
= sizeof(isakmp_index
);
782 d
->num_spi
= htons(1);
783 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
785 error
= isakmp_info_send_common(iph1
, payload
,
789 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
790 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
791 CONSTSTR("Delete ISAKMP-SA"),
792 CONSTSTR("Failed to transmit Delete-ISAKMP-SA message"));
794 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
795 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
796 CONSTSTR("Delete ISAKMP-SA"),
804 * send Delete payload (for IPsec SA) in Informational exchange, based on
805 * pfkey msg. It sends always single SPI.
808 isakmp_info_send_d2(phase2_handle_t
*iph2
)
810 phase1_handle_t
*iph1
;
812 struct isakmp_pl_d
*d
;
813 vchar_t
*payload
= NULL
;
818 if (!FSM_STATE_IS_ESTABLISHED(iph2
->status
))
822 * don't send delete information if there is no phase 1 handler.
823 * It's nonsensical to negotiate phase 1 to send the information.
825 iph1
= ike_session_get_established_ph1(iph2
->parent_session
);
827 iph1
= ike_session_getph1byaddr(iph2
->parent_session
, iph2
->src
, iph2
->dst
);
830 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
831 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
832 CONSTSTR("Information message"),
833 CONSTSTR("Failed to transmit Information message"));
834 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
835 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
836 CONSTSTR("Delete IPSEC-SA"),
837 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
838 plog(ASL_LEVEL_DEBUG
,
839 "No ph1 handler found, could not send DELETE_SA\n");
843 /* create delete payload */
844 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
846 /* send SPIs of inbound SAs. */
848 * XXX should I send outbound SAs's ?
849 * I send inbound SAs's SPI only at the moment because I can't
850 * decode any more if peer send encoded packet without aware of
851 * deletion of SA. Outbound SAs don't come under the situation.
853 tlen
= sizeof(*d
) + pr
->spisize
;
854 payload
= vmalloc(tlen
);
855 if (payload
== NULL
) {
856 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
857 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
858 CONSTSTR("Information message"),
859 CONSTSTR("Failed to transmit Information message"));
860 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
861 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
862 CONSTSTR("Delete IPSEC-SA"),
863 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
865 "failed to get buffer for payload.\n");
869 d
= (struct isakmp_pl_d
*)payload
->v
;
870 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
871 d
->h
.len
= htons(tlen
);
872 d
->doi
= htonl(IPSEC_DOI
);
873 d
->proto_id
= pr
->proto_id
;
874 d
->spi_size
= pr
->spisize
;
875 d
->num_spi
= htons(1);
877 * XXX SPI bits are left-filled, for use with IPComp.
878 * we should be switching to variable-length spi field...
880 spi
= (u_int8_t
*)&pr
->spi
;
881 spi
+= sizeof(pr
->spi
);
883 memcpy(d
+ 1, spi
, pr
->spisize
);
885 error
= isakmp_info_send_common(iph1
, payload
,
889 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
890 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
891 CONSTSTR("Delete IPSEC-SA"),
892 CONSTSTR("Failed to transmit Delete-IPSEC-SA"));
894 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
895 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
896 CONSTSTR("Delete IPSEC-SA"),
905 * send Notification payload (without ISAKMP SA) in an Informational exchange
908 isakmp_info_send_nx(struct isakmp
*isakmp
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
,
909 int type
, vchar_t
*data
)
911 phase1_handle_t
*iph1
= NULL
;
912 struct remoteconf
*rmconf
;
913 vchar_t
*payload
= NULL
;
916 struct isakmp_pl_n
*n
;
917 int spisiz
= 0; /* see below */
918 ike_session_t
*sess
= ike_session_get_session(local
, remote
, FALSE
);
920 /* search appropreate configuration */
921 rmconf
= getrmconf(remote
);
922 if (rmconf
== NULL
) {
923 IPSECSESSIONTRACEREVENT(sess
,
924 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
925 CONSTSTR("Information message"),
926 CONSTSTR("Failed to transmit Information message (no remote configuration)"));
928 "no configuration found for peer address.\n");
932 /* add new entry to isakmp status table. */
933 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
935 IPSECSESSIONTRACEREVENT(sess
,
936 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
937 CONSTSTR("Information message"),
938 CONSTSTR("Failed to transmit Information message (no new Phase 1)"));
940 "failed to allocate ph1");
944 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
945 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
946 fsm_set_state(&iph1
->status
, IKEV1_STATE_INFO
);
947 iph1
->rmconf
= rmconf
;
948 retain_rmconf(iph1
->rmconf
);
949 iph1
->side
= INITIATOR
;
950 iph1
->version
= isakmp
->v
;
952 iph1
->msgid
= 0; /* XXX */
954 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
961 iph1
->frag_chain
= NULL
;
964 /* copy remote address */
965 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
966 IPSECSESSIONTRACEREVENT(sess
,
967 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
968 CONSTSTR("Information message"),
969 CONSTSTR("Failed to transmit Information Message (can't copy Phase 1 addresses)"));
971 "failed to copy ph1 addresses");
973 iph1
= NULL
; /* deleted in copy_ph1addresses */
977 tlen
= sizeof(*n
) + spisiz
;
980 payload
= vmalloc(tlen
);
981 if (payload
== NULL
) {
982 IPSECSESSIONTRACEREVENT(sess
,
983 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
984 CONSTSTR("Information message"),
985 CONSTSTR("Failed to transmit Information Message (can't allocate payload)"));
987 "failed to get buffer to send.\n");
992 n
= (struct isakmp_pl_n
*)payload
->v
;
993 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
994 n
->h
.len
= htons(tlen
);
995 n
->doi
= htonl(IPSEC_DOI
);
996 n
->proto_id
= IPSECDOI_KEY_IKE
;
997 n
->spi_size
= spisiz
;
998 n
->type
= htons(type
);
1000 memset(n
+ 1, 0, spisiz
); /* XXX spisiz is always 0 */
1002 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1004 #ifdef ENABLE_VPNCONTROL_PORT
1005 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1007 if (ike_session_link_phase1(sess
, iph1
))
1010 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
1013 IPSECSESSIONTRACEREVENT(sess
,
1014 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1015 CONSTSTR("Without ISAKMP-SA"),
1016 CONSTSTR("Failed to transmit Without-ISAKMP-SA message"));
1018 IPSECSESSIONTRACEREVENT(sess
,
1019 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1020 CONSTSTR("Without ISAKMP-SA"),
1026 ike_session_unlink_phase1(iph1
);
1032 * send Notification payload (with ISAKMP SA) in an Informational exchange
1035 isakmp_info_send_n1(phase1_handle_t
*iph1
, int type
, vchar_t
*data
)
1037 vchar_t
*payload
= NULL
;
1040 struct isakmp_pl_n
*n
;
1044 * note on SPI size: which description is correct? I have chosen
1047 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
1048 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
1049 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
1050 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
1051 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
1053 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
||
1054 type
== ISAKMP_NTYPE_LOAD_BALANCE
)
1055 spisiz
= sizeof(isakmp_index
);
1059 tlen
= sizeof(*n
) + spisiz
;
1062 payload
= vmalloc(tlen
);
1063 if (payload
== NULL
) {
1064 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1065 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1066 CONSTSTR("ISAKMP-SA"),
1067 CONSTSTR("Failed to transmit ISAKMP-SA message (can't allocate payload)"));
1069 "failed to get buffer to send.\n");
1073 n
= (struct isakmp_pl_n
*)payload
->v
;
1074 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1075 n
->h
.len
= htons(tlen
);
1076 n
->doi
= htonl(iph1
->rmconf
->doitype
);
1077 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
1078 n
->spi_size
= spisiz
;
1079 n
->type
= htons(type
);
1081 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
1083 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1085 #ifdef ENABLE_VPNCONTROL_PORT
1086 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1089 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
1092 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1093 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1094 CONSTSTR("ISAKMP-SA"),
1095 CONSTSTR("Can't transmit ISAKMP-SA message"));
1097 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1098 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1099 CONSTSTR("ISAKMP-SA"),
1107 * send Notification payload (with IPsec SA) in an Informational exchange
1110 isakmp_info_send_n2(phase2_handle_t
*iph2
, int type
, vchar_t
*data
)
1112 phase1_handle_t
*iph1
= iph2
->ph1
;
1113 vchar_t
*payload
= NULL
;
1116 struct isakmp_pl_n
*n
;
1119 if (!iph2
->approval
)
1122 pr
= iph2
->approval
->head
;
1124 /* XXX must be get proper spi */
1125 tlen
= sizeof(*n
) + pr
->spisize
;
1128 payload
= vmalloc(tlen
);
1129 if (payload
== NULL
) {
1130 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1131 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1132 CONSTSTR("IPSEC-SA"),
1133 CONSTSTR("Failed to transmit IPSEC-SA message (can't allocate payload)"));
1135 "failed to get buffer to send.\n");
1139 n
= (struct isakmp_pl_n
*)payload
->v
;
1140 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1141 n
->h
.len
= htons(tlen
);
1142 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1143 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1144 n
->spi_size
= pr
->spisize
;
1145 n
->type
= htons(type
);
1146 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign
1148 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1150 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
1151 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
1154 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1155 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1156 CONSTSTR("IPSEC-SA"),
1157 CONSTSTR("Failed to transmit IPSEC-SA message"));
1159 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1160 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1161 CONSTSTR("IPSEC-SA"),
1170 * When ph1->skeyid_a == NULL, send message without encoding.
1173 isakmp_info_send_common(phase1_handle_t
*iph1
, vchar_t
*payload
, u_int32_t np
, int flags
)
1175 phase2_handle_t
*iph2
= NULL
;
1176 vchar_t
*hash
= NULL
;
1177 struct isakmp
*isakmp
;
1178 struct isakmp_gen
*gen
;
1183 /* add new entry to isakmp status table */
1184 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_INFO
);
1187 "failed to allocate ph2");
1191 iph2
->dst
= dupsaddr(iph1
->remote
);
1192 if (iph2
->dst
== NULL
) {
1194 "failed to duplicate remote address");
1195 ike_session_delph2(iph2
);
1198 iph2
->src
= dupsaddr(iph1
->local
);
1199 if (iph2
->src
== NULL
) {
1201 "failed to duplicate local address");
1202 ike_session_delph2(iph2
);
1205 switch (iph1
->remote
->ss_family
) {
1207 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1208 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1209 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1214 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1215 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1216 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1222 "invalid family: %d\n", iph1
->remote
->ss_family
);
1223 ike_session_delph2(iph2
);
1226 iph2
->side
= INITIATOR
;
1227 fsm_set_state(&iph2
->status
, IKEV1_STATE_INFO
);
1228 iph2
->msgid
= isakmp_newmsgid2(iph1
);
1230 /* get IV and HASH(1) if skeyid_a was generated. */
1231 if (iph1
->skeyid_a
!= NULL
) {
1232 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1233 if (iph2
->ivm
== NULL
) {
1235 "failed to generate IV");
1236 ike_session_delph2(iph2
);
1240 /* generate HASH(1) */
1241 hash
= oakley_compute_hash1(iph1
, iph2
->msgid
, payload
);
1244 "failed to generate HASH");
1245 ike_session_delph2(iph2
);
1249 /* initialized total buffer length */
1251 tlen
+= sizeof(*gen
);
1253 /* IKE-SA is not established */
1256 /* initialized total buffer length */
1259 if ((flags
& ISAKMP_FLAG_A
) == 0)
1260 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
1262 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
1264 ike_session_link_ph2_to_ph1(iph1
, iph2
);
1266 tlen
+= sizeof(*isakmp
) + payload
->l
;
1268 /* create buffer for isakmp payload */
1269 iph2
->sendbuf
= vmalloc(tlen
);
1270 if (iph2
->sendbuf
== NULL
) {
1272 "failed to get buffer to send.\n");
1276 /* create isakmp header */
1277 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
1278 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1279 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1280 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
1281 isakmp
->v
= iph1
->version
;
1282 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
1283 isakmp
->flags
= iph2
->flags
;
1284 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
1285 isakmp
->len
= htonl(tlen
);
1286 p
= (char *)(isakmp
+ 1);
1288 /* create HASH payload */
1290 gen
= (struct isakmp_gen
*)p
;
1291 gen
->np
= np
& 0xff;
1292 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
1294 memcpy(p
, hash
->v
, hash
->l
);
1299 memcpy(p
, payload
->v
, payload
->l
);
1302 #ifdef HAVE_PRINT_ISAKMP_C
1303 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1307 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
1310 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
1312 VPTRINIT(iph2
->sendbuf
);
1315 "failed to encrypt packet");
1318 iph2
->sendbuf
= tmp
;
1321 /* HDR*, HASH(1), N */
1322 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
1324 "failed to send packet");
1325 VPTRINIT(iph2
->sendbuf
);
1329 plog(ASL_LEVEL_DEBUG
,
1330 "sendto Information %s.\n", s_isakmp_nptype(np
));
1333 * don't resend notify message because peer can use Acknowledged
1334 * Informational if peer requires the reply of the notify message.
1337 /* XXX If Acknowledged Informational required, don't delete ph2handle */
1339 VPTRINIT(iph2
->sendbuf
);
1340 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1341 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1342 CONSTSTR("Information message"),
1349 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1350 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1351 CONSTSTR("Information message"),
1352 CONSTSTR("Failed to transmit Information message"));
1359 ike_session_unlink_phase2(iph2
);
1364 * add a notify payload to buffer by reallocating buffer.
1365 * If buf == NULL, the function only create a notify payload.
1367 * XXX Which is SPI to be included, inbound or outbound ?
1370 isakmp_add_pl_n(vchar_t
*buf0
, u_int8_t
**np_p
, int type
, struct saproto
*pr
, vchar_t
*data
)
1372 vchar_t
*buf
= NULL
;
1373 struct isakmp_pl_n
*n
;
1378 **np_p
= ISAKMP_NPTYPE_N
;
1380 tlen
= sizeof(*n
) + pr
->spisize
;
1386 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
1388 buf
= vmalloc(tlen
);
1391 "failed to get a payload buffer.\n");
1395 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
1396 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1397 n
->h
.len
= htons(tlen
);
1398 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1399 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1400 n
->spi_size
= pr
->spisize
;
1401 n
->type
= htons(type
);
1402 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign with cast
1404 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1406 /* save the pointer of next payload type */
1414 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
)
1416 vchar_t
*buf
= NULL
;
1417 struct sadb_msg
*msg
, *next
, *end
;
1419 struct sadb_lifetime
*lt
;
1420 struct sockaddr_storage
*src
, *dst
;
1421 phase2_handle_t
*iph2
;
1424 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1426 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
1428 plog(ASL_LEVEL_DEBUG
,
1429 "pfkey_dump_sadb returned nothing.\n");
1433 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1434 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1437 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1439 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1440 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1445 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1447 "pfkey_check (%s)\n", ipsec_strerror());
1452 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1454 || !mhp
[SADB_EXT_ADDRESS_SRC
]
1455 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
1459 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1460 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1461 lt
= ALIGNED_CAST(struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
1463 created
= lt
->sadb_lifetime_addtime
;
1467 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1468 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1473 /* XXX n^2 algorithm, inefficient */
1475 /* don't delete inbound SAs at the moment (just save them in inbound_spi) */
1476 /* XXX should we remove SAs with opposite direction as well? */
1477 if (CMPSADDR2(dst0
, dst
)) {
1482 for (i
= 0; i
< n
; i
++) {
1485 if (spi
[i
] != sa
->sadb_sa_spi
)
1489 * delete a relative phase 2 handler.
1490 * continue to process if no relative phase 2 handler
1493 if (inbound_spi
&& max_inbound_spi
&& j
< *max_inbound_spi
) {
1494 i_spi
= &inbound_spi
[j
];
1498 iph2
= ike_session_getph2bysaidx2(src
, dst
, proto
, spi
[i
], i_spi
);
1500 pfkey_send_delete(lcconf
->sock_pfkey
,
1501 msg
->sadb_msg_satype
,
1503 src
, dst
, sa
->sadb_sa_spi
);
1507 ike_session_unlink_phase2(iph2
);
1513 plog(ASL_LEVEL_INFO
, "Purged IPsec-SA proto_id=%s spi=%u.\n",
1514 s_ipsecdoi_proto(proto
),
1521 if (max_inbound_spi
) {
1522 *max_inbound_spi
= j
;
1530 * delete all phase2 sa relatived to the destination address.
1531 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1532 * an INITIAL-CONTACT if we have contacted the peer. This matches the
1533 * Sun IKE behavior, and makes rekeying work much better when the peer
1537 info_recv_initialcontact(phase1_handle_t
*iph1
)
1539 vchar_t
*buf
= NULL
;
1540 struct sadb_msg
*msg
, *next
, *end
;
1542 struct sockaddr_storage
*src
, *dst
;
1543 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1545 phase2_handle_t
*iph2
;
1553 // TODO: make sure that is_rekey is cleared for this. and session indicates the same
1555 loc
= racoon_strdup(saddrwop2str(iph1
->local
));
1556 rem
= racoon_strdup(saddrwop2str(iph1
->remote
));
1561 * Purge all IPSEC-SAs for the peer. We can do this
1562 * the easy way (using a PF_KEY SADB_DELETE extension)
1563 * or we can do it the hard way.
1565 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1566 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
1568 plog(ASL_LEVEL_INFO
,
1569 "purging %s SAs for %s -> %s\n",
1570 pfkey_satypes
[i
].ps_name
, loc
, rem
);
1571 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1572 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1573 iph1
->local
, iph1
->remote
) == -1) {
1575 "delete_all %s -> %s failed for %s (%s)\n",
1577 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1581 ike_session_deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
1583 plog(ASL_LEVEL_INFO
,
1584 "purging %s SAs for %s -> %s\n",
1585 pfkey_satypes
[i
].ps_name
, rem
, loc
);
1586 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1587 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1588 iph1
->remote
, iph1
->local
) == -1) {
1590 "delete_all %s -> %s failed for %s (%s)\n",
1592 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1596 ike_session_deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1608 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1610 plog(ASL_LEVEL_DEBUG
,
1611 "pfkey_dump_sadb returned nothing.\n");
1615 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1616 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1619 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1621 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1622 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1627 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1629 "pfkey_check (%s)\n", ipsec_strerror());
1634 if (mhp
[SADB_EXT_SA
] == NULL
1635 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1636 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1640 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
]; // Wcast-align fix (void*) - buffer of pointers to aligned structs
1641 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1642 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1644 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1645 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1651 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1652 * announces the sender of the message was rebooted.
1653 * it is interpreted to delete all SAs which source address
1654 * is the sender of the message.
1655 * racoon only deletes SA which is matched both the
1656 * source address and the destination accress.
1660 * XXX RFC 3947 says that whe MUST NOT use IP+port to find old SAs
1663 if(iph1
->natt_flags
& NAT_DETECTED
){
1664 if (CMPSADDR(iph1
->local
, src
) == 0 &&
1665 CMPSADDR(iph1
->remote
, dst
) == 0)
1667 else if (CMPSADDR(iph1
->remote
, src
) == 0 &&
1668 CMPSADDR(iph1
->local
, dst
) == 0)
1676 /* If there is no NAT-T, we don't have to check addr + port...
1677 * XXX what about a configuration with a remote peers which is not
1678 * NATed, but which NATs some other peers ?
1679 * Here, the INITIAl-CONTACT would also flush all those NATed peers !!
1681 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1682 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1684 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1685 cmpsaddrwop(iph1
->local
, dst
) == 0)
1693 * Make sure this is an SATYPE that we manage.
1694 * This is gross; too bad we couldn't do it the
1697 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1698 if (pfkey_satypes
[i
].ps_satype
==
1699 msg
->sadb_msg_satype
)
1702 if (i
== pfkey_nsatypes
) {
1707 plog(ASL_LEVEL_INFO
,
1708 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1709 pfkey_send_delete(lcconf
->sock_pfkey
,
1710 msg
->sadb_msg_satype
,
1711 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1714 * delete a relative phase 2 handler.
1715 * continue to process if no relative phase 2 handler
1718 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1719 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1722 ike_session_unlink_phase2(iph2
);
1732 isakmp_check_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase1_handle_t
*iph1
)
1734 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1736 plog(ASL_LEVEL_DEBUG
,
1737 "Notify Message received\n");
1739 switch (ntohs(notify
->type
)) {
1740 case ISAKMP_NTYPE_CONNECTED
:
1741 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1742 case ISAKMP_NTYPE_REPLAY_STATUS
:
1743 case ISAKMP_NTYPE_HEARTBEAT
:
1744 #ifdef ENABLE_HYBRID
1745 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1747 plog(ASL_LEVEL_WARNING
,
1748 "Ignore %s notification.\n",
1749 s_isakmp_notify_msg(ntohs(notify
->type
)));
1751 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1752 plog(ASL_LEVEL_WARNING
,
1753 "Ignore INITIAL-CONTACT notification, "
1754 "because it is only accepted after Phase 1.\n");
1756 case ISAKMP_NTYPE_LOAD_BALANCE
:
1757 plog(ASL_LEVEL_WARNING
,
1758 "Ignore LOAD-BALANCE notification, "
1759 "because it is only accepted after Phase 1.\n");
1762 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1764 "Received unknown notification type %s.\n",
1765 s_isakmp_notify_msg(ntohs(notify
->type
)));
1772 isakmp_check_ph2_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase2_handle_t
*iph2
)
1774 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1776 plog(ASL_LEVEL_DEBUG
,
1777 "Phase 2 Notify Message received\n");
1779 switch (ntohs(notify
->type
)) {
1780 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1781 return((void)isakmp_ph2_responder_lifetime(iph2
,
1782 (struct isakmp_pl_resp_lifetime
*)notify
));
1784 case ISAKMP_NTYPE_CONNECTED
:
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(iph2
->ph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1807 "Received unknown notification type %s.\n",
1808 s_isakmp_notify_msg(ntohs(notify
->type
)));
1814 #ifdef ENABLE_VPNCONTROL_PORT
1816 isakmp_info_recv_lb(phase1_handle_t
*iph1
, struct isakmp_pl_lb
*n
, int encrypted
)
1819 if (iph1
->side
!= INITIATOR
)
1821 plog(ASL_LEVEL_DEBUG
,
1822 "LOAD-BALANCE notification ignored - we are not the initiator.\n");
1825 if (iph1
->remote
->ss_family
!= AF_INET
) {
1826 plog(ASL_LEVEL_DEBUG
,
1827 "LOAD-BALANCE notification ignored - only supported for IPv4.\n");
1831 plog(ASL_LEVEL_DEBUG
,
1832 "LOAD-BALANCE notification ignored - not protected.\n");
1835 if (ntohs(n
->h
.len
) != sizeof(struct isakmp_pl_lb
)) {
1836 plog(ASL_LEVEL_DEBUG
,
1837 "Invalid length of payload\n");
1840 vpncontrol_notify_ike_failed(ISAKMP_NTYPE_LOAD_BALANCE
, FROM_REMOTE
,
1841 ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
, 4, (u_int8_t
*)(&(n
->address
)));
1843 plog(ASL_LEVEL_NOTICE
,
1844 "Received LOAD_BALANCE notification.\n");
1846 if (((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
!= ntohl(n
->address
)) {
1847 plog(ASL_LEVEL_DEBUG
,
1848 "Deleting old Phase 1 because of LOAD_BALANCE notification - redirect address=%x.\n",
1851 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1852 isakmp_info_send_d1(iph1
);
1854 isakmp_ph1expire(iph1
);
1863 isakmp_info_recv_r_u (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1865 struct isakmp_pl_ru
*ru_ack
;
1866 vchar_t
*payload
= NULL
;
1870 plog(ASL_LEVEL_DEBUG
,
1871 "DPD R-U-There received\n");
1873 /* XXX should compare cookies with iph1->index?
1874 Or is this already done by calling function? */
1875 tlen
= sizeof(*ru_ack
);
1876 payload
= vmalloc(tlen
);
1877 if (payload
== NULL
) {
1878 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1879 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1880 CONSTSTR("R-U-THERE? ACK"),
1881 CONSTSTR("Failed to transmit DPD response"));
1883 "failed to get buffer to send.\n");
1887 ru_ack
= (struct isakmp_pl_ru
*)payload
->v
;
1888 ru_ack
->h
.np
= ISAKMP_NPTYPE_NONE
;
1889 ru_ack
->h
.len
= htons(tlen
);
1890 ru_ack
->doi
= htonl(IPSEC_DOI
);
1891 ru_ack
->type
= htons(ISAKMP_NTYPE_R_U_THERE_ACK
);
1892 ru_ack
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ? */
1893 ru_ack
->spi_size
= sizeof(isakmp_index
);
1894 memcpy(ru_ack
->i_ck
, ru
->i_ck
, sizeof(cookie_t
));
1895 memcpy(ru_ack
->r_ck
, ru
->r_ck
, sizeof(cookie_t
));
1896 ru_ack
->data
= ru
->data
;
1898 /* XXX Should we do FLAG_A ? */
1899 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
,
1903 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1904 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1905 CONSTSTR("R-U-THERE? ACK"),
1906 CONSTSTR("Failed to transmit DPD ack"));
1908 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1909 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1910 CONSTSTR("R-U-THERE? ACK"),
1914 plog(ASL_LEVEL_DEBUG
, "received a valid R-U-THERE, ACK sent\n");
1916 /* Should we mark tunnel as active ? */
1921 isakmp_info_recv_r_u_ack (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1924 plog(ASL_LEVEL_DEBUG
,
1925 "DPD R-U-There-Ack received\n");
1927 /* XXX Maintain window of acceptable sequence numbers ?
1928 * => ru->data <= iph2->dpd_seq &&
1929 * ru->data >= iph2->dpd_seq - iph2->dpd_fails ? */
1930 if (ntohl(ru
->data
) != iph1
->dpd_seq
) {
1932 "Wrong DPD sequence number (%d, %d expected).\n",
1933 ntohl(ru
->data
), iph1
->dpd_seq
);
1937 if (memcmp(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
)) ||
1938 memcmp(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
))) {
1940 "Cookie mismatch in DPD ACK!.\n");
1944 iph1
->dpd_fails
= 0;
1949 iph1
->dpd_lastack
= time(NULL
);
1951 SCHED_KILL(iph1
->dpd_r_u
);
1953 isakmp_sched_r_u(iph1
, 0);
1955 if (iph1
->side
== INITIATOR
) {
1956 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1957 IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RESP
,
1958 CONSTSTR("Initiator DPD Response"),
1961 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1962 IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RESP
,
1963 CONSTSTR("Responder DPD Response"),
1966 plog(ASL_LEVEL_DEBUG
, "received an R-U-THERE-ACK\n");
1968 #ifdef ENABLE_VPNCONTROL_PORT
1969 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1970 #endif /* ENABLE_VPNCONTROL_PORT */
1977 * send Delete payload (for ISAKMP SA) in Informational exchange.
1980 isakmp_info_send_r_u(void *arg
)
1982 phase1_handle_t
*iph1
= arg
;
1984 /* create R-U-THERE payload */
1985 struct isakmp_pl_ru
*ru
;
1986 vchar_t
*payload
= NULL
;
1990 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1991 plog(ASL_LEVEL_DEBUG
, "DPD r-u send aborted, invalid Phase 1 status %d....\n",
1996 if (iph1
->dpd_fails
>= iph1
->rmconf
->dpd_maxfails
) {
1999 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2000 IPSECSESSIONEVENTCODE_IKEV1_DPD_MAX_RETRANSMIT
,
2001 CONSTSTR("DPD maximum retransmits"),
2002 CONSTSTR("maxed-out of DPD requests without receiving an ack"));
2004 if (iph1
->remote
->ss_family
== AF_INET
)
2005 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
2008 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_PEER_DEAD
, FROM_LOCAL
, address
, 0, NULL
);
2011 plog(ASL_LEVEL_DEBUG
,
2012 "DPD: remote seems to be dead\n");
2014 /* Do not reschedule here: phase1 is deleted,
2015 * DPD will be reactivated when a new ph1 will be negociated
2021 payload
= vmalloc(tlen
);
2022 if (payload
== NULL
) {
2023 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2024 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2025 CONSTSTR("R-U-THERE?"),
2026 CONSTSTR("Failed to transmit DPD request"));
2028 "failed to get buffer for payload.\n");
2031 ru
= (struct isakmp_pl_ru
*)payload
->v
;
2032 ru
->h
.np
= ISAKMP_NPTYPE_NONE
;
2033 ru
->h
.len
= htons(tlen
);
2034 ru
->doi
= htonl(IPSEC_DOI
);
2035 ru
->type
= htons(ISAKMP_NTYPE_R_U_THERE
);
2036 ru
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ?*/
2037 ru
->spi_size
= sizeof(isakmp_index
);
2039 memcpy(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
));
2040 memcpy(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
));
2042 if (iph1
->dpd_seq
== 0){
2043 /* generate a random seq which is not too big */
2045 iph1
->dpd_seq
= rand() & 0x0fff;
2048 ru
->data
= htonl(iph1
->dpd_seq
);
2050 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
2053 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2054 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2055 CONSTSTR("R-U-THERE?"),
2056 CONSTSTR("Failed to transmit DPD request"));
2058 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2059 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
2060 CONSTSTR("R-U-THERE?"),
2064 if (iph1
->side
== INITIATOR
) {
2065 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2066 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_REQ
,
2067 CONSTSTR("Initiator DPD Request"),
2070 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2071 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_REQ
,
2072 CONSTSTR("Responder DPD Request"),
2075 plog(ASL_LEVEL_DEBUG
,
2076 "DPD R-U-There sent (%d)\n", error
);
2078 /* will be decreased if ACK received... */
2081 /* Reschedule the r_u_there with a short delay,
2082 * will be deleted/rescheduled if ACK received before */
2083 isakmp_sched_r_u(iph1
, 1);
2085 plog(ASL_LEVEL_DEBUG
,
2086 "rescheduling send_r_u (%d).\n", iph1
->rmconf
->dpd_retry
);
2090 * monitor DPD (ALGORITHM_INBOUND_DETECT) Informational exchange.
2093 isakmp_info_monitor_r_u_algo_inbound_detect (phase1_handle_t
*iph1
)
2095 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2096 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) aborted, invalid Phase 1 status %d....\n",
2101 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) ....\n");
2103 // check phase1 for ike packets received from peer
2104 if (iph1
->peer_sent_ike
) {
2105 // yes, reshedule check
2106 iph1
->peer_sent_ike
= 0;
2108 /* ike packets received from peer... reschedule dpd */
2109 isakmp_sched_r_u(iph1
, 0);
2111 plog(ASL_LEVEL_DEBUG
,
2112 "ike packets received from peer... reschedule monitor.\n");
2117 // after ike packets, next we check if any data was received
2118 if (!iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2119 isakmp_info_send_r_u(iph1
);
2121 isakmp_sched_r_u(iph1
, 0);
2123 plog(ASL_LEVEL_DEBUG
,
2124 "rescheduling DPD monitoring (for ALGORITHM_INBOUND_DETECT).\n");
2126 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2130 * monitor DPD (ALGORITHM_BLACKHOLE_DETECT) Informational exchange.
2133 isakmp_info_monitor_r_u_algo_blackhole_detect (phase1_handle_t
*iph1
)
2135 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2136 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) aborted, invalid Phase 1 status %d....\n",
2141 plog(ASL_LEVEL_DEBUG
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) ....\n");
2143 // check if data was sent but none was received
2144 if (iph1
->parent_session
->i_sent_data_sc_dpd
&&
2145 !iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2146 isakmp_info_send_r_u(iph1
);
2148 isakmp_sched_r_u(iph1
, 0);
2150 plog(ASL_LEVEL_DEBUG
,
2151 "rescheduling DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) i = %d, peer %d.\n",
2152 iph1
->parent_session
->i_sent_data_sc_dpd
,
2153 iph1
->parent_session
->peer_sent_data_sc_dpd
);
2155 iph1
->parent_session
->i_sent_data_sc_dpd
= 0;
2156 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2160 * monitor DPD Informational exchange.
2163 isakmp_info_monitor_r_u(void *arg
)
2165 phase1_handle_t
*iph1
= arg
;
2167 if (iph1
&& iph1
->rmconf
) {
2168 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
) {
2169 isakmp_info_monitor_r_u_algo_inbound_detect(iph1
);
2170 } else if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2171 isakmp_info_monitor_r_u_algo_blackhole_detect(iph1
);
2173 plog(ASL_LEVEL_DEBUG
, "DPD monitoring aborted, invalid algorithm %d....\n",
2174 iph1
->rmconf
->dpd_algo
);
2179 /* Schedule a new R-U-THERE */
2181 isakmp_sched_r_u(phase1_handle_t
*iph1
, int retry
)
2184 iph1
->rmconf
== NULL
)
2188 if(iph1
->dpd_support
== 0 ||
2189 iph1
->rmconf
->dpd_interval
== 0)
2193 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_retry
,
2194 isakmp_info_send_r_u
, iph1
);
2196 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
||
2197 iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2198 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2199 isakmp_info_monitor_r_u
, iph1
);
2201 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2202 isakmp_info_send_r_u
, iph1
);
2210 * punts dpd for later because of some activity that:
2211 * 1) implicitly does dpd (e.g. phase2 exchanges), or
2212 * 2) indicates liveness (e.g. received ike packets).
2215 isakmp_reschedule_info_monitor_if_pending (phase1_handle_t
*iph1
, char *reason
)
2218 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2219 !iph1
->dpd_support
||
2220 !iph1
->rmconf
->dpd_interval
||
2221 iph1
->rmconf
->dpd_algo
== DPD_ALGO_DEFAULT
) {
2225 if (!iph1
->peer_sent_ike
) {
2226 SCHED_KILL(iph1
->dpd_r_u
);
2228 isakmp_sched_r_u(iph1
, 0);
2230 plog(ASL_LEVEL_DEBUG
,
2231 "%s... rescheduling send_r_u.\n",
2234 iph1
->peer_sent_ike
++;