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
)
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 iph1
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
749 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
750 /* don't call remph1(iph1) until after insph1(iph1) is called */
751 ike_session_delph1(iph1
);
756 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
760 iph1
->frag_chain
= NULL
;
761 iph1
->approval
= NULL
;
763 /* XXX copy remote address */
764 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
765 /* don't call remph1(iph1) until after insph1(iph1) is called */
766 iph1
= NULL
; /* deleted in copy_ph1addresses */
770 if (ike_session_link_phase1(session
, iph1
) != 0) {
771 plog(ASL_LEVEL_DEBUG
, "Failed to link ph1 to session\n");
772 ike_session_delph1(iph1
);
775 // HACK!!! to track rekeys across SIGHUPs
776 if (started_by_api
== VPN_RESTARTED_BY_API
&&
778 iph1
->parent_session
->established
= 1;
779 iph1
->parent_session
->ikev1_state
.ph2cnt
++;
783 /* start phase 1 exchange */
784 iph1
->etype
= rmconf
->etypes
->type
;
785 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
786 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_START
);
787 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
788 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_I_START
);
792 plog(ASL_LEVEL_DEBUG
, "===\n");
796 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
800 "initiate new phase 1 negotiation: %s<=>%s\n",
801 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
806 s_isakmp_etype(iph1
->etype
));
809 gettimeofday(&iph1
->start
, NULL
);
810 gettimeofday(&start
, NULL
);
813 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by me).\n");
815 if (fsm_ikev1_phase1_send_response(iph1
, NULL
)) {
816 ike_session_unlink_phase1(iph1
);
821 gettimeofday(&end
, NULL
);
822 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
824 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
825 timedelta(&start
, &end
));
828 #ifdef ENABLE_VPNCONTROL_PORT
829 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
835 /* new negotiation of phase 1 for responder */
837 ikev1_ph1begin_r(ike_session_t
*session
, vchar_t
*msg
, struct sockaddr_storage
*remote
,
838 struct sockaddr_storage
*local
, u_int8_t etype
)
841 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
842 struct remoteconf
*rmconf
;
843 phase1_handle_t
*iph1
;
844 struct etypes
*etypeok
;
846 struct timeval start
, end
;
849 /* look for my configuration */
850 rmconf
= getrmconf(remote
);
851 if (rmconf
== NULL
) {
858 /* check to be acceptable exchange type */
859 etypeok
= check_etypeok(rmconf
, etype
);
860 if (etypeok
== NULL
) {
862 "not acceptable %s mode\n", s_isakmp_etype(etype
));
866 /* get new entry to isakmp status table. */
867 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
871 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
872 iph1
->rmconf
= rmconf
;
873 retain_rmconf(iph1
->rmconf
);
875 iph1
->side
= RESPONDER
;
876 iph1
->started_by_api
= 0;
877 iph1
->etype
= etypeok
->type
;
878 iph1
->version
= isakmp
->v
;
881 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
882 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_START
);
883 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
884 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_R_START
);
890 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
891 /* don't call remph1(iph1) until after insph1(iph1) is called */
892 ike_session_delph1(iph1
);
898 iph1
->frag_chain
= NULL
;
899 iph1
->approval
= NULL
;
901 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
902 * We have to setup this flag now to correctly generate the first reply.
903 * Don't know if a better check could be done for that ?
905 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
906 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
908 /* copy remote address */
909 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
910 /* don't call remph1(iph1) until after insph1(iph1) is called */
911 iph1
= NULL
; /* deleted in copy_ph1addresses */
915 if (ike_session_link_phase1(session
, iph1
) != 0) {
916 ike_session_delph1(iph1
);
920 plog(ASL_LEVEL_DEBUG
, "===\n");
924 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
928 "respond new phase 1 negotiation: %s<=>%s\n",
929 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
933 "begin %s mode.\n", s_isakmp_etype(etype
));
936 gettimeofday(&iph1
->start
, NULL
);
937 gettimeofday(&start
, NULL
);
940 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by peer).\n");
942 /* now that we have a phase1 handle, feed back into our
943 * main receive function to catch fragmented packets
945 isakmp_main(msg
, remote
, local
);
949 /* new negotiation of phase 2 for initiator */
951 ikev1_ph2begin_i(phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
955 if (xauth_check(iph1
) != 0) {
957 "Attempt to start phase 2 whereas Xauth failed\n");
962 /* found ISAKMP-SA. */
963 plog(ASL_LEVEL_DEBUG
, "===\n");
964 plog(ASL_LEVEL_DEBUG
, "begin QUICK mode.\n");
967 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
971 "initiate new phase 2 negotiation: %s<=>%s\n",
972 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
977 gettimeofday(&iph2
->start
, NULL
);
981 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
983 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by me).\n");
984 if (quick_iprep(iph2
, NULL
))
987 #ifdef ENABLE_VPNCONTROL_PORT
988 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
994 /* new negotiation of phase 2 for responder */
996 ikev1_ph2begin_r(phase1_handle_t
*iph1
, vchar_t
*msg
)
998 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
999 phase2_handle_t
*iph2
= 0;
1002 struct timeval start
, end
;
1004 #ifdef ENABLE_HYBRID
1005 if (xauth_check(iph1
) != 0) {
1007 "Attempt to start Phase 2 whereas Xauth failed\n");
1012 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_SA
);
1015 "failed to allocate Phase 2 entry.\n");
1019 iph2
->side
= RESPONDER
;
1020 iph2
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
1021 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_START
);
1022 iph2
->flags
= isakmp
->flags
;
1023 iph2
->msgid
= isakmp
->msgid
;
1024 iph2
->seq
= pk_getseq();
1025 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1026 if (iph2
->ivm
== NULL
) {
1027 ike_session_delph2(iph2
);
1030 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1031 if (iph2
->dst
== NULL
) {
1032 ike_session_delph2(iph2
);
1035 switch (iph2
->dst
->ss_family
) {
1038 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1044 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1050 "invalid family: %d\n", iph2
->dst
->ss_family
);
1051 ike_session_delph2(iph2
);
1055 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1056 if (iph2
->src
== NULL
) {
1057 ike_session_delph2(iph2
);
1060 switch (iph2
->src
->ss_family
) {
1063 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1069 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1075 "invalid family: %d\n", iph2
->src
->ss_family
);
1076 ike_session_delph2(iph2
);
1080 if (ike_session_link_ph2_to_ph1(iph1
, iph2
))
1084 plog(ASL_LEVEL_DEBUG
, "===\n");
1088 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1091 plog(ASL_LEVEL_INFO
,
1092 "respond new phase 2 negotiation: %s<=>%s\n",
1093 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1098 gettimeofday(&start
, NULL
);
1101 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by peer).\n");
1103 error
= fsm_ikev1_phase2_process_payloads(iph2
, msg
);
1107 #ifdef ENABLE_VPNCONTROL_PORT
1108 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1115 ikev1_phase1_established(phase1_handle_t
*iph1
)
1118 u_int rekey_lifetime
;
1119 int ini_contact
= iph1
->rmconf
->ini_contact
;
1122 gettimeofday(&iph1
->end
, NULL
);
1123 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1124 "Phase 1", s_isakmp_etype(iph1
->etype
),
1125 timedelta(&iph1
->start
, &iph1
->end
));
1128 #ifdef ENABLE_VPNCONTROL_PORT
1130 if (iph1
->side
== RESPONDER
&&
1131 iph1
->local
->ss_family
== AF_INET
) {
1133 struct redirect
*addr
;
1135 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
1136 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
1137 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
1139 if (raddr
== NULL
) {
1141 "failed to send redirect message - memory error.\n");
1143 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
1144 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
1145 plog(ASL_LEVEL_DEBUG
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
1148 (void)ike_session_update_ph1_ph2tree(iph1
);
1149 isakmp_ph1expire(iph1
);
1157 /* save created date. */
1158 (void)time(&iph1
->created
);
1160 /* add to the schedule to expire, and save back pointer. */
1161 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
1162 isakmp_ph1expire_stub
, iph1
);
1164 if (iph1
->rmconf
->initiate_ph1rekey
) {
1165 if (iph1
->side
== INITIATOR
) {
1166 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
1170 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
1174 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
1175 iph1
->approval
->lifetime
);
1176 if (rekey_lifetime
) {
1177 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
1178 isakmp_ph1rekeyexpire_stub
,
1181 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
1185 "failed to get rekey timer - lifetime is too small... probably.\n");
1189 #ifdef ENABLE_HYBRID
1190 /* ignore xauth if it is a rekey */
1191 if (!iph1
->is_rekey
&&
1192 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1193 switch(AUTHMETHOD(iph1
)) {
1194 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1195 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1196 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1197 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1198 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1199 xauth_sendreq(iph1
);
1200 /* XXX Don't process INITIAL_CONTACT */
1209 /* Schedule the r_u_there.... */
1210 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1211 isakmp_sched_r_u(iph1
, 0);
1214 /* INITIAL-CONTACT processing */
1215 /* ignore initial-contact if it is a rekey */
1216 /* don't send anything if local test mode. */
1217 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
1218 /* send INITIAL-CONTACT */
1219 isakmp_info_send_n1(iph1
,
1220 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1221 /* insert a node into contacted list. */
1222 if (ike_session_inscontacted(iph1
->remote
) == -1) {
1224 "failed to add contacted list.\n");
1229 log_ph1established(iph1
);
1230 plog(ASL_LEVEL_DEBUG
, "===\n");
1232 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1234 #ifdef ENABLE_VPNCONTROL_PORT
1235 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1236 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1243 * parse ISAKMP payloads, without ISAKMP base header.
1246 isakmp_parsewoh(np0
, gen
, len
)
1248 struct isakmp_gen
*gen
;
1251 u_char np
= np0
& 0xff;
1254 struct isakmp_parse_t
*p
, *ep
;
1256 plog(ASL_LEVEL_DEBUG
, "begin.\n");
1259 * 5 is a magic number, but any value larger than 2 should be fine
1260 * as we do vrealloc() in the following loop.
1262 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1263 if (result
== NULL
) {
1265 "failed to get buffer.\n");
1268 // Wcast-align fix (void*) - result = aligned buffer of struct isakmp_parse_t
1269 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1270 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1274 /* parse through general headers */
1275 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1276 if (tlen
<= sizeof(struct isakmp_gen
)) {
1277 /* don't send information, see isakmp_ident_r1() */
1279 "invalid length of payload\n");
1284 plog(ASL_LEVEL_DEBUG
,
1285 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1288 p
->len
= ntohs(gen
->len
);
1289 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1290 plog(ASL_LEVEL_DEBUG
,
1291 "invalid length of payload\n");
1300 off
= p
- ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1301 result
= vrealloc(result
, result
->l
* 2);
1302 if (result
== NULL
) {
1303 plog(ASL_LEVEL_DEBUG
,
1304 "failed to realloc buffer.\n");
1308 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)
1309 (result
->v
+ result
->l
- sizeof(*ep
));
1310 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1315 plen
= ntohs(gen
->len
);
1316 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1319 p
->type
= ISAKMP_NPTYPE_NONE
;
1323 plog(ASL_LEVEL_DEBUG
, "succeed.\n");
1329 * parse ISAKMP payloads, including ISAKMP base header.
1335 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1336 struct isakmp_gen
*gen
;
1342 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1343 tlen
= buf
->l
- sizeof(struct isakmp
);
1344 result
= isakmp_parsewoh(np
, gen
, tlen
);
1353 ike_session_initctdtree();
1354 ike_session_init_recvdpkt();
1356 if (isakmp_open() < 0)
1369 ike_session_clear_recvdpkt();
1370 ike_session_clear_contacted();
1374 * make strings containing i_cookie + r_cookie + msgid
1377 isakmp_pindex(index
, msgid
)
1378 const isakmp_index
*index
;
1379 const u_int32_t msgid
;
1381 static char buf
[64];
1385 memset(buf
, 0, sizeof(buf
));
1388 p
= (const u_char
*)index
;
1389 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1390 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1402 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1408 /* open ISAKMP sockets. */
1413 int ifnum
= 0, encap_ifnum
= 0;
1418 int tentative_failures
= 0;
1421 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1424 if (p
->sock
!= -1) {
1428 continue; // socket already open
1431 /* warn if wildcard address - should we forbid this? */
1432 switch (p
->addr
->ss_family
) {
1434 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1435 plog(ASL_LEVEL_WARNING
,
1436 "listening to wildcard address,"
1437 "broadcast IKE packet may kill you\n");
1441 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1442 plog(ASL_LEVEL_WARNING
,
1443 "listening to wildcard address, "
1444 "broadcast IKE packet may kill you\n");
1449 "unsupported address family %d\n",
1450 lcconf
->default_af
);
1455 if (p
->addr
->ss_family
== AF_INET6
&&
1456 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1457 p
->addr
)->sin6_addr
))
1459 plog(ASL_LEVEL_DEBUG
,
1460 "Ignoring multicast address %s\n",
1461 saddr2str((struct sockaddr
*)p
->addr
));
1462 racoon_free(p
->addr
);
1468 if ((p
->sock
= socket(p
->addr
->ss_family
, SOCK_DGRAM
, 0)) < 0) {
1470 "socket (%s)\n", strerror(errno
));
1474 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1475 plog(ASL_LEVEL_ERR
, "failed to put socket in non-blocking mode\n");
1477 /* receive my interface address on inbound packets. */
1478 switch (p
->addr
->ss_family
) {
1480 if (setsockopt(p
->sock
, IPPROTO_IP
,
1482 (const void *)&yes
, sizeof(yes
)) < 0) {
1484 "setsockopt IP_RECVDSTADDR (%s)\n",
1492 pktinfo
= IPV6_RECVPKTINFO
;
1494 pktinfo
= IPV6_RECVDSTADDR
;
1496 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1497 (const void *)&yes
, sizeof(yes
)) < 0)
1500 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1501 pktinfo
, strerror(errno
));
1508 #ifdef IPV6_USE_MIN_MTU
1509 if (p
->addr
->ss_family
== AF_INET6
&&
1510 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1511 (void *)&yes
, sizeof(yes
)) < 0) {
1513 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1519 if (setsockopt_bypass(p
->sock
, p
->addr
->ss_family
) < 0)
1522 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEADDR
, (const void *)&yes
, sizeof(yes
)) < 0) {
1523 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEADDR (%s)\n", strerror(errno
));
1527 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEPORT
, (const void *)&yes
, sizeof(yes
)) < 0) {
1528 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEPORT (%s)\n", strerror(errno
));
1532 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1533 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1534 (void *)&yes
, sizeof(yes
)) < 0) {
1536 "setsockopt (%s)\n", strerror(errno
));
1541 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1542 int tmp_errno
= errno
;
1544 "failed to bind to address %s (%s).\n",
1545 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1547 // if bind failed b/c of a tentative v6 address, try again later
1548 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1549 struct in6_ifreq ifr6
;
1551 bzero(&ifr6
, sizeof(ifr6
));
1552 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1553 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1554 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1556 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1557 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1559 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1560 // address may have been tentantive... invalidate sock but leave address around for another try later
1562 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1563 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1566 tentative_failures
= 1;
1570 "failed to bind to address %s: because of interface address error, flags %x.\n",
1571 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1575 "failed to bind to address %s: can't read interface address flags.\n",
1576 saddr2str((struct sockaddr
*)p
->addr
));
1591 if (p
->source
!= NULL
) {
1592 dispatch_source_cancel(p
->source
);
1595 p
->source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, p
->sock
, 0, dispatch_get_main_queue());
1596 if (p
->source
== NULL
) {
1597 plog(ASL_LEVEL_ERR
, "could not create isakmp socket source.");
1600 dispatch_source_set_event_handler(p
->source
,
1604 dispatch_source_t the_source
= p
->source
;
1605 dispatch_source_set_cancel_handler(p
->source
,
1608 dispatch_release(the_source
);
1609 }); dispatch_resume(p
->source
);
1611 plog(ASL_LEVEL_INFO
,
1612 "%s used as isakmp port (fd=%d)\n",
1613 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
1617 racoon_free(p
->addr
);
1620 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
) {
1629 "no address could be bound.\n");
1635 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1636 plog(ASL_LEVEL_WARNING
,
1637 "NAT-T is enabled in at least one remote{} section,\n");
1638 plog(ASL_LEVEL_WARNING
,
1639 "but no 'isakmp_natt' address was specified!\n");
1643 if (tentative_failures
)
1644 sched_new(5, update_myaddrs
, NULL
);
1650 isakmp_suspend_sockets()
1654 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1656 dispatch_suspend(p
->source
);
1663 isakmp_close_sockets();
1668 isakmp_close_sockets()
1672 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1678 dispatch_source_cancel(p
->source
);
1687 // close sockets for addresses that have gone away
1689 isakmp_close_unused()
1691 struct myaddrs
*p
, *next
, **prev
;
1693 prev
= &(lcconf
->myaddrs
);
1694 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1696 if (p
->in_use
== 0) { // not in use ?
1698 dispatch_source_cancel(p
->source
);
1709 isakmp_send(iph1
, sbuf
)
1710 phase1_handle_t
*iph1
;
1715 vchar_t
*vbuf
= NULL
;
1718 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1722 * Do not add the non ESP marker for a packet that will
1723 * be fragmented. The non ESP marker should appear in
1724 * all fragment's packets, but not in the fragmented packet
1726 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1730 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1732 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1733 must added just before the packet itself. For this we must
1734 allocate a new buffer and release it at the end. */
1736 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1738 "vbuf allocation failed\n");
1741 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
1742 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1747 /* select the socket to be sent */
1748 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
1755 plog (ASL_LEVEL_DEBUG
, "%zu bytes %s\n", sbuf
->l
,
1756 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
1759 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1760 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1762 "isakmp_sendfrags failed\n");
1770 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1771 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1773 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1786 /* called from scheduler */
1788 isakmp_ph1resend_stub(p
)
1791 phase1_handle_t
*iph1
;
1793 iph1
=(phase1_handle_t
*)p
;
1794 if(isakmp_ph1resend(iph1
) < 0){
1796 /* Should not happen...
1798 SCHED_KILL(iph1
->scr
);
1801 ike_session_unlink_phase1(iph1
);
1806 isakmp_ph1resend(iph1
)
1807 phase1_handle_t
*iph1
;
1809 time_t retry_interval
;
1811 // make sure there is a buffer to send
1812 // isakmp_plist_set_all() could have returned NULL
1813 if (iph1
->sendbuf
== NULL
)
1816 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1818 if (iph1
->retry_counter
<= 0) {
1819 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1820 IPSECSESSIONEVENTCODE_IKEV1_PH1_MAX_RETRANSMIT
,
1821 CONSTSTR("Phase 1 Maximum Retransmits"),
1822 CONSTSTR("Phase 1 negotiation failed (Maximum retransmits)"));
1825 "Phase 1 negotiation failed due to time up. %s\n",
1826 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1827 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
1828 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
1829 if (isakmp_ph1rekeyretry(iph1
) == 0)
1834 ike_session_ph1_retransmits(iph1
);
1837 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1838 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1839 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1840 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1841 CONSTSTR("Phase 1 Retransmit"),
1842 CONSTSTR("Failed to retrasmit Phase1"));
1845 "Phase 1 negotiation failed due to send error. %s\n",
1846 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1850 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1851 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1852 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1853 CONSTSTR("Phase 1 Retransmit"),
1857 plog(ASL_LEVEL_DEBUG
,
1858 "Resend Phase 1 packet %s\n",
1859 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1861 iph1
->retry_counter
--;
1862 retry_interval
= ike_session_get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
1863 iph1
->rmconf
->retry_interval
);
1864 iph1
->scr
= sched_new(retry_interval
,
1865 isakmp_ph1resend_stub
, iph1
);
1870 /* called from scheduler */
1872 isakmp_ph2resend_stub(p
)
1875 phase2_handle_t
*iph2
;
1877 iph2
=(phase2_handle_t
*)p
;
1879 if(isakmp_ph2resend(iph2
) < 0){
1880 ike_session_unlink_phase2(iph2
);
1885 isakmp_ph2resend(iph2
)
1886 phase2_handle_t
*iph2
;
1888 time_t retry_interval
;
1890 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1892 //%%% BUG FIX - related to commit bit usage - crash happened here
1893 if (iph2
->ph1
== 0) {
1895 "Internal error - attempt to re-send Phase 2 with no Phase 1 bound.\n");
1899 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)){
1900 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1901 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1902 CONSTSTR("Underlying Phase 1 expired"),
1903 CONSTSTR("Failed to retransmit Phase 2 (underlying Phase 1 expired)"));
1905 "Phase 2 negotiation failed due to Phase 1 expired. %s\n",
1906 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1910 if (iph2
->retry_counter
<= 0) {
1911 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1912 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1913 CONSTSTR("Phase 2 maximum retransmits"),
1914 CONSTSTR("Phase 2 negotiation failed (maximum retransmits)"));
1916 "Phase 2 negotiation failed due to time up. %s\n",
1917 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1920 ike_session_ph2_retransmits(iph2
);
1923 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1924 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1925 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1926 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1927 CONSTSTR("Phase 2 Retransmit"),
1928 CONSTSTR("Failed to retransmit Phase2 message"));
1931 "Phase 2 negotiation failed due to send error. %s\n",
1932 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1936 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1937 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1938 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1939 CONSTSTR("Phase 2 Retransmit"),
1943 plog(ASL_LEVEL_DEBUG
,
1944 "Resend Phase 2 packet %s\n",
1945 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1947 iph2
->retry_counter
--;
1948 retry_interval
= ike_session_get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
1949 iph2
->ph1
->rmconf
->retry_interval
);
1950 iph2
->scr
= sched_new(retry_interval
,
1951 isakmp_ph2resend_stub
, iph2
);
1955 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
1956 "Phase 2 packets sent to peer: retransmit timer armed");
1963 /* called from scheduler */
1965 isakmp_ph1expire_stub(p
)
1969 isakmp_ph1expire((phase1_handle_t
*)p
);
1973 isakmp_ph1expire(iph1
)
1974 phase1_handle_t
*iph1
;
1977 phase1_handle_t
*new_iph1
;
1979 SCHED_KILL(iph1
->sce
);
1981 SCHED_KILL(iph1
->dpd_r_u
);
1984 if(!FSM_STATE_IS_EXPIRED(iph1
->status
)){
1985 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
1986 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
1990 plog(ASL_LEVEL_INFO
,
1991 "ISAKMP-SA expired %s-%s spi:%s\n",
1993 isakmp_pindex(&iph1
->index
, 0));
1996 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
1997 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
2001 * the phase1 deletion is postponed until there is no phase2.
2003 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2004 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2008 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2011 /* called from scheduler */
2013 isakmp_ph1rekeyexpire_stub(p
)
2017 isakmp_ph1rekeyexpire((phase1_handle_t
*)p
, FALSE
);
2021 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
2022 phase1_handle_t
*iph1
;
2023 int ignore_sess_drop_policy
;
2026 struct remoteconf
*rmconf
;
2028 SCHED_KILL(iph1
->sce_rekey
);
2030 // We are going to start the rekey. Let's fire off the
2031 // phase1 expiration timer if it is not done yet.
2032 if (!iph1
->sce
&& iph1
->approval
->lifetimegap
) {
2033 iph1
->sce
= sched_new(iph1
->approval
->lifetimegap
,
2034 isakmp_ph1expire_stub
, iph1
);
2037 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
2038 if (iph1
->sce
== 0 ||
2039 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2044 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2045 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2049 plog(ASL_LEVEL_INFO
,
2050 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2052 isakmp_pindex(&iph1
->index
, 0));
2056 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2060 // exit if there is another ph1 that is established (with a pending rekey timer)
2061 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2062 plog(ASL_LEVEL_INFO
,
2063 "Request for ISAKMP-SA rekey was ignored "
2064 "due to another established ph1.\n");
2068 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2069 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2070 plog(ASL_LEVEL_DEBUG
, "Reschedule Phase 1 rekey.\n");
2071 iph1
->sce_rekey
= sched_new(1,
2072 isakmp_ph1rekeyexpire_stub
,
2077 // get rmconf to initiate rekey with
2078 rmconf
= iph1
->rmconf
;
2080 rmconf
= getrmconf(iph1
->remote
);
2083 /* begin quick mode */
2084 plog(ASL_LEVEL_DEBUG
, "Begin Phase 1 rekey.\n");
2086 /* start phase 1 negotiation as a initiator. */
2087 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2088 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey Failed.\n");
2090 iph1
->is_rekey
= TRUE
;
2093 "Phase1 rekey failed: no configuration found for %s.\n",
2094 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2099 isakmp_ph1rekeyretry(iph1
)
2100 phase1_handle_t
*iph1
;
2103 struct remoteconf
*rmconf
;
2105 // this code path is meant for floated ph1 rekeys that are failing on the first message
2106 if (iph1
->sce
!= 0 ||
2107 iph1
->sce_rekey
!= 0 ||
2108 ((iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
&&
2109 iph1
->status
!= IKEV1_STATE_AGG_I_MSG1SENT
)
2110 || ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0))
2111 || (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
)
2112 || iph1
->is_dying
) {
2116 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2117 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2121 plog(ASL_LEVEL_INFO
,
2122 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2124 isakmp_pindex(&iph1
->index
, 0));
2128 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2129 plog(ASL_LEVEL_INFO
,
2130 "request for ISAKMP-SA rekey was ignored "
2131 "due to idleness.\n");
2135 // exit if there is another ph1 that is established (with a pending rekey timer)
2136 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2137 plog(ASL_LEVEL_INFO
,
2138 "request to retry ISAKMP-SA rekey was ignored "
2139 "due to another established ph1.\n");
2143 // some servers don't like respond to 4500 for rekeys... try accomodate them
2144 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2145 set_port(iph1
->local
, PORT_ISAKMP
);
2147 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2148 set_port(iph1
->remote
, PORT_ISAKMP
);
2150 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2151 rmconf
= getrmconf(iph1
->remote
);
2153 /* begin quick mode */
2154 plog(ASL_LEVEL_DEBUG
, "begin Phase 1 rekey retry.\n");
2156 /* start phase 1 negotiation as a initiator. */
2157 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2158 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey retry Failed.\n");
2161 iph1
->is_rekey
= TRUE
;
2164 "Phase 1 rekey retry failed: no configuration found for %s.\n",
2165 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2171 /* called from scheduler */
2173 isakmp_ph1delete_stub(p
)
2177 isakmp_ph1delete((phase1_handle_t
*)p
);
2181 isakmp_ph1delete(iph1
)
2182 phase1_handle_t
*iph1
;
2186 SCHED_KILL(iph1
->sce
);
2187 SCHED_KILL(iph1
->sce_rekey
);
2189 SCHED_KILL(iph1
->dpd_r_u
);
2192 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2193 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2197 isakmp_info_send_d1(iph1
);
2199 /* don't re-negosiation when the phase 1 SA expires. */
2201 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2202 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2206 plog(ASL_LEVEL_INFO
,
2207 "ISAKMP-SA deleted %s-%s spi:%s\n",
2208 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2212 ike_session_unlink_phase1(iph1
);
2217 /* called from scheduler.
2218 * this function will call only isakmp_ph2delete().
2219 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2220 * by something cause. That's why this function is called after phase 2 SA
2221 * expires in the userland.
2224 isakmp_ph2expire_stub(p
)
2228 isakmp_ph2expire((phase2_handle_t
*)p
);
2232 isakmp_ph2expire(iph2
)
2233 phase2_handle_t
*iph2
;
2237 if (FSM_STATE_IS_EXPIRED(iph2
->status
)) {
2241 SCHED_KILL(iph2
->sce
);
2243 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2244 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2248 plog(ASL_LEVEL_INFO
,
2249 "Phase 2 sa expired %s-%s\n", src
, dst
);
2253 // delete outgoing SAs
2254 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
) && iph2
->approval
) {
2257 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2259 pfkey_send_delete(lcconf
->sock_pfkey
,
2260 ipsecdoi2pfkey_proto(pr
->proto_id
),
2262 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2266 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2267 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
2268 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2273 /* called from scheduler */
2275 isakmp_ph2delete_stub(p
)
2279 isakmp_ph2delete((phase2_handle_t
*)p
);
2283 isakmp_ph2delete(iph2
)
2284 phase2_handle_t
*iph2
;
2288 SCHED_KILL(iph2
->sce
);
2290 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2291 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2295 plog(ASL_LEVEL_INFO
,
2296 "Phase 2 sa deleted %s-%s\n", src
, dst
);
2300 ike_session_unlink_phase2(iph2
);
2306 * Interface between PF_KEYv2 and ISAKMP
2309 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2310 * if phase1 has been finished, begin phase2.
2313 isakmp_post_acquire(phase2_handle_t
*iph2
)
2315 struct remoteconf
*rmconf
;
2316 phase1_handle_t
*iph1
= NULL
;
2318 plog(ASL_LEVEL_DEBUG
, "In post_acquire\n");
2320 /* search appropreate configuration with masking port. */
2321 rmconf
= getrmconf(iph2
->dst
);
2322 if (rmconf
== NULL
) {
2324 "No configuration found for %s.\n",
2325 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2328 /* if passive mode, ignore the acquire message */
2329 if (rmconf
->passive
) {
2330 plog(ASL_LEVEL_DEBUG
,
2331 "Because of passive mode, ignore the acquire message for %s.\n",
2332 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2337 // what if there is another ph2 that is negotiating
2338 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2339 // TODO: postpone this rekey for a second later
2340 plog(ASL_LEVEL_INFO
,
2341 "Request for establishing IPsec-SA was ignored due to another negoing ph2.\n");
2345 // if this is a phase2 rekeys (the policy may not have the current port number).
2346 // so, use the appropriate ports.
2347 if (iph2
->is_rekey
) {
2348 ike_session_update_ph2_ports(iph2
);
2350 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2351 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2353 /* no IKE-SA found. */
2355 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2357 /* start phase 1 negotiation as a initiator. */
2358 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2360 plog(ASL_LEVEL_INFO
,
2361 "IPsec-SA request for %s queued due to no Phase 1 found.\n",
2362 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2364 // exit if there is another ph1 that is established (with a pending rekey timer)
2365 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2366 plog(ASL_LEVEL_INFO
,
2367 "Request for Phase 1 was ignored due to another negotiating Phase 1.\n");
2371 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2372 plog(ASL_LEVEL_INFO
,
2373 "Request for Phase 1 failed. Will try later.\n");
2379 /* found ISAKMP-SA, but on negotiation. */
2380 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2381 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2382 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2383 plog(ASL_LEVEL_INFO
,
2384 "Request for establishing IPsec-SA was queued due to no phase1 found.\n");
2389 /* found established ISAKMP-SA */
2391 /* found ISAKMP-SA. */
2393 /* begin quick mode */
2394 plog(ASL_LEVEL_DEBUG
, "Begin QUICK mode.\n");
2395 if (ikev1_ph2begin_i(iph1
, iph2
))
2401 * receive GETSPI from kernel.
2404 isakmp_post_getspi(phase2_handle_t
*iph2
)
2407 struct timeval start
, end
;
2411 /* don't process it because there is no suitable phase1-sa. */
2412 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)) {
2414 "the negotiation is stopped, "
2415 "because there is no suitable ISAKMP-SA.\n");
2418 fsm_set_state(&iph2
->status
, iph2
->side
== INITIATOR
?
2419 IKEV1_STATE_QUICK_I_GETSPIDONE
: IKEV1_STATE_QUICK_R_GETSPIDONE
);
2422 gettimeofday(&start
, NULL
);
2424 switch (iph2
->side
) {
2426 error
= quick_i1send(iph2
, NULL
);
2429 error
= quick_r2send(iph2
, NULL
);
2433 if (error
) //%%%%%%%% log something ???
2436 gettimeofday(&end
, NULL
);
2437 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
2439 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2440 timedelta(&start
, &end
));
2446 /* called by scheduler */
2448 isakmp_chkph1there_stub(p
)
2451 isakmp_chkph1there((phase2_handle_t
*)p
);
2455 isakmp_chkph1there(iph2
)
2456 phase2_handle_t
*iph2
;
2458 phase1_handle_t
*iph1
;
2460 if ((iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
&& iph2
->status
!= IKEV1_STATE_QUICK_I_START
) ||
2462 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
);
2466 iph2
->retry_checkph1
--;
2468 if (iph2
->retry_checkph1
< 0 /* %%%%||
2469 ike_session_verify_ph2_parent_session(iph2) */) {
2470 if (iph2
->retry_checkph1
< 0) {
2472 "Phase 2 negotiation failed "
2473 "due to time up waiting for Phase 1. %s\n",
2474 sadbsecas2str(iph2
->dst
, iph2
->src
,
2475 iph2
->satype
, 0, 0));
2478 "Phase 2 negotiation failed "
2479 "due to invalid parent session. %s\n",
2480 sadbsecas2str(iph2
->dst
, iph2
->src
,
2481 iph2
->satype
, 0, 0));
2483 plog(ASL_LEVEL_INFO
,
2484 "delete Phase 2 handler.\n");
2486 /* send acquire to kernel as error */
2487 pk_sendeacquire(iph2
);
2488 ike_session_unlink_phase2(iph2
);
2492 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2494 /* XXX Even if ph1 as responder is there, should we not start
2495 * phase 2 negotiation ? */
2497 && FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2498 /* found isakmp-sa */
2500 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2501 plog(ASL_LEVEL_DEBUG
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2502 plog(ASL_LEVEL_DEBUG
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2503 plog(ASL_LEVEL_DEBUG
, "before:\n");
2504 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2505 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2506 set_port(iph2
->src
, extract_port(iph1
->local
));
2507 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2508 plog(ASL_LEVEL_DEBUG
, "After:\n");
2509 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2510 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2512 /* begin quick mode */
2513 if (ikev1_ph2begin_i(iph1
, iph2
)) {
2514 ike_session_unlink_phase2(iph2
);
2518 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2519 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2520 /* start phase 1 negotiation as a initiator. */
2522 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2523 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2525 } else if (rmconf
== NULL
) {
2526 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no remoteconf found... failed to initiate new one\n");
2530 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established ph1 handler found\n");
2532 /* no isakmp-sa found */
2533 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2538 /* copy variable data into ALLOCATED buffer. */
2540 isakmp_set_attr_v(buf
, type
, val
, len
)
2546 struct isakmp_data
*data
;
2548 data
= (struct isakmp_data
*)buf
;
2549 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2550 data
->lorv
= htons((u_int16_t
)len
);
2551 memcpy(data
+ 1, val
, len
);
2553 return buf
+ sizeof(*data
) + len
;
2556 /* copy fixed length data into ALLOCATED buffer. */
2558 isakmp_set_attr_l(buf
, type
, val
)
2563 struct isakmp_data
*data
;
2565 data
= (struct isakmp_data
*)buf
;
2566 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2567 data
->lorv
= htons((u_int16_t
)val
);
2569 return buf
+ sizeof(*data
);
2572 /* add a variable data attribute to the buffer by reallocating it. */
2574 isakmp_add_attr_v(buf0
, type
, val
, len
)
2580 vchar_t
*buf
= NULL
;
2581 struct isakmp_data
*data
;
2585 tlen
= sizeof(*data
) + len
;
2589 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2591 buf
= vmalloc(tlen
);
2594 "failed to get a attribute buffer.\n");
2598 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2599 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2600 data
->lorv
= htons((u_int16_t
)len
);
2601 memcpy(data
+ 1, val
, len
);
2606 /* add a fixed data attribute to the buffer by reallocating it. */
2608 isakmp_add_attr_l(buf0
, type
, val
)
2613 vchar_t
*buf
= NULL
;
2614 struct isakmp_data
*data
;
2618 tlen
= sizeof(*data
);
2622 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2624 buf
= vmalloc(tlen
);
2627 "failed to get a attribute buffer.\n");
2631 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2632 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2633 data
->lorv
= htons((u_int16_t
)val
);
2639 * calculate cookie and set.
2642 isakmp_newcookie(place
, remote
, local
)
2644 struct sockaddr_storage
*remote
;
2645 struct sockaddr_storage
*local
;
2647 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2657 if (remote
->ss_family
!= local
->ss_family
) {
2659 "address family mismatch, remote:%d local:%d\n",
2660 remote
->ss_family
, local
->ss_family
);
2663 switch (remote
->ss_family
) {
2665 alen
= sizeof(struct in_addr
);
2666 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2667 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2671 alen
= sizeof(struct in_addr
);
2672 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2673 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2678 "invalid family: %d\n", remote
->ss_family
);
2681 blen
= (alen
+ sizeof(u_short
)) * 2
2682 + sizeof(time_t) + lcconf
->secret_size
;
2683 buf
= vmalloc(blen
);
2686 "failed to get a cookie.\n");
2691 /* copy my address */
2692 memcpy(p
, sa1
, alen
);
2694 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2695 memcpy(p
, &port
, sizeof(u_short
));
2696 p
+= sizeof(u_short
);
2698 /* copy target address */
2699 memcpy(p
, sa2
, alen
);
2701 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2702 memcpy(p
, &port
, sizeof(u_short
));
2703 p
+= sizeof(u_short
);
2707 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2710 /* copy random value */
2711 buf2
= eay_set_random(lcconf
->secret_size
);
2714 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2715 p
+= lcconf
->secret_size
;
2718 buf2
= eay_sha1_one(buf
);
2719 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2721 sa1
= val2str(place
, sizeof (cookie_t
));
2722 plog(ASL_LEVEL_DEBUG
, "new cookie:\n%s\n", sa1
);
2735 * save partner's(payload) data into phhandle.
2738 isakmp_p2ph(buf
, gen
)
2740 struct isakmp_gen
*gen
;
2742 /* XXX to be checked in each functions for logging. */
2744 plog(ASL_LEVEL_WARNING
,
2745 "ignore this payload, same payload type exist.\n");
2749 if (ntohs(gen
->len
) < sizeof(*gen
)) {
2751 "ignore this payload, invalid payload len %d.\n",
2756 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2759 "failed to get buffer.\n");
2762 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2768 isakmp_newmsgid2(iph1
)
2769 phase1_handle_t
*iph1
;
2774 msgid2
= eay_random();
2775 } while (ike_session_getph2bymsgid(iph1
, msgid2
));
2781 * set values into allocated buffer of isakmp header for phase 1
2784 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2786 phase1_handle_t
*iph1
;
2792 struct isakmp
*isakmp
;
2794 if (vbuf
->l
< sizeof(*isakmp
))
2797 isakmp
= (struct isakmp
*)vbuf
->v
;
2799 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2800 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2801 isakmp
->np
= nptype
;
2802 isakmp
->v
= iph1
->version
;
2803 isakmp
->etype
= etype
;
2804 isakmp
->flags
= flags
;
2805 isakmp
->msgid
= msgid
;
2806 isakmp
->len
= htonl(vbuf
->l
);
2808 return vbuf
->v
+ sizeof(*isakmp
);
2812 * set values into allocated buffer of isakmp header for phase 1
2815 set_isakmp_header1(vbuf
, iph1
, nptype
)
2817 phase1_handle_t
*iph1
;
2820 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2824 * set values into allocated buffer of isakmp header for phase 2
2827 set_isakmp_header2(vbuf
, iph2
, nptype
)
2829 phase2_handle_t
*iph2
;
2832 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2836 * set values into allocated buffer of isakmp payload.
2839 set_isakmp_payload(buf
, src
, nptype
)
2844 struct isakmp_gen
*gen
;
2847 plog(ASL_LEVEL_DEBUG
, "add payload of len %zu, next type %d\n",
2850 gen
= (struct isakmp_gen
*)p
;
2852 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2854 memcpy(p
, src
->v
, src
->l
);
2861 #ifdef HAVE_PRINT_ISAKMP_C
2862 /* for print-isakmp.c */
2864 extern void isakmp_print(const u_char
*, u_int
, const u_char
*);
2866 char *getname(const u_char
*);
2868 char *getname6(const u_char
*);
2870 int safeputchar(int);
2873 * Return a name for the IP address pointed to by ap. This address
2874 * is assumed to be in network byte order.
2880 struct sockaddr_in addr
;
2881 static char ntop_buf
[NI_MAXHOST
];
2883 memset(&addr
, 0, sizeof(addr
));
2884 addr
.sin_len
= sizeof(struct sockaddr_in
);
2885 addr
.sin_family
= AF_INET
;
2886 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2887 if (getnameinfo(&addr
, sizeof(addr
),
2888 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2889 NI_NUMERICHOST
| niflags
))
2890 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2897 * Return a name for the IP6 address pointed to by ap. This address
2898 * is assumed to be in network byte order.
2904 struct sockaddr_in6 addr
;
2905 static char ntop_buf
[NI_MAXHOST
];
2907 memset(&addr
, 0, sizeof(addr
));
2908 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2909 addr
.sin6_family
= AF_INET6
;
2910 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2911 if (getnameinfo(&addr
, addr
.sin6_len
,
2912 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2913 NI_NUMERICHOST
| niflags
))
2914 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2926 ch
= (unsigned char)(c
& 0xff);
2927 if (c
< 0x80 && isprint(c
))
2928 return printf("%c", c
& 0xff);
2930 return printf("\\%03o", c
& 0xff);
2934 isakmp_printpacket(msg
, from
, my
, decoded
)
2936 struct sockaddr_storage
*from
;
2937 struct sockaddr_storage
*my
;
2943 char hostbuf
[NI_MAXHOST
];
2944 char portbuf
[NI_MAXSERV
];
2945 struct isakmp
*isakmp
;
2949 if (loglevel
< ASL_LEVEL_DEBUG
)
2953 plog(ASL_LEVEL_DEBUG
, "begin.\n");
2955 gettimeofday(&tv
, NULL
);
2956 s
= tv
.tv_sec
% 3600;
2957 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2960 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
2961 portbuf
, sizeof(portbuf
),
2962 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2963 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2964 strlcpy(portbuf
, "?", sizeof(portbuf
));
2966 printf("%s:%s", hostbuf
, portbuf
);
2971 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
2972 portbuf
, sizeof(portbuf
),
2973 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2974 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2975 strlcpy(portbuf
, "?", sizeof(portbuf
));
2977 printf("%s:%s", hostbuf
, portbuf
);
2984 printf("(malloc fail)\n");
2988 isakmp
= (struct isakmp
*)buf
->v
;
2989 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2992 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2993 if (buf
->l
< pad
&& 2 < vflag
)
2994 printf("(wrong padding)");
2996 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
3000 snapend
= buf
->v
+ buf
->l
;
3001 isakmp_print(buf
->v
, buf
->l
, NULL
);
3009 #endif /*HAVE_PRINT_ISAKMP_C*/
3012 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
3013 phase1_handle_t
*iph1
;
3014 struct remoteconf
*rmconf
;
3015 struct sockaddr_storage
*remote
, *local
;
3017 u_short
*port
= NULL
;
3019 /* address portion must be grabbed from real remote address "remote" */
3020 iph1
->remote
= dupsaddr(remote
);
3021 if (iph1
->remote
== NULL
) {
3022 ike_session_delph1(iph1
);
3027 * if remote has no port # (in case of initiator - from ACQUIRE msg)
3028 * - if remote.conf specifies port #, use that
3029 * - if remote.conf does not, use 500
3030 * if remote has port # (in case of responder - from recvfrom(2))
3031 * respect content of "remote".
3033 switch (iph1
->remote
->ss_family
) {
3035 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3038 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3041 *port
= htons(PORT_ISAKMP
);
3045 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3048 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3051 *port
= htons(PORT_ISAKMP
);
3056 "invalid family: %d\n", iph1
->remote
->ss_family
);
3057 ike_session_delph1(iph1
);
3062 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3064 iph1
->local
= dupsaddr(local
);
3065 if (iph1
->local
== NULL
) {
3066 ike_session_delph1(iph1
);
3070 switch (iph1
->local
->ss_family
) {
3072 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3075 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3078 *port
= getmyaddrsport(iph1
->local
);
3082 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3085 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3088 *port
= getmyaddrsport(iph1
->local
);
3093 "invalid family: %d\n", iph1
->local
->ss_family
);
3094 ike_session_delph1(iph1
);
3098 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3099 plog (ASL_LEVEL_DEBUG
, "Marking ports as changed\n");
3100 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3108 log_ph1established(iph1
)
3109 const phase1_handle_t
*iph1
;
3113 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3114 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3118 plog(ASL_LEVEL_INFO
,
3119 "ISAKMP-SA established %s-%s spi:%s\n",
3121 isakmp_pindex(&iph1
->index
, 0));
3126 IPSECLOGASLMSG("IPSec Phase 1 established (Initiated by %s).\n",
3127 (iph1
->side
== INITIATOR
)? "me" : "peer");
3132 struct payload_list
*
3133 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3136 plist
= racoon_malloc (sizeof (struct payload_list
));
3140 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3141 plist
->next
->prev
= plist
;
3142 plist
= plist
->next
;
3146 plist
->payload
= payload
;
3147 plist
->payload_type
= payload_type
;
3153 isakmp_plist_set_all (struct payload_list
**plist
, phase1_handle_t
*iph1
)
3155 struct payload_list
*ptr
= *plist
, *first
;
3156 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3157 vchar_t
*buf
= NULL
;
3160 /* Seek to the first item. */
3161 while (ptr
->prev
) ptr
= ptr
->prev
;
3164 /* Compute the whole length. */
3166 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3170 buf
= vmalloc(tlen
);
3173 "failed to get buffer to send.\n");
3179 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3185 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3188 racoon_free (first
);
3189 /* ptr->prev = NULL; first = NULL; ... omitted. */
3204 frag_handler(iph1
, msg
, remote
, local
)
3205 phase1_handle_t
*iph1
;
3207 struct sockaddr_storage
*remote
;
3208 struct sockaddr_storage
*local
;
3212 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3213 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3215 "Packet reassembly failed\n");
3219 /* simply reply if the packet was processed. */
3220 if (ike_session_check_recvdpkt(remote
, local
, newmsg
) > 0) {
3221 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3222 saddr2str((struct sockaddr
*)remote
));
3224 plog(ASL_LEVEL_NOTICE
,
3225 "the reassembled packet is retransmitted by %s.\n",
3226 saddr2str((struct sockaddr
*)remote
));
3231 isakmp_main(newmsg
, remote
, local
);
3241 phase1_handle_t
*iph1
;
3243 vchar_t
*buf
= NULL
;
3244 struct sadb_msg
*msg
, *next
, *end
;
3246 struct sockaddr_storage
*src
, *dst
;
3247 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3249 phase2_handle_t
*iph2
;
3250 phase1_handle_t
*new_iph1
;
3252 plog(ASL_LEVEL_INFO
,
3253 "purging ISAKMP-SA spi=%s.\n",
3254 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3256 /* Mark as expired. */
3257 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
3259 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3262 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3263 * Keep all others phase2 SAs.
3265 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3267 plog(ASL_LEVEL_DEBUG
,
3268 "pfkey_dump_sadb returned nothing.\n");
3272 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3273 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3276 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3278 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3279 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3284 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3286 "pfkey_check (%s)\n", ipsec_strerror());
3291 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3293 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3294 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3298 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3299 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3301 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3302 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3303 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3309 * check in/outbound SAs.
3310 * Select only SAs where src == local and dst == remote (outgoing)
3311 * or src == remote and dst == local (incoming).
3313 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3314 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3319 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3320 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3322 /* Check if there is another valid ISAKMP-SA */
3323 if (new_iph1
!= NULL
) {
3326 /* No handler... still send a pfkey_delete message, but log this !*/
3327 plog(ASL_LEVEL_INFO
,
3328 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3329 ntohl(sa
->sadb_sa_spi
));
3333 * If we have a new ph1, do not purge IPsec-SAs binded
3334 * to a different ISAKMP-SA
3336 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3341 /* If the ph2handle is established, do not purge IPsec-SA */
3342 if (FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(iph2
->status
)) {
3344 plog(ASL_LEVEL_INFO
,
3345 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3346 ntohl(sa
->sadb_sa_spi
),
3347 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3355 pfkey_send_delete(lcconf
->sock_pfkey
,
3356 msg
->sadb_msg_satype
,
3358 src
, dst
, sa
->sadb_sa_spi
);
3360 /* delete a relative phase 2 handle. */
3363 ike_session_unlink_phase2(iph2
);
3366 plog(ASL_LEVEL_INFO
,
3367 "purged IPsec-SA spi=%u.\n",
3368 ntohl(sa
->sadb_sa_spi
));
3376 /* Mark the phase1 handler as EXPIRED */
3377 plog(ASL_LEVEL_INFO
,
3378 "purged ISAKMP-SA spi=%s.\n",
3379 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3381 SCHED_KILL(iph1
->sce
);
3383 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3388 phase2_handle_t
*iph2
;
3393 /* Delete the SPD entry if we generated it
3395 if (iph2
->generated_spidx
) {
3397 u_int64_t force_align
; // Wcast-align fix - force alignment
3398 struct policyindex spidx
;
3400 struct sockaddr_storage addr
;
3402 struct sockaddr_storage
*src
= iph2
->src
;
3403 struct sockaddr_storage
*dst
= iph2
->dst
;
3405 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3407 plog(ASL_LEVEL_INFO
,
3408 "generated policy, deleting it.\n");
3410 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
3411 iph2
->spidx_gen
= &u
.spidx
;
3413 /* make inbound policy */
3416 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
3417 u
.spidx
.ul_proto
= 0;
3420 * Note: code from get_proposal_r
3423 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
3426 * make destination address in spidx from either ID payload
3427 * or phase 1 address into a address in spidx.
3429 if (iph2
->id
!= NULL
3430 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3431 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3432 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3433 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3434 /* get a destination address of a policy */
3435 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
3436 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
, iph2
->version
);
3442 * get scopeid from the SA address.
3443 * note that the phase 1 source address is used as
3444 * a destination address to search for a inbound
3445 * policy entry because rcoon is responder.
3447 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3449 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
3454 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3455 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3456 idi2type
= _XIDT(iph2
->id
);
3460 plog(ASL_LEVEL_DEBUG
,
3461 "Get a destination address of SP index "
3462 "from Phase 1 address "
3463 "due to no ID payloads found "
3464 "OR because ID type is not address.\n");
3467 * copy the SOURCE address of IKE into the
3468 * DESTINATION address of the key to search the
3469 * SPD because the direction of policy is inbound.
3471 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
3472 switch (u
.spidx
.dst
.ss_family
) {
3475 sizeof(struct in_addr
) << 3;
3480 sizeof(struct in6_addr
) << 3;
3489 /* make source address in spidx */
3490 if (iph2
->id_p
!= NULL
3491 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3492 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3493 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3494 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3495 /* get a source address of inbound SA */
3496 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
3497 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
, iph2
->version
);
3503 * get scopeid from the SA address.
3504 * for more detail, see above of this function.
3506 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3508 setscopeid(&u
.spidx
.src
, iph2
->dst
);
3514 /* make id[src,dst] if both ID types are IP address and same */
3515 if (_XIDT(iph2
->id_p
) == idi2type
3516 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
3518 dupsaddr(&u
.spidx
.dst
);
3520 dupsaddr(&u
.spidx
.src
);
3524 plog(ASL_LEVEL_DEBUG
,
3525 "Get a source address of SP index "
3526 "from Phase 1 address "
3527 "due to no ID payloads found "
3528 "OR because ID type is not address.\n");
3530 /* see above comment. */
3531 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
3532 switch (u
.spidx
.src
.ss_family
) {
3535 sizeof(struct in_addr
) << 3;
3540 sizeof(struct in6_addr
) << 3;
3551 plog(ASL_LEVEL_DEBUG
,
3552 "get a src address from ID payload "
3553 "%s prefixlen=%u ul_proto=%u\n",
3554 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
3555 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3556 plog(ASL_LEVEL_DEBUG
,
3557 "get dst address from ID payload "
3558 "%s prefixlen=%u ul_proto=%u\n",
3559 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
3560 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
3563 * convert the ul_proto if it is 0
3564 * because 0 in ID payload means a wild card.
3566 if (u
.spidx
.ul_proto
== 0)
3567 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3571 /* End of code from get_proposal_r
3574 if (pk_sendspddelete(iph2
) < 0) {
3576 "pfkey spddelete(inbound) failed.\n");
3578 plog(ASL_LEVEL_DEBUG
,
3579 "pfkey spddelete(inbound) sent.\n");
3582 #ifdef HAVE_POLICY_FWD
3583 /* make forward policy if required */
3584 if (tunnel_mode_prop(iph2
->approval
)) {
3585 u
.spidx
.dir
= IPSEC_DIR_FWD
;
3586 if (pk_sendspddelete(iph2
) < 0) {
3588 "pfkey spddelete(forward) failed.\n");
3590 plog(ASL_LEVEL_DEBUG
,
3591 "pfkey spddelete(forward) sent.\n");
3596 /* make outbound policy */
3599 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3601 u
.spidx
.src
= u
.spidx
.dst
;
3603 pref
= u
.spidx
.prefs
;
3604 u
.spidx
.prefs
= u
.spidx
.prefd
;
3605 u
.spidx
.prefd
= pref
;
3607 if (pk_sendspddelete(iph2
) < 0) {
3609 "pfkey spddelete(outbound) failed.\n");
3611 plog(ASL_LEVEL_DEBUG
,
3612 "pfkey spddelete(outbound) sent.\n");
3615 iph2
->spidx_gen
=NULL
;
3621 setscopeid(sp_addr0
, sa_addr0
)
3622 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
3624 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3626 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3627 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3629 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3630 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3631 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3634 /* this check should not be here ? */
3635 if (sa_addr
->sin6_family
!= AF_INET6
) {
3637 "can't get scope ID: family mismatch\n");
3641 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3643 "scope ID is not supported except of lladdr.\n");
3647 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
3654 isakmp_plist_append_initial_contact (iph1
, plist
)
3655 phase1_handle_t
*iph1
;
3656 struct payload_list
*plist
;
3658 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
3659 vchar_t
*notp_ini
= NULL
;
3660 struct isakmp_pl_n np
, *nptr
;
3663 np
.doi
= htonl(iph1
->rmconf
->doitype
);
3664 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
3665 np
.spi_size
= sizeof(isakmp_index
);
3666 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
3667 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
3668 + sizeof(isakmp_index
)))) {
3670 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
3671 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
3672 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
3673 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
3674 plog(ASL_LEVEL_DEBUG
,
3675 "added initial-contact payload.\n");
3677 /* insert a node into contacted list. */
3678 if (ike_session_inscontacted(iph1
->remote
) == -1) {
3680 "failed to add contacted list.\n");
3686 "failed to allocate notification payload.\n");
3690 plog(ASL_LEVEL_DEBUG
, "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
3691 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, ike_session_getcontacted(iph1
->remote
)? 1:0);