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"
132 extern caddr_t
val2str (const char *, size_t);
133 u_char i_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the i_ck. */
134 u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
137 static void isakmp_main (vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
138 static void ikev1_received_packet(vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
139 static int ikev1_ph1begin_r (ike_session_t
*session
, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*, u_int8_t
);
140 static int ikev1_ph2begin_i (phase1_handle_t
*, phase2_handle_t
*);
141 static int ikev1_ph2begin_r (phase1_handle_t
*, vchar_t
*);
145 static void frag_handler (phase1_handle_t
*, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
149 * isakmp packet handler
152 isakmp_handler(int so_isakmp
)
154 struct isakmp isakmp
;
156 u_int64_t force_align
; // Wcast-align fix - force alignment
157 char buf
[sizeof (isakmp
) + 4];
158 u_int32_t non_esp
[2];
159 char lbuf
[sizeof(struct udphdr
) +
163 struct sockaddr_storage remote
;
164 struct sockaddr_storage local
;
165 unsigned int remote_len
= sizeof(remote
);
166 unsigned int local_len
= sizeof(local
);
170 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
173 if (slept_at
|| woke_at
) {
174 plog(ASL_LEVEL_DEBUG
,
175 "ignoring isakmp port until power-mgmt event is handled.\n");
179 /* read message by MSG_PEEK */
180 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
181 MSG_PEEK
, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
185 "failed to receive isakmp packet: %s\n",
190 /* keep-alive packet - ignore */
191 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
192 /* Pull the keep-alive packet */
193 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
194 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
196 "failed to receive keep alive packet: %s\n",
203 /* we don't know about portchange yet,
204 look for non-esp marker instead */
205 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
206 extralen
= NON_ESP_MARKER_LEN
;
208 /* now we know if there is an extra non-esp
209 marker at the beginning or not */
210 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
212 /* check isakmp header length, as well as sanity of header length */
213 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
215 "packet shorter than isakmp header size (size: %zu, minimum expected: %zu)\n", len
, sizeof(isakmp
));
217 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
218 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
220 "failed to receive isakmp packet: %s\n",
226 /* reject it if the size is tooooo big. */
227 if (ntohl(isakmp
.len
) > 0xffff) {
229 "the length in the isakmp header is too big.\n");
230 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
231 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
233 "failed to receive isakmp packet: %s\n",
239 /* read real message */
240 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
242 "failed to allocate reading buffer (%u Bytes)\n",
243 ntohl(isakmp
.len
) + extralen
);
245 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
246 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
248 "failed to receive isakmp packet: %s\n",
250 error
= -2; /* serious problem with socket */
255 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
256 0, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
260 "failed to receive isakmp packet: %s\n",
265 if (len
< extralen
) {
267 "invalid len (%zd Bytes) & extralen (%d Bytes)\n",
272 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
274 "failed to allocate reading buffer (%lu Bytes)\n",
279 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
284 plog(ASL_LEVEL_ERR
, "received invalid length (%zd != %zu), why ?\n",
289 plog(ASL_LEVEL_DEBUG
, "%zd bytes message received %s\n",
290 len
, saddr2str_fromto("from %s to %s",
291 (struct sockaddr
*)&remote
,
292 (struct sockaddr
*)&local
));
294 /* avoid packets with malicious port/address */
295 switch (remote
.ss_family
) {
297 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
301 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
306 "invalid family: %d\n", remote
.ss_family
);
311 "src port == 0 (valid as UDP but not with IKE)\n");
315 /* XXX: check sender whether to be allowed or not to accept */
317 /* XXX: I don't know how to check isakmp half connection attack. */
319 /* simply reply if the packet was processed. */
321 if (ike_session_check_recvdpkt(&remote
, &local
, buf
)) {
322 IPSECLOGASLMSG("Received retransmitted packet from %s.\n",
323 saddr2str((struct sockaddr
*)&remote
));
325 plog(ASL_LEVEL_NOTICE
,
326 "the packet is retransmitted by %s.\n",
327 saddr2str((struct sockaddr
*)&remote
));
332 /* isakmp main routine */
333 isakmp_main(buf
, &remote
, &local
);
345 * main processing to handle isakmp payload
348 isakmp_main(vchar_t
*msg
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
)
350 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
351 u_int8_t isakmp_version
= isakmp
->v
;
353 #ifdef HAVE_PRINT_ISAKMP_C
354 isakmp_printpacket(msg
, remote
, local
, 0);
357 /* the initiator's cookie must not be zero */
358 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
360 "malformed cookie received.\n");
364 /* Check the Major and Minor Version fields. */
366 * XXX Is is right to check version here ?
367 * I think it may no be here because the version depends
368 * on exchange status.
370 if (ISAKMP_GETMAJORV(isakmp_version
) != ISAKMP_MAJOR_VERSION_IKEV1
&&
371 ISAKMP_GETMAJORV(isakmp_version
) != ISAKMP_MAJOR_VERSION_IKEV2
) {
372 plog(ASL_LEVEL_ERR
, "invalid major version %d.\n", isakmp_version
);
377 #if ISAKMP_MINOR_VERSION > 0 //%%%%%%%% fix this
378 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) { //%%%%%%%%%%%%%%% ??????
380 "invalid minor version %d.\n",
381 ISAKMP_GETMINORV(isakmp
->v
));
387 if (isakmp_version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
388 /* check the Flags field. */
389 /* XXX How is the exclusive check, E and A ? */
390 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) { //%%%%%%%%%%%% any other flags for IKEv2 ?????
391 plog(ASL_LEVEL_ERR
, "invalid flag 0x%02x.\n", isakmp
->flags
);
395 /* ignore commit bit. */
396 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
397 if (isakmp
->msgid
== 0) { //%%%%% does this apply to V2
398 isakmp_info_send_nx(isakmp
, remote
, local
,
399 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
400 plog(ASL_LEVEL_ERR
, "Commit bit on Phase 1 forbidden.\n");
405 ikev1_received_packet(msg
, local
, remote
);
411 * ikev1_received_packet
412 * Handler for received IKEv1 Packets
415 ikev1_received_packet(vchar_t
*msg
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
417 ike_session_t
*session
;
418 phase1_handle_t
*iph1
;
420 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
421 isakmp_index
*index
= (isakmp_index
*)isakmp
;
423 session
= ike_session_get_session(local
, remote
, 1);
425 plog (ASL_LEVEL_INFO
, "failed to allocate or find ike session.\n");
429 iph1
= ike_session_getph1byindex(session
, index
);
432 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
433 iph1
->side
== INITIATOR
) {
434 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
435 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
436 CONSTSTR("Malformed or unexpected cookie"),
437 CONSTSTR("Failed to process packet (malformed/unexpected cookie)"));
438 plog(ASL_LEVEL_DEBUG
,
439 "Malformed cookie received or "
440 "the initiator's cookies collide.\n");
445 /* Floating ports for NAT-T */
446 if (NATT_AVAILABLE(iph1
) &&
447 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
448 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
449 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
450 { //%%%%%%%%%%%%%%%%%%%% make this a separate function - ikev2 needs it
451 /* prevent memory leak */
452 racoon_free(iph1
->remote
);
453 racoon_free(iph1
->local
);
457 /* copy-in new addresses */
458 iph1
->remote
= dupsaddr(remote
);
459 if (iph1
->remote
== NULL
) {
460 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
461 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
462 CONSTSTR("Failed to duplicate remote address"),
463 CONSTSTR("Failed to process Phase 1 message (can't duplicate remote address"));
465 "Phase 1 failed: dupsaddr failed.\n");
468 iph1
->local
= dupsaddr(local
);
469 if (iph1
->local
== NULL
) {
470 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
471 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
472 CONSTSTR("Failed to duplicate local address"),
473 CONSTSTR("Failed to process Phase 1 message (can't duplicate local address"));
475 "Phase 1 failed: dupsaddr failed.\n");
479 /* set the flag to prevent further port floating
480 (FIXME: should we allow it? E.g. when the NAT gw
482 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
484 /* print some neat info */
485 plog (ASL_LEVEL_INFO
,
486 "NAT-T: ports changed to: %s\n",
487 saddr2str_fromto("%s<->%s", (struct sockaddr
*)iph1
->remote
, (struct sockaddr
*)iph1
->local
));
490 /* must be same addresses in one stream of a phase at least. */
491 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
492 char *saddr_db
, *saddr_act
;
494 saddr_db
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
495 saddr_act
= racoon_strdup(saddr2str((struct sockaddr
*)remote
));
496 STRDUP_FATAL(saddr_db
);
497 STRDUP_FATAL(saddr_act
);
499 plog(ASL_LEVEL_WARNING
,
500 "Remote address mismatched. db=%s, act=%s\n",
501 saddr_db
, saddr_act
);
503 racoon_free(saddr_db
);
504 racoon_free(saddr_act
);
508 * don't check of exchange type here because other type will have
509 * the same index, for example, informational exchange.
512 // received ike packets: update dpd checks
513 isakmp_reschedule_info_monitor_if_pending(iph1
, "IKE packet received from peer");
518 // Check exchange type and process accordingly
520 switch (isakmp
->etype
) {
522 case ISAKMP_ETYPE_IDENT
:
523 case ISAKMP_ETYPE_AGG
:
525 /* phase 1 validity check */
526 if (isakmp
->msgid
!= 0) {
527 plog(ASL_LEVEL_ERR
, "Message id should be zero in Phase 1.\n");
531 /* search for isakmp status record of phase 1 */
534 * the packet must be the 1st message from a initiator
535 * or the 2nd message from the responder.
538 /* search for phase1 handle by index without r_ck */
539 iph1
= ike_session_getph1byindex0(session
, index
);
541 /*it must be the 1st message from a initiator.*/
542 if (memcmp(&isakmp
->r_ck
, r_ck0
,
543 sizeof(cookie_t
)) != 0) {
545 plog(ASL_LEVEL_DEBUG
, "Malformed cookie received "
546 "or the spi expired.\n");
550 /* Initiation of new exchange */
551 ikev1_ph1begin_r(session
, msg
, remote
, local
, isakmp
->etype
);
557 * Don't delete phase 1 handler for mismatch
558 * because of no authentication has been completed.
560 if (iph1
->etype
!= isakmp
->etype
) {
561 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
562 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
563 CONSTSTR("Mismatched exchange type"),
564 CONSTSTR("Failed to process Phase 1 message (mismatched exchange type)"));
566 "Exchange type is mismatched: "
567 "db=%s packet=%s, ignore it.\n",
568 s_isakmp_etype(iph1
->etype
),
569 s_isakmp_etype(isakmp
->etype
));
573 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
574 frag_handler(iph1
, msg
, remote
, local
);
577 fsm_ikev1_phase1_process_payloads(iph1
, msg
);
581 case ISAKMP_ETYPE_INFO
:
582 case ISAKMP_ETYPE_ACKINFO
:
585 * iph1 must be present for Information message.
586 * if iph1 is null then trying to get the phase1 status
587 * as the packet from responder againt initiator's 1st
588 * exchange in phase 1.
589 * NOTE: We think such informational exchange should be ignored.
592 iph1
= ike_session_getph1byindex0(session
, index
);
594 plog(ASL_LEVEL_ERR
, "Unknown Informational exchange received.\n");
597 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
598 plog(ASL_LEVEL_WARNING
,
599 "Remote address mismatched. "
601 saddr2str((struct sockaddr
*)iph1
->remote
));
604 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
605 return frag_handler(iph1
, msg
, remote
, local
);
607 if (isakmp_info_recv(iph1
, msg
) < 0)
612 case ISAKMP_ETYPE_QUICK
:
614 u_int32_t msgid
= isakmp
->msgid
;
615 phase2_handle_t
*iph2
;
618 isakmp_info_send_nx(isakmp
, remote
, local
,
619 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
620 plog(ASL_LEVEL_ERR
, "Can't start the quick mode, "
621 "there is no ISAKMP-SA, %s\n", isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
626 /* Reinit the IVM if it's still there */
627 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
628 oakley_delivm(iph1
->mode_cfg
->ivm
);
629 iph1
->mode_cfg
->ivm
= NULL
;
632 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
633 frag_handler(iph1
, msg
, remote
, local
);
637 /* check status of phase 1 whether negotiated or not. */
638 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
639 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
640 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
641 CONSTSTR("Can't start Phase 2 without valid Phase 1"),
642 CONSTSTR("Failed to start Phase 2 responder (no established Phase 1"));
643 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
644 "there is no valid ISAKMP-SA, %s\n", isakmp_pindex(&iph1
->index
, iph1
->msgid
));
648 /* search isakmp phase 2 stauts record. */
649 iph2
= ike_session_getph2bymsgid(iph1
, msgid
);
651 /* it must be new negotiation as responder */
652 ikev1_ph2begin_r(iph1
, msg
);
658 * we keep to set commit bit during negotiation.
659 * When SA is configured, bit will be reset.
661 * don't initiate commit bit. should be fixed in the future.
663 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
664 iph2
->flags
|= ISAKMP_FLAG_C
;
666 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
) &&
667 (iph2
->ph1
== NULL
|| iph2
->ph1
->approval
== NULL
)) {
668 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
669 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
670 CONSTSTR("Can't continue Phase 2 without valid Phase 1"),
671 CONSTSTR("Failed to continue Phase 2 resonder (invalid linked Phase 1"));
672 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
673 "invalid linked ISAKMP-SA\n");
676 fsm_ikev1_phase2_process_payloads(iph2
, msg
);
680 case ISAKMP_ETYPE_CFG
:
684 "mode config %d from %s, "
685 "but we have no ISAKMP-SA.\n",
686 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
689 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
691 "mode config %d from %s, "
692 "but ISAKMP-SA %s isn't established.\n",
693 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
),
694 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
697 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
698 return frag_handler(iph1
, msg
, remote
, local
);
699 isakmp_cfg_r(iph1
, msg
);
703 case ISAKMP_ETYPE_NEWGRP
:
704 case ISAKMP_ETYPE_AUTH
:
705 case ISAKMP_ETYPE_NONE
:
708 "Invalid exchange type %d from %s.\n",
709 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
714 /* new negotiation of phase 1 for initiator */
716 ikev1_ph1begin_i(ike_session_t
*session
, struct remoteconf
*rmconf
, struct sockaddr_storage
*remote
,
717 struct sockaddr_storage
*local
, int started_by_api
)
720 phase1_handle_t
*iph1
;
722 struct timeval start
, end
;
725 if (session
== NULL
) {
726 session
= ike_session_get_session(local
, remote
, 1);
728 plog (ASL_LEVEL_INFO
, "failed to allocate or find ike session.\n");
733 /* get new entry to isakmp status table. */
734 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
738 iph1
->rmconf
= rmconf
;
739 retain_rmconf(iph1
->rmconf
);
740 iph1
->side
= INITIATOR
;
741 iph1
->started_by_api
= started_by_api
;
742 iph1
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
748 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
749 /* don't call remph1(iph1) until after insph1(iph1) is called */
750 ike_session_delph1(iph1
);
755 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
759 iph1
->frag_chain
= NULL
;
760 iph1
->approval
= NULL
;
762 /* XXX copy remote address */
763 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
764 /* don't call remph1(iph1) until after insph1(iph1) is called */
765 iph1
= NULL
; /* deleted in copy_ph1addresses */
769 if (ike_session_link_phase1(session
, iph1
) != 0) {
770 plog(ASL_LEVEL_DEBUG
, "Failed to link ph1 to session\n");
771 ike_session_delph1(iph1
);
774 // HACK!!! to track rekeys across SIGHUPs
775 if (started_by_api
== VPN_RESTARTED_BY_API
&&
777 iph1
->parent_session
->established
= 1;
778 iph1
->parent_session
->ikev1_state
.ph2cnt
++;
782 /* start phase 1 exchange */
783 iph1
->etype
= rmconf
->etypes
->type
;
784 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
785 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_START
);
786 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
787 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_I_START
);
791 plog(ASL_LEVEL_DEBUG
, "===\n");
795 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
799 "initiate new phase 1 negotiation: %s<=>%s\n",
800 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
805 s_isakmp_etype(iph1
->etype
));
808 gettimeofday(&iph1
->start
, NULL
);
809 gettimeofday(&start
, NULL
);
812 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by me).\n");
814 if (fsm_ikev1_phase1_send_response(iph1
, NULL
)) {
815 ike_session_unlink_phase1(iph1
);
820 gettimeofday(&end
, NULL
);
821 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
823 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
824 timedelta(&start
, &end
));
827 #ifdef ENABLE_VPNCONTROL_PORT
828 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
834 /* new negotiation of phase 1 for responder */
836 ikev1_ph1begin_r(ike_session_t
*session
, vchar_t
*msg
, struct sockaddr_storage
*remote
,
837 struct sockaddr_storage
*local
, u_int8_t etype
)
840 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
841 struct remoteconf
*rmconf
;
842 phase1_handle_t
*iph1
;
843 struct etypes
*etypeok
;
845 struct timeval start
, end
;
848 /* look for my configuration */
849 rmconf
= getrmconf(remote
);
850 if (rmconf
== NULL
) {
857 /* check to be acceptable exchange type */
858 etypeok
= check_etypeok(rmconf
, etype
);
859 if (etypeok
== NULL
) {
861 "not acceptable %s mode\n", s_isakmp_etype(etype
));
865 /* get new entry to isakmp status table. */
866 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
870 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
871 iph1
->rmconf
= rmconf
;
872 retain_rmconf(iph1
->rmconf
);
874 iph1
->side
= RESPONDER
;
875 iph1
->started_by_api
= 0;
876 iph1
->etype
= etypeok
->type
;
877 iph1
->version
= isakmp
->v
;
880 if (iph1
->etype
== ISAKMP_ETYPE_IDENT
)
881 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_START
);
882 else if (iph1
->etype
== ISAKMP_ETYPE_AGG
)
883 fsm_set_state(&iph1
->status
, IKEV1_STATE_AGG_R_START
);
889 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
890 /* don't call remph1(iph1) until after insph1(iph1) is called */
891 ike_session_delph1(iph1
);
897 iph1
->frag_chain
= NULL
;
898 iph1
->approval
= NULL
;
900 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
901 * We have to setup this flag now to correctly generate the first reply.
902 * Don't know if a better check could be done for that ?
904 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
905 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
907 /* copy remote address */
908 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
909 /* don't call remph1(iph1) until after insph1(iph1) is called */
910 iph1
= NULL
; /* deleted in copy_ph1addresses */
914 if (ike_session_link_phase1(session
, iph1
) != 0) {
915 ike_session_delph1(iph1
);
919 plog(ASL_LEVEL_DEBUG
, "===\n");
923 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
927 "respond new phase 1 negotiation: %s<=>%s\n",
928 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
932 "begin %s mode.\n", s_isakmp_etype(etype
));
935 gettimeofday(&iph1
->start
, NULL
);
936 gettimeofday(&start
, NULL
);
939 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by peer).\n");
941 /* now that we have a phase1 handle, feed back into our
942 * main receive function to catch fragmented packets
944 isakmp_main(msg
, remote
, local
);
948 /* new negotiation of phase 2 for initiator */
950 ikev1_ph2begin_i(phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
954 if (xauth_check(iph1
) != 0) {
956 "Attempt to start phase 2 whereas Xauth failed\n");
961 /* found ISAKMP-SA. */
962 plog(ASL_LEVEL_DEBUG
, "===\n");
963 plog(ASL_LEVEL_DEBUG
, "begin QUICK mode.\n");
966 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
970 "initiate new phase 2 negotiation: %s<=>%s\n",
971 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
976 gettimeofday(&iph2
->start
, NULL
);
980 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
982 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by me).\n");
983 if (quick_iprep(iph2
, NULL
))
986 #ifdef ENABLE_VPNCONTROL_PORT
987 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
993 /* new negotiation of phase 2 for responder */
995 ikev1_ph2begin_r(phase1_handle_t
*iph1
, vchar_t
*msg
)
997 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
998 phase2_handle_t
*iph2
= 0;
1001 struct timeval start
, end
;
1003 #ifdef ENABLE_HYBRID
1004 if (xauth_check(iph1
) != 0) {
1006 "Attempt to start Phase 2 whereas Xauth failed\n");
1011 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_SA
);
1014 "failed to allocate Phase 2 entry.\n");
1018 iph2
->side
= RESPONDER
;
1019 iph2
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
1020 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_START
);
1021 iph2
->flags
= isakmp
->flags
;
1022 iph2
->msgid
= isakmp
->msgid
;
1023 iph2
->seq
= pk_getseq();
1024 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1025 if (iph2
->ivm
== NULL
) {
1026 ike_session_delph2(iph2
);
1029 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1030 if (iph2
->dst
== NULL
) {
1031 ike_session_delph2(iph2
);
1034 switch (iph2
->dst
->ss_family
) {
1037 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1043 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1049 "invalid family: %d\n", iph2
->dst
->ss_family
);
1050 ike_session_delph2(iph2
);
1054 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1055 if (iph2
->src
== NULL
) {
1056 ike_session_delph2(iph2
);
1059 switch (iph2
->src
->ss_family
) {
1062 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1068 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1074 "invalid family: %d\n", iph2
->src
->ss_family
);
1075 ike_session_delph2(iph2
);
1079 if (ike_session_link_ph2_to_ph1(iph1
, iph2
))
1083 plog(ASL_LEVEL_DEBUG
, "===\n");
1087 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1090 plog(ASL_LEVEL_INFO
,
1091 "respond new phase 2 negotiation: %s<=>%s\n",
1092 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1097 gettimeofday(&start
, NULL
);
1100 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by peer).\n");
1102 error
= fsm_ikev1_phase2_process_payloads(iph2
, msg
);
1106 #ifdef ENABLE_VPNCONTROL_PORT
1107 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1114 ikev1_phase1_established(phase1_handle_t
*iph1
)
1117 u_int rekey_lifetime
;
1118 int ini_contact
= iph1
->rmconf
->ini_contact
;
1121 gettimeofday(&iph1
->end
, NULL
);
1122 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1123 "Phase 1", s_isakmp_etype(iph1
->etype
),
1124 timedelta(&iph1
->start
, &iph1
->end
));
1127 #ifdef ENABLE_VPNCONTROL_PORT
1129 if (iph1
->side
== RESPONDER
&&
1130 iph1
->local
->ss_family
== AF_INET
) {
1132 struct redirect
*addr
;
1134 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
1135 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
1136 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
1138 if (raddr
== NULL
) {
1140 "failed to send redirect message - memory error.\n");
1142 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
1143 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
1144 plog(ASL_LEVEL_DEBUG
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
1147 (void)ike_session_update_ph1_ph2tree(iph1
);
1148 isakmp_ph1expire(iph1
);
1156 /* save created date. */
1157 (void)time(&iph1
->created
);
1159 /* add to the schedule to expire, and save back pointer. */
1160 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
1161 isakmp_ph1expire_stub
, iph1
);
1163 if (iph1
->rmconf
->initiate_ph1rekey
) {
1164 if (iph1
->side
== INITIATOR
) {
1165 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
1169 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
1173 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
1174 iph1
->approval
->lifetime
);
1175 if (rekey_lifetime
) {
1176 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
1177 isakmp_ph1rekeyexpire_stub
,
1180 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
1184 "failed to get rekey timer - lifetime is too small... probably.\n");
1188 #ifdef ENABLE_HYBRID
1189 /* ignore xauth if it is a rekey */
1190 if (!iph1
->is_rekey
&&
1191 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1192 switch(AUTHMETHOD(iph1
)) {
1193 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1194 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1195 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1196 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1197 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1198 xauth_sendreq(iph1
);
1199 /* XXX Don't process INITIAL_CONTACT */
1208 /* Schedule the r_u_there.... */
1209 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1210 isakmp_sched_r_u(iph1
, 0);
1213 /* INITIAL-CONTACT processing */
1214 /* ignore initial-contact if it is a rekey */
1215 /* don't send anything if local test mode. */
1216 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
1217 /* send INITIAL-CONTACT */
1218 isakmp_info_send_n1(iph1
,
1219 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1220 /* insert a node into contacted list. */
1221 if (ike_session_inscontacted(iph1
->remote
) == -1) {
1223 "failed to add contacted list.\n");
1228 log_ph1established(iph1
);
1229 plog(ASL_LEVEL_DEBUG
, "===\n");
1231 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1233 #ifdef ENABLE_VPNCONTROL_PORT
1234 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1235 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 (extract_port(p
->addr
) == PORT_ISAKMP
) {
1523 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1524 (void *)&yes
, sizeof(yes
)) < 0) {
1526 "setsockopt (%s)\n", strerror(errno
));
1531 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1532 int tmp_errno
= errno
;
1534 "failed to bind to address %s (%s).\n",
1535 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1537 // if bind failed b/c of a tentative v6 address, try again later
1538 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1539 struct in6_ifreq ifr6
;
1541 bzero(&ifr6
, sizeof(ifr6
));
1542 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1543 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1544 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1546 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1547 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1549 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1550 // address may have been tentantive... invalidate sock but leave address around for another try later
1552 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1553 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1556 tentative_failures
= 1;
1560 "failed to bind to address %s: because of interface address error, flags %x.\n",
1561 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1565 "failed to bind to address %s: can't read interface address flags.\n",
1566 saddr2str((struct sockaddr
*)p
->addr
));
1581 if (p
->source
!= NULL
) {
1582 dispatch_source_cancel(p
->source
);
1585 p
->source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, p
->sock
, 0, dispatch_get_main_queue());
1586 if (p
->source
== NULL
) {
1587 plog(ASL_LEVEL_ERR
, "could not create isakmp socket source.");
1590 dispatch_source_set_event_handler(p
->source
,
1594 dispatch_source_t the_source
= p
->source
;
1595 dispatch_source_set_cancel_handler(p
->source
,
1598 dispatch_release(the_source
);
1599 }); dispatch_resume(p
->source
);
1601 plog(ASL_LEVEL_INFO
,
1602 "%s used as isakmp port (fd=%d)\n",
1603 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
1607 racoon_free(p
->addr
);
1610 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
) {
1619 "no address could be bound.\n");
1625 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1626 plog(ASL_LEVEL_WARNING
,
1627 "NAT-T is enabled in at least one remote{} section,\n");
1628 plog(ASL_LEVEL_WARNING
,
1629 "but no 'isakmp_natt' address was specified!\n");
1633 if (tentative_failures
)
1634 sched_new(5, update_myaddrs
, NULL
);
1640 isakmp_suspend_sockets()
1644 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1646 dispatch_suspend(p
->source
);
1653 isakmp_close_sockets();
1658 isakmp_close_sockets()
1662 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1668 dispatch_source_cancel(p
->source
);
1677 // close sockets for addresses that have gone away
1679 isakmp_close_unused()
1681 struct myaddrs
*p
, *next
, **prev
;
1683 prev
= &(lcconf
->myaddrs
);
1684 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1686 if (p
->in_use
== 0) { // not in use ?
1688 dispatch_source_cancel(p
->source
);
1699 isakmp_send(iph1
, sbuf
)
1700 phase1_handle_t
*iph1
;
1705 vchar_t
*vbuf
= NULL
;
1707 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1711 * Do not add the non ESP marker for a packet that will
1712 * be fragmented. The non ESP marker should appear in
1713 * all fragment's packets, but not in the fragmented packet
1715 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1719 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1721 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1722 must added just before the packet itself. For this we must
1723 allocate a new buffer and release it at the end. */
1725 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1727 "vbuf allocation failed\n");
1730 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
1731 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1736 /* select the socket to be sent */
1737 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
1744 plog (ASL_LEVEL_DEBUG
, "%zu bytes %s\n", sbuf
->l
,
1745 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
1748 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1749 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1751 "isakmp_sendfrags failed\n");
1759 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1760 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1762 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1775 /* called from scheduler */
1777 isakmp_ph1resend_stub(p
)
1780 phase1_handle_t
*iph1
;
1782 iph1
=(phase1_handle_t
*)p
;
1783 if(isakmp_ph1resend(iph1
) < 0){
1785 /* Should not happen...
1787 SCHED_KILL(iph1
->scr
);
1790 ike_session_unlink_phase1(iph1
);
1795 isakmp_ph1resend(iph1
)
1796 phase1_handle_t
*iph1
;
1798 time_t retry_interval
;
1801 // make sure there is a buffer to send
1802 // isakmp_plist_set_all() could have returned NULL
1803 if (iph1
->sendbuf
== NULL
)
1806 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1808 if (iph1
->retry_counter
<= 0) {
1809 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1810 IPSECSESSIONEVENTCODE_IKEV1_PH1_MAX_RETRANSMIT
,
1811 CONSTSTR("Phase 1 Maximum Retransmits"),
1812 CONSTSTR("Phase 1 negotiation failed (Maximum retransmits)"));
1815 "Phase 1 negotiation failed due to time up. %s\n",
1816 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1817 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
1818 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
1819 if (isakmp_ph1rekeyretry(iph1
) == 0)
1824 ike_session_ph1_retransmits(iph1
);
1827 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1828 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1829 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1830 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1831 CONSTSTR("Phase 1 Retransmit"),
1832 CONSTSTR("Failed to retrasmit Phase1"));
1835 "Phase 1 negotiation failed due to send error. %s\n",
1836 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1840 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1841 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1842 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1843 CONSTSTR("Phase 1 Retransmit"),
1847 plog(ASL_LEVEL_DEBUG
,
1848 "Resend Phase 1 packet %s\n",
1849 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1851 iph1
->retry_counter
--;
1852 retry_interval
= ike_session_get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
1853 iph1
->rmconf
->retry_interval
);
1854 iph1
->scr
= sched_new(retry_interval
,
1855 isakmp_ph1resend_stub
, iph1
);
1860 /* called from scheduler */
1862 isakmp_ph2resend_stub(p
)
1865 phase2_handle_t
*iph2
;
1867 iph2
=(phase2_handle_t
*)p
;
1869 if(isakmp_ph2resend(iph2
) < 0){
1870 ike_session_unlink_phase2(iph2
);
1875 isakmp_ph2resend(iph2
)
1876 phase2_handle_t
*iph2
;
1878 time_t retry_interval
;
1880 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1882 //%%% BUG FIX - related to commit bit usage - crash happened here
1883 if (iph2
->ph1
== 0) {
1885 "Internal error - attempt to re-send Phase 2 with no Phase 1 bound.\n");
1890 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)){
1891 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1892 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1893 CONSTSTR("Underlying Phase 1 expired"),
1894 CONSTSTR("Failed to retransmit Phase 2 (underlying Phase 1 expired)"));
1896 "Phase 2 negotiation failed due to Phase 1 expired. %s\n",
1897 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1901 if (iph2
->retry_counter
<= 0) {
1902 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1903 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1904 CONSTSTR("Phase 2 maximum retransmits"),
1905 CONSTSTR("Phase 2 negotiation failed (maximum retransmits)"));
1907 "Phase 2 negotiation failed due to time up. %s\n",
1908 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1911 ike_session_ph2_retransmits(iph2
);
1914 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1915 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1916 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1917 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1918 CONSTSTR("Phase 2 Retransmit"),
1919 CONSTSTR("Failed to retransmit Phase2 message"));
1922 "Phase 2 negotiation failed due to send error. %s\n",
1923 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1927 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1928 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1929 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1930 CONSTSTR("Phase 2 Retransmit"),
1934 plog(ASL_LEVEL_DEBUG
,
1935 "Resend Phase 2 packet %s\n",
1936 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1938 iph2
->retry_counter
--;
1939 retry_interval
= ike_session_get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
1940 iph2
->ph1
->rmconf
->retry_interval
);
1941 iph2
->scr
= sched_new(retry_interval
,
1942 isakmp_ph2resend_stub
, iph2
);
1946 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
1947 "Phase 2 packets sent to peer: retransmit timer armed");
1955 /* called from scheduler */
1957 isakmp_ph1expire_stub(p
)
1961 isakmp_ph1expire((phase1_handle_t
*)p
);
1965 isakmp_ph1expire(iph1
)
1966 phase1_handle_t
*iph1
;
1969 phase1_handle_t
*new_iph1
;
1971 SCHED_KILL(iph1
->sce
);
1973 SCHED_KILL(iph1
->dpd_r_u
);
1976 if(!FSM_STATE_IS_EXPIRED(iph1
->status
)){
1977 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
1978 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
1982 plog(ASL_LEVEL_INFO
,
1983 "ISAKMP-SA expired %s-%s spi:%s\n",
1985 isakmp_pindex(&iph1
->index
, 0));
1988 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
1989 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
1993 * the phase1 deletion is postponed until there is no phase2.
1995 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
1996 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2001 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2004 /* called from scheduler */
2006 isakmp_ph1rekeyexpire_stub(p
)
2010 isakmp_ph1rekeyexpire((phase1_handle_t
*)p
, FALSE
);
2014 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
2015 phase1_handle_t
*iph1
;
2016 int ignore_sess_drop_policy
;
2019 struct remoteconf
*rmconf
;
2021 SCHED_KILL(iph1
->sce_rekey
);
2023 // We are going to start the rekey. Let's fire off the
2024 // phase1 expiration timer if it is not done yet.
2025 if (!iph1
->sce
&& iph1
->approval
->lifetimegap
) {
2026 iph1
->sce
= sched_new(iph1
->approval
->lifetimegap
,
2027 isakmp_ph1expire_stub
, iph1
);
2030 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
2031 if (iph1
->sce
== 0 ||
2032 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2037 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2038 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2042 plog(ASL_LEVEL_INFO
,
2043 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2045 isakmp_pindex(&iph1
->index
, 0));
2050 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2055 // exit if there is another ph1 that is established (with a pending rekey timer)
2056 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2057 plog(ASL_LEVEL_INFO
,
2058 "Request for ISAKMP-SA rekey was ignored "
2059 "due to another established ph1.\n");
2063 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2064 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2065 plog(ASL_LEVEL_DEBUG
, "Reschedule Phase 1 rekey.\n");
2066 iph1
->sce_rekey
= sched_new(1,
2067 isakmp_ph1rekeyexpire_stub
,
2072 // get rmconf to initiate rekey with
2073 rmconf
= iph1
->rmconf
;
2075 rmconf
= getrmconf(iph1
->remote
);
2078 /* begin quick mode */
2079 plog(ASL_LEVEL_DEBUG
, "Begin Phase 1 rekey.\n");
2081 /* start phase 1 negotiation as a initiator. */
2083 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2084 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey Failed.\n");
2086 iph1
->is_rekey
= TRUE
;
2090 "Phase1 rekey failed: no configuration found for %s.\n",
2091 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2096 isakmp_ph1rekeyretry(iph1
)
2097 phase1_handle_t
*iph1
;
2100 struct remoteconf
*rmconf
;
2102 // this code path is meant for floated ph1 rekeys that are failing on the first message
2103 if (iph1
->sce
!= 0 ||
2104 iph1
->sce_rekey
!= 0 ||
2105 ((iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
&&
2106 iph1
->status
!= IKEV1_STATE_AGG_I_MSG1SENT
)
2107 || ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0))
2108 || (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
)
2109 || iph1
->is_dying
) {
2113 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2114 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2118 plog(ASL_LEVEL_INFO
,
2119 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2121 isakmp_pindex(&iph1
->index
, 0));
2125 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2126 plog(ASL_LEVEL_INFO
,
2127 "request for ISAKMP-SA rekey was ignored "
2128 "due to idleness.\n");
2132 // exit if there is another ph1 that is established (with a pending rekey timer)
2133 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2134 plog(ASL_LEVEL_INFO
,
2135 "request to retry ISAKMP-SA rekey was ignored "
2136 "due to another established ph1.\n");
2140 // some servers don't like respond to 4500 for rekeys... try accomodate them
2141 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2142 set_port(iph1
->local
, PORT_ISAKMP
);
2144 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2145 set_port(iph1
->remote
, PORT_ISAKMP
);
2147 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2148 rmconf
= getrmconf(iph1
->remote
);
2150 /* begin quick mode */
2151 plog(ASL_LEVEL_DEBUG
, "begin Phase 1 rekey retry.\n");
2153 /* start phase 1 negotiation as a initiator. */
2154 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2155 plog(ASL_LEVEL_DEBUG
, "Phase 1 rekey retry Failed.\n");
2158 iph1
->is_rekey
= TRUE
;
2161 "Phase 1 rekey retry failed: no configuration found for %s.\n",
2162 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2168 /* called from scheduler */
2170 isakmp_ph1delete_stub(p
)
2174 isakmp_ph1delete((phase1_handle_t
*)p
);
2178 isakmp_ph1delete(iph1
)
2179 phase1_handle_t
*iph1
;
2183 SCHED_KILL(iph1
->sce
);
2184 SCHED_KILL(iph1
->sce_rekey
);
2186 SCHED_KILL(iph1
->dpd_r_u
);
2189 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2190 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2194 isakmp_info_send_d1(iph1
);
2196 /* don't re-negosiation when the phase 1 SA expires. */
2198 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2199 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2203 plog(ASL_LEVEL_INFO
,
2204 "ISAKMP-SA deleted %s-%s spi:%s\n",
2205 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2209 ike_session_unlink_phase1(iph1
);
2214 /* called from scheduler.
2215 * this function will call only isakmp_ph2delete().
2216 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2217 * by something cause. That's why this function is called after phase 2 SA
2218 * expires in the userland.
2221 isakmp_ph2expire_stub(p
)
2225 isakmp_ph2expire((phase2_handle_t
*)p
);
2229 isakmp_ph2expire(iph2
)
2230 phase2_handle_t
*iph2
;
2234 if (FSM_STATE_IS_EXPIRED(iph2
->status
)) {
2238 SCHED_KILL(iph2
->sce
);
2240 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2241 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2245 plog(ASL_LEVEL_INFO
,
2246 "Phase 2 sa expired %s-%s\n", src
, dst
);
2250 // delete outgoing SAs
2251 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
) && iph2
->approval
) {
2254 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2256 pfkey_send_delete(lcconf
->sock_pfkey
,
2257 ipsecdoi2pfkey_proto(pr
->proto_id
),
2259 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2262 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2263 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
2264 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2269 /* called from scheduler */
2271 isakmp_ph2delete_stub(p
)
2275 isakmp_ph2delete((phase2_handle_t
*)p
);
2279 isakmp_ph2delete(iph2
)
2280 phase2_handle_t
*iph2
;
2284 SCHED_KILL(iph2
->sce
);
2286 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2287 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2291 plog(ASL_LEVEL_INFO
,
2292 "Phase 2 sa deleted %s-%s\n", src
, dst
);
2296 ike_session_unlink_phase2(iph2
);
2302 * Interface between PF_KEYv2 and ISAKMP
2305 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2306 * if phase1 has been finished, begin phase2.
2309 isakmp_post_acquire(phase2_handle_t
*iph2
)
2311 struct remoteconf
*rmconf
;
2312 phase1_handle_t
*iph1
= NULL
;
2314 plog(ASL_LEVEL_DEBUG
, "In post_acquire\n");
2316 /* search appropreate configuration with masking port. */
2317 rmconf
= getrmconf(iph2
->dst
);
2318 if (rmconf
== NULL
) {
2320 "No configuration found for %s.\n",
2321 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2324 /* if passive mode, ignore the acquire message */
2325 if (rmconf
->passive
) {
2326 plog(ASL_LEVEL_DEBUG
,
2327 "Because of passive mode, ignore the acquire message for %s.\n",
2328 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2333 // what if there is another ph2 that is negotiating
2334 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2335 // TODO: postpone this rekey for a second later
2336 plog(ASL_LEVEL_INFO
,
2337 "Request for establishing IPsec-SA was ignored due to another negoing ph2.\n");
2341 // if this is a phase2 rekeys (the policy may not have the current port number).
2342 // so, use the appropriate ports.
2343 if (iph2
->is_rekey
) {
2344 ike_session_update_ph2_ports(iph2
);
2346 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2347 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2349 iph1
= ike_session_get_established_or_negoing_ph1(iph2
->parent_session
);
2351 /* no IKE-SA found. */
2353 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2355 /* start phase 1 negotiation as a initiator. */
2357 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2359 plog(ASL_LEVEL_INFO
,
2360 "IPsec-SA request for %s queued due to no Phase 1 found.\n",
2361 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2363 // exit if there is another ph1 that is established (with a pending rekey timer)
2364 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2365 plog(ASL_LEVEL_INFO
,
2366 "Request for Phase 1 was ignored due to another negotiating Phase 1.\n");
2370 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2371 plog(ASL_LEVEL_INFO
,
2372 "Request for Phase 1 failed. Will try later.\n");
2380 /* found ISAKMP-SA, but on negotiation. */
2381 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2382 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2383 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2384 plog(ASL_LEVEL_INFO
,
2385 "Request for establishing IPsec-SA was queued due to no phase1 found.\n");
2390 /* found established ISAKMP-SA */
2392 /* found ISAKMP-SA. */
2394 /* begin quick mode */
2396 plog(ASL_LEVEL_DEBUG
, "Begin QUICK mode.\n");
2397 if (ikev1_ph2begin_i(iph1
, iph2
))
2404 * receive GETSPI from kernel.
2407 isakmp_post_getspi(phase2_handle_t
*iph2
)
2410 struct timeval start
, end
;
2414 /* don't process it because there is no suitable phase1-sa. */
2415 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)) {
2417 "the negotiation is stopped, "
2418 "because there is no suitable ISAKMP-SA.\n");
2421 fsm_set_state(&iph2
->status
, iph2
->side
== INITIATOR
?
2422 IKEV1_STATE_QUICK_I_GETSPIDONE
: IKEV1_STATE_QUICK_R_GETSPIDONE
);
2425 gettimeofday(&start
, NULL
);
2427 switch (iph2
->side
) {
2429 error
= quick_i1send(iph2
, NULL
);
2432 error
= quick_r2send(iph2
, NULL
);
2436 if (error
) //%%%%%%%% log something ???
2439 gettimeofday(&end
, NULL
);
2440 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
2442 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2443 timedelta(&start
, &end
));
2449 /* called by scheduler */
2451 isakmp_chkph1there_stub(p
)
2454 isakmp_chkph1there((phase2_handle_t
*)p
);
2458 isakmp_chkph1there(iph2
)
2459 phase2_handle_t
*iph2
;
2461 phase1_handle_t
*iph1
;
2463 if ((iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
&& iph2
->status
!= IKEV1_STATE_QUICK_I_START
) ||
2465 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
);
2469 iph2
->retry_checkph1
--;
2471 if (iph2
->retry_checkph1
< 0 /* %%%%||
2472 ike_session_verify_ph2_parent_session(iph2) */) {
2473 if (iph2
->retry_checkph1
< 0) {
2475 "Phase 2 negotiation failed "
2476 "due to time up waiting for Phase 1. %s\n",
2477 sadbsecas2str(iph2
->dst
, iph2
->src
,
2478 iph2
->satype
, 0, 0));
2481 "Phase 2 negotiation failed "
2482 "due to invalid parent session. %s\n",
2483 sadbsecas2str(iph2
->dst
, iph2
->src
,
2484 iph2
->satype
, 0, 0));
2486 plog(ASL_LEVEL_INFO
,
2487 "delete Phase 2 handler.\n");
2489 /* send acquire to kernel as error */
2490 pk_sendeacquire(iph2
);
2491 ike_session_unlink_phase2(iph2
);
2495 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2497 /* XXX Even if ph1 as responder is there, should we not start
2498 * phase 2 negotiation ? */
2500 && FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2501 /* found isakmp-sa */
2503 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2504 plog(ASL_LEVEL_DEBUG
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2505 plog(ASL_LEVEL_DEBUG
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2506 plog(ASL_LEVEL_DEBUG
, "before:\n");
2507 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2508 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2509 set_port(iph2
->src
, extract_port(iph1
->local
));
2510 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2511 plog(ASL_LEVEL_DEBUG
, "After:\n");
2512 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2513 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2515 /* begin quick mode */
2517 if (ikev1_ph2begin_i(iph1
, iph2
)) {
2518 ike_session_unlink_phase2(iph2
);
2523 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2524 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2525 /* start phase 1 negotiation as a initiator. */
2528 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2529 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2532 } else if (rmconf
== NULL
) {
2533 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no remoteconf found... failed to initiate new one\n");
2537 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: no established ph1 handler found\n");
2539 /* no isakmp-sa found */
2540 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2545 /* copy variable data into ALLOCATED buffer. */
2547 isakmp_set_attr_v(buf
, type
, val
, len
)
2553 struct isakmp_data
*data
;
2555 data
= (struct isakmp_data
*)buf
;
2556 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2557 data
->lorv
= htons((u_int16_t
)len
);
2558 memcpy(data
+ 1, val
, len
);
2560 return buf
+ sizeof(*data
) + len
;
2563 /* copy fixed length data into ALLOCATED buffer. */
2565 isakmp_set_attr_l(buf
, type
, val
)
2570 struct isakmp_data
*data
;
2572 data
= (struct isakmp_data
*)buf
;
2573 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2574 data
->lorv
= htons((u_int16_t
)val
);
2576 return buf
+ sizeof(*data
);
2579 /* add a variable data attribute to the buffer by reallocating it. */
2581 isakmp_add_attr_v(buf0
, type
, val
, len
)
2587 vchar_t
*buf
= NULL
;
2588 struct isakmp_data
*data
;
2592 tlen
= sizeof(*data
) + len
;
2596 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2598 buf
= vmalloc(tlen
);
2601 "failed to get a attribute buffer.\n");
2605 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2606 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2607 data
->lorv
= htons((u_int16_t
)len
);
2608 memcpy(data
+ 1, val
, len
);
2613 /* add a fixed data attribute to the buffer by reallocating it. */
2615 isakmp_add_attr_l(buf0
, type
, val
)
2620 vchar_t
*buf
= NULL
;
2621 struct isakmp_data
*data
;
2625 tlen
= sizeof(*data
);
2629 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2631 buf
= vmalloc(tlen
);
2634 "failed to get a attribute buffer.\n");
2638 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2639 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2640 data
->lorv
= htons((u_int16_t
)val
);
2646 * calculate cookie and set.
2649 isakmp_newcookie(place
, remote
, local
)
2651 struct sockaddr_storage
*remote
;
2652 struct sockaddr_storage
*local
;
2654 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2664 if (remote
->ss_family
!= local
->ss_family
) {
2666 "address family mismatch, remote:%d local:%d\n",
2667 remote
->ss_family
, local
->ss_family
);
2670 switch (remote
->ss_family
) {
2672 alen
= sizeof(struct in_addr
);
2673 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2674 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2678 alen
= sizeof(struct in_addr
);
2679 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2680 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2685 "invalid family: %d\n", remote
->ss_family
);
2688 blen
= (alen
+ sizeof(u_short
)) * 2
2689 + sizeof(time_t) + lcconf
->secret_size
;
2690 buf
= vmalloc(blen
);
2693 "failed to get a cookie.\n");
2698 /* copy my address */
2699 memcpy(p
, sa1
, alen
);
2701 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2702 memcpy(p
, &port
, sizeof(u_short
));
2703 p
+= sizeof(u_short
);
2705 /* copy target address */
2706 memcpy(p
, sa2
, alen
);
2708 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2709 memcpy(p
, &port
, sizeof(u_short
));
2710 p
+= sizeof(u_short
);
2714 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2717 /* copy random value */
2718 buf2
= eay_set_random(lcconf
->secret_size
);
2721 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2722 p
+= lcconf
->secret_size
;
2725 buf2
= eay_sha1_one(buf
);
2726 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2728 sa1
= val2str(place
, sizeof (cookie_t
));
2729 plog(ASL_LEVEL_DEBUG
, "new cookie:\n%s\n", sa1
);
2742 * save partner's(payload) data into phhandle.
2745 isakmp_p2ph(buf
, gen
)
2747 struct isakmp_gen
*gen
;
2749 /* XXX to be checked in each functions for logging. */
2751 plog(ASL_LEVEL_WARNING
,
2752 "ignore this payload, same payload type exist.\n");
2756 if (ntohs(gen
->len
) < sizeof(*gen
)) {
2758 "ignore this payload, invalid payload len %d.\n",
2763 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2766 "failed to get buffer.\n");
2769 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2775 isakmp_newmsgid2(iph1
)
2776 phase1_handle_t
*iph1
;
2781 msgid2
= eay_random();
2782 } while (ike_session_getph2bymsgid(iph1
, msgid2
));
2788 * set values into allocated buffer of isakmp header for phase 1
2791 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2793 phase1_handle_t
*iph1
;
2799 struct isakmp
*isakmp
;
2801 if (vbuf
->l
< sizeof(*isakmp
))
2804 isakmp
= (struct isakmp
*)vbuf
->v
;
2806 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2807 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2808 isakmp
->np
= nptype
;
2809 isakmp
->v
= iph1
->version
;
2810 isakmp
->etype
= etype
;
2811 isakmp
->flags
= flags
;
2812 isakmp
->msgid
= msgid
;
2813 isakmp
->len
= htonl(vbuf
->l
);
2815 return vbuf
->v
+ sizeof(*isakmp
);
2819 * set values into allocated buffer of isakmp header for phase 1
2822 set_isakmp_header1(vbuf
, iph1
, nptype
)
2824 phase1_handle_t
*iph1
;
2827 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2831 * set values into allocated buffer of isakmp header for phase 2
2834 set_isakmp_header2(vbuf
, iph2
, nptype
)
2836 phase2_handle_t
*iph2
;
2839 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2843 * set values into allocated buffer of isakmp payload.
2846 set_isakmp_payload(buf
, src
, nptype
)
2851 struct isakmp_gen
*gen
;
2854 plog(ASL_LEVEL_DEBUG
, "add payload of len %zu, next type %d\n",
2857 gen
= (struct isakmp_gen
*)p
;
2859 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2861 memcpy(p
, src
->v
, src
->l
);
2868 #ifdef HAVE_PRINT_ISAKMP_C
2869 /* for print-isakmp.c */
2871 extern void isakmp_print(const u_char
*, u_int
, const u_char
*);
2873 char *getname(const u_char
*);
2875 char *getname6(const u_char
*);
2877 int safeputchar(int);
2880 * Return a name for the IP address pointed to by ap. This address
2881 * is assumed to be in network byte order.
2887 struct sockaddr_in addr
;
2888 static char ntop_buf
[NI_MAXHOST
];
2890 memset(&addr
, 0, sizeof(addr
));
2891 addr
.sin_len
= sizeof(struct sockaddr_in
);
2892 addr
.sin_family
= AF_INET
;
2893 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2894 if (getnameinfo(&addr
, sizeof(addr
),
2895 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2896 NI_NUMERICHOST
| niflags
))
2897 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2904 * Return a name for the IP6 address pointed to by ap. This address
2905 * is assumed to be in network byte order.
2911 struct sockaddr_in6 addr
;
2912 static char ntop_buf
[NI_MAXHOST
];
2914 memset(&addr
, 0, sizeof(addr
));
2915 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2916 addr
.sin6_family
= AF_INET6
;
2917 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2918 if (getnameinfo(&addr
, addr
.sin6_len
,
2919 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2920 NI_NUMERICHOST
| niflags
))
2921 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2933 ch
= (unsigned char)(c
& 0xff);
2934 if (c
< 0x80 && isprint(c
))
2935 return printf("%c", c
& 0xff);
2937 return printf("\\%03o", c
& 0xff);
2941 isakmp_printpacket(msg
, from
, my
, decoded
)
2943 struct sockaddr_storage
*from
;
2944 struct sockaddr_storage
*my
;
2950 char hostbuf
[NI_MAXHOST
];
2951 char portbuf
[NI_MAXSERV
];
2952 struct isakmp
*isakmp
;
2956 if (loglevel
< ASL_LEVEL_DEBUG
)
2960 plog(ASL_LEVEL_DEBUG
, "begin.\n");
2962 gettimeofday(&tv
, NULL
);
2963 s
= tv
.tv_sec
% 3600;
2964 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2967 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
2968 portbuf
, sizeof(portbuf
),
2969 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2970 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2971 strlcpy(portbuf
, "?", sizeof(portbuf
));
2973 printf("%s:%s", hostbuf
, portbuf
);
2978 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
2979 portbuf
, sizeof(portbuf
),
2980 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2981 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2982 strlcpy(portbuf
, "?", sizeof(portbuf
));
2984 printf("%s:%s", hostbuf
, portbuf
);
2991 printf("(malloc fail)\n");
2995 isakmp
= (struct isakmp
*)buf
->v
;
2996 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2999 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
3000 if (buf
->l
< pad
&& 2 < vflag
)
3001 printf("(wrong padding)");
3003 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
3007 snapend
= buf
->v
+ buf
->l
;
3008 isakmp_print(buf
->v
, buf
->l
, NULL
);
3016 #endif /*HAVE_PRINT_ISAKMP_C*/
3019 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
3020 phase1_handle_t
*iph1
;
3021 struct remoteconf
*rmconf
;
3022 struct sockaddr_storage
*remote
, *local
;
3024 u_short
*port
= NULL
;
3026 /* address portion must be grabbed from real remote address "remote" */
3027 iph1
->remote
= dupsaddr(remote
);
3028 if (iph1
->remote
== NULL
) {
3029 ike_session_delph1(iph1
);
3034 * if remote has no port # (in case of initiator - from ACQUIRE msg)
3035 * - if remote.conf specifies port #, use that
3036 * - if remote.conf does not, use 500
3037 * if remote has port # (in case of responder - from recvfrom(2))
3038 * respect content of "remote".
3040 switch (iph1
->remote
->ss_family
) {
3042 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3045 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3048 *port
= htons(PORT_ISAKMP
);
3052 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3055 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3058 *port
= htons(PORT_ISAKMP
);
3063 "invalid family: %d\n", iph1
->remote
->ss_family
);
3064 ike_session_delph1(iph1
);
3069 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3071 iph1
->local
= dupsaddr(local
);
3072 if (iph1
->local
== NULL
) {
3073 ike_session_delph1(iph1
);
3077 switch (iph1
->local
->ss_family
) {
3079 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3082 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3085 *port
= getmyaddrsport(iph1
->local
);
3089 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3092 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3095 *port
= getmyaddrsport(iph1
->local
);
3100 "invalid family: %d\n", iph1
->local
->ss_family
);
3101 ike_session_delph1(iph1
);
3105 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3106 plog (ASL_LEVEL_DEBUG
, "Marking ports as changed\n");
3107 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3115 log_ph1established(iph1
)
3116 const phase1_handle_t
*iph1
;
3120 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3121 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3125 plog(ASL_LEVEL_INFO
,
3126 "ISAKMP-SA established %s-%s spi:%s\n",
3128 isakmp_pindex(&iph1
->index
, 0));
3133 IPSECLOGASLMSG("IPSec Phase 1 established (Initiated by %s).\n",
3134 (iph1
->side
== INITIATOR
)? "me" : "peer");
3139 struct payload_list
*
3140 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3143 plist
= racoon_malloc (sizeof (struct payload_list
));
3147 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3148 plist
->next
->prev
= plist
;
3149 plist
= plist
->next
;
3153 plist
->payload
= payload
;
3154 plist
->payload_type
= payload_type
;
3160 isakmp_plist_set_all (struct payload_list
**plist
, phase1_handle_t
*iph1
)
3162 struct payload_list
*ptr
= *plist
, *first
;
3163 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3164 vchar_t
*buf
= NULL
;
3167 /* Seek to the first item. */
3168 while (ptr
->prev
) ptr
= ptr
->prev
;
3171 /* Compute the whole length. */
3173 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3177 buf
= vmalloc(tlen
);
3180 "failed to get buffer to send.\n");
3186 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3192 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3195 racoon_free (first
);
3196 /* ptr->prev = NULL; first = NULL; ... omitted. */
3211 frag_handler(iph1
, msg
, remote
, local
)
3212 phase1_handle_t
*iph1
;
3214 struct sockaddr_storage
*remote
;
3215 struct sockaddr_storage
*local
;
3219 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3220 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3222 "Packet reassembly failed\n");
3226 /* simply reply if the packet was processed. */
3227 if (ike_session_check_recvdpkt(remote
, local
, newmsg
) > 0) {
3228 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3229 saddr2str((struct sockaddr
*)remote
));
3231 plog(ASL_LEVEL_NOTICE
,
3232 "the reassembled packet is retransmitted by %s.\n",
3233 saddr2str((struct sockaddr
*)remote
));
3238 isakmp_main(newmsg
, remote
, local
);
3248 phase1_handle_t
*iph1
;
3250 vchar_t
*buf
= NULL
;
3251 struct sadb_msg
*msg
, *next
, *end
;
3253 struct sockaddr_storage
*src
, *dst
;
3254 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3256 phase2_handle_t
*iph2
;
3257 phase1_handle_t
*new_iph1
;
3259 plog(ASL_LEVEL_INFO
,
3260 "purging ISAKMP-SA spi=%s.\n",
3261 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3263 /* Mark as expired. */
3264 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
3266 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3269 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3270 * Keep all others phase2 SAs.
3272 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3274 plog(ASL_LEVEL_DEBUG
,
3275 "pfkey_dump_sadb returned nothing.\n");
3279 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3280 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3283 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3285 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3286 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3291 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3293 "pfkey_check (%s)\n", ipsec_strerror());
3298 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3300 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3301 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3305 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3306 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3308 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3309 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3310 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3316 * check in/outbound SAs.
3317 * Select only SAs where src == local and dst == remote (outgoing)
3318 * or src == remote and dst == local (incoming).
3320 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3321 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3326 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3327 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3329 /* Check if there is another valid ISAKMP-SA */
3330 if (new_iph1
!= NULL
) {
3333 /* No handler... still send a pfkey_delete message, but log this !*/
3334 plog(ASL_LEVEL_INFO
,
3335 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3336 ntohl(sa
->sadb_sa_spi
));
3340 * If we have a new ph1, do not purge IPsec-SAs binded
3341 * to a different ISAKMP-SA
3343 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3348 /* If the ph2handle is established, do not purge IPsec-SA */
3349 if (FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(iph2
->status
)) {
3351 plog(ASL_LEVEL_INFO
,
3352 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3353 ntohl(sa
->sadb_sa_spi
),
3354 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3362 pfkey_send_delete(lcconf
->sock_pfkey
,
3363 msg
->sadb_msg_satype
,
3365 src
, dst
, sa
->sadb_sa_spi
);
3367 /* delete a relative phase 2 handle. */
3370 ike_session_unlink_phase2(iph2
);
3373 plog(ASL_LEVEL_INFO
,
3374 "purged IPsec-SA spi=%u.\n",
3375 ntohl(sa
->sadb_sa_spi
));
3383 /* Mark the phase1 handler as EXPIRED */
3384 plog(ASL_LEVEL_INFO
,
3385 "purged ISAKMP-SA spi=%s.\n",
3386 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3388 SCHED_KILL(iph1
->sce
);
3390 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3395 phase2_handle_t
*iph2
;
3400 /* Delete the SPD entry if we generated it
3402 if (iph2
->generated_spidx
) {
3404 u_int64_t force_align
; // Wcast-align fix - force alignment
3405 struct policyindex spidx
;
3407 struct sockaddr_storage addr
;
3409 struct sockaddr_storage
*src
= iph2
->src
;
3410 struct sockaddr_storage
*dst
= iph2
->dst
;
3412 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3414 plog(ASL_LEVEL_INFO
,
3415 "generated policy, deleting it.\n");
3417 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
3418 iph2
->spidx_gen
= &u
.spidx
;
3420 /* make inbound policy */
3423 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
3424 u
.spidx
.ul_proto
= 0;
3427 * Note: code from get_proposal_r
3430 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
3433 * make destination address in spidx from either ID payload
3434 * or phase 1 address into a address in spidx.
3436 if (iph2
->id
!= NULL
3437 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3438 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3439 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3440 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3441 /* get a destination address of a policy */
3442 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
3443 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
, iph2
->version
);
3449 * get scopeid from the SA address.
3450 * note that the phase 1 source address is used as
3451 * a destination address to search for a inbound
3452 * policy entry because rcoon is responder.
3454 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3456 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
3461 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3462 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3463 idi2type
= _XIDT(iph2
->id
);
3467 plog(ASL_LEVEL_DEBUG
,
3468 "Get a destination address of SP index "
3469 "from Phase 1 address "
3470 "due to no ID payloads found "
3471 "OR because ID type is not address.\n");
3474 * copy the SOURCE address of IKE into the
3475 * DESTINATION address of the key to search the
3476 * SPD because the direction of policy is inbound.
3478 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
3479 switch (u
.spidx
.dst
.ss_family
) {
3482 sizeof(struct in_addr
) << 3;
3487 sizeof(struct in6_addr
) << 3;
3496 /* make source address in spidx */
3497 if (iph2
->id_p
!= NULL
3498 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3499 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3500 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3501 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3502 /* get a source address of inbound SA */
3503 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
3504 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
, iph2
->version
);
3510 * get scopeid from the SA address.
3511 * for more detail, see above of this function.
3513 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3515 setscopeid(&u
.spidx
.src
, iph2
->dst
);
3521 /* make id[src,dst] if both ID types are IP address and same */
3522 if (_XIDT(iph2
->id_p
) == idi2type
3523 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
3525 dupsaddr(&u
.spidx
.dst
);
3527 dupsaddr(&u
.spidx
.src
);
3531 plog(ASL_LEVEL_DEBUG
,
3532 "Get a source address of SP index "
3533 "from Phase 1 address "
3534 "due to no ID payloads found "
3535 "OR because ID type is not address.\n");
3537 /* see above comment. */
3538 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
3539 switch (u
.spidx
.src
.ss_family
) {
3542 sizeof(struct in_addr
) << 3;
3547 sizeof(struct in6_addr
) << 3;
3558 plog(ASL_LEVEL_DEBUG
,
3559 "get a src address from ID payload "
3560 "%s prefixlen=%u ul_proto=%u\n",
3561 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
3562 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3563 plog(ASL_LEVEL_DEBUG
,
3564 "get dst address from ID payload "
3565 "%s prefixlen=%u ul_proto=%u\n",
3566 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
3567 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
3570 * convert the ul_proto if it is 0
3571 * because 0 in ID payload means a wild card.
3573 if (u
.spidx
.ul_proto
== 0)
3574 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3578 /* End of code from get_proposal_r
3581 if (pk_sendspddelete(iph2
) < 0) {
3583 "pfkey spddelete(inbound) failed.\n");
3585 plog(ASL_LEVEL_DEBUG
,
3586 "pfkey spddelete(inbound) sent.\n");
3589 #ifdef HAVE_POLICY_FWD
3590 /* make forward policy if required */
3591 if (tunnel_mode_prop(iph2
->approval
)) {
3592 u
.spidx
.dir
= IPSEC_DIR_FWD
;
3593 if (pk_sendspddelete(iph2
) < 0) {
3595 "pfkey spddelete(forward) failed.\n");
3597 plog(ASL_LEVEL_DEBUG
,
3598 "pfkey spddelete(forward) sent.\n");
3603 /* make outbound policy */
3606 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3608 u
.spidx
.src
= u
.spidx
.dst
;
3610 pref
= u
.spidx
.prefs
;
3611 u
.spidx
.prefs
= u
.spidx
.prefd
;
3612 u
.spidx
.prefd
= pref
;
3614 if (pk_sendspddelete(iph2
) < 0) {
3616 "pfkey spddelete(outbound) failed.\n");
3618 plog(ASL_LEVEL_DEBUG
,
3619 "pfkey spddelete(outbound) sent.\n");
3622 iph2
->spidx_gen
=NULL
;
3628 setscopeid(sp_addr0
, sa_addr0
)
3629 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
3631 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3633 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3634 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3636 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3637 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3638 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3641 /* this check should not be here ? */
3642 if (sa_addr
->sin6_family
!= AF_INET6
) {
3644 "can't get scope ID: family mismatch\n");
3648 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3650 "scope ID is not supported except of lladdr.\n");
3654 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
3661 isakmp_plist_append_initial_contact (iph1
, plist
)
3662 phase1_handle_t
*iph1
;
3663 struct payload_list
*plist
;
3665 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
3666 vchar_t
*notp_ini
= NULL
;
3667 struct isakmp_pl_n np
, *nptr
;
3670 np
.doi
= htonl(iph1
->rmconf
->doitype
);
3671 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
3672 np
.spi_size
= sizeof(isakmp_index
);
3673 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
3674 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
3675 + sizeof(isakmp_index
)))) {
3677 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
3678 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
3679 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
3680 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
3681 plog(ASL_LEVEL_DEBUG
,
3682 "added initial-contact payload.\n");
3684 /* insert a node into contacted list. */
3685 if (ike_session_inscontacted(iph1
->remote
) == -1) {
3687 "failed to add contacted list.\n");
3693 "failed to allocate notification payload.\n");
3697 plog(ASL_LEVEL_DEBUG
, "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
3698 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, ike_session_getcontacted(iph1
->remote
)? 1:0);