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>
87 #include "remoteconf.h"
88 #include "localconf.h"
89 #include "grabmyaddr.h"
90 #include "isakmp_var.h"
95 #include "ipsec_doi.h"
97 #include "crypto_openssl.h"
99 #include "isakmp_ident.h"
100 #include "isakmp_agg.h"
101 #include "isakmp_quick.h"
102 #include "isakmp_inf.h"
103 #include "vpn_control.h"
104 #include "vpn_control_var.h"
106 #include "vendorid.h"
107 #include "isakmp_xauth.h"
108 #include "isakmp_unity.h"
109 #include "isakmp_cfg.h"
112 #include "isakmp_frag.h"
114 #include "strnames.h"
119 # include "nattraversal.h"
121 #include "ike_session.h"
122 # include <netinet/in.h>
123 # include <netinet/udp.h>
124 # include <netinet/in_systm.h>
125 # include <netinet/ip.h>
126 # define SOL_UDP IPPROTO_UDP
127 #include "ipsecSessionTracer.h"
128 #include "ipsecMessageTracer.h"
129 #include "power_mgmt.h"
131 extern caddr_t
val2str (const char *, size_t);
132 u_char i_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the i_ck. */
133 u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
136 static void isakmp_main (vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
137 static void ikev1_received_packet(vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
138 static int ikev1_ph1begin_r (ike_session_t
*session
, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*, u_int8_t
);
139 static int ikev1_ph2begin_i (phase1_handle_t
*, phase2_handle_t
*);
140 static int ikev1_ph2begin_r (phase1_handle_t
*, vchar_t
*);
144 static void frag_handler (phase1_handle_t
*, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
148 * isakmp packet handler
151 isakmp_handler(int so_isakmp
)
153 struct isakmp isakmp
;
155 u_int64_t force_align
; // Wcast-align fix - force alignment
156 char buf
[sizeof (isakmp
) + 4];
157 u_int32_t non_esp
[2];
158 char lbuf
[sizeof(struct udphdr
) +
162 struct sockaddr_storage remote
;
163 struct sockaddr_storage local
;
164 unsigned int remote_len
= sizeof(remote
);
165 unsigned int local_len
= sizeof(local
);
169 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
172 if (slept_at
|| woke_at
) {
173 plog(ASL_LEVEL_DEBUG
,
174 "ignoring isakmp port until power-mgmt event is handled.\n");
178 /* read message by MSG_PEEK */
179 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
180 MSG_PEEK
, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
184 "failed to receive isakmp packet: %s\n",
189 /* keep-alive packet - ignore */
190 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
191 /* Pull the keep-alive packet */
192 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
193 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
195 "failed to receive keep alive packet: %s\n",
202 /* we don't know about portchange yet,
203 look for non-esp marker instead */
204 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
205 extralen
= NON_ESP_MARKER_LEN
;
207 /* now we know if there is an extra non-esp
208 marker at the beginning or not */
209 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
211 /* check isakmp header length, as well as sanity of header length */
212 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
214 "packet shorter than isakmp header size (size: %zu, minimum expected: %zu)\n", len
, sizeof(isakmp
));
216 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
217 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
219 "failed to receive isakmp packet: %s\n",
225 /* reject it if the size is tooooo big. */
226 if (ntohl(isakmp
.len
) > 0xffff) {
228 "the length in the isakmp header is too big.\n");
229 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
230 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
232 "failed to receive isakmp packet: %s\n",
238 /* read real message */
239 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
241 "failed to allocate reading buffer (%u Bytes)\n",
242 ntohl(isakmp
.len
) + extralen
);
244 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
245 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
247 "failed to receive isakmp packet: %s\n",
249 error
= -2; /* serious problem with socket */
254 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
255 0, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
259 "failed to receive isakmp packet: %s\n",
264 if (len
< extralen
) {
266 "invalid len (%zd Bytes) & extralen (%d Bytes)\n",
271 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
273 "failed to allocate reading buffer (%lu Bytes)\n",
278 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
283 plog(ASL_LEVEL_ERR
, "received invalid length (%zd != %zu), why ?\n",
288 plog(ASL_LEVEL_DEBUG
, "%zd bytes message received %s\n",
289 len
, saddr2str_fromto("from %s to %s",
290 (struct sockaddr
*)&remote
,
291 (struct sockaddr
*)&local
));
293 /* avoid packets with malicious port/address */
294 switch (remote
.ss_family
) {
296 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
300 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
305 "invalid family: %d\n", remote
.ss_family
);
310 "src port == 0 (valid as UDP but not with IKE)\n");
314 /* XXX: check sender whether to be allowed or not to accept */
316 /* XXX: I don't know how to check isakmp half connection attack. */
318 /* simply reply if the packet was processed. */
320 if (ike_session_check_recvdpkt(&remote
, &local
, buf
)) {
321 IPSECLOGASLMSG("Received retransmitted packet from %s.\n",
322 saddr2str((struct sockaddr
*)&remote
));
324 plog(ASL_LEVEL_NOTICE
,
325 "the packet is retransmitted by %s.\n",
326 saddr2str((struct sockaddr
*)&remote
));
331 /* isakmp main routine */
332 isakmp_main(buf
, &remote
, &local
);
344 * main processing to handle isakmp payload
347 isakmp_main(vchar_t
*msg
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
)
349 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
350 u_int8_t isakmp_version
= isakmp
->v
;
352 #ifdef HAVE_PRINT_ISAKMP_C
353 isakmp_printpacket(msg
, remote
, local
, 0);
356 /* the initiator's cookie must not be zero */
357 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
359 "malformed cookie received.\n");
363 /* Check the Major and Minor Version fields. */
365 * XXX Is is right to check version here ?
366 * I think it may no be here because the version depends
367 * on exchange status.
369 if (ISAKMP_GETMAJORV(isakmp_version
) != ISAKMP_MAJOR_VERSION_IKEV1
) {
370 plog(ASL_LEVEL_ERR
, "invalid major version %d.\n", isakmp_version
);
375 #if ISAKMP_MINOR_VERSION > 0 //%%%%%%%% fix this
376 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) { //%%%%%%%%%%%%%%% ??????
378 "invalid minor version %d.\n",
379 ISAKMP_GETMINORV(isakmp
->v
));
385 if (isakmp_version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
386 /* check the Flags field. */
387 /* XXX How is the exclusive check, E and A ? */
388 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
389 plog(ASL_LEVEL_ERR
, "invalid flag 0x%02x.\n", isakmp
->flags
);
393 /* ignore commit bit. */
394 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
395 if (isakmp
->msgid
== 0) {
396 isakmp_info_send_nx(isakmp
, remote
, local
,
397 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
398 plog(ASL_LEVEL_ERR
, "Commit bit on Phase 1 forbidden.\n");
403 ikev1_received_packet(msg
, local
, remote
);
409 * ikev1_received_packet
410 * Handler for received IKEv1 Packets
413 ikev1_received_packet(vchar_t
*msg
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
415 ike_session_t
*session
;
416 phase1_handle_t
*iph1
;
418 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
419 isakmp_index
*index
= (isakmp_index
*)isakmp
;
421 session
= ike_session_get_session(local
, remote
, 0, index
);
423 session
= ike_session_get_session(local
, remote
, 1, NULL
);
426 plog (ASL_LEVEL_INFO
, "failed to allocate or find ike session.\n");
430 iph1
= ike_session_getph1byindex(session
, index
);
433 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
434 iph1
->side
== INITIATOR
) {
435 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
436 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
437 CONSTSTR("Malformed or unexpected cookie"),
438 CONSTSTR("Failed to process packet (malformed/unexpected cookie)"));
439 plog(ASL_LEVEL_DEBUG
,
440 "Malformed cookie received or "
441 "the initiator's cookies collide.\n");
446 /* Floating ports for NAT-T */
447 if (NATT_AVAILABLE(iph1
) &&
448 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
449 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
450 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
452 /* prevent memory leak */
453 racoon_free(iph1
->remote
);
454 racoon_free(iph1
->local
);
458 /* copy-in new addresses */
459 iph1
->remote
= dupsaddr(remote
);
460 if (iph1
->remote
== NULL
) {
461 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
462 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
463 CONSTSTR("Failed to duplicate remote address"),
464 CONSTSTR("Failed to process Phase 1 message (can't duplicate remote address"));
466 "Phase 1 failed: dupsaddr failed.\n");
469 iph1
->local
= dupsaddr(local
);
470 if (iph1
->local
== NULL
) {
471 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
472 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
473 CONSTSTR("Failed to duplicate local address"),
474 CONSTSTR("Failed to process Phase 1 message (can't duplicate local address"));
476 "Phase 1 failed: dupsaddr failed.\n");
480 /* set the flag to prevent further port floating
481 (FIXME: should we allow it? E.g. when the NAT gw
483 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
485 /* print some neat info */
486 plog (ASL_LEVEL_INFO
,
487 "NAT-T: ports changed to: %s\n",
488 saddr2str_fromto("%s<->%s", (struct sockaddr
*)iph1
->remote
, (struct sockaddr
*)iph1
->local
));
491 /* must be same addresses in one stream of a phase at least. */
492 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
493 char *saddr_db
, *saddr_act
;
495 saddr_db
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
496 saddr_act
= racoon_strdup(saddr2str((struct sockaddr
*)remote
));
497 STRDUP_FATAL(saddr_db
);
498 STRDUP_FATAL(saddr_act
);
500 plog(ASL_LEVEL_WARNING
,
501 "Remote address mismatched. db=%s, act=%s\n",
502 saddr_db
, saddr_act
);
504 racoon_free(saddr_db
);
505 racoon_free(saddr_act
);
509 * don't check of exchange type here because other type will have
510 * the same index, for example, informational exchange.
513 // received ike packets: update dpd checks
514 isakmp_reschedule_info_monitor_if_pending(iph1
, "IKE packet received from peer");
519 // Check exchange type and process accordingly
521 switch (isakmp
->etype
) {
523 case ISAKMP_ETYPE_IDENT
:
524 case ISAKMP_ETYPE_AGG
:
526 /* phase 1 validity check */
527 if (isakmp
->msgid
!= 0) {
528 plog(ASL_LEVEL_ERR
, "Message id should be zero in Phase 1.\n");
532 /* search for isakmp status record of phase 1 */
535 * the packet must be the 1st message from a initiator
536 * or the 2nd message from the responder.
539 /* search for phase1 handle by index without r_ck */
540 iph1
= ike_session_getph1byindex0(session
, index
);
542 /*it must be the 1st message from a initiator.*/
543 if (memcmp(&isakmp
->r_ck
, r_ck0
,
544 sizeof(cookie_t
)) != 0) {
546 plog(ASL_LEVEL_DEBUG
, "Malformed cookie received "
547 "or the spi expired.\n");
551 /* Initiation of new exchange */
552 ikev1_ph1begin_r(session
, msg
, remote
, local
, isakmp
->etype
);
558 * Don't delete phase 1 handler for mismatch
559 * because of no authentication has been completed.
561 if (iph1
->etype
!= isakmp
->etype
) {
562 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
563 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
564 CONSTSTR("Mismatched exchange type"),
565 CONSTSTR("Failed to process Phase 1 message (mismatched exchange type)"));
567 "Exchange type is mismatched: "
568 "db=%s packet=%s, ignore it.\n",
569 s_isakmp_etype(iph1
->etype
),
570 s_isakmp_etype(isakmp
->etype
));
574 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
575 frag_handler(iph1
, msg
, remote
, local
);
578 fsm_ikev1_phase1_process_payloads(iph1
, msg
);
582 case ISAKMP_ETYPE_INFO
:
583 case ISAKMP_ETYPE_ACKINFO
:
586 * iph1 must be present for Information message.
587 * if iph1 is null then trying to get the phase1 status
588 * as the packet from responder againt initiator's 1st
589 * exchange in phase 1.
590 * NOTE: We think such informational exchange should be ignored.
593 iph1
= ike_session_getph1byindex0(session
, index
);
595 plog(ASL_LEVEL_ERR
, "Unknown Informational exchange received.\n");
598 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
599 plog(ASL_LEVEL_WARNING
,
600 "Remote address mismatched. "
602 saddr2str((struct sockaddr
*)iph1
->remote
));
605 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
606 return frag_handler(iph1
, msg
, remote
, local
);
608 if (isakmp_info_recv(iph1
, msg
) < 0)
613 case ISAKMP_ETYPE_QUICK
:
615 u_int32_t msgid
= isakmp
->msgid
;
616 phase2_handle_t
*iph2
;
619 isakmp_info_send_nx(isakmp
, remote
, local
,
620 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
621 plog(ASL_LEVEL_ERR
, "Can't start the quick mode, "
622 "there is no ISAKMP-SA, %s\n", isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
627 /* Reinit the IVM if it's still there */
628 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
629 oakley_delivm(iph1
->mode_cfg
->ivm
);
630 iph1
->mode_cfg
->ivm
= NULL
;
633 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
634 frag_handler(iph1
, msg
, remote
, local
);
638 /* check status of phase 1 whether negotiated or not. */
639 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
640 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
641 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
642 CONSTSTR("Can't start Phase 2 without valid Phase 1"),
643 CONSTSTR("Failed to start Phase 2 responder (no established Phase 1"));
644 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
645 "there is no valid ISAKMP-SA, %s\n", isakmp_pindex(&iph1
->index
, iph1
->msgid
));
649 /* search isakmp phase 2 stauts record. */
650 iph2
= ike_session_getph2bymsgid(iph1
, msgid
);
652 /* it must be new negotiation as responder */
653 ikev1_ph2begin_r(iph1
, msg
);
659 * we keep to set commit bit during negotiation.
660 * When SA is configured, bit will be reset.
662 * don't initiate commit bit. should be fixed in the future.
664 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
665 iph2
->flags
|= ISAKMP_FLAG_C
;
667 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
) &&
668 (iph2
->ph1
== NULL
|| iph2
->ph1
->approval
== NULL
)) {
669 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
670 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
671 CONSTSTR("Can't continue Phase 2 without valid Phase 1"),
672 CONSTSTR("Failed to continue Phase 2 resonder (invalid linked Phase 1"));
673 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
674 "invalid linked ISAKMP-SA\n");
677 fsm_ikev1_phase2_process_payloads(iph2
, msg
);
681 case ISAKMP_ETYPE_CFG
:
685 "mode config %d from %s, "
686 "but we have no ISAKMP-SA.\n",
687 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
690 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
692 "mode config %d from %s, "
693 "but ISAKMP-SA %s isn't established.\n",
694 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
),
695 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
698 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
699 return frag_handler(iph1
, msg
, remote
, local
);
700 isakmp_cfg_r(iph1
, msg
);
704 case ISAKMP_ETYPE_NEWGRP
:
705 case ISAKMP_ETYPE_AUTH
:
706 case ISAKMP_ETYPE_NONE
:
709 "Invalid exchange type %d from %s.\n",
710 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
715 /* new negotiation of phase 1 for initiator */
717 ikev1_ph1begin_i(ike_session_t
*session
, struct remoteconf
*rmconf
, struct sockaddr_storage
*remote
,
718 struct sockaddr_storage
*local
, int started_by_api
, nw_nat64_prefix_t
*nat64_prefix
)
721 phase1_handle_t
*iph1
;
723 struct timeval start
, end
;
726 if (session
== NULL
) {
727 session
= ike_session_get_session(local
, remote
, 1, NULL
);
729 plog (ASL_LEVEL_INFO
, "failed to allocate or find ike session.\n");
734 /* get new entry to isakmp status table. */
735 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
739 iph1
->rmconf
= rmconf
;
740 retain_rmconf(iph1
->rmconf
);
741 iph1
->side
= INITIATOR
;
742 iph1
->started_by_api
= started_by_api
;
743 if (nat64_prefix
!= NULL
) {
744 memcpy(&iph1
->nat64_prefix
, nat64_prefix
, sizeof(*nat64_prefix
));
746 iph1
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
752 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
753 /* don't call remph1(iph1) until after insph1(iph1) is called */
754 ike_session_delph1(iph1
);
759 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
763 iph1
->frag_chain
= NULL
;
764 iph1
->approval
= NULL
;
766 /* XXX copy remote address */
767 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
768 /* don't call remph1(iph1) until after insph1(iph1) is called */
769 iph1
= NULL
; /* deleted in copy_ph1addresses */
773 if (ike_session_link_phase1(session
, iph1
) != 0) {
774 plog(ASL_LEVEL_DEBUG
, "Failed to link ph1 to session\n");
775 ike_session_delph1(iph1
);
778 // HACK!!! to track rekeys across SIGHUPs
779 if (started_by_api
== VPN_RESTARTED_BY_API
&&
781 iph1
->parent_session
->established
= 1;
782 iph1
->parent_session
->ikev1_state
.ph2cnt
++;
786 /* start phase 1 exchange */
787 iph1
->etype
= rmconf
->etypes
->type
;
788 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
789 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_START
);
790 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
791 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_I_START
);
795 plog(ASL_LEVEL_DEBUG
, "===\n");
799 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
803 "initiate new phase 1 negotiation: %s<=>%s\n",
804 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
809 s_isakmp_etype(iph1
->etype
));
812 gettimeofday(&iph1
->start
, NULL
);
813 gettimeofday(&start
, NULL
);
816 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by me).\n");
818 if (fsm_ikev1_phase1_send_response(iph1
, NULL
)) {
819 ike_session_unlink_phase1(iph1
);
824 gettimeofday(&end
, NULL
);
825 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
827 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
828 timedelta(&start
, &end
));
831 #ifdef ENABLE_VPNCONTROL_PORT
832 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
838 /* new negotiation of phase 1 for responder */
840 ikev1_ph1begin_r(ike_session_t
*session
, vchar_t
*msg
, struct sockaddr_storage
*remote
,
841 struct sockaddr_storage
*local
, u_int8_t etype
)
844 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
845 struct remoteconf
*rmconf
;
846 phase1_handle_t
*iph1
;
847 struct etypes
*etypeok
;
849 struct timeval start
, end
;
852 /* look for my configuration */
853 rmconf
= getrmconf(remote
);
854 if (rmconf
== NULL
) {
861 /* check to be acceptable exchange type */
862 etypeok
= check_etypeok(rmconf
, etype
);
863 if (etypeok
== NULL
) {
865 "not acceptable %s mode\n", s_isakmp_etype(etype
));
869 /* get new entry to isakmp status table. */
870 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
874 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
875 iph1
->rmconf
= rmconf
;
876 retain_rmconf(iph1
->rmconf
);
878 iph1
->side
= RESPONDER
;
879 iph1
->started_by_api
= 0;
880 iph1
->etype
= etypeok
->type
;
881 iph1
->version
= isakmp
->v
;
884 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
885 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_START
);
886 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
887 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_R_START
);
893 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
894 /* don't call remph1(iph1) until after insph1(iph1) is called */
895 ike_session_delph1(iph1
);
901 iph1
->frag_chain
= NULL
;
902 iph1
->approval
= NULL
;
904 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
905 * We have to setup this flag now to correctly generate the first reply.
906 * Don't know if a better check could be done for that ?
908 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
909 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
911 /* copy remote address */
912 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
913 /* don't call remph1(iph1) until after insph1(iph1) is called */
914 iph1
= NULL
; /* deleted in copy_ph1addresses */
918 if (ike_session_link_phase1(session
, iph1
) != 0) {
919 ike_session_delph1(iph1
);
923 plog(ASL_LEVEL_DEBUG
, "===\n");
927 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
931 "respond new phase 1 negotiation: %s<=>%s\n",
932 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
936 "begin %s mode.\n", s_isakmp_etype(etype
));
939 gettimeofday(&iph1
->start
, NULL
);
940 gettimeofday(&start
, NULL
);
943 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by peer).\n");
945 /* now that we have a phase1 handle, feed back into our
946 * main receive function to catch fragmented packets
948 isakmp_main(msg
, remote
, local
);
952 /* new negotiation of phase 2 for initiator */
954 ikev1_ph2begin_i(phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
958 if (xauth_check(iph1
) != 0) {
960 "Attempt to start phase 2 whereas Xauth failed\n");
965 /* found ISAKMP-SA. */
966 plog(ASL_LEVEL_DEBUG
, "===\n");
967 plog(ASL_LEVEL_DEBUG
, "begin QUICK mode.\n");
970 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
974 "initiate new phase 2 negotiation: %s<=>%s\n",
975 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
980 gettimeofday(&iph2
->start
, NULL
);
984 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
985 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
987 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by me).\n");
988 if (quick_iprep(iph2
, NULL
))
991 #ifdef ENABLE_VPNCONTROL_PORT
992 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
998 /* new negotiation of phase 2 for responder */
1000 ikev1_ph2begin_r(phase1_handle_t
*iph1
, vchar_t
*msg
)
1002 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1003 phase2_handle_t
*iph2
= 0;
1006 struct timeval start
, end
;
1008 #ifdef ENABLE_HYBRID
1009 if (xauth_check(iph1
) != 0) {
1011 "Attempt to start Phase 2 whereas Xauth failed\n");
1016 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_SA
);
1019 "failed to allocate Phase 2 entry.\n");
1023 iph2
->side
= RESPONDER
;
1024 iph2
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
1025 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_START
);
1026 iph2
->flags
= isakmp
->flags
;
1027 iph2
->msgid
= isakmp
->msgid
;
1028 iph2
->seq
= pk_getseq();
1029 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1030 if (iph2
->ivm
== NULL
) {
1031 ike_session_delph2(iph2
);
1034 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1035 if (iph2
->dst
== NULL
) {
1036 ike_session_delph2(iph2
);
1039 switch (iph2
->dst
->ss_family
) {
1042 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1048 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1054 "invalid family: %d\n", iph2
->dst
->ss_family
);
1055 ike_session_delph2(iph2
);
1059 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1060 if (iph2
->src
== NULL
) {
1061 ike_session_delph2(iph2
);
1064 switch (iph2
->src
->ss_family
) {
1067 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1073 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1079 "invalid family: %d\n", iph2
->src
->ss_family
);
1080 ike_session_delph2(iph2
);
1084 if (ike_session_link_ph2_to_ph1(iph1
, iph2
))
1087 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
1089 plog(ASL_LEVEL_DEBUG
, "===\n");
1093 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1096 plog(ASL_LEVEL_INFO
,
1097 "respond new phase 2 negotiation: %s<=>%s\n",
1098 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1103 gettimeofday(&start
, NULL
);
1106 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by peer).\n");
1108 error
= fsm_ikev1_phase2_process_payloads(iph2
, msg
);
1112 #ifdef ENABLE_VPNCONTROL_PORT
1113 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1120 ikev1_phase1_established(phase1_handle_t
*iph1
)
1123 u_int rekey_lifetime
;
1124 int ini_contact
= iph1
->rmconf
->ini_contact
;
1127 gettimeofday(&iph1
->end
, NULL
);
1128 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1129 "Phase 1", s_isakmp_etype(iph1
->etype
),
1130 timedelta(&iph1
->start
, &iph1
->end
));
1133 #ifdef ENABLE_VPNCONTROL_PORT
1135 if (iph1
->side
== RESPONDER
&&
1136 iph1
->local
->ss_family
== AF_INET
) {
1138 struct redirect
*addr
;
1140 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
1141 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
1142 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
1144 if (raddr
== NULL
) {
1146 "failed to send redirect message - memory error.\n");
1148 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
1149 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
1150 plog(ASL_LEVEL_DEBUG
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
1153 (void)ike_session_update_ph1_ph2tree(iph1
);
1154 isakmp_ph1expire(iph1
);
1162 /* save created date. */
1163 (void)time(&iph1
->created
);
1165 /* add to the schedule to expire, and save back pointer. */
1166 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
1167 isakmp_ph1expire_stub
, iph1
);
1169 if (iph1
->rmconf
->initiate_ph1rekey
) {
1170 if (iph1
->side
== INITIATOR
) {
1171 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
1175 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
1179 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
1180 iph1
->approval
->lifetime
);
1181 if (rekey_lifetime
) {
1182 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
1183 isakmp_ph1rekeyexpire_stub
,
1186 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
1190 "failed to get rekey timer - lifetime is too small... probably.\n");
1194 #ifdef ENABLE_HYBRID
1195 /* ignore xauth if it is a rekey */
1196 if (!iph1
->is_rekey
&&
1197 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1198 switch(AUTHMETHOD(iph1
)) {
1199 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1200 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1201 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1202 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1203 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1204 xauth_sendreq(iph1
);
1205 /* XXX Don't process INITIAL_CONTACT */
1214 /* Schedule the r_u_there.... */
1215 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1216 isakmp_sched_r_u(iph1
, 0);
1219 /* INITIAL-CONTACT processing */
1220 /* ignore initial-contact if it is a rekey */
1221 /* don't send anything if local test mode. */
1222 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
1223 /* send INITIAL-CONTACT */
1224 isakmp_info_send_n1(iph1
,
1225 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1226 /* insert a node into contacted list. */
1227 if (ike_session_inscontacted(iph1
->remote
) == -1) {
1229 "failed to add contacted list.\n");
1234 log_ph1established(iph1
);
1235 plog(ASL_LEVEL_DEBUG
, "===\n");
1237 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1239 #ifdef ENABLE_VPNCONTROL_PORT
1240 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1241 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1248 * parse ISAKMP payloads, without ISAKMP base header.
1251 isakmp_parsewoh(np0
, gen
, len
)
1253 struct isakmp_gen
*gen
;
1256 u_char np
= np0
& 0xff;
1259 struct isakmp_parse_t
*p
, *ep
;
1261 plog(ASL_LEVEL_DEBUG
, "begin.\n");
1264 * 5 is a magic number, but any value larger than 2 should be fine
1265 * as we do vrealloc() in the following loop.
1267 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1268 if (result
== NULL
) {
1270 "failed to get buffer.\n");
1273 // Wcast-align fix (void*) - result = aligned buffer of struct isakmp_parse_t
1274 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1275 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1279 /* parse through general headers */
1280 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1281 if (tlen
<= sizeof(struct isakmp_gen
)) {
1282 /* don't send information, see isakmp_ident_r1() */
1284 "invalid length of payload\n");
1289 plog(ASL_LEVEL_DEBUG
,
1290 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1293 p
->len
= ntohs(gen
->len
);
1294 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1295 plog(ASL_LEVEL_DEBUG
,
1296 "invalid length of payload\n");
1305 off
= p
- ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1306 result
= vrealloc(result
, result
->l
* 2);
1307 if (result
== NULL
) {
1308 plog(ASL_LEVEL_DEBUG
,
1309 "failed to realloc buffer.\n");
1313 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)
1314 (result
->v
+ result
->l
- sizeof(*ep
));
1315 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1320 plen
= ntohs(gen
->len
);
1321 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1324 p
->type
= ISAKMP_NPTYPE_NONE
;
1328 plog(ASL_LEVEL_DEBUG
, "succeed.\n");
1334 * parse ISAKMP payloads, including ISAKMP base header.
1340 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1341 struct isakmp_gen
*gen
;
1347 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1348 tlen
= buf
->l
- sizeof(struct isakmp
);
1349 result
= isakmp_parsewoh(np
, gen
, tlen
);
1358 ike_session_initctdtree();
1359 ike_session_init_recvdpkt();
1361 if (isakmp_open() < 0)
1374 ike_session_clear_recvdpkt();
1375 ike_session_clear_contacted();
1379 * make strings containing i_cookie + r_cookie + msgid
1382 isakmp_pindex(index
, msgid
)
1383 const isakmp_index
*index
;
1384 const u_int32_t msgid
;
1386 static char buf
[64];
1390 memset(buf
, 0, sizeof(buf
));
1393 p
= (const u_char
*)index
;
1394 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1395 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1407 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1413 /* open ISAKMP sockets. */
1418 int ifnum
= 0, encap_ifnum
= 0;
1423 int tentative_failures
= 0;
1426 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1429 if (p
->sock
!= -1) {
1433 continue; // socket already open
1436 /* warn if wildcard address - should we forbid this? */
1437 switch (p
->addr
->ss_family
) {
1439 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1440 plog(ASL_LEVEL_WARNING
,
1441 "listening to wildcard address,"
1442 "broadcast IKE packet may kill you\n");
1446 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1447 plog(ASL_LEVEL_WARNING
,
1448 "listening to wildcard address, "
1449 "broadcast IKE packet may kill you\n");
1454 "unsupported address family %d\n",
1455 lcconf
->default_af
);
1460 if (p
->addr
->ss_family
== AF_INET6
&&
1461 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1462 p
->addr
)->sin6_addr
))
1464 plog(ASL_LEVEL_DEBUG
,
1465 "Ignoring multicast address %s\n",
1466 saddr2str((struct sockaddr
*)p
->addr
));
1467 racoon_free(p
->addr
);
1473 if ((p
->sock
= socket(p
->addr
->ss_family
, SOCK_DGRAM
, 0)) < 0) {
1475 "socket (%s)\n", strerror(errno
));
1479 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1480 plog(ASL_LEVEL_ERR
, "failed to put socket in non-blocking mode\n");
1482 /* receive my interface address on inbound packets. */
1483 switch (p
->addr
->ss_family
) {
1485 int ifindex
= if_nametoindex(p
->ifname
);
1487 setsockopt(p
->sock
, IPPROTO_IP
,
1488 IP_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1490 "setsockopt IP_BOUND_IF (%s)\n",
1494 if (setsockopt(p
->sock
, IPPROTO_IP
,
1496 (const void *)&yes
, sizeof(yes
)) < 0) {
1498 "setsockopt IP_RECVDSTADDR (%s)\n",
1507 pktinfo
= IPV6_RECVPKTINFO
;
1509 pktinfo
= IPV6_RECVDSTADDR
;
1511 int ifindex
= if_nametoindex(p
->ifname
);
1513 setsockopt(p
->sock
, IPPROTO_IPV6
,
1514 IPV6_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1516 "setsockopt IPV6_BOUND_IF (%s)\n",
1520 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1521 (const void *)&yes
, sizeof(yes
)) < 0)
1524 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1525 pktinfo
, strerror(errno
));
1533 #ifdef IPV6_USE_MIN_MTU
1534 if (p
->addr
->ss_family
== AF_INET6
&&
1535 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1536 (void *)&yes
, sizeof(yes
)) < 0) {
1538 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1544 if (setsockopt_bypass(p
->sock
, p
->addr
->ss_family
) < 0)
1547 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEADDR
, (const void *)&yes
, sizeof(yes
)) < 0) {
1548 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEADDR (%s)\n", strerror(errno
));
1552 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEPORT
, (const void *)&yes
, sizeof(yes
)) < 0) {
1553 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEPORT (%s)\n", strerror(errno
));
1557 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1558 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1559 (void *)&yes
, sizeof(yes
)) < 0) {
1561 "setsockopt (%s)\n", strerror(errno
));
1566 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1567 int tmp_errno
= errno
;
1569 "failed to bind to address %s (%s).\n",
1570 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1572 // if bind failed b/c of a tentative v6 address, try again later
1573 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1574 struct in6_ifreq ifr6
;
1576 bzero(&ifr6
, sizeof(ifr6
));
1577 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1578 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1579 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1581 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1582 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1584 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1585 // address may have been tentantive... invalidate sock but leave address around for another try later
1587 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1588 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1591 tentative_failures
= 1;
1595 "failed to bind to address %s: because of interface address error, flags %x.\n",
1596 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1600 "failed to bind to address %s: can't read interface address flags.\n",
1601 saddr2str((struct sockaddr
*)p
->addr
));
1616 if (p
->source
!= NULL
) {
1617 dispatch_source_cancel(p
->source
);
1620 p
->source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, p
->sock
, 0, dispatch_get_main_queue());
1621 if (p
->source
== NULL
) {
1622 plog(ASL_LEVEL_ERR
, "could not create isakmp socket source.");
1625 dispatch_source_set_event_handler(p
->source
,
1629 dispatch_source_t the_source
= p
->source
;
1630 dispatch_source_set_cancel_handler(p
->source
,
1633 dispatch_release(the_source
);
1634 }); dispatch_resume(p
->source
);
1636 plog(ASL_LEVEL_INFO
,
1637 "%s used as isakmp port (fd=%d)\n",
1638 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
1642 racoon_free(p
->addr
);
1645 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
) {
1654 "no address could be bound.\n");
1660 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1661 plog(ASL_LEVEL_WARNING
,
1662 "NAT-T is enabled in at least one remote{} section,\n");
1663 plog(ASL_LEVEL_WARNING
,
1664 "but no 'isakmp_natt' address was specified!\n");
1668 if (tentative_failures
)
1669 sched_new(5, update_myaddrs
, NULL
);
1675 isakmp_suspend_sockets()
1679 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1681 dispatch_suspend(p
->source
);
1688 isakmp_close_sockets();
1693 isakmp_close_sockets()
1697 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1703 dispatch_source_cancel(p
->source
);
1712 // close sockets for addresses that have gone away
1714 isakmp_close_unused()
1716 struct myaddrs
*p
, *next
, **prev
;
1718 prev
= &(lcconf
->myaddrs
);
1719 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1721 if (p
->in_use
== 0) { // not in use ?
1723 dispatch_source_cancel(p
->source
);
1734 isakmp_send(iph1
, sbuf
)
1735 phase1_handle_t
*iph1
;
1740 vchar_t
*vbuf
= NULL
;
1743 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1747 * Do not add the non ESP marker for a packet that will
1748 * be fragmented. The non ESP marker should appear in
1749 * all fragment's packets, but not in the fragmented packet
1751 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1755 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1757 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1758 must added just before the packet itself. For this we must
1759 allocate a new buffer and release it at the end. */
1761 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1763 "vbuf allocation failed\n");
1766 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
1767 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1772 /* select the socket to be sent */
1773 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
1780 plog (ASL_LEVEL_DEBUG
, "%zu bytes %s\n", sbuf
->l
,
1781 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
1784 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1785 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1787 "isakmp_sendfrags failed\n");
1795 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1796 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1798 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1811 /* called from scheduler */
1813 isakmp_ph1resend_stub(p
)
1816 phase1_handle_t
*iph1
;
1818 iph1
=(phase1_handle_t
*)p
;
1819 if(isakmp_ph1resend(iph1
) < 0){
1821 /* Should not happen...
1823 SCHED_KILL(iph1
->scr
);
1826 ike_session_unlink_phase1(iph1
);
1831 isakmp_ph1resend(iph1
)
1832 phase1_handle_t
*iph1
;
1834 time_t retry_interval
;
1836 // make sure there is a buffer to send
1837 // isakmp_plist_set_all() could have returned NULL
1838 if (iph1
->sendbuf
== NULL
)
1841 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1843 if (iph1
->retry_counter
<= 0) {
1844 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1845 IPSECSESSIONEVENTCODE_IKEV1_PH1_MAX_RETRANSMIT
,
1846 CONSTSTR("Phase 1 Maximum Retransmits"),
1847 CONSTSTR("Phase 1 negotiation failed (Maximum retransmits)"));
1850 "Phase 1 negotiation failed due to time up. %s\n",
1851 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1852 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
1853 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
1854 if (isakmp_ph1rekeyretry(iph1
) == 0)
1859 ike_session_ph1_retransmits(iph1
);
1862 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1863 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1864 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1865 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1866 CONSTSTR("Phase 1 Retransmit"),
1867 CONSTSTR("Failed to retrasmit Phase1"));
1870 "Phase 1 negotiation failed due to send error. %s\n",
1871 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1875 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1876 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1877 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1878 CONSTSTR("Phase 1 Retransmit"),
1882 plog(ASL_LEVEL_DEBUG
,
1883 "Resend Phase 1 packet %s\n",
1884 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1886 iph1
->retry_counter
--;
1887 retry_interval
= ike_session_get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
1888 iph1
->rmconf
->retry_interval
);
1889 iph1
->scr
= sched_new(retry_interval
,
1890 isakmp_ph1resend_stub
, iph1
);
1895 /* called from scheduler */
1897 isakmp_ph2resend_stub(p
)
1900 phase2_handle_t
*iph2
;
1902 iph2
=(phase2_handle_t
*)p
;
1904 if(isakmp_ph2resend(iph2
) < 0){
1905 ike_session_unlink_phase2(iph2
);
1910 isakmp_ph2resend(iph2
)
1911 phase2_handle_t
*iph2
;
1913 time_t retry_interval
;
1915 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1917 //%%% BUG FIX - related to commit bit usage - crash happened here
1918 if (iph2
->ph1
== 0) {
1920 "Internal error - attempt to re-send Phase 2 with no Phase 1 bound.\n");
1924 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)){
1925 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1926 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1927 CONSTSTR("Underlying Phase 1 expired"),
1928 CONSTSTR("Failed to retransmit Phase 2 (underlying Phase 1 expired)"));
1930 "Phase 2 negotiation failed due to Phase 1 expired. %s\n",
1931 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1935 if (iph2
->retry_counter
<= 0) {
1936 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1937 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1938 CONSTSTR("Phase 2 maximum retransmits"),
1939 CONSTSTR("Phase 2 negotiation failed (maximum retransmits)"));
1941 "Phase 2 negotiation failed due to time up. %s\n",
1942 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1945 ike_session_ph2_retransmits(iph2
);
1948 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1949 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1950 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1951 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1952 CONSTSTR("Phase 2 Retransmit"),
1953 CONSTSTR("Failed to retransmit Phase2 message"));
1956 "Phase 2 negotiation failed due to send error. %s\n",
1957 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1961 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1962 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1963 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1964 CONSTSTR("Phase 2 Retransmit"),
1968 plog(ASL_LEVEL_DEBUG
,
1969 "Resend Phase 2 packet %s\n",
1970 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1972 iph2
->retry_counter
--;
1973 retry_interval
= ike_session_get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
1974 iph2
->ph1
->rmconf
->retry_interval
);
1975 iph2
->scr
= sched_new(retry_interval
,
1976 isakmp_ph2resend_stub
, iph2
);
1980 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
1981 "Phase 2 packets sent to peer: retransmit timer armed");
1988 /* called from scheduler */
1990 isakmp_ph1expire_stub(p
)
1994 isakmp_ph1expire((phase1_handle_t
*)p
);
1998 isakmp_ph1expire(iph1
)
1999 phase1_handle_t
*iph1
;
2002 phase1_handle_t
*new_iph1
;
2004 SCHED_KILL(iph1
->sce
);
2006 SCHED_KILL(iph1
->dpd_r_u
);
2009 if(!FSM_STATE_IS_EXPIRED(iph1
->status
)){
2010 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2011 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2015 plog(ASL_LEVEL_INFO
,
2016 "ISAKMP-SA expired %s-%s spi:%s\n",
2018 isakmp_pindex(&iph1
->index
, 0));
2021 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
2022 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
2026 * the phase1 deletion is postponed until there is no phase2.
2028 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2029 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2033 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2036 /* called from scheduler */
2038 isakmp_ph1rekeyexpire_stub(p
)
2042 isakmp_ph1rekeyexpire((phase1_handle_t
*)p
, FALSE
);
2046 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
2047 phase1_handle_t
*iph1
;
2048 int ignore_sess_drop_policy
;
2051 struct remoteconf
*rmconf
;
2053 SCHED_KILL(iph1
->sce_rekey
);
2055 // We are going to start the rekey. Let's fire off the
2056 // phase1 expiration timer if it is not done yet.
2057 if (!iph1
->sce
&& iph1
->approval
->lifetimegap
) {
2058 iph1
->sce
= sched_new(iph1
->approval
->lifetimegap
,
2059 isakmp_ph1expire_stub
, iph1
);
2062 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
2063 if (iph1
->sce
== 0 ||
2064 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2069 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2070 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2074 plog(ASL_LEVEL_INFO
,
2075 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2077 isakmp_pindex(&iph1
->index
, 0));
2081 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2085 // exit if there is another ph1 that is established (with a pending rekey timer)
2086 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2087 plog(ASL_LEVEL_INFO
,
2088 "Request for ISAKMP-SA rekey was ignored "
2089 "due to another established ph1.\n");
2093 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2094 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2095 plog(ASL_LEVEL_DEBUG
, "Reschedule Phase 1 rekey.\n");
2096 iph1
->sce_rekey
= sched_new(1,
2097 isakmp_ph1rekeyexpire_stub
,
2102 // get rmconf to initiate rekey with
2103 rmconf
= iph1
->rmconf
;
2105 rmconf
= getrmconf(iph1
->remote
);
2108 /* begin quick mode */
2109 plog(ASL_LEVEL_DEBUG
, "Begin Phase 1 rekey.\n");
2111 /* start phase 1 negotiation as a initiator. */
2112 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2113 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey Failed.\n");
2115 iph1
->is_rekey
= TRUE
;
2118 "Phase1 rekey failed: no configuration found for %s.\n",
2119 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2124 isakmp_ph1rekeyretry(iph1
)
2125 phase1_handle_t
*iph1
;
2128 struct remoteconf
*rmconf
;
2130 // this code path is meant for floated ph1 rekeys that are failing on the first message
2131 if (iph1
->sce
!= 0 ||
2132 iph1
->sce_rekey
!= 0 ||
2133 ((iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
&&
2134 iph1
->status
!= IKEV1_STATE_AGG_I_MSG1SENT
)
2135 || ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0))
2136 || (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
)
2137 || iph1
->is_dying
) {
2141 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2142 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2146 plog(ASL_LEVEL_INFO
,
2147 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2149 isakmp_pindex(&iph1
->index
, 0));
2153 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2154 plog(ASL_LEVEL_INFO
,
2155 "request for ISAKMP-SA rekey was ignored "
2156 "due to idleness.\n");
2160 // exit if there is another ph1 that is established (with a pending rekey timer)
2161 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2162 plog(ASL_LEVEL_INFO
,
2163 "request to retry ISAKMP-SA rekey was ignored "
2164 "due to another established ph1.\n");
2168 // some servers don't like respond to 4500 for rekeys... try accomodate them
2169 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2170 set_port(iph1
->local
, PORT_ISAKMP
);
2172 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2173 set_port(iph1
->remote
, PORT_ISAKMP
);
2175 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2176 rmconf
= getrmconf(iph1
->remote
);
2178 /* begin quick mode */
2179 plog(ASL_LEVEL_DEBUG
, "begin Phase 1 rekey retry.\n");
2181 /* start phase 1 negotiation as a initiator. */
2182 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2183 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey retry Failed.\n");
2186 iph1
->is_rekey
= TRUE
;
2189 "Phase 1 rekey retry failed: no configuration found for %s.\n",
2190 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2196 /* called from scheduler */
2198 isakmp_ph1delete_stub(p
)
2202 isakmp_ph1delete((phase1_handle_t
*)p
);
2206 isakmp_ph1delete(iph1
)
2207 phase1_handle_t
*iph1
;
2211 SCHED_KILL(iph1
->sce
);
2212 SCHED_KILL(iph1
->sce_rekey
);
2214 SCHED_KILL(iph1
->dpd_r_u
);
2217 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2218 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2222 isakmp_info_send_d1(iph1
);
2224 /* don't re-negosiation when the phase 1 SA expires. */
2226 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2227 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2231 plog(ASL_LEVEL_INFO
,
2232 "ISAKMP-SA deleted %s-%s spi:%s\n",
2233 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2237 ike_session_unlink_phase1(iph1
);
2242 /* called from scheduler.
2243 * this function will call only isakmp_ph2delete().
2244 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2245 * by something cause. That's why this function is called after phase 2 SA
2246 * expires in the userland.
2249 isakmp_ph2expire_stub(p
)
2253 isakmp_ph2expire((phase2_handle_t
*)p
);
2257 isakmp_ph2expire(iph2
)
2258 phase2_handle_t
*iph2
;
2262 if (FSM_STATE_IS_EXPIRED(iph2
->status
)) {
2266 SCHED_KILL(iph2
->sce
);
2268 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2269 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2273 plog(ASL_LEVEL_INFO
,
2274 "Phase 2 sa expired %s-%s\n", src
, dst
);
2278 // delete outgoing SAs
2279 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
) && iph2
->approval
) {
2282 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2284 pfkey_send_delete(lcconf
->sock_pfkey
,
2285 ipsecdoi2pfkey_proto(pr
->proto_id
),
2287 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2291 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2292 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
2293 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2298 /* called from scheduler */
2300 isakmp_ph2delete_stub(p
)
2304 isakmp_ph2delete((phase2_handle_t
*)p
);
2308 isakmp_ph2delete(iph2
)
2309 phase2_handle_t
*iph2
;
2313 SCHED_KILL(iph2
->sce
);
2315 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2316 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2320 plog(ASL_LEVEL_INFO
,
2321 "Phase 2 sa deleted %s-%s\n", src
, dst
);
2325 ike_session_unlink_phase2(iph2
);
2331 * Interface between PF_KEYv2 and ISAKMP
2334 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2335 * if phase1 has been finished, begin phase2.
2338 isakmp_post_acquire(phase2_handle_t
*iph2
)
2340 struct remoteconf
*rmconf
;
2341 phase1_handle_t
*iph1
= NULL
;
2343 plog(ASL_LEVEL_DEBUG
, "In post_acquire\n");
2345 /* search appropreate configuration with masking port. */
2346 rmconf
= getrmconf(iph2
->dst
);
2347 if (rmconf
== NULL
) {
2349 "No configuration found for %s.\n",
2350 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2353 /* if passive mode, ignore the acquire message */
2354 if (rmconf
->passive
) {
2355 plog(ASL_LEVEL_DEBUG
,
2356 "Because of passive mode, ignore the acquire message for %s.\n",
2357 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2362 // what if there is another ph2 that is negotiating
2363 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2364 // TODO: postpone this rekey for a second later
2365 plog(ASL_LEVEL_INFO
,
2366 "Request for establishing IPsec-SA was ignored due to another negoing ph2.\n");
2370 // if this is a phase2 rekeys (the policy may not have the current port number).
2371 // so, use the appropriate ports.
2372 if (iph2
->is_rekey
) {
2373 ike_session_update_ph2_ports(iph2
);
2375 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2376 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2378 /* no IKE-SA found. */
2380 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2382 /* start phase 1 negotiation as a initiator. */
2383 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2385 plog(ASL_LEVEL_INFO
,
2386 "IPsec-SA request for %s queued due to no Phase 1 found.\n",
2387 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2389 // exit if there is another ph1 that is established (with a pending rekey timer)
2390 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2391 plog(ASL_LEVEL_INFO
,
2392 "Request for Phase 1 was ignored due to another negotiating Phase 1.\n");
2396 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, &iph2
->nat64_prefix
) < 0) {
2397 plog(ASL_LEVEL_INFO
,
2398 "Request for Phase 1 failed. Will try later.\n");
2404 /* found ISAKMP-SA, but on negotiation. */
2405 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2406 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2407 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2408 plog(ASL_LEVEL_INFO
,
2409 "Request for establishing IPsec-SA was queued due to no phase1 found.\n");
2414 /* found established ISAKMP-SA */
2416 /* found ISAKMP-SA. */
2418 /* begin quick mode */
2419 plog(ASL_LEVEL_DEBUG
, "Begin QUICK mode.\n");
2420 if (ikev1_ph2begin_i(iph1
, iph2
))
2426 * receive GETSPI from kernel.
2429 isakmp_post_getspi(phase2_handle_t
*iph2
)
2432 struct timeval start
, end
;
2436 /* don't process it because there is no suitable phase1-sa. */
2437 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)) {
2439 "the negotiation is stopped, "
2440 "because there is no suitable ISAKMP-SA.\n");
2443 fsm_set_state(&iph2
->status
, iph2
->side
== INITIATOR
?
2444 IKEV1_STATE_QUICK_I_GETSPIDONE
: IKEV1_STATE_QUICK_R_GETSPIDONE
);
2447 gettimeofday(&start
, NULL
);
2449 switch (iph2
->side
) {
2451 error
= quick_i1send(iph2
, NULL
);
2454 error
= quick_r2send(iph2
, NULL
);
2458 if (error
) //%%%%%%%% log something ???
2461 gettimeofday(&end
, NULL
);
2462 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
2464 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2465 timedelta(&start
, &end
));
2471 /* called by scheduler */
2473 isakmp_chkph1there_stub(p
)
2476 isakmp_chkph1there((phase2_handle_t
*)p
);
2480 isakmp_chkph1there(iph2
)
2481 phase2_handle_t
*iph2
;
2483 phase1_handle_t
*iph1
;
2485 if ((iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
&& iph2
->status
!= IKEV1_STATE_QUICK_I_START
) ||
2487 plog(ASL_LEVEL_DEBUG
, "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
);
2491 iph2
->retry_checkph1
--;
2493 if (iph2
->retry_checkph1
< 0 /* %%%%||
2494 ike_session_verify_ph2_parent_session(iph2) */) {
2495 if (iph2
->retry_checkph1
< 0) {
2497 "Phase 2 negotiation failed "
2498 "due to time up waiting for Phase 1. %s\n",
2499 sadbsecas2str(iph2
->dst
, iph2
->src
,
2500 iph2
->satype
, 0, 0));
2503 "Phase 2 negotiation failed "
2504 "due to invalid parent session. %s\n",
2505 sadbsecas2str(iph2
->dst
, iph2
->src
,
2506 iph2
->satype
, 0, 0));
2508 plog(ASL_LEVEL_INFO
,
2509 "delete Phase 2 handler.\n");
2511 /* send acquire to kernel as error */
2512 pk_sendeacquire(iph2
);
2513 ike_session_unlink_phase2(iph2
);
2517 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2519 /* XXX Even if ph1 as responder is there, should we not start
2520 * phase 2 negotiation ? */
2522 && FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2523 /* found isakmp-sa */
2525 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2526 plog(ASL_LEVEL_DEBUG
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2527 plog(ASL_LEVEL_DEBUG
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2528 plog(ASL_LEVEL_DEBUG
, "before:\n");
2529 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2530 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2531 set_port(iph2
->src
, extract_port(iph1
->local
));
2532 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2533 plog(ASL_LEVEL_DEBUG
, "After:\n");
2534 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2535 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2537 /* begin quick mode */
2538 if (ikev1_ph2begin_i(iph1
, iph2
)) {
2539 ike_session_unlink_phase2(iph2
);
2543 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2544 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2545 /* start phase 1 negotiation as a initiator. */
2547 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, iph1
!= NULL
? &iph1
->nat64_prefix
: NULL
) < 0) {
2548 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2550 } else if (rmconf
== NULL
) {
2551 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no remoteconf found... failed to initiate new one\n");
2555 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established ph1 handler found\n");
2557 /* no isakmp-sa found */
2558 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2563 /* copy variable data into ALLOCATED buffer. */
2565 isakmp_set_attr_v(buf
, type
, val
, len
)
2571 struct isakmp_data
*data
;
2573 data
= (struct isakmp_data
*)buf
;
2574 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2575 data
->lorv
= htons((u_int16_t
)len
);
2576 memcpy(data
+ 1, val
, len
);
2578 return buf
+ sizeof(*data
) + len
;
2581 /* copy fixed length data into ALLOCATED buffer. */
2583 isakmp_set_attr_l(buf
, type
, val
)
2588 struct isakmp_data
*data
;
2590 data
= (struct isakmp_data
*)buf
;
2591 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2592 data
->lorv
= htons((u_int16_t
)val
);
2594 return buf
+ sizeof(*data
);
2597 /* add a variable data attribute to the buffer by reallocating it. */
2599 isakmp_add_attr_v(buf0
, type
, val
, len
)
2605 vchar_t
*buf
= NULL
;
2606 struct isakmp_data
*data
;
2610 tlen
= sizeof(*data
) + len
;
2614 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2616 buf
= vmalloc(tlen
);
2619 "failed to get a attribute buffer.\n");
2623 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2624 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2625 data
->lorv
= htons((u_int16_t
)len
);
2626 memcpy(data
+ 1, val
, len
);
2631 /* add a fixed data attribute to the buffer by reallocating it. */
2633 isakmp_add_attr_l(buf0
, type
, val
)
2638 vchar_t
*buf
= NULL
;
2639 struct isakmp_data
*data
;
2643 tlen
= sizeof(*data
);
2647 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2649 buf
= vmalloc(tlen
);
2652 "failed to get a attribute buffer.\n");
2656 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2657 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2658 data
->lorv
= htons((u_int16_t
)val
);
2664 * calculate cookie and set.
2667 isakmp_newcookie(place
, remote
, local
)
2669 struct sockaddr_storage
*remote
;
2670 struct sockaddr_storage
*local
;
2672 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2682 if (remote
->ss_family
!= local
->ss_family
) {
2684 "address family mismatch, remote:%d local:%d\n",
2685 remote
->ss_family
, local
->ss_family
);
2688 switch (remote
->ss_family
) {
2690 alen
= sizeof(struct in_addr
);
2691 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2692 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2696 alen
= sizeof(struct in_addr
);
2697 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2698 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2703 "invalid family: %d\n", remote
->ss_family
);
2706 blen
= (alen
+ sizeof(u_short
)) * 2
2707 + sizeof(time_t) + lcconf
->secret_size
;
2708 buf
= vmalloc(blen
);
2711 "failed to get a cookie.\n");
2716 /* copy my address */
2717 memcpy(p
, sa1
, alen
);
2719 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2720 memcpy(p
, &port
, sizeof(u_short
));
2721 p
+= sizeof(u_short
);
2723 /* copy target address */
2724 memcpy(p
, sa2
, alen
);
2726 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2727 memcpy(p
, &port
, sizeof(u_short
));
2728 p
+= sizeof(u_short
);
2732 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2735 /* copy random value */
2736 buf2
= eay_set_random(lcconf
->secret_size
);
2739 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2740 p
+= lcconf
->secret_size
;
2743 buf2
= eay_sha1_one(buf
);
2744 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2746 sa1
= val2str(place
, sizeof (cookie_t
));
2747 plog(ASL_LEVEL_DEBUG
, "new cookie:\n%s\n", sa1
);
2760 * save partner's(payload) data into phhandle.
2763 isakmp_p2ph(buf
, gen
)
2765 struct isakmp_gen
*gen
;
2767 /* XXX to be checked in each functions for logging. */
2769 plog(ASL_LEVEL_WARNING
,
2770 "ignore this payload, same payload type exist.\n");
2774 if (ntohs(gen
->len
) < sizeof(*gen
)) {
2776 "ignore this payload, invalid payload len %d.\n",
2781 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2784 "failed to get buffer.\n");
2787 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2793 isakmp_newmsgid2(iph1
)
2794 phase1_handle_t
*iph1
;
2799 msgid2
= eay_random();
2800 } while (ike_session_getph2bymsgid(iph1
, msgid2
));
2806 * set values into allocated buffer of isakmp header for phase 1
2809 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2811 phase1_handle_t
*iph1
;
2817 struct isakmp
*isakmp
;
2819 if (vbuf
->l
< sizeof(*isakmp
))
2822 isakmp
= (struct isakmp
*)vbuf
->v
;
2824 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2825 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2826 isakmp
->np
= nptype
;
2827 isakmp
->v
= iph1
->version
;
2828 isakmp
->etype
= etype
;
2829 isakmp
->flags
= flags
;
2830 isakmp
->msgid
= msgid
;
2831 isakmp
->len
= htonl(vbuf
->l
);
2833 return vbuf
->v
+ sizeof(*isakmp
);
2837 * set values into allocated buffer of isakmp header for phase 1
2840 set_isakmp_header1(vbuf
, iph1
, nptype
)
2842 phase1_handle_t
*iph1
;
2845 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2849 * set values into allocated buffer of isakmp header for phase 2
2852 set_isakmp_header2(vbuf
, iph2
, nptype
)
2854 phase2_handle_t
*iph2
;
2857 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2861 * set values into allocated buffer of isakmp payload.
2864 set_isakmp_payload(buf
, src
, nptype
)
2869 struct isakmp_gen
*gen
;
2872 plog(ASL_LEVEL_DEBUG
, "add payload of len %zu, next type %d\n",
2875 gen
= (struct isakmp_gen
*)p
;
2877 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2879 memcpy(p
, src
->v
, src
->l
);
2886 #ifdef HAVE_PRINT_ISAKMP_C
2887 /* for print-isakmp.c */
2889 extern void isakmp_print(const u_char
*, u_int
, const u_char
*);
2891 char *getname(const u_char
*);
2893 char *getname6(const u_char
*);
2895 int safeputchar(int);
2898 * Return a name for the IP address pointed to by ap. This address
2899 * is assumed to be in network byte order.
2905 struct sockaddr_in addr
;
2906 static char ntop_buf
[NI_MAXHOST
];
2908 memset(&addr
, 0, sizeof(addr
));
2909 addr
.sin_len
= sizeof(struct sockaddr_in
);
2910 addr
.sin_family
= AF_INET
;
2911 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2912 if (getnameinfo(&addr
, sizeof(addr
),
2913 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2914 NI_NUMERICHOST
| niflags
))
2915 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2922 * Return a name for the IP6 address pointed to by ap. This address
2923 * is assumed to be in network byte order.
2929 struct sockaddr_in6 addr
;
2930 static char ntop_buf
[NI_MAXHOST
];
2932 memset(&addr
, 0, sizeof(addr
));
2933 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2934 addr
.sin6_family
= AF_INET6
;
2935 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2936 if (getnameinfo(&addr
, addr
.sin6_len
,
2937 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2938 NI_NUMERICHOST
| niflags
))
2939 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2951 ch
= (unsigned char)(c
& 0xff);
2952 if (c
< 0x80 && isprint(c
))
2953 return printf("%c", c
& 0xff);
2955 return printf("\\%03o", c
& 0xff);
2959 isakmp_printpacket(msg
, from
, my
, decoded
)
2961 struct sockaddr_storage
*from
;
2962 struct sockaddr_storage
*my
;
2968 char hostbuf
[NI_MAXHOST
];
2969 char portbuf
[NI_MAXSERV
];
2970 struct isakmp
*isakmp
;
2974 if (loglevel
< ASL_LEVEL_DEBUG
)
2978 plog(ASL_LEVEL_DEBUG
, "begin.\n");
2980 gettimeofday(&tv
, NULL
);
2981 s
= tv
.tv_sec
% 3600;
2982 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2985 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
2986 portbuf
, sizeof(portbuf
),
2987 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2988 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2989 strlcpy(portbuf
, "?", sizeof(portbuf
));
2991 printf("%s:%s", hostbuf
, portbuf
);
2996 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
2997 portbuf
, sizeof(portbuf
),
2998 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2999 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
3000 strlcpy(portbuf
, "?", sizeof(portbuf
));
3002 printf("%s:%s", hostbuf
, portbuf
);
3009 printf("(malloc fail)\n");
3013 isakmp
= (struct isakmp
*)buf
->v
;
3014 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
3017 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
3018 if (buf
->l
< pad
&& 2 < vflag
)
3019 printf("(wrong padding)");
3021 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
3025 snapend
= buf
->v
+ buf
->l
;
3026 isakmp_print(buf
->v
, buf
->l
, NULL
);
3034 #endif /*HAVE_PRINT_ISAKMP_C*/
3037 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
3038 phase1_handle_t
*iph1
;
3039 struct remoteconf
*rmconf
;
3040 struct sockaddr_storage
*remote
, *local
;
3042 u_short
*port
= NULL
;
3044 /* address portion must be grabbed from real remote address "remote" */
3045 iph1
->remote
= dupsaddr(remote
);
3046 if (iph1
->remote
== NULL
) {
3047 ike_session_delph1(iph1
);
3052 * if remote has no port # (in case of initiator - from ACQUIRE msg)
3053 * - if remote.conf specifies port #, use that
3054 * - if remote.conf does not, use 500
3055 * if remote has port # (in case of responder - from recvfrom(2))
3056 * respect content of "remote".
3058 switch (iph1
->remote
->ss_family
) {
3060 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3063 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3066 *port
= htons(PORT_ISAKMP
);
3070 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3073 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3076 *port
= htons(PORT_ISAKMP
);
3081 "invalid family: %d\n", iph1
->remote
->ss_family
);
3082 ike_session_delph1(iph1
);
3087 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3089 iph1
->local
= dupsaddr(local
);
3090 if (iph1
->local
== NULL
) {
3091 ike_session_delph1(iph1
);
3095 switch (iph1
->local
->ss_family
) {
3097 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3100 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3103 *port
= getmyaddrsport(iph1
->local
);
3107 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3110 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3113 *port
= getmyaddrsport(iph1
->local
);
3118 "invalid family: %d\n", iph1
->local
->ss_family
);
3119 ike_session_delph1(iph1
);
3123 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3124 plog (ASL_LEVEL_DEBUG
, "Marking ports as changed\n");
3125 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3133 log_ph1established(iph1
)
3134 const phase1_handle_t
*iph1
;
3138 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3139 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3143 plog(ASL_LEVEL_INFO
,
3144 "ISAKMP-SA established %s-%s spi:%s\n",
3146 isakmp_pindex(&iph1
->index
, 0));
3151 IPSECLOGASLMSG("IPSec Phase 1 established (Initiated by %s).\n",
3152 (iph1
->side
== INITIATOR
)? "me" : "peer");
3157 struct payload_list
*
3158 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3161 plist
= racoon_malloc (sizeof (struct payload_list
));
3165 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3166 plist
->next
->prev
= plist
;
3167 plist
= plist
->next
;
3171 plist
->payload
= payload
;
3172 plist
->payload_type
= payload_type
;
3178 isakmp_plist_set_all (struct payload_list
**plist
, phase1_handle_t
*iph1
)
3180 struct payload_list
*ptr
= *plist
, *first
;
3181 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3182 vchar_t
*buf
= NULL
;
3185 /* Seek to the first item. */
3186 while (ptr
->prev
) ptr
= ptr
->prev
;
3189 /* Compute the whole length. */
3191 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3195 buf
= vmalloc(tlen
);
3198 "failed to get buffer to send.\n");
3204 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3210 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3213 racoon_free (first
);
3214 /* ptr->prev = NULL; first = NULL; ... omitted. */
3229 frag_handler(iph1
, msg
, remote
, local
)
3230 phase1_handle_t
*iph1
;
3232 struct sockaddr_storage
*remote
;
3233 struct sockaddr_storage
*local
;
3237 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3238 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3240 "Packet reassembly failed\n");
3244 /* simply reply if the packet was processed. */
3245 if (ike_session_check_recvdpkt(remote
, local
, newmsg
) > 0) {
3246 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3247 saddr2str((struct sockaddr
*)remote
));
3249 plog(ASL_LEVEL_NOTICE
,
3250 "the reassembled packet is retransmitted by %s.\n",
3251 saddr2str((struct sockaddr
*)remote
));
3256 isakmp_main(newmsg
, remote
, local
);
3266 phase1_handle_t
*iph1
;
3268 vchar_t
*buf
= NULL
;
3269 struct sadb_msg
*msg
, *next
, *end
;
3271 struct sockaddr_storage
*src
, *dst
;
3272 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3274 phase2_handle_t
*iph2
;
3275 phase1_handle_t
*new_iph1
;
3277 plog(ASL_LEVEL_INFO
,
3278 "purging ISAKMP-SA spi=%s.\n",
3279 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3281 /* Mark as expired. */
3282 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
3284 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3287 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3288 * Keep all others phase2 SAs.
3290 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3292 plog(ASL_LEVEL_DEBUG
,
3293 "pfkey_dump_sadb returned nothing.\n");
3297 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3298 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3301 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3303 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3304 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3309 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3311 "pfkey_check (%s)\n", ipsec_strerror());
3316 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3318 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3319 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3323 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3324 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3326 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3327 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3328 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3334 * check in/outbound SAs.
3335 * Select only SAs where src == local and dst == remote (outgoing)
3336 * or src == remote and dst == local (incoming).
3338 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3339 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3344 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3345 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3347 /* Check if there is another valid ISAKMP-SA */
3348 if (new_iph1
!= NULL
) {
3351 /* No handler... still send a pfkey_delete message, but log this !*/
3352 plog(ASL_LEVEL_INFO
,
3353 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3354 ntohl(sa
->sadb_sa_spi
));
3358 * If we have a new ph1, do not purge IPsec-SAs binded
3359 * to a different ISAKMP-SA
3361 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3366 /* If the ph2handle is established, do not purge IPsec-SA */
3367 if (FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(iph2
->status
)) {
3369 plog(ASL_LEVEL_INFO
,
3370 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3371 ntohl(sa
->sadb_sa_spi
),
3372 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3380 pfkey_send_delete(lcconf
->sock_pfkey
,
3381 msg
->sadb_msg_satype
,
3383 src
, dst
, sa
->sadb_sa_spi
);
3385 /* delete a relative phase 2 handle. */
3388 ike_session_unlink_phase2(iph2
);
3391 plog(ASL_LEVEL_INFO
,
3392 "purged IPsec-SA spi=%u.\n",
3393 ntohl(sa
->sadb_sa_spi
));
3401 /* Mark the phase1 handler as EXPIRED */
3402 plog(ASL_LEVEL_INFO
,
3403 "purged ISAKMP-SA spi=%s.\n",
3404 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3406 SCHED_KILL(iph1
->sce
);
3408 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3413 phase2_handle_t
*iph2
;
3418 /* Delete the SPD entry if we generated it
3420 if (iph2
->generated_spidx
) {
3422 u_int64_t force_align
; // Wcast-align fix - force alignment
3423 struct policyindex spidx
;
3425 struct sockaddr_storage addr
;
3427 struct sockaddr_storage
*src
= iph2
->src
;
3428 struct sockaddr_storage
*dst
= iph2
->dst
;
3430 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3432 plog(ASL_LEVEL_INFO
,
3433 "generated policy, deleting it.\n");
3435 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
3436 iph2
->spidx_gen
= &u
.spidx
;
3438 /* make inbound policy */
3441 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
3442 u
.spidx
.ul_proto
= 0;
3445 * Note: code from get_proposal_r
3448 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
3451 * make destination address in spidx from either ID payload
3452 * or phase 1 address into a address in spidx.
3454 if (iph2
->id
!= NULL
3455 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3456 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3457 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3458 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3459 /* get a destination address of a policy */
3460 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
3461 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
, iph2
->version
);
3467 * get scopeid from the SA address.
3468 * note that the phase 1 source address is used as
3469 * a destination address to search for a inbound
3470 * policy entry because rcoon is responder.
3472 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3474 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
3479 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3480 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3481 idi2type
= _XIDT(iph2
->id
);
3485 plog(ASL_LEVEL_DEBUG
,
3486 "Get a destination address of SP index "
3487 "from Phase 1 address "
3488 "due to no ID payloads found "
3489 "OR because ID type is not address.\n");
3492 * copy the SOURCE address of IKE into the
3493 * DESTINATION address of the key to search the
3494 * SPD because the direction of policy is inbound.
3496 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
3497 switch (u
.spidx
.dst
.ss_family
) {
3500 sizeof(struct in_addr
) << 3;
3505 sizeof(struct in6_addr
) << 3;
3514 /* make source address in spidx */
3515 if (iph2
->id_p
!= NULL
3516 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3517 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3518 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3519 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3520 /* get a source address of inbound SA */
3521 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
3522 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
, iph2
->version
);
3528 * get scopeid from the SA address.
3529 * for more detail, see above of this function.
3531 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3533 setscopeid(&u
.spidx
.src
, iph2
->dst
);
3539 /* make id[src,dst] if both ID types are IP address and same */
3540 if (_XIDT(iph2
->id_p
) == idi2type
3541 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
3543 dupsaddr(&u
.spidx
.dst
);
3545 dupsaddr(&u
.spidx
.src
);
3549 plog(ASL_LEVEL_DEBUG
,
3550 "Get a source address of SP index "
3551 "from Phase 1 address "
3552 "due to no ID payloads found "
3553 "OR because ID type is not address.\n");
3555 /* see above comment. */
3556 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
3557 switch (u
.spidx
.src
.ss_family
) {
3560 sizeof(struct in_addr
) << 3;
3565 sizeof(struct in6_addr
) << 3;
3576 plog(ASL_LEVEL_DEBUG
,
3577 "get a src address from ID payload "
3578 "%s prefixlen=%u ul_proto=%u\n",
3579 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
3580 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3581 plog(ASL_LEVEL_DEBUG
,
3582 "get dst address from ID payload "
3583 "%s prefixlen=%u ul_proto=%u\n",
3584 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
3585 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
3588 * convert the ul_proto if it is 0
3589 * because 0 in ID payload means a wild card.
3591 if (u
.spidx
.ul_proto
== 0)
3592 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3596 /* End of code from get_proposal_r
3599 if (pk_sendspddelete(iph2
) < 0) {
3601 "pfkey spddelete(inbound) failed.\n");
3603 plog(ASL_LEVEL_DEBUG
,
3604 "pfkey spddelete(inbound) sent.\n");
3607 #ifdef HAVE_POLICY_FWD
3608 /* make forward policy if required */
3609 if (tunnel_mode_prop(iph2
->approval
)) {
3610 u
.spidx
.dir
= IPSEC_DIR_FWD
;
3611 if (pk_sendspddelete(iph2
) < 0) {
3613 "pfkey spddelete(forward) failed.\n");
3615 plog(ASL_LEVEL_DEBUG
,
3616 "pfkey spddelete(forward) sent.\n");
3621 /* make outbound policy */
3624 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3626 u
.spidx
.src
= u
.spidx
.dst
;
3628 pref
= u
.spidx
.prefs
;
3629 u
.spidx
.prefs
= u
.spidx
.prefd
;
3630 u
.spidx
.prefd
= pref
;
3632 if (pk_sendspddelete(iph2
) < 0) {
3634 "pfkey spddelete(outbound) failed.\n");
3636 plog(ASL_LEVEL_DEBUG
,
3637 "pfkey spddelete(outbound) sent.\n");
3640 iph2
->spidx_gen
=NULL
;
3646 setscopeid(sp_addr0
, sa_addr0
)
3647 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
3649 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3651 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3652 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3654 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3655 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3656 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3659 /* this check should not be here ? */
3660 if (sa_addr
->sin6_family
!= AF_INET6
) {
3662 "can't get scope ID: family mismatch\n");
3666 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3668 "scope ID is not supported except of lladdr.\n");
3672 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
3679 isakmp_plist_append_initial_contact (iph1
, plist
)
3680 phase1_handle_t
*iph1
;
3681 struct payload_list
*plist
;
3683 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
3684 vchar_t
*notp_ini
= NULL
;
3685 struct isakmp_pl_n np
, *nptr
;
3688 np
.doi
= htonl(iph1
->rmconf
->doitype
);
3689 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
3690 np
.spi_size
= sizeof(isakmp_index
);
3691 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
3692 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
3693 + sizeof(isakmp_index
)))) {
3695 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
3696 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
3697 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
3698 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
3699 plog(ASL_LEVEL_DEBUG
,
3700 "added initial-contact payload.\n");
3702 /* insert a node into contacted list. */
3703 if (ike_session_inscontacted(iph1
->remote
) == -1) {
3705 "failed to add contacted list.\n");
3711 "failed to allocate notification payload.\n");
3715 plog(ASL_LEVEL_DEBUG
, "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
3716 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, ike_session_getcontacted(iph1
->remote
)? 1:0);