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>
76 #include "ike_session.h"
78 #include "localconf.h"
79 #include "remoteconf.h"
84 #include "isakmp_var.h"
87 #include "isakmp_xauth.h"
88 #include "isakmp_unity.h"
89 #include "isakmp_cfg.h"
91 #include "isakmp_inf.h"
93 #include "ipsec_doi.h"
94 #include "crypto_openssl.h"
97 #include "algorithm.h"
101 #include "nattraversal.h"
103 #include "vpn_control_var.h"
104 #include "vpn_control.h"
105 #include "ike_session.h"
106 #include "ipsecSessionTracer.h"
107 #include "ipsecMessageTracer.h"
109 /* information exchange */
110 static int isakmp_info_recv_n (phase1_handle_t
*, struct isakmp_pl_n
*, u_int32_t
, int);
111 static int isakmp_info_recv_d (phase1_handle_t
*, struct isakmp_pl_d
*, u_int32_t
, int);
114 static int isakmp_info_recv_r_u (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
115 static int isakmp_info_recv_r_u_ack (phase1_handle_t
*, struct isakmp_pl_ru
*, u_int32_t
);
118 #ifdef ENABLE_VPNCONTROL_PORT
119 static int isakmp_info_recv_lb (phase1_handle_t
*, struct isakmp_pl_lb
*lb
, int);
123 isakmp_ph1_responder_lifetime (phase1_handle_t
*iph1
, struct isakmp_pl_resp_lifetime
*notify
)
127 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
129 "invalid spi_size in notification payload.\n");
132 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
134 plog(ASL_LEVEL_NOTICE
,
135 "notification message ISAKMP-SA RESPONDER-LIFETIME, "
136 "doi=%d proto_id=%d spi=%s(size=%d).\n",
137 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
141 struct isakmp_pl_attr
*attrpl
;
142 int len
= ntohs(notify
->h
.len
) - (sizeof(*notify
) + notify
->spi_size
);
144 attrpl
= (struct isakmp_pl_attr
*)((char *)(notify
+ 1) + notify
->spi_size
);
154 isakmp_ph2_responder_lifetime (phase2_handle_t
*iph2
, struct isakmp_pl_resp_lifetime
*notify
)
158 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
160 "invalid spi_size in notification payload.\n");
163 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
165 plog(ASL_LEVEL_NOTICE
,
166 "notification message IPSEC-SA RESPONDER-LIFETIME, "
167 "doi=%d proto_id=%d spi=%s(size=%d).\n",
168 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
177 * Information Exchange
180 * receive Information
183 isakmp_info_recv(phase1_handle_t
*iph1
, vchar_t
*msg0
)
186 vchar_t
*pbuf
= NULL
;
189 struct isakmp
*isakmp
;
190 struct isakmp_gen
*gen
;
191 struct isakmp_parse_t
*pa
;
193 vchar_t
*hash
, *payload
;
194 struct isakmp_gen
*nd
;
200 plog(ASL_LEVEL_NOTICE
, "receive Information.\n");
202 encrypted
= ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
);
203 msgid
= ((struct isakmp
*)msg0
->v
)->msgid
;
205 /* Use new IV to decrypt Informational message. */
207 struct isakmp_ivm
*ivm
;
209 if (iph1
->ivm
== NULL
) {
210 plog(ASL_LEVEL_ERR
, "iph1->ivm == NULL\n");
211 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
212 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
213 CONSTSTR("Information message"),
214 CONSTSTR("Failed to process Information Message (no IV)"));
219 ivm
= oakley_newiv2(iph1
, ((struct isakmp
*)msg0
->v
)->msgid
);
222 "failed to compute IV\n");
223 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
224 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
225 CONSTSTR("Information message"),
226 CONSTSTR("Failed to process Information Message (can't compute IV)"));
230 msg
= oakley_do_decrypt(iph1
, msg0
, ivm
->iv
, ivm
->ive
);
234 "failed to decrypt packet\n");
235 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
236 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
237 CONSTSTR("Information message"),
238 CONSTSTR("Failed to decrypt Information message"));
246 if (msg
->l
< sizeof(*isakmp
) + sizeof(*gen
)) {
248 "ignore information because the "
249 "message is way too short\n");
253 isakmp
= (struct isakmp
*)msg
->v
;
254 gen
= (struct isakmp_gen
*)((caddr_t
)isakmp
+ sizeof(struct isakmp
));
258 if (isakmp
->np
!= ISAKMP_NPTYPE_HASH
) {
260 "ignore information because the "
261 "message has no hash payload.\n");
265 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
266 (!iph1
->approval
|| !iph1
->skeyid_a
)) {
268 "ignore information because ISAKMP-SA "
269 "has not been established yet.\n");
274 if (msg
->l
< sizeof(*isakmp
) + ntohs(gen
->len
) + sizeof(*nd
)) {
276 "ignore information because the "
277 "message is too short\n");
281 p
= (caddr_t
) gen
+ sizeof(struct isakmp_gen
);
282 nd
= (struct isakmp_gen
*) ((caddr_t
) gen
+ ntohs(gen
->len
));
284 /* nd length check */
285 if (ntohs(nd
->len
) > msg
->l
- (sizeof(struct isakmp
) +
288 "too long payload length (broken message?)\n");
292 if (ntohs(nd
->len
) < sizeof(*nd
)) {
294 "too short payload length (broken message?)\n");
298 payload
= vmalloc(ntohs(nd
->len
));
299 if (payload
== NULL
) {
301 "cannot allocate memory\n");
305 memcpy(payload
->v
, (caddr_t
) nd
, ntohs(nd
->len
));
308 hash
= oakley_compute_hash1(iph1
, isakmp
->msgid
, payload
);
311 "cannot compute hash\n");
317 if (ntohs(gen
->len
) - sizeof(struct isakmp_gen
) != hash
->l
) {
319 "ignore information due to hash length mismatch\n");
326 if (timingsafe_bcmp(p
, hash
->v
, hash
->l
) != 0) {
328 "ignore information due to hash mismatch\n");
335 plog(ASL_LEVEL_DEBUG
, "hash validated.\n");
340 /* make sure phase 1 was not yet at encrypted state */
341 switch (iph1
->etype
) {
342 case ISAKMP_ETYPE_AGG
:
343 // %%%%% should also check for unity/mode cfg - last pkt is encrypted in such cases
344 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
345 ((iph1
->side
== INITIATOR
&& iph1
->status
== IKEV1_STATE_AGG_I_MSG3SENT
) ||
346 (iph1
->side
== RESPONDER
&& iph1
->status
== IKEV1_STATE_AGG_R_MSG3RCVD
))) {
350 case ISAKMP_ETYPE_IDENT
:
351 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
352 ((iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_IDENT_I_MSG5SENT
353 || iph1
->status
== IKEV1_STATE_IDENT_I_MSG6RCVD
)) ||
354 (iph1
->side
== RESPONDER
&& (iph1
->status
== IKEV1_STATE_IDENT_R_MSG5RCVD
)))) {
359 if ((np
== ISAKMP_NPTYPE_NONE
) &&
360 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
361 (iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_AGG_I_MSG1SENT
))) {
362 // proposal rejected by peer, terminate now.
367 "%s message must be encrypted, status 0x%x, side %d\n",
368 s_isakmp_nptype(np
), iph1
->status
, iph1
->side
);
374 if (!(pbuf
= isakmp_parse(msg
))) {
376 "failed to parse msg");
382 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
; pa
->type
; pa
++) { // Wcast-align fix (void*) - aligned buffer of aligned (unpacked) structs
384 case ISAKMP_NPTYPE_HASH
:
387 case ISAKMP_NPTYPE_N
:
388 if ((ntohs(((struct isakmp_pl_n
*)pa
->ptr
)->type
) == ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
) &&
389 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) &&
390 (iph1
->side
== INITIATOR
&& (iph1
->status
== IKEV1_STATE_AGG_I_MSG1SENT
))) {
391 // proposal rejected by peer, terminate now.
394 "%s message with %s notification receveid, status 0x%x, side %d\n",
395 s_isakmp_nptype(np
), s_isakmp_notify_msg(ISAKMP_NTYPE_NO_PROPOSAL_CHOSEN
), iph1
->status
, iph1
->side
);
398 error
= isakmp_info_recv_n(iph1
,
399 (struct isakmp_pl_n
*)pa
->ptr
,
402 case ISAKMP_NPTYPE_D
:
403 error
= isakmp_info_recv_d(iph1
,
404 (struct isakmp_pl_d
*)pa
->ptr
,
407 case ISAKMP_NPTYPE_NONCE
:
408 /* XXX to be 6.4.2 ike-01.txt */
409 /* XXX IV is to be synchronized. */
411 "ignore Acknowledged Informational\n");
414 /* don't send information, see isakmp_ident_r1() */
417 "reject the packet, "
418 "received unexpected payload type %s.\n",
419 s_isakmp_nptype(gen
->np
));
427 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
428 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
429 CONSTSTR("Information message"),
434 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
435 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
436 CONSTSTR("Information message"),
437 CONSTSTR("Failed to process Information Message"));
444 ike_session_t
*session
= NULL
;
446 if (session
= iph1
->parent_session
) {
447 gettimeofday(&session
->stop_timestamp
, NULL
);
448 if (!session
->term_reason
) {
449 session
->term_reason
= ike_session_stopped_by_peer
;
451 ike_session_purge_ph1s_by_session(session
);
458 * handling of Notification payload
461 isakmp_info_recv_n(phase1_handle_t
*iph1
, struct isakmp_pl_n
*notify
, u_int32_t msgid
, int encrypted
)
469 type
= ntohs(notify
->type
);
472 case ISAKMP_NTYPE_CONNECTED
:
473 case ISAKMP_NTYPE_REPLAY_STATUS
:
475 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
479 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
481 return(isakmp_ph1_responder_lifetime(iph1
,
482 (struct isakmp_pl_resp_lifetime
*)notify
));
485 case ISAKMP_NTYPE_INITIAL_CONTACT
:
487 info_recv_initialcontact(iph1
);
492 case ISAKMP_NTYPE_R_U_THERE
:
494 return isakmp_info_recv_r_u(iph1
,
495 (struct isakmp_pl_ru
*)notify
, msgid
);
497 case ISAKMP_NTYPE_R_U_THERE_ACK
:
499 return isakmp_info_recv_r_u_ack(iph1
,
500 (struct isakmp_pl_ru
*)notify
, msgid
);
503 #ifdef ENABLE_VPNCONTROL_PORT
504 case ISAKMP_NTYPE_LOAD_BALANCE
:
505 isakmp_info_recv_lb(iph1
, (struct isakmp_pl_lb
*)notify
, encrypted
);
511 /* XXX there is a potential of dos attack. */
512 if(type
>= ISAKMP_NTYPE_MINERROR
&&
513 type
<= ISAKMP_NTYPE_MAXERROR
) {
515 /* don't think this realy deletes ph1 ? */
517 "Delete Phase 1 handle.\n");
520 if (ike_session_getph2bymsgid(iph1
, msgid
) == NULL
) {
522 "Fatal %s notify messsage, "
523 "Phase 1 should be deleted.\n",
524 s_isakmp_notify_msg(type
));
527 "Fatal %s notify messsage, "
528 "Phase 2 should be deleted.\n",
529 s_isakmp_notify_msg(type
));
534 "Unhandled notify message %s, "
535 "no Phase 2 handle found.\n",
536 s_isakmp_notify_msg(type
));
542 /* get spi if specified and allocate */
543 if(notify
->spi_size
> 0) {
544 if (ntohs(notify
->h
.len
) < sizeof(*notify
) + notify
->spi_size
) {
546 "Invalid spi_size in notification payload.\n");
549 spi
= val2str((char *)(notify
+ 1), notify
->spi_size
);
551 plog(ASL_LEVEL_NOTICE
,
552 "Notification message %d:%s, "
553 "doi=%d proto_id=%d spi=%s(size=%d).\n",
554 type
, s_isakmp_notify_msg(type
),
555 ntohl(notify
->doi
), notify
->proto_id
, spi
, notify
->spi_size
);
560 /* Send the message data to the logs */
561 if(type
>= ISAKMP_NTYPE_MINERROR
&&
562 type
<= ISAKMP_NTYPE_MAXERROR
) {
563 l
= ntohs(notify
->h
.len
) - sizeof(*notify
) - notify
->spi_size
;
565 nraw
= (char*)notify
;
566 nraw
+= sizeof(*notify
) + notify
->spi_size
;
567 if ((ndata
= vmalloc(l
)) != NULL
) {
568 memcpy(ndata
->v
, nraw
, ndata
->l
);
571 binsanitize(ndata
->v
, ndata
->l
));
575 "Cannot allocate memory\n");
582 #ifdef ENABLE_VPNCONTROL_PORT
584 isakmp_info_vpncontrol_notify_ike_failed (phase1_handle_t
*iph1
, int isakmp_info_initiator
, int type
, vchar_t
*data
)
586 u_int32_t address
= iph1_get_remote_v4_address(iph1
);
587 u_int32_t fail_reason
;
589 /* notify the API that we have received the delete */
591 if (isakmp_info_initiator
== FROM_REMOTE
) {
592 int premature
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_PREMATURE
);
593 int expired
= oakley_find_status_in_certchain(iph1
->cert
, CERT_STATUS_EXPIRED
);
596 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_PREMATURE
;
597 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is pre-mature\n");
598 } else if (expired
) {
599 fail_reason
= VPNCTL_NTYPE_LOCAL_CERT_EXPIRED
;
600 plog(ASL_LEVEL_NOTICE
, ">>> Server reports client's certificate is expired\n");
604 vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
, 0, NULL
);
608 if (type
== ISAKMP_INTERNAL_ERROR
||
609 type
<= ISAKMP_NTYPE_UNEQUAL_PAYLOAD_LENGTHS
) {
610 int premature
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_PREMATURE
);
611 int expired
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_EXPIRED
);
612 int subjname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJNAME
);
613 int subjaltname
= oakley_find_status_in_certchain(iph1
->cert_p
, CERT_STATUS_INVALID_SUBJALTNAME
);
616 fail_reason
= VPNCTL_NTYPE_PEER_CERT_PREMATURE
;
617 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is pre-mature\n");
618 } else if (expired
) {
619 fail_reason
= VPNCTL_NTYPE_PEER_CERT_EXPIRED
;
620 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate is expired\n");
621 } else if (subjname
) {
622 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJNAME
;
623 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject name not valid\n");
624 } else if (subjaltname
) {
625 fail_reason
= VPNCTL_NTYPE_PEER_CERT_INVALID_SUBJALTNAME
;
626 plog(ASL_LEVEL_NOTICE
, ">>> Server's certificate subject alternate name not valid\n");
630 (void)vpncontrol_notify_ike_failed(fail_reason
, isakmp_info_initiator
, address
,
631 (data
? data
->l
: 0), (u_int8_t
*)(data
? data
->v
: NULL
));
636 #endif /* ENABLE_VPNCONTROL_PORT */
639 * handling of Deletion payload
642 isakmp_info_recv_d(phase1_handle_t
*iph1
, struct isakmp_pl_d
*delete, u_int32_t msgid
, int encrypted
)
645 phase1_handle_t
*del_ph1
;
651 if (ntohl(delete->doi
) != IPSEC_DOI
) {
653 "delete payload with invalid doi:%d.\n",
657 * At deconnexion time, Cisco VPN client does this
658 * with a zero DOI. Don't give up in that situation.
660 if (((iph1
->mode_cfg
->flags
&
661 ISAKMP_CFG_VENDORID_UNITY
) == 0) || (delete->doi
!= 0))
668 num_spi
= ntohs(delete->num_spi
);
669 tlen
= ntohs(delete->h
.len
) - sizeof(struct isakmp_pl_d
);
671 if (tlen
!= num_spi
* delete->spi_size
) {
673 "deletion payload with invalid length.\n");
677 plog(ASL_LEVEL_NOTICE
,
678 "delete payload for protocol %s\n",
679 s_ipsecdoi_proto(delete->proto_id
));
681 if(!iph1
->rmconf
->weak_phase1_check
&& !encrypted
) {
682 plog(ASL_LEVEL_WARNING
,
683 "Ignoring unencrypted delete payload "
684 "(check the weak_phase1_check option)\n");
688 switch (delete->proto_id
) {
689 case IPSECDOI_PROTO_ISAKMP
:
690 if (delete->spi_size
!= sizeof(isakmp_index
)) {
692 "delete payload with strange spi "
693 "size %d(proto_id:%d)\n",
694 delete->spi_size
, delete->proto_id
);
698 del_ph1
= ike_session_getph1byindex(iph1
->parent_session
, (isakmp_index
*)(delete + 1));
701 // hack: start a rekey now, if one was pending (only for client).
702 if (del_ph1
->sce_rekey
&&
703 del_ph1
->parent_session
&&
704 del_ph1
->parent_session
->is_client
&&
705 del_ph1
->parent_session
->established
&&
706 !(del_ph1
->rmconf
->natt_multiple_user
&&
707 del_ph1
->parent_session
->is_l2tpvpn_ipsec
)) {
708 isakmp_ph1rekeyexpire(del_ph1
, FALSE
);
712 SCHED_KILL(del_ph1
->scr
);
715 * Do not delete IPsec SAs when receiving an IKE delete notification.
716 * Just delete the IKE SA.
718 #ifdef ENABLE_VPNCONTROL_PORT
719 if (del_ph1
->started_by_api
|| (del_ph1
->is_rekey
&& del_ph1
->parent_session
&& del_ph1
->parent_session
->is_client
)) {
720 if (ike_session_islast_ph1(del_ph1
)) {
721 isakmp_info_vpncontrol_notify_ike_failed(del_ph1
, FROM_REMOTE
, VPNCTL_NTYPE_PH1_DELETE
, NULL
);
725 if (del_ph1
->rmconf
->natt_multiple_user
&&
726 del_ph1
->parent_session
->is_l2tpvpn_ipsec
) {
727 plog(ASL_LEVEL_NOTICE
, "Ignoring IKE delete from peer for L2TP server\n");
730 isakmp_ph1expire(del_ph1
);
734 case IPSECDOI_PROTO_IPSEC_AH
:
735 case IPSECDOI_PROTO_IPSEC_ESP
:
736 if (delete->spi_size
!= sizeof(u_int32_t
)) {
738 "delete payload with strange spi "
739 "size %d(proto_id:%d)\n",
740 delete->spi_size
, delete->proto_id
);
743 if (iph1
->rmconf
->natt_multiple_user
&&
744 iph1
->parent_session
->is_l2tpvpn_ipsec
) {
745 uint32_t *ph2_spi
= ALIGNED_CAST(u_int32_t
*)(delete + 1);
746 phase2_handle_t
*iph2
= ike_session_getph2bysaidx(iph1
->local
, iph1
->remote
, delete->proto_id
, ph2_spi
[0]);
749 iph2
->is_defunct
= 1;
750 plog(ASL_LEVEL_NOTICE
, "Ignoring SA delete from peer for L2TP server\n");
754 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
755 ALIGNED_CAST(u_int32_t
*)(delete + 1), num_spi
, NULL
, NULL
); // Wcast-align fix (void*) - delete payload is aligned
758 case IPSECDOI_PROTO_IPCOMP
:
759 /* need to handle both 16bit/32bit SPI */
760 memset(&spi
, 0, sizeof(spi
));
761 if (delete->spi_size
== sizeof(spi
.spi16
[1])) {
762 memcpy(&spi
.spi16
[1], delete + 1,
763 sizeof(spi
.spi16
[1]));
764 } else if (delete->spi_size
== sizeof(spi
.spi32
))
765 memcpy(&spi
.spi32
, delete + 1, sizeof(spi
.spi32
));
768 "delete payload with strange spi "
769 "size %d(proto_id:%d)\n",
770 delete->spi_size
, delete->proto_id
);
773 purge_ipsec_spi(iph1
->remote
, delete->proto_id
,
774 &spi
.spi32
, num_spi
, NULL
, NULL
);
779 "deletion message received, "
780 "invalid proto_id: %d\n",
785 plog(ASL_LEVEL_NOTICE
, "purged SAs.\n");
791 * send Delete payload (for ISAKMP SA) in Informational exchange.
794 isakmp_info_send_d1(phase1_handle_t
*iph1
)
796 struct isakmp_pl_d
*d
;
797 vchar_t
*payload
= NULL
;
801 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
))
804 /* create delete payload */
806 /* send SPIs of inbound SAs. */
807 /* XXX should send outbound SAs's ? */
808 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
809 payload
= vmalloc(tlen
);
810 if (payload
== NULL
) {
812 "failed to get buffer for payload.\n");
816 d
= (struct isakmp_pl_d
*)payload
->v
;
817 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
818 d
->h
.len
= htons(tlen
);
819 d
->doi
= htonl(IPSEC_DOI
);
820 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
821 d
->spi_size
= sizeof(isakmp_index
);
822 d
->num_spi
= htons(1);
823 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
825 error
= isakmp_info_send_common(iph1
, payload
,
829 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
830 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
831 CONSTSTR("Delete ISAKMP-SA"),
832 CONSTSTR("Failed to transmit Delete-ISAKMP-SA message"));
834 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
835 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
836 CONSTSTR("Delete ISAKMP-SA"),
844 * send Delete payload (for IPsec SA) in Informational exchange, based on
845 * pfkey msg. It sends always single SPI.
848 isakmp_info_send_d2(phase2_handle_t
*iph2
)
850 phase1_handle_t
*iph1
;
852 struct isakmp_pl_d
*d
;
853 vchar_t
*payload
= NULL
;
858 if (!FSM_STATE_IS_ESTABLISHED(iph2
->status
))
862 * don't send delete information if there is no phase 1 handler.
863 * It's nonsensical to negotiate phase 1 to send the information.
865 iph1
= ike_session_get_established_ph1(iph2
->parent_session
);
867 iph1
= ike_session_getph1byaddr(iph2
->parent_session
, iph2
->src
, iph2
->dst
);
870 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
871 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
872 CONSTSTR("Information message"),
873 CONSTSTR("Failed to transmit Information message"));
874 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
875 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
876 CONSTSTR("Delete IPSEC-SA"),
877 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
878 plog(ASL_LEVEL_NOTICE
,
879 "No ph1 handler found, could not send DELETE_SA\n");
883 /* create delete payload */
884 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
886 /* send SPIs of inbound SAs. */
888 * XXX should I send outbound SAs's ?
889 * I send inbound SAs's SPI only at the moment because I can't
890 * decode any more if peer send encoded packet without aware of
891 * deletion of SA. Outbound SAs don't come under the situation.
893 tlen
= sizeof(*d
) + pr
->spisize
;
894 payload
= vmalloc(tlen
);
895 if (payload
== NULL
) {
896 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
897 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
898 CONSTSTR("Information message"),
899 CONSTSTR("Failed to transmit Information message"));
900 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
901 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
902 CONSTSTR("Delete IPSEC-SA"),
903 CONSTSTR("Failed to transmit Delete-IPSEC-SA message"));
905 "failed to get buffer for payload.\n");
909 d
= (struct isakmp_pl_d
*)payload
->v
;
910 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
911 d
->h
.len
= htons(tlen
);
912 d
->doi
= htonl(IPSEC_DOI
);
913 d
->proto_id
= pr
->proto_id
;
914 d
->spi_size
= pr
->spisize
;
915 d
->num_spi
= htons(1);
917 * XXX SPI bits are left-filled, for use with IPComp.
918 * we should be switching to variable-length spi field...
920 spi
= (u_int8_t
*)&pr
->spi
;
921 spi
+= sizeof(pr
->spi
);
923 memcpy(d
+ 1, spi
, pr
->spisize
);
925 error
= isakmp_info_send_common(iph1
, payload
,
929 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
930 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
931 CONSTSTR("Delete IPSEC-SA"),
932 CONSTSTR("Failed to transmit Delete-IPSEC-SA"));
934 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
935 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
936 CONSTSTR("Delete IPSEC-SA"),
945 * send Notification payload (without ISAKMP SA) in an Informational exchange
948 isakmp_info_send_nx(struct isakmp
*isakmp
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
,
949 int type
, vchar_t
*data
)
951 phase1_handle_t
*iph1
= NULL
;
952 struct remoteconf
*rmconf
;
953 vchar_t
*payload
= NULL
;
956 struct isakmp_pl_n
*n
;
957 int spisiz
= 0; /* see below */
958 ike_session_t
*sess
= ike_session_get_session(local
, remote
, FALSE
, NULL
);
960 /* search appropreate configuration */
961 rmconf
= getrmconf(remote
);
962 if (rmconf
== NULL
) {
963 IPSECSESSIONTRACEREVENT(sess
,
964 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
965 CONSTSTR("Information message"),
966 CONSTSTR("Failed to transmit Information message (no remote configuration)"));
968 "no configuration found for peer address.\n");
972 /* add new entry to isakmp status table. */
973 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
975 IPSECSESSIONTRACEREVENT(sess
,
976 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
977 CONSTSTR("Information message"),
978 CONSTSTR("Failed to transmit Information message (no new Phase 1)"));
980 "failed to allocate ph1");
984 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
985 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
986 fsm_set_state(&iph1
->status
, IKEV1_STATE_INFO
);
987 iph1
->rmconf
= rmconf
;
988 retain_rmconf(iph1
->rmconf
);
989 iph1
->side
= INITIATOR
;
990 iph1
->version
= isakmp
->v
;
992 iph1
->msgid
= 0; /* XXX */
994 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1001 iph1
->frag_chain
= NULL
;
1004 /* copy remote address */
1005 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
1006 IPSECSESSIONTRACEREVENT(sess
,
1007 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1008 CONSTSTR("Information message"),
1009 CONSTSTR("Failed to transmit Information Message (can't copy Phase 1 addresses)"));
1011 "failed to copy ph1 addresses");
1013 iph1
= NULL
; /* deleted in copy_ph1addresses */
1017 tlen
= sizeof(*n
) + spisiz
;
1020 payload
= vmalloc(tlen
);
1021 if (payload
== NULL
) {
1022 IPSECSESSIONTRACEREVENT(sess
,
1023 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1024 CONSTSTR("Information message"),
1025 CONSTSTR("Failed to transmit Information Message (can't allocate payload)"));
1027 "failed to get buffer to send.\n");
1032 n
= (struct isakmp_pl_n
*)payload
->v
;
1033 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1034 n
->h
.len
= htons(tlen
);
1035 n
->doi
= htonl(IPSEC_DOI
);
1036 n
->proto_id
= IPSECDOI_KEY_IKE
;
1037 n
->spi_size
= spisiz
;
1038 n
->type
= htons(type
);
1040 memset(n
+ 1, 0, spisiz
); /* XXX spisiz is always 0 */
1042 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1044 #ifdef ENABLE_VPNCONTROL_PORT
1045 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1047 if (ike_session_link_phase1(sess
, iph1
))
1050 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
1053 IPSECSESSIONTRACEREVENT(sess
,
1054 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1055 CONSTSTR("Without ISAKMP-SA"),
1056 CONSTSTR("Failed to transmit Without-ISAKMP-SA message"));
1058 IPSECSESSIONTRACEREVENT(sess
,
1059 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1060 CONSTSTR("Without ISAKMP-SA"),
1066 ike_session_unlink_phase1(iph1
);
1072 * send Notification payload (with ISAKMP SA) in an Informational exchange
1075 isakmp_info_send_n1(phase1_handle_t
*iph1
, int type
, vchar_t
*data
)
1077 vchar_t
*payload
= NULL
;
1080 struct isakmp_pl_n
*n
;
1084 * note on SPI size: which description is correct? I have chosen
1087 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
1088 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
1089 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
1090 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
1091 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
1093 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
||
1094 type
== ISAKMP_NTYPE_LOAD_BALANCE
)
1095 spisiz
= sizeof(isakmp_index
);
1099 tlen
= sizeof(*n
) + spisiz
;
1102 payload
= vmalloc(tlen
);
1103 if (payload
== NULL
) {
1104 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1105 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1106 CONSTSTR("ISAKMP-SA"),
1107 CONSTSTR("Failed to transmit ISAKMP-SA message (can't allocate payload)"));
1109 "failed to get buffer to send.\n");
1113 n
= (struct isakmp_pl_n
*)payload
->v
;
1114 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1115 n
->h
.len
= htons(tlen
);
1116 n
->doi
= htonl(iph1
->rmconf
->doitype
);
1117 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
1118 n
->spi_size
= spisiz
;
1119 n
->type
= htons(type
);
1121 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
1123 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
1125 #ifdef ENABLE_VPNCONTROL_PORT
1126 isakmp_info_vpncontrol_notify_ike_failed(iph1
, FROM_LOCAL
, type
, data
);
1129 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
1132 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1133 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1134 CONSTSTR("ISAKMP-SA"),
1135 CONSTSTR("Can't transmit ISAKMP-SA message"));
1137 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1138 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1139 CONSTSTR("ISAKMP-SA"),
1147 * send Notification payload (with IPsec SA) in an Informational exchange
1150 isakmp_info_send_n2(phase2_handle_t
*iph2
, int type
, vchar_t
*data
)
1152 phase1_handle_t
*iph1
= iph2
->ph1
;
1153 vchar_t
*payload
= NULL
;
1156 struct isakmp_pl_n
*n
;
1159 if (!iph2
->approval
)
1162 pr
= iph2
->approval
->head
;
1164 /* XXX must be get proper spi */
1165 tlen
= sizeof(*n
) + pr
->spisize
;
1168 payload
= vmalloc(tlen
);
1169 if (payload
== NULL
) {
1170 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1171 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1172 CONSTSTR("IPSEC-SA"),
1173 CONSTSTR("Failed to transmit IPSEC-SA message (can't allocate payload)"));
1175 "failed to get buffer to send.\n");
1179 n
= (struct isakmp_pl_n
*)payload
->v
;
1180 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1181 n
->h
.len
= htons(tlen
);
1182 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1183 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1184 n
->spi_size
= pr
->spisize
;
1185 n
->type
= htons(type
);
1186 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign
1188 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1190 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
1191 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
1194 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1195 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1196 CONSTSTR("IPSEC-SA"),
1197 CONSTSTR("Failed to transmit IPSEC-SA message"));
1199 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1200 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1201 CONSTSTR("IPSEC-SA"),
1210 * When ph1->skeyid_a == NULL, send message without encoding.
1213 isakmp_info_send_common(phase1_handle_t
*iph1
, vchar_t
*payload
, u_int32_t np
, int flags
)
1215 phase2_handle_t
*iph2
= NULL
;
1216 vchar_t
*hash
= NULL
;
1217 struct isakmp
*isakmp
;
1218 struct isakmp_gen
*gen
;
1223 /* add new entry to isakmp status table */
1224 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_INFO
);
1227 "failed to allocate ph2");
1231 iph2
->dst
= dupsaddr(iph1
->remote
);
1232 if (iph2
->dst
== NULL
) {
1234 "failed to duplicate remote address");
1235 ike_session_delph2(iph2
);
1238 iph2
->src
= dupsaddr(iph1
->local
);
1239 if (iph2
->src
== NULL
) {
1241 "failed to duplicate local address");
1242 ike_session_delph2(iph2
);
1245 switch (iph1
->remote
->ss_family
) {
1247 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1248 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1249 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1254 #if (!defined(ENABLE_NATT)) || (defined(BROKEN_NATT))
1255 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1256 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1262 "invalid family: %d\n", iph1
->remote
->ss_family
);
1263 ike_session_delph2(iph2
);
1266 iph2
->side
= INITIATOR
;
1267 fsm_set_state(&iph2
->status
, IKEV1_STATE_INFO
);
1268 iph2
->msgid
= isakmp_newmsgid2(iph1
);
1270 /* get IV and HASH(1) if skeyid_a was generated. */
1271 if (iph1
->skeyid_a
!= NULL
) {
1272 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1273 if (iph2
->ivm
== NULL
) {
1275 "failed to generate IV");
1276 ike_session_delph2(iph2
);
1280 /* generate HASH(1) */
1281 hash
= oakley_compute_hash1(iph1
, iph2
->msgid
, payload
);
1284 "failed to generate HASH");
1285 ike_session_delph2(iph2
);
1289 /* initialized total buffer length */
1291 tlen
+= sizeof(*gen
);
1293 /* IKE-SA is not established */
1296 /* initialized total buffer length */
1299 if ((flags
& ISAKMP_FLAG_A
) == 0)
1300 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
1302 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
1304 ike_session_link_ph2_to_ph1(iph1
, iph2
);
1306 tlen
+= sizeof(*isakmp
) + payload
->l
;
1308 /* create buffer for isakmp payload */
1309 iph2
->sendbuf
= vmalloc(tlen
);
1310 if (iph2
->sendbuf
== NULL
) {
1312 "failed to get buffer to send.\n");
1316 /* create isakmp header */
1317 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
1318 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
1319 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
1320 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
1321 isakmp
->v
= iph1
->version
;
1322 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
1323 isakmp
->flags
= iph2
->flags
;
1324 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
1325 isakmp
->len
= htonl(tlen
);
1326 p
= (char *)(isakmp
+ 1);
1328 /* create HASH payload */
1330 gen
= (struct isakmp_gen
*)p
;
1331 gen
->np
= np
& 0xff;
1332 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
1334 memcpy(p
, hash
->v
, hash
->l
);
1339 memcpy(p
, payload
->v
, payload
->l
);
1342 #ifdef HAVE_PRINT_ISAKMP_C
1343 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1347 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
1350 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
1352 VPTRINIT(iph2
->sendbuf
);
1355 "failed to encrypt packet");
1358 iph2
->sendbuf
= tmp
;
1361 /* HDR*, HASH(1), N */
1362 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
1364 "failed to send packet");
1365 VPTRINIT(iph2
->sendbuf
);
1369 plog(ASL_LEVEL_NOTICE
,
1370 "sendto Information %s.\n", s_isakmp_nptype(np
));
1373 * don't resend notify message because peer can use Acknowledged
1374 * Informational if peer requires the reply of the notify message.
1377 /* XXX If Acknowledged Informational required, don't delete ph2handle */
1379 VPTRINIT(iph2
->sendbuf
);
1380 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1381 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1382 CONSTSTR("Information message"),
1389 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1390 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1391 CONSTSTR("Information message"),
1392 CONSTSTR("Failed to transmit Information message"));
1399 ike_session_unlink_phase2(iph2
);
1404 * add a notify payload to buffer by reallocating buffer.
1405 * If buf == NULL, the function only create a notify payload.
1407 * XXX Which is SPI to be included, inbound or outbound ?
1410 isakmp_add_pl_n(vchar_t
*buf0
, u_int8_t
**np_p
, int type
, struct saproto
*pr
, vchar_t
*data
)
1412 vchar_t
*buf
= NULL
;
1413 struct isakmp_pl_n
*n
;
1418 **np_p
= ISAKMP_NPTYPE_N
;
1420 tlen
= sizeof(*n
) + pr
->spisize
;
1426 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
1428 buf
= vmalloc(tlen
);
1431 "failed to get a payload buffer.\n");
1435 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
1436 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
1437 n
->h
.len
= htons(tlen
);
1438 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
1439 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
1440 n
->spi_size
= pr
->spisize
;
1441 n
->type
= htons(type
);
1442 memcpy(n
+ 1, &pr
->spi
, sizeof(u_int32_t
)); // Wcast-align fix - copy instead of assign with cast
1444 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
1446 /* save the pointer of next payload type */
1454 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
)
1456 vchar_t
*buf
= NULL
;
1457 struct sadb_msg
*msg
, *next
, *end
;
1459 struct sadb_lifetime
*lt
;
1460 struct sockaddr_storage
*src
, *dst
;
1461 phase2_handle_t
*iph2
;
1464 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1466 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
1468 plog(ASL_LEVEL_NOTICE
,
1469 "pfkey_dump_sadb returned nothing.\n");
1473 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1474 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1477 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1479 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1480 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1485 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1487 "pfkey_check (%s)\n", ipsec_strerror());
1492 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1494 || !mhp
[SADB_EXT_ADDRESS_SRC
]
1495 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
1499 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); // Wcast-align fix (void*) - buffer of pointers to aligned structs
1500 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1501 lt
= ALIGNED_CAST(struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
1503 created
= lt
->sadb_lifetime_addtime
;
1507 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1508 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1513 /* XXX n^2 algorithm, inefficient */
1515 /* don't delete inbound SAs at the moment (just save them in inbound_spi) */
1516 /* XXX should we remove SAs with opposite direction as well? */
1517 if (CMPSADDR2(dst0
, dst
)) {
1522 for (i
= 0; i
< n
; i
++) {
1525 if (spi
[i
] != sa
->sadb_sa_spi
)
1529 * delete a relative phase 2 handler.
1530 * continue to process if no relative phase 2 handler
1533 if (inbound_spi
&& max_inbound_spi
&& j
< *max_inbound_spi
) {
1534 i_spi
= &inbound_spi
[j
];
1538 iph2
= ike_session_getph2bysaidx2(src
, dst
, proto
, spi
[i
], i_spi
);
1540 pfkey_send_delete(lcconf
->sock_pfkey
,
1541 msg
->sadb_msg_satype
,
1543 src
, dst
, sa
->sadb_sa_spi
);
1547 ike_session_unlink_phase2(iph2
);
1553 plog(ASL_LEVEL_NOTICE
, "Purged IPsec-SA proto_id=%s spi=%u.\n",
1554 s_ipsecdoi_proto(proto
),
1561 if (max_inbound_spi
) {
1562 *max_inbound_spi
= j
;
1570 * delete all phase2 sa relatived to the destination address.
1571 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1572 * an INITIAL-CONTACT if we have contacted the peer. This matches the
1573 * Sun IKE behavior, and makes rekeying work much better when the peer
1577 info_recv_initialcontact(phase1_handle_t
*iph1
)
1579 vchar_t
*buf
= NULL
;
1580 struct sadb_msg
*msg
, *next
, *end
;
1582 struct sockaddr_storage
*src
, *dst
;
1583 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1585 phase2_handle_t
*iph2
;
1593 // TODO: make sure that is_rekey is cleared for this. and session indicates the same
1595 loc
= racoon_strdup(saddrwop2str(iph1
->local
));
1596 rem
= racoon_strdup(saddrwop2str(iph1
->remote
));
1601 * Purge all IPSEC-SAs for the peer. We can do this
1602 * the easy way (using a PF_KEY SADB_DELETE extension)
1603 * or we can do it the hard way.
1605 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1606 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
1608 plog(ASL_LEVEL_NOTICE
,
1609 "purging %s SAs for %s -> %s\n",
1610 pfkey_satypes
[i
].ps_name
, loc
, rem
);
1611 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1612 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1613 iph1
->local
, iph1
->remote
) == -1) {
1615 "delete_all %s -> %s failed for %s (%s)\n",
1617 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1621 ike_session_deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
1623 plog(ASL_LEVEL_NOTICE
,
1624 "purging %s SAs for %s -> %s\n",
1625 pfkey_satypes
[i
].ps_name
, rem
, loc
);
1626 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1627 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1628 iph1
->remote
, iph1
->local
) == -1) {
1630 "delete_all %s -> %s failed for %s (%s)\n",
1632 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1636 ike_session_deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1648 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1650 plog(ASL_LEVEL_NOTICE
,
1651 "pfkey_dump_sadb returned nothing.\n");
1655 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
1656 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
1659 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1661 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1662 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1667 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1669 "pfkey_check (%s)\n", ipsec_strerror());
1674 if (mhp
[SADB_EXT_SA
] == NULL
1675 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1676 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1680 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
]; // Wcast-align fix (void*) - buffer of pointers to aligned structs
1681 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1682 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1684 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1685 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1691 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1692 * announces the sender of the message was rebooted.
1693 * it is interpreted to delete all SAs which source address
1694 * is the sender of the message.
1695 * racoon only deletes SA which is matched both the
1696 * source address and the destination accress.
1700 * XXX RFC 3947 says that whe MUST NOT use IP+port to find old SAs
1703 if(iph1
->natt_flags
& NAT_DETECTED
){
1704 if (CMPSADDR(iph1
->local
, src
) == 0 &&
1705 CMPSADDR(iph1
->remote
, dst
) == 0)
1707 else if (CMPSADDR(iph1
->remote
, src
) == 0 &&
1708 CMPSADDR(iph1
->local
, dst
) == 0)
1716 /* If there is no NAT-T, we don't have to check addr + port...
1717 * XXX what about a configuration with a remote peers which is not
1718 * NATed, but which NATs some other peers ?
1719 * Here, the INITIAl-CONTACT would also flush all those NATed peers !!
1721 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1722 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1724 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1725 cmpsaddrwop(iph1
->local
, dst
) == 0)
1733 * Make sure this is an SATYPE that we manage.
1734 * This is gross; too bad we couldn't do it the
1737 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1738 if (pfkey_satypes
[i
].ps_satype
==
1739 msg
->sadb_msg_satype
)
1742 if (i
== pfkey_nsatypes
) {
1747 plog(ASL_LEVEL_NOTICE
,
1748 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1749 pfkey_send_delete(lcconf
->sock_pfkey
,
1750 msg
->sadb_msg_satype
,
1751 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1754 * delete a relative phase 2 handler.
1755 * continue to process if no relative phase 2 handler
1758 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1759 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1762 ike_session_unlink_phase2(iph2
);
1772 isakmp_check_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase1_handle_t
*iph1
)
1774 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1776 plog(ASL_LEVEL_NOTICE
,
1777 "Notify Message received\n");
1779 switch (ntohs(notify
->type
)) {
1780 case ISAKMP_NTYPE_CONNECTED
:
1781 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1782 case ISAKMP_NTYPE_REPLAY_STATUS
:
1783 case ISAKMP_NTYPE_HEARTBEAT
:
1784 #ifdef ENABLE_HYBRID
1785 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1787 plog(ASL_LEVEL_WARNING
,
1788 "Ignore %s notification.\n",
1789 s_isakmp_notify_msg(ntohs(notify
->type
)));
1791 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1792 plog(ASL_LEVEL_WARNING
,
1793 "Ignore INITIAL-CONTACT notification, "
1794 "because it is only accepted after Phase 1.\n");
1796 case ISAKMP_NTYPE_LOAD_BALANCE
:
1797 plog(ASL_LEVEL_WARNING
,
1798 "Ignore LOAD-BALANCE notification, "
1799 "because it is only accepted after Phase 1.\n");
1802 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1804 "Received unknown notification type %s.\n",
1805 s_isakmp_notify_msg(ntohs(notify
->type
)));
1812 isakmp_check_ph2_notify(struct isakmp_gen
*gen
/* points to Notify payload */, phase2_handle_t
*iph2
)
1814 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1816 plog(ASL_LEVEL_NOTICE
,
1817 "Phase 2 Notify Message received\n");
1819 switch (ntohs(notify
->type
)) {
1820 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1821 return((void)isakmp_ph2_responder_lifetime(iph2
,
1822 (struct isakmp_pl_resp_lifetime
*)notify
));
1824 case ISAKMP_NTYPE_CONNECTED
:
1825 case ISAKMP_NTYPE_REPLAY_STATUS
:
1826 case ISAKMP_NTYPE_HEARTBEAT
:
1827 #ifdef ENABLE_HYBRID
1828 case ISAKMP_NTYPE_UNITY_HEARTBEAT
:
1830 plog(ASL_LEVEL_WARNING
,
1831 "Ignore %s notification.\n",
1832 s_isakmp_notify_msg(ntohs(notify
->type
)));
1834 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1835 plog(ASL_LEVEL_WARNING
,
1836 "Ignore INITIAL-CONTACT notification, "
1837 "because it is only accepted after Phase 1.\n");
1839 case ISAKMP_NTYPE_LOAD_BALANCE
:
1840 plog(ASL_LEVEL_WARNING
,
1841 "Ignore LOAD-BALANCE notification, "
1842 "because it is only accepted after Phase 1.\n");
1845 isakmp_info_send_n1(iph2
->ph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1847 "Received unknown notification type %s.\n",
1848 s_isakmp_notify_msg(ntohs(notify
->type
)));
1854 #ifdef ENABLE_VPNCONTROL_PORT
1856 isakmp_info_recv_lb(phase1_handle_t
*iph1
, struct isakmp_pl_lb
*n
, int encrypted
)
1859 if (iph1
->side
!= INITIATOR
)
1861 plog(ASL_LEVEL_NOTICE
,
1862 "LOAD-BALANCE notification ignored - we are not the initiator.\n");
1866 plog(ASL_LEVEL_NOTICE
,
1867 "LOAD-BALANCE notification ignored - not protected.\n");
1870 if (ntohs(n
->h
.len
) != sizeof(struct isakmp_pl_lb
)) {
1871 plog(ASL_LEVEL_NOTICE
,
1872 "isakmp_info_recv_lb Invalid length of payload\n");
1876 vpncontrol_notify_ike_failed(ISAKMP_NTYPE_LOAD_BALANCE
, FROM_REMOTE
,
1877 iph1_get_remote_v4_address(iph1
), 4, (u_int8_t
*)(&(n
->address
)));
1879 plog(ASL_LEVEL_NOTICE
,
1880 "Received LOAD_BALANCE notification.\n");
1882 if (((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
!= ntohl(n
->address
)) {
1883 plog(ASL_LEVEL_NOTICE
,
1884 "Deleting old Phase 1 because of LOAD_BALANCE notification - redirect address=%x.\n",
1887 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1888 isakmp_info_send_d1(iph1
);
1890 isakmp_ph1expire(iph1
);
1899 isakmp_info_recv_r_u (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1901 struct isakmp_pl_ru
*ru_ack
;
1902 vchar_t
*payload
= NULL
;
1906 plog(ASL_LEVEL_NOTICE
,
1907 "DPD R-U-There received\n");
1909 /* XXX should compare cookies with iph1->index?
1910 Or is this already done by calling function? */
1911 tlen
= sizeof(*ru_ack
);
1912 payload
= vmalloc(tlen
);
1913 if (payload
== NULL
) {
1914 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1915 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1916 CONSTSTR("R-U-THERE? ACK"),
1917 CONSTSTR("Failed to transmit DPD response"));
1919 "failed to get buffer to send.\n");
1923 ru_ack
= (struct isakmp_pl_ru
*)payload
->v
;
1924 ru_ack
->h
.np
= ISAKMP_NPTYPE_NONE
;
1925 ru_ack
->h
.len
= htons(tlen
);
1926 ru_ack
->doi
= htonl(IPSEC_DOI
);
1927 ru_ack
->type
= htons(ISAKMP_NTYPE_R_U_THERE_ACK
);
1928 ru_ack
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ? */
1929 ru_ack
->spi_size
= sizeof(isakmp_index
);
1930 memcpy(ru_ack
->i_ck
, ru
->i_ck
, sizeof(cookie_t
));
1931 memcpy(ru_ack
->r_ck
, ru
->r_ck
, sizeof(cookie_t
));
1932 ru_ack
->data
= ru
->data
;
1934 /* XXX Should we do FLAG_A ? */
1935 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
,
1939 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1940 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
1941 CONSTSTR("R-U-THERE? ACK"),
1942 CONSTSTR("Failed to transmit DPD ack"));
1944 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1945 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
1946 CONSTSTR("R-U-THERE? ACK"),
1950 plog(ASL_LEVEL_NOTICE
, "received a valid R-U-THERE, ACK sent\n");
1952 /* Should we mark tunnel as active ? */
1957 isakmp_info_recv_r_u_ack (phase1_handle_t
*iph1
, struct isakmp_pl_ru
*ru
, u_int32_t msgid
)
1960 plog(ASL_LEVEL_NOTICE
,
1961 "DPD R-U-There-Ack received\n");
1963 /* XXX Maintain window of acceptable sequence numbers ?
1964 * => ru->data <= iph2->dpd_seq &&
1965 * ru->data >= iph2->dpd_seq - iph2->dpd_fails ? */
1966 if (ntohl(ru
->data
) != iph1
->dpd_seq
) {
1968 "Wrong DPD sequence number (%d, %d expected).\n",
1969 ntohl(ru
->data
), iph1
->dpd_seq
);
1973 if (memcmp(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
)) ||
1974 memcmp(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
))) {
1976 "Cookie mismatch in DPD ACK!.\n");
1980 iph1
->dpd_fails
= 0;
1985 iph1
->dpd_lastack
= time(NULL
);
1987 SCHED_KILL(iph1
->dpd_r_u
);
1989 isakmp_sched_r_u(iph1
, 0);
1991 if (iph1
->side
== INITIATOR
) {
1992 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1993 IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RESP
,
1994 CONSTSTR("Initiator DPD Response"),
1997 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1998 IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RESP
,
1999 CONSTSTR("Responder DPD Response"),
2002 plog(ASL_LEVEL_NOTICE
, "received an R-U-THERE-ACK\n");
2004 #ifdef ENABLE_VPNCONTROL_PORT
2005 vpncontrol_notify_peer_resp_ph1(1, iph1
);
2006 #endif /* ENABLE_VPNCONTROL_PORT */
2013 * send Delete payload (for ISAKMP SA) in Informational exchange.
2016 isakmp_info_send_r_u(void *arg
)
2018 phase1_handle_t
*iph1
= arg
;
2020 /* create R-U-THERE payload */
2021 struct isakmp_pl_ru
*ru
;
2022 vchar_t
*payload
= NULL
;
2026 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2027 plog(ASL_LEVEL_NOTICE
, "DPD r-u send aborted, invalid Phase 1 status %d....\n",
2032 if (iph1
->dpd_fails
>= iph1
->rmconf
->dpd_maxfails
) {
2033 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2034 IPSECSESSIONEVENTCODE_IKEV1_DPD_MAX_RETRANSMIT
,
2035 CONSTSTR("DPD maximum retransmits"),
2036 CONSTSTR("maxed-out of DPD requests without receiving an ack"));
2038 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_PEER_DEAD
, FROM_LOCAL
, iph1_get_remote_v4_address(iph1
), 0, NULL
);
2041 plog(ASL_LEVEL_NOTICE
,
2042 "DPD: remote seems to be dead\n");
2044 /* Do not reschedule here: phase1 is deleted,
2045 * DPD will be reactivated when a new ph1 will be negociated
2051 payload
= vmalloc(tlen
);
2052 if (payload
== NULL
) {
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 "failed to get buffer for payload.\n");
2061 ru
= (struct isakmp_pl_ru
*)payload
->v
;
2062 ru
->h
.np
= ISAKMP_NPTYPE_NONE
;
2063 ru
->h
.len
= htons(tlen
);
2064 ru
->doi
= htonl(IPSEC_DOI
);
2065 ru
->type
= htons(ISAKMP_NTYPE_R_U_THERE
);
2066 ru
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX ?*/
2067 ru
->spi_size
= sizeof(isakmp_index
);
2069 memcpy(ru
->i_ck
, iph1
->index
.i_ck
, sizeof(cookie_t
));
2070 memcpy(ru
->r_ck
, iph1
->index
.r_ck
, sizeof(cookie_t
));
2072 if (iph1
->dpd_seq
== 0){
2073 /* generate a random seq which is not too big */
2075 iph1
->dpd_seq
= rand() & 0x0fff;
2078 ru
->data
= htonl(iph1
->dpd_seq
);
2080 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
2083 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2084 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_FAIL
,
2085 CONSTSTR("R-U-THERE?"),
2086 CONSTSTR("Failed to transmit DPD request"));
2088 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2089 IPSECSESSIONEVENTCODE_IKEV1_INFO_NOTICE_TX_SUCC
,
2090 CONSTSTR("R-U-THERE?"),
2094 if (iph1
->side
== INITIATOR
) {
2095 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2096 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_INIT_REQ
,
2097 CONSTSTR("Initiator DPD Request"),
2100 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2101 iph1
->dpd_fails
? IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_RETRANSMIT
: IPSECSESSIONEVENTCODE_IKEV1_DPD_RESP_REQ
,
2102 CONSTSTR("Responder DPD Request"),
2105 plog(ASL_LEVEL_NOTICE
,
2106 "DPD R-U-There sent (%d)\n", error
);
2108 /* will be decreased if ACK received... */
2111 /* Reschedule the r_u_there with a short delay,
2112 * will be deleted/rescheduled if ACK received before */
2113 isakmp_sched_r_u(iph1
, 1);
2115 plog(ASL_LEVEL_NOTICE
,
2116 "rescheduling send_r_u (%d).\n", iph1
->rmconf
->dpd_retry
);
2120 * monitor DPD (ALGORITHM_INBOUND_DETECT) Informational exchange.
2123 isakmp_info_monitor_r_u_algo_inbound_detect (phase1_handle_t
*iph1
)
2125 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2126 plog(ASL_LEVEL_NOTICE
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) aborted, invalid Phase 1 status %d....\n",
2131 plog(ASL_LEVEL_NOTICE
, "DPD monitoring (for ALGORITHM_INBOUND_DETECT) ....\n");
2133 // check phase1 for ike packets received from peer
2134 if (iph1
->peer_sent_ike
) {
2135 // yes, reshedule check
2136 iph1
->peer_sent_ike
= 0;
2138 /* ike packets received from peer... reschedule dpd */
2139 isakmp_sched_r_u(iph1
, 0);
2141 plog(ASL_LEVEL_NOTICE
,
2142 "ike packets received from peer... reschedule monitor.\n");
2147 // after ike packets, next we check if any data was received
2148 if (!iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2149 isakmp_info_send_r_u(iph1
);
2151 isakmp_sched_r_u(iph1
, 0);
2153 plog(ASL_LEVEL_NOTICE
,
2154 "rescheduling DPD monitoring (for ALGORITHM_INBOUND_DETECT).\n");
2156 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2160 * monitor DPD (ALGORITHM_BLACKHOLE_DETECT) Informational exchange.
2163 isakmp_info_monitor_r_u_algo_blackhole_detect (phase1_handle_t
*iph1
)
2165 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2166 plog(ASL_LEVEL_NOTICE
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) aborted, invalid Phase 1 status %d....\n",
2171 plog(ASL_LEVEL_NOTICE
, "DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) ....\n");
2173 // check if data was sent but none was received
2174 if (iph1
->parent_session
->i_sent_data_sc_dpd
&&
2175 !iph1
->parent_session
->peer_sent_data_sc_dpd
) {
2176 isakmp_info_send_r_u(iph1
);
2178 isakmp_sched_r_u(iph1
, 0);
2180 plog(ASL_LEVEL_NOTICE
,
2181 "rescheduling DPD monitoring (for ALGORITHM_BLACKHOLE_DETECT) i = %d, peer %d.\n",
2182 iph1
->parent_session
->i_sent_data_sc_dpd
,
2183 iph1
->parent_session
->peer_sent_data_sc_dpd
);
2185 iph1
->parent_session
->i_sent_data_sc_dpd
= 0;
2186 iph1
->parent_session
->peer_sent_data_sc_dpd
= 0;
2190 * monitor DPD Informational exchange.
2193 isakmp_info_monitor_r_u(void *arg
)
2195 phase1_handle_t
*iph1
= arg
;
2197 if (iph1
&& iph1
->rmconf
) {
2198 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
) {
2199 isakmp_info_monitor_r_u_algo_inbound_detect(iph1
);
2200 } else if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2201 isakmp_info_monitor_r_u_algo_blackhole_detect(iph1
);
2203 plog(ASL_LEVEL_NOTICE
, "DPD monitoring aborted, invalid algorithm %d....\n",
2204 iph1
->rmconf
->dpd_algo
);
2209 /* Schedule a new R-U-THERE */
2211 isakmp_sched_r_u(phase1_handle_t
*iph1
, int retry
)
2214 iph1
->rmconf
== NULL
)
2218 if(iph1
->dpd_support
== 0 ||
2219 iph1
->rmconf
->dpd_interval
== 0)
2223 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_retry
,
2224 isakmp_info_send_r_u
, iph1
);
2226 if (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
||
2227 iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
) {
2228 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2229 isakmp_info_monitor_r_u
, iph1
);
2231 iph1
->dpd_r_u
= sched_new(iph1
->rmconf
->dpd_interval
,
2232 isakmp_info_send_r_u
, iph1
);
2240 * punts dpd for later because of some activity that:
2241 * 1) implicitly does dpd (e.g. phase2 exchanges), or
2242 * 2) indicates liveness (e.g. received ike packets).
2245 isakmp_reschedule_info_monitor_if_pending (phase1_handle_t
*iph1
, char *reason
)
2248 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2249 !iph1
->dpd_support
||
2250 !iph1
->rmconf
->dpd_interval
||
2251 iph1
->rmconf
->dpd_algo
== DPD_ALGO_DEFAULT
) {
2255 if (!iph1
->peer_sent_ike
) {
2256 SCHED_KILL(iph1
->dpd_r_u
);
2258 isakmp_sched_r_u(iph1
, 0);
2260 plog(ASL_LEVEL_NOTICE
,
2261 "%s... rescheduling send_r_u.\n",
2264 iph1
->peer_sent_ike
++;