1 /* $NetBSD: isakmp.c,v 1.20.6.7 2007/08/01 11:52:20 vanhu Exp $ */
3 /* Id: isakmp.c,v 1.74 2006/05/07 21:32:59 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
33 #define __APPLE_API_PRIVATE
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/queue.h>
43 #include <netinet/in.h>
44 #include <net/if_var.h>
45 #include <netinet6/in6_var.h>
46 #include <arpa/inet.h>
48 #ifndef HAVE_NETINET6_IPSEC
49 #include <netinet/ipsec.h>
51 #include <netinet6/ipsec.h>
58 #if TIME_WITH_SYS_TIME
59 # include <sys/time.h>
63 # include <sys/time.h>
84 #include "remoteconf.h"
85 #include "localconf.h"
86 #include "grabmyaddr.h"
87 #include "isakmp_var.h"
92 #include "ipsec_doi.h"
94 #include "crypto_openssl.h"
96 #include "isakmp_ident.h"
97 #include "isakmp_agg.h"
98 #include "isakmp_quick.h"
99 #include "isakmp_inf.h"
100 #include "vpn_control.h"
101 #include "vpn_control_var.h"
103 #include "vendorid.h"
104 #include "isakmp_xauth.h"
105 #include "isakmp_unity.h"
106 #include "isakmp_cfg.h"
109 #include "isakmp_frag.h"
111 #include "strnames.h"
116 # include "nattraversal.h"
118 #include "ike_session.h"
119 # include <netinet/in.h>
120 # include <netinet/udp.h>
121 # include <netinet/in_systm.h>
122 # include <netinet/ip.h>
123 # define SOL_UDP IPPROTO_UDP
124 #include "power_mgmt.h"
126 extern caddr_t
val2str (const char *, size_t);
127 u_char i_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the i_ck. */
128 u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
131 static void isakmp_main (vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
132 static void ikev1_received_packet(vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
133 static int ikev1_ph1begin_r (ike_session_t
*session
, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*, u_int8_t
);
134 static int ikev1_ph2begin_i (phase1_handle_t
*, phase2_handle_t
*);
135 static int ikev1_ph2begin_r (phase1_handle_t
*, vchar_t
*);
139 static void frag_handler (phase1_handle_t
*, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
143 * isakmp packet handler
146 isakmp_handler(int so_isakmp
)
148 struct isakmp isakmp
;
150 u_int64_t force_align
; // Wcast-align fix - force alignment
151 char buf
[sizeof (isakmp
) + 4];
152 u_int32_t non_esp
[2];
153 char lbuf
[sizeof(struct udphdr
) +
157 struct sockaddr_storage remote
;
158 struct sockaddr_storage local
;
159 unsigned int remote_len
= sizeof(remote
);
160 unsigned int local_len
= sizeof(local
);
164 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
167 if (slept_at
|| woke_at
) {
168 plog(ASL_LEVEL_DEBUG
, /* this log is high volume */
169 "ignoring isakmp port until power-mgmt event is handled.\n");
173 /* read message by MSG_PEEK */
174 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
175 MSG_PEEK
, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
179 "failed to receive isakmp packet: %s\n",
184 /* keep-alive packet - ignore */
185 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
186 /* Pull the keep-alive packet */
187 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
188 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
190 "failed to receive keep alive packet: %s\n",
197 /* we don't know about portchange yet,
198 look for non-esp marker instead */
199 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
200 extralen
= NON_ESP_MARKER_LEN
;
202 /* now we know if there is an extra non-esp
203 marker at the beginning or not */
204 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
206 /* check isakmp header length, as well as sanity of header length */
207 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
209 "packet shorter than isakmp header size (size: %zu, minimum expected: %zu)\n", len
, sizeof(isakmp
));
211 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
212 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
214 "failed to receive isakmp packet: %s\n",
220 /* reject it if the size is tooooo big. */
221 if (ntohl(isakmp
.len
) > 0xffff) {
223 "the length in the isakmp header is too big.\n");
224 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
225 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
227 "failed to receive isakmp packet: %s\n",
233 /* read real message */
234 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
236 "failed to allocate reading buffer (%u Bytes)\n",
237 ntohl(isakmp
.len
) + extralen
);
239 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
240 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
242 "failed to receive isakmp packet: %s\n",
244 error
= -2; /* serious problem with socket */
249 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
250 0, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
254 "failed to receive isakmp packet: %s\n",
259 if (len
< extralen
) {
261 "invalid len (%zd Bytes) & extralen (%d Bytes)\n",
266 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
268 "failed to allocate reading buffer (%lu Bytes)\n",
273 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
278 plog(ASL_LEVEL_ERR
, "received invalid length (%zd != %zu), why ?\n",
283 plog(ASL_LEVEL_DEBUG
, "%zd bytes message received %s\n",
284 len
, saddr2str_fromto("from %s to %s",
285 (struct sockaddr
*)&remote
,
286 (struct sockaddr
*)&local
));
288 /* avoid packets with malicious port/address */
289 switch (remote
.ss_family
) {
291 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
295 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
300 "invalid family: %d\n", remote
.ss_family
);
305 "src port == 0 (valid as UDP but not with IKE)\n");
309 /* XXX: check sender whether to be allowed or not to accept */
311 /* XXX: I don't know how to check isakmp half connection attack. */
313 /* simply reply if the packet was processed. */
315 if (ike_session_check_recvdpkt(&remote
, &local
, buf
)) {
316 IPSECLOGASLMSG("Received retransmitted packet from %s.\n",
317 saddr2str((struct sockaddr
*)&remote
));
319 plog(ASL_LEVEL_NOTICE
,
320 "the packet is retransmitted by %s.\n",
321 saddr2str((struct sockaddr
*)&remote
));
326 /* isakmp main routine */
327 isakmp_main(buf
, &remote
, &local
);
339 * main processing to handle isakmp payload
342 isakmp_main(vchar_t
*msg
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
)
344 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
345 u_int8_t isakmp_version
= isakmp
->v
;
347 #ifdef HAVE_PRINT_ISAKMP_C
348 isakmp_printpacket(msg
, remote
, local
, 0);
351 /* the initiator's cookie must not be zero */
352 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
354 "malformed cookie received.\n");
358 /* Check the Major and Minor Version fields. */
360 * XXX Is is right to check version here ?
361 * I think it may no be here because the version depends
362 * on exchange status.
364 if (ISAKMP_GETMAJORV(isakmp_version
) != ISAKMP_MAJOR_VERSION_IKEV1
) {
365 plog(ASL_LEVEL_ERR
, "invalid major version %d.\n", isakmp_version
);
370 #if ISAKMP_MINOR_VERSION > 0 //%%%%%%%% fix this
371 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) { //%%%%%%%%%%%%%%% ??????
373 "invalid minor version %d.\n",
374 ISAKMP_GETMINORV(isakmp
->v
));
380 if (isakmp_version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
381 /* check the Flags field. */
382 /* XXX How is the exclusive check, E and A ? */
383 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
384 plog(ASL_LEVEL_ERR
, "invalid flag 0x%02x.\n", isakmp
->flags
);
388 /* ignore commit bit. */
389 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
390 if (isakmp
->msgid
== 0) {
391 isakmp_info_send_nx(isakmp
, remote
, local
,
392 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
393 plog(ASL_LEVEL_ERR
, "Commit bit on Phase 1 forbidden.\n");
398 ikev1_received_packet(msg
, local
, remote
);
404 * ikev1_received_packet
405 * Handler for received IKEv1 Packets
408 ikev1_received_packet(vchar_t
*msg
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
410 ike_session_t
*session
;
411 phase1_handle_t
*iph1
;
413 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
414 isakmp_index
*index
= (isakmp_index
*)isakmp
;
416 session
= ike_session_get_session(local
, remote
, 0, index
);
418 session
= ike_session_get_session(local
, remote
, 1, NULL
);
421 plog (ASL_LEVEL_NOTICE
, "failed to allocate or find ike session.\n");
425 iph1
= ike_session_getph1byindex(session
, index
);
428 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
429 iph1
->side
== INITIATOR
) {
430 plog(ASL_LEVEL_NOTICE
,
431 "Malformed cookie received or "
432 "the initiator's cookies collide.\n");
437 /* Floating ports for NAT-T */
438 if (NATT_AVAILABLE(iph1
) &&
439 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
440 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
441 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
443 /* prevent memory leak */
444 racoon_free(iph1
->remote
);
445 racoon_free(iph1
->local
);
449 /* copy-in new addresses */
450 iph1
->remote
= dupsaddr(remote
);
451 if (iph1
->remote
== NULL
) {
453 "Phase 1 failed: dupsaddr failed.\n");
456 iph1
->local
= dupsaddr(local
);
457 if (iph1
->local
== NULL
) {
459 "Phase 1 failed: dupsaddr failed.\n");
463 /* set the flag to prevent further port floating
464 (FIXME: should we allow it? E.g. when the NAT gw
466 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
468 /* print some neat info */
469 plog (ASL_LEVEL_NOTICE
,
470 "NAT-T: ports changed to: %s\n",
471 saddr2str_fromto("%s<->%s", (struct sockaddr
*)iph1
->remote
, (struct sockaddr
*)iph1
->local
));
474 /* must be same addresses in one stream of a phase at least. */
475 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
476 char *saddr_db
, *saddr_act
;
478 saddr_db
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
479 saddr_act
= racoon_strdup(saddr2str((struct sockaddr
*)remote
));
480 STRDUP_FATAL(saddr_db
);
481 STRDUP_FATAL(saddr_act
);
483 plog(ASL_LEVEL_WARNING
,
484 "Remote address mismatched. db=%s, act=%s\n",
485 saddr_db
, saddr_act
);
487 racoon_free(saddr_db
);
488 racoon_free(saddr_act
);
492 * don't check of exchange type here because other type will have
493 * the same index, for example, informational exchange.
496 // received ike packets: update dpd checks
497 isakmp_reschedule_info_monitor_if_pending(iph1
, "IKE packet received from peer");
502 // Check exchange type and process accordingly
504 switch (isakmp
->etype
) {
506 case ISAKMP_ETYPE_IDENT
:
507 case ISAKMP_ETYPE_AGG
:
509 /* phase 1 validity check */
510 if (isakmp
->msgid
!= 0) {
511 plog(ASL_LEVEL_ERR
, "Message id should be zero in Phase 1.\n");
515 /* search for isakmp status record of phase 1 */
518 * the packet must be the 1st message from a initiator
519 * or the 2nd message from the responder.
522 /* search for phase1 handle by index without r_ck */
523 iph1
= ike_session_getph1byindex0(session
, index
);
525 /*it must be the 1st message from a initiator.*/
526 if (memcmp(&isakmp
->r_ck
, r_ck0
,
527 sizeof(cookie_t
)) != 0) {
529 plog(ASL_LEVEL_NOTICE
, "Malformed cookie received "
530 "or the spi expired.\n");
534 /* Initiation of new exchange */
535 ikev1_ph1begin_r(session
, msg
, remote
, local
, isakmp
->etype
);
541 * Don't delete phase 1 handler for mismatch
542 * because of no authentication has been completed.
544 if (iph1
->etype
!= isakmp
->etype
) {
546 "Exchange type is mismatched: "
547 "db=%s packet=%s, ignore it.\n",
548 s_isakmp_etype(iph1
->etype
),
549 s_isakmp_etype(isakmp
->etype
));
553 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
554 frag_handler(iph1
, msg
, remote
, local
);
557 fsm_ikev1_phase1_process_payloads(iph1
, msg
);
561 case ISAKMP_ETYPE_INFO
:
562 case ISAKMP_ETYPE_ACKINFO
:
565 * iph1 must be present for Information message.
566 * if iph1 is null then trying to get the phase1 status
567 * as the packet from responder againt initiator's 1st
568 * exchange in phase 1.
569 * NOTE: We think such informational exchange should be ignored.
572 iph1
= ike_session_getph1byindex0(session
, index
);
574 plog(ASL_LEVEL_ERR
, "Unknown Informational exchange received.\n");
577 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
578 plog(ASL_LEVEL_WARNING
,
579 "Remote address mismatched. "
581 saddr2str((struct sockaddr
*)iph1
->remote
));
584 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
585 return frag_handler(iph1
, msg
, remote
, local
);
587 if (isakmp_info_recv(iph1
, msg
) < 0)
592 case ISAKMP_ETYPE_QUICK
:
594 u_int32_t msgid
= isakmp
->msgid
;
595 phase2_handle_t
*iph2
;
598 isakmp_info_send_nx(isakmp
, remote
, local
,
599 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
600 plog(ASL_LEVEL_ERR
, "Can't start the quick mode, "
601 "there is no ISAKMP-SA, %s\n", isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
606 /* Reinit the IVM if it's still there */
607 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
608 oakley_delivm(iph1
->mode_cfg
->ivm
);
609 iph1
->mode_cfg
->ivm
= NULL
;
612 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
613 frag_handler(iph1
, msg
, remote
, local
);
617 /* check status of phase 1 whether negotiated or not. */
618 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
619 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
620 "there is no valid ISAKMP-SA, %s\n", isakmp_pindex(&iph1
->index
, iph1
->msgid
));
624 /* search isakmp phase 2 stauts record. */
625 iph2
= ike_session_getph2bymsgid(iph1
, msgid
);
627 /* it must be new negotiation as responder */
628 ikev1_ph2begin_r(iph1
, msg
);
634 * we keep to set commit bit during negotiation.
635 * When SA is configured, bit will be reset.
637 * don't initiate commit bit. should be fixed in the future.
639 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
640 iph2
->flags
|= ISAKMP_FLAG_C
;
642 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
) &&
643 (iph2
->ph1
== NULL
|| iph2
->ph1
->approval
== NULL
)) {
644 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
645 "invalid linked ISAKMP-SA\n");
648 fsm_ikev1_phase2_process_payloads(iph2
, msg
);
652 case ISAKMP_ETYPE_CFG
:
656 "mode config %d from %s, "
657 "but we have no ISAKMP-SA.\n",
658 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
661 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
663 "mode config %d from %s, "
664 "but ISAKMP-SA %s isn't established.\n",
665 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
),
666 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
669 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
670 return frag_handler(iph1
, msg
, remote
, local
);
671 isakmp_cfg_r(iph1
, msg
);
675 case ISAKMP_ETYPE_NEWGRP
:
676 case ISAKMP_ETYPE_AUTH
:
677 case ISAKMP_ETYPE_NONE
:
680 "Invalid exchange type %d from %s.\n",
681 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
686 /* new negotiation of phase 1 for initiator */
688 ikev1_ph1begin_i(ike_session_t
*session
, struct remoteconf
*rmconf
, struct sockaddr_storage
*remote
,
689 struct sockaddr_storage
*local
, int started_by_api
, nw_nat64_prefix_t
*nat64_prefix
)
692 phase1_handle_t
*iph1
;
694 struct timeval start
, end
;
697 if (session
== NULL
) {
698 session
= ike_session_get_session(local
, remote
, 1, NULL
);
700 plog (ASL_LEVEL_NOTICE
, "failed to allocate or find ike session.\n");
705 /* get new entry to isakmp status table. */
706 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
710 iph1
->rmconf
= rmconf
;
711 retain_rmconf(iph1
->rmconf
);
712 iph1
->side
= INITIATOR
;
713 iph1
->started_by_api
= started_by_api
;
714 if (nat64_prefix
!= NULL
) {
715 memcpy(&iph1
->nat64_prefix
, nat64_prefix
, sizeof(*nat64_prefix
));
717 iph1
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
723 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
724 /* don't call remph1(iph1) until after insph1(iph1) is called */
725 ike_session_delph1(iph1
);
730 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
734 iph1
->frag_chain
= NULL
;
735 iph1
->approval
= NULL
;
737 /* XXX copy remote address */
738 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
739 /* don't call remph1(iph1) until after insph1(iph1) is called */
740 iph1
= NULL
; /* deleted in copy_ph1addresses */
744 if (ike_session_link_phase1(session
, iph1
) != 0) {
745 plog(ASL_LEVEL_NOTICE
, "Failed to link ph1 to session\n");
746 ike_session_delph1(iph1
);
749 // HACK!!! to track rekeys across SIGHUPs
750 if (started_by_api
== VPN_RESTARTED_BY_API
&&
752 iph1
->parent_session
->established
= 1;
753 iph1
->parent_session
->ikev1_state
.ph2cnt
++;
757 /* start phase 1 exchange */
758 iph1
->etype
= rmconf
->etypes
->type
;
759 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
760 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_START
);
761 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
762 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_I_START
);
766 plog(ASL_LEVEL_DEBUG
, "===\n");
770 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
773 plog(ASL_LEVEL_NOTICE
,
774 "initiate new phase 1 negotiation: %s<=>%s\n",
775 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
778 plog(ASL_LEVEL_NOTICE
,
780 s_isakmp_etype(iph1
->etype
));
783 gettimeofday(&iph1
->start
, NULL
);
784 gettimeofday(&start
, NULL
);
787 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by me).\n");
789 if (fsm_ikev1_phase1_send_response(iph1
, NULL
)) {
790 ike_session_unlink_phase1(iph1
);
795 gettimeofday(&end
, NULL
);
796 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
798 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
799 timedelta(&start
, &end
));
802 #ifdef ENABLE_VPNCONTROL_PORT
803 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
809 /* new negotiation of phase 1 for responder */
811 ikev1_ph1begin_r(ike_session_t
*session
, vchar_t
*msg
, struct sockaddr_storage
*remote
,
812 struct sockaddr_storage
*local
, u_int8_t etype
)
815 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
816 struct remoteconf
*rmconf
;
817 phase1_handle_t
*iph1
;
818 struct etypes
*etypeok
;
820 struct timeval start
, end
;
823 /* look for my configuration */
824 rmconf
= getrmconf(remote
);
825 if (rmconf
== NULL
) {
832 /* check to be acceptable exchange type */
833 etypeok
= check_etypeok(rmconf
, etype
);
834 if (etypeok
== NULL
) {
836 "not acceptable %s mode\n", s_isakmp_etype(etype
));
840 /* get new entry to isakmp status table. */
841 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
845 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
846 iph1
->rmconf
= rmconf
;
847 retain_rmconf(iph1
->rmconf
);
849 iph1
->side
= RESPONDER
;
850 iph1
->started_by_api
= 0;
851 iph1
->etype
= etypeok
->type
;
852 iph1
->version
= isakmp
->v
;
855 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
) {
856 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_START
);
857 } else if (iph1
->etype
== ISAKMP_ETYPE_AGG
) {
858 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_R_START
);
860 ike_session_delph1(iph1
);
866 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
867 /* don't call remph1(iph1) until after insph1(iph1) is called */
868 ike_session_delph1(iph1
);
874 iph1
->frag_chain
= NULL
;
875 iph1
->approval
= NULL
;
877 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
878 * We have to setup this flag now to correctly generate the first reply.
879 * Don't know if a better check could be done for that ?
881 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
882 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
884 /* copy remote address */
885 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
886 /* don't call remph1(iph1) until after insph1(iph1) is called */
887 iph1
= NULL
; /* deleted in copy_ph1addresses */
891 if (ike_session_link_phase1(session
, iph1
) != 0) {
892 ike_session_delph1(iph1
);
896 plog(ASL_LEVEL_DEBUG
, "===\n");
900 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
903 plog(ASL_LEVEL_NOTICE
,
904 "respond new phase 1 negotiation: %s<=>%s\n",
905 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
908 plog(ASL_LEVEL_NOTICE
,
909 "begin %s mode.\n", s_isakmp_etype(etype
));
912 gettimeofday(&iph1
->start
, NULL
);
913 gettimeofday(&start
, NULL
);
916 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by peer).\n");
918 /* now that we have a phase1 handle, feed back into our
919 * main receive function to catch fragmented packets
921 isakmp_main(msg
, remote
, local
);
925 /* new negotiation of phase 2 for initiator */
927 ikev1_ph2begin_i(phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
931 if (xauth_check(iph1
) != 0) {
933 "Attempt to start phase 2 whereas Xauth failed\n");
938 /* found ISAKMP-SA. */
939 plog(ASL_LEVEL_DEBUG
, "===\n");
940 plog(ASL_LEVEL_DEBUG
, "begin QUICK mode.\n");
943 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
946 plog(ASL_LEVEL_NOTICE
,
947 "initiate new phase 2 negotiation: %s<=>%s\n",
948 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
953 gettimeofday(&iph2
->start
, NULL
);
957 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
958 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
960 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by me).\n");
961 if (quick_iprep(iph2
, NULL
))
964 #ifdef ENABLE_VPNCONTROL_PORT
965 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
971 /* new negotiation of phase 2 for responder */
973 ikev1_ph2begin_r(phase1_handle_t
*iph1
, vchar_t
*msg
)
975 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
976 phase2_handle_t
*iph2
= 0;
979 struct timeval start
, end
;
982 if (xauth_check(iph1
) != 0) {
984 "Attempt to start Phase 2 whereas Xauth failed\n");
989 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_SA
);
992 "failed to allocate Phase 2 entry.\n");
996 iph2
->side
= RESPONDER
;
997 iph2
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
998 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_START
);
999 iph2
->flags
= isakmp
->flags
;
1000 iph2
->msgid
= isakmp
->msgid
;
1001 iph2
->seq
= pk_getseq();
1002 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1003 if (iph2
->ivm
== NULL
) {
1004 ike_session_delph2(iph2
);
1007 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1008 if (iph2
->dst
== NULL
) {
1009 ike_session_delph2(iph2
);
1012 switch (iph2
->dst
->ss_family
) {
1015 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1021 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1027 "invalid family: %d\n", iph2
->dst
->ss_family
);
1028 ike_session_delph2(iph2
);
1032 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1033 if (iph2
->src
== NULL
) {
1034 ike_session_delph2(iph2
);
1037 switch (iph2
->src
->ss_family
) {
1040 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1046 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1052 "invalid family: %d\n", iph2
->src
->ss_family
);
1053 ike_session_delph2(iph2
);
1057 if (ike_session_link_ph2_to_ph1(iph1
, iph2
))
1060 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
1062 plog(ASL_LEVEL_DEBUG
, "===\n");
1066 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1069 plog(ASL_LEVEL_NOTICE
,
1070 "respond new phase 2 negotiation: %s<=>%s\n",
1071 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1076 gettimeofday(&start
, NULL
);
1079 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by peer).\n");
1081 error
= fsm_ikev1_phase2_process_payloads(iph2
, msg
);
1085 #ifdef ENABLE_VPNCONTROL_PORT
1086 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1093 ikev1_phase1_established(phase1_handle_t
*iph1
)
1096 u_int rekey_lifetime
;
1097 int ini_contact
= iph1
->rmconf
->ini_contact
;
1100 gettimeofday(&iph1
->end
, NULL
);
1101 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1102 "Phase 1", s_isakmp_etype(iph1
->etype
),
1103 timedelta(&iph1
->start
, &iph1
->end
));
1106 #ifdef ENABLE_VPNCONTROL_PORT
1108 if (iph1
->side
== RESPONDER
&&
1109 iph1
->local
->ss_family
== AF_INET
) {
1111 struct redirect
*addr
;
1113 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
1114 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
1115 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
1117 if (raddr
== NULL
) {
1119 "failed to send redirect message - memory error.\n");
1121 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
1122 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
1123 plog(ASL_LEVEL_NOTICE
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
1126 (void)ike_session_update_ph1_ph2tree(iph1
);
1127 isakmp_ph1expire(iph1
);
1135 /* save created date. */
1136 (void)time(&iph1
->created
);
1138 /* add to the schedule to expire, and save back pointer. */
1139 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
1140 isakmp_ph1expire_stub
, iph1
);
1142 if (iph1
->rmconf
->initiate_ph1rekey
) {
1143 if (iph1
->side
== INITIATOR
) {
1144 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
1148 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
1152 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
1153 iph1
->approval
->lifetime
);
1154 if (rekey_lifetime
) {
1155 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
1156 isakmp_ph1rekeyexpire_stub
,
1159 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
1163 "failed to get rekey timer - lifetime is too small... probably.\n");
1167 #ifdef ENABLE_HYBRID
1168 /* ignore xauth if it is a rekey */
1169 if (!iph1
->is_rekey
&&
1170 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1171 switch(AUTHMETHOD(iph1
)) {
1172 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1173 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1174 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1175 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1176 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1177 xauth_sendreq(iph1
);
1178 /* XXX Don't process INITIAL_CONTACT */
1187 /* Schedule the r_u_there.... */
1188 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1189 isakmp_sched_r_u(iph1
, 0);
1192 /* INITIAL-CONTACT processing */
1193 /* ignore initial-contact if it is a rekey */
1194 /* don't send anything if local test mode. */
1195 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
1196 /* send INITIAL-CONTACT */
1197 isakmp_info_send_n1(iph1
,
1198 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1199 /* insert a node into contacted list. */
1200 if (ike_session_inscontacted(iph1
->remote
) == -1) {
1202 "failed to add contacted list.\n");
1207 log_ph1established(iph1
);
1208 plog(ASL_LEVEL_DEBUG
, "===\n");
1210 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1212 #ifdef ENABLE_VPNCONTROL_PORT
1213 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1214 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1221 * parse ISAKMP payloads, without ISAKMP base header.
1224 isakmp_parsewoh(np0
, gen
, len
)
1226 struct isakmp_gen
*gen
;
1229 u_char np
= np0
& 0xff;
1232 struct isakmp_parse_t
*p
, *ep
;
1234 plog(ASL_LEVEL_DEBUG
, "begin.\n");
1237 * 5 is a magic number, but any value larger than 2 should be fine
1238 * as we do vrealloc() in the following loop.
1240 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1241 if (result
== NULL
) {
1243 "failed to get buffer.\n");
1246 // Wcast-align fix (void*) - result = aligned buffer of struct isakmp_parse_t
1247 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1248 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1252 /* parse through general headers */
1253 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1254 if (tlen
<= sizeof(struct isakmp_gen
)) {
1255 /* don't send information, see isakmp_ident_r1() */
1257 "isakmp_parsewoh invalid length of payload (1)\n");
1262 plog(ASL_LEVEL_NOTICE
,
1263 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1266 p
->len
= ntohs(gen
->len
);
1267 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1268 plog(ASL_LEVEL_NOTICE
,
1269 "isakmp_parsewoh invalid length of payload (2)\n");
1278 off
= p
- ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1279 result
= vrealloc(result
, result
->l
* 2);
1280 if (result
== NULL
) {
1281 plog(ASL_LEVEL_NOTICE
,
1282 "failed to realloc buffer.\n");
1286 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)
1287 (result
->v
+ result
->l
- sizeof(*ep
));
1288 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1293 plen
= ntohs(gen
->len
);
1294 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1297 p
->type
= ISAKMP_NPTYPE_NONE
;
1301 plog(ASL_LEVEL_DEBUG
, "succeed.\n");
1307 * parse ISAKMP payloads, including ISAKMP base header.
1313 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1314 struct isakmp_gen
*gen
;
1320 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1321 tlen
= buf
->l
- sizeof(struct isakmp
);
1322 result
= isakmp_parsewoh(np
, gen
, tlen
);
1331 ike_session_initctdtree();
1332 ike_session_init_recvdpkt();
1334 if (isakmp_open() < 0)
1347 ike_session_clear_recvdpkt();
1348 ike_session_clear_contacted();
1352 * make strings containing i_cookie + r_cookie + msgid
1355 isakmp_pindex(index
, msgid
)
1356 const isakmp_index
*index
;
1357 const u_int32_t msgid
;
1359 static char buf
[64];
1363 memset(buf
, 0, sizeof(buf
));
1366 p
= (const u_char
*)index
;
1367 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1368 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1380 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1386 /* open ISAKMP sockets. */
1391 int ifnum
= 0, encap_ifnum
= 0;
1396 int tentative_failures
= 0;
1399 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1402 if (p
->sock
!= -1) {
1406 continue; // socket already open
1409 /* warn if wildcard address - should we forbid this? */
1410 switch (p
->addr
->ss_family
) {
1412 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1413 plog(ASL_LEVEL_WARNING
,
1414 "listening to wildcard address,"
1415 "broadcast IKE packet may kill you\n");
1419 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1420 plog(ASL_LEVEL_WARNING
,
1421 "listening to wildcard address, "
1422 "broadcast IKE packet may kill you\n");
1427 "unsupported address family %d\n",
1428 lcconf
->default_af
);
1433 if (p
->addr
->ss_family
== AF_INET6
&&
1434 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1435 p
->addr
)->sin6_addr
))
1437 plog(ASL_LEVEL_NOTICE
,
1438 "Ignoring multicast address %s\n",
1439 saddr2str((struct sockaddr
*)p
->addr
));
1440 racoon_free(p
->addr
);
1446 if ((p
->sock
= socket(p
->addr
->ss_family
, SOCK_DGRAM
, 0)) < 0) {
1448 "socket (%s)\n", strerror(errno
));
1452 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1453 plog(ASL_LEVEL_ERR
, "failed to put socket in non-blocking mode\n");
1455 /* receive my interface address on inbound packets. */
1456 switch (p
->addr
->ss_family
) {
1458 int ifindex
= if_nametoindex(p
->ifname
);
1460 setsockopt(p
->sock
, IPPROTO_IP
,
1461 IP_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1463 "setsockopt IP_BOUND_IF (%s)\n",
1467 if (setsockopt(p
->sock
, IPPROTO_IP
,
1469 (const void *)&yes
, sizeof(yes
)) < 0) {
1471 "setsockopt IP_RECVDSTADDR (%s)\n",
1480 pktinfo
= IPV6_RECVPKTINFO
;
1482 pktinfo
= IPV6_RECVDSTADDR
;
1484 int ifindex
= if_nametoindex(p
->ifname
);
1486 setsockopt(p
->sock
, IPPROTO_IPV6
,
1487 IPV6_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1489 "setsockopt IPV6_BOUND_IF (%s)\n",
1493 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1494 (const void *)&yes
, sizeof(yes
)) < 0)
1497 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1498 pktinfo
, strerror(errno
));
1506 #ifdef IPV6_USE_MIN_MTU
1507 if (p
->addr
->ss_family
== AF_INET6
&&
1508 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1509 (void *)&yes
, sizeof(yes
)) < 0) {
1511 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1517 if (setsockopt_bypass(p
->sock
, p
->addr
->ss_family
) < 0)
1520 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEADDR
, (const void *)&yes
, sizeof(yes
)) < 0) {
1521 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEADDR (%s)\n", strerror(errno
));
1525 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEPORT
, (const void *)&yes
, sizeof(yes
)) < 0) {
1526 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEPORT (%s)\n", strerror(errno
));
1530 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1531 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1532 (void *)&yes
, sizeof(yes
)) < 0) {
1534 "setsockopt (%s)\n", strerror(errno
));
1539 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1540 int tmp_errno
= errno
;
1542 "failed to bind to address %s (%s).\n",
1543 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1545 // if bind failed b/c of a tentative v6 address, try again later
1546 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1547 struct in6_ifreq ifr6
;
1549 bzero(&ifr6
, sizeof(ifr6
));
1550 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1551 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1552 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1554 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1555 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1557 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1558 // address may have been tentantive... invalidate sock but leave address around for another try later
1560 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1561 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1564 tentative_failures
= 1;
1568 "failed to bind to address %s: because of interface address error, flags %x.\n",
1569 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1573 "failed to bind to address %s: can't read interface address flags.\n",
1574 saddr2str((struct sockaddr
*)p
->addr
));
1589 if (p
->source
!= NULL
) {
1590 dispatch_source_cancel(p
->source
);
1593 p
->source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, p
->sock
, 0, dispatch_get_main_queue());
1594 if (p
->source
== NULL
) {
1595 plog(ASL_LEVEL_ERR
, "could not create isakmp socket source.");
1598 dispatch_source_set_event_handler(p
->source
,
1602 dispatch_source_t the_source
= p
->source
;
1603 dispatch_source_set_cancel_handler(p
->source
,
1606 dispatch_release(the_source
);
1607 }); dispatch_resume(p
->source
);
1609 plog(ASL_LEVEL_NOTICE
,
1610 "%s used as isakmp port (fd=%d)\n",
1611 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
1615 racoon_free(p
->addr
);
1618 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
) {
1627 "no address could be bound.\n");
1633 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1634 plog(ASL_LEVEL_WARNING
,
1635 "NAT-T is enabled in at least one remote{} section,\n");
1636 plog(ASL_LEVEL_WARNING
,
1637 "but no 'isakmp_natt' address was specified!\n");
1641 if (tentative_failures
)
1642 sched_new(5, update_myaddrs
, NULL
);
1648 isakmp_suspend_sockets()
1652 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1654 dispatch_suspend(p
->source
);
1661 isakmp_close_sockets();
1666 isakmp_close_sockets()
1670 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1676 dispatch_source_cancel(p
->source
);
1685 // close sockets for addresses that have gone away
1687 isakmp_close_unused()
1689 struct myaddrs
*p
, *next
, **prev
;
1691 prev
= &(lcconf
->myaddrs
);
1692 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1694 if (p
->in_use
== 0) { // not in use ?
1696 dispatch_source_cancel(p
->source
);
1707 isakmp_send(iph1
, sbuf
)
1708 phase1_handle_t
*iph1
;
1713 vchar_t
*vbuf
= NULL
;
1716 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1720 * Do not add the non ESP marker for a packet that will
1721 * be fragmented. The non ESP marker should appear in
1722 * all fragment's packets, but not in the fragmented packet
1724 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1728 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1730 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1731 must added just before the packet itself. For this we must
1732 allocate a new buffer and release it at the end. */
1734 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1736 "vbuf allocation failed\n");
1739 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
1740 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1745 /* select the socket to be sent */
1746 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
1753 plog (ASL_LEVEL_DEBUG
, "%zu bytes %s\n", sbuf
->l
,
1754 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
1757 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1758 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1760 "isakmp_sendfrags failed\n");
1768 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1769 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1771 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1784 /* called from scheduler */
1786 isakmp_ph1resend_stub(p
)
1789 phase1_handle_t
*iph1
;
1791 iph1
=(phase1_handle_t
*)p
;
1792 if(isakmp_ph1resend(iph1
) < 0){
1794 /* Should not happen...
1796 SCHED_KILL(iph1
->scr
);
1799 ike_session_unlink_phase1(iph1
);
1804 isakmp_ph1resend(iph1
)
1805 phase1_handle_t
*iph1
;
1807 time_t retry_interval
;
1809 // make sure there is a buffer to send
1810 // isakmp_plist_set_all() could have returned NULL
1811 if (iph1
->sendbuf
== NULL
)
1814 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1816 if (iph1
->retry_counter
<= 0) {
1818 "Phase 1 negotiation failed due to time up. %s\n",
1819 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1820 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
1821 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
1822 if (isakmp_ph1rekeyretry(iph1
) == 0)
1827 ike_session_ph1_retransmits(iph1
);
1830 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1832 "Phase 1 negotiation failed due to send error. %s\n",
1833 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1837 plog(ASL_LEVEL_NOTICE
,
1838 "Resend Phase 1 packet %s\n",
1839 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1841 iph1
->retry_counter
--;
1842 retry_interval
= ike_session_get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
1843 iph1
->rmconf
->retry_interval
);
1844 iph1
->scr
= sched_new(retry_interval
,
1845 isakmp_ph1resend_stub
, iph1
);
1850 /* called from scheduler */
1852 isakmp_ph2resend_stub(p
)
1855 phase2_handle_t
*iph2
;
1857 iph2
=(phase2_handle_t
*)p
;
1859 if(isakmp_ph2resend(iph2
) < 0){
1860 ike_session_unlink_phase2(iph2
);
1865 isakmp_ph2resend(iph2
)
1866 phase2_handle_t
*iph2
;
1868 time_t retry_interval
;
1870 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1872 //%%% BUG FIX - related to commit bit usage - crash happened here
1873 if (iph2
->ph1
== 0) {
1875 "Internal error - attempt to re-send Phase 2 with no Phase 1 bound.\n");
1879 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)){
1881 "Phase 2 negotiation failed due to Phase 1 expired. %s\n",
1882 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1886 if (iph2
->retry_counter
<= 0) {
1888 "Phase 2 negotiation failed due to time up. %s\n",
1889 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1892 ike_session_ph2_retransmits(iph2
);
1895 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1897 "Phase 2 negotiation failed due to send error. %s\n",
1898 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1903 plog(ASL_LEVEL_NOTICE
,
1904 "Resend Phase 2 packet %s\n",
1905 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1907 iph2
->retry_counter
--;
1908 retry_interval
= ike_session_get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
1909 iph2
->ph1
->rmconf
->retry_interval
);
1910 iph2
->scr
= sched_new(retry_interval
,
1911 isakmp_ph2resend_stub
, iph2
);
1915 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
1916 "Phase 2 packets sent to peer: retransmit timer armed");
1923 /* called from scheduler */
1925 isakmp_ph1expire_stub(p
)
1929 isakmp_ph1expire((phase1_handle_t
*)p
);
1933 isakmp_ph1expire(iph1
)
1934 phase1_handle_t
*iph1
;
1937 phase1_handle_t
*new_iph1
;
1939 SCHED_KILL(iph1
->sce
);
1941 SCHED_KILL(iph1
->dpd_r_u
);
1944 if(!FSM_STATE_IS_EXPIRED(iph1
->status
)){
1945 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
1946 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
1950 plog(ASL_LEVEL_NOTICE
,
1951 "ISAKMP-SA expired %s-%s spi:%s\n",
1953 isakmp_pindex(&iph1
->index
, 0));
1956 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
1957 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
1961 * the phase1 deletion is postponed until there is no phase2.
1963 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
1964 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
1968 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1971 /* called from scheduler */
1973 isakmp_ph1rekeyexpire_stub(p
)
1977 isakmp_ph1rekeyexpire((phase1_handle_t
*)p
, FALSE
);
1981 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
1982 phase1_handle_t
*iph1
;
1983 int ignore_sess_drop_policy
;
1986 struct remoteconf
*rmconf
;
1988 SCHED_KILL(iph1
->sce_rekey
);
1990 // We are going to start the rekey. Let's fire off the
1991 // phase1 expiration timer if it is not done yet.
1992 if (!iph1
->sce
&& iph1
->approval
->lifetimegap
) {
1993 iph1
->sce
= sched_new(iph1
->approval
->lifetimegap
,
1994 isakmp_ph1expire_stub
, iph1
);
1997 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
1998 if (iph1
->sce
== 0 ||
1999 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2004 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2005 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2009 plog(ASL_LEVEL_NOTICE
,
2010 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2012 isakmp_pindex(&iph1
->index
, 0));
2016 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2020 // exit if there is another ph1 that is established (with a pending rekey timer)
2021 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2022 plog(ASL_LEVEL_NOTICE
,
2023 "Request for ISAKMP-SA rekey was ignored "
2024 "due to another established ph1.\n");
2028 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2029 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2030 plog(ASL_LEVEL_DEBUG
, "Reschedule Phase 1 rekey.\n");
2031 iph1
->sce_rekey
= sched_new(1,
2032 isakmp_ph1rekeyexpire_stub
,
2037 // get rmconf to initiate rekey with
2038 rmconf
= iph1
->rmconf
;
2040 rmconf
= getrmconf(iph1
->remote
);
2043 /* begin quick mode */
2044 plog(ASL_LEVEL_NOTICE
, "Begin Phase 1 rekey.\n");
2046 /* start phase 1 negotiation as a initiator. */
2047 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2048 plog(ASL_LEVEL_NOTICE
, "Phase 1 rekey Failed.\n");
2050 iph1
->is_rekey
= TRUE
;
2053 "Phase1 rekey failed: no configuration found for %s.\n",
2054 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2059 isakmp_ph1rekeyretry(iph1
)
2060 phase1_handle_t
*iph1
;
2063 struct remoteconf
*rmconf
;
2065 // this code path is meant for floated ph1 rekeys that are failing on the first message
2066 if (iph1
->sce
!= 0 ||
2067 iph1
->sce_rekey
!= 0 ||
2068 ((iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
&&
2069 iph1
->status
!= IKEV1_STATE_AGG_I_MSG1SENT
)
2070 || ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0))
2071 || (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
)
2072 || iph1
->is_dying
) {
2076 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2077 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2081 plog(ASL_LEVEL_NOTICE
,
2082 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2084 isakmp_pindex(&iph1
->index
, 0));
2088 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2089 plog(ASL_LEVEL_NOTICE
,
2090 "request for ISAKMP-SA rekey was ignored "
2091 "due to idleness.\n");
2095 // exit if there is another ph1 that is established (with a pending rekey timer)
2096 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2097 plog(ASL_LEVEL_NOTICE
,
2098 "request to retry ISAKMP-SA rekey was ignored "
2099 "due to another established ph1.\n");
2103 // some servers don't like respond to 4500 for rekeys... try accomodate them
2104 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2105 set_port(iph1
->local
, PORT_ISAKMP
);
2107 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2108 set_port(iph1
->remote
, PORT_ISAKMP
);
2110 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2111 rmconf
= getrmconf(iph1
->remote
);
2113 /* begin quick mode */
2114 plog(ASL_LEVEL_NOTICE
, "begin Phase 1 rekey retry.\n");
2116 /* start phase 1 negotiation as a initiator. */
2117 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2118 plog(ASL_LEVEL_NOTICE
, "Phase 1 rekey retry Failed.\n");
2121 iph1
->is_rekey
= TRUE
;
2124 "Phase 1 rekey retry failed: no configuration found for %s.\n",
2125 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2131 /* called from scheduler */
2133 isakmp_ph1delete_stub(p
)
2137 isakmp_ph1delete((phase1_handle_t
*)p
);
2141 isakmp_ph1delete(iph1
)
2142 phase1_handle_t
*iph1
;
2146 SCHED_KILL(iph1
->sce
);
2147 SCHED_KILL(iph1
->sce_rekey
);
2149 SCHED_KILL(iph1
->dpd_r_u
);
2152 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2153 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2157 isakmp_info_send_d1(iph1
);
2159 /* don't re-negosiation when the phase 1 SA expires. */
2161 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2162 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2166 plog(ASL_LEVEL_NOTICE
,
2167 "ISAKMP-SA deleted spi:%s\n",
2168 isakmp_pindex(&iph1
->index
, 0));
2169 plog(ASL_LEVEL_DEBUG
,
2170 "ISAKMP-SA deleted %s-%s spi:%s\n",
2171 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2175 ike_session_unlink_phase1(iph1
);
2180 /* called from scheduler.
2181 * this function will call only isakmp_ph2delete().
2182 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2183 * by something cause. That's why this function is called after phase 2 SA
2184 * expires in the userland.
2187 isakmp_ph2expire_stub(p
)
2191 isakmp_ph2expire((phase2_handle_t
*)p
);
2195 isakmp_ph2expire(iph2
)
2196 phase2_handle_t
*iph2
;
2200 if (FSM_STATE_IS_EXPIRED(iph2
->status
)) {
2204 SCHED_KILL(iph2
->sce
);
2206 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2207 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2211 plog(ASL_LEVEL_NOTICE
,
2212 "Phase 2 sa expired %s-%s\n", src
, dst
);
2216 // delete outgoing SAs
2217 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
) && iph2
->approval
) {
2220 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2222 pfkey_send_delete(lcconf
->sock_pfkey
,
2223 ipsecdoi2pfkey_proto(pr
->proto_id
),
2225 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2229 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2230 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
2231 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2236 /* called from scheduler */
2238 isakmp_ph2delete_stub(p
)
2242 isakmp_ph2delete((phase2_handle_t
*)p
);
2246 isakmp_ph2delete(iph2
)
2247 phase2_handle_t
*iph2
;
2251 SCHED_KILL(iph2
->sce
);
2253 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2254 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2258 plog(ASL_LEVEL_NOTICE
,
2259 "Phase 2 sa deleted %s-%s\n", src
, dst
);
2263 ike_session_unlink_phase2(iph2
);
2269 * Interface between PF_KEYv2 and ISAKMP
2272 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2273 * if phase1 has been finished, begin phase2.
2276 isakmp_post_acquire(phase2_handle_t
*iph2
)
2278 struct remoteconf
*rmconf
;
2279 phase1_handle_t
*iph1
= NULL
;
2281 plog(ASL_LEVEL_DEBUG
, "In post_acquire\n");
2283 /* search appropreate configuration with masking port. */
2284 rmconf
= getrmconf(iph2
->dst
);
2285 if (rmconf
== NULL
) {
2287 "No configuration found for %s.\n",
2288 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2291 /* if passive mode, ignore the acquire message */
2292 if (rmconf
->passive
) {
2293 plog(ASL_LEVEL_NOTICE
,
2294 "Because of passive mode, ignore the acquire message for %s.\n",
2295 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2300 // what if there is another ph2 that is negotiating
2301 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2302 // TODO: postpone this rekey for a second later
2303 plog(ASL_LEVEL_NOTICE
,
2304 "Request for establishing IPsec-SA was ignored due to another negoing ph2.\n");
2308 // if this is a phase2 rekeys (the policy may not have the current port number).
2309 // so, use the appropriate ports.
2310 if (iph2
->is_rekey
) {
2311 ike_session_update_ph2_ports(iph2
);
2313 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2314 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2316 /* no IKE-SA found. */
2318 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2320 /* start phase 1 negotiation as a initiator. */
2321 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2323 plog(ASL_LEVEL_NOTICE
,
2324 "IPsec-SA request for %s queued due to no Phase 1 found.\n",
2325 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2327 // exit if there is another ph1 that is established (with a pending rekey timer)
2328 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2329 plog(ASL_LEVEL_NOTICE
,
2330 "Request for Phase 1 was ignored due to another negotiating Phase 1.\n");
2334 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, &iph2
->nat64_prefix
) < 0) {
2335 plog(ASL_LEVEL_NOTICE
,
2336 "Request for Phase 1 failed. Will try later.\n");
2342 /* found ISAKMP-SA, but on negotiation. */
2343 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2344 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2345 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2346 plog(ASL_LEVEL_NOTICE
,
2347 "Request for establishing IPsec-SA was queued due to no phase1 found.\n");
2352 /* found established ISAKMP-SA */
2354 /* found ISAKMP-SA. */
2356 /* begin quick mode */
2357 plog(ASL_LEVEL_NOTICE
, "Begin QUICK mode.\n");
2358 if (ikev1_ph2begin_i(iph1
, iph2
))
2364 * receive GETSPI from kernel.
2367 isakmp_post_getspi(phase2_handle_t
*iph2
)
2370 struct timeval start
, end
;
2374 /* don't process it because there is no suitable phase1-sa. */
2375 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)) {
2377 "the negotiation is stopped, "
2378 "because there is no suitable ISAKMP-SA.\n");
2381 fsm_set_state(&iph2
->status
, iph2
->side
== INITIATOR
?
2382 IKEV1_STATE_QUICK_I_GETSPIDONE
: IKEV1_STATE_QUICK_R_GETSPIDONE
);
2385 gettimeofday(&start
, NULL
);
2387 switch (iph2
->side
) {
2389 error
= quick_i1send(iph2
, NULL
);
2392 error
= quick_r2send(iph2
, NULL
);
2396 if (error
) //%%%%%%%% log something ???
2399 gettimeofday(&end
, NULL
);
2400 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
2402 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2403 timedelta(&start
, &end
));
2409 /* called by scheduler */
2411 isakmp_chkph1there_stub(p
)
2414 isakmp_chkph1there((phase2_handle_t
*)p
);
2418 isakmp_chkph1there(iph2
)
2419 phase2_handle_t
*iph2
;
2421 phase1_handle_t
*iph1
;
2423 if ((iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
&& iph2
->status
!= IKEV1_STATE_QUICK_I_START
) ||
2425 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: ph2 handle has advanced too far (status %d, START %d, dying %d)... ignoring\n", iph2
->status
, IKEV1_STATE_QUICK_I_START
, iph2
->is_dying
);
2429 iph2
->retry_checkph1
--;
2431 if (iph2
->retry_checkph1
< 0 /* %%%%||
2432 ike_session_verify_ph2_parent_session(iph2) */) {
2433 if (iph2
->retry_checkph1
< 0) {
2435 "Phase 2 negotiation failed "
2436 "due to time up waiting for Phase 1. %s\n",
2437 sadbsecas2str(iph2
->dst
, iph2
->src
,
2438 iph2
->satype
, 0, 0));
2441 "Phase 2 negotiation failed "
2442 "due to invalid parent session. %s\n",
2443 sadbsecas2str(iph2
->dst
, iph2
->src
,
2444 iph2
->satype
, 0, 0));
2446 plog(ASL_LEVEL_NOTICE
,
2447 "delete Phase 2 handler.\n");
2449 /* send acquire to kernel as error */
2450 pk_sendeacquire(iph2
);
2451 ike_session_unlink_phase2(iph2
);
2455 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2457 /* XXX Even if ph1 as responder is there, should we not start
2458 * phase 2 negotiation ? */
2460 && FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2461 /* found isakmp-sa */
2463 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2464 plog(ASL_LEVEL_DEBUG
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2465 plog(ASL_LEVEL_DEBUG
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2466 plog(ASL_LEVEL_DEBUG
, "before:\n");
2467 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2468 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2469 set_port(iph2
->src
, extract_port(iph1
->local
));
2470 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2471 plog(ASL_LEVEL_DEBUG
, "After:\n");
2472 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2473 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2475 /* begin quick mode */
2476 if (ikev1_ph2begin_i(iph1
, iph2
)) {
2477 ike_session_unlink_phase2(iph2
);
2481 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2482 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2483 /* start phase 1 negotiation as a initiator. */
2485 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, iph1
!= NULL
? &iph1
->nat64_prefix
: NULL
) < 0) {
2486 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2488 } else if (rmconf
== NULL
) {
2489 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no remoteconf found... failed to initiate new one\n");
2493 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no established ph1 handler found\n");
2495 /* no isakmp-sa found */
2496 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2501 /* copy variable data into ALLOCATED buffer. */
2503 isakmp_set_attr_v(buf
, type
, val
, len
)
2509 struct isakmp_data
*data
;
2511 data
= (struct isakmp_data
*)buf
;
2512 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2513 data
->lorv
= htons((u_int16_t
)len
);
2514 memcpy(data
+ 1, val
, len
);
2516 return buf
+ sizeof(*data
) + len
;
2519 /* copy fixed length data into ALLOCATED buffer. */
2521 isakmp_set_attr_l(buf
, type
, val
)
2526 struct isakmp_data
*data
;
2528 data
= (struct isakmp_data
*)buf
;
2529 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2530 data
->lorv
= htons((u_int16_t
)val
);
2532 return buf
+ sizeof(*data
);
2535 /* add a variable data attribute to the buffer by reallocating it. */
2537 isakmp_add_attr_v(buf0
, type
, val
, len
)
2543 vchar_t
*buf
= NULL
;
2544 struct isakmp_data
*data
;
2548 tlen
= sizeof(*data
) + len
;
2552 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2554 buf
= vmalloc(tlen
);
2557 "failed to get a attribute buffer.\n");
2561 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2562 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2563 data
->lorv
= htons((u_int16_t
)len
);
2564 memcpy(data
+ 1, val
, len
);
2569 /* add a fixed data attribute to the buffer by reallocating it. */
2571 isakmp_add_attr_l(buf0
, type
, val
)
2576 vchar_t
*buf
= NULL
;
2577 struct isakmp_data
*data
;
2581 tlen
= sizeof(*data
);
2585 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2587 buf
= vmalloc(tlen
);
2590 "failed to get a attribute buffer.\n");
2594 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2595 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2596 data
->lorv
= htons((u_int16_t
)val
);
2602 * calculate cookie and set.
2605 isakmp_newcookie(place
, remote
, local
)
2607 struct sockaddr_storage
*remote
;
2608 struct sockaddr_storage
*local
;
2610 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2620 if (remote
->ss_family
!= local
->ss_family
) {
2622 "address family mismatch, remote:%d local:%d\n",
2623 remote
->ss_family
, local
->ss_family
);
2626 switch (remote
->ss_family
) {
2628 alen
= sizeof(struct in_addr
);
2629 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2630 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2634 alen
= sizeof(struct in_addr
);
2635 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2636 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2641 "invalid family: %d\n", remote
->ss_family
);
2644 blen
= (alen
+ sizeof(u_short
)) * 2
2645 + sizeof(time_t) + lcconf
->secret_size
;
2646 buf
= vmalloc(blen
);
2649 "failed to get a cookie.\n");
2654 /* copy my address */
2655 memcpy(p
, sa1
, alen
);
2657 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2658 memcpy(p
, &port
, sizeof(u_short
));
2659 p
+= sizeof(u_short
);
2661 /* copy target address */
2662 memcpy(p
, sa2
, alen
);
2664 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2665 memcpy(p
, &port
, sizeof(u_short
));
2666 p
+= sizeof(u_short
);
2670 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2673 /* copy random value */
2674 buf2
= eay_set_random(lcconf
->secret_size
);
2677 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2678 p
+= lcconf
->secret_size
;
2681 buf2
= eay_sha1_one(buf
);
2682 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2684 sa1
= val2str(place
, sizeof (cookie_t
));
2685 plog(ASL_LEVEL_DEBUG
, "new cookie:\n%s\n", sa1
);
2698 * save partner's(payload) data into phhandle.
2701 isakmp_p2ph(buf
, gen
)
2703 struct isakmp_gen
*gen
;
2705 /* XXX to be checked in each functions for logging. */
2707 plog(ASL_LEVEL_WARNING
,
2708 "ignore this payload, same payload type exist.\n");
2712 if (ntohs(gen
->len
) < sizeof(*gen
)) {
2714 "ignore this payload, invalid payload len %d.\n",
2719 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2722 "failed to get buffer.\n");
2725 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2731 isakmp_newmsgid2(iph1
)
2732 phase1_handle_t
*iph1
;
2737 msgid2
= eay_random();
2738 } while (ike_session_getph2bymsgid(iph1
, msgid2
));
2744 * set values into allocated buffer of isakmp header for phase 1
2747 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2749 phase1_handle_t
*iph1
;
2755 struct isakmp
*isakmp
;
2757 if (vbuf
->l
< sizeof(*isakmp
))
2760 isakmp
= (struct isakmp
*)vbuf
->v
;
2762 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2763 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2764 isakmp
->np
= nptype
;
2765 isakmp
->v
= iph1
->version
;
2766 isakmp
->etype
= etype
;
2767 isakmp
->flags
= flags
;
2768 isakmp
->msgid
= msgid
;
2769 isakmp
->len
= htonl(vbuf
->l
);
2771 return vbuf
->v
+ sizeof(*isakmp
);
2775 * set values into allocated buffer of isakmp header for phase 1
2778 set_isakmp_header1(vbuf
, iph1
, nptype
)
2780 phase1_handle_t
*iph1
;
2783 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2787 * set values into allocated buffer of isakmp header for phase 2
2790 set_isakmp_header2(vbuf
, iph2
, nptype
)
2792 phase2_handle_t
*iph2
;
2795 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2799 * set values into allocated buffer of isakmp payload.
2802 set_isakmp_payload(buf
, src
, nptype
)
2807 struct isakmp_gen
*gen
;
2810 plog(ASL_LEVEL_DEBUG
, "add payload of len %zu, next type %d\n",
2813 gen
= (struct isakmp_gen
*)p
;
2815 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2817 memcpy(p
, src
->v
, src
->l
);
2824 #ifdef HAVE_PRINT_ISAKMP_C
2825 /* for print-isakmp.c */
2827 extern void isakmp_print(const u_char
*, u_int
, const u_char
*);
2829 char *getname(const u_char
*);
2831 char *getname6(const u_char
*);
2833 int safeputchar(int);
2836 * Return a name for the IP address pointed to by ap. This address
2837 * is assumed to be in network byte order.
2843 struct sockaddr_in addr
;
2844 static char ntop_buf
[NI_MAXHOST
];
2846 memset(&addr
, 0, sizeof(addr
));
2847 addr
.sin_len
= sizeof(struct sockaddr_in
);
2848 addr
.sin_family
= AF_INET
;
2849 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2850 if (getnameinfo(&addr
, sizeof(addr
),
2851 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2852 NI_NUMERICHOST
| niflags
))
2853 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2860 * Return a name for the IP6 address pointed to by ap. This address
2861 * is assumed to be in network byte order.
2867 struct sockaddr_in6 addr
;
2868 static char ntop_buf
[NI_MAXHOST
];
2870 memset(&addr
, 0, sizeof(addr
));
2871 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2872 addr
.sin6_family
= AF_INET6
;
2873 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2874 if (getnameinfo(&addr
, addr
.sin6_len
,
2875 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2876 NI_NUMERICHOST
| niflags
))
2877 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2889 ch
= (unsigned char)(c
& 0xff);
2890 if (c
< 0x80 && isprint(c
))
2891 return printf("%c", c
& 0xff);
2893 return printf("\\%03o", c
& 0xff);
2897 isakmp_printpacket(msg
, from
, my
, decoded
)
2899 struct sockaddr_storage
*from
;
2900 struct sockaddr_storage
*my
;
2906 char hostbuf
[NI_MAXHOST
];
2907 char portbuf
[NI_MAXSERV
];
2908 struct isakmp
*isakmp
;
2912 if (loglevel
< ASL_LEVEL_DEBUG
)
2916 plog(ASL_LEVEL_DEBUG
, "begin.\n");
2918 gettimeofday(&tv
, NULL
);
2919 s
= tv
.tv_sec
% 3600;
2920 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2923 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
2924 portbuf
, sizeof(portbuf
),
2925 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2926 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2927 strlcpy(portbuf
, "?", sizeof(portbuf
));
2929 printf("%s:%s", hostbuf
, portbuf
);
2934 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
2935 portbuf
, sizeof(portbuf
),
2936 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2937 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2938 strlcpy(portbuf
, "?", sizeof(portbuf
));
2940 printf("%s:%s", hostbuf
, portbuf
);
2947 printf("(malloc fail)\n");
2951 isakmp
= (struct isakmp
*)buf
->v
;
2952 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2955 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2956 if (buf
->l
< pad
&& 2 < vflag
)
2957 printf("(wrong padding)");
2959 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
2963 snapend
= buf
->v
+ buf
->l
;
2964 isakmp_print(buf
->v
, buf
->l
, NULL
);
2972 #endif /*HAVE_PRINT_ISAKMP_C*/
2975 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
2976 phase1_handle_t
*iph1
;
2977 struct remoteconf
*rmconf
;
2978 struct sockaddr_storage
*remote
, *local
;
2980 u_short
*port
= NULL
;
2982 /* address portion must be grabbed from real remote address "remote" */
2983 iph1
->remote
= dupsaddr(remote
);
2984 if (iph1
->remote
== NULL
) {
2985 ike_session_delph1(iph1
);
2990 * if remote has no port # (in case of initiator - from ACQUIRE msg)
2991 * - if remote.conf specifies port #, use that
2992 * - if remote.conf does not, use 500
2993 * if remote has port # (in case of responder - from recvfrom(2))
2994 * respect content of "remote".
2996 switch (iph1
->remote
->ss_family
) {
2998 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3001 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3004 *port
= htons(PORT_ISAKMP
);
3008 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3011 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3014 *port
= htons(PORT_ISAKMP
);
3019 "invalid family: %d\n", iph1
->remote
->ss_family
);
3020 ike_session_delph1(iph1
);
3025 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3027 iph1
->local
= dupsaddr(local
);
3028 if (iph1
->local
== NULL
) {
3029 ike_session_delph1(iph1
);
3033 switch (iph1
->local
->ss_family
) {
3035 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3038 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3041 *port
= getmyaddrsport(iph1
->local
);
3045 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3048 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3051 *port
= getmyaddrsport(iph1
->local
);
3056 "invalid family: %d\n", iph1
->local
->ss_family
);
3057 ike_session_delph1(iph1
);
3061 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3062 plog (ASL_LEVEL_NOTICE
, "Marking ports as changed\n");
3063 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3071 log_ph1established(iph1
)
3072 const phase1_handle_t
*iph1
;
3076 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3077 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3081 plog(ASL_LEVEL_NOTICE
,
3082 "ISAKMP-SA established spi:%s\n",
3083 isakmp_pindex(&iph1
->index
, 0));
3084 plog(ASL_LEVEL_DEBUG
,
3085 "ISAKMP-SA established %s-%s spi:%s\n",
3087 isakmp_pindex(&iph1
->index
, 0));
3092 IPSECLOGASLMSG("IPSec Phase 1 established (Initiated by %s).\n",
3093 (iph1
->side
== INITIATOR
)? "me" : "peer");
3098 struct payload_list
*
3099 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3102 plist
= racoon_malloc (sizeof (struct payload_list
));
3106 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3107 plist
->next
->prev
= plist
;
3108 plist
= plist
->next
;
3112 plist
->payload
= payload
;
3113 plist
->payload_type
= payload_type
;
3119 isakmp_plist_set_all (struct payload_list
**plist
, phase1_handle_t
*iph1
)
3121 struct payload_list
*ptr
= *plist
, *first
;
3122 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3123 vchar_t
*buf
= NULL
;
3126 /* Seek to the first item. */
3127 while (ptr
->prev
) ptr
= ptr
->prev
;
3130 /* Compute the whole length. */
3132 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3136 buf
= vmalloc(tlen
);
3139 "failed to get buffer to send.\n");
3145 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3151 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3154 racoon_free (first
);
3155 /* ptr->prev = NULL; first = NULL; ... omitted. */
3170 frag_handler(iph1
, msg
, remote
, local
)
3171 phase1_handle_t
*iph1
;
3173 struct sockaddr_storage
*remote
;
3174 struct sockaddr_storage
*local
;
3178 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3179 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3181 "Packet reassembly failed\n");
3185 /* simply reply if the packet was processed. */
3186 if (ike_session_check_recvdpkt(remote
, local
, newmsg
) > 0) {
3187 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3188 saddr2str((struct sockaddr
*)remote
));
3190 plog(ASL_LEVEL_NOTICE
,
3191 "the reassembled packet is retransmitted by %s.\n",
3192 saddr2str((struct sockaddr
*)remote
));
3197 isakmp_main(newmsg
, remote
, local
);
3207 phase1_handle_t
*iph1
;
3209 vchar_t
*buf
= NULL
;
3210 struct sadb_msg
*msg
, *next
, *end
;
3212 struct sockaddr_storage
*src
, *dst
;
3213 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3215 phase2_handle_t
*iph2
;
3216 phase1_handle_t
*new_iph1
;
3218 plog(ASL_LEVEL_NOTICE
,
3219 "purging ISAKMP-SA spi=%s.\n",
3220 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3222 /* Mark as expired. */
3223 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
3225 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3228 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3229 * Keep all others phase2 SAs.
3231 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3233 plog(ASL_LEVEL_NOTICE
,
3234 "pfkey_dump_sadb returned nothing.\n");
3238 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3239 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3242 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3244 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3245 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3250 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3252 "pfkey_check (%s)\n", ipsec_strerror());
3257 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3259 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3260 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3264 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3265 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3267 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3268 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3269 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3275 * check in/outbound SAs.
3276 * Select only SAs where src == local and dst == remote (outgoing)
3277 * or src == remote and dst == local (incoming).
3279 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3280 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3285 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3286 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3288 /* Check if there is another valid ISAKMP-SA */
3289 if (new_iph1
!= NULL
) {
3292 /* No handler... still send a pfkey_delete message, but log this !*/
3293 plog(ASL_LEVEL_NOTICE
,
3294 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3295 ntohl(sa
->sadb_sa_spi
));
3299 * If we have a new ph1, do not purge IPsec-SAs binded
3300 * to a different ISAKMP-SA
3302 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3307 /* If the ph2handle is established, do not purge IPsec-SA */
3308 if (FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(iph2
->status
)) {
3310 plog(ASL_LEVEL_NOTICE
,
3311 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3312 ntohl(sa
->sadb_sa_spi
),
3313 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3321 pfkey_send_delete(lcconf
->sock_pfkey
,
3322 msg
->sadb_msg_satype
,
3324 src
, dst
, sa
->sadb_sa_spi
);
3326 /* delete a relative phase 2 handle. */
3329 ike_session_unlink_phase2(iph2
);
3332 plog(ASL_LEVEL_NOTICE
,
3333 "purged IPsec-SA spi=%u.\n",
3334 ntohl(sa
->sadb_sa_spi
));
3342 /* Mark the phase1 handler as EXPIRED */
3343 plog(ASL_LEVEL_NOTICE
,
3344 "purged ISAKMP-SA spi=%s.\n",
3345 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3347 SCHED_KILL(iph1
->sce
);
3349 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3354 phase2_handle_t
*iph2
;
3359 /* Delete the SPD entry if we generated it
3361 if (iph2
->generated_spidx
) {
3363 u_int64_t force_align
; // Wcast-align fix - force alignment
3364 struct policyindex spidx
;
3366 struct sockaddr_storage addr
;
3368 struct sockaddr_storage
*src
= iph2
->src
;
3369 struct sockaddr_storage
*dst
= iph2
->dst
;
3371 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3373 plog(ASL_LEVEL_NOTICE
,
3374 "generated policy, deleting it.\n");
3376 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
3377 iph2
->spidx_gen
= &u
.spidx
;
3379 /* make inbound policy */
3382 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
3383 u
.spidx
.ul_proto
= 0;
3386 * Note: code from get_proposal_r
3389 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
3392 * make destination address in spidx from either ID payload
3393 * or phase 1 address into a address in spidx.
3395 if (iph2
->id
!= NULL
3396 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3397 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3398 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3399 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3400 /* get a destination address of a policy */
3401 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
3402 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
, iph2
->version
);
3408 * get scopeid from the SA address.
3409 * note that the phase 1 source address is used as
3410 * a destination address to search for a inbound
3411 * policy entry because rcoon is responder.
3413 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3415 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
3420 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3421 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3422 idi2type
= _XIDT(iph2
->id
);
3426 plog(ASL_LEVEL_NOTICE
,
3427 "Get a destination address of SP index "
3428 "from Phase 1 address "
3429 "due to no ID payloads found "
3430 "OR because ID type is not address.\n");
3433 * copy the SOURCE address of IKE into the
3434 * DESTINATION address of the key to search the
3435 * SPD because the direction of policy is inbound.
3437 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
3438 switch (u
.spidx
.dst
.ss_family
) {
3441 sizeof(struct in_addr
) << 3;
3446 sizeof(struct in6_addr
) << 3;
3455 /* make source address in spidx */
3456 if (iph2
->id_p
!= NULL
3457 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3458 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3459 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3460 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3461 /* get a source address of inbound SA */
3462 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
3463 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
, iph2
->version
);
3469 * get scopeid from the SA address.
3470 * for more detail, see above of this function.
3472 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3474 setscopeid(&u
.spidx
.src
, iph2
->dst
);
3480 /* make id[src,dst] if both ID types are IP address and same */
3481 if (_XIDT(iph2
->id_p
) == idi2type
3482 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
3484 dupsaddr(&u
.spidx
.dst
);
3486 dupsaddr(&u
.spidx
.src
);
3490 plog(ASL_LEVEL_NOTICE
,
3491 "Get a source address of SP index "
3492 "from Phase 1 address "
3493 "due to no ID payloads found "
3494 "OR because ID type is not address.\n");
3496 /* see above comment. */
3497 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
3498 switch (u
.spidx
.src
.ss_family
) {
3501 sizeof(struct in_addr
) << 3;
3506 sizeof(struct in6_addr
) << 3;
3517 plog(ASL_LEVEL_NOTICE
,
3518 "get src/dst address from ID payload "
3519 "prefixlen=%u ul_proto=%u\n",
3520 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3521 plog(ASL_LEVEL_DEBUG
,
3522 "get a src address from ID payload "
3523 "%s prefixlen=%u ul_proto=%u\n",
3524 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
3525 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3526 plog(ASL_LEVEL_DEBUG
,
3527 "get dst address from ID payload "
3528 "%s prefixlen=%u ul_proto=%u\n",
3529 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
3530 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
3533 * convert the ul_proto if it is 0
3534 * because 0 in ID payload means a wild card.
3536 if (u
.spidx
.ul_proto
== 0)
3537 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3541 /* End of code from get_proposal_r
3544 if (pk_sendspddelete(iph2
) < 0) {
3546 "pfkey spddelete(inbound) failed.\n");
3548 plog(ASL_LEVEL_NOTICE
,
3549 "pfkey spddelete(inbound) sent.\n");
3552 #ifdef HAVE_POLICY_FWD
3553 /* make forward policy if required */
3554 if (tunnel_mode_prop(iph2
->approval
)) {
3555 u
.spidx
.dir
= IPSEC_DIR_FWD
;
3556 if (pk_sendspddelete(iph2
) < 0) {
3558 "pfkey spddelete(forward) failed.\n");
3560 plog(ASL_LEVEL_NOTICE
,
3561 "pfkey spddelete(forward) sent.\n");
3566 /* make outbound policy */
3569 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3571 u
.spidx
.src
= u
.spidx
.dst
;
3573 pref
= u
.spidx
.prefs
;
3574 u
.spidx
.prefs
= u
.spidx
.prefd
;
3575 u
.spidx
.prefd
= pref
;
3577 if (pk_sendspddelete(iph2
) < 0) {
3579 "pfkey spddelete(outbound) failed.\n");
3581 plog(ASL_LEVEL_NOTICE
,
3582 "pfkey spddelete(outbound) sent.\n");
3585 iph2
->spidx_gen
=NULL
;
3591 setscopeid(sp_addr0
, sa_addr0
)
3592 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
3594 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3596 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3597 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3599 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3600 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3601 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3604 /* this check should not be here ? */
3605 if (sa_addr
->sin6_family
!= AF_INET6
) {
3607 "can't get scope ID: family mismatch\n");
3611 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3613 "scope ID is not supported except of lladdr.\n");
3617 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
3624 isakmp_plist_append_initial_contact (iph1
, plist
)
3625 phase1_handle_t
*iph1
;
3626 struct payload_list
*plist
;
3628 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
3629 vchar_t
*notp_ini
= NULL
;
3630 struct isakmp_pl_n np
, *nptr
;
3633 np
.doi
= htonl(iph1
->rmconf
->doitype
);
3634 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
3635 np
.spi_size
= sizeof(isakmp_index
);
3636 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
3637 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
3638 + sizeof(isakmp_index
)))) {
3640 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
3641 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
3642 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
3643 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
3644 plog(ASL_LEVEL_NOTICE
,
3645 "added initial-contact payload.\n");
3647 /* insert a node into contacted list. */
3648 if (ike_session_inscontacted(iph1
->remote
) == -1) {
3650 "failed to add contacted list.\n");
3656 "failed to allocate notification payload.\n");
3660 plog(ASL_LEVEL_NOTICE
, "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
3661 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, ike_session_getcontacted(iph1
->remote
)? 1:0);