1 /* $NetBSD: isakmp.c,v 1.20.6.7 2007/08/01 11:52:20 vanhu Exp $ */
3 /* Id: isakmp.c,v 1.74 2006/05/07 21:32:59 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #define __APPLE_API_PRIVATE
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/queue.h>
43 #include <netinet/in.h>
44 #include <net/if_var.h>
45 #include <netinet6/in6_var.h>
46 #include <arpa/inet.h>
48 #ifndef HAVE_NETINET6_IPSEC
49 #include <netinet/ipsec.h>
51 #include <netinet6/ipsec.h>
58 #if TIME_WITH_SYS_TIME
59 # include <sys/time.h>
63 # include <sys/time.h>
84 #include "remoteconf.h"
85 #include "localconf.h"
86 #include "grabmyaddr.h"
87 #include "isakmp_var.h"
92 #include "ipsec_doi.h"
94 #include "crypto_openssl.h"
96 #include "isakmp_ident.h"
97 #include "isakmp_agg.h"
98 #include "isakmp_quick.h"
99 #include "isakmp_inf.h"
100 #include "vpn_control.h"
101 #include "vpn_control_var.h"
103 #include "vendorid.h"
104 #include "isakmp_xauth.h"
105 #include "isakmp_unity.h"
106 #include "isakmp_cfg.h"
109 #include "isakmp_frag.h"
111 #include "strnames.h"
116 # include "nattraversal.h"
118 #include "ike_session.h"
119 # include <netinet/in.h>
120 # include <netinet/udp.h>
121 # include <netinet/in_systm.h>
122 # include <netinet/ip.h>
123 # define SOL_UDP IPPROTO_UDP
124 #include "ipsecSessionTracer.h"
125 #include "ipsecMessageTracer.h"
126 #include "power_mgmt.h"
128 extern caddr_t
val2str (const char *, size_t);
129 u_char i_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the i_ck. */
130 u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
133 static void isakmp_main (vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
134 static void ikev1_received_packet(vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
135 static int ikev1_ph1begin_r (ike_session_t
*session
, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*, u_int8_t
);
136 static int ikev1_ph2begin_i (phase1_handle_t
*, phase2_handle_t
*);
137 static int ikev1_ph2begin_r (phase1_handle_t
*, vchar_t
*);
141 static void frag_handler (phase1_handle_t
*, vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
145 * isakmp packet handler
148 isakmp_handler(int so_isakmp
)
150 struct isakmp isakmp
;
152 u_int64_t force_align
; // Wcast-align fix - force alignment
153 char buf
[sizeof (isakmp
) + 4];
154 u_int32_t non_esp
[2];
155 char lbuf
[sizeof(struct udphdr
) +
159 struct sockaddr_storage remote
;
160 struct sockaddr_storage local
;
161 unsigned int remote_len
= sizeof(remote
);
162 unsigned int local_len
= sizeof(local
);
166 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
169 if (slept_at
|| woke_at
) {
170 plog(ASL_LEVEL_DEBUG
, /* this log is high volume */
171 "ignoring isakmp port until power-mgmt event is handled.\n");
175 /* read message by MSG_PEEK */
176 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
177 MSG_PEEK
, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
181 "failed to receive isakmp packet: %s\n",
186 /* keep-alive packet - ignore */
187 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
188 /* Pull the keep-alive packet */
189 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
190 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
192 "failed to receive keep alive packet: %s\n",
199 /* we don't know about portchange yet,
200 look for non-esp marker instead */
201 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
202 extralen
= NON_ESP_MARKER_LEN
;
204 /* now we know if there is an extra non-esp
205 marker at the beginning or not */
206 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
208 /* check isakmp header length, as well as sanity of header length */
209 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
211 "packet shorter than isakmp header size (size: %zu, minimum expected: %zu)\n", len
, sizeof(isakmp
));
213 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
214 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
216 "failed to receive isakmp packet: %s\n",
222 /* reject it if the size is tooooo big. */
223 if (ntohl(isakmp
.len
) > 0xffff) {
225 "the length in the isakmp header is too big.\n");
226 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
227 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
229 "failed to receive isakmp packet: %s\n",
235 /* read real message */
236 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
238 "failed to allocate reading buffer (%u Bytes)\n",
239 ntohl(isakmp
.len
) + extralen
);
241 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
242 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
244 "failed to receive isakmp packet: %s\n",
246 error
= -2; /* serious problem with socket */
251 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
252 0, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
256 "failed to receive isakmp packet: %s\n",
261 if (len
< extralen
) {
263 "invalid len (%zd Bytes) & extralen (%d Bytes)\n",
268 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
270 "failed to allocate reading buffer (%lu Bytes)\n",
275 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
280 plog(ASL_LEVEL_ERR
, "received invalid length (%zd != %zu), why ?\n",
285 plog(ASL_LEVEL_DEBUG
, "%zd bytes message received %s\n",
286 len
, saddr2str_fromto("from %s to %s",
287 (struct sockaddr
*)&remote
,
288 (struct sockaddr
*)&local
));
290 /* avoid packets with malicious port/address */
291 switch (remote
.ss_family
) {
293 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
297 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
302 "invalid family: %d\n", remote
.ss_family
);
307 "src port == 0 (valid as UDP but not with IKE)\n");
311 /* XXX: check sender whether to be allowed or not to accept */
313 /* XXX: I don't know how to check isakmp half connection attack. */
315 /* simply reply if the packet was processed. */
317 if (ike_session_check_recvdpkt(&remote
, &local
, buf
)) {
318 IPSECLOGASLMSG("Received retransmitted packet from %s.\n",
319 saddr2str((struct sockaddr
*)&remote
));
321 plog(ASL_LEVEL_NOTICE
,
322 "the packet is retransmitted by %s.\n",
323 saddr2str((struct sockaddr
*)&remote
));
328 /* isakmp main routine */
329 isakmp_main(buf
, &remote
, &local
);
341 * main processing to handle isakmp payload
344 isakmp_main(vchar_t
*msg
, struct sockaddr_storage
*remote
, struct sockaddr_storage
*local
)
346 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
347 u_int8_t isakmp_version
= isakmp
->v
;
349 #ifdef HAVE_PRINT_ISAKMP_C
350 isakmp_printpacket(msg
, remote
, local
, 0);
353 /* the initiator's cookie must not be zero */
354 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
356 "malformed cookie received.\n");
360 /* Check the Major and Minor Version fields. */
362 * XXX Is is right to check version here ?
363 * I think it may no be here because the version depends
364 * on exchange status.
366 if (ISAKMP_GETMAJORV(isakmp_version
) != ISAKMP_MAJOR_VERSION_IKEV1
) {
367 plog(ASL_LEVEL_ERR
, "invalid major version %d.\n", isakmp_version
);
372 #if ISAKMP_MINOR_VERSION > 0 //%%%%%%%% fix this
373 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) { //%%%%%%%%%%%%%%% ??????
375 "invalid minor version %d.\n",
376 ISAKMP_GETMINORV(isakmp
->v
));
382 if (isakmp_version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
383 /* check the Flags field. */
384 /* XXX How is the exclusive check, E and A ? */
385 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
386 plog(ASL_LEVEL_ERR
, "invalid flag 0x%02x.\n", isakmp
->flags
);
390 /* ignore commit bit. */
391 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
392 if (isakmp
->msgid
== 0) {
393 isakmp_info_send_nx(isakmp
, remote
, local
,
394 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
395 plog(ASL_LEVEL_ERR
, "Commit bit on Phase 1 forbidden.\n");
400 ikev1_received_packet(msg
, local
, remote
);
406 * ikev1_received_packet
407 * Handler for received IKEv1 Packets
410 ikev1_received_packet(vchar_t
*msg
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
412 ike_session_t
*session
;
413 phase1_handle_t
*iph1
;
415 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
416 isakmp_index
*index
= (isakmp_index
*)isakmp
;
418 session
= ike_session_get_session(local
, remote
, 0, index
);
420 session
= ike_session_get_session(local
, remote
, 1, NULL
);
423 plog (ASL_LEVEL_NOTICE
, "failed to allocate or find ike session.\n");
427 iph1
= ike_session_getph1byindex(session
, index
);
430 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
431 iph1
->side
== INITIATOR
) {
432 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
433 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
434 CONSTSTR("Malformed or unexpected cookie"),
435 CONSTSTR("Failed to process packet (malformed/unexpected cookie)"));
436 plog(ASL_LEVEL_NOTICE
,
437 "Malformed cookie received or "
438 "the initiator's cookies collide.\n");
443 /* Floating ports for NAT-T */
444 if (NATT_AVAILABLE(iph1
) &&
445 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
446 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
447 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
449 /* prevent memory leak */
450 racoon_free(iph1
->remote
);
451 racoon_free(iph1
->local
);
455 /* copy-in new addresses */
456 iph1
->remote
= dupsaddr(remote
);
457 if (iph1
->remote
== NULL
) {
458 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
459 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
460 CONSTSTR("Failed to duplicate remote address"),
461 CONSTSTR("Failed to process Phase 1 message (can't duplicate remote address"));
463 "Phase 1 failed: dupsaddr failed.\n");
466 iph1
->local
= dupsaddr(local
);
467 if (iph1
->local
== NULL
) {
468 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
469 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
470 CONSTSTR("Failed to duplicate local address"),
471 CONSTSTR("Failed to process Phase 1 message (can't duplicate local address"));
473 "Phase 1 failed: dupsaddr failed.\n");
477 /* set the flag to prevent further port floating
478 (FIXME: should we allow it? E.g. when the NAT gw
480 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
482 /* print some neat info */
483 plog (ASL_LEVEL_NOTICE
,
484 "NAT-T: ports changed to: %s\n",
485 saddr2str_fromto("%s<->%s", (struct sockaddr
*)iph1
->remote
, (struct sockaddr
*)iph1
->local
));
488 /* must be same addresses in one stream of a phase at least. */
489 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
490 char *saddr_db
, *saddr_act
;
492 saddr_db
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
493 saddr_act
= racoon_strdup(saddr2str((struct sockaddr
*)remote
));
494 STRDUP_FATAL(saddr_db
);
495 STRDUP_FATAL(saddr_act
);
497 plog(ASL_LEVEL_WARNING
,
498 "Remote address mismatched. db=%s, act=%s\n",
499 saddr_db
, saddr_act
);
501 racoon_free(saddr_db
);
502 racoon_free(saddr_act
);
506 * don't check of exchange type here because other type will have
507 * the same index, for example, informational exchange.
510 // received ike packets: update dpd checks
511 isakmp_reschedule_info_monitor_if_pending(iph1
, "IKE packet received from peer");
516 // Check exchange type and process accordingly
518 switch (isakmp
->etype
) {
520 case ISAKMP_ETYPE_IDENT
:
521 case ISAKMP_ETYPE_AGG
:
523 /* phase 1 validity check */
524 if (isakmp
->msgid
!= 0) {
525 plog(ASL_LEVEL_ERR
, "Message id should be zero in Phase 1.\n");
529 /* search for isakmp status record of phase 1 */
532 * the packet must be the 1st message from a initiator
533 * or the 2nd message from the responder.
536 /* search for phase1 handle by index without r_ck */
537 iph1
= ike_session_getph1byindex0(session
, index
);
539 /*it must be the 1st message from a initiator.*/
540 if (memcmp(&isakmp
->r_ck
, r_ck0
,
541 sizeof(cookie_t
)) != 0) {
543 plog(ASL_LEVEL_NOTICE
, "Malformed cookie received "
544 "or the spi expired.\n");
548 /* Initiation of new exchange */
549 ikev1_ph1begin_r(session
, msg
, remote
, local
, isakmp
->etype
);
555 * Don't delete phase 1 handler for mismatch
556 * because of no authentication has been completed.
558 if (iph1
->etype
!= isakmp
->etype
) {
559 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
560 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
561 CONSTSTR("Mismatched exchange type"),
562 CONSTSTR("Failed to process Phase 1 message (mismatched exchange type)"));
564 "Exchange type is mismatched: "
565 "db=%s packet=%s, ignore it.\n",
566 s_isakmp_etype(iph1
->etype
),
567 s_isakmp_etype(isakmp
->etype
));
571 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
572 frag_handler(iph1
, msg
, remote
, local
);
575 fsm_ikev1_phase1_process_payloads(iph1
, msg
);
579 case ISAKMP_ETYPE_INFO
:
580 case ISAKMP_ETYPE_ACKINFO
:
583 * iph1 must be present for Information message.
584 * if iph1 is null then trying to get the phase1 status
585 * as the packet from responder againt initiator's 1st
586 * exchange in phase 1.
587 * NOTE: We think such informational exchange should be ignored.
590 iph1
= ike_session_getph1byindex0(session
, index
);
592 plog(ASL_LEVEL_ERR
, "Unknown Informational exchange received.\n");
595 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
596 plog(ASL_LEVEL_WARNING
,
597 "Remote address mismatched. "
599 saddr2str((struct sockaddr
*)iph1
->remote
));
602 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
603 return frag_handler(iph1
, msg
, remote
, local
);
605 if (isakmp_info_recv(iph1
, msg
) < 0)
610 case ISAKMP_ETYPE_QUICK
:
612 u_int32_t msgid
= isakmp
->msgid
;
613 phase2_handle_t
*iph2
;
616 isakmp_info_send_nx(isakmp
, remote
, local
,
617 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
618 plog(ASL_LEVEL_ERR
, "Can't start the quick mode, "
619 "there is no ISAKMP-SA, %s\n", isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
624 /* Reinit the IVM if it's still there */
625 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
626 oakley_delivm(iph1
->mode_cfg
->ivm
);
627 iph1
->mode_cfg
->ivm
= NULL
;
630 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
) {
631 frag_handler(iph1
, msg
, remote
, local
);
635 /* check status of phase 1 whether negotiated or not. */
636 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
637 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
638 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
639 CONSTSTR("Can't start Phase 2 without valid Phase 1"),
640 CONSTSTR("Failed to start Phase 2 responder (no established Phase 1"));
641 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
642 "there is no valid ISAKMP-SA, %s\n", isakmp_pindex(&iph1
->index
, iph1
->msgid
));
646 /* search isakmp phase 2 stauts record. */
647 iph2
= ike_session_getph2bymsgid(iph1
, msgid
);
649 /* it must be new negotiation as responder */
650 ikev1_ph2begin_r(iph1
, msg
);
656 * we keep to set commit bit during negotiation.
657 * When SA is configured, bit will be reset.
659 * don't initiate commit bit. should be fixed in the future.
661 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
662 iph2
->flags
|= ISAKMP_FLAG_C
;
664 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
) &&
665 (iph2
->ph1
== NULL
|| iph2
->ph1
->approval
== NULL
)) {
666 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
667 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
668 CONSTSTR("Can't continue Phase 2 without valid Phase 1"),
669 CONSTSTR("Failed to continue Phase 2 resonder (invalid linked Phase 1"));
670 plog(ASL_LEVEL_ERR
, "can't start the quick mode, "
671 "invalid linked ISAKMP-SA\n");
674 fsm_ikev1_phase2_process_payloads(iph2
, msg
);
678 case ISAKMP_ETYPE_CFG
:
682 "mode config %d from %s, "
683 "but we have no ISAKMP-SA.\n",
684 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
687 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
689 "mode config %d from %s, "
690 "but ISAKMP-SA %s isn't established.\n",
691 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
),
692 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
695 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
696 return frag_handler(iph1
, msg
, remote
, local
);
697 isakmp_cfg_r(iph1
, msg
);
701 case ISAKMP_ETYPE_NEWGRP
:
702 case ISAKMP_ETYPE_AUTH
:
703 case ISAKMP_ETYPE_NONE
:
706 "Invalid exchange type %d from %s.\n",
707 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
712 /* new negotiation of phase 1 for initiator */
714 ikev1_ph1begin_i(ike_session_t
*session
, struct remoteconf
*rmconf
, struct sockaddr_storage
*remote
,
715 struct sockaddr_storage
*local
, int started_by_api
, nw_nat64_prefix_t
*nat64_prefix
)
718 phase1_handle_t
*iph1
;
720 struct timeval start
, end
;
723 if (session
== NULL
) {
724 session
= ike_session_get_session(local
, remote
, 1, NULL
);
726 plog (ASL_LEVEL_NOTICE
, "failed to allocate or find ike session.\n");
731 /* get new entry to isakmp status table. */
732 iph1
= ike_session_newph1(ISAKMP_VERSION_NUMBER_IKEV1
);
736 iph1
->rmconf
= rmconf
;
737 retain_rmconf(iph1
->rmconf
);
738 iph1
->side
= INITIATOR
;
739 iph1
->started_by_api
= started_by_api
;
740 if (nat64_prefix
!= NULL
) {
741 memcpy(&iph1
->nat64_prefix
, nat64_prefix
, sizeof(*nat64_prefix
));
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_NOTICE
, "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
));
799 plog(ASL_LEVEL_NOTICE
,
800 "initiate new phase 1 negotiation: %s<=>%s\n",
801 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
804 plog(ASL_LEVEL_NOTICE
,
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
);
886 ike_session_delph1(iph1
);
892 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
893 /* don't call remph1(iph1) until after insph1(iph1) is called */
894 ike_session_delph1(iph1
);
900 iph1
->frag_chain
= NULL
;
901 iph1
->approval
= NULL
;
903 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
904 * We have to setup this flag now to correctly generate the first reply.
905 * Don't know if a better check could be done for that ?
907 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
908 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
910 /* copy remote address */
911 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
912 /* don't call remph1(iph1) until after insph1(iph1) is called */
913 iph1
= NULL
; /* deleted in copy_ph1addresses */
917 if (ike_session_link_phase1(session
, iph1
) != 0) {
918 ike_session_delph1(iph1
);
922 plog(ASL_LEVEL_DEBUG
, "===\n");
926 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
929 plog(ASL_LEVEL_NOTICE
,
930 "respond new phase 1 negotiation: %s<=>%s\n",
931 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
934 plog(ASL_LEVEL_NOTICE
,
935 "begin %s mode.\n", s_isakmp_etype(etype
));
938 gettimeofday(&iph1
->start
, NULL
);
939 gettimeofday(&start
, NULL
);
942 IPSECLOGASLMSG("IPSec Phase 1 started (Initiated by peer).\n");
944 /* now that we have a phase1 handle, feed back into our
945 * main receive function to catch fragmented packets
947 isakmp_main(msg
, remote
, local
);
951 /* new negotiation of phase 2 for initiator */
953 ikev1_ph2begin_i(phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
957 if (xauth_check(iph1
) != 0) {
959 "Attempt to start phase 2 whereas Xauth failed\n");
964 /* found ISAKMP-SA. */
965 plog(ASL_LEVEL_DEBUG
, "===\n");
966 plog(ASL_LEVEL_DEBUG
, "begin QUICK mode.\n");
969 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
972 plog(ASL_LEVEL_NOTICE
,
973 "initiate new phase 2 negotiation: %s<=>%s\n",
974 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
979 gettimeofday(&iph2
->start
, NULL
);
983 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
984 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
986 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by me).\n");
987 if (quick_iprep(iph2
, NULL
))
990 #ifdef ENABLE_VPNCONTROL_PORT
991 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
997 /* new negotiation of phase 2 for responder */
999 ikev1_ph2begin_r(phase1_handle_t
*iph1
, vchar_t
*msg
)
1001 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1002 phase2_handle_t
*iph2
= 0;
1005 struct timeval start
, end
;
1007 #ifdef ENABLE_HYBRID
1008 if (xauth_check(iph1
) != 0) {
1010 "Attempt to start Phase 2 whereas Xauth failed\n");
1015 iph2
= ike_session_newph2(ISAKMP_VERSION_NUMBER_IKEV1
, PHASE2_TYPE_SA
);
1018 "failed to allocate Phase 2 entry.\n");
1022 iph2
->side
= RESPONDER
;
1023 iph2
->version
= ISAKMP_VERSION_NUMBER_IKEV1
;
1024 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_R_START
);
1025 iph2
->flags
= isakmp
->flags
;
1026 iph2
->msgid
= isakmp
->msgid
;
1027 iph2
->seq
= pk_getseq();
1028 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1029 if (iph2
->ivm
== NULL
) {
1030 ike_session_delph2(iph2
);
1033 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
1034 if (iph2
->dst
== NULL
) {
1035 ike_session_delph2(iph2
);
1038 switch (iph2
->dst
->ss_family
) {
1041 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1047 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1053 "invalid family: %d\n", iph2
->dst
->ss_family
);
1054 ike_session_delph2(iph2
);
1058 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1059 if (iph2
->src
== NULL
) {
1060 ike_session_delph2(iph2
);
1063 switch (iph2
->src
->ss_family
) {
1066 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1072 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1078 "invalid family: %d\n", iph2
->src
->ss_family
);
1079 ike_session_delph2(iph2
);
1083 if (ike_session_link_ph2_to_ph1(iph1
, iph2
))
1086 memcpy(&iph2
->nat64_prefix
, &iph1
->nat64_prefix
, sizeof(iph2
->nat64_prefix
));
1088 plog(ASL_LEVEL_DEBUG
, "===\n");
1092 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1095 plog(ASL_LEVEL_NOTICE
,
1096 "respond new phase 2 negotiation: %s<=>%s\n",
1097 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1102 gettimeofday(&start
, NULL
);
1105 IPSECLOGASLMSG("IPSec Phase 2 started (Initiated by peer).\n");
1107 error
= fsm_ikev1_phase2_process_payloads(iph2
, msg
);
1111 #ifdef ENABLE_VPNCONTROL_PORT
1112 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1119 ikev1_phase1_established(phase1_handle_t
*iph1
)
1122 u_int rekey_lifetime
;
1123 int ini_contact
= iph1
->rmconf
->ini_contact
;
1126 gettimeofday(&iph1
->end
, NULL
);
1127 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1128 "Phase 1", s_isakmp_etype(iph1
->etype
),
1129 timedelta(&iph1
->start
, &iph1
->end
));
1132 #ifdef ENABLE_VPNCONTROL_PORT
1134 if (iph1
->side
== RESPONDER
&&
1135 iph1
->local
->ss_family
== AF_INET
) {
1137 struct redirect
*addr
;
1139 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
1140 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
1141 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
1143 if (raddr
== NULL
) {
1145 "failed to send redirect message - memory error.\n");
1147 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
1148 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
1149 plog(ASL_LEVEL_NOTICE
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
1152 (void)ike_session_update_ph1_ph2tree(iph1
);
1153 isakmp_ph1expire(iph1
);
1161 /* save created date. */
1162 (void)time(&iph1
->created
);
1164 /* add to the schedule to expire, and save back pointer. */
1165 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
1166 isakmp_ph1expire_stub
, iph1
);
1168 if (iph1
->rmconf
->initiate_ph1rekey
) {
1169 if (iph1
->side
== INITIATOR
) {
1170 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
1174 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
1178 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
1179 iph1
->approval
->lifetime
);
1180 if (rekey_lifetime
) {
1181 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
1182 isakmp_ph1rekeyexpire_stub
,
1185 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
1189 "failed to get rekey timer - lifetime is too small... probably.\n");
1193 #ifdef ENABLE_HYBRID
1194 /* ignore xauth if it is a rekey */
1195 if (!iph1
->is_rekey
&&
1196 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1197 switch(AUTHMETHOD(iph1
)) {
1198 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1199 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1200 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1201 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1202 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1203 xauth_sendreq(iph1
);
1204 /* XXX Don't process INITIAL_CONTACT */
1213 /* Schedule the r_u_there.... */
1214 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1215 isakmp_sched_r_u(iph1
, 0);
1218 /* INITIAL-CONTACT processing */
1219 /* ignore initial-contact if it is a rekey */
1220 /* don't send anything if local test mode. */
1221 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
1222 /* send INITIAL-CONTACT */
1223 isakmp_info_send_n1(iph1
,
1224 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1225 /* insert a node into contacted list. */
1226 if (ike_session_inscontacted(iph1
->remote
) == -1) {
1228 "failed to add contacted list.\n");
1233 log_ph1established(iph1
);
1234 plog(ASL_LEVEL_DEBUG
, "===\n");
1236 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1238 #ifdef ENABLE_VPNCONTROL_PORT
1239 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1240 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1247 * parse ISAKMP payloads, without ISAKMP base header.
1250 isakmp_parsewoh(np0
, gen
, len
)
1252 struct isakmp_gen
*gen
;
1255 u_char np
= np0
& 0xff;
1258 struct isakmp_parse_t
*p
, *ep
;
1260 plog(ASL_LEVEL_DEBUG
, "begin.\n");
1263 * 5 is a magic number, but any value larger than 2 should be fine
1264 * as we do vrealloc() in the following loop.
1266 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1267 if (result
== NULL
) {
1269 "failed to get buffer.\n");
1272 // Wcast-align fix (void*) - result = aligned buffer of struct isakmp_parse_t
1273 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1274 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1278 /* parse through general headers */
1279 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1280 if (tlen
<= sizeof(struct isakmp_gen
)) {
1281 /* don't send information, see isakmp_ident_r1() */
1283 "isakmp_parsewoh invalid length of payload (1)\n");
1288 plog(ASL_LEVEL_NOTICE
,
1289 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1292 p
->len
= ntohs(gen
->len
);
1293 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1294 plog(ASL_LEVEL_NOTICE
,
1295 "isakmp_parsewoh invalid length of payload (2)\n");
1304 off
= p
- ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1305 result
= vrealloc(result
, result
->l
* 2);
1306 if (result
== NULL
) {
1307 plog(ASL_LEVEL_NOTICE
,
1308 "failed to realloc buffer.\n");
1312 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)
1313 (result
->v
+ result
->l
- sizeof(*ep
));
1314 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1319 plen
= ntohs(gen
->len
);
1320 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1323 p
->type
= ISAKMP_NPTYPE_NONE
;
1327 plog(ASL_LEVEL_DEBUG
, "succeed.\n");
1333 * parse ISAKMP payloads, including ISAKMP base header.
1339 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1340 struct isakmp_gen
*gen
;
1346 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1347 tlen
= buf
->l
- sizeof(struct isakmp
);
1348 result
= isakmp_parsewoh(np
, gen
, tlen
);
1357 ike_session_initctdtree();
1358 ike_session_init_recvdpkt();
1360 if (isakmp_open() < 0)
1373 ike_session_clear_recvdpkt();
1374 ike_session_clear_contacted();
1378 * make strings containing i_cookie + r_cookie + msgid
1381 isakmp_pindex(index
, msgid
)
1382 const isakmp_index
*index
;
1383 const u_int32_t msgid
;
1385 static char buf
[64];
1389 memset(buf
, 0, sizeof(buf
));
1392 p
= (const u_char
*)index
;
1393 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1394 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1406 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1412 /* open ISAKMP sockets. */
1417 int ifnum
= 0, encap_ifnum
= 0;
1422 int tentative_failures
= 0;
1425 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1428 if (p
->sock
!= -1) {
1432 continue; // socket already open
1435 /* warn if wildcard address - should we forbid this? */
1436 switch (p
->addr
->ss_family
) {
1438 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1439 plog(ASL_LEVEL_WARNING
,
1440 "listening to wildcard address,"
1441 "broadcast IKE packet may kill you\n");
1445 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1446 plog(ASL_LEVEL_WARNING
,
1447 "listening to wildcard address, "
1448 "broadcast IKE packet may kill you\n");
1453 "unsupported address family %d\n",
1454 lcconf
->default_af
);
1459 if (p
->addr
->ss_family
== AF_INET6
&&
1460 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1461 p
->addr
)->sin6_addr
))
1463 plog(ASL_LEVEL_NOTICE
,
1464 "Ignoring multicast address %s\n",
1465 saddr2str((struct sockaddr
*)p
->addr
));
1466 racoon_free(p
->addr
);
1472 if ((p
->sock
= socket(p
->addr
->ss_family
, SOCK_DGRAM
, 0)) < 0) {
1474 "socket (%s)\n", strerror(errno
));
1478 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1479 plog(ASL_LEVEL_ERR
, "failed to put socket in non-blocking mode\n");
1481 /* receive my interface address on inbound packets. */
1482 switch (p
->addr
->ss_family
) {
1484 int ifindex
= if_nametoindex(p
->ifname
);
1486 setsockopt(p
->sock
, IPPROTO_IP
,
1487 IP_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1489 "setsockopt IP_BOUND_IF (%s)\n",
1493 if (setsockopt(p
->sock
, IPPROTO_IP
,
1495 (const void *)&yes
, sizeof(yes
)) < 0) {
1497 "setsockopt IP_RECVDSTADDR (%s)\n",
1506 pktinfo
= IPV6_RECVPKTINFO
;
1508 pktinfo
= IPV6_RECVDSTADDR
;
1510 int ifindex
= if_nametoindex(p
->ifname
);
1512 setsockopt(p
->sock
, IPPROTO_IPV6
,
1513 IPV6_BOUND_IF
, &ifindex
, sizeof(ifindex
)) < 0) {
1515 "setsockopt IPV6_BOUND_IF (%s)\n",
1519 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1520 (const void *)&yes
, sizeof(yes
)) < 0)
1523 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1524 pktinfo
, strerror(errno
));
1532 #ifdef IPV6_USE_MIN_MTU
1533 if (p
->addr
->ss_family
== AF_INET6
&&
1534 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1535 (void *)&yes
, sizeof(yes
)) < 0) {
1537 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1543 if (setsockopt_bypass(p
->sock
, p
->addr
->ss_family
) < 0)
1546 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEADDR
, (const void *)&yes
, sizeof(yes
)) < 0) {
1547 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEADDR (%s)\n", strerror(errno
));
1551 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_REUSEPORT
, (const void *)&yes
, sizeof(yes
)) < 0) {
1552 plog(ASL_LEVEL_ERR
, "setsockopt SO_REUSEPORT (%s)\n", strerror(errno
));
1556 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1557 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1558 (void *)&yes
, sizeof(yes
)) < 0) {
1560 "setsockopt (%s)\n", strerror(errno
));
1565 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1566 int tmp_errno
= errno
;
1568 "failed to bind to address %s (%s).\n",
1569 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1571 // if bind failed b/c of a tentative v6 address, try again later
1572 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1573 struct in6_ifreq ifr6
;
1575 bzero(&ifr6
, sizeof(ifr6
));
1576 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1577 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1578 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1580 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1581 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1583 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1584 // address may have been tentantive... invalidate sock but leave address around for another try later
1586 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1587 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1590 tentative_failures
= 1;
1594 "failed to bind to address %s: because of interface address error, flags %x.\n",
1595 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1599 "failed to bind to address %s: can't read interface address flags.\n",
1600 saddr2str((struct sockaddr
*)p
->addr
));
1615 if (p
->source
!= NULL
) {
1616 dispatch_source_cancel(p
->source
);
1619 p
->source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, p
->sock
, 0, dispatch_get_main_queue());
1620 if (p
->source
== NULL
) {
1621 plog(ASL_LEVEL_ERR
, "could not create isakmp socket source.");
1624 dispatch_source_set_event_handler(p
->source
,
1628 dispatch_source_t the_source
= p
->source
;
1629 dispatch_source_set_cancel_handler(p
->source
,
1632 dispatch_release(the_source
);
1633 }); dispatch_resume(p
->source
);
1635 plog(ASL_LEVEL_NOTICE
,
1636 "%s used as isakmp port (fd=%d)\n",
1637 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
1641 racoon_free(p
->addr
);
1644 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
) {
1653 "no address could be bound.\n");
1659 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
1660 plog(ASL_LEVEL_WARNING
,
1661 "NAT-T is enabled in at least one remote{} section,\n");
1662 plog(ASL_LEVEL_WARNING
,
1663 "but no 'isakmp_natt' address was specified!\n");
1667 if (tentative_failures
)
1668 sched_new(5, update_myaddrs
, NULL
);
1674 isakmp_suspend_sockets()
1678 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1680 dispatch_suspend(p
->source
);
1687 isakmp_close_sockets();
1692 isakmp_close_sockets()
1696 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1702 dispatch_source_cancel(p
->source
);
1711 // close sockets for addresses that have gone away
1713 isakmp_close_unused()
1715 struct myaddrs
*p
, *next
, **prev
;
1717 prev
= &(lcconf
->myaddrs
);
1718 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1720 if (p
->in_use
== 0) { // not in use ?
1722 dispatch_source_cancel(p
->source
);
1733 isakmp_send(iph1
, sbuf
)
1734 phase1_handle_t
*iph1
;
1739 vchar_t
*vbuf
= NULL
;
1742 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
1746 * Do not add the non ESP marker for a packet that will
1747 * be fragmented. The non ESP marker should appear in
1748 * all fragment's packets, but not in the fragmented packet
1750 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
1754 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1756 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1757 must added just before the packet itself. For this we must
1758 allocate a new buffer and release it at the end. */
1760 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
1762 "vbuf allocation failed\n");
1765 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
1766 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
1771 /* select the socket to be sent */
1772 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
1779 plog (ASL_LEVEL_DEBUG
, "%zu bytes %s\n", sbuf
->l
,
1780 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
1783 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1784 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
1786 "isakmp_sendfrags failed\n");
1794 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1795 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1797 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1810 /* called from scheduler */
1812 isakmp_ph1resend_stub(p
)
1815 phase1_handle_t
*iph1
;
1817 iph1
=(phase1_handle_t
*)p
;
1818 if(isakmp_ph1resend(iph1
) < 0){
1820 /* Should not happen...
1822 SCHED_KILL(iph1
->scr
);
1825 ike_session_unlink_phase1(iph1
);
1830 isakmp_ph1resend(iph1
)
1831 phase1_handle_t
*iph1
;
1833 time_t retry_interval
;
1835 // make sure there is a buffer to send
1836 // isakmp_plist_set_all() could have returned NULL
1837 if (iph1
->sendbuf
== NULL
)
1840 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
1842 if (iph1
->retry_counter
<= 0) {
1843 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1844 IPSECSESSIONEVENTCODE_IKEV1_PH1_MAX_RETRANSMIT
,
1845 CONSTSTR("Phase 1 Maximum Retransmits"),
1846 CONSTSTR("Phase 1 negotiation failed (Maximum retransmits)"));
1849 "Phase 1 negotiation failed due to time up. %s\n",
1850 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1851 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
1852 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
1853 if (isakmp_ph1rekeyretry(iph1
) == 0)
1858 ike_session_ph1_retransmits(iph1
);
1861 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
1862 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1863 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1864 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1865 CONSTSTR("Phase 1 Retransmit"),
1866 CONSTSTR("Failed to retrasmit Phase1"));
1869 "Phase 1 negotiation failed due to send error. %s\n",
1870 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1874 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
1875 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1876 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1877 CONSTSTR("Phase 1 Retransmit"),
1881 plog(ASL_LEVEL_NOTICE
,
1882 "Resend Phase 1 packet %s\n",
1883 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1885 iph1
->retry_counter
--;
1886 retry_interval
= ike_session_get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
1887 iph1
->rmconf
->retry_interval
);
1888 iph1
->scr
= sched_new(retry_interval
,
1889 isakmp_ph1resend_stub
, iph1
);
1894 /* called from scheduler */
1896 isakmp_ph2resend_stub(p
)
1899 phase2_handle_t
*iph2
;
1901 iph2
=(phase2_handle_t
*)p
;
1903 if(isakmp_ph2resend(iph2
) < 0){
1904 ike_session_unlink_phase2(iph2
);
1909 isakmp_ph2resend(iph2
)
1910 phase2_handle_t
*iph2
;
1912 time_t retry_interval
;
1914 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
1916 //%%% BUG FIX - related to commit bit usage - crash happened here
1917 if (iph2
->ph1
== 0) {
1919 "Internal error - attempt to re-send Phase 2 with no Phase 1 bound.\n");
1923 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)){
1924 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1925 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1926 CONSTSTR("Underlying Phase 1 expired"),
1927 CONSTSTR("Failed to retransmit Phase 2 (underlying Phase 1 expired)"));
1929 "Phase 2 negotiation failed due to Phase 1 expired. %s\n",
1930 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1934 if (iph2
->retry_counter
<= 0) {
1935 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1936 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
1937 CONSTSTR("Phase 2 maximum retransmits"),
1938 CONSTSTR("Phase 2 negotiation failed (maximum retransmits)"));
1940 "Phase 2 negotiation failed due to time up. %s\n",
1941 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1944 ike_session_ph2_retransmits(iph2
);
1947 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
1948 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1949 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1950 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1951 CONSTSTR("Phase 2 Retransmit"),
1952 CONSTSTR("Failed to retransmit Phase2 message"));
1955 "Phase 2 negotiation failed due to send error. %s\n",
1956 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1960 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
1961 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
1962 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1963 CONSTSTR("Phase 2 Retransmit"),
1967 plog(ASL_LEVEL_NOTICE
,
1968 "Resend Phase 2 packet %s\n",
1969 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1971 iph2
->retry_counter
--;
1972 retry_interval
= ike_session_get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
1973 iph2
->ph1
->rmconf
->retry_interval
);
1974 iph2
->scr
= sched_new(retry_interval
,
1975 isakmp_ph2resend_stub
, iph2
);
1979 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
1980 "Phase 2 packets sent to peer: retransmit timer armed");
1987 /* called from scheduler */
1989 isakmp_ph1expire_stub(p
)
1993 isakmp_ph1expire((phase1_handle_t
*)p
);
1997 isakmp_ph1expire(iph1
)
1998 phase1_handle_t
*iph1
;
2001 phase1_handle_t
*new_iph1
;
2003 SCHED_KILL(iph1
->sce
);
2005 SCHED_KILL(iph1
->dpd_r_u
);
2008 if(!FSM_STATE_IS_EXPIRED(iph1
->status
)){
2009 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2010 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2014 plog(ASL_LEVEL_NOTICE
,
2015 "ISAKMP-SA expired %s-%s spi:%s\n",
2017 isakmp_pindex(&iph1
->index
, 0));
2020 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
2021 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
2025 * the phase1 deletion is postponed until there is no phase2.
2027 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2028 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2032 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2035 /* called from scheduler */
2037 isakmp_ph1rekeyexpire_stub(p
)
2041 isakmp_ph1rekeyexpire((phase1_handle_t
*)p
, FALSE
);
2045 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
2046 phase1_handle_t
*iph1
;
2047 int ignore_sess_drop_policy
;
2050 struct remoteconf
*rmconf
;
2052 SCHED_KILL(iph1
->sce_rekey
);
2054 // We are going to start the rekey. Let's fire off the
2055 // phase1 expiration timer if it is not done yet.
2056 if (!iph1
->sce
&& iph1
->approval
->lifetimegap
) {
2057 iph1
->sce
= sched_new(iph1
->approval
->lifetimegap
,
2058 isakmp_ph1expire_stub
, iph1
);
2061 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
2062 if (iph1
->sce
== 0 ||
2063 !FSM_STATE_IS_ESTABLISHED(iph1
->status
) ||
2068 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2069 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2073 plog(ASL_LEVEL_NOTICE
,
2074 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2076 isakmp_pindex(&iph1
->index
, 0));
2080 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2084 // exit if there is another ph1 that is established (with a pending rekey timer)
2085 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2086 plog(ASL_LEVEL_NOTICE
,
2087 "Request for ISAKMP-SA rekey was ignored "
2088 "due to another established ph1.\n");
2092 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2093 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2094 plog(ASL_LEVEL_DEBUG
, "Reschedule Phase 1 rekey.\n");
2095 iph1
->sce_rekey
= sched_new(1,
2096 isakmp_ph1rekeyexpire_stub
,
2101 // get rmconf to initiate rekey with
2102 rmconf
= iph1
->rmconf
;
2104 rmconf
= getrmconf(iph1
->remote
);
2107 /* begin quick mode */
2108 plog(ASL_LEVEL_NOTICE
, "Begin Phase 1 rekey.\n");
2110 /* start phase 1 negotiation as a initiator. */
2111 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2112 plog(ASL_LEVEL_NOTICE
, "Phase 1 rekey Failed.\n");
2114 iph1
->is_rekey
= TRUE
;
2117 "Phase1 rekey failed: no configuration found for %s.\n",
2118 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2123 isakmp_ph1rekeyretry(iph1
)
2124 phase1_handle_t
*iph1
;
2127 struct remoteconf
*rmconf
;
2129 // this code path is meant for floated ph1 rekeys that are failing on the first message
2130 if (iph1
->sce
!= 0 ||
2131 iph1
->sce_rekey
!= 0 ||
2132 ((iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
&&
2133 iph1
->status
!= IKEV1_STATE_AGG_I_MSG1SENT
)
2134 || ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0))
2135 || (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
)
2136 || iph1
->is_dying
) {
2140 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2141 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2145 plog(ASL_LEVEL_NOTICE
,
2146 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2148 isakmp_pindex(&iph1
->index
, 0));
2152 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2153 plog(ASL_LEVEL_NOTICE
,
2154 "request for ISAKMP-SA rekey was ignored "
2155 "due to idleness.\n");
2159 // exit if there is another ph1 that is established (with a pending rekey timer)
2160 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2161 plog(ASL_LEVEL_NOTICE
,
2162 "request to retry ISAKMP-SA rekey was ignored "
2163 "due to another established ph1.\n");
2167 // some servers don't like respond to 4500 for rekeys... try accomodate them
2168 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2169 set_port(iph1
->local
, PORT_ISAKMP
);
2171 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2172 set_port(iph1
->remote
, PORT_ISAKMP
);
2174 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2175 rmconf
= getrmconf(iph1
->remote
);
2177 /* begin quick mode */
2178 plog(ASL_LEVEL_NOTICE
, "begin Phase 1 rekey retry.\n");
2180 /* start phase 1 negotiation as a initiator. */
2181 if (ikev1_ph1begin_i(iph1
->parent_session
, rmconf
, iph1
->remote
, iph1
->local
, 0, &iph1
->nat64_prefix
) < 0) {
2182 plog(ASL_LEVEL_NOTICE
, "Phase 1 rekey retry Failed.\n");
2185 iph1
->is_rekey
= TRUE
;
2188 "Phase 1 rekey retry failed: no configuration found for %s.\n",
2189 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2195 /* called from scheduler */
2197 isakmp_ph1delete_stub(p
)
2201 isakmp_ph1delete((phase1_handle_t
*)p
);
2205 isakmp_ph1delete(iph1
)
2206 phase1_handle_t
*iph1
;
2210 SCHED_KILL(iph1
->sce
);
2211 SCHED_KILL(iph1
->sce_rekey
);
2213 SCHED_KILL(iph1
->dpd_r_u
);
2216 if (LIST_FIRST(&iph1
->bound_ph2tree
) != NULL
) {
2217 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2221 isakmp_info_send_d1(iph1
);
2223 /* don't re-negosiation when the phase 1 SA expires. */
2225 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2226 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2230 plog(ASL_LEVEL_NOTICE
,
2231 "ISAKMP-SA deleted spi:%s\n",
2232 isakmp_pindex(&iph1
->index
, 0));
2233 plog(ASL_LEVEL_DEBUG
,
2234 "ISAKMP-SA deleted %s-%s spi:%s\n",
2235 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2239 ike_session_unlink_phase1(iph1
);
2244 /* called from scheduler.
2245 * this function will call only isakmp_ph2delete().
2246 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2247 * by something cause. That's why this function is called after phase 2 SA
2248 * expires in the userland.
2251 isakmp_ph2expire_stub(p
)
2255 isakmp_ph2expire((phase2_handle_t
*)p
);
2259 isakmp_ph2expire(iph2
)
2260 phase2_handle_t
*iph2
;
2264 if (FSM_STATE_IS_EXPIRED(iph2
->status
)) {
2268 SCHED_KILL(iph2
->sce
);
2270 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2271 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2275 plog(ASL_LEVEL_NOTICE
,
2276 "Phase 2 sa expired %s-%s\n", src
, dst
);
2280 // delete outgoing SAs
2281 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
) && iph2
->approval
) {
2284 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2286 pfkey_send_delete(lcconf
->sock_pfkey
,
2287 ipsecdoi2pfkey_proto(pr
->proto_id
),
2289 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2293 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2294 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
2295 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2300 /* called from scheduler */
2302 isakmp_ph2delete_stub(p
)
2306 isakmp_ph2delete((phase2_handle_t
*)p
);
2310 isakmp_ph2delete(iph2
)
2311 phase2_handle_t
*iph2
;
2315 SCHED_KILL(iph2
->sce
);
2317 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2318 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2322 plog(ASL_LEVEL_NOTICE
,
2323 "Phase 2 sa deleted %s-%s\n", src
, dst
);
2327 ike_session_unlink_phase2(iph2
);
2333 * Interface between PF_KEYv2 and ISAKMP
2336 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2337 * if phase1 has been finished, begin phase2.
2340 isakmp_post_acquire(phase2_handle_t
*iph2
)
2342 struct remoteconf
*rmconf
;
2343 phase1_handle_t
*iph1
= NULL
;
2345 plog(ASL_LEVEL_DEBUG
, "In post_acquire\n");
2347 /* search appropreate configuration with masking port. */
2348 rmconf
= getrmconf(iph2
->dst
);
2349 if (rmconf
== NULL
) {
2351 "No configuration found for %s.\n",
2352 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2355 /* if passive mode, ignore the acquire message */
2356 if (rmconf
->passive
) {
2357 plog(ASL_LEVEL_NOTICE
,
2358 "Because of passive mode, ignore the acquire message for %s.\n",
2359 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2364 // what if there is another ph2 that is negotiating
2365 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2366 // TODO: postpone this rekey for a second later
2367 plog(ASL_LEVEL_NOTICE
,
2368 "Request for establishing IPsec-SA was ignored due to another negoing ph2.\n");
2372 // if this is a phase2 rekeys (the policy may not have the current port number).
2373 // so, use the appropriate ports.
2374 if (iph2
->is_rekey
) {
2375 ike_session_update_ph2_ports(iph2
);
2377 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
2378 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2380 /* no IKE-SA found. */
2382 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2384 /* start phase 1 negotiation as a initiator. */
2385 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2387 plog(ASL_LEVEL_NOTICE
,
2388 "IPsec-SA request for %s queued due to no Phase 1 found.\n",
2389 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2391 // exit if there is another ph1 that is established (with a pending rekey timer)
2392 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2393 plog(ASL_LEVEL_NOTICE
,
2394 "Request for Phase 1 was ignored due to another negotiating Phase 1.\n");
2398 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, &iph2
->nat64_prefix
) < 0) {
2399 plog(ASL_LEVEL_NOTICE
,
2400 "Request for Phase 1 failed. Will try later.\n");
2406 /* found ISAKMP-SA, but on negotiation. */
2407 if (!FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2408 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2409 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2410 plog(ASL_LEVEL_NOTICE
,
2411 "Request for establishing IPsec-SA was queued due to no phase1 found.\n");
2416 /* found established ISAKMP-SA */
2418 /* found ISAKMP-SA. */
2420 /* begin quick mode */
2421 plog(ASL_LEVEL_NOTICE
, "Begin QUICK mode.\n");
2422 if (ikev1_ph2begin_i(iph1
, iph2
))
2428 * receive GETSPI from kernel.
2431 isakmp_post_getspi(phase2_handle_t
*iph2
)
2434 struct timeval start
, end
;
2438 /* don't process it because there is no suitable phase1-sa. */
2439 if (FSM_STATE_IS_EXPIRED(iph2
->ph1
->status
)) {
2441 "the negotiation is stopped, "
2442 "because there is no suitable ISAKMP-SA.\n");
2445 fsm_set_state(&iph2
->status
, iph2
->side
== INITIATOR
?
2446 IKEV1_STATE_QUICK_I_GETSPIDONE
: IKEV1_STATE_QUICK_R_GETSPIDONE
);
2449 gettimeofday(&start
, NULL
);
2451 switch (iph2
->side
) {
2453 error
= quick_i1send(iph2
, NULL
);
2456 error
= quick_r2send(iph2
, NULL
);
2460 if (error
) //%%%%%%%% log something ???
2463 gettimeofday(&end
, NULL
);
2464 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
2466 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2467 timedelta(&start
, &end
));
2473 /* called by scheduler */
2475 isakmp_chkph1there_stub(p
)
2478 isakmp_chkph1there((phase2_handle_t
*)p
);
2482 isakmp_chkph1there(iph2
)
2483 phase2_handle_t
*iph2
;
2485 phase1_handle_t
*iph1
;
2487 if ((iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
&& iph2
->status
!= IKEV1_STATE_QUICK_I_START
) ||
2489 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: ph2 handle has advanced too far (status %d, START %d, dying %d)... ignoring\n", iph2
->status
, IKEV1_STATE_QUICK_I_START
, iph2
->is_dying
);
2493 iph2
->retry_checkph1
--;
2495 if (iph2
->retry_checkph1
< 0 /* %%%%||
2496 ike_session_verify_ph2_parent_session(iph2) */) {
2497 if (iph2
->retry_checkph1
< 0) {
2499 "Phase 2 negotiation failed "
2500 "due to time up waiting for Phase 1. %s\n",
2501 sadbsecas2str(iph2
->dst
, iph2
->src
,
2502 iph2
->satype
, 0, 0));
2505 "Phase 2 negotiation failed "
2506 "due to invalid parent session. %s\n",
2507 sadbsecas2str(iph2
->dst
, iph2
->src
,
2508 iph2
->satype
, 0, 0));
2510 plog(ASL_LEVEL_NOTICE
,
2511 "delete Phase 2 handler.\n");
2513 /* send acquire to kernel as error */
2514 pk_sendeacquire(iph2
);
2515 ike_session_unlink_phase2(iph2
);
2519 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2521 /* XXX Even if ph1 as responder is there, should we not start
2522 * phase 2 negotiation ? */
2524 && FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
2525 /* found isakmp-sa */
2527 plog(ASL_LEVEL_DEBUG
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2528 plog(ASL_LEVEL_DEBUG
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2529 plog(ASL_LEVEL_DEBUG
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2530 plog(ASL_LEVEL_DEBUG
, "before:\n");
2531 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2532 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2533 set_port(iph2
->src
, extract_port(iph1
->local
));
2534 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2535 plog(ASL_LEVEL_DEBUG
, "After:\n");
2536 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2537 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2539 /* begin quick mode */
2540 if (ikev1_ph2begin_i(iph1
, iph2
)) {
2541 ike_session_unlink_phase2(iph2
);
2545 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2546 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2547 /* start phase 1 negotiation as a initiator. */
2549 if (ikev1_ph1begin_i(iph2
->parent_session
, rmconf
, iph2
->dst
, iph2
->src
, 0, iph1
!= NULL
? &iph1
->nat64_prefix
: NULL
) < 0) {
2550 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2552 } else if (rmconf
== NULL
) {
2553 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no remoteconf found... failed to initiate new one\n");
2557 plog(ASL_LEVEL_NOTICE
, "CHKPH1THERE: no established ph1 handler found\n");
2559 /* no isakmp-sa found */
2560 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2565 /* copy variable data into ALLOCATED buffer. */
2567 isakmp_set_attr_v(buf
, type
, val
, len
)
2573 struct isakmp_data
*data
;
2575 data
= (struct isakmp_data
*)buf
;
2576 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2577 data
->lorv
= htons((u_int16_t
)len
);
2578 memcpy(data
+ 1, val
, len
);
2580 return buf
+ sizeof(*data
) + len
;
2583 /* copy fixed length data into ALLOCATED buffer. */
2585 isakmp_set_attr_l(buf
, type
, val
)
2590 struct isakmp_data
*data
;
2592 data
= (struct isakmp_data
*)buf
;
2593 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2594 data
->lorv
= htons((u_int16_t
)val
);
2596 return buf
+ sizeof(*data
);
2599 /* add a variable data attribute to the buffer by reallocating it. */
2601 isakmp_add_attr_v(buf0
, type
, val
, len
)
2607 vchar_t
*buf
= NULL
;
2608 struct isakmp_data
*data
;
2612 tlen
= sizeof(*data
) + len
;
2616 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2618 buf
= vmalloc(tlen
);
2621 "failed to get a attribute buffer.\n");
2625 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2626 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2627 data
->lorv
= htons((u_int16_t
)len
);
2628 memcpy(data
+ 1, val
, len
);
2633 /* add a fixed data attribute to the buffer by reallocating it. */
2635 isakmp_add_attr_l(buf0
, type
, val
)
2640 vchar_t
*buf
= NULL
;
2641 struct isakmp_data
*data
;
2645 tlen
= sizeof(*data
);
2649 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2651 buf
= vmalloc(tlen
);
2654 "failed to get a attribute buffer.\n");
2658 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2659 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2660 data
->lorv
= htons((u_int16_t
)val
);
2666 * calculate cookie and set.
2669 isakmp_newcookie(place
, remote
, local
)
2671 struct sockaddr_storage
*remote
;
2672 struct sockaddr_storage
*local
;
2674 vchar_t
*buf
= NULL
, *buf2
= NULL
;
2684 if (remote
->ss_family
!= local
->ss_family
) {
2686 "address family mismatch, remote:%d local:%d\n",
2687 remote
->ss_family
, local
->ss_family
);
2690 switch (remote
->ss_family
) {
2692 alen
= sizeof(struct in_addr
);
2693 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
2694 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
2698 alen
= sizeof(struct in_addr
);
2699 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
2700 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
2705 "invalid family: %d\n", remote
->ss_family
);
2708 blen
= (alen
+ sizeof(u_short
)) * 2
2709 + sizeof(time_t) + lcconf
->secret_size
;
2710 buf
= vmalloc(blen
);
2713 "failed to get a cookie.\n");
2718 /* copy my address */
2719 memcpy(p
, sa1
, alen
);
2721 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
2722 memcpy(p
, &port
, sizeof(u_short
));
2723 p
+= sizeof(u_short
);
2725 /* copy target address */
2726 memcpy(p
, sa2
, alen
);
2728 port
= ((struct sockaddr_in
*)local
)->sin_port
;
2729 memcpy(p
, &port
, sizeof(u_short
));
2730 p
+= sizeof(u_short
);
2734 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
2737 /* copy random value */
2738 buf2
= eay_set_random(lcconf
->secret_size
);
2741 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
2742 p
+= lcconf
->secret_size
;
2745 buf2
= eay_sha1_one(buf
);
2746 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
2748 sa1
= val2str(place
, sizeof (cookie_t
));
2749 plog(ASL_LEVEL_DEBUG
, "new cookie:\n%s\n", sa1
);
2762 * save partner's(payload) data into phhandle.
2765 isakmp_p2ph(buf
, gen
)
2767 struct isakmp_gen
*gen
;
2769 /* XXX to be checked in each functions for logging. */
2771 plog(ASL_LEVEL_WARNING
,
2772 "ignore this payload, same payload type exist.\n");
2776 if (ntohs(gen
->len
) < sizeof(*gen
)) {
2778 "ignore this payload, invalid payload len %d.\n",
2783 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2786 "failed to get buffer.\n");
2789 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2795 isakmp_newmsgid2(iph1
)
2796 phase1_handle_t
*iph1
;
2801 msgid2
= eay_random();
2802 } while (ike_session_getph2bymsgid(iph1
, msgid2
));
2808 * set values into allocated buffer of isakmp header for phase 1
2811 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
2813 phase1_handle_t
*iph1
;
2819 struct isakmp
*isakmp
;
2821 if (vbuf
->l
< sizeof(*isakmp
))
2824 isakmp
= (struct isakmp
*)vbuf
->v
;
2826 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2827 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2828 isakmp
->np
= nptype
;
2829 isakmp
->v
= iph1
->version
;
2830 isakmp
->etype
= etype
;
2831 isakmp
->flags
= flags
;
2832 isakmp
->msgid
= msgid
;
2833 isakmp
->len
= htonl(vbuf
->l
);
2835 return vbuf
->v
+ sizeof(*isakmp
);
2839 * set values into allocated buffer of isakmp header for phase 1
2842 set_isakmp_header1(vbuf
, iph1
, nptype
)
2844 phase1_handle_t
*iph1
;
2847 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
2851 * set values into allocated buffer of isakmp header for phase 2
2854 set_isakmp_header2(vbuf
, iph2
, nptype
)
2856 phase2_handle_t
*iph2
;
2859 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
2863 * set values into allocated buffer of isakmp payload.
2866 set_isakmp_payload(buf
, src
, nptype
)
2871 struct isakmp_gen
*gen
;
2874 plog(ASL_LEVEL_DEBUG
, "add payload of len %zu, next type %d\n",
2877 gen
= (struct isakmp_gen
*)p
;
2879 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2881 memcpy(p
, src
->v
, src
->l
);
2888 #ifdef HAVE_PRINT_ISAKMP_C
2889 /* for print-isakmp.c */
2891 extern void isakmp_print(const u_char
*, u_int
, const u_char
*);
2893 char *getname(const u_char
*);
2895 char *getname6(const u_char
*);
2897 int safeputchar(int);
2900 * Return a name for the IP address pointed to by ap. This address
2901 * is assumed to be in network byte order.
2907 struct sockaddr_in addr
;
2908 static char ntop_buf
[NI_MAXHOST
];
2910 memset(&addr
, 0, sizeof(addr
));
2911 addr
.sin_len
= sizeof(struct sockaddr_in
);
2912 addr
.sin_family
= AF_INET
;
2913 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2914 if (getnameinfo(&addr
, sizeof(addr
),
2915 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2916 NI_NUMERICHOST
| niflags
))
2917 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2924 * Return a name for the IP6 address pointed to by ap. This address
2925 * is assumed to be in network byte order.
2931 struct sockaddr_in6 addr
;
2932 static char ntop_buf
[NI_MAXHOST
];
2934 memset(&addr
, 0, sizeof(addr
));
2935 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2936 addr
.sin6_family
= AF_INET6
;
2937 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2938 if (getnameinfo(&addr
, addr
.sin6_len
,
2939 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2940 NI_NUMERICHOST
| niflags
))
2941 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
2953 ch
= (unsigned char)(c
& 0xff);
2954 if (c
< 0x80 && isprint(c
))
2955 return printf("%c", c
& 0xff);
2957 return printf("\\%03o", c
& 0xff);
2961 isakmp_printpacket(msg
, from
, my
, decoded
)
2963 struct sockaddr_storage
*from
;
2964 struct sockaddr_storage
*my
;
2970 char hostbuf
[NI_MAXHOST
];
2971 char portbuf
[NI_MAXSERV
];
2972 struct isakmp
*isakmp
;
2976 if (loglevel
< ASL_LEVEL_DEBUG
)
2980 plog(ASL_LEVEL_DEBUG
, "begin.\n");
2982 gettimeofday(&tv
, NULL
);
2983 s
= tv
.tv_sec
% 3600;
2984 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2987 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
2988 portbuf
, sizeof(portbuf
),
2989 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2990 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
2991 strlcpy(portbuf
, "?", sizeof(portbuf
));
2993 printf("%s:%s", hostbuf
, portbuf
);
2998 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
2999 portbuf
, sizeof(portbuf
),
3000 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
3001 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
3002 strlcpy(portbuf
, "?", sizeof(portbuf
));
3004 printf("%s:%s", hostbuf
, portbuf
);
3011 printf("(malloc fail)\n");
3015 isakmp
= (struct isakmp
*)buf
->v
;
3016 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
3019 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
3020 if (buf
->l
< pad
&& 2 < vflag
)
3021 printf("(wrong padding)");
3023 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
3027 snapend
= buf
->v
+ buf
->l
;
3028 isakmp_print(buf
->v
, buf
->l
, NULL
);
3036 #endif /*HAVE_PRINT_ISAKMP_C*/
3039 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
3040 phase1_handle_t
*iph1
;
3041 struct remoteconf
*rmconf
;
3042 struct sockaddr_storage
*remote
, *local
;
3044 u_short
*port
= NULL
;
3046 /* address portion must be grabbed from real remote address "remote" */
3047 iph1
->remote
= dupsaddr(remote
);
3048 if (iph1
->remote
== NULL
) {
3049 ike_session_delph1(iph1
);
3054 * if remote has no port # (in case of initiator - from ACQUIRE msg)
3055 * - if remote.conf specifies port #, use that
3056 * - if remote.conf does not, use 500
3057 * if remote has port # (in case of responder - from recvfrom(2))
3058 * respect content of "remote".
3060 switch (iph1
->remote
->ss_family
) {
3062 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3065 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3068 *port
= htons(PORT_ISAKMP
);
3072 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3075 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3078 *port
= htons(PORT_ISAKMP
);
3083 "invalid family: %d\n", iph1
->remote
->ss_family
);
3084 ike_session_delph1(iph1
);
3089 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3091 iph1
->local
= dupsaddr(local
);
3092 if (iph1
->local
== NULL
) {
3093 ike_session_delph1(iph1
);
3097 switch (iph1
->local
->ss_family
) {
3099 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3102 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3105 *port
= getmyaddrsport(iph1
->local
);
3109 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3112 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3115 *port
= getmyaddrsport(iph1
->local
);
3120 "invalid family: %d\n", iph1
->local
->ss_family
);
3121 ike_session_delph1(iph1
);
3125 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3126 plog (ASL_LEVEL_NOTICE
, "Marking ports as changed\n");
3127 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3135 log_ph1established(iph1
)
3136 const phase1_handle_t
*iph1
;
3140 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3141 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3145 plog(ASL_LEVEL_NOTICE
,
3146 "ISAKMP-SA established spi:%s\n",
3147 isakmp_pindex(&iph1
->index
, 0));
3148 plog(ASL_LEVEL_DEBUG
,
3149 "ISAKMP-SA established %s-%s spi:%s\n",
3151 isakmp_pindex(&iph1
->index
, 0));
3156 IPSECLOGASLMSG("IPSec Phase 1 established (Initiated by %s).\n",
3157 (iph1
->side
== INITIATOR
)? "me" : "peer");
3162 struct payload_list
*
3163 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3166 plist
= racoon_malloc (sizeof (struct payload_list
));
3170 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3171 plist
->next
->prev
= plist
;
3172 plist
= plist
->next
;
3176 plist
->payload
= payload
;
3177 plist
->payload_type
= payload_type
;
3183 isakmp_plist_set_all (struct payload_list
**plist
, phase1_handle_t
*iph1
)
3185 struct payload_list
*ptr
= *plist
, *first
;
3186 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3187 vchar_t
*buf
= NULL
;
3190 /* Seek to the first item. */
3191 while (ptr
->prev
) ptr
= ptr
->prev
;
3194 /* Compute the whole length. */
3196 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3200 buf
= vmalloc(tlen
);
3203 "failed to get buffer to send.\n");
3209 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3215 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3218 racoon_free (first
);
3219 /* ptr->prev = NULL; first = NULL; ... omitted. */
3234 frag_handler(iph1
, msg
, remote
, local
)
3235 phase1_handle_t
*iph1
;
3237 struct sockaddr_storage
*remote
;
3238 struct sockaddr_storage
*local
;
3242 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3243 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3245 "Packet reassembly failed\n");
3249 /* simply reply if the packet was processed. */
3250 if (ike_session_check_recvdpkt(remote
, local
, newmsg
) > 0) {
3251 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3252 saddr2str((struct sockaddr
*)remote
));
3254 plog(ASL_LEVEL_NOTICE
,
3255 "the reassembled packet is retransmitted by %s.\n",
3256 saddr2str((struct sockaddr
*)remote
));
3261 isakmp_main(newmsg
, remote
, local
);
3271 phase1_handle_t
*iph1
;
3273 vchar_t
*buf
= NULL
;
3274 struct sadb_msg
*msg
, *next
, *end
;
3276 struct sockaddr_storage
*src
, *dst
;
3277 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3279 phase2_handle_t
*iph2
;
3280 phase1_handle_t
*new_iph1
;
3282 plog(ASL_LEVEL_NOTICE
,
3283 "purging ISAKMP-SA spi=%s.\n",
3284 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3286 /* Mark as expired. */
3287 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
3289 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3292 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3293 * Keep all others phase2 SAs.
3295 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3297 plog(ASL_LEVEL_NOTICE
,
3298 "pfkey_dump_sadb returned nothing.\n");
3302 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3303 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3306 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3308 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3309 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3314 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3316 "pfkey_check (%s)\n", ipsec_strerror());
3321 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3323 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3324 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3328 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3329 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3331 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3332 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3333 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3339 * check in/outbound SAs.
3340 * Select only SAs where src == local and dst == remote (outgoing)
3341 * or src == remote and dst == local (incoming).
3343 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3344 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3349 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3350 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3352 /* Check if there is another valid ISAKMP-SA */
3353 if (new_iph1
!= NULL
) {
3356 /* No handler... still send a pfkey_delete message, but log this !*/
3357 plog(ASL_LEVEL_NOTICE
,
3358 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3359 ntohl(sa
->sadb_sa_spi
));
3363 * If we have a new ph1, do not purge IPsec-SAs binded
3364 * to a different ISAKMP-SA
3366 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3371 /* If the ph2handle is established, do not purge IPsec-SA */
3372 if (FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(iph2
->status
)) {
3374 plog(ASL_LEVEL_NOTICE
,
3375 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3376 ntohl(sa
->sadb_sa_spi
),
3377 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3385 pfkey_send_delete(lcconf
->sock_pfkey
,
3386 msg
->sadb_msg_satype
,
3388 src
, dst
, sa
->sadb_sa_spi
);
3390 /* delete a relative phase 2 handle. */
3393 ike_session_unlink_phase2(iph2
);
3396 plog(ASL_LEVEL_NOTICE
,
3397 "purged IPsec-SA spi=%u.\n",
3398 ntohl(sa
->sadb_sa_spi
));
3406 /* Mark the phase1 handler as EXPIRED */
3407 plog(ASL_LEVEL_NOTICE
,
3408 "purged ISAKMP-SA spi=%s.\n",
3409 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3411 SCHED_KILL(iph1
->sce
);
3413 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3418 phase2_handle_t
*iph2
;
3423 /* Delete the SPD entry if we generated it
3425 if (iph2
->generated_spidx
) {
3427 u_int64_t force_align
; // Wcast-align fix - force alignment
3428 struct policyindex spidx
;
3430 struct sockaddr_storage addr
;
3432 struct sockaddr_storage
*src
= iph2
->src
;
3433 struct sockaddr_storage
*dst
= iph2
->dst
;
3435 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
3437 plog(ASL_LEVEL_NOTICE
,
3438 "generated policy, deleting it.\n");
3440 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
3441 iph2
->spidx_gen
= &u
.spidx
;
3443 /* make inbound policy */
3446 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
3447 u
.spidx
.ul_proto
= 0;
3450 * Note: code from get_proposal_r
3453 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
3456 * make destination address in spidx from either ID payload
3457 * or phase 1 address into a address in spidx.
3459 if (iph2
->id
!= NULL
3460 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3461 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
3462 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3463 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3464 /* get a destination address of a policy */
3465 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
3466 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
, iph2
->version
);
3472 * get scopeid from the SA address.
3473 * note that the phase 1 source address is used as
3474 * a destination address to search for a inbound
3475 * policy entry because rcoon is responder.
3477 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
3479 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
3484 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
3485 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
3486 idi2type
= _XIDT(iph2
->id
);
3490 plog(ASL_LEVEL_NOTICE
,
3491 "Get a destination address of SP index "
3492 "from Phase 1 address "
3493 "due to no ID payloads found "
3494 "OR because ID type is not address.\n");
3497 * copy the SOURCE address of IKE into the
3498 * DESTINATION address of the key to search the
3499 * SPD because the direction of policy is inbound.
3501 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
3502 switch (u
.spidx
.dst
.ss_family
) {
3505 sizeof(struct in_addr
) << 3;
3510 sizeof(struct in6_addr
) << 3;
3519 /* make source address in spidx */
3520 if (iph2
->id_p
!= NULL
3521 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
3522 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
3523 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
3524 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3525 /* get a source address of inbound SA */
3526 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
3527 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
, iph2
->version
);
3533 * get scopeid from the SA address.
3534 * for more detail, see above of this function.
3536 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
3538 setscopeid(&u
.spidx
.src
, iph2
->dst
);
3544 /* make id[src,dst] if both ID types are IP address and same */
3545 if (_XIDT(iph2
->id_p
) == idi2type
3546 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
3548 dupsaddr(&u
.spidx
.dst
);
3550 dupsaddr(&u
.spidx
.src
);
3554 plog(ASL_LEVEL_NOTICE
,
3555 "Get a source address of SP index "
3556 "from Phase 1 address "
3557 "due to no ID payloads found "
3558 "OR because ID type is not address.\n");
3560 /* see above comment. */
3561 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
3562 switch (u
.spidx
.src
.ss_family
) {
3565 sizeof(struct in_addr
) << 3;
3570 sizeof(struct in6_addr
) << 3;
3581 plog(ASL_LEVEL_NOTICE
,
3582 "get src/dst address from ID payload "
3583 "prefixlen=%u ul_proto=%u\n",
3584 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3585 plog(ASL_LEVEL_DEBUG
,
3586 "get a src address from ID payload "
3587 "%s prefixlen=%u ul_proto=%u\n",
3588 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
3589 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
3590 plog(ASL_LEVEL_DEBUG
,
3591 "get dst address from ID payload "
3592 "%s prefixlen=%u ul_proto=%u\n",
3593 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
3594 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
3597 * convert the ul_proto if it is 0
3598 * because 0 in ID payload means a wild card.
3600 if (u
.spidx
.ul_proto
== 0)
3601 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
3605 /* End of code from get_proposal_r
3608 if (pk_sendspddelete(iph2
) < 0) {
3610 "pfkey spddelete(inbound) failed.\n");
3612 plog(ASL_LEVEL_NOTICE
,
3613 "pfkey spddelete(inbound) sent.\n");
3616 #ifdef HAVE_POLICY_FWD
3617 /* make forward policy if required */
3618 if (tunnel_mode_prop(iph2
->approval
)) {
3619 u
.spidx
.dir
= IPSEC_DIR_FWD
;
3620 if (pk_sendspddelete(iph2
) < 0) {
3622 "pfkey spddelete(forward) failed.\n");
3624 plog(ASL_LEVEL_NOTICE
,
3625 "pfkey spddelete(forward) sent.\n");
3630 /* make outbound policy */
3633 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
3635 u
.spidx
.src
= u
.spidx
.dst
;
3637 pref
= u
.spidx
.prefs
;
3638 u
.spidx
.prefs
= u
.spidx
.prefd
;
3639 u
.spidx
.prefd
= pref
;
3641 if (pk_sendspddelete(iph2
) < 0) {
3643 "pfkey spddelete(outbound) failed.\n");
3645 plog(ASL_LEVEL_NOTICE
,
3646 "pfkey spddelete(outbound) sent.\n");
3649 iph2
->spidx_gen
=NULL
;
3655 setscopeid(sp_addr0
, sa_addr0
)
3656 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
3658 struct sockaddr_in6
*sp_addr
, *sa_addr
;
3660 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
3661 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
3663 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
3664 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
3665 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
3668 /* this check should not be here ? */
3669 if (sa_addr
->sin6_family
!= AF_INET6
) {
3671 "can't get scope ID: family mismatch\n");
3675 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
3677 "scope ID is not supported except of lladdr.\n");
3681 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
3688 isakmp_plist_append_initial_contact (iph1
, plist
)
3689 phase1_handle_t
*iph1
;
3690 struct payload_list
*plist
;
3692 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !ike_session_getcontacted(iph1
->remote
)) {
3693 vchar_t
*notp_ini
= NULL
;
3694 struct isakmp_pl_n np
, *nptr
;
3697 np
.doi
= htonl(iph1
->rmconf
->doitype
);
3698 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
3699 np
.spi_size
= sizeof(isakmp_index
);
3700 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
3701 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
3702 + sizeof(isakmp_index
)))) {
3704 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
3705 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
3706 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
3707 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
3708 plog(ASL_LEVEL_NOTICE
,
3709 "added initial-contact payload.\n");
3711 /* insert a node into contacted list. */
3712 if (ike_session_inscontacted(iph1
->remote
) == -1) {
3714 "failed to add contacted list.\n");
3720 "failed to allocate notification payload.\n");
3724 plog(ASL_LEVEL_NOTICE
, "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
3725 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, ike_session_getcontacted(iph1
->remote
)? 1:0);